Skip to content
Browse files

Merge branch 'MDL-29847_23' of git://github.com/timhunt/moodle into M…

…OODLE_23_STABLE
  • Loading branch information...
2 parents 0c6800e + 3e6b276 commit 9da82e9ba312e08978e221c2f86ff5866cfab6c9 @stronk7 stronk7 committed
Showing with 54 additions and 1 deletion.
  1. +7 −0 lib/cronlib.php
  2. +11 −0 question/engine/bank.php
  3. +9 −1 question/engine/datalib.php
  4. +27 −0 question/previewlib.php
View
7 lib/cronlib.php
@@ -373,6 +373,13 @@ function cron_run() {
}
+ // Run question bank clean-up.
+ mtrace("Starting the question bank cron...", '');
+ require_once($CFG->libdir . '/questionlib.php');
+ question_bank::cron();
+ mtrace('done.');
+
+
//Run registration updated cron
mtrace(get_string('siteupdatesstart', 'hub'));
require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
View
11 question/engine/bank.php
@@ -398,6 +398,17 @@ public static function fraction_options_full() {
self::ensure_fraction_options_initialised();
return self::$fractionoptionsfull;
}
+
+ /**
+ * Perform scheduled maintenance tasks relating to the question bank.
+ */
+ public static function cron() {
+ global $CFG;
+
+ // Delete any old question preview that got left in the database.
+ require_once($CFG->dirroot . '/question/previewlib.php');
+ question_preview_cron();
+ }
}
View
10 question/engine/datalib.php
@@ -758,6 +758,14 @@ public function delete_questions_usage_by_activities(qubaid_condition $qubaids)
* @param qubaid_condition $qubaids identifies which question useages to delete.
*/
protected function delete_usage_records_for_mysql(qubaid_condition $qubaids) {
+ $qubaidtest = $qubaids->usage_id_in();
+ if (strpos($qubaidtest, 'question_usages') !== false &&
+ strpos($qubaidtest, 'IN (SELECT') === 0) {
+ // This horrible hack is required by MDL-29847. It comes from
+ // http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/
+ $qubaidtest = 'IN (SELECT * FROM ' . substr($qubaidtest, 3) . ' AS hack_subquery_alias)';
+ }
+
// TODO once MDL-29589 is fixed, eliminate this method, and instead use the new $DB API.
$this->db->execute('
DELETE qu, qa, qas, qasd
@@ -765,7 +773,7 @@ protected function delete_usage_records_for_mysql(qubaid_condition $qubaids) {
JOIN {question_attempts} qa ON qa.questionusageid = qu.id
LEFT JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id
LEFT JOIN {question_attempt_step_data} qasd ON qasd.attemptstepid = qas.id
- WHERE qu.id ' . $qubaids->usage_id_in(),
+ WHERE qu.id ' . $qubaidtest,
$qubaids->usage_id_in_params());
}
View
27 question/previewlib.php
@@ -316,3 +316,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.');
+}

0 comments on commit 9da82e9

Please sign in to comment.
Something went wrong with that request. Please try again.