MDL-52252 Tags: Add tags to modules (Resources and Activities)
[moodle.git] / course / modlib.php
CommitLineData
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
28defined('MOODLE_INTERNAL') || die;
29
30require_once($CFG->dirroot.'/course/lib.php');
31
32/**
33 * Add course module.
34 *
35 * The function does not check user capabilities.
36 * The function creates course module, module instance, add the module to the correct section.
37 * It also trigger common action that need to be done after adding/updating a module.
38 *
39 * @param object $moduleinfo the moudle data
40 * @param object $course the course of the module
41 * @param object $mform this is required by an existing hack to deal with files during MODULENAME_add_instance()
42 * @return object the updated module info
43 */
44function add_moduleinfo($moduleinfo, $course, $mform = null) {
45 global $DB, $CFG;
46
b4b75872
MG
47 // Attempt to include module library before we make any changes to DB.
48 include_modulelib($moduleinfo->modulename);
49
80fe0c19
JM
50 $moduleinfo->course = $course->id;
51 $moduleinfo = set_moduleinfo_defaults($moduleinfo);
52
53 if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
54 $moduleinfo->groupmode = 0; // Do not set groupmode.
55 }
56
8f2913b9 57 // First add course_module record because we need the context.
80fe0c19
JM
58 $newcm = new stdClass();
59 $newcm->course = $course->id;
60 $newcm->module = $moduleinfo->module;
61 $newcm->instance = 0; // Not known yet, will be updated later (this is similar to restore code).
62 $newcm->visible = $moduleinfo->visible;
7c69ab5d 63 $newcm->visibleold = $moduleinfo->visible;
449d1fa3
MG
64 if (isset($moduleinfo->cmidnumber)) {
65 $newcm->idnumber = $moduleinfo->cmidnumber;
66 }
80fe0c19
JM
67 $newcm->groupmode = $moduleinfo->groupmode;
68 $newcm->groupingid = $moduleinfo->groupingid;
80fe0c19
JM
69 $completion = new completion_info($course);
70 if ($completion->is_enabled()) {
71 $newcm->completion = $moduleinfo->completion;
72 $newcm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
73 $newcm->completionview = $moduleinfo->completionview;
74 $newcm->completionexpected = $moduleinfo->completionexpected;
75 }
76 if(!empty($CFG->enableavailability)) {
400c0fd2 77 // This code is used both when submitting the form, which uses a long
78 // name to avoid clashes, and by unit test code which uses the real
79 // name in the table.
80 $newcm->availability = null;
81 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
82 if ($moduleinfo->availabilityconditionsjson !== '') {
83 $newcm->availability = $moduleinfo->availabilityconditionsjson;
84 }
85 } else if (property_exists($moduleinfo, 'availability')) {
86 $newcm->availability = $moduleinfo->availability;
87 }
06c06038 88 // If there is any availability data, verify it.
89 if ($newcm->availability) {
90 $tree = new \core_availability\tree(json_decode($newcm->availability));
91 // Save time and database space by setting null if the only data
92 // is an empty tree.
93 if ($tree->is_empty()) {
94 $newcm->availability = null;
95 }
96 }
80fe0c19
JM
97 }
98 if (isset($moduleinfo->showdescription)) {
99 $newcm->showdescription = $moduleinfo->showdescription;
100 } else {
101 $newcm->showdescription = 0;
102 }
103
5c3a54c4 104 // From this point we make database changes, so start transaction.
105 $transaction = $DB->start_delegated_transaction();
106
80fe0c19
JM
107 if (!$moduleinfo->coursemodule = add_course_module($newcm)) {
108 print_error('cannotaddcoursemodule');
109 }
110
7fbe33fc
MG
111 if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true) &&
112 isset($moduleinfo->introeditor)) {
80fe0c19
JM
113 $introeditor = $moduleinfo->introeditor;
114 unset($moduleinfo->introeditor);
115 $moduleinfo->intro = $introeditor['text'];
116 $moduleinfo->introformat = $introeditor['format'];
117 }
118
119 $addinstancefunction = $moduleinfo->modulename."_add_instance";
5c3a54c4 120 try {
121 $returnfromfunc = $addinstancefunction($moduleinfo, $mform);
122 } catch (moodle_exception $e) {
123 $returnfromfunc = $e;
124 }
80fe0c19 125 if (!$returnfromfunc or !is_number($returnfromfunc)) {
5c3a54c4 126 // Undo everything we can. This is not necessary for databases which
127 // support transactions, but improves consistency for other databases.
80fe0c19 128 $modcontext = context_module::instance($moduleinfo->coursemodule);
c592eea2 129 context_helper::delete_instance(CONTEXT_MODULE, $moduleinfo->coursemodule);
80fe0c19
JM
130 $DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule));
131
5c3a54c4 132 if ($e instanceof moodle_exception) {
133 throw $e;
134 } else if (!is_number($returnfromfunc)) {
b4b75872 135 print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section));
80fe0c19 136 } else {
b4b75872 137 print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section), $moduleinfo->modulename);
80fe0c19
JM
138 }
139 }
140
141 $moduleinfo->instance = $returnfromfunc;
142
143 $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$moduleinfo->coursemodule));
144
145 // Update embedded links and save files.
146 $modcontext = context_module::instance($moduleinfo->coursemodule);
147 if (!empty($introeditor)) {
148 $moduleinfo->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,
149 'mod_'.$moduleinfo->modulename, 'intro', 0,
150 array('subdirs'=>true), $introeditor['text']);
151 $DB->set_field($moduleinfo->modulename, 'intro', $moduleinfo->intro, array('id'=>$moduleinfo->instance));
152 }
153
dffcf46f
NK
154 // Add module tags.
155 if (core_tag_tag::is_enabled('core', 'course_modules') && isset($moduleinfo->tags)) {
156 core_tag_tag::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule, $modcontext, $moduleinfo->tags);
157 }
158
80fe0c19
JM
159 // Course_modules and course_sections each contain a reference to each other.
160 // So we have to update one of them twice.
161 $sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule, $moduleinfo->section);
162
63deb5c3 163 // Trigger event based on the action we did.
e20d3069
AA
164 // Api create_from_cm expects modname and id property, and we don't want to modify $moduleinfo since we are returning it.
165 $eventdata = clone $moduleinfo;
166 $eventdata->modname = $eventdata->modulename;
167 $eventdata->id = $eventdata->coursemodule;
168 $event = \core\event\course_module_created::create_from_cm($eventdata, $modcontext);
63deb5c3 169 $event->trigger();
80fe0c19 170
63deb5c3 171 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
5c3a54c4 172 $transaction->allow_commit();
80fe0c19
JM
173
174 return $moduleinfo;
175}
176
177
178/**
179 * Common create/update module module actions that need to be processed as soon as a module is created/updaded.
63deb5c3
AA
180 * For example:create grade parent category, add outcomes, rebuild caches, regrade, save plagiarism settings...
181 * Please note this api does not trigger events as of MOODLE 2.6. Please trigger events before calling this api.
80fe0c19
JM
182 *
183 * @param object $moduleinfo the module info
184 * @param object $course the course of the module
80fe0c19 185 *
63deb5c3 186 * @return object moduleinfo update with grading management info
80fe0c19 187 */
63deb5c3
AA
188function edit_module_post_actions($moduleinfo, $course) {
189 global $CFG;
3e6adca9 190 require_once($CFG->libdir.'/gradelib.php');
80fe0c19
JM
191
192 $modcontext = context_module::instance($moduleinfo->coursemodule);
449d1fa3
MG
193 $hasgrades = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_HAS_GRADE, false);
194 $hasoutcomes = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_OUTCOMES, true);
80fe0c19 195
80fe0c19 196 // Sync idnumber with grade_item.
449d1fa3 197 if ($hasgrades && $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$moduleinfo->modulename,
80fe0c19 198 'iteminstance'=>$moduleinfo->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
8164fad4 199 $gradeupdate = false;
80fe0c19
JM
200 if ($grade_item->idnumber != $moduleinfo->cmidnumber) {
201 $grade_item->idnumber = $moduleinfo->cmidnumber;
8164fad4
GF
202 $gradeupdate = true;
203 }
204 if (isset($moduleinfo->gradepass) && $grade_item->gradepass != $moduleinfo->gradepass) {
205 $grade_item->gradepass = $moduleinfo->gradepass;
206 $gradeupdate = true;
207 }
208 if ($gradeupdate) {
80fe0c19
JM
209 $grade_item->update();
210 }
211 }
212
449d1fa3
MG
213 if ($hasgrades) {
214 $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$moduleinfo->modulename,
80fe0c19 215 'iteminstance'=>$moduleinfo->instance, 'courseid'=>$course->id));
449d1fa3
MG
216 } else {
217 $items = array();
218 }
80fe0c19
JM
219
220 // Create parent category if requested and move to correct parent category.
221 if ($items and isset($moduleinfo->gradecat)) {
222 if ($moduleinfo->gradecat == -1) {
223 $grade_category = new grade_category();
224 $grade_category->courseid = $course->id;
225 $grade_category->fullname = $moduleinfo->name;
226 $grade_category->insert();
227 if ($grade_item) {
228 $parent = $grade_item->get_parent_category();
229 $grade_category->set_parent($parent->id);
230 }
231 $moduleinfo->gradecat = $grade_category->id;
232 }
595b911a 233
80fe0c19
JM
234 foreach ($items as $itemid=>$unused) {
235 $items[$itemid]->set_parent($moduleinfo->gradecat);
236 if ($itemid == $grade_item->id) {
237 // Use updated grade_item.
238 $grade_item = $items[$itemid];
239 }
240 }
241 }
242
2cd29dd6 243 require_once($CFG->libdir.'/grade/grade_outcome.php');
80fe0c19 244 // Add outcomes if requested.
449d1fa3 245 if ($hasoutcomes && $outcomes = grade_outcome::fetch_all_available($course->id)) {
80fe0c19
JM
246 $grade_items = array();
247
248 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes.
249 $max_itemnumber = 999;
250 if ($items) {
251 foreach($items as $item) {
252 if ($item->itemnumber > $max_itemnumber) {
253 $max_itemnumber = $item->itemnumber;
254 }
255 }
256 }
257
258 foreach($outcomes as $outcome) {
259 $elname = 'outcome_'.$outcome->id;
260
261 if (property_exists($moduleinfo, $elname) and $moduleinfo->$elname) {
262 // So we have a request for new outcome grade item?
263 if ($items) {
3b2a6bf5 264 $outcomeexists = false;
80fe0c19
JM
265 foreach($items as $item) {
266 if ($item->outcomeid == $outcome->id) {
3b2a6bf5
DW
267 $outcomeexists = true;
268 break;
80fe0c19
JM
269 }
270 }
3b2a6bf5
DW
271 if ($outcomeexists) {
272 continue;
273 }
80fe0c19
JM
274 }
275
276 $max_itemnumber++;
277
278 $outcome_item = new grade_item();
279 $outcome_item->courseid = $course->id;
280 $outcome_item->itemtype = 'mod';
281 $outcome_item->itemmodule = $moduleinfo->modulename;
282 $outcome_item->iteminstance = $moduleinfo->instance;
283 $outcome_item->itemnumber = $max_itemnumber;
284 $outcome_item->itemname = $outcome->fullname;
285 $outcome_item->outcomeid = $outcome->id;
286 $outcome_item->gradetype = GRADE_TYPE_SCALE;
287 $outcome_item->scaleid = $outcome->scaleid;
288 $outcome_item->insert();
289
290 // Move the new outcome into correct category and fix sortorder if needed.
291 if ($grade_item) {
292 $outcome_item->set_parent($grade_item->categoryid);
293 $outcome_item->move_after_sortorder($grade_item->sortorder);
294
295 } else if (isset($moduleinfo->gradecat)) {
296 $outcome_item->set_parent($moduleinfo->gradecat);
297 }
298 }
299 }
300 }
301
302 if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_ADVANCED_GRADING, false)
303 and has_capability('moodle/grade:managegradingforms', $modcontext)) {
304 require_once($CFG->dirroot.'/grade/grading/lib.php');
305 $gradingman = get_grading_manager($modcontext, 'mod_'.$moduleinfo->modulename);
306 $showgradingmanagement = false;
307 foreach ($gradingman->get_available_areas() as $areaname => $aretitle) {
308 $formfield = 'advancedgradingmethod_'.$areaname;
309 if (isset($moduleinfo->{$formfield})) {
310 $gradingman->set_area($areaname);
311 $methodchanged = $gradingman->set_active_method($moduleinfo->{$formfield});
312 if (empty($moduleinfo->{$formfield})) {
313 // Going back to the simple direct grading is not a reason to open the management screen.
314 $methodchanged = false;
315 }
316 $showgradingmanagement = $showgradingmanagement || $methodchanged;
317 }
318 }
319 // Update grading management information.
320 $moduleinfo->gradingman = $gradingman;
321 $moduleinfo->showgradingmanagement = $showgradingmanagement;
322 }
323
449d1fa3
MG
324 rebuild_course_cache($course->id, true);
325 if ($hasgrades) {
326 grade_regrade_final_grades($course->id);
327 }
80fe0c19 328 require_once($CFG->libdir.'/plagiarismlib.php');
8f2913b9 329 plagiarism_save_form_elements($moduleinfo);
80fe0c19
JM
330
331 return $moduleinfo;
332}
333
334
335/**
336 * Set module info default values for the unset module attributs.
337 *
338 * @param object $moduleinfo the current known data of the module
339 * @return object the completed module info
340 */
341function set_moduleinfo_defaults($moduleinfo) {
80fe0c19
JM
342
343 if (empty($moduleinfo->coursemodule)) {
344 // Add.
345 $cm = null;
346 $moduleinfo->instance = '';
347 $moduleinfo->coursemodule = '';
348 } else {
349 // Update.
350 $cm = get_coursemodule_from_id('', $moduleinfo->coursemodule, 0, false, MUST_EXIST);
351 $moduleinfo->instance = $cm->instance;
352 $moduleinfo->coursemodule = $cm->id;
353 }
8f2913b9
DW
354 // For safety.
355 $moduleinfo->modulename = clean_param($moduleinfo->modulename, PARAM_PLUGIN);
80fe0c19
JM
356
357 if (!isset($moduleinfo->groupingid)) {
358 $moduleinfo->groupingid = 0;
359 }
360
80fe0c19
JM
361 if (!isset($moduleinfo->name)) { // Label.
362 $moduleinfo->name = $moduleinfo->modulename;
363 }
364
365 if (!isset($moduleinfo->completion)) {
366 $moduleinfo->completion = COMPLETION_DISABLED;
367 }
368 if (!isset($moduleinfo->completionview)) {
369 $moduleinfo->completionview = COMPLETION_VIEW_NOT_REQUIRED;
370 }
6f9354be
MG
371 if (!isset($moduleinfo->completionexpected)) {
372 $moduleinfo->completionexpected = 0;
373 }
80fe0c19
JM
374
375 // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
376 if (isset($moduleinfo->completionusegrade) && $moduleinfo->completionusegrade) {
377 $moduleinfo->completiongradeitemnumber = 0;
378 } else {
379 $moduleinfo->completiongradeitemnumber = null;
380 }
381
6f9354be
MG
382 if (!isset($moduleinfo->conditiongradegroup)) {
383 $moduleinfo->conditiongradegroup = array();
384 }
385 if (!isset($moduleinfo->conditionfieldgroup)) {
386 $moduleinfo->conditionfieldgroup = array();
387 }
388
80fe0c19
JM
389 return $moduleinfo;
390}
391
392/**
393 * Check that the user can add a module. Also returns some information like the module, context and course section info.
394 * The fucntion create the course section if it doesn't exist.
395 *
396 * @param object $course the course of the module
397 * @param object $modulename the module name
398 * @param object $section the section of the module
399 * @return array list containing module, context, course section.
b4b75872 400 * @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
80fe0c19
JM
401 */
402function can_add_moduleinfo($course, $modulename, $section) {
403 global $DB;
404
405 $module = $DB->get_record('modules', array('name'=>$modulename), '*', MUST_EXIST);
406
407 $context = context_course::instance($course->id);
408 require_capability('moodle/course:manageactivities', $context);
409
410 course_create_sections_if_missing($course, $section);
411 $cw = get_fast_modinfo($course)->get_section_info($section);
412
413 if (!course_allowed_module($course, $module->name)) {
414 print_error('moduledisable');
415 }
416
417 return array($module, $context, $cw);
418}
419
420/**
421 * Check if user is allowed to update module info and returns related item/data to the module.
422 *
423 * @param object $cm course module
424 * @return array - list of course module, context, module, moduleinfo, and course section.
b4b75872 425 * @throws moodle_exception if user is not allowed to perform the action
80fe0c19
JM
426 */
427function can_update_moduleinfo($cm) {
428 global $DB;
429
430 // Check the $USER has the right capability.
431 $context = context_module::instance($cm->id);
432 require_capability('moodle/course:manageactivities', $context);
433
434 // Check module exists.
435 $module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST);
436
437 // Check the moduleinfo exists.
438 $data = $DB->get_record($module->name, array('id'=>$cm->instance), '*', MUST_EXIST);
439
440 // Check the course section exists.
441 $cw = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
442
443 return array($cm, $context, $module, $data, $cw);
444}
445
446
447/**
448 * Update the module info.
449 * This function doesn't check the user capabilities. It updates the course module and the module instance.
450 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
451 *
452 * @param object $cm course module
453 * @param object $moduleinfo module info
454 * @param object $course course of the module
455 * @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.
456 * @return array list of course module and module info.
457 */
458function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
459 global $DB, $CFG;
460
d629c601
DW
461 $data = new stdClass();
462 if ($mform) {
463 $data = $mform->get_data();
464 }
465
b4b75872
MG
466 // Attempt to include module library before we make any changes to DB.
467 include_modulelib($moduleinfo->modulename);
468
80fe0c19
JM
469 $moduleinfo->course = $course->id;
470 $moduleinfo = set_moduleinfo_defaults($moduleinfo);
471
472 if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
473 $moduleinfo->groupmode = $cm->groupmode; // Keep original.
474 }
475
476 // Update course module first.
477 $cm->groupmode = $moduleinfo->groupmode;
478 if (isset($moduleinfo->groupingid)) {
479 $cm->groupingid = $moduleinfo->groupingid;
480 }
80fe0c19
JM
481
482 $completion = new completion_info($course);
af729c3f 483 if ($completion->is_enabled()) {
484 // Completion settings that would affect users who have already completed
485 // the activity may be locked; if so, these should not be updated.
486 if (!empty($moduleinfo->completionunlocked)) {
487 $cm->completion = $moduleinfo->completion;
488 $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
489 $cm->completionview = $moduleinfo->completionview;
490 }
491 // The expected date does not affect users who have completed the activity,
492 // so it is safe to update it regardless of the lock status.
493 $cm->completionexpected = $moduleinfo->completionexpected;
80fe0c19
JM
494 }
495 if (!empty($CFG->enableavailability)) {
400c0fd2 496 // This code is used both when submitting the form, which uses a long
497 // name to avoid clashes, and by unit test code which uses the real
498 // name in the table.
499 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
500 if ($moduleinfo->availabilityconditionsjson !== '') {
501 $cm->availability = $moduleinfo->availabilityconditionsjson;
502 } else {
503 $cm->availability = null;
504 }
505 } else if (property_exists($moduleinfo, 'availability')) {
506 $cm->availability = $moduleinfo->availability;
507 }
06c06038 508 // If there is any availability data, verify it.
509 if ($cm->availability) {
510 $tree = new \core_availability\tree(json_decode($cm->availability));
511 // Save time and database space by setting null if the only data
512 // is an empty tree.
513 if ($tree->is_empty()) {
514 $cm->availability = null;
515 }
516 }
80fe0c19
JM
517 }
518 if (isset($moduleinfo->showdescription)) {
519 $cm->showdescription = $moduleinfo->showdescription;
520 } else {
521 $cm->showdescription = 0;
522 }
523
8f2913b9 524 $DB->update_record('course_modules', $cm);
80fe0c19 525
8f2913b9 526 $modcontext = context_module::instance($moduleinfo->coursemodule);
80fe0c19 527
8f2913b9
DW
528 // Update embedded links and save files.
529 if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
530 $moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id,
531 'mod_'.$moduleinfo->modulename, 'intro', 0,
532 array('subdirs'=>true), $moduleinfo->introeditor['text']);
533 $moduleinfo->introformat = $moduleinfo->introeditor['format'];
534 unset($moduleinfo->introeditor);
535 }
d629c601
DW
536 // Get the a copy of the grade_item before it is modified incase we need to scale the grades.
537 $oldgradeitem = null;
538 $newgradeitem = null;
e7c71c18 539 if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') {
d629c601
DW
540 // Fetch the grade item before it is updated.
541 $oldgradeitem = grade_item::fetch(array('itemtype' => 'mod',
542 'itemmodule' => $moduleinfo->modulename,
543 'iteminstance' => $moduleinfo->instance,
544 'itemnumber' => 0,
545 'courseid' => $moduleinfo->course));
546 }
547
8f2913b9
DW
548 $updateinstancefunction = $moduleinfo->modulename."_update_instance";
549 if (!$updateinstancefunction($moduleinfo, $mform)) {
d629c601
DW
550 print_error('cannotupdatemod', '', course_get_url($course, $cm->section), $moduleinfo->modulename);
551 }
552
553 // This needs to happen AFTER the grademin/grademax have already been updated.
e7c71c18 554 if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') {
d629c601
DW
555 // Get the grade_item after the update call the activity to scale the grades.
556 $newgradeitem = grade_item::fetch(array('itemtype' => 'mod',
557 'itemmodule' => $moduleinfo->modulename,
558 'iteminstance' => $moduleinfo->instance,
559 'itemnumber' => 0,
560 'courseid' => $moduleinfo->course));
561 if ($newgradeitem && $oldgradeitem->gradetype == GRADE_TYPE_VALUE && $newgradeitem->gradetype == GRADE_TYPE_VALUE) {
562 $params = array(
563 $course,
564 $cm,
565 $oldgradeitem->grademin,
566 $oldgradeitem->grademax,
567 $newgradeitem->grademin,
568 $newgradeitem->grademax
569 );
570 if (!component_callback('mod_' . $moduleinfo->modulename, 'rescale_activity_grades', $params)) {
571 print_error('cannotreprocessgrades', '', course_get_url($course, $cm->section), $moduleinfo->modulename);
572 }
573 }
8f2913b9 574 }
80fe0c19 575
8f2913b9
DW
576 // Make sure visibility is set correctly (in particular in calendar).
577 if (has_capability('moodle/course:activityvisibility', $modcontext)) {
578 set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
579 }
80fe0c19 580
8f2913b9
DW
581 if (isset($moduleinfo->cmidnumber)) { // Label.
582 // Set cm idnumber - uniqueness is already verified by form validation.
583 set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
584 }
80fe0c19 585
dffcf46f
NK
586 // Update module tags.
587 if (core_tag_tag::is_enabled('core', 'course_modules') && isset($moduleinfo->tags)) {
588 core_tag_tag::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule, $modcontext, $moduleinfo->tags);
589 }
590
8f2913b9
DW
591 // Now that module is fully updated, also update completion data if required.
592 // (this will wipe all user completion data and recalculate it)
593 if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
594 $completion->reset_all_state($cm);
595 }
9e533215
SL
596 $cm->name = $moduleinfo->name;
597 \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
63deb5c3 598
63deb5c3 599 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
80fe0c19 600
8f2913b9 601 return array($cm, $moduleinfo);
80fe0c19
JM
602}
603
604/**
605 * Include once the module lib file.
606 *
607 * @param string $modulename module name of the lib to include
b4b75872 608 * @throws moodle_exception if lib.php file for the module does not exist
80fe0c19
JM
609 */
610function include_modulelib($modulename) {
611 global $CFG;
612 $modlib = "$CFG->dirroot/mod/$modulename/lib.php";
613 if (file_exists($modlib)) {
614 include_once($modlib);
615 } else {
616 throw new moodle_exception('modulemissingcode', '', '', $modlib);
617 }
618}
619