/** * Determines whether the current plugin supports the supplied feature * * @param string $feature A feature description, either in the form * [entity] or [entity]_[action] * * @return mixed An array of actions for a supplied entity, an array of * required fields for a supplied action, or false on error */ function dhimport_version1_supports($feature) { global $CFG; require_once dirname(__FILE__) . '/version1.class.php'; $data_plugin = new rlip_importplugin_version1(); //delegate to class method return $data_plugin->plugin_supports($feature); }
/** * Helper function that runs the enrolment import for a sample user * * @param array $data Fields to set in the import */ private function run_core_enrolment_import($data) { global $CFG; $file = get_plugin_directory('dhimport', 'version1') . '/version1.class.php'; require_once $file; $provider = new rlipimport_version1_importprovider_createorupdateenrolment($data); $importplugin = new rlip_importplugin_version1($provider); $importplugin->run(); }
function general_validation_message($record, $message, $type) { //need the plugin class for some utility functions $file = get_plugin_directory('dhimport', 'version1') . '/version1.class.php'; require_once $file; // "action" is not always provided. In that case, return only the specific message if (empty($record->action)) { //missing action, general message will be fairly generic $type_display = ucfirst($type); return "{$type_display} could not be processed. {$message}"; return $message; } $msg = ""; if ($type == "enrolment") { if ($record->action != 'create' && $record->action != 'delete') { //invalid action return 'Enrolment could not be processed. ' . $message; } if (!$this->track_role_actions && !$this->track_enrolment_actions) { //error without sufficient information to properly provide details if ($record->action == 'create') { return 'Enrolment could not be created. ' . $message; } else { if ($record->action == 'delete') { return 'Enrolment could not be deleted. ' . $message; } } } //collect role assignment and enrolment messages $lines = array(); if ($this->track_role_actions) { //determine if a user identifier was set $user_identifier_set = !empty($record->username) || !empty($record->email) || !empty($record->idnumber); //determine if all required fields were set $required_fields_set = !empty($record->role) && $user_identifier_set && !empty($record->context); //list of contexts at which role assignments are allowed for specific instances $valid_contexts = array('coursecat', 'course', 'user'); //descriptive string for user and context $user_descriptor = rlip_importplugin_version1::get_user_descriptor($record); $context_descriptor = rlip_importplugin_version1::get_context_descriptor($record); switch ($record->action) { case "create": if ($required_fields_set && in_array($record->context, $valid_contexts) && !empty($record->instance)) { //assignment on a specific context $lines[] = "User with {$user_descriptor} could not be assigned role " . "with shortname \"{$record->role}\" on {$context_descriptor}."; } else { if ($required_fields_set && $record->context == 'system') { //assignment on the system context $lines[] = "User with {$user_descriptor} could not be assigned role " . "with shortname \"{$record->role}\" on the system context."; } else { //not valid $lines[] = "Role assignment could not be created."; } } break; case "delete": if ($required_fields_set && in_array($record->context, $valid_contexts) && !empty($record->instance)) { //unassignment from a specific context $lines[] = "User with {$user_descriptor} could not be unassigned role " . "with shortname \"{$record->role}\" on {$context_descriptor}."; } else { if ($required_fields_set && $record->context == 'system') { //unassignment from the system context $lines[] = "User with {$user_descriptor} could not be unassigned role " . "with shortname \"{$record->role}\" on the system context."; } else { //not valid $lines[] = "Role assignment could not be deleted. "; } } break; } } if ($this->track_enrolment_actions) { //determine if a user identifier was set $user_identifier_set = !empty($record->username) || !empty($record->email) || !empty($record->idnumber); //determine if some required field is missing $missing_required_field = !$user_identifier_set || empty($record->instance); //descriptive string for user $user_descriptor = rlip_importplugin_version1::get_user_descriptor($record); switch ($record->action) { case "create": if ($missing_required_field) { //required field missing, so use generic failure message $lines[] = "Enrolment could not be created."; } else { //more accurate failure message $lines[] = "User with {$user_descriptor} could not be enrolled in " . "course with shortname \"{$record->instance}\"."; } break; case "delete": if ($missing_required_field) { //required field missing, so use generic failure message $lines[] = "Enrolment could not be deleted."; } else { //more accurate failure message $lines[] = "User with {$user_descriptor} could not be unenrolled " . "from course with shortname \"{$record->instance}\"."; } break; } } //create combined message, potentially containing role assignment and //enrolment components $msg = implode(' ', $lines) . ' ' . $message; } if ($type == "course") { $type = ucfirst($type); switch ($record->action) { case "create": if (empty($record->shortname)) { $msg = "Course could not be created. " . $message; } else { $msg = "{$type} with shortname \"{$record->shortname}\" could not be created. " . $message; } break; case "update": if (empty($record->shortname)) { $msg = "Course could not be updated. " . $message; } else { $msg = "{$type} with shortname \"{$record->shortname}\" could not be updated. " . $message; } break; case "delete": if (empty($record->shortname)) { $msg = "Course could not be deleted. " . $message; } else { $msg = "{$type} with shortname \"{$record->shortname}\" could not be deleted. " . $message; } break; default: //invalid action $msg = 'Course could not be processed. ' . $message; break; } } if ($type == "user") { $type = ucfirst($type); switch ($record->action) { case "create": //make sure all required fields are specified if (empty($record->username) || empty($record->email)) { $msg = "User could not be created. " . $message; } else { $user_descriptor = rlip_importplugin_version1::get_user_descriptor($record); $msg = "{$type} with {$user_descriptor} could not be created. " . $message; } break; case "update": //make sure all required fields are specified if (empty($record->username) && empty($record->email) && empty($record->idnumber)) { $msg = "User could not be updated. " . $message; } else { $user_descriptor = rlip_importplugin_version1::get_user_descriptor($record); $msg = "{$type} with {$user_descriptor} could not be updated. " . $message; } break; case "delete": //make sure all required fields are specified if (empty($record->username) && empty($record->email) && empty($record->idnumber)) { $msg = "User could not be deleted. " . $message; } else { $user_descriptor = rlip_importplugin_version1::get_user_descriptor($record); $msg = "{$type} with {$user_descriptor} could not be deleted. " . $message; } break; default: //invalid action $msg = 'User could not be processed. ' . $message; break; } } return $msg; }
/** * Validate restoring default field mappings */ public function test_restoredefaultmappingupdatesrecords() { global $CFG, $DB; $file = get_plugin_directory('dhimport', 'version1') . '/lib.php'; require_once $file; // Obtain available fields. $plugin = new rlip_importplugin_version1(null, false); $options = $plugin->get_available_fields('user'); // Persist default field. rlipimport_version1_save_mapping('user', $options, array()); // Setup validation. $select = 'standardfieldname = customfieldname'; $count = $DB->count_records_select(RLIPIMPORT_VERSION1_MAPPING_TABLE, $select); $this->assertEquals($count, count($options)); // Update all mapping values. $data = array(); foreach ($options as $option) { $data[$option] = $option . 'updated'; } // Persist updated values and validate. rlipimport_version1_save_mapping('user', $options, $data); $select = 'standardfieldname != customfieldname'; $count = $DB->count_records_select(RLIPIMPORT_VERSION1_MAPPING_TABLE, $select); $this->assertEquals($count, count($options)); // Reset and validate state. rlipimport_version1_reset_mappings('user'); $select = 'standardfieldname = customfieldname'; $count = $DB->count_records_select(RLIPIMPORT_VERSION1_MAPPING_TABLE, $select); $this->assertEquals($count, count($options)); }
/** * Validate that field mapping does not use a field if its name should be * mapped to some other value */ public function test_version1importenrolmentfieldimportpreventsstandardfielduse() { global $CFG, $DB; $plugindir = get_plugin_directory('dhimport', 'version1'); require_once $plugindir . '/lib.php'; require_once $plugindir . '/version1.class.php'; // Create the mapping record. $record = new stdClass(); $record->entitytype = 'enrolment'; $record->standardfieldname = 'context'; $record->customfieldname = 'context2'; $DB->insert_record(RLIPIMPORT_VERSION1_MAPPING_TABLE, $record); // Get the import plugin set up. $data = array(); $provider = new rlipimport_version1_importprovider_mockenrolment($data); $importplugin = new rlip_importplugin_version1($provider); $importplugin->mappings = rlipimport_version1_get_mapping('enrolment'); // Transform a sample record. $record = new stdClass(); $record->context = 'course'; $record = $importplugin->apply_mapping('enrolment', $record); $DB->delete_records(RLIPIMPORT_VERSION1_MAPPING_TABLE); // Validate that the field was unset. $this->assertEquals(isset($record->context), false); }
require_login(); $context = context_system::instance(); require_capability('moodle/site:config', $context); $pluginwwwroot = str_replace($CFG->dirroot, $CFG->wwwroot, $plugindir); $baseurl = $pluginwwwroot . '/config_fields.php'; $tab = optional_param('tab', 'user', PARAM_CLEAN); //page header rlipimport_version1_page_setup($baseurl); echo $OUTPUT->header(); //tabs $tabs = rlipimport_version1_get_tabs($baseurl); print_tabs(array($tabs), $tab); //data from db $mappingdata = rlipimport_version1_get_mapping($tab); //options $plugin = new rlip_importplugin_version1(NULL, false); $options = $plugin->get_available_fields($tab); //body $form = new rlip_importfield_form(null, $options); //handle data submission if ($data = $form->get_data()) { if (isset($data->submitbutton)) { rlipimport_version1_save_mapping($tab, $options, $data); //notify to the user that settings were saved echo $OUTPUT->heading(get_string('mappingssaved', 'dhimport_version1')); } else { if (isset($data->reset)) { rlipimport_version1_reset_mappings($tab); redirect($baseurl . "?tab={$tab}&resetmessage=1", '', 0); } }
/** * Validate that DB logging records the correct number of successes and * failues from import file */ public function test_version1dblogginglogscorrectcountsformanualimport() { global $CFG, $DB; require_once $CFG->dirroot . '/local/datahub/lib.php'; $data = array(array('entity' => 'user', 'action' => 'create', 'username' => 'rlipusername', 'password' => 'Rlippassword!0', 'firstname' => 'rlipfirstname', 'lastname' => 'rliplastname', 'email' => '*****@*****.**', 'city' => 'rlipcity', 'country' => 'CA'), array('entity' => 'user', 'action' => 'create', 'username' => 'rlipusername2', 'password' => 'Rlippassword!0', 'firstname' => 'rlipfirstname2', 'lastname' => 'rliplastnam2e', 'email' => '*****@*****.**', 'city' => 'rlipcity', 'country' => 'boguscountry'), array('entity' => 'user', 'action' => 'create', 'username' => 'rlipusername3', 'password' => 'Rlippassword!0', 'firstname' => 'rlipfirstname3', 'lastname' => 'rliplastname3', 'email' => '*****@*****.**', 'city' => 'rlipcity', 'country' => 'boguscountry')); $provider = new rlipimport_version1_importprovider_multiuser($data); $importplugin = new rlip_importplugin_version1($provider); $result = $importplugin->run(); $this->assertNull($result); $exists = $DB->record_exists(RLIP_LOG_TABLE, array('filesuccesses' => 1, 'filefailures' => 2)); $this->assertEquals($exists, true); }
/** * Validate that invalid date combinations are validated (i.e. dates * where all parts are valid but the date is not possible) */ public function test_version1importvalidatesincorrectdatecombination() { $plugin = new rlip_importplugin_version1(); $timestamp = $plugin->parse_date('Feb/31/2010'); $this->assertFalse($timestamp); }
/** * Validate method get_userid_from_record * @param array $usersdata list of users w/ data to insert before test * @param array $inputdata the user import record * @param string $prefix the identifying field prefix (e.g. 'user_') * @param int $expected the matching user's id (false for none expected) * @dataProvider version1_get_userid_from_record_dataprovider */ public function test_version1_get_userid_from_record($usersdata, $inputdata, $prefix, $expected) { global $CFG, $DB; // Create users for test saving ids for later comparison $uids = array(false); foreach ($usersdata as $userdata) { if (!isset($userdata['mnethostid'])) { $userdata['mnethostid'] = $CFG->mnet_localhost_id; } $uids[] = $DB->insert_record('user', (object) $userdata); } $provider = new rlipimport_version1_importprovider_mockuser(array()); $importplugin = new rlip_importplugin_version1($provider); $importplugin->mappings = rlipimport_version1_get_mapping('user'); $importplugin->fslogger = $provider->get_fslogger('dhimport_version1', 'user'); $this->assertEquals($expected ? $uids[$expected] : false, $importplugin->get_userid_from_record((object) $inputdata, 'user.csv', $prefix)); }