MDL-45660 question statistics: make comparison more robust.
[moodle.git] / question / classes / statistics / responses / analysis_for_class.php
CommitLineData
d50b05e6
JP
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/**
472c06f8 18 * @package core_question
d50b05e6
JP
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
22 */
23
24namespace core_question\statistics\responses;
25
26
27
28/**
038014c4 29 * Counts a class of responses for this sub part of the question.
d50b05e6
JP
30 *
31 * No response is one possible class of response to a question.
32 *
038014c4
JP
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}.
43 *
44 * @package core_question
45 * @copyright 2014 The Open University
46 * @author James Pratt me@jamiep.org
d50b05e6
JP
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 */
49class analysis_for_class {
50
51 /**
038014c4 52 * @var string must be unique for each response class within this sub part.
d50b05e6
JP
53 */
54 protected $responseclassid;
55
56 /**
038014c4 57 * @var string represent this class in the response analysis table.
d50b05e6
JP
58 */
59 protected $modelresponse;
60
61 /** @var string the (partial) credit awarded for this responses. */
62 protected $fraction;
63
038014c4 64 /** @var analysis_for_actual_response[] key is the actual response represented as a string as it will be displayed in report.
d50b05e6
JP
65 */
66 protected $actualresponses = array();
67
68 /**
69 * Constructor, just an easy way to set the fields.
038014c4 70 *
d50b05e6
JP
71 * @param \question_possible_response $possibleresponse
72 * @param string $responseclassid
73 */
74 public function __construct($possibleresponse, $responseclassid) {
75 $this->modelresponse = $possibleresponse->responseclass;
76 $this->fraction = $possibleresponse->fraction;
77 $this->responseclassid = $responseclassid;
78 }
79
80 /**
038014c4
JP
81 * Keep a count of a response to this question sub part that falls within this class.
82 *
83 * @param string $actualresponse
d50b05e6 84 * @param float|null $fraction
038014c4
JP
85 * @param int $try
86 * @return \core_question\statistics\responses\analysis_for_actual_response
d50b05e6 87 */
038014c4 88 public function count_response($actualresponse, $fraction, $try) {
d50b05e6
JP
89 if (!isset($this->actualresponses[$actualresponse])) {
90 if ($fraction === null) {
91 $fraction = $this->fraction;
92 }
038014c4 93 $this->add_response($actualresponse, $fraction);
d50b05e6 94 }
038014c4 95 $this->get_response($actualresponse)->increment_count($try);
d50b05e6
JP
96 }
97
98 /**
038014c4
JP
99 * Cache analysis for class.
100 *
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.
d50b05e6 106 */
038014c4
JP
107 public function cache($qubaids, $whichtries, $questionid, $variantno, $subpartid) {
108 foreach ($this->get_responses() as $response) {
109 $analysisforactualresponse = $this->get_response($response);
110 $analysisforactualresponse->cache($qubaids, $whichtries, $questionid, $variantno, $subpartid, $this->responseclassid);
d50b05e6
JP
111 }
112 }
113
038014c4
JP
114 /**
115 * Add an actual response to the data structure.
116 *
117 * @param string $response A string representing the actual response.
118 * @param float $fraction The fraction of grade awarded for this response.
119 */
120 public function add_response($response, $fraction) {
121 $this->actualresponses[$response] = new analysis_for_actual_response($response, $fraction);
d50b05e6
JP
122 }
123
124 /**
038014c4
JP
125 * Used when loading cached counts.
126 *
127 * @param string $response
128 * @param int $try the try number, will be zero if not keeping track of try.
129 * @param int $count the count
130 */
131 public function set_response_count($response, $try, $count) {
132 $this->actualresponses[$response]->set_count($try, $count);
133 }
134
135 /**
136 * Are there actual responses to sub parts that where classified into this class?
137 *
d50b05e6
JP
138 * @return bool whether this analysis has a response class with more than one
139 * different actual response, or if the actual response is different from
140 * the model response.
141 */
142 public function has_actual_responses() {
038014c4
JP
143 $actualresponses = $this->get_responses();
144 if (count($actualresponses) > 1) {
d50b05e6 145 return true;
038014c4
JP
146 } else if (count($actualresponses) === 1) {
147 $singleactualresponse = reset($actualresponses);
8b431dca 148 return (string)$singleactualresponse !== (string)$this->modelresponse;
d50b05e6
JP
149 }
150 return false;
151 }
152
153 /**
038014c4
JP
154 * Return the data to display in the response analysis table.
155 *
156 * @param bool $responseclasscolumn
157 * @param string $partid
d50b05e6
JP
158 * @return object[]
159 */
160 public function data_for_question_response_table($responseclasscolumn, $partid) {
161 $return = array();
038014c4 162 if (count($this->get_responses()) == 0) {
d50b05e6
JP
163 $rowdata = new \stdClass();
164 $rowdata->part = $partid;
165 $rowdata->responseclass = $this->modelresponse;
166 if (!$responseclasscolumn) {
167 $rowdata->response = $this->modelresponse;
168 } else {
169 $rowdata->response = '';
170 }
171 $rowdata->fraction = $this->fraction;
038014c4
JP
172 $rowdata->totalcount = 0;
173 $rowdata->trycount = array();
d50b05e6
JP
174 $return[] = $rowdata;
175 } else {
038014c4
JP
176 foreach ($this->get_responses() as $actualresponse) {
177 $response = $this->get_response($actualresponse);
178 $return[] = $response->data_for_question_response_table($partid, $this->modelresponse);
d50b05e6
JP
179 }
180 }
181 return $return;
182 }
038014c4
JP
183
184 /**
185 * What is the highest try number that an actual response of this response class has been seen?
186 *
187 * @return int try number
188 */
189 public function get_maximum_tries() {
190 $max = 1;
191 foreach ($this->get_responses() as $actualresponse) {
192 $max = max($max, $this->get_response($actualresponse)->get_maximum_tries());
193 }
194 return $max;
195 }
196
197 /**
198 * Return array of the actual responses to this sub part that were classified into this class.
199 *
200 * @return string[] the actual responses we are counting tries at.
201 */
202 protected function get_responses() {
203 return array_keys($this->actualresponses);
204 }
205
206 /**
207 * Get the data structure used to count the responses that match an actual response within this class of responses.
208 *
209 * @param string $response
210 * @return analysis_for_actual_response the instance for keeping count of tries for $response.
211 */
212 protected function get_response($response) {
213 return $this->actualresponses[$response];
214 }
d50b05e6 215}