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

Add validation to course modules to address failing cron tasks #145

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 44 additions & 43 deletions classes/task/cron_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
5 changes: 4 additions & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down