/** * Manage inplace editable saves. * * @param string $itemtype The type of item. * @param int $itemid The ID of the item. * @param mixed $newvalue The new value * @return string */ function tool_usertours_inplace_editable($itemtype, $itemid, $newvalue) { $context = \context_system::instance(); external_api::validate_context($context); require_capability('tool/usertours:managetours', $context); if ($itemtype === 'tourname') { $tour = helper::get_tour($itemid); $tour->set_name($newvalue)->persist(); return helper::render_tourname_inplace_editable($tour); } else { if ($itemtype === 'tourdescription') { $tour = helper::get_tour($itemid); $tour->set_description($newvalue)->persist(); return helper::render_tourdescription_inplace_editable($tour); } else { if ($itemtype === 'tourenabled') { $tour = helper::get_tour($itemid); $tour->set_enabled(!!$newvalue)->persist(); return helper::render_tourenabled_inplace_editable($tour); } else { if ($itemtype === 'stepname') { $step = helper::get_step($itemid); $step->set_title($newvalue)->persist(); return helper::render_stepname_inplace_editable($step); } } } } }
/** * Implements callback inplace_editable() allowing to edit values in-place * * @param string $itemtype * @param int $itemid * @param mixed $newvalue * @return \core\output\inplace_editable */ function core_tag_inplace_editable($itemtype, $itemid, $newvalue) { \external_api::validate_context(context_system::instance()); if ($itemtype === 'tagname') { return \core_tag\output\tagname::update($itemid, $newvalue); } else { if ($itemtype === 'tagareaenable') { return \core_tag\output\tagareaenabled::update($itemid, $newvalue); } else { if ($itemtype === 'tagareacollection') { return \core_tag\output\tagareacollection::update($itemid, $newvalue); } else { if ($itemtype === 'tagareashowstandard') { return \core_tag\output\tagareashowstandard::update($itemid, $newvalue); } else { if ($itemtype === 'tagcollname') { return \core_tag\output\tagcollname::update($itemid, $newvalue); } else { if ($itemtype === 'tagcollsearchable') { return \core_tag\output\tagcollsearchable::update($itemid, $newvalue); } else { if ($itemtype === 'tagflag') { return \core_tag\output\tagflag::update($itemid, $newvalue); } else { if ($itemtype === 'tagisstandard') { return \core_tag\output\tagisstandard::update($itemid, $newvalue); } } } } } } } } }
/** * Updates cohort name and returns instance of this object * * @param int $cohortid * @param string $newvalue * @return static */ public static function update($cohortid, $newvalue) { global $DB; $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST); $cohortcontext = \context::instance_by_id($cohort->contextid); \external_api::validate_context($cohortcontext); require_capability('moodle/cohort:manage', $cohortcontext); $record = (object) array('id' => $cohort->id, 'idnumber' => $newvalue, 'contextid' => $cohort->contextid); cohort_update_cohort($record); $cohort->idnumber = $newvalue; return new static($cohort); }
/** * Updates course module name * * @param int $itemid course module id * @param string $newvalue new name * @return static */ public static function update($itemid, $newvalue) { $context = context_module::instance($itemid); // Check access. \external_api::validate_context($context); require_capability('moodle/course:manageactivities', $context); // Update value. set_coursemodule_name($itemid, $newvalue); $coursemodulerecord = get_coursemodule_from_id('', $itemid, 0, false, MUST_EXIST); // Return instance. $cm = get_fast_modinfo($coursemodulerecord->course)->get_cm($itemid); return new static($cm, true); }
/** * Updates cohort name and returns instance of this object * * @param int $cohortid * @param string $newvalue * @return static */ public static function update($cohortid, $newvalue) { global $DB; $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST); $cohortcontext = \context::instance_by_id($cohort->contextid); \external_api::validate_context($cohortcontext); require_capability('moodle/cohort:manage', $cohortcontext); $newvalue = clean_param($newvalue, PARAM_TEXT); if (strval($newvalue) !== '') { $record = (object) array('id' => $cohort->id, 'name' => $newvalue, 'contextid' => $cohort->contextid); cohort_update_cohort($record); $cohort->name = $newvalue; } return new static($cohort); }
/** * Updates the value in the database and modifies this object respectively. * * ALWAYS check user permissions before performing an update! Throw exceptions if permissions are not sufficient * or value is not legit. * * @param stdClass $section * @param string $itemtype * @param mixed $newvalue * @return \core\output\inplace_editable */ public function inplace_editable_update_section_name($section, $itemtype, $newvalue) { if ($itemtype === 'sectionname' || $itemtype === 'sectionnamenl') { $context = context_course::instance($section->course); external_api::validate_context($context); require_capability('moodle/course:update', $context); $newtitle = clean_param($newvalue, PARAM_TEXT); if (strval($section->name) !== strval($newtitle)) { course_update_section($section->course, $section, array('name' => $newtitle)); } return $this->inplace_editable_render_section_name($section, $itemtype === 'sectionname', true); } }
/** * Validate a list of courses, returning the complete course objects for valid courses. * * @param array $courseids A list of course ids * @param array $courses An array of courses already pre-fetched, indexed by course id. * @return array An array of courses and the validation warnings */ public static function validate_courses($courseids, $courses = array()) { // Delete duplicates. $courseids = array_unique($courseids); $warnings = array(); // Remove courses which are not even requested. $courses = array_intersect_key($courses, array_flip($courseids)); foreach ($courseids as $cid) { // Check the user can function in this context. try { $context = context_course::instance($cid); external_api::validate_context($context); if (!isset($courses[$cid])) { $courses[$cid] = get_course($cid); } } catch (Exception $e) { unset($courses[$cid]); $warnings[] = array('item' => 'course', 'itemid' => $cid, 'warningcode' => '1', 'message' => 'No access rights in course context'); } } return array($courses, $warnings); }
/** * Validate a list of courses, returning the complete course objects for valid courses. * * @param array $courseids A list of course ids * @return array An array of courses and the validation warnings */ public static function validate_courses($courseids) { // Delete duplicates. $courseids = array_unique($courseids); $courses = array(); $warnings = array(); foreach ($courseids as $cid) { // Check the user can function in this context. try { $context = context_course::instance($cid); external_api::validate_context($context); $courses[$cid] = get_course($cid); } catch (Exception $e) { $warnings[] = array('item' => 'course', 'itemid' => $cid, 'warningcode' => '1', 'message' => 'No access rights in course context'); } } return array($courses, $warnings); }