| Línea 14... |
Línea 14... |
| 14 |
// You should have received a copy of the GNU General Public License
|
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/>.
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
| Línea 16... |
Línea 16... |
| 16 |
|
16 |
|
| Línea 17... |
Línea -... |
| 17 |
namespace qbank_statistics;
|
- |
|
| 18 |
|
- |
|
| 19 |
use core_question\statistics\questions\all_calculated_for_qubaid_condition;
|
- |
|
| 20 |
use core_component;
|
17 |
namespace qbank_statistics;
|
| 21 |
|
18 |
|
| 22 |
/**
|
19 |
/**
|
| 23 |
* Helper for statistics
|
20 |
* Helper for statistics
|
| 24 |
*
|
21 |
*
|
| Línea 38... |
Línea 35... |
| 38 |
* @var float Threshold to determine 'Needs checking?'
|
35 |
* @var float Threshold to determine 'Needs checking?'
|
| 39 |
*/
|
36 |
*/
|
| 40 |
private const NEED_FOR_REVISION_UPPER_THRESHOLD = 50;
|
37 |
private const NEED_FOR_REVISION_UPPER_THRESHOLD = 50;
|
| Línea 41... |
Línea 38... |
| 41 |
|
38 |
|
| 42 |
/**
|
- |
|
| 43 |
* For a list of questions find all the places, defined by (component, contextid) where there are attempts.
|
- |
|
| 44 |
*
|
- |
|
| 45 |
* @param int[] $questionids array of question ids that we are interested in.
|
- |
|
| 46 |
* @return \stdClass[] list of objects with fields ->component and ->contextid.
|
- |
|
| 47 |
* @deprecated since Moodle 4.3 please use the method from statistics_bulk_loader.
|
- |
|
| 48 |
* @todo MDL-78090 Final deprecation in Moodle 4.7
|
- |
|
| 49 |
*/
|
- |
|
| 50 |
private static function get_all_places_where_questions_were_attempted(array $questionids): array {
|
- |
|
| 51 |
global $DB;
|
- |
|
| 52 |
|
- |
|
| 53 |
[$questionidcondition, $params] = $DB->get_in_or_equal($questionids);
|
- |
|
| 54 |
// The MIN(qu.id) is just to ensure that the rows have a unique key.
|
- |
|
| 55 |
$places = $DB->get_records_sql("
|
- |
|
| 56 |
SELECT MIN(qu.id) AS somethingunique, qu.component, qu.contextid, " .
|
- |
|
| 57 |
\context_helper::get_preload_record_columns_sql('ctx') . "
|
- |
|
| 58 |
FROM {question_usages} qu
|
- |
|
| 59 |
JOIN {question_attempts} qa ON qa.questionusageid = qu.id
|
- |
|
| 60 |
JOIN {context} ctx ON ctx.id = qu.contextid
|
- |
|
| 61 |
WHERE qa.questionid $questionidcondition
|
- |
|
| 62 |
GROUP BY qu.component, qu.contextid, " .
|
- |
|
| 63 |
implode(', ', array_keys(\context_helper::get_preload_record_columns('ctx'))) . "
|
- |
|
| 64 |
ORDER BY qu.contextid ASC
|
- |
|
| 65 |
", $params);
|
- |
|
| 66 |
|
- |
|
| 67 |
// Strip out the unwanted ids.
|
- |
|
| 68 |
$places = array_values($places);
|
- |
|
| 69 |
foreach ($places as $place) {
|
- |
|
| 70 |
unset($place->somethingunique);
|
- |
|
| 71 |
\context_helper::preload_from_record($place);
|
- |
|
| 72 |
}
|
- |
|
| 73 |
|
- |
|
| 74 |
return $places;
|
- |
|
| 75 |
}
|
- |
|
| 76 |
|
- |
|
| 77 |
/**
|
- |
|
| 78 |
* Load the question statistics for all the attempts belonging to a particular component in a particular context.
|
- |
|
| 79 |
*
|
- |
|
| 80 |
* @param string $component frankenstyle component name, e.g. 'mod_quiz'.
|
- |
|
| 81 |
* @param \context $context the context to load the statistics for.
|
- |
|
| 82 |
* @return all_calculated_for_qubaid_condition|null question statistics.
|
39 |
/**
|
| 83 |
* @deprecated since Moodle 4.3 please use the method from statistics_bulk_loader.
|
- |
|
| 84 |
* @todo MDL-78090 Final deprecation in Moodle 4.7
|
- |
|
| 85 |
*/
|
- |
|
| 86 |
private static function load_statistics_for_place(string $component, \context $context): ?all_calculated_for_qubaid_condition {
|
- |
|
| 87 |
// This check is basically if (component_exists).
|
- |
|
| 88 |
if (empty(core_component::get_component_directory($component))) {
|
- |
|
| 89 |
return null;
|
- |
|
| 90 |
}
|
- |
|
| 91 |
|
- |
|
| 92 |
if (!component_callback_exists($component, 'calculate_question_stats')) {
|
- |
|
| 93 |
return null;
|
- |
|
| 94 |
}
|
- |
|
| 95 |
|
- |
|
| 96 |
return component_callback($component, 'calculate_question_stats', [$context]);
|
- |
|
| 97 |
}
|
- |
|
| 98 |
|
- |
|
| 99 |
/**
|
- |
|
| 100 |
* Extract the value for one question and one type of statistic from a set of statistics.
|
- |
|
| 101 |
*
|
- |
|
| 102 |
* @param all_calculated_for_qubaid_condition $statistics the batch of statistics.
|
- |
|
| 103 |
* @param int $questionid a question id.
|
- |
|
| 104 |
* @param string $item one of the field names in all_calculated_for_qubaid_condition, e.g. 'facility'.
|
- |
|
| 105 |
* @return float|null the required value.
|
- |
|
| 106 |
* @deprecated since Moodle 4.3 please use the method from statistics_bulk_loader.
|
- |
|
| 107 |
* @todo MDL-78090 Final deprecation in Moodle 4.7
|
- |
|
| 108 |
*/
|
- |
|
| 109 |
private static function extract_item_value(all_calculated_for_qubaid_condition $statistics,
|
- |
|
| 110 |
int $questionid, string $item): ?float {
|
- |
|
| 111 |
|
- |
|
| 112 |
// Look in main questions.
|
- |
|
| 113 |
foreach ($statistics->questionstats as $stats) {
|
- |
|
| 114 |
if ($stats->questionid == $questionid && isset($stats->$item)) {
|
- |
|
| 115 |
return $stats->$item;
|
- |
|
| 116 |
}
|
- |
|
| 117 |
}
|
- |
|
| 118 |
|
- |
|
| 119 |
// If not found, look in sub questions.
|
- |
|
| 120 |
foreach ($statistics->subquestionstats as $stats) {
|
- |
|
| 121 |
if ($stats->questionid == $questionid && isset($stats->$item)) {
|
- |
|
| 122 |
return $stats->$item;
|
- |
|
| 123 |
}
|
- |
|
| 124 |
}
|
- |
|
| 125 |
|
- |
|
| 126 |
return null;
|
- |
|
| 127 |
}
|
- |
|
| 128 |
|
- |
|
| 129 |
/**
|
- |
|
| 130 |
* Calculate average for a stats item on a list of questions.
|
- |
|
| 131 |
*
|
- |
|
| 132 |
* @param int[] $questionids list of ids of the questions we are interested in.
|
- |
|
| 133 |
* @param string $item one of the field names in all_calculated_for_qubaid_condition, e.g. 'facility'.
|
- |
|
| 134 |
* @return array array keys are question ids and the corresponding values are the average values.
|
- |
|
| 135 |
* Only questions for which there are data are included.
|
- |
|
| 136 |
* @deprecated since Moodle 4.3 please use the method from statistics_bulk_loader.
|
- |
|
| 137 |
* @todo MDL-78090 Final deprecation in Moodle 4.7
|
- |
|
| 138 |
*/
|
- |
|
| 139 |
private static function calculate_average_question_stats_item(array $questionids, string $item): array {
|
- |
|
| 140 |
$places = self::get_all_places_where_questions_were_attempted($questionids);
|
- |
|
| 141 |
|
- |
|
| 142 |
$counts = [];
|
- |
|
| 143 |
$sums = [];
|
- |
|
| 144 |
|
- |
|
| 145 |
foreach ($places as $place) {
|
- |
|
| 146 |
$statistics = self::load_statistics_for_place($place->component,
|
- |
|
| 147 |
\context::instance_by_id($place->contextid));
|
- |
|
| 148 |
if ($statistics === null) {
|
- |
|
| 149 |
continue;
|
- |
|
| 150 |
}
|
- |
|
| 151 |
|
- |
|
| 152 |
foreach ($questionids as $questionid) {
|
- |
|
| 153 |
$value = self::extract_item_value($statistics, $questionid, $item);
|
- |
|
| 154 |
if ($value === null) {
|
- |
|
| 155 |
continue;
|
- |
|
| 156 |
}
|
- |
|
| 157 |
|
- |
|
| 158 |
$counts[$questionid] = ($counts[$questionid] ?? 0) + 1;
|
- |
|
| 159 |
$sums[$questionid] = ($sums[$questionid] ?? 0) + $value;
|
- |
|
| 160 |
}
|
- |
|
| 161 |
}
|
- |
|
| 162 |
|
- |
|
| 163 |
// Return null if there is no quizzes.
|
- |
|
| 164 |
$averages = [];
|
- |
|
| 165 |
foreach ($sums as $questionid => $sum) {
|
- |
|
| 166 |
$averages[$questionid] = $sum / $counts[$questionid];
|
- |
|
| 167 |
}
|
- |
|
| 168 |
return $averages;
|
- |
|
| 169 |
}
|
- |
|
| 170 |
|
- |
|
| 171 |
/**
|
- |
|
| 172 |
* Calculate average facility index
|
- |
|
| 173 |
*
|
- |
|
| 174 |
* @param int $questionid
|
- |
|
| 175 |
* @return float|null
|
- |
|
| 176 |
* @deprecated since Moodle 4.3 please use the method from statistics_bulk_loader.
|
- |
|
| 177 |
* @todo MDL-78090 Final deprecation in Moodle 4.7
|
40 |
* @deprecated since Moodle 4.3 please use the method from statistics_bulk_loader.
|
| - |
|
41 |
*/
|
| - |
|
42 |
#[\core\attribute\deprecated(
|
| - |
|
43 |
'statistics_bulk_loader or get_required_statistics_fields',
|
| - |
|
44 |
since: '4.3',
|
| - |
|
45 |
mdl: 'MDL-75576',
|
| - |
|
46 |
final: true
|
| 178 |
*/
|
47 |
)]
|
| 179 |
public static function calculate_average_question_facility(int $questionid): ?float {
|
48 |
public static function calculate_average_question_facility(int $questionid): ?float {
|
| 180 |
debugging('Deprecated: please use statistics_bulk_loader instead, ' .
|
- |
|
| 181 |
'or get_required_statistics_fields in your question bank column class.', DEBUG_DEVELOPER);
|
- |
|
| 182 |
$averages = self::calculate_average_question_stats_item([$questionid], 'facility');
|
- |
|
| 183 |
return $averages[$questionid] ?? null;
|
49 |
\core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
|
| Línea 184... |
Línea 50... |
| 184 |
}
|
50 |
}
|
| 185 |
|
- |
|
| 186 |
/**
|
- |
|
| 187 |
* Calculate average discriminative efficiency
|
- |
|
| 188 |
*
|
- |
|
| 189 |
* @param int $questionid question id
|
51 |
|
| 190 |
* @return float|null
|
- |
|
| 191 |
* @deprecated since Moodle 4.3 please use the method from statistics_bulk_loader.
|
52 |
/**
|
| - |
|
53 |
* @deprecated since Moodle 4.3 please use the method from statistics_bulk_loader.
|
| - |
|
54 |
*/
|
| - |
|
55 |
#[\core\attribute\deprecated(
|
| - |
|
56 |
'statistics_bulk_loader or get_required_statistics_fields',
|
| - |
|
57 |
since: '4.3',
|
| - |
|
58 |
mdl: 'MDL-75576',
|
| 192 |
* @todo MDL-78090 Final deprecation in Moodle 4.7
|
59 |
final: true
|
| 193 |
*/
|
60 |
)]
|
| 194 |
public static function calculate_average_question_discriminative_efficiency(int $questionid): ?float {
|
- |
|
| 195 |
debugging('Deprecated: please use statistics_bulk_loader instead, ' .
|
- |
|
| 196 |
'or get_required_statistics_fields in your question bank column class.', DEBUG_DEVELOPER);
|
- |
|
| 197 |
$averages = self::calculate_average_question_stats_item([$questionid], 'discriminativeefficiency');
|
61 |
public static function calculate_average_question_discriminative_efficiency(int $questionid): ?float {
|
| Línea 198... |
Línea 62... |
| 198 |
return $averages[$questionid] ?? null;
|
62 |
\core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
|
| 199 |
}
|
- |
|
| 200 |
|
- |
|
| 201 |
/**
|
- |
|
| 202 |
* Calculate average discriminative efficiency
|
- |
|
| 203 |
*
|
63 |
}
|
| 204 |
* @param int $questionid question id
|
- |
|
| 205 |
* @return float|null
|
64 |
|
| - |
|
65 |
/**
|
| - |
|
66 |
* @deprecated since Moodle 4.3 please use the method from statistics_bulk_loader.
|
| - |
|
67 |
*/
|
| - |
|
68 |
#[\core\attribute\deprecated(
|
| - |
|
69 |
'statistics_bulk_loader or get_required_statistics_fields',
|
| - |
|
70 |
since: '4.3',
|
| 206 |
* @deprecated since Moodle 4.3 please use the method from statistics_bulk_loader.
|
71 |
mdl: 'MDL-75576',
|
| 207 |
* @todo MDL-78090 Final deprecation in Moodle 4.7
|
72 |
final: true
|
| 208 |
*/
|
- |
|
| 209 |
public static function calculate_average_question_discrimination_index(int $questionid): ?float {
|
- |
|
| 210 |
debugging('Deprecated: please use statistics_bulk_loader instead, ' .
|
- |
|
| 211 |
'or get_required_statistics_fields in your question bank column class.', DEBUG_DEVELOPER);
|
73 |
)]
|
| Línea 212... |
Línea 74... |
| 212 |
$averages = self::calculate_average_question_stats_item([$questionid], 'discriminationindex');
|
74 |
public static function calculate_average_question_discrimination_index(int $questionid): ?float {
|
| 213 |
return $averages[$questionid] ?? null;
|
75 |
\core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
|
| 214 |
}
|
76 |
}
|