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 $newcm->groupmode
= $moduleinfo->groupmode
;
71 $newcm->groupingid
= $moduleinfo->groupingid
;
72 $completion = new completion_info($course);
73 if ($completion->is_enabled()) {
74 $newcm->completion
= $moduleinfo->completion
;
75 if ($moduleinfo->completiongradeitemnumber
=== '') {
76 $newcm->completiongradeitemnumber
= null;
78 $newcm->completiongradeitemnumber
= $moduleinfo->completiongradeitemnumber
;
80 $newcm->completionview
= $moduleinfo->completionview
;
81 $newcm->completionexpected
= $moduleinfo->completionexpected
;
83 if(!empty($CFG->enableavailability
)) {
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
87 $newcm->availability
= null;
88 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
89 if ($moduleinfo->availabilityconditionsjson
!== '') {
90 $newcm->availability
= $moduleinfo->availabilityconditionsjson
;
92 } else if (property_exists($moduleinfo, 'availability')) {
93 $newcm->availability
= $moduleinfo->availability
;
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
100 if ($tree->is_empty()) {
101 $newcm->availability
= null;
105 if (isset($moduleinfo->showdescription
)) {
106 $newcm->showdescription
= $moduleinfo->showdescription
;
108 $newcm->showdescription
= 0;
111 // From this point we make database changes, so start transaction.
112 $transaction = $DB->start_delegated_transaction();
114 if (!$moduleinfo->coursemodule
= add_course_module($newcm)) {
115 print_error('cannotaddcoursemodule');
118 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_MOD_INTRO
, true) &&
119 isset($moduleinfo->introeditor
)) {
120 $introeditor = $moduleinfo->introeditor
;
121 unset($moduleinfo->introeditor
);
122 $moduleinfo->intro
= $introeditor['text'];
123 $moduleinfo->introformat
= $introeditor['format'];
126 $addinstancefunction = $moduleinfo->modulename
."_add_instance";
128 $returnfromfunc = $addinstancefunction($moduleinfo, $mform);
129 } catch (moodle_exception
$e) {
130 $returnfromfunc = $e;
132 if (!$returnfromfunc or !is_number($returnfromfunc)) {
133 // Undo everything we can. This is not necessary for databases which
134 // support transactions, but improves consistency for other databases.
135 context_helper
::delete_instance(CONTEXT_MODULE
, $moduleinfo->coursemodule
);
136 $DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule
));
138 if ($returnfromfunc instanceof moodle_exception
) {
139 throw $returnfromfunc;
140 } else if (!is_number($returnfromfunc)) {
141 print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section
));
143 print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section
), $moduleinfo->modulename
);
147 $moduleinfo->instance
= $returnfromfunc;
149 $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$moduleinfo->coursemodule
));
151 // Update embedded links and save files.
152 $modcontext = context_module
::instance($moduleinfo->coursemodule
);
153 if (!empty($introeditor)) {
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
;
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
));
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
);
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
);
172 // Trigger event based on the action we did.
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);
180 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
181 $transaction->allow_commit();
187 * Hook for plugins to take action when a module is created or updated.
189 * @param stdClass $moduleinfo the module info
190 * @param stdClass $course the course of the module
192 * @return stdClass moduleinfo updated by plugins.
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);
205 * Common create/update module module actions that need to be processed as soon as a module is created/updaded.
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.
209 * @param object $moduleinfo the module info
210 * @param object $course the course of the module
212 * @return object moduleinfo update with grading management info
214 function edit_module_post_actions($moduleinfo, $course) {
216 require_once($CFG->libdir
.'/gradelib.php');
218 $modcontext = context_module
::instance($moduleinfo->coursemodule
);
219 $hasgrades = plugin_supports('mod', $moduleinfo->modulename
, FEATURE_GRADE_HAS_GRADE
, false);
220 $hasoutcomes = plugin_supports('mod', $moduleinfo->modulename
, FEATURE_GRADE_OUTCOMES
, true);
222 $items = grade_item
::fetch_all([
224 'itemmodule' => $moduleinfo->modulename
,
225 'iteminstance' => $moduleinfo->instance
,
226 'courseid' => $course->id
,
229 // Create parent category if requested and move to correct parent category.
230 $component = "mod_{$moduleinfo->modulename}";
232 foreach ($items as $item) {
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
;
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();
252 $parent = $item->get_parent_category();
253 $gradecategory->set_parent($parent->id
);
254 $gradecat = $gradecategory->id
;
258 if ($parent = $item->get_parent_category()) {
259 $oldgradecat = $parent->id
;
261 if ($oldgradecat != $gradecat) {
262 $item->set_parent($gradecat);
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;
281 if (!empty($moduleinfo->add
)) {
282 $gradecategory = $item->get_parent_category();
283 if ($item->set_aggregation_fields_for_aggregation(0, $gradecategory->aggregation
)) {
290 require_once($CFG->libdir
.'/grade/grade_outcome.php');
291 // Add outcomes if requested.
292 if ($hasoutcomes && $outcomes = grade_outcome
::fetch_all_available($course->id
)) {
293 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes.
294 $max_itemnumber = 999;
296 foreach($items as $item) {
297 if ($item->itemnumber
> $max_itemnumber) {
298 $max_itemnumber = $item->itemnumber
;
303 foreach($outcomes as $outcome) {
304 $elname = 'outcome_'.$outcome->id
;
306 if (property_exists($moduleinfo, $elname) and $moduleinfo->$elname) {
307 // Check if this is a new outcome grade item.
308 $outcomeexists = false;
310 foreach($items as $item) {
311 if ($item->outcomeid
== $outcome->id
) {
312 $outcomeexists = true;
316 if ($outcomeexists) {
323 $outcomeitem = new grade_item();
324 $outcomeitem->courseid
= $course->id
;
325 $outcomeitem->itemtype
= 'mod';
326 $outcomeitem->itemmodule
= $moduleinfo->modulename
;
327 $outcomeitem->iteminstance
= $moduleinfo->instance
;
328 $outcomeitem->itemnumber
= $max_itemnumber;
329 $outcomeitem->itemname
= $outcome->fullname
;
330 $outcomeitem->outcomeid
= $outcome->id
;
331 $outcomeitem->gradetype
= GRADE_TYPE_SCALE
;
332 $outcomeitem->scaleid
= $outcome->scaleid
;
333 $outcomeitem->insert();
336 // Move the new outcome into the same category and immediately after the first grade item.
337 $item = reset($items);
338 $outcomeitem->set_parent($item->categoryid
);
339 $outcomeitem->move_after_sortorder($item->sortorder
);
340 } else if (isset($moduleinfo->gradecat
)) {
341 $outcomeitem->set_parent($moduleinfo->gradecat
);
344 if (!$outcomeexists) {
345 $gradecategory = $outcomeitem->get_parent_category();
346 if ($outcomeitem->set_aggregation_fields_for_aggregation(0, $gradecategory->aggregation
)) {
347 $outcomeitem->update();
354 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_ADVANCED_GRADING
, false)
355 and has_capability('moodle/grade:managegradingforms', $modcontext)) {
356 require_once($CFG->dirroot
.'/grade/grading/lib.php');
357 $gradingman = get_grading_manager($modcontext, 'mod_'.$moduleinfo->modulename
);
358 $showgradingmanagement = false;
359 foreach ($gradingman->get_available_areas() as $areaname => $aretitle) {
360 $formfield = 'advancedgradingmethod_'.$areaname;
361 if (isset($moduleinfo->{$formfield})) {
362 $gradingman->set_area($areaname);
363 $methodchanged = $gradingman->set_active_method($moduleinfo->{$formfield});
364 if (empty($moduleinfo->{$formfield})) {
365 // Going back to the simple direct grading is not a reason to open the management screen.
366 $methodchanged = false;
368 $showgradingmanagement = $showgradingmanagement ||
$methodchanged;
371 // Update grading management information.
372 $moduleinfo->gradingman
= $gradingman;
373 $moduleinfo->showgradingmanagement
= $showgradingmanagement;
376 rebuild_course_cache($course->id
, true);
378 grade_regrade_final_grades($course->id
);
380 require_once($CFG->libdir
.'/plagiarismlib.php');
381 plagiarism_save_form_elements($moduleinfo);
383 // Allow plugins to extend the course module form.
384 $moduleinfo = plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course);
390 * Set module info default values for the unset module attributs.
392 * @param object $moduleinfo the current known data of the module
393 * @return object the completed module info
395 function set_moduleinfo_defaults($moduleinfo) {
397 if (empty($moduleinfo->coursemodule
)) {
400 $moduleinfo->instance
= '';
401 $moduleinfo->coursemodule
= '';
404 $cm = get_coursemodule_from_id('', $moduleinfo->coursemodule
, 0, false, MUST_EXIST
);
405 $moduleinfo->instance
= $cm->instance
;
406 $moduleinfo->coursemodule
= $cm->id
;
409 $moduleinfo->modulename
= clean_param($moduleinfo->modulename
, PARAM_PLUGIN
);
411 if (!isset($moduleinfo->groupingid
)) {
412 $moduleinfo->groupingid
= 0;
415 if (!isset($moduleinfo->name
)) { // Label.
416 $moduleinfo->name
= $moduleinfo->modulename
;
419 if (!isset($moduleinfo->completion
)) {
420 $moduleinfo->completion
= COMPLETION_DISABLED
;
422 if (!isset($moduleinfo->completionview
)) {
423 $moduleinfo->completionview
= COMPLETION_VIEW_NOT_REQUIRED
;
425 if (!isset($moduleinfo->completionexpected
)) {
426 $moduleinfo->completionexpected
= 0;
429 // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
430 if (isset($moduleinfo->completionusegrade
) && $moduleinfo->completionusegrade
) {
431 $moduleinfo->completiongradeitemnumber
= 0;
432 } else if (!isset($moduleinfo->completiongradeitemnumber
)) {
433 $moduleinfo->completiongradeitemnumber
= null;
436 if (!isset($moduleinfo->conditiongradegroup
)) {
437 $moduleinfo->conditiongradegroup
= array();
439 if (!isset($moduleinfo->conditionfieldgroup
)) {
440 $moduleinfo->conditionfieldgroup
= array();
442 if (!isset($moduleinfo->visibleoncoursepage
)) {
443 $moduleinfo->visibleoncoursepage
= 1;
450 * Check that the user can add a module. Also returns some information like the module, context and course section info.
451 * The fucntion create the course section if it doesn't exist.
453 * @param object $course the course of the module
454 * @param object $modulename the module name
455 * @param object $section the section of the module
456 * @return array list containing module, context, course section.
457 * @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
459 function can_add_moduleinfo($course, $modulename, $section) {
462 $module = $DB->get_record('modules', array('name'=>$modulename), '*', MUST_EXIST
);
464 $context = context_course
::instance($course->id
);
465 require_capability('moodle/course:manageactivities', $context);
467 course_create_sections_if_missing($course, $section);
468 $cw = get_fast_modinfo($course)->get_section_info($section);
470 if (!course_allowed_module($course, $module->name
)) {
471 print_error('moduledisable');
474 return array($module, $context, $cw);
478 * Check if user is allowed to update module info and returns related item/data to the module.
480 * @param object $cm course module
481 * @return array - list of course module, context, module, moduleinfo, and course section.
482 * @throws moodle_exception if user is not allowed to perform the action
484 function can_update_moduleinfo($cm) {
487 // Check the $USER has the right capability.
488 $context = context_module
::instance($cm->id
);
489 require_capability('moodle/course:manageactivities', $context);
491 // Check module exists.
492 $module = $DB->get_record('modules', array('id'=>$cm->module
), '*', MUST_EXIST
);
494 // Check the moduleinfo exists.
495 $data = $DB->get_record($module->name
, array('id'=>$cm->instance
), '*', MUST_EXIST
);
497 // Check the course section exists.
498 $cw = $DB->get_record('course_sections', array('id'=>$cm->section
), '*', MUST_EXIST
);
500 return array($cm, $context, $module, $data, $cw);
505 * Update the module info.
506 * This function doesn't check the user capabilities. It updates the course module and the module instance.
507 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
509 * @param object $cm course module
510 * @param object $moduleinfo module info
511 * @param object $course course of the module
512 * @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.
513 * @return array list of course module and module info.
515 function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
518 $data = new stdClass();
520 $data = $mform->get_data();
523 // Attempt to include module library before we make any changes to DB.
524 include_modulelib($moduleinfo->modulename
);
526 $moduleinfo->course
= $course->id
;
527 $moduleinfo = set_moduleinfo_defaults($moduleinfo);
529 if (!empty($course->groupmodeforce
) or !isset($moduleinfo->groupmode
)) {
530 $moduleinfo->groupmode
= $cm->groupmode
; // Keep original.
533 // Update course module first.
534 $cm->groupmode
= $moduleinfo->groupmode
;
535 if (isset($moduleinfo->groupingid
)) {
536 $cm->groupingid
= $moduleinfo->groupingid
;
539 $completion = new completion_info($course);
540 if ($completion->is_enabled()) {
541 // Completion settings that would affect users who have already completed
542 // the activity may be locked; if so, these should not be updated.
543 if (!empty($moduleinfo->completionunlocked
)) {
544 $cm->completion
= $moduleinfo->completion
;
545 if ($moduleinfo->completiongradeitemnumber
=== '') {
546 $cm->completiongradeitemnumber
= null;
548 $cm->completiongradeitemnumber
= $moduleinfo->completiongradeitemnumber
;
550 $cm->completionview
= $moduleinfo->completionview
;
552 // The expected date does not affect users who have completed the activity,
553 // so it is safe to update it regardless of the lock status.
554 $cm->completionexpected
= $moduleinfo->completionexpected
;
556 if (!empty($CFG->enableavailability
)) {
557 // This code is used both when submitting the form, which uses a long
558 // name to avoid clashes, and by unit test code which uses the real
559 // name in the table.
560 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
561 if ($moduleinfo->availabilityconditionsjson
!== '') {
562 $cm->availability
= $moduleinfo->availabilityconditionsjson
;
564 $cm->availability
= null;
566 } else if (property_exists($moduleinfo, 'availability')) {
567 $cm->availability
= $moduleinfo->availability
;
569 // If there is any availability data, verify it.
570 if ($cm->availability
) {
571 $tree = new \core_availability\tree
(json_decode($cm->availability
));
572 // Save time and database space by setting null if the only data
574 if ($tree->is_empty()) {
575 $cm->availability
= null;
579 if (isset($moduleinfo->showdescription
)) {
580 $cm->showdescription
= $moduleinfo->showdescription
;
582 $cm->showdescription
= 0;
585 $DB->update_record('course_modules', $cm);
587 $modcontext = context_module
::instance($moduleinfo->coursemodule
);
589 // Update embedded links and save files.
590 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_MOD_INTRO
, true)) {
591 $moduleinfo->intro
= file_save_draft_area_files($moduleinfo->introeditor
['itemid'], $modcontext->id
,
592 'mod_'.$moduleinfo->modulename
, 'intro', 0,
593 array('subdirs'=>true), $moduleinfo->introeditor
['text']);
594 $moduleinfo->introformat
= $moduleinfo->introeditor
['format'];
595 unset($moduleinfo->introeditor
);
597 // Get the a copy of the grade_item before it is modified incase we need to scale the grades.
598 $oldgradeitem = null;
599 $newgradeitem = null;
600 if (!empty($data->grade_rescalegrades
) && $data->grade_rescalegrades
== 'yes') {
601 // Fetch the grade item before it is updated.
602 $oldgradeitem = grade_item
::fetch(array('itemtype' => 'mod',
603 'itemmodule' => $moduleinfo->modulename
,
604 'iteminstance' => $moduleinfo->instance
,
606 'courseid' => $moduleinfo->course
));
609 $updateinstancefunction = $moduleinfo->modulename
."_update_instance";
610 if (!$updateinstancefunction($moduleinfo, $mform)) {
611 print_error('cannotupdatemod', '', course_get_url($course, $cm->section
), $moduleinfo->modulename
);
614 // This needs to happen AFTER the grademin/grademax have already been updated.
615 if (!empty($data->grade_rescalegrades
) && $data->grade_rescalegrades
== 'yes') {
616 // Get the grade_item after the update call the activity to scale the grades.
617 $newgradeitem = grade_item
::fetch(array('itemtype' => 'mod',
618 'itemmodule' => $moduleinfo->modulename
,
619 'iteminstance' => $moduleinfo->instance
,
621 'courseid' => $moduleinfo->course
));
622 if ($newgradeitem && $oldgradeitem->gradetype
== GRADE_TYPE_VALUE
&& $newgradeitem->gradetype
== GRADE_TYPE_VALUE
) {
626 $oldgradeitem->grademin
,
627 $oldgradeitem->grademax
,
628 $newgradeitem->grademin
,
629 $newgradeitem->grademax
631 if (!component_callback('mod_' . $moduleinfo->modulename
, 'rescale_activity_grades', $params)) {
632 print_error('cannotreprocessgrades', '', course_get_url($course, $cm->section
), $moduleinfo->modulename
);
637 // Make sure visibility is set correctly (in particular in calendar).
638 if (has_capability('moodle/course:activityvisibility', $modcontext)) {
639 set_coursemodule_visible($moduleinfo->coursemodule
, $moduleinfo->visible
, $moduleinfo->visibleoncoursepage
);
642 if (isset($moduleinfo->cmidnumber
)) { // Label.
643 // Set cm idnumber - uniqueness is already verified by form validation.
644 set_coursemodule_idnumber($moduleinfo->coursemodule
, $moduleinfo->cmidnumber
);
647 // Update module tags.
648 if (core_tag_tag
::is_enabled('core', 'course_modules') && isset($moduleinfo->tags
)) {
649 core_tag_tag
::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule
, $modcontext, $moduleinfo->tags
);
652 // Now that module is fully updated, also update completion data if required.
653 // (this will wipe all user completion data and recalculate it)
654 if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked
)) {
655 $completion->reset_all_state($cm);
657 $cm->name
= $moduleinfo->name
;
658 \core\event\course_module_updated
::create_from_cm($cm, $modcontext)->trigger();
660 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
662 return array($cm, $moduleinfo);
666 * Include once the module lib file.
668 * @param string $modulename module name of the lib to include
669 * @throws moodle_exception if lib.php file for the module does not exist
671 function include_modulelib($modulename) {
673 $modlib = "$CFG->dirroot/mod/$modulename/lib.php";
674 if (file_exists($modlib)) {
675 include_once($modlib);
677 throw new moodle_exception('modulemissingcode', '', '', $modlib);
682 * Get module information data required for updating the module.
684 * @param stdClass $cm course module object
685 * @param stdClass $course course object
686 * @return array required data for updating a module
689 function get_moduleinfo_data($cm, $course) {
692 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
694 $data->coursemodule
= $cm->id
;
695 $data->section
= $cw->section
; // The section number itself - relative!!! (section column in course_sections)
696 $data->visible
= $cm->visible
; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
697 $data->visibleoncoursepage
= $cm->visibleoncoursepage
;
698 $data->cmidnumber
= $cm->idnumber
; // The cm IDnumber
699 $data->groupmode
= groups_get_activity_groupmode($cm); // locked later if forced
700 $data->groupingid
= $cm->groupingid
;
701 $data->course
= $course->id
;
702 $data->module
= $module->id
;
703 $data->modulename
= $module->name
;
704 $data->instance
= $cm->instance
;
705 $data->completion
= $cm->completion
;
706 $data->completionview
= $cm->completionview
;
707 $data->completionexpected
= $cm->completionexpected
;
708 $data->completionusegrade
= is_null($cm->completiongradeitemnumber
) ?
0 : 1;
709 $data->completiongradeitemnumber
= $cm->completiongradeitemnumber
;
710 $data->showdescription
= $cm->showdescription
;
711 $data->tags
= core_tag_tag
::get_item_tags_array('core', 'course_modules', $cm->id
);
712 if (!empty($CFG->enableavailability
)) {
713 $data->availabilityconditionsjson
= $cm->availability
;
716 if (plugin_supports('mod', $data->modulename
, FEATURE_MOD_INTRO
, true)) {
717 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
718 $currentintro = file_prepare_draft_area($draftid_editor, $context->id
, 'mod_'.$data->modulename
, 'intro', 0, array('subdirs'=>true), $data->intro
);
719 $data->introeditor
= array('text'=>$currentintro, 'format'=>$data->introformat
, 'itemid'=>$draftid_editor);
722 if (plugin_supports('mod', $data->modulename
, FEATURE_ADVANCED_GRADING
, false)
723 and has_capability('moodle/grade:managegradingforms', $context)) {
724 require_once($CFG->dirroot
.'/grade/grading/lib.php');
725 $gradingman = get_grading_manager($context, 'mod_'.$data->modulename
);
726 $data->_advancedgradingdata
['methods'] = $gradingman->get_available_methods();
727 $areas = $gradingman->get_available_areas();
729 foreach ($areas as $areaname => $areatitle) {
730 $gradingman->set_area($areaname);
731 $method = $gradingman->get_active_method();
732 $data->_advancedgradingdata
['areas'][$areaname] = array(
733 'title' => $areatitle,
736 $formfield = 'advancedgradingmethod_'.$areaname;
737 $data->{$formfield} = $method;
741 $component = "mod_{$data->modulename}";
742 $items = grade_item
::fetch_all([
744 'itemmodule' => $data->modulename
,
745 'iteminstance' => $data->instance
,
746 'courseid' => $course->id
,
750 // Add existing outcomes.
751 foreach ($items as $item) {
752 if (!empty($item->outcomeid
)) {
753 $data->{'outcome_' . $item->outcomeid
} = 1;
754 } else if (isset($item->gradepass
)) {
755 $gradepassfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $item->itemnumber
, 'gradepass');
756 $data->{$gradepassfieldname} = format_float($item->gradepass
, $item->get_decimals());
761 // set category if present
763 foreach ($items as $item) {
764 if (!isset($gradecat[$item->itemnumber
])) {
765 $gradecat[$item->itemnumber
] = $item->categoryid
;
767 if ($gradecat[$item->itemnumber
] != $item->categoryid
) {
769 $gradecat[$item->itemnumber
] = false;
772 foreach ($gradecat as $itemnumber => $cat) {
773 if ($cat !== false) {
774 $gradecatfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $itemnumber, 'gradecat');
775 // Do not set if mixed categories present.
776 $data->{$gradecatfieldname} = $cat;
780 return array($cm, $context, $module, $data, $cw);
784 * Prepare the standard module information for a new module instance.
786 * @param stdClass $course course object
787 * @param string $modulename module name
788 * @param int $section section number
789 * @return array module information about other required data
792 function prepare_new_moduleinfo_data($course, $modulename, $section) {
795 list($module, $context, $cw) = can_add_moduleinfo($course, $modulename, $section);
799 $data = new stdClass();
800 $data->section
= $section; // The section number itself - relative!!! (section column in course_sections)
801 $data->visible
= $cw->visible
;
802 $data->course
= $course->id
;
803 $data->module
= $module->id
;
804 $data->modulename
= $module->name
;
805 $data->groupmode
= $course->groupmode
;
806 $data->groupingid
= $course->defaultgroupingid
;
808 $data->instance
= '';
809 $data->coursemodule
= '';
811 // Apply completion defaults.
812 $defaults = \core_completion\manager
::get_default_completion($course, $module);
813 foreach ($defaults as $key => $value) {
814 $data->$key = $value;
817 if (plugin_supports('mod', $data->modulename
, FEATURE_MOD_INTRO
, true)) {
818 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
819 file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true));
820 $data->introeditor
= array('text'=>'', 'format'=>FORMAT_HTML
, 'itemid'=>$draftid_editor); // TODO: add better default
823 if (plugin_supports('mod', $data->modulename
, FEATURE_ADVANCED_GRADING
, false)
824 and has_capability('moodle/grade:managegradingforms', $context)) {
825 require_once($CFG->dirroot
.'/grade/grading/lib.php');
827 $data->_advancedgradingdata
['methods'] = grading_manager
::available_methods();
828 $areas = grading_manager
::available_areas('mod_'.$module->name
);
830 foreach ($areas as $areaname => $areatitle) {
831 $data->_advancedgradingdata
['areas'][$areaname] = array(
832 'title' => $areatitle,
835 $formfield = 'advancedgradingmethod_'.$areaname;
836 $data->{$formfield} = '';
840 return array($module, $context, $cw, $cm, $data);