/** * 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'); }
function action_default() { // Get parameters $sort = optional_param('sort', 'name', PARAM_ALPHA); $dir = optional_param('dir', 'ASC', PARAM_ALPHA); $page = optional_param('page', 0, PARAM_INT); $perpage = optional_param('perpage', 30, PARAM_INT); // how many per page $namesearch = trim(optional_param('search', '', PARAM_TEXT)); $alpha = optional_param('alpha', '', PARAM_ALPHA); $parent = $this->optional_param('id', 0, PARAM_INT); $classification = $this->optional_param('classification', NULL, PARAM_SAFEDIR); if ($parent) { $this->print_tabs('subclusters', array('id' => $parent)); } // Define columns $columns = array('name' => get_string('cluster_name', 'block_curr_admin'), 'display' => get_string('cluster_description', 'block_curr_admin')); $extrafilters = array('contexts' => clusterpage::get_contexts('block/curr_admin:cluster:view'), 'parent' => $parent, 'classification' => $classification); $items = cluster_get_listing($sort, $dir, $page * $perpage, $perpage, $namesearch, $alpha, $extrafilters); $numitems = cluster_count_records($namesearch, $alpha, $extrafilters); clusterpage::get_contexts('block/curr_admin:cluster:edit'); clusterpage::get_contexts('block/curr_admin:cluster:delete'); $this->print_list_view($items, $numitems, $columns, $filter = null, $alphaflag = true, $searchflag = true); if ($this->optional_param('id', 0, PARAM_INT)) { //get the non-parent clusters that are accessible based on the edit capability $contexts = clusterpage::get_contexts('block/curr_admin:cluster:edit'); $non_parent_clusters = cluster_get_possible_sub_clusters($this->optional_param('id', 0, PARAM_INT), $contexts); //display the dropdown if there are one or more available clusters if (count($non_parent_clusters) > 0) { echo '<div align="center">'; echo get_string('cluster_subcluster_prompt', 'block_curr_admin') . ': '; $url = $this->get_new_page(array('action' => 'subcluster', 'id' => $this->optional_param('id', 0, PARAM_INT)))->get_url() . '&subclusterid='; popup_form($url, $non_parent_clusters, 'assignsubcluster', '', 'Choose...'); echo '</div>'; } } }
/** * 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); }
function display_default() { global $OUTPUT; // Get parameters $sort = optional_param('sort', 'name', PARAM_ALPHA); $dir = optional_param('dir', 'ASC', PARAM_ALPHA); $page = optional_param('page', 0, PARAM_INT); $perpage = optional_param('perpage', 30, PARAM_INT); // how many per page $namesearch = trim(optional_param('search', '', PARAM_TEXT)); $alpha = optional_param('alpha', '', PARAM_ALPHA); $parent = $this->optional_param('id', 0, PARAM_INT); $classification = $this->optional_param('classification', NULL, PARAM_SAFEDIR); if ($parent) { $this->print_tabs('subclusters', array('id' => $parent)); } // Define columns $columns = array('name' => array('header' => get_string('userset_name', 'local_elisprogram')), 'display' => array('header' => get_string('userset_description', 'local_elisprogram'))); // set sorting if ($dir !== 'DESC') { $dir = 'ASC'; } if (isset($columns[$sort])) { $columns[$sort]['sortable'] = $dir; } else { $sort = 'defaultsortcolumn'; $columns[$sort]['sortable'] = $dir; } $extrafilters = array('contexts' => self::get_contexts('local/elisprogram:userset_view'), 'parent' => $parent, 'classification' => $classification); $items = cluster_get_listing($sort, $dir, $page * $perpage, $perpage, $namesearch, $alpha, $extrafilters); $numitems = cluster_count_records($namesearch, $alpha, $extrafilters); self::get_contexts('local/elisprogram:userset_edit'); self::get_contexts('local/elisprogram:userset_delete'); $this->print_list_view($items, $numitems, $columns, $filter = null, $alphaflag = true, $searchflag = true); if ($this->optional_param('id', 0, PARAM_INT)) { //get the non-parent clusters that are accessible based on the edit capability $contexts = usersetpage::get_contexts('local/elisprogram:userset_edit'); $non_parent_clusters = cluster_get_possible_sub_clusters($this->optional_param('id', 0, PARAM_INT), $contexts); //display the dropdown if there are one or more available clusters if (count($non_parent_clusters) > 0) { echo html_writer::start_tag('div', array('align' => 'center')); echo get_string('cluster_subcluster_prompt', 'local_elisprogram') . ': '; $url = $this->get_new_page(array('action' => 'subcluster', 'id' => $this->optional_param('id', 0, PARAM_INT)))->url . '&subclusterid='; echo $OUTPUT->single_select($url, 'subclusterid', $non_parent_clusters); echo html_writer::end_tag('div'); } } }