forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modlib.php
519 lines (440 loc) · 20.9 KB
/
modlib.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<?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/>.
/**
* Library of functions specific to course/modedit.php and course API functions.
* The course API function calling them are course/lib.php:create_module() and update_module().
* This file has been created has an alternative solution to a full refactor of course/modedit.php
* in order to create the course API functions.
*
* @copyright 2013 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package core_course
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/course/lib.php');
/**
* Add course module.
*
* The function does not check user capabilities.
* The function creates course module, module instance, add the module to the correct section.
* It also trigger common action that need to be done after adding/updating a module.
*
* @param object $moduleinfo the moudle data
* @param object $course the course of the module
* @param object $mform this is required by an existing hack to deal with files during MODULENAME_add_instance()
* @return object the updated module info
*/
function add_moduleinfo($moduleinfo, $course, $mform = null) {
global $DB, $CFG;
$moduleinfo->course = $course->id;
$moduleinfo = set_moduleinfo_defaults($moduleinfo);
if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
$moduleinfo->groupmode = 0; // Do not set groupmode.
}
if (!course_allowed_module($course, $moduleinfo->modulename)) {
print_error('moduledisable', '', '', $moduleinfo->modulename);
}
// First add course_module record because we need the context.
$newcm = new stdClass();
$newcm->course = $course->id;
$newcm->module = $moduleinfo->module;
$newcm->instance = 0; // Not known yet, will be updated later (this is similar to restore code).
$newcm->visible = $moduleinfo->visible;
$newcm->groupmode = $moduleinfo->groupmode;
$newcm->groupingid = $moduleinfo->groupingid;
$newcm->groupmembersonly = $moduleinfo->groupmembersonly;
$completion = new completion_info($course);
if ($completion->is_enabled()) {
$newcm->completion = $moduleinfo->completion;
$newcm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
$newcm->completionview = $moduleinfo->completionview;
$newcm->completionexpected = $moduleinfo->completionexpected;
}
if(!empty($CFG->enableavailability)) {
$newcm->availablefrom = $moduleinfo->availablefrom;
$newcm->availableuntil = $moduleinfo->availableuntil;
$newcm->showavailability = $moduleinfo->showavailability;
}
if (isset($moduleinfo->showdescription)) {
$newcm->showdescription = $moduleinfo->showdescription;
} else {
$newcm->showdescription = 0;
}
if (!$moduleinfo->coursemodule = add_course_module($newcm)) {
print_error('cannotaddcoursemodule');
}
if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
$introeditor = $moduleinfo->introeditor;
unset($moduleinfo->introeditor);
$moduleinfo->intro = $introeditor['text'];
$moduleinfo->introformat = $introeditor['format'];
}
$addinstancefunction = $moduleinfo->modulename."_add_instance";
$returnfromfunc = $addinstancefunction($moduleinfo, $mform);
if (!$returnfromfunc or !is_number($returnfromfunc)) {
// Undo everything we can.
$modcontext = context_module::instance($moduleinfo->coursemodule);
delete_context(CONTEXT_MODULE, $moduleinfo->coursemodule);
$DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule));
if (!is_number($returnfromfunc)) {
print_error('invalidfunction', '', course_get_url($course, $cw->section));
} else {
print_error('cannotaddnewmodule', '', course_get_url($course, $cw->section), $moduleinfo->modulename);
}
}
$moduleinfo->instance = $returnfromfunc;
$DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$moduleinfo->coursemodule));
// Update embedded links and save files.
$modcontext = context_module::instance($moduleinfo->coursemodule);
if (!empty($introeditor)) {
$moduleinfo->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,
'mod_'.$moduleinfo->modulename, 'intro', 0,
array('subdirs'=>true), $introeditor['text']);
$DB->set_field($moduleinfo->modulename, 'intro', $moduleinfo->intro, array('id'=>$moduleinfo->instance));
}
// Course_modules and course_sections each contain a reference to each other.
// So we have to update one of them twice.
$sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule, $moduleinfo->section);
// Make sure visibility is set correctly (in particular in calendar).
// Note: allow them to set it even without moodle/course:activityvisibility.
set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
$DB->set_field('course_modules', 'visibleold', 1, array('id' => $moduleinfo->coursemodule));
if (isset($moduleinfo->cmidnumber)) { // Label.
// Set cm idnumber - uniqueness is already verified by form validation.
set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
}
// Set up conditions.
if ($CFG->enableavailability) {
condition_info::update_cm_from_form((object)array('id'=>$moduleinfo->coursemodule), $moduleinfo, false);
}
$eventname = 'mod_created';
add_to_log($course->id, "course", "add mod",
"../mod/$moduleinfo->modulename/view.php?id=$moduleinfo->coursemodule",
"$moduleinfo->modulename $moduleinfo->instance");
add_to_log($course->id, $moduleinfo->modulename, "add",
"view.php?id=$moduleinfo->coursemodule",
"$moduleinfo->instance", $moduleinfo->coursemodule);
$moduleinfo = edit_module_post_actions($moduleinfo, $course, 'mod_created');
return $moduleinfo;
}
/**
* Common create/update module module actions that need to be processed as soon as a module is created/updaded.
* For example: trigger event, create grade parent category, add outcomes, rebuild caches, regrade, save plagiarism settings...
*
* @param object $moduleinfo the module info
* @param object $course the course of the module
* @param string $eventname the event name to trigger
*
* return object moduleinfo update with grading management info
*/
function edit_module_post_actions($moduleinfo, $course, $eventname) {
global $USER, $CFG;
$modcontext = context_module::instance($moduleinfo->coursemodule);
// Trigger mod_created/mod_updated event with information about this module.
$eventdata = new stdClass();
$eventdata->modulename = $moduleinfo->modulename;
$eventdata->name = $moduleinfo->name;
$eventdata->cmid = $moduleinfo->coursemodule;
$eventdata->courseid = $course->id;
$eventdata->userid = $USER->id;
events_trigger($eventname, $eventdata);
// Sync idnumber with grade_item.
if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$moduleinfo->modulename,
'iteminstance'=>$moduleinfo->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
if ($grade_item->idnumber != $moduleinfo->cmidnumber) {
$grade_item->idnumber = $moduleinfo->cmidnumber;
$grade_item->update();
}
}
$items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$moduleinfo->modulename,
'iteminstance'=>$moduleinfo->instance, 'courseid'=>$course->id));
// Create parent category if requested and move to correct parent category.
if ($items and isset($moduleinfo->gradecat)) {
if ($moduleinfo->gradecat == -1) {
$grade_category = new grade_category();
$grade_category->courseid = $course->id;
$grade_category->fullname = $moduleinfo->name;
$grade_category->insert();
if ($grade_item) {
$parent = $grade_item->get_parent_category();
$grade_category->set_parent($parent->id);
}
$moduleinfo->gradecat = $grade_category->id;
}
foreach ($items as $itemid=>$unused) {
$items[$itemid]->set_parent($moduleinfo->gradecat);
if ($itemid == $grade_item->id) {
// Use updated grade_item.
$grade_item = $items[$itemid];
}
}
}
// Add outcomes if requested.
if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
$grade_items = array();
// Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes.
$max_itemnumber = 999;
if ($items) {
foreach($items as $item) {
if ($item->itemnumber > $max_itemnumber) {
$max_itemnumber = $item->itemnumber;
}
}
}
foreach($outcomes as $outcome) {
$elname = 'outcome_'.$outcome->id;
if (property_exists($moduleinfo, $elname) and $moduleinfo->$elname) {
// So we have a request for new outcome grade item?
if ($items) {
$outcomeexists = false;
foreach($items as $item) {
if ($item->outcomeid == $outcome->id) {
$outcomeexists = true;
break;
}
}
if ($outcomeexists) {
continue;
}
}
$max_itemnumber++;
$outcome_item = new grade_item();
$outcome_item->courseid = $course->id;
$outcome_item->itemtype = 'mod';
$outcome_item->itemmodule = $moduleinfo->modulename;
$outcome_item->iteminstance = $moduleinfo->instance;
$outcome_item->itemnumber = $max_itemnumber;
$outcome_item->itemname = $outcome->fullname;
$outcome_item->outcomeid = $outcome->id;
$outcome_item->gradetype = GRADE_TYPE_SCALE;
$outcome_item->scaleid = $outcome->scaleid;
$outcome_item->insert();
// Move the new outcome into correct category and fix sortorder if needed.
if ($grade_item) {
$outcome_item->set_parent($grade_item->categoryid);
$outcome_item->move_after_sortorder($grade_item->sortorder);
} else if (isset($moduleinfo->gradecat)) {
$outcome_item->set_parent($moduleinfo->gradecat);
}
}
}
}
if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_ADVANCED_GRADING, false)
and has_capability('moodle/grade:managegradingforms', $modcontext)) {
require_once($CFG->dirroot.'/grade/grading/lib.php');
$gradingman = get_grading_manager($modcontext, 'mod_'.$moduleinfo->modulename);
$showgradingmanagement = false;
foreach ($gradingman->get_available_areas() as $areaname => $aretitle) {
$formfield = 'advancedgradingmethod_'.$areaname;
if (isset($moduleinfo->{$formfield})) {
$gradingman->set_area($areaname);
$methodchanged = $gradingman->set_active_method($moduleinfo->{$formfield});
if (empty($moduleinfo->{$formfield})) {
// Going back to the simple direct grading is not a reason to open the management screen.
$methodchanged = false;
}
$showgradingmanagement = $showgradingmanagement || $methodchanged;
}
}
// Update grading management information.
$moduleinfo->gradingman = $gradingman;
$moduleinfo->showgradingmanagement = $showgradingmanagement;
}
rebuild_course_cache($course->id);
grade_regrade_final_grades($course->id);
require_once($CFG->libdir.'/plagiarismlib.php');
plagiarism_save_form_elements($moduleinfo);
return $moduleinfo;
}
/**
* Set module info default values for the unset module attributs.
*
* @param object $moduleinfo the current known data of the module
* @return object the completed module info
*/
function set_moduleinfo_defaults($moduleinfo) {
global $DB;
if (empty($moduleinfo->coursemodule)) {
// Add.
$cm = null;
$moduleinfo->instance = '';
$moduleinfo->coursemodule = '';
} else {
// Update.
$cm = get_coursemodule_from_id('', $moduleinfo->coursemodule, 0, false, MUST_EXIST);
$moduleinfo->instance = $cm->instance;
$moduleinfo->coursemodule = $cm->id;
}
// For safety.
$moduleinfo->modulename = clean_param($moduleinfo->modulename, PARAM_PLUGIN);
if (!isset($moduleinfo->groupingid)) {
$moduleinfo->groupingid = 0;
}
if (!isset($moduleinfo->groupmembersonly)) {
$moduleinfo->groupmembersonly = 0;
}
if (!isset($moduleinfo->name)) { // Label.
$moduleinfo->name = $moduleinfo->modulename;
}
if (!isset($moduleinfo->completion)) {
$moduleinfo->completion = COMPLETION_DISABLED;
}
if (!isset($moduleinfo->completionview)) {
$moduleinfo->completionview = COMPLETION_VIEW_NOT_REQUIRED;
}
// Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
if (isset($moduleinfo->completionusegrade) && $moduleinfo->completionusegrade) {
$moduleinfo->completiongradeitemnumber = 0;
} else {
$moduleinfo->completiongradeitemnumber = null;
}
return $moduleinfo;
}
/**
* Check that the user can add a module. Also returns some information like the module, context and course section info.
* The fucntion create the course section if it doesn't exist.
*
* @param object $course the course of the module
* @param object $modulename the module name
* @param object $section the section of the module
* @return array list containing module, context, course section.
*/
function can_add_moduleinfo($course, $modulename, $section) {
global $DB;
$module = $DB->get_record('modules', array('name'=>$modulename), '*', MUST_EXIST);
$context = context_course::instance($course->id);
require_capability('moodle/course:manageactivities', $context);
course_create_sections_if_missing($course, $section);
$cw = get_fast_modinfo($course)->get_section_info($section);
if (!course_allowed_module($course, $module->name)) {
print_error('moduledisable');
}
return array($module, $context, $cw);
}
/**
* Check if user is allowed to update module info and returns related item/data to the module.
*
* @param object $cm course module
* @return array - list of course module, context, module, moduleinfo, and course section.
*/
function can_update_moduleinfo($cm) {
global $DB;
// Check the $USER has the right capability.
$context = context_module::instance($cm->id);
require_capability('moodle/course:manageactivities', $context);
// Check module exists.
$module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST);
// Check the moduleinfo exists.
$data = $DB->get_record($module->name, array('id'=>$cm->instance), '*', MUST_EXIST);
// Check the course section exists.
$cw = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
return array($cm, $context, $module, $data, $cw);
}
/**
* Update the module info.
* This function doesn't check the user capabilities. It updates the course module and the module instance.
* Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
*
* @param object $cm course module
* @param object $moduleinfo module info
* @param object $course course of the module
* @param object $mform - the mform is required by some specific module in the function MODULE_update_instance(). This is due to a hack in this function.
* @return array list of course module and module info.
*/
function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
global $DB, $CFG;
$moduleinfo->course = $course->id;
$moduleinfo = set_moduleinfo_defaults($moduleinfo);
if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
$moduleinfo->groupmode = $cm->groupmode; // Keep original.
}
// Update course module first.
$cm->groupmode = $moduleinfo->groupmode;
if (isset($moduleinfo->groupingid)) {
$cm->groupingid = $moduleinfo->groupingid;
}
if (isset($moduleinfo->groupmembersonly)) {
$cm->groupmembersonly = $moduleinfo->groupmembersonly;
}
$completion = new completion_info($course);
if ($completion->is_enabled()) {
// Update completion settings.
$cm->completion = $moduleinfo->completion;
$cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
$cm->completionview = $moduleinfo->completionview;
$cm->completionexpected = $moduleinfo->completionexpected;
}
if (!empty($CFG->enableavailability)) {
$cm->availablefrom = $moduleinfo->availablefrom;
$cm->availableuntil = $moduleinfo->availableuntil;
$cm->showavailability = $moduleinfo->showavailability;
condition_info::update_cm_from_form($cm,$moduleinfo,true);
}
if (isset($moduleinfo->showdescription)) {
$cm->showdescription = $moduleinfo->showdescription;
} else {
$cm->showdescription = 0;
}
$DB->update_record('course_modules', $cm);
$modcontext = context_module::instance($moduleinfo->coursemodule);
// Update embedded links and save files.
if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
$moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id,
'mod_'.$moduleinfo->modulename, 'intro', 0,
array('subdirs'=>true), $moduleinfo->introeditor['text']);
$moduleinfo->introformat = $moduleinfo->introeditor['format'];
unset($moduleinfo->introeditor);
}
$updateinstancefunction = $moduleinfo->modulename."_update_instance";
if (!$updateinstancefunction($moduleinfo, $mform)) {
print_error('cannotupdatemod', '', course_get_url($course, $cw->section), $moduleinfo->modulename);
}
// Make sure visibility is set correctly (in particular in calendar).
if (has_capability('moodle/course:activityvisibility', $modcontext)) {
set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
}
if (isset($moduleinfo->cmidnumber)) { // Label.
// Set cm idnumber - uniqueness is already verified by form validation.
set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
}
// Now that module is fully updated, also update completion data if required.
// (this will wipe all user completion data and recalculate it)
if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
$completion->reset_all_state($cm);
}
add_to_log($course->id, "course", "update mod",
"../mod/$moduleinfo->modulename/view.php?id=$moduleinfo->coursemodule",
"$moduleinfo->modulename $moduleinfo->instance");
add_to_log($course->id, $moduleinfo->modulename, "update",
"view.php?id=$moduleinfo->coursemodule",
"$moduleinfo->instance", $moduleinfo->coursemodule);
$moduleinfo = edit_module_post_actions($moduleinfo, $course, 'mod_updated');
return array($cm, $moduleinfo);
}
/**
* Include once the module lib file.
*
* @param string $modulename module name of the lib to include
*/
function include_modulelib($modulename) {
global $CFG;
$modlib = "$CFG->dirroot/mod/$modulename/lib.php";
if (file_exists($modlib)) {
include_once($modlib);
} else {
throw new moodle_exception('modulemissingcode', '', '', $modlib);
}
}