forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-74762 qbank_statistics: improve performance loading the data
This issue greatly improves the performance of displaying statistics in the question bank. 1. The required quiz statistics are now pre-computed by a scheduled task. 2. Cached statistics in the database are now never cleaned up, so the pre-computed stats are always available. 3. The way the cached statistics are loaded for each question that is being displayed is now a bit more efficient. 4. Related to that, there is a new callback which activities can implement, if they want their question statistics to be included in the ones shown in the question bank. Note, there is still further improvement possible to load the statistics for all questions being displayed in bulk. However, that must wait for a future issue, MDL-75576. The other improvements in this issue are significant and we did not want to delay releasing them. Co-authored-by: Jonathan Champ <[email protected]> Co-authored-by: Tim Hunt <[email protected]>
- Loading branch information
1 parent
b077af7
commit f02c16c
Showing
11 changed files
with
322 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
mod/quiz/report/statistics/classes/task/quiz_statistics_cleanup.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?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/>. | ||
|
||
namespace quiz_statistics\task; | ||
|
||
use quiz_attempt; | ||
use quiz; | ||
use quiz_statistics_report; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
require_once($CFG->dirroot . '/mod/quiz/locallib.php'); | ||
require_once($CFG->dirroot . '/mod/quiz/report/statistics/statisticslib.php'); | ||
require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php'); | ||
require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php'); | ||
|
||
/** | ||
* Re-calculate question statistics. | ||
* | ||
* @package quiz_statistics | ||
* @copyright 2022 Catalyst IT Australia Pty Ltd | ||
* @author Nathan Nguyen <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class recalculate extends \core\task\scheduled_task { | ||
|
||
public function get_name() { | ||
return get_string('recalculatetask', 'quiz_statistics'); | ||
} | ||
|
||
public function execute() { | ||
global $DB; | ||
// TODO: MDL-75197, add quizid in quiz_statistics so that it is simpler to find quizzes for stats calculation. | ||
// Only calculate stats for quizzes which have recently finished attempt. | ||
$sql = " | ||
SELECT qa.quiz, MAX(qa.timefinish) as timefinish | ||
FROM {quiz_attempts} qa | ||
WHERE qa.preview = 0 | ||
AND qa.state = :quizstatefinished | ||
GROUP BY qa.quiz | ||
"; | ||
|
||
$params = [ | ||
"quizstatefinished" => quiz_attempt::FINISHED, | ||
]; | ||
|
||
$latestattempts = $DB->get_records_sql($sql, $params); | ||
|
||
foreach ($latestattempts as $attempt) { | ||
$quizobj = quiz::create($attempt->quiz); | ||
$quiz = $quizobj->get_quiz(); | ||
// Hash code for question stats option in question bank. | ||
$qubaids = quiz_statistics_qubaids_condition($quiz->id, new \core\dml\sql_join(), $quiz->grademethod); | ||
|
||
// Check if there is any existing question stats, and it has been calculated after latest quiz attempt. | ||
$records = $DB->get_records_select( | ||
'quiz_statistics', | ||
'hashcode = :hashcode AND timemodified > :timefinish', | ||
[ | ||
'hashcode' => $qubaids->get_hash_code(), | ||
'timefinish' => $attempt->timefinish | ||
] | ||
); | ||
|
||
if (empty($records)) { | ||
$report = new quiz_statistics_report(); | ||
// Clear old cache. | ||
$report->clear_cached_data($qubaids); | ||
// Calculate new stats. | ||
$report->calculate_questions_stats_for_question_bank($quiz->id); | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.