function get_selection_data($ufiltering) { global $SESSION; // get the SQL filter $guest = get_guest(); $sqlwhere = $ufiltering->get_sql_filter("id<>{$guest->id} AND deleted <> 1"); $total = count_records_select('user', "id<>{$guest->id} AND deleted <> 1"); $acount = count_records_select('user', $sqlwhere); $scount = count($SESSION->bulk_users); $userlist = array('acount' => $acount, 'scount' => $scount, 'ausers' => false, 'susers' => false, 'total' => $total); $userlist['ausers'] = get_records_select_menu('user', $sqlwhere, 'fullname', 'id,' . sql_fullname() . ' AS fullname', 0, MAX_BULK_USERS); if ($scount) { if ($scount < MAX_BULK_USERS) { $in = implode(',', $SESSION->bulk_users); } else { $bulkusers = array_slice($SESSION->bulk_users, 0, MAX_BULK_USERS, true); $in = implode(',', $bulkusers); } $userlist['susers'] = get_records_select_menu('user', "id IN ({$in})", 'fullname', 'id,' . sql_fullname() . ' AS fullname'); } return $userlist; }
/** * Returns array of userinfo of all students in this course * or on this site if courseid is id of site * * @uses $CFG * @uses SITEID * @param int $courseid The course in question. * @param string $sort ? * @param string $dir ? * @param int $page ? * @param int $recordsperpage ? * @param string $firstinitial ? * @param string $lastinitial ? * @param ? $group ? * @param string $search ? * @param string $fields A comma separated list of fields to be returned from the chosen table. * @param string $exceptions ? * @return object * @todo Finish documenting this function */ function get_course_students($courseid, $sort = 'ul.timeaccess', $dir = '', $page = '', $recordsperpage = '', $firstinitial = '', $lastinitial = '', $group = NULL, $search = '', $fields = '', $exceptions = '') { global $CFG; if ($courseid == SITEID and $CFG->allusersaresitestudents) { // return users with confirmed, undeleted accounts who are not site teachers // the following is a mess because of different conventions in the different user functions $sort = str_replace('s.timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess $sort = str_replace('timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess $sort = str_replace('u.', '', $sort); // the get_user function doesn't use the u. prefix to fields $fields = str_replace('u.', '', $fields); if ($sort) { $sort = $sort . ' ' . $dir; } // Now we have to make sure site teachers are excluded if ($teachers = get_course_teachers(SITEID)) { foreach ($teachers as $teacher) { $exceptions .= ',' . $teacher->userid; } $exceptions = ltrim($exceptions, ','); } return get_users(true, $search, true, $exceptions, $sort, $firstinitial, $lastinitial, $page, $recordsperpage, $fields ? $fields : '*'); } $LIKE = sql_ilike(); $fullname = sql_fullname('u.firstname', 'u.lastname'); $groupmembers = ''; // make sure it works on the site course $context = get_context_instance(CONTEXT_COURSE, $courseid); $select = "c.contextlevel=" . CONTEXT_COURSE . " AND "; // Must be on a course if ($courseid != SITEID) { // If not site, require specific course $select .= "c.instanceid={$courseid} AND "; } $select .= "rc.capability='moodle/legacy:student' AND rc.permission=" . CAP_ALLOW . " AND "; $select .= ' u.deleted = \'0\' '; if (!$fields) { $fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, ' . 'u.maildisplay, u.mailformat, u.maildigest, u.email, u.city, ' . 'u.country, u.picture, u.idnumber, u.department, u.institution, ' . 'u.emailstop, u.lang, u.timezone, ul.timeaccess as lastaccess'; } if ($search) { $search = ' AND (' . $fullname . ' ' . $LIKE . '\'%' . $search . '%\' OR email ' . $LIKE . '\'%' . $search . '%\') '; } if ($firstinitial) { $select .= ' AND u.firstname ' . $LIKE . '\'' . $firstinitial . '%\' '; } if ($lastinitial) { $select .= ' AND u.lastname ' . $LIKE . '\'' . $lastinitial . '%\' '; } if ($group === 0) { /// Need something here to get all students not in a group return array(); } else { if ($group !== NULL) { $groupmembers = "INNER JOIN {$CFG->prefix}groups_members gm on u.id=gm.userid"; $select .= ' AND gm.groupid = \'' . $group . '\''; } } if (!empty($exceptions)) { $select .= ' AND u.id NOT IN (' . $exceptions . ')'; } if ($sort) { $sort = ' ORDER BY ' . $sort . ' '; } $students = get_records_sql("SELECT {$fields}\n FROM {$CFG->prefix}user u INNER JOIN\n {$CFG->prefix}role_assignments ra on u.id=ra.userid INNER JOIN\n {$CFG->prefix}role_capabilities rc ON ra.roleid=rc.roleid INNER JOIN\n {$CFG->prefix}context c ON c.id=ra.contextid LEFT OUTER JOIN\n {$CFG->prefix}user_lastaccess ul on ul.userid=ra.userid\n {$groupmembers}\n WHERE {$select} {$search} {$sort} {$dir}", $page, $recordsperpage); return $students; }
$sqlwhere = "WHERE (glossaryid = '{$glossary->id}' OR sourceglossaryid = '{$glossary->id}') AND\n (ge.approved != 0 {$userid}) AND gec.entryid IS NULL"; $sqlorderby = ' ORDER BY concept'; } else { $printpivot = 0; $sqlselect = "SELECT ge.*, ce.entryid, c.name AS glossarypivot"; $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories ce, {$CFG->prefix}glossary_categories c"; $sqlwhere = "WHERE ge.id = ce.entryid AND ce.categoryid = '{$hook}' AND\n ce.categoryid = c.id AND ge.approved != 0 AND\n (ge.glossaryid = '{$glossary->id}' OR ge.sourceglossaryid = '{$glossary->id}') AND\n (ge.approved != 0 {$userid})"; $sqlorderby = ' ORDER BY c.name, ge.concept'; } break; case GLOSSARY_AUTHOR_VIEW: $where = ''; if ($sqlsortkey == 'firstname') { $usernamefield = sql_fullname('u.firstname', 'u.lastname'); } else { $usernamefield = sql_fullname('u.lastname', 'u.firstname'); } $where = "AND " . sql_substr() . "(upper({$usernamefield}),1," . $textlib->strlen($hook) . ") = '" . $textlib->strtoupper($hook) . "'"; if ($hook == 'ALL') { $where = ''; } $sqlselect = "SELECT ge.*, {$usernamefield} AS glossarypivot, 1 AS userispivot "; $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}user u"; $sqlwhere = "WHERE ge.userid = u.id AND\n (ge.approved != 0 {$userid})\n {$where} AND \n (ge.glossaryid = '{$glossary->id}' OR ge.sourceglossaryid = '{$glossary->id}')"; $sqlorderby = "ORDER BY {$usernamefield} {$sqlsortorder}, ge.concept"; break; case GLOSSARY_APPROVAL_VIEW: $fullpivot = 0; $printpivot = 0; $where = ''; if ($hook != 'ALL' and $hook != 'SPECIAL') {
$return = $CFG->wwwroot . '/' . $CFG->admin . '/user/user_bulk.php'; if (empty($SESSION->bulk_users)) { redirect($return); } admin_externalpage_print_header(); //TODO: add support for large number of users if ($confirm and confirm_sesskey()) { $primaryadmin = get_admin(); $in = implode(',', $SESSION->bulk_users); if ($rs = get_recordset_select('user', "id IN ({$in})")) { while ($user = rs_fetch_next_record($rs)) { if ($primaryadmin->id != $user->id and $USER->id != $user->id and delete_user($user)) { unset($SESSION->bulk_users[$user->id]); } else { notify(get_string('deletednot', '', fullname($user, true))); } } rs_close($rs); } redirect($return, get_string('changessaved')); } else { $in = implode(',', $SESSION->bulk_users); $userlist = get_records_select_menu('user', "id IN ({$in})", 'fullname', 'id,' . sql_fullname() . ' AS fullname'); $usernames = implode(', ', $userlist); $optionsyes = array(); $optionsyes['confirm'] = 1; $optionsyes['sesskey'] = sesskey(); print_heading(get_string('confirmation', 'admin')); notice_yesno(get_string('deletecheckfull', '', $usernames), 'user_bulk_delete.php', 'user_bulk.php', $optionsyes, NULL, 'post', 'get'); } admin_externalpage_print_footer();
/** * Creates known user filter if present * @param string $fieldname * @param boolean $advanced * @return object filter */ function get_field($fieldname, $advanced) { global $USER, $CFG, $SITE; switch ($fieldname) { case 'username': return new user_filter_text('username', get_string('username'), $advanced, 'username'); case 'realname': return new user_filter_text('realname', get_string('fullname'), $advanced, sql_fullname()); case 'lastname': return new user_filter_text('lastname', get_string('lastname'), $advanced, 'lastname'); case 'firstname': return new user_filter_text('firstname', get_string('firstname'), $advanced, 'firstname'); case 'email': return new user_filter_text('email', get_string('email'), $advanced, 'email'); case 'city': return new user_filter_text('city', get_string('city'), $advanced, 'city'); case 'country': return new user_filter_select('country', get_string('country'), $advanced, 'country', get_list_of_countries(), $USER->country); case 'confirmed': return new user_filter_yesno('confirmed', get_string('confirmed', 'admin'), $advanced, 'confirmed'); case 'profile': return new user_filter_profilefield('profile', get_string('profile'), $advanced); case 'courserole': return new user_filter_courserole('courserole', get_string('courserole', 'filters'), $advanced); case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced); case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess'); case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess'); case 'lastlogin': return new user_filter_date('lastlogin', get_string('lastlogin'), $advanced, 'lastlogin'); case 'timemodified': return new user_filter_date('timemodified', get_string('lastmodified'), $advanced, 'timemodified'); case 'auth': $plugins = get_list_of_plugins('auth'); $choices = array(); foreach ($plugins as $auth) { $choices[$auth] = auth_get_plugin_title($auth); } return new user_filter_simpleselect('auth', get_string('authentication'), $advanced, 'auth', $choices); case 'mnethostid': // include all hosts even those deleted or otherwise problematic if (!($hosts = get_records('mnet_host', '', '', 'id', 'id, wwwroot, name'))) { $hosts = array(); } $choices = array(); foreach ($hosts as $host) { if ($host->id == $CFG->mnet_localhost_id) { $choices[$host->id] = format_string($SITE->fullname) . ' (' . get_string('local') . ')'; } else { if (empty($host->wwwroot)) { // All hosts continue; } else { $choices[$host->id] = $host->name . ' (' . $host->wwwroot . ')'; } } } if ($usedhosts = get_fieldset_sql("SELECT DISTINCT mnethostid FROM {$CFG->prefix}user WHERE deleted=0")) { foreach ($usedhosts as $hostid) { if (empty($hosts[$hostid])) { $choices[$hostid] = 'id: ' . $hostid . ' (' . get_string('error') . ')'; } } } if (count($choices) < 2) { return null; // filter not needed } return new user_filter_simpleselect('mnethostid', 'mnethostid', $advanced, 'mnethostid', $choices); default: return null; } }
/** * Gets the users for a course who are not in a specified group * @param int $groupid The id of the group * @param string searchtext similar to searchtext in role assign, search * @return array An array of the userids of the non-group members, or false if * an error occurred. * This function was changed to get_users_by_capability style * mostly because of the searchtext requirement */ function groups_get_users_not_in_group($courseid, $groupid, $searchtext = '') { global $CFG; $context = get_context_instance(CONTEXT_COURSE, $courseid); if ($searchtext !== '') { // Search for a subset of remaining users $LIKE = sql_ilike(); $FULLNAME = sql_fullname(); $wheresearch = " AND u.id IN (SELECT id FROM {$CFG->prefix}user WHERE {$FULLNAME} {$LIKE} '%{$searchtext}%' OR email {$LIKE} '%{$searchtext}%' )"; } else { $wheresearch = ''; } $capability = 'moodle/course:view'; $doanything = false; // find all possible "student" roles if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context)) { if (!$doanything) { if (!($sitecontext = get_context_instance(CONTEXT_SYSTEM))) { return false; // Something is seriously wrong } $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext); } $validroleids = array(); foreach ($possibleroles as $possiblerole) { if (!$doanything) { if (isset($doanythingroles[$possiblerole->id])) { // We don't want these included continue; } } if ($caps = role_context_capabilities($possiblerole->id, $context, $capability)) { // resolved list if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0 $validroleids[] = $possiblerole->id; } } } if (empty($validroleids)) { return false; } $roleids = '(' . implode(',', $validroleids) . ')'; } else { return false; // No need to continue, since no roles have this capability set } /// Construct the main SQL $select = " SELECT u.id, u.firstname, u.lastname"; $from = " FROM {$CFG->prefix}user u\n INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id\n INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid"; $where = " WHERE ra.contextid " . get_related_contexts_string($context) . "\n AND u.deleted = 0\n AND ra.roleid in {$roleids}\n AND u.id NOT IN (SELECT userid\n FROM {$CFG->prefix}groups_members\n WHERE groupid = {$groupid})\n {$wheresearch}"; $groupby = " GROUP BY u.id, u.firstname, u.lastname "; return get_records_sql($select . $from . $where . $groupby); }
/** * Returns information about the users. * @param string $sqlwhere filter for the users */ function getUserData($sqlwhere) { return get_records_select_menu('user', $sqlwhere, 'fullname', 'id,' . sql_fullname() . ' AS fullname'); }
if ($context->id != $frontpagectx->id) { $where = "WHERE (r.contextid = {$context->id} OR r.contextid in {$listofcontexts})\n AND u.deleted = 0 {$selectrole}\n AND (ul.courseid = {$course->id} OR ul.courseid IS NULL)\n AND u.username != 'guest'\n {$adminroles}\n {$hiddensql} "; $where .= get_course_lastaccess_sql($accesssince); } else { if ($roleid >= 0) { $where = "WHERE (r.contextid = {$context->id} OR r.contextid in {$listofcontexts})\n AND u.deleted = 0 {$selectrole}\n AND u.username != 'guest'"; $where .= get_user_lastaccess_sql($accesssince); } else { $where = "WHERE u.deleted = 0\n AND u.username != 'guest'"; $where .= get_user_lastaccess_sql($accesssince); } } $wheresearch = ''; if (!empty($search)) { $LIKE = sql_ilike(); $fullname = sql_fullname('u.firstname', 'u.lastname'); $wheresearch .= ' AND (' . $fullname . ' ' . $LIKE . ' \'%' . $search . '%\' OR email ' . $LIKE . ' \'%' . $search . '%\' OR idnumber ' . $LIKE . ' \'%' . $search . '%\') '; } if ($currentgroup) { // Displaying a group by choice // FIX: TODO: This will not work if $currentgroup == 0, i.e. "those not in a group" $from .= 'LEFT JOIN ' . $CFG->prefix . 'groups_members gm ON u.id = gm.userid '; $where .= ' AND gm.groupid = ' . $currentgroup; } $totalcount = count_records_sql('SELECT COUNT(distinct u.id) ' . $from . $where); // Each user could have > 1 role if ($table->get_sql_where()) { $where .= ' AND ' . $table->get_sql_where(); } /// Always add r.hidden to sort in order to guarantee hiddens to "win" /// in the resolution of duplicates later - MDL-13935
/** * shortdesc (optional) * * longdesc * * @uses $CFG * @param string $sort ? * @param string $dir ? * @param int $categoryid ? * @param int $categoryid ? * @param string $search ? * @param string $firstinitial ? * @param string $lastinitial ? * @returnobject {@link $USER} records * @todo Finish documenting this function */ function get_users_listing($sort = 'lastaccess', $dir = 'ASC', $page = 0, $recordsperpage = 0, $search = '', $firstinitial = '', $lastinitial = '', $extraselect = '') { global $CFG; $LIKE = sql_ilike(); $fullname = sql_fullname(); $select = "deleted <> '1'"; if (!empty($search)) { $search = trim($search); $select .= " AND ({$fullname} {$LIKE} '%{$search}%' OR email {$LIKE} '%{$search}%' OR username='******') "; } if ($firstinitial) { $select .= ' AND firstname ' . $LIKE . ' \'' . $firstinitial . '%\' '; } if ($lastinitial) { $select .= ' AND lastname ' . $LIKE . ' \'' . $lastinitial . '%\' '; } if ($extraselect) { $select .= " AND {$extraselect} "; } if ($sort) { $sort = ' ORDER BY ' . $sort . ' ' . $dir; } /// warning: will return UNCONFIRMED USERS return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed, mnethostid\n FROM {$CFG->prefix}user\n WHERE {$select} {$sort}", $page, $recordsperpage); }
/** * Search through course users * * If $coursid specifies the site course then this function searches * through all undeleted and confirmed users * * @uses $CFG, $USER * @uses SITEID * @param int $courseid The course in question. * @param string $searchtext ? * @param string $sort ? * @param string $exceptions ? * @return array An array of {@link $USER} records. * @todo Finish documenting this function */ function message_search_users($courseid, $searchtext, $sort = '', $exceptions = '') { global $CFG, $USER; $fullname = sql_fullname(); $LIKE = sql_ilike(); if (!empty($exceptions)) { $except = ' AND u.id NOT IN (' . $exceptions . ') '; } else { $except = ''; } if (!empty($sort)) { $order = ' ORDER BY ' . $sort; } else { $order = ''; } $select = 'u.deleted = \'0\' AND u.confirmed = \'1\''; $fields = 'u.id, u.firstname, u.lastname, u.picture, u.imagealt, mc.id as contactlistid, mc.blocked'; if (!$courseid or $courseid == SITEID) { return get_records_sql("SELECT {$fields}\n FROM {$CFG->prefix}user u\n LEFT OUTER JOIN {$CFG->prefix}message_contacts mc\n ON mc.contactid = u.id AND mc.userid = {$USER->id} \n WHERE {$select}\n AND ({$fullname} {$LIKE} '%{$searchtext}%')\n {$except} {$order}"); } else { $context = get_context_instance(CONTEXT_COURSE, $courseid); $contextlists = get_related_contexts_string($context); // everyone who has a role assignement in this course or higher $users = get_records_sql("SELECT {$fields}\n FROM {$CFG->prefix}user u\n JOIN {$CFG->prefix}role_assignments ra\n ON ra.userid = u.id\n LEFT OUTER JOIN {$CFG->prefix}message_contacts mc\n ON mc.contactid = u.id AND mc.userid = {$USER->id} \n WHERE {$select}\n AND ra.contextid {$contextlists}\n AND ({$fullname} {$LIKE} '%{$searchtext}%')\n {$except} {$order}"); return $users; } }
/** * @param string $search the text to search for. * @param string $u the table alias for the user table in the query being * built. May be ''. * @return string SQL component to go in the where clause of the query */ protected function search_sql($search, $u) { $tests = array(); if ($u) { $u .= '.'; } // If we have a $search string, put a field LIKE '$search%' condition on each field. if ($search) { $conditions = array(sql_fullname($u . 'firstname', $u . 'lastname'), $conditions[] = $u . 'lastname'); foreach ($this->extrafields as $field) { $conditions[] = $u . $field; } $ilike = ' ' . sql_ilike(); if ($this->searchanywhere) { $searchparam = '%' . $search . '%'; } else { $searchparam = $search . '%'; } foreach ($conditions as &$condition) { $condition .= $ilike . "'{$searchparam}'"; } $tests[] = '(' . implode(' OR ', $conditions) . ')'; } // Add some additional sensible conditions $tests[] = $u . "username != 'guest'"; $tests[] = $u . 'deleted = 0'; $tests[] = $u . 'confirmed = 1'; // If we are being asked to exclude any users, do that. if (!empty($this->exclude)) { $tests[] = $u . 'id NOT IN (' . implode(',', $this->exclude) . ')'; } // If we are validating a set list of userids, add an id IN (...) test. if (!empty($this->validatinguserids)) { $tests[] = $u . 'id IN (' . implode(',', $this->validatinguserids) . ')'; } if (empty($tests)) { $tests[] = '1 = 1'; } // Combing the conditions and return. return implode(' AND ', $tests); }
$view_i++; $table->data[$view_i] = array(); $icons = ''; $icons .= '<a href="' . dirname($_SERVER['PHP_SELF']) . '/views_mod.php?courseid=' . $courseid . '&id=' . $view->id . '&sesskey=' . sesskey() . '&action=edit"><img src="' . $CFG->wwwroot . '/pix/t/edit.gif" class="iconsmall" alt="' . get_string("edit") . '" /></a> '; $icons .= '<a href="' . dirname($_SERVER['PHP_SELF']) . '/views_mod.php?courseid=' . $courseid . '&id=' . $view->id . '&sesskey=' . sesskey() . '&action=delete&confirm=1"><img src="' . $CFG->wwwroot . '/pix/t/delete.gif" class="iconsmall" alt="" . get_string("delete"). ""/></a> '; $table->data[$view_i]['name'] = '<a href="' . $CFG->wwwroot . '/blocks/exabis_eportfolio/shared_view.php?courseid=' . $courseid . '&access=id/' . $USER->id . '-' . $view->id . '">' . format_string($view->name) . "</a> (" . $icons . ") "; if ($view->description) { $table->data[$view_i]['name'] .= "<table width=\"98%\"><tr><td>" . format_text($view->description, FORMAT_HTML) . "</td></tr></table>"; } $table->data[$view_i]['timemodified'] = userdate($view->timemodified); $table->data[$view_i]['accessoptions'] = ''; if ($view->shareall) { $table->data[$view_i]['accessoptions'] .= '<div>' . get_string("internalaccess", "block_exabis_eportfolio") . ':</div><div style="padding-left: 10px;">' . get_string("internalaccessall", "block_exabis_eportfolio") . '</div>'; } else { // read users $query = "select " . sql_fullname() . " AS name" . " from {$CFG->prefix}user u" . " JOIN {$CFG->prefix}block_exabeporviewshar vshar WHERE u.id=vshar.userid AND vshar.viewid='" . $view->id . "'" . " ORDER BY name"; $users = get_records_sql($query); if ($users) { foreach ($users as &$user) { $user = $user->name; } $table->data[$view_i]['accessoptions'] .= '<div>' . get_string("internalaccessusers", "block_exabis_eportfolio") . ':</div><div style="padding-left: 10px;">' . join(', ', $users) . '</div>'; } } if ($view->externaccess) { if ($table->data[$view_i]['accessoptions']) { $style = 'padding-top: 10px;'; } else { $style = ''; } $url = block_exabis_eportfolio_get_external_view_url($view);
/** * Gets the users for a course who are not in a specified group, and returns * them in an array organised by role. For the array format, see * groups_get_members_by_role. * @param int $groupid The id of the group * @param string searchtext similar to searchtext in role assign, search * @return array An array of role id or '*' => information about that role * including a list of users */ function groups_get_users_not_in_group_by_role($courseid, $groupid, $searchtext = '', $sort = 'u.lastname ASC') { global $CFG; $context = get_context_instance(CONTEXT_COURSE, $courseid); if ($searchtext !== '') { // Search for a subset of remaining users $LIKE = sql_ilike(); $FULLNAME = sql_fullname(); $wheresearch = " AND u.id IN (SELECT id FROM {$CFG->prefix}user WHERE {$FULLNAME} {$LIKE} '%{$searchtext}%' OR email {$LIKE} '%{$searchtext}%' )"; } else { $wheresearch = ''; } /// Get list of allowed roles if (!($validroleids = groups_get_possible_roles($context))) { return; } $roleids = '(' . implode(',', $validroleids) . ')'; /// Construct the main SQL $select = " SELECT r.id AS roleid,r.shortname AS roleshortname,r.name AS rolename,\n u.id AS userid, u.firstname, u.lastname"; $from = " FROM {$CFG->prefix}user u\n INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id\n INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid"; $where = " WHERE ra.contextid " . get_related_contexts_string($context) . "\n AND u.deleted = 0\n AND ra.roleid in {$roleids}\n AND u.id NOT IN (SELECT userid\n FROM {$CFG->prefix}groups_members\n WHERE groupid = {$groupid})\n {$wheresearch}"; $orderby = " ORDER BY {$sort}"; return groups_calculate_role_people(get_recordset_sql($select . $from . $where . $orderby), $context); }
/** * Creates known user filter if present * @param string $fieldname * @param boolean $advanced * @return object filter */ function get_field($fieldname, $advanced) { global $USER; switch ($fieldname) { case 'username': return new user_filter_text('username', get_string('username'), $advanced, 'username'); case 'realname': return new user_filter_text('realname', get_string('fullname'), $advanced, sql_fullname()); case 'lastname': return new user_filter_text('lastname', get_string('lastname'), $advanced, 'lastname'); case 'firstname': return new user_filter_text('firstname', get_string('firstname'), $advanced, 'firstname'); case 'email': return new user_filter_text('email', get_string('email'), $advanced, 'email'); case 'city': return new user_filter_text('city', get_string('city'), $advanced, 'city'); case 'country': return new user_filter_select('country', get_string('country'), $advanced, 'country', get_list_of_countries(), $USER->country); case 'confirmed': return new user_filter_yesno('confirmed', get_string('confirm'), $advanced, 'confirmed'); case 'profile': return new user_filter_profilefield('profile', get_string('profile'), $advanced); case 'courserole': return new user_filter_courserole('courserole', get_string('courserole', 'filters'), $advanced); case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced); case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess'); case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess'); case 'lastlogin': return new user_filter_date('lastlogin', get_string('lastlogin'), $advanced, 'lastlogin'); case 'auth': $plugins = get_list_of_plugins('auth'); $choices = array(); foreach ($plugins as $auth) { $choices[$auth] = get_string("auth_{$auth}title", 'auth'); } return new user_filter_simpleselect('auth', get_string('authentication'), $advanced, 'auth', $choices); case 'mnethostid': if (!($hosts = get_records('mnet_host', '', '', 'id', 'id, wwwroot, name'))) { return null; } $choices = array(); foreach ($hosts as $host) { if (empty($host->wwwroot)) { continue; // skip all hosts } $choices[$host->id] = $host->name . ' (' . $host->wwwroot . ')'; } if (count($choices < 2)) { return null; // filter not needed } return new user_filter_simpleselect('mnethostid', 'mnethostid', $advanced, 'mnethostid', $choices); default: return null; } }
/** * Get a list of usage ids where the question with slot $slot, and optionally * also with question id $questionid, is in summary state $summarystate. Also * return the total count of such states. * * Only a subset of the ids can be returned by using $orderby, $limitfrom and * $limitnum. A special value 'random' can be passed as $orderby, in which case * $limitfrom is ignored. * * @param int $slot The slot for the questions you want to konw about. * @param int $questionid (optional) Only return attempts that were of this specific question. * @param string $summarystate 'all', 'needsgrading', 'autograded' or 'manuallygraded'. * @param string $orderby 'random', 'date', 'student' or 'idnumber'. * @param int $page implements paging of the results. * Ignored if $orderby = random or $pagesize is null. * @param int $pagesize implements paging of the results. null = all. */ protected function get_usage_ids_where_question_in_state($summarystate, $slot, $questionid = null, $orderby = 'random', $page = 0, $pagesize = null) { global $CFG; $dm = new question_engine_data_mapper(); if ($pagesize && $orderby != 'random') { $limitfrom = $page * $pagesize; } else { $limitfrom = 0; } $qubaids = $this->get_qubaids_condition(); $params = array(); if ($orderby == 'date') { list($statetest, $params) = $dm->in_summary_state_test('manuallygraded', false, 'mangrstate'); $orderby = "(\n SELECT MAX(sortqas.timecreated)\n FROM {question_attempt_steps} sortqas\n WHERE sortqas.questionattemptid = qa.id\n AND sortqas.state {$statetest}\n )"; } else { if ($orderby == 'student' || $orderby == 'idnumber') { $qubaids->from .= " JOIN {user} u ON quiza.userid = u.id "; if ($orderby == 'student') { $orderby = sql_fullname('u.firstname', 'u.lastname'); } } } return $dm->load_questions_usages_where_question_in_state($qubaids, $summarystate, $slot, $questionid, $orderby, $params, $limitfrom, $pagesize); }
break; case 2: redirect($CFG->wwwroot . '/admin/user/user_bulk_message.php'); break; case 3: redirect($CFG->wwwroot . '/admin/user/user_bulk_delete.php'); break; case 4: redirect($CFG->wwwroot . '/admin/user/user_bulk_display.php'); break; default: break; } // prepare user filter types $filters[] = new user_filter_text('username', get_string('username'), 'username'); $filters[] = new user_filter_text('realname', get_string('fullname'), sql_fullname()); $filters[] = new user_filter_text('email', get_string('email'), 'email'); $filters[] = new user_filter_text('city', get_string('city'), 'city'); $filters[] = new user_filter_select('country', get_string('country'), 'country', get_list_of_countries()); $filters[] = new user_filter_yesno('confirmed', get_string('confirm'), 'confirmed'); $filters[] = new user_filter_profilefield('profile', get_string('profile')); $filters[] = new user_filter_courserole('course', get_string('courserole', 'filters')); $filters[] = new user_filter_globalrole('system', get_string('globalrole', 'role')); $filters[] = new user_filter_date('date', get_string('date'), 'lastaccess', array('lastlogin' => get_string('lastlogin'), 'firstaccess' => get_string('firstaccess', 'filters'), 'lastaccess' => get_string('lastaccess'), 'timemodified' => get_string('lastmodified'))); // create the user filter form $user_filter_form =& new user_filter_form(null, $filters); // do output admin_externalpage_setup('userbulk'); admin_externalpage_print_header(); // put the user filter form first $user_filter_form->display();
/** * who has this capability in this context * does not handling user level resolving!!! * i.e 1 person has 2 roles 1 allow, 1 prevent, this will not work properly * @param $context - object * @param $capability - string capability * @param $fields - fields to be pulled * @param $sort - the sort order * @param $limitfrom - number of records to skip (offset) * @param $limitnum - number of records to fetch * @param $groups - single group or array of groups - group(s) user is in * @param $exceptions - list of users to exclude */ function internalmail_get_users_by_capability($context, $capability, $fields = '', $sort = 'u.firstname', $limitfrom = '', $limitnum = '', $groups = '', $exceptions = '', $doanything = true, $search = '', $firstinitial = '', $lastinitial = '') { global $CFG, $USER, $COURSE; /// Sorting out groups if ($groups !== '') { $groupjoin = 'INNER JOIN ' . $CFG->prefix . 'groups_members gm ON gm.userid = ra.userid'; if (is_array($groups)) { $groupsql = 'AND gm.groupid IN (' . implode(',', $groups) . ')'; } else { if ($groups == 0) { if (!has_capability('block/email_list:viewallgroups', $context) && $COURSE->groupmode == 1) { $groupids = groups_get_groups_for_user($USER->id, $COURSE->id); $groupsql = 'AND gm.groupid IN (' . implode(',', $groupids) . ')'; } else { $groupsql = ''; } } else { $groupsql = 'AND gm.groupid = ' . $groups; } } } else { $groupjoin = ''; $groupsql = ''; } /// Sorting out exceptions $exceptionsql = $exceptions ? "AND u.id NOT IN ({$exceptions})" : ''; /// Set up default fields if (empty($fields)) { $fields = 'u.*, ul.timeaccess as lastaccess, ra.hidden'; } /// Set up default sort if (empty($sort)) { $sortby = 'ul.timeaccess'; } $sortby = $sort ? " ORDER BY {$sort} " : ''; /// If context is a course, then construct sql for ul if ($context->contextlevel == CONTEXT_COURSE) { $courseid = $context->instanceid; $coursesql = "AND (ul.courseid = {$courseid} OR ul.courseid IS NULL)"; } else { $coursesql = ''; } $LIKE = sql_ilike(); $fullname = sql_fullname(); $search_sql = ''; if (!empty($search)) { $search = trim($search); $search_sql .= " AND ({$fullname} {$LIKE} '%{$search}%' OR email {$LIKE} '%{$search}%' OR username {$LIKE} '%{$search}%' OR idnumber {$LIKE} '%{$search}%') "; } if ($firstinitial) { $search_sql .= ' AND firstname ' . $LIKE . ' \'' . $firstinitial . '%\''; } if ($lastinitial) { $search_sql .= ' AND lastname ' . $LIKE . ' \'' . $lastinitial . '%\''; } /// Sorting out roles with this capability set if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context)) { if (!$doanything) { if (!($sitecontext = get_context_instance(CONTEXT_SYSTEM))) { return false; // Something is seriously wrong } $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext); } $validroleids = array(); foreach ($possibleroles as $possiblerole) { if (!$doanything) { if (isset($doanythingroles[$possiblerole->id])) { // We don't want these included continue; } } if ($caps = role_context_capabilities($possiblerole->id, $context, $capability)) { // resolved list if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0 $validroleids[] = $possiblerole->id; } } } if (empty($validroleids)) { return false; } $roleids = '(' . implode(',', $validroleids) . ')'; } else { return false; // No need to continue, since no roles have this capability set } /// Construct the main SQL $select = " SELECT {$fields}"; $from = " FROM {$CFG->prefix}user u\r\n INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id\r\n INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid\r\n LEFT OUTER JOIN {$CFG->prefix}user_lastaccess ul ON ul.userid = u.id\r\n {$groupjoin}"; $where = " WHERE ra.contextid " . get_related_contexts_string($context) . "\r\n AND u.deleted = 0\r\n AND ra.roleid in {$roleids}\r\n {$exceptionsql}\r\n {$coursesql}\r\n {$groupsql}\r\n {$search_sql}"; return get_records_sql($select . $from . $where . $sortby, $limitfrom, $limitnum); }
print_box(get_string('globalroleswarning', 'role')); } if ($roleid) { /// prints a form to swap roles /// Get all existing participants in this context. // Why is this not done with get_users??? if (!($contextusers = get_role_users($roleid, $context, false, 'u.id, u.firstname, u.lastname, u.email, ra.hidden'))) { $contextusers = array(); } $select = "username <> 'guest' AND deleted = 0 AND confirmed = 1"; $usercount = count_records_select('user', $select) - count($contextusers); $searchtext = trim($searchtext); if ($searchtext !== '') { // Search for a subset of remaining users $LIKE = sql_ilike(); $FULLNAME = sql_fullname(); $selectsql = " AND ({$FULLNAME} {$LIKE} '%{$searchtext}%' OR email {$LIKE} '%{$searchtext}%') "; $select .= $selectsql; } else { $selectsql = ""; } if ($context->contextlevel > CONTEXT_COURSE && !is_inside_frontpage($context)) { // mod or block (or group?) /************************************************************************ * * * context level is above or equal course context level * * in this case we pull out all users matching search criteria (if any) * * * * MDL-11324 * * a mini get_users_by_capability() call here, this is done instead of * * get_users_by_capability() because *
/** * Make Custom Filter Options * * This function handles filters that require custom values (languages, countries, etc). * * @param string $group The index of the group to which the sub filter belongs to. * @param string $name The name of the sub filter to process. * @param array $help An array representing the help icon for the filter * @return array The customized options for the selected sub-filter */ function make_filter_options_custom($options, $group, $name) { switch ($name) { case 'fullname': //combine the firstname and lastname into a fullname field $firstname = $this->tables[$group]['crlm_user'] . '.firstname'; $lastname = $this->tables[$group]['crlm_user'] . '.lastname'; $options['dbfield'] = sql_fullname($firstname, $lastname); $options['talias'] = ''; //todo: find a better way to do this $this->fieldtofiltermap[$group][$options['dbfield']] = generalized_filter_elisuserprofile::filtertypetext; break; case 'country': //populate dropdown entries for countries $countries = cm_get_list_of_countries(); $options['choices'] = $countries; break; case 'language': //populate dropdown entries for languages $languages = cm_get_list_of_languages(); $options['choices'] = $languages; break; case 'inactive': //populate dropdown entries for inactive flag filtering options $options['choices'] = array('0' => get_string('no'), 1 => get_string('yes')); $options['numeric'] = 1; break; } $pos = strpos($name, 'customfield-'); if ($pos !== false) { $options['contextlevel'] = context_level_base::get_custom_context_level('user', 'block_curr_admin'); } return $options; }