2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This file contains the code to analyse all the responses to a particular
21 * @package core_question
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
27 namespace core_question\statistics\responses
;
28 defined('MOODLE_INTERNAL') ||
die();
31 * Analysis for possible responses for parts of a question. It is up to a question type designer to decide on how many parts their
32 * question has. A sub part might represent a sub question embedded in the question for example in a matching question there are
33 * several sub parts. A numeric question with a unit might be divided into two sub parts for the purposes of response analysis
34 * or the question type designer might decide to treat the answer, both the numeric and unit part,
35 * as a whole for the purposes of response analysis.
37 * Responses can be further divided into 'classes' in which they are classified. One or more of these 'classes' are contained in
40 * @copyright 2013 Open University
41 * @author Jamie Pratt <me@jamiep.org>
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class analysis_for_question
{
47 * Takes either a two index array as a parameter with keys subpartid and classid and values possible_response.
48 * Or takes an array of {@link responses_for_classes} objects.
50 * @param $subparts[string]array[]\question_possible_response $array
52 public function __construct(array $subparts = null) {
53 if (!is_null($subparts)) {
54 foreach ($subparts as $subpartid => $classes) {
55 $this->subparts
[$subpartid] = new analysis_for_subpart($classes);
61 * @var analysis_for_subpart[]
66 * Unique ids for sub parts.
70 public function get_subpart_ids() {
71 return array_keys($this->subparts
);
75 * @param string $subpartid id for sub part.
76 * @return analysis_for_subpart
78 public function get_subpart($subpartid) {
79 return $this->subparts
[$subpartid];
83 * Used to work out what kind of table is needed to display stats.
85 * @return bool whether this question has (a subpart with) more than one response class.
87 public function has_multiple_response_classes() {
88 foreach ($this->subparts
as $subpart) {
89 if ($subpart->has_multiple_response_classes()) {
97 * Used to work out what kind of table is needed to display stats.
99 * @return bool whether this analysis has more than one subpart.
101 public function has_subparts() {
102 return count($this->subparts
) > 1;
106 * Takes an array of {@link \question_classified_response} and adds counts of the responses to the sub parts and classes.
108 * @var \question_classified_response[] $responseparts keys are sub-part id.
110 public function count_response_parts($responseparts) {
111 foreach ($responseparts as $subpartid => $responsepart) {
112 $this->get_subpart($subpartid)->count_response($responsepart);
117 * @param \qubaid_condition $qubaids
118 * @param int $questionid the question id
120 public function cache($qubaids, $questionid) {
121 foreach ($this->subparts
as $subpartid => $subpart) {
122 $subpart->cache($qubaids, $questionid, $subpartid);
127 * @return bool whether this analysis has a response class with more than one
128 * different actual response, or if the actual response is different from
129 * the model response.
131 public function has_actual_responses() {
132 foreach ($this->subparts
as $subpartid => $subpart) {
133 if ($subpart->has_actual_responses()) {