MDL-75576 quiz/question statistics: don't expire by time
[moodle.git] / question / classes / statistics / responses / analysis_for_actual_response.php
blobfd8b2be6b3d36e58991aba1e15cc907d7f3be37b
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 * @package core_question
19 * @copyright 2013 The Open University
20 * @author James Pratt me@jamiep.org
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core_question\statistics\responses;
26 /**
27 * The leafs of the analysis data structure.
29 * - There is a separate data structure for each question or sub question's analysis
30 * {@link \core_question\statistics\responses\analysis_for_question}
31 * or {@link \core_question\statistics\responses\analysis_for_question_all_tries}.
32 * - There are separate analysis for each variant in this top level instance.
33 * - Then there are class instances representing the analysis of each of the sub parts of each variant of the question.
34 * {@link \core_question\statistics\responses\analysis_for_subpart}.
35 * - Then within the sub part analysis there are response class analysis
36 * {@link \core_question\statistics\responses\analysis_for_class}.
37 * - Then within each class analysis there are analysis for each actual response
38 * {@link \core_question\statistics\responses\analysis_for_actual_response}.
40 * @package core_question
41 * @copyright 2014 The Open University
42 * @author James Pratt me@jamiep.org
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class analysis_for_actual_response {
46 /**
47 * @var int[] count per try for this response.
49 protected $trycount = array();
51 /**
52 * @var int total count of tries with this response.
54 protected $totalcount = 0;
56 /**
57 * @var float grade for this response, normally between 0 and 1.
59 protected $fraction;
61 /**
62 * @var string the response as it will be displayed in report.
64 protected $response;
66 /**
67 * @param string $response
68 * @param float $fraction
70 public function __construct($response, $fraction) {
71 $this->response = $response;
72 $this->fraction = $fraction;
75 /**
76 * Used to count the occurrences of response sub parts.
78 * @param int $try the try number, or 0 if only keeping one count, not a count for each try.
80 public function increment_count($try = 0) {
81 $this->totalcount++;
82 if ($try != 0) {
83 if ($try > analyser::MAX_TRY_COUNTED) {
84 $try = analyser::MAX_TRY_COUNTED;
86 if (!isset($this->trycount[$try])) {
87 $this->trycount[$try] = 0;
89 $this->trycount[$try]++;
94 /**
95 * Used to set the count of occurrences of response sub parts, when loading count from cache.
97 * @param int $try the try number, or 0 if only keeping one count, not a count for each try.
98 * @param int $count
100 public function set_count($try, $count) {
101 $this->totalcount = $this->totalcount + $count;
102 $this->trycount[$try] = $count;
106 * Cache analysis for class.
108 * @param \qubaid_condition $qubaids which question usages have been analysed.
109 * @param string $whichtries which tries have been analysed?
110 * @param int $questionid which question.
111 * @param int $variantno which variant.
112 * @param string $subpartid which sub part is this actual response in?
113 * @param string $responseclassid which response class is this actual response in?
114 * @param int|null $calculationtime time when the analysis was done. (Defaults to time()).
116 public function cache($qubaids, $whichtries, $questionid, $variantno, $subpartid, $responseclassid, $calculationtime = null) {
117 global $DB;
118 $row = new \stdClass();
119 $row->hashcode = $qubaids->get_hash_code();
120 $row->whichtries = $whichtries;
121 $row->questionid = $questionid;
122 $row->variant = $variantno;
123 $row->subqid = $subpartid;
124 if ($responseclassid === '') {
125 $row->aid = null;
126 } else {
127 $row->aid = $responseclassid;
129 $row->response = $this->response;
130 $row->credit = $this->fraction;
131 $row->timemodified = $calculationtime ?? time();
132 $analysisid = $DB->insert_record('question_response_analysis', $row);
133 if ($whichtries === \question_attempt::ALL_TRIES) {
134 foreach ($this->trycount as $try => $count) {
135 $countrow = new \stdClass();
136 $countrow->try = $try;
137 $countrow->rcount = $count;
138 $countrow->analysisid = $analysisid;
139 $DB->insert_record('question_response_count', $countrow, false);
141 } else {
142 $countrow = new \stdClass();
143 $countrow->try = 0;
144 $countrow->rcount = $this->totalcount;
145 $countrow->analysisid = $analysisid;
146 $DB->insert_record('question_response_count', $countrow, false);
151 * Returns an object with a property for each column of the question response analysis table.
153 * @param string $partid
154 * @param string $modelresponse
155 * @return object
157 public function data_for_question_response_table($partid, $modelresponse) {
158 $rowdata = new \stdClass();
159 $rowdata->part = $partid;
160 $rowdata->responseclass = $modelresponse;
161 $rowdata->response = $this->response;
162 $rowdata->fraction = $this->fraction;
163 $rowdata->totalcount = $this->totalcount;
164 $rowdata->trycount = $this->trycount;
165 return $rowdata;
169 * What is the highest try number that this response has been seen?
171 * @return int try number
173 public function get_maximum_tries() {
174 return max(array_keys($this->trycount));