/**
  * 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));
     }
 }
Exemplo n.º 2
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');
 }
Exemplo n.º 3
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 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');
 }
 /**
  * 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);
 }
 /**
  * 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'));
 }
Exemplo n.º 7
0
 /**
  * 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);
 }
Exemplo n.º 8
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);
 }
Exemplo n.º 9
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)));
 }
Exemplo n.º 10
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);
 }
Exemplo n.º 11
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;
 }
Exemplo n.º 12
0
 /**
  * Create an ELIS class instance.
  * @param course &$course The course description to assign the class to.
  * @param field &$field A custom field to set when creating the class.
  * @return pmclass The created class.
  */
 public function create_class(course &$course, field &$field)
 {
     $data = new stdClass();
     $data->courseid = $course->id;
     $data->idnumber = 'CLS101';
     $data->startdate = 0;
     $data->enddate = 0;
     $data->starttime = 31603200;
     $data->endtime = 31603200;
     $data->maxstudents = 0;
     $data->moodleCourses = array('moodlecourseid' => '0');
     $data->enrol_from_waitlist = '0';
     $data->field_class_testfield = '';
     $data->starttimeminute = 61;
     $data->starttimehour = 61;
     $data->endtimeminute = 61;
     $data->endtimehour = 61;
     $fieldvar = 'field_' . $field->shortname;
     $data->{$fieldvar} = 'test field data';
     $cls = new pmclass();
     $cls->set_from_data($data);
     $cls->save();
     return $cls;
 }
Exemplo n.º 13
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']);
 }
 /**
  * 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)));
 }
Exemplo n.º 15
0
 function class_update($record, $filename)
 {
     global $DB, $CFG;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elispm::lib('data/pmclass.class.php');
     //NOTE: not checking field lengths because only idnumber can be too long, and
     //we can't set this during updates
     $message = "";
     if (isset($record->idnumber)) {
         if (!($clsid = $DB->get_field(pmclass::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 class instance.", 0, $filename, $this->linenumber, $record, "class");
             return false;
         }
         if (isset($record->assignment)) {
             $message = "Class instance with idnumber \"{$record->idnumber}\" was not re-assigned to course description with name \"{$record->assignment}\" because moving class instances between course descriptions is not supported.";
         }
     }
     if (!$this->validate_class_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, 'class')) {
         return false;
     }
     $record->id = $clsid;
     $pmclass = new pmclass();
     $pmclass->set_from_data($record);
     $pmclass->save();
     //associate this class instance to a track, if necessary
     $this->associate_class_to_track($record, $pmclass->id);
     //associate this class instance to a Moodle course, if necessary
     $this->associate_class_to_moodle_course($record, $pmclass->id);
     //log success
     $success_message = "Class instance with idnumber \"{$record->idnumber}\" successfully updated.";
     $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber);
     return true;
 }
Exemplo n.º 16
0
 /**
  * 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 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)));
 }
Exemplo n.º 18
0
 /**
  * Clone a class
  * @param array $options options for cloning.  Valid options are:
  * - '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
  * - 'targetcourse': the course id to associate the clones with (default:
  *   same as original class)
  * @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())
 {
     //needed by the rollover lib
     global $CFG;
     require_once elis::lib('rollover/lib.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 class object
     $clone = new pmclass($this);
     unset($clone->id);
     if (isset($options['targetcourse'])) {
         $clone->courseid = $options['targetcourse'];
     }
     $idnumber = $clone->idnumber;
     if (isset($userset)) {
         $idnumber = append_once($idnumber, ' - ' . $userset->name, array('maxlength' => 95));
     }
     //get a unique idnumber
     $clone->idnumber = generate_unique_identifier(pmclass::TABLE, 'idnumber', $idnumber, array('idnumber' => $idnumber));
     $clone->autocreate = false;
     // avoid warnings
     $clone->save();
     $objs['classes'] = array($this->id => $clone->id);
     $cmc = $this->_db->get_record(classmoodlecourse::TABLE, array('classid' => $this->id));
     if ($cmc) {
         if ($cmc->autocreated == -1) {
             $cmc->autocreated = elis::$config->local_elisprogram->autocreated_unknown_is_yes;
         }
         if (empty($options['moodlecourses']) || $options['moodlecourses'] == 'copyalways' || $options['moodlecourses'] == 'copyautocreated' && $cmc->autocreated) {
             // create a new Moodle course based on the current class's Moodle course
             $moodlecourseid = course_rollover($cmc->moodlecourseid);
             //check that the course has rolled over successfully
             if (!$moodlecourseid) {
                 return false;
             }
             // Rename the fullname, shortname and idnumber of the restored course
             $restore = new stdClass();
             $restore->id = $moodlecourseid;
             // ELIS-2941: Don't prepend course name if already present ...
             if (strpos($clone->idnumber, $clone->course->name) !== 0) {
                 $restore->fullname = $clone->course->name . '_' . $clone->idnumber;
             } else {
                 $restore->fullname = $clone->idnumber;
             }
             $restore->shortname = $clone->idnumber;
             $this->_db->update_record('course', $restore);
             moodle_attach_class($clone->id, $moodlecourseid);
         } elseif ($options['moodlecourses'] == 'link' || $options['moodlecourses'] == 'copyautocreated' && !$cmc->autocreated) {
             // link to the current class's Moodle course
             moodle_attach_class($clone->id, $cmc->moodlecourseid);
         } else {
             // $options['moodlecourses'] == 'autocreatenew'
             // create a new course based on the course template
             moodle_attach_class($clone->id, 0, '', false, false, true);
         }
     }
     return $objs;
 }
 /**
  * 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 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));
 }
Exemplo n.º 21
0
 /**
  * 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);
         }
     }
 }
Exemplo n.º 22
0
 /**
  * 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');
     }
 }
Exemplo n.º 23
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.
 }
 /**
  * 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));
 }
 /**
  * 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));
 }
Exemplo n.º 26
0
 /**
  * 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));
 }
 /**
  * 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');
 }
Exemplo n.º 28
0
 /**
  * Validate the $student->validate_class_enrolment_limit function.
  */
 public function test_validate_class_enrolment_limit()
 {
     $this->load_csv_data();
     $student = new student(array('userid' => 104, 'classid' => 101));
     $student->load();
     $student->save();
     try {
         $result = $student->validate_class_enrolment_limit();
         $this->assertTrue($result);
     } catch (Exception $e) {
         // Should not reach here.
         $this->assertFalse(true);
     }
     $class = new pmclass(101);
     $class->load();
     $class->maxstudents = 1;
     $class->save();
     $student = new student(array('userid' => 103, 'classid' => 101));
     try {
         $result = $student->validate_class_enrolment_limit();
     } catch (Exception $e) {
         $this->assertTrue($e instanceof pmclass_enrolment_limit_validation_exception);
     }
 }