[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/quiz/report/statistics/ -> statisticslib.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   * 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  /**
  27   * SQL to fetch relevant 'quiz_attempts' records.
  28   *
  29   * @param int    $quizid        quiz id to get attempts for
  30   * @param array  $groupstudents empty array if not using groups or array of students in current group.
  31   * @param string $whichattempts which attempts to use, represented internally as one of the constants as used in
  32   *                                   $quiz->grademethod ie.
  33   *                                   QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
  34   *                                   we calculate stats based on which attempts would affect the grade for each student.
  35   * @param bool   $includeungraded whether to fetch ungraded attempts too
  36   * @return array FROM and WHERE sql fragments and sql params
  37   */
  38  function quiz_statistics_attempts_sql($quizid, $groupstudents, $whichattempts = QUIZ_GRADEAVERAGE, $includeungraded = false) {
  39      global $DB;
  40  
  41      $fromqa = '{quiz_attempts} quiza ';
  42  
  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 ($groupstudents) {
  47          ksort($groupstudents);
  48          list($grpsql, $grpparams) = $DB->get_in_or_equal(array_keys($groupstudents),
  49                  SQL_PARAMS_NAMED, 'statsuser');
  50          list($grpsql, $grpparams) = quiz_statistics_renumber_placeholders(
  51                  $grpsql, $grpparams, 'statsuser');
  52          $whereqa .= " AND quiza.userid $grpsql";
  53          $qaparams += $grpparams;
  54      }
  55  
  56      $whichattemptsql = quiz_report_grade_method_sql($whichattempts);
  57      if ($whichattemptsql) {
  58          $whereqa .= ' AND '.$whichattemptsql;
  59      }
  60  
  61      if (!$includeungraded) {
  62          $whereqa .= ' AND quiza.sumgrades IS NOT NULL';
  63      }
  64  
  65      return array($fromqa, $whereqa, $qaparams);
  66  }
  67  
  68  /**
  69   * Re-number all the params beginning with $paramprefix in a fragment of SQL.
  70   *
  71   * @param string $sql the SQL.
  72   * @param array $params the params.
  73   * @param string $paramprefix the parameter prefix.
  74   * @return array with two elements, the modified SQL, and the modified params.
  75   */
  76  function quiz_statistics_renumber_placeholders($sql, $params, $paramprefix) {
  77      $basenumber = null;
  78      $newparams = array();
  79      $newsql = preg_replace_callback('~:' . preg_quote($paramprefix, '~') . '(\d+)\b~',
  80              function($match) use ($paramprefix, $params, &$newparams, &$basenumber) {
  81                  if ($basenumber === null) {
  82                      $basenumber = $match[1] - 1;
  83                  }
  84                  $oldname = $paramprefix . $match[1];
  85                  $newname = $paramprefix . ($match[1] - $basenumber);
  86                  $newparams[$newname] = $params[$oldname];
  87                  return ':' . $newname;
  88              }, $sql);
  89  
  90      return array($newsql, $newparams);
  91  }
  92  
  93  /**
  94   * Return a {@link qubaid_condition} from the values returned by {@link quiz_statistics_attempts_sql}.
  95   *
  96   * @param int     $quizid
  97   * @param array   $groupstudents
  98   * @param string $whichattempts which attempts to use, represented internally as one of the constants as used in
  99   *                                   $quiz->grademethod ie.
 100   *                                   QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
 101   *                                   we calculate stats based on which attempts would affect the grade for each student.
 102   * @param bool    $includeungraded
 103   * @return        \qubaid_join
 104   */
 105  function quiz_statistics_qubaids_condition($quizid, $groupstudents, $whichattempts = QUIZ_GRADEAVERAGE, $includeungraded = false) {
 106      list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql($quizid, $groupstudents, $whichattempts, $includeungraded);
 107      return new qubaid_join($fromqa, 'quiza.uniqueid', $whereqa, $qaparams);
 108  }
 109  
 110  /**
 111   * This helper function returns a sequence of colours each time it is called.
 112   * Used for choosing colours for graph data series.
 113   * @return string colour name.
 114   * @deprecated since Moodle 3.2
 115   */
 116  function quiz_statistics_graph_get_new_colour() {
 117      debugging('The function quiz_statistics_graph_get_new_colour() is deprecated, please do not use it any more. '
 118          . 'Colours will be handled by the charting library directly.', DEBUG_DEVELOPER);
 119  
 120      static $colourindex = -1;
 121      $colours = array('red', 'green', 'yellow', 'orange', 'purple', 'black',
 122          'maroon', 'blue', 'ltgreen', 'navy', 'ltred', 'ltltgreen', 'ltltorange',
 123          'olive', 'gray', 'ltltred', 'ltorange', 'lime', 'ltblue', 'ltltblue');
 124  
 125      $colourindex = ($colourindex + 1) % count($colours);
 126  
 127      return $colours[$colourindex];
 128  }


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