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;
283 require_once($CFG->libdir
.'/grade/grade_outcome.php');
284 // Add outcomes if requested.
285 if ($hasoutcomes && $outcomes = grade_outcome
::fetch_all_available($course->id
)) {
286 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes.
287 $max_itemnumber = 999;
289 foreach($items as $item) {
290 if ($item->itemnumber
> $max_itemnumber) {
291 $max_itemnumber = $item->itemnumber
;
296 foreach($outcomes as $outcome) {
297 $elname = 'outcome_'.$outcome->id
;
299 if (property_exists($moduleinfo, $elname) and $moduleinfo->$elname) {
300 // Check if this is a new outcome grade item.
302 $outcomeexists = false;
303 foreach($items as $item) {
304 if ($item->outcomeid
== $outcome->id
) {
305 $outcomeexists = true;
309 if ($outcomeexists) {
316 $outcomeitem = new grade_item();
317 $outcomeitem->courseid
= $course->id
;
318 $outcomeitem->itemtype
= 'mod';
319 $outcomeitem->itemmodule
= $moduleinfo->modulename
;
320 $outcomeitem->iteminstance
= $moduleinfo->instance
;
321 $outcomeitem->itemnumber
= $max_itemnumber;
322 $outcomeitem->itemname
= $outcome->fullname
;
323 $outcomeitem->outcomeid
= $outcome->id
;
324 $outcomeitem->gradetype
= GRADE_TYPE_SCALE
;
325 $outcomeitem->scaleid
= $outcome->scaleid
;
326 $outcomeitem->insert();
329 // Move the new outcome into the same category and immediately after the first grade item.
330 $item = reset($items);
331 $outcomeitem->set_parent($item->categoryid
);
332 $outcomeitem->move_after_sortorder($item->sortorder
);
333 } else if (isset($moduleinfo->gradecat
)) {
334 $outcomeitem->set_parent($moduleinfo->gradecat
);
340 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_ADVANCED_GRADING
, false)
341 and has_capability('moodle/grade:managegradingforms', $modcontext)) {
342 require_once($CFG->dirroot
.'/grade/grading/lib.php');
343 $gradingman = get_grading_manager($modcontext, 'mod_'.$moduleinfo->modulename
);
344 $showgradingmanagement = false;
345 foreach ($gradingman->get_available_areas() as $areaname => $aretitle) {
346 $formfield = 'advancedgradingmethod_'.$areaname;
347 if (isset($moduleinfo->{$formfield})) {
348 $gradingman->set_area($areaname);
349 $methodchanged = $gradingman->set_active_method($moduleinfo->{$formfield});
350 if (empty($moduleinfo->{$formfield})) {
351 // Going back to the simple direct grading is not a reason to open the management screen.
352 $methodchanged = false;
354 $showgradingmanagement = $showgradingmanagement ||
$methodchanged;
357 // Update grading management information.
358 $moduleinfo->gradingman
= $gradingman;
359 $moduleinfo->showgradingmanagement
= $showgradingmanagement;
362 rebuild_course_cache($course->id
, true);
364 grade_regrade_final_grades($course->id
);
367 // To be removed (deprecated) with MDL-67526 (both lines).
368 require_once($CFG->libdir
.'/plagiarismlib.php');
369 plagiarism_save_form_elements($moduleinfo);
371 // Allow plugins to extend the course module form.
372 $moduleinfo = plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course);
378 * Set module info default values for the unset module attributs.
380 * @param object $moduleinfo the current known data of the module
381 * @return object the completed module info
383 function set_moduleinfo_defaults($moduleinfo) {
385 if (empty($moduleinfo->coursemodule
)) {
388 $moduleinfo->instance
= '';
389 $moduleinfo->coursemodule
= '';
392 $cm = get_coursemodule_from_id('', $moduleinfo->coursemodule
, 0, false, MUST_EXIST
);
393 $moduleinfo->instance
= $cm->instance
;
394 $moduleinfo->coursemodule
= $cm->id
;
397 $moduleinfo->modulename
= clean_param($moduleinfo->modulename
, PARAM_PLUGIN
);
399 if (!isset($moduleinfo->groupingid
)) {
400 $moduleinfo->groupingid
= 0;
403 if (!isset($moduleinfo->name
)) { // Label.
404 $moduleinfo->name
= $moduleinfo->modulename
;
407 if (!isset($moduleinfo->completion
)) {
408 $moduleinfo->completion
= COMPLETION_DISABLED
;
410 if (!isset($moduleinfo->completionview
)) {
411 $moduleinfo->completionview
= COMPLETION_VIEW_NOT_REQUIRED
;
413 if (!isset($moduleinfo->completionexpected
)) {
414 $moduleinfo->completionexpected
= 0;
417 // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
418 if (isset($moduleinfo->completionusegrade
) && $moduleinfo->completionusegrade
) {
419 $moduleinfo->completiongradeitemnumber
= 0;
420 } else if (!isset($moduleinfo->completiongradeitemnumber
)) {
421 $moduleinfo->completiongradeitemnumber
= null;
424 if (!isset($moduleinfo->conditiongradegroup
)) {
425 $moduleinfo->conditiongradegroup
= array();
427 if (!isset($moduleinfo->conditionfieldgroup
)) {
428 $moduleinfo->conditionfieldgroup
= array();
430 if (!isset($moduleinfo->visibleoncoursepage
)) {
431 $moduleinfo->visibleoncoursepage
= 1;
438 * Check that the user can add a module. Also returns some information like the module, context and course section info.
439 * The fucntion create the course section if it doesn't exist.
441 * @param object $course the course of the module
442 * @param object $modulename the module name
443 * @param object $section the section of the module
444 * @return array list containing module, context, course section.
445 * @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
447 function can_add_moduleinfo($course, $modulename, $section) {
450 $module = $DB->get_record('modules', array('name'=>$modulename), '*', MUST_EXIST
);
452 $context = context_course
::instance($course->id
);
453 require_capability('moodle/course:manageactivities', $context);
455 course_create_sections_if_missing($course, $section);
456 $cw = get_fast_modinfo($course)->get_section_info($section);
458 if (!course_allowed_module($course, $module->name
)) {
459 print_error('moduledisable');
462 return array($module, $context, $cw);
466 * Check if user is allowed to update module info and returns related item/data to the module.
468 * @param object $cm course module
469 * @return array - list of course module, context, module, moduleinfo, and course section.
470 * @throws moodle_exception if user is not allowed to perform the action
472 function can_update_moduleinfo($cm) {
475 // Check the $USER has the right capability.
476 $context = context_module
::instance($cm->id
);
477 require_capability('moodle/course:manageactivities', $context);
479 // Check module exists.
480 $module = $DB->get_record('modules', array('id'=>$cm->module
), '*', MUST_EXIST
);
482 // Check the moduleinfo exists.
483 $data = $DB->get_record($module->name
, array('id'=>$cm->instance
), '*', MUST_EXIST
);
485 // Check the course section exists.
486 $cw = $DB->get_record('course_sections', array('id'=>$cm->section
), '*', MUST_EXIST
);
488 return array($cm, $context, $module, $data, $cw);
493 * Update the module info.
494 * This function doesn't check the user capabilities. It updates the course module and the module instance.
495 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
497 * @param object $cm course module
498 * @param object $moduleinfo module info
499 * @param object $course course of the module
500 * @param object $mform - the mform is required by some specific module in the function MODULE_update_instance(). This is due to a hack in this function.
501 * @return array list of course module and module info.
503 function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
506 $data = new stdClass();
508 $data = $mform->get_data();
511 // Attempt to include module library before we make any changes to DB.
512 include_modulelib($moduleinfo->modulename
);
514 $moduleinfo->course
= $course->id
;
515 $moduleinfo = set_moduleinfo_defaults($moduleinfo);
517 if (!empty($course->groupmodeforce
) or !isset($moduleinfo->groupmode
)) {
518 $moduleinfo->groupmode
= $cm->groupmode
; // Keep original.
521 // Update course module first.
522 $cm->groupmode
= $moduleinfo->groupmode
;
523 if (isset($moduleinfo->groupingid
)) {
524 $cm->groupingid
= $moduleinfo->groupingid
;
527 $completion = new completion_info($course);
528 if ($completion->is_enabled()) {
529 // Completion settings that would affect users who have already completed
530 // the activity may be locked; if so, these should not be updated.
531 if (!empty($moduleinfo->completionunlocked
)) {
532 $cm->completion
= $moduleinfo->completion
;
533 if ($moduleinfo->completiongradeitemnumber
=== '') {
534 $cm->completiongradeitemnumber
= null;
536 $cm->completiongradeitemnumber
= $moduleinfo->completiongradeitemnumber
;
538 $cm->completionview
= $moduleinfo->completionview
;
540 // The expected date does not affect users who have completed the activity,
541 // so it is safe to update it regardless of the lock status.
542 $cm->completionexpected
= $moduleinfo->completionexpected
;
544 if (!empty($CFG->enableavailability
)) {
545 // This code is used both when submitting the form, which uses a long
546 // name to avoid clashes, and by unit test code which uses the real
547 // name in the table.
548 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
549 if ($moduleinfo->availabilityconditionsjson
!== '') {
550 $cm->availability
= $moduleinfo->availabilityconditionsjson
;
552 $cm->availability
= null;
554 } else if (property_exists($moduleinfo, 'availability')) {
555 $cm->availability
= $moduleinfo->availability
;
557 // If there is any availability data, verify it.
558 if ($cm->availability
) {
559 $tree = new \core_availability\tree
(json_decode($cm->availability
));
560 // Save time and database space by setting null if the only data
562 if ($tree->is_empty()) {
563 $cm->availability
= null;
567 if (isset($moduleinfo->showdescription
)) {
568 $cm->showdescription
= $moduleinfo->showdescription
;
570 $cm->showdescription
= 0;
573 $DB->update_record('course_modules', $cm);
575 $modcontext = context_module
::instance($moduleinfo->coursemodule
);
577 // Update embedded links and save files.
578 if (plugin_supports('mod', $moduleinfo->modulename
, FEATURE_MOD_INTRO
, true)) {
579 $moduleinfo->intro
= file_save_draft_area_files($moduleinfo->introeditor
['itemid'], $modcontext->id
,
580 'mod_'.$moduleinfo->modulename
, 'intro', 0,
581 array('subdirs'=>true), $moduleinfo->introeditor
['text']);
582 $moduleinfo->introformat
= $moduleinfo->introeditor
['format'];
583 unset($moduleinfo->introeditor
);
585 // Get the a copy of the grade_item before it is modified incase we need to scale the grades.
586 $oldgradeitem = null;
587 $newgradeitem = null;
588 if (!empty($data->grade_rescalegrades
) && $data->grade_rescalegrades
== 'yes') {
589 // Fetch the grade item before it is updated.
590 $oldgradeitem = grade_item
::fetch(array('itemtype' => 'mod',
591 'itemmodule' => $moduleinfo->modulename
,
592 'iteminstance' => $moduleinfo->instance
,
594 'courseid' => $moduleinfo->course
));
597 $updateinstancefunction = $moduleinfo->modulename
."_update_instance";
598 if (!$updateinstancefunction($moduleinfo, $mform)) {
599 print_error('cannotupdatemod', '', course_get_url($course, $cm->section
), $moduleinfo->modulename
);
602 // This needs to happen AFTER the grademin/grademax have already been updated.
603 if (!empty($data->grade_rescalegrades
) && $data->grade_rescalegrades
== 'yes') {
604 // Get the grade_item after the update call the activity to scale the grades.
605 $newgradeitem = grade_item
::fetch(array('itemtype' => 'mod',
606 'itemmodule' => $moduleinfo->modulename
,
607 'iteminstance' => $moduleinfo->instance
,
609 'courseid' => $moduleinfo->course
));
610 if ($newgradeitem && $oldgradeitem->gradetype
== GRADE_TYPE_VALUE
&& $newgradeitem->gradetype
== GRADE_TYPE_VALUE
) {
614 $oldgradeitem->grademin
,
615 $oldgradeitem->grademax
,
616 $newgradeitem->grademin
,
617 $newgradeitem->grademax
619 if (!component_callback('mod_' . $moduleinfo->modulename
, 'rescale_activity_grades', $params)) {
620 print_error('cannotreprocessgrades', '', course_get_url($course, $cm->section
), $moduleinfo->modulename
);
625 // Make sure visibility is set correctly (in particular in calendar).
626 if (has_capability('moodle/course:activityvisibility', $modcontext)) {
627 set_coursemodule_visible($moduleinfo->coursemodule
, $moduleinfo->visible
, $moduleinfo->visibleoncoursepage
);
630 if (isset($moduleinfo->cmidnumber
)) { // Label.
631 // Set cm idnumber - uniqueness is already verified by form validation.
632 set_coursemodule_idnumber($moduleinfo->coursemodule
, $moduleinfo->cmidnumber
);
635 // Update module tags.
636 if (core_tag_tag
::is_enabled('core', 'course_modules') && isset($moduleinfo->tags
)) {
637 core_tag_tag
::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule
, $modcontext, $moduleinfo->tags
);
640 // Now that module is fully updated, also update completion data if required.
641 // (this will wipe all user completion data and recalculate it)
642 if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked
)) {
643 $completion->reset_all_state($cm);
645 $cm->name
= $moduleinfo->name
;
646 \core\event\course_module_updated
::create_from_cm($cm, $modcontext)->trigger();
648 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
650 return array($cm, $moduleinfo);
654 * Include once the module lib file.
656 * @param string $modulename module name of the lib to include
657 * @throws moodle_exception if lib.php file for the module does not exist
659 function include_modulelib($modulename) {
661 $modlib = "$CFG->dirroot/mod/$modulename/lib.php";
662 if (file_exists($modlib)) {
663 include_once($modlib);
665 throw new moodle_exception('modulemissingcode', '', '', $modlib);
670 * Get module information data required for updating the module.
672 * @param stdClass $cm course module object
673 * @param stdClass $course course object
674 * @return array required data for updating a module
677 function get_moduleinfo_data($cm, $course) {
680 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
682 $data->coursemodule
= $cm->id
;
683 $data->section
= $cw->section
; // The section number itself - relative!!! (section column in course_sections)
684 $data->visible
= $cm->visible
; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
685 $data->visibleoncoursepage
= $cm->visibleoncoursepage
;
686 $data->cmidnumber
= $cm->idnumber
; // The cm IDnumber
687 $data->groupmode
= groups_get_activity_groupmode($cm); // locked later if forced
688 $data->groupingid
= $cm->groupingid
;
689 $data->course
= $course->id
;
690 $data->module
= $module->id
;
691 $data->modulename
= $module->name
;
692 $data->instance
= $cm->instance
;
693 $data->completion
= $cm->completion
;
694 $data->completionview
= $cm->completionview
;
695 $data->completionexpected
= $cm->completionexpected
;
696 $data->completionusegrade
= is_null($cm->completiongradeitemnumber
) ?
0 : 1;
697 $data->completiongradeitemnumber
= $cm->completiongradeitemnumber
;
698 $data->showdescription
= $cm->showdescription
;
699 $data->tags
= core_tag_tag
::get_item_tags_array('core', 'course_modules', $cm->id
);
700 if (!empty($CFG->enableavailability
)) {
701 $data->availabilityconditionsjson
= $cm->availability
;
704 if (plugin_supports('mod', $data->modulename
, FEATURE_MOD_INTRO
, true)) {
705 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
706 $currentintro = file_prepare_draft_area($draftid_editor, $context->id
, 'mod_'.$data->modulename
, 'intro', 0, array('subdirs'=>true), $data->intro
);
707 $data->introeditor
= array('text'=>$currentintro, 'format'=>$data->introformat
, 'itemid'=>$draftid_editor);
710 if (plugin_supports('mod', $data->modulename
, FEATURE_ADVANCED_GRADING
, false)
711 and has_capability('moodle/grade:managegradingforms', $context)) {
712 require_once($CFG->dirroot
.'/grade/grading/lib.php');
713 $gradingman = get_grading_manager($context, 'mod_'.$data->modulename
);
714 $data->_advancedgradingdata
['methods'] = $gradingman->get_available_methods();
715 $areas = $gradingman->get_available_areas();
717 foreach ($areas as $areaname => $areatitle) {
718 $gradingman->set_area($areaname);
719 $method = $gradingman->get_active_method();
720 $data->_advancedgradingdata
['areas'][$areaname] = array(
721 'title' => $areatitle,
724 $formfield = 'advancedgradingmethod_'.$areaname;
725 $data->{$formfield} = $method;
729 $component = "mod_{$data->modulename}";
730 $items = grade_item
::fetch_all([
732 'itemmodule' => $data->modulename
,
733 'iteminstance' => $data->instance
,
734 'courseid' => $course->id
,
738 // Add existing outcomes.
739 foreach ($items as $item) {
740 if (!empty($item->outcomeid
)) {
741 $data->{'outcome_' . $item->outcomeid
} = 1;
742 } else if (isset($item->gradepass
)) {
743 $gradepassfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $item->itemnumber
, 'gradepass');
744 $data->{$gradepassfieldname} = format_float($item->gradepass
, $item->get_decimals());
749 // set category if present
751 foreach ($items as $item) {
752 if (!isset($gradecat[$item->itemnumber
])) {
753 $gradecat[$item->itemnumber
] = $item->categoryid
;
755 if ($gradecat[$item->itemnumber
] != $item->categoryid
) {
757 $gradecat[$item->itemnumber
] = false;
760 foreach ($gradecat as $itemnumber => $cat) {
761 if ($cat !== false) {
762 $gradecatfieldname = component_gradeitems
::get_field_name_for_itemnumber($component, $itemnumber, 'gradecat');
763 // Do not set if mixed categories present.
764 $data->{$gradecatfieldname} = $cat;
768 return array($cm, $context, $module, $data, $cw);
772 * Prepare the standard module information for a new module instance.
774 * @param stdClass $course course object
775 * @param string $modulename module name
776 * @param int $section section number
777 * @return array module information about other required data
780 function prepare_new_moduleinfo_data($course, $modulename, $section) {
783 list($module, $context, $cw) = can_add_moduleinfo($course, $modulename, $section);
787 $data = new stdClass();
788 $data->section
= $section; // The section number itself - relative!!! (section column in course_sections)
789 $data->visible
= $cw->visible
;
790 $data->course
= $course->id
;
791 $data->module
= $module->id
;
792 $data->modulename
= $module->name
;
793 $data->groupmode
= $course->groupmode
;
794 $data->groupingid
= $course->defaultgroupingid
;
796 $data->instance
= '';
797 $data->coursemodule
= '';
799 // Apply completion defaults.
800 $defaults = \core_completion\manager
::get_default_completion($course, $module);
801 foreach ($defaults as $key => $value) {
802 $data->$key = $value;
805 if (plugin_supports('mod', $data->modulename
, FEATURE_MOD_INTRO
, true)) {
806 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
807 file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true));
808 $data->introeditor
= array('text'=>'', 'format'=>FORMAT_HTML
, 'itemid'=>$draftid_editor); // TODO: add better default
811 if (plugin_supports('mod', $data->modulename
, FEATURE_ADVANCED_GRADING
, false)
812 and has_capability('moodle/grade:managegradingforms', $context)) {
813 require_once($CFG->dirroot
.'/grade/grading/lib.php');
815 $data->_advancedgradingdata
['methods'] = grading_manager
::available_methods();
816 $areas = grading_manager
::available_areas('mod_'.$module->name
);
818 foreach ($areas as $areaname => $areatitle) {
819 $data->_advancedgradingdata
['areas'][$areaname] = array(
820 'title' => $areatitle,
823 $formfield = 'advancedgradingmethod_'.$areaname;
824 $data->{$formfield} = '';
828 return array($module, $context, $cw, $cm, $data);