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.

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   * 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  
 133          // Fields we need from the user table.
 134          // TODO Does not support custom user profile fields (MDL-70456).
 135          $extrafields = \core_user\fields::get_identity_fields($context, false);
 136          $params = array();
 137          if (!empty($search)) {
 138              list($filtersql, $params) = users_search_sql($search, 'u', true, $extrafields);
 139              $filtersql .= ' AND ';
 140          } else {
 141              $filtersql = '';
 142          }
 143  
 144          $userfieldsapi = \core_user\fields::for_userpic()->including(...(array_merge($extrafields, ['username'])));
 145          $ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
 146          if ($count) {
 147              $select = "SELECT COUNT(DISTINCT u.id) ";
 148              $orderby = "";
 149          } else {
 150              $select = "SELECT DISTINCT $ufields ";
 151              $orderby = " ORDER BY u.lastname ASC, u.firstname ASC";
 152          }
 153  
 154          $groupjoinsql = '';
 155          $groupwheresql = '';
 156          $courseid = $context->instanceid;
 157          $groupmode = groups_get_course_groupmode(get_course($courseid));
 158  
 159          // We're only interested in separate groups mode because it's the only group mode that requires the user to be a member of
 160          // specific group(s), except when they have the 'moodle/site:accessallgroups' capability.
 161          if ($groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) {
 162              // Fetch the groups that the user can see.
 163              $groups = groups_get_all_groups($courseid, $USER->id, 0, 'g.id');
 164  
 165              // Add join condition to include users that only belong to the same group as the user.
 166              list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED, 'gid', true, 0);
 167              $groupjoinsql = " JOIN {groups_members} gm ON gm.userid = u.id ";
 168              $groupwheresql = " AND gm.groupid $insql ";
 169              $params = array_merge($params, $inparams);
 170          }
 171  
 172          $sql = "$select
 173                   FROM {user} u
 174                   JOIN {grade_grades_history} ggh ON u.id = ggh.userid
 175                   JOIN {grade_items} gi ON gi.id = ggh.itemid
 176                   $groupjoinsql
 177                  WHERE $filtersql gi.courseid = :courseid $groupwheresql";
 178          $sql .= $orderby;
 179          $params['courseid'] = $courseid;
 180  
 181          return array($sql, $params);
 182      }
 183  
 184      /**
 185       * Get a list of graders.
 186       *
 187       * @param int $courseid Id of course for which we need to fetch graders.
 188       *
 189       * @return array list of graders.
 190       */
 191      public static function get_graders($courseid) {
 192          global $DB, $USER;
 193  
 194          $groupjoinsql = $groupwheresql = '';
 195          $inparams = [];
 196          $groupmode = groups_get_course_groupmode(get_course($courseid));
 197          if ($groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', \context_course::instance($courseid))) {
 198              // Fetch the groups that the user can see.
 199              $groups = groups_get_all_groups($courseid, $USER->id, 0, 'g.id');
 200              // Add join condition to include users that only belong to the same group as the user.
 201              list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED, 'gid', true, 0);
 202              $groupjoinsql = " JOIN {groups_members} gm ON gm.userid = u.id ";
 203              $groupwheresql = " AND gm.groupid $insql ";
 204          }
 205  
 206          $userfieldsapi = \core_user\fields::for_name();
 207          $ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
 208          $sql = "SELECT u.id, $ufields
 209                    FROM {user} u
 210                    JOIN {grade_grades_history} ggh ON ggh.usermodified = u.id
 211                    JOIN {grade_items} gi ON gi.id = ggh.itemid
 212                   $groupjoinsql
 213                   WHERE gi.courseid = :courseid $groupwheresql
 214                GROUP BY u.id, $ufields
 215                ORDER BY u.lastname ASC, u.firstname ASC";
 216  
 217          $graders = $DB->get_records_sql($sql, array('courseid' => $courseid) + $inparams);
 218          $return = array(0 => get_string('allgraders', 'gradereport_history'));
 219          foreach ($graders as $grader) {
 220              $return[$grader->id] = fullname($grader);
 221          }
 222          return $return;
 223      }
 224  }