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;
32 use core_courseformat\formatactions
;
33 use core_grades\component_gradeitems
;
35 require_once($CFG->dirroot
.'/course/lib.php');
40 * The function does not check user capabilities.
41 * The function creates course module, module instance, add the module to the correct section.
42 * It also trigger common action that need to be done after adding/updating a module.
44 * @param object $moduleinfo the moudle data
45 * @param object $course the course of the module
46 * @param object $mform this is required by an existing hack to deal with files during MODULENAME_add_instance()
47 * @return object the updated module info
49 function add_moduleinfo($moduleinfo, $course, $mform = null) {
52 // Attempt to include module library before we make any changes to DB.
53 include_modulelib($moduleinfo->modulename
);
55 $moduleinfo->course
= $course->id
;
56 $moduleinfo = set_moduleinfo_defaults($moduleinfo);
58 if (!empty($course->groupmodeforce
) or !isset($moduleinfo->groupmode
)) {
59 $moduleinfo->groupmode
= 0; // Do not set groupmode.
62 // First add course_module record because we need the context.
63 $newcm = new stdClass();
64 $newcm->course
= $course->id
;
65 $newcm->module
= $moduleinfo->module
;
66 $newcm->instance
= 0; // Not known yet, will be updated later (this is similar to restore code).
67 $newcm->visible
= $moduleinfo->visible
;
68 $newcm->visibleold
= $moduleinfo->visible
;
69 $newcm->visibleoncoursepage
= $moduleinfo->visibleoncoursepage
;
70 if (isset($moduleinfo->cmidnumber
)) {
71 $newcm->idnumber
= $moduleinfo->cmidnumber
;
73 if (isset($moduleinfo->downloadcontent
)) {
74 $newcm->downloadcontent
= $moduleinfo->downloadcontent
;
76 if (has_capability('moodle/course:setforcedlanguage', context_course
::instance($course->id
))) {
77 $newcm->lang
= $moduleinfo->lang ??
null;
81 $newcm->groupmode
= $moduleinfo->groupmode
;
82 $newcm->groupingid
= $moduleinfo->groupingid
;
83 $completion = new completion_info($course);
84 if ($completion->is_enabled()) {
85 $newcm->completion
= $moduleinfo->completion
;
86 $newcm->completionpassgrade
= $moduleinfo->completionpassgrade ??
0;
87 if ($moduleinfo->completiongradeitemnumber
=== '') {
88 $newcm->completiongradeitemnumber
= null;
90 $newcm->completiongradeitemnumber
= $moduleinfo->completiongradeitemnumber
;
92 $newcm->completionview
= $moduleinfo->completionview
;
93 $newcm->completionexpected
= $moduleinfo->completionexpected
;
95 if(!empty($CFG->enableavailability
)) {
96 // This code is used both when submitting the form, which uses a long
97 // name to avoid clashes, and by unit test code which uses the real
99 $newcm->availability
= null;
100 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
101 if ($moduleinfo->availabilityconditionsjson
!== '') {
102 $newcm->availability
= $moduleinfo->availabilityconditionsjson
;
104 } else if (property_exists($moduleinfo, 'availability')) {
105 $newcm->availability
= $moduleinfo->availability
;
107 // If there is any availability data, verify it.
108 if ($newcm->availability
) {
109 $tree = new \core_availability\tree
(json_decode($newcm->availability
));
110 // Save time and database space by setting null if the only data
112 if ($tree->is_empty()) {
113 $newcm->availability
= null;
117 if (isset($moduleinfo->showdescription
)) {
118 $newcm->showdescription
= $moduleinfo->showdescription
;
120 $newcm->showdescription
= 0;
122 if (empty($moduleinfo->beforemod
)) {
123 $moduleinfo->beforemod
= null;
126 // From this point we make database changes, so start transaction.
127 $transaction = $DB->start_delegated_transaction();
129 if (!$moduleinfo->coursemodule
= add_course_module($newcm)) {
130 throw new \
moodle_exception('cannotaddcoursemodule');
133 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_MOD_INTRO
, true) &&
134 isset($moduleinfo->introeditor
)) {
135 $introeditor = $moduleinfo->introeditor
;
136 unset($moduleinfo->introeditor
);
137 $moduleinfo->intro
= $introeditor['text'];
138 $moduleinfo->introformat
= $introeditor['format'];
141 $addinstancefunction = $moduleinfo->modulename
."_add_instance";
143 $returnfromfunc = $addinstancefunction($moduleinfo, $mform);
144 } catch (moodle_exception
$e) {
145 $returnfromfunc = $e;
147 if (!$returnfromfunc or !is_number($returnfromfunc)) {
148 // Undo everything we can. This is not necessary for databases which
149 // support transactions, but improves consistency for other databases.
150 context_helper
::delete_instance(CONTEXT_MODULE
, $moduleinfo->coursemodule
);
151 $DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule
));
153 if ($returnfromfunc instanceof moodle_exception
) {
154 throw $returnfromfunc;
155 } else if (!is_number($returnfromfunc)) {
156 throw new \
moodle_exception('invalidfunction', '', course_get_url($course, $moduleinfo->section
));
158 throw new \
moodle_exception('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section
),
159 $moduleinfo->modulename
);
163 $moduleinfo->instance
= $returnfromfunc;
165 $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$moduleinfo->coursemodule
));
167 // Update embedded links and save files.
168 $modcontext = context_module
::instance($moduleinfo->coursemodule
);
169 if (!empty($introeditor)) {
170 // This will respect a module that has set a value for intro in it's modname_add_instance() function.
171 $introeditor['text'] = $moduleinfo->intro
;
173 $moduleinfo->intro
= file_save_draft_area_files($introeditor['itemid'], $modcontext->id
,
174 'mod_'.$moduleinfo->modulename
, 'intro', 0,
175 array('subdirs'=>true), $introeditor['text']);
176 $DB->set_field($moduleinfo->modulename
, 'intro', $moduleinfo->intro
, array('id'=>$moduleinfo->instance
));
180 if (core_tag_tag
::is_enabled('core', 'course_modules') && isset($moduleinfo->tags
)) {
181 core_tag_tag
::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule
, $modcontext, $moduleinfo->tags
);
184 // Course_modules and course_sections each contain a reference to each other.
185 // So we have to update one of them twice.
186 $sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule
, $moduleinfo->section
, $moduleinfo->beforemod
);
188 // Trigger event based on the action we did.
189 // Api create_from_cm expects modname and id property, and we don't want to modify $moduleinfo since we are returning it.
190 $eventdata = clone $moduleinfo;
191 $eventdata->modname
= $eventdata->modulename
;
192 $eventdata->id
= $eventdata->coursemodule
;
193 $event = \core\event\course_module_created
::create_from_cm($eventdata, $modcontext);
196 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
197 $transaction->allow_commit();
203 * Hook for plugins to take action when a module is created or updated.
205 * @param stdClass $moduleinfo the module info
206 * @param stdClass $course the course of the module
208 * @return stdClass moduleinfo updated by plugins.
210 function plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course) {
211 $callbacks = get_plugins_with_function('coursemodule_edit_post_actions', 'lib.php');
212 foreach ($callbacks as $type => $plugins) {
213 foreach ($plugins as $plugin => $pluginfunction) {
214 $moduleinfo = $pluginfunction($moduleinfo, $course);
221 * Common create/update module module actions that need to be processed as soon as a module is created/updaded.
222 * For example:create grade parent category, add outcomes, rebuild caches, regrade, save plagiarism settings...
223 * Please note this api does not trigger events as of MOODLE 2.6. Please trigger events before calling this api.
225 * @param object $moduleinfo the module info
226 * @param object $course the course of the module
228 * @return object moduleinfo update with grading management info
230 function edit_module_post_actions($moduleinfo, $course) {
232 require_once($CFG->libdir
.'/gradelib.php');
234 $modcontext = context_module
::instance($moduleinfo->coursemodule
);
235 $hasgrades = plugin_supports('mod', $moduleinfo->modulename
, FEATURE_GRADE_HAS_GRADE
, false);
236 $hasoutcomes = plugin_supports('mod', $moduleinfo->modulename
, FEATURE_GRADE_OUTCOMES
, true);
238 $items = grade_item
::fetch_all([
240 'itemmodule' => $moduleinfo->modulename
,
241 'iteminstance' => $moduleinfo->instance
,
242 'courseid' => $course->id
,
245 // Create parent category if requested and move to correct parent category.
246 $component = "mod_{$moduleinfo->modulename}";
248 foreach ($items as $item) {
251 // Sync idnumber with grade_item.
252 // Note: This only happens for itemnumber 0 at this time.
253 if ($item->itemnumber
== 0 && ($item->idnumber
!= $moduleinfo->cmidnumber
)) {
254 $item->idnumber
= $moduleinfo->cmidnumber
;
258 // Determine the grade category.
259 $gradecatfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $item->itemnumber
, 'gradecat');
260 if (property_exists($moduleinfo, $gradecatfieldname)) {
261 $gradecat = $moduleinfo->$gradecatfieldname;
262 if ($gradecat == -1) {
263 $gradecategory = new grade_category();
264 $gradecategory->courseid
= $course->id
;
265 $gradecategory->fullname
= $moduleinfo->name
;
266 $gradecategory->insert();
268 $parent = $item->get_parent_category();
269 $gradecategory->set_parent($parent->id
);
270 $gradecat = $gradecategory->id
;
274 if ($parent = $item->get_parent_category()) {
275 $oldgradecat = $parent->id
;
277 if ($oldgradecat != $gradecat) {
278 $item->set_parent($gradecat);
283 // Determine the gradepass.
284 $gradepassfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $item->itemnumber
, 'gradepass');
285 if (isset($moduleinfo->{$gradepassfieldname})) {
286 $gradepass = $moduleinfo->{$gradepassfieldname};
287 if (null !== $gradepass && $gradepass != $item->gradepass
) {
288 $item->gradepass
= $gradepass;
297 if (!empty($moduleinfo->add
)) {
298 $gradecategory = $item->get_parent_category();
299 if ($item->set_aggregation_fields_for_aggregation(0, $gradecategory->aggregation
)) {
306 require_once($CFG->libdir
.'/grade/grade_outcome.php');
307 // Add outcomes if requested.
308 if ($hasoutcomes && $outcomes = grade_outcome
::fetch_all_available($course->id
)) {
309 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes.
310 $max_itemnumber = 999;
312 foreach($items as $item) {
313 if ($item->itemnumber
> $max_itemnumber) {
314 $max_itemnumber = $item->itemnumber
;
319 foreach($outcomes as $outcome) {
320 $elname = 'outcome_'.$outcome->id
;
322 if (property_exists($moduleinfo, $elname) and $moduleinfo->$elname) {
323 // Check if this is a new outcome grade item.
324 $outcomeexists = false;
326 foreach($items as $item) {
327 if ($item->outcomeid
== $outcome->id
) {
328 $outcomeexists = true;
332 if ($outcomeexists) {
339 $outcomeitem = new grade_item();
340 $outcomeitem->courseid
= $course->id
;
341 $outcomeitem->itemtype
= 'mod';
342 $outcomeitem->itemmodule
= $moduleinfo->modulename
;
343 $outcomeitem->iteminstance
= $moduleinfo->instance
;
344 $outcomeitem->itemnumber
= $max_itemnumber;
345 $outcomeitem->itemname
= $outcome->fullname
;
346 $outcomeitem->outcomeid
= $outcome->id
;
347 $outcomeitem->gradetype
= GRADE_TYPE_SCALE
;
348 $outcomeitem->scaleid
= $outcome->scaleid
;
349 $outcomeitem->insert();
352 // Move the new outcome into the same category and immediately after the first grade item.
353 $item = reset($items);
354 $outcomeitem->set_parent($item->categoryid
);
355 $outcomeitem->move_after_sortorder($item->sortorder
);
356 } else if (isset($moduleinfo->gradecat
)) {
357 $outcomeitem->set_parent($moduleinfo->gradecat
);
360 if (!$outcomeexists) {
361 $gradecategory = $outcomeitem->get_parent_category();
362 if ($outcomeitem->set_aggregation_fields_for_aggregation(0, $gradecategory->aggregation
)) {
363 $outcomeitem->update();
370 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_ADVANCED_GRADING
, false)
371 and has_capability('moodle/grade:managegradingforms', $modcontext)) {
372 require_once($CFG->dirroot
.'/grade/grading/lib.php');
373 $gradingman = get_grading_manager($modcontext, 'mod_'.$moduleinfo->modulename
);
374 $showgradingmanagement = false;
375 foreach ($gradingman->get_available_areas() as $areaname => $aretitle) {
376 $formfield = 'advancedgradingmethod_'.$areaname;
377 if (isset($moduleinfo->{$formfield})) {
378 $gradingman->set_area($areaname);
379 $methodchanged = $gradingman->set_active_method($moduleinfo->{$formfield});
380 if (empty($moduleinfo->{$formfield})) {
381 // Going back to the simple direct grading is not a reason to open the management screen.
382 $methodchanged = false;
384 $showgradingmanagement = $showgradingmanagement ||
$methodchanged;
387 // Update grading management information.
388 $moduleinfo->gradingman
= $gradingman;
389 $moduleinfo->showgradingmanagement
= $showgradingmanagement;
392 \course_modinfo
::purge_course_module_cache($course->id
, $moduleinfo->coursemodule
);
393 rebuild_course_cache($course->id
, true, true);
396 // If regrading will be slow, and this is happening in response to front-end UI...
397 if (!empty($moduleinfo->frontend
) && grade_needs_regrade_progress_bar($course->id
)) {
398 // And if it actually needs regrading...
399 $courseitem = grade_item
::fetch_course_item($course->id
);
400 if ($courseitem->needsupdate
) {
401 // Then don't do it as part of this form save, do it on an extra web request with a
403 $moduleinfo->needsfrontendregrade
= true;
407 grade_regrade_final_grades($course->id
);
411 // Allow plugins to extend the course module form.
412 $moduleinfo = plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course);
414 if (!empty($moduleinfo->coursecontentnotification
)) {
415 // Schedule adhoc-task for delivering the course content updated notification.
416 if ($course->visible
&& $moduleinfo->visible
) {
417 $adhocktask = new \core_course\task\
content_notification_task();
418 $adhocktask->set_custom_data(
419 ['update' => $moduleinfo->update
, 'cmid' => $moduleinfo->coursemodule
,
420 'courseid' => $course->id
, 'userfrom' => $USER->id
]);
421 $adhocktask->set_component('course');
422 \core\task\manager
::queue_adhoc_task($adhocktask, true);
430 * Set module info default values for the unset module attributs.
432 * @param object $moduleinfo the current known data of the module
433 * @return object the completed module info
435 function set_moduleinfo_defaults($moduleinfo) {
437 if (empty($moduleinfo->coursemodule
)) {
440 $moduleinfo->instance
= '';
441 $moduleinfo->coursemodule
= '';
444 $cm = get_coursemodule_from_id('', $moduleinfo->coursemodule
, 0, false, MUST_EXIST
);
445 $moduleinfo->instance
= $cm->instance
;
446 $moduleinfo->coursemodule
= $cm->id
;
449 $moduleinfo->modulename
= clean_param($moduleinfo->modulename
, PARAM_PLUGIN
);
451 if (!isset($moduleinfo->groupingid
)) {
452 $moduleinfo->groupingid
= 0;
455 if (!isset($moduleinfo->name
)) { // Label.
456 $moduleinfo->name
= $moduleinfo->modulename
;
459 if (!isset($moduleinfo->completion
)) {
460 $moduleinfo->completion
= COMPLETION_DISABLED
;
462 if (!isset($moduleinfo->completionview
)) {
463 $moduleinfo->completionview
= COMPLETION_VIEW_NOT_REQUIRED
;
465 if (!isset($moduleinfo->completionexpected
)) {
466 $moduleinfo->completionexpected
= 0;
469 // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
470 if (isset($moduleinfo->completionusegrade
) &&
471 $moduleinfo->completionusegrade
&&
472 !isset($moduleinfo->completiongradeitemnumber
474 $moduleinfo->completiongradeitemnumber
= 0;
475 } else if (!isset($moduleinfo->completiongradeitemnumber
)) {
476 // If there is no gradeitemnumber set, make sure to disable completionpassgrade.
477 $moduleinfo->completionpassgrade
= 0;
478 $moduleinfo->completiongradeitemnumber
= null;
481 if (!isset($moduleinfo->conditiongradegroup
)) {
482 $moduleinfo->conditiongradegroup
= array();
484 if (!isset($moduleinfo->conditionfieldgroup
)) {
485 $moduleinfo->conditionfieldgroup
= array();
487 if (!isset($moduleinfo->visibleoncoursepage
)) {
488 $moduleinfo->visibleoncoursepage
= 1;
491 if (!isset($moduleinfo->downloadcontent
)) {
492 $moduleinfo->downloadcontent
= DOWNLOAD_COURSE_CONTENT_ENABLED
;
499 * Check that the user can add a module. Also returns some information like the module, context and course section info.
500 * The fucntion create the course section if it doesn't exist.
502 * @param stdClass $course the course of the module
503 * @param string $modulename the module name
504 * @param int $sectionnum the section of the module
505 * @return array list containing module, context, course section.
506 * @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
508 function can_add_moduleinfo($course, $modulename, $sectionnum) {
511 $module = $DB->get_record('modules', ['name' => $modulename], '*', MUST_EXIST
);
513 $context = context_course
::instance($course->id
);
514 require_capability('moodle/course:manageactivities', $context);
516 // If the $sectionnum is a delegated section, we cannot execute create_if_missing
517 // because it only works to create regular sections. To prevent that from happening, we
518 // check if the section is already there, no matter if it is delegated or not.
519 $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
521 formatactions
::section($course)->create_if_missing([$sectionnum]);
522 $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
525 if (!course_allowed_module($course, $module->name
)) {
526 throw new \
moodle_exception('moduledisable');
529 return [$module, $context, $sectioninfo];
533 * Check if user is allowed to update module info and returns related item/data to the module.
535 * @param object $cm course module
536 * @return array - list of course module, context, module, moduleinfo, and course section.
537 * @throws moodle_exception if user is not allowed to perform the action
539 function can_update_moduleinfo($cm) {
542 // Check the $USER has the right capability.
543 $context = context_module
::instance($cm->id
);
544 require_capability('moodle/course:manageactivities', $context);
546 // Check module exists.
547 $module = $DB->get_record('modules', array('id'=>$cm->module
), '*', MUST_EXIST
);
549 // Check the moduleinfo exists.
550 $data = $DB->get_record($module->name
, array('id'=>$cm->instance
), '*', MUST_EXIST
);
552 // Check the course section exists.
553 $cw = $DB->get_record('course_sections', array('id'=>$cm->section
), '*', MUST_EXIST
);
555 return array($cm, $context, $module, $data, $cw);
560 * Update the module info.
561 * This function doesn't check the user capabilities. It updates the course module and the module instance.
562 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
564 * @param object $cm course module
565 * @param object $moduleinfo module info
566 * @param object $course course of the module
567 * @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.
568 * @return array list of course module and module info.
570 function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
573 $data = new stdClass();
575 $data = $mform->get_data();
578 // Attempt to include module library before we make any changes to DB.
579 include_modulelib($moduleinfo->modulename
);
581 $moduleinfo->course
= $course->id
;
582 $moduleinfo = set_moduleinfo_defaults($moduleinfo);
584 $modcontext = context_module
::instance($moduleinfo->coursemodule
);
585 if (has_capability('moodle/course:setforcedlanguage', $modcontext)) {
586 $cm->lang
= $moduleinfo->lang ??
null;
591 if (!empty($course->groupmodeforce
) or !isset($moduleinfo->groupmode
)) {
592 $moduleinfo->groupmode
= $cm->groupmode
; // Keep original.
595 // Update course module first.
596 $cm->groupmode
= $moduleinfo->groupmode
;
597 if (isset($moduleinfo->groupingid
)) {
598 $cm->groupingid
= $moduleinfo->groupingid
;
601 $completion = new completion_info($course);
602 if ($completion->is_enabled()) {
603 // Completion settings that would affect users who have already completed
604 // the activity may be locked; if so, these should not be updated.
605 if (!empty($moduleinfo->completionunlocked
)) {
606 $cm->completion
= $moduleinfo->completion
;
607 $cm->completionpassgrade
= $moduleinfo->completionpassgrade ??
0;
608 if ($moduleinfo->completiongradeitemnumber
=== '') {
609 $cm->completiongradeitemnumber
= null;
611 $cm->completiongradeitemnumber
= $moduleinfo->completiongradeitemnumber
;
613 $cm->completionview
= $moduleinfo->completionview
;
615 // The expected date does not affect users who have completed the activity,
616 // so it is safe to update it regardless of the lock status.
617 $cm->completionexpected
= $moduleinfo->completionexpected
;
619 if (!empty($CFG->enableavailability
)) {
620 // This code is used both when submitting the form, which uses a long
621 // name to avoid clashes, and by unit test code which uses the real
622 // name in the table.
623 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
624 if ($moduleinfo->availabilityconditionsjson
!== '') {
625 $cm->availability
= $moduleinfo->availabilityconditionsjson
;
627 $cm->availability
= null;
629 } else if (property_exists($moduleinfo, 'availability')) {
630 $cm->availability
= $moduleinfo->availability
;
632 // If there is any availability data, verify it.
633 if ($cm->availability
) {
634 $tree = new \core_availability\tree
(json_decode($cm->availability
));
635 // Save time and database space by setting null if the only data
637 if ($tree->is_empty()) {
638 $cm->availability
= null;
642 if (isset($moduleinfo->showdescription
)) {
643 $cm->showdescription
= $moduleinfo->showdescription
;
645 $cm->showdescription
= 0;
648 $DB->update_record('course_modules', $cm);
650 // Update embedded links and save files.
651 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_MOD_INTRO
, true)) {
652 $moduleinfo->intro
= file_save_draft_area_files($moduleinfo->introeditor
['itemid'], $modcontext->id
,
653 'mod_'.$moduleinfo->modulename
, 'intro', 0,
654 array('subdirs'=>true), $moduleinfo->introeditor
['text']);
655 $moduleinfo->introformat
= $moduleinfo->introeditor
['format'];
656 unset($moduleinfo->introeditor
);
658 // Get the a copy of the grade_item before it is modified incase we need to scale the grades.
659 $oldgradeitem = null;
660 $newgradeitem = null;
661 if (!empty($data->grade_rescalegrades
) && $data->grade_rescalegrades
== 'yes') {
662 // Fetch the grade item before it is updated.
663 $oldgradeitem = grade_item
::fetch(array('itemtype' => 'mod',
664 'itemmodule' => $moduleinfo->modulename
,
665 'iteminstance' => $moduleinfo->instance
,
667 'courseid' => $moduleinfo->course
));
670 $updateinstancefunction = $moduleinfo->modulename
."_update_instance";
671 if (!$updateinstancefunction($moduleinfo, $mform)) {
672 throw new \
moodle_exception('cannotupdatemod', '', course_get_url($course, $cm->section
), $moduleinfo->modulename
);
675 // This needs to happen AFTER the grademin/grademax have already been updated.
676 if (!empty($data->grade_rescalegrades
) && $data->grade_rescalegrades
== 'yes') {
677 // Get the grade_item after the update call the activity to scale the grades.
678 $newgradeitem = grade_item
::fetch(array('itemtype' => 'mod',
679 'itemmodule' => $moduleinfo->modulename
,
680 'iteminstance' => $moduleinfo->instance
,
682 'courseid' => $moduleinfo->course
));
683 if ($newgradeitem && $oldgradeitem->gradetype
== GRADE_TYPE_VALUE
&& $newgradeitem->gradetype
== GRADE_TYPE_VALUE
) {
687 $oldgradeitem->grademin
,
688 $oldgradeitem->grademax
,
689 $newgradeitem->grademin
,
690 $newgradeitem->grademax
692 if (!component_callback('mod_' . $moduleinfo->modulename
, 'rescale_activity_grades', $params)) {
693 throw new \
moodle_exception('cannotreprocessgrades', '', course_get_url($course, $cm->section
),
694 $moduleinfo->modulename
);
699 // Make sure visibility is set correctly (in particular in calendar).
700 if (has_capability('moodle/course:activityvisibility', $modcontext)) {
701 set_coursemodule_visible($moduleinfo->coursemodule
, $moduleinfo->visible
, $moduleinfo->visibleoncoursepage
);
704 if (isset($moduleinfo->cmidnumber
)) { // Label.
705 // Set cm idnumber - uniqueness is already verified by form validation.
706 set_coursemodule_idnumber($moduleinfo->coursemodule
, $moduleinfo->cmidnumber
);
709 if (isset($moduleinfo->downloadcontent
)) {
710 set_downloadcontent($moduleinfo->coursemodule
, $moduleinfo->downloadcontent
);
713 // Update module tags.
714 if (core_tag_tag
::is_enabled('core', 'course_modules') && isset($moduleinfo->tags
)) {
715 core_tag_tag
::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule
, $modcontext, $moduleinfo->tags
);
717 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
719 // Now that module is fully updated, also update completion data if required.
720 // (this will wipe all user completion data and recalculate it)
721 if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked
)) {
722 // Rebuild course cache before resetting completion states to ensure that the cm_info attributes are up to date.
723 course_modinfo
::build_course_cache($course);
724 // Fetch this course module's info.
725 $cminfo = cm_info
::create($cm);
726 $completion->reset_all_state($cminfo);
729 if ($cm->name
!= $moduleinfo->name
) {
730 di
::get(hook\manager
::class)->dispatch(
731 new \core_courseformat\hook\after_cm_name_edited
(
732 get_fast_modinfo($course)->get_cm($cm->id
),
738 $cm->name
= $moduleinfo->name
;
739 \core\event\course_module_updated
::create_from_cm($cm, $modcontext)->trigger();
741 return array($cm, $moduleinfo);
745 * Include once the module lib file.
747 * @param string $modulename module name of the lib to include
748 * @throws moodle_exception if lib.php file for the module does not exist
750 function include_modulelib($modulename) {
752 $modlib = "$CFG->dirroot/mod/$modulename/lib.php";
753 if (file_exists($modlib)) {
754 include_once($modlib);
756 throw new moodle_exception('modulemissingcode', '', '', $modlib);
761 * Get module information data required for updating the module.
763 * @param stdClass $cm course module object
764 * @param stdClass $course course object
765 * @return array required data for updating a module
768 function get_moduleinfo_data($cm, $course) {
771 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
773 $data->coursemodule
= $cm->id
;
774 $data->section
= $cw->section
; // The section number itself - relative!!! (section column in course_sections)
775 $data->visible
= $cm->visible
; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
776 $data->visibleoncoursepage
= $cm->visibleoncoursepage
;
777 $data->cmidnumber
= $cm->idnumber
; // The cm IDnumber
778 $data->groupmode
= groups_get_activity_groupmode($cm); // locked later if forced
779 $data->groupingid
= $cm->groupingid
;
780 $data->course
= $course->id
;
781 $data->module
= $module->id
;
782 $data->modulename
= $module->name
;
783 $data->instance
= $cm->instance
;
784 $data->completion
= $cm->completion
;
785 $data->completionview
= $cm->completionview
;
786 $data->completionexpected
= $cm->completionexpected
;
787 $data->completionusegrade
= is_null($cm->completiongradeitemnumber
) ?
0 : 1;
788 $data->completionpassgrade
= $cm->completionpassgrade
;
789 $data->completiongradeitemnumber
= $cm->completiongradeitemnumber
;
790 $data->showdescription
= $cm->showdescription
;
791 $data->downloadcontent
= $cm->downloadcontent
;
792 $data->lang
= $cm->lang
;
793 $data->tags
= core_tag_tag
::get_item_tags_array('core', 'course_modules', $cm->id
);
794 if (!empty($CFG->enableavailability
)) {
795 $data->availabilityconditionsjson
= $cm->availability
;
798 if (plugin_supports('mod', $data->modulename
, FEATURE_MOD_INTRO
, true)) {
799 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
800 $currentintro = file_prepare_draft_area($draftid_editor, $context->id
, 'mod_'.$data->modulename
, 'intro', 0, array('subdirs'=>true), $data->intro
);
801 $data->introeditor
= array('text'=>$currentintro, 'format'=>$data->introformat
, 'itemid'=>$draftid_editor);
804 if (plugin_supports('mod', $data->modulename
, FEATURE_ADVANCED_GRADING
, false)
805 and has_capability('moodle/grade:managegradingforms', $context)) {
806 require_once($CFG->dirroot
.'/grade/grading/lib.php');
807 $gradingman = get_grading_manager($context, 'mod_'.$data->modulename
);
808 $data->_advancedgradingdata
['methods'] = $gradingman->get_available_methods();
809 $areas = $gradingman->get_available_areas();
811 foreach ($areas as $areaname => $areatitle) {
812 $gradingman->set_area($areaname);
813 $method = $gradingman->get_active_method();
814 $data->_advancedgradingdata
['areas'][$areaname] = array(
815 'title' => $areatitle,
818 $formfield = 'advancedgradingmethod_'.$areaname;
819 $data->{$formfield} = $method;
823 $component = "mod_{$data->modulename}";
824 $items = grade_item
::fetch_all([
826 'itemmodule' => $data->modulename
,
827 'iteminstance' => $data->instance
,
828 'courseid' => $course->id
,
832 // Add existing outcomes.
833 foreach ($items as $item) {
834 if (!empty($item->outcomeid
)) {
835 $data->{'outcome_' . $item->outcomeid
} = 1;
836 } else if (isset($item->gradepass
)) {
837 $gradepassfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $item->itemnumber
, 'gradepass');
838 $data->{$gradepassfieldname} = format_float($item->gradepass
, $item->get_decimals());
843 // set category if present
845 foreach ($items as $item) {
846 if (!isset($gradecat[$item->itemnumber
])) {
847 $gradecat[$item->itemnumber
] = $item->categoryid
;
849 if ($gradecat[$item->itemnumber
] != $item->categoryid
) {
851 $gradecat[$item->itemnumber
] = false;
854 foreach ($gradecat as $itemnumber => $cat) {
855 if ($cat !== false) {
856 $gradecatfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $itemnumber, 'gradecat');
857 // Do not set if mixed categories present.
858 $data->{$gradecatfieldname} = $cat;
862 return array($cm, $context, $module, $data, $cw);
866 * Prepare the standard module information for a new module instance.
868 * @param stdClass $course course object
869 * @param string $modulename module name
870 * @param int $section section number
871 * @param string $suffix the suffix to add to the name of the completion rules.
872 * @return array module information about other required data
875 function prepare_new_moduleinfo_data($course, $modulename, $section, string $suffix = '') {
878 list($module, $context, $cw) = can_add_moduleinfo($course, $modulename, $section);
882 $data = new stdClass();
883 $data->section
= $section; // The section number itself - relative!!! (section column in course_sections)
884 $data->visible
= $cw->visible
;
885 $data->course
= $course->id
;
886 $data->module
= $module->id
;
887 $data->modulename
= $module->name
;
888 $data->groupmode
= $course->groupmode
;
889 $data->groupingid
= $course->defaultgroupingid
;
891 $data->instance
= '';
892 $data->coursemodule
= '';
893 $data->downloadcontent
= DOWNLOAD_COURSE_CONTENT_ENABLED
;
895 // Apply completion defaults.
896 $defaults = \core_completion\manager
::get_default_completion($course, $module, true, $suffix);
897 foreach ($defaults as $key => $value) {
898 $data->$key = $value;
901 if (plugin_supports('mod', $data->modulename
, FEATURE_MOD_INTRO
, true)) {
902 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
903 file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true));
904 $data->introeditor
= array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftid_editor);
907 if (plugin_supports('mod', $data->modulename
, FEATURE_ADVANCED_GRADING
, false)
908 and has_capability('moodle/grade:managegradingforms', $context)) {
909 require_once($CFG->dirroot
.'/grade/grading/lib.php');
911 $data->_advancedgradingdata
['methods'] = grading_manager
::available_methods();
912 $areas = grading_manager
::available_areas('mod_'.$module->name
);
914 foreach ($areas as $areaname => $areatitle) {
915 $data->_advancedgradingdata
['areas'][$areaname] = array(
916 'title' => $areatitle,
919 $formfield = 'advancedgradingmethod_'.$areaname;
920 $data->{$formfield} = '';
924 return array($module, $context, $cw, $cm, $data);