コード例 #1
0
 /**
  * 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');
 }
コード例 #2
0
ファイル: class_delete.class.php プロジェクト: jamesmcq/elis
 /**
  * Performs class deletion
  * @throws moodle_exception If there was an error in passed parameters.
  * @throws data_object_exception If there was an error deleting the entity.
  * @param array $data The incoming data parameter.
  * @return array An array of parameters, if successful.
  */
 public static function class_delete(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::class_delete_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     // Initialize version1elis importplugin for utility functions.
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     // Get the class.
     $clsid = $DB->get_field(pmclass::TABLE, 'id', array('idnumber' => $data['idnumber']));
     if (empty($clsid)) {
         throw new data_object_exception('ws_class_delete_fail_invalid_idnumber', 'local_datahub', '', $data);
     }
     // Capability checking.
     require_capability('local/elisprogram:class_delete', \local_elisprogram\context\pmclass::instance($clsid));
     // Delete the class.
     $pmclass = new pmclass($clsid);
     $pmclass->delete();
     // Verify class deleted & respond.
     if (!$DB->record_exists(pmclass::TABLE, array('id' => $clsid))) {
         return array('messagecode' => get_string('ws_class_delete_success_code', 'local_datahub'), 'message' => get_string('ws_class_delete_success_msg', 'local_datahub'));
     } else {
         throw new data_object_exception('ws_class_delete_fail', 'local_datahub');
     }
 }
コード例 #3
0
 /**
  * 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));
     }
 }
コード例 #4
0
 /**
  * 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');
 }
コード例 #5
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)));
 }
コード例 #6
0
ファイル: enrolments.table.php プロジェクト: jamesmcq/elis
 /**
  * Get an array of allowed clusters for a class that can be passed to get_filter_sql_permissions_elementuser()
  * @param int $classid The classid to get clusters for.
  * @return array An array of objects
  */
 public static function getclustersforclass($classid)
 {
     $clusters = pmclass::get_allowed_clusters($classid);
     $return = array();
     foreach ($clusters as $i => $cluster) {
         if (is_numeric($cluster)) {
             $return[] = (object) array('clusterid' => $cluster);
         } else {
             if ($cluster instanceof userset) {
                 $return[] = (object) array('clusterid' => $cluster->id);
             }
         }
     }
     return $return;
 }
