MDL-75576 quiz/question statistics: don't expire by time
[moodle.git] / question / classes / statistics / responses / analysis_for_class.php
blobc658c3cdf80b9f0cb762e41b91deb76162eb98d8
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;
28 /**
29 * Counts a class of responses for this sub part of the question.
31 * No response is one possible class of response to a question.
33 * - There is a separate data structure for each question or sub question's analysis
34 * {@link \core_question\statistics\responses\analysis_for_question}
35 * or {@link \core_question\statistics\responses\analysis_for_question_all_tries}.
36 * - There are separate analysis for each variant in this top level instance.
37 * - Then there are class instances representing the analysis of each of the sub parts of each variant of the question.
38 * {@link \core_question\statistics\responses\analysis_for_subpart}.
39 * - Then within the sub part analysis there are response class analysis
40 * {@link \core_question\statistics\responses\analysis_for_class}.
41 * - Then within each class analysis there are analysis for each actual response
42 * {@link \core_question\statistics\responses\analysis_for_actual_response}.
44 * @package core_question
45 * @copyright 2014 The Open University
46 * @author James Pratt me@jamiep.org
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49 class analysis_for_class {
51 /**
52 * @var string must be unique for each response class within this sub part.
54 protected $responseclassid;
56 /**
57 * @var string represent this class in the response analysis table.
59 protected $modelresponse;
61 /** @var string the (partial) credit awarded for this responses. */
62 protected $fraction;
64 /** @var analysis_for_actual_response[] key is the actual response represented as a string as it will be displayed in report.
66 protected $actualresponses = array();
68 /**
69 * Constructor, just an easy way to set the fields.
71 * @param \question_possible_response $possibleresponse
72 * @param string $responseclassid
74 public function __construct($possibleresponse, $responseclassid) {
75 $this->modelresponse = $possibleresponse->responseclass;
76 $this->fraction = $possibleresponse->fraction;
77 $this->responseclassid = $responseclassid;
80 /**
81 * Keep a count of a response to this question sub part that falls within this class.
83 * @param string $actualresponse
84 * @param float|null $fraction
85 * @param int $try
86 * @return \core_question\statistics\responses\analysis_for_actual_response
88 public function count_response($actualresponse, $fraction, $try) {
89 if (!isset($this->actualresponses[$actualresponse])) {
90 if ($fraction === null) {
91 $fraction = $this->fraction;
93 $this->add_response($actualresponse, $fraction);
95 $this->get_response($actualresponse)->increment_count($try);
98 /**
99 * Cache analysis for class.
101 * @param \qubaid_condition $qubaids which question usages have been analysed.
102 * @param string $whichtries which tries have been analysed?
103 * @param int $questionid which question.
104 * @param int $variantno which variant.
105 * @param string $subpartid which sub part.
106 * @param int|null $calculationtime time when the analysis was done. (Defaults to time()).
108 public function cache($qubaids, $whichtries, $questionid, $variantno, $subpartid, $calculationtime = null) {
109 foreach ($this->get_responses() as $response) {
110 $analysisforactualresponse = $this->get_response($response);
111 $analysisforactualresponse->cache($qubaids, $whichtries, $questionid, $variantno, $subpartid,
112 $this->responseclassid, $calculationtime);
117 * Add an actual response to the data structure.
119 * @param string $response A string representing the actual response.
120 * @param float $fraction The fraction of grade awarded for this response.
122 public function add_response($response, $fraction) {
123 $this->actualresponses[$response] = new analysis_for_actual_response($response, $fraction);
127 * Used when loading cached counts.
129 * @param string $response
130 * @param int $try the try number, will be zero if not keeping track of try.
131 * @param int $count the count
133 public function set_response_count($response, $try, $count) {
134 $this->actualresponses[$response]->set_count($try, $count);
138 * Are there actual responses to sub parts that where classified into this class?
140 * @return bool whether this analysis has a response class with more than one
141 * different actual response, or if the actual response is different from
142 * the model response.
144 public function has_actual_responses() {
145 $actualresponses = $this->get_responses();
146 if (count($actualresponses) > 1) {
147 return true;
148 } else if (count($actualresponses) === 1) {
149 $singleactualresponse = reset($actualresponses);
150 return (string)$singleactualresponse !== (string)$this->modelresponse;
152 return false;
156 * Return the data to display in the response analysis table.
158 * @param bool $responseclasscolumn
159 * @param string $partid
160 * @return object[]
162 public function data_for_question_response_table($responseclasscolumn, $partid) {
163 $return = array();
164 if (count($this->get_responses()) == 0) {
165 $rowdata = new \stdClass();
166 $rowdata->part = $partid;
167 $rowdata->responseclass = $this->modelresponse;
168 if (!$responseclasscolumn) {
169 $rowdata->response = $this->modelresponse;
170 } else {
171 $rowdata->response = '';
173 $rowdata->fraction = $this->fraction;
174 $rowdata->totalcount = 0;
175 $rowdata->trycount = array();
176 $return[] = $rowdata;
177 } else {
178 foreach ($this->get_responses() as $actualresponse) {
179 $response = $this->get_response($actualresponse);
180 $return[] = $response->data_for_question_response_table($partid, $this->modelresponse);
183 return $return;
187 * What is the highest try number that an actual response of this response class has been seen?
189 * @return int try number
191 public function get_maximum_tries() {
192 $max = 1;
193 foreach ($this->get_responses() as $actualresponse) {
194 $max = max($max, $this->get_response($actualresponse)->get_maximum_tries());
196 return $max;
200 * Return array of the actual responses to this sub part that were classified into this class.
202 * @return string[] the actual responses we are counting tries at.
204 protected function get_responses() {
205 return array_keys($this->actualresponses);
209 * Get the data structure used to count the responses that match an actual response within this class of responses.
211 * @param string $response
212 * @return analysis_for_actual_response the instance for keeping count of tries for $response.
214 protected function get_response($response) {
215 return $this->actualresponses[$response];