diff --git a/block_townsquare.php b/block_townsquare.php index a57fe7c..a333b02 100644 --- a/block_townsquare.php +++ b/block_townsquare.php @@ -16,7 +16,6 @@ use block_townsquare\contentcontroller; - /** * Plugin strings are defined here. * diff --git a/classes/external.php b/classes/external.php index 365decb..f8920e0 100644 --- a/classes/external.php +++ b/classes/external.php @@ -141,15 +141,7 @@ public static function record_usersettings($userid, $timefilterpast, $timefilter // Check if the user already has a record in the database. if ($records = $DB->get_records('block_townsquare_preferences', ['userid' => $userid])) { // If there more than a record (it only should be only one), delete all of them and insert the new one. - if (count($records) > 1) { - try { - foreach ($records as $record) { - $DB->delete_records('block_townsquare_preferences', ['id' => $record->id]); - } - } catch (Exception $e) { - return false; - } - } else { + if (count($records) <= 1) { // Upgrade the existing record. $record = reset($records); $record->timefilterpast = $params['timefilterpast']; @@ -161,6 +153,13 @@ public static function record_usersettings($userid, $timefilterpast, $timefilter $DB->update_record('block_townsquare_preferences', $record); return true; } + try { + foreach ($records as $record) { + $DB->delete_records('block_townsquare_preferences', ['id' => $record->id]); + } + } catch (Exception $e) { + return false; + } } $record = new stdClass(); $record->userid = $params['userid']; diff --git a/classes/letter/post_letter.php b/classes/letter/post_letter.php index 9cc51e9..565a387 100644 --- a/classes/letter/post_letter.php +++ b/classes/letter/post_letter.php @@ -23,6 +23,8 @@ */ namespace block_townsquare\letter; +use context_module; +use core_user\fields; use moodle_url; use stdClass; @@ -146,16 +148,16 @@ private function retrieve_profilepicture() { global $DB, $OUTPUT; // Profile picture is only retrieved if the author is visible. - if (!$this->post->anonymous) { - $user = new stdClass(); - $picturefields = \core_user\fields::get_picture_fields(); - $user = username_load_fields_from_object($user, $DB->get_record('user', ['id' => $this->author->id]), - null, $picturefields); - $user->id = $this->author->id; - $this->author->picture = $OUTPUT->user_picture($user, ['courseid' => $this->courseid, 'link' => false]); - } else { + if ($this->post->anonymous) { $this->author->picture = ''; + return; } + $user = new stdClass(); + $picturefields = fields::get_picture_fields(); + $user = username_load_fields_from_object($user, $DB->get_record('user', ['id' => $this->author->id]), + null, $picturefields); + $user->id = $this->author->id; + $this->author->picture = $OUTPUT->user_picture($user, ['courseid' => $this->courseid, 'link' => false]); } /** @@ -164,11 +166,11 @@ private function retrieve_profilepicture() { * @return void */ private function add_anonymousattribute($postevent): void { - if ($postevent->modulename == 'moodleoverflow') { - $this->post->anonymous = $postevent->anonymous; - } else { + if ($postevent->modulename != 'moodleoverflow') { $this->post->anonymous = false; + return; } + $this->post->anonymous = $postevent->anonymous; } /** @@ -198,7 +200,7 @@ private function add_privatereplyattribute($postevent): void { * @return string */ private function format_post($postevent) { - $context = \context_module::instance($postevent->coursemoduleid); + $context = context_module::instance($postevent->coursemoduleid); $message = file_rewrite_pluginfile_urls($postevent->content, 'pluginfile.php', $context->id, 'mod_'. $postevent->modulename, 'post', $postevent->postid, ['includetoken' => true]); $options = new stdClass(); diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php index 087118c..a1d50a2 100644 --- a/classes/privacy/provider.php +++ b/classes/privacy/provider.php @@ -29,6 +29,7 @@ use core_privacy\local\request\approved_contextlist; use core_privacy\local\request\contextlist; use core_privacy\local\request\writer; +use stdClass; /** @@ -113,7 +114,7 @@ public static function export_user_data(approved_contextlist $contextlist): void $results = $DB->get_records('block_townsquare_preferences', ['userid' => $contextlist->get_user()->id]); foreach ($results as $result) { - $data = new \stdClass(); + $data = new stdClass(); $data->userid = $result->userid; $data->timefilterpast = $result->timefilterpast; $data->timefilterfuture = $result->timefilterfuture; diff --git a/lang/en/block_townsquare.php b/lang/en/block_townsquare.php index 11c3c5e..c219ac5 100644 --- a/lang/en/block_townsquare.php +++ b/lang/en/block_townsquare.php @@ -41,7 +41,7 @@ $string['configcompletionlettercolor'] = 'Configuration for the color of the activity completion letters'; $string['configorientationmarkercolor'] = 'Configuration for the color of the orientation marker'; $string['configpostlettercolor'] = 'Configuration for the color of the post letters'; -$string['configtimespan'] = 'Setting, in which time span townsquare will search for events and notifications'; +$string['configtimespan'] = 'Setting, in which time span townsquare will search for notification in the past and future'; $string['coursefilter'] = 'Course filter'; $string['dataclosemessage'] = 'Please submit your entries until {$a->time}. The database closes afterwards'; $string['dataopenmessage'] = 'The database opens today'; diff --git a/locallib.php b/locallib.php index 458ae32..6c93f92 100644 --- a/locallib.php +++ b/locallib.php @@ -40,7 +40,7 @@ function townsquare_get_colorsetting($lettertype): string { case 'orientationmarker': return get_config('block_townsquare', 'orientationmarkercolor'); default: - throw new \moodle_exception('invalidlettertype', 'block_townsquare'); + throw new moodle_exception('invalidlettertype', 'block_townsquare'); } } diff --git a/tests/contentcontroller_test.php b/tests/contentcontroller_test.php index 7cfcab3..8be35ba 100644 --- a/tests/contentcontroller_test.php +++ b/tests/contentcontroller_test.php @@ -16,6 +16,8 @@ namespace block_townsquare; +use stdClass; + /** * Unit tests for the block_townsquare. * @@ -54,7 +56,7 @@ final class contentcontroller_test extends \advanced_testcase { // Construct functions. public function setUp(): void { - $this->testdata = new \stdClass(); + $this->testdata = new stdClass(); $this->resetAfterTest(); $this->helper_course_set_up(); } diff --git a/tests/coreevents_test.php b/tests/coreevents_test.php index 5e82ff5..dda904c 100644 --- a/tests/coreevents_test.php +++ b/tests/coreevents_test.php @@ -21,6 +21,7 @@ global $CFG; require_once($CFG->libdir . '/completionlib.php'); +use core_completion_external; use stdClass; /** @@ -81,11 +82,11 @@ public function test_sortorder(): void { $timestamp = 9999999999; $result = true; foreach ($coreevents as $event) { - if ($timestamp >= $event->timestart) { - $timestamp = $event->timestart; - } else { + if ($timestamp < $event->timestart) { $result = false; + break; } + $timestamp = $event->timestart; } $this->assertEquals(true, $result); @@ -210,7 +211,7 @@ public function test_activitycompletion(): void { $this->assertEquals(1, $count); // Test case 2: The student marks the assignment as completed, the activity completion event should disappear. - \core_completion_external::update_activity_completion_status_manually($this->testdata->assignment1->cmid, true); + core_completion_external::update_activity_completion_status_manually($this->testdata->assignment1->cmid, true); $coreevents = $this->get_coreevents_from_user($this->testdata->student1); $result = true; diff --git a/tests/external_test.php b/tests/external_test.php index 8168228..ca8fa9a 100644 --- a/tests/external_test.php +++ b/tests/external_test.php @@ -24,6 +24,8 @@ namespace block_townsquare; +use stdClass; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -36,7 +38,7 @@ * @copyright 2024 Tamaro Walter * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * - * @covers \block_townsquare\external + * @covers external * @runTestsInSeparateProcesses */ final class external_test extends \advanced_testcase { @@ -52,7 +54,7 @@ public function setUp(): void { public function test_record_usersettings(): void { global $DB; - $usersetting = new \stdClass(); + $usersetting = new stdClass(); $usersetting->userid = 1; $usersetting->timefilterpast = 432000; $usersetting->timefilterfuture = 2592000; @@ -67,7 +69,7 @@ public function test_record_usersettings(): void { $this->assertEquals(false, $record); // Call the function to record the user settings and check, if the record is created. - $result = \block_townsquare\external::record_usersettings($usersetting->userid, + $result = external::record_usersettings($usersetting->userid, $usersetting->timefilterpast, $usersetting->timefilterfuture, $usersetting->basicletter, $usersetting->completionletter, $usersetting->postletter); @@ -90,7 +92,7 @@ public function test_record_usersettings(): void { $usersetting->postletter = 0; // Call the function to record the user settings and check, if the record is created. - $result = \block_townsquare\external::record_usersettings($usersetting->userid, $usersetting->timefilterpast, + $result = external::record_usersettings($usersetting->userid, $usersetting->timefilterpast, $usersetting->timefilterfuture, $usersetting->basicletter, $usersetting->completionletter, $usersetting->postletter); @@ -116,7 +118,7 @@ public function test_reset_usersettings(): void { $this->assertEquals(1, count($DB->get_records('block_townsquare_preferences', ['userid' => 1]))); // Test case 1: Wrong parameters. - \block_townsquare\external::reset_usersettings(2); + external::reset_usersettings(2); $this->assertEquals(1, count($DB->get_records('block_townsquare_preferences', ['userid' => 1]))); // Test case 2: For some reason, many records from the same user exist. diff --git a/tests/postevents_test.php b/tests/postevents_test.php index 9ad10ab..4f947c7 100644 --- a/tests/postevents_test.php +++ b/tests/postevents_test.php @@ -24,6 +24,7 @@ namespace block_townsquare; +use Exception; use stdClass; /** @@ -82,11 +83,11 @@ public function test_sortorder(): void { $timestamp = 9999999999; $result = true; foreach ($posts as $post) { - if ($timestamp >= $post->timestart) { - $timestamp = $post->timestart; - } else { + if ($timestamp < $post->timestart) { $result = false; + break; } + $timestamp = $post->timestart; } $this->assertEquals(true, $result); @@ -137,7 +138,7 @@ public function test_module_forum(): void { $posts = $this->get_postevents_from_user($this->testdata->teacher); $result = true; - // Check if there are no more moodleoverflow posts. + // Check if there are no more forum posts. foreach ($posts as $post) { if ($post->modulename == 'forum') { $result = false; @@ -384,11 +385,8 @@ private function helper_course_set_up(): void { $this->getDataGenerator()->enrol_user($this->testdata->student2->id, $this->testdata->course2->id, 'student'); // Check if moodleoverflow is available. - if ($DB->get_record('modules', ['name' => 'moodleoverflow', 'visible' => 1])) { - $this->modoverflowinstalled = true; - } else { - $this->modoverflowinstalled = false; - } + $DB->get_record('modules', ['name' => 'moodleoverflow', 'visible' => 1]) ? $this->modoverflowinstalled = true : + $this->modoverflowinstalled = false; } /** @@ -475,7 +473,7 @@ private function make_anonymous($moodleoverflow, $anonymoussetting): void { $moodleoverflow->anonymous = $anonymoussetting; $DB->update_record('moodleoverflow', $moodleoverflow); } else { - throw new \Exception('invalid parameter, anonymoussetting should be 1 or 2'); + throw new Exception('invalid parameter, anonymoussetting should be 1 or 2'); } } diff --git a/tests/privacy/provider_test.php b/tests/privacy/provider_test.php index af5afb5..d98d884 100644 --- a/tests/privacy/provider_test.php +++ b/tests/privacy/provider_test.php @@ -27,9 +27,9 @@ use core_privacy\local\request\approved_contextlist; use core_privacy\local\request\approved_userlist; use core_privacy\local\request\userlist; -use block_townsquare\privacy\provider; use core_privacy\local\request\writer; use core_privacy\tests\provider_testcase; +use stdClass; /** * PHPUnit tests for testing the functionalities of the townsquare privacy provider @@ -51,7 +51,7 @@ final class provider_test extends provider_testcase { public function setUp(): void { // Create a course and a user. - $this->testdata = new \stdClass(); + $this->testdata = new stdClass(); $this->testdata->course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); $this->testdata->student = $this->getDataGenerator()->create_user(); $this->testdata->studentcontext = \context_user::instance($this->testdata->student->id); @@ -235,7 +235,7 @@ public function test_delete_data_for_users(): void { private function helper_add_preference($userid) { global $DB; // Create a user preference for townsquare content filter. - $this->testdata->setting = new \stdClass(); + $this->testdata->setting = new stdClass(); $this->testdata->setting->userid = $userid; $this->testdata->setting->timefilterpast = 15778463; $this->testdata->setting->timefilterfuture = 15778463;