/** * Test successful user creation. */ public function test_success() { global $DB; $this->give_permissions(array('local/elisprogram:class_edit')); // Create custom field. $fieldcat = new field_category(); $fieldcat->name = 'Test'; $fieldcat->save(); $field = new field(); $field->categoryid = $fieldcat->id; $field->shortname = 'testfield'; $field->name = 'Test Field'; $field->datatype = 'text'; $field->save(); $fieldctx = new field_contextlevel(); $fieldctx->fieldid = $field->id; $fieldctx->contextlevel = CONTEXT_ELIS_CLASS; $fieldctx->save(); $course = new course(); $course->idnumber = 'testcourse1'; $course->name = 'Test Course 1'; $course->syllabus = 'Test'; $course->save(); $class = new pmclass(array('idnumber' => 'testclass', 'startdate' => 1357016400, 'enddate' => 1359694800, 'courseid' => $course->id, 'assignment' => $course->idnumber, 'field_testfield' => 'Test Field')); $class->save(); $classupdates = array('idnumber' => 'testclass', 'startdate' => 'Feb/04/2013', 'enddate' => 'Mar/01/2013', 'field_testfield' => 'Test Field 2'); $response = local_datahub_elis_class_update::class_update($classupdates); $this->assertNotEmpty($response); $this->assertInternalType('array', $response); $this->assertArrayHasKey('messagecode', $response); $this->assertArrayHasKey('message', $response); $this->assertArrayHasKey('record', $response); $this->assertEquals(get_string('ws_class_update_success_code', 'local_datahub'), $response['messagecode']); $this->assertEquals(get_string('ws_class_update_success_msg', 'local_datahub'), $response['message']); $this->assertInternalType('array', $response['record']); $this->assertArrayHasKey('id', $response['record']); // Get class. $expectedclass = array('idnumber' => $class->idnumber, 'startdate' => rlip_timestamp(0, 0, 0, 2, 4, 2013), 'enddate' => rlip_timestamp(0, 0, 0, 3, 1, 2013), 'courseid' => $course->id, 'field_testfield' => 'Test Field 2'); $createdclass = new pmclass($response['record']['id']); $createdclass->load(); $createdclass = $createdclass->to_array(); foreach ($expectedclass as $param => $val) { $this->assertArrayHasKey($param, $createdclass); $this->assertEquals($val, $createdclass[$param]); } }
/** * Performs track 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 track_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::track_create_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); // Capability checking. require_capability('local/elisprogram:track_create', context_system::instance()); $data = (object) $data; $record = new stdClass(); $record = $data; // need all custom fields, etc. // Initialize version1elis importplugin for utility functions. $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); // Validate if (empty($data->assignment) || !($curid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->assignment)))) { throw new data_object_exception('ws_track_create_fail_invalid_assignment', 'local_datahub', '', $data); } $record->curid = $curid; unset($record->assignment); if (isset($data->startdate)) { $startdate = $importplugin->parse_date($data->startdate); if (empty($startdate)) { throw new data_object_exception('ws_track_create_fail_invalid_startdate', 'local_datahub', '', $data); } else { $record->startdate = $startdate; } } if (isset($data->enddate)) { $enddate = $importplugin->parse_date($data->enddate); if (empty($enddate)) { throw new data_object_exception('ws_track_create_fail_invalid_enddate', 'local_datahub', '', $data); } else { $record->enddate = $enddate; } } $record->name = !empty($data->name) ? $data->name : ''; $record->description = !empty($data->description) ? $data->description : ''; $record->autocreate = !empty($data->autocreate); $record->timecreated = rlip_timestamp(); $track = new track(); $track->set_from_data($record); $track->save(); // Respond. if (!empty($track->id)) { $trackrec = (array) $DB->get_record(track::TABLE, array('id' => $track->id)); $trackobj = $track->to_array(); // convert multi-valued custom field arrays to comma-separated listing $fields = self::get_track_custom_fields(); foreach ($fields as $field) { // Generate name using custom field prefix. $fullfieldname = data_object_with_custom_fields::CUSTOM_FIELD_PREFIX . $field->shortname; if ($field->multivalued && isset($trackobj[$fullfieldname]) && is_array($trackobj[$fullfieldname])) { $trackobj[$fullfieldname] = implode(',', $trackobj[$fullfieldname]); } } return array('messagecode' => get_string('ws_track_create_success_code', 'local_datahub'), 'message' => get_string('ws_track_create_success_msg', 'local_datahub'), 'record' => array_merge($trackrec, $trackobj)); } else { throw new data_object_exception('ws_track_create_fail', 'local_datahub'); } }
/** * Field mapping function to convert IP birthdate column to birthdate DB field * * @param array $input The input IP data fields * @param string $fieldkey The array key to check for date strings */ public function map_birthdate_field(&$input, $fieldkey) { if (isset($input[$fieldkey])) { $date = $input[$fieldkey]; // Determine which case we are in. if (strpos($date, '/') !== false) { $delimiter = '/'; } else { if (strpos($date, '-') !== false) { $delimiter = '-'; } else { if (strpos($date, '.') !== false) { $delimiter = '.'; } else { return false; } } } $parts = explode($delimiter, $date); if ($delimiter == '/') { // MMM/DD/YYYY or MM/DD/YYYY format. list($month, $day, $year) = $parts; $months = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'); $pos = array_search(strtolower($month), $months); if ($pos !== false) { $month = $pos + 1; } } else { if ($delimiter == '-') { // DD-MM-YYYY format. list($day, $month, $year) = $parts; } else { // YYYY.MM.DD format. list($year, $month, $day) = $parts; } } $timestamp = rlip_timestamp(0, 0, 0, $month, $day, $year); $input[$fieldkey] = strftime('%Y/%m/%d', $timestamp); } }
/** * Create and update user custom fields * * @dataProvider ui_type_provider * @param string $uitype The string value representing a UI type * @param string $data Value for create * @param mixed $expected Expected value after create as a string or int * @param string $updateddata Value for update * @param mixed $updateexpected Expected value after update as string or int * @param string $name The name of the control * @param string $datatype The datatype of the field * @param mixed $maxlength The maxiumum length of the field as int or null * @param mixed $inctime Include time along with the date as string or null * @param mixed $options The options of the field as array or null */ public function test_elis_user_custom_field_import($control, $data, $expected, $updateddata, $updateexpected, $name, $datatype, $maxlength, $inctime, $options) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elis::lib('data/customfield.class.php'); require_once elispm::lib('data/user.class.php'); $fieldid = $this->create_test_field($name, $datatype, $control, $inctime, $maxlength, $options, CONTEXT_ELIS_USER); if ($control === 'datetime' && is_array($expected)) { $expected = rlip_timestamp($expected[0], $expected[1], $expected[2], $expected[3], $expected[4], $expected[5]); } if ($control === 'datetime' && is_array($updateexpected)) { $updateexpected = rlip_timestamp($updateexpected[0], $updateexpected[1], $updateexpected[2], $updateexpected[3], $updateexpected[4], $updateexpected[5]); } $temp = new user(); // Prevent caching issues. $temp->reset_custom_field_list(); $record = new stdClass(); $record->action = 'create'; $record->email = '*****@*****.**'; $record->username = '******'; $record->idnumber = 'testuserid'; $record->firstname = 'testuserfirstname'; $record->lastname = 'testuserlastname'; $record->country = 'CA'; $record->{$name} = $data; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('user', (object) $record, 'bogus'); $usercontext = \local_elisprogram\context\user::instance($DB->get_field(user::TABLE, 'id', array('idnumber' => 'testuserid'))); $this->assert_field_values($datatype, $control, $fieldid, $usercontext->id, $expected); // Update. $record = new stdClass(); $record->action = 'update'; $record->email = '*****@*****.**'; $record->username = '******'; $record->idnumber = 'testuserid'; $record->firstname = 'testuserfirstname'; $record->lastname = 'testuserlastname'; $record->country = 'CA'; $record->{$name} = $updateddata; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('user', (object) $record, 'bogus'); $this->assert_field_values($datatype, $control, $fieldid, $usercontext->id, $updateexpected); }
/** * Validate that mappings are applied during the class instance update action */ public function test_mapping_applied_during_class_update() { global $CFG, $DB; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->dirroot . '/local/eliscore/lib/data/customfield.class.php'; require_once $CFG->dirroot . '/local/elisprogram/accesslib.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/classmoodlecourse.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/curriculum.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/pmclass.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/track.class.php'; $this->init_mapping(); $customfieldid = $this->create_custom_field(CONTEXT_ELIS_CLASS); $pmcourse = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $pmcourse->save(); $program = new curriculum(array('idnumber' => 'testprogramidnumber')); $program->save(); $track = new track(array('curid' => $program->id, 'idnumber' => 'testtrackidnumber')); $track->save(); $category = new stdClass(); $category->name = 'testcategoryname'; $category->id = $DB->insert_record('course_categories', $category); $course = new stdClass(); $course->category = $category->id; $course->shortname = 'testcourseshortname'; $course->fullname = 'testcoursefullname'; $course = create_course($course); $pmclass = new pmclass(array('courseid' => $pmcourse->id, 'idnumber' => 'testclassidnumber')); $pmclass->save(); // Run the class create update. $record = new stdClass(); $record->customaction = 'update'; $record->customcontext = 'class'; $record->customidnumber = 'testclassidnumber'; $record->customstartdate = 'Jan/02/2012'; $record->customenddate = 'Jan/02/2012'; $record->customstarttimehour = '2'; $record->customstarttimeminute = '10'; $record->customendtimehour = '2'; $record->customendtimeminute = '10'; $record->custommaxstudents = '2'; $record->customenrol_from_waitlist = '1'; $record->customassignment = 'testcourseidnumber'; $record->customtrack = 'testtrackidnumber'; $record->customautoenrol = '1'; $record->customlink = 'testcourseshortname'; $record->customtestfieldshortname = '1'; $this->run_pmentity_import((array) $record); // Validation. $data = array('courseid' => $pmcourse->id, 'idnumber' => 'testclassidnumber', 'startdate' => rlip_timestamp(0, 0, 0, 1, 2, 2012), 'enddate' => rlip_timestamp(0, 0, 0, 1, 2, 2012), 'starttimehour' => 2, 'starttimeminute' => 10, 'endtimehour' => 2, 'endtimeminute' => 10, 'maxstudents' => 2, 'enrol_from_waitlist' => 1); $this->assertTrue($DB->record_exists(pmclass::TABLE, $data)); $this->assertTrue($DB->record_exists(trackassignment::TABLE, array('classid' => $pmclass->id, 'trackid' => $track->id, 'autoenrol' => 1))); $this->assertTrue($DB->record_exists(classmoodlecourse::TABLE, array('classid' => $pmclass->id, 'moodlecourseid' => $course->id))); $instance = \local_elisprogram\context\pmclass::instance(1); $this->assertTrue($DB->record_exists(field_data_int::TABLE, array('fieldid' => $customfieldid, 'contextid' => $instance->id, 'data' => 1))); }
/** * Validate that the version 1 import plugin correctly uses field mappings * on course creation */ public function test_version1importusescoursefieldmappings() { global $CFG, $DB; $file = get_plugin_directory('dhimport', 'version1') . '/lib.php'; require_once $file; // Setup. set_config('maxsections', 20, 'moodlecourse'); // Determine the pre-existing category's id. $categoryid = $DB->get_field('course_categories', 'id', array('name' => 'childcategory')); // Set up our mapping of standard field names to custom field names. $mapping = array('action' => 'action1', 'shortname' => 'shortname1', 'fullname' => 'fullname1', 'idnumber' => 'idnumber1', 'summary' => 'summary1', 'format' => 'format1', 'startdate' => 'startdate1', 'newsitems' => 'newsitems1', 'showgrades' => 'showgrades1', 'showreports' => 'showreports1', 'maxbytes' => 'maxbytes1', 'guest' => 'guest1', 'password' => 'password1', 'visible' => 'visible1', 'lang' => 'lang1', 'category' => 'category1', 'numsections' => 'numsections1'); // Store the mapping records in the database. foreach ($mapping as $standardfieldname => $customfieldname) { $record = new stdClass(); $record->entitytype = 'course'; $record->standardfieldname = $standardfieldname; $record->customfieldname = $customfieldname; $DB->insert_record(RLIPIMPORT_VERSION1_MAPPING_TABLE, $record); } // Run the import. $data = array('entity' => 'course', 'action1' => 'create', 'shortname1' => 'fieldmapping', 'fullname1' => 'fieldmappingfullname', 'idnumber1' => 'fieldmappingidnumber', 'summary1' => 'fieldmappingsummary', 'format1' => 'social', 'startdate1' => 'Jan/01/2012', 'newsitems1' => 8, 'showgrades1' => 0, 'showreports1' => 1, 'maxbytes1' => 0, 'guest1' => 1, 'password1' => 'fieldmappingpassword', 'visible1' => 0, 'lang1' => 'en', 'category1' => 'childcategory', 'numsections1' => 15); $this->run_core_course_import($data, false); $params = array('shortname' => 'fieldmapping', 'fullname' => 'fieldmappingfullname', 'idnumber' => 'fieldmappingidnumber', 'summary' => 'fieldmappingsummary', 'format' => 'social', 'numsections' => 15, 'startdate' => rlip_timestamp(0, 0, 0, 1, 1, 2012), 'newsitems' => 8, 'showgrades' => 0, 'showreports' => 1, 'maxbytes' => 0, 'visible' => 0, 'lang' => 'en', 'category' => $categoryid); $courseid = $this->assert_core_course_exists($params); // Validate enrolment record. $data = array('courseid' => $courseid, 'enrol' => 'guest', 'password' => 'fieldmappingpassword', 'status' => ENROL_INSTANCE_ENABLED); $this->assert_record_exists('enrol', $data); // Clean up the mess. $DB->delete_records(RLIPIMPORT_VERSION1_MAPPING_TABLE, array('entitytype' => 'course')); }
/** * Validate that an "instructor" assignment can be updated, setting all available fields * * @param string $username A sample user's username, or null if not used in the import * @param string $email A sample user's email, or null if not used in the import * @param string $idnumber A sample user's idnumber, or null if not used in the import * @dataProvider user_identifier_provider */ public function test_update_elis_user_instructor_enrolment_with_all_fields($actioncreate, $actiondelete, $username, $email, $idnumber) { 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/instructor.class.php'); require_once elispm::lib('data/user.class.php'); $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 update action. $record = new stdClass(); $record->context = 'class_testclassidnumber'; if ($username != null) { $record->user_username = $user->username; } if ($email != null) { $record->user_email = $user->email; } if ($idnumber != null) { $record->user_idnumber = $user->idnumber; } $record->assigntime = 'Jan/01/2012'; $record->completetime = 'Feb/01/2012'; $record->role = 'instructor'; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->class_enrolment_update($record, 'bogus', 'testclassidnumber'); // Validation. $this->assertTrue($DB->record_exists(instructor::TABLE, array('userid' => $user->id, 'classid' => $class->id, 'assigntime' => rlip_timestamp(0, 0, 0, 1, 1, 2012), 'completetime' => rlip_timestamp(0, 0, 0, 2, 1, 2012)))); }
/** * Create an instructor class instance enrolment * * @param object $record One record of import data * @param string $filename The import file name, used for logging * @param string $idnumber The idnumber of the class instance * * @return boolean true on success, otherwise false * @uses $CFG * @uses $DB */ function class_enrolment_create_instructor($record, $filename, $idnumber) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/instructor.class.php'); if (!($crsid = $DB->get_field(pmclass::TABLE, 'id', array('idnumber' => $idnumber)))) { $this->fslogger->log_failure("instance value of \"{$idnumber}\" does not refer to a valid instance of a class context.", 0, $filename, $this->linenumber, $record, "enrolment"); return false; } $userid = $this->get_userid_from_record($record, $filename); // string to describe the user $user_descriptor = $this->get_user_descriptor($record, false, 'user_'); if ($DB->record_exists(instructor::TABLE, array('classid' => $crsid, 'userid' => $userid))) { $this->fslogger->log_failure("User with {$user_descriptor} is already enrolled in " . "class instance \"{$idnumber}\".", 0, $filename, $this->linenumber, $record, "enrolment"); return false; } if (!$this->validate_class_enrolment_data('create', $record, $filename)) { return false; } // obtain the class id $classid = $DB->get_field(pmclass::TABLE, 'id', array('idnumber' => $idnumber)); // determine assignment and completion times $today = rlip_timestamp(0, 0, 0); if (isset($record->assigntime)) { $assigntime = $this->parse_date($record->assigntime); } else { $assigntime = $today; } if (isset($record->completetime)) { $completetime = $this->parse_date($record->completetime); } else { $completetime = $today; } // create the association $insdata = array('userid' => $userid, 'classid' => $classid, 'assigntime' => $assigntime, 'completetime' => $completetime); $instructor = new instructor($insdata); if (!empty($record->role)) { $instructor->roleshortname = $record->role; } $instructor->save(); // log success $success_message = "User with {$user_descriptor} successfully enrolled in class instance \"{$idnumber}\" as an instructor."; $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber); return true; }
/** * Validate that a "student" enrolment can be updated, setting all available fields * * @param string $username A sample user's username, or null if not used in the import * @param string $email A sample user's email, or null if not used in the import * @param string $idnumber A sample user's idnumber, or null if not used in the import * @dataProvider user_identifier_provider */ public function test_update_elis_user_student_enrolment_with_all_fields($actioncreate, $actiondelete, $username, $email, $idnumber) { 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_config('noemailever', true); $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(); $student = new student(array('userid' => $user->id, 'classid' => $class->id, 'enrolmenttime' => 0)); $student->save(); // Validate setup. $this->assertTrue($DB->record_exists(student::TABLE, array('userid' => $user->id, 'classid' => $class->id))); // Run the student enrolment update action. $record = new stdClass(); $record->context = 'class_testclassidnumber'; if ($username != null) { $record->user_username = $user->username; } if ($email != null) { $record->user_email = $user->email; } if ($idnumber != null) { $record->user_idnumber = $user->idnumber; } $record->enrolmenttime = 'Jan/01/2012'; $record->completetime = 'Feb/01/2012'; $record->completestatusid = student::STUSTATUS_PASSED; $record->grade = 80; $record->credits = 3; $record->locked = 1; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->class_enrolment_update($record, 'bogus', 'testclassidnumber'); // Validation. $this->assertTrue($DB->record_exists(student::TABLE, array('userid' => $user->id, 'classid' => $class->id, 'enrolmenttime' => rlip_timestamp(0, 0, 0, 1, 1, 2012), 'completetime' => rlip_timestamp(0, 0, 0, 2, 1, 2012), 'completestatusid' => student::STUSTATUS_PASSED, 'grade' => 80, 'credits' => 3, 'locked' => 1))); }
/** * Test update track import. */ public function test_update_elis_track_import() { global $DB; $this->run_core_program_import(array(), true); $this->assertTrue($DB->record_exists(curriculum::TABLE, array('idnumber' => 'testprogramid'))); $this->run_core_track_import(array(), true); $data = array('action' => 'update', 'context' => 'track', 'idnumber' => 'testtrackid', 'name' => 'testtrackpdated', 'description' => 'testdescriptionupdated', 'startdate' => 'Jan/01/2012', 'enddate' => 'Jan/01/2012'); $this->run_core_track_import($data, false); unset($data['action'], $data['context'], $data['description']); $data['startdate'] = rlip_timestamp(0, 0, 0, 1, 1, 2012); $data['enddate'] = rlip_timestamp(0, 0, 0, 1, 1, 2012); $this->assertTrue($DB->record_exists(track::TABLE, $data)); }
/** * Validate that create actions are converted to updates for a student and instructor enrolment when the * "createorupdate" flag is enabled. * * This test first enrols a user as a student within a class and then updates the enrolment completion date and * then attempts to enrol the same user as an instructor within a the same class and then update that enrolment * completion date. In all four cases, the action is specified as a "create". */ public function test_elis_createorupdate_updates_student_and_instructor_enrolment() { 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/instructor.class.php'; require_once $CFG->dirroot . '/local/elisprogram/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' => 'testclassidnumber')); $class->save(); // Create the test user. $user = new user(array('username' => 'testuserusername', 'email' => '*****@*****.**', 'idnumber' => 'testuseridnumber', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'country' => 'CA')); $user->save(); $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); // 1 Run the first student enrolment. $record = new stdClass(); $record->action = 'create'; $record->context = 'class_testclassidnumber'; $record->user_username = '******'; $record->role = 'student'; $record->completetime = 'Jan/01/2012'; // NOTE: we clone() the record because the createorupdate setting will rewrite the action parameter. $importplugin->process_record('enrolment', clone $record, 'bogus'); // Validation of the enrolment record. $params = array('classid' => $class->id, 'userid' => $user->id, 'completetime' => rlip_timestamp(0, 0, 0, 1, 1, 2012)); $this->assertTrue($DB->record_exists(student::TABLE, $params)); // 2 Run the second student enrolment. $record->completetime = 'Jan/02/2012'; $importplugin->process_record('enrolment', clone $record, 'bogus'); $params['completetime'] = rlip_timestamp(0, 0, 0, 1, 2, 2012); $this->assertTrue($DB->record_exists(student::TABLE, $params)); // 3 Run the first teacher enrolment. $record->role = 'instructor'; $record->completetime = 'Jan/01/2012'; $importplugin->process_record('enrolment', clone $record, 'bogus'); $params['completetime'] = rlip_timestamp(0, 0, 0, 1, 1, 2012); $this->assertTrue($DB->record_exists(instructor::TABLE, $params)); // 4 Run the second teacher enrolment. $record->completetime = 'Jan/02/2012'; $importplugin->process_record('enrolment', clone $record, 'bogus'); $params['completetime'] = rlip_timestamp(0, 0, 0, 1, 2, 2012); $this->assertTrue($DB->record_exists(instructor::TABLE, $params)); }
/** * Validate that the old date format works as expected */ public function test_version1importparsesvalidlegacydate() { $plugin = new rlip_importplugin_version1(); $timestamp = $plugin->parse_date('01/02/2010'); $this->assertEquals($timestamp, rlip_timestamp(0, 0, 0, 1, 2, 2010)); }
/** * Validate that setting profile fields works on user update */ public function test_version1importsetsuserprofilefieldsonupdate() { global $CFG, $DB; // Perform default "user create" import. $this->run_core_user_import(array()); // Create custom field category. $category = new stdClass(); $category->sortorder = $DB->count_records('user_info_category') + 1; $category->id = $DB->insert_record('user_info_category', $category); // Create custom profile fields. $this->create_profile_field('rlipcheckbox', 'checkbox', $category->id); $this->create_profile_field('rlipdatetime', 'datetime', $category->id, 2000, 3000); $this->create_profile_field('rliplegacydatetime', 'datetime', $category->id, 2000, 3000); $this->create_profile_field('rlipmenu', 'menu', $category->id, "rlipoption1\nrlipoption2"); $this->create_profile_field('rliptextarea', 'textarea', $category->id); $this->create_profile_field('rliptext', 'text', $category->id); // Run import. $data = array(); $data['action'] = 'update'; $data['username'] = '******'; $data['profile_field_rlipcheckbox'] = '1'; $data['profile_field_rlipdatetime'] = 'jan/12/2011'; $data['profile_field_rliplegacydatetime'] = '1/12/2011'; $data['profile_field_rlipmenu'] = 'rlipoption1'; $data['profile_field_rliptextarea'] = 'rliptextarea'; $data['profile_field_rliptext'] = 'rliptext'; $this->run_core_user_import($data, false); // Fetch the user and their profile field data. $user = $DB->get_record('user', array('username' => 'rlipusername', 'mnethostid' => $CFG->mnet_localhost_id)); profile_load_data($user); fix_moodle_profile_fields($user); // Validate data. $this->assertEquals(isset($user->profile_field_rlipcheckbox), true); $this->assertEquals($user->profile_field_rlipcheckbox, 1); $this->assertEquals(isset($user->profile_field_rlipdatetime), true); $this->assertEquals($user->profile_field_rlipdatetime, rlip_timestamp(0, 0, 0, 1, 12, 2011)); $this->assertEquals(isset($user->profile_field_rliplegacydatetime), true); $this->assertEquals($user->profile_field_rliplegacydatetime, rlip_timestamp(0, 0, 0, 1, 12, 2011)); $this->assertEquals(isset($user->profile_field_rlipmenu), true); $this->assertEquals($user->profile_field_rlipmenu, 'rlipoption1'); $this->assertEquals(isset($user->profile_field_rliptextarea['text']), true); $this->assertEquals($user->profile_field_rliptextarea['text'], 'rliptextarea'); $this->assertEquals(isset($user->profile_field_rliptext), true); $this->assertEquals($user->profile_field_rliptext, 'rliptext'); }
/** * Validate that multivalue functionality is only used for "menu of choices" UI types * * @dataProvider ui_type_provider * @param string $uitype The string value representing a UI type * @param string $data The value provided for that field * @param string $expected The expected stored value * @param int $maxlength The max length for data input */ public function test_multivalue_functionality_only_used_for_menu_of_choices($uitype, $data, $expected, $maxlength, $inctime) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elis::lib('data/customfield.class.php'); require_once elispm::lib('data/user.class.php'); // Set up the custom field, category, context association, and owner. $fieldid = $this->create_test_field('user', 'char', $uitype, true, null, $maxlength, $inctime); if ($uitype === 'datetime') { $expected[0] = rlip_timestamp($expected[0][0], $expected[0][1], $expected[0][2], $expected[0][3], $expected[0][4], $expected[0][5]); } // Reset the field list. $temp = new user(); $temp->reset_custom_field_list(); // Run the entity create action. $record = array('action' => 'create', 'idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'password' => 'testuserpassword', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'city' => 'testusercity', 'country' => 'CA', 'testfieldshortname' => $data); $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('user', (object) $record, 'bogus'); // Validation. $this->assert_field_values('user', user::TABLE, field_data_char::TABLE, $fieldid, $expected); }
/** * Validate that mappings are applied during the instructor enrolment update action */ public function test_mapping_applied_during_instructor_enrolment_update() { 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 update action. $record = new stdClass(); $record->customaction = 'update'; $record->customcontext = 'class_testclassidnumber'; $record->customuser_username = '******'; $record->customuser_email = '*****@*****.**'; $record->customuser_idnumber = 'testuseridnumber'; $record->customassigntime = 'Jan/02/2012'; $record->customcompletetime = 'Jan/02/2012'; $record->customrole = 'instructor'; $this->run_enrolment_import((array) $record); // Validation. $this->assertTrue($DB->record_exists(instructor::TABLE, array('classid' => $pmclass->id, 'userid' => $userid, 'assigntime' => rlip_timestamp(0, 0, 0, 1, 2, 2012), 'completetime' => rlip_timestamp(0, 0, 0, 1, 2, 2012)))); }
/** * Performs class_enrolment 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_enrolment_create(array $data) { global $USER, $DB; if (static::require_elis_dependencies() !== true) { throw new moodle_exception('ws_function_requires_elis', 'local_datahub'); } static $completestatuses = array('not completed' => STUSTATUS_NOTCOMPLETE, 'not_completed' => STUSTATUS_NOTCOMPLETE, 'notcompleted' => STUSTATUS_NOTCOMPLETE, 'failed' => 'STUSTATUS_FAILED', 'passed' => STUSTATUS_PASSED); // Parameter validation. $params = self::validate_parameters(self::class_enrolment_create_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Parse class if (empty($data->class_idnumber) || !($classid = $DB->get_field(pmclass::TABLE, 'id', array('idnumber' => $data->class_idnumber)))) { throw new data_object_exception('ws_class_enrolment_create_fail_invalid_class', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:class_enrol', \local_elisprogram\context\pmclass::instance($classid)); // Initialize version1elis importplugin for utility functions. $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $userparams = array(); $userid = $importplugin->get_userid_from_record($data, '', $userparams); if ($userid == false) { $a = new stdClass(); if (empty($userparams)) { $a->userparams = '{empty}'; } else { $a->userparams = ''; foreach ($userparams as $userfield => $uservalue) { $subfield = strpos($userfield, '_'); $userfield = substr($userfield, $subfield === false ? 0 : $subfield + 1); if (!empty($a->userparams)) { $a->userparams .= ', '; } $a->userparams .= "{$userfield}: '{$uservalue}'"; } } throw new data_object_exception('ws_class_enrolment_create_fail_invalid_user', 'local_datahub', '', $a); } $record = new stdClass(); $record->userid = $userid; $record->classid = $classid; $record->completestatusid = STUSTATUS_NOTCOMPLETE; if (isset($data->completestatus)) { $completestatus = strtolower($data->completestatus); if (isset($completestatuses[$completestatus])) { $record->completestatusid = $completestatuses[$completestatus]; } else { throw new data_object_exception('ws_class_enrolment_create_fail_invalid_completestatus', 'local_datahub', '', $data); } } $record->grade = isset($data->grade) && is_numeric($data->grade) ? $data->grade : 0; $record->credits = isset($data->credits) && is_numeric($data->credits) ? $data->credits : 0; $record->locked = !empty($data->locked) ? 1 : 0; if (isset($data->enrolmenttime)) { $enrolmenttime = $importplugin->parse_date($data->enrolmenttime); if (empty($enrolmenttime)) { throw new data_object_exception('ws_class_enrolment_create_fail_invalid_enrolmenttime', 'local_datahub', '', $data); } else { $record->enrolmenttime = $enrolmenttime; } } else { $record->enrolmenttime = rlip_timestamp(); } $record->completetime = 0; if (isset($data->completetime)) { $completetime = $importplugin->parse_date($data->completetime); if (empty($completetime)) { throw new data_object_exception('ws_class_enrolment_create_fail_invalid_completetime', 'local_datahub', '', $data); } else { $record->completetime = $completetime; } } else { if (!empty($record->completestatusid)) { $record->completetime = rlip_timestamp(); } } $stu = new student($record); $stu->save(); // Respond. if (!empty($stu->id)) { return array('messagecode' => get_string('ws_class_enrolment_create_success_code', 'local_datahub'), 'message' => get_string('ws_class_enrolment_create_success_msg', 'local_datahub'), 'record' => $stu->to_array()); } else { throw new data_object_exception('ws_class_enrolment_create_fail', 'local_datahub'); } }
/** * Validate that log retrieval respects the start time filter */ public function test_getlogsrespectsactualstarttimefilter() { global $CFG; require_once $CFG->dirroot . '/local/datahub/lib.php'; // Create a test user. $this->create_test_user(); // Calculate the start time of the current day. $starttime = rlip_timestamp(0, 0, 0); // Create log records. $this->create_db_log(array('starttime' => 0)); $this->create_db_log(array('starttime' => $starttime + 12 * HOURSECS)); // Validate count when filtering for a particular day. $where = 'starttime >= :param0 AND starttime < :param1'; $params = array('param0' => $starttime, 'param1' => $starttime + DAYSECS); $logs = rlip_get_logs($where, $params); $count = 0; foreach ($logs as $log) { $count++; } $this->assertEquals($count, 1); }
/** * Converts a date in MMM/DD/YYYY format * to a unix timestamp * @todo: consider further generalizing / moving to base class * * @param string $date Date in MMM/DD/YYYY format * @return mixed The unix timestamp, or false if date is * not in the right format */ function parse_date($date) { global $CFG; require_once $CFG->dirroot . '/local/datahub/lib.php'; //make sure there are three parts $parts = explode('/', $date); if (count($parts) != 3) { return false; } //make sure the month is valid $month = $parts[0]; $day = $parts[1]; $year = $parts[2]; $months = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'); $pos = array_search(strtolower($month), $months); if ($pos === false) { //legacy format (zero values handled below by checkdate) $month = (int) $month; } else { //new "text" format $month = $pos + 1; } //make sure the combination of date components is valid $day = (int) $day; $year = (int) $year; if (!checkdate($month, $day, $year)) { //invalid combination of month, day and year return false; } // return unix timestamp return rlip_timestamp(0, 0, 0, $month, $day, $year); }