From 6ff1b9d520363c4c03ecbd8dec7af4b6e3f47a5f Mon Sep 17 00:00:00 2001 From: Vithusha Kethiri Date: Thu, 22 Aug 2024 10:18:21 +1000 Subject: [PATCH] Add validation to course modules to address failing tasks and update lib file for error handling --- classes/task/cron_task.php | 87 +++++++++++++++++++------------------- lib.php | 5 ++- 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/classes/task/cron_task.php b/classes/task/cron_task.php index 74f0a7a..e8acbb4 100644 --- a/classes/task/cron_task.php +++ b/classes/task/cron_task.php @@ -50,51 +50,52 @@ public function execute() { $fs = get_file_storage(); $timeframe = strtotime('-90 days'); if ($personalblog = $DB->get_record('oublog', ['global' => 1], '*', IGNORE_MULTIPLE)) { - $cm = get_coursemodule_from_instance('oublog', $personalblog->id); - $context = \context_module::instance($cm->id); - $instancesql = " - SELECT op.id - FROM {oublog_instances} bi - INNER JOIN {oublog_posts} op ON bi.id = op.oubloginstancesid - WHERE bi.oublogid = :blogid AND op.timedeleted < :timeframe "; - $posts = $DB->get_recordset_sql($instancesql, ['blogid' => $personalblog->id, - 'timeframe' => $timeframe]); - foreach ($posts as $post) { - $transaction = $DB->start_delegated_transaction(); - // Delete files from this post. - $params = ['postid' => $post->id]; - $fs->delete_area_files_select($context->id, 'mod_oublog', 'message', - 'IN (:postid)', $params); - $fs->delete_area_files_select($context->id, 'mod_oublog', 'attachment', - 'IN (:postid)', $params); - - $commentids = $DB->get_records('oublog_comments', $params, '', 'id'); - if ($commentids) { - list($insql, $paramsinsql) = $DB->get_in_or_equal(array_keys($commentids), SQL_PARAMS_NAMED); - $fs->delete_area_files_select($context->id, 'mod_oublog', 'messagecomment', - $insql, $paramsinsql); - $DB->delete_records_select('oublog_comments', "id $insql", $paramsinsql); - } - - $DB->delete_records_select('oublog_comments_moderated', 'postid IN (:postid)', $params); - - // Delete all edits (including files) on posts owned by these users - $editids = $DB->get_records('oublog_edits', $params, '', 'id'); - if ($editids) { - list($insql, $paramsinsql) = $DB->get_in_or_equal(array_keys($editids), SQL_PARAMS_NAMED); - $fs->delete_area_files_select($context->id, 'mod_oublog', 'edit', - $insql, $paramsinsql); - $DB->delete_records_select('oublog_edits', "id $insql", $paramsinsql); + if($cm = get_coursemodule_from_instance('oublog', $personalblog->id)){ + $context = \context_module::instance($cm->id); + $instancesql = " + SELECT op.id + FROM {oublog_instances} bi + INNER JOIN {oublog_posts} op ON bi.id = op.oubloginstancesid + WHERE bi.oublogid = :blogid AND op.timedeleted < :timeframe "; + $posts = $DB->get_recordset_sql($instancesql, ['blogid' => $personalblog->id, + 'timeframe' => $timeframe]); + foreach ($posts as $post) { + $transaction = $DB->start_delegated_transaction(); + // Delete files from this post. + $params = ['postid' => $post->id]; + $fs->delete_area_files_select($context->id, 'mod_oublog', 'message', + 'IN (:postid)', $params); + $fs->delete_area_files_select($context->id, 'mod_oublog', 'attachment', + 'IN (:postid)', $params); + + $commentids = $DB->get_records('oublog_comments', $params, '', 'id'); + if ($commentids) { + list($insql, $paramsinsql) = $DB->get_in_or_equal(array_keys($commentids), SQL_PARAMS_NAMED); + $fs->delete_area_files_select($context->id, 'mod_oublog', 'messagecomment', + $insql, $paramsinsql); + $DB->delete_records_select('oublog_comments', "id $insql", $paramsinsql); + } + + $DB->delete_records_select('oublog_comments_moderated', 'postid IN (:postid)', $params); + + // Delete all edits (including files) on posts owned by these users + $editids = $DB->get_records('oublog_edits', $params, '', 'id'); + if ($editids) { + list($insql, $paramsinsql) = $DB->get_in_or_equal(array_keys($editids), SQL_PARAMS_NAMED); + $fs->delete_area_files_select($context->id, 'mod_oublog', 'edit', + $insql, $paramsinsql); + $DB->delete_records_select('oublog_edits', "id $insql", $paramsinsql); + } + + // Delete tag instances from all these posts. + $DB->delete_records_select('oublog_taginstances', 'postid IN (:postid)', $params); + + // Delete the actual posts. + $DB->delete_records_select('oublog_posts', 'id IN (:postid)', $params); + $transaction->allow_commit(); } - - // Delete tag instances from all these posts. - $DB->delete_records_select('oublog_taginstances', 'postid IN (:postid)', $params); - - // Delete the actual posts. - $DB->delete_records_select('oublog_posts', 'id IN (:postid)', $params); - $transaction->allow_commit(); + $posts->close(); } - $posts->close(); } } diff --git a/lib.php b/lib.php index 5cc0f1e..c534fa9 100644 --- a/lib.php +++ b/lib.php @@ -177,7 +177,10 @@ function oublog_delete_instance($oublogid) { oublog_grade_item_delete($oublog); // Delete event in calendar when deleting activity. - \core_completion\api::update_completion_date_event($cm->id, 'oublog', $oublogid, null); + if (isset($cm)) { + \core_completion\api::update_completion_date_event($cm->id, 'oublog', $oublogid, null); + } + // oublog return($DB->delete_records('oublog', array('id'=>$oublog->id)));