mirror of
https://github.com/moodle/moodle.git
synced 2025-08-11 03:46:42 +02:00
Merge branch 'MDL-80187-main' of https://github.com/ferranrecio/moodle
This commit is contained in:
commit
ff519c1f65
18 changed files with 1752 additions and 164 deletions
|
@ -177,7 +177,7 @@ abstract class base {
|
|||
*
|
||||
* @param int|stdClass $courseorid either course id or
|
||||
* an object that has the property 'format' and may contain property 'id'
|
||||
* @return course_format
|
||||
* @return base
|
||||
*/
|
||||
public static final function instance($courseorid) {
|
||||
global $DB;
|
||||
|
@ -523,7 +523,7 @@ abstract class base {
|
|||
/**
|
||||
* Returns the display name of the given section that the course prefers.
|
||||
*
|
||||
* @param int|stdClass $section Section object from database or just field course_sections.section
|
||||
* @param int|stdClass|section_info $section Section object from database or just field course_sections.section
|
||||
* @return string Display name that the course format prefers, e.g. "Topic 2"
|
||||
*/
|
||||
public function get_section_name($section) {
|
||||
|
@ -1526,18 +1526,22 @@ abstract class base {
|
|||
*
|
||||
* Do not call this function directly, instead call course_delete_section()
|
||||
*
|
||||
* @param int|stdClass|section_info $section
|
||||
* @param int|stdClass|section_info $sectionornum
|
||||
* @param bool $forcedeleteifnotempty if set to false section will not be deleted if it has modules in it.
|
||||
* @return bool whether section was deleted
|
||||
*/
|
||||
public function delete_section($section, $forcedeleteifnotempty = false) {
|
||||
public function delete_section($sectionornum, $forcedeleteifnotempty = false) {
|
||||
global $DB;
|
||||
if (!$this->uses_sections()) {
|
||||
// Not possible to delete section if sections are not used.
|
||||
return false;
|
||||
}
|
||||
if (!is_object($section)) {
|
||||
$section = $DB->get_record('course_sections', array('course' => $this->get_courseid(), 'section' => $section),
|
||||
if (is_object($sectionornum)) {
|
||||
$section = $sectionornum;
|
||||
} else {
|
||||
$section = $DB->get_record(
|
||||
'course_sections',
|
||||
['course' => $this->get_courseid(), 'section' => $sectionornum],
|
||||
'id,section,sequence,summary');
|
||||
}
|
||||
if (!$section || !$section->section) {
|
||||
|
|
150
course/format/classes/formatactions.php
Normal file
150
course/format/classes/formatactions.php
Normal file
|
@ -0,0 +1,150 @@
|
|||
<?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_courseformat\local\courseactions;
|
||||
use core_courseformat\local\sectionactions;
|
||||
use core_courseformat\local\cmactions;
|
||||
use coding_exception;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Class to instantiate course format actions.
|
||||
*
|
||||
* This class is used to access course content actions.
|
||||
*
|
||||
* All course actions are divided into three main clases:
|
||||
* - course: actions related to the course.
|
||||
* - section: actions related to the sections.
|
||||
* - cm: actions related to the course modules.
|
||||
*
|
||||
* Format plugin can provide their own actions classes by extending the actions classes
|
||||
* with the following namespaces:
|
||||
* - course: format_{PLUGINNAME}\courseformat\courseactions
|
||||
* - section: format_{PLUGINNAME}\courseformat\sectionactions
|
||||
* - cm: format_{PLUGINNAME}\courseformat\cmactions
|
||||
*
|
||||
* There a static method to get the general formatactions instance:
|
||||
* - formatactions::instance($courseorid): returns an instance to access all available actions.
|
||||
*
|
||||
* The class also provides some convenience methods to get specific actions level on a specific course:
|
||||
* - formatactions::course($courseorid): returns an instance of the course actions class.
|
||||
* - formatactions::section($courseorid): returns an instance of the section actions class.
|
||||
* - formatactions::cm($courseorid): returns an instance of the cm actions class.
|
||||
*
|
||||
* There are two ways of executing actions. For example, to execute a section action
|
||||
* called "move_after" the options are:
|
||||
*
|
||||
* Option A: ideal for executing only one action.
|
||||
*
|
||||
* formatactions::section($courseid)->move_after($sectioninfo, $aftersectioninfo);
|
||||
*
|
||||
* Option B: when actions in the same course are going to be executed at different levels.
|
||||
*
|
||||
* $actions = formatactions::instance($courseid);
|
||||
* $actions->section->move_after($sectioninfo, $aftersectioninfo);
|
||||
*
|
||||
* @package core_courseformat
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class formatactions {
|
||||
/**
|
||||
* @var courseactions|null courseactions instance.
|
||||
*/
|
||||
public courseactions $course;
|
||||
|
||||
/**
|
||||
* @var sectionactions sectionactions instance.
|
||||
*/
|
||||
public sectionactions $section;
|
||||
|
||||
/**
|
||||
* @var cmactions cmactions instance.
|
||||
*/
|
||||
public cmactions $cm;
|
||||
|
||||
/**
|
||||
* Returns an instance of the actions class for the given course format.
|
||||
*
|
||||
* @param base $format the course format.
|
||||
*/
|
||||
protected function __construct(base $format) {
|
||||
$actionclasses = [
|
||||
'course' => courseactions::class,
|
||||
'section' => sectionactions::class,
|
||||
'cm' => cmactions::class,
|
||||
];
|
||||
foreach ($actionclasses as $action => $classname) {
|
||||
$formatalternative = 'format_' . $format->get_format() . '\\courseformat\\' . $action . 'actions';
|
||||
if (class_exists($formatalternative)) {
|
||||
if (!is_subclass_of($formatalternative, $classname)) {
|
||||
throw new coding_exception("The \"$formatalternative\" must extend \"$classname\"");
|
||||
}
|
||||
$actionclasses[$action] = $formatalternative;
|
||||
}
|
||||
$this->$action = new $actionclasses[$action]($format->get_course());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of the actions class for the given course format.
|
||||
* @param int|stdClass $courseorid course id or record.
|
||||
* @return courseactions
|
||||
*/
|
||||
public static function course($courseorid): courseactions {
|
||||
return self::instance($courseorid)->course;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of the actions class for the given course format.
|
||||
*
|
||||
* @param int|stdClass $courseorid course id or record.
|
||||
* @return sectionactions
|
||||
*/
|
||||
public static function section($courseorid): sectionactions {
|
||||
return self::instance($courseorid)->section;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of the actions class for the given course format.
|
||||
* @param int|stdClass $courseorid course id or record.
|
||||
* @return cmactions
|
||||
*/
|
||||
public static function cm($courseorid): cmactions {
|
||||
return self::instance($courseorid)->cm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a course action loader instance.
|
||||
* @param int|stdClass $courseorid course id or course.
|
||||
* @return self
|
||||
*/
|
||||
public static function instance(int|stdClass $courseorid): self {
|
||||
$coursesectionscache = \cache::make('core', 'courseactionsinstances');
|
||||
$format = base::instance($courseorid);
|
||||
$courseid = $format->get_courseid();
|
||||
$cachekey = "{$courseid}_{$format->get_format()}";
|
||||
$cachedinstance = $coursesectionscache->get($cachekey);
|
||||
if ($cachedinstance) {
|
||||
return $cachedinstance;
|
||||
}
|
||||
$result = new self($format);
|
||||
$coursesectionscache->set($cachekey, $result);
|
||||
return $result;
|
||||
}
|
||||
}
|
87
course/format/classes/local/baseactions.php
Normal file
87
course/format/classes/local/baseactions.php
Normal file
|
@ -0,0 +1,87 @@
|
|||
<?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\local;
|
||||
|
||||
use core_courseformat\base as course_format;
|
||||
use section_info;
|
||||
use cm_info;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Format base actions.
|
||||
*
|
||||
* This class defined the format actions base class extended by the course, section and cm actions.
|
||||
*
|
||||
* It also provides helpers to get the most recent modinfo and format information. Those
|
||||
* convenience methods are meant to improve the actions readability and prevent excessive
|
||||
* message chains.
|
||||
*
|
||||
* @package core_courseformat
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class baseactions {
|
||||
/**
|
||||
* @var stdClass the course object.
|
||||
*/
|
||||
protected stdClass $course;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param stdClass $course the course object.
|
||||
*/
|
||||
public function __construct(stdClass $course) {
|
||||
$this->course = $course;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the course.
|
||||
* @return stdClass the course object.
|
||||
*/
|
||||
protected function get_course(): stdClass {
|
||||
return $this->course;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the course format.
|
||||
* @return course_format the course format.
|
||||
*/
|
||||
protected function get_format(): course_format {
|
||||
return course_format::instance($this->course);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the section info.
|
||||
* @param int $sectionid the section id.
|
||||
* @param int $strictness Use MUST_EXIST to throw exception if it doesn't
|
||||
* @return section_info|null Information for numbered section or null if not found
|
||||
*/
|
||||
protected function get_section_info($sectionid, int $strictness = IGNORE_MISSING): ?section_info {
|
||||
// Course actions must always get the most recent version of the section info.
|
||||
return get_fast_modinfo($this->course->id)->get_section_info_by_id($sectionid, $strictness);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cm info.
|
||||
* @param int $cmid the cm id.
|
||||
* @return cm_info|null Information for numbered cm or null if not found
|
||||
*/
|
||||
protected function get_cm_info($cmid): ?cm_info {
|
||||
// Course actions must always get the most recent version of the cm info.
|
||||
return get_fast_modinfo($this->course->id)->get_cm($cmid);
|
||||
}
|
||||
}
|
28
course/format/classes/local/cmactions.php
Normal file
28
course/format/classes/local/cmactions.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?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\local;
|
||||
|
||||
/**
|
||||
* Course module course format actions.
|
||||
*
|
||||
* @package core_courseformat
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class cmactions extends baseactions {
|
||||
// All course module actions will go here.
|
||||
}
|
28
course/format/classes/local/courseactions.php
Normal file
28
course/format/classes/local/courseactions.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?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\local;
|
||||
|
||||
/**
|
||||
* Course actions.
|
||||
*
|
||||
* @package core_courseformat
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class courseactions extends baseactions {
|
||||
// All general course actions will go here.
|
||||
}
|
335
course/format/classes/local/sectionactions.php
Normal file
335
course/format/classes/local/sectionactions.php
Normal file
|
@ -0,0 +1,335 @@
|
|||
<?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\local;
|
||||
|
||||
use section_info;
|
||||
use stdClass;
|
||||
use core\event\course_section_deleted;
|
||||
|
||||
/**
|
||||
* Section course format actions.
|
||||
*
|
||||
* @package core_courseformat
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class sectionactions extends baseactions {
|
||||
/**
|
||||
* Create a course section using a record object.
|
||||
*
|
||||
* If $fields->section is not set, the section is added to the end of the course.
|
||||
*
|
||||
* @param stdClass $fields the fields to set on the section
|
||||
* @param bool $skipcheck the position check has already been made and we know it can be used
|
||||
* @return stdClass the created section record
|
||||
*/
|
||||
protected function create_from_object(stdClass $fields, bool $skipcheck = false): stdClass {
|
||||
global $DB;
|
||||
[
|
||||
'position' => $position,
|
||||
'lastsection' => $lastsection,
|
||||
] = $this->calculate_positions($fields, $skipcheck);
|
||||
|
||||
// First add section to the end.
|
||||
$sectionrecord = (object) [
|
||||
'course' => $this->course->id,
|
||||
'section' => $lastsection + 1,
|
||||
'summary' => $fields->summary ?? '',
|
||||
'summaryformat' => $fields->summaryformat ?? FORMAT_HTML,
|
||||
'sequence' => '',
|
||||
'name' => $fields->name ?? null,
|
||||
'visible' => $fields->visible ?? 1,
|
||||
'availability' => null,
|
||||
'component' => $fields->component ?? null,
|
||||
'itemid' => $fields->itemid ?? null,
|
||||
'timemodified' => time(),
|
||||
];
|
||||
$sectionrecord->id = $DB->insert_record("course_sections", $sectionrecord);
|
||||
|
||||
// Now move it to the specified position.
|
||||
if ($position > 0 && $position <= $lastsection) {
|
||||
move_section_to($this->course, $sectionrecord->section, $position, true);
|
||||
$sectionrecord->section = $position;
|
||||
}
|
||||
|
||||
\core\event\course_section_created::create_from_section($sectionrecord)->trigger();
|
||||
|
||||
rebuild_course_cache($this->course->id, true);
|
||||
return $sectionrecord;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the position and lastsection values.
|
||||
*
|
||||
* Each section number must be unique inside a course. However, the section creation is not always
|
||||
* explicit about the final position. By default, regular sections are created at the last position.
|
||||
* However, delegated section can alter that order, because all delegated sections should have higher
|
||||
* numbers. Apart, restore operations can also create sections with a forced specific number.
|
||||
*
|
||||
* This method returns what is the best position for a new section data and, also, what is the current
|
||||
* last section number. The last section is needed to decide if the new section must be moved or not after
|
||||
* insertion.
|
||||
*
|
||||
* @param stdClass $fields the fields to set on the section
|
||||
* @param bool $skipcheck the position check has already been made and we know it can be used
|
||||
* @return array with the new section position (position key) and the course last section value (lastsection key)
|
||||
*/
|
||||
private function calculate_positions($fields, $skipcheck): array {
|
||||
if (!isset($fields->section)) {
|
||||
$skipcheck = false;
|
||||
}
|
||||
if ($skipcheck) {
|
||||
return [
|
||||
'position' => $fields->section,
|
||||
'lastsection' => $fields->section - 1,
|
||||
];
|
||||
}
|
||||
|
||||
$lastsection = $this->get_last_section_number();
|
||||
if (!empty($fields->component)) {
|
||||
return [
|
||||
'position' => $fields->section ?? $lastsection + 1,
|
||||
'lastsection' => $lastsection,
|
||||
];
|
||||
}
|
||||
return [
|
||||
'position' => $fields->section ?? $this->get_last_section_number(false) + 1,
|
||||
'lastsection' => $lastsection,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last section number in the course.
|
||||
* @param bool $includedelegated whether to include delegated sections
|
||||
* @return int
|
||||
*/
|
||||
protected function get_last_section_number(bool $includedelegated = true): int {
|
||||
global $DB;
|
||||
|
||||
$delegtadefilter = $includedelegated ? '' : ' AND component IS NULL';
|
||||
|
||||
return (int) $DB->get_field_sql(
|
||||
'SELECT max(section) from {course_sections} WHERE course = ?' . $delegtadefilter,
|
||||
[$this->course->id]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a delegated section.
|
||||
*
|
||||
* @param string $component the name of the plugin
|
||||
* @param int|null $itemid the id of the delegated section
|
||||
* @param stdClass|null $fields the fields to set on the section
|
||||
* @return section_info the created section
|
||||
*/
|
||||
public function create_delegated(
|
||||
string $component,
|
||||
?int $itemid = null,
|
||||
?stdClass $fields = null
|
||||
): section_info {
|
||||
$record = ($fields) ? clone $fields : new stdClass();
|
||||
$record->component = $component;
|
||||
$record->itemid = $itemid;
|
||||
|
||||
$record = $this->create_from_object($record);
|
||||
return $this->get_section_info($record->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a course section and adds it to the specified position
|
||||
*
|
||||
* This method returns a section record, not a section_info object. This prevents the regeneration
|
||||
* of the modinfo object each time we create a section.
|
||||
*
|
||||
* If position is greater than number of existing sections, the section is added to the end.
|
||||
* This will become sectionnum of the new section. All existing sections at this or bigger
|
||||
* position will be shifted down.
|
||||
*
|
||||
* @param int $position The position to add to, 0 means to the end.
|
||||
* @param bool $skipcheck the check has already been made and we know that the section with this position does not exist
|
||||
* @return stdClass created section object)
|
||||
*/
|
||||
public function create(int $position = 0, bool $skipcheck = false): stdClass {
|
||||
$record = (object) [
|
||||
'section' => $position,
|
||||
];
|
||||
return $this->create_from_object($record, $skipcheck);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create course sections if they are not created yet.
|
||||
* @param int[] $sectionnums the section numbers to create
|
||||
* @return bool whether any section was created
|
||||
*/
|
||||
public function create_if_missing(array $sectionnums): bool {
|
||||
$result = false;
|
||||
$modinfo = get_fast_modinfo($this->course);
|
||||
// Ensure we add the sections in order.
|
||||
sort($sectionnums);
|
||||
// Delegated sections must be displaced when creating a regular section.
|
||||
$skipcheck = !$modinfo->has_delegated_sections();
|
||||
|
||||
$sections = $modinfo->get_section_info_all();
|
||||
foreach ($sectionnums as $sectionnum) {
|
||||
if (isset($sections[$sectionnum]) && empty($sections[$sectionnum]->component)) {
|
||||
continue;
|
||||
}
|
||||
$this->create($sectionnum, $skipcheck);
|
||||
$result = true;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a course section.
|
||||
* @param section_info $sectioninfo the section to delete.
|
||||
* @param bool $forcedeleteifnotempty whether to force section deletion if it contains modules.
|
||||
* @param bool $async whether or not to try to delete the section using an adhoc task. Async also depends on a plugin hook.
|
||||
* @return bool whether section was deleted
|
||||
*/
|
||||
public function delete(section_info $sectioninfo, bool $forcedeleteifnotempty = true, bool $async = false): bool {
|
||||
// Check the 'course_module_background_deletion_recommended' hook first.
|
||||
// Only use asynchronous deletion if at least one plugin returns true and if async deletion has been requested.
|
||||
// Both are checked because plugins should not be allowed to dictate the deletion behaviour, only support/decline it.
|
||||
// It's up to plugins to handle things like whether or not they are enabled.
|
||||
if ($async && $pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended')) {
|
||||
foreach ($pluginsfunction as $plugintype => $plugins) {
|
||||
foreach ($plugins as $pluginfunction) {
|
||||
if ($pluginfunction()) {
|
||||
return $this->delete_async($sectioninfo, $forcedeleteifnotempty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->delete_format_data(
|
||||
$sectioninfo,
|
||||
$forcedeleteifnotempty,
|
||||
$this->get_delete_event($sectioninfo)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the event to trigger when deleting a section.
|
||||
* @param section_info $sectioninfo the section to delete.
|
||||
* @return course_section_deleted the event to trigger
|
||||
*/
|
||||
protected function get_delete_event(section_info $sectioninfo): course_section_deleted {
|
||||
global $DB;
|
||||
// Section record is needed for the event snapshot.
|
||||
$sectionrecord = $DB->get_record('course_sections', ['id' => $sectioninfo->id]);
|
||||
|
||||
$format = course_get_format($this->course);
|
||||
$sectionname = $format->get_section_name($sectioninfo);
|
||||
$context = \context_course::instance($this->course->id);
|
||||
$event = course_section_deleted::create(
|
||||
[
|
||||
'objectid' => $sectioninfo->id,
|
||||
'courseid' => $this->course->id,
|
||||
'context' => $context,
|
||||
'other' => [
|
||||
'sectionnum' => $sectioninfo->section,
|
||||
'sectionname' => $sectionname,
|
||||
],
|
||||
]
|
||||
);
|
||||
$event->add_record_snapshot('course_sections', $sectionrecord);
|
||||
return $event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a course section.
|
||||
* @param section_info $sectioninfo the section to delete.
|
||||
* @param bool $forcedeleteifnotempty whether to force section deletion if it contains modules.
|
||||
* @param course_section_deleted $event the event to trigger
|
||||
* @return bool whether section was deleted
|
||||
*/
|
||||
protected function delete_format_data(
|
||||
section_info $sectioninfo,
|
||||
bool $forcedeleteifnotempty,
|
||||
course_section_deleted $event
|
||||
): bool {
|
||||
$format = course_get_format($this->course);
|
||||
$result = $format->delete_section($sectioninfo, $forcedeleteifnotempty);
|
||||
if ($result) {
|
||||
$event->trigger();
|
||||
}
|
||||
rebuild_course_cache($this->course->id, true);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Course section deletion, using an adhoc task for deletion of the modules it contains.
|
||||
* 1. Schedule all modules within the section for adhoc removal.
|
||||
* 2. Move all modules to course section 0.
|
||||
* 3. Delete the resulting empty section.
|
||||
*
|
||||
* @param section_info $sectioninfo the section to schedule for deletion.
|
||||
* @param bool $forcedeleteifnotempty whether to force section deletion if it contains modules.
|
||||
* @return bool true if the section was scheduled for deletion, false otherwise.
|
||||
*/
|
||||
protected function delete_async(section_info $sectioninfo, bool $forcedeleteifnotempty = true): bool {
|
||||
global $DB, $USER;
|
||||
|
||||
if (!$forcedeleteifnotempty && (!empty($sectioninfo->sequence) || !empty($sectioninfo->summary))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Event needs to be created before the section activities are moved to section 0.
|
||||
$event = $this->get_delete_event($sectioninfo);
|
||||
|
||||
$affectedmods = $DB->get_records_select(
|
||||
'course_modules',
|
||||
'course = ? AND section = ? AND deletioninprogress <> ?',
|
||||
[$this->course->id, $sectioninfo->id, 1],
|
||||
'',
|
||||
'id'
|
||||
);
|
||||
|
||||
// Flag those modules having no existing deletion flag. Some modules may have been
|
||||
// scheduled for deletion manually, and we don't want to create additional adhoc deletion
|
||||
// tasks for these. Moving them to section 0 will suffice.
|
||||
$DB->set_field(
|
||||
'course_modules',
|
||||
'deletioninprogress',
|
||||
'1',
|
||||
['course' => $this->course->id, 'section' => $sectioninfo->id]
|
||||
);
|
||||
|
||||
// Move all modules to section 0.
|
||||
$sectionzero = $DB->get_record('course_sections', ['course' => $this->course->id, 'section' => '0']);
|
||||
$modules = $DB->get_records('course_modules', ['section' => $sectioninfo->id], '');
|
||||
foreach ($modules as $mod) {
|
||||
moveto_module($mod, $sectionzero);
|
||||
}
|
||||
|
||||
$removaltask = new \core_course\task\course_delete_modules();
|
||||
$data = [
|
||||
'cms' => $affectedmods,
|
||||
'userid' => $USER->id,
|
||||
'realuserid' => \core\session\manager::get_realuser()->id,
|
||||
];
|
||||
$removaltask->set_custom_data($data);
|
||||
\core\task\manager::queue_adhoc_task($removaltask);
|
||||
|
||||
// Ensure we have the latest section info.
|
||||
$sectioninfo = $this->get_section_info($sectioninfo->id);
|
||||
return $this->delete_format_data($sectioninfo, $forcedeleteifnotempty, $event);
|
||||
}
|
||||
}
|
29
course/format/tests/fixtures/format_theunittest_cmactions.php
vendored
Normal file
29
course/format/tests/fixtures/format_theunittest_cmactions.php
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?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 format_theunittest\courseformat;
|
||||
|
||||
use core_courseformat\local\cmactions as core_cm_actions;
|
||||
|
||||
/**
|
||||
* Fixture for fake course module actions testing.
|
||||
*
|
||||
* @package core_course
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class cmactions extends core_cm_actions {
|
||||
}
|
29
course/format/tests/fixtures/format_theunittest_courseactions.php
vendored
Normal file
29
course/format/tests/fixtures/format_theunittest_courseactions.php
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?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 format_theunittest\courseformat;
|
||||
|
||||
use core_courseformat\local\courseactions as core_course_actions;
|
||||
|
||||
/**
|
||||
* Fixture for fake course actions testing.
|
||||
*
|
||||
* @package core_course
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class courseactions extends core_course_actions {
|
||||
}
|
29
course/format/tests/fixtures/format_theunittest_sectionactions.php
vendored
Normal file
29
course/format/tests/fixtures/format_theunittest_sectionactions.php
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?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 format_theunittest\courseformat;
|
||||
|
||||
use core_courseformat\local\sectionactions as core_section_actions;
|
||||
|
||||
/**
|
||||
* Fixture for fake section actions testing.
|
||||
*
|
||||
* @package core_course
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class sectionactions extends core_section_actions {
|
||||
}
|
146
course/format/tests/formatactions_test.php
Normal file
146
course/format/tests/formatactions_test.php
Normal file
|
@ -0,0 +1,146 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Course format actions class tests.
|
||||
*
|
||||
* @package core_courseformat
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @coversDefaultClass \core_courseformat\base
|
||||
*/
|
||||
class formatactions_test extends \advanced_testcase {
|
||||
|
||||
/**
|
||||
* Setup to ensure that fixtures are loaded.
|
||||
*/
|
||||
public static function setUpBeforeClass(): void {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest.php');
|
||||
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_courseactions.php');
|
||||
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_sectionactions.php');
|
||||
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_cmactions.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for get_instance static method.
|
||||
* @dataProvider provider_classname_action
|
||||
* @covers ::instance
|
||||
* @param string $format
|
||||
* @param array $classnames
|
||||
*/
|
||||
public function test_instance(string $format, array $classnames): void {
|
||||
$this->resetAfterTest();
|
||||
$course = $this->getDataGenerator()->create_course(['format' => $format]);
|
||||
|
||||
$instance1 = formatactions::instance($course);
|
||||
$this->assertInstanceOf('\core_courseformat\formatactions', $instance1);
|
||||
|
||||
$instance2 = formatactions::instance($course->id);
|
||||
$this->assertInstanceOf('\core_courseformat\formatactions', $instance2);
|
||||
|
||||
// Validate the method is caching the result.
|
||||
$this->assertEquals($instance1, $instance2);
|
||||
|
||||
// Validate public attribute classes.
|
||||
$this->assertInstanceOf($classnames['course'], $instance1->course);
|
||||
$this->assertInstanceOf($classnames['section'], $instance1->section);
|
||||
$this->assertInstanceOf($classnames['cm'], $instance1->cm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the course action instance is created correctly.
|
||||
* @dataProvider provider_classname_action
|
||||
* @covers ::course
|
||||
* @param string $format
|
||||
* @param array $classnames
|
||||
*/
|
||||
public function test_course_action_instance(string $format, array $classnames): void {
|
||||
$this->resetAfterTest();
|
||||
$course = $this->getDataGenerator()->create_course(['format' => $format]);
|
||||
|
||||
$instance1 = formatactions::course($course);
|
||||
$this->assertInstanceOf($classnames['course'], $instance1);
|
||||
|
||||
$instance2 = formatactions::course($course->id);
|
||||
$this->assertInstanceOf($classnames['course'], $instance2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the section action instance is created correctly.
|
||||
* @dataProvider provider_classname_action
|
||||
* @covers ::section
|
||||
*
|
||||
* @param string $format
|
||||
* @param array $classnames
|
||||
*/
|
||||
public function test_static_sectionactions_instance(string $format, array $classnames): void {
|
||||
$this->resetAfterTest();
|
||||
$course = $this->getDataGenerator()->create_course(['format' => $format]);
|
||||
|
||||
$instance1 = formatactions::section($course);
|
||||
$this->assertInstanceOf($classnames['section'], $instance1);
|
||||
|
||||
$instance2 = formatactions::section($course->id);
|
||||
$this->assertInstanceOf($classnames['section'], $instance2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the cm action instance is created correctly.
|
||||
* @dataProvider provider_classname_action
|
||||
* @covers ::cm
|
||||
*
|
||||
* @param string $format
|
||||
* @param array $classnames
|
||||
*/
|
||||
public function test_static_cmactions_instance(string $format, array $classnames): void {
|
||||
$this->resetAfterTest();
|
||||
$course = $this->getDataGenerator()->create_course(['format' => $format]);
|
||||
|
||||
$instance1 = formatactions::cm($course);
|
||||
$this->assertInstanceOf($classnames['cm'], $instance1);
|
||||
|
||||
$instance2 = formatactions::cm($course->id);
|
||||
$this->assertInstanceOf($classnames['cm'], $instance2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for format class names scenarios.
|
||||
* @return array
|
||||
*/
|
||||
public static function provider_classname_action(): array {
|
||||
return [
|
||||
'Topics format' => [
|
||||
'format' => 'topics',
|
||||
'classnames' => [
|
||||
'course' => '\core_courseformat\local\courseactions',
|
||||
'section' => '\core_courseformat\local\sectionactions',
|
||||
'cm' => '\core_courseformat\local\cmactions',
|
||||
],
|
||||
],
|
||||
'The unit test fixture format' => [
|
||||
'format' => 'theunittest',
|
||||
'classnames' => [
|
||||
'course' => '\format_theunittest\courseformat\courseactions',
|
||||
'section' => '\format_theunittest\courseformat\sectionactions',
|
||||
'cm' => '\format_theunittest\courseformat\cmactions',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
149
course/format/tests/local/baseactions_test.php
Normal file
149
course/format/tests/local/baseactions_test.php
Normal file
|
@ -0,0 +1,149 @@
|
|||
<?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\local;
|
||||
use ReflectionMethod;
|
||||
use section_info;
|
||||
use cm_info;
|
||||
|
||||
/**
|
||||
* Base format actions class tests.
|
||||
*
|
||||
* @package core_courseformat
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @coversDefaultClass \core_courseformat\base
|
||||
*/
|
||||
class baseactions_test extends \advanced_testcase {
|
||||
/**
|
||||
* Setup to ensure that fixtures are loaded.
|
||||
*/
|
||||
public static function setUpBeforeClass(): void {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/course/lib.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reflection method for a base class instance.
|
||||
* @param baseactions $baseinstance
|
||||
* @param string $methodname
|
||||
* @return ReflectionMethod
|
||||
*/
|
||||
private function get_base_reflection_method(baseactions $baseinstance, string $methodname): ReflectionMethod {
|
||||
$reflectionclass = new \reflectionclass($baseinstance);
|
||||
$method = $reflectionclass->getMethod($methodname);
|
||||
$method->setAccessible(true);
|
||||
return $method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for get_instance static method.
|
||||
* @covers ::get_format
|
||||
*/
|
||||
public function test_get_format(): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(['format' => 'topics']);
|
||||
|
||||
$baseactions = new baseactions($course);
|
||||
$method = $this->get_base_reflection_method($baseactions, 'get_format');
|
||||
|
||||
$format = $method->invoke($baseactions);
|
||||
$this->assertEquals('topics', $format->get_format());
|
||||
$this->assertEquals('format_topics', $format::class);
|
||||
|
||||
// Format should be always the most updated one.
|
||||
$course->format = 'weeks';
|
||||
$DB->update_record('course', $course);
|
||||
|
||||
$format = $method->invoke($baseactions);
|
||||
$this->assertEquals('weeks', $format->get_format());
|
||||
$this->assertEquals('format_weeks', $format::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for get_instance static method.
|
||||
* @covers ::get_section_info
|
||||
*/
|
||||
public function test_get_section_info(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(
|
||||
['format' => 'topics', 'numsections' => 4],
|
||||
['createsections' => true]
|
||||
);
|
||||
|
||||
$modinfo = get_fast_modinfo($course->id);
|
||||
$originalsection = $modinfo->get_section_info(1);
|
||||
|
||||
$baseactions = new baseactions($course);
|
||||
$method = $this->get_base_reflection_method($baseactions, 'get_section_info');
|
||||
|
||||
$sectioninfo = $method->invoke($baseactions, $originalsection->id);
|
||||
$this->assertInstanceOf(section_info::class, $sectioninfo);
|
||||
$this->assertEquals($originalsection->id, $sectioninfo->id);
|
||||
$this->assertEquals($originalsection->section, $sectioninfo->section);
|
||||
|
||||
// Section info should be always the most updated one.
|
||||
course_update_section($course, $originalsection, (object)['name' => 'New name']);
|
||||
move_section_to($course, 1, 3);
|
||||
|
||||
$sectioninfo = $method->invoke($baseactions, $originalsection->id);
|
||||
$this->assertInstanceOf(section_info::class, $sectioninfo);
|
||||
$this->assertEquals($originalsection->id, $sectioninfo->id);
|
||||
$this->assertEquals(3, $sectioninfo->section);
|
||||
|
||||
$this->assertEquals('New name', $sectioninfo->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for get_instance static method.
|
||||
* @covers ::get_cm_info
|
||||
*/
|
||||
public function test_get_cm_info(): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(
|
||||
['format' => 'topics', 'numsections' => 4],
|
||||
['createsections' => true]
|
||||
);
|
||||
$activity = $this->getDataGenerator()->create_module('label', ['course' => $course->id]);
|
||||
|
||||
$modinfo = get_fast_modinfo($course->id);
|
||||
$destinationsection = $modinfo->get_section_info(3);
|
||||
$originalcm = $modinfo->get_cm($activity->cmid);
|
||||
|
||||
$baseactions = new baseactions($course);
|
||||
$method = $this->get_base_reflection_method($baseactions, 'get_cm_info');
|
||||
|
||||
$cm = $method->invoke($baseactions, $originalcm->id);
|
||||
$this->assertInstanceOf(cm_info::class, $cm);
|
||||
$this->assertEquals($originalcm->id, $cm->id);
|
||||
$this->assertEquals($originalcm->sectionnum, $cm->sectionnum);
|
||||
$this->assertEquals($originalcm->name, $cm->name);
|
||||
|
||||
// CM info should be always the most updated one.
|
||||
moveto_module($originalcm, $destinationsection);
|
||||
|
||||
$cm = $method->invoke($baseactions, $originalcm->id);
|
||||
$this->assertInstanceOf(cm_info::class, $cm);
|
||||
$this->assertEquals($originalcm->id, $cm->id);
|
||||
$this->assertEquals($destinationsection->section, $cm->sectionnum);
|
||||
}
|
||||
}
|
544
course/format/tests/local/sectionactions_test.php
Normal file
544
course/format/tests/local/sectionactions_test.php
Normal file
|
@ -0,0 +1,544 @@
|
|||
<?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\local;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Section format actions class tests.
|
||||
*
|
||||
* @package core_courseformat
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @coversDefaultClass \core_courseformat\sectionactions
|
||||
*/
|
||||
class sectionactions_test extends \advanced_testcase {
|
||||
/**
|
||||
* Setup to ensure that fixtures are loaded.
|
||||
*/
|
||||
public static function setUpBeforeClass(): void {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/course/lib.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for create_delegated method.
|
||||
* @covers ::create_delegated
|
||||
* @dataProvider create_delegated_provider
|
||||
* @param string $component the name of the plugin
|
||||
* @param int|null $itemid the id of the delegated section
|
||||
* @param stdClass|null $fields the fields to set on the section
|
||||
*/
|
||||
public function test_create_delegated(string $component, ?int $itemid, ?stdClass $fields): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
|
||||
|
||||
$sectionactions = new sectionactions($course);
|
||||
$section = $sectionactions->create_delegated($component, $itemid, $fields);
|
||||
|
||||
$this->assertEquals($component, $section->component);
|
||||
$this->assertEquals($itemid, $section->itemid);
|
||||
if (!empty($fields)) {
|
||||
foreach ($fields as $field => $value) {
|
||||
$this->assertEquals($value, $section->$field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_create_delegated.
|
||||
* @return array
|
||||
*/
|
||||
public static function create_delegated_provider(): array {
|
||||
return [
|
||||
'component with no itemid or fields' => [
|
||||
'mod_assign',
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'component with itemid but no fields' => [
|
||||
'mod_assign',
|
||||
1,
|
||||
null,
|
||||
],
|
||||
'component with itemid and empty fields' => [
|
||||
'mod_assign',
|
||||
1,
|
||||
new stdClass(),
|
||||
],
|
||||
'component with itemid and name field' => [
|
||||
'mod_assign',
|
||||
1,
|
||||
(object) ['name' => 'new name'],
|
||||
],
|
||||
'component with no itemid but name field' => [
|
||||
'mod_assign',
|
||||
null,
|
||||
(object) ['name' => 'new name'],
|
||||
],
|
||||
'component with itemid and summary' => [
|
||||
'mod_assign',
|
||||
1,
|
||||
(object) ['summary' => 'summary'],
|
||||
],
|
||||
'component with itemid and summary, summaryformat ' => [
|
||||
'mod_assign',
|
||||
1,
|
||||
(object) ['summary' => 'summary', 'summaryformat' => 1],
|
||||
],
|
||||
'component with itemid and section number' => [
|
||||
'mod_assign',
|
||||
1,
|
||||
(object) ['section' => 2],
|
||||
],
|
||||
'component with itemid and visible 1' => [
|
||||
'mod_assign',
|
||||
1,
|
||||
(object) ['visible' => 1],
|
||||
],
|
||||
'component with itemid and visible 0' => [
|
||||
'mod_assign',
|
||||
1,
|
||||
(object) ['visible' => 0],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for create method.
|
||||
* @covers ::create
|
||||
* @dataProvider create_provider
|
||||
* @param int $sectionnum the name of the plugin
|
||||
* @param bool $skip if the validation should be skipped
|
||||
* @param bool $expectexception if the method should throw an exception
|
||||
* @param int $expected the expected section number
|
||||
*/
|
||||
public function test_create(int $sectionnum, bool $skip, bool $expectexception, int $expected): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
|
||||
|
||||
$sectionactions = new sectionactions($course);
|
||||
|
||||
if ($expectexception) {
|
||||
$this->expectException(\dml_write_exception::class);
|
||||
}
|
||||
$section = $sectionactions->create($sectionnum, $skip);
|
||||
|
||||
$this->assertEquals($expected, $section->section);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_create_delegated.
|
||||
* @return array
|
||||
*/
|
||||
public static function create_provider(): array {
|
||||
return [
|
||||
'section 1' => [
|
||||
'sectionnum' => 1,
|
||||
'skip' => false,
|
||||
'expectexception' => false,
|
||||
'expected' => 1,
|
||||
],
|
||||
'section 2' => [
|
||||
'sectionnum' => 2,
|
||||
'skip' => false,
|
||||
'expectexception' => false,
|
||||
'expected' => 2,
|
||||
],
|
||||
'section 3' => [
|
||||
'sectionnum' => 3,
|
||||
'skip' => false,
|
||||
'expectexception' => false,
|
||||
'expected' => 2,
|
||||
],
|
||||
'section 4' => [
|
||||
'sectionnum' => 4,
|
||||
'skip' => false,
|
||||
'expectexception' => false,
|
||||
'expected' => 2,
|
||||
],
|
||||
'section 1 with exception' => [
|
||||
'sectionnum' => 1,
|
||||
'skip' => true,
|
||||
'expectexception' => true,
|
||||
'expected' => 0,
|
||||
],
|
||||
'section 2 with skip validation' => [
|
||||
'sectionnum' => 2,
|
||||
'skip' => true,
|
||||
'expectexception' => false,
|
||||
'expected' => 2,
|
||||
],
|
||||
'section 5 with skip validation' => [
|
||||
'sectionnum' => 5,
|
||||
'skip' => true,
|
||||
'expectexception' => false,
|
||||
'expected' => 5,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create sections when there are sections with comonent (delegated sections) in the course.
|
||||
* @covers ::create
|
||||
* @covers ::create_delegated
|
||||
*/
|
||||
public function test_create_with_delegated_sections(): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(
|
||||
['format' => 'topics', 'numsections' => 1],
|
||||
['createsections' => true],
|
||||
);
|
||||
|
||||
$sectionactions = new sectionactions($course);
|
||||
$section = $sectionactions->create_delegated('mod_forum', 1);
|
||||
$this->assertEquals(2, $section->section);
|
||||
$delegateid = $section->id;
|
||||
|
||||
// Regular sections are created before delegated ones.
|
||||
$section = $sectionactions->create(2);
|
||||
$this->assertEquals(2, $section->section);
|
||||
$regularid = $section->id;
|
||||
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
|
||||
$section2 = $modinfo->get_section_info(2);
|
||||
$this->assertEquals($regularid, $section2->id);
|
||||
$this->assertEquals(2, $section2->section);
|
||||
|
||||
$sectiondelegated = $modinfo->get_section_info_by_component('mod_forum', 1);
|
||||
$this->assertEquals($delegateid, $sectiondelegated->id);
|
||||
$this->assertEquals(3, $sectiondelegated->section);
|
||||
|
||||
// New delegates should be after the current delegate sections.
|
||||
$section = $sectionactions->create_delegated('mod_forum', 2);
|
||||
$this->assertEquals(4, $section->section);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for create_if_missing method.
|
||||
* @covers ::create_if_missing
|
||||
* @dataProvider create_if_missing_provider
|
||||
* @param array $sectionnums the section numbers to create
|
||||
* @param bool $expected the expected result
|
||||
*/
|
||||
public function test_create_if_missing(array $sectionnums, bool $expected): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 2]);
|
||||
|
||||
$sectionactions = new sectionactions($course);
|
||||
$result = $sectionactions->create_if_missing($sectionnums);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
foreach ($sectionnums as $sectionnum) {
|
||||
$section = $modinfo->get_section_info($sectionnum);
|
||||
$this->assertEquals($sectionnum, $section->section);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_create_delegated.
|
||||
* @return array
|
||||
*/
|
||||
public static function create_if_missing_provider(): array {
|
||||
return [
|
||||
'existing section' => [
|
||||
'sectionnum' => [1],
|
||||
'expected' => false,
|
||||
],
|
||||
'unexisting section' => [
|
||||
'sectionnum' => [3],
|
||||
'expected' => true,
|
||||
],
|
||||
'several existing sections' => [
|
||||
'sectionnum' => [1, 2],
|
||||
'expected' => false,
|
||||
],
|
||||
'several unexisting sections' => [
|
||||
'sectionnum' => [3, 4],
|
||||
'expected' => true,
|
||||
],
|
||||
'empty array' => [
|
||||
'sectionnum' => [],
|
||||
'expected' => false,
|
||||
],
|
||||
'existent and unexistent sections' => [
|
||||
'sectionnum' => [1, 2, 3, 4],
|
||||
'expected' => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create if missing when the course has delegated sections.
|
||||
* @covers ::create_if_missing
|
||||
* @covers ::create_delegated
|
||||
*/
|
||||
public function test_create_if_missing_with_delegated_sections(): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(
|
||||
['format' => 'topics', 'numsections' => 1],
|
||||
['createsections' => true],
|
||||
);
|
||||
|
||||
$sectionactions = new sectionactions($course);
|
||||
$section = $sectionactions->create_delegated('mod_forum', 1);
|
||||
$delegateid = $section->id;
|
||||
|
||||
$result = $sectionactions->create_if_missing([1, 2]);
|
||||
$this->assertTrue($result);
|
||||
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
$section = $modinfo->get_section_info(2);
|
||||
$this->assertEquals(2, $section->section);
|
||||
$this->assertNotEquals($delegateid, $section->id);
|
||||
$delegatedsection = $modinfo->get_section_info_by_id($delegateid);
|
||||
$this->assertEquals(3, $delegatedsection->section);
|
||||
|
||||
$result = $sectionactions->create_if_missing([1, 2]);
|
||||
$this->assertFalse($result);
|
||||
|
||||
$result = $sectionactions->create_if_missing([1, 2, 3]);
|
||||
$this->assertTrue($result);
|
||||
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
$section = $modinfo->get_section_info(3);
|
||||
$this->assertEquals(3, $section->section);
|
||||
$this->assertNotEquals($delegateid, $section->id);
|
||||
$delegatedsection = $modinfo->get_section_info_by_id($delegateid);
|
||||
$this->assertEquals(4, $delegatedsection->section);
|
||||
|
||||
$result = $sectionactions->create_if_missing([1, 2, 3]);
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for delete method.
|
||||
* @covers ::delete
|
||||
*/
|
||||
public function test_delete(): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$generator = $this->getDataGenerator();
|
||||
|
||||
$course = $generator->create_course(
|
||||
['numsections' => 6, 'format' => 'topics'],
|
||||
['createsections' => true]
|
||||
);
|
||||
$assign0 = $generator->create_module('assign', ['course' => $course, 'section' => 0]);
|
||||
$assign1 = $generator->create_module('assign', ['course' => $course, 'section' => 1]);
|
||||
$assign21 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
|
||||
$assign22 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
|
||||
$assign3 = $generator->create_module('assign', ['course' => $course, 'section' => 3]);
|
||||
$assign5 = $generator->create_module('assign', ['course' => $course, 'section' => 5]);
|
||||
$assign6 = $generator->create_module('assign', ['course' => $course, 'section' => 6]);
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
$sectionactions = new sectionactions($course);
|
||||
$sections = get_fast_modinfo($course)->get_section_info_all();
|
||||
|
||||
// Attempt to delete 0-section.
|
||||
$this->assertFalse($sectionactions->delete($sections[0], true));
|
||||
$this->assertTrue($DB->record_exists('course_modules', ['id' => $assign0->cmid]));
|
||||
$this->assertEquals(6, course_get_format($course)->get_last_section_number());
|
||||
|
||||
// Delete last section.
|
||||
$this->assertTrue($sectionactions->delete($sections[6], true));
|
||||
$this->assertFalse($DB->record_exists('course_modules', ['id' => $assign6->cmid]));
|
||||
$this->assertEquals(5, course_get_format($course)->get_last_section_number());
|
||||
|
||||
// Delete empty section.
|
||||
$this->assertTrue($sectionactions->delete($sections[4], false));
|
||||
$this->assertEquals(4, course_get_format($course)->get_last_section_number());
|
||||
|
||||
// Delete section in the middle (2).
|
||||
$this->assertFalse($sectionactions->delete($sections[2], false));
|
||||
$this->assertEquals(4, course_get_format($course)->get_last_section_number());
|
||||
$sections = get_fast_modinfo($course)->get_section_info_all();
|
||||
$this->assertTrue($sectionactions->delete($sections[2], true));
|
||||
$this->assertFalse($DB->record_exists('course_modules', ['id' => $assign21->cmid]));
|
||||
$this->assertFalse($DB->record_exists('course_modules', ['id' => $assign22->cmid]));
|
||||
$this->assertEquals(3, course_get_format($course)->get_last_section_number());
|
||||
$this->assertEquals(
|
||||
[
|
||||
0 => [$assign0->cmid],
|
||||
1 => [$assign1->cmid],
|
||||
2 => [$assign3->cmid],
|
||||
3 => [$assign5->cmid],
|
||||
],
|
||||
get_fast_modinfo($course)->sections
|
||||
);
|
||||
|
||||
// Remove marked section.
|
||||
course_set_marker($course->id, 1);
|
||||
$this->assertTrue(course_get_format($course)->is_section_current(1));
|
||||
$this->assertTrue($sectionactions->delete(
|
||||
get_fast_modinfo($course)->get_section_info(1),
|
||||
true
|
||||
));
|
||||
$this->assertFalse(course_get_format($course)->is_section_current(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that triggering a course_section_deleted event works as expected.
|
||||
* @covers ::delete
|
||||
*/
|
||||
public function test_section_deleted_event(): void {
|
||||
global $USER, $DB;
|
||||
$this->resetAfterTest();
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
// Create the course with sections.
|
||||
$course = $this->getDataGenerator()->create_course(['numsections' => 10], ['createsections' => true]);
|
||||
$coursecontext = \context_course::instance($course->id);
|
||||
|
||||
$section = get_fast_modinfo($course)->get_section_info(10);
|
||||
$sectionrecord = $DB->get_record('course_sections', ['id' => $section->id]);
|
||||
|
||||
$sectionactions = new sectionactions($course);
|
||||
$sectionactions->delete($section);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$event = array_pop($events); // Delete section event.
|
||||
$sink->close();
|
||||
|
||||
// Validate event data.
|
||||
$this->assertInstanceOf('\core\event\course_section_deleted', $event);
|
||||
$this->assertEquals('course_sections', $event->objecttable);
|
||||
$this->assertEquals($section->id, $event->objectid);
|
||||
$this->assertEquals($course->id, $event->courseid);
|
||||
$this->assertEquals($coursecontext->id, $event->contextid);
|
||||
$this->assertEquals($section->section, $event->other['sectionnum']);
|
||||
$expecteddesc = "The user with id '{$event->userid}' deleted section number '{$event->other['sectionnum']}' " .
|
||||
"(section name '{$event->other['sectionname']}') for the course with id '{$event->courseid}'";
|
||||
$this->assertEquals($expecteddesc, $event->get_description());
|
||||
$this->assertEquals($sectionrecord, $event->get_record_snapshot('course_sections', $event->objectid));
|
||||
$this->assertNull($event->get_url());
|
||||
$this->assertEventContextNotUsed($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test async section deletion hook.
|
||||
* @covers ::delete
|
||||
*/
|
||||
public function test_async_section_deletion_hook_implemented(): void {
|
||||
// Async section deletion (provided section contains modules), depends on the 'true' being returned by at least one plugin
|
||||
// implementing the 'course_module_adhoc_deletion_recommended' hook. In core, is implemented by the course recyclebin,
|
||||
// which will only return true if the plugin is enabled. To make sure async deletion occurs, this test enables recyclebin.
|
||||
global $DB, $USER;
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
|
||||
// Ensure recyclebin is enabled.
|
||||
set_config('coursebinenable', true, 'tool_recyclebin');
|
||||
|
||||
// Create course, module and context.
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course(['numsections' => 4, 'format' => 'topics'], ['createsections' => true]);
|
||||
$assign0 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
|
||||
$assign1 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
|
||||
$assign2 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
|
||||
$assign3 = $generator->create_module('assign', ['course' => $course, 'section' => 0]);
|
||||
|
||||
$sectionactions = new sectionactions($course);
|
||||
|
||||
// Delete empty section. No difference from normal, synchronous behaviour.
|
||||
$this->assertTrue($sectionactions->delete(get_fast_modinfo($course)->get_section_info(4), false, true));
|
||||
$this->assertEquals(3, course_get_format($course)->get_last_section_number());
|
||||
|
||||
// Delete a module in section 2 (using async). Need to verify this doesn't generate two tasks when we delete
|
||||
// the section in the next step.
|
||||
course_delete_module($assign2->cmid, true);
|
||||
|
||||
// Confirm that the module is pending deletion in its current section.
|
||||
$section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => '2']); // For event comparison.
|
||||
$this->assertEquals(true, $DB->record_exists('course_modules', ['id' => $assign2->cmid, 'deletioninprogress' => 1,
|
||||
'section' => $section->id]));
|
||||
|
||||
// Non-empty section, no forcedelete, so no change.
|
||||
$this->assertFalse($sectionactions->delete(get_fast_modinfo($course)->get_section_info(2), false, true));
|
||||
|
||||
$sink = $this->redirectEvents();
|
||||
$this->assertTrue($sectionactions->delete(get_fast_modinfo($course)->get_section_info(2), true, true));
|
||||
|
||||
// Now, confirm that:
|
||||
// a) the section's modules have been flagged for deletion and moved to section 0 and;
|
||||
// b) the section has been deleted and;
|
||||
// c) course_section_deleted event has been fired. The course_module_deleted events will only fire once they have been
|
||||
// removed from section 0 via the adhoc task.
|
||||
|
||||
// Modules should have been flagged for deletion and moved to section 0.
|
||||
$sectionid = $DB->get_field('course_sections', 'id', ['course' => $course->id, 'section' => 0]);
|
||||
$this->assertEquals(
|
||||
3,
|
||||
$DB->count_records('course_modules', ['section' => $sectionid, 'deletioninprogress' => 1])
|
||||
);
|
||||
|
||||
// Confirm the section has been deleted.
|
||||
$this->assertEquals(2, course_get_format($course)->get_last_section_number());
|
||||
|
||||
// Check event fired.
|
||||
$events = $sink->get_events();
|
||||
$event = array_pop($events);
|
||||
$sink->close();
|
||||
$this->assertInstanceOf('\core\event\course_section_deleted', $event);
|
||||
$this->assertEquals($section->id, $event->objectid);
|
||||
$this->assertEquals($USER->id, $event->userid);
|
||||
$this->assertEquals('course_sections', $event->objecttable);
|
||||
$this->assertEquals(null, $event->get_url());
|
||||
$this->assertEquals($section, $event->get_record_snapshot('course_sections', $section->id));
|
||||
|
||||
// Now, run the adhoc task to delete the modules from section 0.
|
||||
$sink = $this->redirectEvents(); // To capture the events.
|
||||
\phpunit_util::run_all_adhoc_tasks();
|
||||
|
||||
// Confirm the modules have been deleted.
|
||||
list($insql, $assignids) = $DB->get_in_or_equal([$assign0->cmid, $assign1->cmid, $assign2->cmid]);
|
||||
$cmcount = $DB->count_records_select('course_modules', 'id ' . $insql, $assignids);
|
||||
$this->assertEmpty($cmcount);
|
||||
|
||||
// Confirm other modules in section 0 still remain.
|
||||
$this->assertEquals(1, $DB->count_records('course_modules', ['id' => $assign3->cmid]));
|
||||
|
||||
// Confirm that events were generated for all 3 of the modules.
|
||||
$events = $sink->get_events();
|
||||
$sink->close();
|
||||
$count = 0;
|
||||
while (!empty($events)) {
|
||||
$event = array_pop($events);
|
||||
if ($event instanceof \core\event\course_module_deleted &&
|
||||
in_array($event->objectid, [$assign0->cmid, $assign1->cmid, $assign2->cmid])) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
$this->assertEquals(3, $count);
|
||||
}
|
||||
}
|
171
course/lib.php
171
course/lib.php
|
@ -26,6 +26,7 @@ defined('MOODLE_INTERNAL') || die;
|
|||
|
||||
use core_course\external\course_summary_exporter;
|
||||
use core_courseformat\base as course_format;
|
||||
use core_courseformat\formatactions;
|
||||
use core\output\local\action_menu\subpanel as action_menu_subpanel;
|
||||
|
||||
require_once($CFG->libdir.'/completionlib.php');
|
||||
|
@ -541,42 +542,7 @@ function add_course_module($mod) {
|
|||
* @return stdClass created section object
|
||||
*/
|
||||
function course_create_section($courseorid, $position = 0, $skipcheck = false) {
|
||||
global $DB;
|
||||
$courseid = is_object($courseorid) ? $courseorid->id : $courseorid;
|
||||
|
||||
// Find the last sectionnum among existing sections.
|
||||
if ($skipcheck) {
|
||||
$lastsection = $position - 1;
|
||||
} else {
|
||||
$lastsection = (int)$DB->get_field_sql('SELECT max(section) from {course_sections} WHERE course = ?', [$courseid]);
|
||||
}
|
||||
|
||||
// First add section to the end.
|
||||
$cw = new stdClass();
|
||||
$cw->course = $courseid;
|
||||
$cw->section = $lastsection + 1;
|
||||
$cw->summary = '';
|
||||
$cw->summaryformat = FORMAT_HTML;
|
||||
$cw->sequence = '';
|
||||
$cw->name = null;
|
||||
$cw->visible = 1;
|
||||
$cw->availability = null;
|
||||
$cw->component = null;
|
||||
$cw->itemid = null;
|
||||
$cw->timemodified = time();
|
||||
$cw->id = $DB->insert_record("course_sections", $cw);
|
||||
|
||||
// Now move it to the specified position.
|
||||
if ($position > 0 && $position <= $lastsection) {
|
||||
$course = is_object($courseorid) ? $courseorid : get_course($courseorid);
|
||||
move_section_to($course, $cw->section, $position, true);
|
||||
$cw->section = $position;
|
||||
}
|
||||
|
||||
core\event\course_section_created::create_from_section($cw)->trigger();
|
||||
|
||||
rebuild_course_cache($courseid, true);
|
||||
return $cw;
|
||||
return formatactions::section($courseorid)->create($position, $skipcheck);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -590,14 +556,7 @@ function course_create_sections_if_missing($courseorid, $sections) {
|
|||
if (!is_array($sections)) {
|
||||
$sections = array($sections);
|
||||
}
|
||||
$existing = array_keys(get_fast_modinfo($courseorid)->get_section_info_all());
|
||||
if ($newsections = array_diff($sections, $existing)) {
|
||||
foreach ($newsections as $sectionnum) {
|
||||
course_create_section($courseorid, $sectionnum, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return formatactions::section($courseorid)->create_if_missing($sections);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1278,61 +1237,18 @@ function move_section_to($course, $section, $destination, $ignorenumsections = f
|
|||
* check if section can actually be deleted.
|
||||
*
|
||||
* @param int|stdClass $course
|
||||
* @param int|stdClass|section_info $section
|
||||
* @param int|stdClass|section_info $sectionornum
|
||||
* @param bool $forcedeleteifnotempty if set to false section will not be deleted if it has modules in it.
|
||||
* @param bool $async whether or not to try to delete the section using an adhoc task. Async also depends on a plugin hook.
|
||||
* @return bool whether section was deleted
|
||||
*/
|
||||
function course_delete_section($course, $section, $forcedeleteifnotempty = true, $async = false) {
|
||||
global $DB;
|
||||
|
||||
// Prepare variables.
|
||||
$courseid = (is_object($course)) ? $course->id : (int)$course;
|
||||
$sectionnum = (is_object($section)) ? $section->section : (int)$section;
|
||||
$section = $DB->get_record('course_sections', array('course' => $courseid, 'section' => $sectionnum));
|
||||
if (!$section) {
|
||||
// No section exists, can't proceed.
|
||||
function course_delete_section($course, $sectionornum, $forcedeleteifnotempty = true, $async = false) {
|
||||
$sectionnum = (is_object($sectionornum)) ? $sectionornum->section : (int)$sectionornum;
|
||||
$sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
|
||||
if (!$sectioninfo) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check the 'course_module_background_deletion_recommended' hook first.
|
||||
// Only use asynchronous deletion if at least one plugin returns true and if async deletion has been requested.
|
||||
// Both are checked because plugins should not be allowed to dictate the deletion behaviour, only support/decline it.
|
||||
// It's up to plugins to handle things like whether or not they are enabled.
|
||||
if ($async && $pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended')) {
|
||||
foreach ($pluginsfunction as $plugintype => $plugins) {
|
||||
foreach ($plugins as $pluginfunction) {
|
||||
if ($pluginfunction()) {
|
||||
return course_delete_section_async($section, $forcedeleteifnotempty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$format = course_get_format($course);
|
||||
$sectionname = $format->get_section_name($section);
|
||||
|
||||
// Delete section.
|
||||
$result = $format->delete_section($section, $forcedeleteifnotempty);
|
||||
|
||||
// Trigger an event for course section deletion.
|
||||
if ($result) {
|
||||
$context = context_course::instance($courseid);
|
||||
$event = \core\event\course_section_deleted::create(
|
||||
array(
|
||||
'objectid' => $section->id,
|
||||
'courseid' => $courseid,
|
||||
'context' => $context,
|
||||
'other' => array(
|
||||
'sectionnum' => $section->section,
|
||||
'sectionname' => $sectionname,
|
||||
)
|
||||
)
|
||||
);
|
||||
$event->add_record_snapshot('course_sections', $section);
|
||||
$event->trigger();
|
||||
}
|
||||
return $result;
|
||||
return formatactions::section($course)->delete($sectioninfo, $forcedeleteifnotempty, $async);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1346,75 +1262,14 @@ function course_delete_section($course, $section, $forcedeleteifnotempty = true,
|
|||
* @return bool true if the section was scheduled for deletion, false otherwise.
|
||||
*/
|
||||
function course_delete_section_async($section, $forcedeleteifnotempty = true) {
|
||||
global $DB, $USER;
|
||||
|
||||
// Objects only, and only valid ones.
|
||||
if (!is_object($section) || empty($section->id)) {
|
||||
if (!is_object($section) || empty($section->id) || empty($section->course)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Does the object currently exist in the DB for removal (check for stale objects).
|
||||
$section = $DB->get_record('course_sections', array('id' => $section->id));
|
||||
if (!$section || !$section->section) {
|
||||
// No section exists, or the section is 0. Can't proceed.
|
||||
$sectioninfo = get_fast_modinfo($section->course)->get_section_info_by_id($section->id);
|
||||
if (!$sectioninfo) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check whether the section can be removed.
|
||||
if (!$forcedeleteifnotempty && (!empty($section->sequence) || !empty($section->summary))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$format = course_get_format($section->course);
|
||||
$sectionname = $format->get_section_name($section);
|
||||
|
||||
// Flag those modules having no existing deletion flag. Some modules may have been scheduled for deletion manually, and we don't
|
||||
// want to create additional adhoc deletion tasks for these. Moving them to section 0 will suffice.
|
||||
$affectedmods = $DB->get_records_select('course_modules', 'course = ? AND section = ? AND deletioninprogress <> ?',
|
||||
[$section->course, $section->id, 1], '', 'id');
|
||||
$DB->set_field('course_modules', 'deletioninprogress', '1', ['course' => $section->course, 'section' => $section->id]);
|
||||
|
||||
// Move all modules to section 0.
|
||||
$modules = $DB->get_records('course_modules', ['section' => $section->id], '');
|
||||
$sectionzero = $DB->get_record('course_sections', ['course' => $section->course, 'section' => '0']);
|
||||
foreach ($modules as $mod) {
|
||||
moveto_module($mod, $sectionzero);
|
||||
}
|
||||
|
||||
// Create and queue an adhoc task for the deletion of the modules.
|
||||
$removaltask = new \core_course\task\course_delete_modules();
|
||||
$data = array(
|
||||
'cms' => $affectedmods,
|
||||
'userid' => $USER->id,
|
||||
'realuserid' => \core\session\manager::get_realuser()->id
|
||||
);
|
||||
$removaltask->set_custom_data($data);
|
||||
\core\task\manager::queue_adhoc_task($removaltask);
|
||||
|
||||
// Delete the now empty section, passing in only the section number, which forces the function to fetch a new object.
|
||||
// The refresh is needed because the section->sequence is now stale.
|
||||
$result = $format->delete_section($section->section, $forcedeleteifnotempty);
|
||||
|
||||
// Trigger an event for course section deletion.
|
||||
if ($result) {
|
||||
$context = \context_course::instance($section->course);
|
||||
$event = \core\event\course_section_deleted::create(
|
||||
array(
|
||||
'objectid' => $section->id,
|
||||
'courseid' => $section->course,
|
||||
'context' => $context,
|
||||
'other' => array(
|
||||
'sectionnum' => $section->section,
|
||||
'sectionname' => $sectionname,
|
||||
)
|
||||
)
|
||||
);
|
||||
$event->add_record_snapshot('course_sections', $section);
|
||||
$event->trigger();
|
||||
}
|
||||
rebuild_course_cache($section->course, true);
|
||||
|
||||
return $result;
|
||||
return formatactions::section($section->course)->delete_async($sectioninfo, $forcedeleteifnotempty);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,6 +12,9 @@ information provided here is intended especially for developers.
|
|||
- `course_purge_section_cache`
|
||||
- `course_purge_module_cache`
|
||||
- `get_array_of_activities`
|
||||
* New format actions classes. Those classes will eventually replace all course/lib.php content editing functions.
|
||||
All new methods are distributed in three classes formats can extend. Method can be accessed using static
|
||||
methods (see doc block of core_courseformat\formatactions for more information).
|
||||
|
||||
=== 4.3 ===
|
||||
* The `core_course_renderer::course_section_cm_completion` method has been removed, and can no longer be used
|
||||
|
|
|
@ -45,6 +45,7 @@ $string['cachedef_contentbank_allowed_categories'] = 'Allowed content bank cours
|
|||
$string['cachedef_contentbank_allowed_courses'] = 'Allowed content bank courses for current user';
|
||||
$string['cachedef_contentbank_enabled_extensions'] = 'Allowed extensions and its supporter plugins in content bank';
|
||||
$string['cachedef_contentbank_context_extensions'] = 'Allowed extensions and its supporter plugins in a content bank context';
|
||||
$string['cachedef_courseactionsinstances'] = 'Loaded course actions instances';
|
||||
$string['cachedef_coursecat'] = 'Course categories lists for particular user';
|
||||
$string['cachedef_coursecatrecords'] = 'Course categories records';
|
||||
$string['cachedef_coursesectionspreferences'] = 'Course section preferences';
|
||||
|
|
|
@ -238,6 +238,16 @@ $definitions = array(
|
|||
'simplekeys' => true,
|
||||
'simpledata' => true,
|
||||
],
|
||||
// Course actions instances cache.
|
||||
'courseactionsinstances' => [
|
||||
'mode' => cache_store::MODE_REQUEST,
|
||||
'simplekeys' => true,
|
||||
'simpledata' => false,
|
||||
'staticacceleration' => true,
|
||||
// Executing actions in more than 10 courses usually means executing the same action on each course
|
||||
// so there is no need for caching individual course instances.
|
||||
'staticaccelerationsize' => 10,
|
||||
],
|
||||
// Used to store data for repositories to avoid repetitive DB queries within one request.
|
||||
'repositories' => array(
|
||||
'mode' => cache_store::MODE_REQUEST,
|
||||
|
|
|
@ -87,6 +87,12 @@ class course_modinfo {
|
|||
*/
|
||||
private $sectioninfobyid;
|
||||
|
||||
/**
|
||||
* Index of delegated sections (indexed by component and itemid)
|
||||
* @var array
|
||||
*/
|
||||
private $delegatedsections;
|
||||
|
||||
/**
|
||||
* User ID
|
||||
* @var int
|
||||
|
@ -350,6 +356,36 @@ class course_modinfo {
|
|||
return $this->sectioninfobyid[$sectionid];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets data about specific delegated section.
|
||||
* @param string $component Component name
|
||||
* @param int $itemid Item id
|
||||
* @param int $strictness Use MUST_EXIST to throw exception if it doesn't
|
||||
* @return section_info|null Information for numbered section or null if not found
|
||||
*/
|
||||
public function get_section_info_by_component(
|
||||
string $component,
|
||||
int $itemid,
|
||||
int $strictness = IGNORE_MISSING
|
||||
): ?section_info {
|
||||
if (!isset($this->delegatedsections[$component][$itemid])) {
|
||||
if ($strictness === MUST_EXIST) {
|
||||
throw new moodle_exception('sectionnotexist');
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return $this->delegatedsections[$component][$itemid];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the course has delegated sections.
|
||||
* @return bool
|
||||
*/
|
||||
public function has_delegated_sections(): bool {
|
||||
return !empty($this->delegatedsections);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static cache for generated course_modinfo instances
|
||||
*
|
||||
|
@ -582,11 +618,18 @@ class course_modinfo {
|
|||
// Expand section objects
|
||||
$this->sectioninfobynum = [];
|
||||
$this->sectioninfobyid = [];
|
||||
$this->delegatedsections = [];
|
||||
foreach ($coursemodinfo->sectioncache as $data) {
|
||||
$sectioninfo = new section_info($data, $data->section, null, null,
|
||||
$this, null);
|
||||
$this->sectioninfobynum[$data->section] = $sectioninfo;
|
||||
$this->sectioninfobyid[$data->id] = $sectioninfo;
|
||||
if (!empty($sectioninfo->component)) {
|
||||
if (!isset($this->delegatedsections[$sectioninfo->component])) {
|
||||
$this->delegatedsections[$sectioninfo->component] = [];
|
||||
}
|
||||
$this->delegatedsections[$sectioninfo->component][$sectioninfo->itemid] = $sectioninfo;
|
||||
}
|
||||
}
|
||||
ksort($this->sectioninfobynum);
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ use coding_exception;
|
|||
use context_course;
|
||||
use context_module;
|
||||
use course_modinfo;
|
||||
use core_courseformat\formatactions;
|
||||
use moodle_exception;
|
||||
use moodle_url;
|
||||
use Exception;
|
||||
|
@ -1112,6 +1113,123 @@ class modinfolib_test extends advanced_testcase {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_section_info_by_component method
|
||||
*
|
||||
* @covers \course_modinfo::get_section_info_by_component
|
||||
* @dataProvider get_section_info_by_component_provider
|
||||
*
|
||||
* @param string $component the component name
|
||||
* @param int $itemid the section number
|
||||
* @param int $strictness the search strict mode
|
||||
* @param bool $expectnull if the function will return a null
|
||||
* @param bool $expectexception if the function will throw an exception
|
||||
*/
|
||||
public function test_get_section_info_by_component(
|
||||
string $component,
|
||||
int $itemid,
|
||||
int $strictness,
|
||||
bool $expectnull,
|
||||
bool $expectexception
|
||||
): void {
|
||||
$this->resetAfterTest();
|
||||
$course = $this->getDataGenerator()->create_course(['numsections' => 1]);
|
||||
|
||||
formatactions::section($course)->create_delegated('mod_forum', 42);
|
||||
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
|
||||
if ($expectexception) {
|
||||
$this->expectException(moodle_exception::class);
|
||||
}
|
||||
|
||||
$section = $modinfo->get_section_info_by_component($component, $itemid, $strictness);
|
||||
|
||||
if ($expectnull) {
|
||||
$this->assertNull($section);
|
||||
} else {
|
||||
$this->assertEquals($component, $section->component);
|
||||
$this->assertEquals($itemid, $section->itemid);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_get_section_info_by_component().
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_section_info_by_component_provider(): array {
|
||||
return [
|
||||
'Valid component and itemid' => [
|
||||
'component' => 'mod_forum',
|
||||
'itemid' => 42,
|
||||
'strictness' => IGNORE_MISSING,
|
||||
'expectnull' => false,
|
||||
'expectexception' => false,
|
||||
],
|
||||
'Invalid component' => [
|
||||
'component' => 'mod_nonexisting',
|
||||
'itemid' => 42,
|
||||
'strictness' => IGNORE_MISSING,
|
||||
'expectnull' => true,
|
||||
'expectexception' => false,
|
||||
],
|
||||
'Invalid itemid' => [
|
||||
'component' => 'mod_forum',
|
||||
'itemid' => 0,
|
||||
'strictness' => IGNORE_MISSING,
|
||||
'expectnull' => true,
|
||||
'expectexception' => false,
|
||||
],
|
||||
'Invalid component and itemid' => [
|
||||
'component' => 'mod_nonexisting',
|
||||
'itemid' => 0,
|
||||
'strictness' => IGNORE_MISSING,
|
||||
'expectnull' => true,
|
||||
'expectexception' => false,
|
||||
],
|
||||
'Invalid component must exists' => [
|
||||
'component' => 'mod_nonexisting',
|
||||
'itemid' => 42,
|
||||
'strictness' => MUST_EXIST,
|
||||
'expectnull' => true,
|
||||
'expectexception' => true,
|
||||
],
|
||||
'Invalid itemid must exists' => [
|
||||
'component' => 'mod_forum',
|
||||
'itemid' => 0,
|
||||
'strictness' => MUST_EXIST,
|
||||
'expectnull' => true,
|
||||
'expectexception' => true,
|
||||
],
|
||||
'Invalid component and itemid must exists' => [
|
||||
'component' => 'mod_nonexisting',
|
||||
'itemid' => 0,
|
||||
'strictness' => MUST_EXIST,
|
||||
'expectnull' => false,
|
||||
'expectexception' => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test has_delegated_sections method
|
||||
*
|
||||
* @covers \course_modinfo::has_delegated_sections
|
||||
*/
|
||||
public function test_has_delegated_sections(): void {
|
||||
$this->resetAfterTest();
|
||||
$course = $this->getDataGenerator()->create_course(['numsections' => 1]);
|
||||
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
$this->assertFalse($modinfo->has_delegated_sections());
|
||||
|
||||
formatactions::section($course)->create_delegated('mod_forum', 42);
|
||||
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
$this->assertTrue($modinfo->has_delegated_sections());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test purge_section_cache_by_id method
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue