Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #78, Implements chunked deletes in event observers #79

Open
wants to merge 1 commit into
base: MOODLE_401_STABLE
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 58 additions & 7 deletions classes/quiz_observers.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@
namespace block_grade_me;

class quiz_observers {
const DELETE_RECORDS_CHUNK = 10000;

/**
* Deletes records with specified IDs from block_grade_me_quiz_ngrade table
* in chunks of self::DELETE_RECORDS_CHUNK to prevent timeouts in larger InnoDB tables.
*
* @param array $ids Array of record IDs.
* @return void
*/
private static function delete_from_ids( $ids ) {
global $DB;
if(empty($ids)) {
return;
}
foreach (array_chunk($ids, self::DELETE_RECORDS_CHUNK) as $todelete) {
list($idsql, $idparams) = $DB->get_in_or_equal($todelete);
$DB->execute('
DELETE FROM {block_grade_me_quiz_ngrade}
WHERE id ' . $idsql,
$idparams
);
}
}

/**
* A course content has been deleted.
Expand All @@ -35,7 +58,13 @@ class quiz_observers {
*/
public static function course_content_deleted($event) {
global $DB;
$DB->delete_records('block_grade_me_quiz_ngrade', ['courseid' => $event->courseid]);
$allids = $DB->get_records_sql_menu('
SELECT DISTINCT id, id AS id2
FROM {block_grade_me_quiz_ngrade}
WHERE courseid = ?',
[$event->courseid]
);
self::delete_from_ids($allids);
}

/**
Expand All @@ -47,7 +76,13 @@ public static function course_content_deleted($event) {
public static function course_reset_ended($event) {
global $DB;
if (!empty($event->other['reset_options']['reset_quiz_attempts'])) {
$DB->delete_records('block_grade_me_quiz_ngrade', ['courseid' => $event->other['reset_options']['courseid']]);
$allids = $DB->get_records_sql_menu('
SELECT DISTINCT id, id AS id2
FROM {block_grade_me_quiz_ngrade}
WHERE courseid = ?',
[$event->other['reset_options']['courseid']]
);
self::delete_from_ids($allids);
}
}

Expand All @@ -60,7 +95,13 @@ public static function course_reset_ended($event) {
public static function course_module_deleted($event) {
global $DB;
if ($event->other['modulename'] == 'quiz') {
$DB->delete_records('block_grade_me_quiz_ngrade', ['quizid' => $event->other['instanceid']]);
$allids = $DB->get_records_sql_menu('
SELECT DISTINCT id, id AS id2
FROM {block_grade_me_quiz_ngrade}
WHERE quizid = ?',
[$event->other['instanceid']]
);
self::delete_from_ids($allids);
}
}

Expand All @@ -77,8 +118,13 @@ public static function attempt_deleted(\mod_quiz\event\attempt_deleted $event) {
try {
$attemptrecord = $event->get_record_snapshot('quiz_attempts', $event->objectid);
if (!empty($attemptrecord)) {
$params = ['attemptid' => $attemptrecord->uniqueid];
$DB->delete_records('block_grade_me_quiz_ngrade', $params);
$allids = $DB->get_records_sql_menu('
SELECT DISTINCT id, id AS id2
FROM {block_grade_me_quiz_ngrade}
WHERE attemptid = ?',
[$attemptrecord->uniqueid]
);
self::delete_from_ids($allids);
}
} catch (\Exception $e) {
return false;
Expand Down Expand Up @@ -123,8 +169,13 @@ public static function question_manually_graded(\mod_quiz\event\question_manuall
$count = $DB->get_record_sql($sql, [$record->uniqueid]);
// Delete attempts if all questions are graded for attempt, leave other attempts by user for quiz untouched.
if (empty($count->attempts)) {
$DB->delete_records('block_grade_me_quiz_ngrade',
['attemptid' => $record->uniqueid, 'quizid' => $event->other['quizid']]);
$allids = $DB->get_records_sql_menu('
SELECT DISTINCT id, id AS id2
FROM {block_grade_me_quiz_ngrade}
WHERE attemptid = ? AND quizid = ?',
[$record->uniqueid, $event->other['quizid']]
);
self::delete_from_ids($allids);
}
}
}