Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  use mod_quiz\quiz_attempt;
  27  
  28  defined('MOODLE_INTERNAL') || die;
  29  
  30  /**
  31   * SQL to fetch relevant 'quiz_attempts' records.
  32   *
  33   * @param int    $quizid        quiz id to get attempts for
  34   * @param \core\dml\sql_join $groupstudentsjoins Contains joins, wheres, params, empty if not using groups
  35   * @param string $whichattempts which attempts to use, represented internally as one of the constants as used in
  36   *                                   $quiz->grademethod ie.
  37   *                                   QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
  38   *                                   we calculate stats based on which attempts would affect the grade for each student.
  39   * @param bool   $includeungraded whether to fetch ungraded attempts too
  40   * @return array FROM and WHERE sql fragments and sql params
  41   */
  42  function quiz_statistics_attempts_sql($quizid, \core\dml\sql_join $groupstudentsjoins,
  43          $whichattempts = QUIZ_GRADEAVERAGE, $includeungraded = false) {
  44      $fromqa = "{quiz_attempts} quiza ";
  45      $whereqa = 'quiza.quiz = :quizid AND quiza.preview = 0 AND quiza.state = :quizstatefinished';
  46      $qaparams = ['quizid' => (int)$quizid, 'quizstatefinished' => quiz_attempt::FINISHED];
  47  
  48      if (!empty($groupstudentsjoins->joins)) {
  49          $fromqa .= "\nJOIN {user} u ON u.id = quiza.userid
  50              {$groupstudentsjoins->joins} ";
  51          $whereqa .= " AND {$groupstudentsjoins->wheres}";
  52          $qaparams += $groupstudentsjoins->params;
  53      }
  54  
  55      $whichattemptsql = quiz_report_grade_method_sql($whichattempts);
  56      if ($whichattemptsql) {
  57          $whereqa .= ' AND ' . $whichattemptsql;
  58      }
  59  
  60      if (!$includeungraded) {
  61          $whereqa .= ' AND quiza.sumgrades IS NOT NULL';
  62      }
  63  
  64      return [$fromqa, $whereqa, $qaparams];
  65  }
  66  
  67  /**
  68   * Return a {@link qubaid_condition} from the values returned by {@link quiz_statistics_attempts_sql}.
  69   *
  70   * @param int     $quizid
  71   * @param \core\dml\sql_join $groupstudentsjoins Contains joins, wheres, params
  72   * @param string $whichattempts which attempts to use, represented internally as one of the constants as used in
  73   *                                   $quiz->grademethod ie.
  74   *                                   QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
  75   *                                   we calculate stats based on which attempts would affect the grade for each student.
  76   * @param bool    $includeungraded
  77   * @return        \qubaid_join
  78   */
  79  function quiz_statistics_qubaids_condition($quizid, \core\dml\sql_join $groupstudentsjoins,
  80          $whichattempts = QUIZ_GRADEAVERAGE, $includeungraded = false) {
  81      list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql(
  82              $quizid, $groupstudentsjoins, $whichattempts, $includeungraded);
  83      return new qubaid_join($fromqa, 'quiza.uniqueid', $whereqa, $qaparams);
  84  }