/**
  * Load the data that will be needed to perform the calculations.
  *
  * @param int $offlinequizid the offlinequiz id.
  * @param int $currentgroup the current group. 0 for none.
  * @param array $groupstudents students in this group.
  * @param bool $allattempts use all attempts, or just first attempts.
  */
 public function load_step_data($offlinequizid, $currentgroup, $groupstudents, $allattempts, $offlinegroupid)
 {
     global $DB;
     $this->allattempts = $allattempts;
     $questionids = array();
     foreach ($this->questions as $question) {
         $questionids[] = $question->id;
     }
     // Get the SQL and params for question IDs.
     list($qsql, $qparams) = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED, 'q');
     list($fromqa, $whereqa, $qaparams) = offlinequiz_statistics_attempts_sql($offlinequizid, $currentgroup, $groupstudents, $allattempts, false, $offlinegroupid);
     // Trying to make this work on MySQL
     // First we get the questionattemptids that we actually need.
     $questionattemptids = $DB->get_fieldset_sql("\n                SELECT qa.id\n                  FROM {$fromqa}\n                  JOIN {question_attempts} qa ON qa.questionusageid = offlinequiza.usageid\n                 WHERE qa.questionid {$qsql}\n                   AND {$whereqa}", $qparams + $qaparams);
     list($qaidssql, $qaidsparams) = $DB->get_in_or_equal($questionattemptids, SQL_PARAMS_NAMED, 'qaid');
     $params = array_merge($qparams, $qaparams, $qaidsparams);
     // Works already quite a bit faster.
     $this->lateststeps = $DB->get_records_sql("\n                SELECT\n                    qas.id,\n                    offlinequiza.sumgrades,\n                    qa.questionid,\n                    qa.slot,\n                    qa.maxmark,\n                    qas.fraction * qa.maxmark as mark\n\n                FROM {$fromqa}\n                JOIN {question_attempts} qa ON qa.questionusageid = offlinequiza.usageid\n                JOIN (\n                      SELECT questionattemptid, MAX(id) AS latestid\n                        FROM {question_attempt_steps} qass\n                       WHERE qass.questionattemptid {$qaidssql}\n                    GROUP BY questionattemptid\n                ) lateststepid ON lateststepid.questionattemptid = qa.id\n                JOIN {question_attempt_steps} qas ON qas.id = lateststepid.latestid\n\n                WHERE\n                    qa.questionid {$qsql} AND\n                    {$whereqa}", $params);
 }
 /**
  * Generate the snipped of HTML that says when the stats were last caculated,
  * with a recalcuate now button.
  * @param object $offlinequizstats the overall offlinequiz statistics.
  * @param int $offlinequizid the offlinequiz id.
  * @param int $currentgroup the id of the currently selected group, or 0.
  * @param array $groupstudents ids of students in the group.
  * @param bool $useallattempts whether to use all attempts, instead of just
  *      first attempts.
  * @return string a HTML snipped saying when the stats were last computed,
  *      or blank if that is not appropriate.
  */
 protected function output_caching_info($offlinequizstats, $offlinequizid, $currentgroup, $groupstudents, $useallattempts, $reporturl, $offlinegroupid)
 {
     global $DB, $OUTPUT;
     if (empty($offlinequizstats->timemodified)) {
         return '';
     }
     // Find the number of attempts since the cached statistics were computed.
     list($fromqa, $whereqa, $qaparams) = offlinequiz_statistics_attempts_sql($offlinequizid, $currentgroup, $groupstudents, $useallattempts, true, false, $offlinegroupid);
     $count = $DB->count_records_sql("\n                SELECT COUNT(1)\n                FROM {$fromqa}\n                WHERE {$whereqa}\n                AND offlinequiza.timefinish > {$offlinequizstats->timemodified}", $qaparams);
     if (!$count) {
         $count = 0;
     }
     // Generate the output.
     $a = new stdClass();
     $a->lastcalculated = format_time(time() - $offlinequizstats->timemodified);
     $a->count = $count;
     $recalcualteurl = new moodle_url($reporturl, array('recalculate' => 1, 'sesskey' => sesskey()));
     $output = '<br/>';
     $output .= $OUTPUT->box_start('boxaligncenter generalbox boxwidthnormal mdl-align', 'cachingnotice');
     $output .= get_string('lastcalculated', 'offlinequiz_statistics', $a);
     $output .= $OUTPUT->single_button($recalcualteurl, get_string('recalculatenow', 'offlinequiz_statistics'));
     $output .= $OUTPUT->box_end(true);
     return $output;
 }