From a705aa068d00a49f218053602bce12712ff3ad5e Mon Sep 17 00:00:00 2001 From: TamaroWalter Date: Tue, 12 Mar 2024 19:00:07 +0100 Subject: [PATCH] Completed updates for moodle 404 --- classes/manager/mail_manager.php | 6 +++--- index.php | 6 ++++-- lib.php | 7 ++++--- locallib.php | 22 +++++----------------- post.php | 7 +++++-- tests/review_test.php | 9 ++++++--- tests/subscriptions_test.php | 2 ++ 7 files changed, 29 insertions(+), 30 deletions(-) diff --git a/classes/manager/mail_manager.php b/classes/manager/mail_manager.php index 9cc0d8cf70..388c017eaf 100644 --- a/classes/manager/mail_manager.php +++ b/classes/manager/mail_manager.php @@ -26,6 +26,7 @@ use context_course; use context_module; +use core\context\course; use core_php_time_limit; use mod_moodleoverflow\anonymous; use mod_moodleoverflow\output\moodleoverflow_email; @@ -496,9 +497,8 @@ private static function send_post($userto, $post, array &$coursemodules, array & // Preapare to actually send the post now. Build up the content. $cleanname = str_replace('"', "'", strip_tags(format_string($moodleoverflow->name))); - $coursecontext = context_course::instance($course->id); - // TODO: deprecated, option array should not contain context objects. - $shortname = format_string($course->shortname, true, ['context' => $coursecontext]); + $formatter = \core\di::get(\core\formatting::class); + $shortname = $formatter->format_string($course->shortname, true, course::instance($course->id)); // Define a header to make mails easier to track. $emailmessageid = generate_email_messageid('moodlemoodleoverflow' . $moodleoverflow->id); diff --git a/index.php b/index.php index c4390d12d6..0431279801 100644 --- a/index.php +++ b/index.php @@ -23,6 +23,8 @@ */ // Require needed files. +use core\context\course; + require_once(dirname(dirname(dirname(__FILE__))) . '/config.php'); require_once(dirname(__FILE__) . '/locallib.php'); require_once($CFG->dirroot . '/course/lib.php'); @@ -214,8 +216,8 @@ $returnto = moodleoverflow_go_back_to($url); // Prepare the message to be displayed. - // TODO: Deprecated, options should not contain a context object. - $shortname = format_string($course->shortname, true, ['context' => context_course::instance($course->id)]); + $formatter = \core\di::get(\core\formatting::class); + $shortname = $formatter->format_string($course->shortname, true, course::instance($course->id)); $notification = \core\output\notification::NOTIFY_SUCCESS; // Redirect the user depending on the subscription state. diff --git a/lib.php b/lib.php index 18fe92bcf3..0ade1d1b42 100644 --- a/lib.php +++ b/lib.php @@ -29,6 +29,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use core\context\course; + defined('MOODLE_INTERNAL') || die(); require_once(dirname(__FILE__) . '/locallib.php'); @@ -883,9 +885,8 @@ function moodleoverflow_send_mails() { // Preapare to actually send the post now. Build up the content. $cleanname = str_replace('"', "'", strip_tags(format_string($moodleoverflow->name))); - $coursecontext = context_course::instance($course->id); - // TODO: Deprecated, options should not contain a context object. - $shortname = format_string($course->shortname, true, ['context' => $coursecontext]); + $formatter = \core\di::get(\core\formatting::class); + $shortname = $formatter->format_string($course->shortname, true, course::instance($course->id)); // Define a header to make mails easier to track. $emailmessageid = generate_email_messageid('moodlemoodleoverflow' . $moodleoverflow->id); diff --git a/locallib.php b/locallib.php index 1c2e5e2188..4009c021c8 100644 --- a/locallib.php +++ b/locallib.php @@ -24,7 +24,6 @@ * @copyright 2017 Kennet Winter * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - use mod_moodleoverflow\anonymous; use mod_moodleoverflow\capabilities; use mod_moodleoverflow\event\post_deleted; @@ -928,12 +927,7 @@ function moodleoverflow_print_discussion($course, $cm, $moodleoverflow, $discuss // Retrieve all posts of the discussion. $posts = moodleoverflow_get_all_discussion_posts($discussion->id, $istracked, $modulecontext); - /*$newpost = []; - foreach($posts as $posti) { - $newpost[] = $posti->message; - } - var_dump($newpost); - */$usermapping = anonymous::get_userid_mapping($moodleoverflow, $discussion->id); + $usermapping = anonymous::get_userid_mapping($moodleoverflow, $discussion->id); // Start with the parent post. $post = $posts[$post->id]; @@ -1432,12 +1426,6 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co } $mustachedata->questioner = $post->userid == $discussion->userid ? 'questioner' : ''; - // Set options for the post. - $options = new stdClass(); - $options->para = false; - $options->trusted = false; - $options->context = $modulecontext; - $reviewdelay = get_config('moodleoverflow', 'reviewpossibleaftertime'); $mustachedata->reviewdelay = format_time($reviewdelay); $mustachedata->needsreview = !$post->reviewed; @@ -1446,8 +1434,8 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co $mustachedata->withinreviewperiod = $reviewable; // Prepare the post. - // TODO: Deprecated $courseiddonotuse deprecated course id, use context option instead. - $mustachedata->postcontent = format_text($post->message, $post->messageformat, $options, $course->id); + $formatter = \core\di::get(\core\formatting::class); + $mustachedata->postcontent = $formatter->format_text($post->message, $post->messageformat, context_module::instance($cm->id)); // Load the attachments. $mustachedata->attachments = get_attachments($post, $cm); @@ -1857,10 +1845,10 @@ function moodleoverflow_delete_post($post, $deletechildren, $cm, $moodleoverflow $attachments = $fs->get_area_files($context->id, 'mod_moodleoverflow', 'attachment', $post->id, "filename", true); foreach ($attachments as $attachment) { - // Get file + // Get file. $file = $fs->get_file($context->id, 'mod_moodleoverflow', 'attachment', $post->id, $attachment->get_filepath(), $attachment->get_filename()); - // Delete it if it exists + // Delete it if it exists. if ($file) { $file->delete(); } diff --git a/post.php b/post.php index 976c8f538c..303ba6223c 100644 --- a/post.php +++ b/post.php @@ -25,6 +25,7 @@ // TODO refactor this. For more readability, and to avoid security issues. // Include config and locallib. +use mod_moodleoverflow\anonymous; use mod_moodleoverflow\review; require_once(dirname(dirname(dirname(__FILE__))) . '/config.php'); @@ -490,7 +491,9 @@ empty($post->id) ? null : $post->id, mod_moodleoverflow_post_form::attachment_options($moodleoverflow)); -if ($draftitemid && $edit && \mod_moodleoverflow\anonymous::is_post_anonymous($discussion, $moodleoverflow, $post->userid) && $post->userid != $USER->id) { +if ($draftitemid && $edit && anonymous::is_post_anonymous($discussion, $moodleoverflow, $post->userid) + && $post->userid != $USER->id) { + $usercontext = context_user::instance($USER->id); $anonymousstr = get_string('anonymous', 'moodleoverflow'); foreach (get_file_storage()->get_area_files($usercontext->id, 'user', 'draft', $draftitemid) as $file) { @@ -634,7 +637,7 @@ if ($realpost->userid == $USER->id) { $message .= get_string('postupdated', 'moodleoverflow'); } else { - if (\mod_moodleoverflow\anonymous::is_post_anonymous($discussion, $moodleoverflow, $realpost->userid)) { + if (anonymous::is_post_anonymous($discussion, $moodleoverflow, $realpost->userid)) { $name = get_string('anonymous', 'moodleoverflow'); } else { $realuser = $DB->get_record('user', ['id' => $realpost->userid]); diff --git a/tests/review_test.php b/tests/review_test.php index d6ceeb90e6..13d54ce7af 100644 --- a/tests/review_test.php +++ b/tests/review_test.php @@ -292,15 +292,18 @@ private function create_post($options) { private function check_mail_records($teacherpost, $studentpost, $review1, $review2, $mailed) { global $DB; - $this->assert_matches_properties(['mailed' => MOODLEOVERFLOW_MAILED_PENDING, 'reviewed' => $review1, 'timereviewed' => null], + $this->assert_matches_properties(['mailed' => MOODLEOVERFLOW_MAILED_PENDING, + 'reviewed' => $review1, 'timereviewed' => null], $DB->get_record('moodleoverflow_posts', ['id' => $teacherpost->id])); - $this->assert_matches_properties(['mailed' => MOODLEOVERFLOW_MAILED_PENDING, 'reviewed' => $review2, 'timereviewed' => null], + $this->assert_matches_properties(['mailed' => MOODLEOVERFLOW_MAILED_PENDING, + 'reviewed' => $review2, 'timereviewed' => null], $DB->get_record('moodleoverflow_posts', ['id' => $studentpost->id])); $this->run_send_mails(); $this->run_send_mails(); // Execute twice to ensure no duplicate mails. - $this->assert_matches_properties(['mailed' => MOODLEOVERFLOW_MAILED_SUCCESS, 'reviewed' => $review1, 'timereviewed' => null], + $this->assert_matches_properties(['mailed' => MOODLEOVERFLOW_MAILED_SUCCESS, + 'reviewed' => $review1, 'timereviewed' => null], $DB->get_record('moodleoverflow_posts', ['id' => $teacherpost->id])); $this->assert_matches_properties(['mailed' => $mailed, 'reviewed' => $review2, 'timereviewed' => null], $DB->get_record('moodleoverflow_posts', ['id' => $studentpost->id])); diff --git a/tests/subscriptions_test.php b/tests/subscriptions_test.php index 2c8651e26e..756a67eae5 100644 --- a/tests/subscriptions_test.php +++ b/tests/subscriptions_test.php @@ -1397,6 +1397,7 @@ public function is_subscribable_provider() { * @param array $options * * @dataProvider is_subscribable_provider + * @return void */ public function test_is_subscribable_logged_out($options): void { $this->resetAfterTest(true); @@ -1416,6 +1417,7 @@ public function test_is_subscribable_logged_out($options): void { * @param array $options * * @dataProvider is_subscribable_provider + * @return void */ public function test_is_subscribable_is_guest($options): void { global $DB;