protected function definition() { global $CFG, $DB; $collection = $this->_customdata['collection']; $context = $this->_customdata['context']; $mform = $this->_form; // Text search box. $mform->addElement('text', 'search', get_string('search')); $mform->setType('search', PARAM_TEXT); $options = array(\mod_mediagallery\base::TYPE_ALL => get_string('typeall', 'mediagallery'), \mod_mediagallery\base::TYPE_IMAGE => get_string('typeimage', 'mediagallery'), \mod_mediagallery\base::TYPE_VIDEO => get_string('typevideo', 'mediagallery'), \mod_mediagallery\base::TYPE_AUDIO => get_string('typeaudio', 'mediagallery')); $mform->addElement('select', 'type', get_string('mediatype', 'mediagallery'), $options); // Role select dropdown includes all roles, but using course-specific // names if applied. The reason for not restricting to roles that can // be assigned at course level is that upper-level roles display in the // enrolments table so it makes sense to let users filter by them. $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true); if (!empty($CFG->showenrolledusersrolesonly)) { $rolenames = role_fix_names(get_roles_used_in_context($context)); } $mform->addElement('select', 'role', get_string('role'), array(0 => get_string('all')) + $rolenames); // Filter by group. $allgroups = groups_get_all_groups($collection->course); $groupsmenu[0] = get_string('allparticipants'); foreach ($allgroups as $gid => $unused) { $groupsmenu[$gid] = $allgroups[$gid]->name; } if (count($groupsmenu) > 1) { $mform->addElement('select', 'group', get_string('group'), $groupsmenu); } // Submit button does not use add_action_buttons because that adds // another fieldset which causes the CSS style to break in an unfixable // way due to fieldset quirks. $group = array(); $group[] = $mform->createElement('submit', 'submitbutton', get_string('filter')); $group[] = $mform->createElement('submit', 'resetbutton', get_string('reset')); $group[] = $mform->createElement('submit', 'exportbutton', get_string('exportascsv', 'mediagallery')); $mform->addGroup($group, 'buttons', '', ' ', false); $mform->addElement('hidden', 'id', $context->instanceid); $mform->setType('id', PARAM_INT); $mform->addElement('hidden', 'action', 'search'); $mform->setType('action', PARAM_ALPHA); }
// Not needed anymore. unset($contextid); unset($courseid); require_login($course); $systemcontext = context_system::instance(); $isfrontpage = $course->id == SITEID; $frontpagectx = context_course::instance(SITEID); if ($isfrontpage) { $PAGE->set_pagelayout('admin'); require_capability('moodle/site:viewparticipants', $systemcontext); } else { $PAGE->set_pagelayout('incourse'); require_capability('moodle/course:viewparticipants', $context); } $rolenamesurl = new moodle_url("{$CFG->wwwroot}/user/index.php?contextid={$context->id}&sifirst=&silast="); $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true); if ($isfrontpage) { $rolenames[0] = get_string('allsiteusers', 'role'); } else { $rolenames[0] = get_string('allparticipants'); } // Make sure other roles may not be selected by any means. if (empty($rolenames[$roleid])) { print_error('noparticipants'); } // No roles to display yet? // frontpage course is an exception, on the front page course we should display all users. if (empty($rolenames) && !$isfrontpage) { if (has_capability('moodle/role:assign', $context)) { redirect($CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php?contextid=' . $context->id); } else {
$isfrontpage = ($course->id == SITEID); $frontpagectx = get_context_instance(CONTEXT_COURSE, SITEID); if ($isfrontpage) { $PAGE->set_pagelayout('admin'); require_capability('moodle/site:viewparticipants', $systemcontext); } else { $PAGE->set_pagelayout('incourse'); require_capability('moodle/course:viewparticipants', $context); } $rolenamesurl = new moodle_url("$CFG->wwwroot/user/index.php?contextid=$context->id&sifirst=&silast="); $allroles = get_all_roles(); $roles = get_profile_roles($context); $allrolenames = array(); if ($isfrontpage) { $rolenames = array(0=>get_string('allsiteusers', 'role')); } else { $rolenames = array(0=>get_string('allparticipants')); } foreach ($allroles as $role) { $allrolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on if (isset($roles[$role->id])) { $rolenames[$role->id] = $allrolenames[$role->id]; } } // make sure other roles may not be selected by any means
/** * Obtains a list of the possible roles that group members might come from, * on a course. Generally this includes only profile roles. * * @param context $context Context of course * @return Array of role ID integers, or false if error/none. */ function groups_get_possible_roles($context) { $roles = get_profile_roles($context); return array_keys($roles); }
/** * Return the current user roles and groups in the current course * @return array Array in the first element the groups in the second the user roles */ public function get_groups_roles() { global $USER; $groups = array(); $roles = array(); if (!has_capability('moodle/course:viewparticipants', $this->context)) { return array($groups, $roles); } if ($groupmode = $this->course->groupmode) { $aag = has_capability('moodle/site:accessallgroups', $this->context); if ($groupmode == VISIBLEGROUPS or $aag) { $allowedgroups = groups_get_all_groups($this->course->id, 0, $this->course->defaultgroupingid); } else { $allowedgroups = groups_get_all_groups($this->course->id, $USER->id, $this->course->defaultgroupingid); } $activegroup = groups_get_course_group($this->course, true, $allowedgroups); $groupsmenu = array(); if (!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) { $groupsmenu[0] = get_string('allparticipants'); } if ($allowedgroups) { foreach ($allowedgroups as $group) { $groupsmenu[$group->id] = format_string($group->name); } } $groups = $groupsmenu; } $allroles = get_all_roles(); $roles = get_profile_roles($this->context); $allrolenames = array(); $rolenames = array(0 => get_string('allparticipants')); foreach ($allroles as $role) { $allrolenames[$role->id] = strip_tags(role_get_name($role, $this->context)); if (isset($roles[$role->id])) { $rolenames[$role->id] = $allrolenames[$role->id]; } } $roles = $rolenames; return array($groups, $roles); }
/** * Outputs the content of the creation tab and manages actions taken in this tab */ public function view_creation() { global $SESSION, $OUTPUT, $PAGE, $DB; $id = $this->cm->id; $context = context_course::instance($this->course->id); // Get applicable roles! $rolenames = array(); if ($roles = get_profile_roles($context)) { foreach ($roles as $role) { $rolenames[$role->id] = strip_tags(role_get_name($role, $context)); } } // Check if everything has been confirmed, so we can finally start working! if (optional_param('confirm', 0, PARAM_BOOL)) { if (isset($SESSION->grouptool->view_administration->createGroups)) { require_capability('mod/grouptool:create_groups', $this->context); // Create groups! $data = $SESSION->grouptool->view_administration; switch ($data->mode) { case GROUPTOOL_GROUPS_AMOUNT: // Allocate members from the selected role to groups! switch ($data->allocateby) { case 'no': case 'random': case 'lastname': $orderby = 'lastname, firstname'; break; case 'firstname': $orderby = 'firstname, lastname'; break; case 'idnumber': $orderby = 'idnumber'; break; default: print_error('unknoworder'); } $users = groups_get_potential_members($this->course->id, $data->roleid, $data->cohortid, $orderby); $usercnt = count($users); $numgrps = $data->amount; $userpergrp = floor($usercnt / $numgrps); list($error, $preview) = $this->create_groups($data, $users, $userpergrp, $numgrps); break; case GROUPTOOL_MEMBERS_AMOUNT: // Allocate members from the selected role to groups! switch ($data->allocateby) { case 'no': case 'random': case 'lastname': $orderby = 'lastname, firstname'; break; case 'firstname': $orderby = 'firstname, lastname'; break; case 'idnumber': $orderby = 'idnumber'; break; default: print_error('unknoworder'); } $users = groups_get_potential_members($this->course->id, $data->roleid, $data->cohortid, $orderby); $usercnt = count($users); $numgrps = ceil($usercnt / $data->amount); $userpergrp = $data->amount; if (!empty($data->nosmallgroups) and $usercnt % $data->amount != 0) { /* * If there would be one group with a small number of member * reduce the number of groups */ $missing = $userpergrp * $numgrps - $usercnt; if ($missing > $userpergrp * (1 - GROUPTOOL_AUTOGROUP_MIN_RATIO)) { // Spread the users from the last small group! $numgrps--; $userpergrp = floor($usercnt / $numgrps); } } list($error, $preview) = $this->create_groups($data, $users, $userpergrp, $numgrps); break; case GROUPTOOL_1_PERSON_GROUPS: $users = groups_get_potential_members($this->course->id, $data->roleid, $data->cohortid); if (!isset($data->groupingname)) { $data->groupingname = null; } list($error, $prev) = $this->create_one_person_groups($users, $data->namingscheme, $data->grouping, $data->groupingname); $preview = $prev; break; case GROUPTOOL_FROMTO_GROUPS: if (!isset($data->groupingname)) { $data->groupingname = null; } list($error, $preview) = $this->create_fromto_groups($data); break; } $class = $error ? 'notifyproblem' : 'notifysuccess'; $preview = $OUTPUT->notification($preview, $class); echo $OUTPUT->box(html_writer::tag('div', $preview, array('class' => 'centered')), 'generalbox'); } unset($SESSION->grouptool->view_administration); } // Create the form-object! $mform = new \mod_grouptool\group_creation_form(null, array('id' => $id, 'roles' => $rolenames, 'show_grpsize' => $this->grouptool->use_size && $this->grouptool->use_individual)); if ($fromform = $mform->get_data()) { require_capability('mod/grouptool:create_groups', $this->context); // Save submitted data in session and show confirmation dialog! if (!isset($SESSION->grouptool)) { $SESSION->grouptool = new stdClass(); } if (!isset($SESSION->grouptool->view_administration)) { $SESSION->grouptool->view_administration = new stdClass(); } $SESSION->grouptool->view_administration = $fromform; $data = $SESSION->grouptool->view_administration; $preview = ""; switch ($data->mode) { case GROUPTOOL_GROUPS_AMOUNT: // Allocate members from the selected role to groups! switch ($data->allocateby) { case 'no': case 'random': case 'lastname': $orderby = 'lastname, firstname'; break; case 'firstname': $orderby = 'firstname, lastname'; break; case 'idnumber': $orderby = 'idnumber'; break; default: print_error('unknoworder'); } $users = groups_get_potential_members($this->course->id, $data->roleid, $data->cohortid, $orderby); $usercnt = count($users); $numgrps = clean_param($data->amount, PARAM_INT); $userpergrp = floor($usercnt / $numgrps); list($error, $preview) = $this->create_groups($data, $users, $userpergrp, $numgrps, true); break; case GROUPTOOL_MEMBERS_AMOUNT: // Allocate members from the selected role to groups! switch ($data->allocateby) { case 'no': case 'random': case 'lastname': $orderby = 'lastname, firstname'; break; case 'firstname': $orderby = 'firstname, lastname'; break; case 'idnumber': $orderby = 'idnumber'; break; default: print_error('unknoworder'); } $users = groups_get_potential_members($this->course->id, $data->roleid, $data->cohortid, $orderby); $usercnt = count($users); $numgrps = ceil($usercnt / $data->amount); $userpergrp = clean_param($data->amount, PARAM_INT); if (!empty($data->nosmallgroups) and $usercnt % clean_param($data->amount, PARAM_INT) != 0) { /* * If there would be one group with a small number of member * reduce the number of groups */ $missing = $userpergrp * $numgrps - $usercnt; if ($missing > $userpergrp * (1 - GROUPTOOL_AUTOGROUP_MIN_RATIO)) { // Spread the users from the last small group! $numgrps--; $userpergrp = floor($usercnt / $numgrps); } } list($error, $preview) = $this->create_groups($data, $users, $userpergrp, $numgrps, true); break; case GROUPTOOL_1_PERSON_GROUPS: $users = groups_get_potential_members($this->course->id, $data->roleid, $data->cohortid); if (!isset($data->groupingname)) { $data->groupingname = null; } list($error, $prev) = $this->create_one_person_groups($users, $data->namingscheme, $data->grouping, $data->groupingname, true); $preview = $prev; break; case GROUPTOOL_FROMTO_GROUPS: if (!isset($data->groupingname)) { $data->groupingname = null; } list($error, $preview) = $this->create_fromto_groups($data, true); break; } $preview = html_writer::tag('div', $preview, array('class' => 'centered')); $tab = required_param('tab', PARAM_ALPHANUMEXT); if ($error) { $text = get_string('create_groups_confirm_problem', 'grouptool'); $url = new moodle_url("view.php?id={$id}&tab=" . $tab); $back = new single_button($url, get_string('back'), 'post'); $confirmboxcontent = $this->confirm($text, $back); } else { $continue = "view.php?id={$id}&tab=" . $tab . "&confirm=true"; $cancel = "view.php?id={$id}&tab=" . $tab; $text = get_string('create_groups_confirm', 'grouptool'); $confirmboxcontent = $this->confirm($text, $continue, $cancel); } echo $OUTPUT->heading(get_string('preview'), 2, 'centered') . $OUTPUT->box($preview, 'generalbox') . $confirmboxcontent; } else { $mform->display(); } }
/** * Get the role names of all roles the users shown have. * * @param array $userids * @access public * @return array */ public function get_roles_for_users($userids) { global $DB; if (empty($userids)) { return array(); } $roles = array(); $context = $this->params['context']; list($insql, $params) = $DB->get_in_or_equal($userids); $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true); $sql = "SELECT ra.id, ra.userid, ra.roleid\n FROM {role_assignments} ra\n WHERE ra.userid {$insql} AND ra.contextid = ?"; $params[] = $context->get_course_context()->id; $rolers = $DB->get_recordset_sql($sql, $params); foreach ($rolers as $record) { if (isset($rolenames[$record->roleid])) { $roles[$record->userid][$record->roleid] = $rolenames[$record->roleid]; } } return $roles; }