From f5e42695bdc81d7efeb7e6680573ba600148327f Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 3 Apr 2012 17:57:25 +0100 Subject: [PATCH] MDL-32322 quiz reports: split attemptsrepor.php into one file per class. --- mod/quiz/report/attemptsreport.php | 471 +---------------- mod/quiz/report/attemptsreport_table.php | 484 ++++++++++++++++++ mod/quiz/report/overview/overview_table.php | 2 + mod/quiz/report/responses/responses_table.php | 2 + 4 files changed, 495 insertions(+), 464 deletions(-) create mode 100755 mod/quiz/report/attemptsreport_table.php diff --git a/mod/quiz/report/attemptsreport.php b/mod/quiz/report/attemptsreport.php index 2df3c791262..8a7704a22c4 100644 --- a/mod/quiz/report/attemptsreport.php +++ b/mod/quiz/report/attemptsreport.php @@ -15,14 +15,12 @@ // along with Moodle. If not, see . /** - * The file defines some subclasses that can be used when you are building - * a report like the overview or responses report, that basically has one - * row per attempt. + * The file defines a base class that can be used to build a report like the + * overview or responses report, that has one row per attempt. * - * @package mod - * @subpackage quiz - * @copyright 2010 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package mod_quiz + * @copyright 2010 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -34,8 +32,8 @@ require_once($CFG->libdir.'/tablelib.php'); /** * Base class for quiz reports that are basically a table with one row for each attempt. * - * @copyright 2010 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @copyright 2010 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ abstract class quiz_attempt_report extends quiz_default_report { /** @var object the quiz context. */ @@ -288,458 +286,3 @@ abstract class quiz_attempt_report extends quiz_default_report { } } } - -/** - * Base class for the table used by {@link quiz_attempt_report}s. - * - * @copyright 2010 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -abstract class quiz_attempt_report_table extends table_sql { - public $useridfield = 'userid'; - - /** @var moodle_url the URL of this report. */ - protected $reporturl; - - /** @var array the display options. */ - protected $displayoptions; - - /** - * @var array information about the latest step of each question. - * Loaded by {@link load_question_latest_steps()}, if applicable. - */ - protected $lateststeps = null; - - protected $quiz; - protected $context; - protected $qmsubselect; - protected $qmfilter; - protected $attemptsmode; - protected $groupstudents; - protected $students; - protected $questions; - protected $includecheckboxes; - - public function __construct($uniqueid, $quiz, $context, $qmsubselect, $qmfilter, - $attemptsmode, $groupstudents, $students, $questions, $includecheckboxes, - $reporturl, $displayoptions) { - parent::__construct($uniqueid); - $this->quiz = $quiz; - $this->context = $context; - $this->qmsubselect = $qmsubselect; - $this->qmfilter = $qmfilter; - $this->attemptsmode = $attemptsmode; - $this->groupstudents = $groupstudents; - $this->students = $students; - $this->questions = $questions; - $this->includecheckboxes = $includecheckboxes; - $this->reporturl = $reporturl; - $this->displayoptions = $displayoptions; - } - - public function col_checkbox($attempt) { - if ($attempt->attempt) { - return ''; - } else { - return ''; - } - } - - public function col_picture($attempt) { - global $OUTPUT; - $user = new stdClass(); - $user->id = $attempt->userid; - $user->lastname = $attempt->lastname; - $user->firstname = $attempt->firstname; - $user->imagealt = $attempt->imagealt; - $user->picture = $attempt->picture; - $user->email = $attempt->email; - return $OUTPUT->user_picture($user); - } - - public function col_fullname($attempt) { - $html = parent::col_fullname($attempt); - if ($this->is_downloading()) { - return $html; - } - - return $html . html_writer::empty_tag('br') . html_writer::link( - new moodle_url('/mod/quiz/review.php', array('attempt' => $attempt->attempt)), - get_string('reviewattempt', 'quiz'), array('class' => 'reviewlink')); - } - - public function col_timestart($attempt) { - if ($attempt->attempt) { - return userdate($attempt->timestart, $this->strtimeformat); - } else { - return '-'; - } - } - - public function col_timefinish($attempt) { - if ($attempt->attempt && $attempt->timefinish) { - return userdate($attempt->timefinish, $this->strtimeformat); - } else { - return '-'; - } - } - - public function col_duration($attempt) { - if ($attempt->timefinish) { - return format_time($attempt->timefinish - $attempt->timestart); - } else if ($attempt->timestart) { - return get_string('unfinished', 'quiz'); - } else { - return '-'; - } - } - - public function col_feedbacktext($attempt) { - if (!$attempt->timefinish) { - return '-'; - } - - $feedback = quiz_report_feedback_for_grade( - quiz_rescale_grade($attempt->sumgrades, $this->quiz, false), - $this->quiz->id, $this->context); - - if ($this->is_downloading()) { - $feedback = strip_tags($feedback); - } - - return $feedback; - } - - public function get_row_class($attempt) { - if ($this->qmsubselect && $attempt->gradedattempt) { - return 'gradedattempt'; - } else { - return ''; - } - } - - /** - * Make a link to review an individual question in a popup window. - * - * @param string $data HTML fragment. The text to make into the link. - * @param object $attempt data for the row of the table being output. - * @param int $slot the number used to identify this question within this usage. - */ - public function make_review_link($data, $attempt, $slot) { - global $OUTPUT; - - $stepdata = $this->lateststeps[$attempt->usageid][$slot]; - $state = question_state::get($stepdata->state); - - $flag = ''; - if ($stepdata->flagged) { - $flag = ' ' . $OUTPUT->pix_icon('i/flagged', get_string('flagged', 'question'), - 'moodle', array('class' => 'questionflag')); - } - - $feedbackimg = ''; - if ($state->is_finished() && $state != question_state::$needsgrading) { - $feedbackimg = ' ' . $this->icon_for_fraction($stepdata->fraction); - } - - $output = html_writer::tag('span', html_writer::tag('span', - $data . $feedbackimg . $flag, - array('class' => $state->get_state_class(true))), array('class' => 'que')); - - $url = new moodle_url('/mod/quiz/reviewquestion.php', - array('attempt' => $attempt->attempt, 'slot' => $slot)); - $output = $OUTPUT->action_link($url, $output, - new popup_action('click', $url, 'reviewquestion', - array('height' => 450, 'width' => 650)), - array('title' => get_string('reviewresponse', 'quiz'))); - - return $output; - } - - /** - * Return an appropriate icon (green tick, red cross, etc.) for a grade. - * @param float $fraction grade on a scale 0..1. - * @return string html fragment. - */ - protected function icon_for_fraction($fraction) { - global $OUTPUT; - - $state = question_state::graded_state_for_fraction($fraction); - if ($state == question_state::$gradedright) { - $icon = 'i/tick_green_big'; - } else if ($state == question_state::$gradedpartial) { - $icon = 'i/tick_amber_big'; - } else { - $icon = 'i/cross_red_big'; - } - - return $OUTPUT->pix_icon($icon, get_string($state->get_feedback_class(), 'question'), - 'moodle', array('class' => 'icon')); - } - - /** - * Load information about the latest state of selected questions in selected attempts. - * - * The results are returned as an two dimensional array $qubaid => $slot => $dataobject - * - * @param qubaid_condition $qubaids used to restrict which usages are included - * in the query. See {@link qubaid_condition}. - * @param array $slots A list of slots for the questions you want to konw about. - * @return array of records. See the SQL in this function to see the fields available. - */ - protected function load_question_latest_steps(qubaid_condition $qubaids) { - $dm = new question_engine_data_mapper(); - $latesstepdata = $dm->load_questions_usages_latest_steps( - $qubaids, array_keys($this->questions)); - - $lateststeps = array(); - foreach ($latesstepdata as $step) { - $lateststeps[$step->questionusageid][$step->slot] = $step; - } - - return $lateststeps; - } - - /** - * @return bool should {@link query_db()} call {@link load_question_latest_steps}? - */ - protected function requires_latest_steps_loaded() { - return false; - } - - /** - * Is this a column that depends on joining to the latest state information? - * If so, return the corresponding slot. If not, return false. - * @param string $column a column name - * @return int false if no, else a slot. - */ - protected function is_latest_step_column($column) { - return false; - } - - /** - * Get any fields that might be needed when sorting on date for a particular slot. - * @param int $slot the slot for the column we want. - * @param string $alias the table alias for latest state information relating to that slot. - */ - protected function get_required_latest_state_fields($slot, $alias) { - return ''; - } - - /** - * Contruct all the parts of the main database query. - * @param array $reportstudents list if userids of users to include in the report. - * @return array with 4 elements ($fields, $from, $where, $params) that can be used to - * build the actual database query. - */ - public function base_sql($reportstudents) { - global $DB; - - $fields = $DB->sql_concat('u.id', "'#'", 'COALESCE(quiza.attempt, 0)') . ' AS uniqueid,'; - - if ($this->qmsubselect) { - $fields .= "\n(CASE WHEN $this->qmsubselect THEN 1 ELSE 0 END) AS gradedattempt,"; - } - - $extrafields = get_extra_user_fields_sql($this->context, 'u', '', - array('id', 'idnumber', 'firstname', 'lastname', 'picture', - 'imagealt', 'institution', 'department', 'email')); - $fields .= ' - quiza.uniqueid AS usageid, - quiza.id AS attempt, - u.id AS userid, - u.idnumber, - u.firstname, - u.lastname, - u.picture, - u.imagealt, - u.institution, - u.department, - u.email' . $extrafields . ', - quiza.sumgrades, - quiza.timefinish, - quiza.timestart, - CASE WHEN quiza.timefinish = 0 THEN null - WHEN quiza.timefinish > quiza.timestart THEN quiza.timefinish - quiza.timestart - ELSE 0 END AS duration'; - // To explain that last bit, in MySQL, qa.timestart and qa.timefinish - // are unsigned. Since MySQL 5.5.5, when they introduced strict mode, - // subtracting a larger unsigned int from a smaller one gave an error. - // Therefore, we avoid doing that. timefinish can be non-zero and less - // than timestart when you have two load-balanced servers with very - // badly synchronised clocks, and a student does a really quick attempt.'; - - // This part is the same for all cases - join users and quiz_attempts tables - $from = "\n{user} u"; - $from .= "\nLEFT JOIN {quiz_attempts} quiza ON - quiza.userid = u.id AND quiza.quiz = :quizid"; - $params = array('quizid' => $this->quiz->id); - - if ($this->qmsubselect && $this->qmfilter) { - $from .= " AND $this->qmsubselect"; - } - switch ($this->attemptsmode) { - case QUIZ_REPORT_ATTEMPTS_ALL: - // Show all attempts, including students who are no longer in the course - $where = 'quiza.id IS NOT NULL AND quiza.preview = 0'; - break; - case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH: - // Show only students with attempts - list($usql, $uparams) = $DB->get_in_or_equal( - $reportstudents, SQL_PARAMS_NAMED, 'u'); - $params += $uparams; - $where = "u.id $usql AND quiza.preview = 0 AND quiza.id IS NOT NULL"; - break; - case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH_NO: - // Show only students without attempts - list($usql, $uparams) = $DB->get_in_or_equal( - $reportstudents, SQL_PARAMS_NAMED, 'u'); - $params += $uparams; - $where = "u.id $usql AND quiza.id IS NULL"; - break; - case QUIZ_REPORT_ATTEMPTS_ALL_STUDENTS: - // Show all students with or without attempts - list($usql, $uparams) = $DB->get_in_or_equal( - $reportstudents, SQL_PARAMS_NAMED, 'u'); - $params += $uparams; - $where = "u.id $usql AND (quiza.preview = 0 OR quiza.preview IS NULL)"; - break; - } - - return array($fields, $from, $where, $params); - } - - /** - * Add the information about the latest state of the question with slot - * $slot to the query. - * - * The extra information is added as a join to a - * 'table' with alias qa$slot, with columns that are a union of - * the columns of the question_attempts and question_attempts_states tables. - * - * @param int $slot the question to add information for. - */ - protected function add_latest_state_join($slot) { - $alias = 'qa' . $slot; - - $fields = $this->get_required_latest_state_fields($slot, $alias); - if (!$fields) { - return; - } - - // This condition roughly filters the list of attempts to be considered. - // It is only used in a subselect to help crappy databases (see MDL-30122) - // therefore, it is better to use a very simple join, which may include - // too many records, than to do a super-accurate join. - $qubaids = new qubaid_join("{quiz_attempts} {$alias}quiza", "{$alias}quiza.uniqueid", - "{$alias}quiza.quiz = :{$alias}quizid", array("{$alias}quizid" => $this->sql->params['quizid'])); - - $dm = new question_engine_data_mapper(); - list($inlineview, $viewparams) = $dm->question_attempt_latest_state_view($alias, $qubaids); - - $this->sql->fields .= ",\n$fields"; - $this->sql->from .= "\nLEFT JOIN $inlineview ON " . - "$alias.questionusageid = quiza.uniqueid AND $alias.slot = :{$alias}slot"; - $this->sql->params[$alias . 'slot'] = $slot; - $this->sql->params = array_merge($this->sql->params, $viewparams); - } - - /** - * Get an appropriate qubaid_condition for loading more data about the - * attempts we are displaying. - * @return qubaid_condition - */ - protected function get_qubaids_condition() { - if (is_null($this->rawdata)) { - throw new coding_exception( - 'Cannot call get_qubaids_condition until the main data has been loaded.'); - } - - if ($this->is_downloading()) { - // We want usages for all attempts. - return new qubaid_join($this->sql->from, 'quiza.uniqueid', - $this->sql->where, $this->sql->params); - } - - $qubaids = array(); - foreach ($this->rawdata as $attempt) { - if ($attempt->usageid > 0) { - $qubaids[] = $attempt->usageid; - } - } - - return new qubaid_list($qubaids); - } - - public function query_db($pagesize, $useinitialsbar = true) { - $doneslots = array(); - foreach ($this->get_sort_columns() as $column => $notused) { - $slot = $this->is_latest_step_column($column); - if ($slot && !in_array($slot, $doneslots)) { - $this->add_latest_state_join($slot); - $doneslots[] = $slot; - } - } - - parent::query_db($pagesize, $useinitialsbar); - - if ($this->requires_latest_steps_loaded()) { - $qubaids = $this->get_qubaids_condition(); - $this->lateststeps = $this->load_question_latest_steps($qubaids); - } - } - - public function get_sort_columns() { - // Add attemptid as a final tie-break to the sort. This ensures that - // Attempts by the same student appear in order when just sorting by name. - $sortcolumns = parent::get_sort_columns(); - $sortcolumns['quiza.id'] = SORT_ASC; - return $sortcolumns; - } - - public function wrap_html_start() { - if ($this->is_downloading() || !$this->includecheckboxes) { - return; - } - - $url = new moodle_url($this->reporturl, $this->displayoptions); - $url->param('sesskey', sesskey()); - - echo '
'; - echo '
'; - - echo html_writer::input_hidden_params($url); - echo '
'; - } - - public function wrap_html_finish() { - if ($this->is_downloading() || !$this->includecheckboxes) { - return; - } - - echo '
'; - echo '' . - get_string('selectall', 'quiz') . ' / '; - echo '' . - get_string('selectnone', 'quiz') . ' '; - echo '  '; - $this->submit_buttons(); - echo '
'; - // Close form - echo '
'; - echo '
'; - } - - /** - * Output any submit buttons required by the $this->includecheckboxes form. - */ - protected function submit_buttons() { - global $PAGE; - if (has_capability('mod/quiz:deleteattempts', $this->context)) { - echo ''; - $PAGE->requires->event_handler('#deleteattemptsbutton', 'click', 'M.util.show_confirm_dialog', - array('message' => get_string('deleteattemptcheck', 'quiz'))); - } - } -} diff --git a/mod/quiz/report/attemptsreport_table.php b/mod/quiz/report/attemptsreport_table.php new file mode 100755 index 00000000000..4f5dde96cc4 --- /dev/null +++ b/mod/quiz/report/attemptsreport_table.php @@ -0,0 +1,484 @@ +. + +/** + * Base class for the table used by a {@link quiz_attempt_report}. + * + * @package mod_quiz + * @copyright 2010 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir.'/tablelib.php'); + + +/** + * Base class for the table used by a {@link quiz_attempt_report}. + * + * @copyright 2010 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class quiz_attempt_report_table extends table_sql { + public $useridfield = 'userid'; + + /** @var moodle_url the URL of this report. */ + protected $reporturl; + + /** @var array the display options. */ + protected $displayoptions; + + /** + * @var array information about the latest step of each question. + * Loaded by {@link load_question_latest_steps()}, if applicable. + */ + protected $lateststeps = null; + + protected $quiz; + protected $context; + protected $qmsubselect; + protected $qmfilter; + protected $attemptsmode; + protected $groupstudents; + protected $students; + protected $questions; + protected $includecheckboxes; + + public function __construct($uniqueid, $quiz, $context, $qmsubselect, $qmfilter, + $attemptsmode, $groupstudents, $students, $questions, $includecheckboxes, + $reporturl, $displayoptions) { + parent::__construct($uniqueid); + $this->quiz = $quiz; + $this->context = $context; + $this->qmsubselect = $qmsubselect; + $this->qmfilter = $qmfilter; + $this->attemptsmode = $attemptsmode; + $this->groupstudents = $groupstudents; + $this->students = $students; + $this->questions = $questions; + $this->includecheckboxes = $includecheckboxes; + $this->reporturl = $reporturl; + $this->displayoptions = $displayoptions; + } + + public function col_checkbox($attempt) { + if ($attempt->attempt) { + return ''; + } else { + return ''; + } + } + + public function col_picture($attempt) { + global $OUTPUT; + $user = new stdClass(); + $user->id = $attempt->userid; + $user->lastname = $attempt->lastname; + $user->firstname = $attempt->firstname; + $user->imagealt = $attempt->imagealt; + $user->picture = $attempt->picture; + $user->email = $attempt->email; + return $OUTPUT->user_picture($user); + } + + public function col_fullname($attempt) { + $html = parent::col_fullname($attempt); + if ($this->is_downloading()) { + return $html; + } + + return $html . html_writer::empty_tag('br') . html_writer::link( + new moodle_url('/mod/quiz/review.php', array('attempt' => $attempt->attempt)), + get_string('reviewattempt', 'quiz'), array('class' => 'reviewlink')); + } + + public function col_timestart($attempt) { + if ($attempt->attempt) { + return userdate($attempt->timestart, $this->strtimeformat); + } else { + return '-'; + } + } + + public function col_timefinish($attempt) { + if ($attempt->attempt && $attempt->timefinish) { + return userdate($attempt->timefinish, $this->strtimeformat); + } else { + return '-'; + } + } + + public function col_duration($attempt) { + if ($attempt->timefinish) { + return format_time($attempt->timefinish - $attempt->timestart); + } else if ($attempt->timestart) { + return get_string('unfinished', 'quiz'); + } else { + return '-'; + } + } + + public function col_feedbacktext($attempt) { + if (!$attempt->timefinish) { + return '-'; + } + + $feedback = quiz_report_feedback_for_grade( + quiz_rescale_grade($attempt->sumgrades, $this->quiz, false), + $this->quiz->id, $this->context); + + if ($this->is_downloading()) { + $feedback = strip_tags($feedback); + } + + return $feedback; + } + + public function get_row_class($attempt) { + if ($this->qmsubselect && $attempt->gradedattempt) { + return 'gradedattempt'; + } else { + return ''; + } + } + + /** + * Make a link to review an individual question in a popup window. + * + * @param string $data HTML fragment. The text to make into the link. + * @param object $attempt data for the row of the table being output. + * @param int $slot the number used to identify this question within this usage. + */ + public function make_review_link($data, $attempt, $slot) { + global $OUTPUT; + + $stepdata = $this->lateststeps[$attempt->usageid][$slot]; + $state = question_state::get($stepdata->state); + + $flag = ''; + if ($stepdata->flagged) { + $flag = ' ' . $OUTPUT->pix_icon('i/flagged', get_string('flagged', 'question'), + 'moodle', array('class' => 'questionflag')); + } + + $feedbackimg = ''; + if ($state->is_finished() && $state != question_state::$needsgrading) { + $feedbackimg = ' ' . $this->icon_for_fraction($stepdata->fraction); + } + + $output = html_writer::tag('span', html_writer::tag('span', + $data . $feedbackimg . $flag, + array('class' => $state->get_state_class(true))), array('class' => 'que')); + + $url = new moodle_url('/mod/quiz/reviewquestion.php', + array('attempt' => $attempt->attempt, 'slot' => $slot)); + $output = $OUTPUT->action_link($url, $output, + new popup_action('click', $url, 'reviewquestion', + array('height' => 450, 'width' => 650)), + array('title' => get_string('reviewresponse', 'quiz'))); + + return $output; + } + + /** + * Return an appropriate icon (green tick, red cross, etc.) for a grade. + * @param float $fraction grade on a scale 0..1. + * @return string html fragment. + */ + protected function icon_for_fraction($fraction) { + global $OUTPUT; + + $state = question_state::graded_state_for_fraction($fraction); + if ($state == question_state::$gradedright) { + $icon = 'i/tick_green_big'; + } else if ($state == question_state::$gradedpartial) { + $icon = 'i/tick_amber_big'; + } else { + $icon = 'i/cross_red_big'; + } + + return $OUTPUT->pix_icon($icon, get_string($state->get_feedback_class(), 'question'), + 'moodle', array('class' => 'icon')); + } + + /** + * Load information about the latest state of selected questions in selected attempts. + * + * The results are returned as an two dimensional array $qubaid => $slot => $dataobject + * + * @param qubaid_condition $qubaids used to restrict which usages are included + * in the query. See {@link qubaid_condition}. + * @param array $slots A list of slots for the questions you want to konw about. + * @return array of records. See the SQL in this function to see the fields available. + */ + protected function load_question_latest_steps(qubaid_condition $qubaids) { + $dm = new question_engine_data_mapper(); + $latesstepdata = $dm->load_questions_usages_latest_steps( + $qubaids, array_keys($this->questions)); + + $lateststeps = array(); + foreach ($latesstepdata as $step) { + $lateststeps[$step->questionusageid][$step->slot] = $step; + } + + return $lateststeps; + } + + /** + * @return bool should {@link query_db()} call {@link load_question_latest_steps}? + */ + protected function requires_latest_steps_loaded() { + return false; + } + + /** + * Is this a column that depends on joining to the latest state information? + * If so, return the corresponding slot. If not, return false. + * @param string $column a column name + * @return int false if no, else a slot. + */ + protected function is_latest_step_column($column) { + return false; + } + + /** + * Get any fields that might be needed when sorting on date for a particular slot. + * @param int $slot the slot for the column we want. + * @param string $alias the table alias for latest state information relating to that slot. + */ + protected function get_required_latest_state_fields($slot, $alias) { + return ''; + } + + /** + * Contruct all the parts of the main database query. + * @param array $reportstudents list if userids of users to include in the report. + * @return array with 4 elements ($fields, $from, $where, $params) that can be used to + * build the actual database query. + */ + public function base_sql($reportstudents) { + global $DB; + + $fields = $DB->sql_concat('u.id', "'#'", 'COALESCE(quiza.attempt, 0)') . ' AS uniqueid,'; + + if ($this->qmsubselect) { + $fields .= "\n(CASE WHEN $this->qmsubselect THEN 1 ELSE 0 END) AS gradedattempt,"; + } + + $extrafields = get_extra_user_fields_sql($this->context, 'u', '', + array('id', 'idnumber', 'firstname', 'lastname', 'picture', + 'imagealt', 'institution', 'department', 'email')); + $fields .= ' + quiza.uniqueid AS usageid, + quiza.id AS attempt, + u.id AS userid, + u.idnumber, + u.firstname, + u.lastname, + u.picture, + u.imagealt, + u.institution, + u.department, + u.email' . $extrafields . ', + quiza.sumgrades, + quiza.timefinish, + quiza.timestart, + CASE WHEN quiza.timefinish = 0 THEN null + WHEN quiza.timefinish > quiza.timestart THEN quiza.timefinish - quiza.timestart + ELSE 0 END AS duration'; + // To explain that last bit, in MySQL, qa.timestart and qa.timefinish + // are unsigned. Since MySQL 5.5.5, when they introduced strict mode, + // subtracting a larger unsigned int from a smaller one gave an error. + // Therefore, we avoid doing that. timefinish can be non-zero and less + // than timestart when you have two load-balanced servers with very + // badly synchronised clocks, and a student does a really quick attempt.'; + + // This part is the same for all cases - join users and quiz_attempts tables + $from = "\n{user} u"; + $from .= "\nLEFT JOIN {quiz_attempts} quiza ON + quiza.userid = u.id AND quiza.quiz = :quizid"; + $params = array('quizid' => $this->quiz->id); + + if ($this->qmsubselect && $this->qmfilter) { + $from .= " AND $this->qmsubselect"; + } + switch ($this->attemptsmode) { + case QUIZ_REPORT_ATTEMPTS_ALL: + // Show all attempts, including students who are no longer in the course + $where = 'quiza.id IS NOT NULL AND quiza.preview = 0'; + break; + case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH: + // Show only students with attempts + list($usql, $uparams) = $DB->get_in_or_equal( + $reportstudents, SQL_PARAMS_NAMED, 'u'); + $params += $uparams; + $where = "u.id $usql AND quiza.preview = 0 AND quiza.id IS NOT NULL"; + break; + case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH_NO: + // Show only students without attempts + list($usql, $uparams) = $DB->get_in_or_equal( + $reportstudents, SQL_PARAMS_NAMED, 'u'); + $params += $uparams; + $where = "u.id $usql AND quiza.id IS NULL"; + break; + case QUIZ_REPORT_ATTEMPTS_ALL_STUDENTS: + // Show all students with or without attempts + list($usql, $uparams) = $DB->get_in_or_equal( + $reportstudents, SQL_PARAMS_NAMED, 'u'); + $params += $uparams; + $where = "u.id $usql AND (quiza.preview = 0 OR quiza.preview IS NULL)"; + break; + } + + return array($fields, $from, $where, $params); + } + + /** + * Add the information about the latest state of the question with slot + * $slot to the query. + * + * The extra information is added as a join to a + * 'table' with alias qa$slot, with columns that are a union of + * the columns of the question_attempts and question_attempts_states tables. + * + * @param int $slot the question to add information for. + */ + protected function add_latest_state_join($slot) { + $alias = 'qa' . $slot; + + $fields = $this->get_required_latest_state_fields($slot, $alias); + if (!$fields) { + return; + } + + // This condition roughly filters the list of attempts to be considered. + // It is only used in a subselect to help crappy databases (see MDL-30122) + // therefore, it is better to use a very simple join, which may include + // too many records, than to do a super-accurate join. + $qubaids = new qubaid_join("{quiz_attempts} {$alias}quiza", "{$alias}quiza.uniqueid", + "{$alias}quiza.quiz = :{$alias}quizid", array("{$alias}quizid" => $this->sql->params['quizid'])); + + $dm = new question_engine_data_mapper(); + list($inlineview, $viewparams) = $dm->question_attempt_latest_state_view($alias, $qubaids); + + $this->sql->fields .= ",\n$fields"; + $this->sql->from .= "\nLEFT JOIN $inlineview ON " . + "$alias.questionusageid = quiza.uniqueid AND $alias.slot = :{$alias}slot"; + $this->sql->params[$alias . 'slot'] = $slot; + $this->sql->params = array_merge($this->sql->params, $viewparams); + } + + /** + * Get an appropriate qubaid_condition for loading more data about the + * attempts we are displaying. + * @return qubaid_condition + */ + protected function get_qubaids_condition() { + if (is_null($this->rawdata)) { + throw new coding_exception( + 'Cannot call get_qubaids_condition until the main data has been loaded.'); + } + + if ($this->is_downloading()) { + // We want usages for all attempts. + return new qubaid_join($this->sql->from, 'quiza.uniqueid', + $this->sql->where, $this->sql->params); + } + + $qubaids = array(); + foreach ($this->rawdata as $attempt) { + if ($attempt->usageid > 0) { + $qubaids[] = $attempt->usageid; + } + } + + return new qubaid_list($qubaids); + } + + public function query_db($pagesize, $useinitialsbar = true) { + $doneslots = array(); + foreach ($this->get_sort_columns() as $column => $notused) { + $slot = $this->is_latest_step_column($column); + if ($slot && !in_array($slot, $doneslots)) { + $this->add_latest_state_join($slot); + $doneslots[] = $slot; + } + } + + parent::query_db($pagesize, $useinitialsbar); + + if ($this->requires_latest_steps_loaded()) { + $qubaids = $this->get_qubaids_condition(); + $this->lateststeps = $this->load_question_latest_steps($qubaids); + } + } + + public function get_sort_columns() { + // Add attemptid as a final tie-break to the sort. This ensures that + // Attempts by the same student appear in order when just sorting by name. + $sortcolumns = parent::get_sort_columns(); + $sortcolumns['quiza.id'] = SORT_ASC; + return $sortcolumns; + } + + public function wrap_html_start() { + if ($this->is_downloading() || !$this->includecheckboxes) { + return; + } + + $url = new moodle_url($this->reporturl, $this->displayoptions); + $url->param('sesskey', sesskey()); + + echo '
'; + echo '
'; + + echo html_writer::input_hidden_params($url); + echo '
'; + } + + public function wrap_html_finish() { + if ($this->is_downloading() || !$this->includecheckboxes) { + return; + } + + echo '
'; + echo '' . + get_string('selectall', 'quiz') . ' / '; + echo '' . + get_string('selectnone', 'quiz') . ' '; + echo '  '; + $this->submit_buttons(); + echo '
'; + // Close form + echo '
'; + echo '
'; + } + + /** + * Output any submit buttons required by the $this->includecheckboxes form. + */ + protected function submit_buttons() { + global $PAGE; + if (has_capability('mod/quiz:deleteattempts', $this->context)) { + echo ''; + $PAGE->requires->event_handler('#deleteattemptsbutton', 'click', 'M.util.show_confirm_dialog', + array('message' => get_string('deleteattemptcheck', 'quiz'))); + } + } +} diff --git a/mod/quiz/report/overview/overview_table.php b/mod/quiz/report/overview/overview_table.php index 3f9e77f0166..f434003d4b7 100644 --- a/mod/quiz/report/overview/overview_table.php +++ b/mod/quiz/report/overview/overview_table.php @@ -26,6 +26,8 @@ defined('MOODLE_INTERNAL') || die(); +require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport_table.php'); + /** * This is a table subclass for displaying the quiz grades report. diff --git a/mod/quiz/report/responses/responses_table.php b/mod/quiz/report/responses/responses_table.php index f4b02bf2f1c..ef16998e33d 100644 --- a/mod/quiz/report/responses/responses_table.php +++ b/mod/quiz/report/responses/responses_table.php @@ -26,6 +26,8 @@ defined('MOODLE_INTERNAL') || die(); +require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport_table.php'); + /** * This is a table subclass for displaying the quiz responses report.