-
Notifications
You must be signed in to change notification settings - Fork 2
/
edit_message_form.php
278 lines (242 loc) · 12.4 KB
/
edit_message_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?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/>.
/**
* Message edit form (with edit.php)
*
* @package blocks
* @subpackage news
* @copyright 2011 The Open University
* @author Stacey Walker <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use block_news\system;
use block_news\message;
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.');
// Must be included from a Moodle page.
}
require_once($CFG->libdir.'/formslib.php');
/**
* message edit form definition
* @package blocks
* @subpackage news
*/
class block_news_edit_message_form extends moodleform {
/**
* Maximum width/height of thumbnail images.
* @var int
*/
const THUMBNAIL_MAX_EDGE = 340;
/** @var array File types allowed for message images */
const IMAGE_FILE_TYPES = array('.jpg', '.png');
/** @var array Restrictions for message image file manager */
const IMAGE_FILE_OPTIONS = array('subdirs' => 0, 'maxfiles' => 1, 'accepted_types' => self::IMAGE_FILE_TYPES);
/** @var int Exact width for message images */
const IMAGE_WIDTH = 700;
/** @var int Exact height for message images */
const IMAGE_HEIGHT = 330;
/** @var int Max filesize (in bytes) for message images (currently 100KB) */
const IMAGE_MAX_FILESIZE = 102400;
protected $displaytype = 0;
protected $publishstate = '';
protected $groupingsupportbygroup = 0;
/**
* Overide constructor to pass in publish radio button state before
* definition() is called
*
* @param array $customdata
*/
public function __construct($customdata) {
$this->displaytype = $customdata['displaytype'];
$this->publishstate = $customdata['publishstate'];
$this->groupingsupportbygroup = $customdata['groupingsupportbygroup'];
parent::__construct();
}
public function definition() {
global $COURSE, $PAGE;
$mform =& $this->_form;
// Hiddens.
$mform->addElement('hidden', 'bi');
$mform->setType('bi', PARAM_INT);
$mform->addElement('hidden', 'm');
$mform->setType('m', PARAM_INT);
$mform->addElement('hidden', 'newsfeedid');
$mform->setType('newsfeedid', PARAM_INT);
$mform->addElement('hidden', 'mode');
$mform->setType('mode', PARAM_RAW);
if ( $this->displaytype == system::DISPLAY_SEPARATE_INTO_EVENT_AND_NEWSITEMS ) {
$messagetype = array(message::MESSAGETYPE_NEWS => get_string('newsitem', 'block_news'),
message::MESSAGETYPE_EVENT => get_string('event', 'block_news'));
$mform->addElement('select', 'messagetype', get_string('messagetype', 'block_news'), $messagetype);
$mform->setDefault('messagetype', message::MESSAGETYPE_NEWS);
}
$mform->addElement('text', 'title', get_string('msgedittitle', 'block_news'),
array('size' => '40'));
$mform->setType('title', PARAM_TEXT);
$mform->addRule('title', null, 'required', null, 'client');
$mform->addRule('title', null, 'maxlength', 80, 'server');
$mform->addElement('editor', 'message', get_string('msgeditmessage', 'block_news'),
array('cols' => 50, 'rows' => 30), array('maxfiles' => EDITOR_UNLIMITED_FILES));
$mform->addRule('message', null, 'required', null, 'client');
if ( $this->displaytype == system::DISPLAY_SEPARATE_INTO_EVENT_AND_NEWSITEMS ) {
$mform->addElement('date_time_selector', 'eventstart',
get_string('msgediteventstart', 'block_news'), array('optional' => false));
$mform->disabledIf('eventstart', 'messagetype', 'neq', message::MESSAGETYPE_EVENT);
$mform->disabledIf('eventstart[hour]', 'alldayevent', 'checked');
$mform->disabledIf('eventstart[minute]', 'alldayevent', 'checked');
$mform->addElement('advcheckbox', 'alldayevent', get_string('msgeditalldayevent', 'block_news'));
$mform->setDefault('alldayevent', 1);
$mform->disabledIf('alldayevent', 'messagetype', 'neq', message::MESSAGETYPE_EVENT);
$mform->addElement('date_time_selector', 'eventend',
get_string('msgediteventend', 'block_news'), array('optional' => false));
$mform->disabledIf('eventend', 'messagetype', 'neq', message::MESSAGETYPE_EVENT);
$mform->disabledIf('eventend', 'alldayevent', 'checked');
$mform->addElement('text', 'eventlocation', get_string('msgediteventlocation', 'block_news'));
$mform->disabledIf('eventlocation', 'messagetype', 'neq', message::MESSAGETYPE_EVENT);
$mform->setType('eventlocation', PARAM_TEXT);
}
$mform->addElement('filemanager', 'attachments',
get_string('msgedithlpattach', 'block_news'), null, array('subdirs' => 0));
$mform->addElement('filemanager', 'messageimage', get_string('messageimage', 'block_news'),
null, self::IMAGE_FILE_OPTIONS);
$mform->disabledIf('messageimage', 'messagetype', 'eq', message::MESSAGETYPE_EVENT);
$mform->addElement('text', 'imagedesc', get_string('imagedesc', 'block_news'),
array('size' => '40'));
$mform->setType('imagedesc', PARAM_TEXT);
$mform->disabledIf('imagedesc', 'messagetype', 'eq', message::MESSAGETYPE_EVENT);
$mform->addElement('checkbox', 'imagedescnotnecessary',
get_string('imagedescnotnecessary', 'block_news'));
$mform->setDefault('imagedescnotnecessary', 0);
$mform->disabledIf('imagedescnotnecessary', 'messagetype',
'eq', message::MESSAGETYPE_EVENT);
$mform->addElement('selectyesno', 'messagevisible',
get_string('msgeditvisible', 'block_news'));
$mform->setDefault('messagevisible', 1);
// If config_groupingsupport is group.
if ($this->groupingsupportbygroup) {
$groupingsdata = groups_get_all_groupings($COURSE->id);
$groupings = [];
$groupinggroups = [];
foreach ($groupingsdata as $grouping) {
$groupsdata = groups_get_all_groups($COURSE->id, 0, $grouping->id);
// Simplify grouping groups data array, for JS functionality on the DOM element.
$groupinggroups[$grouping->id] = array_keys($groupsdata);
$groupings[$grouping->id] = $grouping->name;
}
$groups = groups_get_all_groups($COURSE->id);
$groupoptions = [];
if (!empty($groups)) {
$groupoptions[0] = get_string('allparticipants');
foreach ($groups as $group) {
$groupoptions[$group->id] = $group->name;
}
$mform->addElement('select', 'groupids',
get_string('msgeditgroup', 'block_news'), $groupoptions,
['multiple' => true]);
$mform->setDefault('groupid', 0);
if (!empty($groupings)) {
$groupings[0] = '';
ksort($groupings);
// Use a data attribute to add grouping groups array to the DOM.
$strparams = json_encode($groupinggroups);
$mform->addElement('select', 'grouping',
get_string('msgeditselectgrouping', 'block_news'),
$groupings, ['data-groupinggroups' => $strparams]);
$PAGE->requires->js_call_amd('block_news/groupings', 'init');
}
}
}
// Publish radio buttons - content determined by value passed in constructor.
$attributes = array('class' => 'publish_radioopt');
$radioarray = array();
if ($this->publishstate == "ap") { // Already publsihed.
// Leave out 'immediately'.
$radioarray[] = $mform->createElement('radio', 'publish', '',
get_string('msgeditatspecdate', 'block_news'), 1, $attributes);
$radioarray[] = $mform->createElement('radio', 'publish', '',
get_string('msgeditalreadypub', 'block_news'), 2, $attributes);
} else {
$radioarray[] = $mform->createElement('radio', 'publish', '',
get_string('msgeditimmediately', 'block_news'), 0, $attributes);
$radioarray[] = $mform->createElement('radio', 'publish', '',
get_string('msgeditatspecdate', 'block_news'), 1, $attributes);
// Leave out 'already published'.
}
$mform->addGroup($radioarray, 'radioar',
get_string('msgeditpublish', 'block_news'),
array(' '), false);
// Add date_time selector in optional area.
$mform->addElement('date_time_selector', 'messagedate',
get_string('msgeditmessagedate', 'block_news'), array('optional' => false));
$mform->disabledIf('messagedate', 'publish', 'neq', 1);
$mform->setAdvanced('optional');
$mform->addElement('checkbox', 'messagerepeat',
get_string('msgeditrepeat', 'block_news'));
$mform->disabledIf('messagerepeat', 'publish', 'neq', 1);
$mform->setDefault('messagerepeat', 0);
$mform->addElement('selectyesno', 'hideauthor',
get_string('msgedithideauthor', 'block_news'));
$mform->setDefault('hideauthor', (int) get_config('block_news', 'block_news_hideauthor'));
$mform->disabledIf('hideauthor', 'messagetype', 'eq', message::MESSAGETYPE_EVENT);
$mform->addElement('static', 'lastupdated',
get_string('msgeditlastupdated', 'block_news'));
$this->add_action_buttons();
}
/**
* Ensure that if an event is added with an end date, the end is after the start.
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if ($this->displaytype == system::DISPLAY_SEPARATE_INTO_EVENT_AND_NEWSITEMS
&& $data['messagetype'] == message::MESSAGETYPE_EVENT) {
if ($data['eventstart'] < time()) {
$errors['eventstart'] = get_string('erroreventstart', 'block_news');
}
if (!$data['alldayevent'] && $data['eventend'] < $data['eventstart']) {
$errors['eventend'] = get_string('erroreventend', 'block_news');
}
}
if (!empty($data['messageimage'])) {
$imageerrors = '';
$draftfiles = file_get_drafarea_files($data['messageimage']);
if ($image = reset($draftfiles->list)) { // There's only 1 file allowed, so this will give us the image.
if ($image->size > self::IMAGE_MAX_FILESIZE) {
$imageerrors .= get_string('errorimagesize', 'block_news', self::IMAGE_MAX_FILESIZE / 1024);
}
if ($image->image_width != self::IMAGE_WIDTH || $image->image_height != self::IMAGE_HEIGHT) {
$errorparts = (object) ['width' => self::IMAGE_WIDTH, 'height' => self::IMAGE_HEIGHT];
$imageerrors .= get_string('errorimagedimensions', 'block_news', $errorparts);
}
if (!empty($imageerrors)) {
$errors['messageimage'] = $imageerrors;
}
if (empty($data['imagedesc']) && (!isset($data['imagedescnotnecessary']) or $data['imagedescnotnecessary'] == 0)) {
$errors['imagedesc'] = get_string('errorimagedesc', 'block_news');
}
}
}
if (!empty($data['groupids'])) {
if (in_array('0', $data['groupids']) && count($data['groupids']) > 1) {
$errors['groupids'] = get_string('errorinvalidgroups', 'block_news');
}
}
return $errors;
}
}