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 * @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
22 */
23
24namespace core_question\statistics\responses;
25
26/**
27 * The leafs of the analysis data structure.
28 *
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}.
39 *
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
44 */
45class analysis_for_actual_response {
46    /**
47     * @var int[] count per try for this response.
48     */
49    protected $trycount = array();
50
51    /**
52     * @var int total count of tries with this response.
53     */
54    protected $totalcount = 0;
55
56    /**
57     * @var float grade for this response, normally between 0 and 1.
58     */
59    protected $fraction;
60
61    /**
62     * @var string the response as it will be displayed in report.
63     */
64    protected $response;
65
66    /**
67     * @param string $response
68     * @param float  $fraction
69     */
70    public function __construct($response, $fraction) {
71        $this->response = $response;
72        $this->fraction = $fraction;
73    }
74
75    /**
76     * Used to count the occurrences of response sub parts.
77     *
78     * @param int $try the try number, or 0 if only keeping one count, not a count for each try.
79     */
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;
85            }
86            if (!isset($this->trycount[$try])) {
87                $this->trycount[$try] = 0;
88            }
89            $this->trycount[$try]++;
90        }
91
92    }
93
94    /**
95     * Used to set the count of occurrences of response sub parts, when loading count from cache.
96     *
97     * @param int $try the try number, or 0 if only keeping one count, not a count for each try.
98     * @param int $count
99     */
100    public function set_count($try, $count) {
101        $this->totalcount = $this->totalcount + $count;
102        $this->trycount[$try] = $count;
103    }
104
105    /**
106     * Cache analysis for class.
107     *
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     */
115    public function cache($qubaids, $whichtries, $questionid, $variantno, $subpartid, $responseclassid) {
116        global $DB;
117        $row = new \stdClass();
118        $row->hashcode = $qubaids->get_hash_code();
119        $row->whichtries = $whichtries;
120        $row->questionid = $questionid;
121        $row->variant = $variantno;
122        $row->subqid = $subpartid;
123        if ($responseclassid === '') {
124            $row->aid = null;
125        } else {
126            $row->aid = $responseclassid;
127        }
128        $row->response = $this->response;
129        $row->credit = $this->fraction;
130        $row->timemodified = time();
131        $analysisid = $DB->insert_record('question_response_analysis', $row);
132        if ($whichtries === \question_attempt::ALL_TRIES) {
133            foreach ($this->trycount as $try => $count) {
134                $countrow = new \stdClass();
135                $countrow->try = $try;
136                $countrow->rcount = $count;
137                $countrow->analysisid = $analysisid;
138                $DB->insert_record('question_response_count', $countrow, false);
139            }
140        } else {
141            $countrow = new \stdClass();
142            $countrow->try = 0;
143            $countrow->rcount = $this->totalcount;
144            $countrow->analysisid = $analysisid;
145            $DB->insert_record('question_response_count', $countrow, false);
146        }
147    }
148
149    /**
150     * Returns an object with a property for each column of the question response analysis table.
151     *
152     * @param string $partid
153     * @param string $modelresponse
154     * @return object
155     */
156    public function data_for_question_response_table($partid, $modelresponse) {
157        $rowdata = new \stdClass();
158        $rowdata->part = $partid;
159        $rowdata->responseclass = $modelresponse;
160        $rowdata->response = $this->response;
161        $rowdata->fraction = $this->fraction;
162        $rowdata->totalcount = $this->totalcount;
163        $rowdata->trycount = $this->trycount;
164        return $rowdata;
165    }
166
167    /**
168     * What is the highest try number that this response has been seen?
169     *
170     * @return int try number
171     */
172    public function get_maximum_tries() {
173        return max(array_keys($this->trycount));
174    }
175}
176