/** * Test validation of duplicates * @expectedException data_object_validation_exception */ public function test_classmoodlecourse_preventsduplicates() { $dataset = $this->createCsvDataSet(array(classmoodlecourse::TABLE => elispm::file('tests/fixtures/class_moodle_course.csv'))); $this->loadDataSet($dataset); $classmoodlecourse = new classmoodlecourse(array('classid' => 100, 'moodlecourseid' => 100)); $classmoodlecourse->save(); }
/** * Test validation that class duplicate with autocreate creates and links to a moodle course */ public function test_autocreatemoodlecourse_createsandlinksmoodlecourse() { global $DB; $class = new pmclass(1); $classmoodle = new classmoodlecourse(array('moodlecourseid' => 2, 'classid' => 1)); $classmoodle->save(); $userset = new stdClass(); $userset->name = 'test'; $options = array(); $options['targetcluster'] = $userset; $options['classes'] = 1; $options['moodlecourses'] = 'copyalways'; $options['classmap'] = array(); $return = $class->duplicate($options); // Make sure that a we get a class returned. $this->assertTrue(is_array($return['classes'])); // Get the new returned id. $id = $return['classes'][1]; $recordexists = $DB->record_exists('local_elisprogram_cls_mdl', array('classid' => $id)); // We want to validate that a link to the new moodle course was created. $this->assertTrue($recordexists); // Get the new course id. $record = $DB->get_record('local_elisprogram_cls_mdl', array('classid' => $id)); $courseexists = $DB->record_exists('course', array('id' => $record->moodlecourseid)); // We want to validate that new moodle course was created. $this->assertTrue($recordexists); ini_set('max_execution_time', '0'); }
/** * 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 export respects grade letter boundaries and the * existence of an associated Moodle course * * @param int $grade The enrolment grade * @param int $letter The expected enrolment grade letter * @dataProvider grade_letter_provider */ public function test_exportrespects_gradeletters($grade, $letter) { global $CFG, $DB; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/student.class.php'); require_once elispm::lib('data/classmoodlecourse.class.php'); // Setup. $this->load_csv_data(); // Create a Moodle course category. $categorydata = new stdClass(); $categorydata->name = 'testcategory'; $categorydata->id = $DB->insert_record('course_categories', $categorydata); // Create a Moodle course. $coursedata = new stdClass(); $coursedata->category = $categorydata->id; $coursedata->fullname = 'testcourse'; $coursedata = create_course($coursedata); // Associate the PM class instance to the Moodle course. $classmoodle = new classmoodlecourse(array('classid' => 200, 'moodlecourseid' => $coursedata->id)); $classmoodle->save(); // Create grade letter mappings. $context = context_course::instance($coursedata->id); $mappings = array(10 => 1, 20 => 2, 30 => 3, 40 => 4, 50 => 5, 60 => 6, 70 => 7, 80 => 8, 90 => 9, 100 => 10); foreach ($mappings as $insertlowerboundary => $insertletter) { $record = new stdClass(); $record->contextid = $context->id; $record->lowerboundary = $insertlowerboundary; $record->letter = $insertletter; $DB->insert_record('grade_letters', $record); } // Set the enrolment grade. $sql = "UPDATE {" . student::TABLE . "}\n SET grade = ?"; $params = array($grade); $DB->execute($sql, $params); $data = $this->get_export_data(); // Validation. $this->assertEquals(2, count($data)); $this->assertEquals((string) $letter, $data[1][9]); }
/** * Attach a class record from this system to an existing Moodle course. * * @uses $CURMAN * @param int $clsid The class ID. * @param int $mdlid The Moodle course ID. * @param string $siteconfig The full system path to a Moodle congif.php file (defaults to local). * @param bool $enrolinstructor Flag for enroling instructors into the Moodle course (optional). * @param bool $enrolstudent Flag for enroling students into the Moodle course (optional). * @return bool True on success, False otherwise. */ function moodle_attach_class($clsid, $mdlid, $siteconfig = '', $enrolinstructor = false, $enrolstudent = false, $autocreate = false) { global $CURMAN; $result = true; $moodlecourseid = $mdlid; /// Look for an existing link for this class. if (!($clsmdl = $CURMAN->db->get_record(CLSMDLTABLE, 'classid', $clsid))) { /// Make sure the specified Moodle site config file exists. if (!empty($siteconfig) && !file_exists($siteconfig)) { return false; } if ($autocreate) { // auto create is checked, create connect to moodle course $cls = new cmclass($clsid); $temp = new coursetemplate(); $temp->data_load_record($cls->courseid); // no template defined, so do nothing if (empty($temp->id) || empty($temp->location)) { print_error('notemplate', 'block_curr_admin'); } $classname = $temp->templateclass; $obj = new $classname(); $courseId = $temp->location; //$obj->parseCourseId($temp->location);//print_object($courseid);die(); $moodlecourseid = content_rollover($courseId, $cls->startdate); // Rename the fullname, shortname and idnumber of the restored course $restore->id = $moodlecourseid; $restore->fullname = addslashes($cls->course->name . '_' . $cls->idnumber); $restore->shortname = addslashes($cls->idnumber); $CURMAN->db->update_record('course', $restore); } $newrec = array('classid' => $clsid, 'moodlecourseid' => $moodlecourseid, 'siteconfig' => $siteconfig, 'autocreated' => $autocreate ? 1 : 0); $clsmdl = new classmoodlecourse($newrec); $result = $clsmdl->data_insert_record() === true; } else { $clsmdl = new classmoodlecourse($clsmdl->id); } if ($enrolinstructor) { $result = $result && $clsmdl->data_enrol_instructors(); } if ($enrolstudent) { $result = $result && $clsmdl->data_enrol_students(); } events_trigger('crlm_class_associated', $clsmdl); return $result; }
/** * Validate that the sync prevents grades from syncing into two classes when running for a specific user */ public function test_nosynchappenswhencourselinkedtotwoclassesforspecificuserid() { 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); // Create second association. $classmoodlecourse = new \classmoodlecourse(array('classid' => 101, 'moodlecourseid' => 2)); $classmoodlecourse->save(); // Call and validate. $sync = new \local_elisprogram\moodle\synchronize(); $sync->synchronize_moodle_class_grades(100); $this->assert_num_students(0); // Remove second association. $DB->delete_records(\classmoodlecourse::TABLE, array('classid' => 101, 'moodlecourseid' => 2)); // Call and validate. $sync = new \local_elisprogram\moodle\synchronize(); $sync->synchronize_moodle_class_grades(100); $this->assert_num_students(1); }
function delete() { $status = true; if (!empty($this->id)) { instructor::delete_for_class($this->id); student::delete_for_class($this->id); trackassignmentclass::delete_for_class($this->id); classmoodlecourse::delete_for_class($this->id); student_grade::delete_for_class($this->id); attendance::delete_for_class($this->id); taginstance::delete_for_class($this->id); waitlist::delete_for_class($this->id); classmoodlecourse::delete_for_class($this->id); $level = context_level_base::get_custom_context_level('class', 'block_curr_admin'); $result = delete_context($level, $this->id); $status = $this->data_delete_record(); } return $status; }
/** * Validate that the check_for_moodle_courses method deletes the correct set of orphaned associations for a specific user. * @param array $associations The list of associations and information regarding whether they should be cleaned up or not. * @dataProvider dataprovider_checkformoodlecourses */ public function test_checkformoodlecoursesrespectsuseridparameter($associations) { global $DB; // Set up our classes. $this->load_csv_data(); $student = new student(array('userid' => 103, 'classid' => 103)); $sink = $this->redirectMessages(); $student->save(); // Track which associations should remain. $remainingassociations = array(); foreach ($associations as $association) { // Persist the record. $record = new classmoodlecourse($association); $record->save(); // Test user is enrolled in class 103, so this one should be deleted. if ($association['classid'] != 103) { // It should persist after the method is called. $remainingassociations[] = $association; } } // Delete orphaned records. pmclass::check_for_moodle_courses(103); // Validate count. $this->assertEquals(count($remainingassociations), $DB->count_records(classmoodlecourse::TABLE)); // Validate records specifically. foreach ($remainingassociations as $remainingassociation) { $params = array('classid' => $remainingassociation['classid'], 'moodlecourseid' => $remainingassociation['moodlecourseid']); $exists = $DB->record_exists(classmoodlecourse::TABLE, $params); $this->assertTrue($exists); } }
function moodle_get_classes() { return classmoodlecourse::find(array(new join_filter('moodlecourseid', 'course', 'id'), new join_filter('classid', pmclass::TABLE, 'id'), new field_filter('id', null, field_filter::NEQ))); }
function delete() { if (!empty($this->id)) { //clean make the delete cascade into association records $filter = new field_filter('classid', $this->id); instructor::delete_records($filter, $this->_db); student::delete_records($filter, $this->_db); trackassignment::delete_records($filter, $this->_db); classmoodlecourse::delete_records($filter, $this->_db); student_grade::delete_records($filter, $this->_db); waitlist::delete_records($filter, $this->_db); parent::delete(); $context = \local_elisprogram\context\pmclass::instance($this->id); $context->delete(); } }
/** * Set up data that is needed for testing */ private function set_up_required_data($assignusertouserset = true, $assigncoursetoclass = true, $assigntracktoclass = true, $initclusterprofile = false, $initusersetfielddata = true) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->dirroot . '/lib/enrollib.php'; require_once elis::lib('data/customfield.class.php'); require_once elispm::file('accesslib.php'); require_once elispm::lib('data/classmoodlecourse.class.php'); require_once elispm::lib('data/clusterassignment.class.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/pmclass.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'); $fieldcategoryid = $DB->get_field(field_category::TABLE, 'id', array('name' => 'Associated Group')); $this->assertNotEquals(false, $fieldcategoryid); $fieldcategory = new field_category($fieldcategoryid); $fieldcategory->load(); // Set up the test user. $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); // Set up the test course description and class instance. $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); $pmclass = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber')); $pmclass->save(); $category = new stdClass(); $category->name = 'testcategoryname'; $category->id = $DB->insert_record('course_categories', $category); // Set up the test Moodle course. set_config('enrol_plugins_enabled', 'manual'); set_config('defaultenrol', 1, 'enrol_manual'); set_config('status', ENROL_INSTANCE_ENABLED, 'enrol_manual'); $course = new stdClass(); $course->category = $category->id; $course->shortname = 'testcourseshortname'; $course->fullname = 'testcoursefullname'; $course = create_course($course); if ($assigncoursetoclass) { // Assign the Moodle course to a class instance. $classmoodlecourse = new classmoodlecourse(array('classid' => $pmclass->id, 'moodlecourseid' => $course->id)); $classmoodlecourse->save(); } // Set up the test program and track. $curriculum = new curriculum(array('idnumber' => 'testcurriculumidnumber')); $curriculum->save(); $track = new track(array('curid' => $curriculum->id, 'idnumber' => 'testtrackidnumber')); $track->save(); if ($assigntracktoclass) { // Assign the test track to the test class instance. $trackassignment = new trackassignment(array('trackid' => $track->id, 'classid' => $pmclass->id, 'autoenrol' => 1)); $trackassignment->save(); } // Set up the test userset. $userset = new userset(); $usersetdata = array('name' => 'testusersetname'); if ($initusersetfielddata) { $usersetdata['field_userset_group'] = 1; $usersetdata['field_userset_groupings'] = 1; } $userset->set_from_data((object) $usersetdata); $userset->save(); // Assign the test user to the test track. $usertrack = new usertrack(array('userid' => $user->id, 'trackid' => $track->id)); $usertrack->save(); $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => $track->id)); $clustertrack->save(); if ($assignusertouserset) { // Assign the test user to the test userset. $clusterassignment = new clusterassignment(array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'manual')); $clusterassignment->save(); } if ($initclusterprofile) { // Set up a file we can use to auto-associate users to a userset. $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'autoassociate', 'name' => 'autoassociate', 'datatype' => 'bool')); $field->save(); // Ensure manual field owner exists for syncing. field_owner::ensure_field_owner_exists($field, 'manual'); $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual')); $owner = new field_owner($ownerid); $owner->param_control = 'checkbox'; $owner->save(); field_owner::ensure_field_owner_exists($field, 'moodle_profile'); $DB->execute("UPDATE {" . field_owner::TABLE . "} SET exclude = ?", array(pm_moodle_profile::sync_to_moodle)); $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER)); $fieldcontextlevel->save(); // The associated Moodle user profile field. require_once $CFG->dirroot . '/user/profile/definelib.php'; require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php'; $profiledefinecheckbox = new profile_define_checkbox(); $data = new stdClass(); $data->datatype = 'checkbox'; $data->categoryid = 99999; $data->shortname = 'autoassociate'; $data->name = 'autoassociate'; $profiledefinecheckbox->define_save($data); $mfield = $DB->get_record('user_info_field', array('shortname' => 'autoassociate')); // The "cluster-profile" association. $usersetprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $mfield->id, 'value' => true)); $usersetprofile->save(); } // Enrol the user in the Moodle course. $mdluserid = $DB->get_field('user', 'id', array('username' => 'testuserusername')); $roleid = create_role('testrole', 'testrole', 'testrole'); enrol_try_internal_enrol($course->id, $mdluserid, $roleid); // Set up the necessary config data. set_config('userset_groups', 1, 'elisprogram_usetgroups'); set_config('siteguest', ''); // Validate setup. $this->assertEquals(0, $DB->count_records('groups')); }