Skip to content

Commit

Permalink
phpmd cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Jul 1, 2024
1 parent 7420b34 commit 17514af
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 158 deletions.
132 changes: 56 additions & 76 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, $PAGE;
global $CFG, $DB, $PAGE;

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

// 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 @@ -101,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 @@ -125,7 +123,7 @@ 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) {
Expand All @@ -136,7 +134,6 @@ public static function moodleoverflow_send_mails(): bool {

// 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 @@ -147,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 @@ -231,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 @@ -267,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 @@ -332,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 @@ -360,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,9 +419,7 @@ private static function send_post($userto, $post, array &$coursemodules, array &

// Check whether the user is subscribed to the discussion.
$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 @@ -462,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 @@ -520,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 @@ -555,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
4 changes: 1 addition & 3 deletions classes/ratings.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ public static function moodleoverflow_get_rating($postid) {
global $DB;

// Retrieve the full post.
if (!$post = $DB->get_record('moodleoverflow_posts', ['id' => $postid])) {
throw new moodle_exception('postnotexist', 'moodleoverflow');
}
$post = moodleoverflow_get_record_or_exception('moodleoverflow_posts', ['id' => $postid], 'postnotexist');

// Get the rating for this single post.
return self::moodleoverflow_get_ratings_by_discussion($post->discussion, $postid);
Expand Down
22 changes: 6 additions & 16 deletions classes/readtracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,8 @@ public static function moodleoverflow_is_tracked($moodleoverflow, $user = null)
$user = $USER;
}

// Guests cannot track a moodleoverflow.
if (isguestuser($USER) || empty($USER->id)) {
return false;
}

// Check if the moodleoverflow can be generally tracked.
if (!self::moodleoverflow_can_track_moodleoverflows($moodleoverflow)) {
// Guests cannot track a moodleoverflow. The moodleoverflow should be generally trackable.
if (isguestuser($USER) || empty($USER->id) || !self::moodleoverflow_can_track_moodleoverflows($moodleoverflow)) {
return false;
}

Expand Down Expand Up @@ -135,11 +130,9 @@ public static function moodleoverflow_mark_moodleoverflow_read($cm, $userid = nu

// Iterate through all of this discussions.
foreach ($discussions as $discussionid) {

// Mark the discussion as read.
if (!self::moodleoverflow_mark_discussion_read($discussionid, context_module::instance($cm->id), $userid)) {
throw new moodle_exception('markreadfailed', 'moodleoverflow');
}
$markedcheck = self::moodleoverflow_mark_discussion_read($discussionid, context_module::instance($cm->id), $userid);
moodleoverflow_throw_exception_with_check($markedcheck !== true, 'markreadfailed');
}

return true;
Expand Down Expand Up @@ -172,11 +165,8 @@ public static function moodleoverflow_mark_discussion_read($discussionid, $modco
}

// Mark the post as read.
if (!self::moodleoverflow_mark_post_read($userid, $post)) {
throw new moodle_exception('markreadfailed', 'moodleoverflow');

return false;
}
$postreadcheck = self::moodleoverflow_mark_post_read($userid, $post);
moodleoverflow_throw_exception_with_check(!$postreadcheck, 'markreadfailed');
}

// The discussion has been marked as read.
Expand Down
15 changes: 2 additions & 13 deletions classes/task/send_mails.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,10 @@ public function send_review_notifications() {
}

$course = null;

$moodleoverflow = null;
$usersto = null;
$cm = null;

$discussion = null;

$success = [];

foreach ($postinfos as $postinfo) {
Expand Down Expand Up @@ -135,16 +132,8 @@ public function send_review_notifications() {
cron_setup_user($userto, $course);
}

$maildata = new moodleoverflow_email(
$course,
$cm,
$moodleoverflow,
$discussion,
$post,
$userfrom,
$userto,
false
);
$maildata = new moodleoverflow_email($course, $cm, $moodleoverflow, $discussion,
$post, $userfrom, $userto, false);

$textcontext = $maildata->export_for_template($renderertext, true);
$htmlcontext = $maildata->export_for_template($rendererhtml, false);
Expand Down
15 changes: 5 additions & 10 deletions discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
defined('MOODLE_INTERNAL') || die();

global $CFG, $PAGE, $USER, $SESSION, $OUTPUT;
global $CFG, $DB, $PAGE, $USER, $SESSION, $OUTPUT;

// Include config and locallib.
require_once('../../config.php');
Expand All @@ -42,19 +42,14 @@
$PAGE->add_body_class('limitedwidth');

// Check if the discussion is valid.
if (!$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $d])) {
throw new moodle_exception('invaliddiscussionid', 'moodleoverflow');
}
$discussion = moodleoverflow_get_record_or_exception('moodleoverflow_discussions', ['id' => $d], 'invaliddiscussionid');

// Check if the related moodleoverflow instance is valid.
if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow])) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
$moodleoverflow = moodleoverflow_get_record_or_exception('moodleoverflow', ['id' => $discussion->moodleoverflow],
'invalidmoodleoverflowid');

// Check if the related moodleoverflow instance is valid.
if (!$course = $DB->get_record('course', ['id' => $discussion->course])) {
throw new moodle_exception('invalidcourseid');
}
$course = moodleoverflow_get_record_or_exception('course', ['id' => $discussion->course], 'invalidcourseid', '*', true);

// Save the allowmultiplemarks setting.
$marksetting = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id], 'allowmultiplemarks');
Expand Down
14 changes: 5 additions & 9 deletions externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,15 @@ public static function record_vote($postid, $ratingid) {
$post = $DB->get_record('moodleoverflow_posts', ['id' => $params['postid']], '*', MUST_EXIST);

// Check if the discussion is valid.
if (!$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $post->discussion])) {
throw new moodle_exception('invaliddiscussionid', 'moodleoverflow');
}
$discussion = moodleoverflow_get_record_or_exception('moodleoverflow_discussions', ['id' => $post->discussion],
'invaliddiscussionid');

// Check if the related moodleoverflow instance is valid.
if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow])) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
$moodleoverflow = moodleoverflow_get_record_or_exception('moodleoverflow', ['id' => $discussion->moodleoverflow],
'invalidmoodleoverflowid');

// Check if the related moodleoverflow instance is valid.
if (!$course = $DB->get_record('course', ['id' => $discussion->course])) {
throw new moodle_exception('invalidcourseid');
}
$course = moodleoverflow_get_record_or_exception('course', ['id' => $discussion->course], 'invalidcourseid', '*', true);

// Get the related coursemodule and its context.
if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id)) {
Expand Down
Loading

0 comments on commit 17514af

Please sign in to comment.