Merge branch 'MDL-56511-master' of git://github.com/andrewnicols/moodle
[moodle.git] / question / classes / statistics / questions / calculator.php
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/>.
17 /**
18  * Question statistics calculator class. Used in the quiz statistics report but also available for use elsewhere.
19  *
20  * @package    core
21  * @subpackage questionbank
22  * @copyright  2013 Open University
23  * @author     Jamie Pratt <me@jamiep.org>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 namespace core_question\statistics\questions;
28 defined('MOODLE_INTERNAL') || die();
30 /**
31  * This class has methods to compute the question statistics from the raw data.
32  *
33  * @copyright 2013 Open University
34  * @author    Jamie Pratt <me@jamiep.org>
35  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class calculator {
39     /**
40      * @var all_calculated_for_qubaid_condition all the stats calculated for slots and sub-questions and variants of those
41      *                                                  questions.
42      */
43     protected $stats;
45     /**
46      * @var float
47      */
48     protected $sumofmarkvariance = 0;
50     /**
51      * @var array[] keyed by a string representing the pool of questions that this random question draws from.
52      *              string as returned from {@link \core_question\statistics\questions\calculated::random_selector_string}
53      */
54     protected $randomselectors = array();
56     /**
57      * @var \progress_trace
58      */
59     protected $progress;
61     /**
62      * @var string The class name of the class to instantiate to store statistics calculated.
63      */
64     protected $statscollectionclassname = '\core_question\statistics\questions\all_calculated_for_qubaid_condition';
66     /**
67      * Constructor.
68      *
69      * @param object[] questions to analyze, keyed by slot, also analyses sub questions for random questions.
70      *                              we expect some extra fields - slot, maxmark and number on the full question data objects.
71      * @param \core\progress\base|null $progress the element to send progress messages to, default is {@link \core\progress\none}.
72      */
73     public function __construct($questions, $progress = null) {
75         if ($progress === null) {
76             $progress = new \core\progress\none();
77         }
78         $this->progress = $progress;
79         $this->stats = new $this->statscollectionclassname();
80         foreach ($questions as $slot => $question) {
81             $this->stats->initialise_for_slot($slot, $question);
82             $this->stats->for_slot($slot)->randomguessscore = $this->get_random_guess_score($question);
83         }
84     }
86     /**
87      * Calculate the stats.
88      *
89      * @param \qubaid_condition $qubaids Which question usages to calculate the stats for?
90      * @return all_calculated_for_qubaid_condition The calculated stats.
91      */
92     public function calculate($qubaids) {
94         $this->progress->start_progress('', 6);
96         list($lateststeps, $summarks) = $this->get_latest_steps($qubaids);
98         if ($lateststeps) {
99             $this->progress->start_progress('', count($lateststeps), 1);
100             // Compute the statistics of position, and for random questions, work
101             // out which questions appear in which positions.
102             foreach ($lateststeps as $step) {
104                 $this->progress->increment_progress();
106                 $israndomquestion = ($step->questionid != $this->stats->for_slot($step->slot)->questionid);
107                 $breakdownvariants = !$israndomquestion && $this->stats->for_slot($step->slot)->break_down_by_variant();
108                 // If this is a variant we have not seen before create a place to store stats calculations for this variant.
109                 if ($breakdownvariants && !$this->stats->has_slot($step->slot, $step->variant)) {
110                     $question = $this->stats->for_slot($step->slot)->question;
111                     $this->stats->initialise_for_slot($step->slot, $question, $step->variant);
112                     $this->stats->for_slot($step->slot, $step->variant)->randomguessscore =
113                                                                                     $this->get_random_guess_score($question);
114                 }
116                 // Step data walker for main question.
117                 $this->initial_steps_walker($step, $this->stats->for_slot($step->slot), $summarks, true, $breakdownvariants);
119                 // If this is a random question do the calculations for sub question stats.
120                 if ($israndomquestion) {
121                     if (!$this->stats->has_subq($step->questionid)) {
122                         $this->stats->initialise_for_subq($step);
123                     } else if ($this->stats->for_subq($step->questionid)->maxmark != $step->maxmark) {
124                         $this->stats->for_subq($step->questionid)->differentweights = true;
125                     }
127                     // If this is a variant of this subq we have not seen before create a place to store stats calculations for it.
128                     if (!$this->stats->has_subq($step->questionid, $step->variant)) {
129                         $this->stats->initialise_for_subq($step, $step->variant);
130                     }
132                     $this->initial_steps_walker($step, $this->stats->for_subq($step->questionid), $summarks, false);
134                     // Extra stuff we need to do in this loop for subqs to keep track of where they need to be displayed later.
136                     $number = $this->stats->for_slot($step->slot)->question->number;
137                     $this->stats->for_subq($step->questionid)->usedin[$number] = $number;
139                     // Keep track of which random questions are actually selected from each pool of questions that random
140                     // questions are pulled from.
141                     $randomselectorstring = $this->stats->for_slot($step->slot)->random_selector_string();
142                     if (!isset($this->randomselectors[$randomselectorstring])) {
143                         $this->randomselectors[$randomselectorstring] = array();
144                     }
145                     $this->randomselectors[$randomselectorstring][$step->questionid] = $step->questionid;
146                 }
147             }
148             $this->progress->end_progress();
150             foreach ($this->randomselectors as $key => $notused) {
151                 ksort($this->randomselectors[$key]);
152                 $this->randomselectors[$key] = implode(',', $this->randomselectors[$key]);
153             }
155             $this->stats->subquestions = question_load_questions($this->stats->get_all_subq_ids());
156             // Compute the statistics for sub questions, if there are any.
157             $this->progress->start_progress('', count($this->stats->subquestions), 1);
158             foreach ($this->stats->subquestions as $qid => $subquestion) {
159                 $this->progress->increment_progress();
160                 $subquestion->maxmark = $this->stats->for_subq($qid)->maxmark;
161                 $this->stats->for_subq($qid)->question = $subquestion;
162                 $this->stats->for_subq($qid)->randomguessscore = $this->get_random_guess_score($subquestion);
164                 if ($variants = $this->stats->for_subq($qid)->get_variants()) {
165                     foreach ($variants as $variant) {
166                         $this->stats->for_subq($qid, $variant)->question = $subquestion;
167                         $this->stats->for_subq($qid, $variant)->randomguessscore = $this->get_random_guess_score($subquestion);
168                     }
169                     $this->stats->for_subq($qid)->sort_variants();
170                 }
171                 $this->initial_question_walker($this->stats->for_subq($qid));
173                 if ($this->stats->for_subq($qid)->usedin) {
174                     sort($this->stats->for_subq($qid)->usedin, SORT_NUMERIC);
175                     $this->stats->for_subq($qid)->positions = implode(',', $this->stats->for_subq($qid)->usedin);
176                 } else {
177                     $this->stats->for_subq($qid)->positions = '';
178                 }
179             }
180             $this->progress->end_progress();
182             // Finish computing the averages, and put the sub-question data into the
183             // corresponding questions.
185             // This cannot be a foreach loop because we need to have both
186             // $question and $nextquestion available, but apart from that it is
187             // foreach ($this->questions as $qid => $question).
188             $slots = $this->stats->get_all_slots();
189             $this->progress->start_progress('', count($slots), 1);
190             foreach ($slots as $slot) {
191                 $this->stats->for_slot($slot)->sort_variants();
192                 $this->progress->increment_progress();
193                 $nextslot = current($slots);
195                 $this->initial_question_walker($this->stats->for_slot($slot));
197                 // The rest of this loop is to finish working out where randomly selected question stats should be displayed.
198                 if ($this->stats->for_slot($slot)->question->qtype == 'random') {
199                     $randomselectorstring = $this->stats->for_slot($slot)->random_selector_string();
200                     if ($nextslot &&  ($randomselectorstring == $this->stats->for_slot($nextslot)->random_selector_string())) {
201                         continue; // Next loop iteration.
202                     }
203                     if (isset($this->randomselectors[$randomselectorstring])) {
204                         $this->stats->for_slot($slot)->subquestions = $this->randomselectors[$randomselectorstring];
205                     }
206                 }
207             }
208             $this->progress->end_progress();
210             // Go through the records one more time.
211             $this->progress->start_progress('', count($lateststeps), 1);
212             foreach ($lateststeps as $step) {
213                 $this->progress->increment_progress();
214                 $israndomquestion = ($this->stats->for_slot($step->slot)->question->qtype == 'random');
215                 $this->secondary_steps_walker($step, $this->stats->for_slot($step->slot), $summarks);
217                 if ($israndomquestion) {
218                     $this->secondary_steps_walker($step, $this->stats->for_subq($step->questionid), $summarks);
219                 }
220             }
221             $this->progress->end_progress();
223             $slots = $this->stats->get_all_slots();
224             $this->progress->start_progress('', count($slots), 1);
225             $sumofcovariancewithoverallmark = 0;
226             foreach ($this->stats->get_all_slots() as $slot) {
227                 $this->progress->increment_progress();
228                 $this->secondary_question_walker($this->stats->for_slot($slot));
230                 $this->sumofmarkvariance += $this->stats->for_slot($slot)->markvariance;
232                 if ($this->stats->for_slot($slot)->covariancewithoverallmark >= 0) {
233                     $sumofcovariancewithoverallmark += sqrt($this->stats->for_slot($slot)->covariancewithoverallmark);
234                 }
235             }
236             $this->progress->end_progress();
238             $subqids = $this->stats->get_all_subq_ids();
239             $this->progress->start_progress('', count($subqids), 1);
240             foreach ($subqids as $subqid) {
241                 $this->progress->increment_progress();
242                 $this->secondary_question_walker($this->stats->for_subq($subqid));
243             }
244             $this->progress->end_progress();
246             foreach ($this->stats->get_all_slots() as $slot) {
247                 if ($sumofcovariancewithoverallmark) {
248                     if ($this->stats->for_slot($slot)->negcovar) {
249                         $this->stats->for_slot($slot)->effectiveweight = null;
250                     } else {
251                         $this->stats->for_slot($slot)->effectiveweight =
252                                                         100 * sqrt($this->stats->for_slot($slot)->covariancewithoverallmark) /
253                                                         $sumofcovariancewithoverallmark;
254                     }
255                 } else {
256                     $this->stats->for_slot($slot)->effectiveweight = null;
257                 }
258             }
259             $this->stats->cache($qubaids);
261             // All finished.
262             $this->progress->end_progress();
263         }
264         return $this->stats;
265     }
267     /**
268      * Used when computing Coefficient of Internal Consistency by quiz statistics.
269      *
270      * @return float
271      */
272     public function get_sum_of_mark_variance() {
273         return $this->sumofmarkvariance;
274     }
276     /**
277      * Get the latest step data from the db, from which we will calculate stats.
278      *
279      * @param \qubaid_condition $qubaids Which question usages to get the latest steps for?
280      * @return array with two items
281      *              - $lateststeps array of latest step data for the question usages
282      *              - $summarks    array of total marks for each usage, indexed by usage id
283      */
284     protected function get_latest_steps($qubaids) {
285         $dm = new \question_engine_data_mapper();
287         $fields = "    qas.id,
288     qa.questionusageid,
289     qa.questionid,
290     qa.variant,
291     qa.slot,
292     qa.maxmark,
293     qas.fraction * qa.maxmark as mark";
295         $lateststeps = $dm->load_questions_usages_latest_steps($qubaids, $this->stats->get_all_slots(), $fields);
296         $summarks = array();
297         if ($lateststeps) {
298             foreach ($lateststeps as $step) {
299                 if (!isset($summarks[$step->questionusageid])) {
300                     $summarks[$step->questionusageid] = 0;
301                 }
302                 $summarks[$step->questionusageid] += $step->mark;
303             }
304         }
306         return array($lateststeps, $summarks);
307     }
309     /**
310      * Calculating the stats is a four step process.
311      *
312      * We loop through all 'last step' data first.
313      *
314      * Update $stats->totalmarks, $stats->markarray, $stats->totalothermarks
315      * and $stats->othermarksarray to include another state.
316      *
317      * @param object     $step         the state to add to the statistics.
318      * @param calculated $stats        the question statistics we are accumulating.
319      * @param array      $summarks     of the sum of marks for each question usage, indexed by question usage id
320      * @param bool       $positionstat whether this is a statistic of position of question.
321      * @param bool       $dovariantalso do we also want to do the same calculations for this variant?
322      */
323     protected function initial_steps_walker($step, $stats, $summarks, $positionstat = true, $dovariantalso = true) {
324         $stats->s++;
325         $stats->totalmarks += $step->mark;
326         $stats->markarray[] = $step->mark;
328         if ($positionstat) {
329             $stats->totalothermarks += $summarks[$step->questionusageid] - $step->mark;
330             $stats->othermarksarray[] = $summarks[$step->questionusageid] - $step->mark;
332         } else {
333             $stats->totalothermarks += $summarks[$step->questionusageid];
334             $stats->othermarksarray[] = $summarks[$step->questionusageid];
335         }
336         if ($dovariantalso) {
337             $this->initial_steps_walker($step, $stats->variantstats[$step->variant], $summarks, $positionstat, false);
338         }
339     }
341     /**
342      * Then loop through all questions for the first time.
343      *
344      * Perform some computations on the per-question statistics calculations after
345      * we have been through all the step data.
346      *
347      * @param calculated $stats question stats to update.
348      */
349     protected function initial_question_walker($stats) {
350         $stats->markaverage = $stats->totalmarks / $stats->s;
352         if ($stats->maxmark != 0) {
353             $stats->facility = $stats->markaverage / $stats->maxmark;
354         } else {
355             $stats->facility = null;
356         }
358         $stats->othermarkaverage = $stats->totalothermarks / $stats->s;
360         $stats->summarksaverage = $stats->totalsummarks / $stats->s;
362         sort($stats->markarray, SORT_NUMERIC);
363         sort($stats->othermarksarray, SORT_NUMERIC);
365         // Here we have collected enough data to make the decision about which questions have variants whose stats we also want to
366         // calculate. We delete the initialised structures where they are not needed.
367         if (!$stats->get_variants() || !$stats->break_down_by_variant()) {
368             $stats->clear_variants();
369         }
371         foreach ($stats->get_variants() as $variant) {
372             $this->initial_question_walker($stats->variantstats[$variant]);
373         }
374     }
376     /**
377      * Loop through all last step data again.
378      *
379      * Now we know the averages, accumulate the date needed to compute the higher
380      * moments of the question scores.
381      *
382      * @param object $step        the state to add to the statistics.
383      * @param calculated $stats       the question statistics we are accumulating.
384      * @param float[]  $summarks    of the sum of marks for each question usage, indexed by question usage id
385      */
386     protected function secondary_steps_walker($step, $stats, $summarks) {
387         $markdifference = $step->mark - $stats->markaverage;
388         if ($stats->subquestion) {
389             $othermarkdifference = $summarks[$step->questionusageid] - $stats->othermarkaverage;
390         } else {
391             $othermarkdifference = $summarks[$step->questionusageid] - $step->mark - $stats->othermarkaverage;
392         }
393         $overallmarkdifference = $summarks[$step->questionusageid] - $stats->summarksaverage;
395         $sortedmarkdifference = array_shift($stats->markarray) - $stats->markaverage;
396         $sortedothermarkdifference = array_shift($stats->othermarksarray) - $stats->othermarkaverage;
398         $stats->markvariancesum += pow($markdifference, 2);
399         $stats->othermarkvariancesum += pow($othermarkdifference, 2);
400         $stats->covariancesum += $markdifference * $othermarkdifference;
401         $stats->covariancemaxsum += $sortedmarkdifference * $sortedothermarkdifference;
402         $stats->covariancewithoverallmarksum += $markdifference * $overallmarkdifference;
404         if (isset($stats->variantstats[$step->variant])) {
405             $this->secondary_steps_walker($step, $stats->variantstats[$step->variant], $summarks);
406         }
407     }
409     /**
410      * And finally loop through all the questions again.
411      *
412      * Perform more per-question statistics calculations.
413      *
414      * @param calculated $stats question stats to update.
415      */
416     protected function secondary_question_walker($stats) {
417         if ($stats->s > 1) {
418             $stats->markvariance = $stats->markvariancesum / ($stats->s - 1);
419             $stats->othermarkvariance = $stats->othermarkvariancesum / ($stats->s - 1);
420             $stats->covariance = $stats->covariancesum / ($stats->s - 1);
421             $stats->covariancemax = $stats->covariancemaxsum / ($stats->s - 1);
422             $stats->covariancewithoverallmark = $stats->covariancewithoverallmarksum /
423                 ($stats->s - 1);
424             $stats->sd = sqrt($stats->markvariancesum / ($stats->s - 1));
426             if ($stats->covariancewithoverallmark >= 0) {
427                 $stats->negcovar = 0;
428             } else {
429                 $stats->negcovar = 1;
430             }
431         } else {
432             $stats->markvariance = null;
433             $stats->othermarkvariance = null;
434             $stats->covariance = null;
435             $stats->covariancemax = null;
436             $stats->covariancewithoverallmark = null;
437             $stats->sd = null;
438             $stats->negcovar = 0;
439         }
441         if ($stats->markvariance * $stats->othermarkvariance) {
442             $stats->discriminationindex = 100 * $stats->covariance /
443                 sqrt($stats->markvariance * $stats->othermarkvariance);
444         } else {
445             $stats->discriminationindex = null;
446         }
448         if ($stats->covariancemax) {
449             $stats->discriminativeefficiency = 100 * $stats->covariance /
450                 $stats->covariancemax;
451         } else {
452             $stats->discriminativeefficiency = null;
453         }
455         foreach ($stats->variantstats as $variantstat) {
456             $this->secondary_question_walker($variantstat);
457         }
458     }
460     /**
461      * Given the question data find the average grade that random guesses would get.
462      *
463      * @param object $questiondata the full question object.
464      * @return float the random guess score for this question.
465      */
466     protected function get_random_guess_score($questiondata) {
467         return \question_bank::get_qtype(
468             $questiondata->qtype, false)->get_random_guess_score($questiondata);
469     }
471     /**
472      * Find time of non-expired statistics in the database.
473      *
474      * @param \qubaid_condition $qubaids Which question usages to look for?
475      * @return int|bool Time of cached record that matches this qubaid_condition or false is non found.
476      */
477     public function get_last_calculated_time($qubaids) {
478         return $this->stats->get_last_calculated_time($qubaids);
479     }
481     /**
482      * Load cached statistics from the database.
483      *
484      * @param \qubaid_condition $qubaids Which question usages to load the cached stats for?
485      * @return all_calculated_for_qubaid_condition The cached stats.
486      */
487     public function get_cached($qubaids) {
488         $this->stats->get_cached($qubaids);
489         return $this->stats;
490     }