quiz_analyser.php 8.24 KB
Newer Older
0815-xyz's avatar
0815-xyz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

namespace mod_adaptivequiz\local\questionanalysis;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot.'/tag/lib.php');

use core_tag_tag;
use Exception;
use InvalidArgumentException;
use mod_adaptivequiz\local\attempt\attempt_state;
use mod_adaptivequiz\local\questionanalysis\statistics\question_statistic;
use question_engine;
use stdClass;

/**
 * Questions-analyser class.
 *
 * The class provides a mechanism for loading and analysing question usage, performance, and efficacy.
 *
 * @package    mod_adaptivequiz
 * @copyright  2013 Remote-Learner {@link http://www.remote-learner.ca/}
 * @copyright  2022 onwards Vitaly Potenko <potenkov@gmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class quiz_analyser {

    /** @var array $questions An array of all questions loaded and their stats */
    protected $questions = array();

    /** @var array $statistics An array of the statistics added to this report */
    protected $statistics = array();

    /**
     * Constructor - Create a new analyser.
     *
     * @return void
     */
    public function __construct() {

    }

    /**
     * Load attempts from an adaptive quiz instance.
     *
     * @param int $instance
     */
    public function load_attempts(int $instance): void {
        global $DB;

        $adaptivequiz  = $DB->get_record('adaptivequiz', ['id' => $instance], '*');

        // Get all of the completed attempts for this adaptive quiz instance.
        $attempts  = $DB->get_records('adaptivequiz_attempt',
            ['instance' => $instance, 'attemptstate' => attempt_state::COMPLETED]);

        foreach ($attempts as $attempt) {
            if ($attempt->uniqueid == 0) {
                continue;
            }

            $user = $DB->get_record('user', ['id' => $attempt->userid]);
            if (!$user) {
                $user = new stdClass();
                $user->firstname = get_string('unknownuser', 'adaptivequiz');
                $user->lastname = '#' . $attempt->userid;
            }

            // For each attempt, get the attempt's final score.
            $score = new attempt_score($attempt->measure, $attempt->standarderror, $adaptivequiz->lowestlevel,
                $adaptivequiz->highestlevel);

            // For each attempt, loop through all questions asked and add that usage
            // to the question.
            $quba = question_engine::load_questions_usage_by_activity($attempt->uniqueid);
            foreach ($quba->get_slots() as $i => $slot) {
                $question = $quba->get_question($slot);

                // Create a question-analyser for the question.
                if (empty($this->questions[$question->id])) {
                    $tags = core_tag_tag::get_item_tags_array('core_question', 'question', $question->id);
                    $difficulty = adaptivequiz_get_difficulty_from_tags($tags);
                    $this->questions[$question->id] = new question_analyser($quba->get_owning_context(), $question,
                        $difficulty, $adaptivequiz->lowestlevel, $adaptivequiz->highestlevel);
                }

                // Record the attempt score and the individual question result.
                // KNIGHT: Questions are considered correct if the marked fraction is at least at the acceptance threshold determined by the plugin settings
                $correct = ($quba->get_question_fraction($slot) >= $adaptivequiz->acceptancethreshold);
                $answer = $quba->get_response_summary($slot);
                $this->questions[$question->id]->add_result($attempt->id, $user, $score, $correct, $answer);
            }
        }
    }

    /**
     * Add a statistic to calculate.
     *
     * @param string $key A key to identify this statistic for sorting and printing.
     * @param question_statistic $statistic
     * @return void
     */
    public function add_statistic($key, question_statistic $statistic) {
        if (!empty($this->statistics[$key])) {
            throw new InvalidArgumentException("Statistic key '$key' is already in use.");
        }
        $this->statistics[$key] = $statistic;
        foreach ($this->questions as $question) {
            $question->add_statistic($key, $statistic);
        }
    }

    /**
     * Answer a header row.
     *
     * @return array
     */
    public function get_header() {
        $header = array();
        $header['id'] = get_string('id', 'adaptivequiz');
        $header['name'] = get_string('adaptivequizname', 'adaptivequiz');
        $header['level'] = get_string('attemptquestion_level', 'adaptivequiz');
        foreach ($this->statistics as $key => $statistic) {
            $header[$key] = $statistic->get_display_name();
        }
        return $header;
    }

    /**
     * Return an array of table records, sorted by the statistics given.
     *
     * @param string $sort Which statistic to sort on.
     * @param string $direction ASC or DESC.
     * @return array
     */
    public function get_records($sort = null, $direction = 'ASC') {
        if (empty($this->questions)) {
            return [];
        }

        $records = [];
        foreach ($this->questions as $question) {
            $record = [];
            $record[] = $question->get_question_definition()->id;
            $record[] = $question->get_question_definition()->name;
            $record[] = $question->get_question_level();
            foreach ($this->statistics as $key => $statistic) {
                $record[] = $question->get_statistic_result($key)->printable();
            }
            $records[] = $record;
        }

        if ($direction != 'ASC' && $direction != 'DESC') {
            throw new InvalidArgumentException('Invalid sort direction. Must be SORT_ASC or SORT_DESC, \''.$direction.'\' given.');
        }
        if ($direction == 'DESC') {
            $direction = SORT_DESC;
        } else {
            $direction = SORT_ASC;
        }

        if (!is_null($sort)) {
            $sortkeys = [];
            foreach ($this->questions as $question) {
                if ($sort == 'name') {
                    $sortkeys[] = $question->get_question_definition()->name;
                    $sorttype = SORT_REGULAR;
                } else if ($sort == 'level') {
                    $sortkeys[] = $question->get_question_level();
                    $sorttype = SORT_NUMERIC;
                } else {
                    $sortkeys[] = $question->get_statistic_result($sort)->sortable();
                    $sorttype = SORT_NUMERIC;
                }
            }
            array_multisort($sortkeys, $direction, $sorttype, $records);
        }

        return $records;
    }

    /**
     * Answer a question-analyzer for a particular question id analyze
     *
     * @param int $qid The question id
     * @return question_analyser
     * @throws Exception
     */
    public function get_question_analyzer($qid) {
        if (!isset($this->questions[$qid])) {
            throw new Exception('Question-id not found.');
        }
        return $this->questions[$qid];
    }

    /**
     * Answer the record for a single question
     *
     * @param int $qid The question id
     * @return array
     * @throws Exception
     */
    public function get_record($qid) {
        $question = $this->get_question_analyzer($qid);
        $record = [];
        $record[] = $question->get_question_definition()->id;
        $record[] = $question->get_question_definition()->name;
        $record[] = $question->get_question_level();
        foreach ($this->statistics as $key => $statistic) {
            $record[] = $question->get_statistic_result($key)->printable();
        }
        return $record;
    }
}