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

ci/codechecker #189

Open
wants to merge 19 commits into
base: master
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
25 changes: 11 additions & 14 deletions backup/moodle2/backup_moodleoverflow_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,35 @@ class backup_moodleoverflow_activity_structure_step extends backup_activity_stru
* @return backup_nested_element
*/
protected function define_structure() {

// To know if we are including userinfo.
$userinfo = $this->get_setting_value('userinfo');

// Define the root element describing the moodleoverflow instance.
$moodleoverflow = new backup_nested_element('moodleoverflow', ['id'], [
'name', 'intro', 'introformat', 'maxbytes', 'maxattachments', 'timecreated', 'timemodified',
'forcesubscribe', 'trackingtype', 'ratingpreference', 'coursewidereputation', 'allowrating',
'allowreputation', 'allownegativereputation', 'grademaxgrade', 'gradescalefactor', 'gradecat',
'anonymous', 'allowmultiplemarks', ]);
'name', 'intro', 'introformat', 'maxbytes', 'maxattachments', 'timecreated', 'timemodified', 'forcesubscribe',
'trackingtype', 'ratingpreference', 'coursewidereputation', 'allowrating', 'allowreputation', 'allownegativereputation',
'grademaxgrade', 'gradescalefactor', 'gradecat', 'anonymous', 'allowmultiplemarks', ]);

// Define each element separated.
$discussions = new backup_nested_element('discussions');
$discussion = new backup_nested_element('discussion', ['id'], [
'name', 'firstpost', 'userid', 'timestart', 'timemodified', 'usermodified', ]);

$posts = new backup_nested_element('posts');
$post = new backup_nested_element('post', ['id'], [
'parent', 'userid', 'created', 'modified',
'message', 'messageformat', 'attachment', 'mailed', 'reviewed', 'timereviewed', ]);
$post = new backup_nested_element('post', ['id'], ['parent', 'userid', 'created', 'modified', 'message',
'messageformat', 'attachment', 'mailed', 'reviewed', 'timereviewed', ]);

$ratings = new backup_nested_element('ratings');
$rating = new backup_nested_element('rating', ['id'], [
'userid', 'rating', 'firstrated', 'lastchanged', ]);
$rating = new backup_nested_element('rating', ['id'], ['userid', 'rating', 'firstrated', 'lastchanged']);

$discussionsubs = new backup_nested_element('discuss_subs');
$discussionsub = new backup_nested_element('discuss_sub', ['id'], [
'userid', 'preference', ]);
$discussionsub = new backup_nested_element('discuss_sub', ['id'], ['userid', 'preference']);

$subscriptions = new backup_nested_element('subscriptions');
$subscription = new backup_nested_element('subscription', ['id'], ['userid']);

$readposts = new backup_nested_element('readposts');
$read = new backup_nested_element('read', ['id'], [
'userid', 'discussionid', 'postid', 'firstread', 'lastread', ]);
$read = new backup_nested_element('read', ['id'], ['userid', 'discussionid', 'postid', 'firstread', 'lastread']);

