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.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [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   * Helper class for gradehistory report.
  19   *
  20   * @package    gradereport_history
  21   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace gradereport_history;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  /**
  30   * Helper class for gradehistory report.
  31   *
  32   * @since      Moodle 2.8
  33   * @package    gradereport_history
  34   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class helper {
  38  
  39      /**
  40       * Initialise the js to handle the user selection {@link gradereport_history_user_button}.
  41       *
  42       * @param int $courseid       course id.
  43       * @param array $currentusers List of currently selected users.
  44       *
  45       * @return output\user_button the user select button.
  46       */
  47      public static function init_js($courseid, array $currentusers = null) {
  48          global $PAGE;
  49  
  50          // Load the strings for js.
  51          $PAGE->requires->strings_for_js(array(
  52              'errajaxsearch',
  53              'finishselectingusers',
  54              'foundoneuser',
  55              'foundnusers',
  56              'loadmoreusers',
  57              'selectusers',
  58          ), 'gradereport_history');
  59          $PAGE->requires->strings_for_js(array(
  60              'loading'
  61          ), 'admin');
  62          $PAGE->requires->strings_for_js(array(
  63              'noresults',
  64              'search'
  65          ), 'moodle');
  66  
  67          $arguments = array(
  68              'courseid'            => $courseid,
  69              'ajaxurl'             => '/grade/report/history/users_ajax.php',
  70              'url'                 => $PAGE->url->out(false),
  71              'selectedUsers'       => $currentusers,
  72          );
  73  
  74          // Load the yui module.
  75          $PAGE->requires->yui_module(
  76              'moodle-gradereport_history-userselector',
  77              'Y.M.gradereport_history.UserSelector.init',
  78              array($arguments)
  79          );
  80      }
  81  
  82      /**
  83       * Retrieve a list of users.
  84       *
  85       * We're interested in anyone that had a grade history in this course. This api returns a list of such users based on various
  86       * criteria passed.
  87       *
  88       * @param \context $context Context of the page where the results would be shown.
  89       * @param string $search the text to search for (empty string = find all).
  90       * @param int $page page number, defaults to 0.
  91       * @param int $perpage Number of entries to display per page, defaults to 0.
  92       *
  93       * @return array list of users.
  94       */
  95      public static function get_users($context, $search = '', $page = 0, $perpage = 25) {
  96          global $DB;
  97  
  98          list($sql, $params) = self::get_users_sql_and_params($context, $search);
  99          $limitfrom = $page * $perpage;
 100          $limitto = $limitfrom + $perpage;
 101          $users = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
 102          return $users;
 103      }
 104  
 105      /**
 106       * Get total number of users present for the given search criteria.
 107       *
 108       * @param \context $context Context of the page where the results would be shown.
 109       * @param string $search the text to search for (empty string = find all).
 110       *
 111       * @return int number of users found.
 112       */
 113      public static function get_users_count($context, $search = '') {
 114          global $DB;
 115  
 116          list($sql, $params) = self::get_users_sql_and_params($context, $search, true);
 117          return $DB->count_records_sql($sql, $params);
 118  
 119      }
 120  
 121      /**
 122       * Get sql and params to use to get list of users.
 123       *
 124       * @param \context $context Context of the page where the results would be shown.
 125       * @param string $search the text to search for (empty string = find all).
 126       * @param bool $count setting this to true, returns an sql to get count only instead of the complete data records.
 127       *
 128       * @return array sql and params list
 129       */
 130      protected static function get_users_sql_and_params($context, $search = '', $count = false) {
 131          global $DB, $USER;
 132          $userfieldsapi = \core_user\fields::for_identity($context)->with_userpic()->including('username');
 133          $userfieldssql = $userfieldsapi->get_sql('u', true, '', '', false);
 134          // Fields we need from the user table.
 135          $extrafields = [];
 136          foreach ($userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]) as $field) {
 137              $extrafields[$field] = $userfieldssql->mappings[$field];
 138          }
 139          $params = array();
 140          if (!empty($search)) {
 141              list($filtersql, $params) = users_search_sql($search, 'u', true, $extrafields);
 142              $filtersql .= ' AND ';
 143          } else {
 144              $filtersql = '';
 145          }
 146  
 147          $userfieldjoinssql = $userfieldssql->joins;
 148          if ($count) {
 149              $select = "SELECT COUNT(DISTINCT u.id) ";
 150              $orderby = "";
 151          } else {
 152              $select = "SELECT DISTINCT $userfieldssql->selects ";
 153              $orderby = " ORDER BY u.lastname ASC, u.firstname ASC";
 154          }
 155  
 156          $groupjoinsql = '';
 157          $groupwheresql = '';
 158          $courseid = $context->instanceid;
 159          $groupmode = groups_get_course_groupmode(get_course($courseid));
 160  
 161          // We're only interested in separate groups mode because it's the only group mode that requires the user to be a member of
 162          // specific group(s), except when they have the 'moodle/site:accessallgroups' capability.
 163          if ($groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) {
 164              // Fetch the groups that the user can see.
 165              $groups = groups_get_all_groups($courseid, $USER->id, 0, 'g.id');
 166              // Add join condition to include users that only belong to the same group as the user.
 167              list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED, 'gid', true, 0);
 168              $groupjoinsql = " JOIN {groups_members} gm ON gm.userid = u.id ";
 169              $groupwheresql = " AND gm.groupid $insql ";
 170              $params = array_merge($params, $inparams);
 171          }
 172  
 173          $sql = "$select
 174                   FROM {user} u
 175                   JOIN {grade_grades_history} ggh ON u.id = ggh.userid
 176                   JOIN {grade_items} gi ON gi.id = ggh.itemid
 177                   $userfieldjoinssql
 178                   $groupjoinsql
 179                  WHERE $filtersql gi.courseid = :courseid $groupwheresql";
 180          $sql .= $orderby;
 181          $params['courseid'] = $courseid;
 182          $params = array_merge($userfieldssql->params, $params);
 183          return array($sql, $params);
 184      }
 185  
 186      /**
 187       * Get a list of graders.
 188       *
 189       * @param int $courseid Id of course for which we need to fetch graders.
 190       *
 191       * @return array list of graders.
 192       */
 193      public static function get_graders($courseid) {
 194          global $DB, $USER;
 195  
 196          $groupjoinsql = $groupwheresql = '';
 197          $inparams = [];
 198          $groupmode = groups_get_course_groupmode(get_course($courseid));
 199          if ($groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', \context_course::instance($courseid))) {
 200              // Fetch the groups that the user can see.
 201              $groups = groups_get_all_groups($courseid, $USER->id, 0, 'g.id');
 202              // Add join condition to include users that only belong to the same group as the user.
 203              list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED, 'gid', true, 0);
 204              $groupjoinsql = " JOIN {groups_members} gm ON gm.userid = u.id ";
 205              $groupwheresql = " AND gm.groupid $insql ";
 206          }
 207  
 208          $userfieldsapi = \core_user\fields::for_name();
 209          $ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
 210          $sql = "SELECT u.id, $ufields
 211                    FROM {user} u
 212                    JOIN {grade_grades_history} ggh ON ggh.usermodified = u.id
 213                    JOIN {grade_items} gi ON gi.id = ggh.itemid
 214                   $groupjoinsql
 215                   WHERE gi.courseid = :courseid $groupwheresql
 216                GROUP BY u.id, $ufields
 217                ORDER BY u.lastname ASC, u.firstname ASC";
 218  
 219          $graders = $DB->get_records_sql($sql, array('courseid' => $courseid) + $inparams);
 220          $return = array(0 => get_string('allgraders', 'gradereport_history'));
 221          foreach ($graders as $grader) {
 222              $return[$grader->id] = fullname($grader);
 223          }
 224          return $return;
 225      }
 226  }