2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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.
23 * @copyright 2013 Jerome Mouneyrac
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 * @package core_course
28 defined('MOODLE_INTERNAL') ||
die;
30 use \core_grades\component_gradeitems
;
32 require_once($CFG->dirroot
.'/course/lib.php');
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.
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
46 function add_moduleinfo($moduleinfo, $course, $mform = null) {
49 // Attempt to include module library before we make any changes to DB.
50 include_modulelib($moduleinfo->modulename
);
52 $moduleinfo->course
= $course->id
;
53 $moduleinfo = set_moduleinfo_defaults($moduleinfo);
55 if (!empty($course->groupmodeforce
) or !isset($moduleinfo->groupmode
)) {
56 $moduleinfo->groupmode
= 0; // Do not set groupmode.
59 // First add course_module record because we need the context.
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
;
65 $newcm->visibleold
= $moduleinfo->visible
;
66 $newcm->visibleoncoursepage
= $moduleinfo->visibleoncoursepage
;
67 if (isset($moduleinfo->cmidnumber
)) {
68 $newcm->idnumber
= $moduleinfo->cmidnumber
;
70 if (isset($moduleinfo->downloadcontent
)) {
71 $newcm->downloadcontent
= $moduleinfo->downloadcontent
;
73 $newcm->groupmode
= $moduleinfo->groupmode
;
74 $newcm->groupingid
= $moduleinfo->groupingid
;
75 $completion = new completion_info($course);
76 if ($completion->is_enabled()) {
77 $newcm->completion
= $moduleinfo->completion
;
78 $newcm->completionpassgrade
= $moduleinfo->completionpassgrade ??
0;
79 if ($moduleinfo->completiongradeitemnumber
=== '') {
80 $newcm->completiongradeitemnumber
= null;
82 $newcm->completiongradeitemnumber
= $moduleinfo->completiongradeitemnumber
;
84 $newcm->completionview
= $moduleinfo->completionview
;
85 $newcm->completionexpected
= $moduleinfo->completionexpected
;
87 if(!empty($CFG->enableavailability
)) {
88 // This code is used both when submitting the form, which uses a long
89 // name to avoid clashes, and by unit test code which uses the real
91 $newcm->availability
= null;
92 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
93 if ($moduleinfo->availabilityconditionsjson
!== '') {
94 $newcm->availability
= $moduleinfo->availabilityconditionsjson
;
96 } else if (property_exists($moduleinfo, 'availability')) {
97 $newcm->availability
= $moduleinfo->availability
;
99 // If there is any availability data, verify it.
100 if ($newcm->availability
) {
101 $tree = new \core_availability\tree
(json_decode($newcm->availability
));
102 // Save time and database space by setting null if the only data
104 if ($tree->is_empty()) {
105 $newcm->availability
= null;
109 if (isset($moduleinfo->showdescription
)) {
110 $newcm->showdescription
= $moduleinfo->showdescription
;
112 $newcm->showdescription
= 0;
115 // From this point we make database changes, so start transaction.
116 $transaction = $DB->start_delegated_transaction();
118 if (!$moduleinfo->coursemodule
= add_course_module($newcm)) {
119 print_error('cannotaddcoursemodule');
122 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_MOD_INTRO
, true) &&
123 isset($moduleinfo->introeditor
)) {
124 $introeditor = $moduleinfo->introeditor
;
125 unset($moduleinfo->introeditor
);
126 $moduleinfo->intro
= $introeditor['text'];
127 $moduleinfo->introformat
= $introeditor['format'];
130 $addinstancefunction = $moduleinfo->modulename
."_add_instance";
132 $returnfromfunc = $addinstancefunction($moduleinfo, $mform);
133 } catch (moodle_exception
$e) {
134 $returnfromfunc = $e;
136 if (!$returnfromfunc or !is_number($returnfromfunc)) {
137 // Undo everything we can. This is not necessary for databases which
138 // support transactions, but improves consistency for other databases.
139 context_helper
::delete_instance(CONTEXT_MODULE
, $moduleinfo->coursemodule
);
140 $DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule
));
142 if ($returnfromfunc instanceof moodle_exception
) {
143 throw $returnfromfunc;
144 } else if (!is_number($returnfromfunc)) {
145 print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section
));
147 print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section
), $moduleinfo->modulename
);
151 $moduleinfo->instance
= $returnfromfunc;
153 $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$moduleinfo->coursemodule
));
155 // Update embedded links and save files.
156 $modcontext = context_module
::instance($moduleinfo->coursemodule
);
157 if (!empty($introeditor)) {
158 // This will respect a module that has set a value for intro in it's modname_add_instance() function.
159 $introeditor['text'] = $moduleinfo->intro
;
161 $moduleinfo->intro
= file_save_draft_area_files($introeditor['itemid'], $modcontext->id
,
162 'mod_'.$moduleinfo->modulename
, 'intro', 0,
163 array('subdirs'=>true), $introeditor['text']);
164 $DB->set_field($moduleinfo->modulename
, 'intro', $moduleinfo->intro
, array('id'=>$moduleinfo->instance
));
168 if (core_tag_tag
::is_enabled('core', 'course_modules') && isset($moduleinfo->tags
)) {
169 core_tag_tag
::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule
, $modcontext, $moduleinfo->tags
);
172 // Course_modules and course_sections each contain a reference to each other.
173 // So we have to update one of them twice.
174 $sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule
, $moduleinfo->section
);
176 // Trigger event based on the action we did.
177 // Api create_from_cm expects modname and id property, and we don't want to modify $moduleinfo since we are returning it.
178 $eventdata = clone $moduleinfo;
179 $eventdata->modname
= $eventdata->modulename
;
180 $eventdata->id
= $eventdata->coursemodule
;
181 $event = \core\event\course_module_created
::create_from_cm($eventdata, $modcontext);
184 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
185 $transaction->allow_commit();
191 * Hook for plugins to take action when a module is created or updated.
193 * @param stdClass $moduleinfo the module info
194 * @param stdClass $course the course of the module
196 * @return stdClass moduleinfo updated by plugins.
198 function plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course) {
199 $callbacks = get_plugins_with_function('coursemodule_edit_post_actions', 'lib.php');
200 foreach ($callbacks as $type => $plugins) {
201 foreach ($plugins as $plugin => $pluginfunction) {
202 $moduleinfo = $pluginfunction($moduleinfo, $course);
209 * Common create/update module module actions that need to be processed as soon as a module is created/updaded.
210 * For example:create grade parent category, add outcomes, rebuild caches, regrade, save plagiarism settings...
211 * Please note this api does not trigger events as of MOODLE 2.6. Please trigger events before calling this api.
213 * @param object $moduleinfo the module info
214 * @param object $course the course of the module
216 * @return object moduleinfo update with grading management info
218 function edit_module_post_actions($moduleinfo, $course) {
220 require_once($CFG->libdir
.'/gradelib.php');
222 $modcontext = context_module
::instance($moduleinfo->coursemodule
);
223 $hasgrades = plugin_supports('mod', $moduleinfo->modulename
, FEATURE_GRADE_HAS_GRADE
, false);
224 $hasoutcomes = plugin_supports('mod', $moduleinfo->modulename
, FEATURE_GRADE_OUTCOMES
, true);
226 $items = grade_item
::fetch_all([
228 'itemmodule' => $moduleinfo->modulename
,
229 'iteminstance' => $moduleinfo->instance
,
230 'courseid' => $course->id
,
233 // Create parent category if requested and move to correct parent category.
234 $component = "mod_{$moduleinfo->modulename}";
236 foreach ($items as $item) {
239 // Sync idnumber with grade_item.
240 // Note: This only happens for itemnumber 0 at this time.
241 if ($item->itemnumber
== 0 && ($item->idnumber
!= $moduleinfo->cmidnumber
)) {
242 $item->idnumber
= $moduleinfo->cmidnumber
;
246 // Determine the grade category.
247 $gradecatfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $item->itemnumber
, 'gradecat');
248 if (property_exists($moduleinfo, $gradecatfieldname)) {
249 $gradecat = $moduleinfo->$gradecatfieldname;
250 if ($gradecat == -1) {
251 $gradecategory = new grade_category();
252 $gradecategory->courseid
= $course->id
;
253 $gradecategory->fullname
= $moduleinfo->name
;
254 $gradecategory->insert();
256 $parent = $item->get_parent_category();
257 $gradecategory->set_parent($parent->id
);
258 $gradecat = $gradecategory->id
;
262 if ($parent = $item->get_parent_category()) {
263 $oldgradecat = $parent->id
;
265 if ($oldgradecat != $gradecat) {
266 $item->set_parent($gradecat);
271 // Determine the gradepass.
272 $gradepassfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $item->itemnumber
, 'gradepass');
273 if (isset($moduleinfo->{$gradepassfieldname})) {
274 $gradepass = $moduleinfo->{$gradepassfieldname};
275 if (null !== $gradepass && $gradepass != $item->gradepass
) {
276 $item->gradepass
= $gradepass;
285 if (!empty($moduleinfo->add
)) {
286 $gradecategory = $item->get_parent_category();
287 if ($item->set_aggregation_fields_for_aggregation(0, $gradecategory->aggregation
)) {
294 require_once($CFG->libdir
.'/grade/grade_outcome.php');
295 // Add outcomes if requested.
296 if ($hasoutcomes && $outcomes = grade_outcome
::fetch_all_available($course->id
)) {
297 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes.
298 $max_itemnumber = 999;
300 foreach($items as $item) {
301 if ($item->itemnumber
> $max_itemnumber) {
302 $max_itemnumber = $item->itemnumber
;
307 foreach($outcomes as $outcome) {
308 $elname = 'outcome_'.$outcome->id
;
310 if (property_exists($moduleinfo, $elname) and $moduleinfo->$elname) {
311 // Check if this is a new outcome grade item.
312 $outcomeexists = false;
314 foreach($items as $item) {
315 if ($item->outcomeid
== $outcome->id
) {
316 $outcomeexists = true;
320 if ($outcomeexists) {
327 $outcomeitem = new grade_item();
328 $outcomeitem->courseid
= $course->id
;
329 $outcomeitem->itemtype
= 'mod';
330 $outcomeitem->itemmodule
= $moduleinfo->modulename
;
331 $outcomeitem->iteminstance
= $moduleinfo->instance
;
332 $outcomeitem->itemnumber
= $max_itemnumber;
333 $outcomeitem->itemname
= $outcome->fullname
;
334 $outcomeitem->outcomeid
= $outcome->id
;
335 $outcomeitem->gradetype
= GRADE_TYPE_SCALE
;
336 $outcomeitem->scaleid
= $outcome->scaleid
;
337 $outcomeitem->insert();
340 // Move the new outcome into the same category and immediately after the first grade item.
341 $item = reset($items);
342 $outcomeitem->set_parent($item->categoryid
);
343 $outcomeitem->move_after_sortorder($item->sortorder
);
344 } else if (isset($moduleinfo->gradecat
)) {
345 $outcomeitem->set_parent($moduleinfo->gradecat
);
348 if (!$outcomeexists) {
349 $gradecategory = $outcomeitem->get_parent_category();
350 if ($outcomeitem->set_aggregation_fields_for_aggregation(0, $gradecategory->aggregation
)) {
351 $outcomeitem->update();
358 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_ADVANCED_GRADING
, false)
359 and has_capability('moodle/grade:managegradingforms', $modcontext)) {
360 require_once($CFG->dirroot
.'/grade/grading/lib.php');
361 $gradingman = get_grading_manager($modcontext, 'mod_'.$moduleinfo->modulename
);
362 $showgradingmanagement = false;
363 foreach ($gradingman->get_available_areas() as $areaname => $aretitle) {
364 $formfield = 'advancedgradingmethod_'.$areaname;
365 if (isset($moduleinfo->{$formfield})) {
366 $gradingman->set_area($areaname);
367 $methodchanged = $gradingman->set_active_method($moduleinfo->{$formfield});
368 if (empty($moduleinfo->{$formfield})) {
369 // Going back to the simple direct grading is not a reason to open the management screen.
370 $methodchanged = false;
372 $showgradingmanagement = $showgradingmanagement ||
$methodchanged;
375 // Update grading management information.
376 $moduleinfo->gradingman
= $gradingman;
377 $moduleinfo->showgradingmanagement
= $showgradingmanagement;
380 \course_modinfo
::purge_course_module_cache($course->id
, $moduleinfo->coursemodule
);
381 rebuild_course_cache($course->id
, true, true);
383 grade_regrade_final_grades($course->id
);
386 // To be removed (deprecated) with MDL-67526 (both lines).
387 require_once($CFG->libdir
.'/plagiarismlib.php');
388 plagiarism_save_form_elements($moduleinfo);
390 // Allow plugins to extend the course module form.
391 $moduleinfo = plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course);
393 if (!empty($moduleinfo->coursecontentnotification
)) {
394 // Schedule adhoc-task for delivering the course content updated notification.
395 if ($course->visible
&& $moduleinfo->visible
) {
396 $adhocktask = new \core_course\task\
content_notification_task();
397 $adhocktask->set_custom_data(
398 ['update' => $moduleinfo->update
, 'cmid' => $moduleinfo->coursemodule
,
399 'courseid' => $course->id
, 'userfrom' => $USER->id
]);
400 $adhocktask->set_component('course');
401 \core\task\manager
::queue_adhoc_task($adhocktask, true);
409 * Set module info default values for the unset module attributs.
411 * @param object $moduleinfo the current known data of the module
412 * @return object the completed module info
414 function set_moduleinfo_defaults($moduleinfo) {
416 if (empty($moduleinfo->coursemodule
)) {
419 $moduleinfo->instance
= '';
420 $moduleinfo->coursemodule
= '';
423 $cm = get_coursemodule_from_id('', $moduleinfo->coursemodule
, 0, false, MUST_EXIST
);
424 $moduleinfo->instance
= $cm->instance
;
425 $moduleinfo->coursemodule
= $cm->id
;
428 $moduleinfo->modulename
= clean_param($moduleinfo->modulename
, PARAM_PLUGIN
);
430 if (!isset($moduleinfo->groupingid
)) {
431 $moduleinfo->groupingid
= 0;
434 if (!isset($moduleinfo->name
)) { // Label.
435 $moduleinfo->name
= $moduleinfo->modulename
;
438 if (!isset($moduleinfo->completion
)) {
439 $moduleinfo->completion
= COMPLETION_DISABLED
;
441 if (!isset($moduleinfo->completionview
)) {
442 $moduleinfo->completionview
= COMPLETION_VIEW_NOT_REQUIRED
;
444 if (!isset($moduleinfo->completionexpected
)) {
445 $moduleinfo->completionexpected
= 0;
448 // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
449 if (isset($moduleinfo->completionusegrade
) && $moduleinfo->completionusegrade
) {
450 $moduleinfo->completiongradeitemnumber
= 0;
451 } else if (!isset($moduleinfo->completiongradeitemnumber
)) {
452 // If there is no gradeitemnumber set, make sure to disable completionpassgrade.
453 $moduleinfo->completionpassgrade
= 0;
454 $moduleinfo->completiongradeitemnumber
= null;
457 if (!isset($moduleinfo->conditiongradegroup
)) {
458 $moduleinfo->conditiongradegroup
= array();
460 if (!isset($moduleinfo->conditionfieldgroup
)) {
461 $moduleinfo->conditionfieldgroup
= array();
463 if (!isset($moduleinfo->visibleoncoursepage
)) {
464 $moduleinfo->visibleoncoursepage
= 1;
467 if (!isset($moduleinfo->downloadcontent
)) {
468 $moduleinfo->downloadcontent
= DOWNLOAD_COURSE_CONTENT_ENABLED
;
475 * Check that the user can add a module. Also returns some information like the module, context and course section info.
476 * The fucntion create the course section if it doesn't exist.
478 * @param object $course the course of the module
479 * @param object $modulename the module name
480 * @param object $section the section of the module
481 * @return array list containing module, context, course section.
482 * @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
484 function can_add_moduleinfo($course, $modulename, $section) {
487 $module = $DB->get_record('modules', array('name'=>$modulename), '*', MUST_EXIST
);
489 $context = context_course
::instance($course->id
);
490 require_capability('moodle/course:manageactivities', $context);
492 course_create_sections_if_missing($course, $section);
493 $cw = get_fast_modinfo($course)->get_section_info($section);
495 if (!course_allowed_module($course, $module->name
)) {
496 print_error('moduledisable');
499 return array($module, $context, $cw);
503 * Check if user is allowed to update module info and returns related item/data to the module.
505 * @param object $cm course module
506 * @return array - list of course module, context, module, moduleinfo, and course section.
507 * @throws moodle_exception if user is not allowed to perform the action
509 function can_update_moduleinfo($cm) {
512 // Check the $USER has the right capability.
513 $context = context_module
::instance($cm->id
);
514 require_capability('moodle/course:manageactivities', $context);
516 // Check module exists.
517 $module = $DB->get_record('modules', array('id'=>$cm->module
), '*', MUST_EXIST
);
519 // Check the moduleinfo exists.
520 $data = $DB->get_record($module->name
, array('id'=>$cm->instance
), '*', MUST_EXIST
);
522 // Check the course section exists.
523 $cw = $DB->get_record('course_sections', array('id'=>$cm->section
), '*', MUST_EXIST
);
525 return array($cm, $context, $module, $data, $cw);
530 * Update the module info.
531 * This function doesn't check the user capabilities. It updates the course module and the module instance.
532 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
534 * @param object $cm course module
535 * @param object $moduleinfo module info
536 * @param object $course course of the module
537 * @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.
538 * @return array list of course module and module info.
540 function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
543 $data = new stdClass();
545 $data = $mform->get_data();
548 // Attempt to include module library before we make any changes to DB.
549 include_modulelib($moduleinfo->modulename
);
551 $moduleinfo->course
= $course->id
;
552 $moduleinfo = set_moduleinfo_defaults($moduleinfo);
554 if (!empty($course->groupmodeforce
) or !isset($moduleinfo->groupmode
)) {
555 $moduleinfo->groupmode
= $cm->groupmode
; // Keep original.
558 // Update course module first.
559 $cm->groupmode
= $moduleinfo->groupmode
;
560 if (isset($moduleinfo->groupingid
)) {
561 $cm->groupingid
= $moduleinfo->groupingid
;
564 $completion = new completion_info($course);
565 if ($completion->is_enabled()) {
566 // Completion settings that would affect users who have already completed
567 // the activity may be locked; if so, these should not be updated.
568 if (!empty($moduleinfo->completionunlocked
)) {
569 $cm->completion
= $moduleinfo->completion
;
570 $cm->completionpassgrade
= $moduleinfo->completionpassgrade ??
0;
571 if ($moduleinfo->completiongradeitemnumber
=== '') {
572 $cm->completiongradeitemnumber
= null;
574 $cm->completiongradeitemnumber
= $moduleinfo->completiongradeitemnumber
;
576 $cm->completionview
= $moduleinfo->completionview
;
578 // The expected date does not affect users who have completed the activity,
579 // so it is safe to update it regardless of the lock status.
580 $cm->completionexpected
= $moduleinfo->completionexpected
;
582 if (!empty($CFG->enableavailability
)) {
583 // This code is used both when submitting the form, which uses a long
584 // name to avoid clashes, and by unit test code which uses the real
585 // name in the table.
586 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
587 if ($moduleinfo->availabilityconditionsjson
!== '') {
588 $cm->availability
= $moduleinfo->availabilityconditionsjson
;
590 $cm->availability
= null;
592 } else if (property_exists($moduleinfo, 'availability')) {
593 $cm->availability
= $moduleinfo->availability
;
595 // If there is any availability data, verify it.
596 if ($cm->availability
) {
597 $tree = new \core_availability\tree
(json_decode($cm->availability
));
598 // Save time and database space by setting null if the only data
600 if ($tree->is_empty()) {
601 $cm->availability
= null;
605 if (isset($moduleinfo->showdescription
)) {
606 $cm->showdescription
= $moduleinfo->showdescription
;
608 $cm->showdescription
= 0;
611 $DB->update_record('course_modules', $cm);
613 $modcontext = context_module
::instance($moduleinfo->coursemodule
);
615 // Update embedded links and save files.
616 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_MOD_INTRO
, true)) {
617 $moduleinfo->intro
= file_save_draft_area_files($moduleinfo->introeditor
['itemid'], $modcontext->id
,
618 'mod_'.$moduleinfo->modulename
, 'intro', 0,
619 array('subdirs'=>true), $moduleinfo->introeditor
['text']);
620 $moduleinfo->introformat
= $moduleinfo->introeditor
['format'];
621 unset($moduleinfo->introeditor
);
623 // Get the a copy of the grade_item before it is modified incase we need to scale the grades.
624 $oldgradeitem = null;
625 $newgradeitem = null;
626 if (!empty($data->grade_rescalegrades
) && $data->grade_rescalegrades
== 'yes') {
627 // Fetch the grade item before it is updated.
628 $oldgradeitem = grade_item
::fetch(array('itemtype' => 'mod',
629 'itemmodule' => $moduleinfo->modulename
,
630 'iteminstance' => $moduleinfo->instance
,
632 'courseid' => $moduleinfo->course
));
635 $updateinstancefunction = $moduleinfo->modulename
."_update_instance";
636 if (!$updateinstancefunction($moduleinfo, $mform)) {
637 print_error('cannotupdatemod', '', course_get_url($course, $cm->section
), $moduleinfo->modulename
);
640 // This needs to happen AFTER the grademin/grademax have already been updated.
641 if (!empty($data->grade_rescalegrades
) && $data->grade_rescalegrades
== 'yes') {
642 // Get the grade_item after the update call the activity to scale the grades.
643 $newgradeitem = grade_item
::fetch(array('itemtype' => 'mod',
644 'itemmodule' => $moduleinfo->modulename
,
645 'iteminstance' => $moduleinfo->instance
,
647 'courseid' => $moduleinfo->course
));
648 if ($newgradeitem && $oldgradeitem->gradetype
== GRADE_TYPE_VALUE
&& $newgradeitem->gradetype
== GRADE_TYPE_VALUE
) {
652 $oldgradeitem->grademin
,
653 $oldgradeitem->grademax
,
654 $newgradeitem->grademin
,
655 $newgradeitem->grademax
657 if (!component_callback('mod_' . $moduleinfo->modulename
, 'rescale_activity_grades', $params)) {
658 print_error('cannotreprocessgrades', '', course_get_url($course, $cm->section
), $moduleinfo->modulename
);
663 // Make sure visibility is set correctly (in particular in calendar).
664 if (has_capability('moodle/course:activityvisibility', $modcontext)) {
665 set_coursemodule_visible($moduleinfo->coursemodule
, $moduleinfo->visible
, $moduleinfo->visibleoncoursepage
);
668 if (isset($moduleinfo->cmidnumber
)) { // Label.
669 // Set cm idnumber - uniqueness is already verified by form validation.
670 set_coursemodule_idnumber($moduleinfo->coursemodule
, $moduleinfo->cmidnumber
);
673 if (isset($moduleinfo->downloadcontent
)) {
674 set_downloadcontent($moduleinfo->coursemodule
, $moduleinfo->downloadcontent
);
677 // Update module tags.
678 if (core_tag_tag
::is_enabled('core', 'course_modules') && isset($moduleinfo->tags
)) {
679 core_tag_tag
::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule
, $modcontext, $moduleinfo->tags
);
682 // Now that module is fully updated, also update completion data if required.
683 // (this will wipe all user completion data and recalculate it)
684 if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked
)) {
685 // Rebuild course cache before resetting completion states to ensure that the cm_info attributes are up to date.
686 course_modinfo
::build_course_cache($course);
687 // Fetch this course module's info.
688 $cminfo = cm_info
::create($cm);
689 $completion->reset_all_state($cminfo);
691 $cm->name
= $moduleinfo->name
;
692 \core\event\course_module_updated
::create_from_cm($cm, $modcontext)->trigger();
694 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
696 return array($cm, $moduleinfo);
700 * Include once the module lib file.
702 * @param string $modulename module name of the lib to include
703 * @throws moodle_exception if lib.php file for the module does not exist
705 function include_modulelib($modulename) {
707 $modlib = "$CFG->dirroot/mod/$modulename/lib.php";
708 if (file_exists($modlib)) {
709 include_once($modlib);
711 throw new moodle_exception('modulemissingcode', '', '', $modlib);
716 * Get module information data required for updating the module.
718 * @param stdClass $cm course module object
719 * @param stdClass $course course object
720 * @return array required data for updating a module
723 function get_moduleinfo_data($cm, $course) {
726 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
728 $data->coursemodule
= $cm->id
;
729 $data->section
= $cw->section
; // The section number itself - relative!!! (section column in course_sections)
730 $data->visible
= $cm->visible
; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
731 $data->visibleoncoursepage
= $cm->visibleoncoursepage
;
732 $data->cmidnumber
= $cm->idnumber
; // The cm IDnumber
733 $data->groupmode
= groups_get_activity_groupmode($cm); // locked later if forced
734 $data->groupingid
= $cm->groupingid
;
735 $data->course
= $course->id
;
736 $data->module
= $module->id
;
737 $data->modulename
= $module->name
;
738 $data->instance
= $cm->instance
;
739 $data->completion
= $cm->completion
;
740 $data->completionview
= $cm->completionview
;
741 $data->completionexpected
= $cm->completionexpected
;
742 $data->completionusegrade
= is_null($cm->completiongradeitemnumber
) ?
0 : 1;
743 $data->completionpassgrade
= $cm->completionpassgrade
;
744 $data->completiongradeitemnumber
= $cm->completiongradeitemnumber
;
745 $data->showdescription
= $cm->showdescription
;
746 $data->downloadcontent
= $cm->downloadcontent
;
747 $data->tags
= core_tag_tag
::get_item_tags_array('core', 'course_modules', $cm->id
);
748 if (!empty($CFG->enableavailability
)) {
749 $data->availabilityconditionsjson
= $cm->availability
;
752 if (plugin_supports('mod', $data->modulename
, FEATURE_MOD_INTRO
, true)) {
753 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
754 $currentintro = file_prepare_draft_area($draftid_editor, $context->id
, 'mod_'.$data->modulename
, 'intro', 0, array('subdirs'=>true), $data->intro
);
755 $data->introeditor
= array('text'=>$currentintro, 'format'=>$data->introformat
, 'itemid'=>$draftid_editor);
758 if (plugin_supports('mod', $data->modulename
, FEATURE_ADVANCED_GRADING
, false)
759 and has_capability('moodle/grade:managegradingforms', $context)) {
760 require_once($CFG->dirroot
.'/grade/grading/lib.php');
761 $gradingman = get_grading_manager($context, 'mod_'.$data->modulename
);
762 $data->_advancedgradingdata
['methods'] = $gradingman->get_available_methods();
763 $areas = $gradingman->get_available_areas();
765 foreach ($areas as $areaname => $areatitle) {
766 $gradingman->set_area($areaname);
767 $method = $gradingman->get_active_method();
768 $data->_advancedgradingdata
['areas'][$areaname] = array(
769 'title' => $areatitle,
772 $formfield = 'advancedgradingmethod_'.$areaname;
773 $data->{$formfield} = $method;
777 $component = "mod_{$data->modulename}";
778 $items = grade_item
::fetch_all([
780 'itemmodule' => $data->modulename
,
781 'iteminstance' => $data->instance
,
782 'courseid' => $course->id
,
786 // Add existing outcomes.
787 foreach ($items as $item) {
788 if (!empty($item->outcomeid
)) {
789 $data->{'outcome_' . $item->outcomeid
} = 1;
790 } else if (isset($item->gradepass
)) {
791 $gradepassfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $item->itemnumber
, 'gradepass');
792 $data->{$gradepassfieldname} = format_float($item->gradepass
, $item->get_decimals());
797 // set category if present
799 foreach ($items as $item) {
800 if (!isset($gradecat[$item->itemnumber
])) {
801 $gradecat[$item->itemnumber
] = $item->categoryid
;
803 if ($gradecat[$item->itemnumber
] != $item->categoryid
) {
805 $gradecat[$item->itemnumber
] = false;
808 foreach ($gradecat as $itemnumber => $cat) {
809 if ($cat !== false) {
810 $gradecatfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $itemnumber, 'gradecat');
811 // Do not set if mixed categories present.
812 $data->{$gradecatfieldname} = $cat;
816 return array($cm, $context, $module, $data, $cw);
820 * Prepare the standard module information for a new module instance.
822 * @param stdClass $course course object
823 * @param string $modulename module name
824 * @param int $section section number
825 * @return array module information about other required data
828 function prepare_new_moduleinfo_data($course, $modulename, $section) {
831 list($module, $context, $cw) = can_add_moduleinfo($course, $modulename, $section);
835 $data = new stdClass();
836 $data->section
= $section; // The section number itself - relative!!! (section column in course_sections)
837 $data->visible
= $cw->visible
;
838 $data->course
= $course->id
;
839 $data->module
= $module->id
;
840 $data->modulename
= $module->name
;
841 $data->groupmode
= $course->groupmode
;
842 $data->groupingid
= $course->defaultgroupingid
;
844 $data->instance
= '';
845 $data->coursemodule
= '';
846 $data->downloadcontent
= DOWNLOAD_COURSE_CONTENT_ENABLED
;
848 // Apply completion defaults.
849 $defaults = \core_completion\manager
::get_default_completion($course, $module);
850 foreach ($defaults as $key => $value) {
851 $data->$key = $value;
854 if (plugin_supports('mod', $data->modulename
, FEATURE_MOD_INTRO
, true)) {
855 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
856 file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true));
857 $data->introeditor
= array('text'=>'', 'format'=>FORMAT_HTML
, 'itemid'=>$draftid_editor); // TODO: add better default
860 if (plugin_supports('mod', $data->modulename
, FEATURE_ADVANCED_GRADING
, false)
861 and has_capability('moodle/grade:managegradingforms', $context)) {
862 require_once($CFG->dirroot
.'/grade/grading/lib.php');
864 $data->_advancedgradingdata
['methods'] = grading_manager
::available_methods();
865 $areas = grading_manager
::available_areas('mod_'.$module->name
);
867 foreach ($areas as $areaname => $areatitle) {
868 $data->_advancedgradingdata
['areas'][$areaname] = array(
869 'title' => $areatitle,
872 $formfield = 'advancedgradingmethod_'.$areaname;
873 $data->{$formfield} = '';
877 return array($module, $context, $cw, $cm, $data);