Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

namespace core_courseformat;

< use core\event\course_module_updated; < use core_courseformat\base as course_format;
use core_courseformat\stateupdates;
> use core\event\course_module_updated;
use cm_info; use section_info; use stdClass; use course_modinfo; use moodle_exception; use context_module; use context_course;
< use cache;
/** * Contains the core course state actions. * * The methods from this class should be executed via "core_courseformat_edit" web service. * * Each format plugin could extend this class to provide new actions to the editor. * Extended classes should be locate in "format_XXX\course" namespace and * extends core_courseformat\stateactions. * * @package core_courseformat * @copyright 2021 Ferran Recio <ferran@moodle.com> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class stateactions { /** * Move course modules to another location in the same course. * * @param stateupdates $updates the affected course elements track * @param stdClass $course the course object * @param int[] $ids the list of affected course module ids * @param int $targetsectionid optional target section id * @param int $targetcmid optional target cm id */ public function cm_move( stateupdates $updates, stdClass $course, array $ids, ?int $targetsectionid = null, ?int $targetcmid = null ): void { // Validate target elements. if (!$targetsectionid && !$targetcmid) { throw new moodle_exception("Action cm_move requires targetsectionid or targetcmid"); }
< $this->validate_cms($course, $ids, __FUNCTION__); < < // Check capabilities on every activity context. < foreach ($ids as $cmid) { < $modcontext = context_module::instance($cmid); < require_capability('moodle/course:manageactivities', $modcontext); < } < < $modinfo = get_fast_modinfo($course);
> $this->validate_cms($course, $ids, __FUNCTION__, ['moodle/course:manageactivities']); > // The moveto_module function move elements before a specific target. > // To keep the order the movements must be done in descending order (last activity first). > $ids = $this->sort_cm_ids_by_course_position($course, $ids, true);
// Target cm has more priority than target section. if (!empty($targetcmid)) { $this->validate_cms($course, [$targetcmid], __FUNCTION__);
< $targetcm = $modinfo->get_cm($targetcmid); < $targetsection = $modinfo->get_section_info_by_id($targetcm->section, MUST_EXIST);
> $targetcm = get_fast_modinfo($course)->get_cm($targetcmid); > $targetsectionid = $targetcm->section;
} else { $this->validate_sections($course, [$targetsectionid], __FUNCTION__);
< $targetcm = null; < $targetsection = $modinfo->get_section_info_by_id($targetsectionid, MUST_EXIST);
} // The origin sections must be updated as well. $originalsections = [];
< $cms = $this->get_cm_info($modinfo, $ids); < foreach ($cms as $cm) { < $currentsection = $modinfo->get_section_info_by_id($cm->section, MUST_EXIST); < moveto_module($cm, $targetsection, $targetcm);
> $beforecmdid = $targetcmid; > foreach ($ids as $cmid) { > // An updated $modinfo is needed on every loop as activities list change. > $modinfo = get_fast_modinfo($course); > $cm = $modinfo->get_cm($cmid); > $currentsectionid = $cm->section; > $targetsection = $modinfo->get_section_info_by_id($targetsectionid, MUST_EXIST); > $beforecm = (!empty($beforecmdid)) ? $modinfo->get_cm($beforecmdid) : null; > if ($beforecm === null || $beforecm->id != $cmid) { > moveto_module($cm, $targetsection, $beforecm); > } > $beforecmdid = $cm->id;
$updates->add_cm_put($cm->id);
< if ($currentsection->id != $targetsection->id) { < $originalsections[$currentsection->id] = true;
> if ($currentsectionid != $targetsectionid) { > $originalsections[$currentsectionid] = true;
} // If some of the original sections are also target sections, we don't need to update them.
< if (array_key_exists($targetsection->id, $originalsections)) { < unset($originalsections[$targetsection->id]);
> if (array_key_exists($targetsectionid, $originalsections)) { > unset($originalsections[$targetsectionid]);
} } // Use section_state to return the full affected section and activities updated state. $this->cm_state($updates, $course, $ids, $targetsectionid, $targetcmid); foreach (array_keys($originalsections) as $sectionid) { $updates->add_section_put($sectionid); } } /**
> * Sort the cm ids list depending on the course position. * Move course sections to another location in the same course. > * * > * Some actions like move should be done in an specific order. * @param stateupdates $updates the affected course elements track > * * @param stdClass $course the course object > * @param stdClass $course the course object * @param int[] $ids the list of affected course module ids > * @param int[] $cmids the array of section $ids * @param int $targetsectionid optional target section id > * @param bool $descending if the sort order must be descending instead of ascending * @param int $targetcmid optional target cm id > * @return int[] the array of section ids sorted by section number */ > */ public function section_move( > protected function sort_cm_ids_by_course_position( stateupdates $updates, > stdClass $course, stdClass $course, > array $cmids, array $ids, > bool $descending = false ?int $targetsectionid = null, > ): array { ?int $targetcmid = null > $modinfo = get_fast_modinfo($course); ): void { > $cmlist = array_keys($modinfo->get_cms()); // Validate target elements. > $cmposition = []; if (!$targetsectionid) { > foreach ($cmids as $cmid) { throw new moodle_exception("Action cm_move requires targetsectionid"); > $cmposition[$cmid] = array_search($cmid, $cmlist); } > } > $sorting = ($descending) ? -1 : 1; $this->validate_sections($course, $ids, __FUNCTION__); > $sortfunction = function ($acmid, $bcmid) use ($sorting, $cmposition) { > return ($cmposition[$acmid] <=> $cmposition[$bcmid]) * $sorting; $coursecontext = context_course::instance($course->id); > }; require_capability('moodle/course:movesections', $coursecontext); > usort($cmids, $sortfunction); > return $cmids; $modinfo = get_fast_modinfo($course); > } > // Target section. > /**
$this->validate_sections($course, [$targetsectionid], __FUNCTION__); $targetsection = $modinfo->get_section_info_by_id($targetsectionid, MUST_EXIST); $affectedsections = [$targetsection->section => true]; $sections = $this->get_section_info($modinfo, $ids); foreach ($sections as $section) { $affectedsections[$section->section] = true; move_section_to($course, $section->section, $targetsection->section); } // Use section_state to return the section and activities updated state. $this->section_state($updates, $course, $ids, $targetsectionid); // All course sections can be renamed because of the resort. $allsections = $modinfo->get_section_info_all(); foreach ($allsections as $section) { // Ignore the affected sections because they are already in the updates. if (isset($affectedsections[$section->section])) { continue; } $updates->add_section_put($section->id); } // The section order is at a course level. $updates->add_course_put(); } /**
> * Move course sections after to another location in the same course. * Create a course section. > * * > * @param stateupdates $updates the affected course elements track * This method follows the same logic as changenumsections.php. > * @param stdClass $course the course object * > * @param int[] $ids the list of affected course module ids * @param stateupdates $updates the affected course elements track > * @param int $targetsectionid optional target section id * @param stdClass $course the course object > * @param int $targetcmid optional target cm id * @param int[] $ids not used > */ * @param int $targetsectionid optional target section id (if not passed section will be appended) > public function section_move_after( * @param int $targetcmid not used > stateupdates $updates, */ > stdClass $course, public function section_add( > array $ids, stateupdates $updates, > ?int $targetsectionid = null, stdClass $course, > ?int $targetcmid = null array $ids = [], > ): void { ?int $targetsectionid = null, > // Validate target elements. ?int $targetcmid = null > if (!$targetsectionid) { ): void { > throw new moodle_exception("Action section_move_after requires targetsectionid"); > } $coursecontext = context_course::instance($course->id); > require_capability('moodle/course:update', $coursecontext); > $this->validate_sections($course, $ids, __FUNCTION__); > // Get course format settings. > $coursecontext = context_course::instance($course->id); $format = course_get_format($course->id); > require_capability('moodle/course:movesections', $coursecontext); $lastsectionnumber = $format->get_last_section_number(); > $maxsections = $format->get_max_sections(); > // Section will move after the target section. This means it should be processed in > // descending order to keep the relative course order. if ($lastsectionnumber >= $maxsections) { > $this->validate_sections($course, [$targetsectionid], __FUNCTION__); throw new moodle_exception('maxsectionslimit', 'moodle', $maxsections); > $ids = $this->sort_section_ids_by_section_number($course, $ids, true); } > > $format = course_get_format($course->id); $modinfo = get_fast_modinfo($course); > $affectedsections = [$targetsectionid => true]; > // Get target section. > foreach ($ids as $id) { if ($targetsectionid) { > // An update section_info is needed as section numbers can change on every section movement. $this->validate_sections($course, [$targetsectionid], __FUNCTION__); > $modinfo = get_fast_modinfo($course); $targetsection = $modinfo->get_section_info_by_id($targetsectionid, MUST_EXIST); > $section = $modinfo->get_section_info_by_id($id, MUST_EXIST); // Inserting sections at any position except in the very end requires capability to move sections. > $targetsection = $modinfo->get_section_info_by_id($targetsectionid, MUST_EXIST); require_capability('moodle/course:movesections', $coursecontext); > $affectedsections[$section->id] = true; $insertposition = $targetsection->section + 1; > $format->move_section_after($section, $targetsection); } else { > } // Get last section. > $insertposition = 0; > // Use section_state to return the section and activities updated state. } > $this->section_state($updates, $course, $ids, $targetsectionid); > course_create_section($course, $insertposition); > // All course sections can be renamed because of the resort. > $modinfo = get_fast_modinfo($course); // Adding a section affects the full course structure. > $allsections = $modinfo->get_section_info_all(); $this->course_state($updates, $course); > foreach ($allsections as $section) { } > // Ignore the affected sections because they are already in the updates. > if (isset($affectedsections[$section->id])) { /** > continue; * Delete course sections. > } * > $updates->add_section_put($section->id); * This method follows the same logic as editsection.php. > } * > // The section order is at a course level. * @param stateupdates $updates the affected course elements track > $updates->add_course_put(); * @param stdClass $course the course object > } * @param int[] $ids section ids > * @param int $targetsectionid not used > /** * @param int $targetcmid not used > * Sort the sections ids depending on the section number. */ > * public function section_delete( > * Some actions like move should be done in an specific order. stateupdates $updates, > * stdClass $course, > * @param stdClass $course the course object array $ids = [], > * @param int[] $sectionids the array of section $ids ?int $targetsectionid = null, > * @param bool $descending if the sort order must be descending instead of ascending ?int $targetcmid = null > * @return int[] the array of section ids sorted by section number ): void { > */ > protected function sort_section_ids_by_section_number( $coursecontext = context_course::instance($course->id); > stdClass $course, require_capability('moodle/course:update', $coursecontext); > array $sectionids, require_capability('moodle/course:movesections', $coursecontext); > bool $descending = false > ): array { $modinfo = get_fast_modinfo($course); > $sorting = ($descending) ? -1 : 1; > $sortfunction = function ($asection, $bsection) use ($sorting) { foreach ($ids as $sectionid) { > return ($asection->section <=> $bsection->section) * $sorting; $section = $modinfo->get_section_info_by_id($sectionid, MUST_EXIST); > }; // Send all activity deletions. > $modinfo = get_fast_modinfo($course); if (!empty($modinfo->sections[$section->section])) { > $sections = $this->get_section_info($modinfo, $sectionids); foreach ($modinfo->sections[$section->section] as $modnumber) { > uasort($sections, $sortfunction); $cm = $modinfo->cms[$modnumber]; > return array_keys($sections); $updates->add_cm_remove($cm->id); > } } > } > /**
< $modinfo = get_fast_modinfo($course); <
}
> // We need to get the latest modinfo on each iteration because the section numbers change. > $modinfo = get_fast_modinfo($course);
// Removing a section affects the full course structure. $this->course_state($updates, $course); } /**
> * Hide course sections. * Move course cms to the right. Indent = 1. > * * > * @param stateupdates $updates the affected course elements track * @param stateupdates $updates the affected course elements track > * @param stdClass $course the course object * @param stdClass $course the course object > * @param int[] $ids section ids * @param int[] $ids cm ids > * @param int $targetsectionid not used * @param int $targetsectionid not used > * @param int $targetcmid not used * @param int $targetcmid not used > */ */ > public function section_hide( public function cm_moveright( > stateupdates $updates, stateupdates $updates, > stdClass $course, stdClass $course, > array $ids = [], array $ids = [], > ?int $targetsectionid = null, ?int $targetsectionid = null, > ?int $targetcmid = null ?int $targetcmid = null > ): void { ): void { > $this->set_section_visibility($updates, $course, $ids, 0); $this->set_cm_indentation($updates, $course, $ids, 1); > } } > > /** /** > * Show course sections. * Move course cms to the left. Indent = 0. > * * > * @param stateupdates $updates the affected course elements track * @param stateupdates $updates the affected course elements track > * @param stdClass $course the course object * @param stdClass $course the course object > * @param int[] $ids section ids * @param int[] $ids cm ids > * @param int $targetsectionid not used * @param int $targetsectionid not used > * @param int $targetcmid not used * @param int $targetcmid not used > */ */ > public function section_show( public function cm_moveleft( > stateupdates $updates, stateupdates $updates, > stdClass $course, stdClass $course, > array $ids = [], array $ids = [], > ?int $targetsectionid = null, ?int $targetsectionid = null, > ?int $targetcmid = null ?int $targetcmid = null > ): void { ): void { > $this->set_section_visibility($updates, $course, $ids, 1); $this->set_cm_indentation($updates, $course, $ids, 0); > } } > > /** /** > * Show course sections. * Internal method to define the cm indentation level. > * * > * @param stateupdates $updates the affected course elements track * @param stateupdates $updates the affected course elements track > * @param stdClass $course the course object * @param stdClass $course the course object > * @param int[] $ids section ids * @param int[] $ids cm ids > * @param int $visible the new visible value * @param int $indent new value for indentation > */ */ > protected function set_section_visibility ( protected function set_cm_indentation( > stateupdates $updates, stateupdates $updates, > stdClass $course, stdClass $course, > array $ids, array $ids, > int $visible int $indent > ) { ): void { > $this->validate_sections($course, $ids, __FUNCTION__); global $DB; > $coursecontext = context_course::instance($course->id); > require_all_capabilities(['moodle/course:update', 'moodle/course:sectionvisibility'], $coursecontext); $this->validate_cms($course, $ids, __FUNCTION__); > > $modinfo = get_fast_modinfo($course); // Check capabilities on every activity context. > foreach ($ids as $cmid) { > foreach ($ids as $sectionid) { $modcontext = context_module::instance($cmid); > $section = $modinfo->get_section_info_by_id($sectionid, MUST_EXIST); require_capability('moodle/course:manageactivities', $modcontext); > course_update_section($course, $section, ['visible' => $visible]); } > } $modinfo = get_fast_modinfo($course); > $this->section_state($updates, $course, $ids); $cms = $this->get_cm_info($modinfo, $ids); > } list($insql, $inparams) = $DB->get_in_or_equal(array_keys($cms), SQL_PARAMS_NAMED); > $DB->set_field_select('course_modules', 'indent', $indent, "id $insql", $inparams); > /** rebuild_course_cache($course->id, false, true); > * Show course cms. foreach ($cms as $cm) { > * $modcontext = context_module::instance($cm->id); > * @param stateupdates $updates the affected course elements track course_module_updated::create_from_cm($cm, $modcontext)->trigger(); > * @param stdClass $course the course object $updates->add_cm_put($cm->id); > * @param int[] $ids cm ids } > * @param int $targetsectionid not used } > * @param int $targetcmid not used > */ /** > public function cm_show( * Extract several cm_info from the course_modinfo. > stateupdates $updates, * > stdClass $course, * @param course_modinfo $modinfo the course modinfo. > array $ids = [], * @param int[] $ids the course modules $ids > ?int $targetsectionid = null, * @return cm_info[] the extracted cm_info objects > ?int $targetcmid = null */ > ): void { protected function get_cm_info (course_modinfo $modinfo, array $ids): array { > $this->set_cm_visibility($updates, $course, $ids, 1, 1); $cms = []; > } foreach ($ids as $cmid) { > $cms[$cmid] = $modinfo->get_cm($cmid); > /** } > * Hide course cms. return $cms; > * } > * @param stateupdates $updates the affected course elements track > * @param stdClass $course the course object /** > * @param int[] $ids cm ids * Extract several section_info from the course_modinfo. > * @param int $targetsectionid not used * > * @param int $targetcmid not used * @param course_modinfo $modinfo the course modinfo. > */ * @param int[] $ids the course modules $ids > public function cm_hide( * @return section_info[] the extracted section_info objects > stateupdates $updates, */ > stdClass $course, protected function get_section_info(course_modinfo $modinfo, array $ids): array { > array $ids = [], $sections = []; > ?int $targetsectionid = null, foreach ($ids as $sectionid) { > ?int $targetcmid = null $sections[$sectionid] = $modinfo->get_section_info_by_id($sectionid); > ): void { } > $this->set_cm_visibility($updates, $course, $ids, 0, 1); return $sections; > } } > > /** /** > * Stealth course cms. * Update the course content section collapsed value. > * * > * @param stateupdates $updates the affected course elements track * @param stateupdates $updates the affected course elements track > * @param stdClass $course the course object * @param stdClass $course the course object > * @param int[] $ids cm ids * @param int[] $ids the collapsed section ids > * @param int $targetsectionid not used * @param int $targetsectionid not used > * @param int $targetcmid not used * @param int $targetcmid not used > */ */ > public function cm_stealth( public function section_content_collapsed( > stateupdates $updates, stateupdates $updates, > stdClass $course, stdClass $course, > array $ids = [], array $ids = [], > ?int $targetsectionid = null, ?int $targetsectionid = null, > ?int $targetcmid = null ?int $targetcmid = null > ): void { ): void { > $this->set_cm_visibility($updates, $course, $ids, 1, 0); if (!empty($ids)) { > } $this->validate_sections($course, $ids, __FUNCTION__); > } > /** $format = course_get_format($course->id); > * Internal method to define the cm visibility. $format->set_sections_preference('contentcollapsed', $ids); > * } > * @param stateupdates $updates the affected course elements track > * @param stdClass $course the course object /** > * @param int[] $ids cm ids * Update the course index section collapsed value. > * @param int $visible the new visible value * > * @param int $coursevisible the new course visible value * @param stateupdates $updates the affected course elements track > */ * @param stdClass $course the course object > protected function set_cm_visibility( * @param int[] $ids the collapsed section ids > stateupdates $updates, * @param int $targetsectionid not used > stdClass $course, * @param int $targetcmid not used > array $ids, */ > int $visible, public function section_index_collapsed( > int $coursevisible stateupdates $updates, > ): void { stdClass $course, > global $CFG; array $ids = [], > ?int $targetsectionid = null, > $this->validate_cms( ?int $targetcmid = null > $course, ): void { > $ids, if (!empty($ids)) { > __FUNCTION__, $this->validate_sections($course, $ids, __FUNCTION__); > ['moodle/course:manageactivities', 'moodle/course:activityvisibility'] } > ); $format = course_get_format($course->id); > $format->set_sections_preference('indexcollapsed', $ids); > $format = course_get_format($course->id); } > $modinfo = get_fast_modinfo($course); > /** > $cms = $this->get_cm_info($modinfo, $ids); * Add the update messages of the updated version of any cm and section related to the cm ids. > foreach ($cms as $cm) { * > // Check stealth availability. * This action is mainly used by legacy actions to partially update the course state when the > if (!$coursevisible) { * result of core_course_edit_module is not enough to generate the correct state data. > $section = $cm->get_section_info(); * > $allowstealth = !empty($CFG->allowstealth) && $format->allow_stealth_module_visibility($cm, $section); * @param stateupdates $updates the affected course elements track > $coursevisible = ($allowstealth) ? 0 : 1; * @param stdClass $course the course object > } * @param int[] $ids the list of affected course module ids > set_coursemodule_visible($cm->id, $visible, $coursevisible, false); * @param int $targetsectionid optional target section id > $modcontext = context_module::instance($cm->id); * @param int $targetcmid optional target cm id > course_module_updated::create_from_cm($cm, $modcontext)->trigger(); */ > } public function cm_state( > course_modinfo::purge_course_modules_cache($course->id, $ids); stateupdates $updates, > rebuild_course_cache($course->id, false, true); stdClass $course, > array $ids, > foreach ($cms as $cm) { ?int $targetsectionid = null, > $updates->add_cm_put($cm->id); ?int $targetcmid = null > } ): void { > } > // Collect all section and cm to return. > /** $cmids = []; > * Duplicate a course modules instances into the same course. foreach ($ids as $cmid) { > * $cmids[$cmid] = true; > * @param stateupdates $updates the affected course elements track } > * @param stdClass $course the course object if ($targetcmid) { > * @param int[] $ids course modules ids to duplicate $cmids[$targetcmid] = true; > * @param int|null $targetsectionid optional target section id destination } > * @param int|null $targetcmid optional target before cm id destination > */ $sectionids = []; > public function cm_duplicate( if ($targetsectionid) { > stateupdates $updates, $this->validate_sections($course, [$targetsectionid], __FUNCTION__); > stdClass $course, $sectionids[$targetsectionid] = true; > array $ids = [], } > ?int $targetsectionid = null, > ?int $targetcmid = null $this->validate_cms($course, array_keys($cmids), __FUNCTION__); > ): void { > $this->validate_cms( $modinfo = course_modinfo::instance($course); > $course, > $ids, foreach (array_keys($cmids) as $cmid) { > __FUNCTION__, > ['moodle/course:manageactivities', 'moodle/backup:backuptargetimport', 'moodle/restore:restoretargetimport'] // Add this action to updates array. > ); $updates->add_cm_put($cmid); > > $modinfo = get_fast_modinfo($course); $cm = $modinfo->get_cm($cmid); > $cms = $this->get_cm_info($modinfo, $ids); $sectionids[$cm->section] = true; > } > // Check capabilities on every activity context. > foreach ($cms as $cm) { foreach (array_keys($sectionids) as $sectionid) { > if (!course_allowed_module($course, $cm->modname)) { $updates->add_section_put($sectionid); > throw new moodle_exception('No permission to create that activity'); } > } } > } > /** > $targetsection = null; * Add the update messages of the updated version of any cm and section related to the section ids. > if (!empty($targetsectionid)) { * > $this->validate_sections($course, [$targetsectionid], __FUNCTION__); * This action is mainly used by legacy actions to partially update the course state when the > $targetsection = $modinfo->get_section_info_by_id($targetsectionid, MUST_EXIST); * result of core_course_edit_module is not enough to generate the correct state data. > } * > * @param stateupdates $updates the affected course elements track > $beforecm = null; * @param stdClass $course the course object > if (!empty($targetcmid)) { * @param int[] $ids the list of affected course section ids > $this->validate_cms($course, [$targetcmid], __FUNCTION__); * @param int $targetsectionid optional target section id > $beforecm = $modinfo->get_cm($targetcmid); * @param int $targetcmid optional target cm id > $targetsection = $modinfo->get_section_info_by_id($beforecm->section, MUST_EXIST); */ > } public function section_state( > stateupdates $updates, > // Duplicate course modules. stdClass $course, > $affectedcmids = []; array $ids, > foreach ($cms as $cm) { ?int $targetsectionid = null, > if ($newcm = duplicate_module($course, $cm)) { ?int $targetcmid = null > if ($targetsection) { ): void { > moveto_module($newcm, $targetsection, $beforecm); > } else { $cmids = []; > $affectedcmids[] = $newcm->id; if ($targetcmid) { > } $this->validate_cms($course, [$targetcmid], __FUNCTION__); > } $cmids[$targetcmid] = true; > } } > > if ($targetsection) { $sectionids = []; > $this->section_state($updates, $course, [$targetsection->id]); foreach ($ids as $sectionid) { > } else { $sectionids[$sectionid] = true; > $this->cm_state($updates, $course, $affectedcmids); } > } if ($targetsectionid) { > } $sectionids[$targetsectionid] = true; > } > /** > * Delete course cms. $this->validate_sections($course, array_keys($sectionids), __FUNCTION__); > * > * @param stateupdates $updates the affected course elements track $modinfo = course_modinfo::instance($course); > * @param stdClass $course the course object > * @param int[] $ids section ids foreach (array_keys($sectionids) as $sectionid) { > * @param int $targetsectionid not used $sectioninfo = $modinfo->get_section_info_by_id($sectionid); > * @param int $targetcmid not used $updates->add_section_put($sectionid); > */ // Add cms. > public function cm_delete( if (empty($modinfo->sections[$sectioninfo->section])) { > stateupdates $updates, continue; > stdClass $course, } > array $ids = [], > ?int $targetsectionid = null, foreach ($modinfo->sections[$sectioninfo->section] as $modnumber) { > ?int $targetcmid = null $mod = $modinfo->cms[$modnumber]; > ): void { if ($mod->is_visible_on_course_page()) { > $cmids[$mod->id] = true; > $this->validate_cms($course, $ids, __FUNCTION__, ['moodle/course:manageactivities']); } > } > $format = course_get_format($course->id); } > $modinfo = get_fast_modinfo($course); > $affectedsections = []; foreach (array_keys($cmids) as $cmid) { > // Add this action to updates array. > $cms = $this->get_cm_info($modinfo, $ids); $updates->add_cm_put($cmid); > foreach ($cms as $cm) { } > $section = $cm->get_section_info(); } > $affectedsections[$section->id] = $section; > $format->delete_module($cm, true); /** > $updates->add_cm_remove($cm->id); * Add all the update messages from the complete course state. > } * > * This action is mainly used by legacy actions to partially update the course state when the > foreach ($affectedsections as $sectionid => $section) { * result of core_course_edit_module is not enough to generate the correct state data. > $updates->add_section_put($sectionid); * > } * @param stateupdates $updates the affected course elements track > } * @param stdClass $course the course object > * @param int[] $ids the list of affected course module ids (not used) > /**
< $this->validate_cms($course, $ids, __FUNCTION__); < < // Check capabilities on every activity context. < foreach ($ids as $cmid) { < $modcontext = context_module::instance($cmid); < require_capability('moodle/course:manageactivities', $modcontext); < }
> $this->validate_cms($course, $ids, __FUNCTION__, ['moodle/course:manageactivities']);
?int $targetsectionid = null, ?int $targetcmid = null ): void { $modinfo = course_modinfo::instance($course); $updates->add_course_put(); // Add sections updates. $sections = $modinfo->get_section_info_all(); $sectionids = []; foreach ($sections as $sectioninfo) { $sectionids[] = $sectioninfo->id; } if (!empty($sectionids)) { $this->section_state($updates, $course, $sectionids); } } /** * Checks related to sections: course format support them, all given sections exist and topic 0 is not included. * * @param stdClass $course The course where given $sectionids belong. * @param array $sectionids List of sections to validate. * @param string|null $info additional information in case of error (default null). * @throws moodle_exception if any id is not valid */ protected function validate_sections(stdClass $course, array $sectionids, ?string $info = null): void { global $DB; if (empty($sectionids)) { throw new moodle_exception('emptysectionids', 'core', null, $info); } // No section actions are allowed if course format does not support sections. $courseformat = course_get_format($course->id); if (!$courseformat->uses_sections()) { throw new moodle_exception('sectionactionnotsupported', 'core', null, $info); } list($insql, $inparams) = $DB->get_in_or_equal($sectionids, SQL_PARAMS_NAMED); // Check if all the given sections exist. $couintsections = $DB->count_records_select('course_sections', "id $insql", $inparams); if ($couintsections != count($sectionids)) { throw new moodle_exception('unexistingsectionid', 'core', null, $info); } } /**
< * Checks related to course modules: all given cm exist.
> * Checks related to course modules: all given cm exist and the user has the required capabilities.
* * @param stdClass $course The course where given $cmids belong. * @param array $cmids List of course module ids to validate. * @param string $info additional information in case of error.
> * @param array $capabilities optional capabilities checks per each cm context.
* @throws moodle_exception if any id is not valid */
< protected function validate_cms(stdClass $course, array $cmids, ?string $info = null): void {
> protected function validate_cms(stdClass $course, array $cmids, ?string $info = null, array $capabilities = []): void {
if (empty($cmids)) { throw new moodle_exception('emptycmids', 'core', null, $info); } $moduleinfo = get_fast_modinfo($course->id); $intersect = array_intersect($cmids, array_keys($moduleinfo->get_cms())); if (count($cmids) != count($intersect)) { throw new moodle_exception('unexistingcmid', 'core', null, $info);
> } } > if (!empty($capabilities)) { } > foreach ($cmids as $cmid) { } > $modcontext = context_module::instance($cmid); > require_all_capabilities($capabilities, $modcontext); > }