/** * Create and update userset 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_userset_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/userset.class.php'); $fieldid = $this->create_test_field($name, $datatype, $control, $inctime, $maxlength, $options, CONTEXT_ELIS_USERSET); 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]); } $record = new stdClass(); $record->action = 'create'; $record->context = 'cluster'; $record->name = 'testcluster'; $record->{$name} = $data; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('course', (object) $record, 'bogus'); $usersetcontext = \local_elisprogram\context\userset::instance($DB->get_field(userset::TABLE, 'id', array('name' => 'testcluster'))); $this->assert_field_values($datatype, $control, $fieldid, $usersetcontext->id, $expected); // Update. $record = new stdClass(); $record->action = 'update'; $record->context = 'cluster'; $record->name = 'testcluster'; $record->parent = 0; $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, $usersetcontext->id, $updateexpected); }
/** * Tests deepsight_datatable_usersetuser_base::get_userset_subsets * @dataProvider dataprovider_get_userset_subsets * @param int $parentuserset The ID of a userset to pass to the function as the parent userset ID. * @param bool $includeparent Whether to include the parent ID in the return array. * @param array $expectedresults The expected return value. */ public function test_get_userset_subsets($parentuserset, $includeparent, $expectedresults) { $dataset = $this->createCsvDataSet(array(user::TABLE => elispm::file('tests/fixtures/deepsight_user.csv'), userset::TABLE => elispm::file('tests/fixtures/deepsight_usersetwithsubsets.csv'))); $this->loadDataSet($dataset); accesslib_clear_all_caches(true); // Set up contexts. for ($i = 1; $i <= 6; $i++) { $ctx = \local_elisprogram\context\userset::instance($i); } accesslib_clear_all_caches(true); $actualresults = deepsight_datatable_standard::get_userset_subsets($parentuserset, $includeparent); $this->assertEquals($expectedresults, array_keys($actualresults)); }
/** * Set up some base user data */ protected function set_up_users() { global $DB, $USER; // Set up a cluster administrator as a PM user. $clusteradmin = new user(array('idnumber' => 'clusteradmin', 'username' => 'clusteradmin', 'firstname' => 'Cluster', 'lastname' => 'Admin', 'email' => '*****@*****.**', 'country' => 'CA')); $clusteradmin->save(); $USER = $DB->get_record('user', array('username' => 'clusteradmin')); // Set up a user set member as a PM user. $clusteruser = new user(array('idnumber' => 'clusteruser', 'username' => 'clusteruser', 'firstname' => 'Cluster', 'lastname' => 'User', 'email' => '*****@*****.**', 'country' => 'CA')); $clusteruser->save(); // Set up our test role. $syscontext = context_system::instance(); $roleid = create_role('clusteradmin', 'clusteradmin', 'clusteradmin'); assign_capability('local/elisprogram:user_edit', CAP_ALLOW, $roleid, $syscontext->id); // Assign the userset administrator an appropriate role on the userset. $instance = \local_elisprogram\context\userset::instance(1); role_assign($roleid, $USER->id, $instance->id); // Assign the user to the user set. $clusterassignment = new clusterassignment(array('clusterid' => 1, 'userid' => $clusteruser->id)); $clusterassignment->save(); }
/** * Test available table only shows usersets 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, 'userset' => array(1, 2, 3)) * @param int $tableprogramid The ID of the program 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, $tableprogramid, $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 'userset': $context = \local_elisprogram\context\userset::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_programuserset_available_mock($DB, 'test', 'http://localhost', 'testuniqid'); $table->set_programid($tableprogramid); // Perform test. $actualresults = $table->get_search_results(array(), array(), 0, 20); // Verify result. $this->assert_search_results($expectedresults, $expectedtotal, $actualresults); // Restore user. $USER = $userbackup; }
/** * Handle the event when a user is unassigned from a user set. * * @uses $DB * @param object $usersetinfo The ELIS crlm_cluster_assignments record object. * @return bool True on success or failure (event handlers must always return true). */ function elis_files_userset_deassigned($usersetinfo) { global $DB; // Only proceed here if we have valid userid,clusterid & the Alfresco plug-in is actually enabled. if (empty($usersetinfo->userid) || empty($usersetinfo->clusterid) || !($repo = repository_factory::factory('elisfiles'))) { return true; } $user = new user($usersetinfo->userid); // Get the Moodle user info from the CM user record. if (!$muser = $user->get_moodleuser()) { return true; } if (!($userset = $DB->get_record(userset::TABLE, array('id' => $usersetinfo->clusterid)))) { return true; } if (!file_exists(elispm::file('plugins/usetclassify/usersetclassification.class.php'))) { return true; } require_once(elispm::file('plugins/usetclassify/usersetclassification.class.php')); // Get the extra user set data and ensure it is present before proceeding. $usersetdata = usersetclassification::get_for_cluster($userset); if (empty($usersetdata->params)) { return true; } $usersetparams = unserialize($usersetdata->params); // Make sure this user set has the Alfresco shared folder property defined if (empty($usersetparams['elis_files_shared_folder'])) { return true; } // Does this organization have an Alfresco storage space? if (!$uuid = $repo->get_userset_store($userset->id, false)) { return true; } $context = \local_elisprogram\context\userset::instance($userset->id); $sql = 'SELECT rc.* FROM {role_assignments} ra INNER JOIN {role} r ON ra.roleid = r.id INNER JOIN {role_capabilities} rc ON r.id = rc.roleid WHERE ra.contextid = :contextid AND ra.userid = :userid AND rc.capability IN (:cap1, :cap2) AND rc.permission = '.CAP_ALLOW; $params = array( 'contextid' => $context->id, 'userid' => $muser->id, 'cap1' => 'repository/elisfiles:createusersetcontent', 'cap2' => 'local/elisprogram:userset_enrol' ); // Check if the user has a specific role assignment on the user set context with the editing capability if ($DB->record_exists_sql($sql, $params)) { // Remove all non-editing permissions for this user on the organization shared space. if ($permissions = elis_files_get_permissions($uuid, $muser->username)) { foreach ($permissions as $permission) { // Do not remove editing permissions if this user still actually has a user set membership. if ($permission == ELIS_FILES_ROLE_COLLABORATOR) { continue; } elis_files_set_permission($muser->username, $uuid, $permission, ELIS_FILES_CAPABILITY_DENIED); } } // Remove all permissions for this user on the organization shared space. } else if ($permissions = elis_files_get_permissions($uuid, $muser->username)) { require_once(elispm::lib('data/clusterassignment.class.php')); foreach ($permissions as $permission) { // Do not remove view permissions if this user still actually has a user set membership. $params = array( 'userid' => $usersetinfo->userid, 'clusterid' => $userset->id ); if ($permission == ELIS_FILES_ROLE_CONSUMER && $DB->record_exists(clusterassignment::TABLE, $params)) { continue; } elis_files_set_permission($muser->username, $uuid, $permission, ELIS_FILES_CAPABILITY_DENIED); } } 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); }
/** * Returns ELIS User Set context instance. * * @static * @param int $instanceid The ELIS userset id * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; * IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended); * MUST_EXIST means we will throw an exception if no record or multiple records found. * @return \local_elisprogram\context\userset|bool Context instance or false if instance was not found. */ public static function instance($instanceid, $strictness = MUST_EXIST) { global $DB; $contextlevel = \local_eliscore\context\helper::get_level_from_class_name(get_called_class()); if ($context = \local_eliscore\context\base::cache_get($contextlevel, $instanceid)) { return $context; } if (!($record = $DB->get_record('context', array('contextlevel' => $contextlevel, 'instanceid' => $instanceid)))) { if ($userset = $DB->get_record(\userset::TABLE, array('id' => $instanceid), 'id,parent', $strictness)) { if ($userset->parent) { $parentcontext = \local_elisprogram\context\userset::instance($userset->parent); $record = \local_eliscore\context\base::insert_context_record($contextlevel, $userset->id, $parentcontext->path); } else { $record = \local_eliscore\context\base::insert_context_record($contextlevel, $userset->id, '/' . SYSCONTEXTID, 0); } } } if ($record) { $context = new \local_elisprogram\context\userset($record); \local_eliscore\context\base::cache_add($context); return $context; } return false; }
/** * Get the previous UUID value that the user was browsing inside of and return it so long as their current * context relates to the context of where that UUID is located. * * @uses $USER * @param int $cid A course record ID. * @param int $uid A user record ID. * @param bool $shared A flag to indicate whether the user is currently located in the shared repository area. * @param int $oid A cluster record ID. * @return string The UUID of the last location the user was browsing files in. */ function get_repository_location(&$cid, &$uid, &$shared, &$oid) { global $COURSE, $USER; if (!isset($USER->elis_files_repository_location)) { return false; } $location = $USER->elis_files_repository_location; // Get the non context based permissions $capabilities = array( 'repository/elisfiles:viewowncontent' => false, 'repository/elisfiles:createowncontent' => false, 'repository/elisfiles:viewsharedcontent' => false, 'repository/elisfiles:createsharedcontent' => false ); $this->get_other_capabilities($USER, $capabilities); // If the previous value comes from within a cluster that is not the current cluster, return the root // storage value for the current cluster directory. if (!empty($location->uuid) && !empty($oid) && !empty($location->oid) && ($location->oid != $oid) && ($location->uid === 0) && ($location->shared == $shared) && ($location->uid == $uid)) { $cluster_context = \local_elisprogram\context\userset::instance($oid); $syscontext = context_system::instance(); $has_permissions = has_capability('repository/elisfiles:viewusersetcontent', $cluster_context) || has_capability('repository/elisfiles:createusersetcontent', $cluster_context) || has_capability('repository/elisfiles:viewsitecontent', $syscontext) || has_capability('repository/elisfiles:createsitecontent', $syscontext); if ($has_permissions) { return $this->get_userset_store($oid); } } // If the previous value comes from within a course that is not the current course, return the root // storage value for the current course directory. if (!empty($location->uuid) && isset($location->cid) && ($location->cid != $cid) && ($location->uid === 0) && ($location->shared == $shared) && ($location->uid == $uid)) { if (empty($cid)) { $cid = $COURSE->id; } if ($cid == SITEID) { $context = context_system::instance(); if (has_capability('repository/elisfiles:viewsitecontent', $context)) { $root = $this->get_root(); if (!empty($root->uuid)) { return $root->uuid; } } } else { $context = context_course::instance($cid); $syscontext = context_system::instance(); $has_permissions = has_capability('repository/elisfiles:viewcoursecontent', $context) || has_capability('repository/elisfiles:createcoursecontent', $context) || has_capability('repository/elisfiles:viewsitecontent', $syscontext) || has_capability('repository/elisfiles:createsitecontent', $syscontext); if ($has_permissions) { return $this->get_course_store($cid); } } } if (empty($location->uuid)) { // If we have explicity requested a user's home directory, make sure we return that if ((isset($location->oid) && $location->oid == $oid) && (isset($location->cid) && $location->cid == $cid) && (isset($location->uid) && ($location->uid != $uid) && ($uid === $USER->id)) && empty($location->uuid)) { // Check for correct permissions $syscontext = context_system::instance(); $has_permissions = $capabilities['repository/elisfiles:viewowncontent'] || $capabilities['repository/elisfiles:createowncontent'] || has_capability('repository/elisfiles:viewsitecontent', $syscontext) || has_capability('repository/elisfiles:createsitecontent', $syscontext); if ($has_permissions) { $shared = (boolean)0; return $this->get_user_store($uid); } } // If we requested the shared repository location if ((isset($location->oid) && $location->oid == $oid) && (isset($location->cid) && $location->cid == $cid) && (isset($location->uid) && $location->uid == $uid) && ((isset($location->shared) && ($location->shared != $shared) && ($shared == true)) || (!isset($location->shared) && $shared == true))) { $syscontext = context_system::instance(); $has_permissions = $capabilities['repository/elisfiles:viewsharedcontent'] || $capabilities['repository/elisfiles:createsharedcontent'] || has_capability('repository/elisfiles:viewsitecontent', $syscontext) || has_capability('repository/elisfiles:createsitecontent', $syscontext); if ($has_permissions) { $uid = 0; return $this->suuid; } } } // Otherwise, we are using the same settings as the previous location, so ensure that the calling script // has those values. if (empty($uid)) { $cid = (isset($location->cid) && $location->cid == $cid ? $location->cid : $cid); } $oid = ((!empty($location->uuid) && isset($location->oid)) ? $location->oid : $oid); $uid = ((!empty($location->uuid) && isset($location->uid)) ? $location->uid : $uid); $shared = ((!empty($location->uuid) && isset($location->shared)) ? $location->shared : $shared); return $location->uuid; }
/** * ELIS-4749: Test for assigning a user a role on a user context */ public function test_assignuserforusersetctx() { 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_USERSET)); foreach ($rolesctx as $rolectx) { $roleid = $rolectx->roleid; break; } // Get user to assign role. $user = new user(103); $muser = $user->get_moodleuser(); // Get specific context. $usrset = new userset(1); $context = \local_elisprogram\context\userset::instance($usrset->id); // Assign role. $this->assertGreaterThan(0, role_assign($roleid, $muser->id, $context->id)); }
/** * Performs the following tests for the available list * - Doesn't show child subsets. * - DOES show grandchild and beyond subsets. * - Doesn't show ancestor usersets. * - Doesn't show current userset. * - Only shows usersets which the current user has local/elisprogram:userset_edit permissions on. * * @dataProvider dataprovider_available_shows_correct_usersets * @param array $permissions An array of information specifying the contexts to assign the associate permission on. * This is formatted like array('system' => true, 'userset' => 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. */ public function test_available_shows_correct_usersets($permissions, $tableusersetid, $expectedresults) { global $USER, $DB, $CFG; $userbackup = $USER; $_GET['id'] = $tableusersetid; // Set up permissions. $USER = $this->setup_permissions_test(); // Set up contexts. for ($i = 1; $i <= 6; $i++) { $ctx = \local_elisprogram\context\userset::instance($i); } // Set up permissions. $perm = 'local/elisprogram:userset_edit'; foreach ($permissions as $contexttype => $ids) { if ($contexttype === 'system') { $this->give_permission_for_context($USER->id, $perm, context_system::instance()); } else { foreach ($ids as $contextinstanceid) { switch ($contexttype) { case 'userset': $context = \local_elisprogram\context\userset::instance($contextinstanceid); break; } $this->give_permission_for_context($USER->id, $perm, $context); } } } accesslib_clear_all_caches(true); // Construct test table. $table = new deepsight_datatable_usersetsubuserset_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, count($expectedresults), $actualresults); // Restore user. $USER = $userbackup; }
/** * Determines if a paritcular cluster is set up for groupings * * @param int $clusterid The id of the cluster in question * @return boolean True if this grouping is allowed, otherwise false */ function userset_groups_grouping_allowed($clusterid) { global $CFG, $DB; $enabled = get_config('elisprogram_usetgroups', 'userset_groupings'); if (empty($enabled)) { return false; } //retrieve the config field if ($fieldid = $DB->get_field(field::TABLE, 'id', array('shortname' => 'userset_groupings'))) { //retrieve the cluster context instance $context_instance = \local_elisprogram\context\userset::instance($clusterid); //construct the specific field $field = new field($fieldid); //retrieve the appropriate field's data for this cluster based on the context instance if ($field_data = field_data::get_for_context_and_field($context_instance, $field)) { //this should really only return one record, so return true of any have non-empty data foreach ($field_data as $field_datum) { if (!empty($field_datum->data)) { return true; } } } } return false; }
/** * 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 */ function after_cm_entity_add($cm_entity) { global $USER, $DB; //make sure a valid role is set if (!empty(elis::$config->local_elisprogram->default_cluster_role_id) && $DB->record_exists('role', array('id' => elis::$config->local_elisprogram->default_cluster_role_id))) { //get the context instance for capability checking $context_instance = \local_elisprogram\context\userset::instance($cm_entity->id); //assign the appropriate role if the user does not have the edit capability if (!has_capability('local/elisprogram:userset_edit', $context_instance)) { role_assign(elis::$config->local_elisprogram->default_cluster_role_id, $USER->id, $context_instance->id); } } }
/** * 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; }
/** * Performs userset 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 userset_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::userset_delete_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Validate if (empty($data->name) || !($usid = $DB->get_field(userset::TABLE, 'id', array('name' => $data->name)))) { throw new data_object_exception('ws_userset_delete_fail_invalid_name', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:userset_delete', \local_elisprogram\context\userset::instance($usid)); $userset = new userset($usid); if (!empty($data->recursive)) { $userset->deletesubs = true; } $userset->delete(); // Verify deletion & respond. if (!$DB->record_exists(userset::TABLE, array('name' => $data->name))) { return array('messagecode' => get_string('ws_userset_delete_success_code', 'local_datahub', empty($data->recursive) ? '' : get_string('ws_userset_delete_recursive', 'local_datahub')), 'message' => get_string('ws_userset_delete_success_msg', 'local_datahub', empty($data->recursive) ? '' : get_string('ws_userset_delete_subsets', 'local_datahub'))); } else { throw new data_object_exception('ws_userset_delete_fail', 'local_datahub'); } }
/** * 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); }
/** * Check the current user's capability to view the current node * @param int $id course id related to uuid * @param int $shared shared flag related to uuid * @param int $oid user set id related to uuid * @param string $uuid node uuid * @param int $userid user id related to uuid * @uses $USER * @return boolean Return true or false */ function check_viewing_permissions($id, $shared, $oid, $uuid, $userid) { global $USER; /// Get the context instance for where we originated viewing this browser from. //error_log("check_viewing_permissions({$id}, {$shared}, {$oid}, {$uuid}, {$userid})"); if (!empty($oid)) { $userset_context = \local_elisprogram\context\userset::instance($oid); } if ($id == SITEID) { $context = context_system::instance(); } else { $context = context_course::instance($id); } // Get the non context based permissions $capabilities = array('repository/elisfiles:viewowncontent'=> false, 'repository/elisfiles:viewsharedcontent'=> false); $this->elis_files->get_other_capabilities($USER, $capabilities); $canview = false; $syscontext = context_system::instance(); $site_files_permission = has_capability('repository/elisfiles:viewsitecontent', $syscontext); if (empty($userid) && empty($shared) && empty($oid)) { $has_permission = $site_files_permission || $id == SITEID && has_capability('repository/elisfiles:viewsitecontent', $context) || $id != SITEID && has_capability('repository/elisfiles:viewcoursecontent', $context); if ($has_permission) { $canview = true; } } else if (empty($userid) && $shared == true) { $canview = $site_files_permission || $capabilities['repository/elisfiles:viewsharedcontent']; } else { if (($USER->id == $userid) && empty($oid)) { $canview = $site_files_permission || $capabilities['repository/elisfiles:viewowncontent']; } else { if (has_capability('repository/elisfiles:viewsitecontent', $context, $USER->id)) { $canview = true; } else if (!empty($oid) && has_capability('repository/elisfiles:viewusersetcontent', $userset_context)) { $canview = true; } } } return $canview; }
/** * Gets all userset ids that are subsets of the given userset. * @param int $usersetid The ID of the userset to get subsets for. * @param bool $includeparent Whether to include the passed parent userset ID in the return array. * @return array An array of userset IDs */ public static function get_userset_subsets($usersetid, $includeparent = true) { global $DB; $parentctx = \local_elisprogram\context\userset::instance($usersetid); if (empty($parentctx)) { return array(); } $usersets = array(); $path = $includeparent === true ? $parentctx->path . '%' : $parentctx->path . '/%'; // Get sub usersets. $sql = 'SELECT userset.id as clusterid, userset.* FROM {' . userset::TABLE . '} userset JOIN {context} ctx ON userset.id = ctx.instanceid WHERE ctx.contextlevel = ? AND ctx.path LIKE ?'; $params = array(CONTEXT_ELIS_USERSET, $path); return $DB->get_records_sql($sql, $params); }
/** * Performs useret 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 userset_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::userset_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. // Validate if (empty($data->name) || !($usersetid = $DB->get_field(userset::TABLE, 'id', array('name' => $data->name)))) { throw new data_object_exception('ws_userset_update_fail_invalid_name', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:userset_edit', \local_elisprogram\context\userset::instance($usersetid)); $usid = 0; if (!empty($data->parent) && strtolower($data->parent) != 'top' && !($usid = $DB->get_field(userset::TABLE, 'id', array('name' => $data->parent)))) { throw new data_object_exception('ws_userset_update_fail_invalid_parent', 'local_datahub', '', $data); } if ($usid || strtolower($data->parent) == 'top') { $record->parent = $usid; } $userset = new userset($usersetid); $userset->set_from_data($record); $userset->save(); // Respond. if (!empty($userset->id)) { $usrec = (array) $DB->get_record(userset::TABLE, array('id' => $userset->id)); $usobj = $userset->to_array(); // convert multi-valued custom field arrays to comma-separated listing $fields = self::get_userset_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($usobj[$fullfieldname]) && is_array($usobj[$fullfieldname])) { $usobj[$fullfieldname] = implode(',', $usobj[$fullfieldname]); } } return array('messagecode' => get_string('ws_userset_update_success_code', 'local_datahub'), 'message' => get_string('ws_userset_update_success_msg', 'local_datahub'), 'record' => array_merge($usrec, $usobj)); } else { throw new data_object_exception('ws_userset_update_fail', 'local_datahub'); } }
/** * Test that a child User Set context instance is updated when that User Set is promoted to the top level due to parent * User Set deletion */ public function test_childusersetcontext_onpromotion_duringparentdeletion() { $newobj = $this->inituserset(); $newobj->save(); $ctx1 = \local_elisprogram\context\userset::instance($newobj->id); $data = array('name' => 'Test Sub User Set 1A', 'display' => 'We\'re just testing user set creation with child user sets!', 'parent' => $newobj->id); $subuserset = new userset($data); $subuserset->save(); // Delete the parent user set, promoting the sub-user-set. $newobj->delete(); $context = \local_elisprogram\context\userset::instance($subuserset->id); // Validate that the curent state of the context record is valid. $this->assertGreaterThan(0, $context->id); $this->assertEquals(CONTEXT_ELIS_USERSET, $context->contextlevel); $this->assertEquals($subuserset->id, $context->instanceid); // Create the context path to valid that this in the returned context object. $path = '/' . SYSCONTEXTID . '/' . $context->id; $this->assertEquals($path, $context->path); $this->assertEquals(substr_count($path, '/'), $context->depth); }
/** * Performs userset_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 userset_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::userset_enrolment_delete_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_delete_fail_invalid_userset', 'local_datahub', '', $data); } if (empty($data->plugin)) { $data->plugin = 'manual'; } // 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_delete_fail_invalid_user', 'local_datahub', '', $a); } $id = $DB->get_field(clusterassignment::TABLE, 'id', array('clusterid' => $clstid, 'userid' => $userid, 'plugin' => $data->plugin)); // Respond. if (!empty($id) && ($clstass = new clusterassignment($id))) { $clstass->delete(); return array('messagecode' => get_string('ws_userset_enrolment_delete_success_code', 'local_datahub'), 'message' => get_string('ws_userset_enrolment_delete_success_msg', 'local_datahub')); } else { throw new data_object_exception('ws_userset_enrolment_delete_fail', 'local_datahub'); } }
/** * 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); }
/** * Test whether a user can enrol users into a sub-userset if they have the required capability on the * parent userset. */ public function test_getallowedclusterswithparentpermission() { global $DB; $this->load_csv_data(); // Create role with cap: 'local/elisprogram:class_view'. $testrole = new stdClass(); $testrole->name = 'ELIS Sub-Userset Manager'; $testrole->shortname = '_test_ELIS_3848'; $testrole->description = 'ELIS userset enrol into sub-userser'; $testrole->archetype = ''; $testrole->id = create_role($testrole->name, $testrole->shortname, $testrole->description, $testrole->archetype); // Ensure our new role is assignable to ELIS class contexts. set_role_contextlevels($testrole->id, array(CONTEXT_ELIS_USERSET)); // Ensure the role has our required capability assigned. $syscontext = context_system::instance(); assign_capability('local/elisprogram:userset', CAP_ALLOW, $testrole->id, $syscontext->id, true); assign_capability('local/elisprogram:userset_view', CAP_ALLOW, $testrole->id, $syscontext->id, true); assign_capability('local/elisprogram:userset_create', CAP_ALLOW, $testrole->id, $syscontext->id, true); assign_capability('local/elisprogram:userset_enrol_userset_user', CAP_ALLOW, $testrole->id, $syscontext->id, true); $syscontext->mark_dirty(); // Assign a test user a role within the parent userset. $context = \local_elisprogram\context\userset::instance(1); role_assign($testrole->id, 100, $context->id); // Assign a test user a role within the sub-sub-userset. $ctx2 = \local_elisprogram\context\userset::instance(4); role_assign($testrole->id, 100, $ctx2->id); // Switch to testuser. $USER = $DB->get_record('user', array('id' => 100)); $USER->access = get_user_accessdata($USER->id); load_role_access_by_context($testrole->id, $context, $USER->access); // We need to force the accesslib cache to refresh. $GLOBALS['USER'] = $USER; // Check which of the parent usersets the user has access to based on the sub-userset. $allowed = userset::get_allowed_clusters(2); $this->assertInternalType('array', $allowed); $this->assertEquals(1, count($allowed)); // Check which of the parent usersets the user has access to basdd on the sub-sub-userset. $allowed = userset::get_allowed_clusters(4); $this->assertInternalType('array', $allowed); $this->assertEquals(2, count($allowed)); }
/** * Test the get_child_contexts() function of \local_elisprogram\context\userset */ public function test_userset_child_contexts() { $this->fixture_userset(); $expected = array(1 => array(2, 3, 4), 2 => array(4), 3 => array(), 4 => array()); foreach ($expected as $parentid => $childids) { $ctx = \local_elisprogram\context\userset::instance($parentid); $children = $ctx->get_child_contexts(); $count = 0; foreach ($children as $child) { if (!empty($childids)) { $this->assertTrue($child instanceof \local_elisprogram\context\userset); $this->assertContains($child->instanceid, $childids); } $count++; } $this->assertEquals(count($childids), $count); } }
/** * 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); }
function _cluster_allowed($id) { global $DB; if (isset($this->contexts['cluster'])) { $context = \local_elisprogram\context\userset::instance($id); $ancestorids = substr(str_replace('/', ',', $context->path), 1); $select = "id IN ({$ancestorids})\n AND instanceid IN (" . implode(',', $this->contexts['cluster']) . ")\n AND contextlevel = " . CONTEXT_ELIS_USERSET; return $DB->record_exists_select('context', $select); } return false; }
/** * Validate that mappings are applied during the user set delete action */ public function test_mapping_applied_during_userset_delete() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/accesslib.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/userset.class.php'; $this->init_mapping(); context_helper::reset_caches(); $parentuserset = new userset(array('name' => 'testparentusersetname')); $parentuserset->save(); \local_elisprogram\context\userset::instance($parentuserset->id); $userset = new userset(array('parent' => $parentuserset->id, 'name' => 'testusersetname')); $userset->save(); \local_elisprogram\context\userset::instance($userset->id); // Run the course delete action. $record = new stdClass(); $record->customaction = 'delete'; $record->customcontext = 'cluster'; $record->customname = 'testparentusersetname'; $record->customrecursive = '1'; $this->run_pmentity_import((array) $record); // Validation. $this->assertEquals(0, $DB->count_records(userset::TABLE)); }
/** * Test userset capability check. */ public function test_usersetcapabilitycheck() { global $DB, $USER; $role = $DB->get_record('role', array('shortname' => 'editingteacher')); // Assign the test user the editing teacher role on a test cluster. $ctx = \local_elisprogram\context\userset::instance($this->tusersetid); $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 'cluster' level. $contextscluster = new pm_context_set(); $contextscluster->contexts = array('cluster' => array($this->tusersetid)); $contextscluster->contextlevel = 'cluster'; $perm = 'local/elisprogram:userset_enrol_userset_user'; $contexts = pm_context_set::for_user_with_capability('cluster', $perm, $this->mdluserid); $this->assertEquals($contextscluster, $contexts); // Validate the return value when looking at the 'user' level. $contextsuser = new pm_context_set(); $contextsuser->contexts = array('cluster' => array($this->tusersetid)); $contextsuser->contextlevel = 'user'; $contexts = pm_context_set::for_user_with_capability('user', 'local/elisprogram:userset_enrol_userset_user', $this->mdluserid); $this->assertEquals($contextsuser, $contexts); // Validate checking for users with the given capability on this context. $users = pm_get_users_by_capability('cluster', $this->tusersetid, 'local/elisprogram:userset_enrol_userset_user'); $this->assertEquals($this->mdluserid, current($users)->id); }
/** * Test creating a new userset entity with a default role assignment defined. */ public function test_createusersetwithdefaultroleassignment() { global $USER, $DB; list($rcid, $reid) = $this->create_roles('userset'); // Setup the editor role to be the default role for the userset context. elis::$config->local_elisprogram->default_cluster_role_id = $reid; $sysctx = context_system::instance(); // Assign the test user the creator role. role_assign($rcid, $USER->id, $sysctx->id); // Create a new userset entity. $data = array('idnumber' => 'program100', 'name' => 'program100', 'description' => 'program100'); $obj = new userset($data); $obj->save(); // Initialize a new userset management page and invoke the code that handles default role assignments. $page = new usersetpage(); $page->after_cm_entity_add($obj); $usersetctx = \local_elisprogram\context\userset::instance($obj->id); $params = array('roleid' => $reid, 'userid' => $USER->id, 'contextid' => $usersetctx->id); $this->assertTrue($DB->record_exists('role_assignments', $params)); }
/** * 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)); }
protected function get_context() { if (!isset($this->_context)) { $id = $this->required_param('id', PARAM_INT); $context_instance = \local_elisprogram\context\userset::instance($id); $this->set_context($context_instance); } return $this->_context; }