Merge branch 'MDL-41417_m24' of https://github.com/markn86/moodle into MOODLE_24_STABLE
This commit is contained in:
+13
-9
@@ -328,19 +328,23 @@ class course_edit_form extends moodleform {
|
||||
|
||||
/// perform some extra moodle validation
|
||||
function validation($data, $files) {
|
||||
global $DB, $CFG;
|
||||
global $DB;
|
||||
|
||||
$errors = parent::validation($data, $files);
|
||||
if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
|
||||
if (!empty($data['id'])) {
|
||||
unset($foundcourses[$data['id']]);
|
||||
|
||||
// Add field validation check for duplicate shortname.
|
||||
if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
|
||||
if (empty($data['id']) || $course->id != $data['id']) {
|
||||
$errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
|
||||
}
|
||||
if (!empty($foundcourses)) {
|
||||
foreach ($foundcourses as $foundcourse) {
|
||||
$foundcoursenames[] = $foundcourse->fullname;
|
||||
}
|
||||
|
||||
// Add field validation check for duplicate idnumber.
|
||||
if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
|
||||
if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
|
||||
if (empty($data['id']) || $course->id != $data['id']) {
|
||||
$errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
|
||||
}
|
||||
$foundcoursenamestring = implode(',', $foundcoursenames);
|
||||
$errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3962,6 +3962,20 @@ function update_course($data, $editoroptions = NULL) {
|
||||
$data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0);
|
||||
}
|
||||
|
||||
// Check we don't have a duplicate shortname.
|
||||
if (!empty($data->shortname) && $oldcourse->shortname != $data->shortname) {
|
||||
if ($DB->record_exists('course', array('shortname' => $data->shortname))) {
|
||||
throw new moodle_exception('shortnametaken', '', '', $data->shortname);
|
||||
}
|
||||
}
|
||||
|
||||
// Check we don't have a duplicate idnumber.
|
||||
if (!empty($data->idnumber) && $oldcourse->idnumber != $data->idnumber) {
|
||||
if ($DB->record_exists('course', array('idnumber' => $data->idnumber))) {
|
||||
throw new moodle_exception('courseidnumbertaken', '', '', $data->idnumber);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($data->category) or empty($data->category)) {
|
||||
// prevent nulls and 0 in category field
|
||||
unset($data->category);
|
||||
|
||||
@@ -97,6 +97,53 @@ class courselib_testcase extends advanced_testcase {
|
||||
$this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
|
||||
}
|
||||
|
||||
public function test_update_course() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$defaultcategory = $DB->get_field_select('course_categories', 'MIN(id)', 'parent = 0');
|
||||
|
||||
$course = new stdClass();
|
||||
$course->fullname = 'Apu loves Unit Təsts';
|
||||
$course->shortname = 'test1';
|
||||
$course->idnumber = '1';
|
||||
$course->summary = 'Awesome!';
|
||||
$course->summaryformat = FORMAT_PLAIN;
|
||||
$course->format = 'topics';
|
||||
$course->newsitems = 0;
|
||||
$course->numsections = 5;
|
||||
$course->category = $defaultcategory;
|
||||
|
||||
$created = create_course($course);
|
||||
// Ensure the checks only work on idnumber/shortname that are not already ours.
|
||||
update_course($created);
|
||||
|
||||
$course->shortname = 'test2';
|
||||
$course->idnumber = '2';
|
||||
|
||||
$created2 = create_course($course);
|
||||
|
||||
// Test duplicate idnumber.
|
||||
$created2->idnumber = '1';
|
||||
try {
|
||||
update_course($created2);
|
||||
$this->fail('Expected exception when trying to update a course with duplicate idnumber');
|
||||
} catch (moodle_exception $e) {
|
||||
$this->assertEquals(get_string('courseidnumbertaken', 'error', $created2->idnumber), $e->getMessage());
|
||||
}
|
||||
|
||||
// Test duplicate shortname.
|
||||
$created2->idnumber = '2';
|
||||
$created2->shortname = 'test1';
|
||||
try {
|
||||
update_course($created2);
|
||||
$this->fail('Expected exception when trying to update a course with a duplicate shortname');
|
||||
} catch (moodle_exception $e) {
|
||||
$this->assertEquals(get_string('shortnametaken', 'error', $created2->shortname), $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function test_course_add_cm_to_section() {
|
||||
global $DB;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
Reference in New Issue
Block a user