case 'updatetitle': require_capability('moodle/course:manageactivities', $modcontext); require_once $CFG->libdir . '/gradelib.php'; $cm = get_coursemodule_from_id('', $id, 0, false, MUST_EXIST); $module = new stdClass(); $module->id = $cm->instance; // Escape strings as they would be by mform if (!empty($CFG->formatstringstriptags)) { $module->name = clean_param($title, PARAM_TEXT); } else { $module->name = clean_param($title, PARAM_CLEANHTML); } if (!empty($module->name)) { $DB->update_record($cm->modname, $module); $cm->name = $module->name; \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); rebuild_course_cache($cm->course); } else { $module->name = $cm->name; } // Attempt to update the grade item if relevant $grademodule = $DB->get_record($cm->modname, array('id' => $cm->instance)); $grademodule->cmidnumber = $cm->idnumber; $grademodule->modname = $cm->modname; grade_update_mod_grades($grademodule); // We need to return strings after they've been through filters for multilang $stringoptions = new stdClass(); $stringoptions->context = $coursecontext; echo json_encode(array('instancename' => html_entity_decode(format_string($module->name, true, $stringoptions)))); break; }
/** * Updates the course section * * This function does not check permissions or clean values - this has to be done prior to calling it. * * @param int|stdClass $course * @param stdClass $section record from course_sections table - it will be updated with the new values * @param array|stdClass $data */ function course_update_section($course, $section, $data) { global $DB; $courseid = is_object($course) ? $course->id : (int) $course; // Some fields can not be updated using this method. $data = array_diff_key((array) $data, array('id', 'course', 'section', 'sequence')); $changevisibility = array_key_exists('visible', $data) && (bool) $data['visible'] != (bool) $section->visible; if (array_key_exists('name', $data) && \core_text::strlen($data['name']) > 255) { throw new moodle_exception('maximumchars', 'moodle', '', 255); } // Update record in the DB and course format options. $data['id'] = $section->id; $DB->update_record('course_sections', $data); rebuild_course_cache($courseid, true); course_get_format($courseid)->update_section_format_options($data); // Update fields of the $section object. foreach ($data as $key => $value) { if (property_exists($section, $key)) { $section->{$key} = $value; } } // Trigger an event for course section update. $event = \core\event\course_section_updated::create(array('objectid' => $section->id, 'courseid' => $courseid, 'context' => context_course::instance($courseid), 'other' => array('sectionnum' => $section->section))); $event->trigger(); // If section visibility was changed, hide the modules in this section too. if ($changevisibility && !empty($section->sequence)) { $modules = explode(',', $section->sequence); foreach ($modules as $moduleid) { if ($cm = get_coursemodule_from_id(null, $moduleid, $courseid)) { if ($data['visible']) { // As we unhide the section, we use the previously saved visibility stored in visibleold. set_coursemodule_visible($moduleid, $cm->visibleold); } else { // We hide the section, so we hide the module but we store the original state in visibleold. set_coursemodule_visible($moduleid, 0); $DB->set_field('course_modules', 'visibleold', $cm->visible, array('id' => $moduleid)); } \core\event\course_module_updated::create_from_cm($cm)->trigger(); } } } }
/** * For a given course section, marks it visible or hidden, * and does the same for every activity in that section * * @param int $courseid course id * @param int $sectionnumber The section number to adjust * @param int $visibility The new visibility * @return array A list of resources which were hidden in the section */ function set_section_visible($courseid, $sectionnumber, $visibility) { global $DB; $resourcestotoggle = array(); if ($section = $DB->get_record("course_sections", array("course" => $courseid, "section" => $sectionnumber))) { $DB->set_field("course_sections", "visible", "{$visibility}", array("id" => $section->id)); if (!empty($section->sequence)) { $modules = explode(",", $section->sequence); foreach ($modules as $moduleid) { if ($cm = get_coursemodule_from_id(null, $moduleid, $courseid)) { if ($visibility) { // As we unhide the section, we use the previously saved visibility stored in visibleold. set_coursemodule_visible($moduleid, $cm->visibleold); } else { // We hide the section, so we hide the module but we store the original state in visibleold. set_coursemodule_visible($moduleid, 0); $DB->set_field('course_modules', 'visibleold', $cm->visible, array('id' => $moduleid)); } \core\event\course_module_updated::create_from_cm($cm)->trigger(); } } } rebuild_course_cache($courseid, true); // Determine which modules are visible for AJAX update if (!empty($modules)) { list($insql, $params) = $DB->get_in_or_equal($modules); $select = 'id ' . $insql . ' AND visible = ?'; array_push($params, $visibility); if (!$visibility) { $select .= ' AND visibleold = 1'; } $resourcestotoggle = $DB->get_fieldset_select('course_modules', 'id', $select, $params); } } return $resourcestotoggle; }
/** * Tests for create_from_cm method. */ public function test_course_module_create_from_cm() { $this->resetAfterTest(); $this->setAdminUser(); // Create course and modules. $course = $this->getDataGenerator()->create_course(array('numsections' => 5)); // Generate an assignment. $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); // Get the module context. $modcontext = context_module::instance($assign->cmid); // Get course module. $cm = get_coursemodule_from_id(null, $assign->cmid, $course->id, false, MUST_EXIST); // Create an event from course module. $event = \core\event\course_module_updated::create_from_cm($cm, $modcontext); // Trigger the events. $sink = $this->redirectEvents(); $event->trigger(); $events = $sink->get_events(); $event2 = array_pop($events); // Test event data. $this->assertInstanceOf('\\core\\event\\course_module_updated', $event); $this->assertEquals($cm->id, $event2->objectid); $this->assertEquals($modcontext, $event2->get_context()); $this->assertEquals($cm->modname, $event2->other['modulename']); $this->assertEquals($cm->instance, $event2->other['instanceid']); $this->assertEquals($cm->name, $event2->other['name']); $this->assertEventContextNotUsed($event2); $this->assertSame('mod_updated', $event2->get_legacy_eventname()); $arr = array(array($cm->course, "course", "update mod", "../mod/assign/view.php?id={$cm->id}", "assign {$cm->instance}"), array($cm->course, "assign", "update", "view.php?id={$cm->id}", $cm->instance, $cm->id)); $this->assertEventLegacyLogData($arr, $event); }
public static function move_item($user, $itemid, $courseid, $locationid) { global $DB, $USER; $params = self::validate_parameters(self::move_item_parameters(), array('user' => $user, 'itemid' => $itemid, 'courseid' => $courseid, 'locationid' => $locationid)); $item = $DB->get_record('equella', array('id' => $params['itemid']), '*', MUST_EXIST); self::check_modify_permissions($params['user'], $item->course); $cm = get_coursemodule_from_instance('equella', $item->id, $item->course, false, MUST_EXIST); $oldCourse = $cm->course; $newCourse = $params['courseid']; $newSection = $DB->get_record('course_sections', array('course' => $newCourse, 'section' => $params['locationid']), '*', MUST_EXIST); delete_mod_from_section($cm->id, $cm->section); $cm->section = $newSection->id; $cm->course = $newCourse; $item->course = $newCourse; $item->section = $newSection->section; $item->instance = $cm->instance; $item->cm = $cm->id; $success = $DB->update_record("course_modules", $cm); if ($success) { $success = equella_update_instance($item); if (!($sectionid = course_add_cm_to_section($newCourse, $cm->id, $newSection->section))) { print_error('cannotaddcoursemoduletosection'); return null; } if (class_exists('core\\event\\course_module_updated')) { $event = \core\event\course_module_updated::create_from_cm($cm); $event->trigger(); } else { $eventdata = new stdClass(); $eventdata->modulename = 'equella'; $eventdata->name = $item->name; $eventdata->cmid = $cm->id; $eventdata->courseid = $item->course; $eventdata->userid = $USER->id; events_trigger('mod_updated', $eventdata); add_to_log($item->course, "course", "update mod", "../mod/equella/view.php?id={$cm->id}", "equella {$item->instance}"); add_to_log($item->course, "equella", "update equella resource", "view.php?id={$cm->id}", "{$item->instance}", $cm->id); } rebuild_course_cache($oldCourse); rebuild_course_cache($newCourse); } return array('success' => $success); }
/** * Update the module info. * This function doesn't check the user capabilities. It updates the course module and the module instance. * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...). * * @param object $cm course module * @param object $moduleinfo module info * @param object $course course of the module * @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. * @return array list of course module and module info. */ function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) { global $DB, $CFG; // Attempt to include module library before we make any changes to DB. include_modulelib($moduleinfo->modulename); $moduleinfo->course = $course->id; $moduleinfo = set_moduleinfo_defaults($moduleinfo); if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) { $moduleinfo->groupmode = $cm->groupmode; // Keep original. } // Update course module first. $cm->groupmode = $moduleinfo->groupmode; if (isset($moduleinfo->groupingid)) { $cm->groupingid = $moduleinfo->groupingid; } $completion = new completion_info($course); if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) { // Update completion settings. $cm->completion = $moduleinfo->completion; $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber; $cm->completionview = $moduleinfo->completionview; $cm->completionexpected = $moduleinfo->completionexpected; } if (!empty($CFG->enableavailability)) { // This code is used both when submitting the form, which uses a long // name to avoid clashes, and by unit test code which uses the real // name in the table. if (property_exists($moduleinfo, 'availabilityconditionsjson')) { if ($moduleinfo->availabilityconditionsjson !== '') { $cm->availability = $moduleinfo->availabilityconditionsjson; } else { $cm->availability = null; } } else { if (property_exists($moduleinfo, 'availability')) { $cm->availability = $moduleinfo->availability; } } } if (isset($moduleinfo->showdescription)) { $cm->showdescription = $moduleinfo->showdescription; } else { $cm->showdescription = 0; } $DB->update_record('course_modules', $cm); $modcontext = context_module::instance($moduleinfo->coursemodule); // Update embedded links and save files. if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) { $moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id, 'mod_' . $moduleinfo->modulename, 'intro', 0, array('subdirs' => true), $moduleinfo->introeditor['text']); $moduleinfo->introformat = $moduleinfo->introeditor['format']; unset($moduleinfo->introeditor); } $updateinstancefunction = $moduleinfo->modulename . "_update_instance"; if (!$updateinstancefunction($moduleinfo, $mform)) { print_error('cannotupdatemod', '', course_get_url($course, $cw->section), $moduleinfo->modulename); } // Make sure visibility is set correctly (in particular in calendar). if (has_capability('moodle/course:activityvisibility', $modcontext)) { set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible); } if (isset($moduleinfo->cmidnumber)) { // Label. // Set cm idnumber - uniqueness is already verified by form validation. set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber); } // Now that module is fully updated, also update completion data if required. // (this will wipe all user completion data and recalculate it) if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) { $completion->reset_all_state($cm); } $cm->name = $moduleinfo->name; \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); $moduleinfo = edit_module_post_actions($moduleinfo, $course); return array($cm, $moduleinfo); }
function RWSAUQuiz() { global $CFG; global $DB; global $RWSLB; global $RWSUID; RWSCMAuth(); RWSCRAuth(); RWSCMUSvc(); RWSCMMaint(); $r_pm = RWSGSOpt("quizid", PARAM_ALPHANUM); if ($r_pm === false || strlen($r_pm) == 0) { RWSSErr("2067"); } $r_qzmi = intval($r_pm); $r_cmod = RWSCMUQuiz($r_qzmi); $r_sfl = RWSGSOpt("sfile", RWSPRF); if ($r_sfl === false) { $r_sn = RWSGSOpt("sname", PARAM_FILE); $r_sd = RWSGSOpt("sdata", PARAM_NOTAGS); $r_ecd = true; } else { $r_sn = $r_sfl->filename; $r_sd = $r_sfl->filedata; $r_ecd = false; } $r_imp = false; if ($r_sd !== false && strlen($r_sd) > 0) { if ($r_sn === false || strlen($r_sn) == 0) { RWSSErr("2075"); } $r_sn = clean_filename($r_sn); $r_imp = true; } $r_cid = $r_cmod->course; $r_crs = RWSCMUCourse($r_cid, true); $r_mr = $DB->get_record("modules", array("id" => $r_cmod->module)); if ($r_mr === false) { RWSSErr("2043"); } $r_qiz = $DB->get_record($r_mr->name, array("id" => $r_cmod->instance)); if ($r_qiz === false) { RWSSErr("2044"); } $r_ren = false; $r_pm = RWSGSOpt("rename", PARAM_TEXT); if ($r_pm !== false && strlen($r_pm) > 0) { $r_ren = trim(clean_text(strip_tags($r_pm, "<lang><span>"))); $r_qiz->name = $r_ren; } if ($r_ren === false) { if ($r_sd === false || strlen($r_sd) == 0) { RWSSErr("2080"); } } $r_sec = $DB->get_record("course_sections", array("id" => $r_cmod->section)); if ($r_sec === false) { RWSSErr("2079"); } $r_qiz->coursemodule = $r_cmod->id; $r_qiz->section = $r_sec->section; $r_qiz->visible = $r_cmod->visible; $r_qiz->cmidnumber = $r_cmod->idnumber; $r_qiz->groupmode = groups_get_activity_groupmode($r_cmod); $r_qiz->groupingid = $r_cmod->groupingid; $r_qiz->groupmembersonly = $r_cmod->groupmembersonly; $r_qiz->course = $r_cid; $r_qiz->module = $r_mr->id; $r_qiz->modulename = $r_mr->name; $r_qiz->instance = $r_cmod->instance; if (respondusws_floatcompare($CFG->version, 2011120500.0, 2) >= 0) { $r_qiz->showdescription = 0; } $r_cpl = new completion_info($r_crs); if ($r_cpl->is_enabled()) { $r_qiz->completion = $r_cmod->completion; $r_qiz->completionview = $r_cmod->completionview; $r_qiz->completionexpected = $r_cmod->completionexpected; $r_qiz->completionusegrade = is_null($r_cmod->completiongradeitemnumber) ? 0 : 1; } if ($CFG->enableavailability) { $r_qiz->availablefrom = $r_cmod->availablefrom; $r_qiz->availableuntil = $r_cmod->availableuntil; if ($r_qiz->availableuntil) { $r_qiz->availableuntil = strtotime("23:59:59", $r_qiz->availableuntil); } $r_qiz->showavailability = $r_cmod->showavailability; } $r_its = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $r_qiz->modulename, 'iteminstance' => $r_qiz->instance, 'courseid' => $r_cid)); if ($r_its) { foreach ($r_its as $r_it) { if (!empty($r_it->outcomeid)) { $r_qiz->{'outcome_' . $r_it->outcomeid} = 1; } } $r_gc = false; foreach ($r_its as $r_it) { if ($r_gc === false) { $r_gc = $r_it->categoryid; continue; } if ($r_gc != $r_it->categoryid) { $r_gc = false; break; } } if ($r_gc !== false) { $r_qiz->gradecat = $r_gc; } } if ($r_imp) { RWSIQSet($r_qiz, $r_sn, $r_sd, $r_ecd); } $DB->update_record("course_modules", $r_qiz); if (is_null($r_qiz->quizpassword) && !is_null($r_qiz->password)) { $r_qiz->quizpassword = $r_qiz->password; } $r_res = quiz_update_instance($r_qiz); if (!$r_res || is_string($r_res)) { RWSSErr("2081"); } RWSSLBSet($r_qiz); set_coursemodule_visible($r_qzmi, $r_qiz->visible); if (isset($r_qiz->cmidnumber)) { set_coursemodule_idnumber($r_qzmi, $r_qiz->cmidnumber); } RWSUQGrades($r_qiz); if ($r_cpl->is_enabled() && !empty($r_qiz->completionunlocked)) { $r_cpl->reset_all_state($r_qiz); } if (respondusws_floatcompare($CFG->version, 2014051200, 2) >= 0) { $r_qiz->modname = $r_qiz->modulename; $r_qiz->id = $r_qiz->coursemodule; \core\event\course_module_updated::create_from_cm($r_qiz)->trigger(); } else { $r_evt = new stdClass(); $r_evt->modulename = $r_qiz->modulename; $r_evt->name = $r_qiz->name; $r_evt->cmid = $r_qiz->coursemodule; $r_evt->courseid = $r_qiz->course; $r_evt->userid = $RWSUID; events_trigger("mod_updated", $r_evt); } rebuild_course_cache($r_cid); grade_regrade_final_grades($r_cid); if ($RWSLB->mex || $RWSLB->bex) { if ($RWSLB->mok) { if ($RWSLB->perr) { RWSSWarn("3003"); } } else { if ($RWSLB->bok) { if ($RWSLB->perr) { RWSSWarn("3003"); } } else { RWSSWarn("3001"); } } } else { RWSSWarn("3000"); } RWSSStat("1004"); }
/** * Update the module info. * This function doesn't check the user capabilities. It updates the course module and the module instance. * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...). * * @param object $cm course module * @param object $moduleinfo module info * @param object $course course of the module * @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. * @return array list of course module and module info. */ function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) { global $DB, $CFG; $data = new stdClass(); if ($mform) { $data = $mform->get_data(); } // Attempt to include module library before we make any changes to DB. include_modulelib($moduleinfo->modulename); $moduleinfo->course = $course->id; $moduleinfo = set_moduleinfo_defaults($moduleinfo); if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) { $moduleinfo->groupmode = $cm->groupmode; // Keep original. } // Update course module first. $cm->groupmode = $moduleinfo->groupmode; if (isset($moduleinfo->groupingid)) { $cm->groupingid = $moduleinfo->groupingid; } $completion = new completion_info($course); if ($completion->is_enabled()) { // Completion settings that would affect users who have already completed // the activity may be locked; if so, these should not be updated. if (!empty($moduleinfo->completionunlocked)) { $cm->completion = $moduleinfo->completion; $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber; $cm->completionview = $moduleinfo->completionview; } // The expected date does not affect users who have completed the activity, // so it is safe to update it regardless of the lock status. $cm->completionexpected = $moduleinfo->completionexpected; } if (!empty($CFG->enableavailability)) { // This code is used both when submitting the form, which uses a long // name to avoid clashes, and by unit test code which uses the real // name in the table. if (property_exists($moduleinfo, 'availabilityconditionsjson')) { if ($moduleinfo->availabilityconditionsjson !== '') { $cm->availability = $moduleinfo->availabilityconditionsjson; } else { $cm->availability = null; } } else { if (property_exists($moduleinfo, 'availability')) { $cm->availability = $moduleinfo->availability; } } // If there is any availability data, verify it. if ($cm->availability) { $tree = new \core_availability\tree(json_decode($cm->availability)); // Save time and database space by setting null if the only data // is an empty tree. if ($tree->is_empty()) { $cm->availability = null; } } } if (isset($moduleinfo->showdescription)) { $cm->showdescription = $moduleinfo->showdescription; } else { $cm->showdescription = 0; } $DB->update_record('course_modules', $cm); $modcontext = context_module::instance($moduleinfo->coursemodule); // Update embedded links and save files. if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) { $moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id, 'mod_' . $moduleinfo->modulename, 'intro', 0, array('subdirs' => true), $moduleinfo->introeditor['text']); $moduleinfo->introformat = $moduleinfo->introeditor['format']; unset($moduleinfo->introeditor); } // Get the a copy of the grade_item before it is modified incase we need to scale the grades. $oldgradeitem = null; $newgradeitem = null; if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') { // Fetch the grade item before it is updated. $oldgradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => $moduleinfo->modulename, 'iteminstance' => $moduleinfo->instance, 'itemnumber' => 0, 'courseid' => $moduleinfo->course)); } $updateinstancefunction = $moduleinfo->modulename . "_update_instance"; if (!$updateinstancefunction($moduleinfo, $mform)) { print_error('cannotupdatemod', '', course_get_url($course, $cm->section), $moduleinfo->modulename); } // This needs to happen AFTER the grademin/grademax have already been updated. if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') { // Get the grade_item after the update call the activity to scale the grades. $newgradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => $moduleinfo->modulename, 'iteminstance' => $moduleinfo->instance, 'itemnumber' => 0, 'courseid' => $moduleinfo->course)); if ($newgradeitem && $oldgradeitem->gradetype == GRADE_TYPE_VALUE && $newgradeitem->gradetype == GRADE_TYPE_VALUE) { $params = array($course, $cm, $oldgradeitem->grademin, $oldgradeitem->grademax, $newgradeitem->grademin, $newgradeitem->grademax); if (!component_callback('mod_' . $moduleinfo->modulename, 'rescale_activity_grades', $params)) { print_error('cannotreprocessgrades', '', course_get_url($course, $cm->section), $moduleinfo->modulename); } } } // Make sure visibility is set correctly (in particular in calendar). if (has_capability('moodle/course:activityvisibility', $modcontext)) { set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible); } if (isset($moduleinfo->cmidnumber)) { // Label. // Set cm idnumber - uniqueness is already verified by form validation. set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber); } // Update module tags. if (core_tag_tag::is_enabled('core', 'course_modules') && isset($moduleinfo->tags)) { core_tag_tag::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule, $modcontext, $moduleinfo->tags); } // Now that module is fully updated, also update completion data if required. // (this will wipe all user completion data and recalculate it) if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) { $completion->reset_all_state($cm); } $cm->name = $moduleinfo->name; \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); $moduleinfo = edit_module_post_actions($moduleinfo, $course); return array($cm, $moduleinfo); }