/** * Test validation of entries with the same userid and clusterid but different plugin */ public function test_clusterassignment_validationallowsmultipleplugins() { $this->load_csv_data(); $clusterassignment = new clusterassignment(array('clusterid' => 1, 'userid' => 1, 'plugin' => 'moodleprofile')); $clusterassignment->save(); $this->assertEquals(1, 1); }
/** * Test custom userset theme assignment. */ public function test_customusersetthemectx() { $this->load_csv_data(); // ELIS user with the associated moodle user. $user = new user(103); $muser = $user->get_moodleuser(); // Userset with a custom theme. $userset = new userset(1); $userset->field__elis_userset_theme = 'formal_white'; $userset->field__elis_userset_themepriority = 1; $userset->save(); // Assign the user to the user set. $usersetassign = new clusterassignment(); $usersetassign->userid = $user->id; $usersetassign->clusterid = $userset->id; $usersetassign->save(); // Pretend to be that user. $this->setUser($muser->id); // Initialize page. $page = new moodle_page(); $page->initialise_theme_and_output(); // Assert we have our theme. $this->assertEquals('formal_white', $page->theme->name); $this->setUser(null); }
function cluster_manual_assign_user($clusterid, $userid, $autoenrol = true, $leader = false) { global $CFG; if (!is_numeric($clusterid) || !is_numeric($userid) || $clusterid <= 0 || $userid <= 0) { return false; } if (clusterassignment::exists(array(new field_filter('userid', $userid), new field_filter('clusterid', $clusterid), new field_filter('plugin', 'manual')))) { return true; } $record = new clusterassignment(); $record->clusterid = $clusterid; $record->userid = $userid; $record->plugin = 'manual'; $record->autoenrol = $autoenrol; $record->leader = $leader; $record->save(); return true; }
/** * Test successful userset enrolment deletion. */ public function test_success() { global $DB, $USER; $this->give_permissions(array('local/elisprogram:userset_enrol')); // Initialize version1elis importplugin for utility functions. $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); // Create test userset. $datagen = new elis_program_datagenerator($DB); $userset = $datagen->create_userset(array('name' => 'TestUsersetEnrolmentDelete')); $userid = $DB->get_field(user::TABLE, 'id', array('username' => 'assigninguser')); // Create the userset enrolment record to delete. $clstass = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $userid, 'plugin' => 'manual')); $clstass->save(); $data = array('userset_name' => $userset->name, 'user_username' => 'assigninguser', 'user_email' => '*****@*****.**'); $response = local_datahub_elis_userset_enrolment_delete::userset_enrolment_delete($data); $this->assertNotEmpty($response); $this->assertInternalType('array', $response); $this->assertArrayHasKey('messagecode', $response); $this->assertArrayHasKey('message', $response); $this->assertEquals(get_string('ws_userset_enrolment_delete_success_code', 'local_datahub'), $response['messagecode']); $this->assertEquals(get_string('ws_userset_enrolment_delete_success_msg', 'local_datahub'), $response['message']); $this->assertFalse($DB->record_exists(clusterassignment::TABLE, array('clusterid' => $userset->id, 'userid' => $userid, 'plugin' => 'manual'))); }
/** * Tests contexts in userset data object. * * Covers: * local/elisprogram/lib/data/userset.class.php:334 * local/elisprogram/lib/data/userset.class.php:453 * local/elisprogram/lib/data/userset.class.php:561 * local/elisprogram/lib/data/userset.class.php:595 * local/elisprogram/lib/data/userset.class.php:616 * local/elisprogram/lib/data/userset.class.php:721 * local/elisprogram/lib/data/userset.class.php:755 * local/elisprogram/lib/data/userset.class.php:847 * local/elisprogram/lib/data/userset.class.php:901 */ public function test_usersetcontexts() { global $USER, $DB; require_once elispm::file('plugins/usetclassify/usersetclassification.class.php'); require_once elispm::file('plugins/usetclassify/lib.php'); $this->setup_users(); $this->setup_usersets(); // TEST local/elisprogram/lib/data/userset.class.php:334. $res = userset::get_allowed_clusters(1); // TEST local/elisprogram/lib/data/userset.class.php:453. $ussfilter = new usersubset_filter('id', new field_filter('id', 1)); $res = $ussfilter->get_sql(); // TEST // local/elisprogram/lib/data/userset.class.php:561 // local/elisprogram/lib/data/userset.class.php:595 // local/elisprogram/lib/data/userset.class.php:616 // local/elisprogram/lib/data/userset.class.php:721 // local/elisprogram/lib/data/userset.class.php:755. $field = new field(array('shortname' => USERSET_CLASSIFICATION_FIELD)); $field->load(); $userset = $this->create_userset($field); // Get a role to assign. $rolesctx = $DB->get_records('role_context_levels', array('contextlevel' => CONTEXT_ELIS_USERSET)); foreach ($rolesctx as $i => $rolectx) { $roleid = $rolectx->roleid; } // Add userset_view capability to our role. $usersetcontext = \local_elisprogram\context\userset::instance($userset->id); $rc = new stdClass(); $rc->contextid = $usersetcontext->id; $rc->roleid = $roleid; $rc->capability = 'local/elisprogram:userset_view'; $rc->permission = 1; $rc->timemodified = time(); $rc->modifierid = 0; $DB->insert_record('role_capabilities', $rc); $rc = new stdClass(); $rc->contextid = $usersetcontext->id; $rc->roleid = $roleid; $rc->capability = 'local/elisprogram:userset_enrol_userset_user'; $rc->permission = 1; $rc->timemodified = time(); $rc->modifierid = 0; $DB->insert_record('role_capabilities', $rc); // Assign role. $user = new user(103); $muser = $user->get_moodleuser(); $raid = role_assign($roleid, $muser->id, $usersetcontext->id); $this->setUser(100); // Assign other user to userset. $clst = new clusterassignment(); $clst->clusterid = $userset->id; $clst->userid = 104; $clst->plugin = 'manual'; $clst->save(); // Get cluster listing. $capability = 'local/elisprogram:userset_view'; $contexts = get_contexts_by_capability_for_user('cluster', $capability, 100); $extrafilters = array('contexts' => $contexts, 'classification' => 'test field data'); $res = cluster_get_listing('name', 'ASC', 0, 0, '', '', $extrafilters, 104); $res = cluster_count_records('', '', $extrafilters); // TEST local/elisprogram/lib/data/userset.class.php:847. cluster_get_non_child_clusters(1); // TEST local/elisprogram/lib/data/userset.class.php:901. cluster_get_possible_sub_clusters(1); $this->setUser(null); }
/** * Enrol the test user in the provided context * * @param string $contextlevel The string descriptor of the context level * @param string $role The shortname of the import record's role column */ private function create_enrolment($contextlevel, $role) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; switch ($contextlevel) { case 'curriculum': // Program enrolment. require_once elispm::lib('data/curriculumstudent.class.php'); $data = array('curriculumid' => 1, 'userid' => 1); $curriculumstudent = new curriculumstudent($data); $curriculumstudent->save(); break; case 'track': // Track enrolment. require_once elispm::lib('data/usertrack.class.php'); $data = array('trackid' => 1, 'userid' => 1); $usertrack = new usertrack($data); $usertrack->save(); break; case 'cluster': // User set enrolment. require_once elispm::lib('data/clusterassignment.class.php'); $data = array('clusterid' => 1, 'userid' => 1); $clusterassignment = new clusterassignment($data); $clusterassignment->save(); break; case 'class': if ($role == 'instructor') { // Class instance instructor enrolment. require_once elispm::lib('data/instructor.class.php'); $data = array('classid' => 1, 'userid' => 1); $instructor = new instructor($data); $instructor->save(); } else { // Class instance student enrolment. require_once elispm::lib('data/student.class.php'); $data = array('classid' => 1, 'userid' => 1); $student = new student($data); $student->save(); } break; case 'user': // Moodle user role assignment. $roleid = $DB->get_field('role', 'id', array('shortname' => $role)); $userid = $DB->get_field('user', 'id', array('idnumber' => 'testuseridnumber')); $context = context_user::instance($userid); role_assign($roleid, $userid, $context->id); break; default: break; } }
/** * Validate that counting the number of role assignments on a particular * cluster for a particular role respects special userset permissions */ public function test_clusterrolepagecountroleusersrespectsusersetpermissions() { global $CFG, $USER, $DB; require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/userset.class.php'); accesslib_clear_all_caches(true); // Create a user record so that Moodle and PM ids don't match by fluke. set_config('auto_assign_user_idnumber', 0, 'local_elisprogram'); elis::$config = new elis_config(); create_user_record('bogususer', 'Bogususer!0'); // Create our test userset. $userset = new userset(array('name' => 'testuserset')); $userset->save(); // The user who is assigned to the user set. $assigneduser = new user(array('idnumber' => 'assigned', 'username' => 'assigned', 'firstname' => 'assigned', 'lastname' => 'assigned', 'email' => '*****@*****.**', 'country' => 'CA')); $assigneduser->save(); // Userset assignment. $clusterassignment = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $assigneduser->id)); $clusterassignment->save(); // User who is potentially assigning the userset member a new role within the userset. $assigninguser = new user(array('idnumber' => 'assigning', 'username' => 'assigning', 'firstname' => 'assigning', 'lastname' => 'assigning', 'email' => '*****@*****.**', 'country' => 'CA')); $assigninguser->save(); // Need the system context for role assignments. $systemcontext = context_system::instance(); // Set up the role that allows a user to assign roles but only to userset members. $permissionsroleid = create_role('permissionsrole', 'permissionsrole', 'permissionsrole'); // Enable the appropriate capabilities. assign_capability('moodle/role:assign', CAP_ALLOW, $permissionsroleid, $systemcontext->id); assign_capability('local/elisprogram:userset_role_assign_userset_users', CAP_ALLOW, $permissionsroleid, $systemcontext->id); // Perform the role assignment. $moodleuserid = $DB->get_field('user', 'id', array('username' => 'assigning')); role_assign($permissionsroleid, $moodleuserid, $systemcontext->id); // Imitate the user assigned the role which allows for further role assignments only on userset members. $USER = $DB->get_record('user', array('id' => $moodleuserid)); // Test role for potential assignment to userset members. $roleid = create_role('targetrole', 'targetrole', 'targetrole'); // Assign the both users to the userset role. $contextclass = \local_eliscore\context\helper::get_class_for_level(CONTEXT_ELIS_USERSET); $usersetcontext = $contextclass::instance($userset->id); role_assign($roleid, $moodleuserid, $usersetcontext->id); $moodleuserid = $DB->get_field('user', 'id', array('username' => 'assigned')); role_assign($roleid, $moodleuserid, $usersetcontext->id); // Obtain the count of assigned users. $page = new cluster_rolepage(array('id' => $userset->id)); $count = $page->count_role_users($roleid, $usersetcontext); // List should only contain the userset member. $this->assertEquals(1, $count); }
function cluster_assign_to_user($clusterid, $userid, $autoenrol = true, $leader = false) { if (!is_numeric($clusterid) || !is_numeric($userid) || $clusterid <= 0 || $userid <= 0) { // invalid data return false; } if (clusterassignment::exists(array(new field_filter('userid', $userid), new field_filter('clusterid', $clusterid)))) { // user already assigned return true; } $usass = new clusterassignment(); $usass->userid = $userid; $usass->clusterid = $clusterid; $usass->save(); events_trigger('cluster_assigned', $usass); return true; }
/** * Test available table shows only programs the user has permission to enrol into based on * local/elisprogram:program_enrol_userset_user permission on a parent userset. * * @dataProvider dataprovider_available_permissions_program_enrol_userset_user * @param array $usersetidsforperm An array of userset IDs to assign the local/elisprogram:program_enrol_userset_user on. * @param array $clusterassignments An array of arrays of parameters to construct clusterassignments with. * @param array $clustercurriculums An array of arrays of parameters to construct clustercurriculums with. * @param int $tableuserid The id of the user to manage associations for. * @param array $expectedresults The expected page of results. * @param int $expectedtotal The expected number of total results. */ public function test_available_permissions_program_enrol_userset_user($usersetidsforperm, $clusterassignments, $clustercurriculums, $tableuserid, $expectedresults, $expectedtotal) { global $USER, $DB, $CFG; $userbackup = $USER; // Import usersets. $dataset = $this->createCsvDataSet(array(userset::TABLE => elispm::file('tests/fixtures/deepsight_userset.csv'))); $this->loadDataSet($dataset); // Set up permissions. $USER = $this->setup_permissions_test(); // Initialize userset contexts. for ($i = 1; $i <= 5; $i++) { $ctx = \local_elisprogram\context\userset::instance($i); } accesslib_clear_all_caches(true); // Set up capabilities. $capability = 'local/elisprogram:program_enrol_userset_user'; foreach ($usersetidsforperm as $usersetid) { $this->give_permission_for_context($USER->id, $capability, \local_elisprogram\context\userset::instance($usersetid)); } // Create clusterassignments. foreach ($clusterassignments as $clusterassignment) { $clusterassignment = new clusterassignment($clusterassignment); $clusterassignment->save(); } // Create clustercurriculums. foreach ($clustercurriculums as $clustercurriculum) { $clustercurriculum = new clustercurriculum($clustercurriculum); $clustercurriculum->save(); } // Construct test table. $table = new deepsight_datatable_userprogram_available_mock($DB, 'test', 'http://localhost', 'testuniqid'); $table->set_userid($tableuserid); // Perform test. $actualresults = $table->get_search_results(array(), array(), 0, 20); // Verify. $this->assert_search_results($expectedresults, $expectedtotal, $actualresults); }
/** * Set up data that is needed for testing * * @param boolean $setcustomfielddata Specify whether the userset's custom fields should be set * @param boolean $assignuser Specify whether the user should be directly assigned to the user set * @param boolean $setautoassociatefields Specity whether we should set up fields that allow userset autoassociation */ private function set_up_required_data($setcustomfielddata = true, $assignuser = true, $setautoassociatefields = false) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elis::lib('data/customfield.class.php'); require_once elispm::file('accesslib.php'); require_once elispm::file('enrol/userset/moodleprofile/userset_profile.class.php'); require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/usermoodle.class.php'); require_once elispm::lib('data/userset.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(); $user->synchronize_moodle_user(); // We need a system-level role assignment. $roleid = create_role('systemrole', 'systemrole', 'systemrole'); $userid = $DB->get_field('user', 'id', array('username' => 'testuserusername')); $context = context_system::instance(); role_assign($roleid, $userid, $context->id); // Set up the userset. $userset = new userset(); $usersetdata = array('name' => 'testusersetname'); if ($setcustomfielddata) { $usersetdata['field_userset_group'] = 1; $usersetdata['field_userset_groupings'] = 1; } $userset->set_from_data((object) $usersetdata); $userset->save(); if ($setautoassociatefields) { // 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' => 1)); $usersetprofile->save(); } if ($assignuser) { // Assign the user to the user set. $clusterassignment = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $user->id, 'plugin' => 'manual')); $clusterassignment->save(); } }
/** * Test available table obeys track_enrol_userset_users permission. * * Test available table shows only users that are in clusters where: * - the assigner is also in the cluster * - the assigner has the local/elisprogram:track_enrol_userset_user permission * - the current track is associated with the cluster. * * @dataProvider dataprovider_available_permissions_track_enrol_userset_user * @param array $usersetidsforperm An array of userset IDs to assign the local/elisprogram:track_enrol_userset_user on. * @param array $clusterassignments An array of arrays of parameters to construct clusterassignments with. * @param array $clustertracks An array of arrays of parameters to construct clustertracks with. * @param int $tabletrackid The id of the track to manage associations for. * @param array $expectedresults The expected page of results. * @param int $expectedtotal The expected number of total results. */ public function test_available_permissions_track_enrol_userset_user($usersetidsforperm, $clusterassignments, $clustertracks, $tabletrackid, $expectedresults, $expectedtotal) { global $USER, $DB, $CFG; $userbackup = $USER; // Import usersets. $dataset = $this->createCsvDataSet(array(userset::TABLE => elispm::file('tests/fixtures/deepsight_userset.csv'))); $this->loadDataSet($dataset); // Set up permissions. $USER = $this->setup_permissions_test(); $capability = 'local/elisprogram:track_enrol_userset_user'; foreach ($usersetidsforperm as $usersetid) { $this->give_permission_for_context($USER->id, $capability, \local_elisprogram\context\userset::instance($usersetid)); } // Create clusterassignments. foreach ($clusterassignments as $clusterassignment) { $clusterassignment = new clusterassignment($clusterassignment); $clusterassignment->save(); } // Create clustertracks. foreach ($clustertracks as $clustertrack) { $clustertrack = new clustertrack($clustertrack); $clustertrack->save(); } // 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. $this->assert_search_results($expectedresults, $expectedtotal, $actualresults); // Restore user. $USER = $userbackup; }
/** * Test user with view and edit permissions on a userset. * This test only rund when local/elisprogram/setuplib.php exists. */ public function test_manual_field_is_view_or_editable_with_view_edit_permissions_on_userset() { global $DB, $CFG; // Skip test if local/elisprogram doesn't exist. if (!file_exists($CFG->dirroot . '/local/elisprogram/lib/setup.php')) { $this->markTestSkipped('Requires local/elisprogram to be installed.'); } $this->resetAfterTest(true); $this->load_libraries_for_additional_tests(); // Load CSV data. $this->load_csv_data(); // Setup place holders for capabilities. $editcap = 'local/elisprogram:user_edit'; $viewcap = 'local/elisprogram:user_view'; // Retrieve the PM user id to be assigned to a userset $param = array('id' => 103); $pmuserinusersetid = $DB->get_field('local_elisprogram_usr', 'id', $param); // Retrieve the user who will be assigned a role in the user set. $param = array('id' => 101); $userroleinuserset = $DB->get_record('user', $param); // Set user with role as logged in user $this->setUser($userroleinuserset); // Get the userset context. $usersetcontext = \local_elisprogram\context\userset::instance(1); // System context. $syscontext = context_system::instance(); // Create role and assign capabilites to it. $roleid = create_role('testrole', 'testrole', 'testrole'); assign_capability($editcap, CAP_ALLOW, $roleid, $syscontext->id); assign_capability($viewcap, CAP_ALLOW, $roleid, $syscontext->id); // Assin role to user in the userset context. role_assign($roleid, $userroleinuserset->id, $usersetcontext->id); // Add user to cluster/userset. $usersetassign = new clusterassignment(array('clusterid' => 1, 'userid' => $pmuserinusersetid, 'plugin' => 'manual')); $usersetassign->save(); $field = new field(array('id' => 101)); $field->load(); $result = manual_field_is_view_or_editable($field, $syscontext, $editcap, $viewcap, 'user', $pmuserinusersetid); $this->assertEquals(MANUAL_FIELD_EDITABLE, $result); }
/** * Validate that user unenrolment from user sets only happens for the manual * userset ("cluster") plugin */ public function test_elis_user_userset_unenrolment_respects_userset_plugin() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/userset.class.php'); $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); $userset = new userset(array('name' => 'testusersetname')); $userset->save(); $clusterassignment = new clusterassignment(array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'manual', 'autoenrol' => 0)); $clusterassignment->save(); $clusterassignment = new clusterassignment(array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'another', 'autoenrol' => 0)); $clusterassignment->save(); // Validate setup. $this->assertTrue($DB->record_exists(clusterassignment::TABLE, array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'manual', 'autoenrol' => 0))); $this->assertTrue($DB->record_exists(clusterassignment::TABLE, array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'another', 'autoenrol' => 0))); // Run the userset enrolment delete action. $record = new stdClass(); $record->context = 'cluster_testusersetname'; $record->user_username = '******'; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->cluster_enrolment_delete($record, 'bogus', 'testusersetname'); // Validation. $this->assertEquals(1, $DB->count_records(clusterassignment::TABLE)); $this->assertTrue($DB->record_exists(clusterassignment::TABLE, array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'another', 'autoenrol' => 0))); }
/** * Performs userset_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 userset_enrolment_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::userset_enrolment_create_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Parse userset if (empty($data->userset_name) || !($clstid = $DB->get_field(userset::TABLE, 'id', array('name' => $data->userset_name)))) { throw new data_object_exception('ws_userset_enrolment_create_fail_invalid_userset', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:userset_enrol', \local_elisprogram\context\userset::instance($clstid)); // 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_userset_enrolment_create_fail_invalid_user', 'local_datahub', '', $a); } $record = new stdClass(); $record->userid = $userid; $record->clusterid = $clstid; $record->plugin = 'manual'; if (!empty($data->plugin)) { $record->plugin = $data->plugin; } $record->leader = !empty($data->leader); $clstass = new clusterassignment($record); $clstass->save(); // Respond. if (!empty($clstass->id)) { return array('messagecode' => get_string('ws_userset_enrolment_create_success_code', 'local_datahub'), 'message' => get_string('ws_userset_enrolment_create_success_msg', 'local_datahub'), 'record' => $clstass->to_array()); } else { throw new data_object_exception('ws_userset_enrolment_create_fail', 'local_datahub'); } }
/** * Create a cluster (user set) enrolment * * @param object $record One record of import data * @param string $filename The import file name, used for logging * @param string $name The name of the cluster / user set * * @return boolean true on success, otherwise false */ function cluster_enrolment_create($record, $filename, $name) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/userset.class.php'); if (!($clusterid = $DB->get_field(userset::TABLE, 'id', array('name' => $name)))) { $this->fslogger->log_failure("instance value of \"{$name}\" does not refer to a valid instance of a user set 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(clusterassignment::TABLE, array('clusterid' => $clusterid, 'userid' => $userid))) { $this->fslogger->log_failure("User with {$user_descriptor} is already enrolled in user set \"{$name}\".", 0, $filename, $this->linenumber, $record, "enrolment"); return false; } if (!$this->validate_cluster_enrolment_data('create', $record, $filename)) { return false; } //obtain the cluster / userset id $clusterid = $DB->get_field(userset::TABLE, 'id', array('name' => $name)); //create the association $clusterassignment = new clusterassignment(array('userid' => $userid, 'clusterid' => $clusterid, 'plugin' => 'manual', 'autoenrol' => 0)); $clusterassignment->save(); //log success $success_message = "User with {$user_descriptor} successfully enrolled in user set \"{$name}\"."; $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber); return true; }
/** * Validate that the listing respects the local/elisprogram:track_enrol_userset_user * capability as long as the appropriate userset and track are associated to * one another and the target user is in the userset */ public function test_availableusersrespectsindirectusersetpermissions() { global $DB, $USER; $this->load_csv_data(); set_config('siteguest', ''); set_config('siteadmins', ''); accesslib_clear_all_caches_for_unit_testing(); // Create a test user to be in the userset. $usersetmember = new user(array('idnumber' => 'usersetmember', 'username' => 'usersetmember', 'firstname' => 'usersetmember', 'lastname' => 'usersetmember', 'email' => '*****@*****.**', 'country' => 'CA')); $usersetmember->save(); // Our test userset. $userset = new userset(array('name' => 'userset')); $userset->save(); // Assign the test user to the test userset. $clusterassignment = new clusterassignment(array('userid' => $usersetmember->id, 'clusterid' => $userset->id)); $clusterassignment->save(); // Assign the userset to our track. $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => 1)); $clustertrack->save(); // Set up a db record for the active user for permissions reasons. // (i.e. so they are not treated as an admin). $activeuser = new user(array('idnumber' => 'activeuser', 'username' => 'activeuser', 'firstname' => 'activeuser', 'lastname' => 'activeuser', 'email' => '*****@*****.**', 'country' => 'CA')); $activeuser->save(); // Set up our test role. $roleid = create_role('testrole', 'testrole', 'testrole'); $syscontext = context_system::instance(); assign_capability('local/elisprogram:track_enrol_userset_user', CAP_ALLOW, $roleid, $syscontext->id); // Perform the role necessary assignment. $moodleuser = $DB->get_record('user', array('username' => 'activeuser')); // Make sure all the contexts are created, so that we can find the children. $contextclass = \local_eliscore\context\helper::get_class_for_level(CONTEXT_ELIS_USERSET); $instance = $contextclass::instance($userset->id); role_assign($roleid, $moodleuser->id, $instance->id); // Assume the role of the user with the role assignment. $USER = $moodleuser; $usersrecset = usertrack::get_available_users(1); $users = array(); foreach ($usersrecset as $key => $user) { $users[$key] = $user; } unset($usersrecset); $this->assertEquals(1, count($users)); // Validate user. $this->assertArrayHasKey($usersetmember->id, $users); $user = $users[$usersetmember->id]; $this->assertEquals($usersetmember->username, 'usersetmember'); // Validate count. $count = usertrack::count_available_users(1); $this->assertEquals(1, $count); }
/** * Test the basic functionality of the methods for fetching users as a * recordset when applying userset permissions and an appropriate SQL filter */ public function test_usermanagementgetsusersrecordsetrespectsfilters() { global $USER, $DB; require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('lib.php'); // Make sure we don't hit corner-cases with permissions. set_config('siteguest', ''); set_config('siteadmins', ''); // Prevent accesslib caching. accesslib_clear_all_caches(true); // Data setup. $this->load_csv_data(); $this->set_up_users(); // Assign a second user to the user set. $secondclusteruser = new user(array('idnumber' => 'secondclusteruser', 'username' => 'secondclusteruser', 'firstname' => 'Secondcluster', 'lastname' => 'User', 'email' => '*****@*****.**', 'country' => 'CA')); $secondclusteruser->save(); $clusterassignment = new clusterassignment(array('clusterid' => 1, 'userid' => $secondclusteruser->id)); $clusterassignment->save(); // The context set our user set administrator has access to. $contextset = pm_context_set::for_user_with_capability('cluster', 'local/elisprogram:user_edit', $USER->id); // Add a filter to filter down to only our first test user. $extrasql = array('username = :testusername', array('testusername' => 'clusteruser')); // Validate record. $users = usermanagement_get_users_recordset('name', 'ASC', 0, 0, $extrasql, $contextset); $this->assertTrue($users->valid()); $user = $users->current(); $this->assertEquals('clusteruser', $user->idnumber); $this->assertNull($users->next()); }
/** * Validate that mappings are applied during the user set enrolment delete action */ public function test_mapping_applied_during_userset_enrolment_delete() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/clusterassignment.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/userset.class.php'; $this->init_mapping(); $userid = $this->create_test_user(); $userset = new userset(array('name' => 'testusersetname')); $userset->save(); $clusterassignment = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $userid, 'plugin' => 'manual')); $clusterassignment->save(); // Run the user set enrolment delete action. $record = new stdClass(); $record->customaction = 'delete'; $record->customcontext = 'cluster_testusersetname'; $record->customuser_username = '******'; $record->customuser_email = '*****@*****.**'; $record->customuser_idnumber = 'testuseridnumber'; $this->run_enrolment_import((array) $record); // Validation. $this->assertEquals(0, $DB->count_records(clusterassignment::TABLE)); }
/** * Test available table shows only usersets the user has permission to enrol into based on * local/elisprogram:userset_enrol_userset_user permission on a parent userset. * * @dataProvider dataprovider_available_permissions_userset_enrol_userset_user * @param array $usersetidsforperm An array of userset IDs to assign the local/elisprogram:userset_enrol_userset_user on. * @param array $clusterassignments An array of arrays of parameters to construct clusterassignments with. * @param int $tableuserid The id of the user to manage associations for. * @param array $expectedresults The expected page of results. * @param int $expectedtotal The expected number of total results. */ public function test_available_permissions_userset_enrol_userset_user($usersetidsforperm, $clusterassignments, $tableuserid, $expectedresults, $expectedtotal) { global $USER, $DB, $CFG; $userbackup = $USER; // Set up permissions. $USER = $this->setup_permissions_test(); // Set up userset contexts. for ($i = 1; $i <= 6; $i++) { $ctx = \local_elisprogram\context\userset::instance($i); } accesslib_clear_all_caches(true); // Assign capabilities. $capability = 'local/elisprogram:userset_enrol_userset_user'; foreach ($usersetidsforperm as $usersetid) { $this->give_permission_for_context($USER->id, $capability, \local_elisprogram\context\userset::instance($usersetid)); } // Create clusterassignments. foreach ($clusterassignments as $clusterassignment) { $clusterassignment = new clusterassignment($clusterassignment); $clusterassignment->save(); } // Construct test table. $table = new deepsight_datatable_useruserset_available_mock($DB, 'test', 'http://localhost', 'testuniqid'); $table->set_userid($tableuserid); // Perform test. $actualresults = $table->get_search_results(array(), array(), 0, 20); // Verify. $this->assert_search_results($expectedresults, $expectedtotal, $actualresults); }
/** * Validate that deleting a userset deletes all appropriate associations */ public function test_delete_elis_userset_deletes_associations() { global $CFG, $DB; // Entities. require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/userset.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/curriculum.class.php'); require_once elispm::lib('data/track.class.php'); require_once elis::lib('data/customfield.class.php'); // Associations. require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/clustercurriculum.class.php'); require_once elispm::lib('data/clustertrack.class.php'); require_once elispm::file('enrol/userset/moodleprofile/userset_profile.class.php'); // For context level access. require_once elispm::file('accesslib.php'); $origfieldcount = $DB->count_records(field::TABLE); // Set up user set. $userset = new userset(array('name' => 'testusersetname')); $userset->save(); // Set up other entities and associations. // Cluster enrolment. $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); $clusterassignment = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $user->id)); $clusterassignment->save(); // Cluster-curriculum assignment. $curriculum = new curriculum(array('idnumber' => 'testcurriculumidnumber')); $curriculum->save(); $clustercurriculum = new clustercurriculum(array('clusterid' => $userset->id, 'curriculumid' => $curriculum->id)); $clustercurriculum->save(); // Cluster-track assignment. $track = new track(array('curid' => $curriculum->id, 'idnumber' => 'testtrackidnumber')); $track->save(); $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => $track->id)); $clustertrack->save(); // Custom field. $field = new field(array('name' => 'testfieldname', 'categoryid' => 9999)); $field->save(); $context = \local_elisprogram\context\userset::instance($userset->id); $data = new field_data_int(array('contextid' => $context->id, 'fieldid' => $field->id, 'data' => 1)); $data->save(); // Cluster profile criteria. $clusterprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $field->id, 'value' => 0)); $clusterprofile->save(); // Validate setup. $this->assertEquals(1, $DB->count_records(userset::TABLE)); $this->assertEquals(1, $DB->count_records(user::TABLE)); $this->assertEquals(1, $DB->count_records(clusterassignment::TABLE)); $this->assertEquals(1, $DB->count_records(curriculum::TABLE)); $this->assertEquals(1, $DB->count_records(clustercurriculum::TABLE)); $this->assertEquals(1, $DB->count_records(track::TABLE)); $this->assertEquals(1, $DB->count_records(clustertrack::TABLE)); $this->assertEquals(1 + $origfieldcount, $DB->count_records(field::TABLE)); $this->assertEquals(1, $DB->count_records(field_data_int::TABLE)); $this->assertEquals(1, $DB->count_records(userset_profile::TABLE)); // Run the import. $data = array('action' => 'delete'); $this->run_core_userset_import($data, true); // Validation. $this->assertEquals(0, $DB->count_records(userset::TABLE)); $this->assertEquals(1, $DB->count_records(user::TABLE)); $this->assertEquals(0, $DB->count_records(clusterassignment::TABLE)); $this->assertEquals(1, $DB->count_records(curriculum::TABLE)); $this->assertEquals(0, $DB->count_records(clustercurriculum::TABLE)); $this->assertEquals(1, $DB->count_records(track::TABLE)); $this->assertEquals(0, $DB->count_records(clustertrack::TABLE)); $this->assertEquals(1 + $origfieldcount, $DB->count_records(field::TABLE)); $this->assertEquals(0, $DB->count_records(field_data_int::TABLE)); $this->assertEquals(0, $DB->count_records(userset_profile::TABLE)); }
/** * 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')); }