/** * 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)); } }
function test_find() { $name = "Math"; $course_number = "112"; $test_course = new course($name, $course_number); $test_course->save(); $name2 = "The Same Exact Name"; $course_number2 = "112"; $test_course2 = new course($name2, $course_number2); $test_course2->save(); $result = course::find($test_course->getId()); $this->assertEquals($test_course, $result); }
/** * Test block_elisadmin_load_menu_children_course function. */ public function test_block_elisadmin_load_menu_children_course() { global $DB, $USER; accesslib_clear_all_caches(true); // Create test user - ensure the returned user is NOT a site admin. if they are, our capability restrictions won't work. $testuser = new user(); $testuser->username = '******'; $testuser->idnumber = 'testELIS4093'; $testuser->firstname = 'testELIS4093'; $testuser->lastname = 'testELIS4093'; $testuser->email = '*****@*****.**'; $testuser->country = 'CA'; $testuser->save(); $testmuser = $testuser->get_moodleuser(); // Create role with cap: 'local/elisprogram:class_view'. $testrole = new stdClass(); $testrole->name = 'ELIS Class View'; $testrole->shortname = '_test_ELIS_4093'; $testrole->description = 'ELIS Class View'; $testrole->archetype = ''; $testrole->id = create_role($testrole->name, $testrole->shortname, $testrole->description, $testrole->archetype); // Ensure our new role is assignable to ELIS class contexts. set_role_contextlevels($testrole->id, array(CONTEXT_ELIS_CLASS)); // Ensure the role has our required capability assigned. $sitecontext = context_system::instance(); assign_capability('local/elisprogram:class_view', CAP_ALLOW, $testrole->id, $sitecontext->id, true); $sitecontext->mark_dirty(); // Create ELIS Course Description. $testcrs = new course(array('name' => 'CD-ELIS-4093', 'idnumber' => 'CDELIS4093', 'syllabus' => '')); $testcrs->save(); $testcrs->load(); // Create three(3) Class Instances for Course Descrption. $testcls1 = new pmclass(array('courseid' => $testcrs->id, 'idnumber' => 'CI_ELIS_4093.1')); $testcls1->save(); $testcls1->load(); $testcls2 = new pmclass(array('courseid' => $testcrs->id, 'idnumber' => 'CI_ELIS_4093.2')); $testcls2->save(); $testcls2->load(); $testcls3 = new pmclass(array('courseid' => $testcrs->id, 'idnumber' => 'CI_ELIS_4093.3')); $testcls3->save(); $testcls3->load(); // Assign testuser new role in one Class Instance. $context = \local_elisprogram\context\pmclass::instance($testcls2->id); role_assign($testrole->id, $testmuser->id, $context->id); // Switch to testuser. $USER = $testmuser; $this->setUser($testmuser); $items = block_elisadmin_load_menu_children_course($testcrs->id, 0, 0, 5, ''); $this->assertEquals(1, count($items)); $this->assertTrue($items[0]->name == 'pmclass_2'); }
/** * Validate that appropriate fields are synched over to Moodle when PM user is enrolled in a class instance during an import. */ public function test_user_sync_on_pm_user_create() { global $CFG, $DB; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/classmoodlecourse.class.php'); require_once elispm::lib('data/course.class.php'); require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/user.class.php'); // Configure the elis enrolment plugin. $roleid = $DB->get_field('role', 'id', array(), IGNORE_MULTIPLE); set_config('roleid', $roleid, 'enrol_elis'); $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); $class = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber')); $class->save(); $category = new stdClass(); $category->name = 'testcategoryname'; $category->id = $DB->insert_record('course_categories', $category); // Create the associated context. context_coursecat::instance($category->id); $mdlcourse = new stdClass(); $mdlcourse->category = $category->id; $mdlcourse->fullname = 'testcoursefullname'; $mdlcourse = create_course($mdlcourse); // Associate class instance to Moodle course. $classmoodlecourse = new classmoodlecourse(array('classid' => $class->id, 'moodlecourseid' => $mdlcourse->id)); $classmoodlecourse->save(); // Run the enrolment create action. $record = new stdClass(); $record->context = 'class_testclassidnumber'; $record->user_username = '******'; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->class_enrolment_create($record, 'bogus', 'testclassidnumber'); // Validate the enrolment. $enrolid = $DB->get_field('enrol', 'id', array('enrol' => 'elis', 'courseid' => $mdlcourse->id)); $this->assertNotEquals(false, $enrolid); $mdluserid = $DB->get_field('user', 'id', array('username' => 'testuserusername')); $this->assertNotEquals(false, $mdluserid); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $enrolid, 'userid' => $mdluserid))); // Validate the role assignment. $mdlcoursecontext = context_course::instance($mdlcourse->id); $this->assertTrue($DB->record_exists('role_assignments', array('roleid' => $roleid, 'contextid' => $mdlcoursecontext->id, 'userid' => $mdluserid))); }
/** * Validate that the provided custom field type and value produce the * specified error message on course description update * * @param string $uitype The input control / UI type * @param string $value The value to use for the custom field * @param string $message The expected error message * @param array $otherparams Other parameters to give to the field owner * @dataProvider type_error_provider */ public function test_course_update_customfield_message($uitype, $value, $message, $otherparams) { global $CFG; require_once $CFG->dirroot . '/local/elisprogram/accesslib.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; $this->create_custom_field(CONTEXT_ELIS_COURSE, $uitype, $otherparams); // Create mapping record. $this->create_mapping_record('course', 'testfieldshortname', 'customtestfieldshortname'); $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); $data = array('action' => 'update', 'context' => 'course', 'idnumber' => 'testcourseidnumber', 'customtestfieldshortname' => $value); $message = '[course.csv line 2] Course description with idnumber "testcourseidnumber" could not be updated. ' . $message . "\n"; $this->assert_data_produces_error($data, $message, 'course'); }
/** * Validate that enrolling a user into a user set via IP auto-enrolls them in * an associated track, and any associated programs or class instances */ public function test_userset_enrolment_creates_track_enrolment() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/clustertrack.class.php'); require_once elispm::lib('data/course.class.php'); require_once elispm::lib('data/curriculum.class.php'); require_once elispm::lib('data/curriculumcourse.class.php'); require_once elispm::lib('data/curriculumstudent.class.php'); require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/student.class.php'); require_once elispm::lib('data/track.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/userset.class.php'); require_once elispm::lib('data/usertrack.class.php'); // Make sure no emails are sent. set_config('noemailever', true); // Set up data. // Test user. $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); // Test user set. $userset = new userset(array('name' => 'testusersetname')); $userset->save(); // Test program and track. $program = new curriculum(array('idnumber' => 'testprogramidnumber')); $program->save(); $track = new track(array('curid' => $program->id, 'idnumber' => 'testtrackidnumber')); $track->save(); // Associate the userset to the track. $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => $track->id, 'autoenrol' => 1)); $clustertrack->save(); // Test course and class. $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); $class = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclass1idnumber')); $class->save(); // Associate course to the program. $curriculumcourse = new curriculumcourse(array('curriculumid' => $program->id, 'courseid' => $course->id)); $curriculumcourse->save(); // Associate track to the test class. $trackassignment = new trackassignment(array('trackid' => $track->id, 'classid' => $class->id, 'autoenrol' => 1)); $trackassignment->save(); // Run the assignment create action. $record = new stdClass(); $record->context = 'userset_testusersetname'; $record->user_username = '******'; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->cluster_enrolment_create($record, 'bogus', 'testusersetname'); // Validation. // Userset assignment should trigger track assignment. $this->assertTrue($DB->record_exists(usertrack::TABLE, array('userid' => $user->id, 'trackid' => $track->id))); // Track assignment should trigger program assignment. $this->assertTrue($DB->record_exists(curriculumstudent::TABLE, array('userid' => $user->id, 'curriculumid' => $program->id))); // Track assignment should create a class enrolment. $this->assertTrue($DB->record_exists(student::TABLE, array('userid' => $user->id, 'classid' => $class->id))); }
/** * Validate that an appropriate error is logged when max field lengths are * exceeded during a class instance create action * * @param string $field The identifier for the field we are testing * @param int $length The length we are testing at * @param string $customvalue A custom value to use rather than simply repeating a character, * or null if not applicable * @dataProvider classcreatefieldprovider */ public function test_classinstancecreatelogserrorwhenfieldstoolong($field, $length, $customvalue) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/pmclass.class.php'; $course = new course(array('idnumber' => 'testcourseidnumber', 'name' => 'testcoursename', 'syllabus' => '')); $course->save(); $data = array('action' => 'create', 'context' => 'class', 'assignment' => 'testcourseidnumber', 'idnumber' => 'testclassidnumber'); if ($customvalue !== null) { $value = $customvalue; } else { $value = str_repeat('a', $length); } $data[$field] = $value; $maxlength = $length - 1; $expectederror = "{$field} value of \"{$value}\" exceeds the maximum field length of {$maxlength}.\n"; $this->assert_data_produces_error($data, $expectederror, 'course'); }
/** * Test failure conditions. * @dataProvider dataprovider_failure * @expectedException moodle_exception * @param array $user The incoming user data. */ public function test_failure(array $user, $giveperms = true) { global $DB; if ($giveperms === true) { $this->give_permissions(array('local/elisprogram:class_create')); } $course = new course(); $course->idnumber = 'testcourse1'; $course->name = 'Test Course 1'; $course->syllabus = 'Test'; $course->save(); // Create a class (used for duplicate test). $class = new pmclass(); $class->idnumber = 'testclass1'; $class->courseid = $course->id; $class->save(); $response = local_datahub_elis_class_create::class_create($user); }
/** * Validate that track-class associations can be created during a class instance * update action * * @param mixed $autoenrol The appropriate autoenrol value specified * @param int $dbautoenrol The value expected to be set in the db for autoenrol * @dataProvider autoenrol_provider */ public function test_associate_track_during_class_update($autoenrol, $dbautoenrol) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/course.class.php'); require_once elispm::lib('data/curriculum.class.php'); require_once elispm::lib('data/curriculumcourse.class.php'); require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/track.class.php'); // Create the course description. $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); // Create the class instance. $pmclass = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber')); $pmclass->save(); // Create the curriculum / program. $curriculum = new curriculum(array('idnumber' => 'testcurriculumidnumber')); $curriculum->save(); // Associate the course description to the program. $curriculumcourse = new curriculumcourse(array('curriculumid' => $curriculum->id, 'courseid' => $course->id)); $curriculumcourse->save(); // Create the track. $track = new track(array('curid' => $curriculum->id, 'idnumber' => 'testtrackidnumber')); $track->save(); // Run the class instance update action. $record = new stdClass(); $record->assignment = 'testcourseidnumber'; $record->idnumber = 'testclassidnumber'; $record->track = 'testtrackidnumber'; if ($autoenrol !== null) { $record->autoenrol = $autoenrol; } $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->class_update($record, 'bogus'); // Validation. $classid = $DB->get_field(pmclass::TABLE, 'id', array('idnumber' => 'testclassidnumber')); $this->assertTrue($DB->record_exists(trackassignment::TABLE, array('trackid' => $track->id, 'classid' => $classid, 'autoenrol' => $dbautoenrol))); }
/** * Performs course update * @throws moodle_exception If there was an error in passed parameters. * @throws data_object_exception If there was an error updating the entity. * @param array $data The incoming data parameter. * @return array An array of parameters, if successful. */ public static function course_update(array $data) { global $USER, $DB; if (static::require_elis_dependencies() !== true) { throw new moodle_exception('ws_function_requires_elis', 'local_datahub'); } // Parameter validation. $params = self::validate_parameters(self::course_update_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Validate course exists if (!($crsid = $DB->get_field(course::TABLE, 'id', array('idnumber' => $data->idnumber)))) { throw new data_object_exception('ws_course_update_fail_invalid_idnumber', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:course_edit', \local_elisprogram\context\course::instance($crsid)); // Initialize version1elis importplugin for utility functions. $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); // Validate credits. if (isset($data->credits) && !(is_numeric($data->credits) && $data->credits >= 0)) { throw new data_object_exception('ws_course_update_fail_invalid_credits', 'local_datahub', '', $data); } // Validate completion grade. if (isset($data->completion_grade) && !(is_numeric($data->completion_grade) && $data->completion_grade >= 0 && $data->completion_grade <= 100)) { throw new data_object_exception('ws_course_update_fail_invalid_completion_grade', 'local_datahub', '', $data); } // Handle assignment to program. if (isset($data->assignment) && !empty($data->assignment)) { $programid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->assignment)); if ($programid) { $curriculumcourseid = $DB->get_field(curriculumcourse::TABLE, 'id', array('curriculumid' => $programid, 'courseid' => $crsid)); // Only assign if it is not already assigned if (!$curriculumcourseid) { $data->curriculum = array($programid); } } else { throw new data_object_exception('ws_course_update_fail_invalid_assignment', 'local_datahub', '', $data); } } $course = new course($crsid); $course->load(); $course->set_from_data($data); $course->save(); // Handle linking to Moodle course. if (isset($data->link) && !empty($data->link)) { $moodlecourseid = $DB->get_field('course', 'id', array('shortname' => $data->link)); if ($moodlecourseid) { $importplugin->associate_course_to_moodle_course($data, $course->id); } else { throw new data_object_exception('ws_course_update_fail_invalid_link', 'local_datahub', '', $data); } } // Respond. if (!empty($course->id)) { $courserec = (array) $DB->get_record(course::TABLE, array('id' => $course->id)); $courseobj = $course->to_array(); // convert multi-valued custom field arrays to comma-separated listing $fields = self::get_course_custom_fields(); foreach ($fields as $field) { // Generate name using custom field prefix. $fullfieldname = data_object_with_custom_fields::CUSTOM_FIELD_PREFIX . $field->shortname; if ($field->multivalued && isset($courseobj[$fullfieldname]) && is_array($courseobj[$fullfieldname])) { $courseobj[$fullfieldname] = implode(',', $courseobj[$fullfieldname]); } } return array('messagecode' => get_string('ws_course_update_success_code', 'local_datahub'), 'message' => get_string('ws_course_update_success_msg', 'local_datahub'), 'record' => array_merge($courserec, $courseobj)); } else { throw new data_object_exception('ws_course_update_fail', 'local_datahub'); } }
/** * Test sync_completionelements method. * * @dataProvider dataprovider_sync_completionelements * @param array $gradeitem Array of parameters to create grade_item. * @param array $gradegrade Array of parameters to create grade_grade. * @param array $coursecompletion Array of parameters to create coursecompletion. * @param array $studentgrade Array of parameters to create student_grade. * @param int $timenow Current timestamp to enable testing with time. * @param array $expectedstudentgrade Array of parameters we expect to be set in the student_grade. */ public function test_sync_completionelements($gradeitem, $gradegrade, $coursecompletion, $studentgrade, $timenow, $expectedstudentgrade) { global $DB; $sync = new \local_elisprogram\moodle\synchronize(); // Test data setup. $crs = new \course(array('idnumber' => 'CRS1', 'name' => 'Course 1', 'syllabus' => '')); $crs->save(); $cls = new \pmclass(array('courseid' => $crs->id, 'idnumber' => 'CLS1')); $cls->save(); $usr = new \user(array('username' => 'test1', 'idnumber' => 'test2', 'firstname' => 'test', 'lastname' => 'user', 'email' => '*****@*****.**', 'country' => 'CA')); $usr->save(); $musr = $this->getDataGenerator()->create_user(); $gradeitem = new \grade_item($gradeitem, false); $gradeitem->insert(); $gradegrade['itemid'] = $gradeitem->id; $gradegrade['userid'] = $musr->id; $gradegrade = new \grade_grade($gradegrade, false); $gradegrade->insert(); $coursecompletion['courseid'] = $crs->id; $coursecompletion = new \coursecompletion($coursecompletion); $coursecompletion->save(); if ($studentgrade !== false) { $studentgrade['classid'] = $cls->id; $studentgrade['userid'] = $usr->id; $studentgrade['completionid'] = $coursecompletion->id; $studentgrade = new \student_grade($studentgrade); $studentgrade->save(); $studentgrade = new \student_grade($studentgrade->id); $studentgrade->load(); } // Method parameter setup. $causer = (object) array('cmid' => $usr->id, 'pmclassid' => $cls->id); $gis = array($gradeitem->id => (object) array('id' => $gradeitem->id, 'grademax' => $gradeitem->grademax)); $compelements = array($gradeitem->id => (object) array('id' => $coursecompletion->id, 'completion_grade' => $coursecompletion->completion_grade)); $moodlegrades = array($gradeitem->id => $gradegrade); if ($studentgrade !== false) { $cmgrades = array($coursecompletion->id => $studentgrade->to_object()); } else { $cmgrades = array(); } $sync->sync_completionelements($causer, $gis, $compelements, $moodlegrades, $cmgrades, $timenow); $actualstudentgrade = false; if ($studentgrade !== false) { $actualstudentgrade = $DB->get_record(\student_grade::TABLE, array('id' => $studentgrade->id)); } else { $actualstudentgrades = $DB->get_records(\student_grade::TABLE, array(), 'id DESC'); if (!empty($actualstudentgrades)) { $actualstudentgrade = array_shift($actualstudentgrades); } } if ($actualstudentgrade !== false) { if ($expectedstudentgrade !== false) { $expectedstudentgrade['id'] = $actualstudentgrade->id; $expectedstudentgrade['classid'] = $cls->id; $expectedstudentgrade['userid'] = $usr->id; $expectedstudentgrade['completionid'] = $coursecompletion->id; // This is here for tests where we can't reliably predetermine timemodified (i.e. no-sync cases). if (!isset($expectedstudentgrade['timemodified'])) { $expectedstudentgrade['timemodified'] = $actualstudentgrade->timemodified; } $expectedstudentgrade = (object) $expectedstudentgrade; $this->assertEquals($expectedstudentgrade, $actualstudentgrade); } else { $this->assertTrue(false, 'A student_grade was created when one was not expected.'); } } else { // If $expectedstudentgrade is false we were expected no grade to be created. If not, we have a problem. if ($expectedstudentgrade !== false) { $this->assertTrue(false, 'No student_grade created when one was expected'); } else { $this->assertTrue(true); } } }
/** * Validate that createorupdate still works when the class idnumber contains * an underscore */ public function testeliscreateorupdatesupportsunderscores() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/course.class.php'); require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/student.class.php'); require_once elispm::lib('data/user.class.php'); // Set up initial conditions. set_config('createorupdate', 1, 'dhimport_version1elis'); // Create the test course. $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); // Create the test class. $class = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclass_idnumber')); $class->save(); // Create the test user. $user = new user(array('username' => 'testuserusername', 'email' => '*****@*****.**', 'idnumber' => 'testuseridnumber', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'country' => 'CA')); $user->save(); // Set up an existing enrolment. $student = new student(array('userid' => $user->id, 'classid' => $class->id, 'completestatusid' => student::STUSTATUS_FAILED)); $student->save(); // Import the record, with create acting as an update. $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $record = new stdClass(); $record->action = 'create'; $record->context = 'class_testclass_idnumber'; $record->user_idnumber = 'testuseridnumber'; $record->completestatusid = student::STUSTATUS_PASSED; $importplugin->process_record('enrolment', $record, 'bogus'); // Validation. $this->assertEquals(1, $DB->count_records(student::TABLE)); $params = array('userid' => $user->id, 'classid' => $class->id, 'completestatusid' => student::STUSTATUS_PASSED); $exists = $DB->record_exists(student::TABLE, $params); $this->assertTrue($exists); }
/** * Validate that credits are correctly transferred from course to enrolment * for a specific user * * @param array $enrolments Enrolment records to create * @param array $expectedenrolments Records to validate * @param array $classids The ids of the classes we should run the method for * @dataProvider dataprovider_credits */ public function test_enrolmentupdatesetscreditsforspecificuserid($enrolments, $expectedenrolments, $classids) { global $DB; $this->load_csv_data(true); // Set up a second course and class. $course = new course(array('name' => 'secondcourse', 'idnumber' => 'secondcourse', 'syllabus' => '', 'credits' => 1)); $course->save(); $pmclass = new pmclass(array('courseid' => $course->id, 'idnumber' => 'secondclass')); $pmclass->save(); $this->save_enrolments($enrolments); $pmuserid = 103; foreach ($classids as $classid) { $pmclass = new pmclass($classid); $sink = $this->redirectMessages(); $pmclass->update_enrolment_status($pmuserid); } $expectedenrolments = $this->filter_by_userid($expectedenrolments, $pmuserid); $this->validate_expected_enrolments($expectedenrolments, $pmuserid); }
/** * Validate that a success message is logged on enrolment delete */ public function testenrolmentdeletelogssuccessmessage() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/pmclass.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/student.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/user.class.php'; $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); $pmclass = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber')); $pmclass->save(); $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); $student = new student(array('classid' => $pmclass->id, 'userid' => $user->id)); $student->save(); $record = new stdClass(); // TODO: consider using fewer fields. $record->context = 'class_testclassidnumber'; $record->user_idnumber = 'testuseridnumber'; $record->user_username = '******'; $record->user_email = '*****@*****.**'; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->dblogger = new rlip_dblogger_import(false); $importplugin->fslogger = new silent_fslogger(null); $importplugin->class_enrolment_delete_student($record, 'bogus', 'testclassidnumber'); $importplugin->dblogger->flush('bogus'); $expectedmessage = 'All lines from import file bogus were successfully processed.'; $where = 'statusmessage = ?'; $exists = $DB->record_exists_select(RLIP_LOG_TABLE, $where, array($expectedmessage)); $this->assertTrue($exists); }
/** * Create an ELIS course. * @param field &$field A custom field to set when creating the course. * @return course The created course. */ public function create_course(field &$field) { $data = new stdClass(); $data->name = 'Test Course'; $data->code = ''; $data->idnumber = 'CRS1'; $data->syllabus = ''; $data->lengthdescription = ''; $data->length = 0; $data->credits = ''; $data->completion_grade = '0'; $data->cost = ''; $data->version = ''; $data->templateclass = 'moodlecourseurl'; $data->locationlabel = ''; $data->location = ''; $data->temptype = ''; $fieldvar = 'field_' . $field->shortname; $data->{$fieldvar} = 'test field data'; $crs = new course(); $crs->set_from_data($data); $crs->save(); return $crs; }
/** * Validate that a Moodle user with duplicate username does not produce an error when enrolling an ELIS user to a class. */ public function test_elisuserstudentenrolmenthandlesduplicatemoodleuser() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/course.class.php'); require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/user.class.php'); // Create an ELIS course. $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); // Create an ELIS class. $class = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber')); $class->save(); // Create a Moodle course. $mdlcoursedata = new stdClass(); $mdlcoursedata->shortname = "testcourse"; $mdlcoursedata->fullname = "Test Course"; $mdlcoursedata->category = 1; $mdlcourse = create_course($mdlcoursedata); // Associate ELIS class to Moodle course. $result = moodle_attach_class($class->id, $mdlcourse->id); $this->assertTrue($result); // Create an ELIS user. $user = new user(array('idnumber' => 'testidnumber', 'username' => 'testusername', 'firstname' => 'testfirstname', 'lastname' => 'testlasname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); // Change the idnumber of the associated Moodle user record. $DB->execute("UPDATE {user} SET idnumber = 'bogus' WHERE idnumber = 'testidnumber'"); // Remove the ELIS user to Moodle user association record. $DB->execute("DELETE FROM {" . usermoodle::TABLE . "} WHERE cuserid = " . $user->id); // Run the class enrolment create action. $record = new stdClass(); $record->action = 'enrol'; $record->context = 'class_testclassidnumber'; $record->user_username = $user->username; $record->user_email = $user->email; $record->user_idnumber = $user->idnumber; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('enrolment', (object) $record, 'bogus'); // Validation. $this->assertTrue($DB->record_exists(student::TABLE, array('userid' => $user->id, 'classid' => $class->id))); }
/** * Clone a course. * @param array $options options for cloning. Valid options are: * - 'classes': whether or not to clone classes (default: false) * - 'moodlecourses': whether or not to clone Moodle courses (if they were * autocreated). Values can be (default: "copyalways"): * - "copyalways": always copy course * - "copyautocreated": only copy autocreated courses * - "autocreatenew": autocreate new courses from course template * - "link": link to existing course * - 'targetcluster': the cluster id or cluster object (if any) to * associate the clones with (default: none) * @return array array of array of object IDs created. Key in outer array * is type of object (plural). Key in inner array is original object ID, * value is new object ID. Outer array also has an entry called 'errors', * which is an array of any errors encountered when duplicating the * object. */ function duplicate(array $options) { require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/coursetemplate.class.php'); $objs = array('errors' => array()); if (isset($options['targetcluster'])) { $userset = $options['targetcluster']; if (!is_object($userset) || !is_a($userset, 'userset')) { $options['targetcluster'] = $userset = new userset($userset); } } // Due to lazy loading, we need to pre-load this object $this->load(); // clone main course object $clone = new course($this); unset($clone->id); $idnumber = $clone->idnumber; $name = $clone->name; if (isset($userset)) { $to_append = ' - ' . $userset->name; // if cluster specified, append cluster's name to course $idnumber = append_once($idnumber, $to_append, array('maxlength' => 95)); $name = append_once($name, $to_append, array('maxlength' => 250)); } //get a unique idnumber $clone->idnumber = generate_unique_identifier(course::TABLE, 'idnumber', $idnumber, array('idnumber' => $idnumber)); if ($clone->idnumber != $idnumber) { //get the suffix appended and add it to the name $parts = explode('.', $clone->idnumber); $suffix = end($parts); $clone->name = $name . '.' . $suffix; } else { $clone->name = $name; } $clone->save(); $objs['courses'] = array($this->id => $clone->id); $options['targetcourse'] = $clone->id; // copy completion elements $compelems = $this->get_completion_elements(); foreach ($compelems as $compelem) { unset($compelem->id); $clone->save_completion_element($compelem); } unset($compelems); // copy template $template = $this->_db->get_record(coursetemplate::TABLE, array('courseid' => $this->id)); $template = new coursetemplate($template); unset($template->id); $template->courseid = $clone->id; $template->save(); // copy the classes if (!empty($options['classes'])) { $classes = pmclass_get_record_by_courseid($this->id); if (!empty($classes)) { $objs['classes'] = array(); foreach ($classes as $class) { $class = new pmclass($class); $rv = $class->duplicate($options); if (isset($rv['errors']) && !empty($rv['errors'])) { $objs['errors'] = array_merge($objs['errors'], $rv['errors']); } if (isset($rv['classes'])) { $objs['classes'] = $objs['classes'] + $rv['classes']; } } } } return $objs; }
/** * Update a course * @todo: consider factoring this some more once other actions exist * * @param object $record One record of import data * @param string $filename The import file name, used for logging * @return boolean true on success, otherwise false */ function course_update($record, $filename) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; $message = ""; //field length checking $lengthcheck = $this->check_course_field_lengths($record, $filename); if (!$lengthcheck) { return false; } if (isset($record->idnumber)) { if (!($crsid = $DB->get_field(course::TABLE, 'id', array('idnumber' => $record->idnumber)))) { $identifier = $this->get_field_mapping('idnumber'); $this->fslogger->log_failure("{$identifier} value of \"{$record->idnumber}\" does not refer to a valid course description.", 0, $filename, $this->linenumber, $record, "course"); return false; } } $currid = 0; if (isset($record->assignment)) { if (!($currid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $record->assignment)))) { $identifier = $this->get_field_mapping('assignment'); $this->fslogger->log_failure("{$identifier} value of \"{$record->assignment}\" does not refer to a valid program.", 0, $filename, $this->linenumber, $record, "course"); return false; } else { if ($DB->record_exists(curriculumcourse::TABLE, array('curriculumid' => $currid, 'courseid' => $crsid))) { $identifier = $this->get_field_mapping('idnumber'); $message = "Course description with {$identifier} \"{$record->idnumber}\" already assigned to program with idnumber \"{$record->assignment}\"."; } } } if (!$this->validate_course_data('update', $record, $filename)) { return false; } $record = $this->add_custom_field_prefixes($record); //custom field validation if (!$this->validate_custom_field_data('update', $record, $filename, 'course')) { return false; } $record = $this->initialize_course_fields($record); $record->id = $crsid; $course = new course(); $course->set_from_data($record); $course->save(); if ($currid != 0) { $currcrs = new curriculumcourse(); $assoc = new stdClass(); $assoc->curriculumid = $currid; $assoc->courseid = $course->id; $currcrs->set_from_data($assoc); $currcrs->save(); } //associate this course description to a Moodle course, if necessary $this->associate_course_to_moodle_course($record, $course->id); //log success $success_message = "Course description with idnumber \"{$record->idnumber}\" successfully updated."; $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber); return true; }
/** * Test main newenrolmentemail() function. */ public function test_version1importnewenrolmentemail() { global $CFG, $DB; // This is needed by the required files. require_once dirname(__FILE__) . '/other/rlip_importplugin_version1elis_fakeemail.php'; $importplugin = new rlip_importplugin_version1elis_fakeemail(); // Create Moodle course. $course = $this->getDataGenerator()->create_course(); // Enrol some students into Moodle course. $user2 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user2->id, $course->id); $user3 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user3->id, $course->id); // Enrol teachers into Moodle course. $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); $teacher = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); $teacher2 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($teacher2->id, $course->id, $teacherrole->id); // Create ELIS class and ELIS user. $ecourse = new course(array('name' => 'Test Course', 'idnumber' => 'CRS100', 'syllabus' => '')); $ecourse->save(); $eclass = new pmclass(array('idnumber' => 'CLS100', 'courseid' => $ecourse->id)); $eclass->save(); $euser = new user(array('username' => 'testuser', 'idnumber' => 'testuser', 'firstname' => 'Test', 'lastname' => 'User', 'email' => '*****@*****.**', 'city' => 'Waterloo', 'country' => 'CA')); $euser->save(); $muser = $euser->get_moodleuser(); // Create student object for elis user/elis class. $student = new student(array('userid' => $euser->id, 'classid' => $eclass->id)); // Test false return when student "no_moodle_enrol" is set. $student->no_moodle_enrol = true; $result = $importplugin->newenrolmentemail($student); $this->assertFalse($result); $student->no_moodle_enrol = false; // Test false return when ELIS class is not linked to Moodle course. $DB->delete_records(classmoodlecourse::TABLE, array('classid' => $eclass->id, 'moodlecourseid' => $course->id)); $result = $importplugin->newenrolmentemail($student); $this->assertFalse($result); // Test false return when ELIS class is linked to a Moodle course, but Moodle course does not exist. $cmcid = $DB->insert_record(classmoodlecourse::TABLE, array('classid' => $eclass->id, 'moodlecourseid' => 999999999)); $result = $importplugin->newenrolmentemail($student); $this->assertFalse($result); $DB->update_record(classmoodlecourse::TABLE, array('id' => $cmcid, 'moodlecourseid' => $course->id)); // Test false return when ELIS user is not linked to Moodle user. $DB->delete_records(usermoodle::TABLE, array('cuserid' => $euser->id, 'muserid' => $muser->id)); $result = $importplugin->newenrolmentemail($student); $this->assertFalse($result); // Test false return when ELIS user is linked to Moodle user, but Moodle user does not exist. $usermoodleid = $DB->insert_record(usermoodle::TABLE, array('cuserid' => $euser->id, 'muserid' => 99999999)); $result = $importplugin->newenrolmentemail($student); $this->assertFalse($result); $DB->update_record(usermoodle::TABLE, array('id' => $usermoodleid, 'muserid' => $muser->id)); // Test false return when not enabled. set_config('newenrolmentemailenabled', '0', 'dhimport_version1elis'); set_config('newenrolmentemailsubject', 'Test Subject', 'dhimport_version1elis'); set_config('newenrolmentemailtemplate', 'Test Body', 'dhimport_version1elis'); set_config('newenrolmentemailfrom', 'teacher', 'dhimport_version1elis'); $result = $importplugin->newenrolmentemail($student); $this->assertFalse($result); // Test false return when enabled but empty template. set_config('newenrolmentemailenabled', '1', 'dhimport_version1elis'); set_config('newenrolmentemailsubject', 'Test Subject', 'dhimport_version1elis'); set_config('newenrolmentemailtemplate', '', 'dhimport_version1elis'); set_config('newenrolmentemailfrom', 'teacher', 'dhimport_version1elis'); $result = $importplugin->newenrolmentemail($student); $this->assertFalse($result); // Test success when enabled, has template text, and user has email. $testsubject = 'Test Subject'; $testbody = 'Test Body'; set_config('newenrolmentemailenabled', '1', 'dhimport_version1elis'); set_config('newenrolmentemailsubject', $testsubject, 'dhimport_version1elis'); set_config('newenrolmentemailtemplate', $testbody, 'dhimport_version1elis'); set_config('newenrolmentemailfrom', 'admin', 'dhimport_version1elis'); $result = $importplugin->newenrolmentemail($student); $this->assertNotEmpty($result); $this->assertInternalType('array', $result); $this->assertArrayHasKey('user', $result); $this->assertEquals($muser, $result['user']); $this->assertArrayHasKey('from', $result); $this->assertEquals(get_admin(), $result['from']); $this->assertArrayHasKey('subject', $result); $this->assertEquals($testsubject, $result['subject']); $this->assertArrayHasKey('body', $result); $this->assertEquals($testbody, $result['body']); // Test success and from is set to teacher when selected. $testsubject = 'Test Subject'; $testbody = 'Test Body'; set_config('newenrolmentemailenabled', '1', 'dhimport_version1elis'); set_config('newenrolmentemailsubject', $testsubject, 'dhimport_version1elis'); set_config('newenrolmentemailtemplate', $testbody, 'dhimport_version1elis'); set_config('newenrolmentemailfrom', 'teacher', 'dhimport_version1elis'); $result = $importplugin->newenrolmentemail($student); $this->assertNotEmpty($result); $this->assertInternalType('array', $result); $this->assertArrayHasKey('user', $result); $this->assertEquals($muser, $result['user']); $this->assertArrayHasKey('from', $result); $this->assertEquals($teacher, $result['from']); $this->assertArrayHasKey('subject', $result); $this->assertEquals($testsubject, $result['subject']); $this->assertArrayHasKey('body', $result); $this->assertEquals($testbody, $result['body']); // Test that subject is replaced by empty string when not present. $testsubject = null; $testbody = 'Test Body'; set_config('newenrolmentemailenabled', '1', 'dhimport_version1elis'); set_config('newenrolmentemailsubject', $testsubject, 'dhimport_version1elis'); set_config('newenrolmentemailtemplate', $testbody, 'dhimport_version1elis'); set_config('newenrolmentemailfrom', 'admin', 'dhimport_version1elis'); $result = $importplugin->newenrolmentemail($student); $this->assertNotEmpty($result); $this->assertInternalType('array', $result); $this->assertArrayHasKey('user', $result); $this->assertEquals($muser, $result['user']); $this->assertArrayHasKey('from', $result); $this->assertEquals(get_admin(), $result['from']); $this->assertArrayHasKey('subject', $result); $this->assertEquals('', $result['subject']); $this->assertArrayHasKey('body', $result); $this->assertEquals($testbody, $result['body']); // Full testing of replacement is done below, but just test that it's being done at all from the main function. $testsubject = 'Test Subject'; $testbody = 'Test Body %%user_username%%'; $expectedtestbody = 'Test Body ' . $muser->username; set_config('newenrolmentemailenabled', '1', 'dhimport_version1elis'); set_config('newenrolmentemailsubject', $testsubject, 'dhimport_version1elis'); set_config('newenrolmentemailtemplate', $testbody, 'dhimport_version1elis'); set_config('newenrolmentemailfrom', 'admin', 'dhimport_version1elis'); $result = $importplugin->newenrolmentemail($student); $this->assertNotEmpty($result); $this->assertInternalType('array', $result); $this->assertArrayHasKey('user', $result); $this->assertEquals($muser, $result['user']); $this->assertArrayHasKey('from', $result); $this->assertEquals(get_admin(), $result['from']); $this->assertArrayHasKey('subject', $result); $this->assertEquals($testsubject, $result['subject']); $this->assertArrayHasKey('body', $result); $this->assertEquals($expectedtestbody, $result['body']); }
/** * Test failure conditions. * @dataProvider dataprovider_failure * @expectedException moodle_exception * @param array $coursetodelete The incoming course data. */ public function test_failure(array $coursetodelete) { global $DB; $this->give_permissions(array('local/elisprogram:course_delete')); // Create test course to update. $crs = new course(array('idnumber' => 'testcourse', 'name' => 'Test Course', 'syllabus' => '')); $crs->save(); $response = local_datahub_elis_course_delete::course_delete($coursetodelete); }
/** * Validate that the role field is handled as needed during unassignment * * @param string $role The input value for the role field * @dataProvider role_provider */ public function test_elis_user_instructor_unenrolment_handles_role($role) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/course.class.php'); require_once elispm::lib('data/instructor.class.php'); require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/user.class.php'); set_config('coursecontact', 'teacher,editingteacher'); set_config('default_instructor_role', 'teacher', 'local_elisprogram'); $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); $class = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber')); $class->save(); $instructor = new instructor(array('userid' => $user->id, 'classid' => $class->id)); $instructor->save(); // Validate setup. $this->assertTrue($DB->record_exists(instructor::TABLE, array('userid' => $user->id, 'classid' => $class->id))); // Run the instructor assignment delete action. $record = new stdClass(); $record->context = 'class_testclassidnumber'; $record->user_username = '******'; $record->role = $role; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->class_enrolment_delete($record, 'bogus', 'testclassidnumber'); // Validation. $this->assertEquals(0, $DB->count_records(instructor::TABLE)); }
/** * Validate that mappings are applied during the class instance delete action */ public function test_mapping_applied_during_class_delete() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/pmclass.class.php'; $this->init_mapping(); $pmcourse = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $pmcourse->save(); $pmclass = new pmclass(array('courseid' => $pmcourse->id, 'idnumber' => 'testclassidnumber')); $pmclass->save(); // Run the course delete action. $record = new stdClass(); $record->customaction = 'delete'; $record->customcontext = 'class'; $record->customidnumber = 'testclassidnumber'; $this->run_pmentity_import((array) $record); // Validation. $this->assertEquals(0, $DB->count_records(pmclass::TABLE)); }
/** * Test failure conditions. * @dataProvider dataprovider_failure * @expectedException moodle_exception * @param array $params The incoming parameters. */ public function test_failure(array $params, $giveperms = true) { global $DB; if ($giveperms === true) { $this->give_permissions(array('local/elisprogram:class_delete')); } $course = new course(array('idnumber' => 'testcourse', 'name' => 'Test Course', 'syllabus' => '')); $course->save(); $pmclass = new pmclass(array('idnumber' => 'testclass', 'courseid' => $course->id)); $pmclass->save(); $response = local_datahub_elis_class_delete::class_delete($params); }
/** * Test creating a new course entity with a default role assignment defined. */ public function test_createcoursewithdefaultroleassignment() { global $DB; list($rcid, $reid) = $this->create_roles('course'); // Setup the global USER object to be our test user. $USER = $DB->get_record('user', array('id' => 100)); // Setup the editor role to be the default role for the course context. elis::$config->local_elisprogram->default_course_role_id = $reid; $sysctx = context_system::instance(); // Assign the test user the creator role. role_assign($rcid, $USER->id, $sysctx->id); // Create a new course entity. $data = array('idnumber' => 'course100', 'name' => 'course100', 'syllabus' => 'course100'); $obj = new course($data); $obj->save(); // Initialize a new course management page and invoke the code that handles default role assignments. $page = new coursepage(); $page->after_cm_entity_add($obj); $coursectx = \local_elisprogram\context\course::instance($obj->id); $params = array('roleid' => $reid, 'userid' => $USER->id, 'contextid' => $coursectx->id); $this->assertTrue($DB->record_exists('role_assignments', $params)); }
/** * Initialize a new course description object */ protected function initcourse() { $data = array('idnumber' => '__fcH__TESTID001__', 'name' => 'Test Course 1', 'syllabus' => ''); $newcourse = new course($data); $newcourse->save(); $this->tcourseid = $newcourse->id; }
/** * Test failure conditions. * @dataProvider dataprovider_failure * @expectedException moodle_exception * @param array $course The incoming ELIS course data. */ public function test_failure(array $course) { global $DB; $this->give_permissions(array('local/elisprogram:course_create')); // Setup duplicate course. $dupcrs = new course(array('idnumber' => 'duptestcourse', 'name' => 'Duplicate Test Course')); $dupcrs->save(); $response = local_datahub_elis_course_create::course_create($course); }
/** * Validate that mappings are applied during the instructor enrolment delete action */ public function test_mapping_applied_during_instructor_enrolment_delete() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/instructor.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/pmclass.class.php'; $this->init_mapping(); $userid = $this->create_test_user(); $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); $pmclass = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber')); $pmclass->save(); $instructor = new instructor(array('classid' => $pmclass->id, 'userid' => $userid)); $instructor->save(); // Run the instructor enrolment delete action. $record = new stdClass(); $record->customaction = 'delete'; $record->customcontext = 'class_testclassidnumber'; $record->customuser_username = '******'; $record->customuser_email = '*****@*****.**'; $record->customuser_idnumber = 'testuseridnumber'; $record->customrole = 'instructor'; $this->run_enrolment_import((array) $record); // Validation. $this->assertEquals(0, $DB->count_records(instructor::TABLE)); }
/** * Validate that the method respects the locked status when run for a * specific user */ public function test_methodonlyupdatesunlockedenrolmentsforspecificuserid() { global $DB; $this->load_csv_data(); // Set up enrolments. $this->make_course_enrollable(); enrol_try_internal_enrol(2, 100, 1); enrol_try_internal_enrol(2, 101, 1); // Set required PM course grade. $pmcourse = new \course(array('id' => 100, 'completion_grade' => 50)); $pmcourse->save(); // Set up course grade item. $coursegradeitem = \grade_item::fetch_course_item(2); $coursegradeitem->grademax = 100; $coursegradeitem->needsupdate = false; $coursegradeitem->locked = true; $coursegradeitem->update(); // Assign student grades. $coursegradegrade = new \grade_grade(array('itemid' => 1, 'userid' => 100, 'finalgrade' => 100)); $coursegradegrade->insert(); $coursegradegrade = new \grade_grade(array('itemid' => 1, 'userid' => 101, 'finalgrade' => 100)); $coursegradegrade->insert(); // Enrol the student. $student = new \student(); $student->userid = 103; $student->classid = 100; $student->grade = 0; $student->completestatusid = STUSTATUS_NOTCOMPLETE; $student->locked = 1; $student->save(); // Call and validate that locked record is not changed. $sync = new \local_elisprogram\moodle\synchronize(); $sync->synchronize_moodle_class_grades(100); $this->assert_student_exists(100, 103, 0, STUSTATUS_NOTCOMPLETE, null, null, 1); $DB->execute("UPDATE {" . \student::TABLE . "} SET locked = 0"); // Call and validate that unlocked record is changed. $sync = new \local_elisprogram\moodle\synchronize(); $sync->synchronize_moodle_class_grades(100); // Validate count. $count = $DB->count_records(\student::TABLE, array('completestatusid' => STUSTATUS_PASSED)); $this->assertEquals(1, $count); // NOTE: this method does not lock enrolments. $this->assert_student_exists(100, 103, 100, STUSTATUS_PASSED, null, null, 0); }
/** * Validate that class instance-moodle course associations can be created during a class instance update action. * * @param string $link The link attribute to use in the import, or 'auto' to auto-create from template. * @dataProvider link_course_provider */ public function test_associate_moodle_course_during_class_update($link) { global $CFG, $DB, $USER; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/classmoodlecourse.class.php'); require_once elispm::lib('data/coursetemplate.class.php'); require_once elispm::lib('data/course.class.php'); require_once elispm::lib('data/pmclass.class.php'); // Make sure $USER is set up for backup/restore. $USER->id = $DB->get_field_select('user', 'id', "username != 'guest' AND deleted = 0", array(), IGNORE_MULTIPLE); // Need the moodle/backup:backupcourse capability. $guestroleid = create_role('guestrole', 'guestrole', 'guestrole'); set_config('guestroleid', $guestroleid); set_config('siteguest', ''); $systemcontext = context_system::instance(); $roleid = create_role('testrole', 'testrole', 'testrole'); assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $roleid, $systemcontext->id); role_assign($roleid, $USER->id, $systemcontext->id); $coursecategory = new stdClass(); $coursecategory->name = 'testcoursecategoryname'; $coursecategory->id = $DB->insert_record('course_categories', $coursecategory); $moodlecourse = new stdClass(); $moodlecourse->category = $coursecategory->id; $moodlecourse->shortname = 'testcourseshortname'; $moodlecourse->fullname = 'testcoursefullname'; $moodlecourse = create_course($moodlecourse); $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); $class = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber')); $class->save(); // Need this for the 'auto' case, at the very least. $coursetemplate = new coursetemplate(array('courseid' => $course->id, 'location' => $moodlecourse->id, 'templateclass' => 'moodlecourseurl')); $coursetemplate->save(); // Run the class instance create action. $record = new stdClass(); $record->idnumber = 'testclassidnumber'; $record->assignment = 'testcourseidnumber'; $record->link = $link; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->class_update($record, 'bogus'); // Validation. if ($record->link == 'auto') { $moodlecourseid = $moodlecourse->id + 1; } else { $moodlecourseid = $moodlecourse->id; } $dbautocreated = $record->link == 'auto' ? 1 : 0; $this->assertTrue($DB->record_exists(classmoodlecourse::TABLE, array('classid' => $class->id, 'moodlecourseid' => $moodlecourseid, 'enroltype' => 0, 'enrolplugin' => 'crlm', 'autocreated' => $dbautocreated))); ini_set('max_execution_time', '0'); }
/** * Validate that course description-moodle template course associations * can be created during a course description update action */ public function test_associate_moodle_course_during_course_update() { global $CFG, $DB; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/course.class.php'); require_once elispm::lib('data/coursetemplate.class.php'); $coursecategory = new stdClass(); $coursecategory->name = 'testcoursecategoryname'; $coursecategory->id = $DB->insert_record('course_categories', $coursecategory); $templatecourse = new stdClass(); $templatecourse->category = $coursecategory->id; $templatecourse->shortname = 'testcourseshortname'; $templatecourse->fullname = 'testcoursefullname'; $templatecourse = create_course($templatecourse); // Create the course description. $pmcourse = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $pmcourse->save(); // Run the course description update action. $record = new stdClass(); $record->name = 'testcoursename'; $record->idnumber = 'testcourseidnumber'; $record->link = $templatecourse->shortname; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->course_update($record, 'bogus'); // Validation. $this->assertTrue($DB->record_exists(coursetemplate::TABLE, array('courseid' => $pmcourse->id, 'location' => $templatecourse->id, 'templateclass' => 'moodlecourseurl'))); }