From 7f0722aeb60031c28d239a6ac1fc1c621de22bd7 Mon Sep 17 00:00:00 2001 From: tamaro Date: Wed, 17 Sep 2025 14:24:57 +0200 Subject: [PATCH 1/3] codecleaning with new codechecker version --- ...kup_moodleoverflow_activity_task.class.php | 1 - .../backup_moodleoverflow_stepslib.php | 7 +- ...ore_moodleoverflow_activity_task.class.php | 192 +++++++--- .../restore_moodleoverflow_stepslib.php | 44 ++- classes/anonymous.php | 13 +- classes/capabilities.php | 2 - classes/discussion/discussion.php | 70 +++- classes/event/course_module_viewed.php | 1 - classes/event/discussion_created.php | 1 - classes/event/discussion_deleted.php | 2 - .../event/discussion_subscription_created.php | 1 - classes/event/discussion_viewed.php | 2 - classes/event/post_deleted.php | 1 - classes/event/rating_created.php | 1 - classes/event/rating_deleted.php | 1 - classes/event/rating_updated.php | 1 - classes/event/readtracking_disabled.php | 1 - classes/event/readtracking_enabled.php | 1 - classes/event/subscription_created.php | 1 - classes/event/subscription_deleted.php | 1 - classes/manager/mail_manager.php | 49 ++- classes/observer.php | 6 +- classes/output/email/renderer.php | 9 +- classes/output/email/renderer_textemail.php | 10 +- classes/output/moodleoverflow_email.php | 30 +- classes/post/post.php | 138 +++++-- classes/post/post_control.php | 138 ++++--- classes/post_form.php | 18 +- classes/privacy/data_export_helper.php | 59 ++- classes/privacy/provider.php | 61 +-- classes/ratings.php | 83 +++-- classes/readtracking.php | 17 +- classes/review.php | 21 +- classes/subscriptions.php | 62 +--- classes/tables/userstats_table.php | 15 +- classes/task/clean_readrecords.php | 3 - classes/task/send_daily_mails.php | 1 - classes/task/send_mails.php | 1 - classes/task/send_review_mails.php | 27 +- db/upgrade.php | 10 +- db/upgradelib.php | 22 +- discussion.php | 20 +- externallib.php | 58 +-- grade.php | 2 +- index.php | 24 +- lib.php | 41 +-- locallib.php | 347 +++++++++++++----- markposts.php | 21 +- mod_form.php | 34 +- post.php | 23 +- renderer.php | 1 - resetanonymous.php | 19 +- settings.php | 174 ++++++--- subscribe.php | 24 +- tests/behat/behat_mod_moodleoverflow.php | 42 ++- tests/dailymail_test.php | 8 +- tests/discussion_test.php | 13 +- tests/generator/lib.php | 1 - tests/locallib_test.php | 2 - tests/notification_mail_test.php | 1 - tests/post_test.php | 30 +- tests/privacy_provider_test.php | 155 ++++---- tests/ratings_test.php | 7 +- tests/readtracking_test.php | 1 - tests/review_test.php | 29 +- tests/subscriptions_test.php | 117 +++--- tests/userstats_test.php | 8 +- tracking.php | 3 - userstats.php | 5 +- view.php | 11 +- 70 files changed, 1518 insertions(+), 827 deletions(-) diff --git a/backup/moodle2/backup_moodleoverflow_activity_task.class.php b/backup/moodle2/backup_moodleoverflow_activity_task.class.php index 240538b00e7..368c9c07668 100644 --- a/backup/moodle2/backup_moodleoverflow_activity_task.class.php +++ b/backup/moodle2/backup_moodleoverflow_activity_task.class.php @@ -36,7 +36,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class backup_moodleoverflow_activity_task extends backup_activity_task { - /** * No specific settings for this activity */ diff --git a/backup/moodle2/backup_moodleoverflow_stepslib.php b/backup/moodle2/backup_moodleoverflow_stepslib.php index 391f6754efb..6640f4a11e0 100644 --- a/backup/moodle2/backup_moodleoverflow_stepslib.php +++ b/backup/moodle2/backup_moodleoverflow_stepslib.php @@ -32,7 +32,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class backup_moodleoverflow_activity_structure_step extends backup_activity_structure_step { - /** * Defines the backup structure of the module * @@ -108,11 +107,13 @@ protected function define_structure() { // All these source definitions only happen if we are including user info. if ($userinfo) { - $discussion->set_source_sql(' + $discussion->set_source_sql( + ' SELECT * FROM {moodleoverflow_discussions} WHERE moodleoverflow = ?', - [backup::VAR_PARENTID]); + [backup::VAR_PARENTID] + ); // Need posts ordered by id so parents are always before childs on restore. $post->set_source_table('moodleoverflow_posts', ['discussion' => backup::VAR_PARENTID], 'id ASC'); diff --git a/backup/moodle2/restore_moodleoverflow_activity_task.class.php b/backup/moodle2/restore_moodleoverflow_activity_task.class.php index 69b3703f34a..c706015b28b 100644 --- a/backup/moodle2/restore_moodleoverflow_activity_task.class.php +++ b/backup/moodle2/restore_moodleoverflow_activity_task.class.php @@ -38,7 +38,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class restore_moodleoverflow_activity_task extends restore_activity_task { - /** * Define (add) particular settings this activity can have */ @@ -79,18 +78,24 @@ public static function define_decode_rules() { $rules[] = new restore_decode_rule('MOODLEOVERFLOWVIEWBYF', '/mod/moodleoverflow/view.php?f=$1', 'moodleoverflow'); // Link to forum discussion. - $rules[] = new restore_decode_rule('MOODLEOVERFLOWDISCUSSIONVIEW', + $rules[] = new restore_decode_rule( + 'MOODLEOVERFLOWDISCUSSIONVIEW', '/mod/moodleoverflow/discussion.php?d=$1', - 'moodleoverflow_discussion'); + 'moodleoverflow_discussion' + ); // Link to discussion with parent and with anchor posts. - $rules[] = new restore_decode_rule('MOODLEOVERFLOWDISCUSSIONVIEWPARENT', + $rules[] = new restore_decode_rule( + 'MOODLEOVERFLOWDISCUSSIONVIEWPARENT', '/mod/moodleoverflow/discussion.php?d=$1&parent=$2', - ['moodleoverflow_discussion', 'moodleoverflow_post']); - $rules[] = new restore_decode_rule('MOODLEOVERFLOWDISCUSSIONVIEWINSIDE', '/mod/moodleoverflow/discussion.php?d=$1#$2', - ['moodleoverflow_discussion', 'moodleoverflow_post']); + ['moodleoverflow_discussion', 'moodleoverflow_post'] + ); + $rules[] = new restore_decode_rule( + 'MOODLEOVERFLOWDISCUSSIONVIEWINSIDE', + '/mod/moodleoverflow/discussion.php?d=$1#$2', + ['moodleoverflow_discussion', 'moodleoverflow_post'] + ); return $rules; - } /** @@ -102,49 +107,134 @@ public static function define_decode_rules() { public static function define_restore_log_rules() { $rules = []; - $rules[] = new restore_log_rule('moodleoverflow', 'add', - 'view.php?id={course_module}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'update', - 'view.php?id={course_module}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'view', - 'view.php?id={course_module}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'view moodleoverflow', - 'view.php?id={course_module}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'mark read', - 'view.php?f={moodleoverflow}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'start tracking', - 'view.php?f={moodleoverflow}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'stop tracking', - 'view.php?f={moodloeoverflow}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'subscribe', - 'view.php?f={moodleoverflow}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'unsubscribe', - 'view.php?f={moodleoverflow}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'subscriber', - 'subscribers.php?id={moodleoverflow}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'subscribers', - 'subscribers.php?id={moodleoverflow}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'view subscribers', - 'subscribers.php?id={moodleoverflow}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'add discussion', - 'discussion.php?d={moodleoverflow_discussion}', '{moodleoverflow_discussion}'); - $rules[] = new restore_log_rule('moodleoverflow', 'view discussion', - 'discussion.php?d={moodleoverflow_discussion}', '{moodleoverflow_discussion}'); - $rules[] = new restore_log_rule('moodleoverflow', 'move discussion', - 'discussion.php?d={moodleoverflow_discussion}', '{moodleoverflow_discussion}'); - $rules[] = new restore_log_rule('moodleoverflow', 'delete discussi', - 'view.php?id={course_module}', '{moodleoverflow}', - null, 'delete discussion'); - $rules[] = new restore_log_rule('moodleoverflow', 'delete discussion', - 'view.php?id={course_module}', '{moodleoverflow}'); - $rules[] = new restore_log_rule('moodleoverflow', 'add post', - 'discussion.php?d={moodleoverflow_discussion}&parent={moodleoverflow_post}', '{moodleoverflow_post}'); - $rules[] = new restore_log_rule('moodleoverflow', 'update post', - 'discussion.php?d={moodleoverflow_discussion}&parent={moodleoverflow_post}', '{moodleoverflow_post}'); - $rules[] = new restore_log_rule('moodleoverflow', 'prune post', - 'discussion.php?d={moodleoverflow_discussion}', '{moodleoverflow_post}'); - $rules[] = new restore_log_rule('moodleoverflow', 'delete post', - 'discussion.php?d={moodleoverflow_discussion}', '[post]'); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'add', + 'view.php?id={course_module}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'update', + 'view.php?id={course_module}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'view', + 'view.php?id={course_module}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'view moodleoverflow', + 'view.php?id={course_module}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'mark read', + 'view.php?f={moodleoverflow}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'start tracking', + 'view.php?f={moodleoverflow}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'stop tracking', + 'view.php?f={moodloeoverflow}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'subscribe', + 'view.php?f={moodleoverflow}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'unsubscribe', + 'view.php?f={moodleoverflow}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'subscriber', + 'subscribers.php?id={moodleoverflow}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'subscribers', + 'subscribers.php?id={moodleoverflow}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'view subscribers', + 'subscribers.php?id={moodleoverflow}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'add discussion', + 'discussion.php?d={moodleoverflow_discussion}', + '{moodleoverflow_discussion}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'view discussion', + 'discussion.php?d={moodleoverflow_discussion}', + '{moodleoverflow_discussion}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'move discussion', + 'discussion.php?d={moodleoverflow_discussion}', + '{moodleoverflow_discussion}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'delete discussi', + 'view.php?id={course_module}', + '{moodleoverflow}', + null, + 'delete discussion' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'delete discussion', + 'view.php?id={course_module}', + '{moodleoverflow}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'add post', + 'discussion.php?d={moodleoverflow_discussion}&parent={moodleoverflow_post}', + '{moodleoverflow_post}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'update post', + 'discussion.php?d={moodleoverflow_discussion}&parent={moodleoverflow_post}', + '{moodleoverflow_post}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'prune post', + 'discussion.php?d={moodleoverflow_discussion}', + '{moodleoverflow_post}' + ); + $rules[] = new restore_log_rule( + 'moodleoverflow', + 'delete post', + 'discussion.php?d={moodleoverflow_discussion}', + '[post]' + ); return $rules; } diff --git a/backup/moodle2/restore_moodleoverflow_stepslib.php b/backup/moodle2/restore_moodleoverflow_stepslib.php index 590a37f886a..3ef49245c19 100644 --- a/backup/moodle2/restore_moodleoverflow_stepslib.php +++ b/backup/moodle2/restore_moodleoverflow_stepslib.php @@ -32,7 +32,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class restore_moodleoverflow_activity_structure_step extends restore_activity_structure_step { - /** * Defines structure of path elements to be processed during the restore. * @@ -45,20 +44,34 @@ protected function define_structure() { $paths[] = new restore_path_element('moodleoverflow', '/activity/moodleoverflow'); if ($userinfo) { - $paths[] = new restore_path_element('moodleoverflow_discussion', - '/activity/moodleoverflow/discussions/discussion'); - $paths[] = new restore_path_element('moodleoverflow_post', - '/activity/moodleoverflow/discussions/discussion/posts/post'); - $paths[] = new restore_path_element('moodleoverflow_discuss_sub', - '/activity/moodleoverflow/discussions/discussion/discuss_subs/discuss_sub'); - $paths[] = new restore_path_element('moodleoverflow_rating', - '/activity/moodleoverflow/discussions/discussion/posts/post/ratings/rating'); - $paths[] = new restore_path_element('moodleoverflow_subscription', - '/activity/moodleoverflow/subscriptions/subscription'); - $paths[] = new restore_path_element('moodleoverflow_read', - '/activity/moodleoverflow/readposts/read'); - $paths[] = new restore_path_element('moodleoverflow_track', - '/activity/moodleoverflow/tracking/track'); + $paths[] = new restore_path_element( + 'moodleoverflow_discussion', + '/activity/moodleoverflow/discussions/discussion' + ); + $paths[] = new restore_path_element( + 'moodleoverflow_post', + '/activity/moodleoverflow/discussions/discussion/posts/post' + ); + $paths[] = new restore_path_element( + 'moodleoverflow_discuss_sub', + '/activity/moodleoverflow/discussions/discussion/discuss_subs/discuss_sub' + ); + $paths[] = new restore_path_element( + 'moodleoverflow_rating', + '/activity/moodleoverflow/discussions/discussion/posts/post/ratings/rating' + ); + $paths[] = new restore_path_element( + 'moodleoverflow_subscription', + '/activity/moodleoverflow/subscriptions/subscription' + ); + $paths[] = new restore_path_element( + 'moodleoverflow_read', + '/activity/moodleoverflow/readposts/read' + ); + $paths[] = new restore_path_element( + 'moodleoverflow_track', + '/activity/moodleoverflow/tracking/track' + ); } // Return the paths wrapped into standard activity structure. @@ -178,7 +191,6 @@ protected function process_moodleoverflow_subscription($data) { $newitemid = $DB->insert_record('moodleoverflow_subscriptions', $data); $this->set_mapping('moodleoverflow_subscription', $oldid, $newitemid, true); - } /** diff --git a/classes/anonymous.php b/classes/anonymous.php index 0c7de5b5b8f..d0072efa092 100644 --- a/classes/anonymous.php +++ b/classes/anonymous.php @@ -32,7 +32,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class anonymous { - /** * Used if nothing is anonymous. */ @@ -86,8 +85,11 @@ public static function get_userid_mapping($moodleoverflow, $discussionid) { } if ($moodleoverflow->anonymous == self::QUESTION_ANONYMOUS) { return [ - $DB->get_field('moodleoverflow_posts', 'userid', - ['parent' => 0, 'discussion' => $discussionid]) => get_string('questioner', 'mod_moodleoverflow'), + $DB->get_field( + 'moodleoverflow_posts', + 'userid', + ['parent' => 0, 'discussion' => $discussionid] + ) => get_string('questioner', 'mod_moodleoverflow'), ]; } @@ -96,7 +98,9 @@ public static function get_userid_mapping($moodleoverflow, $discussionid) { 'FROM {moodleoverflow_posts} ' . 'WHERE discussion = :discussion ' . 'GROUP BY userid ' . - 'ORDER BY MIN(created) ASC;', ['discussion' => $discussionid]); + 'ORDER BY MIN(created) ASC;', + ['discussion' => $discussionid] + ); $mapping = []; $questioner = array_shift($userids); @@ -108,5 +112,4 @@ public static function get_userid_mapping($moodleoverflow, $discussionid) { } return $mapping; } - } diff --git a/classes/capabilities.php b/classes/capabilities.php index 581fe7624a6..fe45f94ab08 100644 --- a/classes/capabilities.php +++ b/classes/capabilities.php @@ -34,7 +34,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class capabilities { - /** capability add instance */ const ADD_INSTANCE = 'mod/moodleoverflow:addinstance'; @@ -100,5 +99,4 @@ public static function has(string $capability, context $context, $userid = null) return self::$cache[$key]; } - } diff --git a/classes/discussion/discussion.php b/classes/discussion/discussion.php index bbc99a97773..6ba285a0f54 100644 --- a/classes/discussion/discussion.php +++ b/classes/discussion/discussion.php @@ -27,7 +27,6 @@ // Import namespace from the locallib, needs a check later which namespaces are really needed. use mod_moodleoverflow\anonymous; - // Important namespaces. use mod_moodleoverflow\readtracking; use mod_moodleoverflow\review; @@ -51,7 +50,6 @@ * Accessing these functions directly without the checks from the post control could lead to serious errors. */ class discussion { - /** @var int The discussion ID */ private $id; @@ -107,8 +105,17 @@ class discussion { * @param int $timestart The course ID. * @param int $usermodified The course ID. */ - public function __construct($id, $course, $moodleoverflow, $name, $firstpost, - $userid, $timemodified, $timestart, $usermodified) { + public function __construct( + $id, + $course, + $moodleoverflow, + $name, + $firstpost, + $userid, + $timemodified, + $timestart, + $usermodified + ) { $this->id = $id; $this->course = $course; $this->moodleoverflow = $moodleoverflow; @@ -195,8 +202,16 @@ public static function from_record($record) { * * @return object discussion object without id. */ - public static function construct_without_id($course, $moodleoverflow, $name, $firstpost, - $userid, $timemodified, $timestart, $usermodified) { + public static function construct_without_id( + $course, + $moodleoverflow, + $name, + $firstpost, + $userid, + $timemodified, + $timestart, + $usermodified + ) { $id = null; $instance = new self($id, $course, $moodleoverflow, $name, $firstpost, $userid, $timemodified, $timestart, $usermodified); return $instance; @@ -216,8 +231,20 @@ public function moodleoverflow_add_discussion($prepost) { $this->id = $DB->insert_record('moodleoverflow_discussions', $this->build_db_object()); // Create the first/parent post for the new discussion and add it do the DB. - $post = post::construct_without_id($this->id, 0, $prepost->userid, $prepost->timenow, $prepost->timenow, $prepost->message, - $prepost->messageformat, "", 0, $prepost->reviewed, null, $prepost->formattachments); + $post = post::construct_without_id( + $this->id, + 0, + $prepost->userid, + $prepost->timenow, + $prepost->timenow, + $prepost->message, + $prepost->messageformat, + "", + 0, + $prepost->reviewed, + null, + $prepost->formattachments + ); // Add it to the DB and save the id of the first/parent post. $this->firstpost = $post->moodleoverflow_add_new_post(); @@ -285,7 +312,6 @@ public function moodleoverflow_delete_discussion($prepost) { // The discussion has been deleted. $transaction->allow_commit(); return true; - } catch (Exception $e) { $transaction->rollback($e); } @@ -305,9 +331,20 @@ public function moodleoverflow_add_post_to_discussion($prepost) { $this->posts_check(); // Create the post that will be added to the new discussion. - $post = post::construct_without_id($this->id, $prepost->parentid, $prepost->userid, $prepost->timenow, $prepost->timenow, - $prepost->message, $prepost->messageformat, "", 0, $prepost->reviewed, null, - $prepost->formattachments); + $post = post::construct_without_id( + $this->id, + $prepost->parentid, + $prepost->userid, + $prepost->timenow, + $prepost->timenow, + $prepost->message, + $prepost->messageformat, + "", + 0, + $prepost->reviewed, + null, + $prepost->formattachments + ); // Add the post to the DB. $postid = $post->moodleoverflow_add_new_post(); @@ -536,8 +573,13 @@ public function get_coursemodule() { $this->existence_check(); if (empty($this->cmobject)) { - if (!$this->cmobject = $DB->get_coursemodule_from_instance('moodleoverflow', $this->get_moodleoverflow()->id, - $this->get_moodleoverflow()->course)) { + if ( + !$this->cmobject = $DB->get_coursemodule_from_instance( + 'moodleoverflow', + $this->get_moodleoverflow()->id, + $this->get_moodleoverflow()->course + ) + ) { throw new \moodle_exception('invalidcoursemodule'); } } diff --git a/classes/event/course_module_viewed.php b/classes/event/course_module_viewed.php index 9e807e47220..70b4db7c349 100644 --- a/classes/event/course_module_viewed.php +++ b/classes/event/course_module_viewed.php @@ -35,7 +35,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class course_module_viewed extends \core\event\course_module_viewed { - /** * Initialize the event */ diff --git a/classes/event/discussion_created.php b/classes/event/discussion_created.php index fd59baee365..bf06a7b0788 100644 --- a/classes/event/discussion_created.php +++ b/classes/event/discussion_created.php @@ -67,5 +67,4 @@ public function get_description() { public static function get_name() { return get_string('eventdiscussioncreated', 'mod_moodleoverflow'); } - } diff --git a/classes/event/discussion_deleted.php b/classes/event/discussion_deleted.php index d0abdfb3805..7f52e4c1110 100644 --- a/classes/event/discussion_deleted.php +++ b/classes/event/discussion_deleted.php @@ -38,7 +38,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class discussion_deleted extends \core\event\base { - /** * Init method. * @@ -69,4 +68,3 @@ public static function get_name() { return get_string('eventdiscussiondeleted', 'mod_moodleoverflow'); } } - diff --git a/classes/event/discussion_subscription_created.php b/classes/event/discussion_subscription_created.php index 5dfd3c7bea7..6ae0e049b50 100644 --- a/classes/event/discussion_subscription_created.php +++ b/classes/event/discussion_subscription_created.php @@ -68,5 +68,4 @@ public function get_description() { public static function get_name() { return get_string('eventdiscussionsubscriptioncreated', 'mod_moodleoverflow'); } - } diff --git a/classes/event/discussion_viewed.php b/classes/event/discussion_viewed.php index 4f5996aee9b..69e72cdd319 100644 --- a/classes/event/discussion_viewed.php +++ b/classes/event/discussion_viewed.php @@ -32,7 +32,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class discussion_viewed extends \core\event\base { - /** * Init method. * @@ -63,4 +62,3 @@ public static function get_name() { return get_string('eventdiscussionviewed', 'mod_moodleoverflow'); } } - diff --git a/classes/event/post_deleted.php b/classes/event/post_deleted.php index 9f3ab2bb628..13fa43f08fc 100644 --- a/classes/event/post_deleted.php +++ b/classes/event/post_deleted.php @@ -68,5 +68,4 @@ public function get_description() { public static function get_name() { return get_string('eventpostdeleted', 'mod_moodleoverflow'); } - } diff --git a/classes/event/rating_created.php b/classes/event/rating_created.php index d05a2c83144..656162211cc 100644 --- a/classes/event/rating_created.php +++ b/classes/event/rating_created.php @@ -32,7 +32,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class rating_created extends \core\event\base { - /** * Init method. * diff --git a/classes/event/rating_deleted.php b/classes/event/rating_deleted.php index b20e75df656..c75f2061130 100644 --- a/classes/event/rating_deleted.php +++ b/classes/event/rating_deleted.php @@ -32,7 +32,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class rating_deleted extends \core\event\base { - /** * Init method. * diff --git a/classes/event/rating_updated.php b/classes/event/rating_updated.php index 5fd74d66b7a..533744e0fb7 100644 --- a/classes/event/rating_updated.php +++ b/classes/event/rating_updated.php @@ -32,7 +32,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class rating_updated extends \core\event\base { - /** * Init method. * diff --git a/classes/event/readtracking_disabled.php b/classes/event/readtracking_disabled.php index 360321ca812..ee5ba809aae 100644 --- a/classes/event/readtracking_disabled.php +++ b/classes/event/readtracking_disabled.php @@ -38,7 +38,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class readtracking_disabled extends \core\event\base { - /** * Init method. * diff --git a/classes/event/readtracking_enabled.php b/classes/event/readtracking_enabled.php index afe4f16449e..e12e914f3c8 100644 --- a/classes/event/readtracking_enabled.php +++ b/classes/event/readtracking_enabled.php @@ -38,7 +38,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class readtracking_enabled extends \core\event\base { - /** * Init method. * diff --git a/classes/event/subscription_created.php b/classes/event/subscription_created.php index ec858da3bf8..a26af5357fd 100644 --- a/classes/event/subscription_created.php +++ b/classes/event/subscription_created.php @@ -67,5 +67,4 @@ public function get_description() { public static function get_name() { return get_string('eventsubscriptioncreated', 'mod_moodleoverflow'); } - } diff --git a/classes/event/subscription_deleted.php b/classes/event/subscription_deleted.php index 8544e755770..f61263157d8 100644 --- a/classes/event/subscription_deleted.php +++ b/classes/event/subscription_deleted.php @@ -67,5 +67,4 @@ public function get_description() { public static function get_name() { return get_string('eventsubscriptiondeleted', 'mod_moodleoverflow'); } - } diff --git a/classes/manager/mail_manager.php b/classes/manager/mail_manager.php index 4fe9c432946..44a1124b597 100644 --- a/classes/manager/mail_manager.php +++ b/classes/manager/mail_manager.php @@ -115,13 +115,27 @@ public static function moodleoverflow_send_mails(): bool { // Fill the caches with objects if needed. // Add additional information that were not retrievable from the database to the objects if needed. - self::moodleoverflow_update_mail_caches($record, $coursemodules, $courses, $moodleoverflows, - $discussions, $posts, $authors, $recipients); + self::moodleoverflow_update_mail_caches( + $record, + $coursemodules, + $courses, + $moodleoverflows, + $discussions, + $posts, + $authors, + $recipients + ); // Filter records that are not getting mailed. // Check if the user can see the post. - if (!moodleoverflow_user_can_see_post($moodleoverflows[$record->moodleoverflowid], $discussions[$record->discussionid], - $posts[$record->postid], $coursemodules[$record->cmid])) { + if ( + !moodleoverflow_user_can_see_post( + $moodleoverflows[$record->moodleoverflowid], + $discussions[$record->discussionid], + $posts[$record->postid], + $coursemodules[$record->cmid] + ) + ) { continue; } @@ -183,8 +197,11 @@ public static function moodleoverflow_send_mails(): bool { // The email object is build. Now build all data that is needed for the event that really send the mail. // Build post subject. - $subject = html_to_text(get_string('postmailsubject', 'moodleoverflow', - ['subject' => $email->get_subject(), 'courseshortname' => $email->get_coursename()]), 0); + $subject = html_to_text(get_string( + 'postmailsubject', + 'moodleoverflow', + ['subject' => $email->get_subject(), 'courseshortname' => $email->get_coursename()] + ), 0); // Finally: send the notification mail. $userto = $recipients[$record->usertoid]; @@ -332,9 +349,16 @@ public static function moodleoverflow_get_unmailed_posts($starttime, $endtime): * * @return void */ - public static function moodleoverflow_update_mail_caches(object $record, array &$coursemodules, array &$courses, - array &$moodleoverflows, array &$discussions, array &$posts, - array &$authors, array &$recipients ): void { + public static function moodleoverflow_update_mail_caches( + object $record, + array &$coursemodules, + array &$courses, + array &$moodleoverflows, + array &$discussions, + array &$posts, + array &$authors, + array &$recipients + ): void { // Define cache types and their corresponding record properties. $cachetypes = [ 'coursemodules' => ['id' => 'cmid', 'groupingid' => 'cmgroupingid'], @@ -390,8 +414,10 @@ public static function moodleoverflow_process_maildigest_record(object $data): v global $DB; // LEARNWEB-TODO: Rename database table attribute names. Rethink the table structure. What should the mail have? // If the record exists, update it. If not, insert a new record. - if ($dbrecord = $DB->get_record('moodleoverflow_mail_info', ['userid' => $data->usertoid, 'courseid' => $data->courseid, - 'forumid' => $data->moodleoverflowid, 'forumdiscussionid' => $data->discussionid, ], 'numberofposts, id')) { + if ( + $dbrecord = $DB->get_record('moodleoverflow_mail_info', ['userid' => $data->usertoid, 'courseid' => $data->courseid, + 'forumid' => $data->moodleoverflowid, 'forumdiscussionid' => $data->discussionid, ], 'numberofposts, id') + ) { $dbrecord->numberofposts++; $DB->update_record('moodleoverflow_mail_info', $dbrecord); } else { @@ -433,6 +459,5 @@ public static function moodleoverflow_mark_old_posts_as_mailed($endtime) { WHERE (created < :endtime) AND mailed IN (:mailedpending, :mailedreviewsent) AND reviewed = 1"; return $DB->execute($sql, $params); - } } diff --git a/classes/observer.php b/classes/observer.php index 8dc215b203c..44aff1b9aba 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -28,7 +28,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class mod_moodleoverflow_observer { - /** * Triggered via user_enrolment_deleted event. * @@ -42,7 +41,6 @@ public static function user_enrolment_deleted(\core\event\user_enrolment_deleted // Check if the user was enrolled. if ($cp->lastenrol) { - // Get the moodleoverflow instances from which the user was unenrolled from. $moodleoverflows = $DB->get_records('moodleoverflow', ['course' => $cp->courseid], '', 'id'); @@ -52,7 +50,7 @@ public static function user_enrolment_deleted(\core\event\user_enrolment_deleted } // Get the sql parameters for the moodleoverflow instances and add the user ID. - list($select, $params) = $DB->get_in_or_equal(array_keys($moodleoverflows), SQL_PARAMS_NAMED); + [$select, $params] = $DB->get_in_or_equal(array_keys($moodleoverflows), SQL_PARAMS_NAMED); $params['userid'] = $cp->userid; // Delete all records that are connected to those moodleoverflow instances. @@ -100,7 +98,6 @@ public static function role_assigned(\core\event\role_assigned $event) { // Loop through all moodleoverflows. foreach ($moodleoverflows as $moodleoverflow) { - // If user doesn't have allowforcesubscribe capability then don't subscribe. // Retrieve the context of the module. @@ -128,7 +125,6 @@ public static function course_module_created(\core\event\course_module_created $ // Check if a moodleoverflow instance was created. if ($event->other['modulename'] === 'moodleoverflow') { - // Require the library. require_once($CFG->dirroot . '/mod/moodleoverflow/lib.php'); diff --git a/classes/output/email/renderer.php b/classes/output/email/renderer.php index fdf8ef3f75e..6c812fc1022 100644 --- a/classes/output/email/renderer.php +++ b/classes/output/email/renderer.php @@ -37,7 +37,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class renderer extends \mod_moodleoverflow_renderer { - /** * The template name for this renderer. * @@ -58,9 +57,13 @@ public function moodleoverflow_email_template() { public function format_message_text($cm, $post) { // Convert the message. $message = file_rewrite_pluginfile_urls( - $post->message, 'pluginfile.php', + $post->message, + 'pluginfile.php', \context_module::instance($cm->id)->id, - 'mod_moodleoverflow', 'post', $post->id); + 'mod_moodleoverflow', + 'post', + $post->id + ); // Initiate some options. $options = new \stdClass(); diff --git a/classes/output/email/renderer_textemail.php b/classes/output/email/renderer_textemail.php index 4d8c72d29b2..da7e574d16e 100644 --- a/classes/output/email/renderer_textemail.php +++ b/classes/output/email/renderer_textemail.php @@ -36,7 +36,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class renderer_textemail extends renderer { - /** * The template name for this renderer. * @@ -56,9 +55,14 @@ public function moodleoverflow_email_template() { */ public function format_message_text($cm, $post) { // Format the text. - $message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php', + $message = file_rewrite_pluginfile_urls( + $post->message, + 'pluginfile.php', \context_module::instance($cm->id)->id, - 'mod_moodleoverflow', 'post', $post->id); + 'mod_moodleoverflow', + 'post', + $post->id + ); // Print the message. return format_text_email($message, $post->messageformat); diff --git a/classes/output/moodleoverflow_email.php b/classes/output/moodleoverflow_email.php index f5c57f7d54b..13d1d90b92d 100644 --- a/classes/output/moodleoverflow_email.php +++ b/classes/output/moodleoverflow_email.php @@ -35,7 +35,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class moodleoverflow_email implements \renderable, \templatable { - /** * The course that the moodleoverflow post is in. * @@ -316,7 +315,8 @@ public function get_coursename() { public function get_courselink() { $link = new \moodle_url( // Posts are viewed on the topic. - '/course/view.php', [ + '/course/view.php', + [ 'id' => $this->course->id, ] ); @@ -420,7 +420,8 @@ public function is_firstpost() { */ public function get_replylink() { return new \moodle_url( - '/mod/moodleoverflow/post.php', [ + '/mod/moodleoverflow/post.php', + [ 'reply' => $this->post->id, ] ); @@ -432,12 +433,17 @@ public function get_replylink() { * @return string */ public function get_unsubscribemoodleoverflowlink() { - if (!subscriptions::is_subscribable($this->moodleoverflow, - \context_module::instance($this->cm->id))) { + if ( + !subscriptions::is_subscribable( + $this->moodleoverflow, + \context_module::instance($this->cm->id) + ) + ) { return null; } $link = new \moodle_url( - '/mod/moodleoverflow/subscribe.php', [ + '/mod/moodleoverflow/subscribe.php', + [ 'id' => $this->moodleoverflow->id, ] ); @@ -465,7 +471,8 @@ public function get_parentpostlink() { protected function get_discussionurl() { return new \moodle_url( // Posts are viewed on the topic. - '/mod/moodleoverflow/discussion.php', [ + '/mod/moodleoverflow/discussion.php', + [ // Within a discussion. 'd' => $this->discussion->id, ] @@ -491,7 +498,8 @@ public function get_discussionlink() { public function get_moodleoverflowindexlink() { $link = new \moodle_url( // Posts are viewed on the topic. - '/mod/moodleoverflow/index.php', [ + '/mod/moodleoverflow/index.php', + [ 'id' => $this->course->id, ] ); @@ -507,7 +515,8 @@ public function get_moodleoverflowindexlink() { public function get_moodleoverflowviewlink() { $link = new \moodle_url( // Posts are viewed on the topic. - '/mod/moodleoverflow/view.php', [ + '/mod/moodleoverflow/view.php', + [ 'm' => $this->moodleoverflow->id, ] ); @@ -526,7 +535,8 @@ public function get_authorlink() { } $link = new \moodle_url( - '/user/view.php', [ + '/user/view.php', + [ 'id' => $this->post->userid, 'course' => $this->course->id, ] diff --git a/classes/post/post.php b/classes/post/post.php index 942d5ddbd8d..da379362468 100644 --- a/classes/post/post.php +++ b/classes/post/post.php @@ -56,7 +56,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class post { - // Attributes. The most important attributes are private and can only be changed by internal functions. // Other attributes can be accessed directly. @@ -134,8 +133,21 @@ class post { * @param int $timereviewed The time where the post was reviewed * @param object $formattachments Information about attachments of the post_form */ - public function __construct($id, $discussion, $parent, $userid, $created, $modified, $message, - $messageformat, $attachment, $mailed, $reviewed, $timereviewed, $formattachments = false) { + public function __construct( + $id, + $discussion, + $parent, + $userid, + $created, + $modified, + $message, + $messageformat, + $attachment, + $mailed, + $reviewed, + $timereviewed, + $formattachments = false + ) { $this->id = $id; $this->discussion = $discussion; $this->parent = $parent; @@ -218,8 +230,20 @@ public static function from_record($record): post { $timereviewed = $record->timereviewed; } - return new self($id, $discussion, $parent, $userid, $created, $modified, $message, $messageformat, $attachment, $mailed, - $reviewed, $timereviewed); + return new self( + $id, + $discussion, + $parent, + $userid, + $created, + $modified, + $message, + $messageformat, + $attachment, + $mailed, + $reviewed, + $timereviewed + ); } /** @@ -240,11 +264,36 @@ public static function from_record($record): post { * * @return object post object without id */ - public static function construct_without_id($discussion, $parent, $userid, $created, $modified, $message, - $messageformat, $attachment, $mailed, $reviewed, $timereviewed, $formattachments = false) { + public static function construct_without_id( + $discussion, + $parent, + $userid, + $created, + $modified, + $message, + $messageformat, + $attachment, + $mailed, + $reviewed, + $timereviewed, + $formattachments = false + ) { $id = null; - return new self($id, $discussion, $parent, $userid, $created, $modified, $message, $messageformat, $attachment, $mailed, - $reviewed, $timereviewed, $formattachments); + return new self( + $id, + $discussion, + $parent, + $userid, + $created, + $modified, + $message, + $messageformat, + $attachment, + $mailed, + $reviewed, + $timereviewed, + $formattachments + ); } // Post Functions. @@ -264,8 +313,15 @@ public function moodleoverflow_add_new_post() { // Save draft files to permanent file area. $context = \context_module::instance($this->get_coursemodule()->id); $draftid = file_get_submitted_draft_itemid('introeditor'); - $this->message = file_save_draft_area_files($draftid, $context->id, 'mod_moodleoverflow', 'post', - $this->id, mod_moodleoverflow_post_form::editor_options($context, $this->id), $this->message); + $this->message = file_save_draft_area_files( + $draftid, + $context->id, + 'mod_moodleoverflow', + 'post', + $this->id, + mod_moodleoverflow_post_form::editor_options($context, $this->id), + $this->message + ); $DB->update_record('moodleoverflow_posts', $this->build_db_object()); // Update the attachments. This happens after the DB update call, as this function changes the DB record as well. @@ -328,12 +384,24 @@ public function moodleoverflow_delete_post($deletechildren) { // Delete the attachments. $fs = get_file_storage(); $context = \context_module::instance($coursemoduleid); - $attachments = $fs->get_area_files($context->id, 'mod_moodleoverflow', 'attachment', - $this->id, "filename", true); + $attachments = $fs->get_area_files( + $context->id, + 'mod_moodleoverflow', + 'attachment', + $this->id, + "filename", + true + ); foreach ($attachments as $attachment) { // Get file. - $file = $fs->get_file($context->id, 'mod_moodleoverflow', 'attachment', $this->id, - $attachment->get_filepath(), $attachment->get_filename()); + $file = $fs->get_file( + $context->id, + 'mod_moodleoverflow', + 'attachment', + $this->id, + $attachment->get_filepath(), + $attachment->get_filename() + ); // Delete it if it exists. if ($file) { $file->delete(); @@ -391,8 +459,15 @@ public function moodleoverflow_edit_post($time, $postmessage, $messageformat, $f // Update the message and save draft files to permanent file area. $context = \context_module::instance($this->get_coursemodule()->id); $draftid = file_get_submitted_draft_itemid('introeditor'); - $this->message = file_save_draft_area_files($draftid, $context->id, 'mod_moodleoverflow', 'post', - $this->id, mod_moodleoverflow_post_form::editor_options($context, $this->id), $postmessage); + $this->message = file_save_draft_area_files( + $draftid, + $context->id, + 'mod_moodleoverflow', + 'post', + $this->id, + mod_moodleoverflow_post_form::editor_options($context, $this->id), + $postmessage + ); // Update the record in the database. $DB->update_record('moodleoverflow_posts', $this->build_db_object()); @@ -453,8 +528,14 @@ public function moodleoverflow_add_attachment() { $context = \context_module::instance($this->get_coursemodule()->id); $info = file_get_draft_area_info($this->formattachments); $present = ($info['filecount'] > 0) ? '1' : ''; - file_save_draft_area_files($this->formattachments, $context->id, 'mod_moodleoverflow', 'attachment', $this->id, - \mod_moodleoverflow_post_form::attachment_options($this->get_moodleoverflow())); + file_save_draft_area_files( + $this->formattachments, + $context->id, + 'mod_moodleoverflow', + 'attachment', + $this->id, + \mod_moodleoverflow_post_form::attachment_options($this->get_moodleoverflow()) + ); $DB->set_field('moodleoverflow_posts', 'attachment', $present, ['id' => $this->id]); } @@ -484,11 +565,20 @@ public function moodleoverflow_get_attachments() { $attachments[$i] = []; $attachments[$i]['filename'] = $file->get_filename(); $mimetype = $file->get_mimetype(); - $iconimage = $OUTPUT->pix_icon(file_file_icon($file), - get_mimetype_description($file), 'moodle', - ['class' => 'icon']); - $path = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), - $file->get_itemid(), $file->get_filepath(), $file->get_filename()); + $iconimage = $OUTPUT->pix_icon( + file_file_icon($file), + get_mimetype_description($file), + 'moodle', + ['class' => 'icon'] + ); + $path = moodle_url::make_pluginfile_url( + $file->get_contextid(), + $file->get_component(), + $file->get_filearea(), + $file->get_itemid(), + $file->get_filepath(), + $file->get_filename() + ); $attachments[$i]['icon'] = $iconimage; $attachments[$i]['filepath'] = $path; diff --git a/classes/post/post_control.php b/classes/post/post_control.php index d2fb43be86e..513b0648408 100644 --- a/classes/post/post_control.php +++ b/classes/post/post_control.php @@ -70,7 +70,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class post_control { - /** @var string the Interaction type, the interactions are: * - create (creates a new discussion with a first post) * - reply (replies to a existing post, can be an answer or a comment) @@ -117,17 +116,14 @@ public function detect_interaction(stdClass $urlparameter): void { $this->interaction = 'create'; $this->info->moodleoverflowid = $urlparameter->create; $this->build_prepost_create($this->info->moodleoverflowid); - } else if ($urlparameter->edit) { $this->interaction = 'edit'; $this->info->editpostid = $urlparameter->edit; $this->build_prepost_edit($this->info->editpostid); - } else if ($urlparameter->reply) { $this->interaction = 'reply'; $this->info->replypostid = $urlparameter->reply; $this->build_prepost_reply($this->info->replypostid); - } else if ($urlparameter->delete) { $this->interaction = 'delete'; $this->info->deletepostid = $urlparameter->delete; @@ -303,18 +299,24 @@ private function build_prepost_edit(int $editpostid): void { // as the review class should be refactored with the new way of working with posts. $alreadyreviewed = review::should_post_be_reviewed($this->info->relatedpost->get_db_object(), $this->info->moodleoverflow) && $this->info->relatedpost->reviewed; - if (($beyondtime || $alreadyreviewed) && !has_capability('mod/moodleoverflow:editanypost', - $this->info->modulecontext)) { - throw new moodle_exception('maxtimehaspassed', 'moodleoverflow', '', - format_time(get_config('moodleoverflow', 'maxeditingtime'))); + if ( + ($beyondtime || $alreadyreviewed) && !has_capability( + 'mod/moodleoverflow:editanypost', + $this->info->modulecontext + ) + ) { + throw new moodle_exception( + 'maxtimehaspassed', + 'moodleoverflow', + '', + format_time(get_config('moodleoverflow', 'maxeditingtime')) + ); } // If the current user is not the one who posted this post. if ($this->info->relatedpost->get_userid() != $USER->id) { - // Check if the current user has not the capability to edit any post. if (!has_capability('mod/moodleoverflow:editanypost', $this->info->modulecontext)) { - // Display the error. Capabilities are missing. throw new moodle_exception('cannoteditposts', 'moodleoverflow'); } @@ -369,8 +371,10 @@ private function execute_create(object $form): void { $this->check_user_can_create_discussion(); // Set the post to not reviewed if questions should be reviewed and the user is not a reviewed themselves. - if (review::get_review_level($this->info->moodleoverflow) >= review::QUESTIONS && - !capabilities::has(capabilities::REVIEW_POST, $this->info->modulecontext, $USER->id)) { + if ( + review::get_review_level($this->info->moodleoverflow) >= review::QUESTIONS && + !capabilities::has(capabilities::REVIEW_POST, $this->info->modulecontext, $USER->id) + ) { $this->prepost->reviewed = 0; } else { $this->prepost->reviewed = 1; @@ -380,9 +384,16 @@ private function execute_create(object $form): void { $this->prepost->subject = $form->subject; // Create the discussion object. - $discussion = discussion::construct_without_id($this->prepost->courseid, $this->prepost->moodleoverflowid, - $this->prepost->subject, 0, $this->prepost->userid, - $this->prepost->timenow, $this->prepost->timenow, $this->prepost->userid); + $discussion = discussion::construct_without_id( + $this->prepost->courseid, + $this->prepost->moodleoverflowid, + $this->prepost->subject, + 0, + $this->prepost->userid, + $this->prepost->timenow, + $this->prepost->timenow, + $this->prepost->userid + ); if (!$discussion->moodleoverflow_add_discussion($this->prepost)) { throw new moodle_exception('couldnotadd', 'moodleoverflow'); } @@ -398,9 +409,12 @@ private function execute_create(object $form): void { // Subscribe to this thread. // Please be aware that in future the use of get_db_object() should be replaced with only $this->info->discussion, // as the subscription class should be refactored with the new way of working with posts. - subscriptions::moodleoverflow_post_subscription($form, $this->info->moodleoverflow, - $discussion->get_db_object(), - $this->info->modulecontext); + subscriptions::moodleoverflow_post_subscription( + $form, + $this->info->moodleoverflow, + $discussion->get_db_object(), + $this->info->modulecontext + ); // Define the location to redirect the user after successfully posting. $redirectto = new moodle_url('/mod/moodleoverflow/view.php', ['m' => $form->moodleoverflow]); @@ -418,8 +432,10 @@ private function execute_reply(object $form): void { $this->check_user_can_create_reply(); // Set to not reviewed, if posts should be reviewed, and user is not a reviewer themselves. - if (review::get_review_level($this->info->moodleoverflow) == review::EVERYTHING && - !has_capability('mod/moodleoverflow:reviewpost', \context_module::instance($this->info->cm->id))) { + if ( + review::get_review_level($this->info->moodleoverflow) == review::EVERYTHING && + !has_capability('mod/moodleoverflow:reviewpost', \context_module::instance($this->info->cm->id)) + ) { $this->prepost->reviewed = 0; } else { $this->prepost->reviewed = 1; @@ -432,8 +448,11 @@ private function execute_reply(object $form): void { // The creation was successful. $redirectmessage = html_writer::tag('p', get_string("postaddedsuccess", "moodleoverflow")); - $redirectmessage .= html_writer::tag('p', get_string("postaddedtimeleft", "moodleoverflow", - format_time(get_config('moodleoverflow', 'maxeditingtime')))); + $redirectmessage .= html_writer::tag('p', get_string( + "postaddedtimeleft", + "moodleoverflow", + format_time(get_config('moodleoverflow', 'maxeditingtime')) + )); // Trigger the post created event. $params = ['context' => $this->info->modulecontext, 'objectid' => $newpostid, @@ -447,15 +466,19 @@ private function execute_reply(object $form): void { // Subscribe to this thread. // Please be aware that in future the use of build_db_object() should be replaced with only $this->info->discussion, // as the subscription class should be refactored with the new way of working with posts. - subscriptions::moodleoverflow_post_subscription($form, $this->info->moodleoverflow, - $this->info->discussion->get_db_object(), - $this->info->modulecontext); + subscriptions::moodleoverflow_post_subscription( + $form, + $this->info->moodleoverflow, + $this->info->discussion->get_db_object(), + $this->info->modulecontext + ); // Define the location to redirect the user after successfully posting. - $redirectto = new moodle_url('/mod/moodleoverflow/discussion.php', - ['d' => $this->prepost->discussionid, 'p' => $newpostid]); + $redirectto = new moodle_url( + '/mod/moodleoverflow/discussion.php', + ['d' => $this->prepost->discussionid, 'p' => $newpostid] + ); redirect(\moodleoverflow_go_back_to($redirectto), $redirectmessage, null, \core\output\notification::NOTIFY_SUCCESS); - } /** @@ -502,8 +525,10 @@ private function execute_edit(object $form): void { $event->trigger(); // Define the location to redirect the user after successfully editing. - $redirectto = new moodle_url('/mod/moodleoverflow/discussion.php', - ['d' => $this->prepost->discussionid, 'p' => $form->edit]); + $redirectto = new moodle_url( + '/mod/moodleoverflow/discussion.php', + ['d' => $this->prepost->discussionid, 'p' => $form->edit] + ); redirect(moodleoverflow_go_back_to($redirectto), $redirectmessage, null, \core\output\notification::NOTIFY_SUCCESS); } @@ -518,8 +543,10 @@ public function execute_delete(): void { // Check if the user has the capability to delete the post. $timepassed = time() - $this->info->relatedpost->created; - $SESSION->errorreturnurl = new moodle_url('/mod/moodleoverflow/discussion.php', - ['d' => $this->info->discussion->get_id()]); + $SESSION->errorreturnurl = new moodle_url( + '/mod/moodleoverflow/discussion.php', + ['d' => $this->info->discussion->get_id()] + ); if (($timepassed > get_config('moodleoverflow', 'maxeditingtime')) && !$this->info->deleteanypost) { throw new moodle_exception('cannotdeletepost', 'moodleoverflow'); } @@ -574,13 +601,23 @@ public function build_postform(array $pageparams) { // Prepare the attachments. $draftitemid = file_get_submitted_draft_itemid('attachments'); - file_prepare_draft_area($draftitemid, $this->info->modulecontext->id, 'mod_moodleoverflow', 'attachment', - empty($this->prepost->postid) ? null : $this->prepost->postid, - mod_moodleoverflow_post_form::attachment_options($this->info->moodleoverflow)); + file_prepare_draft_area( + $draftitemid, + $this->info->modulecontext->id, + 'mod_moodleoverflow', + 'attachment', + empty($this->prepost->postid) ? null : $this->prepost->postid, + mod_moodleoverflow_post_form::attachment_options($this->info->moodleoverflow) + ); // If the post is anonymous, attachments should have an anonymous author when editing the attachment. - if ($draftitemid && $this->interaction == 'edit' && anonymous::is_post_anonymous($this->info->discussion, - $this->info->moodleoverflow, $this->prepost->userid)) { + if ( + $draftitemid && $this->interaction == 'edit' && anonymous::is_post_anonymous( + $this->info->discussion, + $this->info->moodleoverflow, + $this->prepost->userid + ) + ) { $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) { @@ -607,8 +644,11 @@ public function build_postform(array $pageparams) { if ($this->prepost->messageformat == FORMAT_HTML) { $data->name = html_writer::tag('a', $CFG->wwwroot . '/user/view.php?id' . $USER->id . '&course=' . $this->prepost->courseid . '">' . fullname($USER)); - $this->prepost->message .= html_writer::tag('p', html_writer::tag('span', - get_string('editedby', 'moodleoverflow', $data), ["class" => "edited"])); + $this->prepost->message .= html_writer::tag('p', html_writer::tag( + 'span', + get_string('editedby', 'moodleoverflow', $data), + ["class" => "edited"] + )); } else { $data->name = fullname($USER); $this->prepost->message .= "\n\n(" . get_string('editedby', 'moodleoverflow', $data) . ')'; @@ -634,8 +674,7 @@ public function build_postform(array $pageparams) { 'discussion' => $this->prepost->discussionid, 'course' => $this->prepost->courseid, ] - + $pageparams - ); + + $pageparams); return $mformpost; } @@ -741,7 +780,7 @@ private function assemble_prepost(): void { */ private function check_interaction($interaction): void { if ($this->interaction != $interaction) { - throw new moodle_exception('wronginteraction' , 'moodleoverflow'); + throw new moodle_exception('wronginteraction', 'moodleoverflow'); } } @@ -770,8 +809,13 @@ private function check_course_exists(int $courseid): object { * @throws moodle_exception */ private function check_coursemodule_exists(int $moodleoverflowid, int $courseid): object { - if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflowid, - $courseid)) { + if ( + !$cm = get_coursemodule_from_instance( + 'moodleoverflow', + $moodleoverflowid, + $courseid + ) + ) { throw new moodle_exception('invalidcoursemodule'); } return $cm; @@ -890,9 +934,11 @@ private function catch_unenrolled(): void { if (enrol_selfenrol_available($this->info->course->id)) { $SESSION->wantsurl = qualified_me(); $SESSION->enrolcancel = get_local_referer(false); - redirect(new moodle_url('/enrol/index.php', ['id' => $this->info->course->id, + redirect( + new moodle_url('/enrol/index.php', ['id' => $this->info->course->id, 'returnurl' => '/mod/moodleoverflow/view.php?m=' . $this->info->moodleoverflow->id, ]), - get_string('youneedtoenrol')); + get_string('youneedtoenrol') + ); } } } diff --git a/classes/post_form.php b/classes/post_form.php index 58212feee88..be610b09d7f 100644 --- a/classes/post_form.php +++ b/classes/post_form.php @@ -35,7 +35,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class mod_moodleoverflow_post_form extends moodleform { - /** * Form definition. * @@ -59,15 +58,24 @@ public function definition() { $modform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); // The message. - $modform->addElement('editor', 'message', get_string('message', 'moodleoverflow'), null, - self::editor_options($modcontext, (empty($post->id) ? null : $post->id))); + $modform->addElement( + 'editor', + 'message', + get_string('message', 'moodleoverflow'), + null, + self::editor_options($modcontext, (empty($post->id) ? null : $post->id)) + ); $modform->setType('message', PARAM_RAW); $modform->addRule('message', get_string('required'), 'required', null, 'client'); if (moodleoverflow_can_create_attachment($moodleoverflow, $modcontext)) { - $modform->addElement('filemanager', 'attachments', + $modform->addElement( + 'filemanager', + 'attachments', get_string('attachment', 'moodleoverflow'), - null, self::attachment_options($moodleoverflow)); + null, + self::attachment_options($moodleoverflow) + ); $modform->addHelpButton('attachments', 'attachment', 'moodleoverflow'); } diff --git a/classes/privacy/data_export_helper.php b/classes/privacy/data_export_helper.php index 4e23c43fcab..32a886c4a59 100644 --- a/classes/privacy/data_export_helper.php +++ b/classes/privacy/data_export_helper.php @@ -44,7 +44,7 @@ class data_export_helper { public static function export_discussion_data($userid, array $mappings) { global $DB; // Find all of the discussions, and discussion subscriptions for this forum. - list($foruminsql, $forumparams) = $DB->get_in_or_equal(array_keys($mappings), SQL_PARAMS_NAMED); + [$foruminsql, $forumparams] = $DB->get_in_or_equal(array_keys($mappings), SQL_PARAMS_NAMED); $sql = "SELECT d.*, @@ -83,7 +83,9 @@ public static function export_discussion_data($userid, array $mappings) { ]; // Store the discussion content. writer::with_context($context)->export_data( - static::get_discussion_area($discussion), $discussiondata); + static::get_discussion_area($discussion), + $discussiondata + ); // Forum discussions do not have any files associately directly with them. } $discussions->close(); @@ -103,7 +105,7 @@ public static function export_all_posts($userid, array $mappings) { global $DB; // Find all of the posts, and post subscriptions for this forum. - list($foruminsql, $forumparams) = $DB->get_in_or_equal(array_keys($mappings), SQL_PARAMS_NAMED); + [$foruminsql, $forumparams] = $DB->get_in_or_equal(array_keys($mappings), SQL_PARAMS_NAMED); $sql = "SELECT p.discussion AS id, @@ -241,7 +243,12 @@ protected static function export_post_data($userid, \context $context, $postarea 'author_was_you' => transform::yesno($post->userid == $userid), ]; $postdata->message = writer::with_context($context)->rewrite_pluginfile_urls( - $postarea, 'mod_moodleoverflow', 'attachment', $post->id, $post->message); + $postarea, + 'mod_moodleoverflow', + 'attachment', + $post->id, + $post->message + ); $postdata->message = format_text($postdata->message, $post->messageformat, (object) [ 'para' => false, @@ -250,7 +257,11 @@ protected static function export_post_data($userid, \context $context, $postarea // Store the post and the associated files. writer::with_context($context)->export_data($postarea, $postdata)->export_area_files( - $postarea, 'mod_moodleoverflow', 'attachment', $post->id); + $postarea, + 'mod_moodleoverflow', + 'attachment', + $post->id + ); if ($post->userid == $userid) { // Store all ratings against this post as the post belongs to the user. All ratings on it are ratings of their content. @@ -322,7 +333,11 @@ public static function export_subscription_data(\stdClass $forum) { if (null !== $forum->subscribed) { // The user is subscribed to this forum. writer::with_context(\context_module::instance($forum->cmid))->export_metadata( - [], 'subscriptionpreference', 1, get_string('privacy:subscribedtoforum', 'mod_moodleoverflow')); + [], + 'subscriptionpreference', + 1, + get_string('privacy:subscribedtoforum', 'mod_moodleoverflow') + ); return true; } @@ -352,11 +367,11 @@ protected static function export_discussion_subscription_data(\context_module $c break; } writer::with_context($context)->export_metadata( - $area, - 'subscriptionpreference', - $discussion->preference, - get_string('privacy:discussionsubscriptionpreference', 'mod_moodleoverflow', $a) - ); + $area, + 'subscriptionpreference', + $discussion->preference, + get_string('privacy:discussionsubscriptionpreference', 'mod_moodleoverflow', $a) + ); return true; } @@ -376,7 +391,11 @@ public static function export_tracking_data(\stdClass $forum) { if (null !== $forum->tracked) { // The user has a main preference to track all forums, but has opted out of this one. writer::with_context(\context_module::instance($forum->cmid))->export_metadata( - [], 'trackreadpreference', 0, get_string('privacy:readtrackingdisabled', 'mod_moodleoverflow')); + [], + 'trackreadpreference', + 0, + get_string('privacy:readtrackingdisabled', 'mod_moodleoverflow') + ); return true; } @@ -393,7 +412,11 @@ public static function export_tracking_data(\stdClass $forum) { public static function export_grade_data(\stdClass $forum) { if ($forum->grade) { writer::with_context(\context_module::instance($forum->cmid))->export_metadata( - [], 'grade', $forum->grade, get_string('privacy:grade', 'mod_moodleoverflow')); + [], + 'grade', + $forum->grade, + get_string('privacy:grade', 'mod_moodleoverflow') + ); return true; } @@ -416,14 +439,14 @@ protected static function export_read_data(\context_module $context, array $post 'lastread' => $post->lastread, ]; writer::with_context($context)->export_metadata( - $postarea, - 'postread', - (object) [ + $postarea, + 'postread', + (object) [ 'firstread' => $post->firstread, 'lastread' => $post->lastread, ], - get_string('privacy:postwasread', 'mod_moodleoverflow', $a) - ); + get_string('privacy:postwasread', 'mod_moodleoverflow', $a) + ); return true; } diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php index c1df6fe1f03..83829b66718 100644 --- a/classes/privacy/provider.php +++ b/classes/privacy/provider.php @@ -25,6 +25,7 @@ namespace mod_moodleoverflow\privacy; use core_privacy\local\request\approved_userlist; +use core_privacy\local\request\core_userlist_provider; use core_privacy\local\request\userlist; use core_privacy\local\metadata\collection; use core_privacy\local\request\approved_contextlist; @@ -39,10 +40,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class provider implements + core_userlist_provider, \core_privacy\local\metadata\provider, - \core_privacy\local\request\plugin\provider, - \core_privacy\local\request\core_userlist_provider { - + \core_privacy\local\request\plugin\provider { /** * Return the fields which contain personal data. * @@ -51,16 +51,19 @@ class provider implements * @return collection the updated collection of metadata items. */ public static function get_metadata(collection $collection): collection { - $collection->add_database_table('moodleoverflow_discussions', + $collection->add_database_table( + 'moodleoverflow_discussions', [ 'name' => 'privacy:metadata:moodleoverflow_discussions:name', 'userid' => 'privacy:metadata:moodleoverflow_discussions:userid', 'timemodified' => 'privacy:metadata:moodleoverflow_discussions:timemodified', 'usermodified' => 'privacy:metadata:moodleoverflow_discussions:usermodified', ], - 'privacy:metadata:moodleoverflow_discussions'); + 'privacy:metadata:moodleoverflow_discussions' + ); - $collection->add_database_table('moodleoverflow_posts', + $collection->add_database_table( + 'moodleoverflow_posts', [ 'discussion' => 'privacy:metadata:moodleoverflow_posts:discussion', 'parent' => 'privacy:metadata:moodleoverflow_posts:parent', @@ -69,9 +72,11 @@ public static function get_metadata(collection $collection): collection { 'modified' => 'privacy:metadata:moodleoverflow_posts:modified', 'message' => 'privacy:metadata:moodleoverflow_posts:message', ], - 'privacy:metadata:moodleoverflow_posts'); + 'privacy:metadata:moodleoverflow_posts' + ); - $collection->add_database_table('moodleoverflow_read', + $collection->add_database_table( + 'moodleoverflow_read', [ 'userid' => 'privacy:metadata:moodleoverflow_read:userid', 'discussionid' => 'privacy:metadata:moodleoverflow_read:discussionid', @@ -79,24 +84,30 @@ public static function get_metadata(collection $collection): collection { 'firstread' => 'privacy:metadata:moodleoverflow_read:firstread', 'lastread' => 'privacy:metadata:moodleoverflow_read:lastread', ], - 'privacy:metadata:moodleoverflow_read'); + 'privacy:metadata:moodleoverflow_read' + ); - $collection->add_database_table('moodleoverflow_subscriptions', + $collection->add_database_table( + 'moodleoverflow_subscriptions', [ 'userid' => 'privacy:metadata:moodleoverflow_subscriptions:userid', 'moodleoverflow' => 'privacy:metadata:moodleoverflow_subscriptions:moodleoverflow', ], - 'privacy:metadata:moodleoverflow_subscriptions'); + 'privacy:metadata:moodleoverflow_subscriptions' + ); - $collection->add_database_table('moodleoverflow_discuss_subs', + $collection->add_database_table( + 'moodleoverflow_discuss_subs', [ 'userid' => 'privacy:metadata:moodleoverflow_discuss_subs:userid', 'discussion' => 'privacy:metadata:moodleoverflow_discuss_subs:discussion', 'preference' => 'privacy:metadata:moodleoverflow_discuss_subs:preference', ], - 'privacy:metadata:moodleoverflow_discuss_subs'); + 'privacy:metadata:moodleoverflow_discuss_subs' + ); - $collection->add_database_table('moodleoverflow_ratings', + $collection->add_database_table( + 'moodleoverflow_ratings', [ 'userid' => 'privacy:metadata:moodleoverflow_ratings:userid', 'postid' => 'privacy:metadata:moodleoverflow_ratings:postid', @@ -104,24 +115,30 @@ public static function get_metadata(collection $collection): collection { 'firstrated' => 'privacy:metadata:moodleoverflow_ratings:firstrated', 'lastchanged' => 'privacy:metadata:moodleoverflow_ratings:lastchanged', ], - 'privacy:metadata:moodleoverflow_ratings'); + 'privacy:metadata:moodleoverflow_ratings' + ); - $collection->add_database_table('moodleoverflow_tracking', + $collection->add_database_table( + 'moodleoverflow_tracking', [ 'userid' => 'privacy:metadata:moodleoverflow_tracking:userid', 'moodleoverflowid' => 'privacy:metadata:moodleoverflow_tracking:moodleoverflowid', ], - 'privacy:metadata:moodleoverflow_tracking'); + 'privacy:metadata:moodleoverflow_tracking' + ); - $collection->add_database_table('moodleoverflow_grades', + $collection->add_database_table( + 'moodleoverflow_grades', [ 'userid' => 'privacy:metadata:moodleoverflow_grades:userid', 'moodleoverflowid' => 'privacy:metadata:moodleoverflow_grades:moodleoverflowid', 'grade' => 'privacy:metadata:moodleoverflow_grades:grade', ], - 'privacy:metadata:moodleoverflow_grades'); + 'privacy:metadata:moodleoverflow_grades' + ); - $collection->link_subsystem('core_files', + $collection->link_subsystem( + 'core_files', 'privacy:metadata:core_files' ); @@ -201,7 +218,7 @@ public static function export_user_data(approved_contextlist $contextlist) { $user = $contextlist->get_user(); $userid = $user->id; - list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED); + [$contextsql, $contextparams] = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED); $sql = "SELECT c.id AS contextid, @@ -481,7 +498,7 @@ public static function delete_data_for_users(approved_userlist $userlist) { $cm = $DB->get_record('course_modules', ['id' => $context->instanceid]); $moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $cm->instance]); - list($userinsql, $userinparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED); + [$userinsql, $userinparams] = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED); $params = array_merge(['moodleoverflowid' => $moodleoverflow->id], $userinparams); // Delete the entries from the table tracking, subscriptions, read and discussion_subs. diff --git a/classes/ratings.php b/classes/ratings.php index b3abf895ebe..fd983aae236 100644 --- a/classes/ratings.php +++ b/classes/ratings.php @@ -33,7 +33,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class ratings { - /** * Add a rating. * This is the basic function to add or edit ratings. @@ -59,12 +58,20 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati $post = moodleoverflow_get_record_or_exception('moodleoverflow_posts', ['id' => $postid], 'invalidparentpostid'); // Check if the post belongs to a discussion. - $discussion = moodleoverflow_get_record_or_exception('moodleoverflow_discussions', ['id' => $post->discussion], - 'notpartofdiscussion'); + $discussion = moodleoverflow_get_record_or_exception( + 'moodleoverflow_discussions', + ['id' => $post->discussion], + 'notpartofdiscussion' + ); // Get the related course. - $course = moodleoverflow_get_record_or_exception('course', ['id' => $moodleoverflow->course], - 'invalidcourseid', '*', true); + $course = moodleoverflow_get_record_or_exception( + 'course', + ['id' => $moodleoverflow->course], + 'invalidcourseid', + '*', + true + ); // Are multiple marks allowed? $markssetting = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id], 'allowmultiplemarks'); @@ -76,7 +83,6 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati // Redirect the user if capabilities are missing. if (!self::moodleoverflow_user_can_rate($post, $modulecontext, $userid)) { - // Catch unenrolled users. $returnurl = '/mod/moodleoverflow/view.php?m' . $moodleoverflow->id; moodleoverflow_catch_unenrolled_user($coursecontext, $course->id, $returnurl); @@ -92,8 +98,12 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati // Check if we are removing a mark. if (in_array($rating / 10, $possibleratings)) { - moodleoverflow_get_config_or_exception('moodleoverflow', 'allowratingchange', - 'noratingchangeallowed', 'moodleoverflow'); + moodleoverflow_get_config_or_exception( + 'moodleoverflow', + 'allowratingchange', + 'noratingchangeallowed', + 'moodleoverflow' + ); // Delete the rating. return self::moodleoverflow_remove_rating($postid, $rating / 10, $userid, $modulecontext); @@ -114,7 +124,6 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati // Check if multiple marks are not enabled. if (!$multiplemarks) { - // Get other ratings in the discussion. $sql = "SELECT * FROM {moodleoverflow_ratings} @@ -124,22 +133,37 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati // If there is an old rating, update it. Else create a new rating record. if ($otherrating) { return self::moodleoverflow_update_rating_record($post->id, $rating, $userid, $otherrating->id, $modulecontext); - } else { - return self::moodleoverflow_add_rating_record($moodleoverflow->id, $discussion->id, $post->id, - $rating, $userid, $modulecontext); + return self::moodleoverflow_add_rating_record( + $moodleoverflow->id, + $discussion->id, + $post->id, + $rating, + $userid, + $modulecontext + ); } } else { // If multiplemarks are allowed, only create a new rating. - return self::moodleoverflow_add_rating_record($moodleoverflow->id, $discussion->id, $post->id, - $rating, $userid, $modulecontext); + return self::moodleoverflow_add_rating_record( + $moodleoverflow->id, + $discussion->id, + $post->id, + $rating, + $userid, + $modulecontext + ); } } // Update an rating record. if ($oldrating['normal']) { - moodleoverflow_get_config_or_exception('moodleoverflow', 'allowratingchange', - 'noratingchangeallowed', 'moodleoverflow'); + moodleoverflow_get_config_or_exception( + 'moodleoverflow', + 'allowratingchange', + 'noratingchangeallowed', + 'moodleoverflow' + ); // Check if the rating can still be changed. if (!self::moodleoverflow_can_be_changed($postid, $oldrating['normal']->rating, $userid)) { @@ -151,8 +175,14 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati } // Create a new rating record. - return self::moodleoverflow_add_rating_record($moodleoverflow->id, $post->discussion, $postid, - $rating, $userid, $modulecontext); + return self::moodleoverflow_add_rating_record( + $moodleoverflow->id, + $post->discussion, + $postid, + $rating, + $userid, + $modulecontext + ); } /** @@ -168,8 +198,11 @@ public static function moodleoverflow_add_rating($moodleoverflow, $postid, $rati */ public static function moodleoverflow_get_reputation($moodleoverflowid, $userid, $forcesinglerating = false) { // Check the moodleoverflow instance. - $moodleoverflow = moodleoverflow_get_record_or_exception('moodleoverflow', ['id' => $moodleoverflowid], - 'invalidmoodleoverflowid'); + $moodleoverflow = moodleoverflow_get_record_or_exception( + 'moodleoverflow', + ['id' => $moodleoverflowid], + 'invalidmoodleoverflowid' + ); // Check whether the reputation can be summed over the whole course. if ($moodleoverflow->coursewidereputation && !$forcesinglerating) { @@ -310,7 +343,6 @@ public static function moodleoverflow_get_ratings_by_discussion($discussionid, $ // A single post is requested. if ($postid) { - // Check if the post is part of the discussion. if (array_key_exists($postid, $votes)) { return $votes[$postid]; @@ -337,10 +369,8 @@ public static function moodleoverflow_discussion_is_solved($discussionid, $teach // Is the teachers solved-status requested? if ($teacher) { - // Check if a teacher marked a solution as solved. if ($DB->record_exists('moodleoverflow_ratings', ['discussionid' => $discussionid, 'rating' => 3])) { - // Return the rating records. return $DB->get_records('moodleoverflow_ratings', ['discussionid' => $discussionid, 'rating' => 3]); } @@ -351,7 +381,6 @@ public static function moodleoverflow_discussion_is_solved($discussionid, $teach // Check if the topic starter marked a solution as helpful. if ($DB->record_exists('moodleoverflow_ratings', ['discussionid' => $discussionid, 'rating' => 4])) { - // Return the rating records. return $DB->get_records('moodleoverflow_ratings', ['discussionid' => $discussionid, 'rating' => 4]); } @@ -684,8 +713,10 @@ private static function moodleoverflow_sort_postgroup(&$posts, $low, $high) { if ($posts[$low]->votesdifference == $posts[$low + 1]->votesdifference) { $tempstartindex = $low; $tempendindex = $tempstartindex + 1; - while (($tempendindex + 1 <= $high) && - ($posts[$tempendindex]->votesdifference == $posts[$tempendindex + 1]->votesdifference)) { + while ( + ($tempendindex + 1 <= $high) && + ($posts[$tempendindex]->votesdifference == $posts[$tempendindex + 1]->votesdifference) + ) { $tempendindex++; } moodleoverflow_quick_array_sort($posts, $tempstartindex, $tempendindex, 'modified', 'asc'); diff --git a/classes/readtracking.php b/classes/readtracking.php index bc2b556e82d..3b368c3dffb 100644 --- a/classes/readtracking.php +++ b/classes/readtracking.php @@ -35,7 +35,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class readtracking { - /** * Determine if a user can track moodleoverflows and optionally a particular moodleoverflow instance. * Checks the site settings and the moodleoverflow settings (if requested). @@ -100,8 +99,10 @@ public static function moodleoverflow_is_tracked($moodleoverflow, $user = null) $forced = ($moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_FORCED); // Check the preferences of the user. - $userpreference = $DB->get_record('moodleoverflow_tracking', - ['userid' => $user->id, 'moodleoverflowid' => $moodleoverflow->id]); + $userpreference = $DB->get_record( + 'moodleoverflow_tracking', + ['userid' => $user->id, 'moodleoverflowid' => $moodleoverflow->id] + ); // Return the boolean. if (get_config('moodleoverflow', 'allowforcedreadtracking')) { @@ -158,7 +159,6 @@ public static function moodleoverflow_mark_discussion_read($discussionid, $modco // Iterate through all posts of the discussion. foreach ($posts as $post) { - // Ignore already read posts. if (!is_null($post->postread)) { continue; @@ -232,7 +232,6 @@ public static function moodleoverflow_add_read_record($userid, $postid) { // Check for read records for this user an this post. $oldrecord = $DB->get_record('moodleoverflow_read', ['postid' => $postid, 'userid' => $userid]); if (!$oldrecord) { - // If there are no old records, create a new one. $sql = "INSERT INTO {moodleoverflow_read} (userid, postid, discussionid, moodleoverflowid, firstread, lastread) SELECT ?, p.id, p.discussion, d.moodleoverflow, ?, ? @@ -364,7 +363,6 @@ public static function moodleoverflow_stop_tracking($moodleoverflowid, $userid = // Stop tracking the moodleoverflow if not already stopped. if (!$isstopped) { - // Create the tracking object. $tracking = new \stdClass(); $tracking->userid = $userid; @@ -414,7 +412,6 @@ public static function get_untracked_moodleoverflows($userid, $courseid) { // Check whether readtracking may be forced. if (get_config('moodleoverflow', 'allowforcedreadtracking')) { - // Create a part of a sql-statement. $trackingsql = "AND (m.trackingtype = " . MOODLEOVERFLOW_TRACKING_OFF . " OR (m.trackingtype = " . MOODLEOVERFLOW_TRACKING_OPTIONAL . " AND mt.id IS NOT NULL))"; @@ -466,12 +463,14 @@ public static function moodleoverflow_count_unread_posts_moodleoverflow($cm) { "WHERE m.id = :moodleoverflowid", ['userid' => $USER->id, 'moodleoverflowid' => $cm->instance]); // Return if tracking is off, or ((optional or forced, but forced disallowed by admin) and user has disabled tracking). - if ($moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_OFF || ( + if ( + $moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_OFF || ( ($moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_OPTIONAL || ( $moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_FORCED && !get_config('moodleoverflow', 'allowforcedreadtracking') ) - ) && $moodleoverflow->hasdisabledtracking)) { + ) && $moodleoverflow->hasdisabledtracking) + ) { return 0; } // Get the current timestamp and the cutoffdate. diff --git a/classes/review.php b/classes/review.php index a566e9896da..41a07340700 100644 --- a/classes/review.php +++ b/classes/review.php @@ -32,7 +32,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class review { - /** * Nothing has to be reviewed. */ @@ -68,9 +67,10 @@ public static function get_short_review_info_for_discussion(int $discussionid) { global $DB; return $DB->get_record_sql( - 'SELECT COUNT(*) as count, MIN(id) AS first ' . + 'SELECT COUNT(*) as count, MIN(id) AS first ' . 'FROM {moodleoverflow_posts} ' . - 'WHERE discussion = :discussionid AND reviewed = 0 AND created < :cutofftime', [ + 'WHERE discussion = :discussionid AND reviewed = 0 AND created < :cutofftime', + [ 'discussionid' => $discussionid, 'cutofftime' => time() - get_config('moodleoverflow', 'reviewpossibleaftertime'), ] @@ -95,8 +95,12 @@ public static function get_first_review_post($moodleoverflowid, $afterpostid = n $addwhere = ''; if ($afterpostid) { - $afterdiscussionid = $DB->get_field('moodleoverflow_posts', 'discussion', ['id' => $afterpostid], - MUST_EXIST); + $afterdiscussionid = $DB->get_field( + 'moodleoverflow_posts', + 'discussion', + ['id' => $afterpostid], + MUST_EXIST + ); $orderby = 'CASE WHEN (p.discussion > :afterdiscussionid OR (p.discussion = :afterdiscussionid2 AND p.id > :afterpostid)) THEN 0 ELSE 1 END, '; @@ -159,15 +163,14 @@ public static function is_post_in_review_period($post): bool { public static function count_outstanding_reviews_in_moodleoverflow($moodleoverflowid): int { global $DB; return $DB->count_records_sql( - 'SELECT COUNT(*) ' . + 'SELECT COUNT(*) ' . 'FROM {moodleoverflow_posts} p ' . 'JOIN {moodleoverflow_discussions} d ON d.id = p.discussion ' . - 'WHERE d.moodleoverflow = :moodleoverflowid AND p.created < :cutofftime AND reviewed = 0', [ + 'WHERE d.moodleoverflow = :moodleoverflowid AND p.created < :cutofftime AND reviewed = 0', + [ 'moodleoverflowid' => $moodleoverflowid, 'cutofftime' => time() - get_config('moodleoverflow', 'reviewpossibleaftertime'), ] ); } - - } diff --git a/classes/subscriptions.php b/classes/subscriptions.php index e1c0075a224..982a090b0da 100644 --- a/classes/subscriptions.php +++ b/classes/subscriptions.php @@ -37,7 +37,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class subscriptions { - /** * The status value for an unsubscribed discussion. * @@ -105,8 +104,10 @@ class subscriptions { public static function is_subscribed($userid, $moodleoverflow, $context, $discussionid = null) { // Is the user forced to be subscribed to the moodleoverflow? - if (self::is_forcesubscribed($moodleoverflow) && - has_capability('mod/moodleoverflow:allowforcesubscribe', $context, $userid)) { + if ( + self::is_forcesubscribed($moodleoverflow) && + has_capability('mod/moodleoverflow:allowforcesubscribe', $context, $userid) + ) { return true; } @@ -191,10 +192,8 @@ public static function fill_subscription_cache($moodleoverflowid, $userid = null // Check if the moodleoverflow has not been fetched as a whole. if (!isset(self::$fetchedmoodleoverflows[$moodleoverflowid])) { - // Is a specified user requested? if (isset($userid)) { - // Create the cache for the user. if (!isset(self::$moodleoverflowcache[$userid])) { self::$moodleoverflowcache[$userid] = []; @@ -202,7 +201,6 @@ public static function fill_subscription_cache($moodleoverflowid, $userid = null // Check if the user is subscribed to the moodleoverflow. if (!isset(self::$moodleoverflowcache[$userid][$moodleoverflowid])) { - // Request to the database. $params = ['userid' => $userid, 'moodleoverflow' => $moodleoverflowid]; if ($DB->record_exists('moodleoverflow_subscriptions', $params)) { @@ -211,16 +209,13 @@ public static function fill_subscription_cache($moodleoverflowid, $userid = null self::$moodleoverflowcache[$userid][$moodleoverflowid] = false; } } - } else { // The request is not connected to a specific user. - // Request all records. $params = ['moodleoverflow' => $moodleoverflowid]; $subscriptions = $DB->get_recordset('moodleoverflow_subscriptions', $params, '', 'id, userid'); // Loop through the records. foreach ($subscriptions as $data) { - // Create a new record if necessary. if (!isset(self::$moodleoverflowcache[$data->userid])) { self::$moodleoverflowcache[$data->userid] = []; @@ -274,10 +269,8 @@ public static function fill_discussion_subscription_cache($moodleoverflowid, $us // Check if the discussions of this moodleoverflow has been fetched as a whole. if (!isset(self::$fetcheddiscussions[$moodleoverflowid])) { - // Check if data for a specific user is requested. if (isset($userid)) { - // Create a new record if necessary. if (!isset(self::$discussioncache[$userid])) { self::$discussioncache[$userid] = []; @@ -285,11 +278,14 @@ public static function fill_discussion_subscription_cache($moodleoverflowid, $us // Check if the moodleoverflow instance is already cached. if (!isset(self::$discussioncache[$userid][$moodleoverflowid])) { - // Get all records. $params = ['userid' => $userid, 'moodleoverflow' => $moodleoverflowid]; - $subscriptions = $DB->get_recordset('moodleoverflow_discuss_subs', $params, - null, 'id, discussion, preference'); + $subscriptions = $DB->get_recordset( + 'moodleoverflow_discuss_subs', + $params, + null, + 'id, discussion, preference' + ); // Loop through all of these and add them to the discussion cache. foreach ($subscriptions as $id => $data) { @@ -299,14 +295,17 @@ public static function fill_discussion_subscription_cache($moodleoverflowid, $us // Close the record set. $subscriptions->close(); } - } else { // No user ID is submitted. // Get all records. $params = ['moodleoverflow' => $moodleoverflowid]; - $subscriptions = $DB->get_recordset('moodleoverflow_discuss_subs', $params, - null, 'id, userid, discussion, preference'); + $subscriptions = $DB->get_recordset( + 'moodleoverflow_discuss_subs', + $params, + null, + 'id, userid, discussion, preference' + ); // Loop throuch all of them and add them to the discussion cache. foreach ($subscriptions as $id => $data) { @@ -428,7 +427,7 @@ public static function get_unsubscribable_moodleoverflows() { } // Get a list of all moodleoverflows the user is connected to. - list($coursesql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'c'); + [$coursesql, $courseparams] = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'c'); // Find all moodleoverflows from the user's courses that they are subscribed to and which are not set to forced. // It is possible for users to be subscribed to a moodleoveflow in subscriptions disallowed mode so they must be @@ -469,11 +468,11 @@ public static function get_potential_subscribers($context, $fields, $sort = '') global $DB; // Only enrolled users can subscribe. - list($esql, $params) = get_enrolled_sql($context, 'mod/moodleoverflow:allowforcesubscribe'); + [$esql, $params] = get_enrolled_sql($context, 'mod/moodleoverflow:allowforcesubscribe'); // Default ordering of the list. if (!$sort) { - list($sort, $sortparams) = users_order_by_sql('u'); + [$sort, $sortparams] = users_order_by_sql('u'); $params = array_merge($params, $sortparams); } @@ -546,14 +545,12 @@ public static function get_subscribed_users($moodleoverflow, $context, $fields = if (self::subscription_disabled($moodleoverflow)) { $results = []; } else { - // Only enrolled users can subscribe to a moodleoverflow. - list($esql, $params) = get_enrolled_sql($context, '', 0, true); + [$esql, $params] = get_enrolled_sql($context, '', 0, true); $params['moodleoverflowid'] = $moodleoverflow->id; // Check discussion subscriptions as well? if ($includediscussions) { - // Determine more params. $params['smoodleoverflowid'] = $moodleoverflow->id; $params['dsmoodleoverflowid'] = $moodleoverflow->id; @@ -571,7 +568,6 @@ public static function get_subscribed_users($moodleoverflow, $context, $fields = JOIN {user} u ON u.id = subscriptions.userid JOIN ($esql) je ON je.id = u.id ORDER BY u.email ASC"; - } else { // Dont include the discussion subscriptions. @@ -665,7 +661,6 @@ public static function subscribe_user($userid, $moodleoverflow, $context, $userr // If the subscription was requested by the user, remove all records for the discussions within this moodleoverflow. if ($userrequest) { - // Delete all those discussion subscriptions. $params = [ 'userid' => $userid, @@ -718,13 +713,11 @@ public static function unsubscribe_user($userid, $moodleoverflow, $context, $use // Check if there is a subscription record. $params = ['userid' => $userid, 'moodleoverflow' => $moodleoverflow->id]; if ($subscription = $DB->get_record('moodleoverflow_subscriptions', $params)) { - // Delete this record. $DB->delete_records('moodleoverflow_subscriptions', ['id' => $subscription->id]); // Was the unsubscription requested by the user? if ($userrequest) { - // Delete the discussion subscriptions as well. $params = [ 'userid' => $userid, @@ -783,29 +776,23 @@ public static function subscribe_user_to_discussion($userid, $discussion, $conte // Check if the user is already subscribed to the moodleoverflow. $params = ['userid' => $userid, 'moodleoverflow' => $discussion->moodleoverflow]; if ($DB->record_exists('moodleoverflow_subscriptions', $params)) { - // Check if the user is unsubscribed from the discussion. if ($subscription && $subscription->preference == self::MOODLEOVERFLOW_DISCUSSION_UNSUBSCRIBED) { - // Delete the discussion preference. $DB->delete_records('moodleoverflow_discuss_subs', ['id' => $subscription->id]); unset(self::$discussioncache[$userid][$discussion->moodleoverflow][$discussion->id]); - } else { // The user is already subscribed to the forum. return false; } - } else { // The user is not subscribed to the moodleoverflow. // Check if there is already a subscription to the discussion. if ($subscription) { - // Update the existing record. $subscription->preference = time(); $DB->update_record('moodleoverflow_discuss_subs', $subscription); - } else { // Else a new record needs to be created. $subscription = new stdClass(); @@ -858,31 +845,25 @@ public static function unsubscribe_user_from_discussion($userid, $discussion, $c // Check if the user is subscribed to the moodleoverflow. $params = ['userid' => $userid, 'moodleoverflow' => $discussion->moodleoverflow]; if (!$DB->record_exists('moodleoverflow_subscriptions', $params)) { - // Check if the user isn't subscribed to the moodleoverflow. if ($subscription && $subscription->preference != self::MOODLEOVERFLOW_DISCUSSION_UNSUBSCRIBED) { - // Delete the discussion subscription. $DB->delete_records('moodleoverflow_discuss_subs', ['id' => $subscription->id]); unset(self::$discussioncache[$userid][$discussion->moodleoverflow][$discussion->id]); - } else { // Else the user is not subscribed to the moodleoverflow. // Nothing has to be done here. return false; } - } else { // There is an subscription record for this moodleoverflow. // Check whether an subscription record for this discussion. if ($subscription) { - // Update the existing record. $subscription->preference = self::MOODLEOVERFLOW_DISCUSSION_UNSUBSCRIBED; $DB->update_record('moodleoverflow_discuss_subs', $subscription); - } else { // There is no record. @@ -951,7 +932,6 @@ public static function moodleoverflow_get_subscribe_link($moodleoverflow, $conte } else if ($isdisabled && !has_capability('mod/moodleoverflow:managesubscriptions', $context)) { return $messages['cantsubscribe']; } else { - // The user needs to be enrolled. if (!is_enrolled($context, $USER, '', true)) { return ''; @@ -1008,7 +988,6 @@ public static function moodleoverflow_post_subscription($fromform, $moodleoverfl // Do not continue if subscriptions are disabled. if ($disabled) { - // If the user is subscribed, unsubscribe him. $subscribed = self::is_subscribed($USER->id, $moodleoverflow, $modulecontext); $coursecontext = \context_course::instance($moodleoverflow->course); @@ -1056,7 +1035,6 @@ public static function get_discussion_subscription_icon($moodleoverflow, $contex // Create an icon to unsubscribe. if ($status) { - // Create the icon. $string = get_string('clicktounsubscribe', 'moodleoverflow'); $output = $OUTPUT->pix_icon('i/subscribed', $string, 'mod_moodleoverflow'); diff --git a/classes/tables/userstats_table.php b/classes/tables/userstats_table.php index 4e4bd348a06..56ba38d63a9 100644 --- a/classes/tables/userstats_table.php +++ b/classes/tables/userstats_table.php @@ -39,7 +39,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class userstats_table extends \flexible_table { - /** @var int the Course ID*/ private $courseid; @@ -104,7 +103,7 @@ public function out() { public function get_table_data() { // Get all userdata from a course. $context = \context_course::instance($this->courseid); - $users = get_enrolled_users($context , '', 0, 'u.id, u.firstname, u.lastname'); + $users = get_enrolled_users($context, '', 0, 'u.id, u.firstname, u.lastname'); // Step 1.0: Build the datatable with all relevant information. $ratingdata = $this->get_rating_data(); @@ -120,15 +119,14 @@ public function get_table_data() { } // Did a student submit a rating? - if ($row->rateuserid == $student->id ) { + if ($row->rateuserid == $student->id) { $this->process_submitted_ratings($student, $row); } // Did the student write a post? - if ($row->postuserid == $student->id ) { + if ($row->postuserid == $student->id) { $this->process_written_posts($student, $row); } - } // Get the user reputation from the course. $student->forumreputation = ratings::moodleoverflow_get_reputation_instance($this->moodleoverflowid, $student->id); @@ -330,9 +328,10 @@ private function process_submitted_ratings($student, $row) { private function process_written_posts($student, $row) { // Only count a written post if: the post is not in an anonymous discussion: // or the post is in a partial anonymous discussion and the user is not the starter of the discussion. - if (!array_key_exists($row->postid, $student->submittedposts) && - ($row->anonymoussetting == 0 || ($row->anonymoussetting == 1 && $row->postuserid != $row->discussuserid))) { - + if ( + !array_key_exists($row->postid, $student->submittedposts) && + ($row->anonymoussetting == 0 || ($row->anonymoussetting == 1 && $row->postuserid != $row->discussuserid)) + ) { $this->increment_forumactivity($student, $row); $student->courseactivity += 1; $student->submittedposts[$row->postid] = $row->postid; diff --git a/classes/task/clean_readrecords.php b/classes/task/clean_readrecords.php index f7c58206a19..0b602cd95db 100644 --- a/classes/task/clean_readrecords.php +++ b/classes/task/clean_readrecords.php @@ -35,7 +35,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class clean_readrecords extends \core\task\scheduled_task { - /** * Get a descriptive name for this task (shown to admins). * @@ -56,6 +55,4 @@ public function execute() { // The cron is finished. return true; } - } - diff --git a/classes/task/send_daily_mails.php b/classes/task/send_daily_mails.php index 807103c1409..1c9d432da1a 100644 --- a/classes/task/send_daily_mails.php +++ b/classes/task/send_daily_mails.php @@ -27,7 +27,6 @@ * This task sends a daily mail of unread posts */ class send_daily_mails extends \core\task\scheduled_task { - /** * Return the task's name as shown in admin screens. * diff --git a/classes/task/send_mails.php b/classes/task/send_mails.php index 8fd15f98b77..3362d614f8d 100644 --- a/classes/task/send_mails.php +++ b/classes/task/send_mails.php @@ -38,7 +38,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class send_mails extends \core\task\scheduled_task { - /** * Get a descriptive name for this task (shwon to admins). * diff --git a/classes/task/send_review_mails.php b/classes/task/send_review_mails.php index 2466c119d97..fceae571821 100644 --- a/classes/task/send_review_mails.php +++ b/classes/task/send_review_mails.php @@ -40,7 +40,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class send_review_mails extends \core\task\scheduled_task { - /** * Get a descriptive name for this task (shown to admins). * @@ -59,7 +58,6 @@ public function execute() { // The cron is finished. return true; - } /** @@ -132,8 +130,16 @@ 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); @@ -153,14 +159,15 @@ public function send_review_notifications() { } if (!empty($success)) { - list($insql, $inparams) = $DB->get_in_or_equal($success); + [$insql, $inparams] = $DB->get_in_or_equal($success); $DB->set_field_select( - 'moodleoverflow_posts', 'mailed', MOODLEOVERFLOW_MAILED_REVIEW_SUCCESS, - 'id ' . $insql, $inparams - ); + 'moodleoverflow_posts', + 'mailed', + MOODLEOVERFLOW_MAILED_REVIEW_SUCCESS, + 'id ' . $insql, + $inparams + ); mtrace('Sent review notifications for ' . count($success) . ' posts successfully!'); } } - } - diff --git a/db/upgrade.php b/db/upgrade.php index ae8a539965b..8fc139d088b 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -83,7 +83,6 @@ function xmldb_moodleoverflow_upgrade($oldversion) { } if ($oldversion < 2019052600) { - // Define table moodleoverflow_grades to be created. $table = new xmldb_table('moodleoverflow_grades'); @@ -135,7 +134,6 @@ function xmldb_moodleoverflow_upgrade($oldversion) { } if ($oldversion < 2021060800) { - // Define table moodleoverflow to be edited. $table = new xmldb_table('moodleoverflow'); @@ -171,7 +169,6 @@ function xmldb_moodleoverflow_upgrade($oldversion) { } if ($oldversion < 2021111700) { - // Define table moodleoverflow to be edited. $table = new xmldb_table('moodleoverflow'); @@ -196,7 +193,6 @@ function xmldb_moodleoverflow_upgrade($oldversion) { } if ($oldversion < 2022072000) { - // Define field needsreview to be added to moodleoverflow. $table = new xmldb_table('moodleoverflow'); $field = new xmldb_field('needsreview', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'anonymous'); @@ -231,7 +227,6 @@ function xmldb_moodleoverflow_upgrade($oldversion) { } if ($oldversion < 2022110700) { - if (get_capability_info('mod/moodleoverflow:reviewpost')) { foreach (get_archetype_roles('manager') as $role) { unassign_capability('mod/moodleoverflow:reviewpost', $role->id); @@ -239,7 +234,10 @@ function xmldb_moodleoverflow_upgrade($oldversion) { foreach (get_archetype_roles('teacher') as $role) { assign_capability( - 'mod/moodleoverflow:reviewpost', CAP_ALLOW, $role->id, context_system::instance() + 'mod/moodleoverflow:reviewpost', + CAP_ALLOW, + $role->id, + context_system::instance() ); } } diff --git a/db/upgradelib.php b/db/upgradelib.php index 7f1cd5f32da..e52840a6dc9 100644 --- a/db/upgradelib.php +++ b/db/upgradelib.php @@ -303,10 +303,10 @@ function mod_moodleoverflow_move_draftfiles_to_permanent_filearea() { $baseurl = $CFG->wwwroot . '/draftfile.php/'; $posts = $DB->get_recordset_select( - 'moodleoverflow_posts', - $DB->sql_like('message', ':searchurl'), - ['searchurl' => '%' . $DB->sql_like_escape($baseurl) . '%'], - 'discussion' + 'moodleoverflow_posts', + $DB->sql_like('message', ':searchurl'), + ['searchurl' => '%' . $DB->sql_like_escape($baseurl) . '%'], + 'discussion' ); $discussion = null; @@ -315,7 +315,6 @@ function mod_moodleoverflow_move_draftfiles_to_permanent_filearea() { $postsupdated = 0; foreach ($posts as $post) { - if (!$discussion || $discussion->id != $post->discussion) { $discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $post->discussion]); $moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow]); @@ -339,8 +338,16 @@ function mod_moodleoverflow_move_draftfiles_to_permanent_filearea() { continue; } - $post->message = mod_moodleoverflow_file_save_draft_area_files($draftid, $context->id, 'mod_moodleoverflow', 'post', - $post->id, $usercontext, mod_forum_post_form::editor_options($context, null), $post->message); + $post->message = mod_moodleoverflow_file_save_draft_area_files( + $draftid, + $context->id, + 'mod_moodleoverflow', + 'post', + $post->id, + $usercontext, + mod_forum_post_form::editor_options($context, null), + $post->message + ); $DB->set_field('moodleoverflow_posts', 'message', $post->message, ['id' => $post->id]); @@ -348,5 +355,4 @@ function mod_moodleoverflow_move_draftfiles_to_permanent_filearea() { } return $postsupdated; - } diff --git a/discussion.php b/discussion.php index 488e34e14a9..0767430184a 100644 --- a/discussion.php +++ b/discussion.php @@ -43,8 +43,11 @@ $discussion = moodleoverflow_get_record_or_exception('moodleoverflow_discussions', ['id' => $d], 'invaliddiscussionid'); // Check if the related moodleoverflow instance is valid. -$moodleoverflow = moodleoverflow_get_record_or_exception('moodleoverflow', ['id' => $discussion->moodleoverflow], - 'invalidmoodleoverflowid'); +$moodleoverflow = moodleoverflow_get_record_or_exception( + 'moodleoverflow', + ['id' => $discussion->moodleoverflow], + 'invalidmoodleoverflowid' +); // Check if the related moodleoverflow instance is valid. $course = moodleoverflow_get_record_or_exception('course', ['id' => $discussion->course], 'invalidcourseid', '*', true); @@ -111,8 +114,11 @@ // Has the user the capability to view the post? if (!moodleoverflow_user_can_see_post($moodleoverflow, $discussion, $post, $cm)) { - throw new moodle_exception('noviewdiscussionspermission', 'moodleoverflow', - "$CFG->wwwroot/mod/moodleoverflow/view.php?m=$moodleoverflow->id"); + throw new moodle_exception( + 'noviewdiscussionspermission', + 'moodleoverflow', + "$CFG->wwwroot/mod/moodleoverflow/view.php?m=$moodleoverflow->id" + ); } // Append the discussion name to the navigation. @@ -127,8 +133,10 @@ $discussion->name = get_string('privacy:anonym_discussion_name', 'mod_moodleoverflow'); } -$node = $forumnode->add(format_string($discussion->name), - new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $discussion->id])); +$node = $forumnode->add( + format_string($discussion->name), + new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $discussion->id]) +); $node->display = false; if ($node && ($post->id != $discussion->firstpost)) { $node->add(format_string($post->subject), $PAGE->url); diff --git a/externallib.php b/externallib.php index 98d8739e3b1..0c991eb78ff 100644 --- a/externallib.php +++ b/externallib.php @@ -39,7 +39,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class mod_moodleoverflow_external extends external_api { - // LEARNWEB-TODO: Adapt the functions to the new way of working with posts. /** * Returns description of method parameters @@ -90,12 +89,18 @@ public static function record_vote($postid, $ratingid) { $post = $DB->get_record('moodleoverflow_posts', ['id' => $params['postid']], '*', MUST_EXIST); // Check if the discussion is valid. - $discussion = moodleoverflow_get_record_or_exception('moodleoverflow_discussions', ['id' => $post->discussion], - 'invaliddiscussionid'); + $discussion = moodleoverflow_get_record_or_exception( + 'moodleoverflow_discussions', + ['id' => $post->discussion], + 'invaliddiscussionid' + ); // Check if the related moodleoverflow instance is valid. - $moodleoverflow = moodleoverflow_get_record_or_exception('moodleoverflow', ['id' => $discussion->moodleoverflow], - 'invalidmoodleoverflowid'); + $moodleoverflow = moodleoverflow_get_record_or_exception( + 'moodleoverflow', + ['id' => $discussion->moodleoverflow], + 'invalidmoodleoverflowid' + ); // Check if the related moodleoverflow instance is valid. $course = moodleoverflow_get_record_or_exception('course', ['id' => $discussion->course], 'invalidcourseid', '*', true); @@ -111,15 +116,24 @@ public static function record_vote($postid, $ratingid) { require_capability('mod/moodleoverflow:ratepost', $context); // Rate the post. - if (!\mod_moodleoverflow\ratings::moodleoverflow_add_rating($moodleoverflow, - $params['postid'], $params['ratingid'], $cm, $USER->id)) { + if ( + !\mod_moodleoverflow\ratings::moodleoverflow_add_rating( + $moodleoverflow, + $params['postid'], + $params['ratingid'], + $cm, + $USER->id + ) + ) { throw new moodle_exception('ratingfailed', 'moodleoverflow'); } $post = moodleoverflow_get_post_full($params['postid']); $postownerid = $post->userid; - $rating = \mod_moodleoverflow\ratings::moodleoverflow_get_ratings_by_discussion($discussion->id, - $params['postid']); + $rating = \mod_moodleoverflow\ratings::moodleoverflow_get_ratings_by_discussion( + $discussion->id, + $params['postid'] + ); $ownerrating = \mod_moodleoverflow\ratings::moodleoverflow_get_reputation($moodleoverflow->id, $postownerid); $raterrating = \mod_moodleoverflow\ratings::moodleoverflow_get_reputation($moodleoverflow->id, $USER->id); @@ -259,14 +273,14 @@ public static function review_reject_post($postid, $reason = null) { $userto->anonymous = anonymous::is_post_anonymous($discussion, $moodleoverflow, $post->userid); $maildata = new moodleoverflow_email( - $course, - $cm, - $moodleoverflow, - $discussion, - $post, - $userto, - $userto, - false + $course, + $cm, + $moodleoverflow, + $discussion, + $post, + $userto, + $userto, + false ); $textcontext = $maildata->export_for_template($renderertext, true); @@ -278,11 +292,11 @@ public static function review_reject_post($postid, $reason = null) { } email_to_user( - $userto, - \core_user::get_noreply_user(), - get_string('email_rejected_subject', 'moodleoverflow', $textcontext), - $OUTPUT->render_from_template('mod_moodleoverflow/email_rejected_text', $textcontext), - $OUTPUT->render_from_template('mod_moodleoverflow/email_rejected_html', $htmlcontext) + $userto, + \core_user::get_noreply_user(), + get_string('email_rejected_subject', 'moodleoverflow', $textcontext), + $OUTPUT->render_from_template('mod_moodleoverflow/email_rejected_text', $textcontext), + $OUTPUT->render_from_template('mod_moodleoverflow/email_rejected_html', $htmlcontext) ); $url = review::get_first_review_post($moodleoverflow->id, $post->id); diff --git a/grade.php b/grade.php index ab46489e8b0..21a1a330292 100644 --- a/grade.php +++ b/grade.php @@ -28,4 +28,4 @@ $cm = get_coursemodule_from_id('moodleoverflow', $id, 0, false, MUST_EXIST); -redirect('view.php?id='.$cm->id); +redirect('view.php?id=' . $cm->id); diff --git a/index.php b/index.php index 193d769764f..ef1ba7f8274 100644 --- a/index.php +++ b/index.php @@ -124,7 +124,6 @@ // Loop through allmoodleoverflows. foreach ($modinfo->get_instances_of('moodleoverflow') as $moodleoverflowid => $cm) { - // Check whether the user can see the instance. if (!$cm->uservisible || !isset($moodleoverflows[$moodleoverflowid])) { continue; @@ -159,10 +158,8 @@ // Handle course wide subscriptions or unsubscriptions if requested. if (!is_null($subscribe)) { - // Catch guests and not subscribable moodleoverflows. if (isguestuser() || !$showsubscriptioncolumns) { - // Redirect the user back. $url = new moodle_url('/mod/moodleoverflow/index.php', ['id' => $id]); $notification = \core\output\notification::NOTIFY_ERROR; @@ -171,7 +168,6 @@ // Loop through all moodleoverflows. foreach ($modinfo->get_instances_of('moodleoverflow') as $moodleoverflowid => $cm) { - // Initiate variables. $moodleoverflow = $moodleoverflows[$moodleoverflowid]; $modulecontext = context_module::instance($cm->id); @@ -196,7 +192,6 @@ $forcesubscribed = \mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow) && has_capability('mod/moodleoverflow:allowforcesubscribe', $modulecontext); if (!$forcesubscribed) { - // Check the current state. $subscribed = \mod_moodleoverflow\subscriptions::is_subscribed($USER->id, $moodleoverflow, $modulecontext); $subscribable = \mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext); @@ -228,10 +223,8 @@ // Check if there are moodleoverflows. if ($generalmoodleoverflows) { - // Loop through all of the moodleoverflows. foreach ($generalmoodleoverflows as $moodleoverflow) { - // Retrieve the contexts. $cm = $modinfo->instances['moodleoverflow'][$moodleoverflow->id]; $modulecontext = context_module::instance($cm->id); @@ -241,7 +234,6 @@ // Check whether the user can track the moodleoverflow. if ($cantrack) { - // Check whether the tracking is disabled. if ($moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_OFF) { $unreadlink = '-'; @@ -252,8 +244,8 @@ // Check if this moodleoverflow is manually untracked. if (isset($untracked[$moodleoverflow->id])) { $unreadlink = '-'; - - } else if ($unread = \mod_moodleoverflow\readtracking::moodleoverflow_count_unread_posts_moodleoverflow($cm) + } else if ( + $unread = \mod_moodleoverflow\readtracking::moodleoverflow_count_unread_posts_moodleoverflow($cm) ) { // There are unread posts in the moodleoverflow instance. @@ -266,7 +258,6 @@ $string['markallread'] . '" class="iconsmall" />'; $unreadlink .= ''; $unreadlink .= ''; - } else { // There are no unread messages for this moodleoverflow instance. @@ -281,13 +272,11 @@ // Define the string. $trackedlink = $string['yes']; - } else if ($moodleoverflow->trackingtype === MOODLEOVERFLOW_TRACKING_OFF) { // Tracking is set to off. // Define the string. $trackedlink = '-'; - } else { // Tracking is optional. @@ -334,7 +323,6 @@ // Add the subscription information to the rows. if ($showsubscriptioncolumns) { - // Set options to create the subscription link. $suboptions = [ 'subscribed' => $string['yes'], @@ -344,8 +332,11 @@ ]; // Add the subscription link to the row. - $row[] = \mod_moodleoverflow\subscriptions::moodleoverflow_get_subscribe_link($moodleoverflow, - $modulecontext, $suboptions); + $row[] = \mod_moodleoverflow\subscriptions::moodleoverflow_get_subscribe_link( + $moodleoverflow, + $modulecontext, + $suboptions + ); } // Add the rows to the table. @@ -365,7 +356,6 @@ // Show the subscribe all option only to non-guest and enrolled users. if (!isguestuser() && isloggedin() && $showsubscriptioncolumns) { - // Create a box. echo $OUTPUT->box_start('subscription'); diff --git a/lib.php b/lib.php index ac849241aa0..7ce8cd3c638 100644 --- a/lib.php +++ b/lib.php @@ -154,7 +154,6 @@ function moodleoverflow_instance_created($context, $moodleoverflow) { // Check if users are forced to be subscribed to the moodleoverflow instance. if ($moodleoverflow->forcesubscribe == MOODLEOVERFLOW_INITIALSUBSCRIBE) { - // Get a list of all potential subscribers. $users = \mod_moodleoverflow\subscriptions::get_potential_subscribers($context, 'u.id, u.email'); @@ -494,7 +493,6 @@ function moodleoverflow_extend_settings_navigation(settings_navigation $settings // Display a link to the index. if ($enrolled && $activeenrolled) { - // Generate the text of the link. $linktext = get_string('gotoindex', 'moodleoverflow'); @@ -509,7 +507,6 @@ function moodleoverflow_extend_settings_navigation(settings_navigation $settings // Display a link to subscribe or unsubscribe. if ($cansubscribe) { - // Choose the linktext depending on the current state of subscription. $issubscribed = \mod_moodleoverflow\subscriptions::is_subscribed($USER->id, $moodleoverflow, $context); if ($issubscribed) { @@ -525,7 +522,6 @@ function moodleoverflow_extend_settings_navigation(settings_navigation $settings // Display a link to enable or disable readtracking. if ($enrolled && $cantrack) { - // Check some basic capabilities. $isoptional = ($moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_OPTIONAL); $forceallowed = get_config('moodleoverflow', 'allowforcedreadtracking'); @@ -533,7 +529,6 @@ function moodleoverflow_extend_settings_navigation(settings_navigation $settings // Check whether the readtracking state can be changed. if ($isoptional || (!$forceallowed && $isforced)) { - // Generate the text of the link depending on the current state. $istracked = \mod_moodleoverflow\readtracking::moodleoverflow_is_tracked($moodleoverflow); if ($istracked) { @@ -568,15 +563,13 @@ function moodleoverflow_get_context($moodleoverflowid, $context = null) { // If the context does not exist, find the context. if (!$context || !($context instanceof context_module)) { - // Try to take current page context to save on DB query. - if ($PAGE->cm && $PAGE->cm->modname === 'moodleoverflow' && $PAGE->cm->instance == $moodleoverflowid + if ( + $PAGE->cm && $PAGE->cm->modname === 'moodleoverflow' && $PAGE->cm->instance == $moodleoverflowid && $PAGE->context->contextlevel == CONTEXT_MODULE && $PAGE->context->instanceid == $PAGE->cm->id ) { $context = $PAGE->context; - } else { - // Get the context via the coursemodule. $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflowid); $context = \context_module::instance($cm->id); @@ -633,7 +626,8 @@ function moodleoverflow_cm_info_view(cm_info $cm) { */ function moodleoverflow_can_create_attachment($moodleoverflow, $context) { // If maxbytes == 1 it means no attachments at all. - if (empty($moodleoverflow->maxattachments) || $moodleoverflow->maxbytes == 1 || + if ( + empty($moodleoverflow->maxattachments) || $moodleoverflow->maxbytes == 1 || !has_capability('mod/moodleoverflow:createattachment', $context) ) { return false; @@ -650,7 +644,7 @@ function moodleoverflow_can_create_attachment($moodleoverflow, $context) { * * @return array array of grades */ -function moodleoverflow_get_user_grades($moodleoverflow, $userid=0) { +function moodleoverflow_get_user_grades($moodleoverflow, $userid = 0) { global $CFG, $DB; $params = ["moodleoverflowid" => $moodleoverflow->id]; @@ -677,25 +671,20 @@ function moodleoverflow_get_user_grades($moodleoverflow, $userid=0) { */ function moodleoverflow_update_grades($moodleoverflow, $userid, $nullifnone = null) { global $CFG; - require_once($CFG->libdir.'/gradelib.php'); + require_once($CFG->libdir . '/gradelib.php'); // Try to get the grades to update. if ($grades = moodleoverflow_get_user_grades($moodleoverflow, $userid)) { - moodleoverflow_grade_item_update($moodleoverflow, $grades); - } else if ($userid && $nullifnone) { - // Insert a grade with rawgrade = null. As described in Gradebook API. $grade = new stdClass(); $grade->userid = $userid; $grade->rawgrade = null; moodleoverflow_grade_item_update($moodleoverflow, $grade); - } else { moodleoverflow_grade_item_update($moodleoverflow); } - } /** @@ -706,23 +695,21 @@ function moodleoverflow_update_grades($moodleoverflow, $userid, $nullifnone = nu * * @return int grade_update function success code */ -function moodleoverflow_grade_item_update($moodleoverflow, $grades=null) { +function moodleoverflow_grade_item_update($moodleoverflow, $grades = null) { global $CFG, $DB; if (!function_exists('grade_update')) { // Workaround for buggy PHP versions. - require_once($CFG->libdir.'/gradelib.php'); + require_once($CFG->libdir . '/gradelib.php'); } $params = ['itemname' => $moodleoverflow->name, 'idnumber' => $moodleoverflow->id]; if ($moodleoverflow->grademaxgrade <= 0) { $params['gradetype'] = GRADE_TYPE_NONE; - } else if ($moodleoverflow->grademaxgrade > 0) { $params['gradetype'] = GRADE_TYPE_VALUE; $params['grademax'] = $moodleoverflow->grademaxgrade; $params['grademin'] = 0; - } if ($grades === 'reset') { @@ -730,8 +717,16 @@ function moodleoverflow_grade_item_update($moodleoverflow, $grades=null) { $grades = null; } - $gradeupdate = grade_update('mod/moodleoverflow', $moodleoverflow->course, 'mod', 'moodleoverflow', - $moodleoverflow->id, 0, $grades, $params); + $gradeupdate = grade_update( + 'mod/moodleoverflow', + $moodleoverflow->course, + 'mod', + 'moodleoverflow', + $moodleoverflow->id, + 0, + $grades, + $params + ); // Modify grade item category id. if (!is_null($moodleoverflow->gradecat) && $moodleoverflow->gradecat > 0) { diff --git a/locallib.php b/locallib.php index f049731883e..1c103b0e9d3 100644 --- a/locallib.php +++ b/locallib.php @@ -79,8 +79,13 @@ function moodleoverflow_get_discussions($cm, $page = -1, $perpage = 0) { $userdata = 'u.email, u.picture, u.imagealt'; if ($CFG->branch >= 311) { - $usermodifiedfields = \core_user\fields::for_name()->get_sql('um', false, 'um', - '', false)->selects . + $usermodifiedfields = \core_user\fields::for_name()->get_sql( + 'um', + false, + 'um', + '', + false + )->selects . ', um.email AS umemail, um.picture AS umpicture, um.imagealt AS umimagealt'; } else { $usermodifiedfields = get_all_user_name_fields(true, 'um', null, 'um') . @@ -187,7 +192,8 @@ function moodleoverflow_print_latest_discussions($moodleoverflow, $cm, $page = - } if ($markallread && $unreads) { - echo html_writer::link(new moodle_url($markallread), + echo html_writer::link( + new moodle_url($markallread), get_string('markallread_forum', 'mod_moodleoverflow'), ['class' => 'btn btn-secondary my-2'] ); @@ -198,7 +204,6 @@ function moodleoverflow_print_latest_discussions($moodleoverflow, $cm, $page = - // If we want paging. if ($page != -1) { - // Get the number of discussions. $numberofdiscussions = moodleoverflow_get_discussions_count($cm); @@ -211,8 +216,10 @@ function moodleoverflow_print_latest_discussions($moodleoverflow, $cm, $page = - // Check whether the user can subscribe to the discussion. $cansubtodiscussion = false; - if ((!is_guest($context, $USER) && isloggedin()) && has_capability('mod/moodleoverflow:viewdiscussion', $context) - && \mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $context)) { + if ( + (!is_guest($context, $USER) && isloggedin()) && has_capability('mod/moodleoverflow:viewdiscussion', $context) + && \mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $context) + ) { $cansubtodiscussion = true; } @@ -318,7 +325,6 @@ function moodleoverflow_print_latest_discussions($moodleoverflow, $cm, $page = - // Needs to be included for reputation to update properly. $preparedarray[$i]['userlink'] = $CFG->wwwroot . '/user/view.php?id=' . $discussion->userid . '&course=' . $moodleoverflow->course; - } else { $preparedarray[$i]['username'] = get_string('privacy:anonym_user_name', 'mod_moodleoverflow'); $preparedarray[$i]['userlink'] = null; @@ -377,7 +383,10 @@ function moodleoverflow_print_latest_discussions($moodleoverflow, $cm, $page = - // Discussion subscription. if (\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $context)) { $preparedarray[$i]['discussionsubicon'] = \mod_moodleoverflow\subscriptions::get_discussion_subscription_icon( - $moodleoverflow, $context, $discussion->discussion); + $moodleoverflow, + $context, + $discussion->discussion + ); } } @@ -642,7 +651,6 @@ function moodleoverflow_get_discussions_unread($cm) { } return $returnarray; } else { - // If there are no unread messages, return an empty array. return []; } @@ -733,14 +741,15 @@ function moodleoverflow_user_can_see_post($moodleoverflow, $discussion, $post, $ return false; } - if (!($post->reviewed == 1 || $post->userid == $USER->id || - capabilities::has(capabilities::REVIEW_POST, $modulecontext))) { + if ( + !($post->reviewed == 1 || $post->userid == $USER->id || + capabilities::has(capabilities::REVIEW_POST, $modulecontext)) + ) { return false; } // The user has the capability to see the discussion. return \core_availability\info_module::is_user_visible($cm, $USER->id, false); - } /** @@ -826,16 +835,25 @@ function moodleoverflow_add_discussion($discussion, $modulecontext, $userid = nu $post->course = $moodleoverflow->course; // Set to not reviewed, if questions should be reviewed, and user is not a reviewer themselves. - if (review::get_review_level($moodleoverflow) >= review::QUESTIONS && - !capabilities::has(capabilities::REVIEW_POST, $modulecontext, $userid)) { + if ( + review::get_review_level($moodleoverflow) >= review::QUESTIONS && + !capabilities::has(capabilities::REVIEW_POST, $modulecontext, $userid) + ) { $post->reviewed = 0; } // Submit the post to the database and get its id. $post->id = $DB->insert_record('moodleoverflow_posts', $post); // Save draft files to permanent file area. - $post->message = file_save_draft_area_files($discussion->draftideditor, $context->id, 'mod_moodleoverflow', 'post', - $post->id, mod_forum_post_form::editor_options($context, null), $post->message); + $post->message = file_save_draft_area_files( + $discussion->draftideditor, + $context->id, + 'mod_moodleoverflow', + 'post', + $post->id, + mod_forum_post_form::editor_options($context, null), + $post->message + ); $DB->set_field('moodleoverflow_posts', 'message', $post->message, ['id' => $post->id]); // Create the discussion object. @@ -934,8 +952,15 @@ function moodleoverflow_user_can_post($modulecontext, $posttoreplyto, $considerr * @param bool $multiplemarks The setting of multiplemarks (default: multiplemarks are not allowed) * @param stdClass|null $limitedanswersetting Two Unix timestamp wrapped in a stdClass, upper and lower label for answering. */ -function moodleoverflow_print_discussion($course, $cm, $moodleoverflow, $discussion, $post, - $multiplemarks = false, ?stdClass $limitedanswersetting = null) { +function moodleoverflow_print_discussion( + $course, + $cm, + $moodleoverflow, + $discussion, + $post, + $multiplemarks = false, + ?stdClass $limitedanswersetting = null +) { global $USER, $DB; // Check if the current is the starter of the discussion. $ownpost = (isloggedin() && ($USER->id == $post->userid)); @@ -959,17 +984,14 @@ function moodleoverflow_print_discussion($course, $cm, $moodleoverflow, $discuss // Lets clear all posts above level 2. // Check if there are answers. if (isset($post->children)) { - // Itereate through all answers. foreach ($post->children as $aid => $a) { $answercount += 1; // Check for each answer if they have children as well. if (isset($post->children[$aid]->children)) { - // Iterate through all comments. foreach ($post->children[$aid]->children as $cid => $c) { - // Delete the children of the comments. if (isset($post->children[$aid]->children[$cid]->children)) { unset($post->children[$aid]->children[$cid]->children); @@ -987,8 +1009,25 @@ function moodleoverflow_print_discussion($course, $cm, $moodleoverflow, $discuss $postread = !empty($post->postread); // Print the starting post. - echo moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $course, - $ownpost, false, '', '', $postread, true, $istracked, 0, $usermapping, 0, $multiplemarks, $limitedanswersetting); + echo moodleoverflow_print_post( + $post, + $discussion, + $moodleoverflow, + $cm, + $course, + $ownpost, + false, + '', + '', + $postread, + true, + $istracked, + 0, + $usermapping, + 0, + $multiplemarks, + $limitedanswersetting + ); // Print answer divider. if ($answercount == 1) { @@ -1001,8 +1040,19 @@ function moodleoverflow_print_discussion($course, $cm, $moodleoverflow, $discuss echo '
'; // Print the other posts. - echo moodleoverflow_print_posts_nested($course, $cm, $moodleoverflow, $discussion, $post, $istracked, $posts, - null, $usermapping, $multiplemarks, $limitedanswersetting); + echo moodleoverflow_print_posts_nested( + $course, + $cm, + $moodleoverflow, + $discussion, + $post, + $istracked, + $posts, + null, + $usermapping, + $multiplemarks, + $limitedanswersetting + ); echo '
'; } @@ -1067,7 +1117,6 @@ function moodleoverflow_get_all_discussion_posts($discussionid, $tracking, $modc // Assign ratings to the posts. foreach ($posts as $postid => $post) { - // Assign the ratings to the matching posts. $posts[$postid]->upvotes = $discussionratings[$post->id]->upvotes; $posts[$postid]->downvotes = $discussionratings[$post->id]->downvotes; @@ -1081,7 +1130,6 @@ function moodleoverflow_get_all_discussion_posts($discussionid, $tracking, $modc // Find all children of this post. foreach ($posts as $postid => $post) { - // Is it an old post? if ($tracking) { if (readtracking::moodleoverflow_is_old_post($post)) { @@ -1140,12 +1188,25 @@ function moodleoverflow_get_all_discussion_posts($discussionid, $tracking, $modc * @throws dml_exception * @throws moodle_exception */ -function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $course, - $ownpost = false, $link = false, - $footer = '', $highlight = '', $postisread = null, - $dummyifcantsee = true, $istracked = false, - $iscomment = false, $usermapping = [], $level = 0, - $multiplemarks = false, ?stdClass $limitedanswersetting = null) { +function moodleoverflow_print_post( + $post, + $discussion, + $moodleoverflow, + $cm, + $course, + $ownpost = false, + $link = false, + $footer = '', + $highlight = '', + $postisread = null, + $dummyifcantsee = true, + $istracked = false, + $iscomment = false, + $usermapping = [], + $level = 0, + $multiplemarks = false, + ?stdClass $limitedanswersetting = null +) { global $USER, $CFG, $OUTPUT, $PAGE, $DB; // Require the filelib. @@ -1173,7 +1234,6 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co // Check if the user has the capability to see posts. if (!moodleoverflow_user_can_see_post($moodleoverflow, $discussion, $post, $cm)) { - // No dummy message is requested. if (!$dummyifcantsee) { echo ''; @@ -1259,16 +1319,25 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co // When the post is already marked, remove the mark instead. $link = '/mod/moodleoverflow/discussion.php'; if ($post->markedhelpful) { - $commands[] = html_writer::tag('a', $str->marknothelpful, - ['class' => 'markhelpful onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'helpful']); + $commands[] = html_writer::tag( + 'a', + $str->marknothelpful, + ['class' => 'markhelpful onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'helpful'] + ); } else { // If there are already marked posts, change the string of the button. if ($helpfulposts) { - $commands[] = html_writer::tag('a', $str->alsomarkhelpful, - ['class' => 'markhelpful onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'helpful']); + $commands[] = html_writer::tag( + 'a', + $str->alsomarkhelpful, + ['class' => 'markhelpful onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'helpful'] + ); } else { - $commands[] = html_writer::tag('a', $str->markhelpful, - ['class' => 'markhelpful onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'helpful']); + $commands[] = html_writer::tag( + 'a', + $str->markhelpful, + ['class' => 'markhelpful onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'helpful'] + ); } } } @@ -1277,20 +1346,28 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co $canmarksolved = (($iscomment != $post->parent) && !empty($post->parent) && capabilities::has(capabilities::MARK_SOLVED, $modulecontext)); if ($canmarksolved) { - // When the post is already marked, remove the mark instead. $link = '/mod/moodleoverflow/discussion.php'; if ($post->markedsolution) { - $commands[] = html_writer::tag('a', $str->marknotsolved, - ['class' => 'marksolved onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'solved']); + $commands[] = html_writer::tag( + 'a', + $str->marknotsolved, + ['class' => 'marksolved onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'solved'] + ); } else { // If there are already marked posts, change the string of the button. if ($solvedposts) { - $commands[] = html_writer::tag('a', $str->alsomarksolved, - ['class' => 'marksolved onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'solved']); + $commands[] = html_writer::tag( + 'a', + $str->alsomarksolved, + ['class' => 'marksolved onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'solved'] + ); } else { - $commands[] = html_writer::tag('a', $str->marksolved, - ['class' => 'marksolved onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'solved']); + $commands[] = html_writer::tag( + 'a', + $str->marksolved, + ['class' => 'marksolved onlyifreviewed', 'role' => 'button', 'data-moodleoverflow-action' => 'solved'] + ); } } } @@ -1299,9 +1376,12 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co $age = time() - $post->created; // Make a link to edit your own post within the given time and not already reviewed. - if (($ownpost && ($age < get_config('moodleoverflow', 'maxeditingtime')) && - (!review::should_post_be_reviewed($post, $moodleoverflow) || !$post->reviewed)) - || capabilities::has(capabilities::EDIT_ANY_POST, $modulecontext) + if ( + ($ownpost && + ($age < get_config('moodleoverflow', 'maxeditingtime')) && + (!review::should_post_be_reviewed($post, $moodleoverflow) || + !$post->reviewed)) || + capabilities::has(capabilities::EDIT_ANY_POST, $modulecontext) ) { $editurl = new moodle_url('/mod/moodleoverflow/post.php', ['edit' => $post->id]); $commands[] = ['url' => $editurl, 'text' => $str->edit]; @@ -1309,9 +1389,10 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co // Give the option to delete a post. $notold = ($age < get_config('moodleoverflow', 'maxeditingtime')); - if (($ownpost && $notold && capabilities::has(capabilities::DELETE_OWN_POST, $modulecontext)) || - capabilities::has(capabilities::DELETE_ANY_POST, $modulecontext)) { - + if ( + ($ownpost && $notold && capabilities::has(capabilities::DELETE_OWN_POST, $modulecontext)) || + capabilities::has(capabilities::DELETE_ANY_POST, $modulecontext) + ) { $link = '/mod/moodleoverflow/post.php'; $commands[] = ['url' => new moodle_url($link, ['delete' => $post->id]), 'text' => $str->delete]; } @@ -1326,10 +1407,16 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co // Check if limitedanswertime is on. $settingexist = $limitedanswersetting->la_starttime != 0 || $limitedanswersetting->la_endtime != 0; if ($settingexist) { - $infolimited = $limitedanswersetting->la_starttime ? " " . get_string('la_info_starttime', - 'moodleoverflow', ['limitedanswerdate' => date('d.m.Y H:i', $limitedanswersetting->la_starttime)]) : ''; - $infolimited .= $limitedanswersetting->la_endtime ? " " . get_string('la_info_endtime', 'moodleoverflow', - ['limitedanswerdate' => date('d.m.Y H:i', $limitedanswersetting->la_endtime)]) : ''; + $infolimited = $limitedanswersetting->la_starttime ? " " . get_string( + 'la_info_starttime', + 'moodleoverflow', + ['limitedanswerdate' => date('d.m.Y H:i', $limitedanswersetting->la_starttime)] + ) : ''; + $infolimited .= $limitedanswersetting->la_endtime ? " " . get_string( + 'la_info_endtime', + 'moodleoverflow', + ['limitedanswerdate' => date('d.m.Y H:i', $limitedanswersetting->la_endtime)] + ) : ''; echo html_writer::div($infolimited, 'alert alert-warning', ['role' => 'alert']); } if (is_currently_time_limited($limitedanswersetting)) { @@ -1384,7 +1471,6 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co // Check the actual rating. if ($rating) { - // Convert the object. $rating = $rating->rating; @@ -1478,8 +1564,14 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co $mustachedata->withinreviewperiod = $reviewable; // Prepare the post. - $post->message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php', $mcid, 'mod_moodleoverflow', - 'post', $post->id); + $post->message = file_rewrite_pluginfile_urls( + $post->message, + 'pluginfile.php', + $mcid, + 'mod_moodleoverflow', + 'post', + $post->id + ); $options = new stdClass(); $options->para = false; $options->newlines = true; @@ -1556,7 +1648,7 @@ function render_limited_answer(string $htmlattributes, array &$commands, string $limitedanswerobject = html_writer::tag('span', $helpstring . ' ' . $helpicon); // Save the span in the commands with an extra value. - $commands[] = ['text' => $limitedanswerobject, 'attributes' => $limitedanswerattributes, 'limitedanswer' => $role]; + $commands[] = ['text' => $limitedanswerobject, 'attributes' => $limitedanswerattributes, 'limitedanswer' => $role]; } /** @@ -1578,9 +1670,19 @@ function render_limited_answer(string $htmlattributes, array &$commands, string * @throws dml_exception * @throws moodle_exception */ -function moodleoverflow_print_posts_nested($course, &$cm, $moodleoverflow, $discussion, $parent, - $istracked, $posts, $iscomment = null, $usermapping = [], - $multiplemarks = false, ?stdClass $limitedanswersetting = null) { +function moodleoverflow_print_posts_nested( + $course, + &$cm, + $moodleoverflow, + $discussion, + $parent, + $istracked, + $posts, + $iscomment = null, + $usermapping = [], + $multiplemarks = false, + ?stdClass $limitedanswersetting = null +) { global $USER; // Prepare the output. @@ -1588,13 +1690,11 @@ function moodleoverflow_print_posts_nested($course, &$cm, $moodleoverflow, $disc // If there are answers. if (!empty($posts[$parent->id]->children)) { - // We do not need the other parts of this variable anymore. $posts = $posts[$parent->id]->children; // Iterate through all answers. foreach ($posts as $post) { - // Answers should be seperated from each other. // While comments should be indented. if (!isset($iscomment)) { @@ -1621,13 +1721,40 @@ function moodleoverflow_print_posts_nested($course, &$cm, $moodleoverflow, $disc $postread = !empty($post->postread); // Print the answer. - $output .= moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $course, $ownpost, false, '', '', - $postread, true, $istracked, $parentid, $usermapping, $level, - $multiplemarks, $limitedanswersetting); + $output .= moodleoverflow_print_post( + $post, + $discussion, + $moodleoverflow, + $cm, + $course, + $ownpost, + false, + '', + '', + $postread, + true, + $istracked, + $parentid, + $usermapping, + $level, + $multiplemarks, + $limitedanswersetting + ); // Print its children. - $output .= moodleoverflow_print_posts_nested($course, $cm, $moodleoverflow, - $discussion, $post, $istracked, $posts, $parentid, $usermapping, $multiplemarks, $limitedanswersetting); + $output .= moodleoverflow_print_posts_nested( + $course, + $cm, + $moodleoverflow, + $discussion, + $post, + $istracked, + $posts, + $parentid, + $usermapping, + $multiplemarks, + $limitedanswersetting + ); // End the div. $output .= "\n"; @@ -1671,11 +1798,20 @@ function get_attachments($post, $cm) { $attachments[$i]['filename'] = $file->get_filename(); $mimetype = $file->get_mimetype(); - $iconimage = $OUTPUT->pix_icon(file_file_icon($file), - get_mimetype_description($file), 'moodle', - ['class' => 'icon']); - $path = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), - $file->get_itemid(), $file->get_filepath(), $file->get_filename()); + $iconimage = $OUTPUT->pix_icon( + file_file_icon($file), + get_mimetype_description($file), + 'moodle', + ['class' => 'icon'] + ); + $path = moodle_url::make_pluginfile_url( + $file->get_contextid(), + $file->get_component(), + $file->get_filearea(), + $file->get_itemid(), + $file->get_filepath(), + $file->get_filename() + ); $attachments[$i]['icon'] = $iconimage; $attachments[$i]['filepath'] = $path; @@ -1712,8 +1848,14 @@ function moodleoverflow_add_attachment($post, $forum, $cm) { $info = file_get_draft_area_info($post->attachments); $present = ($info['filecount'] > 0) ? '1' : ''; - file_save_draft_area_files($post->attachments, $context->id, 'mod_moodleoverflow', 'attachment', $post->id, - mod_moodleoverflow_post_form::attachment_options($forum)); + file_save_draft_area_files( + $post->attachments, + $context->id, + 'mod_moodleoverflow', + 'attachment', + $post->id, + mod_moodleoverflow_post_form::attachment_options($forum) + ); $DB->set_field('moodleoverflow_posts', 'attachment', $present, ['id' => $post->id]); @@ -1746,8 +1888,10 @@ function moodleoverflow_add_new_post($post) { } // Set to not reviewed, if posts should be reviewed, and user is not a reviewer themselves. - if (review::get_review_level($moodleoverflow) == review::EVERYTHING && - !has_capability('mod/moodleoverflow:reviewpost', $context)) { + if ( + review::get_review_level($moodleoverflow) == review::EVERYTHING && + !has_capability('mod/moodleoverflow:reviewpost', $context) + ) { $post->reviewed = 0; } else { $post->reviewed = 1; @@ -1756,8 +1900,15 @@ function moodleoverflow_add_new_post($post) { // Add the post to the database. $post->id = $DB->insert_record('moodleoverflow_posts', $post); // Save draft files to permanent file area. - $post->message = file_save_draft_area_files($post->draftideditor, $context->id, 'mod_moodleoverflow', 'post', - $post->id, mod_forum_post_form::editor_options($context, null), $post->message); + $post->message = file_save_draft_area_files( + $post->draftideditor, + $context->id, + 'mod_moodleoverflow', + 'post', + $post->id, + mod_forum_post_form::editor_options($context, null), + $post->message + ); $DB->set_field('moodleoverflow_posts', 'message', $post->message, ['id' => $post->id]); moodleoverflow_add_attachment($post, $moodleoverflow, $cm); @@ -1796,13 +1947,11 @@ function moodleoverflow_delete_discussion($discussion, $course, $cm, $moodleover // Get all posts related to the discussion. if ($posts = $DB->get_records('moodleoverflow_posts', ['discussion' => $discussion->id])) { - // Iterate through them and delete each one. foreach ($posts as $post) { $post->course = $discussion->course; $post->moodleoverflow = $discussion->moodleoverflow; if (!moodleoverflow_delete_post($post, 'ignore', $cm, $moodleoverflow)) { - // If the deletion failed, change the pointer. $result = false; } @@ -1860,12 +2009,24 @@ function moodleoverflow_delete_post($post, $deletechildren, $cm, $moodleoverflow // First delete the actual files on the disk. $fs = get_file_storage(); $context = context_module::instance($cm->id); - $attachments = $fs->get_area_files($context->id, 'mod_moodleoverflow', 'attachment', - $post->id, "filename", true); + $attachments = $fs->get_area_files( + $context->id, + 'mod_moodleoverflow', + 'attachment', + $post->id, + "filename", + true + ); foreach ($attachments as $attachment) { // Get file. - $file = $fs->get_file($context->id, 'mod_moodleoverflow', 'attachment', $post->id, - $attachment->get_filepath(), $attachment->get_filename()); + $file = $fs->get_file( + $context->id, + 'mod_moodleoverflow', + 'attachment', + $post->id, + $attachment->get_filepath(), + $attachment->get_filename() + ); // Delete it if it exists. if ($file) { $file->delete(); @@ -1984,7 +2145,6 @@ function moodleoverflow_count_discussions($moodleoverflow, $course) { // Check whether the cache for the moodleoverflow is set. if (!isset($cache[$course->id])) { - // Count the number of discussions. $sql = "SELECT m.id, COUNT(d.id) as dcount FROM {moodleoverflow} m @@ -1995,7 +2155,6 @@ function moodleoverflow_count_discussions($moodleoverflow, $course) { // Check whether there are discussions. if ($counts) { - // Loop through all records. foreach ($counts as $count) { $counts[$count->id] = $count->dcount; @@ -2003,7 +2162,6 @@ function moodleoverflow_count_discussions($moodleoverflow, $course) { // Cache the course. $cache[$course->id] = $counts; - } else { // There are no records. @@ -2061,18 +2219,14 @@ function moodleoverflow_update_user_grade_on_db($moodleoverflow, $postuserrating $grade = $postuserrating / $moodleoverflow->gradescalefactor; if ($grade > $moodleoverflow->grademaxgrade) { - $grade = $moodleoverflow->grademaxgrade; } // Save updated grade on local table. if ($DB->record_exists('moodleoverflow_grades', ['userid' => $userid, 'moodleoverflowid' => $moodleoverflow->id])) { - $DB->set_field('moodleoverflow_grades', 'grade', $grade, ['userid' => $userid, 'moodleoverflowid' => $moodleoverflow->id, ]); - } else { - $gradedataobject = new stdClass(); $gradedataobject->moodleoverflowid = $moodleoverflow->id; $gradedataobject->userid = $userid; @@ -2097,7 +2251,6 @@ function moodleoverflow_update_all_grades_for_cm($moodleoverflowid) { // Check whether moodleoverflow object has the added params. if ($moodleoverflow->grademaxgrade > 0 && $moodleoverflow->gradescalefactor > 0) { - // Get all users id. $params = ['moodleoverflowid' => $moodleoverflowid, 'moodleoverflowid2' => $moodleoverflowid]; $sql = 'SELECT DISTINCT u.userid FROM ( @@ -2158,7 +2311,7 @@ function moodleoverflow_quick_array_sort(&$array, $low, $high, $key, $order) { $right = $high; $pivot = $array[intval(($low + $high) / 2)]->$key; - $compare = function($a, $b) use ($order) { + $compare = function ($a, $b) use ($order) { if ($order == 'asc') { return $a < $b; } else { diff --git a/markposts.php b/markposts.php index 5753501e34e..72f8aedf426 100644 --- a/markposts.php +++ b/markposts.php @@ -65,19 +65,15 @@ // Default relink address. if ($returndiscussion === 0) { - // If no parameter is set, relink to the view. $returnto = new moodle_url("/mod/moodleoverflow/view.php", ['m' => $moodleoverflow->id]); - } else { - // Else relink back to the discussion we are coming from. $returnto = new moodle_url("/mod/moodleoverflow/discussion.php", ['d' => $returndiscussion]); } // Guests can't mark posts as read. if (isguestuser()) { - // Set Page-Parameter. $PAGE->set_title($course->shortname); $PAGE->set_heading($course->fullname); @@ -94,19 +90,21 @@ // Delete a single discussion. if (!empty($discussionid)) { - // Check if the discussion exists. $options = ['id' => $discussionid, 'moodleoverflow' => $moodleoverflow->id]; $discussion = moodleoverflow_get_record_or_exception('moodleoverflow_discussions', $options, 'invaliddiscussionid'); // Mark all the discussions read. - if (!\mod_moodleoverflow\readtracking::moodleoverflow_mark_discussion_read($discussionid, - context_module::instance($cm->id), $user->id)) { - + if ( + !\mod_moodleoverflow\readtracking::moodleoverflow_mark_discussion_read( + $discussionid, + context_module::instance($cm->id), + $user->id + ) + ) { // Display an error, if something failes. $message = get_string('markreadfailed', 'moodleoverflow'); $status = \core\output\notification::NOTIFY_ERROR; - } else { // The discussion is successfully marked as read. $message = get_string('markmoodleoverflowreadsuccessful', 'moodleoverflow'); @@ -116,18 +114,13 @@ // Redirect the user. redirect(moodleoverflow_go_back_to($returnto), $message, null, $status); exit; - } else { - // Mark all message read in the current instance. if (!\mod_moodleoverflow\readtracking::moodleoverflow_mark_moodleoverflow_read($cm, $user->id)) { - // Display an error, if something fails. $message = get_string('markreadfailed', 'moodleoverflow'); $status = \core\output\notification::NOTIFY_ERROR; - } else { - // All posts of the instance have been marked as read. $message = get_string('markdiscussionreadsuccessful', 'moodleoverflow'); $status = \core\output\notification::NOTIFY_SUCCESS; diff --git a/mod_form.php b/mod_form.php index 81e5d35b3a2..106c422e987 100644 --- a/mod_form.php +++ b/mod_form.php @@ -40,7 +40,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class mod_moodleoverflow_mod_form extends moodleform_mod { - /** * Defines forms elements. */ @@ -51,8 +50,11 @@ public function definition() { $mform = $this->_form; if ($this->get_instance()) { - $PAGE->requires->js_call_amd('mod_moodleoverflow/warnmodechange', 'init', - [$this->get_current()->forcesubscribe]); + $PAGE->requires->js_call_amd( + 'mod_moodleoverflow/warnmodechange', + 'init', + [$this->get_current()->forcesubscribe] + ); } // Adding the "general" fieldset, where all the common settings are showed. @@ -169,8 +171,11 @@ public function definition() { $mform->setDefault('trackingtype', $default); // Grade options. - $mform->addElement('header', 'gradeheading', - $CFG->branch >= 311 ? get_string('gradenoun') : get_string('grade')); + $mform->addElement( + 'header', + 'gradeheading', + $CFG->branch >= 311 ? get_string('gradenoun') : get_string('grade') + ); $mform->addElement('text', 'grademaxgrade', get_string('modgrademaxgrade', 'grades')); $mform->setType('grademaxgrade', PARAM_INT); @@ -183,7 +188,8 @@ public function definition() { if ($this->_features->gradecat) { $mform->addElement( - 'select', 'gradecat', + 'select', + 'gradecat', get_string('gradecategoryonmodform', 'grades'), grade_get_categories_menu($COURSE->id, $this->_outcomesused) ); @@ -254,14 +260,22 @@ public function definition() { // Limited answer setting elements.. $mform->addElement('hidden', 'la_answersfound', $answersfound); $mform->setType('la_answersfound', PARAM_BOOL); - $mform->addElement('date_time_selector', 'la_starttime', get_string('la_starttime', 'moodleoverflow'), - ['optional' => true]); + $mform->addElement( + 'date_time_selector', + 'la_starttime', + get_string('la_starttime', 'moodleoverflow'), + ['optional' => true] + ); $mform->addHelpButton('la_starttime', 'la_starttime', 'moodleoverflow'); $mform->disabledIf('la_starttime', 'la_answersfound', 'eq', true); - $mform->addElement('date_time_selector', 'la_endtime', get_string('la_endtime', 'moodleoverflow'), - ['optional' => true]); + $mform->addElement( + 'date_time_selector', + 'la_endtime', + get_string('la_endtime', 'moodleoverflow'), + ['optional' => true] + ); $mform->addHelpButton('la_endtime', 'la_endtime', 'moodleoverflow'); diff --git a/post.php b/post.php index 2a304f0d937..2e2e42d386d 100644 --- a/post.php +++ b/post.php @@ -69,8 +69,11 @@ // The guest needs to login. $strlogin = get_string('noguestpost', 'forum') . '

