mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 13:02:07 +02:00
MDL-32570 enrol_imsenterprise: don't hardcode default category id
This commit is contained in:
parent
d45e65ccad
commit
259e7e6207
@ -415,10 +415,10 @@ function process_group_tag($tagcontents) {
|
||||
} else {
|
||||
// If not found and not allowed to create, stick with default
|
||||
$this->log_line('Category '.$group->category.' not found in Moodle database, so using default category instead.');
|
||||
$course->category = 1;
|
||||
$course->category = $this->get_default_category_id();
|
||||
}
|
||||
} else {
|
||||
$course->category = 1;
|
||||
$course->category = $this->get_default_category_id();
|
||||
}
|
||||
$course->timecreated = time();
|
||||
$course->startdate = time();
|
||||
@ -834,6 +834,27 @@ function load_role_mappings() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the default category id (often known as 'Miscellaneous'),
|
||||
* statically cached to avoid multiple DB lookups on big imports.
|
||||
*
|
||||
* @return int id of default category.
|
||||
*/
|
||||
private function get_default_category_id() {
|
||||
global $CFG;
|
||||
require_once($CFG->libdir.'/coursecatlib.php');
|
||||
|
||||
static $defaultcategoryid = null;
|
||||
|
||||
if ($defaultcategoryid === null) {
|
||||
$category = coursecat::get_default();
|
||||
$defaultcategoryid = $category->id;
|
||||
}
|
||||
|
||||
return $defaultcategoryid;
|
||||
}
|
||||
|
||||
|
||||
} // end of class
|
||||
|
||||
|
||||
|
@ -153,6 +153,32 @@ class enrol_imsenterprise_testcase extends advanced_testcase {
|
||||
$this->assertEquals(($prevncourses + 2), $DB->count_records('course'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new course without a category.
|
||||
*/
|
||||
public function test_course_add_default_category() {
|
||||
global $DB, $CFG;
|
||||
require_once($CFG->libdir.'/coursecatlib.php');
|
||||
|
||||
$this->imsplugin->set_config('createnewcategories', false);
|
||||
|
||||
// Delete the default category, to ensure the plugin handles this gracefully.
|
||||
$defaultcat = coursecat::get_default();
|
||||
$defaultcat->delete_full(false);
|
||||
|
||||
// Create an course with the IMS plugin without a category.
|
||||
$course1 = new stdClass();
|
||||
$course1->idnumber = 'id1';
|
||||
$course1->imsshort = 'id1';
|
||||
$course1->category = '';
|
||||
$this->set_xml_file(false, array($course1));
|
||||
$this->imsplugin->cron();
|
||||
|
||||
// Check the course has been created.
|
||||
$dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber), '*', MUST_EXIST);
|
||||
// Check that it belongs to a category which exists.
|
||||
$this->assertTrue($DB->record_exists('course_categories', array('id' => $dbcourse->category)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Course attributes mapping to IMS enterprise group description tags
|
||||
|
Loading…
x
Reference in New Issue
Block a user