-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmod_form.php
470 lines (417 loc) · 18.6 KB
/
mod_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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?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/>.
/**
* The main mod_videotime configuration form.
*
* @package mod_videotime
* @copyright 2021 bdecent gmbh <https://bdecent.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_videotime\output\next_activity_button;
use mod_videotime\videotime_instance;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/course/moodleform_mod.php');
/**
* Module instance settings form.
*
* @package mod_videotime
* @copyright 2021 bdecent gmbh <https://bdecent.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_videotime_mod_form extends moodleform_mod {
/**
* Defines forms elements
*/
public function definition() {
global $CFG, $COURSE, $PAGE, $DB;
$mform = $this->_form;
if (!videotime_has_pro()) {
$mform->addElement('static', '', '', html_writer::link(
new moodle_url('https://link.bdecent.de/videotimepro1'),
html_writer::img(
'https://link.bdecent.de/videotimepro1/image.jpg',
'',
['width' => '100%', 'class' => 'img-responsive', 'style' => 'max-width:700px']
)
));
} else {
$PAGE->requires->js_call_amd(
'mod_videotime/mod_form',
'init'
);
}
// Adding the "general" fieldset, where all the common settings are showed.
$mform->addElement('header', 'general', get_string('general', 'form'));
if (videotime_has_pro() && videotime_has_repository()) {
$needssetup = false;
try {
$api = new \videotimeplugin_repository\api();
} catch (\videotimeplugin_repository\exception\api_not_configured $e) {
$needssetup = true;
} catch (\videotimeplugin_repository\exception\api_not_authenticated $e) {
$needssetup = true;
}
$group = [];
$group[] = $mform->createElement('text', 'vimeo_url', get_string('vimeo_url', 'videotime'));
if (!$needssetup) {
$group[] = $mform->createElement('button', 'pull_from_vimeo', get_string('pull_from_vimeo', 'videotime'));
}
$mform->addGroup($group, 'vimeo_url_group', get_string('vimeo_url', 'videotime'), null, false);
$mform->addHelpButton('vimeo_url_group', 'vimeo_url', 'videotime');
$mform->setType('vimeo_url_group', PARAM_RAW);
$group = [];
if (!$needssetup) {
$group[] = $mform->createElement('static', 'choose_video_label', '', '- or -');
$group[] = $mform->createElement('button', 'choose_video', get_string('choose_video', 'videotime'));
} else if (is_siteadmin()) {
$group[] = $mform->createElement('static', 'choose_video_label', '', '- or -');
$group[] = $mform->createElement(
'html',
html_writer::link(
new moodle_url('/mod/videotime/plugin/repository/overview.php'),
get_string('setup_repository', 'videotime')
)
);
}
$mform->addGroup($group);
$PAGE->requires->js_call_amd(
'videotimeplugin_repository/mod_form',
'init',
[videotime_is_totara(), $this->context->id]
);
} else {
$mform->addElement('text', 'vimeo_url', get_string('vimeo_url', 'videotime'), ['size' => 100]);
$mform->addHelpButton('vimeo_url', 'vimeo_url', 'videotime');
}
$mform->setType('vimeo_url', PARAM_URL);
// Adding the standard "name" field.
$mform->addElement('text', 'name', get_string('activity_name', 'mod_videotime'), ['size' => '64']);
if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT);
} else {
$mform->setType('name', PARAM_CLEANHTML);
}
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('name', 'activity_name', 'mod_videotime');
// Adding the standard "intro" and "introformat" fields.
if ($CFG->branch >= 29) {
$this->standard_intro_elements();
} else {
$this->add_intro_editor();
}
$mform->addElement(
'advcheckbox',
'show_description_in_player',
'',
get_string('show_description_in_player', 'videotime')
);
$mform->setDefault('show_description_in_player', get_config('videotime', 'show_description_in_player'));
// Video Time video description.
$mform->addElement(
'editor',
'video_description',
get_string('video_description', 'videotime'),
['rows' => 10],
[
'maxfiles' => EDITOR_UNLIMITED_FILES,
'noclean' => true,
'context' => $this->context,
'subdirs' => true,
],
);
$mform->setType('video_description', PARAM_RAW); // No XSS prevention here, users must be trusted.
$mform->addHelpButton('video_description', 'video_description', 'videotime');
// Add section tab plugins.
$mform->addElement('header', 'tabs', get_string('tabs', 'videotime'));
$mform->addElement('advcheckbox', 'enabletabs', get_string('enabletabs', 'videotime'));
$mform->setType('enabletabs', PARAM_BOOL);
$mform->setDefault('enabletabs', get_config('videotime', 'enabletabs'));
foreach (array_keys(core_component::get_plugin_list('videotimetab')) as $name) {
if (!empty(get_config('videotimetab_' . $name, 'enabled'))) {
$classname = "\\videotimetab_$name\\tab";
$classname::add_form_fields($mform);
}
}
// Add standard elements.
$this->standard_coursemodule_elements();
// Add fields from extensions.
foreach (array_keys(core_component::get_plugin_list('videotimeplugin')) as $name) {
component_callback("videotimeplugin_$name", 'add_form_fields', [$mform, get_class($this)]);
}
// Add standard buttons.
$this->add_action_buttons();
if (!videotime_has_pro()) {
$mform->addElement('static', '', '', html_writer::link(
new moodle_url('https://link.bdecent.de/videotimepro2'),
html_writer::img(
'https://link.bdecent.de/videotimepro2/image.jpg',
'',
['width' => '100%', 'class' => 'img-responsive', 'style' => 'max-width:700px']
)
));
}
}
/**
* Add an editor for an activity's introduction field.
*
* NOTE: Copied from parent classes to change showdescription string.
*
* @param null $customlabel Override default label for editor
* @throws coding_exception
*/
protected function standard_intro_elements($customlabel = null) {
global $CFG;
$required = $CFG->requiremodintro;
$mform = $this->_form;
$label = is_null($customlabel) ? get_string('moduleintro') : $customlabel;
$mform->addElement('editor', 'introeditor', $label, ['rows' => 10], [
'maxfiles' => EDITOR_UNLIMITED_FILES,
'noclean' => true,
'context' => $this->context,
'subdirs' => true,
]);
$mform->setType('introeditor', PARAM_RAW); // No XSS prevention here, users must be trusted.
if ($required) {
$mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
}
// If the 'show description' feature is enabled, this checkbox appears below the intro.
// We want to hide that when using the singleactivity course format because it is confusing.
if ($this->_features->showdescription && $this->courseformat->has_view_page()) {
$mform->addElement('advcheckbox', 'showdescription', get_string('showdescription'));
$mform->addHelpButton('showdescription', 'showdescription', 'videotime');
}
}
/**
* Add custom completion rules.
*
* @return array Array of string IDs of added items, empty array if none
* @throws coding_exception
*/
public function add_completion_rules() {
$mform =& $this->_form;
if (videotime_has_pro()) {
// Completion on view and seconds.
$group = [];
$group[] =& $mform->createElement(
'advcheckbox',
$this->get_suffixed_name('completion_on_view_time'),
'',
get_string('completion_on_view', 'videotime') . ': '
);
$group[] =& $mform->createElement(
'text',
$this->get_suffixed_name('completion_on_view_time_second'),
'',
['size' => 3]
);
$group[] =& $mform->createElement('static', 'seconds', '', get_string('seconds', 'videotime'));
$mform->setType($this->get_suffixed_name('completion_on_view_time_second'), PARAM_INT);
$mform->addGroup($group, $this->get_suffixed_name('completion_on_view_time_group'), '', [' '], false);
$mform->disabledIf(
$this->get_suffixed_name('completion_on_view_time_second'),
$this->get_suffixed_name('completion_on_view_time'),
'notchecked'
);
$group = [];
$group[] =& $mform->createElement(
'advcheckbox',
$this->get_suffixed_name('completion_on_percent'),
'',
get_string('completion_on_percent', 'videotime') . ': '
);
$group[] =& $mform->createElement('text', $this->get_suffixed_name('completion_on_percent_value'), '', ['size' => 3]);
$group[] =& $mform->createElement('static', 'percent_label', '', '%');
$mform->setType($this->get_suffixed_name('completion_on_percent_value'), PARAM_INT);
$mform->addGroup($group, $this->get_suffixed_name('completion_on_percent_group'), '', [' '], false);
$mform->disabledIf(
$this->get_suffixed_name('completion_on_percent_value'),
$this->get_suffixed_name('completion_on_percent'),
'notchecked'
);
$mform->addElement(
'advcheckbox',
$this->get_suffixed_name('completion_on_finish'),
'',
get_string('completion_on_finish', 'videotime')
);
$mform->setType($this->get_suffixed_name('completion_on_finish'), PARAM_BOOL);
$mform->addElement(
'advcheckbox',
$this->get_suffixed_name('completion_hide_detail'),
'',
get_string('completion_hide_detail', 'videotime')
);
$mform->setType($this->get_suffixed_name('completion_hide_detail'), PARAM_BOOL);
return [
$this->get_suffixed_name('completion_on_finish'),
$this->get_suffixed_name('completion_hide_detail'),
$this->get_suffixed_name('completion_on_view_time_group'),
$this->get_suffixed_name('completion_on_percent_group'),
];
}
return [];
}
/**
* Called during validation to see whether some module-specific completion rules are selected.
*
* @param array $data Input data not yet validated.
* @return bool True if one or more rules is enabled, false if none are.
*/
public function completion_rule_enabled($data) {
return (
(!empty($data[$this->get_suffixed_name('completion_on_view_time')]) &&
$data[$this->get_suffixed_name('completion_on_view_time_second')] != 0)) ||
!empty($data[$this->get_suffixed_name('completion_on_finish')] ||
(
!empty($data[$this->get_suffixed_name('completion_on_percent')])
&& $data[$this->get_suffixed_name('completion_on_percent_value')])
);
}
/**
* Validates the form input
*
* @param array $data submitted data
* @param array $files submitted files
* @return array eventual errors indexed by the field name
*/
public function validation($data, $files) {
global $USER;
$errors = parent::validation($data, $files);
if (!isset($data['vimeo_url']) || empty($data['vimeo_url'])) {
$fs = get_file_storage();
if (
empty($data['livefeed']) && (
empty($data['mediafile'])
|| !$files = $fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $data['mediafile'])
)
) {
$errors['vimeo_url'] = get_string('required');
}
} else if (!filter_var($data['vimeo_url'], FILTER_VALIDATE_URL)) {
$errors['vimeo_url'] = get_string('vimeo_url_invalid', 'videotime');
}
// Make sure seconds are set if completion on view time is enabled.
if (
isset($data[$this->get_suffixed_name('completion_on_view_time')])
&& $data[$this->get_suffixed_name('completion_on_view_time')]
) {
if (
isset($data[$this->get_suffixed_name('completion_on_view_time_second')])
&& !$data[$this->get_suffixed_name('completion_on_view_time_second')]
) {
$errors[$this->get_suffixed_name('completion_on_view_time_second')] = get_string('required');
}
}
// Make sure percent value is set if completion on percent is enabled.
if (
isset($data[$this->get_suffixed_name('completion_on_percent')])
&& $data[$this->get_suffixed_name('completion_on_percent')]
) {
if (
isset($data[$this->get_suffixed_name('completion_on_percent_value')]) &&
!$data[$this->get_suffixed_name('completion_on_percent_value')]
) {
$errors[$this->get_suffixed_name('completion_on_percent_value')] = get_string('required');
}
}
foreach (array_keys(core_component::get_plugin_list('videotimeplugin')) as $name) {
$errors += component_callback("videotimeplugin_$name", 'validation', [$data, $files], []);
}
return $errors;
}
/**
* Prepares the form before data are set
*
* @param array $defaultvalues
*/
public function data_preprocessing(&$defaultvalues) {
parent::data_preprocessing($defaultvalues);
// Editing existing instance.
if ($this->current->instance) {
$draftitemid = file_get_submitted_draft_itemid('video_description');
$videodescription = $defaultvalues['video_description'];
$videodescriptionformat = $defaultvalues['video_description_format'];
$defaultvalues['video_description'] = [];
$defaultvalues['video_description']['format'] = $videodescriptionformat;
$defaultvalues['video_description']['text'] = file_prepare_draft_area(
$draftitemid,
$this->context->id,
'mod_videotime',
'video_description',
0,
[],
$videodescription
);
$defaultvalues['video_description']['itemid'] = $draftitemid;
foreach (array_keys(core_component::get_plugin_list('videotimeplugin')) as $name) {
component_callback("videotimeplugin_$name", 'data_preprocessing', [&$defaultvalues, $this->current->instance]);
}
foreach (array_keys(core_component::get_plugin_list('videotimetab')) as $name) {
$classname = "\\videotimetab_$name\\tab";
$classname::data_preprocessing($defaultvalues, $this->current->instance);
}
}
}
/**
* Completion suffix
*/
public function get_suffix(): string {
if (method_exists(parent::class, 'get_suffix')) {
return parent::get_suffix();
}
return '';
}
/**
* Completion condition with suffix
*
* @param string $name Name without suffix
* @return string
*/
public function get_suffixed_name($name): string {
return $name . $this->get_suffix();
}
/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data the form data to be modified.
*/
public function data_postprocessing($data) {
parent::data_postprocessing($data);
// Turn off completion settings if the checkboxes aren't ticked.
$suffix = $this->get_suffix();
if (!empty($data->completionunlocked)) {
$suffix = $this->get_suffix();
$completion = $data->{'completion' . $suffix};
$autocompletion = !empty($completion) && $completion == COMPLETION_TRACKING_AUTOMATIC;
if (empty($data->{'completion_on_finish' . $suffix}) || !$autocompletion) {
$data->{'completion_on_finish' . $suffix} = 0;
}
if (empty($data->{'completion_on_percent_value' . $suffix}) || !$autocompletion) {
$data->{'completion_on_percent' . $suffix} = 0;
}
if (empty($data->{'completion_on_view_time_second' . $suffix}) || !$autocompletion) {
$data->{'completion_on_view_time' . $suffix} = 0;
}
if (empty($data->{'completion_hide_detail' . $suffix}) || !$autocompletion) {
$data->{'completion_hide_detail' . $suffix} = 0;
}
}
}
}