' . get_string('liketologin'); echo $OUTPUT->header(); - echo $OUTPUT->confirm($strlogin, get_login_url(), - $CFG->wwwroot . '/mod/moodleoverflow/view.php?m= ' . $information->moodleoverflow->id); + echo $OUTPUT->confirm( + $strlogin, + get_login_url(), + $CFG->wwwroot . '/mod/moodleoverflow/view.php?m= ' . $information->moodleoverflow->id + ); echo $OUTPUT->footer(); exit; } @@ -106,15 +109,19 @@ $information = $postcontrol->get_information(); echo $OUTPUT->header(); if ($information->deletetype == 'plural') { - echo $OUTPUT->confirm(get_string('deletesureplural', 'moodleoverflow', $information->replycount + 1), - 'post.php?delete='.$delete.'&confirm='.$delete, - $CFG->wwwroot . '/mod/moodleoverflow/discussion.php?d=' . $information->discussion->get_id() . - '#p' . $information->relatedpost->get_id()); + echo $OUTPUT->confirm( + get_string('deletesureplural', 'moodleoverflow', $information->replycount + 1), + 'post.php?delete=' . $delete . '&confirm=' . $delete, + $CFG->wwwroot . '/mod/moodleoverflow/discussion.php?d=' . $information->discussion->get_id() . + '#p' . $information->relatedpost->get_id() + ); } else { - echo $OUTPUT->confirm(get_string('deletesure', 'moodleoverflow', $information->replycount), + echo $OUTPUT->confirm( + get_string('deletesure', 'moodleoverflow', $information->replycount), "post.php?delete=$delete&confirm=$delete", $CFG->wwwroot . '/mod/moodleoverflow/discussion.php?d=' . $information->discussion->get_id() . - '#p' . $information->relatedpost->get_id()); + '#p' . $information->relatedpost->get_id() + ); } echo $OUTPUT->footer(); } diff --git a/renderer.php b/renderer.php index 905bc3d12d4..293f010f17d 100644 --- a/renderer.php +++ b/renderer.php @@ -35,7 +35,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class mod_moodleoverflow_renderer extends plugin_renderer_base { - /** * Display the discussion list for the view.php. * diff --git a/resetanonymous.php b/resetanonymous.php index f130b333924..cd33bcf84d1 100644 --- a/resetanonymous.php +++ b/resetanonymous.php @@ -39,14 +39,21 @@ if ($confirmed !== 1) { $a = new stdClass(); - $a->fullanoncount = $DB->count_records('moodleoverflow', - ['anonymous' => anonymous::EVERYTHING_ANONYMOUS]); - $a->questionanoncount = $DB->count_records('moodleoverflow', - ['anonymous' => anonymous::QUESTION_ANONYMOUS]); + $a->fullanoncount = $DB->count_records( + 'moodleoverflow', + ['anonymous' => anonymous::EVERYTHING_ANONYMOUS] + ); + $a->questionanoncount = $DB->count_records( + 'moodleoverflow', + ['anonymous' => anonymous::QUESTION_ANONYMOUS] + ); echo $OUTPUT->header(); echo html_writer::div( - $OUTPUT->confirm(get_string('resetanonymous_warning', 'moodleoverflow', $a), - new moodle_url($PAGE->url, ['confirmed' => true, 'sesskey' => sesskey()]), $returnurl), + $OUTPUT->confirm( + get_string('resetanonymous_warning', 'moodleoverflow', $a), + new moodle_url($PAGE->url, ['confirmed' => true, 'sesskey' => sesskey()]), + $returnurl + ), 'mod_moodleoverflow-hack-primary-to-danger-btn' ); echo $OUTPUT->footer(); diff --git a/settings.php b/settings.php index 73941449f6b..da5fe39d854 100644 --- a/settings.php +++ b/settings.php @@ -25,28 +25,47 @@ defined('MOODLE_INTERNAL') || die; if ($ADMIN->fulltree) { - require_once($CFG->dirroot . '/mod/moodleoverflow/lib.php'); // Number of discussions per page. - $settings->add(new admin_setting_configtext('moodleoverflow/manydiscussions', get_string('manydiscussions', 'moodleoverflow'), - get_string('configmanydiscussions', 'moodleoverflow'), 10, PARAM_INT)); + $settings->add(new admin_setting_configtext( + 'moodleoverflow/manydiscussions', + get_string('manydiscussions', 'moodleoverflow'), + get_string('configmanydiscussions', 'moodleoverflow'), + 10, + PARAM_INT + )); if (isset($CFG->maxbytes)) { $maxbytes = 0; if (get_config('moodleoverflow', 'maxbytes')) { $maxbytes = get_config('moodleoverflow', 'maxbytes'); } - $settings->add(new admin_setting_configselect('moodleoverflow/maxbytes', get_string('maxattachmentsize', 'moodleoverflow'), - get_string('configmaxbytes', 'moodleoverflow'), 512000, get_max_upload_sizes($CFG->maxbytes, 0, 0, $maxbytes))); + $settings->add(new admin_setting_configselect( + 'moodleoverflow/maxbytes', + get_string('maxattachmentsize', 'moodleoverflow'), + get_string('configmaxbytes', 'moodleoverflow'), + 512000, + get_max_upload_sizes($CFG->maxbytes, 0, 0, $maxbytes) + )); } // Default number of attachments allowed per post in all moodlevoerflows. - $settings->add(new admin_setting_configtext('moodleoverflow/maxattachments', get_string('maxattachments', 'moodleoverflow'), - get_string('configmaxattachments', 'moodleoverflow'), 9, PARAM_INT)); + $settings->add(new admin_setting_configtext( + 'moodleoverflow/maxattachments', + get_string('maxattachments', 'moodleoverflow'), + get_string('configmaxattachments', 'moodleoverflow'), + 9, + PARAM_INT + )); - $settings->add(new admin_setting_configtext('moodleoverflow/maxeditingtime', get_string('maxeditingtime', 'moodleoverflow'), - get_string('configmaxeditingtime', 'moodleoverflow'), 3600, PARAM_INT)); + $settings->add(new admin_setting_configtext( + 'moodleoverflow/maxeditingtime', + get_string('maxeditingtime', 'moodleoverflow'), + get_string('configmaxeditingtime', 'moodleoverflow'), + 3600, + PARAM_INT + )); // Default read tracking settings. @@ -54,92 +73,159 @@ $options[MOODLEOVERFLOW_TRACKING_OPTIONAL] = get_string('trackingoptional', 'moodleoverflow'); $options[MOODLEOVERFLOW_TRACKING_OFF] = get_string('trackingoff', 'moodleoverflow'); $options[MOODLEOVERFLOW_TRACKING_FORCED] = get_string('trackingon', 'moodleoverflow'); - $settings->add(new admin_setting_configselect('moodleoverflow/trackingtype', get_string('trackingtype', 'moodleoverflow'), - get_string('configtrackingtype', 'moodleoverflow'), MOODLEOVERFLOW_TRACKING_OPTIONAL, $options)); + $settings->add(new admin_setting_configselect( + 'moodleoverflow/trackingtype', + get_string('trackingtype', 'moodleoverflow'), + get_string('configtrackingtype', 'moodleoverflow'), + MOODLEOVERFLOW_TRACKING_OPTIONAL, + $options + )); // Should unread posts be tracked for each user? - $settings->add(new admin_setting_configcheckbox('moodleoverflow/trackreadposts', - get_string('trackmoodleoverflow', 'moodleoverflow'), get_string('configtrackmoodleoverflow', 'moodleoverflow'), 1)); + $settings->add(new admin_setting_configcheckbox( + 'moodleoverflow/trackreadposts', + get_string('trackmoodleoverflow', 'moodleoverflow'), + get_string('configtrackmoodleoverflow', 'moodleoverflow'), + 1 + )); // Allow moodleoverflows to be set to forced read tracking. - $settings->add(new admin_setting_configcheckbox('moodleoverflow/allowforcedreadtracking', - get_string('forcedreadtracking', 'moodleoverflow'), get_string('configforcedreadtracking', 'moodleoverflow'), 0)); + $settings->add(new admin_setting_configcheckbox( + 'moodleoverflow/allowforcedreadtracking', + get_string('forcedreadtracking', 'moodleoverflow'), + get_string('configforcedreadtracking', 'moodleoverflow'), + 0 + )); // Default number of days that a post is considered old. - $settings->add(new admin_setting_configtext('moodleoverflow/oldpostdays', get_string('oldpostdays', 'moodleoverflow'), - get_string('configoldpostdays', 'moodleoverflow'), 14, PARAM_INT)); + $settings->add(new admin_setting_configtext( + 'moodleoverflow/oldpostdays', + get_string('oldpostdays', 'moodleoverflow'), + get_string('configoldpostdays', 'moodleoverflow'), + 14, + PARAM_INT + )); // Default time (hour) to execute 'clean_read_records' cron. $options = []; for ($i = 0; $i < 24; $i++) { $options[$i] = sprintf("%02d", $i); } - $settings->add(new admin_setting_configselect('moodleoverflow/cleanreadtime', get_string('cleanreadtime', 'moodleoverflow'), - get_string('configcleanreadtime', 'moodleoverflow'), 2, $options)); + $settings->add(new admin_setting_configselect( + 'moodleoverflow/cleanreadtime', + get_string('cleanreadtime', 'moodleoverflow'), + get_string('configcleanreadtime', 'moodleoverflow'), + 2, + $options + )); $url = new moodle_url('/mod/moodleoverflow/resetanonymous.php'); - $settings->add(new admin_setting_configcheckbox('moodleoverflow/allowanonymous', + $settings->add(new admin_setting_configcheckbox( + 'moodleoverflow/allowanonymous', get_string('allowanonymous', 'moodleoverflow'), get_string('allowanonymous_desc', 'moodleoverflow', $url->out(false)), 1 )); // Allow teachers to disable ratings/reputation. - $settings->add(new admin_setting_configcheckbox('moodleoverflow/allowdisablerating', - get_string('allowdisablerating', 'moodleoverflow'), get_string('configallowdisablerating', 'moodleoverflow'), 1)); + $settings->add(new admin_setting_configcheckbox( + 'moodleoverflow/allowdisablerating', + get_string('allowdisablerating', 'moodleoverflow'), + get_string('configallowdisablerating', 'moodleoverflow'), + 1 + )); // Allow users to change their votes? - $settings->add(new admin_setting_configcheckbox('moodleoverflow/allowratingchange', - get_string('allowratingchange', 'moodleoverflow'), get_string('configallowratingchange', 'moodleoverflow'), 1)); + $settings->add(new admin_setting_configcheckbox( + 'moodleoverflow/allowratingchange', + get_string('allowratingchange', 'moodleoverflow'), + get_string('configallowratingchange', 'moodleoverflow'), + 1 + )); // Allow teachers to enable review before publish. - $settings->add(new admin_setting_configcheckbox('moodleoverflow/allowreview', - get_string('allowreview', 'moodleoverflow'), get_string('allowreview_desc', 'moodleoverflow'), 1)); + $settings->add(new admin_setting_configcheckbox( + 'moodleoverflow/allowreview', + get_string('allowreview', 'moodleoverflow'), + get_string('allowreview_desc', 'moodleoverflow'), + 1 + )); - $settings->add(new admin_setting_configtext('moodleoverflow/reviewpossibleaftertime', + $settings->add(new admin_setting_configtext( + 'moodleoverflow/reviewpossibleaftertime', get_string('reviewpossibleaftertime', 'moodleoverflow'), - get_string('reviewpossibleaftertime_desc', 'moodleoverflow'), 1800, PARAM_INT)); + get_string('reviewpossibleaftertime_desc', 'moodleoverflow'), + 1800, + PARAM_INT + )); // Set scales for the reputation. $votesettings = []; // Votescale: How much reputation gives a vote for another post? - $settings->add($votesettings[] = new admin_setting_configtext('moodleoverflow/votescalevote', + $settings->add($votesettings[] = new admin_setting_configtext( + 'moodleoverflow/votescalevote', get_string('votescalevote', 'moodleoverflow'), - get_string('configvotescalevote', 'moodleoverflow'), 1, PARAM_INT)); + get_string('configvotescalevote', 'moodleoverflow'), + 1, + PARAM_INT + )); // Votescale: How much reputation gives a post that has been downvoted? - $settings->add($votesettings[] = new admin_setting_configtext('moodleoverflow/votescaledownvote', - get_string('votescaledownvote', 'moodleoverflow'), get_string('configvotescaledownvote', 'moodleoverflow'), -5, PARAM_INT)); + $settings->add($votesettings[] = new admin_setting_configtext( + 'moodleoverflow/votescaledownvote', + get_string('votescaledownvote', 'moodleoverflow'), + get_string('configvotescaledownvote', 'moodleoverflow'), + -5, + PARAM_INT + )); // Votescale: How much reputation gives a post that has been upvoted? - $settings->add($votesettings[] = new admin_setting_configtext('moodleoverflow/votescaleupvote', + $settings->add($votesettings[] = new admin_setting_configtext( + 'moodleoverflow/votescaleupvote', get_string('votescaleupvote', 'moodleoverflow'), - get_string('configvotescaleupvote', 'moodleoverflow'), 5, PARAM_INT)); + get_string('configvotescaleupvote', 'moodleoverflow'), + 5, + PARAM_INT + )); // Votescale: How much reputation gives a post that is marked as solved. - $settings->add($votesettings[] = new admin_setting_configtext('moodleoverflow/votescalesolved', + $settings->add($votesettings[] = new admin_setting_configtext( + 'moodleoverflow/votescalesolved', get_string('votescalesolved', 'moodleoverflow'), - get_string('configvotescalesolved', 'moodleoverflow'), 30, PARAM_INT)); + get_string('configvotescalesolved', 'moodleoverflow'), + 30, + PARAM_INT + )); // Votescale: How much reputation gives a post that is marked as helpful. - $settings->add($votesettings[] = new admin_setting_configtext('moodleoverflow/votescalehelpful', + $settings->add($votesettings[] = new admin_setting_configtext( + 'moodleoverflow/votescalehelpful', get_string('votescalehelpful', 'moodleoverflow'), - get_string('configvotescalehelpful', 'moodleoverflow'), 15, PARAM_INT)); + get_string('configvotescalehelpful', 'moodleoverflow'), + 15, + PARAM_INT + )); // Number of discussions per page. - $settings->add($votesettings[] = new admin_setting_configtext('moodleoverflow/maxmailingtime', + $settings->add($votesettings[] = new admin_setting_configtext( + 'moodleoverflow/maxmailingtime', get_string('maxmailingtime', 'moodleoverflow'), - get_string('configmaxmailingtime', 'moodleoverflow'), 48, PARAM_INT)); + get_string('configmaxmailingtime', 'moodleoverflow'), + 48, + PARAM_INT + )); foreach ($votesettings as $setting) { $setting->set_updatedcallback('moodleoverflow_update_all_grades'); } // Allow teachers to see cumulative userstats. - $settings->add(new admin_setting_configcheckbox('moodleoverflow/showuserstats', - get_string('showuserstats', 'moodleoverflow'), get_string('configshowuserstats', 'moodleoverflow'), 0)); - - + $settings->add(new admin_setting_configcheckbox( + 'moodleoverflow/showuserstats', + get_string('showuserstats', 'moodleoverflow'), + get_string('configshowuserstats', 'moodleoverflow'), + 0 + )); } diff --git a/subscribe.php b/subscribe.php index 0af8f73c2ad..9408e1c2a40 100644 --- a/subscribe.php +++ b/subscribe.php @@ -52,8 +52,11 @@ } if (!is_null($discussionid)) { $url->param('d', $discussionid); - $discussion = moodleoverflow_get_record_or_exception('moodleoverflow_discussions', - ['id' => $discussionid, 'moodleoverflow' => $id], 'invaliddiscussionid'); + $discussion = moodleoverflow_get_record_or_exception( + 'moodleoverflow_discussions', + ['id' => $discussionid, 'moodleoverflow' => $id], + 'invaliddiscussionid' + ); } // Set the pages URL. @@ -90,9 +93,7 @@ // Retrieve the user from the database. $user = $DB->get_record('user', ['id' => $user], '*', MUST_EXIST); - } else { - // The user requested the subscription himself. $user = $USER; } @@ -106,7 +107,6 @@ // Guests, visitors and not enrolled people cannot subscribe. $isenrolled = is_enrolled($context, $USER, '', true); if (is_null($mode) && !$isenrolled) { - // Prepare the output. $PAGE->set_title($course->shortname); $PAGE->set_heading($course->fullname); @@ -140,7 +140,6 @@ // Set the new mode. switch ($mode) { - // Everyone can choose what he wants. case MOODLEOVERFLOW_CHOOSESUBSCRIBE: \mod_moodleoverflow\subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_CHOOSESUBSCRIBE); @@ -198,10 +197,8 @@ // Check if the user is subscribed to the moodleoverflow. // The action is to unsubscribe the user. if ($issubscribed) { - // Check if there is a sesskey. if (is_null($sesskey)) { - // Perpare the output. $PAGE->set_title($course->shortname); $PAGE->set_heading($course->fullname); @@ -212,7 +209,6 @@ // Was a discussion id submitted? if ($discussionid) { - // Create a new info object. $info2 = new stdClass(); $info2->moodleoverflow = format_string($moodleoverflow->name); @@ -221,7 +217,6 @@ // Create a confirm statement. $string = get_string('confirmunsubscribediscussion', 'moodleoverflow', $info2); echo $OUTPUT->confirm($string, $PAGE->url, $viewurl); - } else { // The discussion is not involved. @@ -240,16 +235,13 @@ // Check if a discussion id is submitted. if ($discussionid === null) { - // Unsubscribe the user and redirect him back to where he is coming from. if (\mod_moodleoverflow\subscriptions::unsubscribe_user($user->id, $moodleoverflow, $context, true)) { redirect($returnto, get_string('nownotsubscribed', 'moodleoverflow', $info), null, $notify['success']); } else { throw new moodle_exception('cannotunsubscribe', 'moodleoverflow', get_local_referer(false)); } - } else { - // Unsubscribe the user from the discussion. if (\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $context)) { $info->discussion = $discussion->name; @@ -258,7 +250,6 @@ throw new moodle_exception('cannotunsubscribe', 'moodleoverflow', get_local_referer(false)); } } - } else { // The user needs to be subscribed. @@ -281,7 +272,6 @@ // Check the session key. if (is_null($sesskey)) { - // Prepare the output. $PAGE->set_title($course->shortname); $PAGE->set_heading($course->fullname); @@ -292,7 +282,6 @@ // Check whether a discussion is referenced. if ($discussionid) { - // Create a new info object. $info2 = new stdClass(); $info2->moodleoverflow = format_string($moodleoverflow->name); @@ -301,7 +290,6 @@ // Create a confirm dialog. $string = get_string('confirmsubscribediscussion', 'moodleoverflow', $info2); echo $OUTPUT->confirm($string, $PAGE->url, $viewurl); - } else { // No discussion is referenced. @@ -320,12 +308,10 @@ // Check if the subscription is refered to a discussion. if ($discussionid == null) { - // Subscribe the user to the moodleoverflow instance. \mod_moodleoverflow\subscriptions::subscribe_user($user->id, $moodleoverflow, $context, true); redirect($returnto, get_string('nowsubscribed', 'moodleoverflow', $info), null, $notify['success']); exit; - } else { $info->discussion = $discussion->name; diff --git a/tests/behat/behat_mod_moodleoverflow.php b/tests/behat/behat_mod_moodleoverflow.php index 2d129a760d1..d8cac2fefc7 100644 --- a/tests/behat/behat_mod_moodleoverflow.php +++ b/tests/behat/behat_mod_moodleoverflow.php @@ -27,7 +27,7 @@ require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); -use Behat\Gherkin\Node\TableNode as TableNode; +use Behat\Gherkin\Node\TableNode; use Behat\Mink\Exception\ElementNotFoundException; use Behat\Mink\Exception\ExpectationException; @@ -40,8 +40,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class behat_mod_moodleoverflow extends behat_base { - - /** * Build basic background for moodleoverflow tests. * Builds: @@ -129,8 +127,12 @@ public function i_add_a_moodleoverflow_discussion_with_posts_from_different_user * @return void * @throws Exception */ - public function i_navigate_as_user_to_the_discussion(string $user, string $course, string $moodleoverflow, - string $discussion): void { + public function i_navigate_as_user_to_the_discussion( + string $user, + string $course, + string $moodleoverflow, + string $discussion + ): void { $this->execute('behat_auth::i_log_in_as', $user); $this->execute('behat_navigation::i_am_on_course_homepage', $course); $this->execute('behat_general::click_link', $this->escape($moodleoverflow)); @@ -283,8 +285,10 @@ protected function add_new_discussion($moodleoverflowname, TableNode $table, $bu // Fill form and post. $this->execute('behat_forms::i_set_the_following_fields_to_these_values', $table); - $this->execute('behat_forms::press_button', get_string('posttomoodleoverflow', - 'moodleoverflow')); + $this->execute('behat_forms::press_button', get_string( + 'posttomoodleoverflow', + 'moodleoverflow' + )); $this->execute('behat_general::i_wait_to_be_redirected'); } @@ -293,9 +297,11 @@ protected function add_new_discussion($moodleoverflowname, TableNode $table, $bu * @param string $discussiontitle */ protected function find_moodleoverflow_discussion_card(string $discussiontitle): \Behat\Mink\Element\Element { - return $this->find('xpath', - '//*[contains(concat(" ",normalize-space(@class)," ")," moodleoverflowdiscussion ")][.//*[text()="'. - $discussiontitle . '"]]'); + return $this->find( + 'xpath', + '//*[contains(concat(" ",normalize-space(@class)," ")," moodleoverflowdiscussion ")][.//*[text()="' . + $discussiontitle . '"]]' + ); } // phpcs:disable moodle.Files.LineLength.TooLong @@ -316,8 +322,12 @@ public function should_exist_in_the_moodleoverflow_discussion_card($element, $se $containernode = $this->find_moodleoverflow_discussion_card($discussiontitle); // Specific exception giving info about where can't we find the element. - $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, - "$element in the moodleoverflow discussion card."); + $exception = new ElementNotFoundException( + $this->getSession(), + $selectortype, + null, + "$element in the moodleoverflow discussion card." + ); // Looks for the requested node inside the container node. $this->find($selectortype, $element, $exception, $containernode); @@ -338,8 +348,12 @@ public function i_click_on_in_the_moodleoverflow_discussion_card($element, $sele $containernode = $this->find_moodleoverflow_discussion_card($discussiontitle); // Specific exception giving info about where can't we find the element. - $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, - "$element in the moodleoverflow discussion card."); + $exception = new ElementNotFoundException( + $this->getSession(), + $selectortype, + null, + "$element in the moodleoverflow discussion card." + ); // Looks for the requested node inside the container node. $node = $this->find($selectortype, $element, $exception, $containernode); diff --git a/tests/dailymail_test.php b/tests/dailymail_test.php index 9ac3c1e39d5..806cb6525d1 100644 --- a/tests/dailymail_test.php +++ b/tests/dailymail_test.php @@ -42,8 +42,6 @@ * @covers \mod_moodleoverflow\task\send_daily_mails::execute */ final class dailymail_test extends \advanced_testcase { - - /** @var stdClass test environment * This Class contains the test environment: * - the message sink to check if mails were sent. @@ -70,7 +68,7 @@ public function setUp(): void { $this->env->course = $this->getDataGenerator()->create_course(); $location = ['course' => $this->env->course->id, 'forcesubscribe' => MOODLEOVERFLOW_FORCESUBSCRIBE]; $this->env->moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $location); - $this->env->coursemodule = get_coursemodule_from_instance('moodleoverflow', $this->env->moodleoverflow->id); + $this->env->cm = get_coursemodule_from_instance('moodleoverflow', $this->env->moodleoverflow->id); } /** @@ -200,8 +198,8 @@ public function test_content_of_mail_delivery(): void { // Build the text that the mail should have. // Text structure at get_string('digestunreadpost', moodleoverflow). - $linktocourse = 'env->course->id; - $linktoforum = 'env->coursemodule->id; + $linktocourse = 'env->course->id; + $linktoforum = 'env->cm->id; $linktodiscussion = 'env->discussions[0]->id; diff --git a/tests/discussion_test.php b/tests/discussion_test.php index a69926e95e6..b4a2efac8ef 100644 --- a/tests/discussion_test.php +++ b/tests/discussion_test.php @@ -36,7 +36,6 @@ * @covers \mod_moodleoverflow\discussion\discussion */ final class discussion_test extends \advanced_testcase { - /** @var \stdClass test course */ private $course; @@ -95,8 +94,16 @@ public function test_create_discussion(): void { $this->setUser($this->teacher); // Build a new discussion object. - $discussion = discussion::construct_without_id($this->course->id, $this->moodleoverflow->id, 'Discussion Topic', - 0, $this->teacher->id, $time, $time, $this->teacher->id); + $discussion = discussion::construct_without_id( + $this->course->id, + $this->moodleoverflow->id, + 'Discussion Topic', + 0, + $this->teacher->id, + $time, + $time, + $this->teacher->id + ); $discussionid = $discussion->moodleoverflow_add_discussion($prepost); $posts = $discussion->moodleoverflow_get_discussion_posts(); $post = $posts[$discussion->get_firstpostid()]; diff --git a/tests/generator/lib.php b/tests/generator/lib.php index c8d2873704f..0e9e4991409 100644 --- a/tests/generator/lib.php +++ b/tests/generator/lib.php @@ -33,7 +33,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class mod_moodleoverflow_generator extends testing_module_generator { - /** * @var int keep track of how many moodleoverflow discussions have been created. */ diff --git a/tests/locallib_test.php b/tests/locallib_test.php index dbbf58d11c1..f325b9111a9 100644 --- a/tests/locallib_test.php +++ b/tests/locallib_test.php @@ -37,7 +37,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class locallib_test extends advanced_testcase { - public function setUp(): void { parent::setUp(); \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache(); @@ -174,5 +173,4 @@ public function test_moodleoverflow_disallow_subscribe_on_create(): void { $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($user->id, $mo, $context)); } } - } diff --git a/tests/notification_mail_test.php b/tests/notification_mail_test.php index c5cb4b0fd23..d422819b193 100644 --- a/tests/notification_mail_test.php +++ b/tests/notification_mail_test.php @@ -47,7 +47,6 @@ */ #[CoversClass(mail_manager::class)] final class notification_mail_test extends \advanced_testcase { - // Attributes. /** @var object The data that will be used for testing. diff --git a/tests/post_test.php b/tests/post_test.php index ff6729482a1..9d070faeb59 100644 --- a/tests/post_test.php +++ b/tests/post_test.php @@ -42,7 +42,6 @@ * @covers \mod_moodleoverflow\post\post */ final class post_test extends \advanced_testcase { - /** @var stdClass test course */ private $course; @@ -92,8 +91,19 @@ public function test_create_post(): void { // Build a new post object. $time = time(); $message = 'a unique message'; - $post = post::construct_without_id($this->discussion->get_id(), $this->post->get_id(), $this->teacher->id, $time, - $time, $message, 0, '', 0, 1, null); + $post = post::construct_without_id( + $this->discussion->get_id(), + $this->post->get_id(), + $this->teacher->id, + $time, + $time, + $message, + 0, + '', + 0, + 1, + null + ); $post->moodleoverflow_add_new_post(); // The post should be in the database. @@ -126,7 +136,7 @@ public function test_edit_post(): void { // The message and modified time should be changed. $post = $DB->get_record('moodleoverflow_posts', ['id' => $this->post->get_id()]); - $this->assertEquals($message, $post->message); + $this->assertEquals($message, $post->message); $this->assertEquals($time, $post->modified); } @@ -203,12 +213,12 @@ private function helper_course_set_up() { private function add_new_attachment($object, $modulecontext, $filename, $filecontent) { global $DB; $fileinfo = [ - 'contextid' => $modulecontext->id, // ID of the context. - 'component' => 'mod_moodleoverflow', // Your component name. - 'filearea' => 'attachment', // Usually = table name. - 'itemid' => $object->id, // Usually = ID of the item (e.g. the post. - 'filepath' => '/', // Any path beginning and ending in /. - 'filename' => $filename, // Any filename. + 'contextid' => $modulecontext->id, // ID of the context. + 'component' => 'mod_moodleoverflow', // Your component name. + 'filearea' => 'attachment', // Usually = table name. + 'itemid' => $object->id, // Usually = ID of the item (e.g. the post. + 'filepath' => '/', // Any path beginning and ending in /. + 'filename' => $filename, // Any filename. ]; $fs = get_file_storage(); $fs->create_file_from_string($fileinfo, $filecontent); // Creates a new file containing the text 'hello world'. diff --git a/tests/privacy_provider_test.php b/tests/privacy_provider_test.php index b02612c3e8e..28f91775e5e 100644 --- a/tests/privacy_provider_test.php +++ b/tests/privacy_provider_test.php @@ -125,9 +125,9 @@ protected function assert_post_data($expected, $actual, $writer) { */ public function test_user_has_never_posted(): void { // Create a course with moodleoverflow forums. - list($course, $forum) = $this->create_courses_and_modules(3); + [$course, $forum] = $this->create_courses_and_modules(3); // Create users. - list($user, $otheruser) = $this->create_and_enrol_users($course, 2); + [$user, $otheruser] = $this->create_and_enrol_users($course, 2); // Post to forum. $this->generator->post_to_forum($forum, $otheruser); @@ -156,8 +156,8 @@ public function test_user_has_never_posted(): void { */ public function test_user_has_never_posted_subscribed_to_forum(): void { // Create a course, with a forum, our user under test, another user, and a discussion + post from the other user. - list($course, $forum) = $this->create_courses_and_modules(3); - list($user, $otheruser) = $this->create_and_enrol_users($course, 2); + [$course, $forum] = $this->create_courses_and_modules(3); + [$user, $otheruser] = $this->create_and_enrol_users($course, 2); $this->generator->post_to_forum($forum, $otheruser); $cm = get_coursemodule_from_instance('moodleoverflow', $forum->id); $context = \context_module::instance($cm->id); @@ -189,13 +189,13 @@ public function test_user_has_never_posted_subscribed_to_forum(): void { */ public function test_user_has_never_posted_subscribed_to_discussion(): void { // Create a course, with a forum, our user under test, another user, and a discussion + post from the other user. - list($course, $forum) = $this->create_courses_and_modules(3); + [$course, $forum] = $this->create_courses_and_modules(3); // Create users. - list($user, $otheruser) = $this->create_and_enrol_users($course, 2); + [$user, $otheruser] = $this->create_and_enrol_users($course, 2); // Post twice - only the second discussion should be included. $this->generator->post_to_forum($forum, $otheruser); - list($discussion, $post) = $this->generator->post_to_forum($forum, $otheruser); + [$discussion, $post] = $this->generator->post_to_forum($forum, $otheruser); $cm = get_coursemodule_from_instance('moodleoverflow', $forum->id); $context = \context_module::instance($cm->id); // Subscribe the user to the discussion. @@ -233,11 +233,11 @@ public function test_user_has_never_posted_subscribed_to_discussion(): void { * content returned. */ public function test_user_has_posted_own_discussion(): void { - list($course, $forum) = $this->create_courses_and_modules(3); - list($user, $otheruser) = $this->create_users($course, 2); + [$course, $forum] = $this->create_courses_and_modules(3); + [$user, $otheruser] = $this->create_users($course, 2); // Post twice - only the second discussion should be included. - list($discussion, $post) = $this->generator->post_to_forum($forum, $user); - list($otherdiscussion) = $this->generator->post_to_forum($forum, $otheruser); + [$discussion, $post] = $this->generator->post_to_forum($forum, $user); + [$otherdiscussion] = $this->generator->post_to_forum($forum, $otheruser); $cm = get_coursemodule_from_instance('moodleoverflow', $forum->id); $context = \context_module::instance($cm->id); // Retrieve all contexts - only this context should be returned. @@ -251,8 +251,11 @@ public function test_user_has_posted_own_discussion(): void { $this->assertTrue($writer->has_any_data()); // The other discussion should not have been returned as we did not post in it. $this->assertEmpty($writer->get_data(data_export_helper::get_subcontext($otherdiscussion))); - $this->assert_discussion_data($discussion, - $writer->get_data(data_export_helper::get_subcontext($discussion)), $user->id); + $this->assert_discussion_data( + $discussion, + $writer->get_data(data_export_helper::get_subcontext($discussion)), + $user->id + ); $this->assert_post_data($post, $writer->get_data(data_export_helper::get_subcontext($discussion, $post)), $writer); } @@ -263,11 +266,11 @@ public function test_user_has_posted_own_discussion(): void { public function test_user_has_posted_reply(): void { global $DB; // Create several courses and forums. We only insert data into the final one. - list($course, $forum) = $this->create_courses_and_modules(3); - list($user, $otheruser) = $this->create_users($course, 2); + [$course, $forum] = $this->create_courses_and_modules(3); + [$user, $otheruser] = $this->create_users($course, 2); // Post twice - only the second discussion should be included. - list($discussion, $post) = $this->generator->post_to_forum($forum, $otheruser); - list($otherdiscussion) = $this->generator->post_to_forum($forum, $otheruser); + [$discussion, $post] = $this->generator->post_to_forum($forum, $otheruser); + [$otherdiscussion] = $this->generator->post_to_forum($forum, $otheruser); $cm = get_coursemodule_from_instance('moodleoverflow', $forum->id); $context = \context_module::instance($cm->id); // Post a reply to the other person's post. @@ -302,8 +305,8 @@ public function test_user_has_posted_reply(): void { public function test_user_has_rated_others(): void { $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('moodleoverflow', ['course' => $course->id, 'scale' => 100]); - list($user, $otheruser) = $this->create_users($course, 2); - list($discussion, $post) = $this->generator->post_to_forum($forum, $otheruser); + [$user, $otheruser] = $this->create_users($course, 2); + [$discussion, $post] = $this->generator->post_to_forum($forum, $otheruser); $cm = get_coursemodule_from_instance('moodleoverflow', $forum->id); $context = \context_module::instance($cm->id); // Rate the other users content. @@ -342,8 +345,8 @@ public function test_user_has_rated_others(): void { public function test_user_has_been_rated(): void { $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('moodleoverflow', [ 'course' => $course->id, 'scale' => 100]); - list($user, $otheruser) = $this->create_users($course, 3); - list($discussion, $post) = $this->generator->post_to_forum($forum, $user); + [$user, $otheruser] = $this->create_users($course, 3); + [$discussion, $post] = $this->generator->post_to_forum($forum, $user); $cm = get_coursemodule_from_instance('moodleoverflow', $forum->id); $context = \context_module::instance($cm->id); // Other users rate my content. @@ -380,7 +383,7 @@ public function test_user_tracking_data(): void { $cmoff = get_coursemodule_from_instance('moodleoverflow', $forumoff->id); $contextoff = \context_module::instance($cmoff->id); $forumon = $this->getDataGenerator()->create_module('moodleoverflow', ['course' => $course->id]); - list($user) = $this->create_users($course, 1); + [$user] = $this->create_users($course, 1); // Set user tracking data. readtracking::moodleoverflow_stop_tracking($forumoff->id, $user->id); readtracking::moodleoverflow_start_tracking($forumon->id, $user->id); @@ -410,19 +413,19 @@ public function test_user_read_posts(): void { $forum3 = $this->getDataGenerator()->create_module('moodleoverflow', ['course' => $course->id]); $cm3 = get_coursemodule_from_instance('moodleoverflow', $forum3->id); $context3 = \context_module::instance($cm3->id); - list($author, $user) = $this->create_users($course, 2); - list($f1d1, $f1p1) = $this->generator->post_to_forum($forum1, $author); + [$author, $user] = $this->create_users($course, 2); + [$f1d1, $f1p1] = $this->generator->post_to_forum($forum1, $author); $f1p1reply = $this->generator->post_to_discussion($forum1, $f1d1, $author); $f1d1 = $DB->get_record('moodleoverflow_discussions', ['id' => $f1d1->id]); - list($f1d2, $f1p2) = $this->generator->post_to_forum($forum1, $author); - list($f2d1, $f2p1) = $this->generator->post_to_forum($forum2, $author); + [$f1d2, $f1p2] = $this->generator->post_to_forum($forum1, $author); + [$f2d1, $f2p1] = $this->generator->post_to_forum($forum2, $author); $f2p1reply = $this->generator->post_to_discussion($forum2, $f2d1, $author); $f2d1 = $DB->get_record('moodleoverflow_discussions', ['id' => $f2d1->id]); - list($f2d2, $f2p2) = $this->generator->post_to_forum($forum2, $author); - list($f3d1, $f3p1) = $this->generator->post_to_forum($forum3, $author); + [$f2d2, $f2p2] = $this->generator->post_to_forum($forum2, $author); + [$f3d1, $f3p1] = $this->generator->post_to_forum($forum3, $author); $f3p1reply = $this->generator->post_to_discussion($forum3, $f3d1, $author); $f3d1 = $DB->get_record('moodleoverflow_discussions', ['id' => $f3d1->id]); - list($f3d2, $f3p2) = $this->generator->post_to_forum($forum3, $author); + [$f3d2, $f3p2] = $this->generator->post_to_forum($forum3, $author); // Insert read info. // User has read post1, but not the reply or second post in forum1. @@ -496,24 +499,26 @@ public function test_post_attachment_inclusion(): void { global $DB; $fs = get_file_storage(); $course = $this->getDataGenerator()->create_course(); - list($author) = $this->create_users($course, 2); + [$author] = $this->create_users($course, 2); $forum = $this->getDataGenerator()->create_module('moodleoverflow', ['course' => $course->id, 'scale' => 100]); $cm = get_coursemodule_from_instance('moodleoverflow', $forum->id); $context = \context_module::instance($cm->id); // Create a new discussion + post in the forum. - list($discussion, $post) = $this->generator->post_to_forum($forum, $author); + [$discussion, $post] = $this->generator->post_to_forum($forum, $author); $discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $discussion->id]); // Add a number of replies. $this->generator->reply_to_post($post, $author); $reply = $this->generator->reply_to_post($post, $author); $this->generator->reply_to_post($reply, $author); // Add a fake inline image to the original post. - $createdfile = $fs->create_file_from_string(['contextid' => $context->id, 'component' => 'mod_moodleoverflow', + $createdfile = $fs->create_file_from_string( + ['contextid' => $context->id, 'component' => 'mod_moodleoverflow', 'filearea' => 'attachment', 'itemid' => $post->id, 'filepath' => '/', 'filename' => 'example.jpg', ], - 'image contents (not really)'); + 'image contents (not really)' + ); // Create a second discussion + post in the forum without tags. - list(, $otherpost) = $this->generator->post_to_forum($forum, $author); + [, $otherpost] = $this->generator->post_to_forum($forum, $author); // Add a number of replies. $this->generator->reply_to_post($otherpost, $author); $this->generator->reply_to_post($otherpost, $author); @@ -556,7 +561,7 @@ public function test_all_users_deleted_from_context(): void { foreach ($forums as $forum) { $context = $contexts[$forum->id]; // Create a new discussion + post in the forum. - list($discussion, $post) = $this->generator->post_to_forum($forum, $user); + [$discussion, $post] = $this->generator->post_to_forum($forum, $user); $discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $discussion->id]); $discussions[$discussion->id] = $discussion; // Add a number of replies. @@ -618,7 +623,7 @@ public function test_all_users_deleted_from_context(): void { }); // All forum discussions and posts should have been deleted in this forum. $this->assertCount(0, $DB->get_records('moodleoverflow_discussions', ['moodleoverflow' => $forum->id])); - list ($insql, $inparams) = $DB->get_in_or_equal(array_keys($discussionsinforum)); + [$insql, $inparams] = $DB->get_in_or_equal(array_keys($discussionsinforum)); $this->assertCount(0, $DB->get_records_select('moodleoverflow_posts', "discussion {$insql}", $inparams)); // All uploaded files relating to this context should have been deleted (post content). foreach ($postsinforum as $post) { @@ -642,7 +647,7 @@ public function test_all_users_deleted_from_context(): void { }); // Forum discussions and posts should not have been deleted in this forum. $this->assertGreaterThan(0, $DB->count_records('moodleoverflow_discussions', ['moodleoverflow' => $forum->id])); - list ($insql, $inparams) = $DB->get_in_or_equal(array_keys($discussionsinforum)); + [$insql, $inparams] = $DB->get_in_or_equal(array_keys($discussionsinforum)); $this->assertGreaterThan(0, $DB->count_records_select('moodleoverflow_posts', "discussion {$insql}", $inparams)); // Uploaded files relating to this context should remain. foreach ($postsinforum as $post) { @@ -686,7 +691,7 @@ public function test_delete_data_for_user(): void { foreach ($forums as $forum) { $context = $contexts[$forum->id]; // Create a new discussion + post in the forum. - list($discussion, $post) = $this->generator->post_to_forum($forum, $user); + [$discussion, $post] = $this->generator->post_to_forum($forum, $user); $discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $discussion->id]); $discussions[$discussion->id] = $discussion; $postsbyforum[$user->id][$context->id] = []; @@ -728,7 +733,6 @@ public function test_delete_data_for_user(): void { $forum = $forums[$discussion->moodleoverflow]; // Rate the other users content. if ($post->userid != $user->id) { - $rating = []; $rating['moodleoverflowid'] = $forum->id; $rating['discussionid'] = $discussion->id; @@ -745,7 +749,7 @@ public function test_delete_data_for_user(): void { $otherpostids = []; foreach ($postsbyforum as $user => $contexts) { foreach ($contexts as $thiscontextid => $theseposts) { - $thesepostids = array_map(function($post) { + $thesepostids = array_map(function ($post) { return $post->id; }, $theseposts); @@ -758,8 +762,8 @@ public function test_delete_data_for_user(): void { } } } - list($postinsql, $postinparams) = $DB->get_in_or_equal($deletedpostids, SQL_PARAMS_NAMED); - list($otherpostinsql, $otherpostinparams) = $DB->get_in_or_equal($otherpostids, SQL_PARAMS_NAMED); + [$postinsql, $postinparams] = $DB->get_in_or_equal($deletedpostids, SQL_PARAMS_NAMED); + [$otherpostinsql, $otherpostinparams] = $DB->get_in_or_equal($otherpostids, SQL_PARAMS_NAMED); $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist( \core_user::get_user($user1->id), @@ -775,19 +779,23 @@ public function test_delete_data_for_user(): void { // Four of those posts should have been marked as deleted. // That means that the user ID is null and the message is empty. $this->assertCount(4, $DB->get_records_select('moodleoverflow_posts', "userid = :userid" - . " AND " . $DB->sql_compare_text('message') . " = " . $DB->sql_compare_text(':message') - , [ + . " AND " . $DB->sql_compare_text('message') . " = " . $DB->sql_compare_text(':message'), [ 'userid' => 0, 'message' => '', ])); // Only user1's posts should have been marked this way. $this->assertCount(4, $DB->get_records('moodleoverflow_posts', ['userid' => 0])); - $this->assertCount(4, $DB->get_records_select('moodleoverflow_posts', - $DB->sql_compare_text('message') . " = " . $DB->sql_compare_text(':message'), ['message' => ''])); + $this->assertCount(4, $DB->get_records_select( + 'moodleoverflow_posts', + $DB->sql_compare_text('message') . " = " . $DB->sql_compare_text(':message'), + ['message' => ''] + )); // Only the posts in the first discussion should have been marked this way. - $this->assertCount(4, $DB->get_records_select('moodleoverflow_posts', "userid = :userid AND id {$postinsql}", + $this->assertCount(4, $DB->get_records_select( + 'moodleoverflow_posts', + "userid = :userid AND id {$postinsql}", array_merge($postinparams, ['userid' => 0]) )); @@ -897,7 +905,7 @@ public function test_delete_data_for_users(): void { $context = $contexts[$moodleoverflow->id]; // Create a new discussion + post in the moodleoverflow. - list($discussion, $post) = $this->generator->post_to_forum($moodleoverflow, $user); + [$discussion, $post] = $this->generator->post_to_forum($moodleoverflow, $user); $discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $discussion->id]); $discussions[$discussion->id] = $discussion; @@ -981,8 +989,7 @@ public function test_delete_data_for_users(): void { // That means the userid equals 0, message equals empty string, and message format equals FORMAT_PLAIN. $this->assertCount(4, $DB->get_records_select('moodleoverflow_posts', "userid = :userid" . " AND " . $DB->sql_compare_text('message') . " = " . $DB->sql_compare_text(':message') - . " AND " . $DB->sql_compare_text('messageformat') . " = " . $DB->sql_compare_text(':messageformat') - , [ + . " AND " . $DB->sql_compare_text('messageformat') . " = " . $DB->sql_compare_text(':messageformat'), [ 'userid' => 0, 'messageformat' => FORMAT_PLAIN, 'message' => '', @@ -991,7 +998,7 @@ public function test_delete_data_for_users(): void { // Ratings were made anonymous from the affected posts. // All ratings from user1 in Moodleoverflow1 should have been deleted so 16 entries should be anonymous. // (20 post in Moodleoverflow -4 post belonging to user 1). - $this->assertCount(16, $DB->get_records_select('moodleoverflow_ratings', "userid = :userid" , ['userid' => 0 ])); + $this->assertCount(16, $DB->get_records_select('moodleoverflow_ratings', "userid = :userid", ['userid' => 0 ])); // All ratings should still exist (2 Moodleoverflows * 5 users * 16 post from other people). $this->assertCount(160, $DB->get_records('moodleoverflow_ratings')); @@ -1006,8 +1013,7 @@ public function test_delete_data_for_users(): void { // All of the discussions from moodleoverflow 1 should have been filed with empty values. // That means the userid equals 0, and name equals empty string. $this->assertCount(1, $DB->get_records_select('moodleoverflow_discussions', "userid = :userid" - . " AND " . $DB->sql_compare_text('name') . " = " . $DB->sql_compare_text(':name') - , [ + . " AND " . $DB->sql_compare_text('name') . " = " . $DB->sql_compare_text(':name'), [ 'userid' => 0, 'name' => '', ])); @@ -1017,7 +1023,7 @@ public function test_delete_data_for_users(): void { $otherpostids = []; foreach ($postsbyforum as $user => $contexts) { foreach ($contexts as $thiscontextid => $theseposts) { - $thesepostids = array_map(function($post) { + $thesepostids = array_map(function ($post) { return $post->id; }, $theseposts); @@ -1030,15 +1036,18 @@ public function test_delete_data_for_users(): void { } } } - list($postinsql, $postinparams) = $DB->get_in_or_equal($deletedpostids, SQL_PARAMS_NAMED); - list($otherpostinsql, $otherpostinparams) = $DB->get_in_or_equal($otherpostids, SQL_PARAMS_NAMED); + [$postinsql, $postinparams] = $DB->get_in_or_equal($deletedpostids, SQL_PARAMS_NAMED); + [$otherpostinsql, $otherpostinparams] = $DB->get_in_or_equal($otherpostids, SQL_PARAMS_NAMED); // For each context 20 files are created. // 2 are from the critical user in the critical context, which are deleted. $this->assertCount(0, $DB->get_records_select('files', "itemid {$postinsql}", $postinparams)); // All other files still exist. - $this->assertCount(18, $DB->get_records_select('files', "filename <> '.' AND itemid {$otherpostinsql}", - $otherpostinparams)); + $this->assertCount(18, $DB->get_records_select( + 'files', + "filename <> '.' AND itemid {$otherpostinsql}", + $otherpostinparams + )); } /** @@ -1051,7 +1060,7 @@ public function test_get_users_in_context_post_author(): void { $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $context = \context_module::instance($cm->id); - list($author) = $this->create_users($course, 2); + [$author] = $this->create_users($course, 2); $this->generator->post_to_forum($moodleoverflow, $author); @@ -1074,9 +1083,9 @@ public function test_get_users_in_context_post_authors(): void { $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $context = \context_module::instance($cm->id); - list($author, $user) = $this->create_users($course, 3); + [$author, $user] = $this->create_users($course, 3); - list($fd1) = $this->generator->post_to_forum($moodleoverflow, $author); + [$fd1] = $this->generator->post_to_forum($moodleoverflow, $author); $this->generator->post_to_discussion($moodleoverflow, $fd1, $user); $DB->get_record('moodleoverflow_discussions', ['id' => $fd1->id]); @@ -1101,8 +1110,8 @@ public function test_get_users_in_context_post_ratings(): void { $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', ['course' => $course->id]); $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $context = \context_module::instance($cm->id); - list($author, $user) = $this->create_users($course, 3); - list($fd1, $fp1) = $this->generator->post_to_forum($moodleoverflow, $author); + [$author, $user] = $this->create_users($course, 3); + [$fd1, $fp1] = $this->generator->post_to_forum($moodleoverflow, $author); $time = time(); $rating = (object) ['userid' => $user->id, 'postid' => $fp1->id, 'discussionid' => $fd1->id, 'moodleoverflowid' => $moodleoverflow->id, 'rating' => 1, 'firstrated' => $time, @@ -1128,7 +1137,7 @@ public function test_get_users_in_context_with_subscription(): void { $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', ['course' => $course->id]); $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $context = \context_module::instance($cm->id); - list($user) = $this->create_users($course, 2); + [$user] = $this->create_users($course, 2); // Subscribe the user to the moodleoverflow. subscriptions::subscribe_user($user->id, $moodleoverflow, $context); @@ -1151,11 +1160,11 @@ public function test_get_users_in_context_with_discussion_subscription(): void { $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $context = \context_module::instance($cm->id); $othermoodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', ['course' => $course->id]); - list($author, $user, $otheruser) = $this->create_users($course, 3); + [$author, $user, $otheruser] = $this->create_users($course, 3); // Post in both of the moodleoverflows. - list($fd1) = $this->generator->post_to_forum($moodleoverflow, $author); - list($ofd1) = $this->generator->post_to_forum($othermoodleoverflow, $author); + [$fd1] = $this->generator->post_to_forum($moodleoverflow, $author); + [$ofd1] = $this->generator->post_to_forum($othermoodleoverflow, $author); // Subscribe the user to the discussions. subscriptions::subscribe_user_to_discussion($user->id, $fd1, $context); @@ -1181,11 +1190,11 @@ public function test_get_users_in_context_with_read_post_tracking(): void { $othermoodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', ['course' => $course->id]); $othercm = get_coursemodule_from_instance('moodleoverflow', $othermoodleoverflow->id); - list($author, $user, $otheruser) = $this->create_users($course, 3); + [$author, $user, $otheruser] = $this->create_users($course, 3); // Post in both of the moodleoverflows. - list(, $fp1) = $this->generator->post_to_forum($moodleoverflow, $author); - list(, $ofp1) = $this->generator->post_to_forum($othermoodleoverflow, $author); + [, $fp1] = $this->generator->post_to_forum($moodleoverflow, $author); + [, $ofp1] = $this->generator->post_to_forum($othermoodleoverflow, $author); // Add read information for those users. readtracking::moodleoverflow_add_read_record($user->id, $fp1->id); @@ -1222,7 +1231,7 @@ public function test_get_users_in_context_with_tracking_preferences(): void { $othermoodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', ['course' => $course->id]); $othercm = get_coursemodule_from_instance('moodleoverflow', $othermoodleoverflow->id); $othercontext = \context_module::instance($othercm->id); - list(, $user, $otheruser) = $this->create_users($course, 3); + [, $user, $otheruser] = $this->create_users($course, 3); // Stop tracking the read posts. readtracking::moodleoverflow_stop_tracking($moodleoverflow->id, $user->id); @@ -1263,8 +1272,8 @@ public function test_grades(): void { $cm = get_coursemodule_from_instance('moodleoverflow', $forum->id); $context = \context_module::instance($cm->id); - list($user, $user2) = $this->create_and_enrol_users($course, 2); - list( , $post) = $this->generator->post_to_forum($forum, $user); + [$user, $user2] = $this->create_and_enrol_users($course, 2); + [ , $post] = $this->generator->post_to_forum($forum, $user); ratings::moodleoverflow_add_rating($forum, $post->id, RATING_UPVOTE, $cm, $user2->id); moodleoverflow_update_all_grades_for_cm($forum->id); $grades = grade_get_grades($course->id, 'mod', 'moodleoverflow', $forum->id, [$user->id, $user2->id]); diff --git a/tests/ratings_test.php b/tests/ratings_test.php index 5c3682ce5ed..60d91c5f98c 100644 --- a/tests/ratings_test.php +++ b/tests/ratings_test.php @@ -366,7 +366,8 @@ private function set_votesdifference() { foreach ([$this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6] as $answer) { $answer->upvotes = 1; $answer->downvotes = 1; - $answer->votesdifference = $answer->upvotes - $answer->downvotes;; + $answer->votesdifference = $answer->upvotes - $answer->downvotes; + ; } } @@ -536,7 +537,7 @@ private function create_twogroups($group1, $group2) { * @param array $orderposts * @return void */ - private function process_groups(String $group1, string $group2, array $orderposts = []) { + private function process_groups(string $group1, string $group2, array $orderposts = []) { $this->create_twogroups($group1, $group2); $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]; @@ -553,7 +554,7 @@ private function process_groups(String $group1, string $group2, array $orderpost * @param object $answer */ private function set_group(string $group, object $answer) { - switch($group) { + switch ($group) { case 'sh': $answer->markedhelpful = 1; $answer->markedsolution = 1; diff --git a/tests/readtracking_test.php b/tests/readtracking_test.php index 4d78b9e89e7..91440f92984 100644 --- a/tests/readtracking_test.php +++ b/tests/readtracking_test.php @@ -39,7 +39,6 @@ * @covers \readtracking */ final class readtracking_test extends advanced_testcase { - /** * Test the logic in the moodleoverflow_can_track_moodleoverflows() function. */ diff --git a/tests/review_test.php b/tests/review_test.php index dce3b3f9ef8..be2e3811622 100644 --- a/tests/review_test.php +++ b/tests/review_test.php @@ -45,7 +45,6 @@ * @covers \mod_moodleoverflow_external::review_reject_post */ final class review_test extends \advanced_testcase { - /** @var \mod_moodleoverflow_generator $generator */ private $generator; /** @@ -280,8 +279,8 @@ private function assert_matches_properties($expected, $actual) { private function create_post($options) { $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); - list(, $teacherpost) = $this->generator->post_to_forum($moodleoverflow, $this->teacher); - list(, $studentpost) = $this->generator->post_to_forum($moodleoverflow, $this->student); + [, $teacherpost] = $this->generator->post_to_forum($moodleoverflow, $this->teacher); + [, $studentpost] = $this->generator->post_to_forum($moodleoverflow, $this->student); return ['teacherpost' => $teacherpost, 'studentpost' => $studentpost]; } @@ -299,20 +298,28 @@ 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, + $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, + $DB->get_record('moodleoverflow_posts', ['id' => $teacherpost->id]) + ); + $this->assert_matches_properties( + ['mailed' => MOODLEOVERFLOW_MAILED_PENDING, 'reviewed' => $review2, 'timereviewed' => null, ], - $DB->get_record('moodleoverflow_posts', ['id' => $studentpost->id])); + $DB->get_record('moodleoverflow_posts', ['id' => $studentpost->id]) + ); $this->run_send_notification_mails(); $this->run_send_review_mails(); - $this->assert_matches_properties(['mailed' => MOODLEOVERFLOW_MAILED_SUCCESS, + $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])); + $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 e5a695249c0..f44232a2f6d 100644 --- a/tests/subscriptions_test.php +++ b/tests/subscriptions_test.php @@ -39,7 +39,6 @@ * @covers \subscriptions */ final class subscriptions_test extends advanced_testcase { - /** * Test setUp. */ @@ -125,7 +124,7 @@ public function test_subscription_modes(): void { $modulecontext = \context_module::instance($moodleoverflow->cmid); // Create a user enrolled in the course as a student. - list ($user) = $this->helper_create_users($course, 1); + [$user] = $this->helper_create_users($course, 1); // Must be logged in as the current user. $this->setUser($user); @@ -133,8 +132,10 @@ public function test_subscription_modes(): void { // Test the forced subscription. subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_FORCESUBSCRIBE); $moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id]); - $this->assertEquals(MOODLEOVERFLOW_FORCESUBSCRIBE, - subscriptions::get_subscription_mode($moodleoverflow)); + $this->assertEquals( + MOODLEOVERFLOW_FORCESUBSCRIBE, + subscriptions::get_subscription_mode($moodleoverflow) + ); $this->assertTrue(subscriptions::is_forcesubscribed($moodleoverflow)); $this->assertFalse(subscriptions::is_subscribable($moodleoverflow, $modulecontext)); $this->assertFalse(subscriptions::subscription_disabled($moodleoverflow)); @@ -178,7 +179,7 @@ public function test_unsubscribable_moodleoverflows(): void { $modulecontext = \context_module::instance($cm->id); // Create a user enrolled in the course as a student. - list ($user) = $this->helper_create_users($course, 1); + [$user] = $this->helper_create_users($course, 1); // Must be logged in as the current user. $this->setUser($user); @@ -225,11 +226,11 @@ public function test_moodleoverflow_toggle_as_other(): void { $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $modulecontext = \context_module::instance($cm->id); - list ($author) = $this->helper_create_users($course, 1); + [$author] = $this->helper_create_users($course, 1); // Post a discussion to the moodleoverflow. $discussion = new \stdClass(); - list($discussion->id, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); + [$discussion->id, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); $discussion->moodleoverflow = $moodleoverflow->id; @@ -326,18 +327,22 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_unsub $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); // Create users enrolled in the course as students. - list($author) = $this->helper_create_users($course, 1); + [$author] = $this->helper_create_users($course, 1); // Check that the user is currently not subscribed to the moodleoverflow. $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow)); // Post a discussion to the moodleoverflow. - list($discussion, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); + [$discussion, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); // Check that the user is unsubscribed from the discussion too. - $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, - $moodleoverflow, $discussion)); + $this->assertFalse(subscriptions::is_subscribed( + $author->id, + $moodleoverflow, + $moodleoverflow, + $discussion + )); } /** @@ -357,12 +362,15 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc $modulecontext = \context_module::instance($cm->id); // Create users enrolled in the course as students. - list($author) = $this->helper_create_users($course, 1); + [$author] = $this->helper_create_users($course, 1); // Enrol the user in the moodleoverflow. // If a subscription was added, we get the record ID. - $this->assertIsInt(subscriptions::subscribe_user($author->id, - $moodleoverflow, $modulecontext)); + $this->assertIsInt(subscriptions::subscribe_user( + $author->id, + $moodleoverflow, + $modulecontext + )); // If we already have a subscription when subscribing the user, we get a boolean (true). $this->assertTrue(subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext)); @@ -371,12 +379,16 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // Post a discussion to the moodleoverflow. - list($discussion, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); + [$discussion, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); // Check that the user is subscribed to the discussion too. - $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion)); + $this->assertTrue(subscriptions::is_subscribed( + $author->id, + $moodleoverflow, + $modulecontext, + $discussion + )); } /** @@ -396,31 +408,41 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_unsub $modulecontext = \context_module::instance($cm->id); // Create a user enrolled in the course as a student. - list($author) = $this->helper_create_users($course, 1); + [$author] = $this->helper_create_users($course, 1); // Check that the user is currently not subscribed to the moodleoverflow. $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow)); // Post a discussion to the moodleoverflow. $discussion = new \stdClass(); - list($discussion->id, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); + [$discussion->id, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); $discussion->moodleoverflow = $moodleoverflow->id; // Attempting to unsubscribe from the discussion should not make a change. - $this->assertFalse(subscriptions::unsubscribe_user_from_discussion($author->id, - $discussion, $modulecontext)); + $this->assertFalse(subscriptions::unsubscribe_user_from_discussion( + $author->id, + $discussion, + $modulecontext + )); // Then subscribe them to the discussion. - $this->assertTrue(subscriptions::subscribe_user_to_discussion($author->id, - $discussion, $modulecontext)); + $this->assertTrue(subscriptions::subscribe_user_to_discussion( + $author->id, + $discussion, + $modulecontext + )); // Check that the user is still unsubscribed from the moodleoverflow. $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow)); // But subscribed to the discussion. - $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); + $this->assertTrue(subscriptions::is_subscribed( + $author->id, + $moodleoverflow, + $modulecontext, + $discussion->id + )); } /** @@ -440,7 +462,7 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc $modulecontext = \context_module::instance($cm->id); // Create two users enrolled in the course as students. - list($author) = $this->helper_create_users($course, 2); + [$author] = $this->helper_create_users($course, 2); // Enrol the student in the moodleoverflow. subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); @@ -450,7 +472,7 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc // Post a discussion to the moodleoverflow. $discussion = new \stdClass(); - list($discussion->id, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); + [$discussion->id, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); $discussion->moodleoverflow = $moodleoverflow->id; @@ -461,8 +483,12 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // But unsubscribed from the discussion. - $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext, - $discussion->id)); + $this->assertFalse(subscriptions::is_subscribed( + $author->id, + $moodleoverflow, + $modulecontext, + $discussion->id + )); } /** @@ -479,12 +505,12 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed( $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $modulecontext = \context_module::instance($cm->id); - list($author) = $this->helper_create_users($course, 2); + [$author] = $this->helper_create_users($course, 2); subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext); // Post a discussion to the moodleoverflow. $discussion = new \stdClass(); - list($discussion->id, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); + [$discussion->id, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); $discussion->moodleoverflow = $moodleoverflow->id; @@ -583,14 +609,14 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_unsubscribe $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $modulecontext = \context_module::instance($cm->id); - list($author) = $this->helper_create_users($course, 2); + [$author] = $this->helper_create_users($course, 2); // Check that the user is currently unsubscribed to the moodleoverflow. $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext)); // Post a discussion to the moodleoverflow. $discussion = new \stdClass(); - list($discussion->id, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); + [$discussion->id, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); $discussion->moodleoverflow = $moodleoverflow->id; @@ -746,7 +772,7 @@ public function test_fetch_subscribed_users_discussion_subscriptions(): void { // Create the discussion. $discussion = new \stdClass(); - list($discussion->id, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $users[0]); + [$discussion->id, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $users[0]); unset($post); $discussion->moodleoverflow = $moodleoverflow->id; @@ -824,8 +850,12 @@ public function test_force_subscribed_to_moodleoverflow(): void { // Check that the user is currently subscribed to the moodleoverflow. $this->assertTrue(subscriptions::is_subscribed($user->id, $moodleoverflow, $context)); - assign_capability('mod/moodleoverflow:allowforcesubscribe', CAP_PROHIBIT, $roleids['student'], - $context); + assign_capability( + 'mod/moodleoverflow:allowforcesubscribe', + CAP_PROHIBIT, + $roleids['student'], + $context + ); $context->mark_dirty(); $this->assertFalse(has_capability('mod/moodleoverflow:allowforcesubscribe', $context, $user->id)); } @@ -976,7 +1006,7 @@ public function test_discussion_subscription_cache_prefill(): void { $author = $users[0]; for ($i = 0; $i < 20; $i++) { $discussion = new \stdClass(); - list($discussion->id, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); + [$discussion->id, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); $discussion->moodleoverflow = $moodleoverflow->id; $discussions[] = $discussion; @@ -1039,7 +1069,7 @@ public function test_discussion_subscription_cache_fill(): void { $author = $users[0]; for ($i = 0; $i < 20; $i++) { $discussion = new \stdClass(); - list($discussion->id, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); + [$discussion->id, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $author); unset($post); $discussion->moodleoverflow = $moodleoverflow->id; $discussions[] = $discussion; @@ -1086,7 +1116,6 @@ public function test_discussion_subscription_cache_fill(): void { } else { $this->assertTrue(false, 'Unexpected amount of reads required to fill discussion subscription cache.'); } - } /** @@ -1105,11 +1134,11 @@ public function test_moodleoverflow_subscribe_toggle_as_other_repeat_subscriptio $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options); $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); $modulecontext = \context_module::instance($cm->id); - list($user) = $this->helper_create_users($course, 1); + [$user] = $this->helper_create_users($course, 1); // Post a discussion to the moodleoverflow. $discussion = new \stdClass(); - list($discussion->id, $post) = $this->helper_post_to_moodleoverflow($moodleoverflow, $user); + [$discussion->id, $post] = $this->helper_post_to_moodleoverflow($moodleoverflow, $user); unset($post); $discussion->moodleoverflow = $moodleoverflow->id; @@ -1263,7 +1292,9 @@ public function test_is_subscribable_loggedin($options, $expect): void { $this->getDataGenerator()->enrol_user($user->id, $course->id); $this->setUser($user); - $this->assertEquals($expect, subscriptions::is_subscribable($moodleoverflow, - \context_module::instance($moodleoverflow->cmid))); + $this->assertEquals($expect, subscriptions::is_subscribable( + $moodleoverflow, + \context_module::instance($moodleoverflow->cmid) + )); } } diff --git a/tests/userstats_test.php b/tests/userstats_test.php index 45a82819c95..41071946939 100644 --- a/tests/userstats_test.php +++ b/tests/userstats_test.php @@ -40,7 +40,6 @@ * @covers \userstats_table */ final class userstats_test extends \advanced_testcase { - /** @var \stdClass test course */ private $course; @@ -144,7 +143,6 @@ public function test_activity(): void { $data = $this->create_statstable(); $activity = $this->get_specific_userstats($data, $this->user1, 'forumactivity'); $this->assertEquals(5, $activity); - } /** * Test, if the reputation is calculated correctly. @@ -158,8 +156,10 @@ public function test_reputation(): void { $this->create_solution($this->teacher, $this->discussion1[1], $this->answer1); // Calculate the forum reputation of user2. - $reputation = \mod_moodleoverflow\ratings::moodleoverflow_get_reputation_instance($this->moodleoverflow->id, - $this->user2->id); + $reputation = \mod_moodleoverflow\ratings::moodleoverflow_get_reputation_instance( + $this->moodleoverflow->id, + $this->user2->id + ); // Create the user statistics table for this course and save it in $data. $data = $this->create_statstable(); $reputation2 = $this->get_specific_userstats($data, $this->user2, 'forumreputation'); diff --git a/tracking.php b/tracking.php index 4c5c5b22247..b5e0513d24c 100644 --- a/tracking.php +++ b/tracking.php @@ -90,12 +90,10 @@ // Redirect the user back to where he is coming from. redirect($returnpageurl, get_string('nownottracking', 'moodleoverflow', $info), 1); - } else { // The insertion failed. Print an error message. throw new moodle_exception('cannottrack', 'moodleoverflow', get_local_referer(false)); } - } else { // The moodleoverflow instance is not tracked. The next step is to track. @@ -109,7 +107,6 @@ // Redirect the user back to where he is coming from. redirect($returnto, get_string('nowtracking', 'moodleoverflow', $info), 1); - } else { // The deletion failed. Print an error message. throw new moodle_exception('cannottrack', 'moodleoverflow', get_local_referer(false)); diff --git a/userstats.php b/userstats.php index 1b5eecf1658..be4922b526e 100644 --- a/userstats.php +++ b/userstats.php @@ -26,7 +26,7 @@ */ // Include config and locallib. -require_once(__DIR__.'/../../config.php'); +require_once(__DIR__ . '/../../config.php'); global $CFG, $PAGE, $DB, $OUTPUT, $SESSION; require_once($CFG->dirroot . '/mod/moodleoverflow/locallib.php'); @@ -64,10 +64,9 @@ // Output starts here. echo $OUTPUT->header(); echo $OUTPUT->heading(''); - $table = new userstats_table('statisticstable' , $course->id, $moodleoverflow->id, $PAGE->url); + $table = new userstats_table('statisticstable', $course->id, $moodleoverflow->id, $PAGE->url); $table->out(); echo $OUTPUT->footer(); } else { redirect(new moodle_url('/')); } - diff --git a/view.php b/view.php index 52b55a157a4..2b0aa5c4195 100644 --- a/view.php +++ b/view.php @@ -26,9 +26,9 @@ */ // Include config and locallib. -require_once(__DIR__.'/../../config.php'); +require_once(__DIR__ . '/../../config.php'); global $CFG, $PAGE, $DB, $OUTPUT, $SESSION, $USER; -require_once($CFG->dirroot.'/mod/moodleoverflow/locallib.php'); +require_once($CFG->dirroot . '/mod/moodleoverflow/locallib.php'); // Declare optional parameters. $id = optional_param('id', 0, PARAM_INT); // Course Module ID. @@ -129,8 +129,11 @@ $reviewpost = \mod_moodleoverflow\review::get_first_review_post($moodleoverflow->id); if ($reviewpost) { - echo html_writer::link($reviewpost, get_string('review_needed', 'mod_moodleoverflow'), - ['class' => 'btn btn-danger my-2']); + echo html_writer::link( + $reviewpost, + get_string('review_needed', 'mod_moodleoverflow'), + ['class' => 'btn btn-danger my-2'] + ); } } From b31ed914b062754bf79c0a3b80bbb0483771f9ce Mon Sep 17 00:00:00 2001 From: tamaro Date: Wed, 17 Sep 2025 17:15:27 +0200 Subject: [PATCH 2/3] fix typo --- tests/ratings_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ratings_test.php b/tests/ratings_test.php index 60d91c5f98c..dc93fad9fa4 100644 --- a/tests/ratings_test.php +++ b/tests/ratings_test.php @@ -532,7 +532,7 @@ private function create_twogroups($group1, $group2) { /** * Executing the sort function and comparing the sorted post to the expected order. - * @param String $group1 + * @param string $group1 * @param string $group2 * @param array $orderposts * @return void From 04197ad6a15e78f59c449226292704200ebf24a2 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 17 Sep 2025 15:15:45 +0000 Subject: [PATCH 3/3] Sync with Learnweb's centralized pull request template --- .github/PULL_REQUEST_TEMPLATE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 81ba4368ddf..cabc576d79c 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,4 +1,4 @@ -> **Note:** Please fill out all required sections and remove irrelevant ones. +> **Note:** Please fill out all relevant sections and remove irrelevant ones. ### 🔀 Purpose of this PR: - [ ] Fixes a bug