/**
  * Test student save works when $USER object not set
  */
 public function test_student_save_nouserobject()
 {
     global $DB, $USER;
     // Create Moodle course category.
     $crscat = create_course_category((object) array('name' => 'Test Course category', 'idnumber' => 'MCC-1'));
     // Create Moodle course.
     $crsdata = array('category' => $crscat->id, 'fullname' => 'MC-TEST-ELIS-8484', 'shortname' => 'MC-TEST-ELIS-8484', 'idnumber' => 'MC-TEST-ELIS-8484');
     $mdlcrs = new stdClass();
     $mdlcrs->id = $DB->insert_record('course', (object) $crsdata);
     $cddata = array('name' => 'CD-ELIS-8484', 'code' => 'CD-ELIS-8484', 'idnumber' => 'CD-ELIS-8484', 'syllabus' => 'syllabus');
     $cd = new course($cddata);
     $cd->save();
     $ci = new pmclass(array('idnumber' => 'CI-ELIS-8484', 'courseid' => $cd->id, 'moodlecourseid' => $mdlcrs->id, 'autocreate' => 0));
     $ci->save();
     $testuser = new user(array('idnumber' => 'testuserelis8484', 'username' => 'testuserelis8484', 'firstname' => 'Test', 'lastname' => 'User-ELIS8484', 'email' => '*****@*****.**', 'city' => 'Waterloo', 'country' => 'CA'));
     $testuser->save();
     $USER = null;
     $sturec = new stdClass();
     $sturec->userid = $testuser->id;
     $sturec->classid = $ci->id;
     $sturec->grade = 0;
     $sturec->enrolmenttime = time();
     $student = new student($sturec);
     $student->save();
     $this->assertFalse(empty($student));
     if (!empty($student)) {
         $this->assertFalse(empty($student->id));
     }
 }
示例#2
0
 /**
  * Create categories
  *
  * @param array $categories - see create_categories_parameters() for the array structure
  * @return array - see create_categories_returns() for the array structure
  * @since Moodle 2.3
  */
 public static function create_categories($categories)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . "/course/lib.php";
     $params = self::validate_parameters(self::create_categories_parameters(), array('categories' => $categories));
     $transaction = $DB->start_delegated_transaction();
     $createdcategories = array();
     foreach ($params['categories'] as $category) {
         if ($category['parent']) {
             if (!$DB->record_exists('course_categories', array('id' => $category['parent']))) {
                 throw new moodle_exception('unknowcategory');
             }
             $context = context_coursecat::instance($category['parent']);
         } else {
             $context = context_system::instance();
         }
         self::validate_context($context);
         require_capability('moodle/category:manage', $context);
         // Check name.
         if (textlib::strlen($category['name']) > 255) {
             throw new moodle_exception('categorytoolong');
         }
         $newcategory = new stdClass();
         $newcategory->name = $category['name'];
         $newcategory->parent = $category['parent'];
         $newcategory->sortorder = 999;
         // Same as in the course/editcategory.php .
         // Format the description.
         if (!empty($category['description'])) {
             $newcategory->description = $category['description'];
         }
         $newcategory->descriptionformat = external_validate_format($category['descriptionformat']);
         if (isset($category['theme']) and !empty($CFG->allowcategorythemes)) {
             $newcategory->theme = $category['theme'];
         }
         // Check id number.
         if (!empty($category['idnumber'])) {
             // Same as in course/editcategory_form.php .
             if (textlib::strlen($category['idnumber']) > 100) {
                 throw new moodle_exception('idnumbertoolong');
             }
             if ($existing = $DB->get_record('course_categories', array('idnumber' => $category['idnumber']))) {
                 if ($existing->id) {
                     throw new moodle_exception('idnumbertaken');
                 }
             }
             $newcategory->idnumber = $category['idnumber'];
         }
         $newcategory = create_course_category($newcategory);
         // Populate special fields.
         fix_course_sortorder();
         $createdcategories[] = array('id' => $newcategory->id, 'name' => $newcategory->name);
     }
     $transaction->allow_commit();
     return $createdcategories;
 }
示例#3
0
 public function test_create_course_category()
 {
     global $CFG, $DB;
     $this->resetAfterTest(true);
     // Create the category
     $data = new stdClass();
     $data->name = 'aaa';
     $data->description = 'aaa';
     $data->idnumber = '';
     $category1 = create_course_category($data);
     // Initially confirm that base data was inserted correctly
     $this->assertEquals($data->name, $category1->name);
     $this->assertEquals($data->description, $category1->description);
     $this->assertEquals($data->idnumber, $category1->idnumber);
     // sortorder should be blank initially
     $this->assertEmpty($category1->sortorder);
     // Calling fix_course_sortorder() should provide a new sortorder
     fix_course_sortorder();
     $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
     $this->assertGreaterThanOrEqual(1, $category1->sortorder);
     // Create two more categories and test the sortorder worked correctly
     $data->name = 'ccc';
     $category2 = create_course_category($data);
     $this->assertEmpty($category2->sortorder);
     $data->name = 'bbb';
     $category3 = create_course_category($data);
     $this->assertEmpty($category3->sortorder);
     // Calling fix_course_sortorder() should provide a new sortorder to give category1,
     // category2, category3. New course categories are ordered by id not name
     fix_course_sortorder();
     $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
     $category2 = $DB->get_record('course_categories', array('id' => $category2->id));
     $category3 = $DB->get_record('course_categories', array('id' => $category3->id));
     $this->assertGreaterThanOrEqual($category1->sortorder, $category2->sortorder);
     $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder);
     $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder);
 }
            $newcategory->theme = $data->theme;
        }
        if ($id) {
            // Update an existing category.
            $newcategory->id = $category->id;
            if ($newcategory->parent != $category->parent) {
                // check category manage capability if parent changed
                require_capability('moodle/category:manage', get_category_or_system_context((int) $newcategory->parent));
                $parent_cat = $DB->get_record('course_categories', array('id' => $newcategory->parent));
                move_category($newcategory, $parent_cat);
            }
        } else {
            // Create a new category.
            $newcategory->description = $data->description_editor['text'];
            // Don't overwrite the $newcategory object as it'll be processed by file_postupdate_standard_editor in a moment
            $category = create_course_category($newcategory);
            $newcategory->id = $category->id;
            $categorycontext = $category->context;
            redirect($CFG->wwwroot . '/course/index.php');
            if ($data->batch == 1) {
                $batch = new stdClass();
                $batch->id = '';
                $batch->categoryid = $newcategory->id;
                $batch->startdate = $data->batchstart;
                $batch->enddate = $data->batchend;
                $batchObj = new batch();
                $batchObj->addnewbatch($batch);
            }
        }
        ?>
示例#5
0
 protected function create_category($category)
 {
     global $CFG;
     require_once $CFG->dirroot . '/course/lib.php';
     return create_course_category($category);
 }