Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/user/ -> index.php (source)

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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  /**
  18   * Lists all the users within a given course.
  19   *
  20   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package core_user
  23   */
  24  
  25  require_once('../config.php');
  26  require_once($CFG->dirroot.'/user/lib.php');
  27  require_once($CFG->dirroot.'/course/lib.php');
  28  require_once($CFG->dirroot.'/notes/lib.php');
  29  require_once($CFG->libdir.'/tablelib.php');
  30  require_once($CFG->libdir.'/filelib.php');
  31  require_once($CFG->dirroot.'/enrol/locallib.php');
  32  
  33  use core_table\local\filter\filter;
  34  use core_table\local\filter\integer_filter;
  35  use core_table\local\filter\string_filter;
  36  
  37  define('DEFAULT_PAGE_SIZE', 20);
  38  
  39  $page         = optional_param('page', 0, PARAM_INT); // Which page to show.
  40  $perpage      = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
  41  $contextid    = optional_param('contextid', 0, PARAM_INT); // One of this or.
  42  $courseid     = optional_param('id', 0, PARAM_INT); // This are required.
  43  $newcourse    = optional_param('newcourse', false, PARAM_BOOL);
  44  $roleid       = optional_param('roleid', 0, PARAM_INT);
  45  $urlgroupid   = optional_param('group', 0, PARAM_INT);
  46  
  47  $PAGE->set_url('/user/index.php', array(
  48          'page' => $page,
  49          'perpage' => $perpage,
  50          'contextid' => $contextid,
  51          'id' => $courseid,
  52          'newcourse' => $newcourse));
  53  
  54  if ($contextid) {
  55      $context = context::instance_by_id($contextid, MUST_EXIST);
  56      if ($context->contextlevel != CONTEXT_COURSE) {
  57          print_error('invalidcontext');
  58      }
  59      $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
  60  } else {
  61      $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  62      $context = context_course::instance($course->id, MUST_EXIST);
  63  }
  64  // Not needed anymore.
  65  unset($contextid);
  66  unset($courseid);
  67  
  68  require_login($course);
  69  
  70  $systemcontext = context_system::instance();
  71  $isfrontpage = ($course->id == SITEID);
  72  
  73  $frontpagectx = context_course::instance(SITEID);
  74  
  75  if ($isfrontpage) {
  76      $PAGE->set_pagelayout('admin');
  77      course_require_view_participants($systemcontext);
  78  } else {
  79      $PAGE->set_pagelayout('incourse');
  80      course_require_view_participants($context);
  81  }
  82  
  83  // Trigger events.
  84  user_list_view($course, $context);
  85  
  86  $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
  87  
  88  $PAGE->set_title("$course->shortname: ".get_string('participants'));
  89  $PAGE->set_heading($course->fullname);
  90  $PAGE->set_pagetype('course-view-' . $course->format);
  91  $PAGE->set_docs_path('enrol/users');
  92  $PAGE->add_body_class('path-user');                     // So we can style it independently.
  93  $PAGE->set_other_editing_capability('moodle/course:manageactivities');
  94  
  95  // Expand the users node in the settings navigation when it exists because those pages
  96  // are related to this one.
  97  $node = $PAGE->settingsnav->find('users', navigation_node::TYPE_CONTAINER);
  98  if ($node) {
  99      $node->force_open();
 100  }
 101  
 102  echo $OUTPUT->header();
 103  echo $OUTPUT->heading(get_string('participants'));
 104  
 105  $filterset = new \core_user\table\participants_filterset();
 106  $filterset->add_filter(new integer_filter('courseid', filter::JOINTYPE_DEFAULT, [(int)$course->id]));
 107  
 108  $participanttable = new \core_user\table\participants("user-index-participants-{$course->id}");
 109  
 110  $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
 111  $filtergroupids = $urlgroupid ? [$urlgroupid] : [];
 112  
 113  // Force group filtering if user should only see a subset of groups' users.
 114  if ($course->groupmode != NOGROUPS && !$canaccessallgroups) {
 115      if ($filtergroupids) {
 116          $filtergroupids = array_intersect(
 117              $filtergroupids,
 118              array_keys(groups_get_all_groups($course->id, $USER->id))
 119          );
 120      } else {
 121          $filtergroupids = array_keys(groups_get_all_groups($course->id, $USER->id));
 122      }
 123  
 124      if (empty($filtergroupids)) {
 125          if ($course->groupmode == SEPARATEGROUPS) {
 126              // The user is not in a group so show message and exit.
 127              echo $OUTPUT->notification(get_string('notingroup'));
 128              echo $OUTPUT->footer();
 129              exit();
 130          } else {
 131              $filtergroupids = [(int) groups_get_course_group($course, true)];
 132          }
 133      }
 134  }
 135  
 136  // Apply groups filter if included in URL or forced due to lack of capabilities.
 137  if (!empty($filtergroupids)) {
 138      $filterset->add_filter(new integer_filter('groups', filter::JOINTYPE_DEFAULT, $filtergroupids));
 139  }
 140  
 141  // Display single group information if requested in the URL.
 142  if ($urlgroupid > 0 && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
 143      $grouprenderer = $PAGE->get_renderer('core_group');
 144      $groupdetailpage = new \core_group\output\group_details($urlgroupid);
 145      echo $grouprenderer->group_details($groupdetailpage);
 146  }
 147  
 148  // Filter by role if passed via URL (used on profile page).
 149  if ($roleid) {
 150      $viewableroles = get_profile_roles($context);
 151  
 152      // Apply filter if the user can view this role.
 153      if (array_key_exists($roleid, $viewableroles)) {
 154          $filterset->add_filter(new integer_filter('roles', filter::JOINTYPE_DEFAULT, [$roleid]));
 155      }
 156  }
 157  
 158  // Manage enrolments.
 159  $manager = new course_enrolment_manager($PAGE, $course);
 160  $enrolbuttons = $manager->get_manual_enrol_buttons();
 161  $enrolrenderer = $PAGE->get_renderer('core_enrol');
 162  $enrolbuttonsout = '';
 163  foreach ($enrolbuttons as $enrolbutton) {
 164      $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
 165  }
 166  
 167  echo html_writer::div($enrolbuttonsout, 'd-flex justify-content-end', [
 168      'data-region' => 'wrapper',
 169      'data-table-uniqueid' => $participanttable->uniqueid,
 170  ]);
 171  
 172  // Render the user filters.
 173  $userrenderer = $PAGE->get_renderer('core_user');
 174  echo $userrenderer->participants_filter($context, $participanttable->uniqueid);
 175  
 176  echo '<div class="userlist">';
 177  
 178  // Do this so we can get the total number of rows.
 179  ob_start();
 180  $participanttable->set_filterset($filterset);
 181  $participanttable->out($perpage, true);
 182  $participanttablehtml = ob_get_contents();
 183  ob_end_clean();
 184  
 185  echo html_writer::start_tag('form', [
 186      'action' => 'action_redir.php',
 187      'method' => 'post',
 188      'id' => 'participantsform',
 189      'data-course-id' => $course->id,
 190      'data-table-unique-id' => $participanttable->uniqueid,
 191  ]);
 192  echo '<div>';
 193  echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
 194  echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
 195  
 196  echo html_writer::tag(
 197      'p',
 198      get_string('countparticipantsfound', 'core_user', $participanttable->totalrows),
 199      [
 200          'data-region' => 'participant-count',
 201      ]
 202  );
 203  
 204  echo $participanttablehtml;
 205  
 206  $bulkoptions = (object) [
 207      'uniqueid' => $participanttable->uniqueid,
 208  ];
 209  
 210  if ($bulkoperations) {
 211      echo '<br /><div class="buttons"><div class="form-inline">';
 212  
 213      echo html_writer::start_tag('div', array('class' => 'btn-group'));
 214  
 215      if ($participanttable->get_page_size() < $participanttable->totalrows) {
 216          // Select all users, refresh table showing all users and mark them all selected.
 217          $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
 218          echo html_writer::empty_tag('input', [
 219              'type' => 'button',
 220              'id' => 'checkall',
 221              'class' => 'btn btn-secondary',
 222              'value' => $label,
 223              'data-target-page-size' => TABLE_SHOW_ALL_PAGE_SIZE,
 224          ]);
 225      }
 226      echo html_writer::end_tag('div');
 227      $displaylist = array();
 228      if (!empty($CFG->messaging) && has_all_capabilities(['moodle/site:sendmessage', 'moodle/course:bulkmessaging'], $context)) {
 229          $displaylist['#messageselect'] = get_string('messageselectadd');
 230      }
 231      if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
 232          $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
 233      }
 234  
 235      $params = ['operation' => 'download_participants'];
 236  
 237      $downloadoptions = [];
 238      $formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
 239      foreach ($formats as $format) {
 240          if ($format->is_enabled()) {
 241              $params = ['operation' => 'download_participants', 'dataformat' => $format->name];
 242              $url = new moodle_url('bulkchange.php', $params);
 243              $downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
 244          }
 245      }
 246  
 247      if (!empty($downloadoptions)) {
 248          $displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
 249      }
 250  
 251      if ($context->id != $frontpagectx->id) {
 252          $instances = $manager->get_enrolment_instances();
 253          $plugins = $manager->get_enrolment_plugins(false);
 254          foreach ($instances as $key => $instance) {
 255              if (!isset($plugins[$instance->enrol])) {
 256                  // Weird, some broken stuff in plugin.
 257                  continue;
 258              }
 259              $plugin = $plugins[$instance->enrol];
 260              $bulkoperations = $plugin->get_bulk_operations($manager);
 261  
 262              $pluginoptions = [];
 263              foreach ($bulkoperations as $key => $bulkoperation) {
 264                  $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
 265                  $url = new moodle_url('bulkchange.php', $params);
 266                  $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
 267              }
 268              if (!empty($pluginoptions)) {
 269                  $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
 270                  $displaylist[] = [$name => $pluginoptions];
 271              }
 272          }
 273      }
 274  
 275      $selectactionparams = array(
 276          'id' => 'formactionid',
 277          'class' => 'ml-2',
 278          'data-action' => 'toggle',
 279          'data-togglegroup' => 'participants-table',
 280          'data-toggle' => 'action',
 281          'disabled' => 'disabled'
 282      );
 283      $label = html_writer::tag('label', get_string("withselectedusers"),
 284              ['for' => 'formactionid', 'class' => 'col-form-label d-inline']);
 285      $select = html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], $selectactionparams);
 286      echo html_writer::tag('div', $label . $select);
 287  
 288      echo '<input type="hidden" name="id" value="' . $course->id . '" />';
 289      echo '<div class="d-none" data-region="state-help-icon">' . $OUTPUT->help_icon('publishstate', 'notes') . '</div>';
 290      echo '</div></div></div>';
 291  
 292      $bulkoptions->noteStateNames = note_get_state_names();
 293  }
 294  echo '</form>';
 295  
 296  $PAGE->requires->js_call_amd('core_user/participants', 'init', [$bulkoptions]);
 297  echo '</div>';  // Userlist.
 298  
 299  $enrolrenderer = $PAGE->get_renderer('core_enrol');
 300  // Need to re-generate the buttons to avoid having elements with duplicate ids on the page.
 301  $enrolbuttons = $manager->get_manual_enrol_buttons();
 302  $enrolbuttonsout = '';
 303  foreach ($enrolbuttons as $enrolbutton) {
 304      $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
 305  }
 306  echo html_writer::div($enrolbuttonsout, 'd-flex justify-content-end', [
 307      'data-region' => 'wrapper',
 308      'data-table-uniqueid' => $participanttable->uniqueid,
 309  ]);
 310  
 311  if ($newcourse == 1) {
 312      $str = get_string('proceedtocourse', 'enrol');
 313      // The margin is to make it line up with the enrol users button when they are both on the same line.
 314      $classes = 'my-1';
 315      $url = course_get_url($course);
 316      echo $OUTPUT->single_button($url, $str, 'GET', array('class' => $classes));
 317  }
 318  
 319  echo $OUTPUT->footer();