Ejemplo n.º 1
0
 public function display($quiz, $cm, $course)
 {
     global $CFG, $DB, $OUTPUT;
     // Initialise the required data.
     $this->mode = 'stack';
     $this->context = context_module::instance($cm->id);
     list($currentgroup, $students, $groupstudents, $allowed) = $this->load_relevant_students($cm, $course);
     $this->qubaids = quiz_statistics_qubaids_condition($quiz->id, $currentgroup, $groupstudents, true);
     $questionsused = $this->get_stack_questions_used_in_attempt($this->qubaids);
     $questionid = optional_param('questionid', 0, PARAM_INT);
     // Display the appropriate page.
     $this->print_header_and_tabs($cm, $course, $quiz);
     if (!$questionsused) {
         $this->display_no_stack_questions();
     } else {
         if (!$questionid) {
             $this->display_index($questionsused);
         } else {
             if (array_key_exists($questionid, $questionsused)) {
                 $this->display_analysis($questionsused[$questionid]);
             } else {
                 $this->display_unknown_question();
             }
         }
     }
 }
 public function get_stats($quiz, $whichattempts = QUIZ_GRADEAVERAGE, $groupstudents = array())
 {
     $qubaids = quiz_statistics_qubaids_condition($quiz->id, $groupstudents, $whichattempts);
     $this->clear_cached_data($qubaids);
     $questions = $this->load_and_initialise_questions_for_calculations($quiz);
     return $this->get_quiz_and_questions_stats($quiz, $whichattempts, $groupstudents, $questions);
 }
 /**
  * Create a quiz add questions to it, walk through quiz attempts and then check results.
  *
  * @param PHPUnit_Extensions_Database_DataSet_ITable[] of data read from csv file "questionsXX.csv",
  *                                                                                  "stepsXX.csv" and "resultsXX.csv".
  * @dataProvider get_data_for_walkthrough
  */
 public function test_walkthrough_from_csv($quizsettings, $csvdata)
 {
     // CSV data files for these tests were generated using :
     // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
     $this->resetAfterTest(true);
     question_bank::get_qtype('random')->clear_caches_before_testing();
     $this->create_quiz($quizsettings, $csvdata['questions']);
     $attemptids = $this->walkthrough_attempts($csvdata['steps']);
     $this->check_attempts_results($csvdata['results'], $attemptids);
     $this->report = new quiz_statistics_report();
     $whichattempts = QUIZ_GRADEAVERAGE;
     $groupstudents = array();
     $questions = $this->report->load_and_initialise_questions_for_calculations($this->quiz);
     list($quizstats, $questionstats, $subquestionstats) = $this->report->get_quiz_and_questions_stats($this->quiz, $whichattempts, $groupstudents, $questions);
     $qubaids = quiz_statistics_qubaids_condition($this->quiz->id, $groupstudents, $whichattempts);
     // We will create some quiz and question stat calculator instances and some response analyser instances, just in order
     // to check the time of the
     $quizcalc = new quiz_statistics_calculator();
     // Should not be a delay of more than one second between the calculation of stats above and here.
     $this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids));
     $qcalc = new \core_question\statistics\questions\calculator($questions);
     $this->assertTimeCurrent($qcalc->get_last_calculated_time($qubaids));
     foreach ($questions as $question) {
         if (!question_bank::get_qtype($question->qtype, false)->can_analyse_responses()) {
             continue;
         }
         $responesstats = new \core_question\statistics\responses\analyser($question);
         $this->assertTimeCurrent($responesstats->get_last_analysed_time($qubaids));
     }
     // These quiz stats and the question stats found in qstats00.csv were calculated independently in spreadsheet which is
     // available in open document or excel format here :
     // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/statsspreadsheet
     $quizstatsexpected = array('median' => 4.5, 'firstattemptsavg' => 4.617333332, 'allattemptsavg' => 4.617333332, 'firstattemptscount' => 25, 'allattemptscount' => 25, 'standarddeviation' => 0.8117265554, 'skewness' => -0.092502502, 'kurtosis' => -0.7073968557, 'cic' => -87.22309355420001, 'errorratio' => 136.8294900795, 'standarderror' => 1.1106813066);
     foreach ($quizstatsexpected as $statname => $statvalue) {
         $this->assertEquals($statvalue, $quizstats->{$statname}, $quizstats->{$statname}, abs($statvalue) * 1.5E-5);
     }
     for ($rowno = 0; $rowno < $csvdata['qstats']->getRowCount(); $rowno++) {
         $slotqstats = $csvdata['qstats']->getRow($rowno);
         foreach ($slotqstats as $statname => $slotqstat) {
             if ($statname !== 'slot') {
                 switch ($statname) {
                     case 'covariance':
                     case 'discriminationindex':
                     case 'discriminativeefficiency':
                     case 'effectiveweight':
                         $precision = 1.0E-5;
                         break;
                     default:
                         $precision = 1.0E-6;
                 }
                 $slot = $slotqstats['slot'];
                 $delta = abs($slotqstat) * $precision;
                 $actual = $questionstats[$slot]->{$statname};
                 $this->assertEquals(floatval($slotqstat), $actual, "{$statname} for slot {$slot}", $delta);
             }
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * Compute the quiz statistics.
  *
  * @param int   $quizid            the quiz id.
  * @param int $whichattempts which attempts to use, represented internally as one of the constants as used in
  *                                   $quiz->grademethod ie.
  *                                   QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
  *                                   we calculate stats based on which attempts would affect the grade for each student.
  * @param array $groupstudents     students in this group.
  * @param int   $p                 number of positions (slots).
  * @param float $sumofmarkvariance sum of mark variance, calculated as part of question statistics
  * @return quiz_statistics_calculated $quizstats The statistics for overall attempt scores.
  */
 public function calculate($quizid, $whichattempts, $groupstudents, $p, $sumofmarkvariance)
 {
     $quizstats = new quiz_statistics_calculated($whichattempts);
     $countsandaverages = $this->attempt_counts_and_averages($quizid, $groupstudents);
     foreach ($countsandaverages as $propertyname => $value) {
         $quizstats->{$propertyname} = $value;
     }
     $s = $quizstats->s();
     if ($s == 0) {
         return $quizstats;
     }
     // Recalculate sql again this time possibly including test for first attempt.
     list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql($quizid, $groupstudents, $whichattempts);
     $quizstats->median = $this->median($s, $fromqa, $whereqa, $qaparams);
     if ($s > 1) {
         $powers = $this->sum_of_powers_of_difference_to_mean($quizstats->avg(), $fromqa, $whereqa, $qaparams);
         $quizstats->standarddeviation = sqrt($powers->power2 / ($s - 1));
         // Skewness.
         if ($s > 2) {
             // See http://docs.moodle.org/dev/Quiz_item_analysis_calculations_in_practise#Skewness_and_Kurtosis.
             $m2 = $powers->power2 / $s;
             $m3 = $powers->power3 / $s;
             $m4 = $powers->power4 / $s;
             $k2 = $s * $m2 / ($s - 1);
             $k3 = $s * $s * $m3 / (($s - 1) * ($s - 2));
             if ($k2 != 0) {
                 $quizstats->skewness = $k3 / pow($k2, 3 / 2);
                 // Kurtosis.
                 if ($s > 3) {
                     $k4 = $s * $s * (($s + 1) * $m4 - 3 * ($s - 1) * $m2 * $m2) / (($s - 1) * ($s - 2) * ($s - 3));
                     $quizstats->kurtosis = $k4 / ($k2 * $k2);
                 }
                 if ($p > 1) {
                     $quizstats->cic = 100 * $p / ($p - 1) * (1 - $sumofmarkvariance / $k2);
                     $quizstats->errorratio = 100 * sqrt(1 - $quizstats->cic / 100);
                     $quizstats->standarderror = $quizstats->errorratio * $quizstats->standarddeviation / 100;
                 }
             }
         }
     }
     $quizstats->cache(quiz_statistics_qubaids_condition($quizid, $groupstudents, $whichattempts));
     return $quizstats;
 }
Ejemplo n.º 5
0
    protected function analyse_responses($quizstatisticsid, $quizid, $currentgroup,
            $nostudentsingroup, $useallattempts, $groupstudents, $questions, $subquestions) {

        $qubaids = quiz_statistics_qubaids_condition(
                $quizid, $currentgroup, $groupstudents, $useallattempts);

        $done = array();
        foreach ($questions as $question) {
            if (!question_bank::get_qtype($question->qtype, false)->can_analyse_responses()) {
                continue;
            }
            $done[$question->id] = 1;

            $responesstats = new quiz_statistics_response_analyser($question);
            $responesstats->analyse($qubaids);
            $responesstats->store_cached($quizstatisticsid);
        }

        foreach ($subquestions as $question) {
            if (!question_bank::get_qtype($question->qtype, false)->can_analyse_responses() ||
                    isset($done[$question->id])) {
                continue;
            }
            $done[$question->id] = 1;

            $responesstats = new quiz_statistics_response_analyser($question);
            $responesstats->analyse($qubaids);
            $responesstats->store_cached($quizstatisticsid);
        }
    }
Ejemplo n.º 6
0
$modcontext = context_module::instance($cm->id);
require_capability('quiz/statistics:view', $modcontext);
if (groups_get_activity_groupmode($cm)) {
    $groups = groups_get_activity_allowed_groups($cm);
} else {
    $groups = array();
}
if ($currentgroup && !in_array($currentgroup, array_keys($groups))) {
    print_error('groupnotamember', 'group');
}
if (empty($currentgroup)) {
    $groupstudents = array();
} else {
    $groupstudents = get_users_by_capability($modcontext, array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'), '', '', '', '', $currentgroup, '', false);
}
$qubaids = quiz_statistics_qubaids_condition($quizid, $groupstudents, $whichattempts);
// Load the rest of the required data.
$questions = quiz_report_get_significant_questions($quiz);
// Only load main question not sub questions.
$questionstatistics = $DB->get_records_select('question_statistics', 'hashcode = ? AND slot IS NOT NULL', array($qubaids->get_hash_code()));
// Create the graph, and set the basic options.
$graph = new graph(800, 600);
$graph->parameter['title'] = '';
$graph->parameter['y_label_left'] = '%';
$graph->parameter['x_label'] = get_string('position', 'quiz_statistics');
$graph->parameter['y_label_angle'] = 90;
$graph->parameter['x_label_angle'] = 0;
$graph->parameter['x_axis_angle'] = 60;
$graph->parameter['legend'] = 'outside-right';
$graph->parameter['legend_border'] = 'black';
$graph->parameter['legend_offset'] = 4;
Ejemplo n.º 7
0
 /**
  * Get the quiz and question statistics, either by loading the cached results,
  * or by recomputing them.
  *
  * @param object $quiz               the quiz settings.
  * @param string $whichattempts      which attempts to use, represented internally as one of the constants as used in
  *                                   $quiz->grademethod ie.
  *                                   QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
  *                                   we calculate stats based on which attempts would affect the grade for each student.
  * @param string $whichtries         which tries to analyse for response analysis. Will be one of
  *                                   question_attempt::FIRST_TRY, LAST_TRY or ALL_TRIES.
  * @param array  $groupstudents      students in this group.
  * @param array  $questions          full question data.
  * @param \core\progress\base|null   $progress
  * @return array with 2 elements:    - $quizstats The statistics for overall attempt scores.
  *                                   - $questionstats \core_question\statistics\questions\all_calculated_for_qubaid_condition
  */
 public function get_all_stats_and_analysis($quiz, $whichattempts, $whichtries, $groupstudents, $questions, $progress = null)
 {
     if ($progress === null) {
         $progress = new \core\progress\none();
     }
     $qubaids = quiz_statistics_qubaids_condition($quiz->id, $groupstudents, $whichattempts);
     $qcalc = new \core_question\statistics\questions\calculator($questions, $progress);
     $quizcalc = new \quiz_statistics\calculator($progress);
     $progress->start_progress('', 3);
     if ($quizcalc->get_last_calculated_time($qubaids) === false) {
         // Recalculate now.
         $questionstats = $qcalc->calculate($qubaids);
         $progress->progress(1);
         $quizstats = $quizcalc->calculate($quiz->id, $whichattempts, $groupstudents, count($questions), $qcalc->get_sum_of_mark_variance());
         $progress->progress(2);
     } else {
         $quizstats = $quizcalc->get_cached($qubaids);
         $progress->progress(1);
         $questionstats = $qcalc->get_cached($qubaids);
         $progress->progress(2);
     }
     if ($quizstats->s()) {
         $subquestions = $questionstats->get_sub_questions();
         $this->analyse_responses_for_all_questions_and_subquestions($questions, $subquestions, $qubaids, $whichtries, $progress);
     }
     $progress->progress(3);
     $progress->end_progress();
     return array($quizstats, $questionstats);
 }
Ejemplo n.º 8
0
 /**
  * Get the quiz and question statistics, either by loading the cached results,
  * or by recomputing them.
  *
  * @param object $quiz the quiz settings.
  * @param string $whichattempts which attempts to use, represented internally as one of the constants as used in
  *                                   $quiz->grademethod ie.
  *                                   QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
  *                                   we calculate stats based on which attempts would affect the grade for each student.
  * @param array $groupstudents students in this group.
  * @param array $questions full question data.
  * @return array with 4 elements:
  *     - $quizstats The statistics for overall attempt scores.
  *     - $questionstats array of \core_question\statistics\questions\calculated objects keyed by slot.
  *     - $subquestionstats array of \core_question\statistics\questions\calculated_for_subquestion objects keyed by question id.
  */
 public function get_quiz_and_questions_stats($quiz, $whichattempts, $groupstudents, $questions)
 {
     $qubaids = quiz_statistics_qubaids_condition($quiz->id, $groupstudents, $whichattempts);
     $qcalc = new \core_question\statistics\questions\calculator($questions);
     $quizcalc = new quiz_statistics_calculator();
     if ($quizcalc->get_last_calculated_time($qubaids) === false) {
         // Recalculate now.
         list($questionstats, $subquestionstats) = $qcalc->calculate($qubaids);
         $quizstats = $quizcalc->calculate($quiz->id, $whichattempts, $groupstudents, count($questions), $qcalc->get_sum_of_mark_variance());
         if ($quizstats->s()) {
             $this->analyse_responses_for_all_questions_and_subquestions($qubaids, $questions, $subquestionstats);
         }
     } else {
         $quizstats = $quizcalc->get_cached($qubaids);
         list($questionstats, $subquestionstats) = $qcalc->get_cached($qubaids);
     }
     return array($quizstats, $questionstats, $subquestionstats);
 }
 /**
  * Check the question stats and the response counts used in the statistics report. If the appropriate files exist in fixtures/.
  *
  * @param PHPUnit_Extensions_Database_DataSet_ITable[] $csvdata Data loaded from csv files for this test.
  * @param string $whichattempts
  * @param string $whichtries
  * @param \core\dml\sql_join $groupstudentsjoins
  * @return array with contents 0 => $questions, 1 => $quizstats, 2=> $questionstats, 3=> $qubaids Might be needed for further
  *               testing.
  */
 protected function check_stats_calculations_and_response_analysis($csvdata, $whichattempts, $whichtries, \core\dml\sql_join $groupstudentsjoins)
 {
     $this->report = new quiz_statistics_report();
     $questions = $this->report->load_and_initialise_questions_for_calculations($this->quiz);
     list($quizstats, $questionstats) = $this->report->get_all_stats_and_analysis($this->quiz, $whichattempts, $whichtries, $groupstudentsjoins, $questions);
     $qubaids = quiz_statistics_qubaids_condition($this->quiz->id, $groupstudentsjoins, $whichattempts);
     // We will create some quiz and question stat calculator instances and some response analyser instances, just in order
     // to check the last analysed time then returned.
     $quizcalc = new \quiz_statistics\calculator();
     // Should not be a delay of more than one second between the calculation of stats above and here.
     $this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids));
     $qcalc = new \core_question\statistics\questions\calculator($questions);
     $this->assertTimeCurrent($qcalc->get_last_calculated_time($qubaids));
     if (isset($csvdata['responsecounts'])) {
         $this->check_response_counts($csvdata['responsecounts'], $qubaids, $questions, $whichtries);
     }
     if (isset($csvdata['qstats'])) {
         $this->check_question_stats($csvdata['qstats'], $questionstats);
         return array($questions, $quizstats, $questionstats, $qubaids);
     }
     return array($questions, $quizstats, $questionstats, $qubaids);
 }