Skip to content

Commit

Permalink
codecleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Sep 19, 2024
1 parent 889603b commit 4ea0aa2
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 48 deletions.
1 change: 0 additions & 1 deletion block_townsquare.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use block_townsquare\contentcontroller;


/**
* Plugin strings are defined here.
*
Expand Down
17 changes: 8 additions & 9 deletions classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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'];
Expand Down
26 changes: 14 additions & 12 deletions classes/letter/post_letter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/

namespace block_townsquare\letter;
use context_module;
use core_user\fields;
use moodle_url;
use stdClass;

Expand Down Expand Up @@ -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]);
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lang/en/block_townsquare.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/contentcontroller_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace block_townsquare;

use stdClass;

/**
* Unit tests for the block_townsquare.
*
Expand Down Expand Up @@ -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();
}
Expand Down
9 changes: 5 additions & 4 deletions tests/coreevents_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
global $CFG;
require_once($CFG->libdir . '/completionlib.php');

use core_completion_external;
use stdClass;

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace block_townsquare;

use stdClass;

defined('MOODLE_INTERNAL') || die();

global $CFG;
Expand All @@ -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 {
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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.
Expand Down
18 changes: 8 additions & 10 deletions tests/postevents_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace block_townsquare;

use Exception;
use stdClass;

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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');
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/privacy/provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4ea0aa2

Please sign in to comment.