Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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