-
Notifications
You must be signed in to change notification settings - Fork 6
/
mod_form.php
318 lines (278 loc) · 11.4 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
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
use mod_learningmap\completion\custom_completion;
use mod_learningmap\mapworker;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/course/moodleform_mod.php');
require_once($CFG->dirroot . '/mod/learningmap/lib.php');
/**
* Editing form for mod_learningmap
*
* @package mod_learningmap
* @copyright 2021-2024, ISB Bayern
* @author Stefan Hanauska <stefan.hanauska@csg-in.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_learningmap_mod_form extends moodleform_mod {
/**
* Defines the editing form for mod_learningmap
*
* @return void
*/
public function definition(): void {
global $PAGE, $OUTPUT;
$mform = &$this->_form;
$cm = get_fast_modinfo($this->current->course);
$s = [];
$activitysel = [];
// Gets only sections with content.
foreach ($cm->get_sections() as $sectionnum => $section) {
$sectioninfo = $cm->get_section_info($sectionnum);
$s['name'] = $sectioninfo->name;
if (empty($s['name'])) {
$s['name'] = get_string('section') . ' ' . $sectionnum;
}
$s['coursemodules'] = [];
foreach ($section as $cmid) {
$module = $cm->get_cm($cmid);
// Get only course modules which are not deleted.
if ($module->deletioninprogress == 0) {
$s['coursemodules'][] = [
'id' => $cmid,
'name' => s($module->name),
'completionenabled' => $module->completion > 0,
'hidden' => $module->visible == 0,
];
}
}
$activitysel[] = $s;
}
$mform->addElement('header', 'general', get_string('general', 'form'));
$mform->addElement('text', 'name', get_string('name', 'learningmap'), ['size' => '64']);
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', null, 'required', null, 'client');
$mform->addHelpButton('name', 'name', 'learningmap');
$this->standard_intro_elements();
$mform->addElement(
'html',
$OUTPUT->render_from_template(
'mod_learningmap/inlinehelp',
['usecaselink' => get_config('mod_learningmap', 'usecaselink')]
)
);
$features = [];
foreach (LEARNINGMAP_FEATURES as $feature) {
$features[] = [
'name' => $feature,
'title' => get_string($feature, 'learningmap'),
'text' => get_string($feature . '_help', 'learningmap'),
'alt' => get_string('help'),
];
}
$mform->addElement(
'html',
$OUTPUT->render_from_template(
'mod_learningmap/formitem',
['sections' => $activitysel,
'help' => $OUTPUT->help_icon('intro', 'learningmap', ''),
'completiondisabled' => $cm->get_course()->enablecompletion == 0,
'features' => $features,
]
)
);
$mform->addElement('advcheckbox', 'showmaponcoursepage', get_string('showmaponcoursepage', 'learningmap'));
$mform->setType('showmaponcoursepage', PARAM_INT);
$mform->addHelpButton('showmaponcoursepage', 'showmaponcoursepage', 'learningmap');
$backlinkallowed = get_config('mod_learningmap', 'backlinkallowed');
if ($backlinkallowed) {
$mform->addElement('advcheckbox', 'backlink', get_string('showbacklink', 'learningmap'));
$mform->setType('backlink', PARAM_INT);
$mform->addHelpButton('backlink', 'showbacklink', 'learningmap');
} else {
$mform->addElement('hidden', 'backlink', 0);
}
$mform->addElement(
'filemanager',
'backgroundfile',
get_string('backgroundfile', 'learningmap'),
null,
[
'accepted_types' => 'web_image',
'maxfiles' => 1,
'subdirs' => 0,
]
);
$mform->addRule('backgroundfile', null, 'required', null, 'client');
$mform->addHelpButton('backgroundfile', 'backgroundfile', 'learningmap');
$mform->addElement('textarea', 'svgcode', get_string('svgcode', 'learningmap'));
$mform->setType('svgcode', PARAM_RAW);
$mform->addElement('hidden', 'placestore');
$mform->setType('placestore', PARAM_RAW);
$mform->closeHeaderBefore('header');
$PAGE->requires->js_call_amd('mod_learningmap/learningmap', 'init');
$this->standard_coursemodule_elements();
$this->add_action_buttons(true, false, null);
$mform->addHelpButton('groupmode', 'groupmode', 'learningmap');
}
/**
* Remove visible groups here to avoid warning
*
* @return void
*/
public function definition_after_data() {
$this->_form->_elements[$this->_form->_elementIndex['groupmode']]->removeOption(VISIBLEGROUPS);
parent::definition_after_data();
}
/**
* Returns whether the custom completion rules are enabled.
*
* @param array $data form data
* @return bool
*/
public function completion_rule_enabled($data): bool {
return (!empty($data['completiontype' . $this->get_suffix()]));
}
/**
* Adds the custom completion rules for mod_learningmap
*
* @return array
*/
public function add_completion_rules(): array {
$mform = $this->_form;
$completionoptions = [
custom_completion::NOCOMPLETION => get_string('nocompletion', 'learningmap'),
custom_completion::COMPLETION_WITH_ONE_TARGET => get_string('completion_with_one_target', 'learningmap'),
custom_completion::COMPLETION_WITH_ALL_TARGETS => get_string('completion_with_all_targets', 'learningmap'),
custom_completion::COMPLETION_WITH_ALL_PLACES => get_string('completion_with_all_places', 'mod_learningmap'),
];
$completiontype = 'completiontype' . $this->get_suffix();
$mform->addElement(
'select',
$completiontype,
get_string('completiontype', 'learningmap'),
$completionoptions,
[]
);
$mform->setType($completiontype, PARAM_INT);
$mform->hideIf($completiontype, 'completion', 'neq', COMPLETION_TRACKING_AUTOMATIC);
return([$completiontype]);
}
/**
* Processes the form data before loading the form. Adds the default values for empty forms, replaces the CSS
* with the values for editing.
*
* @param array $defaultvalues
* @return void
*/
public function data_preprocessing(&$defaultvalues): void {
global $OUTPUT;
// Initialize a new learningmap instance.
if (!$this->current->instance) {
// Every map gets a unique id for applying CSS.
$mapid = uniqid();
// The editmode setting loads the CSS styles for the editor.
$options = ['editmode' => true, 'mapid' => $mapid];
// Loads the SVG template to the textarea for the introeditor.
// The textarea is hidden in the browser.
$defaultvalues['svgcode'] = $OUTPUT->render_from_template(
'mod_learningmap/svgskeleton',
$options
);
// Default behaviour is to act as a label. As the user can't see the module description on a view page,
// the description is shown by default.
$defaultvalues['showdescription'] = 1;
$defaultvalues['showmaponcoursepage'] = 1;
// Encodes the base settings as json. Further default settings are
// generated by javascript to avoid duplicate code.
$defaultvalues['placestore'] = json_encode($options);
} else {
$context = context_module::instance($defaultvalues['coursemodule']);
if (empty($defaultvalues['svgcode'])) {
$mapcode = $defaultvalues['intro'];
$filearea = 'intro';
} else {
$mapcode = $defaultvalues['svgcode'];
$filearea = 'background';
}
$defaultvalues['svgcode'] = file_rewrite_pluginfile_URLS(
$mapcode,
'pluginfile.php',
$context->id,
'mod_learningmap',
$filearea,
null
);
$modinfo = get_fast_modinfo($defaultvalues['course']);
$cm = $modinfo->get_cm($defaultvalues['coursemodule']);
// Replace the stylesheet for editing mode.
$mapworker = new mapworker(
$mapcode,
json_decode($defaultvalues['placestore'], true),
$cm,
true
);
$mapworker->process_map_objects();
$mapworker->replace_stylesheet();
$defaultvalues['svgcode'] = $mapworker->get_svgcode();
$draftitemid = file_get_submitted_draft_itemid('backgroundfile');
$defaultvalues['svgcode'] = file_prepare_draft_area(
$draftitemid,
$context->id,
'mod_learningmap',
'background',
0,
['subdirs' => 0, 'maxfiles' => 1],
$defaultvalues['svgcode']
);
$defaultvalues['backgroundfile'] = $draftitemid;
}
}
/**
* Processes the form data after the form is submitted.
* Replaces the CSS in the SVG with the parts suitable for output.
*
* @param stdClass $data
* @return void
*/
public function data_postprocessing($data): void {
// As this form is also used for setting the default completion settings, there might not be an actual learningmap.
if (!empty($data->svgcode)) {
$mapworker = new mapworker(
$data->svgcode,
json_decode($data->placestore, true)
);
$mapworker->replace_stylesheet();
$data->svgcode = $mapworker->get_svgcode();
$data->svgcode = file_rewrite_urls_to_pluginfile(
$data->svgcode,
$data->backgroundfile
);
}
parent::data_postprocessing($data);
}
/**
* Get the suffix to be added to the completion elements when creating them.
* This acts as a spare for compatibility with Moodle 4.1 and 4.2.
*
* @return string The suffix
*/
public function get_suffix(): string {
if (method_exists(get_parent_class($this), 'get_suffix')) {
return parent::get_suffix();
}
return '';
}
}