/** * Move incoming usersets to be a subuset of current userset. * @throws moodle_exception * @param array $elements An array of userset information to assign to the track. * @param bool $bulkaction Whether this is a bulk-action or not. * @return array An array to format as JSON and return to the Javascript. */ protected function _respond_to_js(array $elements, $bulkaction) { global $DB, $USER; // The userset that will be the new parent set. $curusersetid = required_param('id', PARAM_INT); // Limit incoming usersets to possible-to-move usersets. $possiblesubsets = cluster_get_possible_sub_clusters($curusersetid); $elements = array_intersect_key($elements, $possiblesubsets); unset($possiblesubsets); // We need edit permissions. $perm = 'local/elisprogram:userset_edit'; $userseteditctx = pm_context_set::for_user_with_capability('cluster', $perm, $USER->id); if ($userseteditctx->context_allowed($curusersetid, 'cluster') !== true) { throw new moodle_exception('not_permitted', 'local_elisprogram'); } // Loop through requested elements to move. Check for permissions and do an sanity check on IDs and parent ID, then move. foreach ($elements as $tomoveusersetid => $label) { // Ensure user has edit perm on $tomoveusersetid. if ($userseteditctx->context_allowed($tomoveusersetid, 'cluster')) { $tomove = new userset($tomoveusersetid); $tomove->load(); // The userset we're moving shouldn't be the userset we're moving below, and it shouldn't already be a child // of the new parent. if ($tomove->id !== $curusersetid && $tomove->parent !== $curusersetid) { $tomove->parent = $curusersetid; $tomove->save(); } } } return array('result' => 'success', 'msg' => 'Success'); }
/** * Test successful userset update */ public function test_success() { global $DB; // Create custom field. $fieldcat = new field_category(); $fieldcat->name = 'Test'; $fieldcat->save(); $field = new field(); $field->categoryid = $fieldcat->id; $field->shortname = 'testfield'; $field->name = 'Test Field'; $field->datatype = 'text'; $field->save(); $fieldctx = new field_contextlevel(); $fieldctx->fieldid = $field->id; $fieldctx->contextlevel = CONTEXT_ELIS_USERSET; $fieldctx->save(); $this->give_permissions(array('local/elisprogram:userset_edit')); $userset = array('name' => 'testuserset', 'display' => 'test userset description', 'parent' => 'top', 'field_testfield' => 'Test field'); // Setup userset to update. $us = new userset(array('name' => 'testuserset')); $us->save(); $response = local_datahub_elis_userset_update::userset_update($userset); $this->assertNotEmpty($response); $this->assertInternalType('array', $response); $this->assertArrayHasKey('messagecode', $response); $this->assertArrayHasKey('message', $response); $this->assertArrayHasKey('record', $response); $this->assertEquals(get_string('ws_userset_update_success_code', 'local_datahub'), $response['messagecode']); $this->assertEquals(get_string('ws_userset_update_success_msg', 'local_datahub'), $response['message']); $this->assertInternalType('array', $response['record']); $this->assertArrayHasKey('id', $response['record']); // Get Userset. $updatedus = new userset($response['record']['id']); $updatedus->load(); $updatedus = $updatedus->to_array(); $expectus = array('id' => $response['record']['id'], 'name' => 'testuserset', 'display' => 'test userset description', 'parent' => 0, 'field_testfield' => 'Test field'); foreach ($expectus as $param => $val) { $this->assertArrayHasKey($param, $updatedus); $this->assertEquals($val, $updatedus[$param]); } }
/** * Validate that the "find_userset_folders" method respect site files * capabilities assigned at the system level * @uses $USER, $DB * @param string $capability The site files capabilility to assign to the test user * @param boolean $createonly The "create only" flag, as needed by the method * @param array $names The expected set of userset names that should be returned * @dataProvider userset_folders_provider */ public function test_find_userset_folders_respects_site_files($capability, $createonly, $names) { if (!class_exists('elispm')) { $this->markTestSkipped('local_elisprogram needed for test'); return false; } $this->resetAfterTest(true); $this->setup_test_data_xml(); global $DB, $USER; require_once(elis::lib('data/customfield.class.php')); require_once(elispm::lib('data/userset.class.php')); require_once(elispm::file('plugins/usetclassify/usersetclassification.class.php')); require_once(elispm::file('accesslib.php')); // Make sure the test user is not mistaken for a site admin or guest set_config('siteadmins', ''); set_config('siteguest', ''); $classification = new usersetclassification(array( 'shortname' => 'testclassification' )); $classification->param_elis_files_shared_folder = 1; $classification->save(); $userset = new userset(array( 'name' => 'testusersetname' )); $userset->save(); $userset->reset_custom_field_list(); $userset->load(); $userset->field__elis_userset_classification = 'testclassification'; $userset->save(); $this->setUser(100); // Assign the "site files" role to the test user $roleid = $this->assign_role_capability($capability); $systemcontext = context_system::instance(); role_assign($roleid, $USER->id, $systemcontext->id); // Assign the "view userset content" role to the test user $usersetcontext = \local_elisprogram\context\userset::instance($userset->id); $roleid = $this->assign_role_capability('repository/elisfiles:viewusersetcontent', 100); role_assign($roleid, $USER->id, $usersetcontext->id); // Obtain the set of userset folders $elisfiles = repository_factory::factory(); $folders = array(); $elisfiles->find_userset_folders($folders, $createonly); // Validate that the method returned the right number of folders $this->assertEquals(count($names), count($folders)); // Validate the specific names foreach ($names as $i => $name) { $this->assertEquals($name, $folders[$i]['name']); } }
public function test_deleteparentpromotechildren() { // Load great-grandfather, grandfather, parent, child usersets. ids 5,6,7,8, respectively. $dataset = $this->createCsvDataSet(array(userset::TABLE => elispm::file('tests/fixtures/userset_grandfathers.csv'), 'context' => elispm::file('tests/fixtures/userset_context.csv'))); $this->loadDataSet($dataset); // Delete grandfather userset. $grandfather = new userset(6); $grandfather->load(); $grandfather->deletesubs = 0; $grandfather->delete(); $parent = new userset(7); $parent->load(); $child = new userset(8); $child->load(); $this->assertEquals('0', $parent->parent); $this->assertEquals('1', $parent->depth); $this->assertEquals('7', $child->parent); $this->assertEquals('2', $child->depth); }
public function save() { $plugins = get_plugin_list(self::ENROL_PLUGIN_TYPE); foreach ($plugins as $plugin => $plugindir) { require_once elis::plugin_file(self::ENROL_PLUGIN_TYPE . '_' . $plugin, 'lib.php'); } if (!empty($this->id)) { // cache the database values, so we can know if the parent changed $old = new userset($this->id); $old->load(); } // figure out the right depth for the cluster if (empty($this->depth) || !empty($this->id)) { if ($this->parent == 0) { $this->depth = 1; } else { $this->depth = $this->parentset->depth + 1; } } parent::save(); if (isset($old) && $this->parent != $old->parent) { $cluster_context_instance = \local_elisprogram\context\userset::instance($this->id); // find all subclusters and adjust their depth $delta_depth = $this->depth - $old->depth; $LIKE = $this->_db->sql_like('path', '?'); $sql = 'UPDATE {' . self::TABLE . "}\n SET depth = depth + ?\n WHERE id IN (SELECT instanceid\n FROM {context}\n WHERE contextlevel = ?\n AND {$LIKE})"; $this->_db->execute($sql, array($delta_depth, CONTEXT_ELIS_USERSET, "{$cluster_context_instance->path}/%")); // Blank out the depth and path for associated records and child records in context table $sql = "UPDATE {context}\n SET depth=0, path=NULL\n WHERE id=? OR {$LIKE}"; $this->_db->execute($sql, array($cluster_context_instance->id, "{$cluster_context_instance->path}/%")); // Rebuild any blanked out records in context table \local_eliscore\context\helper::build_all_paths(false, array(CONTEXT_ELIS_USERSET)); } $plugins = get_plugin_list(self::ENROL_PLUGIN_TYPE); foreach ($plugins as $plugin => $plugindir) { call_user_func('userset_' . $plugin . '_update', $this); } if (isset($old)) { //signal that the cluster was created events_trigger('pm_userset_updated', $this); } else { events_trigger('pm_userset_created', $this); } }