first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,497 @@
<?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/>.
/**
* A collection of all the question statistics calculated for an activity instance ie. the stats calculated for slots and
* sub-questions and variants of those questions.
*
* @package core_question
* @copyright 2014 The Open University
* @author James Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_question\statistics\questions;
use question_bank;
/**
* A collection of all the question statistics calculated for an activity instance.
*
* @package core_question
* @copyright 2014 The Open University
* @author James Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class all_calculated_for_qubaid_condition {
/**
* @var int previously, the time after which statistics are automatically recomputed.
* @deprecated since Moodle 4.3. Use of pre-computed stats is no longer time-limited.
* @todo MDL-78090 Final deprecation in Moodle 4.7
*/
const TIME_TO_CACHE = 900; // 15 minutes.
/**
* @var object[]
*/
public $subquestions = [];
/**
* Holds slot (position) stats and stats for variants of questions in slots.
*
* @var calculated[]
*/
public $questionstats = array();
/**
* Holds sub-question stats and stats for variants of subqs.
*
* @var calculated_for_subquestion[]
*/
public $subquestionstats = array();
/**
* Set up a calculated_for_subquestion instance ready to store a randomly selected question's stats.
*
* @param object $step
* @param int|null $variant Is this to keep track of a variant's stats? If so what is the variant, if not null.
*/
public function initialise_for_subq($step, $variant = null) {
$newsubqstat = new calculated_for_subquestion($step, $variant);
if ($variant === null) {
$this->subquestionstats[$step->questionid] = $newsubqstat;
} else {
$this->subquestionstats[$step->questionid]->variantstats[$variant] = $newsubqstat;
}
}
/**
* Set up a calculated instance ready to store a slot question's stats.
*
* @param int $slot
* @param object $question
* @param int|null $variant Is this to keep track of a variant's stats? If so what is the variant, if not null.
*/
public function initialise_for_slot($slot, $question, $variant = null) {
$newqstat = new calculated($question, $slot, $variant);
if ($variant === null) {
$this->questionstats[$slot] = $newqstat;
} else {
$this->questionstats[$slot]->variantstats[$variant] = $newqstat;
}
}
/**
* Do we have stats for a particular quesitonid (and optionally variant)?
*
* @param int $questionid The id of the sub question.
* @param int|null $variant if not null then we want the object to store a variant of a sub-question's stats.
* @return bool whether those stats exist (yet).
*/
public function has_subq($questionid, $variant = null) {
if ($variant === null) {
return isset($this->subquestionstats[$questionid]);
} else {
return isset($this->subquestionstats[$questionid]->variantstats[$variant]);
}
}
/**
* Reference for a item stats instance for a questionid and optional variant no.
*
* @param int $questionid The id of the sub question.
* @param int|null $variant if not null then we want the object to store a variant of a sub-question's stats.
* @return calculated|calculated_for_subquestion stats instance for a questionid and optional variant no.
* Will be a calculated_for_subquestion if no variant specified.
* @throws \coding_exception if there is an attempt to respond to a non-existant set of stats.
*/
public function for_subq($questionid, $variant = null) {
if ($variant === null) {
if (!isset($this->subquestionstats[$questionid])) {
throw new \coding_exception('Reference to unknown question id ' . $questionid);
} else {
return $this->subquestionstats[$questionid];
}
} else {
if (!isset($this->subquestionstats[$questionid]->variantstats[$variant])) {
throw new \coding_exception('Reference to unknown question id ' . $questionid .
' variant ' . $variant);
} else {
return $this->subquestionstats[$questionid]->variantstats[$variant];
}
}
}
/**
* ids of all randomly selected question for all slots.
*
* @return int[] An array of all sub-question ids.
*/
public function get_all_subq_ids() {
return array_keys($this->subquestionstats);
}
/**
* All slots nos that stats have been calculated for.
*
* @return int[] An array of all slot nos.
*/
public function get_all_slots() {
return array_keys($this->questionstats);
}
/**
* Do we have stats for a particular slot (and optionally variant)?
*
* @param int $slot The slot no.
* @param int|null $variant if provided then we want the object which stores a variant of a position's stats.
* @return bool whether those stats exist (yet).
*/
public function has_slot($slot, $variant = null) {
if ($variant === null) {
return isset($this->questionstats[$slot]);
} else {
return isset($this->questionstats[$slot]->variantstats[$variant]);
}
}
/**
* Get position stats instance for a slot and optional variant no.
*
* @param int $slot The slot no.
* @param int|null $variant if provided then we want the object which stores a variant of a position's stats.
* @return calculated|calculated_for_subquestion An instance of the class storing the calculated position stats.
* @throws \coding_exception if there is an attempt to respond to a non-existant set of stats.
*/
public function for_slot($slot, $variant = null) {
if ($variant === null) {
if (!isset($this->questionstats[$slot])) {
throw new \coding_exception('Reference to unknown slot ' . $slot);
} else {
return $this->questionstats[$slot];
}
} else {
if (!isset($this->questionstats[$slot]->variantstats[$variant])) {
throw new \coding_exception('Reference to unknown slot ' . $slot . ' variant ' . $variant);
} else {
return $this->questionstats[$slot]->variantstats[$variant];
}
}
}
/**
* Load cached statistics from the database.
*
* @param \qubaid_condition $qubaids Which question usages to load stats for?
*/
public function get_cached($qubaids) {
global $DB;
$timemodified = self::get_last_calculated_time($qubaids);
$questionstatrecs = $DB->get_records('question_statistics',
['hashcode' => $qubaids->get_hash_code(), 'timemodified' => $timemodified]);
$questionids = array();
foreach ($questionstatrecs as $fromdb) {
if (is_null($fromdb->variant) && !$fromdb->slot) {
$questionids[] = $fromdb->questionid;
}
}
$this->subquestions = question_load_questions($questionids);
foreach ($questionstatrecs as $fromdb) {
if (is_null($fromdb->variant)) {
if ($fromdb->slot) {
if (!isset($this->questionstats[$fromdb->slot])) {
debugging('Statistics found for slot ' . $fromdb->slot .
' in stats ' . json_encode($qubaids->from_where_params()) .
' which is not an analysable question.', DEBUG_DEVELOPER);
}
$this->questionstats[$fromdb->slot]->populate_from_record($fromdb);
} else {
$this->subquestionstats[$fromdb->questionid] = new calculated_for_subquestion();
$this->subquestionstats[$fromdb->questionid]->populate_from_record($fromdb);
if (isset($this->subquestions[$fromdb->questionid])) {
$this->subquestionstats[$fromdb->questionid]->question =
$this->subquestions[$fromdb->questionid];
} else {
$this->subquestionstats[$fromdb->questionid]->question = question_bank::get_qtype(
'missingtype', false)->make_deleted_instance($fromdb->questionid, 1);
}
}
}
}
// Add cached variant stats to data structure.
foreach ($questionstatrecs as $fromdb) {
if (!is_null($fromdb->variant)) {
if ($fromdb->slot) {
if (!isset($this->questionstats[$fromdb->slot])) {
debugging('Statistics found for slot ' . $fromdb->slot .
' in stats ' . json_encode($qubaids->from_where_params()) .
' which is not an analysable question.', DEBUG_DEVELOPER);
continue;
}
$newcalcinstance = new calculated();
$this->questionstats[$fromdb->slot]->variantstats[$fromdb->variant] = $newcalcinstance;
$newcalcinstance->question = $this->questionstats[$fromdb->slot]->question;
} else {
$newcalcinstance = new calculated_for_subquestion();
$this->subquestionstats[$fromdb->questionid]->variantstats[$fromdb->variant] = $newcalcinstance;
if (isset($this->subquestions[$fromdb->questionid])) {
$newcalcinstance->question = $this->subquestions[$fromdb->questionid];
} else {
$newcalcinstance->question = question_bank::get_qtype(
'missingtype', false)->make_deleted_instance($fromdb->questionid, 1);
}
}
$newcalcinstance->populate_from_record($fromdb);
}
}
}
/**
* Find time of non-expired statistics in the database.
*
* @param \qubaid_condition $qubaids Which question usages to look for stats for?
* @return int|bool Time of cached record that matches this qubaid_condition or false if non found.
*/
public function get_last_calculated_time($qubaids) {
global $DB;
$lastcalculatedtime = $DB->get_field('question_statistics', 'COALESCE(MAX(timemodified), 0)',
['hashcode' => $qubaids->get_hash_code()]);
if ($lastcalculatedtime) {
return $lastcalculatedtime;
} else {
return false;
}
}
/**
* Save stats to db, first cleaning up any old ones.
*
* @param \qubaid_condition $qubaids Which question usages are we caching the stats of?
*/
public function cache($qubaids) {
global $DB;
$transaction = $DB->start_delegated_transaction();
$timemodified = time();
foreach ($this->get_all_slots() as $slot) {
$this->for_slot($slot)->cache($qubaids, $timemodified);
}
foreach ($this->get_all_subq_ids() as $subqid) {
$this->for_subq($subqid)->cache($qubaids, $timemodified);
}
$transaction->allow_commit();
}
/**
* Return all sub-questions used.
*
* @return \object[] array of questions.
*/
public function get_sub_questions() {
return $this->subquestions;
}
/**
* Return all stats for one slot, stats for the slot itself, and either :
* - variants of question
* - variants of randomly selected questions
* - randomly selected questions
*
* @param int $slot the slot no
* @param bool|int $limitvariants limit number of variants and sub-questions displayed?
* @return calculated|calculated_for_subquestion[] stats to display
*/
public function structure_analysis_for_one_slot($slot, $limitvariants = false) {
return array_merge(array($this->for_slot($slot)), $this->all_subq_and_variant_stats_for_slot($slot, $limitvariants));
}
/**
* Call after calculations to output any error messages.
*
* @return string[] Array of strings describing error messages found during stats calculation.
*/
public function any_error_messages() {
$errors = array();
foreach ($this->get_all_slots() as $slot) {
foreach ($this->for_slot($slot)->get_sub_question_ids() as $subqid) {
if ($this->for_subq($subqid)->differentweights) {
$name = $this->for_subq($subqid)->question->name;
$errors[] = get_string('erroritemappearsmorethanoncewithdifferentweight', 'question', $name);
}
}
}
return $errors;
}
/**
* Return all stats for variants of question in slot $slot.
*
* @param int $slot The slot no.
* @return calculated[] The instances storing the calculated stats.
*/
protected function all_variant_stats_for_one_slot($slot) {
$toreturn = array();
foreach ($this->for_slot($slot)->get_variants() as $variant) {
$toreturn[] = $this->for_slot($slot, $variant);
}
return $toreturn;
}
/**
* Return all stats for variants of randomly selected questions for one slot $slot.
*
* @param int $slot The slot no.
* @return calculated[] The instances storing the calculated stats.
*/
protected function all_subq_variants_for_one_slot($slot) {
$toreturn = array();
$displayorder = 1;
foreach ($this->for_slot($slot)->get_sub_question_ids() as $subqid) {
if ($variants = $this->for_subq($subqid)->get_variants()) {
foreach ($variants as $variant) {
$toreturn[] = $this->make_new_subq_stat_for($displayorder, $slot, $subqid, $variant);
}
}
$displayorder++;
}
return $toreturn;
}
/**
* Return all stats for randomly selected questions for one slot $slot.
*
* @param int $slot The slot no.
* @return calculated[] The instances storing the calculated stats.
*/
protected function all_subqs_for_one_slot($slot) {
$displayorder = 1;
$toreturn = array();
foreach ($this->for_slot($slot)->get_sub_question_ids() as $subqid) {
$toreturn[] = $this->make_new_subq_stat_for($displayorder, $slot, $subqid);
$displayorder++;
}
return $toreturn;
}
/**
* Return all variant or 'sub-question' stats one slot, either :
* - variants of question
* - variants of randomly selected questions
* - randomly selected questions
*
* @param int $slot the slot no
* @param bool $limited limit number of variants and sub-questions displayed?
* @return calculated|calculated_for_subquestion|calculated_question_summary[] stats to display
*/
protected function all_subq_and_variant_stats_for_slot($slot, $limited) {
// Random question in this slot?
if ($this->for_slot($slot)->get_sub_question_ids()) {
$toreturn = array();
if ($limited) {
$randomquestioncalculated = $this->for_slot($slot);
if ($subqvariantstats = $this->all_subq_variants_for_one_slot($slot)) {
// There are some variants from randomly selected questions.
// If we're showing a limited view of the statistics then add a question summary stat
// rather than a stat for each subquestion.
$summarystat = $this->make_new_calculated_question_summary_stat($randomquestioncalculated, $subqvariantstats);
$toreturn = array_merge($toreturn, [$summarystat]);
}
if ($subqstats = $this->all_subqs_for_one_slot($slot)) {
// There are some randomly selected questions.
// If we're showing a limited view of the statistics then add a question summary stat
// rather than a stat for each subquestion.
$summarystat = $this->make_new_calculated_question_summary_stat($randomquestioncalculated, $subqstats);
$toreturn = array_merge($toreturn, [$summarystat]);
}
foreach ($toreturn as $index => $calculated) {
$calculated->subqdisplayorder = $index;
}
} else {
$displaynumber = 1;
foreach ($this->for_slot($slot)->get_sub_question_ids() as $subqid) {
$toreturn[] = $this->make_new_subq_stat_for($displaynumber, $slot, $subqid);
if ($variants = $this->for_subq($subqid)->get_variants()) {
foreach ($variants as $variant) {
$toreturn[] = $this->make_new_subq_stat_for($displaynumber, $slot, $subqid, $variant);
}
}
$displaynumber++;
}
}
return $toreturn;
} else {
$variantstats = $this->all_variant_stats_for_one_slot($slot);
if ($limited && $variantstats) {
$variantquestioncalculated = $this->for_slot($slot);
// If we're showing a limited view of the statistics then add a question summary stat
// rather than a stat for each variation.
$summarystat = $this->make_new_calculated_question_summary_stat($variantquestioncalculated, $variantstats);
return [$summarystat];
} else {
return $variantstats;
}
}
}
/**
* We need a new object for display. Sub-question stats can appear more than once in different slots.
* So we create a clone of the object and then we can set properties on the object that are per slot.
*
* @param int $displaynumber The display number for this sub question.
* @param int $slot The slot number.
* @param int $subqid The sub question id.
* @param null|int $variant The variant no.
* @return calculated_for_subquestion The object for display.
*/
protected function make_new_subq_stat_for($displaynumber, $slot, $subqid, $variant = null) {
$slotstat = fullclone($this->for_subq($subqid, $variant));
$slotstat->question->number = $this->for_slot($slot)->question->number;
$slotstat->subqdisplayorder = $displaynumber;
return $slotstat;
}
/**
* Create a summary calculated object for a calculated question. This is used as a placeholder
* to indicate that a calculated question has sub questions or variations to show rather than listing each
* subquestion or variation directly.
*
* @param calculated $randomquestioncalculated The calculated instance for the random question slot.
* @param calculated[] $subquestionstats The instances of the calculated stats of the questions that are being summarised.
* @return calculated_question_summary
*/
protected function make_new_calculated_question_summary_stat($randomquestioncalculated, $subquestionstats) {
$question = $randomquestioncalculated->question;
$slot = $randomquestioncalculated->slot;
$calculatedsummary = new calculated_question_summary($question, $slot, $subquestionstats);
return $calculatedsummary;
}
}
@@ -0,0 +1,304 @@
<?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/>.
/**
* Question statistics calculations class. Used in the quiz statistics report but also available for use elsewhere.
*
* @package core
* @subpackage questionbank
* @copyright 2013 Open University
* @author Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_question\statistics\questions;
defined('MOODLE_INTERNAL') || die();
/**
* This class is used to return the stats as calculated by {@link \core_question\statistics\questions\calculator}
*
* @copyright 2013 Open University
* @author Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calculated {
public $questionid;
// These first fields are the final fields cached in the db and shown in reports.
// See : http://docs.moodle.org/dev/Quiz_statistics_calculations#Position_statistics .
public $slot = null;
/**
* @var null|integer if this property is not null then this is the stats for a variant of a question or when inherited by
* calculated_for_subquestion and not null then this is the stats for a variant of a sub question.
*/
public $variant = null;
/**
* @var bool is this a sub question.
*/
public $subquestion = false;
/**
* @var string if this stat has been picked as a min, median or maximum facility value then this string says which stat this
* is. Prepended to question name for display.
*/
public $minmedianmaxnotice = '';
/**
* @var int total attempts at this question.
*/
public $s = 0;
/**
* @var float effective weight of this question.
*/
public $effectiveweight;
/**
* @var bool is covariance of this questions mark with other question marks negative?
*/
public $negcovar;
/**
* @var float
*/
public $discriminationindex;
/**
* @var float
*/
public $discriminativeefficiency;
/**
* @var float standard deviation
*/
public $sd;
/**
* @var float
*/
public $facility;
/**
* @var float max mark achievable for this question.
*/
public $maxmark;
/**
* @var string comma separated list of the positions in which this question appears.
*/
public $positions;
/**
* @var null|float The average score that students would have got by guessing randomly. Or null if not calculable.
*/
public $randomguessscore = null;
// End of fields in db.
protected $fieldsindb = array('questionid', 'slot', 'subquestion', 's', 'effectiveweight', 'negcovar', 'discriminationindex',
'discriminativeefficiency', 'sd', 'facility', 'subquestions', 'maxmark', 'positions', 'randomguessscore', 'variant');
// Fields used for intermediate calculations.
public $totalmarks = 0;
public $totalothermarks = 0;
/**
* @var float The total of marks achieved for all positions in all attempts where this item was seen.
*/
public $totalsummarks = 0;
public $markvariancesum = 0;
public $othermarkvariancesum = 0;
public $covariancesum = 0;
public $covariancemaxsum = 0;
public $subquestions = '';
public $covariancewithoverallmarksum = 0;
public $markarray = array();
public $othermarksarray = array();
public $markaverage;
public $othermarkaverage;
/**
* @var float The average for all attempts, of the sum of the marks for all positions in which this item appeared.
*/
public $summarksaverage;
public $markvariance;
public $othermarkvariance;
public $covariance;
public $covariancemax;
public $covariancewithoverallmark;
/**
* @var object full question data
*/
public $question;
/**
* An array of calculated stats for each variant of the question. Even when there is just one variant we still calculate this
* data as there is no way to know if there are variants before we have finished going through the attempt data one time.
*
* @var calculated[] $variants
*/
public $variantstats = array();
/**
* Set if this record has been retrieved from cache. This is the time that the statistics were calculated.
*
* @var integer
*/
public $timemodified;
/**
* Set up a calculated instance ready to store a question's (or a variant of a slot's question's)
* stats for one slot in the quiz.
*
* @param null|object $question
* @param null|int $slot
* @param null|int $variant
*/
public function __construct($question = null, $slot = null, $variant = null) {
if ($question !== null) {
$this->questionid = $question->id;
$this->maxmark = $question->maxmark;
$this->positions = $question->number;
$this->question = $question;
}
if ($slot !== null) {
$this->slot = $slot;
}
if ($variant !== null) {
$this->variant = $variant;
}
}
/**
* Used to determine which random questions pull sub questions from the same pools. Where pool means category and possibly
* all the sub categories of that category.
*
* @return null|string represents the pool of questions from which this question draws if it is random, or null if not.
*/
public function random_selector_string() {
if ($this->question->qtype == 'random') {
return $this->question->category .'/'. $this->question->questiontext;
} else {
return null;
}
}
/**
* Cache calculated stats stored in this object in 'question_statistics' table.
*
* @param \qubaid_condition $qubaids
* @param int|null $timemodified the modified time to store. Defaults to the current time.
*/
public function cache($qubaids, $timemodified = null) {
global $DB;
$toinsert = new \stdClass();
$toinsert->hashcode = $qubaids->get_hash_code();
$toinsert->timemodified = $timemodified ?? time();
foreach ($this->fieldsindb as $field) {
$toinsert->{$field} = $this->{$field};
}
$DB->insert_record('question_statistics', $toinsert, false);
if ($this->get_variants()) {
foreach ($this->variantstats as $variantstat) {
$variantstat->cache($qubaids, $timemodified);
}
}
}
/**
* Load properties of this class from db record.
*
* @param object $record Given a record from 'question_statistics' copy stats from record to properties.
*/
public function populate_from_record($record) {
foreach ($this->fieldsindb as $field) {
$this->$field = $record->$field;
}
$this->timemodified = $record->timemodified;
}
/**
* Sort the variants of this question by variant number.
*/
public function sort_variants() {
ksort($this->variantstats);
}
/**
* Get any sub question ids for this question.
*
* @return int[] array of sub-question ids or empty array if there are none.
*/
public function get_sub_question_ids() {
if ($this->subquestions !== '') {
return explode(',', $this->subquestions);
} else {
return array();
}
}
/**
* Array of variants that have appeared in the attempt data for this question. Or an empty array if there is only one variant.
*
* @return int[] the variant nos.
*/
public function get_variants() {
$variants = array_keys($this->variantstats);
if (count($variants) > 1 || reset($variants) != 1) {
return $variants;
} else {
return array();
}
}
/**
* Do we break down the stats for this question by variant or not?
*
* @return bool Do we?
*/
public function break_down_by_variant() {
$qtype = \question_bank::get_qtype($this->question->qtype);
return $qtype->break_down_stats_and_response_analysis_by_variant($this->question);
}
/**
* Delete the data structure for storing variant stats.
*/
public function clear_variants() {
$this->variantstats = array();
}
}
@@ -0,0 +1,70 @@
<?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/>.
/**
* Class for storing calculated sub question statistics and intermediate calculation values.
*
* @package core_question
* @copyright 2013 The Open University
* @author James Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_question\statistics\questions;
defined('MOODLE_INTERNAL') || die();
/**
* A class to store calculated stats for a sub question.
*
* @package core_question
* @copyright 2013 The Open University
* @author James Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calculated_for_subquestion extends calculated {
public $subquestion = true;
/**
* @var array What slots is this sub question used in?
*/
public $usedin = array();
/**
* @var bool Have the slots this sub question has been used in got different grades?
*/
public $differentweights = false;
public $negcovar = 0;
/**
* @var int only set immediately before display in the table. The order of display in the table.
*/
public $subqdisplayorder;
/**
* Constructor.
*
* @param object|null $step the step data for the step that this sub-question was first encountered in.
* @param int|null $variant the variant no
*/
public function __construct($step = null, $variant = null) {
if ($step !== null) {
$this->questionid = $step->questionid;
$this->maxmark = $step->maxmark;
}
$this->variant = $variant;
}
}
@@ -0,0 +1,187 @@
<?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/>.
/**
* Question statistics calculations class. Used in the quiz statistics report.
*
* @package core_question
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_question\statistics\questions;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/engine/lib.php');
/**
* Class calculated_question_summary
*
* This class is used to indicate the statistics for a random question slot should
* be rendered with a link to a summary of the displayed questions.
*
* It's used in the limited view of the statistics calculation in lieu of adding
* the stats for each subquestion individually.
*
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calculated_question_summary extends calculated {
/**
* @var int only set immediately before display in the table. The order of display in the table.
*/
public $subqdisplayorder;
/**
* @var calculated[] The instances storing the calculated stats of the questions that are being summarised.
*/
protected $subqstats;
/**
* calculated_question_summary constructor.
*
* @param \stdClass $question
* @param int $slot
* @param calculated[] $subqstats The instances of the calculated stats of the questions that are being summarised.
*/
public function __construct($question, $slot, $subqstats) {
parent::__construct($question, $slot);
$this->subqstats = $subqstats;
$this->subquestions = implode(',', array_column($subqstats, 'questionid'));
}
/**
* This is a summary stat so never breakdown by variant.
*
* @return bool
*/
public function break_down_by_variant() {
return false;
}
/**
* Returns the minimum and maximum values of the given attribute in the summarised calculated stats.
*
* @param string $attribute The attribute that we are looking for its extremums.
* @return array An array of [min,max]
*/
public function get_min_max_of($attribute) {
$getmethod = 'get_min_max_of_' . $attribute;
if (method_exists($this, $getmethod)) {
return $this->$getmethod();
} else {
$min = $max = null;
$set = false;
// We cannot simply use min or max functions because, in theory, some attributes might be non-scalar.
foreach (array_column($this->subqstats, $attribute) as $value) {
if (is_scalar($value) || is_null($value)) {
if (!$set) { // It is not good enough to check if (!isset($min)),
// because $min might have been set to null in an earlier iteration.
$min = $value;
$max = $value;
$set = true;
}
$min = $this->min($min, $value);
$max = $this->max($max, $value);
}
}
return [$min, $max];
}
}
/**
* Returns the minimum and maximum values of the standard deviation in the summarised calculated stats.
* @return array An array of [min,max]
*/
protected function get_min_max_of_sd() {
$min = $max = null;
$set = false;
foreach ($this->subqstats as $subqstat) {
if (isset($subqstat->sd) && $subqstat->maxmark > \question_utils::MARK_TOLERANCE) {
$value = $subqstat->sd / $subqstat->maxmark;
} else {
$value = null;
}
if (!$set) { // It is not good enough to check if (!isset($min)),
// because $min might have been set to null in an earlier iteration.
$min = $value;
$max = $value;
$set = true;
}
$min = $this->min($min, $value);
$max = $this->max($max, $value);
}
return [$min, $max];
}
/**
* Find higher value.
* A zero value is almost considered equal to zero in comparisons. The only difference is that when being compared to zero,
* zero is higher than null.
*
* @param float|null $value1
* @param float|null $value2
* @return float|null
*/
protected function max(float $value1 = null, float $value2 = null) {
$temp1 = $value1 ?: 0;
$temp2 = $value2 ?: 0;
$tempmax = max($temp1, $temp2);
if (!$tempmax && $value1 !== 0 && $value2 !== 0) {
$max = null;
} else {
$max = $tempmax;
}
return $max;
}
/**
* Find lower value.
* A zero value is almost considered equal to zero in comparisons. The only difference is that when being compared to zero,
* zero is lower than null.
*
* @param float|null $value1
* @param float|null $value2
* @return mixed|null
*/
protected function min(float $value1 = null, float $value2 = null) {
$temp1 = $value1 ?: 0;
$temp2 = $value2 ?: 0;
$tempmin = min($temp1, $temp2);
if (!$tempmin && $value1 !== 0 && $value2 !== 0) {
$min = null;
} else {
$min = $tempmin;
}
return $min;
}
}
@@ -0,0 +1,494 @@
<?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/>.
/**
* Question statistics calculator class. Used in the quiz statistics report but also available for use elsewhere.
*
* @package core
* @subpackage questionbank
* @copyright 2013 Open University
* @author Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_question\statistics\questions;
defined('MOODLE_INTERNAL') || die();
/**
* This class has methods to compute the question statistics from the raw data.
*
* @copyright 2013 Open University
* @author Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calculator {
/**
* @var all_calculated_for_qubaid_condition all the stats calculated for slots and sub-questions and variants of those
* questions.
*/
protected $stats;
/**
* @var float
*/
protected $sumofmarkvariance = 0;
/**
* @var array[] keyed by a string representing the pool of questions that this random question draws from.
* string as returned from {@link \core_question\statistics\questions\calculated::random_selector_string}
*/
protected $randomselectors = array();
/**
* @var \progress_trace
*/
protected $progress;
/**
* @var string The class name of the class to instantiate to store statistics calculated.
*/
protected $statscollectionclassname = '\core_question\statistics\questions\all_calculated_for_qubaid_condition';
/**
* Constructor.
*
* @param object[] questions to analyze, keyed by slot, also analyses sub questions for random questions.
* we expect some extra fields - slot, maxmark and number on the full question data objects.
* @param \core\progress\base|null $progress the element to send progress messages to, default is {@link \core\progress\none}.
*/
public function __construct($questions, $progress = null) {
if ($progress === null) {
$progress = new \core\progress\none();
}
$this->progress = $progress;
$this->stats = new $this->statscollectionclassname();
foreach ($questions as $slot => $question) {
$this->stats->initialise_for_slot($slot, $question);
$this->stats->for_slot($slot)->randomguessscore = $this->get_random_guess_score($question);
}
}
/**
* Calculate the stats.
*
* @param \qubaid_condition $qubaids Which question usages to calculate the stats for?
* @return all_calculated_for_qubaid_condition The calculated stats.
*/
public function calculate($qubaids) {
$this->progress->start_progress('', 6);
list($lateststeps, $summarks) = $this->get_latest_steps($qubaids);
if ($lateststeps) {
$this->progress->start_progress('', count($lateststeps), 1);
// Compute the statistics of position, and for random questions, work
// out which questions appear in which positions.
foreach ($lateststeps as $step) {
$this->progress->increment_progress();
$israndomquestion = ($step->questionid != $this->stats->for_slot($step->slot)->questionid);
$breakdownvariants = !$israndomquestion && $this->stats->for_slot($step->slot)->break_down_by_variant();
// If this is a variant we have not seen before create a place to store stats calculations for this variant.
if ($breakdownvariants && !$this->stats->has_slot($step->slot, $step->variant)) {
$question = $this->stats->for_slot($step->slot)->question;
$this->stats->initialise_for_slot($step->slot, $question, $step->variant);
$this->stats->for_slot($step->slot, $step->variant)->randomguessscore =
$this->get_random_guess_score($question);
}
// Step data walker for main question.
$this->initial_steps_walker($step, $this->stats->for_slot($step->slot), $summarks, true, $breakdownvariants);
// If this is a random question do the calculations for sub question stats.
if ($israndomquestion) {
if (!$this->stats->has_subq($step->questionid)) {
$this->stats->initialise_for_subq($step);
} else if ($this->stats->for_subq($step->questionid)->maxmark != $step->maxmark) {
$this->stats->for_subq($step->questionid)->differentweights = true;
}
// If this is a variant of this subq we have not seen before create a place to store stats calculations for it.
if (!$this->stats->has_subq($step->questionid, $step->variant)) {
$this->stats->initialise_for_subq($step, $step->variant);
}
$this->initial_steps_walker($step, $this->stats->for_subq($step->questionid), $summarks, false);
// Extra stuff we need to do in this loop for subqs to keep track of where they need to be displayed later.
$number = $this->stats->for_slot($step->slot)->question->number;
$this->stats->for_subq($step->questionid)->usedin[$number] = $number;
// Keep track of which random questions are actually selected from each pool of questions that random
// questions are pulled from.
$randomselectorstring = $this->stats->for_slot($step->slot)->random_selector_string();
if (!isset($this->randomselectors[$randomselectorstring])) {
$this->randomselectors[$randomselectorstring] = array();
}
$this->randomselectors[$randomselectorstring][$step->questionid] = $step->questionid;
}
}
$this->progress->end_progress();
foreach ($this->randomselectors as $key => $notused) {
ksort($this->randomselectors[$key]);
$this->randomselectors[$key] = implode(',', $this->randomselectors[$key]);
}
$this->stats->subquestions = question_load_questions($this->stats->get_all_subq_ids());
// Compute the statistics for sub questions, if there are any.
$this->progress->start_progress('', count($this->stats->subquestions), 1);
foreach ($this->stats->subquestions as $qid => $subquestion) {
$this->progress->increment_progress();
$subquestion->maxmark = $this->stats->for_subq($qid)->maxmark;
$this->stats->for_subq($qid)->question = $subquestion;
$this->stats->for_subq($qid)->randomguessscore = $this->get_random_guess_score($subquestion);
if ($variants = $this->stats->for_subq($qid)->get_variants()) {
foreach ($variants as $variant) {
$this->stats->for_subq($qid, $variant)->question = $subquestion;
$this->stats->for_subq($qid, $variant)->randomguessscore = $this->get_random_guess_score($subquestion);
}
$this->stats->for_subq($qid)->sort_variants();
}
$this->initial_question_walker($this->stats->for_subq($qid));
if ($this->stats->for_subq($qid)->usedin) {
sort($this->stats->for_subq($qid)->usedin, SORT_NUMERIC);
$this->stats->for_subq($qid)->positions = implode(',', $this->stats->for_subq($qid)->usedin);
} else {
$this->stats->for_subq($qid)->positions = '';
}
}
$this->progress->end_progress();
// Finish computing the averages, and put the sub-question data into the
// corresponding questions.
$slots = $this->stats->get_all_slots();
$totalnumberofslots = count($slots);
$maxindex = $totalnumberofslots - 1;
$this->progress->start_progress('', $totalnumberofslots, 1);
foreach ($slots as $index => $slot) {
$this->stats->for_slot($slot)->sort_variants();
$this->progress->increment_progress();
$nextslotindex = $index + 1;
$nextslot = ($nextslotindex > $maxindex) ? false : $slots[$nextslotindex];
$this->initial_question_walker($this->stats->for_slot($slot));
// The rest of this loop is to finish working out where randomly selected question stats should be displayed.
if ($this->stats->for_slot($slot)->question->qtype == 'random') {
$randomselectorstring = $this->stats->for_slot($slot)->random_selector_string();
if ($nextslot && ($randomselectorstring == $this->stats->for_slot($nextslot)->random_selector_string())) {
continue; // Next loop iteration.
}
if (isset($this->randomselectors[$randomselectorstring])) {
$this->stats->for_slot($slot)->subquestions = $this->randomselectors[$randomselectorstring];
}
}
}
$this->progress->end_progress();
// Go through the records one more time.
$this->progress->start_progress('', count($lateststeps), 1);
foreach ($lateststeps as $step) {
$this->progress->increment_progress();
$israndomquestion = ($this->stats->for_slot($step->slot)->question->qtype == 'random');
$this->secondary_steps_walker($step, $this->stats->for_slot($step->slot), $summarks);
if ($israndomquestion) {
$this->secondary_steps_walker($step, $this->stats->for_subq($step->questionid), $summarks);
}
}
$this->progress->end_progress();
$slots = $this->stats->get_all_slots();
$this->progress->start_progress('', count($slots), 1);
$sumofcovariancewithoverallmark = 0;
foreach ($this->stats->get_all_slots() as $slot) {
$this->progress->increment_progress();
$this->secondary_question_walker($this->stats->for_slot($slot));
$this->sumofmarkvariance += $this->stats->for_slot($slot)->markvariance;
$covariancewithoverallmark = $this->stats->for_slot($slot)->covariancewithoverallmark;
if (null !== $covariancewithoverallmark && $covariancewithoverallmark >= 0) {
$sumofcovariancewithoverallmark += sqrt($covariancewithoverallmark);
}
}
$this->progress->end_progress();
$subqids = $this->stats->get_all_subq_ids();
$this->progress->start_progress('', count($subqids), 1);
foreach ($subqids as $subqid) {
$this->progress->increment_progress();
$this->secondary_question_walker($this->stats->for_subq($subqid));
}
$this->progress->end_progress();
foreach ($this->stats->get_all_slots() as $slot) {
if ($sumofcovariancewithoverallmark) {
if ($this->stats->for_slot($slot)->negcovar) {
$this->stats->for_slot($slot)->effectiveweight = null;
} else {
$this->stats->for_slot($slot)->effectiveweight =
100 * sqrt($this->stats->for_slot($slot)->covariancewithoverallmark) /
$sumofcovariancewithoverallmark;
}
} else {
$this->stats->for_slot($slot)->effectiveweight = null;
}
}
$this->stats->cache($qubaids);
}
// All finished.
$this->progress->end_progress();
return $this->stats;
}
/**
* Used when computing Coefficient of Internal Consistency by quiz statistics.
*
* @return float
*/
public function get_sum_of_mark_variance() {
return $this->sumofmarkvariance;
}
/**
* Get the latest step data from the db, from which we will calculate stats.
*
* @param \qubaid_condition $qubaids Which question usages to get the latest steps for?
* @return array with two items
* - $lateststeps array of latest step data for the question usages
* - $summarks array of total marks for each usage, indexed by usage id
*/
protected function get_latest_steps($qubaids) {
$dm = new \question_engine_data_mapper();
$fields = " qas.id,
qa.questionusageid,
qa.questionid,
qa.variant,
qa.slot,
qa.maxmark,
qas.fraction * qa.maxmark as mark";
$lateststeps = $dm->load_questions_usages_latest_steps($qubaids, $this->stats->get_all_slots(), $fields);
$summarks = array();
if ($lateststeps) {
foreach ($lateststeps as $step) {
if (!isset($summarks[$step->questionusageid])) {
$summarks[$step->questionusageid] = 0;
}
$summarks[$step->questionusageid] += $step->mark;
}
}
return array($lateststeps, $summarks);
}
/**
* Calculating the stats is a four step process.
*
* We loop through all 'last step' data first.
*
* Update $stats->totalmarks, $stats->markarray, $stats->totalothermarks
* and $stats->othermarksarray to include another state.
*
* @param object $step the state to add to the statistics.
* @param calculated $stats the question statistics we are accumulating.
* @param array $summarks of the sum of marks for each question usage, indexed by question usage id
* @param bool $positionstat whether this is a statistic of position of question.
* @param bool $dovariantalso do we also want to do the same calculations for this variant?
*/
protected function initial_steps_walker($step, $stats, $summarks, $positionstat = true, $dovariantalso = true) {
$stats->s++;
$stats->totalmarks += $step->mark;
$stats->markarray[] = $step->mark;
if ($positionstat) {
$stats->totalothermarks += $summarks[$step->questionusageid] - $step->mark;
$stats->othermarksarray[] = $summarks[$step->questionusageid] - $step->mark;
} else {
$stats->totalothermarks += $summarks[$step->questionusageid];
$stats->othermarksarray[] = $summarks[$step->questionusageid];
}
if ($dovariantalso) {
$this->initial_steps_walker($step, $stats->variantstats[$step->variant], $summarks, $positionstat, false);
}
}
/**
* Then loop through all questions for the first time.
*
* Perform some computations on the per-question statistics calculations after
* we have been through all the step data.
*
* @param calculated $stats question stats to update.
*/
protected function initial_question_walker($stats) {
if ($stats->s != 0) {
$stats->markaverage = $stats->totalmarks / $stats->s;
$stats->othermarkaverage = $stats->totalothermarks / $stats->s;
$stats->summarksaverage = $stats->totalsummarks / $stats->s;
} else {
$stats->markaverage = 0;
$stats->othermarkaverage = 0;
$stats->summarksaverage = 0;
}
if ($stats->maxmark != 0) {
$stats->facility = $stats->markaverage / $stats->maxmark;
} else {
$stats->facility = null;
}
sort($stats->markarray, SORT_NUMERIC);
sort($stats->othermarksarray, SORT_NUMERIC);
// Here we have collected enough data to make the decision about which questions have variants whose stats we also want to
// calculate. We delete the initialised structures where they are not needed.
if (!$stats->get_variants() || !$stats->break_down_by_variant()) {
$stats->clear_variants();
}
foreach ($stats->get_variants() as $variant) {
$this->initial_question_walker($stats->variantstats[$variant]);
}
}
/**
* Loop through all last step data again.
*
* Now we know the averages, accumulate the date needed to compute the higher
* moments of the question scores.
*
* @param object $step the state to add to the statistics.
* @param calculated $stats the question statistics we are accumulating.
* @param float[] $summarks of the sum of marks for each question usage, indexed by question usage id
*/
protected function secondary_steps_walker($step, $stats, $summarks) {
$markdifference = $step->mark - $stats->markaverage;
if ($stats->subquestion) {
$othermarkdifference = $summarks[$step->questionusageid] - $stats->othermarkaverage;
} else {
$othermarkdifference = $summarks[$step->questionusageid] - $step->mark - $stats->othermarkaverage;
}
$overallmarkdifference = $summarks[$step->questionusageid] - $stats->summarksaverage;
$sortedmarkdifference = array_shift($stats->markarray) - $stats->markaverage;
$sortedothermarkdifference = array_shift($stats->othermarksarray) - $stats->othermarkaverage;
$stats->markvariancesum += pow($markdifference, 2);
$stats->othermarkvariancesum += pow($othermarkdifference, 2);
$stats->covariancesum += $markdifference * $othermarkdifference;
$stats->covariancemaxsum += $sortedmarkdifference * $sortedothermarkdifference;
$stats->covariancewithoverallmarksum += $markdifference * $overallmarkdifference;
if (isset($stats->variantstats[$step->variant])) {
$this->secondary_steps_walker($step, $stats->variantstats[$step->variant], $summarks);
}
}
/**
* And finally loop through all the questions again.
*
* Perform more per-question statistics calculations.
*
* @param calculated $stats question stats to update.
*/
protected function secondary_question_walker($stats) {
if ($stats->s > 1) {
$stats->markvariance = $stats->markvariancesum / ($stats->s - 1);
$stats->othermarkvariance = $stats->othermarkvariancesum / ($stats->s - 1);
$stats->covariance = $stats->covariancesum / ($stats->s - 1);
$stats->covariancemax = $stats->covariancemaxsum / ($stats->s - 1);
$stats->covariancewithoverallmark = $stats->covariancewithoverallmarksum /
($stats->s - 1);
$stats->sd = sqrt($stats->markvariancesum / ($stats->s - 1));
if ($stats->covariancewithoverallmark >= 0) {
$stats->negcovar = 0;
} else {
$stats->negcovar = 1;
}
} else {
$stats->markvariance = null;
$stats->othermarkvariance = null;
$stats->covariance = null;
$stats->covariancemax = null;
$stats->covariancewithoverallmark = null;
$stats->sd = null;
$stats->negcovar = 0;
}
if ($stats->markvariance * $stats->othermarkvariance) {
$stats->discriminationindex = 100 * $stats->covariance /
sqrt($stats->markvariance * $stats->othermarkvariance);
} else {
$stats->discriminationindex = null;
}
if ($stats->covariancemax) {
$stats->discriminativeefficiency = 100 * $stats->covariance /
$stats->covariancemax;
} else {
$stats->discriminativeefficiency = null;
}
foreach ($stats->variantstats as $variantstat) {
$this->secondary_question_walker($variantstat);
}
}
/**
* Given the question data find the average grade that random guesses would get.
*
* @param object $questiondata the full question object.
* @return float the random guess score for this question.
*/
protected function get_random_guess_score($questiondata) {
return \question_bank::get_qtype(
$questiondata->qtype, false)->get_random_guess_score($questiondata);
}
/**
* Find time of non-expired statistics in the database.
*
* @param \qubaid_condition $qubaids Which question usages to look for?
* @return int|bool Time of cached record that matches this qubaid_condition or false is non found.
*/
public function get_last_calculated_time($qubaids) {
return $this->stats->get_last_calculated_time($qubaids);
}
/**
* Load cached statistics from the database.
*
* @param \qubaid_condition $qubaids Which question usages to load the cached stats for?
* @return all_calculated_for_qubaid_condition The cached stats.
*/
public function get_cached($qubaids) {
$this->stats->get_cached($qubaids);
return $this->stats;
}
}