コード例 #7
0
ファイル: track.class.php プロジェクト: jamesmcq/elis
 /**
  * Clone a track
  * @param array $options options for cloning.  Valid options are:
  * - 'targetcurriculum': the curriculum id to associate the clones with
  *   (default: same as original track)
  * - 'classmap': a mapping of class IDs to use from the original track to
  *   the cloned track.  If a class from the original track is not mapped, a
  *   new class will be created
  * - 'moodlecourse': 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
  * @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 = array())
 {
     $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 track object
     $clone = new track($this);
     unset($clone->id);
     if (isset($options['targetcurriculum'])) {
         $clone->curid = $options['targetcurriculum'];
     }
     $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(track::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->autocreate = false;
     // avoid warnings
     $clone->save();
     $objs['tracks'] = array($this->id => $clone->id);
     // associate with target cluster (if any)
     if (isset($userset)) {
         clustertrack::associate($userset->id, $clone->id);
     }
     // copy classes
     $clstrks = track_assignment_get_listing($this->id);
     if ($clstrks->valid() === true) {
         $objs['classes'] = array();
         if (!isset($options['classmap'])) {
             $options['classmap'] = array();
         }
         foreach ($clstrks as $clstrkdata) {
             $newclstrk = new trackassignment($clstrkdata);
             $newclstrk->trackid = $clone->id;
             unset($newclstrk->id);
             if (isset($options['classmap'][$clstrkdata->clsid])) {
                 // use existing duplicate class
                 $class = new pmclass($options['classmap'][$clstrkdata->clsid]);
             } else {
                 // no existing duplicate -> duplicate class
                 $class = new pmclass($clstrkdata->clsid);
                 $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'];
                 }
             }
             $newclstrk->classid = $class->id;
             $newclstrk->courseid = $class->courseid;
             $newclstrk->save();
         }
     }
     unset($clstrks);
     return $objs;
 }
コード例 #8
0
 /**
  * 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));
 }
コード例 #9
0
ファイル: class_create.class.php プロジェクト: jamesmcq/elis
 /**
  * Performs class creation
  * @throws moodle_exception If there was an error in passed parameters.
  * @throws data_object_exception If there was an error creating the entity.
  * @param array $data The incoming data parameter.
  * @return array An array of parameters, if successful.
  */
 public static function class_create(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::class_create_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     // Capability checking.
     require_capability('local/elisprogram:class_create', context_system::instance());
     // Initialize version1elis importplugin for utility functions.
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     // Create the class.
     $data = (object) $data;
     $data = $importplugin->add_custom_field_prefixes($data);
     // Parse startdate and enddate.
     foreach (array('startdate', 'enddate') as $date) {
         if (isset($data->{$date})) {
             $data->{$date} = $importplugin->parse_date($data->{$date});
         }
     }
     // Check for duplicate idnumbers.
     if ($DB->record_exists(pmclass::TABLE, array('idnumber' => $data->idnumber))) {
         throw new moodle_exception('ws_class_create_fail_duplicateidnumber', 'local_datahub');
     }
     // Do course assignment.
     $crsid = $DB->get_field(course::TABLE, 'id', array('idnumber' => $data->assignment));
     if (empty($crsid)) {
         throw new moodle_exception('ws_class_create_fail_invalidcourseassignment', 'local_datahub');
     }
     $data->courseid = $crsid;
     $class = new pmclass();
     $class->set_from_data($data);
     $class->save();
     // Associate this class instance to a track, if necessary.
     $importplugin->associate_class_to_track($data, $class->id);
     // Associate this class instance to a Moodle course, if necessary.
     $importplugin->associate_class_to_moodle_course($data, $class->id);
     // Respond.
     if (!empty($class->id)) {
         $classrec = (array) $DB->get_record(pmclass::TABLE, array('id' => $class->id));
         $classobj = $class->to_array();
         // Convert multi-valued custom field arrays to comma-separated listing.
         $fields = static::get_class_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 && !empty($classobj[$fullfieldname]) && is_array($classobj[$fullfieldname])) {
                 $classobj[$fullfieldname] = implode(',', $classobj[$fullfieldname]);
             }
         }
         return array('messagecode' => get_string('ws_class_create_success_code', 'local_datahub'), 'message' => get_string('ws_class_create_success_msg', 'local_datahub'), 'record' => array_merge($classrec, $classobj));
     } else {
         throw new data_object_exception('ws_class_create_fail', 'local_datahub');
     }
 }
コード例 #10
0
ファイル: synchronize_test.php プロジェクト: jamesmcq/elis
 /**
  * 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);
         }
     }
 }
コード例 #11
0
 /**
  * Test coursecatalogpage->display_savenew() for setting of class enrolment time (ELIS-8518)
  * @uses $DB
  * @uses $USER
  */
 public function test_display_savenew_classenrolmenttime()
 {
     global $DB, $USER;
     $this->load_csv_data_for_enrolment_test();
     $USER = $DB->get_record('user', array('id' => 100));
     // Set user to the test user.
     $_GET['clsid'] = 100;
     // Class GET parameter is expected by coursecatalogpage function.
     $now = time();
     // Set the startdate on the ELIS class to a day in the future.
     $pmclass = new pmclass(100);
     $pmclass->startdate = $now + 60 * 60 * 24;
     $pmclass->save();
     // Set the startdate on the associated Moodle course to a day in the past.
     $course = new stdClass();
     $course->id = 100;
     $course->startdate = $now - 60 * 60 * 24;
     $DB->update_record('course', $course);
     $coursecatalogpage = new coursecatalogpage();
     try {
         $coursecatalogpage->display_savenew();
     } catch (Exception $e) {
         // Ignore the redirect error message because we are not testing that here.
     }
     $enrolmenttime = $DB->get_field('local_elisprogram_cls_enrol', 'enrolmenttime', array('classid' => 100, 'userid' => 103));
     $difference = abs($now - $enrolmenttime);
     $this->assertLessThanOrEqual(1, $difference);
     // Allow for 1 second variance, just in case.
 }
