MDL-63186 question: Calculate extremums of summarised questions
[moodle.git] / question / classes / statistics / questions / calculated_random_question_summary.php
blob2075421cf74f397f0da5e693da6bd8c77d7d70b6
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.
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/>.
17 /**
18 * Question statistics calculations class. Used in the quiz statistics report.
20 * @package core_question
21 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_question\statistics\questions;
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Class calculated_random_question_summary
31 * This class is used to indicate the statistics for a random question slot should
32 * be rendered with a link to a summary of the displayed questions.
34 * It's used in the limited view of the statistics calculation in lieu of adding
35 * the stats for each subquestion individually.
37 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class calculated_random_question_summary extends calculated {
42 /**
43 * @var int only set immediately before display in the table. The order of display in the table.
45 public $subqdisplayorder;
47 /**
48 * @var calculated[] The instances storing the calculated stats of the questions that are being summarised.
50 protected $subqstats;
52 /**
53 * calculated_random_question_summary constructor.
55 * @param \stdClass $question
56 * @param int $slot
57 * @param calculated[] $subqstats The instances of the calculated stats of the questions that are being summarised.
59 public function __construct($question, $slot, $subqstats) {
60 parent::__construct($question, $slot);
62 $this->subqstats = $subqstats;
63 $this->subquestions = implode(',', array_column($subqstats, 'questionid'));
66 /**
67 * This is a summary stat so never breakdown by variant.
69 * @return bool
71 public function break_down_by_variant() {
72 return false;
75 /**
76 * Returns the minimum and maximum values of the given attribute in the summarised calculated stats.
78 * @param string $attribute The attribute that we are looking for its extremums.
79 * @return array An array of [min,max]
81 public function get_min_max_of($attribute) {
82 $getmethod = 'get_min_max_of_' . $attribute;
83 if (method_exists($this, $getmethod)) {
84 return $this->$getmethod();
85 } else {
86 $min = $max = null;
88 // We cannot simply use min or max functions because, in theory, some attributes might be non-scalar.
89 foreach (array_column($this->subqstats, $attribute) as $value) {
90 if (is_scalar($value)) {
91 if (!isset($min)) {
92 $min = $value;
94 if (!isset($max)) {
95 $max = $value;
98 $min = min($min, $value);
99 $max = max($max, $value);
103 return [$min, $max];
108 * Returns the minimum and maximum values of the standard deviation in the summarised calculated stats.
109 * @return array An array of [min,max]
111 protected function get_min_max_of_sd() {
112 $min = $max = null;
114 foreach ($this->subqstats as $subqstat) {
115 if (isset($subqstat->sd) && $subqstat->maxmark) {
116 if (!isset($min)) {
117 $min = $subqstat->sd / $subqstat->maxmark;
119 if (!isset($max)) {
120 $max = $subqstat->sd / $subqstat->maxmark;
123 $min = min($min, $subqstat->sd / $subqstat->maxmark);
124 $max = max($max, $subqstat->sd / $subqstat->maxmark);
128 return [$min, $max];