Skip to content

Commit

Permalink
MDL-41256 course: improved debugging on course creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mackensen committed Aug 26, 2013
1 parent 838d78a commit 9930e42
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions course/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,15 +721,15 @@ public static function update_courses($courses) {
if (array_key_exists('shortname', $course) && ($oldcourse->shortname != $course['shortname'])) {
require_capability('moodle/course:changeshortname', $context);
if ($DB->record_exists('course', array('shortname' => $course['shortname']))) {
throw new moodle_exception('shortnametaken');
throw new moodle_exception('shortnametaken', '', '', $course['shortname']);
}
}

// Check if the id number already exist and user have capability.
if (array_key_exists('idnumber', $course) && ($oldcourse->idnumber != $course['idnumber'])) {
require_capability('moodle/course:changeidnumber', $context);
if ($DB->record_exists('course', array('idnumber' => $course['idnumber']))) {
throw new moodle_exception('idnumbertaken');
throw new moodle_exception('courseidnumbertaken', '', '', $course['idnumber']);
}
}

Expand Down
8 changes: 4 additions & 4 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2234,17 +2234,17 @@ function create_course($data, $editoroptions = NULL) {
//check the categoryid - must be given for all new courses
$category = $DB->get_record('course_categories', array('id'=>$data->category), '*', MUST_EXIST);

//check if the shortname already exist
// Check if the shortname already exists.
if (!empty($data->shortname)) {
if ($DB->record_exists('course', array('shortname' => $data->shortname))) {
throw new moodle_exception('shortnametaken');
throw new moodle_exception('shortnametaken', '', '', $data->shortname);
}
}

//check if the id number already exist
// Check if the idnumber already exists.
if (!empty($data->idnumber)) {
if ($DB->record_exists('course', array('idnumber' => $data->idnumber))) {
throw new moodle_exception('idnumbertaken');
throw new moodle_exception('courseidnumbertaken', '', '', $data->idnumber);
}
}

Expand Down
17 changes: 17 additions & 0 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,23 @@ public function test_create_course() {
// Ensure blocks have been associated to the course.
$blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id));
$this->assertGreaterThan(0, $blockcount);

// Ensure that the shortname isn't duplicated.
try {
$created = create_course($course);
} catch (moodle_exception $e) {
$message = $e->getMessage();
}
$this->assertEquals($message, get_string('shortnametaken', 'error', $course->shortname));

// Ensure that the idnumber isn't duplicated.
$course->shortname .= '1';
try {
$created = create_course($course);
} catch (moodle_exception $e) {
$message = $e->getMessage();
}
$this->assertEquals($message, get_string('courseidnumbertaken', 'error', $course->idnumber));
}

public function test_create_course_with_generator() {
Expand Down
5 changes: 3 additions & 2 deletions lang/en/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
$string['courseformatnotfound'] = 'The course format \'{$a}\' doesn\'t exist or is not recognized';
$string['coursegroupunknown'] = 'Course corresponding to group {$a} not specified';
$string['courseidnotfound'] = 'Course id doesn\'t exist';
$string['courseidnumbertaken'] = 'ID number is already used for another course ({$a})';
$string['coursemisconf'] = 'Course is misconfigured';
$string['courserequestdisabled'] = 'Sorry, but course requests have been disabled by the administrator.';
$string['csvcolumnduplicates'] = 'Duplicate columns detected';
Expand Down Expand Up @@ -267,7 +268,7 @@
$string['hackdetected'] = 'Hack attack detected!';
$string['hashpoolproblem'] = 'Incorrect pool file content {$a}.';
$string['headersent'] = 'Headers already sent';
$string['idnumbertaken'] = 'ID number is already used for another course';
$string['idnumbertaken'] = 'This ID number is already in use';
$string['idnumbertoolong'] = 'ID number is too long';
$string['importformatnotimplement'] = 'Sorry, importing this format is not yet implemented!';
$string['incorrectext'] = 'File has an incorrect extension';
Expand Down Expand Up @@ -469,7 +470,7 @@
$string['sessionerroruser2'] = 'A server error that affects your login session was detected. Please login again or restart your browser.';
$string['sessionipnomatch'] = 'Sorry, but your IP number seems to have changed from when you first logged in. This security feature prevents crackers stealing your identity while logged in to this site. Normal users should not be seeing this message - please ask the site administrator for help.';
$string['sessionipnomatch2'] = 'Sorry, but your IP number seems to have changed from when you first logged in. This security feature prevents crackers stealing your identity while logged in to this site. You may see this error if you use wireless networks or if you are roaming between different networks. Please ask the site administrator for more help.<br /><br />If you want to continue please press F5 key to refresh this page.';
$string['shortnametaken'] = 'Short name is already used for another course';
$string['shortnametaken'] = 'Short name is already used for another course ({$a})';
$string['scheduledbackupsdisabled'] = 'Scheduled backups have been disabled by the server admin';
$string['socksnotsupported'] = 'SOCKS5 proxy is not supported in PHP4';
$string['spellcheckernotconf'] = 'Spellchecker not configured';
Expand Down

0 comments on commit 9930e42

Please sign in to comment.