コード例 #12
0
 /**
  * Tests contexts in pmclass data object.
  *
  * Covers:
  * local/elisprogram/lib/data/pmclass.class.php:259
  */
 public function test_classcontexts()
 {
     $this->setup_curriculum();
     $this->setup_curriculumcourse();
     $this->setup_courses();
     $this->setup_classes();
     $pmclass = new pmclass(101);
     $pmclass->delete();
 }
コード例 #13
0
ファイル: waitlist_test.php プロジェクト: jamesmcq/elis
 /**
  * Test the autoenrol after course completion function.
  */
 public function test_check_autoenrol_after_course_completion()
 {
     $dataset = $this->createCsvDataSet(array(course::TABLE => elispm::file('tests/fixtures/pmcourse.csv'), pmclass::TABLE => elispm::file('tests/fixtures/pmclass.csv'), user::TABLE => elispm::file('tests/fixtures/pmuser.csv'), student::TABLE => elispm::file('tests/fixtures/student.csv'), waitlist::TABLE => elispm::file('tests/fixtures/waitlist2.csv')));
     $this->loadDataSet($dataset);
     $class = new pmclass(100);
     $class->load();
     $class->maxstudents = 2;
     $class->enrol_from_waitlist = 1;
     $class->save();
     $student = new student(array('userid' => 103, 'classid' => 100));
     $student->completestatusid = STUSTATUS_PASSED;
     $student->save();
     $return = waitlist::check_autoenrol_after_course_completion($student);
     $this->assertTrue($return);
 }
コード例 #14
0
 /**
  * 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);
     }
 }
コード例 #15
0
ファイル: studentpage.class.php プロジェクト: jamesmcq/elis
 /**
  * Override print_num_items to display the max number of students allowed in this class
  *
  * @param int $numitems max number of students
  */
 public function print_num_items($classid, $max)
 {
     $pmclass = new pmclass($classid);
     $students = $pmclass->get_completion_counts($classid);
     $langfailed = get_string('num_students_failed', static::LANG_FILE);
     $langpassed = get_string('num_students_passed', static::LANG_FILE);
     $langnotcomplete = get_string('num_students_not_complete', static::LANG_FILE);
     $langmaxstudents = get_string('num_max_students', static::LANG_FILE);
     if (!empty($students[STUSTATUS_FAILED])) {
         // echo '<div style="float:right;">'.$langfailed.': '.$students[STUSTATUS_FAILED].'</div><br />';
     }
     if (!empty($students[STUSTATUS_PASSED])) {
         // echo '<div style="float:right;">'.$langpassed.': '.$students[STUSTATUS_PASSED].'</div><br />';
     }
     if (!empty($students[STUSTATUS_NOTCOMPLETE])) {
         // echo '<div style="float:right;">'.$langnotcomplete.': '.$students[STUSTATUS_NOTCOMPLETE].'</div><br />';
     }
     if (!empty($max)) {
         echo '<div style="float:right;">' . $langmaxstudents . ': ' . $max . '</div><br />';
     }
 }
コード例 #16
0
 function get_item_display_options($column, $class)
 {
     $classobj = new pmclass($class);
     if (!$classobj->is_enrollable()) {
         return get_string('notenrollable', 'enrol');
     }
     if (student::count_enroled($class->id) < $class->maxstudents || empty($class->maxstudents)) {
         $action = 'savenew';
     } else {
         $action = 'confirmwaitlist';
     }
     return '<a href="index.php?s=crscat&amp;section=curr&amp;clsid=' . "{$class->id}&amp;action={$action}\">" . get_string('choose') . '</a>';
 }
