Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * This file contains the code to analyse all the responses to a particular question.
19
 *
20
 * @package    core_question
21
 * @copyright  2014 Open University
22
 * @author     Jamie Pratt <me@jamiep.org>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace core_question\statistics\responses;
27
defined('MOODLE_INTERNAL') || die();
28
 
29
/**
30
 * This class can compute, store and cache the analysis of the responses to a particular question.
31
 *
32
 * @package    core_question
33
 * @copyright  2014 The Open University
34
 * @author     James Pratt me@jamiep.org
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class analyser {
38
    /**
39
     * @var int When analysing responses and breaking down the count of responses per try, how many columns should we break down
40
     * tries into? This is set to 5 columns, any response in a try more than try 5 will be counted in the fifth column.
41
     */
42
    const MAX_TRY_COUNTED = 5;
43
 
44
    /** @var object full question data from db. */
45
    protected $questiondata;
46
 
47
    /**
48
     * @var analysis_for_question|analysis_for_question_all_tries
49
     */
50
    public $analysis;
51
 
52
    /**
53
     * @var int used during calculations, so all results are stored with the same timestamp.
54
     */
55
    protected $calculationtime;
56
 
57
    /**
58
     * @var array Two index array first index is unique string for each sub question part, the second string index is the 'class'
59
     * that sub-question part can be classified into.
60
     *
61
     * This is the return value from {@link \question_type::get_possible_responses()} see that method for fuller documentation.
62
     */
63
    public $responseclasses = array();
64
 
65
    /**
66
     * @var bool whether to break down response analysis by variant. This only applies to questions that have variants and is
67
     *           used to suppress the break down of analysis by variant when there are going to be very many variants.
68
     */
69
    protected $breakdownbyvariant;
70
 
71
    /**
72
     * Create a new instance of this class for holding/computing the statistics
73
     * for a particular question.
74
     *
75
     * @param object $questiondata the full question data from the database defining this question.
76
     * @param string $whichtries   which tries to analyse.
77
     */
78
    public function __construct($questiondata, $whichtries = \question_attempt::LAST_TRY) {
79
        $this->questiondata = $questiondata;
80
        $qtypeobj = \question_bank::get_qtype($this->questiondata->qtype);
81
        if ($whichtries != \question_attempt::ALL_TRIES) {
82
            $this->analysis = new analysis_for_question($qtypeobj->get_possible_responses($this->questiondata));
83
        } else {
84
            $this->analysis = new analysis_for_question_all_tries($qtypeobj->get_possible_responses($this->questiondata));
85
        }
86
 
87
        $this->breakdownbyvariant = $qtypeobj->break_down_stats_and_response_analysis_by_variant($this->questiondata);
88
    }
89
 
90
    /**
91
     * Does the computed analysis have sub parts?
92
     *
93
     * @return bool whether this analysis has more than one subpart.
94
     */
95
    public function has_subparts() {
96
        return count($this->responseclasses) > 1;
97
    }
98
 
99
    /**
100
     * Does the computed analysis's sub parts have classes?
101
     *
102
     * @return bool whether this analysis has (a subpart with) more than one response class.
103
     */
104
    public function has_response_classes() {
105
        foreach ($this->responseclasses as $partclasses) {
106
            if (count($partclasses) > 1) {
107
                return true;
108
            }
109
        }
110
        return false;
111
    }
112
 
113
    /**
114
     * Analyse all the response data for all the specified attempts at this question.
115
     *
116
     * @param \qubaid_condition $qubaids which attempts to consider.
117
     * @param string $whichtries         which tries to analyse. Will be one of
118
     *                                   \question_attempt::FIRST_TRY, LAST_TRY or ALL_TRIES.
119
     * @return analysis_for_question
120
     */
121
    public function calculate($qubaids, $whichtries = \question_attempt::LAST_TRY) {
122
        $this->calculationtime = time();
123
        // Load data.
124
        $dm = new \question_engine_data_mapper();
125
        $questionattempts = $dm->load_attempts_at_question($this->questiondata->id, $qubaids);
126
 
127
        // Analyse it.
128
        foreach ($questionattempts as $qa) {
129
            $responseparts = $qa->classify_response($whichtries);
130
            if ($this->breakdownbyvariant) {
131
                $this->analysis->count_response_parts($qa->get_variant(), $responseparts);
132
            } else {
133
                $this->analysis->count_response_parts(1, $responseparts);
134
            }
135
 
136
        }
137
        $this->analysis->cache($qubaids, $whichtries, $this->questiondata->id, $this->calculationtime);
138
        return $this->analysis;
139
    }
140
 
141
    /**
142
     * Retrieve the computed response analysis from the question_response_analysis table.
143
     *
144
     * @param \qubaid_condition $qubaids    load the analysis of which question usages?
145
     * @param string            $whichtries load the analysis of which tries?
146
     * @return analysis_for_question|boolean analysis or false if no cached analysis found.
147
     */
148
    public function load_cached($qubaids, $whichtries) {
149
        global $DB;
150
 
151
        $timemodified = self::get_last_analysed_time($qubaids, $whichtries);
152
        // Variable name 'analyses' is the plural of 'analysis'.
153
        $responseanalyses = $DB->get_records('question_response_analysis',
154
                ['hashcode' => $qubaids->get_hash_code(), 'whichtries' => $whichtries,
155
                        'questionid' => $this->questiondata->id, 'timemodified' => $timemodified]);
156
        if (!$responseanalyses) {
157
            return false;
158
        }
159
 
160
        $analysisids = [];
161
        foreach ($responseanalyses as $responseanalysis) {
162
            $analysisforsubpart = $this->analysis->get_analysis_for_subpart($responseanalysis->variant, $responseanalysis->subqid);
163
            $class = $analysisforsubpart->get_response_class($responseanalysis->aid);
164
            $class->add_response($responseanalysis->response, $responseanalysis->credit);
165
            $analysisids[] = $responseanalysis->id;
166
        }
167
        [$sql, $params] = $DB->get_in_or_equal($analysisids);
168
        $counts = $DB->get_records_select('question_response_count', "analysisid {$sql}", $params);
169
        foreach ($counts as $count) {
170
            $responseanalysis = $responseanalyses[$count->analysisid];
171
            $analysisforsubpart = $this->analysis->get_analysis_for_subpart($responseanalysis->variant, $responseanalysis->subqid);
172
            $class = $analysisforsubpart->get_response_class($responseanalysis->aid);
173
            $class->set_response_count($responseanalysis->response, $count->try, $count->rcount);
174
 
175
        }
176
        return $this->analysis;
177
    }
178
 
179
 
180
    /**
181
     * Find time of non-expired analysis in the database.
182
     *
183
     * @param \qubaid_condition $qubaids    check for the analysis of which question usages?
184
     * @param string            $whichtries check for the analysis of which tries?
185
     * @return integer|boolean Time of cached record that matches this qubaid_condition or false if none found.
186
     */
187
    public function get_last_analysed_time($qubaids, $whichtries) {
188
        global $DB;
189
        return $DB->get_field('question_response_analysis', 'MAX(timemodified)',
190
                ['hashcode' => $qubaids->get_hash_code(), 'whichtries' => $whichtries,
191
                        'questionid' => $this->questiondata->id]);
192
    }
193
}