MDL-71165 course: core_course_update_course external method

This commit is contained in:
Sara Arjona 2021-03-25 08:59:15 +01:00 committed by Andrew Nicols
parent a9b0f4dafe
commit 6347b916bc
12 changed files with 2052 additions and 1 deletions

View file

@ -514,6 +514,14 @@ $functions = array(
'type' => 'read',
'ajax' => true,
],
'core_course_update_course' => [
'classname' => 'core_course\external\update_course',
'methodname' => 'execute',
'description' => 'Update course contents.',
'type' => 'write',
'ajax' => true,
'capabilities' => 'moodle/course:sectionvisibility, moodle/course:activityvisibility',
],
'core_course_edit_module' => array(
'classname' => 'core_course_external',
'methodname' => 'edit_module',

View file

@ -92,6 +92,12 @@ class course_modinfo {
*/
private $sections;
/**
* Array from section id => section num.
* @var array
*/
private $sectionids;
/**
* Array from int (cm id) => cm_info object
* @var cm_info[]
@ -330,6 +336,24 @@ class course_modinfo {
return $this->sectioninfo[$sectionnumber];
}
/**
* Gets data about specific section ID.
* @param int $sectionid ID (not number) of section
* @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_id(int $sectionid, int $strictness = IGNORE_MISSING): ?section_info {
if (!isset($this->sectionids[$sectionid])) {
if ($strictness === MUST_EXIST) {
throw new moodle_exception('sectionnotexist');
} else {
return null;
}
}
return $this->get_section_info($this->sectionids[$sectionid], $strictness);
}
/**
* Static cache for generated course_modinfo instances
*
@ -469,6 +493,7 @@ class course_modinfo {
// Set initial values
$this->userid = $userid;
$this->sections = array();
$this->sectionids = [];
$this->cms = array();
$this->instances = array();
$this->groups = null;
@ -540,6 +565,7 @@ class course_modinfo {
// Expand section objects
$this->sectioninfo = array();
foreach ($coursemodinfo->sectioncache as $number => $data) {
$this->sectionids[$data->id] = $number;
$this->sectioninfo[$number] = new section_info($data, $number, null, null,
$this, null);
}

View file

@ -33,7 +33,7 @@ require_once($CFG->libdir . '/modinfolib.php');
* @copyright 2012 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_modinfolib_testcase extends advanced_testcase {
class modinfolib_test extends advanced_testcase {
public function test_section_info_properties() {
global $DB, $CFG;
@ -919,4 +919,86 @@ class core_modinfolib_testcase extends advanced_testcase {
list($course, $cm) = get_course_and_cm_from_cmid($hiddenpage->cmid, 'page', 0, $manager->id);
$this->assertTrue($cm->uservisible);
}
/**
* Test test_get_section_info_by_id method
*
* @dataProvider get_section_info_by_id_provider
* @covers ::get_section_info_by_id
*
* @param int $sectionnum 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_id(
int $sectionnum,
int $strictness = IGNORE_MISSING,
bool $expectnull = false,
bool $expectexception = false
) {
global $DB;
$this->resetAfterTest();
// Create a course with 4 sections.
$course = $this->getDataGenerator()->create_course(['numsections' => 4]);
// Index sections.
$sectionindex = [];
$modinfo = get_fast_modinfo($course);
$allsections = $modinfo->get_section_info_all();
foreach ($allsections as $section) {
$sectionindex[$section->section] = $section->id;
}
if ($expectexception) {
$this->expectException(moodle_exception::class);
}
$sectionid = $sectionindex[$sectionnum] ?? -1;
$section = $modinfo->get_section_info_by_id($sectionid, $strictness);
if ($expectnull) {
$this->assertNull($section);
} else {
$this->assertEquals($sectionid, $section->id);
$this->assertEquals($sectionnum, $section->section);
}
}
/**
* Data provider for test_get_section_info_by_id().
*
* @return array
*/
public function get_section_info_by_id_provider() {
return [
'Valid section id' => [
'sectionnum' => 1,
'strictness' => IGNORE_MISSING,
'expectnull' => false,
'expectexception' => false,
],
'Section zero' => [
'sectionnum' => 0,
'strictness' => IGNORE_MISSING,
'expectnull' => false,
'expectexception' => false,
],
'invalid section ignore missing' => [
'sectionnum' => -1,
'strictness' => IGNORE_MISSING,
'expectnull' => true,
'expectexception' => false,
],
'invalid section must exists' => [
'sectionnum' => -1,
'strictness' => MUST_EXIST,
'expectnull' => false,
'expectexception' => true,
],
];
}
}