コード例 #17
0
ファイル: pmentities_test.php プロジェクト: jamesmcq/elis
 /**
  * Test creating a new class entity with a default role assignment defined.
  */
 public function test_createclasswithdefaultroleassignment()
 {
     global $USER, $DB;
     list($rcid, $reid) = $this->create_roles('class');
     // Setup the editor role to be the default role for the class context.
     elis::$config->local_elisprogram->default_class_role_id = $reid;
     $sysctx = context_system::instance();
     // Assign the test user the creator role.
     role_assign($rcid, $USER->id, $sysctx->id);
     // Create a new class entity.
     $data = array('courseid' => 100, 'idnumber' => 'program100', 'name' => 'program100', 'description' => 'program100');
     $obj = new pmclass($data);
     $obj->save();
     // Initialize a new class management page and invoke the code that handles default role assignments.
     $page = new pmclasspage();
     $sink = $this->redirectMessages();
     $page->after_cm_entity_add($obj);
     $classctx = \local_elisprogram\context\pmclass::instance($obj->id);
     $params = array('roleid' => $reid, 'userid' => $USER->id, 'contextid' => $classctx->id);
     $this->assertTrue($DB->record_exists('role_assignments', $params));
 }
コード例 #18
0
 /**
  * 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));
 }
コード例 #19
0
ファイル: student.class.php プロジェクト: jamesmcq/elis
 /**
  * Determines whether the current user is allowed to create, edit, and delete associations
  * between a user and a class
  *
  * @param    int      $userid    The id of the user being associated to the class
  * @param    int      $classid   The id of the class we are associating the user to
  * @uses     $DB
  * @uses     $USER;
  * @return   boolean             True if the current user has the required permissions, otherwise false
  */
 public static function can_manage_assoc($userid, $classid)
 {
     global $DB, $USER;
     // TODO: Ugly, this needs to be overhauled
     $cpage = new pmclasspage();
     if (!pmclasspage::can_enrol_into_class($classid)) {
         //the users who satisfty this condition are a superset of those who can manage associations
         return false;
     } else {
         if ($cpage->_has_capability('local/elisprogram:class_enrol', $classid)) {
             //current user has the direct capability
             return true;
         }
     }
     //get the context for the "indirect" capability
     $context = pm_context_set::for_user_with_capability('cluster', 'local/elisprogram:class_enrol_userset_user', $USER->id);
     $allowed_clusters = array();
     $allowed_clusters = pmclass::get_allowed_clusters($classid);
     //query to get users associated to at least one enabling cluster
     $cluster_select = '';
     if (empty($allowed_clusters)) {
         $cluster_select = '0=1';
     } else {
         $cluster_select = 'clusterid IN (' . implode(',', $allowed_clusters) . ')';
     }
     $select = "userid = ? AND {$cluster_select}";
     //user just needs to be in one of the possible clusters
     if ($DB->record_exists_select(clusterassignment::TABLE, $select, array($userid))) {
         return true;
     }
     return false;
 }
コード例 #20
0
 /**
  * Validate that the provided custom field type and value produce the
  * specified error message on class instance 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_class_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/pmclass.class.php';
     $this->create_custom_field(CONTEXT_ELIS_CLASS, $uitype, $otherparams);
     // Create mapping record.
     $this->create_mapping_record('course', 'testfieldshortname', 'customtestfieldshortname');
     $courseid = $this->create_test_course();
     $class = new pmclass(array('courseid' => $courseid, 'idnumber' => 'testclassidnumber'));
     $class->save();
     $data = array('action' => 'update', 'context' => 'class', 'idnumber' => 'testclassidnumber', 'customtestfieldshortname' => $value);
     $message = '[course.csv line 2] Class instance with idnumber "testclassidnumber" could not be updated. ' . $message . "\n";
     $this->assert_data_produces_error($data, $message, 'course');
 }
コード例 #21
0
 /**
  * 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);
 }
コード例 #22
0
ファイル: lib.php プロジェクト: jamesmcq/elis
/**
 * Check for nags...
 *
 */
