| 1 | efrain | 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 |  * A collection of all the question statistics calculated for an activity instance ie. the stats calculated for slots and
 | 
        
           |  |  | 19 |  * sub-questions and variants of those questions.
 | 
        
           |  |  | 20 |  *
 | 
        
           |  |  | 21 |  * @package    core_question
 | 
        
           |  |  | 22 |  * @copyright  2014 The Open University
 | 
        
           |  |  | 23 |  * @author     James Pratt me@jamiep.org
 | 
        
           |  |  | 24 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 25 |  */
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | namespace core_question\statistics\questions;
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | use question_bank;
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | /**
 | 
        
           |  |  | 32 |  * A collection of all the question statistics calculated for an activity instance.
 | 
        
           |  |  | 33 |  *
 | 
        
           |  |  | 34 |  * @package    core_question
 | 
        
           |  |  | 35 |  * @copyright  2014 The Open University
 | 
        
           |  |  | 36 |  * @author     James Pratt me@jamiep.org
 | 
        
           |  |  | 37 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 38 |  */
 | 
        
           |  |  | 39 | class all_calculated_for_qubaid_condition {
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |     /**
 | 
        
           |  |  | 42 |      * @var object[]
 | 
        
           |  |  | 43 |      */
 | 
        
           |  |  | 44 |     public $subquestions = [];
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |     /**
 | 
        
           |  |  | 47 |      * Holds slot (position) stats and stats for variants of questions in slots.
 | 
        
           |  |  | 48 |      *
 | 
        
           |  |  | 49 |      * @var calculated[]
 | 
        
           |  |  | 50 |      */
 | 
        
           |  |  | 51 |     public $questionstats = array();
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |     /**
 | 
        
           |  |  | 54 |      * Holds sub-question stats and stats for variants of subqs.
 | 
        
           |  |  | 55 |      *
 | 
        
           |  |  | 56 |      * @var calculated_for_subquestion[]
 | 
        
           |  |  | 57 |      */
 | 
        
           |  |  | 58 |     public $subquestionstats = array();
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |     /**
 | 
        
           |  |  | 61 |      * Set up a calculated_for_subquestion instance ready to store a randomly selected question's stats.
 | 
        
           |  |  | 62 |      *
 | 
        
           |  |  | 63 |      * @param object     $step
 | 
        
           |  |  | 64 |      * @param int|null   $variant Is this to keep track of a variant's stats? If so what is the variant, if not null.
 | 
        
           |  |  | 65 |      */
 | 
        
           |  |  | 66 |     public function initialise_for_subq($step, $variant = null) {
 | 
        
           |  |  | 67 |         $newsubqstat = new calculated_for_subquestion($step, $variant);
 | 
        
           |  |  | 68 |         if ($variant === null) {
 | 
        
           |  |  | 69 |             $this->subquestionstats[$step->questionid] = $newsubqstat;
 | 
        
           |  |  | 70 |         } else {
 | 
        
           |  |  | 71 |             $this->subquestionstats[$step->questionid]->variantstats[$variant] = $newsubqstat;
 | 
        
           |  |  | 72 |         }
 | 
        
           |  |  | 73 |     }
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |     /**
 | 
        
           |  |  | 76 |      * Set up a calculated instance ready to store a slot question's stats.
 | 
        
           |  |  | 77 |      *
 | 
        
           |  |  | 78 |      * @param int      $slot
 | 
        
           |  |  | 79 |      * @param object   $question
 | 
        
           |  |  | 80 |      * @param int|null $variant Is this to keep track of a variant's stats? If so what is the variant, if not null.
 | 
        
           |  |  | 81 |      */
 | 
        
           |  |  | 82 |     public function initialise_for_slot($slot, $question, $variant = null) {
 | 
        
           |  |  | 83 |         $newqstat = new calculated($question, $slot, $variant);
 | 
        
           |  |  | 84 |         if ($variant === null) {
 | 
        
           |  |  | 85 |             $this->questionstats[$slot] = $newqstat;
 | 
        
           |  |  | 86 |         } else {
 | 
        
           |  |  | 87 |             $this->questionstats[$slot]->variantstats[$variant] = $newqstat;
 | 
        
           |  |  | 88 |         }
 | 
        
           |  |  | 89 |     }
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |     /**
 | 
        
           |  |  | 92 |      * Do we have stats for a particular quesitonid (and optionally variant)?
 | 
        
           |  |  | 93 |      *
 | 
        
           |  |  | 94 |      * @param int  $questionid The id of the sub question.
 | 
        
           |  |  | 95 |      * @param int|null $variant if not null then we want the object to store a variant of a sub-question's stats.
 | 
        
           |  |  | 96 |      * @return bool whether those stats exist (yet).
 | 
        
           |  |  | 97 |      */
 | 
        
           |  |  | 98 |     public function has_subq($questionid, $variant = null) {
 | 
        
           |  |  | 99 |         if ($variant === null) {
 | 
        
           |  |  | 100 |             return isset($this->subquestionstats[$questionid]);
 | 
        
           |  |  | 101 |         } else {
 | 
        
           |  |  | 102 |             return isset($this->subquestionstats[$questionid]->variantstats[$variant]);
 | 
        
           |  |  | 103 |         }
 | 
        
           |  |  | 104 |     }
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 |     /**
 | 
        
           |  |  | 107 |      * Reference for a item stats instance for a questionid and optional variant no.
 | 
        
           |  |  | 108 |      *
 | 
        
           |  |  | 109 |      * @param int  $questionid The id of the sub question.
 | 
        
           |  |  | 110 |      * @param int|null $variant if not null then we want the object to store a variant of a sub-question's stats.
 | 
        
           |  |  | 111 |      * @return calculated|calculated_for_subquestion stats instance for a questionid and optional variant no.
 | 
        
           |  |  | 112 |      *     Will be a calculated_for_subquestion if no variant specified.
 | 
        
           |  |  | 113 |      * @throws \coding_exception if there is an attempt to respond to a non-existant set of stats.
 | 
        
           |  |  | 114 |      */
 | 
        
           |  |  | 115 |     public function for_subq($questionid, $variant = null) {
 | 
        
           |  |  | 116 |         if ($variant === null) {
 | 
        
           |  |  | 117 |             if (!isset($this->subquestionstats[$questionid])) {
 | 
        
           |  |  | 118 |                 throw new \coding_exception('Reference to unknown question id ' . $questionid);
 | 
        
           |  |  | 119 |             } else {
 | 
        
           |  |  | 120 |                 return $this->subquestionstats[$questionid];
 | 
        
           |  |  | 121 |             }
 | 
        
           |  |  | 122 |         } else {
 | 
        
           |  |  | 123 |             if (!isset($this->subquestionstats[$questionid]->variantstats[$variant])) {
 | 
        
           |  |  | 124 |                 throw new \coding_exception('Reference to unknown question id ' . $questionid .
 | 
        
           |  |  | 125 |                         ' variant ' . $variant);
 | 
        
           |  |  | 126 |             } else {
 | 
        
           |  |  | 127 |                 return $this->subquestionstats[$questionid]->variantstats[$variant];
 | 
        
           |  |  | 128 |             }
 | 
        
           |  |  | 129 |         }
 | 
        
           |  |  | 130 |     }
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 |     /**
 | 
        
           |  |  | 133 |      * ids of all randomly selected question for all slots.
 | 
        
           |  |  | 134 |      *
 | 
        
           |  |  | 135 |      * @return int[] An array of all sub-question ids.
 | 
        
           |  |  | 136 |      */
 | 
        
           |  |  | 137 |     public function get_all_subq_ids() {
 | 
        
           |  |  | 138 |         return array_keys($this->subquestionstats);
 | 
        
           |  |  | 139 |     }
 | 
        
           |  |  | 140 |   | 
        
           |  |  | 141 |     /**
 | 
        
           |  |  | 142 |      * All slots nos that stats have been calculated for.
 | 
        
           |  |  | 143 |      *
 | 
        
           |  |  | 144 |      * @return int[] An array of all slot nos.
 | 
        
           |  |  | 145 |      */
 | 
        
           |  |  | 146 |     public function get_all_slots() {
 | 
        
           |  |  | 147 |         return array_keys($this->questionstats);
 | 
        
           |  |  | 148 |     }
 | 
        
           |  |  | 149 |   | 
        
           |  |  | 150 |     /**
 | 
        
           |  |  | 151 |      * Do we have stats for a particular slot (and optionally variant)?
 | 
        
           |  |  | 152 |      *
 | 
        
           |  |  | 153 |      * @param int  $slot The slot no.
 | 
        
           |  |  | 154 |      * @param int|null $variant if provided then we want the object which stores a variant of a position's stats.
 | 
        
           |  |  | 155 |      * @return bool whether those stats exist (yet).
 | 
        
           |  |  | 156 |      */
 | 
        
           |  |  | 157 |     public function has_slot($slot, $variant = null) {
 | 
        
           |  |  | 158 |         if ($variant === null) {
 | 
        
           |  |  | 159 |             return isset($this->questionstats[$slot]);
 | 
        
           |  |  | 160 |         } else {
 | 
        
           |  |  | 161 |             return isset($this->questionstats[$slot]->variantstats[$variant]);
 | 
        
           |  |  | 162 |         }
 | 
        
           |  |  | 163 |     }
 | 
        
           |  |  | 164 |   | 
        
           |  |  | 165 |     /**
 | 
        
           |  |  | 166 |      * Get position stats instance for a slot and optional variant no.
 | 
        
           |  |  | 167 |      *
 | 
        
           |  |  | 168 |      * @param int  $slot The slot no.
 | 
        
           |  |  | 169 |      * @param int|null $variant if provided then we want the object which stores a variant of a position's stats.
 | 
        
           |  |  | 170 |      * @return calculated|calculated_for_subquestion An instance of the class storing the calculated position stats.
 | 
        
           |  |  | 171 |      * @throws \coding_exception if there is an attempt to respond to a non-existant set of stats.
 | 
        
           |  |  | 172 |      */
 | 
        
           |  |  | 173 |     public function for_slot($slot, $variant = null) {
 | 
        
           |  |  | 174 |         if ($variant === null) {
 | 
        
           |  |  | 175 |             if (!isset($this->questionstats[$slot])) {
 | 
        
           |  |  | 176 |                 throw new \coding_exception('Reference to unknown slot ' . $slot);
 | 
        
           |  |  | 177 |             } else {
 | 
        
           |  |  | 178 |                 return $this->questionstats[$slot];
 | 
        
           |  |  | 179 |             }
 | 
        
           |  |  | 180 |         } else {
 | 
        
           |  |  | 181 |             if (!isset($this->questionstats[$slot]->variantstats[$variant])) {
 | 
        
           |  |  | 182 |                 throw new \coding_exception('Reference to unknown slot ' . $slot . ' variant ' . $variant);
 | 
        
           |  |  | 183 |             } else {
 | 
        
           |  |  | 184 |                 return $this->questionstats[$slot]->variantstats[$variant];
 | 
        
           |  |  | 185 |             }
 | 
        
           |  |  | 186 |         }
 | 
        
           |  |  | 187 |     }
 | 
        
           |  |  | 188 |   | 
        
           |  |  | 189 |     /**
 | 
        
           |  |  | 190 |      * Load cached statistics from the database.
 | 
        
           |  |  | 191 |      *
 | 
        
           |  |  | 192 |      * @param \qubaid_condition $qubaids Which question usages to load stats for?
 | 
        
           |  |  | 193 |      */
 | 
        
           |  |  | 194 |     public function get_cached($qubaids) {
 | 
        
           |  |  | 195 |         global $DB;
 | 
        
           |  |  | 196 |   | 
        
           |  |  | 197 |         $timemodified = self::get_last_calculated_time($qubaids);
 | 
        
           |  |  | 198 |         $questionstatrecs = $DB->get_records('question_statistics',
 | 
        
           |  |  | 199 |                 ['hashcode' => $qubaids->get_hash_code(), 'timemodified' => $timemodified]);
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 |         $questionids = array();
 | 
        
           |  |  | 202 |         foreach ($questionstatrecs as $fromdb) {
 | 
        
           |  |  | 203 |             if (is_null($fromdb->variant) && !$fromdb->slot) {
 | 
        
           |  |  | 204 |                 $questionids[] = $fromdb->questionid;
 | 
        
           |  |  | 205 |             }
 | 
        
           |  |  | 206 |         }
 | 
        
           |  |  | 207 |         $this->subquestions = question_load_questions($questionids);
 | 
        
           |  |  | 208 |         foreach ($questionstatrecs as $fromdb) {
 | 
        
           |  |  | 209 |             if (is_null($fromdb->variant)) {
 | 
        
           |  |  | 210 |                 if ($fromdb->slot) {
 | 
        
           |  |  | 211 |                     if (!isset($this->questionstats[$fromdb->slot])) {
 | 
        
           |  |  | 212 |                         debugging('Statistics found for slot ' . $fromdb->slot .
 | 
        
           |  |  | 213 |                             ' in stats ' . json_encode($qubaids->from_where_params()) .
 | 
        
           |  |  | 214 |                             ' which is not an analysable question.', DEBUG_DEVELOPER);
 | 
        
           |  |  | 215 |                     }
 | 
        
           |  |  | 216 |                     $this->questionstats[$fromdb->slot]->populate_from_record($fromdb);
 | 
        
           |  |  | 217 |                 } else {
 | 
        
           |  |  | 218 |                     $this->subquestionstats[$fromdb->questionid] = new calculated_for_subquestion();
 | 
        
           |  |  | 219 |                     $this->subquestionstats[$fromdb->questionid]->populate_from_record($fromdb);
 | 
        
           |  |  | 220 |                     if (isset($this->subquestions[$fromdb->questionid])) {
 | 
        
           |  |  | 221 |                         $this->subquestionstats[$fromdb->questionid]->question =
 | 
        
           |  |  | 222 |                             $this->subquestions[$fromdb->questionid];
 | 
        
           |  |  | 223 |                     } else {
 | 
        
           |  |  | 224 |                         $this->subquestionstats[$fromdb->questionid]->question = question_bank::get_qtype(
 | 
        
           |  |  | 225 |                             'missingtype', false)->make_deleted_instance($fromdb->questionid, 1);
 | 
        
           |  |  | 226 |                     }
 | 
        
           |  |  | 227 |                 }
 | 
        
           |  |  | 228 |             }
 | 
        
           |  |  | 229 |         }
 | 
        
           |  |  | 230 |         // Add cached variant stats to data structure.
 | 
        
           |  |  | 231 |         foreach ($questionstatrecs as $fromdb) {
 | 
        
           |  |  | 232 |             if (!is_null($fromdb->variant)) {
 | 
        
           |  |  | 233 |                 if ($fromdb->slot) {
 | 
        
           |  |  | 234 |                     if (!isset($this->questionstats[$fromdb->slot])) {
 | 
        
           |  |  | 235 |                         debugging('Statistics found for slot ' . $fromdb->slot .
 | 
        
           |  |  | 236 |                             ' in stats ' . json_encode($qubaids->from_where_params()) .
 | 
        
           |  |  | 237 |                             ' which is not an analysable question.', DEBUG_DEVELOPER);
 | 
        
           |  |  | 238 |                         continue;
 | 
        
           |  |  | 239 |                     }
 | 
        
           |  |  | 240 |                     $newcalcinstance = new calculated();
 | 
        
           |  |  | 241 |                     $this->questionstats[$fromdb->slot]->variantstats[$fromdb->variant] = $newcalcinstance;
 | 
        
           |  |  | 242 |                     $newcalcinstance->question = $this->questionstats[$fromdb->slot]->question;
 | 
        
           |  |  | 243 |                 } else {
 | 
        
           |  |  | 244 |                     $newcalcinstance = new calculated_for_subquestion();
 | 
        
           |  |  | 245 |                     $this->subquestionstats[$fromdb->questionid]->variantstats[$fromdb->variant] = $newcalcinstance;
 | 
        
           |  |  | 246 |                     if (isset($this->subquestions[$fromdb->questionid])) {
 | 
        
           |  |  | 247 |                         $newcalcinstance->question = $this->subquestions[$fromdb->questionid];
 | 
        
           |  |  | 248 |                     } else {
 | 
        
           |  |  | 249 |                         $newcalcinstance->question = question_bank::get_qtype(
 | 
        
           |  |  | 250 |                             'missingtype', false)->make_deleted_instance($fromdb->questionid, 1);
 | 
        
           |  |  | 251 |                     }
 | 
        
           |  |  | 252 |                 }
 | 
        
           |  |  | 253 |                 $newcalcinstance->populate_from_record($fromdb);
 | 
        
           |  |  | 254 |             }
 | 
        
           |  |  | 255 |         }
 | 
        
           |  |  | 256 |     }
 | 
        
           |  |  | 257 |   | 
        
           |  |  | 258 |     /**
 | 
        
           |  |  | 259 |      * Find time of non-expired statistics in the database.
 | 
        
           |  |  | 260 |      *
 | 
        
           |  |  | 261 |      * @param \qubaid_condition $qubaids Which question usages to look for stats for?
 | 
        
           |  |  | 262 |      * @return int|bool Time of cached record that matches this qubaid_condition or false if non found.
 | 
        
           |  |  | 263 |      */
 | 
        
           |  |  | 264 |     public function get_last_calculated_time($qubaids) {
 | 
        
           |  |  | 265 |         global $DB;
 | 
        
           |  |  | 266 |         $lastcalculatedtime = $DB->get_field('question_statistics', 'COALESCE(MAX(timemodified), 0)',
 | 
        
           |  |  | 267 |                 ['hashcode' => $qubaids->get_hash_code()]);
 | 
        
           |  |  | 268 |         if ($lastcalculatedtime) {
 | 
        
           |  |  | 269 |             return $lastcalculatedtime;
 | 
        
           |  |  | 270 |         } else {
 | 
        
           |  |  | 271 |             return false;
 | 
        
           |  |  | 272 |         }
 | 
        
           |  |  | 273 |     }
 | 
        
           |  |  | 274 |   | 
        
           |  |  | 275 |     /**
 | 
        
           |  |  | 276 |      * Save stats to db, first cleaning up any old ones.
 | 
        
           |  |  | 277 |      *
 | 
        
           |  |  | 278 |      * @param \qubaid_condition $qubaids Which question usages are we caching the stats of?
 | 
        
           |  |  | 279 |      */
 | 
        
           |  |  | 280 |     public function cache($qubaids) {
 | 
        
           |  |  | 281 |         global $DB;
 | 
        
           |  |  | 282 |   | 
        
           |  |  | 283 |         $transaction = $DB->start_delegated_transaction();
 | 
        
           |  |  | 284 |         $timemodified = time();
 | 
        
           |  |  | 285 |   | 
        
           |  |  | 286 |         foreach ($this->get_all_slots() as $slot) {
 | 
        
           |  |  | 287 |             $this->for_slot($slot)->cache($qubaids, $timemodified);
 | 
        
           |  |  | 288 |         }
 | 
        
           |  |  | 289 |   | 
        
           |  |  | 290 |         foreach ($this->get_all_subq_ids() as $subqid) {
 | 
        
           |  |  | 291 |             $this->for_subq($subqid)->cache($qubaids, $timemodified);
 | 
        
           |  |  | 292 |         }
 | 
        
           |  |  | 293 |   | 
        
           |  |  | 294 |         $transaction->allow_commit();
 | 
        
           |  |  | 295 |     }
 | 
        
           |  |  | 296 |   | 
        
           |  |  | 297 |     /**
 | 
        
           |  |  | 298 |      * Return all sub-questions used.
 | 
        
           |  |  | 299 |      *
 | 
        
           |  |  | 300 |      * @return \object[] array of questions.
 | 
        
           |  |  | 301 |      */
 | 
        
           |  |  | 302 |     public function get_sub_questions() {
 | 
        
           |  |  | 303 |         return $this->subquestions;
 | 
        
           |  |  | 304 |     }
 | 
        
           |  |  | 305 |   | 
        
           |  |  | 306 |     /**
 | 
        
           |  |  | 307 |      * Return all stats for one slot, stats for the slot itself, and either :
 | 
        
           |  |  | 308 |      *  - variants of question
 | 
        
           |  |  | 309 |      *  - variants of randomly selected questions
 | 
        
           |  |  | 310 |      *  - randomly selected questions
 | 
        
           |  |  | 311 |      *
 | 
        
           |  |  | 312 |      * @param int      $slot          the slot no
 | 
        
           |  |  | 313 |      * @param bool|int $limitvariants limit number of variants and sub-questions displayed?
 | 
        
           |  |  | 314 |      * @return calculated|calculated_for_subquestion[] stats to display
 | 
        
           |  |  | 315 |      */
 | 
        
           |  |  | 316 |     public function structure_analysis_for_one_slot($slot, $limitvariants = false) {
 | 
        
           |  |  | 317 |         return array_merge(array($this->for_slot($slot)), $this->all_subq_and_variant_stats_for_slot($slot, $limitvariants));
 | 
        
           |  |  | 318 |     }
 | 
        
           |  |  | 319 |   | 
        
           |  |  | 320 |     /**
 | 
        
           |  |  | 321 |      * Call after calculations to output any error messages.
 | 
        
           |  |  | 322 |      *
 | 
        
           |  |  | 323 |      * @return string[] Array of strings describing error messages found during stats calculation.
 | 
        
           |  |  | 324 |      */
 | 
        
           |  |  | 325 |     public function any_error_messages() {
 | 
        
           |  |  | 326 |         $errors = array();
 | 
        
           |  |  | 327 |         foreach ($this->get_all_slots() as $slot) {
 | 
        
           |  |  | 328 |             foreach ($this->for_slot($slot)->get_sub_question_ids() as $subqid) {
 | 
        
           |  |  | 329 |                 if ($this->for_subq($subqid)->differentweights) {
 | 
        
           |  |  | 330 |                     $name = $this->for_subq($subqid)->question->name;
 | 
        
           |  |  | 331 |                     $errors[] = get_string('erroritemappearsmorethanoncewithdifferentweight', 'question', $name);
 | 
        
           |  |  | 332 |                 }
 | 
        
           |  |  | 333 |             }
 | 
        
           |  |  | 334 |         }
 | 
        
           |  |  | 335 |         return $errors;
 | 
        
           |  |  | 336 |     }
 | 
        
           |  |  | 337 |   | 
        
           |  |  | 338 |     /**
 | 
        
           |  |  | 339 |      * Return all stats for variants of question in slot $slot.
 | 
        
           |  |  | 340 |      *
 | 
        
           |  |  | 341 |      * @param int $slot The slot no.
 | 
        
           |  |  | 342 |      * @return calculated[] The instances storing the calculated stats.
 | 
        
           |  |  | 343 |      */
 | 
        
           |  |  | 344 |     protected function all_variant_stats_for_one_slot($slot) {
 | 
        
           |  |  | 345 |         $toreturn = array();
 | 
        
           |  |  | 346 |         foreach ($this->for_slot($slot)->get_variants() as $variant) {
 | 
        
           |  |  | 347 |             $toreturn[] = $this->for_slot($slot, $variant);
 | 
        
           |  |  | 348 |         }
 | 
        
           |  |  | 349 |         return $toreturn;
 | 
        
           |  |  | 350 |     }
 | 
        
           |  |  | 351 |   | 
        
           |  |  | 352 |     /**
 | 
        
           |  |  | 353 |      * Return all stats for variants of randomly selected questions for one slot $slot.
 | 
        
           |  |  | 354 |      *
 | 
        
           |  |  | 355 |      * @param int $slot The slot no.
 | 
        
           |  |  | 356 |      * @return calculated[] The instances storing the calculated stats.
 | 
        
           |  |  | 357 |      */
 | 
        
           |  |  | 358 |     protected function all_subq_variants_for_one_slot($slot) {
 | 
        
           |  |  | 359 |         $toreturn = array();
 | 
        
           |  |  | 360 |         $displayorder = 1;
 | 
        
           |  |  | 361 |         foreach ($this->for_slot($slot)->get_sub_question_ids() as $subqid) {
 | 
        
           |  |  | 362 |             if ($variants = $this->for_subq($subqid)->get_variants()) {
 | 
        
           |  |  | 363 |                 foreach ($variants as $variant) {
 | 
        
           |  |  | 364 |                     $toreturn[] = $this->make_new_subq_stat_for($displayorder, $slot, $subqid, $variant);
 | 
        
           |  |  | 365 |                 }
 | 
        
           |  |  | 366 |             }
 | 
        
           |  |  | 367 |             $displayorder++;
 | 
        
           |  |  | 368 |         }
 | 
        
           |  |  | 369 |         return $toreturn;
 | 
        
           |  |  | 370 |     }
 | 
        
           |  |  | 371 |   | 
        
           |  |  | 372 |     /**
 | 
        
           |  |  | 373 |      * Return all stats for randomly selected questions for one slot $slot.
 | 
        
           |  |  | 374 |      *
 | 
        
           |  |  | 375 |      * @param int $slot The slot no.
 | 
        
           |  |  | 376 |      * @return calculated[] The instances storing the calculated stats.
 | 
        
           |  |  | 377 |      */
 | 
        
           |  |  | 378 |     protected function all_subqs_for_one_slot($slot) {
 | 
        
           |  |  | 379 |         $displayorder = 1;
 | 
        
           |  |  | 380 |         $toreturn = array();
 | 
        
           |  |  | 381 |         foreach ($this->for_slot($slot)->get_sub_question_ids() as $subqid) {
 | 
        
           |  |  | 382 |             $toreturn[] = $this->make_new_subq_stat_for($displayorder, $slot, $subqid);
 | 
        
           |  |  | 383 |             $displayorder++;
 | 
        
           |  |  | 384 |         }
 | 
        
           |  |  | 385 |         return $toreturn;
 | 
        
           |  |  | 386 |     }
 | 
        
           |  |  | 387 |   | 
        
           |  |  | 388 |     /**
 | 
        
           |  |  | 389 |      * Return all variant or 'sub-question' stats one slot, either :
 | 
        
           |  |  | 390 |      *  - variants of question
 | 
        
           |  |  | 391 |      *  - variants of randomly selected questions
 | 
        
           |  |  | 392 |      *  - randomly selected questions
 | 
        
           |  |  | 393 |      *
 | 
        
           |  |  | 394 |      * @param int $slot the slot no
 | 
        
           |  |  | 395 |      * @param bool $limited limit number of variants and sub-questions displayed?
 | 
        
           |  |  | 396 |      * @return calculated|calculated_for_subquestion|calculated_question_summary[] stats to display
 | 
        
           |  |  | 397 |      */
 | 
        
           |  |  | 398 |     protected function all_subq_and_variant_stats_for_slot($slot, $limited) {
 | 
        
           |  |  | 399 |         // Random question in this slot?
 | 
        
           |  |  | 400 |         if ($this->for_slot($slot)->get_sub_question_ids()) {
 | 
        
           |  |  | 401 |             $toreturn = array();
 | 
        
           |  |  | 402 |   | 
        
           |  |  | 403 |             if ($limited) {
 | 
        
           |  |  | 404 |                 $randomquestioncalculated = $this->for_slot($slot);
 | 
        
           |  |  | 405 |   | 
        
           |  |  | 406 |                 if ($subqvariantstats = $this->all_subq_variants_for_one_slot($slot)) {
 | 
        
           |  |  | 407 |                     // There are some variants from randomly selected questions.
 | 
        
           |  |  | 408 |                     // If we're showing a limited view of the statistics then add a question summary stat
 | 
        
           |  |  | 409 |                     // rather than a stat for each subquestion.
 | 
        
           |  |  | 410 |                     $summarystat = $this->make_new_calculated_question_summary_stat($randomquestioncalculated, $subqvariantstats);
 | 
        
           |  |  | 411 |   | 
        
           |  |  | 412 |                     $toreturn = array_merge($toreturn, [$summarystat]);
 | 
        
           |  |  | 413 |                 }
 | 
        
           |  |  | 414 |   | 
        
           |  |  | 415 |                 if ($subqstats = $this->all_subqs_for_one_slot($slot)) {
 | 
        
           |  |  | 416 |                     // There are some randomly selected questions.
 | 
        
           |  |  | 417 |                     // If we're showing a limited view of the statistics then add a question summary stat
 | 
        
           |  |  | 418 |                     // rather than a stat for each subquestion.
 | 
        
           |  |  | 419 |                     $summarystat = $this->make_new_calculated_question_summary_stat($randomquestioncalculated, $subqstats);
 | 
        
           |  |  | 420 |   | 
        
           |  |  | 421 |                     $toreturn = array_merge($toreturn, [$summarystat]);
 | 
        
           |  |  | 422 |                 }
 | 
        
           |  |  | 423 |   | 
        
           |  |  | 424 |                 foreach ($toreturn as $index => $calculated) {
 | 
        
           |  |  | 425 |                     $calculated->subqdisplayorder = $index;
 | 
        
           |  |  | 426 |                 }
 | 
        
           |  |  | 427 |             } else {
 | 
        
           |  |  | 428 |                 $displaynumber = 1;
 | 
        
           |  |  | 429 |                 foreach ($this->for_slot($slot)->get_sub_question_ids() as $subqid) {
 | 
        
           |  |  | 430 |                     $toreturn[] = $this->make_new_subq_stat_for($displaynumber, $slot, $subqid);
 | 
        
           |  |  | 431 |                     if ($variants = $this->for_subq($subqid)->get_variants()) {
 | 
        
           |  |  | 432 |                         foreach ($variants as $variant) {
 | 
        
           |  |  | 433 |                             $toreturn[] = $this->make_new_subq_stat_for($displaynumber, $slot, $subqid, $variant);
 | 
        
           |  |  | 434 |                         }
 | 
        
           |  |  | 435 |                     }
 | 
        
           |  |  | 436 |                     $displaynumber++;
 | 
        
           |  |  | 437 |                 }
 | 
        
           |  |  | 438 |             }
 | 
        
           |  |  | 439 |   | 
        
           |  |  | 440 |             return $toreturn;
 | 
        
           |  |  | 441 |         } else {
 | 
        
           |  |  | 442 |             $variantstats = $this->all_variant_stats_for_one_slot($slot);
 | 
        
           |  |  | 443 |             if ($limited && $variantstats) {
 | 
        
           |  |  | 444 |                 $variantquestioncalculated = $this->for_slot($slot);
 | 
        
           |  |  | 445 |   | 
        
           |  |  | 446 |                 // If we're showing a limited view of the statistics then add a question summary stat
 | 
        
           |  |  | 447 |                 // rather than a stat for each variation.
 | 
        
           |  |  | 448 |                 $summarystat = $this->make_new_calculated_question_summary_stat($variantquestioncalculated, $variantstats);
 | 
        
           |  |  | 449 |   | 
        
           |  |  | 450 |                 return [$summarystat];
 | 
        
           |  |  | 451 |             } else {
 | 
        
           |  |  | 452 |                 return $variantstats;
 | 
        
           |  |  | 453 |             }
 | 
        
           |  |  | 454 |         }
 | 
        
           |  |  | 455 |     }
 | 
        
           |  |  | 456 |   | 
        
           |  |  | 457 |     /**
 | 
        
           |  |  | 458 |      * We need a new object for display. Sub-question stats can appear more than once in different slots.
 | 
        
           |  |  | 459 |      * So we create a clone of the object and then we can set properties on the object that are per slot.
 | 
        
           |  |  | 460 |      *
 | 
        
           |  |  | 461 |      * @param int  $displaynumber                   The display number for this sub question.
 | 
        
           |  |  | 462 |      * @param int  $slot                            The slot number.
 | 
        
           |  |  | 463 |      * @param int  $subqid                          The sub question id.
 | 
        
           |  |  | 464 |      * @param null|int $variant                     The variant no.
 | 
        
           |  |  | 465 |      * @return calculated_for_subquestion           The object for display.
 | 
        
           |  |  | 466 |      */
 | 
        
           |  |  | 467 |     protected function make_new_subq_stat_for($displaynumber, $slot, $subqid, $variant = null) {
 | 
        
           |  |  | 468 |         $slotstat = fullclone($this->for_subq($subqid, $variant));
 | 
        
           |  |  | 469 |         $slotstat->question->number = $this->for_slot($slot)->question->number;
 | 
        
           |  |  | 470 |         $slotstat->subqdisplayorder = $displaynumber;
 | 
        
           |  |  | 471 |         return $slotstat;
 | 
        
           |  |  | 472 |     }
 | 
        
           |  |  | 473 |   | 
        
           |  |  | 474 |     /**
 | 
        
           |  |  | 475 |      * Create a summary calculated object for a calculated question. This is used as a placeholder
 | 
        
           |  |  | 476 |      * to indicate that a calculated question has sub questions or variations to show rather than listing each
 | 
        
           |  |  | 477 |      * subquestion or variation directly.
 | 
        
           |  |  | 478 |      *
 | 
        
           |  |  | 479 |      * @param  calculated $randomquestioncalculated The calculated instance for the random question slot.
 | 
        
           |  |  | 480 |      * @param  calculated[] $subquestionstats The instances of the calculated stats of the questions that are being summarised.
 | 
        
           |  |  | 481 |      * @return calculated_question_summary
 | 
        
           |  |  | 482 |      */
 | 
        
           |  |  | 483 |     protected function make_new_calculated_question_summary_stat($randomquestioncalculated, $subquestionstats) {
 | 
        
           |  |  | 484 |         $question = $randomquestioncalculated->question;
 | 
        
           |  |  | 485 |         $slot = $randomquestioncalculated->slot;
 | 
        
           |  |  | 486 |         $calculatedsummary = new calculated_question_summary($question, $slot, $subquestionstats);
 | 
        
           |  |  | 487 |   | 
        
           |  |  | 488 |         return $calculatedsummary;
 | 
        
           |  |  | 489 |     }
 | 
        
           |  |  | 490 | }
 |