Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/user/ -> index.php (source)

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

   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  $participantsperpage = intval(get_config('moodlecourse', 'participantsperpage'));
  38  define('DEFAULT_PAGE_SIZE', (!empty($participantsperpage) ? $participantsperpage : 20));
  39  
  40  $page         = optional_param('page', 0, PARAM_INT); // Which page to show.
  41  $perpage      = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
  42  $contextid    = optional_param('contextid', 0, PARAM_INT); // One of this or.
  43  $courseid     = optional_param('id', 0, PARAM_INT); // This are required.
  44  $newcourse    = optional_param('newcourse', false, PARAM_BOOL);
  45  $roleid       = optional_param('roleid', 0, PARAM_INT);
  46  $urlgroupid   = optional_param('group', 0, PARAM_INT);
  47  
  48  $PAGE->set_url('/user/index.php', array(
  49          'page' => $page,
  50          'perpage' => $perpage,
  51          'contextid' => $contextid,
  52          'id' => $courseid,
  53          'newcourse' => $newcourse));
  54  
  55  if ($contextid) {
  56      $context = context::instance_by_id($contextid, MUST_EXIST);
  57      if ($context->contextlevel != CONTEXT_COURSE) {
  58          throw new \moodle_exception('invalidcontext');
  59      }
  60      $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
  61  } else {
  62      $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  63      $context = context_course::instance($course->id, MUST_EXIST);
  64  }
  65  // Not needed anymore.
  66  unset($contextid);
  67  unset($courseid);
  68  
  69  require_login($course);
  70  
  71  $systemcontext = context_system::instance();
  72  $isfrontpage = ($course->id == SITEID);
  73  
  74  $frontpagectx = context_course::instance(SITEID);
  75  
  76  if ($isfrontpage) {
  77      $PAGE->set_pagelayout('admin');
  78      course_require_view_participants($systemcontext);
  79  } else {
  80      $PAGE->set_pagelayout('incourse');
  81      course_require_view_participants($context);
  82  }
  83  
  84  // Trigger events.
  85  user_list_view($course, $context);
  86  
  87  $PAGE->set_title("$course->shortname: ".get_string('participants'));
  88  $PAGE->set_heading($course->fullname);
  89  $PAGE->set_pagetype('course-view-participants');
  90  $PAGE->set_docs_path('enrol/users');
  91  $PAGE->add_body_class('path-user');                     // So we can style it independently.
  92  $PAGE->set_other_editing_capability('moodle/course:manageactivities');
  93  
  94  // Expand the users node in the settings navigation when it exists because those pages
  95  // are related to this one.
  96  $node = $PAGE->settingsnav->find('users', navigation_node::TYPE_CONTAINER);
  97  if ($node) {
  98      $node->force_open();
  99  }
 100  
 101  echo $OUTPUT->header();
 102  
 103  $participanttable = new \core_user\table\participants("user-index-participants-{$course->id}");
 104  
 105  // Manage enrolments.
 106  $manager = new course_enrolment_manager($PAGE, $course);
 107  $enrolbuttons = $manager->get_manual_enrol_buttons();
 108  $enrolrenderer = $PAGE->get_renderer('core_enrol');
 109  $enrolbuttonsout = '';
 110  foreach ($enrolbuttons as $enrolbutton) {
 111      $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
 112  }
 113  
 114  echo $OUTPUT->render_participants_tertiary_nav($course, html_writer::div($enrolbuttonsout, '', [
 115      'data-region' => 'wrapper',
 116      'data-table-uniqueid' => $participanttable->uniqueid,
 117  ]));
 118  
 119  echo $OUTPUT->heading(get_string('enrolledusers', 'enrol'));
 120  
 121  $filterset = new \core_user\table\participants_filterset();
 122  $filterset->add_filter(new integer_filter('courseid', filter::JOINTYPE_DEFAULT, [(int)$course->id]));
 123  
 124  $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
 125  $filtergroupids = $urlgroupid ? [$urlgroupid] : [];
 126  
 127  // Force group filtering if user should only see a subset of groups' users.
 128  if ($course->groupmode != NOGROUPS && !$canaccessallgroups) {
 129      if ($filtergroupids) {
 130          $filtergroupids = array_intersect(
 131              $filtergroupids,
 132              array_keys(groups_get_all_groups($course->id, $USER->id))
 133          );
 134      } else {
 135          $filtergroupids = array_keys(groups_get_all_groups($course->id, $USER->id));
 136      }
 137  
 138      if (empty($filtergroupids)) {
 139          if ($course->groupmode == SEPARATEGROUPS) {
 140              // The user is not in a group so show message and exit.
 141              echo $OUTPUT->notification(get_string('notingroup'));
 142              echo $OUTPUT->footer();
 143              exit();
 144          } else {
 145              $filtergroupids = [(int) groups_get_course_group($course, true)];
 146          }
 147      }
 148  }
 149  
 150  // Apply groups filter if included in URL or forced due to lack of capabilities.
 151  if (!empty($filtergroupids)) {
 152      $filterset->add_filter(new integer_filter('groups', filter::JOINTYPE_DEFAULT, $filtergroupids));
 153  }
 154  
 155  // Display single group information if requested in the URL.
 156  if ($urlgroupid > 0 && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
 157      $grouprenderer = $PAGE->get_renderer('core_group');
 158      $groupdetailpage = new \core_group\output\group_details($urlgroupid);
 159      echo $grouprenderer->group_details($groupdetailpage);
 160  }
 161  
 162  // Filter by role if passed via URL (used on profile page).
 163  if ($roleid) {
 164      $viewableroles = get_profile_roles($context);
 165  
 166      // Apply filter if the user can view this role.
 167      if (array_key_exists($roleid, $viewableroles)) {
 168          $filterset->add_filter(new integer_filter('roles', filter::JOINTYPE_DEFAULT, [$roleid]));
 169      }
 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  echo '<br /><div class="buttons"><div class="form-inline">';
 211  
 212  echo html_writer::start_tag('div', array('class' => 'btn-group'));
 213  
 214  if ($participanttable->get_page_size() < $participanttable->totalrows) {
 215      // Select all users, refresh table showing all users and mark them all selected.
 216      $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
 217      echo html_writer::empty_tag('input', [
 218          'type' => 'button',
 219          'id' => 'checkall',
 220          'class' => 'btn btn-secondary',
 221          'value' => $label,
 222          'data-target-page-size' => TABLE_SHOW_ALL_PAGE_SIZE,
 223      ]);
 224  }
 225  echo html_writer::end_tag('div');
 226  $displaylist = array();
 227  if (!empty($CFG->messaging) && has_all_capabilities(['moodle/site:sendmessage', 'moodle/course:bulkmessaging'], $context)) {
 228      $displaylist['#messageselect'] = get_string('messageselectadd');
 229  }
 230  if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
 231      $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
 232  }
 233  
 234  $params = ['operation' => 'download_participants'];
 235  
 236  $downloadoptions = [];
 237  $formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
 238  foreach ($formats as $format) {
 239      if ($format->is_enabled()) {
 240          $params = ['operation' => 'download_participants', 'dataformat' => $format->name];
 241          $url = new moodle_url('bulkchange.php', $params);
 242          $downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
 243      }
 244  }
 245  
 246  if (!empty($downloadoptions)) {
 247      $displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
 248  }
 249  
 250  if ($context->id != $frontpagectx->id) {
 251      $instances = $manager->get_enrolment_instances();
 252      $plugins = $manager->get_enrolment_plugins(false);
 253      foreach ($instances as $key => $instance) {
 254          if (!isset($plugins[$instance->enrol])) {
 255              // Weird, some broken stuff in plugin.
 256              continue;
 257          }
 258          $plugin = $plugins[$instance->enrol];
 259          $bulkoperations = $plugin->get_bulk_operations($manager);
 260  
 261          $pluginoptions = [];
 262          foreach ($bulkoperations as $key => $bulkoperation) {
 263              $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
 264              $url = new moodle_url('bulkchange.php', $params);
 265              $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
 266          }
 267          if (!empty($pluginoptions)) {
 268              $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
 269              $displaylist[] = [$name => $pluginoptions];
 270          }
 271      }
 272  }
 273  
 274  $selectactionparams = array(
 275      'id' => 'formactionid',
 276      'class' => 'ml-2',
 277      'data-action' => 'toggle',
 278      'data-togglegroup' => 'participants-table',
 279      'data-toggle' => 'action',
 280      'disabled' => 'disabled'
 281  );
 282  $label = html_writer::tag('label', get_string("withselectedusers"),
 283          ['for' => 'formactionid', 'class' => 'col-form-label d-inline']);
 284  $select = html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], $selectactionparams);
 285  echo html_writer::tag('div', $label . $select);
 286  
 287  echo '<input type="hidden" name="id" value="' . $course->id . '" />';
 288  echo '<div class="d-none" data-region="state-help-icon">' . $OUTPUT->help_icon('publishstate', 'notes') . '</div>';
 289  echo '</div></div></div>';
 290  
 291  $bulkoptions->noteStateNames = note_get_state_names();
 292  
 293  echo '</form>';
 294  
 295  $PAGE->requires->js_call_amd('core_user/participants', 'init', [$bulkoptions]);
 296  echo '</div>';  // Userlist.
 297  
 298  $enrolrenderer = $PAGE->get_renderer('core_enrol');
 299  // Need to re-generate the buttons to avoid having elements with duplicate ids on the page.
 300  $enrolbuttons = $manager->get_manual_enrol_buttons();
 301  $enrolbuttonsout = '';
 302  foreach ($enrolbuttons as $enrolbutton) {
 303      $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
 304  }
 305  echo html_writer::div($enrolbuttonsout, 'd-flex justify-content-end', [
 306      'data-region' => 'wrapper',
 307      'data-table-uniqueid' => $participanttable->uniqueid,
 308  ]);
 309  
 310  echo $OUTPUT->footer();