index.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Lists all the users within a given course.
  18. *
  19. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core_user
  22. */
  23. require_once('../config.php');
  24. require_once($CFG->dirroot.'/user/lib.php');
  25. require_once($CFG->libdir.'/tablelib.php');
  26. require_once($CFG->libdir.'/filelib.php');
  27. define('USER_SMALL_CLASS', 20); // Below this is considered small.
  28. define('USER_LARGE_CLASS', 200); // Above this is considered large.
  29. define('DEFAULT_PAGE_SIZE', 20);
  30. define('SHOW_ALL_PAGE_SIZE', 5000);
  31. define('MODE_BRIEF', 0);
  32. define('MODE_USERDETAILS', 1);
  33. $page = optional_param('page', 0, PARAM_INT); // Which page to show.
  34. $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
  35. $mode = optional_param('mode', null, PARAM_INT); // Use the MODE_ constants.
  36. $accesssince = optional_param('accesssince', 0, PARAM_INT); // Filter by last access. -1 = never.
  37. $search = optional_param('search', '', PARAM_RAW); // Make sure it is processed with p() or s() when sending to output!
  38. $roleid = optional_param('roleid', 0, PARAM_INT); // Optional roleid, 0 means all enrolled users (or all on the frontpage).
  39. $contextid = optional_param('contextid', 0, PARAM_INT); // One of this or.
  40. $courseid = optional_param('id', 0, PARAM_INT); // This are required.
  41. $selectall = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
  42. $PAGE->set_url('/user/index.php', array(
  43. 'page' => $page,
  44. 'perpage' => $perpage,
  45. 'mode' => $mode,
  46. 'accesssince' => $accesssince,
  47. 'search' => $search,
  48. 'roleid' => $roleid,
  49. 'contextid' => $contextid,
  50. 'id' => $courseid));
  51. if ($contextid) {
  52. $context = context::instance_by_id($contextid, MUST_EXIST);
  53. if ($context->contextlevel != CONTEXT_COURSE) {
  54. print_error('invalidcontext');
  55. }
  56. $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
  57. } else {
  58. $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  59. $context = context_course::instance($course->id, MUST_EXIST);
  60. }
  61. // Not needed anymore.
  62. unset($contextid);
  63. unset($courseid);
  64. require_login($course);
  65. $systemcontext = context_system::instance();
  66. $isfrontpage = ($course->id == SITEID);
  67. $frontpagectx = context_course::instance(SITEID);
  68. if ($isfrontpage) {
  69. $PAGE->set_pagelayout('admin');
  70. require_capability('moodle/site:viewparticipants', $systemcontext);
  71. } else {
  72. $PAGE->set_pagelayout('incourse');
  73. require_capability('moodle/course:viewparticipants', $context);
  74. }
  75. $rolenamesurl = new moodle_url("$CFG->wwwroot/user/index.php?contextid=$context->id&sifirst=&silast=");
  76. $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true);
  77. if ($isfrontpage) {
  78. $rolenames[0] = get_string('allsiteusers', 'role');
  79. } else {
  80. $rolenames[0] = get_string('allparticipants');
  81. }
  82. // Make sure other roles may not be selected by any means.
  83. if (empty($rolenames[$roleid])) {
  84. print_error('noparticipants');
  85. }
  86. // No roles to display yet?
  87. // frontpage course is an exception, on the front page course we should display all users.
  88. if (empty($rolenames) && !$isfrontpage) {
  89. if (has_capability('moodle/role:assign', $context)) {
  90. redirect($CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id);
  91. } else {
  92. print_error('noparticipants');
  93. }
  94. }
  95. // Trigger events.
  96. user_list_view($course, $context);
  97. $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
  98. $countries = get_string_manager()->get_list_of_countries();
  99. $strnever = get_string('never');
  100. $datestring = new stdClass();
  101. $datestring->year = get_string('year');
  102. $datestring->years = get_string('years');
  103. $datestring->day = get_string('day');
  104. $datestring->days = get_string('days');
  105. $datestring->hour = get_string('hour');
  106. $datestring->hours = get_string('hours');
  107. $datestring->min = get_string('min');
  108. $datestring->mins = get_string('mins');
  109. $datestring->sec = get_string('sec');
  110. $datestring->secs = get_string('secs');
  111. if ($mode !== null) {
  112. $mode = (int)$mode;
  113. $SESSION->userindexmode = $mode;
  114. } else if (isset($SESSION->userindexmode)) {
  115. $mode = (int)$SESSION->userindexmode;
  116. } else {
  117. $mode = MODE_BRIEF;
  118. }
  119. // Check to see if groups are being used in this course
  120. // and if so, set $currentgroup to reflect the current group.
  121. $groupmode = groups_get_course_groupmode($course); // Groups are being used.
  122. $currentgroup = groups_get_course_group($course, true);
  123. if (!$currentgroup) { // To make some other functions work better later.
  124. $currentgroup = null;
  125. }
  126. $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
  127. $PAGE->set_title("$course->shortname: ".get_string('participants'));
  128. $PAGE->set_heading($course->fullname);
  129. $PAGE->set_pagetype('course-view-' . $course->format);
  130. $PAGE->add_body_class('path-user'); // So we can style it independently.
  131. $PAGE->set_other_editing_capability('moodle/course:manageactivities');
  132. echo $OUTPUT->header();
  133. echo $OUTPUT->heading(get_string('participants'));
  134. echo '<div class="userlist">';
  135. if ($isseparategroups and (!$currentgroup) ) {
  136. // The user is not in the group so show message and exit.
  137. echo $OUTPUT->heading(get_string("notingroup"));
  138. echo $OUTPUT->footer();
  139. exit;
  140. }
  141. // Should use this variable so that we don't break stuff every time a variable is added or changed.
  142. $baseurl = new moodle_url('/user/index.php', array(
  143. 'contextid' => $context->id,
  144. 'roleid' => $roleid,
  145. 'id' => $course->id,
  146. 'perpage' => $perpage,
  147. 'accesssince' => $accesssince,
  148. 'search' => s($search)));
  149. // Setting up tags.
  150. if ($course->id == SITEID) {
  151. $filtertype = 'site';
  152. } else if ($course->id && !$currentgroup) {
  153. $filtertype = 'course';
  154. $filterselect = $course->id;
  155. } else {
  156. $filtertype = 'group';
  157. $filterselect = $currentgroup;
  158. }
  159. // Get the hidden field list.
  160. if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
  161. $hiddenfields = array(); // Teachers and admins are allowed to see everything.
  162. } else {
  163. $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
  164. }
  165. if (isset($hiddenfields['lastaccess'])) {
  166. // Do not allow access since filtering.
  167. $accesssince = 0;
  168. }
  169. // Print settings and things in a table across the top.
  170. $controlstable = new html_table();
  171. $controlstable->attributes['class'] = 'controls';
  172. $controlstable->cellspacing = 0;
  173. $controlstable->data[] = new html_table_row();
  174. // Print my course menus.
  175. if ($mycourses = enrol_get_my_courses()) {
  176. $courselist = array();
  177. $popupurl = new moodle_url('/user/index.php?roleid='.$roleid.'&sifirst=&silast=');
  178. foreach ($mycourses as $mycourse) {
  179. $coursecontext = context_course::instance($mycourse->id);
  180. $courselist[$mycourse->id] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
  181. }
  182. if (has_capability('moodle/site:viewparticipants', $systemcontext)) {
  183. unset($courselist[SITEID]);
  184. $courselist = array(SITEID => format_string($SITE->shortname, true, array('context' => $systemcontext))) + $courselist;
  185. }
  186. $select = new single_select($popupurl, 'id', $courselist, $course->id, null, 'courseform');
  187. $select->set_label(get_string('mycourses'));
  188. $controlstable->data[0]->cells[] = $OUTPUT->render($select);
  189. }
  190. if ($groupmenu = groups_print_course_menu($course, $baseurl->out(), true)) {
  191. $controlstable->data[0]->cells[] = $groupmenu;
  192. }
  193. if (!isset($hiddenfields['lastaccess'])) {
  194. // Get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
  195. // We need to make it diferently for normal courses and site course.
  196. if (!$isfrontpage) {
  197. $minlastaccess = $DB->get_field_sql('SELECT min(timeaccess)
  198. FROM {user_lastaccess}
  199. WHERE courseid = ?
  200. AND timeaccess != 0', array($course->id));
  201. $lastaccess0exists = $DB->record_exists('user_lastaccess', array('courseid' => $course->id, 'timeaccess' => 0));
  202. } else {
  203. $minlastaccess = $DB->get_field_sql('SELECT min(lastaccess)
  204. FROM {user}
  205. WHERE lastaccess != 0');
  206. $lastaccess0exists = $DB->record_exists('user', array('lastaccess' => 0));
  207. }
  208. $now = usergetmidnight(time());
  209. $timeaccess = array();
  210. $baseurl->remove_params('accesssince');
  211. // Makes sense for this to go first.
  212. $timeoptions[0] = get_string('selectperiod');
  213. // Days.
  214. for ($i = 1; $i < 7; $i++) {
  215. if (strtotime('-'.$i.' days', $now) >= $minlastaccess) {
  216. $timeoptions[strtotime('-'.$i.' days', $now)] = get_string('numdays', 'moodle', $i);
  217. }
  218. }
  219. // Weeks.
  220. for ($i = 1; $i < 10; $i++) {
  221. if (strtotime('-'.$i.' weeks', $now) >= $minlastaccess) {
  222. $timeoptions[strtotime('-'.$i.' weeks', $now)] = get_string('numweeks', 'moodle', $i);
  223. }
  224. }
  225. // Months.
  226. for ($i = 2; $i < 12; $i++) {
  227. if (strtotime('-'.$i.' months', $now) >= $minlastaccess) {
  228. $timeoptions[strtotime('-'.$i.' months', $now)] = get_string('nummonths', 'moodle', $i);
  229. }
  230. }
  231. // Try a year.
  232. if (strtotime('-1 year', $now) >= $minlastaccess) {
  233. $timeoptions[strtotime('-1 year', $now)] = get_string('lastyear');
  234. }
  235. if (!empty($lastaccess0exists)) {
  236. $timeoptions[-1] = get_string('never');
  237. }
  238. if (count($timeoptions) > 1) {
  239. $select = new single_select($baseurl, 'accesssince', $timeoptions, $accesssince, null, 'timeoptions');
  240. $select->set_label(get_string('usersnoaccesssince'));
  241. $controlstable->data[0]->cells[] = $OUTPUT->render($select);
  242. }
  243. }
  244. $formatmenu = array( '0' => get_string('brief'),
  245. '1' => get_string('userdetails'));
  246. $select = new single_select($baseurl, 'mode', $formatmenu, $mode, null, 'formatmenu');
  247. $select->set_label(get_string('userlist'));
  248. $userlistcell = new html_table_cell();
  249. $userlistcell->attributes['class'] = 'right';
  250. $userlistcell->text = $OUTPUT->render($select);
  251. $controlstable->data[0]->cells[] = $userlistcell;
  252. echo html_writer::table($controlstable);
  253. if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) {
  254. // Display info about the group.
  255. if ($group = groups_get_group($currentgroup)) {
  256. if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
  257. $groupinfotable = new html_table();
  258. $groupinfotable->attributes['class'] = 'groupinfobox';
  259. $picturecell = new html_table_cell();
  260. $picturecell->attributes['class'] = 'left side picture';
  261. $picturecell->text = print_group_picture($group, $course->id, true, true, false);
  262. $contentcell = new html_table_cell();
  263. $contentcell->attributes['class'] = 'content';
  264. $contentheading = $group->name;
  265. if (has_capability('moodle/course:managegroups', $context)) {
  266. $aurl = new moodle_url('/group/group.php', array('id' => $group->id, 'courseid' => $group->courseid));
  267. $contentheading .= '&nbsp;' . $OUTPUT->action_icon($aurl, new pix_icon('t/edit', get_string('editgroupprofile')));
  268. }
  269. $group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group',
  270. 'description', $group->id);
  271. if (!isset($group->descriptionformat)) {
  272. $group->descriptionformat = FORMAT_MOODLE;
  273. }
  274. $options = array('overflowdiv' => true);
  275. $formatteddesc = format_text($group->description, $group->descriptionformat, $options);
  276. $contentcell->text = $OUTPUT->heading($contentheading, 3) . $formatteddesc;
  277. $groupinfotable->data[] = new html_table_row(array($picturecell, $contentcell));
  278. echo html_writer::table($groupinfotable);
  279. }
  280. }
  281. }
  282. // Define a table showing a list of users in the current role selection.
  283. $tablecolumns = array();
  284. $tableheaders = array();
  285. if ($bulkoperations && $mode === MODE_BRIEF) {
  286. $tablecolumns[] = 'select';
  287. $tableheaders[] = get_string('select');
  288. }
  289. $tablecolumns[] = 'userpic';
  290. $tablecolumns[] = 'fullname';
  291. $extrafields = get_extra_user_fields($context);
  292. $tableheaders[] = get_string('userpic');
  293. $tableheaders[] = get_string('fullnameuser');
  294. if ($mode === MODE_BRIEF) {
  295. foreach ($extrafields as $field) {
  296. $tablecolumns[] = $field;
  297. $tableheaders[] = get_user_field_name($field);
  298. }
  299. }
  300. if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
  301. $tablecolumns[] = 'city';
  302. $tableheaders[] = get_string('city');
  303. }
  304. if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
  305. $tablecolumns[] = 'country';
  306. $tableheaders[] = get_string('country');
  307. }
  308. if (!isset($hiddenfields['lastaccess'])) {
  309. $tablecolumns[] = 'lastaccess';
  310. if ($course->id == SITEID) {
  311. // Exception case for viewing participants on site home.
  312. $tableheaders[] = get_string('lastsiteaccess');
  313. } else {
  314. $tableheaders[] = get_string('lastcourseaccess');
  315. }
  316. }
  317. if ($bulkoperations && $mode === MODE_USERDETAILS) {
  318. $tablecolumns[] = 'select';
  319. $tableheaders[] = get_string('select');
  320. }
  321. $table = new flexible_table('user-index-participants-'.$course->id);
  322. $table->define_columns($tablecolumns);
  323. $table->define_headers($tableheaders);
  324. $table->define_baseurl($baseurl->out());
  325. if (!isset($hiddenfields['lastaccess'])) {
  326. $table->sortable(true, 'lastaccess', SORT_DESC);
  327. } else {
  328. $table->sortable(true, 'firstname', SORT_ASC);
  329. }
  330. $table->no_sorting('roles');
  331. $table->no_sorting('groups');
  332. $table->no_sorting('groupings');
  333. $table->no_sorting('select');
  334. $table->set_attribute('cellspacing', '0');
  335. $table->set_attribute('id', 'participants');
  336. $table->set_attribute('class', 'generaltable generalbox');
  337. $table->set_control_variables(array(
  338. TABLE_VAR_SORT => 'ssort',
  339. TABLE_VAR_HIDE => 'shide',
  340. TABLE_VAR_SHOW => 'sshow',
  341. TABLE_VAR_IFIRST => 'sifirst',
  342. TABLE_VAR_ILAST => 'silast',
  343. TABLE_VAR_PAGE => 'spage'
  344. ));
  345. $table->setup();
  346. list($esql, $params) = get_enrolled_sql($context, null, $currentgroup, true);
  347. $joins = array("FROM {user} u");
  348. $wheres = array();
  349. $userfields = array('username', 'email', 'city', 'country', 'lang', 'timezone', 'maildisplay');
  350. $mainuserfields = user_picture::fields('u', $userfields);
  351. $extrasql = get_extra_user_fields_sql($context, 'u', '', $userfields);
  352. if ($isfrontpage) {
  353. $select = "SELECT $mainuserfields, u.lastaccess$extrasql";
  354. $joins[] = "JOIN ($esql) e ON e.id = u.id"; // Everybody on the frontpage usually.
  355. if ($accesssince) {
  356. $wheres[] = get_user_lastaccess_sql($accesssince);
  357. }
  358. } else {
  359. $select = "SELECT $mainuserfields, COALESCE(ul.timeaccess, 0) AS lastaccess$extrasql";
  360. $joins[] = "JOIN ($esql) e ON e.id = u.id"; // Course enrolled users only.
  361. $joins[] = "LEFT JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = :courseid)"; // Not everybody accessed course yet.
  362. $params['courseid'] = $course->id;
  363. if ($accesssince) {
  364. $wheres[] = get_course_lastaccess_sql($accesssince);
  365. }
  366. }
  367. // Performance hacks - we preload user contexts together with accounts.
  368. $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
  369. $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
  370. $params['contextlevel'] = CONTEXT_USER;
  371. $select .= $ccselect;
  372. $joins[] = $ccjoin;
  373. // Limit list to users with some role only.
  374. if ($roleid) {
  375. // We want to query both the current context and parent contexts.
  376. list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
  377. $wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $relatedctxsql)";
  378. $params = array_merge($params, array('roleid' => $roleid), $relatedctxparams);
  379. }
  380. $from = implode("\n", $joins);
  381. if ($wheres) {
  382. $where = "WHERE " . implode(" AND ", $wheres);
  383. } else {
  384. $where = "";
  385. }
  386. $totalcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
  387. if (!empty($search)) {
  388. $fullname = $DB->sql_fullname('u.firstname', 'u.lastname');
  389. $wheres[] = "(". $DB->sql_like($fullname, ':search1', false, false) .
  390. " OR ". $DB->sql_like('email', ':search2', false, false) .
  391. " OR ". $DB->sql_like('idnumber', ':search3', false, false) .") ";
  392. $params['search1'] = "%$search%";
  393. $params['search2'] = "%$search%";
  394. $params['search3'] = "%$search%";
  395. }
  396. list($twhere, $tparams) = $table->get_sql_where();
  397. if ($twhere) {
  398. $wheres[] = $twhere;
  399. $params = array_merge($params, $tparams);
  400. }
  401. $from = implode("\n", $joins);
  402. if ($wheres) {
  403. $where = "WHERE " . implode(" AND ", $wheres);
  404. } else {
  405. $where = "";
  406. }
  407. if ($table->get_sql_sort()) {
  408. $sort = ' ORDER BY '.$table->get_sql_sort();
  409. } else {
  410. $sort = '';
  411. }
  412. $matchcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
  413. $table->initialbars(true);
  414. $table->pagesize($perpage, $matchcount);
  415. // List of users at the current visible page - paging makes it relatively short.
  416. $userlist = $DB->get_recordset_sql("$select $from $where $sort", $params, $table->get_page_start(), $table->get_page_size());
  417. // If there are multiple Roles in the course, then show a drop down menu for switching.
  418. if (count($rolenames) > 1) {
  419. echo '<div class="rolesform">';
  420. echo $OUTPUT->single_select($rolenamesurl, 'roleid', $rolenames, $roleid, null,
  421. 'rolesform', array('label' => get_string('currentrole', 'role')));
  422. echo '</div>';
  423. } else if (count($rolenames) == 1) {
  424. // When all users with the same role - print its name.
  425. echo '<div class="rolesform">';
  426. echo get_string('role').get_string('labelsep', 'langconfig');
  427. $rolename = reset($rolenames);
  428. echo $rolename;
  429. echo '</div>';
  430. }
  431. $editlink = '';
  432. if ($course->id != SITEID && has_capability('moodle/course:enrolreview', $context)) {
  433. $editlink = new moodle_url('/enrol/users.php', array('id' => $course->id));
  434. }
  435. if ($roleid > 0) {
  436. $a = new stdClass();
  437. $a->number = $totalcount;
  438. $a->role = $rolenames[$roleid];
  439. $heading = format_string(get_string('xuserswiththerole', 'role', $a));
  440. if ($currentgroup and !empty($group)) {
  441. $a->group = $group->name;
  442. $heading .= ' ' . format_string(get_string('ingroup', 'role', $a));
  443. }
  444. if ($accesssince && !empty($timeoptions[$accesssince])) {
  445. $a->timeperiod = $timeoptions[$accesssince];
  446. $heading .= ' ' . format_string(get_string('inactiveformorethan', 'role', $a));
  447. }
  448. $heading .= ": $a->number";
  449. if (!empty($editlink)) {
  450. $editlink->param('role', $roleid);
  451. $heading .= $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit')));
  452. }
  453. echo $OUTPUT->heading($heading, 3);
  454. } else {
  455. if ($course->id == SITEID and $roleid < 0) {
  456. $strallparticipants = get_string('allsiteusers', 'role');
  457. } else {
  458. $strallparticipants = get_string('allparticipants');
  459. }
  460. if (!empty($editlink)) {
  461. $editlink = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit')));
  462. }
  463. if ($matchcount < $totalcount) {
  464. echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount.'/'.$totalcount . $editlink, 3);
  465. } else {
  466. echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount . $editlink, 3);
  467. }
  468. }
  469. if ($bulkoperations) {
  470. echo '<form action="action_redir.php" method="post" id="participantsform">';
  471. echo '<div>';
  472. echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
  473. echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
  474. }
  475. if ($mode === MODE_USERDETAILS) { // Print simple listing.
  476. if ($totalcount < 1) {
  477. echo $OUTPUT->heading(get_string('nothingtodisplay'));
  478. } else {
  479. if ($totalcount > $perpage) {
  480. $firstinitial = $table->get_initial_first();
  481. $lastinitial = $table->get_initial_last();
  482. $strall = get_string('all');
  483. $alpha = explode(',', get_string('alphabet', 'langconfig'));
  484. // Bar of first initials.
  485. echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
  486. if (!empty($firstinitial)) {
  487. echo '<a href="'.$baseurl->out().'&amp;sifirst=">'.$strall.'</a>';
  488. } else {
  489. echo '<strong>'.$strall.'</strong>';
  490. }
  491. foreach ($alpha as $letter) {
  492. if ($letter == $firstinitial) {
  493. echo ' <strong>'.$letter.'</strong>';
  494. } else {
  495. echo ' <a href="'.$baseurl->out().'&amp;sifirst='.$letter.'">'.$letter.'</a>';
  496. }
  497. }
  498. echo '</div>';
  499. // Bar of last initials.
  500. echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
  501. if (!empty($lastinitial)) {
  502. echo '<a href="'.$baseurl->out().'&amp;silast=">'.$strall.'</a>';
  503. } else {
  504. echo '<strong>'.$strall.'</strong>';
  505. }
  506. foreach ($alpha as $letter) {
  507. if ($letter == $lastinitial) {
  508. echo ' <strong>'.$letter.'</strong>';
  509. } else {
  510. echo ' <a href="'.$baseurl->out().'&amp;silast='.$letter.'">'.$letter.'</a>';
  511. }
  512. }
  513. echo '</div>';
  514. $pagingbar = new paging_bar($matchcount, intval($table->get_page_start() / $perpage), $perpage, $baseurl);
  515. $pagingbar->pagevar = 'spage';
  516. echo $OUTPUT->render($pagingbar);
  517. }
  518. if ($matchcount > 0) {
  519. $usersprinted = array();
  520. foreach ($userlist as $user) {
  521. if (in_array($user->id, $usersprinted)) { // Prevent duplicates by r.hidden - MDL-13935.
  522. continue;
  523. }
  524. $usersprinted[] = $user->id; // Add new user to the array of users printed.
  525. context_helper::preload_from_record($user);
  526. $context = context_course::instance($course->id);
  527. $usercontext = context_user::instance($user->id);
  528. $countries = get_string_manager()->get_list_of_countries();
  529. // Get the hidden field list.
  530. if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
  531. $hiddenfields = array();
  532. } else {
  533. $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
  534. }
  535. $table = new html_table();
  536. $table->attributes['class'] = 'userinfobox';
  537. $row = new html_table_row();
  538. $row->cells[0] = new html_table_cell();
  539. $row->cells[0]->attributes['class'] = 'left side';
  540. $row->cells[0]->text = $OUTPUT->user_picture($user, array('size' => 100, 'courseid' => $course->id));
  541. $row->cells[1] = new html_table_cell();
  542. $row->cells[1]->attributes['class'] = 'content';
  543. $row->cells[1]->text = $OUTPUT->container(fullname($user, has_capability('moodle/site:viewfullnames', $context)), 'username');
  544. $row->cells[1]->text .= $OUTPUT->container_start('info');
  545. if (!empty($user->role)) {
  546. $row->cells[1]->text .= get_string('role').get_string('labelsep', 'langconfig').$user->role.'<br />';
  547. }
  548. if ($user->maildisplay == 1 or ($user->maildisplay == 2 and ($course->id != SITEID) and !isguestuser()) or
  549. in_array('email', $extrafields) or ($user->id == $USER->id)) {
  550. $row->cells[1]->text .= get_string('email').get_string('labelsep', 'langconfig').html_writer::link("mailto:$user->email", $user->email) . '<br />';
  551. }
  552. foreach ($extrafields as $field) {
  553. if ($field === 'email') {
  554. // Skip email because it was displayed with different logic above
  555. // because this page is intended for students too.
  556. continue;
  557. }
  558. $row->cells[1]->text .= get_user_field_name($field) .
  559. get_string('labelsep', 'langconfig') . s($user->{$field}) . '<br />';
  560. }
  561. if (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) {
  562. $row->cells[1]->text .= get_string('city').get_string('labelsep', 'langconfig');
  563. if ($user->city && !isset($hiddenfields['city'])) {
  564. $row->cells[1]->text .= $user->city;
  565. }
  566. if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) {
  567. if ($user->city && !isset($hiddenfields['city'])) {
  568. $row->cells[1]->text .= ', ';
  569. }
  570. $row->cells[1]->text .= $countries[$user->country];
  571. }
  572. $row->cells[1]->text .= '<br />';
  573. }
  574. if (!isset($hiddenfields['lastaccess'])) {
  575. if ($user->lastaccess) {
  576. $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').userdate($user->lastaccess);
  577. $row->cells[1]->text .= '&nbsp; ('. format_time(time() - $user->lastaccess, $datestring) .')';
  578. } else {
  579. $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').get_string('never');
  580. }
  581. }
  582. $row->cells[1]->text .= $OUTPUT->container_end();
  583. $row->cells[2] = new html_table_cell();
  584. $row->cells[2]->attributes['class'] = 'links';
  585. $row->cells[2]->text = '';
  586. $links = array();
  587. if ($CFG->enableblogs && ($CFG->bloglevel != BLOG_USER_LEVEL || $USER->id == $user->id)) {
  588. $links[] = html_writer::link(new moodle_url('/blog/index.php?userid='.$user->id), get_string('blogs', 'blog'));
  589. }
  590. if (!empty($CFG->enablenotes) and (has_capability('moodle/notes:manage', $context) || has_capability('moodle/notes:view', $context))) {
  591. $links[] = html_writer::link(new moodle_url('/notes/index.php?course=' . $course->id. '&user='.$user->id), get_string('notes', 'notes'));
  592. }
  593. if (has_capability('moodle/site:viewreports', $context) or has_capability('moodle/user:viewuseractivitiesreport', $usercontext)) {
  594. $links[] = html_writer::link(new moodle_url('/course/user.php?id='. $course->id .'&user='. $user->id), get_string('activity'));
  595. }
  596. if ($USER->id != $user->id && !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas', $context) && !is_siteadmin($user->id)) {
  597. $links[] = html_writer::link(new moodle_url('/course/loginas.php?id='. $course->id .'&user='. $user->id .'&sesskey='. sesskey()), get_string('loginas'));
  598. }
  599. $links[] = html_writer::link(new moodle_url('/user/view.php?id='. $user->id .'&course='. $course->id), get_string('fullprofile') . '...');
  600. $row->cells[2]->text .= implode('', $links);
  601. if ($bulkoperations) {
  602. if ($selectall) {
  603. $checked = 'checked="true"';
  604. } else {
  605. $checked = '';
  606. }
  607. $row->cells[2]->text .= '<br /><input type="checkbox" class="usercheckbox" name="user'.$user->id.'" ' .$checked .'/> ';
  608. }
  609. $table->data = array($row);
  610. echo html_writer::table($table);
  611. }
  612. } else {
  613. echo $OUTPUT->heading(get_string('nothingtodisplay'));
  614. }
  615. }
  616. } else {
  617. $countrysort = (strpos($sort, 'country') !== false);
  618. $timeformat = get_string('strftimedate');
  619. if ($userlist) {
  620. $usersprinted = array();
  621. foreach ($userlist as $user) {
  622. if (in_array($user->id, $usersprinted)) { // Prevent duplicates by r.hidden - MDL-13935.
  623. continue;
  624. }
  625. $usersprinted[] = $user->id; // Add new user to the array of users printed.
  626. context_helper::preload_from_record($user);
  627. if ($user->lastaccess) {
  628. $lastaccess = format_time(time() - $user->lastaccess, $datestring);
  629. } else {
  630. $lastaccess = $strnever;
  631. }
  632. if (empty($user->country)) {
  633. $country = '';
  634. } else {
  635. if ($countrysort) {
  636. $country = '('.$user->country.') '.$countries[$user->country];
  637. } else {
  638. $country = $countries[$user->country];
  639. }
  640. }
  641. $usercontext = context_user::instance($user->id);
  642. if ($piclink = ($USER->id == $user->id || has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext))) {
  643. $profilelink = '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></strong>';
  644. } else {
  645. $profilelink = '<strong>'.fullname($user).'</strong>';
  646. }
  647. $data = array();
  648. if ($bulkoperations) {
  649. if ($selectall) {
  650. $checked = 'checked="true"';
  651. } else {
  652. $checked = '';
  653. }
  654. $data[] = '<input type="checkbox" class="usercheckbox" name="user'.$user->id.'" ' . $checked .'/>';
  655. }
  656. $data[] = $OUTPUT->user_picture($user, array('size' => 35, 'courseid' => $course->id));
  657. $data[] = $profilelink;
  658. if ($mode === MODE_BRIEF) {
  659. foreach ($extrafields as $field) {
  660. $data[] = $user->{$field};
  661. }
  662. }
  663. if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
  664. $data[] = $user->city;
  665. }
  666. if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
  667. $data[] = $country;
  668. }
  669. if (!isset($hiddenfields['lastaccess'])) {
  670. $data[] = $lastaccess;
  671. }
  672. $table->add_data($data);
  673. }
  674. }
  675. $table->print_html();
  676. }
  677. $perpageurl = clone($baseurl);
  678. $perpageurl->remove_params('perpage');
  679. if ($perpage == SHOW_ALL_PAGE_SIZE) {
  680. $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
  681. echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
  682. } else if ($matchcount > 0 && $perpage < $matchcount) {
  683. $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
  684. echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $matchcount)), array(), 'showall');
  685. }
  686. if ($bulkoperations) {
  687. echo '<br /><div class="buttons">';
  688. if ($matchcount > 0 && $perpage < $matchcount) {
  689. $perpageurl = clone($baseurl);
  690. $perpageurl->remove_params('perpage');
  691. $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
  692. $perpageurl->param('selectall', true);
  693. $showalllink = $perpageurl;
  694. } else {
  695. $showalllink = false;
  696. }
  697. if ($perpage < $matchcount) {
  698. // Select all users, refresh page showing all users and mark them all selected.
  699. $label = get_string('selectalluserswithcount', 'moodle', $matchcount);
  700. echo '<input type="button" id="checkall" value="' . $label . '" data-showallink="' . $showalllink . '" /> ';
  701. // Select all users, mark all users on page as selected.
  702. echo '<input type="button" id="checkallonpage" value="' . get_string('selectallusersonpage') . '" /> ';
  703. } else {
  704. echo '<input type="button" id="checkallonpage" value="' . get_string('selectall') . '" /> ';
  705. }
  706. echo '<input type="button" id="checknone" value="'.get_string('deselectall').'" /> ';
  707. $displaylist = array();
  708. $displaylist['messageselect.php'] = get_string('messageselectadd');
  709. if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
  710. $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
  711. $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
  712. }
  713. echo $OUTPUT->help_icon('withselectedusers');
  714. echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
  715. echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
  716. echo '<input type="hidden" name="id" value="'.$course->id.'" />';
  717. echo '<noscript style="display:inline">';
  718. echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
  719. echo '</noscript>';
  720. echo '</div></div>';
  721. echo '</form>';
  722. $module = array('name' => 'core_user', 'fullpath' => '/user/module.js');
  723. $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
  724. }
  725. // Show a search box if all participants don't fit on a single screen.
  726. if ($totalcount > $perpage) {
  727. echo '<form action="index.php" class="searchform"><div><input type="hidden" name="id" value="'.$course->id.'" />';
  728. echo '<label for="search">' . get_string('search', 'search') . ' </label>';
  729. echo '<input type="text" id="search" name="search" value="'.s($search).'" />&nbsp;<input type="submit" value="'.get_string('search').'" /></div></form>'."\n";
  730. }
  731. echo '</div>'; // Userlist.
  732. echo $OUTPUT->footer();
  733. if ($userlist) {
  734. $userlist->close();
  735. }
  736. /**
  737. * Returns SQL that can be used to limit a query to a period where the user last accessed a course..
  738. *
  739. * @param string $accesssince
  740. * @return string
  741. */
  742. function get_course_lastaccess_sql($accesssince='') {
  743. if (empty($accesssince)) {
  744. return '';
  745. }
  746. if ($accesssince == -1) { // Never.
  747. return 'ul.timeaccess = 0';
  748. } else {
  749. return 'ul.timeaccess != 0 AND ul.timeaccess < '.$accesssince;
  750. }
  751. }
  752. /**
  753. * Returns SQL that can be used to limit a query to a period where the user last accessed the system.
  754. *
  755. * @param string $accesssince
  756. * @return string
  757. */
  758. function get_user_lastaccess_sql($accesssince='') {
  759. if (empty($accesssince)) {
  760. return '';
  761. }
  762. if ($accesssince == -1) { // Never.
  763. return 'u.lastaccess = 0';
  764. } else {
  765. return 'u.lastaccess != 0 AND u.lastaccess < '.$accesssince;
  766. }
  767. }