function pm_check_for_nags()
{
    $status = true;
    mtrace("Checking notifications<br />\n");
    $status = pmclass::check_for_nags() && $status;
    $status = pmclass::check_for_moodle_courses() && $status;
    $status = course::check_for_nags() && $status;
    $status = curriculum::check_for_nags() && $status;
    return $status;
}
コード例 #23
0
 /**
  * Initialize a new class object
  * @param int $courseid A course record ID
  */
 protected function initclass($courseid)
 {
     $data = array('idnumber' => '__fcH__TESTID001__', 'courseid' => $courseid);
     $newclass = new pmclass($data);
     $newclass->save();
     $this->tclassid = $newclass->id;
 }
コード例 #24
0
ファイル: waitlist.class.php プロジェクト: jamesmcq/elis
 /**
  *
  * @global object $CFG
  * @uses $CFG
  * @uses $OUTPUT
  */
 public function enrol()
 {
     global $CFG;
     $class = new pmclass($this->classid);
     // enrol directly in the course
     $student = new student();
     // TBD: new student($this); didn't work!!!
     $student->userid = $this->userid;
     $student->classid = $this->classid;
     $student->enrolmenttime = max(time(), $class->startdate);
     // Disable validation rules for prerequisites and enrolment_limits
     $student->validation_overrides[] = 'prerequisites';
     $student->validation_overrides[] = 'enrolment_limit';
     $courseid = $class->get_moodle_course_id();
     if ($courseid) {
         $course = $this->_db->get_record('course', array('id' => $courseid));
         // check that the elis plugin allows for enrolments from the course
         // catalog -- if not, see if there are other plugins that allow
         // self-enrolment.
         $plugin = enrol_get_plugin('elis');
         $enrol = $plugin->get_or_create_instance($course);
         if (!$enrol->{enrol_elis_plugin::ENROL_FROM_COURSE_CATALOG_DB}) {
             // get course enrolment plugins, and see if any of them allow self-enrolment
             $enrols = enrol_get_plugins(true);
             $enrolinstances = enrol_get_instances($course->id, true);
             foreach ($enrolinstances as $instance) {
                 if (!isset($enrols[$instance->enrol])) {
                     continue;
                 }
                 $form = $enrols[$instance->enrol]->enrol_page_hook($instance);
                 if ($form) {
                     // at least one plugin allows self-enrolment -- send
                     // the user to the course enrolment page, and prevent
                     // automatic enrolment
                     $a = new stdClass();
                     $a->id = $course->id;
                     $a->idnumber = $class->idnumber;
                     $a->wwwroot = $CFG->wwwroot;
                     $subject = get_string('moodleenrol_subj', self::LANG_FILE, $a);
                     $message = get_string('moodleenrol', self::LANG_FILE, $a);
                     $student->no_moodle_enrol = true;
                     break;
                 }
             }
         }
     }
     $student->save();
     // Send notification, if enabled.
     $sendnotification = !empty(elis::$config->local_elisprogram->notify_enroledfromwaitlist_user) ? true : false;
     if ($sendnotification === true) {
         if (!isset($message)) {
             $a = new stdClass();
             $a->idnum = $class->idnumber;
             $subject = get_string('nowenroled', self::LANG_FILE, $a);
             $message = get_string('nowenroled', self::LANG_FILE, $a);
         }
         $cuser = new user($this->userid);
         $cuser->load();
         $from = get_admin();
         notification::notify($message, $cuser, $from);
     }
     $this->delete();
 }
コード例 #25
0
 /**
  * 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);
 }
コード例 #26
0
 /**
  * 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);
 }
コード例 #27
0
 /**
  * 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)));
 }
コード例 #28
0
 /**
  * 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));
 }
コード例 #29
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');
 }
コード例 #30
0
 /**
  * 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']);
 }