diff --git a/mod/quiz/report/grading/report.php b/mod/quiz/report/grading/report.php index 2a3294aae77..6697e746633 100644 --- a/mod/quiz/report/grading/report.php +++ b/mod/quiz/report/grading/report.php @@ -626,7 +626,7 @@ class quiz_grading_report extends quiz_default_report { protected function get_usage_ids_where_question_in_state($summarystate, $slot, $questionid = null, $orderby = 'random', $page = 0, $pagesize = null) { $dm = new question_engine_data_mapper(); - + $extraselect = ''; if ($pagesize && $orderby != 'random') { $limitfrom = $page * $pagesize; } else { @@ -643,16 +643,17 @@ class quiz_grading_report extends quiz_default_report { foreach ($userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]) as $field) { $customfields[] = $field; } - if ($orderby == 'date') { + if ($orderby === 'date') { list($statetest, $params) = $dm->in_summary_state_test( 'manuallygraded', false, 'mangrstate'); - $orderby = "( + $extraselect = "( SELECT MAX(sortqas.timecreated) FROM {question_attempt_steps} sortqas WHERE sortqas.questionattemptid = qa.id AND sortqas.state $statetest - )"; - } else if ($orderby == 'studentfirstname' || $orderby == 'studentlastname' || in_array($orderby, $customfields)) { + ) as tcreated"; + $orderby = "tcreated"; + } else if ($orderby === 'studentfirstname' || $orderby === 'studentlastname' || in_array($orderby, $customfields)) { $qubaids->from .= " JOIN {user} u ON quiza.userid = u.id {$userfieldssql->joins}"; // For name sorting, map orderby form value to // actual column names; 'idnumber' maps naturally. @@ -666,7 +667,7 @@ class quiz_grading_report extends quiz_default_report { } return $dm->load_questions_usages_where_question_in_state($qubaids, $summarystate, - $slot, $questionid, $orderby, $params, $limitfrom, $pagesize); + $slot, $questionid, $orderby, $params, $limitfrom, $pagesize, $extraselect); } /** diff --git a/question/engine/datalib.php b/question/engine/datalib.php index 5875a6b2fa4..6ab69366d0e 100644 --- a/question/engine/datalib.php +++ b/question/engine/datalib.php @@ -713,24 +713,29 @@ ORDER BY * @param int $limitfrom implements paging of the results. * Ignored if $orderby = random or $limitnum is null. * @param int $limitnum implements paging of the results. null = all. + * @param string $extraselect anything passed here will be added to the SELECT list, use this to return extra data. * @return array with two elements, an array of usage ids, and a count of the total number. */ public function load_questions_usages_where_question_in_state( qubaid_condition $qubaids, $summarystate, $slot, $questionid = null, - $orderby = 'random', $params = array(), $limitfrom = 0, $limitnum = null) { + $orderby = 'random', $params = array(), $limitfrom = 0, $limitnum = null, $extraselect = '') { $extrawhere = ''; if ($questionid) { $extrawhere .= ' AND qa.questionid = :questionid'; $params['questionid'] = $questionid; } - if ($summarystate != 'all') { + if ($summarystate !== 'all') { list($test, $sparams) = $this->in_summary_state_test($summarystate); $extrawhere .= ' AND qas.state ' . $test; $params += $sparams; } - if ($orderby == 'random') { + if (!empty($extraselect)) { + $extraselect = ', ' . $extraselect; + } + + if ($orderby === 'random') { $sqlorderby = ''; } else if ($orderby) { $sqlorderby = 'ORDER BY ' . $orderby; @@ -747,29 +752,24 @@ ORDER BY $qubaidswhere = $qubaids->where(); // Must call this before params. $params += $qubaids->from_where_params(); $params['slot'] = $slot; + $sql = "SELECT qa.questionusageid, + 1 + $extraselect + FROM {$qubaids->from_question_attempts('qa')} + JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id + AND qas.sequencenumber = {$this->latest_step_for_qa_subquery()} + JOIN {question} q ON q.id = qa.questionid + WHERE {$qubaidswhere} + AND qa.slot = :slot + $extrawhere + $sqlorderby"; - $qubaids = $this->db->get_records_sql_menu(" -SELECT - qa.questionusageid, - 1 - -FROM {$qubaids->from_question_attempts('qa')} -JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id - AND qas.sequencenumber = {$this->latest_step_for_qa_subquery()} -JOIN {question} q ON q.id = qa.questionid - -WHERE - {$qubaidswhere} AND - qa.slot = :slot - $extrawhere - -$sqlorderby - ", $params); + $qubaids = $this->db->get_records_sql_menu($sql, $params); $qubaids = array_keys($qubaids); $count = count($qubaids); - if ($orderby == 'random') { + if ($orderby === 'random') { shuffle($qubaids); $limitfrom = 0; } diff --git a/question/engine/upgrade.txt b/question/engine/upgrade.txt index 6e6b5e02fbd..357485fb19d 100644 --- a/question/engine/upgrade.txt +++ b/question/engine/upgrade.txt @@ -1,5 +1,9 @@ This files describes API changes for the core question engine. +=== 4.0 === +1) A new optional parameter $extraselect has been added as a part of load_questions_usages_where_question_in_state() + method in question/engine/datalib.php, anything passed here will be added to the SELECT list, use this to return extra data. + === 3.9 === 1) In the past, whenever a question_usage_by_activity was loaded from the database,