Commit | Line | Data |
---|---|---|
80fe0c19 JM |
1 | <?php |
2 | // This file is part of Moodle - http://moodle.org/ | |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
18 | * Library of functions specific to course/modedit.php and course API functions. | |
19 | * The course API function calling them are course/lib.php:create_module() and update_module(). | |
20 | * This file has been created has an alternative solution to a full refactor of course/modedit.php | |
21 | * in order to create the course API functions. | |
22 | * | |
23 | * @copyright 2013 Jerome Mouneyrac | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
25 | * @package core_course | |
26 | */ | |
27 | ||
28 | defined('MOODLE_INTERNAL') || die; | |
29 | ||
fcc88fdd AN |
30 | use \core_grades\component_gradeitems; |
31 | ||
80fe0c19 JM |
32 | require_once($CFG->dirroot.'/course/lib.php'); |
33 | ||
34 | /** | |
35 | * Add course module. | |
36 | * | |
37 | * The function does not check user capabilities. | |
38 | * The function creates course module, module instance, add the module to the correct section. | |
39 | * It also trigger common action that need to be done after adding/updating a module. | |
40 | * | |
41 | * @param object $moduleinfo the moudle data | |
42 | * @param object $course the course of the module | |
43 | * @param object $mform this is required by an existing hack to deal with files during MODULENAME_add_instance() | |
44 | * @return object the updated module info | |
45 | */ | |
46 | function add_moduleinfo($moduleinfo, $course, $mform = null) { | |
47 | global $DB, $CFG; | |
48 | ||
b4b75872 MG |
49 | // Attempt to include module library before we make any changes to DB. |
50 | include_modulelib($moduleinfo->modulename); | |
51 | ||
80fe0c19 JM |
52 | $moduleinfo->course = $course->id; |
53 | $moduleinfo = set_moduleinfo_defaults($moduleinfo); | |
54 | ||
55 | if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) { | |
56 | $moduleinfo->groupmode = 0; // Do not set groupmode. | |
57 | } | |
58 | ||
8f2913b9 | 59 | // First add course_module record because we need the context. |
80fe0c19 JM |
60 | $newcm = new stdClass(); |
61 | $newcm->course = $course->id; | |
62 | $newcm->module = $moduleinfo->module; | |
63 | $newcm->instance = 0; // Not known yet, will be updated later (this is similar to restore code). | |
64 | $newcm->visible = $moduleinfo->visible; | |
7c69ab5d | 65 | $newcm->visibleold = $moduleinfo->visible; |
78ef2ac1 | 66 | $newcm->visibleoncoursepage = $moduleinfo->visibleoncoursepage; |
449d1fa3 MG |
67 | if (isset($moduleinfo->cmidnumber)) { |
68 | $newcm->idnumber = $moduleinfo->cmidnumber; | |
69 | } | |
80fe0c19 JM |
70 | $newcm->groupmode = $moduleinfo->groupmode; |
71 | $newcm->groupingid = $moduleinfo->groupingid; | |
80fe0c19 JM |
72 | $completion = new completion_info($course); |
73 | if ($completion->is_enabled()) { | |
74 | $newcm->completion = $moduleinfo->completion; | |
0a5a2ca8 | 75 | if ($moduleinfo->completiongradeitemnumber === '') { |
c0351419 AN |
76 | $newcm->completiongradeitemnumber = null; |
77 | } else { | |
78 | $newcm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber; | |
79 | } | |
80fe0c19 JM |
80 | $newcm->completionview = $moduleinfo->completionview; |
81 | $newcm->completionexpected = $moduleinfo->completionexpected; | |
82 | } | |
83 | if(!empty($CFG->enableavailability)) { | |
400c0fd2 | 84 | // This code is used both when submitting the form, which uses a long |
85 | // name to avoid clashes, and by unit test code which uses the real | |
86 | // name in the table. | |
87 | $newcm->availability = null; | |
88 | if (property_exists($moduleinfo, 'availabilityconditionsjson')) { | |
89 | if ($moduleinfo->availabilityconditionsjson !== '') { | |
90 | $newcm->availability = $moduleinfo->availabilityconditionsjson; | |
91 | } | |
92 | } else if (property_exists($moduleinfo, 'availability')) { | |
93 | $newcm->availability = $moduleinfo->availability; | |
94 | } | |
06c06038 | 95 | // If there is any availability data, verify it. |
96 | if ($newcm->availability) { | |
97 | $tree = new \core_availability\tree(json_decode($newcm->availability)); | |
98 | // Save time and database space by setting null if the only data | |
99 | // is an empty tree. | |
100 | if ($tree->is_empty()) { | |
101 | $newcm->availability = null; | |
102 | } | |
103 | } | |
80fe0c19 JM |
104 | } |
105 | if (isset($moduleinfo->showdescription)) { | |
106 | $newcm->showdescription = $moduleinfo->showdescription; | |
107 | } else { | |
108 | $newcm->showdescription = 0; | |
109 | } | |
110 | ||
5c3a54c4 | 111 | // From this point we make database changes, so start transaction. |
112 | $transaction = $DB->start_delegated_transaction(); | |
113 | ||
80fe0c19 JM |
114 | if (!$moduleinfo->coursemodule = add_course_module($newcm)) { |
115 | print_error('cannotaddcoursemodule'); | |
116 | } | |
117 | ||
7fbe33fc MG |
118 | if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true) && |
119 | isset($moduleinfo->introeditor)) { | |
80fe0c19 JM |
120 | $introeditor = $moduleinfo->introeditor; |
121 | unset($moduleinfo->introeditor); | |
122 | $moduleinfo->intro = $introeditor['text']; | |
123 | $moduleinfo->introformat = $introeditor['format']; | |
124 | } | |
125 | ||
126 | $addinstancefunction = $moduleinfo->modulename."_add_instance"; | |
5c3a54c4 | 127 | try { |
128 | $returnfromfunc = $addinstancefunction($moduleinfo, $mform); | |
129 | } catch (moodle_exception $e) { | |
130 | $returnfromfunc = $e; | |
131 | } | |
80fe0c19 | 132 | if (!$returnfromfunc or !is_number($returnfromfunc)) { |
5c3a54c4 | 133 | // Undo everything we can. This is not necessary for databases which |
134 | // support transactions, but improves consistency for other databases. | |
c592eea2 | 135 | context_helper::delete_instance(CONTEXT_MODULE, $moduleinfo->coursemodule); |
80fe0c19 JM |
136 | $DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule)); |
137 | ||
70a557db | 138 | if ($returnfromfunc instanceof moodle_exception) { |
139 | throw $returnfromfunc; | |
5c3a54c4 | 140 | } else if (!is_number($returnfromfunc)) { |
b4b75872 | 141 | print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section)); |
80fe0c19 | 142 | } else { |
b4b75872 | 143 | print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section), $moduleinfo->modulename); |
80fe0c19 JM |
144 | } |
145 | } | |
146 | ||
147 | $moduleinfo->instance = $returnfromfunc; | |
148 | ||
149 | $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$moduleinfo->coursemodule)); | |
150 | ||
151 | // Update embedded links and save files. | |
152 | $modcontext = context_module::instance($moduleinfo->coursemodule); | |
153 | if (!empty($introeditor)) { | |
4fc74fe5 DB |
154 | // This will respect a module that has set a value for intro in it's modname_add_instance() function. |
155 | $introeditor['text'] = $moduleinfo->intro; | |
156 | ||
80fe0c19 JM |
157 | $moduleinfo->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, |
158 | 'mod_'.$moduleinfo->modulename, 'intro', 0, | |
159 | array('subdirs'=>true), $introeditor['text']); | |
160 | $DB->set_field($moduleinfo->modulename, 'intro', $moduleinfo->intro, array('id'=>$moduleinfo->instance)); | |
161 | } | |
162 | ||
dffcf46f NK |
163 | // Add module tags. |
164 | if (core_tag_tag::is_enabled('core', 'course_modules') && isset($moduleinfo->tags)) { | |
165 | core_tag_tag::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule, $modcontext, $moduleinfo->tags); | |
166 | } | |
167 | ||
80fe0c19 JM |
168 | // Course_modules and course_sections each contain a reference to each other. |
169 | // So we have to update one of them twice. | |
170 | $sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule, $moduleinfo->section); | |
171 | ||
63deb5c3 | 172 | // Trigger event based on the action we did. |
e20d3069 AA |
173 | // Api create_from_cm expects modname and id property, and we don't want to modify $moduleinfo since we are returning it. |
174 | $eventdata = clone $moduleinfo; | |
175 | $eventdata->modname = $eventdata->modulename; | |
176 | $eventdata->id = $eventdata->coursemodule; | |
177 | $event = \core\event\course_module_created::create_from_cm($eventdata, $modcontext); | |
63deb5c3 | 178 | $event->trigger(); |
80fe0c19 | 179 | |
63deb5c3 | 180 | $moduleinfo = edit_module_post_actions($moduleinfo, $course); |
5c3a54c4 | 181 | $transaction->allow_commit(); |
80fe0c19 JM |
182 | |
183 | return $moduleinfo; | |
184 | } | |
185 | ||
8995c270 DW |
186 | /** |
187 | * Hook for plugins to take action when a module is created or updated. | |
188 | * | |
189 | * @param stdClass $moduleinfo the module info | |
190 | * @param stdClass $course the course of the module | |
191 | * | |
192 | * @return stdClass moduleinfo updated by plugins. | |
193 | */ | |
194 | function plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course) { | |
195 | $callbacks = get_plugins_with_function('coursemodule_edit_post_actions', 'lib.php'); | |
196 | foreach ($callbacks as $type => $plugins) { | |
197 | foreach ($plugins as $plugin => $pluginfunction) { | |
198 | $moduleinfo = $pluginfunction($moduleinfo, $course); | |
199 | } | |
200 | } | |
201 | return $moduleinfo; | |
202 | } | |
80fe0c19 JM |
203 | |
204 | /** | |
205 | * Common create/update module module actions that need to be processed as soon as a module is created/updaded. | |
63deb5c3 AA |
206 | * For example:create grade parent category, add outcomes, rebuild caches, regrade, save plagiarism settings... |
207 | * Please note this api does not trigger events as of MOODLE 2.6. Please trigger events before calling this api. | |
80fe0c19 JM |
208 | * |
209 | * @param object $moduleinfo the module info | |
210 | * @param object $course the course of the module | |
80fe0c19 | 211 | * |
63deb5c3 | 212 | * @return object moduleinfo update with grading management info |
80fe0c19 | 213 | */ |
63deb5c3 AA |
214 | function edit_module_post_actions($moduleinfo, $course) { |
215 | global $CFG; | |
3e6adca9 | 216 | require_once($CFG->libdir.'/gradelib.php'); |
80fe0c19 JM |
217 | |
218 | $modcontext = context_module::instance($moduleinfo->coursemodule); | |
449d1fa3 MG |
219 | $hasgrades = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_HAS_GRADE, false); |
220 | $hasoutcomes = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_OUTCOMES, true); | |
80fe0c19 | 221 | |
fcc88fdd AN |
222 | $items = grade_item::fetch_all([ |
223 | 'itemtype' => 'mod', | |
224 | 'itemmodule' => $moduleinfo->modulename, | |
225 | 'iteminstance' => $moduleinfo->instance, | |
226 | 'courseid' => $course->id, | |
227 | ]); | |
80fe0c19 JM |
228 | |
229 | // Create parent category if requested and move to correct parent category. | |
fcc88fdd AN |
230 | $component = "mod_{$moduleinfo->modulename}"; |
231 | if ($items) { | |
232 | foreach ($items as $item) { | |
233 | $update = false; | |
234 | ||
235 | // Sync idnumber with grade_item. | |
236 | // Note: This only happens for itemnumber 0 at this time. | |
237 | if ($item->itemnumber == 0 && ($item->idnumber != $moduleinfo->cmidnumber)) { | |
238 | $item->idnumber = $moduleinfo->cmidnumber; | |
239 | $update = true; | |
80fe0c19 | 240 | } |
595b911a | 241 | |
fcc88fdd AN |
242 | // Determine the grade category. |
243 | $gradecatfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $item->itemnumber, 'gradecat'); | |
244 | if (property_exists($moduleinfo, $gradecatfieldname)) { | |
245 | $gradecat = $moduleinfo->$gradecatfieldname; | |
246 | if ($gradecat == -1) { | |
247 | $gradecategory = new grade_category(); | |
248 | $gradecategory->courseid = $course->id; | |
249 | $gradecategory->fullname = $moduleinfo->name; | |
250 | $gradecategory->insert(); | |
251 | ||
252 | $parent = $item->get_parent_category(); | |
253 | $gradecategory->set_parent($parent->id); | |
254 | $gradecat = $gradecategory->id; | |
255 | } | |
256 | ||
257 | $oldgradecat = null; | |
258 | if ($parent = $item->get_parent_category()) { | |
259 | $oldgradecat = $parent->id; | |
260 | } | |
261 | if ($oldgradecat != $gradecat) { | |
262 | $item->set_parent($gradecat); | |
263 | $update = true; | |
264 | } | |
265 | } | |
266 | ||
267 | // Determine the gradepass. | |
268 | $gradepassfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $item->itemnumber, 'gradepass'); | |
269 | if (isset($moduleinfo->{$gradepassfieldname})) { | |
270 | $gradepass = $moduleinfo->{$gradepassfieldname}; | |
271 | if (null !== $gradepass && $gradepass != $item->gradepass) { | |
272 | $item->gradepass = $gradepass; | |
273 | $update = true; | |
274 | } | |
275 | } | |
276 | ||
277 | if ($update) { | |
278 | $item->update(); | |
80fe0c19 JM |
279 | } |
280 | } | |
281 | } | |
282 | ||
2cd29dd6 | 283 | require_once($CFG->libdir.'/grade/grade_outcome.php'); |
80fe0c19 | 284 | // Add outcomes if requested. |
449d1fa3 | 285 | if ($hasoutcomes && $outcomes = grade_outcome::fetch_all_available($course->id)) { |
80fe0c19 JM |
286 | // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes. |
287 | $max_itemnumber = 999; | |
288 | if ($items) { | |
289 | foreach($items as $item) { | |
290 | if ($item->itemnumber > $max_itemnumber) { | |
291 | $max_itemnumber = $item->itemnumber; | |
292 | } | |
293 | } | |
294 | } | |
295 | ||
296 | foreach($outcomes as $outcome) { | |
297 | $elname = 'outcome_'.$outcome->id; | |
298 | ||
299 | if (property_exists($moduleinfo, $elname) and $moduleinfo->$elname) { | |
fcc88fdd | 300 | // Check if this is a new outcome grade item. |
80fe0c19 | 301 | if ($items) { |
3b2a6bf5 | 302 | $outcomeexists = false; |
80fe0c19 JM |
303 | foreach($items as $item) { |
304 | if ($item->outcomeid == $outcome->id) { | |
3b2a6bf5 DW |
305 | $outcomeexists = true; |
306 | break; | |
80fe0c19 JM |
307 | } |
308 | } | |
3b2a6bf5 DW |
309 | if ($outcomeexists) { |
310 | continue; | |
311 | } | |
80fe0c19 JM |
312 | } |
313 | ||
314 | $max_itemnumber++; | |
315 | ||
fcc88fdd AN |
316 | $outcomeitem = new grade_item(); |
317 | $outcomeitem->courseid = $course->id; | |
318 | $outcomeitem->itemtype = 'mod'; | |
319 | $outcomeitem->itemmodule = $moduleinfo->modulename; | |
320 | $outcomeitem->iteminstance = $moduleinfo->instance; | |
321 | $outcomeitem->itemnumber = $max_itemnumber; | |
322 | $outcomeitem->itemname = $outcome->fullname; | |
323 | $outcomeitem->outcomeid = $outcome->id; | |
324 | $outcomeitem->gradetype = GRADE_TYPE_SCALE; | |
325 | $outcomeitem->scaleid = $outcome->scaleid; | |
326 | $outcomeitem->insert(); | |
80fe0c19 | 327 | |
fcc88fdd AN |
328 | if ($items) { |
329 | // Move the new outcome into the same category and immediately after the first grade item. | |
330 | $item = reset($items); | |
331 | $outcomeitem->set_parent($item->categoryid); | |
332 | $outcomeitem->move_after_sortorder($item->sortorder); | |
80fe0c19 | 333 | } else if (isset($moduleinfo->gradecat)) { |
fcc88fdd | 334 | $outcomeitem->set_parent($moduleinfo->gradecat); |
80fe0c19 JM |
335 | } |
336 | } | |
337 | } | |
338 | } | |
339 | ||
340 | if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_ADVANCED_GRADING, false) | |
341 | and has_capability('moodle/grade:managegradingforms', $modcontext)) { | |
342 | require_once($CFG->dirroot.'/grade/grading/lib.php'); | |
343 | $gradingman = get_grading_manager($modcontext, 'mod_'.$moduleinfo->modulename); | |
344 | $showgradingmanagement = false; | |
345 | foreach ($gradingman->get_available_areas() as $areaname => $aretitle) { | |
346 | $formfield = 'advancedgradingmethod_'.$areaname; | |
347 | if (isset($moduleinfo->{$formfield})) { | |
348 | $gradingman->set_area($areaname); | |
349 | $methodchanged = $gradingman->set_active_method($moduleinfo->{$formfield}); | |
350 | if (empty($moduleinfo->{$formfield})) { | |
351 | // Going back to the simple direct grading is not a reason to open the management screen. | |
352 | $methodchanged = false; | |
353 | } | |
354 | $showgradingmanagement = $showgradingmanagement || $methodchanged; | |
355 | } | |
356 | } | |
357 | // Update grading management information. | |
358 | $moduleinfo->gradingman = $gradingman; | |
359 | $moduleinfo->showgradingmanagement = $showgradingmanagement; | |
360 | } | |
361 | ||
449d1fa3 MG |
362 | rebuild_course_cache($course->id, true); |
363 | if ($hasgrades) { | |
364 | grade_regrade_final_grades($course->id); | |
365 | } | |
d0d65b49 AG |
366 | |
367 | // To be removed (deprecated) with MDL-67526 (both lines). | |
80fe0c19 | 368 | require_once($CFG->libdir.'/plagiarismlib.php'); |
8f2913b9 | 369 | plagiarism_save_form_elements($moduleinfo); |
80fe0c19 | 370 | |
8995c270 DW |
371 | // Allow plugins to extend the course module form. |
372 | $moduleinfo = plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course); | |
373 | ||
80fe0c19 JM |
374 | return $moduleinfo; |
375 | } | |
376 | ||
80fe0c19 JM |
377 | /** |
378 | * Set module info default values for the unset module attributs. | |
379 | * | |
380 | * @param object $moduleinfo the current known data of the module | |
381 | * @return object the completed module info | |
382 | */ | |
383 | function set_moduleinfo_defaults($moduleinfo) { | |
80fe0c19 JM |
384 | |
385 | if (empty($moduleinfo->coursemodule)) { | |
386 | // Add. | |
387 | $cm = null; | |
388 | $moduleinfo->instance = ''; | |
389 | $moduleinfo->coursemodule = ''; | |
390 | } else { | |
391 | // Update. | |
392 | $cm = get_coursemodule_from_id('', $moduleinfo->coursemodule, 0, false, MUST_EXIST); | |
393 | $moduleinfo->instance = $cm->instance; | |
394 | $moduleinfo->coursemodule = $cm->id; | |
395 | } | |
8f2913b9 DW |
396 | // For safety. |
397 | $moduleinfo->modulename = clean_param($moduleinfo->modulename, PARAM_PLUGIN); | |
80fe0c19 JM |
398 | |
399 | if (!isset($moduleinfo->groupingid)) { | |
400 | $moduleinfo->groupingid = 0; | |
401 | } | |
402 | ||
80fe0c19 JM |
403 | if (!isset($moduleinfo->name)) { // Label. |
404 | $moduleinfo->name = $moduleinfo->modulename; | |
405 | } | |
406 | ||
407 | if (!isset($moduleinfo->completion)) { | |
408 | $moduleinfo->completion = COMPLETION_DISABLED; | |
409 | } | |
410 | if (!isset($moduleinfo->completionview)) { | |
411 | $moduleinfo->completionview = COMPLETION_VIEW_NOT_REQUIRED; | |
412 | } | |
6f9354be MG |
413 | if (!isset($moduleinfo->completionexpected)) { |
414 | $moduleinfo->completionexpected = 0; | |
415 | } | |
80fe0c19 JM |
416 | |
417 | // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not. | |
418 | if (isset($moduleinfo->completionusegrade) && $moduleinfo->completionusegrade) { | |
419 | $moduleinfo->completiongradeitemnumber = 0; | |
fe795b59 | 420 | } else if (!isset($moduleinfo->completiongradeitemnumber)) { |
80fe0c19 JM |
421 | $moduleinfo->completiongradeitemnumber = null; |
422 | } | |
423 | ||
6f9354be MG |
424 | if (!isset($moduleinfo->conditiongradegroup)) { |
425 | $moduleinfo->conditiongradegroup = array(); | |
426 | } | |
427 | if (!isset($moduleinfo->conditionfieldgroup)) { | |
428 | $moduleinfo->conditionfieldgroup = array(); | |
429 | } | |
78ef2ac1 PEK |
430 | if (!isset($moduleinfo->visibleoncoursepage)) { |
431 | $moduleinfo->visibleoncoursepage = 1; | |
432 | } | |
6f9354be | 433 | |
80fe0c19 JM |
434 | return $moduleinfo; |
435 | } | |
436 | ||
437 | /** | |
438 | * Check that the user can add a module. Also returns some information like the module, context and course section info. | |
439 | * The fucntion create the course section if it doesn't exist. | |
440 | * | |
441 | * @param object $course the course of the module | |
442 | * @param object $modulename the module name | |
443 | * @param object $section the section of the module | |
444 | * @return array list containing module, context, course section. | |
b4b75872 | 445 | * @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course |
80fe0c19 JM |
446 | */ |
447 | function can_add_moduleinfo($course, $modulename, $section) { | |
448 | global $DB; | |
449 | ||
450 | $module = $DB->get_record('modules', array('name'=>$modulename), '*', MUST_EXIST); | |
451 | ||
452 | $context = context_course::instance($course->id); | |
453 | require_capability('moodle/course:manageactivities', $context); | |
454 | ||
455 | course_create_sections_if_missing($course, $section); | |
456 | $cw = get_fast_modinfo($course)->get_section_info($section); | |
457 | ||
458 | if (!course_allowed_module($course, $module->name)) { | |
459 | print_error('moduledisable'); | |
460 | } | |
461 | ||
462 | return array($module, $context, $cw); | |
463 | } | |
464 | ||
465 | /** | |
466 | * Check if user is allowed to update module info and returns related item/data to the module. | |
467 | * | |
468 | * @param object $cm course module | |
469 | * @return array - list of course module, context, module, moduleinfo, and course section. | |
b4b75872 | 470 | * @throws moodle_exception if user is not allowed to perform the action |
80fe0c19 JM |
471 | */ |
472 | function can_update_moduleinfo($cm) { | |
473 | global $DB; | |
474 | ||
475 | // Check the $USER has the right capability. | |
476 | $context = context_module::instance($cm->id); | |
477 | require_capability('moodle/course:manageactivities', $context); | |
478 | ||
479 | // Check module exists. | |
480 | $module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST); | |
481 | ||
482 | // Check the moduleinfo exists. | |
483 | $data = $DB->get_record($module->name, array('id'=>$cm->instance), '*', MUST_EXIST); | |
484 | ||
485 | // Check the course section exists. | |
486 | $cw = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST); | |
487 | ||
488 | return array($cm, $context, $module, $data, $cw); | |
489 | } | |
490 | ||
491 | ||
492 | /** | |
493 | * Update the module info. | |
494 | * This function doesn't check the user capabilities. It updates the course module and the module instance. | |
495 | * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...). | |
496 | * | |
497 | * @param object $cm course module | |
498 | * @param object $moduleinfo module info | |
499 | * @param object $course course of the module | |
500 | * @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. | |
501 | * @return array list of course module and module info. | |
502 | */ | |
503 | function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) { | |
504 | global $DB, $CFG; | |
505 | ||
d629c601 DW |
506 | $data = new stdClass(); |
507 | if ($mform) { | |
508 | $data = $mform->get_data(); | |
509 | } | |
510 | ||
b4b75872 MG |
511 | // Attempt to include module library before we make any changes to DB. |
512 | include_modulelib($moduleinfo->modulename); | |
513 | ||
80fe0c19 JM |
514 | $moduleinfo->course = $course->id; |
515 | $moduleinfo = set_moduleinfo_defaults($moduleinfo); | |
516 | ||
517 | if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) { | |
518 | $moduleinfo->groupmode = $cm->groupmode; // Keep original. | |
519 | } | |
520 | ||
521 | // Update course module first. | |
522 | $cm->groupmode = $moduleinfo->groupmode; | |
523 | if (isset($moduleinfo->groupingid)) { | |
524 | $cm->groupingid = $moduleinfo->groupingid; | |
525 | } | |
80fe0c19 JM |
526 | |
527 | $completion = new completion_info($course); | |
af729c3f | 528 | if ($completion->is_enabled()) { |
529 | // Completion settings that would affect users who have already completed | |
530 | // the activity may be locked; if so, these should not be updated. | |
531 | if (!empty($moduleinfo->completionunlocked)) { | |
c3777543 | 532 | $cm->completion = $moduleinfo->completion; |
0a5a2ca8 | 533 | if ($moduleinfo->completiongradeitemnumber === '') { |
c0351419 AN |
534 | $cm->completiongradeitemnumber = null; |
535 | } else { | |
536 | $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber; | |
537 | } | |
af729c3f | 538 | $cm->completionview = $moduleinfo->completionview; |
539 | } | |
540 | // The expected date does not affect users who have completed the activity, | |
541 | // so it is safe to update it regardless of the lock status. | |
542 | $cm->completionexpected = $moduleinfo->completionexpected; | |
80fe0c19 JM |
543 | } |
544 | if (!empty($CFG->enableavailability)) { | |
400c0fd2 | 545 | // This code is used both when submitting the form, which uses a long |
546 | // name to avoid clashes, and by unit test code which uses the real | |
547 | // name in the table. | |
548 | if (property_exists($moduleinfo, 'availabilityconditionsjson')) { | |
549 | if ($moduleinfo->availabilityconditionsjson !== '') { | |
550 | $cm->availability = $moduleinfo->availabilityconditionsjson; | |
551 | } else { | |
552 | $cm->availability = null; | |
553 | } | |
554 | } else if (property_exists($moduleinfo, 'availability')) { | |
555 | $cm->availability = $moduleinfo->availability; | |
556 | } | |
06c06038 | 557 | // If there is any availability data, verify it. |
558 | if ($cm->availability) { | |
559 | $tree = new \core_availability\tree(json_decode($cm->availability)); | |
560 | // Save time and database space by setting null if the only data | |
561 | // is an empty tree. | |
562 | if ($tree->is_empty()) { | |
563 | $cm->availability = null; | |
564 | } | |
565 | } | |
80fe0c19 JM |
566 | } |
567 | if (isset($moduleinfo->showdescription)) { | |
568 | $cm->showdescription = $moduleinfo->showdescription; | |
569 | } else { | |
570 | $cm->showdescription = 0; | |
571 | } | |
572 | ||
8f2913b9 | 573 | $DB->update_record('course_modules', $cm); |
80fe0c19 | 574 | |
8f2913b9 | 575 | $modcontext = context_module::instance($moduleinfo->coursemodule); |
80fe0c19 | 576 | |
8f2913b9 DW |
577 | // Update embedded links and save files. |
578 | if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) { | |
579 | $moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id, | |
580 | 'mod_'.$moduleinfo->modulename, 'intro', 0, | |
581 | array('subdirs'=>true), $moduleinfo->introeditor['text']); | |
582 | $moduleinfo->introformat = $moduleinfo->introeditor['format']; | |
583 | unset($moduleinfo->introeditor); | |
584 | } | |
d629c601 DW |
585 | // Get the a copy of the grade_item before it is modified incase we need to scale the grades. |
586 | $oldgradeitem = null; | |
587 | $newgradeitem = null; | |
e7c71c18 | 588 | if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') { |
d629c601 DW |
589 | // Fetch the grade item before it is updated. |
590 | $oldgradeitem = grade_item::fetch(array('itemtype' => 'mod', | |
591 | 'itemmodule' => $moduleinfo->modulename, | |
592 | 'iteminstance' => $moduleinfo->instance, | |
593 | 'itemnumber' => 0, | |
594 | 'courseid' => $moduleinfo->course)); | |
595 | } | |
596 | ||
8f2913b9 DW |
597 | $updateinstancefunction = $moduleinfo->modulename."_update_instance"; |
598 | if (!$updateinstancefunction($moduleinfo, $mform)) { | |
d629c601 DW |
599 | print_error('cannotupdatemod', '', course_get_url($course, $cm->section), $moduleinfo->modulename); |
600 | } | |
601 | ||
602 | // This needs to happen AFTER the grademin/grademax have already been updated. | |
e7c71c18 | 603 | if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') { |
d629c601 DW |
604 | // Get the grade_item after the update call the activity to scale the grades. |
605 | $newgradeitem = grade_item::fetch(array('itemtype' => 'mod', | |
606 | 'itemmodule' => $moduleinfo->modulename, | |
607 | 'iteminstance' => $moduleinfo->instance, | |
608 | 'itemnumber' => 0, | |
609 | 'courseid' => $moduleinfo->course)); | |
610 | if ($newgradeitem && $oldgradeitem->gradetype == GRADE_TYPE_VALUE && $newgradeitem->gradetype == GRADE_TYPE_VALUE) { | |
611 | $params = array( | |
612 | $course, | |
613 | $cm, | |
614 | $oldgradeitem->grademin, | |
615 | $oldgradeitem->grademax, | |
616 | $newgradeitem->grademin, | |
617 | $newgradeitem->grademax | |
618 | ); | |
619 | if (!component_callback('mod_' . $moduleinfo->modulename, 'rescale_activity_grades', $params)) { | |
620 | print_error('cannotreprocessgrades', '', course_get_url($course, $cm->section), $moduleinfo->modulename); | |
621 | } | |
622 | } | |
8f2913b9 | 623 | } |
80fe0c19 | 624 | |
8f2913b9 DW |
625 | // Make sure visibility is set correctly (in particular in calendar). |
626 | if (has_capability('moodle/course:activityvisibility', $modcontext)) { | |
8341055e | 627 | set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible, $moduleinfo->visibleoncoursepage); |
8f2913b9 | 628 | } |
80fe0c19 | 629 | |
8f2913b9 DW |
630 | if (isset($moduleinfo->cmidnumber)) { // Label. |
631 | // Set cm idnumber - uniqueness is already verified by form validation. | |
632 | set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber); | |
633 | } | |
80fe0c19 | 634 | |
dffcf46f NK |
635 | // Update module tags. |
636 | if (core_tag_tag::is_enabled('core', 'course_modules') && isset($moduleinfo->tags)) { | |
637 | core_tag_tag::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule, $modcontext, $moduleinfo->tags); | |
638 | } | |
639 | ||
8f2913b9 DW |
640 | // Now that module is fully updated, also update completion data if required. |
641 | // (this will wipe all user completion data and recalculate it) | |
642 | if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) { | |
643 | $completion->reset_all_state($cm); | |
644 | } | |
9e533215 SL |
645 | $cm->name = $moduleinfo->name; |
646 | \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); | |
63deb5c3 | 647 | |
63deb5c3 | 648 | $moduleinfo = edit_module_post_actions($moduleinfo, $course); |
80fe0c19 | 649 | |
8f2913b9 | 650 | return array($cm, $moduleinfo); |
80fe0c19 JM |
651 | } |
652 | ||
653 | /** | |
654 | * Include once the module lib file. | |
655 | * | |
656 | * @param string $modulename module name of the lib to include | |
b4b75872 | 657 | * @throws moodle_exception if lib.php file for the module does not exist |
80fe0c19 JM |
658 | */ |
659 | function include_modulelib($modulename) { | |
660 | global $CFG; | |
661 | $modlib = "$CFG->dirroot/mod/$modulename/lib.php"; | |
662 | if (file_exists($modlib)) { | |
663 | include_once($modlib); | |
664 | } else { | |
665 | throw new moodle_exception('modulemissingcode', '', '', $modlib); | |
666 | } | |
667 | } | |
668 | ||
796876b0 JL |
669 | /** |
670 | * Get module information data required for updating the module. | |
671 | * | |
672 | * @param stdClass $cm course module object | |
673 | * @param stdClass $course course object | |
674 | * @return array required data for updating a module | |
675 | * @since Moodle 3.2 | |
676 | */ | |
677 | function get_moduleinfo_data($cm, $course) { | |
678 | global $CFG; | |
679 | ||
680 | list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm); | |
681 | ||
682 | $data->coursemodule = $cm->id; | |
683 | $data->section = $cw->section; // The section number itself - relative!!! (section column in course_sections) | |
684 | $data->visible = $cm->visible; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides | |
8341055e | 685 | $data->visibleoncoursepage = $cm->visibleoncoursepage; |
796876b0 JL |
686 | $data->cmidnumber = $cm->idnumber; // The cm IDnumber |
687 | $data->groupmode = groups_get_activity_groupmode($cm); // locked later if forced | |
688 | $data->groupingid = $cm->groupingid; | |
689 | $data->course = $course->id; | |
690 | $data->module = $module->id; | |
691 | $data->modulename = $module->name; | |
692 | $data->instance = $cm->instance; | |
693 | $data->completion = $cm->completion; | |
694 | $data->completionview = $cm->completionview; | |
695 | $data->completionexpected = $cm->completionexpected; | |
696 | $data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1; | |
fe795b59 | 697 | $data->completiongradeitemnumber = $cm->completiongradeitemnumber; |
796876b0 JL |
698 | $data->showdescription = $cm->showdescription; |
699 | $data->tags = core_tag_tag::get_item_tags_array('core', 'course_modules', $cm->id); | |
700 | if (!empty($CFG->enableavailability)) { | |
701 | $data->availabilityconditionsjson = $cm->availability; | |
702 | } | |
703 | ||
704 | if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) { | |
705 | $draftid_editor = file_get_submitted_draft_itemid('introeditor'); | |
706 | $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro); | |
707 | $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor); | |
708 | } | |
709 | ||
710 | if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false) | |
711 | and has_capability('moodle/grade:managegradingforms', $context)) { | |
712 | require_once($CFG->dirroot.'/grade/grading/lib.php'); | |
713 | $gradingman = get_grading_manager($context, 'mod_'.$data->modulename); | |
714 | $data->_advancedgradingdata['methods'] = $gradingman->get_available_methods(); | |
715 | $areas = $gradingman->get_available_areas(); | |
716 | ||
717 | foreach ($areas as $areaname => $areatitle) { | |
718 | $gradingman->set_area($areaname); | |
719 | $method = $gradingman->get_active_method(); | |
720 | $data->_advancedgradingdata['areas'][$areaname] = array( | |
721 | 'title' => $areatitle, | |
722 | 'method' => $method, | |
723 | ); | |
724 | $formfield = 'advancedgradingmethod_'.$areaname; | |
725 | $data->{$formfield} = $method; | |
726 | } | |
727 | } | |
728 | ||
fcc88fdd AN |
729 | $component = "mod_{$data->modulename}"; |
730 | $items = grade_item::fetch_all([ | |
731 | 'itemtype' => 'mod', | |
732 | 'itemmodule' => $data->modulename, | |
733 | 'iteminstance' => $data->instance, | |
734 | 'courseid' => $course->id, | |
735 | ]); | |
736 | ||
737 | if ($items) { | |
796876b0 JL |
738 | // Add existing outcomes. |
739 | foreach ($items as $item) { | |
740 | if (!empty($item->outcomeid)) { | |
741 | $data->{'outcome_' . $item->outcomeid} = 1; | |
16e62ecc | 742 | } else if (isset($item->gradepass)) { |
fcc88fdd AN |
743 | $gradepassfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $item->itemnumber, 'gradepass'); |
744 | $data->{$gradepassfieldname} = format_float($item->gradepass, $item->get_decimals()); | |
796876b0 | 745 | } |
fcc88fdd | 746 | |
796876b0 JL |
747 | } |
748 | ||
749 | // set category if present | |
fcc88fdd | 750 | $gradecat = []; |
796876b0 | 751 | foreach ($items as $item) { |
fcc88fdd AN |
752 | if (!isset($gradecat[$item->itemnumber])) { |
753 | $gradecat[$item->itemnumber] = $item->categoryid; | |
796876b0 | 754 | } |
fcc88fdd AN |
755 | if ($gradecat[$item->itemnumber] != $item->categoryid) { |
756 | // Mixed categories. | |
757 | $gradecat[$item->itemnumber] = false; | |
796876b0 JL |
758 | } |
759 | } | |
fcc88fdd AN |
760 | foreach ($gradecat as $itemnumber => $cat) { |
761 | if ($cat !== false) { | |
762 | $gradecatfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $itemnumber, 'gradecat'); | |
763 | // Do not set if mixed categories present. | |
764 | $data->{$gradecatfieldname} = $cat; | |
765 | } | |
796876b0 JL |
766 | } |
767 | } | |
768 | return array($cm, $context, $module, $data, $cw); | |
769 | } | |
770 | ||
771 | /** | |
772 | * Prepare the standard module information for a new module instance. | |
773 | * | |
774 | * @param stdClass $course course object | |
775 | * @param string $modulename module name | |
776 | * @param int $section section number | |
777 | * @return array module information about other required data | |
778 | * @since Moodle 3.2 | |
779 | */ | |
780 | function prepare_new_moduleinfo_data($course, $modulename, $section) { | |
781 | global $CFG; | |
782 | ||
783 | list($module, $context, $cw) = can_add_moduleinfo($course, $modulename, $section); | |
784 | ||
785 | $cm = null; | |
786 | ||
787 | $data = new stdClass(); | |
788 | $data->section = $section; // The section number itself - relative!!! (section column in course_sections) | |
789 | $data->visible = $cw->visible; | |
790 | $data->course = $course->id; | |
791 | $data->module = $module->id; | |
792 | $data->modulename = $module->name; | |
793 | $data->groupmode = $course->groupmode; | |
794 | $data->groupingid = $course->defaultgroupingid; | |
795 | $data->id = ''; | |
796 | $data->instance = ''; | |
797 | $data->coursemodule = ''; | |
798 | ||
7f53e8aa MG |
799 | // Apply completion defaults. |
800 | $defaults = \core_completion\manager::get_default_completion($course, $module); | |
801 | foreach ($defaults as $key => $value) { | |
802 | $data->$key = $value; | |
803 | } | |
804 | ||
796876b0 JL |
805 | if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) { |
806 | $draftid_editor = file_get_submitted_draft_itemid('introeditor'); | |
807 | file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true)); | |
808 | $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default | |
809 | } | |
810 | ||
811 | if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false) | |
812 | and has_capability('moodle/grade:managegradingforms', $context)) { | |
813 | require_once($CFG->dirroot.'/grade/grading/lib.php'); | |
814 | ||
815 | $data->_advancedgradingdata['methods'] = grading_manager::available_methods(); | |
816 | $areas = grading_manager::available_areas('mod_'.$module->name); | |
817 | ||
818 | foreach ($areas as $areaname => $areatitle) { | |
819 | $data->_advancedgradingdata['areas'][$areaname] = array( | |
820 | 'title' => $areatitle, | |
821 | 'method' => '', | |
822 | ); | |
823 | $formfield = 'advancedgradingmethod_'.$areaname; | |
824 | $data->{$formfield} = ''; | |
825 | } | |
826 | } | |
827 | ||
828 | return array($module, $context, $cw, $cm, $data); | |
829 | } |