Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
/user/ -> index.php (source)

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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          print_error('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  $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
  88  
  89  $PAGE->set_title("$course->shortname: ".get_string('participants'));
  90  $PAGE->set_heading($course->fullname);
  91  $PAGE->set_pagetype('course-view-participants');
  92  $PAGE->set_docs_path('enrol/users');
  93  $PAGE->add_body_class('path-user');                     // So we can style it independently.
  94  $PAGE->set_other_editing_capability('moodle/course:manageactivities');
  95  
  96  // Expand the users node in the settings navigation when it exists because those pages
  97  // are related to this one.
  98  $node = $PAGE->settingsnav->find('users', navigation_node::TYPE_CONTAINER);
  99  if ($node) {
 100      $node->force_open();
 101  }
 102  
 103  echo $OUTPUT->header();
 104  
 105  $participanttable = new \core_user\table\participants("user-index-participants-{$course->id}");
 106  
 107  // Manage enrolments.
 108  $manager = new course_enrolment_manager($PAGE, $course);
 109  $enrolbuttons = $manager->get_manual_enrol_buttons();
 110  $enrolrenderer = $PAGE->get_renderer('core_enrol');
 111  $enrolbuttonsout = '';
 112  foreach ($enrolbuttons as $enrolbutton) {
 113      $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
 114  }
 115  
 116  echo $OUTPUT->render_participants_tertiary_nav($course, html_writer::div($enrolbuttonsout, '', [
 117      'data-region' => 'wrapper',
 118      'data-table-uniqueid' => $participanttable->uniqueid,
 119  ]));
 120  
 121  echo $OUTPUT->heading(get_string('enrolledusers', 'enrol'));
 122  
 123  $filterset = new \core_user\table\participants_filterset();
 124  $filterset->add_filter(new integer_filter('courseid', filter::JOINTYPE_DEFAULT, [(int)$course->id]));
 125  
 126  $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
 127  $filtergroupids = $urlgroupid ? [$urlgroupid] : [];
 128  
 129  // Force group filtering if user should only see a subset of groups' users.
 130  if ($course->groupmode != NOGROUPS && !$canaccessallgroups) {
 131      if ($filtergroupids) {
 132          $filtergroupids = array_intersect(
 133              $filtergroupids,
 134              array_keys(groups_get_all_groups($course->id, $USER->id))
 135          );
 136      } else {
 137          $filtergroupids = array_keys(groups_get_all_groups($course->id, $USER->id));
 138      }
 139  
 140      if (empty($filtergroupids)) {
 141          if ($course->groupmode == SEPARATEGROUPS) {
 142              // The user is not in a group so show message and exit.
 143              echo $OUTPUT->notification(get_string('notingroup'));
 144              echo $OUTPUT->footer();
 145              exit();
 146          } else {
 147              $filtergroupids = [(int) groups_get_course_group($course, true)];
 148          }
 149      }
 150  }
 151  
 152  // Apply groups filter if included in URL or forced due to lack of capabilities.
 153  if (!empty($filtergroupids)) {
 154      $filterset->add_filter(new integer_filter('groups', filter::JOINTYPE_DEFAULT, $filtergroupids));
 155  }
 156  
 157  // Display single group information if requested in the URL.
 158  if ($urlgroupid > 0 && ($course->groupmode != SEPARATEGROUPS || $canaccessallgroups)) {
 159      $grouprenderer = $PAGE->get_renderer('core_group');
 160      $groupdetailpage = new \core_group\output\group_details($urlgroupid);
 161      echo $grouprenderer->group_details($groupdetailpage);
 162  }
 163  
 164  // Filter by role if passed via URL (used on profile page).
 165  if ($roleid) {
 166      $viewableroles = get_profile_roles($context);
 167  
 168      // Apply filter if the user can view this role.
 169      if (array_key_exists($roleid, $viewableroles)) {
 170          $filterset->add_filter(new integer_filter('roles', filter::JOINTYPE_DEFAULT, [$roleid]));
 171      }
 172  }
 173  
 174  // Render the user filters.
 175  $userrenderer = $PAGE->get_renderer('core_user');
 176  echo $userrenderer->participants_filter($context, $participanttable->uniqueid);
 177  
 178  echo '<div class="userlist">';
 179  
 180  // Do this so we can get the total number of rows.
 181  ob_start();
 182  $participanttable->set_filterset($filterset);
 183  $participanttable->out($perpage, true);
 184  $participanttablehtml = ob_get_contents();
 185  ob_end_clean();
 186  
 187  echo html_writer::start_tag('form', [
 188      'action' => 'action_redir.php',
 189      'method' => 'post',
 190      'id' => 'participantsform',
 191      'data-course-id' => $course->id,
 192      'data-table-unique-id' => $participanttable->uniqueid,
 193  ]);
 194  echo '<div>';
 195  echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
 196  echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
 197  
 198  echo html_writer::tag(
 199      'p',
 200      get_string('countparticipantsfound', 'core_user', $participanttable->totalrows),
 201      [
 202          'data-region' => 'participant-count',
 203      ]
 204  );
 205  
 206  echo $participanttablehtml;
 207  
 208  $bulkoptions = (object) [
 209      'uniqueid' => $participanttable->uniqueid,
 210  ];
 211  
 212  if ($bulkoperations) {
 213      echo '<br /><div class="buttons"><div class="form-inline">';
 214  
 215      echo html_writer::start_tag('div', array('class' => 'btn-group'));
 216  
 217      if ($participanttable->get_page_size() < $participanttable->totalrows) {
 218          // Select all users, refresh table showing all users and mark them all selected.
 219          $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
 220          echo html_writer::empty_tag('input', [
 221              'type' => 'button',
 222              'id' => 'checkall',
 223              'class' => 'btn btn-secondary',
 224              'value' => $label,
 225              'data-target-page-size' => TABLE_SHOW_ALL_PAGE_SIZE,
 226          ]);
 227      }
 228      echo html_writer::end_tag('div');
 229      $displaylist = array();
 230      if (!empty($CFG->messaging) && has_all_capabilities(['moodle/site:sendmessage', 'moodle/course:bulkmessaging'], $context)) {
 231          $displaylist['#messageselect'] = get_string('messageselectadd');
 232      }
 233      if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
 234          $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
 235      }
 236  
 237      $params = ['operation' => 'download_participants'];
 238  
 239      $downloadoptions = [];
 240      $formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
 241      foreach ($formats as $format) {
 242          if ($format->is_enabled()) {
 243              $params = ['operation' => 'download_participants', 'dataformat' => $format->name];
 244              $url = new moodle_url('bulkchange.php', $params);
 245              $downloadoptions[$url->out(false)] = get_string('dataformat', $format->component);
 246          }
 247      }
 248  
 249      if (!empty($downloadoptions)) {
 250          $displaylist[] = [get_string('downloadas', 'table') => $downloadoptions];
 251      }
 252  
 253      if ($context->id != $frontpagectx->id) {
 254          $instances = $manager->get_enrolment_instances();
 255          $plugins = $manager->get_enrolment_plugins(false);
 256          foreach ($instances as $key => $instance) {
 257              if (!isset($plugins[$instance->enrol])) {
 258                  // Weird, some broken stuff in plugin.
 259                  continue;
 260              }
 261              $plugin = $plugins[$instance->enrol];
 262              $bulkoperations = $plugin->get_bulk_operations($manager);
 263  
 264              $pluginoptions = [];
 265              foreach ($bulkoperations as $key => $bulkoperation) {
 266                  $params = ['plugin' => $plugin->get_name(), 'operation' => $key];
 267                  $url = new moodle_url('bulkchange.php', $params);
 268                  $pluginoptions[$url->out(false)] = $bulkoperation->get_title();
 269              }
 270              if (!empty($pluginoptions)) {
 271                  $name = get_string('pluginname', 'enrol_' . $plugin->get_name());
 272                  $displaylist[] = [$name => $pluginoptions];
 273              }
 274          }
 275      }
 276  
 277      $selectactionparams = array(
 278          'id' => 'formactionid',
 279          'class' => 'ml-2',
 280          'data-action' => 'toggle',
 281          'data-togglegroup' => 'participants-table',
 282          'data-toggle' => 'action',
 283          'disabled' => 'disabled'
 284      );
 285      $label = html_writer::tag('label', get_string("withselectedusers"),
 286              ['for' => 'formactionid', 'class' => 'col-form-label d-inline']);
 287      $select = html_writer::select($displaylist, 'formaction', '', ['' => 'choosedots'], $selectactionparams);
 288      echo html_writer::tag('div', $label . $select);
 289  
 290      echo '<input type="hidden" name="id" value="' . $course->id . '" />';
 291      echo '<div class="d-none" data-region="state-help-icon">' . $OUTPUT->help_icon('publishstate', 'notes') . '</div>';
 292      echo '</div></div></div>';
 293  
 294      $bulkoptions->noteStateNames = note_get_state_names();
 295  }
 296  echo '</form>';
 297  
 298  $PAGE->requires->js_call_amd('core_user/participants', 'init', [$bulkoptions]);
 299  echo '</div>';  // Userlist.
 300  
 301  $enrolrenderer = $PAGE->get_renderer('core_enrol');
 302  // Need to re-generate the buttons to avoid having elements with duplicate ids on the page.
 303  $enrolbuttons = $manager->get_manual_enrol_buttons();
 304  $enrolbuttonsout = '';
 305  foreach ($enrolbuttons as $enrolbutton) {
 306      $enrolbuttonsout .= $enrolrenderer->render($enrolbutton);
 307  }
 308  echo html_writer::div($enrolbuttonsout, 'd-flex justify-content-end', [
 309      'data-region' => 'wrapper',
 310      'data-table-uniqueid' => $participanttable->uniqueid,
 311  ]);
 312  
 313  echo $OUTPUT->footer();