$grades = new backup_nested_element('grades');
$grade = new backup_nested_element('grade', ['id'], ['userid', 'grade']);
Expand All @@ -100,6 +94,9 @@ protected function define_structure() {
$moodleoverflow->add_child($readposts);
$readposts->add_child($read);

$moodleoverflow->add_child($grades);
$grades->add_child($grade);

$moodleoverflow->add_child($tracking);
$tracking->add_child($track);

Expand Down
136 changes: 57 additions & 79 deletions classes/manager/mail_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class mail_manager {
* @return bool
*/
public static function moodleoverflow_send_mails(): bool {
global $DB, $CFG, $PAGE;
global $CFG, $DB, $PAGE;

// Get the course object of the top level site.
$site = get_site();
Expand All @@ -86,10 +86,8 @@ public static function moodleoverflow_send_mails(): bool {
$moodleoverflows = [];
$courses = [];
$coursemodules = [];
$subscribedusers = [];

// Posts older than x days will not be mailed.
// This will avoid problems with the cron not ran for a long time.
// Posts older than x days will not be mailed. This will avoid problems with the cron not ran for a long time.
$timenow = time();
$endtime = $timenow - get_config('moodleoverflow', 'maxeditingtime');
$starttime = $endtime - (get_config('moodleoverflow', 'maxmailingtime') * 60 * 60);
Expand All @@ -102,7 +100,6 @@ public static function moodleoverflow_send_mails(): bool {
mtrace('Errors occurred while trying to mark some posts as being mailed.');
return false;
}

// Loop through all posts to be mailed.
foreach ($posts as $postid => $post) {
self::check_post($post, $mailcount, $users, $discussions, $errorcount, $posts, $postid,
Expand All @@ -126,18 +123,17 @@ public static function moodleoverflow_send_mails(): bool {
$userto->markposts = [];

// Cache the capabilities of the user.
cron_setup_user($userto);
$CFG->branch >= 402 ? \core\cron::setup_user($userto) : cron_setup_user($userto);

// Reset the caches.
foreach ($coursemodules as $moodleoverflowid => $unused) {
foreach ($coursemodules as $moodleoverflowid) {
$coursemodules[$moodleoverflowid]->cache = new stdClass();
$coursemodules[$moodleoverflowid]->cache->caps = [];
unset($coursemodules[$moodleoverflowid]->uservisible);
}

// Loop through all posts of this users.
foreach ($posts as $post) {

self::send_post($userto, $post, $coursemodules, $errorcount,
$discussions, $moodleoverflows, $courses, $mailcount, $users, $site, $textout, $htmlout);
}
Expand All @@ -148,18 +144,13 @@ public static function moodleoverflow_send_mails(): bool {
}

// Check for all posts whether errors occurred.
if ($posts) {

// Loop through all posts.
foreach ($posts as $post) {

// Tracing information.
mtrace($mailcount[$post->id] . " users were sent post $post->id");
foreach ($posts as $post) {
// Tracing information.
mtrace($mailcount[$post->id] . " users were sent post $post->id");

// Mark the posts with errors in the database.
if ($errorcount[$post->id]) {
$DB->set_field('moodleoverflow_posts', 'mailed', self::MOODLEOVERFLOW_MAILED_ERROR, ['id' => $post->id]);
}
// Mark the posts with errors in the database.
if ($errorcount[$post->id]) {
$DB->set_field('moodleoverflow_posts', 'mailed', self::MOODLEOVERFLOW_MAILED_ERROR, ['id' => $post->id]);
}
}

Expand Down Expand Up @@ -232,7 +223,6 @@ public static function moodleoverflow_mark_old_posts_as_mailed($endtime) {
* @param stdClass $user
*/
public static function moodleoverflow_minimise_user_record(stdClass $user) {

// Remove all information for the mail generation that are not needed.
unset($user->institution);
unset($user->department);
Expand Down Expand Up @@ -268,61 +258,31 @@ public static function moodleoverflow_minimise_user_record(stdClass $user) {
private static function check_post($post, array &$mailcount, array &$users, array &$discussions, array &$errorcount,
array &$posts, int $postid, array &$moodleoverflows, array &$courses,
array &$coursemodules) {
global $DB;
// Check the cache if the discussion exists.
$discussionid = $post->discussion;
if (!isset($discussions[$discussionid])) {
// Retrieve the discussion from the database.
$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $post->discussion]);

// If there is a record, update the cache. Else ignore the post.
if ($discussion) {
$discussions[$discussionid] = $discussion;
subscriptions::fill_subscription_cache($discussion->moodleoverflow);
subscriptions::fill_discussion_subscription_cache($discussion->moodleoverflow);
} else {
mtrace('Could not find discussion ' . $discussionid);
unset($posts[$postid]);
return;
}
if (!self::cache_record('moodleoverflow_discussions', $discussionid, $discussions,
'Could not find discussion ', $posts, $postid, true)) {
return;
}

// Retrieve the connected moodleoverflow instance from the database.
$moodleoverflowid = $discussions[$discussionid]->moodleoverflow;
if (!isset($moodleoverflows[$moodleoverflowid])) {

// Retrieve the record from the database and update the cache.
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflowid]);
if ($moodleoverflow) {
$moodleoverflows[$moodleoverflowid] = $moodleoverflow;
} else {
mtrace('Could not find moodleoverflow ' . $moodleoverflowid);
unset($posts[$postid]);
return;
}
if (!self::cache_record('moodleoverflow', $moodleoverflowid, $moodleoverflows,
'Could not find moodleoverflow ', $posts, $postid, false)) {
return;
}

// Retrieve the connected courses from the database.
$courseid = $moodleoverflows[$moodleoverflowid]->course;
if (!isset($courses[$courseid])) {

// Retrieve the record from the database and update the cache.
$course = $DB->get_record('course', ['id' => $courseid]);
if ($course) {
$courses[$courseid] = $course;
} else {
mtrace('Could not find course ' . $courseid);
unset($posts[$postid]);
return;
}
if (!self::cache_record('course', $courseid, $courses,
'Could not find course ', $posts, $postid, false)) {
return;
}

// Retrieve the connected course modules from the database.
if (!isset($coursemodules[$moodleoverflowid])) {

// Retrieve the coursemodule and update the cache.
$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflowid, $courseid);
if ($cm) {
if ($cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflowid, $courseid)) {
$coursemodules[$moodleoverflowid] = $cm;
} else {
mtrace('Could not find course module for moodleoverflow ' . $moodleoverflowid);
Expand All @@ -333,14 +293,12 @@ private static function check_post($post, array &$mailcount, array &$users, arra

// Cache subscribed users of each moodleoverflow.
if (!isset($subscribedusers[$moodleoverflowid])) {

// Retrieve the context module.
$modulecontext = context_module::instance($coursemodules[$moodleoverflowid]->id);

// Retrieve all subscribed users.
$mid = $moodleoverflows[$moodleoverflowid];
$subusers = subscriptions::get_subscribed_users($mid, $modulecontext, 'u.*', true);
if ($subusers) {
if ($subusers = subscriptions::get_subscribed_users($mid, $modulecontext, 'u.*', true)) {
// Loop through all subscribed users.
foreach ($subusers as $postuser) {
// Save the user into the cache.
Expand All @@ -361,6 +319,39 @@ private static function check_post($post, array &$mailcount, array &$users, arra
$errorcount[$postid] = 0;
}

/**
* Helper function for check_post(). Caches the a record exists in the database and caches the record if needed.
* @param string $table
* @param int $id
* @param array $cache
* @param string $errorMessage
* @param array $posts
* @param int $postid
* @param bool $fillsubscache If the subscription cache is being filled (only when checking discussion cache)
* @return bool
* @throws \dml_exception
*/
private static function cache_record($table, $id, &$cache, $errormessage, &$posts, $postid, $fillsubscache) {
global $DB;
// Check if cache if an record exists already in the cache.
if (!isset($cache[$id])) {
// If there is a record in the database, update the cache. Else ignore the post.
if ($record = $DB->get_record($table, ['id' => $id])) {
$cache[$id] = $record;
if ($fillsubscache) {
subscriptions::fill_subscription_cache($record->moodleoverflow);
subscriptions::fill_discussion_subscription_cache($record->moodleoverflow);
}
} else {
mtrace($errormessage . $id);
unset($posts[$postid]);
return false;
}
}
return true;
}


/**
* Send the Mail with information of the post depending on theinformation available.
* E.g. anonymous post do not include names, users who want resumes do not get single mails.
Expand Down Expand Up @@ -427,11 +418,8 @@ private static function send_post($userto, $post, array &$coursemodules, array &
}

// Check whether the user is subscribed to the discussion.
$iscm = $coursemodules[$moodleoverflow->id];
$uid = $userto->id;
$did = $post->discussion;
$issubscribed = subscriptions::is_subscribed($uid, $moodleoverflow, $modulecontext, $did);
if (!$issubscribed) {
if (!subscriptions::is_subscribed($uid, $moodleoverflow, $modulecontext, $post->discussion)) {
return;
}

Expand Down Expand Up @@ -464,7 +452,7 @@ private static function send_post($userto, $post, array &$coursemodules, array &
}

// Setup roles and languages.
cron_setup_user($userto, $course);
$CFG->branch >= 402 ? \core\cron::setup_user($userto, $course) : cron_setup_user($userto, $course);

// Cache the users capability to view full names.
if (!isset($userto->viewfullnames[$moodleoverflow->id])) {
Expand Down Expand Up @@ -522,16 +510,7 @@ private static function send_post($userto, $post, array &$coursemodules, array &
}

// Format the data.
$data = new moodleoverflow_email(
$course,
$cm,
$moodleoverflow,
$discussion,
$post,
$userfrom,
$userto,
$canreply
);
$data = new moodleoverflow_email($course, $cm, $moodleoverflow, $discussion, $post, $userfrom, $userto, $canreply);

// Retrieve the unsubscribe-link.
$userfrom->customheaders[] = sprintf('List-Unsubscribe: <%s>', $data->get_unsubscribediscussionlink());
Expand All @@ -557,7 +536,6 @@ private static function send_post($userto, $post, array &$coursemodules, array &

// Check whether the post is a reply.
if ($post->parent) {

// Add a reply header.
$parentid = generate_email_messageid(hash('sha256', $post->parent . 'to' . $userto->id));
$userfrom->customheaders[] = "In-Reply-To: $parentid";
Expand Down
Loading
Loading