forked from open-lms-open-source/moodle-mod_lightboxgallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageadd_form.php
98 lines (76 loc) · 3.71 KB
/
imageadd_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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Prints a particular instance of lightboxgallery
*
* @package mod_lightboxgallery
* @author Adam Olley <[email protected]>
* @copyright 2012 NetSpot Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__).'/locallib.php');
require_once($CFG->libdir.'/formslib.php');
require_once(dirname(__FILE__).'/imageclass.php');
class mod_lightboxgallery_imageadd_form extends moodleform {
public function definition() {
global $COURSE, $cm;
$mform =& $this->_form;
$gallery = $this->_customdata;
$handlecollisions = !get_config('lightboxgallery', 'overwritefiles');
$mform->addElement('header', 'general', get_string('addimage', 'lightboxgallery'));
$mform->addElement('filepicker', 'image', get_string('file'), '0',
array('maxbytes' => $COURSE->maxbytes, 'accepted_types' => array('web_image', 'archive')));
$mform->addRule('image', get_string('required'), 'required', null, 'client');
$mform->addHelpButton('image', 'addimage', 'lightboxgallery');
if ($this->can_resize()) {
$resizegroup = array();
$resizegroup[] = &$mform->createElement('select', 'resize', get_string('edit_resize', 'lightboxgallery'),
lightboxgallery_resize_options());
$resizegroup[] = &$mform->createElement('checkbox', 'resizedisabled', null, get_string('disable'));
$mform->setType('resize', PARAM_INT);
$mform->addGroup($resizegroup, 'resizegroup', get_string('edit_resize', 'lightboxgallery'), ' ', false);
$mform->setDefault('resizedisabled', 1);
$mform->disabledIf('resizegroup', 'resizedisabled', 'checked');
$mform->setAdvanced('resizegroup');
}
$mform->addElement('hidden', 'id', $cm->id);
$this->add_action_buttons(true, get_string('addimage', 'lightboxgallery'));
}
public function validation($data, $files) {
global $USER;
if ($errors = parent::validation($data, $files)) {
return $errors;
}
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
$fs = get_file_storage();
if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['image'], 'id', false)) {
$errors['image'] = get_string('required');
return $errors;
} else {
$file = reset($files);
if ($file->get_mimetype() != 'application/zip' && !$file->is_valid_image()) {
$errors['image'] = get_string('invalidfiletype', 'error', $file->get_filename());
// Better delete current file, it is not usable anyway.
$fs->delete_area_files($usercontext->id, 'user', 'draft', $data['image']);
}
}
return $errors;
}
private function can_resize() {
global $gallery;
return !in_array($gallery->autoresize, array(AUTO_RESIZE_UPLOAD, AUTO_RESIZE_BOTH));
}
}