MDL-13114 tool_uploadcourse: ID number errors handling
authorFrederic Massart <fred@moodle.com>
Mon, 1 Jul 2013 05:47:50 +0000 (13:47 +0800)
committerFrederic Massart <fred@moodle.com>
Mon, 15 Jul 2013 02:02:55 +0000 (10:02 +0800)
admin/tool/uploadcourse/classes/course.php
admin/tool/uploadcourse/tests/course_test.php

index 20ff323..abb3ee0 100644 (file)
@@ -374,6 +374,7 @@ class tool_uploadcourse_course {
      * @return bool false is any error occured.
      */
     public function prepare() {
+        global $DB;
         $this->prepared = true;
 
         // Validate the shortname.
@@ -536,6 +537,14 @@ class tool_uploadcourse_course {
             }
         }
 
+        // If the course does not exist, ensure that the ID number is not taken.
+        if (!$exists && isset($coursedata['idnumber'])) {
+            if ($DB->count_records_select('course', 'idnumber = :idn', array('idn' => $coursedata['idnumber'])) > 0) {
+                $this->error('idnumberalreadyinuse', new lang_string('idnumberalreadyinuse', 'tool_uploadcourse'));
+                return false;
+            }
+        }
+
         // Ultimate check mode vs. existence.
         switch ($mode) {
             case tool_uploadcourse_processor::MODE_CREATE_NEW:
index 42dd5c8..c1af49e 100644 (file)
@@ -779,4 +779,30 @@ class tool_uploadcourse_course_testcase extends advanced_testcase {
         $this->assertEquals(strtotime('1970-01-01 GMT + ' . $data['enrolment_1_enrolperiod']), $enroldata['manual']->enrolperiod);
         $this->assertEquals(strtotime('12th July 2013'), $enroldata['manual']->enrolenddate);
     }
+
+    public function test_idnumber_problems() {
+        $this->resetAfterTest(true);
+
+        $c1 = $this->getDataGenerator()->create_course(array('idnumber' => 'Taken'));
+        $c2 = $this->getDataGenerator()->create_course();
+
+        // Create with existing ID number.
+        $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
+        $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
+        $data = array('shortname' => 'c2', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '1',
+            'idnumber' => $c1->idnumber);
+        $co = new tool_uploadcourse_course($mode, $updatemode, $data);
+        $this->assertFalse($co->prepare());
+        $this->assertArrayHasKey('idnumberalreadyinuse', $co->get_errors());
+
+        // Rename to existing ID number.
+        $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
+        $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
+        $data = array('shortname' => $c2->shortname, 'rename' => 'SN', 'idnumber' => $c1->idnumber);
+        $importoptions = array('canrename' => true);
+        $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
+        $this->assertFalse($co->prepare());
+        $this->assertArrayHasKey('cannotrenameidnumberconflict', $co->get_errors());
+    }
+
 }