MDL-29847 question preview: garbage collect old preview data.

This adds cron code which looks for question previews that have not been
touched for more than 24 hours, and deletes them.

We try to delete previews immediately. For example if the user clicks
start again, then we immediately delete their previous preview. However,
we can't do that if they just close the preview window. Hence we need
some cron code to clean up old preview that have got left lying around.

Normally, this code will not have much to do, so it will be very fast,
so we can afford to run it every cron.

This has been implemented in such a way that in future it will be easy
to add other cron code to the question bank.

Sadly, to make this work on MySQL, we require a horrible hack in the
already hacky delete_usage_records_for_mysql function.
This commit is contained in:
Tim Hunt
2012-08-17 10:53:05 +01:00
parent 76c9fcc967
commit cb12522cea
4 changed files with 54 additions and 1 deletions
+27
View File
@@ -313,3 +313,30 @@ function restart_preview($previewid, $questionid, $displayoptions, $context) {
redirect(question_preview_url($questionid, $displayoptions->behaviour,
$displayoptions->maxmark, $displayoptions, $displayoptions->variant, $context));
}
/**
* Scheduled tasks relating to question preview. Specifically, delete any old
* previews that are left over in the database.
*/
function question_preview_cron() {
$maxage = 24*60*60; // We delete previews that have not been touched for 24 hours.
$lastmodifiedcutoff = time() - $maxage;
mtrace("\n Cleaning up old question previews...", '');
$oldpreviews = new qubaid_join('{question_usages} quba', 'quba.id',
'quba.component = :qubacomponent
AND NOT EXISTS (
SELECT 1
FROM {question_attempts} qa
JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id
WHERE qa.questionusageid = quba.id
AND (qa.timemodified > :qamodifiedcutoff
OR qas.timecreated > :stepcreatedcutoff)
)
',
array('qubacomponent' => 'core_question_preview',
'qamodifiedcutoff' => $lastmodifiedcutoff, 'stepcreatedcutoff' => $lastmodifiedcutoff));
question_engine::delete_questions_usage_by_activities($oldpreviews);
mtrace('done.');
}