/** * Performs track_enrolment deletion * @throws moodle_exception If there was an error in passed parameters. * @throws data_object_exception If there was an error deleting the association. * @param array $data The incoming data parameter. * @return array An array of parameters, if successful. */ public static function track_enrolment_delete(array $data) { global $DB, $USER; if (static::require_elis_dependencies() !== true) { throw new moodle_exception('ws_function_requires_elis', 'local_datahub'); } // Parameter validation. $params = self::validate_parameters(self::track_enrolment_delete_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Parse track. if (empty($data->track_idnumber) || !($trackid = $DB->get_field(track::TABLE, 'id', array('idnumber' => $data->track_idnumber)))) { throw new data_object_exception('ws_track_enrolment_delete_fail_invalid_track', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:track_enrol', \local_elisprogram\context\track::instance($trackid)); // 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_track_enrolment_delete_fail_invalid_user', 'local_datahub', '', $a); } $retval = $DB->get_record(usertrack::TABLE, array('userid' => $userid, 'trackid' => $trackid)); // Respond. if (!empty($retval->id)) { $usertrack = new usertrack($retval->id); $usertrack->unenrol(); return array('messagecode' => get_string('ws_track_enrolment_delete_success_code', 'local_datahub'), 'message' => get_string('ws_track_enrolment_delete_success_msg', 'local_datahub')); } else { throw new data_object_exception('ws_track_enrolment_delete_fail', 'local_datahub'); } }
/** * Test available table only shows tracks that the assigner has the local/elisprogram::associate permission on. * @dataProvider dataprovider_available_permissions_associate * @param array $contextstoassign An array of information specifying the contexts to assign the associate permission on. * This is formatted like array('system' => true, 'track' => array(1, 2, 3)) * @param int $tableusersetid The ID of the userset we're going to manage. * @param array $expectedresults The expected page of results. * @param int $expectedtotal The expected number of total results. */ public function test_available_permissions_associate($contextstoassign, $tableusersetid, $expectedresults, $expectedtotal) { global $USER, $DB, $CFG; $userbackup = $USER; // Set up permissions. $USER = $this->setup_permissions_test(); // Set up capabilities. foreach ($contextstoassign as $contexttype => $ids) { if ($contexttype === 'system') { $this->give_permission_for_context($USER->id, 'local/elisprogram:associate', context_system::instance()); } else { foreach ($ids as $contextinstanceid) { switch ($contexttype) { case 'track': $context = \local_elisprogram\context\track::instance($contextinstanceid); break; } $this->give_permission_for_context($USER->id, 'local/elisprogram:associate', $context); } } } accesslib_clear_all_caches(true); // Construct test table. $table = new deepsight_datatable_usersettrack_available_mock($DB, 'test', 'http://localhost', 'testuniqid'); $table->set_usersetid($tableusersetid); // Perform test. $actualresults = $table->get_search_results(array(), array(), 0, 20); // Verify result. $this->assert_search_results($expectedresults, $expectedtotal, $actualresults); // Restore user. $USER = $userbackup; }
/** * Test track capability check. */ public function test_trackcapabilitycheck() { global $DB, $USER; $role = $DB->get_record('role', array('shortname' => 'editingteacher')); // Assign the test user the editing teacher role on a test track. $ctx = \local_elisprogram\context\track::instance($this->ttrackid); $this->assertNotEmpty(role_assign($role->id, $this->mdluserid, $ctx->id)); load_role_access_by_context($role->id, $ctx, $USER->access); // We need to force the accesslib cache to refresh. // Validate the return value when looking at the 'track' level. $contextstrack = new pm_context_set(); $contextstrack->contexts = array('track' => array($this->ttrackid)); $contextstrack->contextlevel = 'track'; $contexts = pm_context_set::for_user_with_capability('track', 'local/elisprogram:userset_enrol_userset_user', $this->mdluserid); $this->assertEquals($contextstrack, $contexts); // Validate the return value when looking at the 'class' level. $contextsclass = new pm_context_set(); $contextsclass->contexts = array('track' => array($this->ttrackid)); $contextsclass->contextlevel = 'class'; $perm = 'local/elisprogram:userset_enrol_userset_user'; $contexts = pm_context_set::for_user_with_capability('class', $perm, $this->mdluserid); $this->assertEquals($contextsclass, $contexts); // Validate checking for users with the given capability on this context. $users = pm_get_users_by_capability('track', $this->ttrackid, 'local/elisprogram:userset_enrol_userset_user'); $this->assertEquals($this->mdluserid, current($users)->id); }
/** * ELIS-4746: Test for assigning a user a role on a track context */ public function test_assignuserfortrackctx() { global $DB; // Get role to assign (we'll just take the first one returned). $rolesctx = $DB->get_records('role_context_levels', array('contextlevel' => CONTEXT_ELIS_TRACK)); foreach ($rolesctx as $rolectx) { $roleid = $rolectx->roleid; break; } // Get user to assign role. $user = new user(103); $muser = $user->get_moodleuser(); // Get specific context. $trk = new track(1); $context = \local_elisprogram\context\track::instance($trk->id); // Assign role. $this->assertGreaterThan(0, role_assign($roleid, $muser->id, $context->id)); }
/** * Performs track delete * @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_delete(array $data) { global $USER, $DB; if (static::require_elis_dependencies() !== true) { throw new moodle_exception('ws_function_requires_elis', 'local_datahub'); } // Parameter validation. $params = self::validate_parameters(self::track_delete_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Validate if (empty($data->idnumber) || !($trkid = $DB->get_field(track::TABLE, 'id', array('idnumber' => $data->idnumber)))) { throw new data_object_exception('ws_track_delete_fail_invalid_idnumber', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:track_delete', \local_elisprogram\context\track::instance($trkid)); $track = new track($trkid); $track->delete(); // Verify deletion & respond. if (!$DB->record_exists(track::TABLE, array('idnumber' => $data->idnumber))) { return array('messagecode' => get_string('ws_track_delete_success_code', 'local_datahub'), 'message' => get_string('ws_track_delete_success_msg', 'local_datahub')); } else { throw new data_object_exception('ws_track_delete_fail', 'local_datahub'); } }
/** * Performs track update * @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_update(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_update_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $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->idnumber) || !($trkid = $DB->get_field(track::TABLE, 'id', array('idnumber' => $data->idnumber)))) { throw new data_object_exception('ws_track_update_fail_invalid_idnumber', 'local_datahub', '', $data); } unset($record->idnumber); // Capability checking. require_capability('local/elisprogram:track_edit', \local_elisprogram\context\track::instance($trkid)); if (isset($data->startdate)) { $startdate = $importplugin->parse_date($data->startdate); if (empty($startdate)) { throw new data_object_exception('ws_track_update_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_update_fail_invalid_enddate', 'local_datahub', '', $data); } else { $record->enddate = $enddate; } } $track = new track($trkid); $track->load(); $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_update_success_code', 'local_datahub'), 'message' => get_string('ws_track_update_success_msg', 'local_datahub'), 'record' => array_merge($trackrec, $trackobj)); } else { throw new data_object_exception('ws_track_update_fail', 'local_datahub'); } }
/** * Removes all associations with a track, this entails removing * user track, cluster track and class track associations * @param none * @return none */ function delete() { // Cascade //clean make the delete cascade into association records $filter = new field_filter('trackid', $this->id); usertrack::delete_records($filter, $this->_db); clustertrack::delete_records($filter, $this->_db); trackassignment::delete_records($filter, $this->_db); parent::delete(); //Delete this leve's context $context = \local_elisprogram\context\track::instance($this->id); $context->delete(); }
/** * Test that a new Track context instance can be created and saved to the database. */ public function test_trackcontext() { $program = $this->initprogram(); $program->save(); $newobj = $this->inittrack($program->id); $newobj->save(); $context = \local_elisprogram\context\track::instance($newobj->id); // Validate that a context record was actually created with correct values. $this->assertGreaterThan(0, $context->id); $this->assertEquals(CONTEXT_ELIS_TRACK, $context->contextlevel); $this->assertEquals($newobj->id, $context->instanceid); // Create the context path to validate this in the returned context object. $pctx = \local_elisprogram\context\program::instance($program->id); $path = '/' . SYSCONTEXTID . '/' . $pctx->id . '/' . $context->id; $this->assertEquals($path, $context->path); $this->assertEquals(substr_count($path, '/'), $context->depth); }
/** * Create and update track 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_track_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/track.class.php'); 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]); } $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $record = new stdClass(); $record->action = 'create'; $record->context = 'curriculum'; $record->idnumber = 'testprogramid'; $record->name = 'testprogram'; $importplugin->curriculum_create($record, 'bogus'); $fieldid = $this->create_test_field($name, $datatype, $control, $inctime, $maxlength, $options, CONTEXT_ELIS_TRACK); $record = new stdClass(); $record->action = 'create'; $record->context = 'track'; $record->name = 'testtrack'; $record->idnumber = 'testtrackid'; $record->assignment = 'testprogramid'; $record->{$name} = $data; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('course', (object) $record, 'bogus'); $trackcontext = \local_elisprogram\context\track::instance($DB->get_field(track::TABLE, 'id', array('idnumber' => 'testtrackid'))); $this->assert_field_values($datatype, $control, $fieldid, $trackcontext->id, $expected); // Update. $record = new stdClass(); $record->action = 'update'; $record->context = 'track'; $record->idnumber = 'testtrackid'; $record->{$name} = $updateddata; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('course', (object) $record, 'bogus'); $this->assert_field_values($datatype, $control, $fieldid, $trackcontext->id, $updateexpected); }
/** * Test creating a new track entity with a default role assignment defined. */ public function test_createtrackwithdefaultroleassignment() { global $DB, $USER; list($rcid, $reid) = $this->create_roles('track'); // Setup the editor role to be the default role for the track context. elis::$config->local_elisprogram->default_track_role_id = $reid; $sysctx = context_system::instance(); // Assign the test user the creator role. role_assign($rcid, $USER->id, $sysctx->id); // Create a new track entity. $data = array('curid' => '1', 'idnumber' => 'track100', 'name' => 'track100', 'description' => 'track100'); $obj = new track($data); $obj->save(); // Initialize a new track management page and invoke the code that handles default role assignments. $page = new trackpage(); $page->after_cm_entity_add($obj); $trackctx = \local_elisprogram\context\track::instance($obj->id); $params = array('roleid' => $reid, 'userid' => $USER->id, 'contextid' => $trackctx->id); $this->assertTrue($DB->record_exists('role_assignments', $params)); }
/** * Validate that mappings are applied during the track update action */ public function test_mapping_applied_during_track_update() { global $CFG, $DB; 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/curriculum.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/track.class.php'; $this->init_mapping(); $customfieldid = $this->create_custom_field(CONTEXT_ELIS_TRACK); $program = new curriculum(array('idnumber' => 'testprogramidnumber')); $program->save(); $track = new track(array('curid' => $program->id, 'idnumber' => 'testtrackidnumber')); $track->save(); // Run the track update action. $record = new stdClass(); $record->customaction = 'update'; $record->customcontext = 'track'; $record->customidnumber = 'testtrackidnumber'; $record->customname = 'updatedtesttrackname'; $record->customdescription = 'updatedtesttrackdescription'; $record->customstartdate = 'Jan/02/2012'; $record->customenddate = 'Jan/02/2012'; $record->customtestfieldshortname = '1'; $this->run_pmentity_import((array) $record); // Validation. $data = array('idnumber' => 'testtrackidnumber', 'name' => 'updatedtesttrackname', 'startdate' => rlip_timestamp(0, 0, 0, 1, 2, 2012), 'enddate' => rlip_timestamp(0, 0, 0, 1, 2, 2012)); $this->assertTrue($DB->record_exists(track::TABLE, $data)); $record = $DB->get_record(track::TABLE, array('idnumber' => 'testtrackidnumber')); $this->assertEquals('updatedtesttrackdescription', $record->description); $instance = \local_elisprogram\context\track::instance(1); $this->assertTrue($DB->record_exists(field_data_int::TABLE, array('fieldid' => $customfieldid, 'contextid' => $instance->id, 'data' => 1))); }
/** * Test the get_child_contexts() function of \local_elisprogram\context\track */ public function test_track_child_contexts() { $this->fixture_program_track(); $ctx = \local_elisprogram\context\track::instance(1); $children = $ctx->get_child_contexts(); $count = 0; foreach ($children as $child) { $count++; } $this->assertEquals(0, $count); }
/** * Test available table shows correct search results based on local/elisprogram:track_enrol perms. * * @dataProvider dataprovider_available_track_enrol_perms * @param array $permcontexts An array of context objects for which to assign the local/elisprogram:track_enrol permission. * @param int $tabletrackid The ID of the track to use for the table. * @param array $expectedresults An array of expected search results. * @param int $expectedtotal The expected total number of search results. */ public function test_available_track_enrol_perms($permcontexts, $tabletrackid, $expectedresults, $expectedtotal) { global $USER, $DB, $CFG; $userbackup = $USER; $USER = $this->setup_permissions_test(); // Set up permissions. foreach ($permcontexts as $level => $id) { $context = null; switch ($level) { case 'system': $permcontext = context_system::instance(); break; case 'program': $permcontext = \local_elisprogram\context\program::instance($id); break; case 'track': $permcontext = \local_elisprogram\context\track::instance($id); break; } $this->give_permission_for_context($USER->id, 'local/elisprogram:track_enrol', $permcontext); } // Construct test table. $table = new deepsight_datatable_trackuser_available_mock($DB, 'test', 'http://localhost', 'testuniqid'); $table->set_trackid($tabletrackid); // Perform test. $actualresults = $table->get_search_results(array(), array(), 0, 20); // Verify result. $this->assert_search_results($expectedresults, $expectedtotal, $actualresults); // Restore user. $USER = $userbackup; }
/** * Test available table shows only tracks the user has permission to enrol into based on the * local/elisprogram:track_enrol permissions on the track. * * @dataProvider dataprovider_available_permissions_track_enrol * @param array $trackstoallow An array of track IDs to assign the local/elisprogram:track_enrol permission at. * @param int $tableuserid The ID of the user we're managing. * @param array $expectedresults The expected page of results. * @param int $expectedtotal The expected number of total results. */ public function test_available_permissions_track_enrol($trackstoallow, $tableuserid, $expectedresults, $expectedtotal) { global $USER, $DB, $CFG; $userbackup = $USER; // Set up permissions. $USER = $this->setup_permissions_test(); foreach ($trackstoallow as $trackid) { $this->give_permission_for_context($USER->id, 'local/elisprogram:track_enrol', \local_elisprogram\context\track::instance($trackid)); } // Construct test table. $table = new deepsight_datatable_usertrack_available_mock($DB, 'test', 'http://localhost', 'testuniqid'); $table->set_userid($tableuserid); // Perform test. $actualresults = $table->get_search_results(array(), array(), 0, 20); // Verify result. $this->assert_search_results($expectedresults, $expectedtotal, $actualresults); // Restore user. $USER = $userbackup; }
protected function get_context() { if (!isset($this->_context)) { $id = $this->required_param('id', PARAM_INT); $context_instance = \local_elisprogram\context\track::instance($id); $this->set_context($context_instance); } return $this->_context; }
/** * Hook that gets called after a CM entity is added through this page * (Note: this function should only use the id field from the supplied cm entity * as the rest of the data is not guaranteed to be there) * * @param object $cm_entity The CM entity added * @uses $DB * @uses $USER */ function after_cm_entity_add($cm_entity) { global $DB, $USER; //make sure a valid role is set if (!empty(elis::$config->local_elisprogram->default_track_role_id) && $DB->record_exists('role', array('id' => elis::$config->local_elisprogram->default_track_role_id))) { //get the context instance for capability checking $context_instance = \local_elisprogram\context\track::instance($cm_entity->id); //assign the appropriate role if the user does not have the edit capability if (!has_capability('local/elisprogram:track_edit', $context_instance)) { role_assign(elis::$config->local_elisprogram->default_track_role_id, $USER->id, $context_instance->id); } } }