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 require_once($CFG->dirroot.'/course/lib.php');
35 * The function does not check user capabilities.
36 * The function creates course module, module instance, add the module to the correct section.
37 * It also trigger common action that need to be done after adding/updating a module.
39 * @param object $moduleinfo the moudle data
40 * @param object $course the course of the module
41 * @param object $mform this is required by an existing hack to deal with files during MODULENAME_add_instance()
42 * @return object the updated module info
44 function add_moduleinfo($moduleinfo, $course, $mform = null) {
47 // Attempt to include module library before we make any changes to DB.
48 include_modulelib($moduleinfo->modulename);
50 $moduleinfo->course = $course->id;
51 $moduleinfo = set_moduleinfo_defaults($moduleinfo);
53 if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
54 $moduleinfo->groupmode = 0; // Do not set groupmode.
57 // First add course_module record because we need the context.
58 $newcm = new stdClass();
59 $newcm->course = $course->id;
60 $newcm->module = $moduleinfo->module;
61 $newcm->instance = 0; // Not known yet, will be updated later (this is similar to restore code).
62 $newcm->visible = $moduleinfo->visible;
63 $newcm->visibleold = $moduleinfo->visible;
64 if (isset($moduleinfo->cmidnumber)) {
65 $newcm->idnumber = $moduleinfo->cmidnumber;
67 $newcm->groupmode = $moduleinfo->groupmode;
68 $newcm->groupingid = $moduleinfo->groupingid;
69 $completion = new completion_info($course);
70 if ($completion->is_enabled()) {
71 $newcm->completion = $moduleinfo->completion;
72 $newcm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
73 $newcm->completionview = $moduleinfo->completionview;
74 $newcm->completionexpected = $moduleinfo->completionexpected;
76 if(!empty($CFG->enableavailability)) {
77 // This code is used both when submitting the form, which uses a long
78 // name to avoid clashes, and by unit test code which uses the real
80 $newcm->availability = null;
81 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
82 if ($moduleinfo->availabilityconditionsjson !== '') {
83 $newcm->availability = $moduleinfo->availabilityconditionsjson;
85 } else if (property_exists($moduleinfo, 'availability')) {
86 $newcm->availability = $moduleinfo->availability;
88 // If there is any availability data, verify it.
89 if ($newcm->availability) {
90 $tree = new \core_availability\tree(json_decode($newcm->availability));
91 // Save time and database space by setting null if the only data
93 if ($tree->is_empty()) {
94 $newcm->availability = null;
98 if (isset($moduleinfo->showdescription)) {
99 $newcm->showdescription = $moduleinfo->showdescription;
101 $newcm->showdescription = 0;
104 // From this point we make database changes, so start transaction.
105 $transaction = $DB->start_delegated_transaction();
107 if (!$moduleinfo->coursemodule = add_course_module($newcm)) {
108 print_error('cannotaddcoursemodule');
111 if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true) &&
112 isset($moduleinfo->introeditor)) {
113 $introeditor = $moduleinfo->introeditor;
114 unset($moduleinfo->introeditor);
115 $moduleinfo->intro = $introeditor['text'];
116 $moduleinfo->introformat = $introeditor['format'];
119 $addinstancefunction = $moduleinfo->modulename."_add_instance";
121 $returnfromfunc = $addinstancefunction($moduleinfo, $mform);
122 } catch (moodle_exception $e) {
123 $returnfromfunc = $e;
125 if (!$returnfromfunc or !is_number($returnfromfunc)) {
126 // Undo everything we can. This is not necessary for databases which
127 // support transactions, but improves consistency for other databases.
128 context_helper::delete_instance(CONTEXT_MODULE, $moduleinfo->coursemodule);
129 $DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule));
131 if ($returnfromfunc instanceof moodle_exception) {
132 throw $returnfromfunc;
133 } else if (!is_number($returnfromfunc)) {
134 print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section));
136 print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section), $moduleinfo->modulename);
140 $moduleinfo->instance = $returnfromfunc;
142 $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$moduleinfo->coursemodule));
144 // Update embedded links and save files.
145 $modcontext = context_module::instance($moduleinfo->coursemodule);
146 if (!empty($introeditor)) {
147 // This will respect a module that has set a value for intro in it's modname_add_instance() function.
148 $introeditor['text'] = $moduleinfo->intro;
150 $moduleinfo->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,
151 'mod_'.$moduleinfo->modulename, 'intro', 0,
152 array('subdirs'=>true), $introeditor['text']);
153 $DB->set_field($moduleinfo->modulename, 'intro', $moduleinfo->intro, array('id'=>$moduleinfo->instance));
157 if (core_tag_tag::is_enabled('core', 'course_modules') && isset($moduleinfo->tags)) {
158 core_tag_tag::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule, $modcontext, $moduleinfo->tags);
161 // Course_modules and course_sections each contain a reference to each other.
162 // So we have to update one of them twice.
163 $sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule, $moduleinfo->section);
165 // Trigger event based on the action we did.
166 // Api create_from_cm expects modname and id property, and we don't want to modify $moduleinfo since we are returning it.
167 $eventdata = clone $moduleinfo;
168 $eventdata->modname = $eventdata->modulename;
169 $eventdata->id = $eventdata->coursemodule;
170 $event = \core\event\course_module_created::create_from_cm($eventdata, $modcontext);
173 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
174 $transaction->allow_commit();
180 * Hook for plugins to take action when a module is created or updated.
182 * @param stdClass $moduleinfo the module info
183 * @param stdClass $course the course of the module
185 * @return stdClass moduleinfo updated by plugins.
187 function plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course) {
188 $callbacks = get_plugins_with_function('coursemodule_edit_post_actions', 'lib.php');
189 foreach ($callbacks as $type => $plugins) {
190 foreach ($plugins as $plugin => $pluginfunction) {
191 $moduleinfo = $pluginfunction($moduleinfo, $course);
198 * Common create/update module module actions that need to be processed as soon as a module is created/updaded.
199 * For example:create grade parent category, add outcomes, rebuild caches, regrade, save plagiarism settings...
200 * Please note this api does not trigger events as of MOODLE 2.6. Please trigger events before calling this api.
202 * @param object $moduleinfo the module info
203 * @param object $course the course of the module
205 * @return object moduleinfo update with grading management info
207 function edit_module_post_actions($moduleinfo, $course) {
209 require_once($CFG->libdir.'/gradelib.php');
211 $modcontext = context_module::instance($moduleinfo->coursemodule);
212 $hasgrades = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_HAS_GRADE, false);
213 $hasoutcomes = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_OUTCOMES, true);
215 // Sync idnumber with grade_item.
216 if ($hasgrades && $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$moduleinfo->modulename,
217 'iteminstance'=>$moduleinfo->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
218 $gradeupdate = false;
219 if ($grade_item->idnumber != $moduleinfo->cmidnumber) {
220 $grade_item->idnumber = $moduleinfo->cmidnumber;
223 if (isset($moduleinfo->gradepass) && $grade_item->gradepass != $moduleinfo->gradepass) {
224 $grade_item->gradepass = $moduleinfo->gradepass;
228 $grade_item->update();
233 $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$moduleinfo->modulename,
234 'iteminstance'=>$moduleinfo->instance, 'courseid'=>$course->id));
239 // Create parent category if requested and move to correct parent category.
240 if ($items and isset($moduleinfo->gradecat)) {
241 if ($moduleinfo->gradecat == -1) {
242 $grade_category = new grade_category();
243 $grade_category->courseid = $course->id;
244 $grade_category->fullname = $moduleinfo->name;
245 $grade_category->insert();
247 $parent = $grade_item->get_parent_category();
248 $grade_category->set_parent($parent->id);
250 $moduleinfo->gradecat = $grade_category->id;
253 foreach ($items as $itemid=>$unused) {
254 $items[$itemid]->set_parent($moduleinfo->gradecat);
255 if ($itemid == $grade_item->id) {
256 // Use updated grade_item.
257 $grade_item = $items[$itemid];
262 require_once($CFG->libdir.'/grade/grade_outcome.php');
263 // Add outcomes if requested.
264 if ($hasoutcomes && $outcomes = grade_outcome::fetch_all_available($course->id)) {
265 $grade_items = array();
267 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes.
268 $max_itemnumber = 999;
270 foreach($items as $item) {
271 if ($item->itemnumber > $max_itemnumber) {
272 $max_itemnumber = $item->itemnumber;
277 foreach($outcomes as $outcome) {
278 $elname = 'outcome_'.$outcome->id;
280 if (property_exists($moduleinfo, $elname) and $moduleinfo->$elname) {
281 // So we have a request for new outcome grade item?
283 $outcomeexists = false;
284 foreach($items as $item) {
285 if ($item->outcomeid == $outcome->id) {
286 $outcomeexists = true;
290 if ($outcomeexists) {
297 $outcome_item = new grade_item();
298 $outcome_item->courseid = $course->id;
299 $outcome_item->itemtype = 'mod';
300 $outcome_item->itemmodule = $moduleinfo->modulename;
301 $outcome_item->iteminstance = $moduleinfo->instance;
302 $outcome_item->itemnumber = $max_itemnumber;
303 $outcome_item->itemname = $outcome->fullname;
304 $outcome_item->outcomeid = $outcome->id;
305 $outcome_item->gradetype = GRADE_TYPE_SCALE;
306 $outcome_item->scaleid = $outcome->scaleid;
307 $outcome_item->insert();
309 // Move the new outcome into correct category and fix sortorder if needed.
311 $outcome_item->set_parent($grade_item->categoryid);
312 $outcome_item->move_after_sortorder($grade_item->sortorder);
314 } else if (isset($moduleinfo->gradecat)) {
315 $outcome_item->set_parent($moduleinfo->gradecat);
321 if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_ADVANCED_GRADING, false)
322 and has_capability('moodle/grade:managegradingforms', $modcontext)) {
323 require_once($CFG->dirroot.'/grade/grading/lib.php');
324 $gradingman = get_grading_manager($modcontext, 'mod_'.$moduleinfo->modulename);
325 $showgradingmanagement = false;
326 foreach ($gradingman->get_available_areas() as $areaname => $aretitle) {
327 $formfield = 'advancedgradingmethod_'.$areaname;
328 if (isset($moduleinfo->{$formfield})) {
329 $gradingman->set_area($areaname);
330 $methodchanged = $gradingman->set_active_method($moduleinfo->{$formfield});
331 if (empty($moduleinfo->{$formfield})) {
332 // Going back to the simple direct grading is not a reason to open the management screen.
333 $methodchanged = false;
335 $showgradingmanagement = $showgradingmanagement || $methodchanged;
338 // Update grading management information.
339 $moduleinfo->gradingman = $gradingman;
340 $moduleinfo->showgradingmanagement = $showgradingmanagement;
343 rebuild_course_cache($course->id, true);
345 grade_regrade_final_grades($course->id);
347 require_once($CFG->libdir.'/plagiarismlib.php');
348 plagiarism_save_form_elements($moduleinfo);
350 // Allow plugins to extend the course module form.
351 $moduleinfo = plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course);
358 * Set module info default values for the unset module attributs.
360 * @param object $moduleinfo the current known data of the module
361 * @return object the completed module info
363 function set_moduleinfo_defaults($moduleinfo) {
365 if (empty($moduleinfo->coursemodule)) {
368 $moduleinfo->instance = '';
369 $moduleinfo->coursemodule = '';
372 $cm = get_coursemodule_from_id('', $moduleinfo->coursemodule, 0, false, MUST_EXIST);
373 $moduleinfo->instance = $cm->instance;
374 $moduleinfo->coursemodule = $cm->id;
377 $moduleinfo->modulename = clean_param($moduleinfo->modulename, PARAM_PLUGIN);
379 if (!isset($moduleinfo->groupingid)) {
380 $moduleinfo->groupingid = 0;
383 if (!isset($moduleinfo->name)) { // Label.
384 $moduleinfo->name = $moduleinfo->modulename;
387 if (!isset($moduleinfo->completion)) {
388 $moduleinfo->completion = COMPLETION_DISABLED;
390 if (!isset($moduleinfo->completionview)) {
391 $moduleinfo->completionview = COMPLETION_VIEW_NOT_REQUIRED;
393 if (!isset($moduleinfo->completionexpected)) {
394 $moduleinfo->completionexpected = 0;
397 // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
398 if (isset($moduleinfo->completionusegrade) && $moduleinfo->completionusegrade) {
399 $moduleinfo->completiongradeitemnumber = 0;
401 $moduleinfo->completiongradeitemnumber = null;
404 if (!isset($moduleinfo->conditiongradegroup)) {
405 $moduleinfo->conditiongradegroup = array();
407 if (!isset($moduleinfo->conditionfieldgroup)) {
408 $moduleinfo->conditionfieldgroup = array();
415 * Check that the user can add a module. Also returns some information like the module, context and course section info.
416 * The fucntion create the course section if it doesn't exist.
418 * @param object $course the course of the module
419 * @param object $modulename the module name
420 * @param object $section the section of the module
421 * @return array list containing module, context, course section.
422 * @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
424 function can_add_moduleinfo($course, $modulename, $section) {
427 $module = $DB->get_record('modules', array('name'=>$modulename), '*', MUST_EXIST);
429 $context = context_course::instance($course->id);
430 require_capability('moodle/course:manageactivities', $context);
432 course_create_sections_if_missing($course, $section);
433 $cw = get_fast_modinfo($course)->get_section_info($section);
435 if (!course_allowed_module($course, $module->name)) {
436 print_error('moduledisable');
439 return array($module, $context, $cw);
443 * Check if user is allowed to update module info and returns related item/data to the module.
445 * @param object $cm course module
446 * @return array - list of course module, context, module, moduleinfo, and course section.
447 * @throws moodle_exception if user is not allowed to perform the action
449 function can_update_moduleinfo($cm) {
452 // Check the $USER has the right capability.
453 $context = context_module::instance($cm->id);
454 require_capability('moodle/course:manageactivities', $context);
456 // Check module exists.
457 $module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST);
459 // Check the moduleinfo exists.
460 $data = $DB->get_record($module->name, array('id'=>$cm->instance), '*', MUST_EXIST);
462 // Check the course section exists.
463 $cw = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
465 return array($cm, $context, $module, $data, $cw);
470 * Update the module info.
471 * This function doesn't check the user capabilities. It updates the course module and the module instance.
472 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
474 * @param object $cm course module
475 * @param object $moduleinfo module info
476 * @param object $course course of the module
477 * @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.
478 * @return array list of course module and module info.
480 function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
483 $data = new stdClass();
485 $data = $mform->get_data();
488 // Attempt to include module library before we make any changes to DB.
489 include_modulelib($moduleinfo->modulename);
491 $moduleinfo->course = $course->id;
492 $moduleinfo = set_moduleinfo_defaults($moduleinfo);
494 if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
495 $moduleinfo->groupmode = $cm->groupmode; // Keep original.
498 // Update course module first.
499 $cm->groupmode = $moduleinfo->groupmode;
500 if (isset($moduleinfo->groupingid)) {
501 $cm->groupingid = $moduleinfo->groupingid;
504 $completion = new completion_info($course);
505 if ($completion->is_enabled()) {
506 // Completion settings that would affect users who have already completed
507 // the activity may be locked; if so, these should not be updated.
508 if (!empty($moduleinfo->completionunlocked)) {
509 $cm->completion = $moduleinfo->completion;
510 $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
511 $cm->completionview = $moduleinfo->completionview;
513 // The expected date does not affect users who have completed the activity,
514 // so it is safe to update it regardless of the lock status.
515 $cm->completionexpected = $moduleinfo->completionexpected;
517 if (!empty($CFG->enableavailability)) {
518 // This code is used both when submitting the form, which uses a long
519 // name to avoid clashes, and by unit test code which uses the real
520 // name in the table.
521 if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
522 if ($moduleinfo->availabilityconditionsjson !== '') {
523 $cm->availability = $moduleinfo->availabilityconditionsjson;
525 $cm->availability = null;
527 } else if (property_exists($moduleinfo, 'availability')) {
528 $cm->availability = $moduleinfo->availability;
530 // If there is any availability data, verify it.
531 if ($cm->availability) {
532 $tree = new \core_availability\tree(json_decode($cm->availability));
533 // Save time and database space by setting null if the only data
535 if ($tree->is_empty()) {
536 $cm->availability = null;
540 if (isset($moduleinfo->showdescription)) {
541 $cm->showdescription = $moduleinfo->showdescription;
543 $cm->showdescription = 0;
546 $DB->update_record('course_modules', $cm);
548 $modcontext = context_module::instance($moduleinfo->coursemodule);
550 // Update embedded links and save files.
551 if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
552 $moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id,
553 'mod_'.$moduleinfo->modulename, 'intro', 0,
554 array('subdirs'=>true), $moduleinfo->introeditor['text']);
555 $moduleinfo->introformat = $moduleinfo->introeditor['format'];
556 unset($moduleinfo->introeditor);
558 // Get the a copy of the grade_item before it is modified incase we need to scale the grades.
559 $oldgradeitem = null;
560 $newgradeitem = null;
561 if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') {
562 // Fetch the grade item before it is updated.
563 $oldgradeitem = grade_item::fetch(array('itemtype' => 'mod',
564 'itemmodule' => $moduleinfo->modulename,
565 'iteminstance' => $moduleinfo->instance,
567 'courseid' => $moduleinfo->course));
570 $updateinstancefunction = $moduleinfo->modulename."_update_instance";
571 if (!$updateinstancefunction($moduleinfo, $mform)) {
572 print_error('cannotupdatemod', '', course_get_url($course, $cm->section), $moduleinfo->modulename);
575 // This needs to happen AFTER the grademin/grademax have already been updated.
576 if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') {
577 // Get the grade_item after the update call the activity to scale the grades.
578 $newgradeitem = grade_item::fetch(array('itemtype' => 'mod',
579 'itemmodule' => $moduleinfo->modulename,
580 'iteminstance' => $moduleinfo->instance,
582 'courseid' => $moduleinfo->course));
583 if ($newgradeitem && $oldgradeitem->gradetype == GRADE_TYPE_VALUE && $newgradeitem->gradetype == GRADE_TYPE_VALUE) {
587 $oldgradeitem->grademin,
588 $oldgradeitem->grademax,
589 $newgradeitem->grademin,
590 $newgradeitem->grademax
592 if (!component_callback('mod_' . $moduleinfo->modulename, 'rescale_activity_grades', $params)) {
593 print_error('cannotreprocessgrades', '', course_get_url($course, $cm->section), $moduleinfo->modulename);
598 // Make sure visibility is set correctly (in particular in calendar).
599 if (has_capability('moodle/course:activityvisibility', $modcontext)) {
600 set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
603 if (isset($moduleinfo->cmidnumber)) { // Label.
604 // Set cm idnumber - uniqueness is already verified by form validation.
605 set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
608 // Update module tags.
609 if (core_tag_tag::is_enabled('core', 'course_modules') && isset($moduleinfo->tags)) {
610 core_tag_tag::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule, $modcontext, $moduleinfo->tags);
613 // Now that module is fully updated, also update completion data if required.
614 // (this will wipe all user completion data and recalculate it)
615 if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
616 $completion->reset_all_state($cm);
618 $cm->name = $moduleinfo->name;
619 \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
621 $moduleinfo = edit_module_post_actions($moduleinfo, $course);
623 return array($cm, $moduleinfo);
627 * Include once the module lib file.
629 * @param string $modulename module name of the lib to include
630 * @throws moodle_exception if lib.php file for the module does not exist
632 function include_modulelib($modulename) {
634 $modlib = "$CFG->dirroot/mod/$modulename/lib.php";
635 if (file_exists($modlib)) {
636 include_once($modlib);
638 throw new moodle_exception('modulemissingcode', '', '', $modlib);
643 * Get module information data required for updating the module.
645 * @param stdClass $cm course module object
646 * @param stdClass $course course object
647 * @return array required data for updating a module
650 function get_moduleinfo_data($cm, $course) {
653 list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
655 $data->coursemodule = $cm->id;
656 $data->section = $cw->section; // The section number itself - relative!!! (section column in course_sections)
657 $data->visible = $cm->visible; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
658 $data->cmidnumber = $cm->idnumber; // The cm IDnumber
659 $data->groupmode = groups_get_activity_groupmode($cm); // locked later if forced
660 $data->groupingid = $cm->groupingid;
661 $data->course = $course->id;
662 $data->module = $module->id;
663 $data->modulename = $module->name;
664 $data->instance = $cm->instance;
665 $data->completion = $cm->completion;
666 $data->completionview = $cm->completionview;
667 $data->completionexpected = $cm->completionexpected;
668 $data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
669 $data->showdescription = $cm->showdescription;
670 $data->tags = core_tag_tag::get_item_tags_array('core', 'course_modules', $cm->id);
671 if (!empty($CFG->enableavailability)) {
672 $data->availabilityconditionsjson = $cm->availability;
675 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
676 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
677 $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
678 $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
681 if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
682 and has_capability('moodle/grade:managegradingforms', $context)) {
683 require_once($CFG->dirroot.'/grade/grading/lib.php');
684 $gradingman = get_grading_manager($context, 'mod_'.$data->modulename);
685 $data->_advancedgradingdata['methods'] = $gradingman->get_available_methods();
686 $areas = $gradingman->get_available_areas();
688 foreach ($areas as $areaname => $areatitle) {
689 $gradingman->set_area($areaname);
690 $method = $gradingman->get_active_method();
691 $data->_advancedgradingdata['areas'][$areaname] = array(
692 'title' => $areatitle,
695 $formfield = 'advancedgradingmethod_'.$areaname;
696 $data->{$formfield} = $method;
700 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename,
701 'iteminstance'=>$data->instance, 'courseid'=>$course->id))) {
702 // Add existing outcomes.
703 foreach ($items as $item) {
704 if (!empty($item->outcomeid)) {
705 $data->{'outcome_' . $item->outcomeid} = 1;
706 } else if (isset($item->gradepass)) {
707 $decimalpoints = $item->get_decimals();
708 $data->gradepass = format_float($item->gradepass, $decimalpoints);
712 // set category if present
714 foreach ($items as $item) {
715 if ($gradecat === false) {
716 $gradecat = $item->categoryid;
719 if ($gradecat != $item->categoryid) {
725 if ($gradecat !== false) {
726 // do not set if mixed categories present
727 $data->gradecat = $gradecat;
730 return array($cm, $context, $module, $data, $cw);
734 * Prepare the standard module information for a new module instance.
736 * @param stdClass $course course object
737 * @param string $modulename module name
738 * @param int $section section number
739 * @return array module information about other required data
742 function prepare_new_moduleinfo_data($course, $modulename, $section) {
745 list($module, $context, $cw) = can_add_moduleinfo($course, $modulename, $section);
749 $data = new stdClass();
750 $data->section = $section; // The section number itself - relative!!! (section column in course_sections)
751 $data->visible = $cw->visible;
752 $data->course = $course->id;
753 $data->module = $module->id;
754 $data->modulename = $module->name;
755 $data->groupmode = $course->groupmode;
756 $data->groupingid = $course->defaultgroupingid;
758 $data->instance = '';
759 $data->coursemodule = '';
761 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
762 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
763 file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true));
764 $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
767 if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
768 and has_capability('moodle/grade:managegradingforms', $context)) {
769 require_once($CFG->dirroot.'/grade/grading/lib.php');
771 $data->_advancedgradingdata['methods'] = grading_manager::available_methods();
772 $areas = grading_manager::available_areas('mod_'.$module->name);
774 foreach ($areas as $areaname => $areatitle) {
775 $data->_advancedgradingdata['areas'][$areaname] = array(
776 'title' => $areatitle,
779 $formfield = 'advancedgradingmethod_'.$areaname;
780 $data->{$formfield} = '';
784 return array($module, $context, $cw, $cm, $data);