Skip to content

Commit

Permalink
WIP: php mess cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Jun 14, 2024
1 parent e45fb02 commit 6733fb3
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 530 deletions.
1 change: 0 additions & 1 deletion classes/manager/mail_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ 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.
Expand Down
4 changes: 2 additions & 2 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ function moodleoverflow_user_can_see_discussion($moodleoverflow, $discussion, $c
}

// Retrieve the coursemodule.
if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $moodleoverflow->course)) {
if (!get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $moodleoverflow->course)) {
throw new moodle_exception('invalidcoursemodule');
}

Expand Down Expand Up @@ -1566,7 +1566,7 @@ function moodleoverflow_print_posts_nested($course, &$cm, $moodleoverflow, $disc
* @return array
*/
function get_attachments($post, $cm) {
global $CFG, $OUTPUT;
global $OUTPUT;
$attachments = [];

if (empty($post->attachment)) {
Expand Down
4 changes: 0 additions & 4 deletions tests/dailymail_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,6 @@ public function test_content_of_mail_delivery(): void {
$linktodiscussion = '<a href=3D"https://www.example.com/moodle/mod/moodleoverflow/=discussion.php?d=3D'
. $this->discussion[0]->id;

// Assemble text.
$text = 'Course: ' . $linktocourse . ' -> ' . $linktoforum . ', Topic: '
. $linktodiscussion . ' has ' . $messagecount . ' unread posts.';

$this->assertStringContainsString($linktocourse, $message);
$this->assertStringContainsString($linktoforum, $message);
$this->assertStringContainsString($linktodiscussion, $message);
Expand Down
60 changes: 23 additions & 37 deletions tests/ratings_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ final class ratings_test extends \advanced_testcase {
/** @var stdClass answer from user 2 */
private $answer6;

/** @var \mod_moodleoverflow_generator $generator */
private $generator;

/**
* Test setUp.
*/
Expand All @@ -77,8 +74,8 @@ public function setUp(): void {
*/
public function tearDown(): void {
// Clear all caches.
\mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
\mod_moodleoverflow\subscriptions::reset_discussion_cache();
subscriptions::reset_moodleoverflow_cache();
subscriptions::reset_discussion_cache();
}

// Begin of test functions.
Expand Down Expand Up @@ -163,40 +160,32 @@ public function test_answersorting_twogroups(): void {
$this->set_ratingpreferences(0);

// Test case 1: helpful and solved post, only solved posts.
$group1 = 'sh';
$group2 = 's';
$this->process_groups($group1, $group2);
$this->process_groups('sh', 's');

// Test case 2: helpful and solved post, only helpful posts.
$group2 = 'h';
$this->process_groups($group1, $group2);
$this->process_groups('sh', 'h');

// Test case 3: helpful and solved post, not-marked posts.
$group2 = 'o';
$this->process_groups($group1, $group2);
$this->process_groups('sh', 'o');

// Test case 4: only solved posts and only helpful posts with ratingpreferences = 0.
$group1 = 's';
$group2 = 'h';
$this->set_ratingpreferences(0);
$rightorder = [$this->post, $this->answer6, $this->answer5, $this->answer4, $this->answer2, $this->answer1, $this->answer3];
$this->process_groups($group1, $group2, $rightorder);
$this->process_groups('s', 'h', $rightorder);

// Test case 5: only solved posts and only helpful posts with ratingpreferences = 1.
$this->set_ratingpreferences(1);
$this->process_groups($group1, $group2);
$this->process_groups('s', 'h');

// Test case 6: only solved posts and not-marked posts.
$group2 = 'o';
$this->create_twogroups($group1, $group2);
$this->create_twogroups('s', 'o');
$posts = [$this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6];
$rightorder = [$this->post, $this->answer2, $this->answer1, $this->answer3, $this->answer6, $this->answer5, $this->answer4];
$result = $this->postsorderequal(ratings::moodleoverflow_sort_answers_by_ratings($posts), $rightorder);
$this->assertEquals(1, $result);

// Test case 6: only helpful posts and not-marked posts.
$group1 = 'h';
$this->process_groups($group1, $group2);
$this->process_groups('h', 'o');
}

/**
Expand All @@ -213,8 +202,7 @@ public function test_answersorting_onegroup(): void {
$this->set_ratingpreferences(0);

// Test case 1: only solved and helpful posts.
$group = 'sh';
$this->create_onegroup($group);
$this->create_onegroup('sh');
$posts = [$this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6];
$rightorder = [$this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5];
$result = $this->postsorderequal(ratings::moodleoverflow_sort_answers_by_ratings($posts), $rightorder);
Expand All @@ -227,8 +215,7 @@ public function test_answersorting_onegroup(): void {
$this->assertEquals(1, $result);

// Test case 2: only solvedposts.
$group = 's';
$this->create_onegroup($group);
$this->create_onegroup('s');
$rightorder = [$this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5];
$result = $this->postsorderequal(ratings::moodleoverflow_sort_answers_by_ratings($posts), $rightorder);
$this->assertEquals(1, $result);
Expand All @@ -240,8 +227,7 @@ public function test_answersorting_onegroup(): void {
$this->assertEquals(1, $result);

// Test case 3: only helpful posts.
$group = 'h';
$this->create_onegroup($group);
$this->create_onegroup('h');
$rightorder = [$this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5];
$result = $this->postsorderequal(ratings::moodleoverflow_sort_answers_by_ratings($posts), $rightorder);
$this->assertEquals(1, $result);
Expand All @@ -253,8 +239,7 @@ public function test_answersorting_onegroup(): void {
$this->assertEquals(1, $result);

// Test case 4: only not marked posts.
$group = 'o';
$this->create_onegroup($group);
$this->create_onegroup('o');
$rightorder = [$this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5];
$result = $this->postsorderequal(ratings::moodleoverflow_sort_answers_by_ratings($posts), $rightorder);
$this->assertEquals(1, $result);
Expand Down Expand Up @@ -292,15 +277,15 @@ private function helper_course_set_up() {
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');

// Create a discussion, a parent post and six answers.
$this->generator = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow');
$discussion = $this->generator->post_to_forum($moodleoverflow, $teacher);
$generator = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow');
$discussion = $generator->post_to_forum($moodleoverflow, $teacher);
$this->post = $DB->get_record('moodleoverflow_posts', ['id' => $discussion[0]->firstpost], '*');
$this->answer1 = $this->generator->reply_to_post($discussion[1], $user1, true);
$this->answer2 = $this->generator->reply_to_post($discussion[1], $user1, true);
$this->answer3 = $this->generator->reply_to_post($discussion[1], $user1, true);
$this->answer4 = $this->generator->reply_to_post($discussion[1], $user2, true);
$this->answer5 = $this->generator->reply_to_post($discussion[1], $user2, true);
$this->answer6 = $this->generator->reply_to_post($discussion[1], $user2, true);
$this->answer1 = $generator->reply_to_post($discussion[1], $user1, true);
$this->answer2 = $generator->reply_to_post($discussion[1], $user1, true);
$this->answer3 = $generator->reply_to_post($discussion[1], $user1, true);
$this->answer4 = $generator->reply_to_post($discussion[1], $user2, true);
$this->answer5 = $generator->reply_to_post($discussion[1], $user2, true);
$this->answer6 = $generator->reply_to_post($discussion[1], $user2, true);
}


Expand All @@ -315,7 +300,8 @@ private function postsorderequal($sortedposts, $rightorder) {
if (count($sortedposts) != count($rightorder)) {
return 0;
}
for ($i = 0; $i < count($sortedposts); $i++) {
$numberofposts = count($sortedposts);
for ($i = 0; $i < $numberofposts; $i++) {
// Get the current elements.
$sortedpost = current($sortedposts);
$post = current($rightorder);
Expand Down
Loading

0 comments on commit 6733fb3

Please sign in to comment.