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 401 and 402] [Versions 401 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   * Common functions for the quiz statistics report.
  19   *
  20   * @package    quiz_statistics
  21   * @copyright  2013 The Open University
  22   * @author     James Pratt me@jamiep.org
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die;
  27  
  28  /**
  29   * SQL to fetch relevant 'quiz_attempts' records.
  30   *
  31   * @param int    $quizid        quiz id to get attempts for
  32   * @param \core\dml\sql_join $groupstudentsjoins Contains joins, wheres, params, empty if not using groups
  33   * @param string $whichattempts which attempts to use, represented internally as one of the constants as used in
  34   *                                   $quiz->grademethod ie.
  35   *                                   QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
  36   *                                   we calculate stats based on which attempts would affect the grade for each student.
  37   * @param bool   $includeungraded whether to fetch ungraded attempts too
  38   * @return array FROM and WHERE sql fragments and sql params
  39   */
  40  function quiz_statistics_attempts_sql($quizid, \core\dml\sql_join $groupstudentsjoins,
  41          $whichattempts = QUIZ_GRADEAVERAGE, $includeungraded = false) {
  42      $fromqa = "{quiz_attempts} quiza ";
  43      $whereqa = 'quiza.quiz = :quizid AND quiza.preview = 0 AND quiza.state = :quizstatefinished';
  44      $qaparams = array('quizid' => (int)$quizid, 'quizstatefinished' => quiz_attempt::FINISHED);
  45  
  46      if (!empty($groupstudentsjoins->joins)) {
  47          $fromqa .= "\nJOIN {user} u ON u.id = quiza.userid
  48              {$groupstudentsjoins->joins} ";
  49          $whereqa .= " AND {$groupstudentsjoins->wheres}";
  50          $qaparams += $groupstudentsjoins->params;
  51      }
  52  
  53      $whichattemptsql = quiz_report_grade_method_sql($whichattempts);
  54      if ($whichattemptsql) {
  55          $whereqa .= ' AND ' . $whichattemptsql;
  56      }
  57  
  58      if (!$includeungraded) {
  59          $whereqa .= ' AND quiza.sumgrades IS NOT NULL';
  60      }
  61  
  62      return array($fromqa, $whereqa, $qaparams);
  63  }
  64  
  65  /**
  66   * Return a {@link qubaid_condition} from the values returned by {@link quiz_statistics_attempts_sql}.
  67   *
  68   * @param int     $quizid
  69   * @param \core\dml\sql_join $groupstudentsjoins Contains joins, wheres, params
  70   * @param string $whichattempts which attempts to use, represented internally as one of the constants as used in
  71   *                                   $quiz->grademethod ie.
  72   *                                   QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
  73   *                                   we calculate stats based on which attempts would affect the grade for each student.
  74   * @param bool    $includeungraded
  75   * @return        \qubaid_join
  76   */
  77  function quiz_statistics_qubaids_condition($quizid, \core\dml\sql_join $groupstudentsjoins,
  78          $whichattempts = QUIZ_GRADEAVERAGE, $includeungraded = false) {
  79      list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql(
  80              $quizid, $groupstudentsjoins, $whichattempts, $includeungraded);
  81      return new qubaid_join($fromqa, 'quiza.uniqueid', $whereqa, $qaparams);
  82  }
  83  
  84  /**
  85   * This helper function returns a sequence of colours each time it is called.
  86   * Used for choosing colours for graph data series.
  87   * @return string colour name.
  88   * @deprecated since Moodle 3.2
  89   */
  90  function quiz_statistics_graph_get_new_colour() {
  91      debugging('The function quiz_statistics_graph_get_new_colour() is deprecated, please do not use it any more. '
  92          . 'Colours will be handled by the charting library directly.', DEBUG_DEVELOPER);
  93  
  94      static $colourindex = -1;
  95      $colours = array('red', 'green', 'yellow', 'orange', 'purple', 'black',
  96          'maroon', 'blue', 'ltgreen', 'navy', 'ltred', 'ltltgreen', 'ltltorange',
  97          'olive', 'gray', 'ltltred', 'ltorange', 'lime', 'ltblue', 'ltltblue');
  98  
  99      $colourindex = ($colourindex + 1) % count($colours);
 100  
 101      return $colours[$colourindex];
 102  }