[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/grade/report/history/classes/ -> helper.php (source)

   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  
 132          // Fields we need from the user table.
 133          $extrafields = get_extra_user_fields($context);
 134          $params = array();
 135          if (!empty($search)) {
 136              list($filtersql, $params) = users_search_sql($search, 'u', true, $extrafields);
 137              $filtersql .= ' AND ';
 138          } else {
 139              $filtersql = '';
 140          }
 141  
 142          $ufields = \user_picture::fields('u', $extrafields).',u.username';
 143          if ($count) {
 144              $select = "SELECT COUNT(DISTINCT u.id) ";
 145              $orderby = "";
 146          } else {
 147              $select = "SELECT DISTINCT $ufields ";
 148              $orderby = " ORDER BY u.lastname ASC, u.firstname ASC";
 149          }
 150          $sql = "$select
 151                   FROM {user} u
 152                   JOIN {grade_grades_history} ggh ON u.id = ggh.userid
 153                   JOIN {grade_items} gi ON gi.id = ggh.itemid
 154                  WHERE $filtersql gi.courseid = :courseid";
 155          $sql .= $orderby;
 156          $params['courseid'] = $context->instanceid;
 157  
 158          return array($sql, $params);
 159      }
 160  
 161      /**
 162       * Get a list of graders.
 163       *
 164       * @param int $courseid Id of course for which we need to fetch graders.
 165       *
 166       * @return array list of graders.
 167       */
 168      public static function get_graders($courseid) {
 169          global $DB;
 170  
 171          $ufields = get_all_user_name_fields(true, 'u');
 172          $sql = "SELECT u.id, $ufields
 173                    FROM {user} u
 174                    JOIN {grade_grades_history} ggh ON ggh.usermodified = u.id
 175                    JOIN {grade_items} gi ON gi.id = ggh.itemid
 176                   WHERE gi.courseid = :courseid
 177                GROUP BY u.id, $ufields
 178                ORDER BY u.lastname ASC, u.firstname ASC";
 179  
 180          $graders = $DB->get_records_sql($sql, array('courseid' => $courseid));
 181          $return = array(0 => get_string('allgraders', 'gradereport_history'));
 182          foreach ($graders as $grader) {
 183              $return[$grader->id] = fullname($grader);
 184          }
 185          return $return;
 186      }
 187  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1