Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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   * User searching requests.
  19   *
  20   * @package    gradereport_history
  21   * @copyright  2013 NetSpot Pty Ltd (https://www.netspot.com.au)
  22   * @author     Adam Olley <adam.olley@netspot.com.au>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  define('AJAX_SCRIPT', true);
  27  
  28  require_once(__DIR__ . '/../../../config.php');
  29  
  30  $id = required_param('id', PARAM_INT); // Course id.
  31  $search = optional_param('search', '', PARAM_RAW);
  32  $page = optional_param('page', 0, PARAM_INT);
  33  
  34  $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
  35  $context = context_course::instance($course->id, MUST_EXIST);
  36  
  37  if ($course->id == SITEID) {
  38      throw new moodle_exception('invalidcourse');
  39  }
  40  
  41  require_sesskey();
  42  require_login($course);
  43  require_capability('gradereport/history:view', $context);
  44  require_capability('moodle/grade:viewall', $context);
  45  
  46  $outcome = new stdClass();
  47  $outcome->success = true;
  48  $outcome->error = '';
  49  
  50  $users = \gradereport_history\helper::get_users($context, $search, $page, 25);
  51  $outcome->response = array('users' => array());
  52  $outcome->response['totalusers'] = \gradereport_history\helper::get_users_count($context, $search);;
  53  
  54  $userfieldsapi = \core_user\fields::for_identity($context)->with_userpic();
  55  $extrafields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
  56  $useroptions = array('link' => false, 'visibletoscreenreaders' => false);
  57  
  58  // Format the user record.
  59  foreach ($users as $user) {
  60      $newuser = new stdClass();
  61      $newuser->userid = $user->id;
  62      $newuser->picture = $OUTPUT->user_picture($user, $useroptions);
  63      $newuser->fullname = fullname($user);
  64      $fieldvalues = [];
  65      foreach ($extrafields as $field) {
  66          if ($user->{$field}) {
  67              $fieldvalues[] = $user->{$field};
  68          }
  69      }
  70      $newuser->extrafields = implode(', ', $fieldvalues);
  71      $outcome->response['users'][] = $newuser;
  72  }
  73  
  74  $outcome->success = true;
  75  echo $OUTPUT->header();
  76  echo json_encode($outcome);
  77  echo $OUTPUT->footer();