question_analyser.php 5.55 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
<?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/>.

/**
 * This class provides a mechanism for analysing the usage, performance, and efficacy
 * of a single question in an adaptive quiz.
 *
 * @copyright  2013 Middlebury College {@link http://www.middlebury.edu/}
 * @copyright  2022 onwards Vitaly Potenko <potenkov@gmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace mod_adaptivequiz\local\questionanalysis;

use context;
use InvalidArgumentException;
use mod_adaptivequiz\local\catalgo;
use mod_adaptivequiz\local\questionanalysis\statistics\question_statistic;
use mod_adaptivequiz\local\questionanalysis\statistics\question_statistic_result;
use question_definition;
use stdClass;

class question_analyser {

    /** @var context the context this usage belongs to. */
    protected $context;

    /** @var question_definition $definition The question  */
    protected $definition = null;

    /** @var float $level The question level */
    protected $level = null;

    /** @var float $lowestlevel The lowest question-level in the adaptive quiz */
    protected $lowestlevel = null;

    /** @var float $highestlevel The highest question-level in the adaptive quiz */
    protected $highestlevel = null;

    /** @var array $results An array of the re objects */
    protected $results = array();

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

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

    /**
     * Constructor - Create a new analyser.
     *
     * @param object $context
     * @param question_definition $definition
     * @param float $level The level (0-1) of the question.
     * @return void
     */
    public function __construct ($context, question_definition $definition, $level, $lowestlevel, $highestlevel) {
        $this->context = $context;
        $this->definition = $definition;
        $this->level = $level;
        $this->lowestlevel = $lowestlevel;
        $this->highestlevel = $highestlevel;
    }

    /**
     * Add an usage result for this question.
     *
     * @param attempt_score $score The user's score on this attempt.
     * @param boolean $correct True if the user answered correctly.
     * @param string $answer
     * @return void
     */
    public function add_result ($attemptid, $user, attempt_score $score, $correct, $answer) {
        $result = new stdClass();
        $result->attemptid = $attemptid;
        $result->user = $user;
        $result->score = $score;
        $result->correct = $correct;
        $result->answer = $answer;
        $this->results[] = $result;
    }

    /**
     * @return context the context this usage belongs to.
     */
    public function get_owning_context() {
        return $this->context;
    }

    /**
     * Answer the question definition for this question.
     *
     * @return question_definition
     */
    public function get_question_definition () {
        return $this->definition;
    }

    /**
     * Answer the question level for this question.
     *
     * @return int
     */
    public function get_question_level () {
        return $this->level;
    }

    /**
     * Answer the question level for this question in logits.
     *
     * @return int
     */
    public function get_question_level_in_logits () {
        return catalgo::convert_linear_to_logit($this->level, $this->lowestlevel, $this->highestlevel);
    }

    /**
     * Answer the results for this question
     *
     * @return array An array of stdClass objects.
     */
    public function get_results () {
        return $this->results;
    }

    /**
     * Add and calculate a statistic.
     *
     * @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;
        $this->statisticresults[$key] = $statistic->calculate($this);
    }

    /**
     * Answer a statistic result.
     *
     * @param string $key A key to identify this statistic.
     * @return question_statistic_result
     */
    public function get_statistic_result ($key) {
        if (empty($this->statisticresults[$key])) {
            throw new InvalidArgumentException("Unknown statistic key '$key'.");
        }
        return $this->statisticresults[$key];
    }

    /**
     * Utility function to map a logit value to this question's scale
     *
     * @param $logit
     * @return float Scaled value
     */
    public function map_logit_to_scale ($logit) {
        return catalgo::map_logit_to_scale($logit, $this->highestlevel, $this->lowestlevel);
    }
}