This repository has been archived by the owner on Jul 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_edit_form.php
116 lines (95 loc) · 5.47 KB
/
image_edit_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->libdir.'/formslib.php');
class mod_gallery_image_edit_form extends moodleform {
protected function definition() {
$mform = $this->_form;
$action = $this->_customdata['action'];
$gallery = $this->_customdata['gallery'];
$data = array();
foreach($this->_customdata['images'] as $image) {
if($action == 'batchedit') {
$mform->addElement ('hidden','mod-gallery-batch-'.$image->id(),'1');
$mform->setType('mod-gallery-batch-'.$image->id(),PARAM_BOOL);
}
$uniqueId = '';
$imagePreview = '';
if($action == 'addimagedesc') {
$uniqueId = clean_param($image->stored_file()->get_filename(), PARAM_ALPHANUM);
$imagePreview = moodle_url::make_pluginfile_url($image->stored_file()->get_contextid(), $image->stored_file()->get_component(),
$image->stored_file()->get_filearea(), $image->stored_file()->get_itemid(),
$image->stored_file()->get_filepath(), $image->stored_file()->get_filename());
} else {
$uniqueId = $image->id();
$imagePreview = $image->thumbnail();
}
$mform->addElement('header','header-'.$uniqueId,$image->data()->name);
$mform->setExpanded('header-'.$uniqueId);
$mform->addElement('text','name-'.$uniqueId, get_string('imagename','gallery'),array('size'=>'40'));
$mform->setType('name-'.$uniqueId, PARAM_TEXT);
$mform->addElement('editor', 'desc-'.$uniqueId,'<img src="'.$imagePreview.'" style="max-width:136px; max-height:150px;" />',
array('rows' => 3), array('collapsed' => true));
$mform->setType('desc-'.$uniqueId, PARAM_RAW);
if($gallery->imageattachments())
$mform->addElement('filemanager', 'attachments-'.$uniqueId, get_string('attachments', 'gallery'), null, array('subdirs' => 0));
$mform->addElement('checkbox', 'sourcetype-'.$uniqueId, get_string('sourceown','gallery'));
$mform->setType('sourcetype-'.$uniqueId, PARAM_BOOL);
$mform->addElement('text','source-'.$uniqueId, get_string('source','gallery'), array('size'=>'70'));
$mform->setType('source-'.$uniqueId,PARAM_TEXT);
$mform->disabledIf('source-'.$uniqueId, 'sourcetype-'.$uniqueId, 'checked');
$data['name-'.$uniqueId] = $image->data()->name;
$data['desc-'.$uniqueId]['text'] = $image->data()->description;
$data['desc-'.$uniqueId]['format'] = $image->data()->descriptionformat;
if($image->data()->sourcetype == GALLERY_IMAGE_SOURCE_TEXT) {
$data['source-'.$uniqueId] = $image->data()->sourcetext;
$data['sourcetype-'.$uniqueId] = false;
} elseif($image->data()->sourcetype == GALLERY_IMAGE_SOURCE_OWN) {
$data['source-'.$uniqueId] = '';
$data['sourcetype-'.$uniqueId] = true;
}
if($action != 'addimagedesc' && $gallery->imageattachments()) {
$draftitemid = file_get_submitted_draft_itemid('attachments-'.$uniqueId);
file_prepare_draft_area($draftitemid, $this->_customdata['contextid'], 'mod_gallery', GALLERY_IMAGE_ATTACHMENTS_FILEAREA, $image->id(),array('subdirs' => 0));
$data['attachments-'.$uniqueId] = $draftitemid;
}
}
$mform->addElement('hidden','gaction',$action);
if($action == 'editimage' || $action == 'editimageg') {
$mform->addElement('hidden','image',$this->_customdata['image']);
$mform->setType('image',PARAM_INT);
}
$mform->setType('gaction', PARAM_ALPHA);
$mform->addElement('hidden','id',$this->_customdata['id']);
$mform->setType('id',PARAM_INT);
if($action == 'editimage' || $action == 'editimageg' || $action == 'batchedit') {
$this->add_action_buttons(true, get_string('savechanges','gallery'));
} else {
$this->add_action_buttons(true, get_string('saveimages','gallery'));
}
$this->set_data($data);
}
/**
* Perform minimal validation on the grade form
* @param array $data
* @param array $files
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
$action = $this->_customdata['action'];
foreach($this->_customdata['images'] as $image) {
$uniqueId = '';
if($action == 'addimagedesc')
$uniqueId = clean_param($image->stored_file()->get_filename(), PARAM_ALPHANUM);
else
$uniqueId = $image->id();
$sourceTypeName = 'sourcetype-'.$uniqueId;
$sourceName = 'source-'.$uniqueId;
$source = '';
if(isset($data[$sourceName]))
$source = trim($data[$sourceName]);
if(!isset($data[$sourceTypeName]) && empty($source))
$errors[$sourceName] = get_string('missingsourceerror','gallery');
}
return $errors;
}
}