+ * @copyright 2023 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-// TODO refactor this. For more readability, and to avoid security issues.
-
-// Include config and locallib.
-use mod_moodleoverflow\anonymous;
+use mod_moodleoverflow\post\post_control;
use mod_moodleoverflow\review;
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
-global $CFG, $USER, $DB, $PAGE, $SESSION, $OUTPUT;
require_once(dirname(__FILE__) . '/locallib.php');
+
+global $CFG, $USER, $DB, $PAGE, $SESSION, $OUTPUT;
require_once($CFG->libdir . '/completionlib.php');
-// Declare optional parameters.
+// Declare optional url parameters.
$moodleoverflow = optional_param('moodleoverflow', 0, PARAM_INT);
$reply = optional_param('reply', 0, PARAM_INT);
$edit = optional_param('edit', 0, PARAM_INT);
$delete = optional_param('delete', 0, PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_INT);
-$count = 0;
-$count += $moodleoverflow ? 1 : 0;
-$count += $reply ? 1 : 0;
-$count += $edit ? 1 : 0;
-$count += $delete ? 1 : 0;
-
-if ($count !== 1) {
- throw new coding_exception('Exactly one parameter should be specified!');
-}
-
// Set the URL that should be used to return to this page.
-$PAGE->set_url('/mod/moodleoverflow/post.php', [
- 'moodleoverflow' => $moodleoverflow,
- 'reply' => $reply,
- 'edit' => $edit,
- 'delete' => $delete,
- 'confirm' => $confirm,
-]);
+$PAGE->set_url('/mod/moodleoverflow/post.php', ['moodleoverflow' => $moodleoverflow, 'reply' => $reply, 'edit' => $edit,
+ 'delete' => $delete, 'confirm' => $confirm, ]);
// These params will be passed as hidden variables later in the form.
$pageparams = ['moodleoverflow' => $moodleoverflow, 'reply' => $reply, 'edit' => $edit];
@@ -65,739 +48,105 @@
// Get the system context instance.
$systemcontext = context_system::instance();
-// Catch guests.
-if (!isloggedin() || isguestuser()) {
-
- // The user is starting a new discussion in a moodleoverflow instance.
- if (!empty($moodleoverflow)) {
-
- // Check the moodleoverflow instance is valid.
- if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow])) {
- throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
- }
-
- // The user is replying to an existing moodleoverflow discussion.
- } else if (!empty($reply)) {
+// Create a post_control object to control and lead the process.
+$postcontrol = new post_control();
- // Check if the related post exists.
- if (!$parent = moodleoverflow_get_post_full($reply)) {
- throw new moodle_exception('invalidparentpostid', 'moodleoverflow');
- }
-
- // Check if the post is part of a valid discussion.
- if (!$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $parent->discussion])) {
- throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
- }
+// Put all interaction parameters in one object for the post_control.
+$urlparameter = new \stdClass();
+$urlparameter->create = $moodleoverflow;
+$urlparameter->reply = $reply;
+$urlparameter->edit = $edit;
+$urlparameter->delete = $delete;
- // Check if the post is related to a valid moodleoverflow instance.
- if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow])) {
- throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
- }
- }
-
- // Get the related course.
- if (!$course = $DB->get_record('course', ['id' => $moodleoverflow->course])) {
- throw new moodle_exception('invalidcourseid');
- }
-
- // Get the related coursemodule and its context.
- if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id)) {
- throw new moodle_exception('invalidcoursemodule');
- }
-
- // Get the context of the module.
- $modulecontext = context_module::instance($cm->id);
-
- // Set parameters for the page.
- $PAGE->set_cm($cm, $course, $moodleoverflow);
- $PAGE->set_context($modulecontext);
- $PAGE->set_title($course->shortname);
- $PAGE->set_heading($course->fullname);
-
- // The page should not be large, only pages containing broad tables are usually.
- $PAGE->add_body_class('limitedwidth');
+// Catch guests.
+if (!isloggedin() || isguestuser()) {
+ // Gather information and set the page right so that user can be redirected to the right site.
+ $information = $postcontrol->catch_guest();
// The guest needs to login.
- echo $OUTPUT->header();
$strlogin = get_string('noguestpost', 'forum') . '
' . get_string('liketologin');
- echo $OUTPUT->confirm($strlogin, get_login_url(), $CFG->wwwroot . '/mod/moodleoverflow/view.php?m=' . $moodleoverflow->id);
+ echo $OUTPUT->header();
+ echo $OUTPUT->confirm($strlogin, get_login_url(),
+ $CFG->wwwroot . '/mod/moodleoverflow/view.php?m= ' . $information->moodleoverflow->id);
echo $OUTPUT->footer();
exit;
}
-// First step: A general login is needed to post something.
+// Require a general login to post something.
+// TODO: should course or id really be zero?.
require_login(0, false);
-// First possibility: User is starting a new discussion in a moodleoverflow instance.
-if (!empty($moodleoverflow)) {
-
- // Check the moodleoverflow instance is valid.
- if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow])) {
- throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
- }
-
- // Get the related course.
- if (!$course = $DB->get_record('course', ['id' => $moodleoverflow->course])) {
- throw new moodle_exception('invalidcourseid');
- }
-
- // Get the related coursemodule.
- if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id)) {
- throw new moodle_exception('invalidcoursemodule');
- }
-
- // Retrieve the contexts.
- $modulecontext = context_module::instance($cm->id);
- $coursecontext = context_course::instance($course->id);
-
- // Check if the user can start a new discussion.
- if (!moodleoverflow_user_can_post_discussion($moodleoverflow, $cm, $modulecontext)) {
-
- // Catch unenrolled user.
- if (!isguestuser() && !is_enrolled($coursecontext)) {
- if (enrol_selfenrol_available($course->id)) {
- $SESSION->wantsurl = qualified_me();
- $SESSION->enrolcancel = get_local_referer(false);
- redirect(new moodle_url('/enrol/index.php', [
- 'id' => $course->id,
- 'returnurl' => '/mod/moodleoverflow/view.php?m=' . $moodleoverflow->id,
- ]), get_string('youneedtoenrol'));
- }
- }
-
- // Notify the user, that he can not post a new discussion.
- throw new moodle_exception('nopostmoodleoverflow', 'moodleoverflow');
- }
-
- // Where is the user coming from?
- $SESSION->fromurl = get_local_referer(false);
-
- // Load all the $post variables.
- $post = new stdClass();
- $post->course = $course->id;
- $post->moodleoverflow = $moodleoverflow->id;
- $post->discussion = 0;
- $post->parent = 0;
- $post->subject = '';
- $post->userid = $USER->id;
- $post->message = '';
-
- // Unset where the user is coming from.
- // Allows to calculate the correct return url later.
- unset($SESSION->fromdiscussion);
-
-} else if (!empty($reply)) {
- // Second possibility: The user is writing a new reply.
-
- // Check if the post exists.
- if (!$parent = moodleoverflow_get_post_full($reply)) {
- throw new moodle_exception('invalidparentpostid', 'moodleoverflow');
- }
-
- // Check if the post is part of a discussion.
- if (!$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $parent->discussion])) {
- throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
- }
-
- // Check if the discussion is part of a moodleoverflow instance.
- if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow])) {
- throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
- }
-
- // Check if the moodleoverflow instance is part of a course.
- if (!$course = $DB->get_record('course', ['id' => $discussion->course])) {
- throw new moodle_exception('invalidcourseid');
- }
-
- // Retrieve the related coursemodule.
- if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id)) {
- throw new moodle_exception('invalidcoursemodule');
- }
-
- // Ensure the coursemodule is set correctly.
- $PAGE->set_cm($cm, $course, $moodleoverflow);
-
- // Retrieve the other contexts.
- $modulecontext = context_module::instance($cm->id);
- $coursecontext = context_course::instance($course->id);
-
- // Check whether the user is allowed to post.
- if (!moodleoverflow_user_can_post($modulecontext, $parent)) {
-
- // Give the user the chance to enroll himself to the course.
- if (!isguestuser() && !is_enrolled($coursecontext)) {
- $SESSION->wantsurl = qualified_me();
- $SESSION->enrolcancel = get_local_referer(false);
- redirect(new moodle_url('/enrol/index.php',
- ['id' => $course->id, 'returnurl' => '/mod/moodleoverflow/view.php?m=' . $moodleoverflow->id]),
- get_string('youneedtoenrol'));
- }
-
- // Print the error message.
- throw new moodle_exception('nopostmoodleoverflow', 'moodleoverflow');
- }
-
- // Make sure the user can post here.
- if (!$cm->visible && !has_capability('moodle/course:viewhiddenactivities', $modulecontext)) {
- throw new moodle_exception('activityiscurrentlyhidden');
- }
-
- // Load the $post variable.
- $post = new stdClass();
- $post->course = $course->id;
- $post->moodleoverflow = $moodleoverflow->id;
- $post->discussion = $parent->discussion;
- $post->parent = $parent->id;
- $post->subject = $discussion->name;
- $post->userid = $USER->id;
- $post->message = '';
-
- // Append 'RE: ' to the discussions subject.
- $strre = get_string('re', 'moodleoverflow');
- if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
- $post->subject = $strre . ' ' . $post->subject;
- }
-
- // Unset where the user is coming from.
- // Allows to calculate the correct return url later.
- unset($SESSION->fromdiscussion);
-
-
-} else if (!empty($edit)) {
- // Third possibility: The user is editing his own post.
-
- // Check if the submitted post exists.
- if (!$post = moodleoverflow_get_post_full($edit)) {
- throw new moodle_exception('invalidpostid', 'moodleoverflow');
- }
-
- // Get the parent post of this post if it is not the starting post of the discussion.
- if ($post->parent) {
- if (!$parent = moodleoverflow_get_post_full($post->parent)) {
- throw new moodle_exception('invalidparentpostid', 'moodleoverflow');
- }
- }
-
- // Check if the post refers to a valid discussion.
- if (!$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $post->discussion])) {
- throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
- }
-
- // Check if the post refers to a valid moodleoverflow instance.
- if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow])) {
- throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
- }
-
- // Check if the post refers to a valid course.
- if (!$course = $DB->get_record('course', ['id' => $discussion->course])) {
- throw new moodle_exception('invalidcourseid');
- }
-
- // Retrieve the related coursemodule.
- if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id)) {
- throw new moodle_exception('invalidcoursemodule');
- } else {
- $modulecontext = context_module::instance($cm->id);
- }
-
- // Set the pages context.
- $PAGE->set_cm($cm, $course, $moodleoverflow);
-
- // Check if the post can be edited.
- $beyondtime = ((time() - $post->created) > get_config('moodleoverflow', 'maxeditingtime'));
- $alreadyreviewed = review::should_post_be_reviewed($post, $moodleoverflow) && $post->reviewed;
- if (($beyondtime || $alreadyreviewed) && !has_capability('mod/moodleoverflow:editanypost', $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 ($post->userid <> $USER->id) {
-
- // Check if the current user has not the capability to edit any post.
- if (!has_capability('mod/moodleoverflow:editanypost', $modulecontext)) {
-
- // Display the error. Capabilities are missing.
- throw new moodle_exception('cannoteditposts', 'moodleoverflow');
- }
- }
-
- // Load the $post variable.
- $post->edit = $edit;
- $post->course = $course->id;
- $post->moodleoverflow = $moodleoverflow->id;
-
- // Unset where the user is coming from.
- // Allows to calculate the correct return url later.
- unset($SESSION->fromdiscussion);
-
-} else if (!empty($delete)) {
- // Fourth possibility: The user is deleting a post.
- // Check if the post is existing.
- if (!$post = moodleoverflow_get_post_full($delete)) {
- throw new moodle_exception('invalidpostid', 'moodleoverflow');
- }
-
- // Get the related discussion.
- if (!$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $post->discussion])) {
- throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
- }
-
- // Get the related moodleoverflow instance.
- if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow])) {
- throw new moodle_exception('invalidmoodleoverflowid', 'moodleoveflow');
- }
-
- // Get the related coursemodule.
- if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $moodleoverflow->course)) {
- throw new moodle_exception('invalidcoursemodule');
- }
-
- // Get the related course.
- if (!$course = $DB->get_record('course', ['id' => $moodleoverflow->course])) {
- throw new moodle_exception('invalidcourseid');
- }
-
- // Require a login and retrieve the modulecontext.
- require_login($course, false, $cm);
- $modulecontext = context_module::instance($cm->id);
-
- // Check some capabilities.
- $deleteownpost = has_capability('mod/moodleoverflow:deleteownpost', $modulecontext);
- $deleteanypost = has_capability('mod/moodleoverflow:deleteanypost', $modulecontext);
- if (!(($post->userid == $USER->id && $deleteownpost) || $deleteanypost)) {
- throw new moodle_exception('cannotdeletepost', 'moodleoverflow');
- }
-
- // Count all replies of this post.
- $replycount = moodleoverflow_count_replies($post, false);
+// Now the post_control checks which interaction is wanted and builds a prepost.
+$postcontrol->detect_interaction($urlparameter);
+// If a post is being deleted, delete it immediately.
+if ($postcontrol->get_interaction() == 'delete') {
// Has the user confirmed the deletion?
if (!empty($confirm) && confirm_sesskey()) {
-
- // Check if the user has the capability to delete the post.
- $timepassed = time() - $post->created;
- if (($timepassed > get_config('moodleoverflow', 'maxeditingtime')) && !$deleteanypost) {
- $url = new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $post->discussion]);
- throw new moodle_exception('cannotdeletepost', 'moodleoverflow', moodleoverflow_go_back_to($url));
- }
-
- // A normal user cannot delete his post if there are direct replies.
- if ($replycount && !$deleteanypost) {
- $url = new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $post->discussion]);
- throw new moodle_exception('couldnotdeletereplies', 'moodleoverflow', moodleoverflow_go_back_to($url));
- } else {
- // Delete the post.
-
- // The post is the starting post of a discussion. Delete the topic as well.
- if (!$post->parent) {
- moodleoverflow_delete_discussion($discussion, $course, $cm, $moodleoverflow);
-
- // Trigger the discussion deleted event.
- $params = [
- 'objectid' => $discussion->id,
- 'context' => $modulecontext,
- ];
-
- $event = \mod_moodleoverflow\event\discussion_deleted::create($params);
- $event->trigger();
-
- // Redirect the user back to start page of the moodleoverflow instance.
- redirect("view.php?m=$discussion->moodleoverflow");
- exit;
-
- } else if (moodleoverflow_delete_post($post, $deleteanypost, $cm, $moodleoverflow)) {
- // Delete a single post.
- // Redirect back to the discussion.
- $discussionurl = new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $discussion->id]);
- redirect(moodleoverflow_go_back_to($discussionurl));
- exit;
-
- } else {
- // Something went wrong.
- throw new moodle_exception('errorwhiledelete', 'moodleoverflow');
- }
- }
+ $postcontrol->execute_delete();
} else {
// Deletion needs to be confirmed.
-
- moodleoverflow_set_return();
- $PAGE->navbar->add(get_string('delete', 'moodleoverflow'));
- $PAGE->set_title($course->shortname);
- $PAGE->set_heading($course->fullname);
-
- // The page should not be large, only pages containing broad tables are usually.
- $PAGE->add_body_class('limitedwidth');
-
- // Check if there are replies for the post.
- if ($replycount) {
-
- // Check if the user has capabilities to delete more than one post.
- if (!$deleteanypost) {
- throw new moodle_exception('couldnotdeletereplies', 'moodleoverflow',
- moodleoverflow_go_back_to(new moodle_url('/mod/moodleoverflow/discussion.php',
- ['d' => $post->discussion, 'p' . $post->id])));
- }
-
- // Request a confirmation to delete the post.
- echo $OUTPUT->header();
- echo $OUTPUT->confirm(get_string("deletesureplural", "moodleoverflow", $replycount + 1),
- "post.php?delete=$delete&confirm=$delete", $CFG->wwwroot . '/mod/moodleoverflow/discussion.php?d=' .
- $post->discussion . '#p' . $post->id);
-
+ $postcontrol->confirm_delete();
+
+ // Display a confirmation request depending on the number of posts that are being deleted.
+ $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());
} else {
- // Delete a single post.
-
- // Print a confirmation message.
- echo $OUTPUT->header();
- echo $OUTPUT->confirm(get_string("deletesure", "moodleoverflow", $replycount),
+ echo $OUTPUT->confirm(get_string('deletesure', 'moodleoverflow', $information->replycount),
"post.php?delete=$delete&confirm=$delete",
- $CFG->wwwroot . '/mod/moodleoverflow/discussion.php?d=' . $post->discussion . '#p' . $post->id);
+ $CFG->wwwroot . '/mod/moodleoverflow/discussion.php?d=' . $information->discussion->get_id() .
+ '#p' . $information->relatedpost->get_id());
}
+ echo $OUTPUT->footer();
}
- echo $OUTPUT->footer();
exit;
-
-} else {
- // Last posibility: the action is not known.
-
- throw new moodle_exception('unknownaction');
}
-// Second step: The user must be logged on properly. Must be enrolled to the course as well.
-require_login($course, false, $cm);
+// A post will be created or edited. For that the post_control builds a post_form.
+$mformpost = $postcontrol->build_postform($pageparams);
-// Get the contexts.
-$modulecontext = context_module::instance($cm->id);
-$coursecontext = context_course::instance($course->id);
-
-// Get the subject.
-if ($edit) {
- $subject = $discussion->name;
-} else if ($reply) {
- $subject = $post->subject;
-} else if ($moodleoverflow) {
- $subject = $post->subject;
-}
+// The User now entered information in the form. The post.php now needs to process the information and call the right function.
-// Get attachments.
-$draftitemid = file_get_submitted_draft_itemid('attachments');
-file_prepare_draft_area($draftitemid,
- $modulecontext->id,
- 'mod_moodleoverflow',
- 'attachment',
- empty($post->id) ? null : $post->id,
- mod_moodleoverflow_post_form::attachment_options($moodleoverflow));
+// Get attributes from the postcontrol.
+$information = $postcontrol->get_information();
+$prepost = $postcontrol->get_prepost();
-if ($draftitemid && $edit && anonymous::is_post_anonymous($discussion, $moodleoverflow, $post->userid)
- && $post->userid != $USER->id) {
-
- $usercontext = context_user::instance($USER->id);
- $anonymousstr = get_string('anonymous', 'moodleoverflow');
- foreach (get_file_storage()->get_area_files($usercontext->id, 'user', 'draft', $draftitemid) as $file) {
- $file->set_author($anonymousstr);
- }
-}
-
-// Prepare the form.
-$formarray = [
- 'course' => $course,
- 'cm' => $cm,
- 'coursecontext' => $coursecontext,
- 'modulecontext' => $modulecontext,
- 'moodleoverflow' => $moodleoverflow,
- 'post' => $post,
- 'edit' => $edit,
-];
-$mformpost = new mod_moodleoverflow_post_form('post.php', $formarray, 'post', '', ['id' => 'mformmoodleoverflow']);
-
-// The current user is not the original author.
-// Append the message to the end of the message.
-if ($USER->id != $post->userid) {
-
- // Create a temporary object.
- $data = new stdClass();
- $data->date = userdate($post->modified);
- $post->messageformat = editors_get_preferred_format();
-
- // Append the message depending on the messages format.
- if ($post->messageformat == FORMAT_HTML) {
- $data->name = '' . fullname($USER) . '';
- $post->message .= '(' . get_string('editedby', 'moodleoverflow', $data) . ')
';
- } else {
- $data->name = fullname($USER);
- $post->message .= "\n\n(" . get_string('editedby', 'moodleoverflow', $data) . ')';
- }
-
- // Delete the temporary object.
- unset($data);
-}
-
-// Define the heading for the form.
-$formheading = '';
-if (!empty($parent)) {
- $heading = get_string('yourreply', 'moodleoverflow');
- $formheading = get_string('reply', 'moodleoverflow');
-} else {
- $heading = get_string('yournewtopic', 'moodleoverflow');
-}
-
-// Get the original post.
-$postid = empty($post->id) ? null : $post->id;
-$postmessage = empty($post->message) ? null : $post->message;
-
-// Set data for the form.
-// TODO Refactor.
-$param1 = (isset($discussion->id) ? [$discussion->id] : []);
-$param2 = (isset($post->format) ? ['format' => $post->format] : []);
-$param3 = (isset($discussion->timestart) ? ['timestart' => $discussion->timestart] : []);
-$param4 = (isset($discussion->timeend) ? ['timeend' => $discussion->timeend] : []);
-$param5 = (isset($discussion->id) ? ['discussion' => $discussion->id] : []);
-$mformpost->set_data([
- 'attachments' => $draftitemid,
- 'general' => $heading,
- 'subject' => $subject,
- 'message' => [
- 'text' => $postmessage,
- 'format' => editors_get_preferred_format(),
- 'itemid' => $postid,
- ],
- 'userid' => $post->userid,
- 'parent' => $post->parent,
- 'discussion' => $post->discussion,
- 'course' => $course->id,
- ] + $pageparams + $param1 + $param2 + $param3 + $param4 + $param5);
-
-// Is it canceled?
+// If the interaction was cancelled, the user needs to be redirected.
if ($mformpost->is_cancelled()) {
-
- // Redirect the user back.
- if (!isset($discussion->id)) {
- redirect(new moodle_url('/mod/moodleoverflow/view.php', ['m' => $moodleoverflow->id]));
+ if (!isset($prepost->discussionid)) {
+ redirect(new moodle_url('/mod/moodleoverflow/view.php', ['m' => $prepost->moodleoverflowid]));
} else {
- redirect(new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $discussion->id]));
+ redirect(new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $prepost->discussionid]));
}
-
- // Cancel.
- exit();
+ exit;
}
-// Is it submitted?
+// If the post_form is submitted, the post_control executes the right function.
if ($fromform = $mformpost->get_data()) {
-
- // Redirect url in case of occuring errors.
- if (empty($SESSION->fromurl)) {
- $errordestination = "$CFG->wwwroot/mod/moodleoverflow/view.php?m=$moodleoverflow->id";
- } else {
- $errordestination = $SESSION->fromurl;
- }
-
- // Format the submitted data.
- $fromform->messageformat = $fromform->message['format'];
- $fromform->message = $fromform->message['text'];
- $fromform->messagetrust = trusttext_trusted($modulecontext);
-
- // If we are updating a post.
- if ($fromform->edit) {
-
- // Initiate some variables.
- unset($fromform->groupid);
- $fromform->id = $fromform->edit;
- $message = '';
-
- // The FORUM-Plugin had an bug: https://tracker.moodle.org/browse/MDL-4314
- // This is a fix for it.
- if (!$realpost = $DB->get_record('moodleoverflow_posts', ['id' => $fromform->id])) {
- $realpost = new stdClass();
- $realpost->userid = -1;
- }
-
- // Check the capabilities of the user.
- // He may proceed if he can edit any post or if he has the startnewdiscussion
- // capability or the capability to reply and is editing his own post.
- $editanypost = has_capability('mod/moodleoverflow:editanypost', $modulecontext);
- $replypost = has_capability('mod/moodleoverflow:replypost', $modulecontext);
- $startdiscussion = has_capability('mod/moodleoverflow:startdiscussion', $modulecontext);
- $ownpost = ($realpost->userid == $USER->id);
- if (!(($ownpost && ($replypost || $startdiscussion)) || $editanypost)) {
- throw new moodle_exception('cannotupdatepost', 'moodleoverflow');
- }
-
- // Update the post or print an error message.
- $updatepost = $fromform;
- $updatepost->moodleoverflow = $moodleoverflow->id;
- if (!moodleoverflow_update_post($updatepost, $mformpost)) {
- throw new moodle_exception('couldnotupdate', 'moodleoverflow', $errordestination);
- }
-
- // Create a success-message.
- if ($realpost->userid == $USER->id) {
- $message .= get_string('postupdated', 'moodleoverflow');
- } else {
- if (anonymous::is_post_anonymous($discussion, $moodleoverflow, $realpost->userid)) {
- $name = get_string('anonymous', 'moodleoverflow');
- } else {
- $realuser = $DB->get_record('user', ['id' => $realpost->userid]);
- $name = fullname($realuser);
- }
- $message .= get_string('editedpostupdated', 'moodleoverflow', $name);
- }
-
- // Create a link to go back to the discussion.
- $discussionurl = new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $discussion->id], 'p' . $fromform->id);
-
- // Set some parameters.
- $params = [
- 'context' => $modulecontext,
- 'objectid' => $fromform->id,
- 'other' => [
- 'discussionid' => $discussion->id,
- 'moodleoverflowid' => $moodleoverflow->id,
- ], ];
-
- // If the editing user is not the original author, add the original author to the params.
- if ($realpost->userid != $USER->id) {
- $params['relateduserid'] = $realpost->userid;
- }
-
- // Trigger post updated event.
- $event = \mod_moodleoverflow\event\post_updated::create($params);
- $event->trigger();
-
- // Redirect back to the discussion.
- redirect(moodleoverflow_go_back_to($discussionurl), $message, null, \core\output\notification::NOTIFY_SUCCESS);
-
- // Cancel.
- exit;
-
- } else if ($fromform->discussion) {
- // Add a new post to an existing discussion.
-
- // Set some basic variables.
- unset($fromform->groupid);
- $message = '';
- $addpost = $fromform;
- $addpost->moodleoverflow = $moodleoverflow->id;
-
- // Create the new post.
- if ($fromform->id = moodleoverflow_add_new_post($addpost)) {
-
- // Subscribe to this thread.
- $discussion = new \stdClass();
- $discussion->id = $fromform->discussion;
- $discussion->moodleoverflow = $moodleoverflow->id;
- \mod_moodleoverflow\subscriptions::moodleoverflow_post_subscription($fromform,
- $moodleoverflow, $discussion, $modulecontext);
-
- // Print a success-message.
- $message .= '' . get_string("postaddedsuccess", "moodleoverflow") . '
';
- $message .= '' . get_string("postaddedtimeleft", "moodleoverflow",
- format_time(get_config('moodleoverflow', 'maxeditingtime'))) . '
';
-
- // Set the URL that links back to the discussion.
- $link = '/mod/moodleoverflow/discussion.php';
- $discussionurl = new moodle_url($link, ['d' => $discussion->id], 'p' . $fromform->id);
-
- // Trigger post created event.
- $params = [
- 'context' => $modulecontext,
- 'objectid' => $fromform->id,
- 'other' => [
- 'discussionid' => $discussion->id,
- 'moodleoverflowid' => $moodleoverflow->id,
- ], ];
- $event = \mod_moodleoverflow\event\post_created::create($params);
- $event->trigger();
- redirect(
- moodleoverflow_go_back_to($discussionurl),
- $message,
- \core\output\notification::NOTIFY_SUCCESS
- );
-
- // Print an error if the answer could not be added.
- } else {
- throw new moodle_exception('couldnotadd', 'moodleoverflow', $errordestination);
- }
-
- // The post has been added.
- exit;
-
- } else {
- // Add a new discussion.
-
- // The location to redirect the user after successfully posting.
- $redirectto = new moodle_url('view.php', ['m' => $fromform->moodleoverflow]);
-
- $discussion = $fromform;
- $discussion->name = $fromform->subject;
-
- // Check if the user is allowed to post here.
- if (!moodleoverflow_user_can_post_discussion($moodleoverflow)) {
- throw new moodle_exception('cannotcreatediscussion', 'moodleoverflow');
- }
-
- // Check if the creation of the new discussion failed.
- if (!$discussion->id = moodleoverflow_add_discussion($discussion, $modulecontext)) {
-
- throw new moodle_exception('couldnotadd', 'moodleoverflow', $errordestination);
-
- } else { // The creation of the new discussion was successful.
-
- $params = [
- 'context' => $modulecontext,
- 'objectid' => $discussion->id,
- 'other' => [
- 'moodleoverflowid' => $moodleoverflow->id,
- ],
- ];
-
- $message = '' . get_string("postaddedsuccess", "moodleoverflow") . '
';
-
- // Trigger the discussion created event.
- $params = [
- 'context' => $modulecontext,
- 'objectid' => $discussion->id,
- ];
- $event = \mod_moodleoverflow\event\discussion_created::create($params);
- $event->trigger();
- // Subscribe to this thread.
- $discussion->moodleoverflow = $moodleoverflow->id;
- \mod_moodleoverflow\subscriptions::moodleoverflow_post_subscription($fromform,
- $moodleoverflow, $discussion, $modulecontext);
- }
-
- // Redirect back to te discussion.
- redirect(moodleoverflow_go_back_to($redirectto->out()), $message, null, \core\output\notification::NOTIFY_SUCCESS);
-
- // Do not continue.
- exit;
- }
+ $postcontrol->execute_interaction($fromform);
+ exit;
}
// If the script gets to this point, nothing has been submitted.
-// We have to display the form.
-// $course and $moodleoverflow are defined.
-// $discussion is only used for replying and editing.
+// The post_form will be displayed.
// Define the message to be displayed above the form.
-$toppost = new stdClass();
-$toppost->subject = get_string("addanewdiscussion", "moodleoverflow");
+$toppost = new \stdClass();
+$toppost->subject = get_string('addanewdiscussion', 'moodleoverflow');
// Initiate the page.
-$PAGE->set_title("$course->shortname: $moodleoverflow->name " . format_string($toppost->subject));
-$PAGE->set_heading($course->fullname);
-
-// The page should not be large, only pages containing broad tables are usually.
+$PAGE->set_title($information->course->shortname . ': ' .
+ $information->moodleoverflow->name . ' ' .
+ format_string($toppost->subject));
+$PAGE->set_heading($information->course->fullname);
$PAGE->add_body_class('limitedwidth');
-// Display the header.
+// Display all.
echo $OUTPUT->header();
-
-// Display the form.
$mformpost->display();
-
-// Display the footer.
echo $OUTPUT->footer();
diff --git a/tests/discussion_test.php b/tests/discussion_test.php
new file mode 100644
index 0000000000..0a536ebfcb
--- /dev/null
+++ b/tests/discussion_test.php
@@ -0,0 +1,162 @@
+.
+
+/**
+ * PHP Unit Tests for the Discussion class.
+ * @package mod_moodleoverflow
+ * @copyright 2023 Tamaro Walter
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_moodleoverflow;
+
+use mod_moodleoverflow\post\post;
+use mod_moodleoverflow\discussion\discussion;
+
+/**
+ * Tests if the functions from the discussion class are working correctly.
+ * As the discussion class works as an administrator of the post class, most of the testcases are already realized in the
+ * post_test.php file.
+ * @package mod_moodleoverflow
+ * @copyright 2023 Tamaro Walter
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @covers \mod_moodleoverflow\discussion\discussion
+ */
+class discussion_test extends \advanced_testcase {
+
+ /** @var \stdClass test course */
+ private $course;
+
+ /** @var \stdClass coursemodule */
+ private $coursemodule;
+
+ /** @var \stdClass modulecontext */
+ private $modulecontext;
+
+ /** @var \stdClass test moodleoverflow */
+ private $moodleoverflow;
+
+ /** @var \stdClass test teacher */
+ private $teacher;
+
+ /** @var discussion a discussion */
+ private $discussion;
+
+ /** @var post the post from the discussion */
+ private $post;
+
+ /** @var \mod_moodleoverflow_generator $generator */
+ private $generator;
+
+ public function setUp(): void {
+ $this->resetAfterTest();
+ $this->helper_course_set_up();
+ }
+
+ public function tearDown(): void {
+ // Clear all caches.
+ subscriptions::reset_moodleoverflow_cache();
+ subscriptions::reset_discussion_cache();
+ }
+
+ /**
+ * Test, if a discussion is being created correctly
+ */
+ public function test_create_discussion(): void {
+ global $DB;
+
+ // Build a prepost object with important information.
+ $time = time();
+ $prepost = new \stdClass();
+ $prepost->userid = $this->teacher->id;
+ $prepost->timenow = $time;
+ $prepost->message = 'a message';
+ $prepost->messageformat = 1;
+ $prepost->reviewed = 0;
+ $prepost->formattachments = '';
+ $prepost->modulecontext = $this->modulecontext;
+
+ // 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);
+ $discussionid = $discussion->moodleoverflow_add_discussion($prepost);
+ $posts = $discussion->moodleoverflow_get_discussion_posts();
+ $post = $posts[$discussion->get_firstpostid()];
+
+ // The discussion and the firstpost should be in the DB.
+ $dbdiscussion = $DB->get_record('moodleoverflow_discussions', ['id' => $discussion->get_id()]);
+ $this->assertEquals($dbdiscussion->id, $discussionid);
+ $this->assertEquals('Discussion Topic', $dbdiscussion->name);
+
+ $dbpost = $DB->get_record('moodleoverflow_posts', ['id' => $discussion->get_firstpostid()]);
+ $this->assertEquals($dbpost->id, $post->get_id());
+ $this->assertEquals($dbpost->discussion, $post->get_discussionid());
+ $this->assertEquals($prepost->message, $dbpost->message);
+ }
+
+ /**
+ * Test, if a post and its attachment are deleted successfully.
+ * @covers ::moodleoverflow_delete_post
+ */
+ public function test_delete_discussion(): void {
+ global $DB;
+ // Build the prepost object with necessary information.
+ $prepost = new \stdClass();
+ $prepost->modulecontext = $this->modulecontext;
+
+ // Delete the discussion, but save the IDs first.
+ $discussionid = $this->discussion->get_id();
+ $postid = $this->discussion->get_firstpostid();
+ $this->discussion->moodleoverflow_delete_discussion($prepost);
+
+ // The discussion and the post should not be in the DB anymore.
+ $discussion = count($DB->get_records('moodleoverflow_discussions', ['id' => $discussionid]));
+ $this->assertEquals(0, $discussion);
+
+ $post = count($DB->get_records('moodleoverflow_posts', ['id' => $postid]));
+ $this->assertEquals(0, $post);
+ }
+
+ /**
+ * This function creates:
+ * - a course with a moodleoverflow
+ * - a new discussion with a post. The post has an attachment.
+ */
+ private function helper_course_set_up() {
+ global $DB;
+ // Create a new course with a moodleoverflow forum.
+ $this->course = $this->getDataGenerator()->create_course();
+ $location = ['course' => $this->course->id];
+ $this->moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $location);
+ $this->coursemodule = get_coursemodule_from_instance('moodleoverflow', $this->moodleoverflow->id);
+ $this->modulecontext = \context_module::instance($this->coursemodule->id);
+
+ // Create a teacher.
+ $this->teacher = $this->getDataGenerator()->create_user(['firstname' => 'Tamaro', 'lastname' => 'Walter']);
+ $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, 'student');
+
+ // Create a discussion started from the teacher.
+ $this->generator = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow');
+ $discussion = $this->generator->post_to_forum($this->moodleoverflow, $this->teacher);
+
+ // Get the discussion and post object.
+ $discussionrecord = $DB->get_record('moodleoverflow_discussions', ['id' => $discussion[0]->id]);
+ $postrecord = $DB->get_record('moodleoverflow_posts', ['id' => $discussion[1]->id]);
+
+ $this->discussion = discussion::from_record($discussionrecord);
+ $this->post = post::from_record($postrecord);
+ }
+}
diff --git a/tests/post_test.php b/tests/post_test.php
index b29614c879..d2e62d5948 100644
--- a/tests/post_test.php
+++ b/tests/post_test.php
@@ -15,7 +15,7 @@
// along with Moodle. If not, see .
/**
- * PHP Unit test for post related functions in the locallib.
+ * PHP Unit Tests for the Post class.
*
* @package mod_moodleoverflow
* @copyright 2023 Tamaro Walter
@@ -23,15 +23,21 @@
*/
namespace mod_moodleoverflow;
+// Use the post class.
+use mod_moodleoverflow\post\post;
+use mod_moodleoverflow\discussion\discussion;
+
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../locallib.php');
/**
- * PHP Unit test for post related functions in the locallib.
+ *
+ * Tests if the functions from the post class are working correctly.
*
* @package mod_moodleoverflow
* @copyright 2023 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @covers \mod_moodleoverflow\post\post
*/
class post_test extends \advanced_testcase {
@@ -41,25 +47,24 @@ class post_test extends \advanced_testcase {
/** @var \stdClass coursemodule */
private $coursemodule;
+ /** @var \stdClass modulecontext */
+ private $modulecontext;
+
/** @var \stdClass test moodleoverflow */
private $moodleoverflow;
/** @var \stdClass test teacher */
private $teacher;
- /** @var \stdClass a discussion */
+ /** @var discussion a discussion */
private $discussion;
- /** @var \stdClass a post */
+ /** @var post a post */
private $post;
- /** @var \stdClass an attachment */
- private $attachment;
-
/** @var \mod_moodleoverflow_generator $generator */
private $generator;
-
public function setUp(): void {
$this->resetAfterTest();
$this->helper_course_set_up();
@@ -67,46 +72,78 @@ public function setUp(): void {
public function tearDown(): void {
// Clear all caches.
- \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
- \mod_moodleoverflow\subscriptions::reset_discussion_cache();
+ subscriptions::reset_moodleoverflow_cache();
+ subscriptions::reset_discussion_cache();
}
/**
- * Test if a post and its attachment are deleted successfully.
- * @covers ::moodleoverflow_delete_post
+ * Test, if a post is being created correctly
*/
- public function test_moodleoverflow_delete_post(): void {
+ public function test_create_post(): void {
+ global $DB;
+ // 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->moodleoverflow_add_new_post();
+
+ // The post should be in the database.
+ $postscount = count($DB->get_records('moodleoverflow_posts', ['id' => $post->get_id()]));
+ $this->assertEquals(1, $postscount);
+ }
+
+ /**
+ * Test, if the message of a post can be edited successfully.
+ */
+ public function test_edit_post(): void {
global $DB;
- // The attachment should exist.
- $numberofattachments = count($DB->get_records('files', ['itemid' => $this->post->id]));
- $this->assertEquals(2, $numberofattachments);
+ // The post and the attachment should exist.
+ $numberofattachments = count($DB->get_records('files', ['itemid' => $this->post->get_id()]));
+ $this->assertEquals(2, $numberofattachments); // One Attachment is saved twice in 'files'.
+ $post = count($DB->get_records('moodleoverflow_posts', ['id' => $this->post->get_id()]));
+ $this->assertEquals(1, $post);
- // Delete the post from the teacher with its attachment.
- moodleoverflow_delete_post($this->post, false, $this->coursemodule, $this->moodleoverflow);
+ // Gather important parameters.
+ $message = 'a new message';
- // Now try to get the attachment.
- $numberofattachments = count($DB->get_records('files', ['itemid' => $this->post->id]));
+ $time = time();
- $this->assertEquals(0, $numberofattachments);
+ // Update the post.
+ $this->post->moodleoverflow_edit_post($time, $message, $this->post->messageformat, $this->post->formattachments);
+
+ // 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($time, $post->modified);
}
/**
- * Test if a post and its attachment are deleted successfully.
- * @covers ::moodleoverflow_delete_discussion
+ * Test, if a post and its attachment are deleted successfully.
+ * @covers ::moodleoverflow_delete_post
*/
- public function test_moodleoverflow_delete_discussion(): void {
+ public function test_moodleoverflow_delete_post(): void {
global $DB;
- $numberofattachments = count($DB->get_records('files', ['itemid' => $this->post->id, 'filearea' => 'attachment']));
- $this->assertEquals(2, $numberofattachments);
+ // The post and the attachment should exist.
+ $numberofattachments = count($DB->get_records('files', ['itemid' => $this->post->get_id()]));
+ $this->assertEquals(2, $numberofattachments); // One Attachment is saved twice in 'files'.
+ $post = count($DB->get_records('moodleoverflow_posts', ['id' => $this->post->get_id()]));
+ $this->assertEquals(1, $post);
- // Delete the post from the teacher with its attachment.
- moodleoverflow_delete_discussion($this->discussion[0], $this->course, $this->coursemodule, $this->moodleoverflow);
+ // Delete the post with its attachment.
+ // Save the post id as it gets unsettled by the post object after being deleted.
+ $postid = $this->post->get_id();
+ $this->post->moodleoverflow_delete_post(true);
- // Now try to get the attachment.
- $numberofattachments = count($DB->get_records('files', ['itemid' => $this->post->id]));
+ // Now try to get the attachment, it should be deleted from the database.
+ $numberofattachments = count($DB->get_records('files', ['itemid' => $postid]));
$this->assertEquals(0, $numberofattachments);
+
+ // Try to find the post, it should be deleted.
+ $post = count($DB->get_records('moodleoverflow_posts', ['id' => $postid]));
+ $this->assertEquals(0, $post);
}
/**
@@ -121,6 +158,7 @@ private function helper_course_set_up() {
$location = ['course' => $this->course->id];
$this->moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $location);
$this->coursemodule = get_coursemodule_from_instance('moodleoverflow', $this->moodleoverflow->id);
+ $this->modulecontext = \context_module::instance($this->coursemodule->id);
// Create a teacher.
$this->teacher = $this->getDataGenerator()->create_user(['firstname' => 'Tamaro', 'lastname' => 'Walter']);
@@ -128,29 +166,40 @@ private function helper_course_set_up() {
// Create a discussion started from the teacher.
$this->generator = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow');
- $this->discussion = $this->generator->post_to_forum($this->moodleoverflow, $this->teacher);
- $this->post = $DB->get_record('moodleoverflow_posts', ['id' => $this->discussion[0]->firstpost], '*');
+ $discussion = $this->generator->post_to_forum($this->moodleoverflow, $this->teacher);
+ $discussionrecord = $DB->get_record('moodleoverflow_discussions', ['id' => $discussion[0]->id]);
+ $this->discussion = discussion::from_record($discussionrecord);
+
+ // Get a temporary post from the DB to add the attachment.
+ $temppost = $DB->get_record('moodleoverflow_posts', ['id' => $this->discussion->get_firstpostid()]);
// Create an attachment by inserting it directly in the database and update the post record.
+ $this->add_new_attachment($temppost, $this->modulecontext, 'world.txt', 'hello world');
- $modulecontext = \context_module::instance($this->coursemodule->id);
+ // Build the real post object now. That is the object that will be tested.
+ $postrecord = $DB->get_record('moodleoverflow_posts', ['id' => $this->discussion->get_firstpostid()]);
+ $this->post = post::from_record($postrecord);
+ }
+ /**
+ * @param object $object // The object that will have the attachment
+ * @param $modulecontext
+ * @param string $filename
+ * @param $filecontent
+ * @return void
+ */
+ 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' => $this->post->id, // Usually = ID of row in table.
- 'filepath' => '/', // Any path beginning and ending in /.
- 'filename' => 'NH.jpg', // 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();
-
- // Create a new file containing the text 'hello world'.
- $fs->create_file_from_string($fileinfo, 'hello world');
-
- $this->post->attachment = 1;
- $DB->update_record('moodleoverflow_posts', $this->post);
-
+ $fs->create_file_from_string($fileinfo, $filecontent); // Creates a new file containing the text 'hello world'.
+ $DB->update_record('moodleoverflow_posts', $object);
}
}
diff --git a/tests/ratings_test.php b/tests/ratings_test.php
index f0dccc267b..5792913d4e 100644
--- a/tests/ratings_test.php
+++ b/tests/ratings_test.php
@@ -23,6 +23,7 @@
*/
namespace mod_moodleoverflow;
use mod_moodleoverflow\ratings;
+use stdClass;
defined('MOODLE_INTERNAL') || die();
@@ -38,46 +39,46 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class ratings_test extends \advanced_testcase {
- /** @var \stdClass test course */
+ /** @var stdClass test course */
private $course;
- /** @var \stdClass coursemodule */
+ /** @var stdClass coursemodule */
private $coursemodule;
- /** @var \stdClass test moodleoverflow */
+ /** @var stdClass test moodleoverflow */
private $moodleoverflow;
- /** @var \stdClass test teacher */
+ /** @var stdClass test teacher */
private $teacher;
- /** @var \stdClass test user */
+ /** @var stdClass test user */
private $user1;
- /** @var \stdClass another test user */
+ /** @var stdClass another test user */
private $user2;
- /** @var \stdClass a discussion */
+ /** @var stdClass a discussion */
private $discussion;
- /** @var \stdClass a post from the teacher*/
+ /** @var stdClass a post from the teacher*/
private $post;
- /** @var \stdClass answer from user 1 */
+ /** @var stdClass answer from user 1 */
private $answer1;
- /** @var \stdClass answer from user 1 */
+ /** @var stdClass answer from user 1 */
private $answer2;
- /** @var \stdClass answer from user 1 */
+ /** @var stdClass answer from user 1 */
private $answer3;
- /** @var \stdClass answer from user 2 */
+ /** @var stdClass answer from user 2 */
private $answer4;
- /** @var \stdClass answer from user 2 */
+ /** @var stdClass answer from user 2 */
private $answer5;
- /** @var \stdClass answer from user 2 */
+ /** @var stdClass answer from user 2 */
private $answer6;
/** @var \mod_moodleoverflow_generator $generator */
diff --git a/tests/subscriptions_test.php b/tests/subscriptions_test.php
index 2de58cf8b2..9e0061c90c 100644
--- a/tests/subscriptions_test.php
+++ b/tests/subscriptions_test.php
@@ -45,8 +45,8 @@ class subscriptions_test extends advanced_testcase {
*/
public function setUp(): void {
// Clear all caches.
- \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
- \mod_moodleoverflow\subscriptions::reset_discussion_cache();
+ subscriptions::reset_moodleoverflow_cache();
+ subscriptions::reset_discussion_cache();
}
/**
@@ -54,8 +54,8 @@ public function setUp(): void {
*/
public function tearDown(): void {
// Clear all caches.
- \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
- \mod_moodleoverflow\subscriptions::reset_discussion_cache();
+ subscriptions::reset_moodleoverflow_cache();
+ subscriptions::reset_discussion_cache();
}
/**
@@ -129,34 +129,34 @@ public function test_subscription_modes(): void {
$this->setUser($user);
// Test the forced subscription.
- \mod_moodleoverflow\subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_FORCESUBSCRIBE);
+ subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_FORCESUBSCRIBE);
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id]);
$this->assertEquals(MOODLEOVERFLOW_FORCESUBSCRIBE,
- \mod_moodleoverflow\subscriptions::get_subscription_mode($moodleoverflow));
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow));
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext));
- $this->assertFalse(\mod_moodleoverflow\subscriptions::subscription_disabled($moodleoverflow));
+ subscriptions::get_subscription_mode($moodleoverflow));
+ $this->assertTrue(subscriptions::is_forcesubscribed($moodleoverflow));
+ $this->assertFalse(subscriptions::is_subscribable($moodleoverflow, $modulecontext));
+ $this->assertFalse(subscriptions::subscription_disabled($moodleoverflow));
// Test the disallowed subscription.
- \mod_moodleoverflow\subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_DISALLOWSUBSCRIBE);
+ subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_DISALLOWSUBSCRIBE);
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id]);
- $this->assertTrue(\mod_moodleoverflow\subscriptions::subscription_disabled($moodleoverflow));
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext));
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow));
+ $this->assertTrue(subscriptions::subscription_disabled($moodleoverflow));
+ $this->assertFalse(subscriptions::is_subscribable($moodleoverflow, $modulecontext));
+ $this->assertFalse(subscriptions::is_forcesubscribed($moodleoverflow));
// Test the initial subscription.
- \mod_moodleoverflow\subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_INITIALSUBSCRIBE);
+ subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_INITIALSUBSCRIBE);
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id]);
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext));
- $this->assertFalse(\mod_moodleoverflow\subscriptions::subscription_disabled($moodleoverflow));
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow));
+ $this->assertTrue(subscriptions::is_subscribable($moodleoverflow, $modulecontext));
+ $this->assertFalse(subscriptions::subscription_disabled($moodleoverflow));
+ $this->assertFalse(subscriptions::is_forcesubscribed($moodleoverflow));
// Test the choose subscription.
- \mod_moodleoverflow\subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_CHOOSESUBSCRIBE);
+ subscriptions::set_subscription_mode($moodleoverflow->id, MOODLEOVERFLOW_CHOOSESUBSCRIBE);
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id]);
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext));
- $this->assertFalse(\mod_moodleoverflow\subscriptions::subscription_disabled($moodleoverflow));
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow));
+ $this->assertTrue(subscriptions::is_subscribable($moodleoverflow, $modulecontext));
+ $this->assertFalse(subscriptions::subscription_disabled($moodleoverflow));
+ $this->assertFalse(subscriptions::is_forcesubscribed($moodleoverflow));
}
/**
@@ -182,7 +182,7 @@ public function test_unsubscribable_moodleoverflows(): void {
$this->setUser($user);
// Without any subscriptions, there should be nothing returned.
- $result = \mod_moodleoverflow\subscriptions::get_unsubscribable_moodleoverflows();
+ $result = subscriptions::get_unsubscribable_moodleoverflows();
$this->assertEquals(0, count($result));
// Create the moodleoverflows.
@@ -196,15 +196,15 @@ public function test_unsubscribable_moodleoverflows(): void {
$this->getDataGenerator()->create_module('moodleoverflow', $options);
// At present the user is only subscribed to the initial moodleoverflow.
- $result = \mod_moodleoverflow\subscriptions::get_unsubscribable_moodleoverflows();
+ $result = subscriptions::get_unsubscribable_moodleoverflows();
$this->assertEquals(1, count($result));
// Ensure that the user is enrolled in all of the moodleoverflows execpt force subscribe.
- \mod_moodleoverflow\subscriptions::subscribe_user($user->id, $disallow, $modulecontext);
- \mod_moodleoverflow\subscriptions::subscribe_user($user->id, $choose, $modulecontext);
+ subscriptions::subscribe_user($user->id, $disallow, $modulecontext);
+ subscriptions::subscribe_user($user->id, $choose, $modulecontext);
// At present the user is subscribed to all three moodleoverflows.
- $result = \mod_moodleoverflow\subscriptions::get_unsubscribable_moodleoverflows();
+ $result = subscriptions::get_unsubscribable_moodleoverflows();
$this->assertEquals(3, count($result));
}
@@ -236,11 +236,11 @@ public function test_moodleoverflow_toggle_as_other(): void {
$discussion->moodleoverflow = $moodleoverflow->id;
// Check that the user is currently not subscribed to the moodleoverflow.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow,
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow,
$modulecontext));
// Check that the user is unsubscribed from the discussion too.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow,
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow,
$modulecontext, $discussion->id));
// Check thast we have no records in either on the subscription tables.
@@ -253,7 +253,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
// Subscribing to the moodleoverflow should create a record in the subscription table,
// but the moodleoverflow discussion subscriptions table.
- \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext);
+ subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(1, $count);
@@ -263,7 +263,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
// Unsubscribing should remove the record from the moodleoverflow subscription table.
// Do not modify the moodleoverflow discussion subscriptions table.
- \mod_moodleoverflow\subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext);
+ subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(0, $count);
@@ -273,7 +273,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
// Enroling the user in the discussion should add one record to the
// moodleoverflow discussion table without modifying the form subscription.
- \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(0, $count);
@@ -283,7 +283,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
// Unsubscribing should remove the record from the moodleoverflow subscriptions
// table and not modify the moodleoverflow discussion subscription table.
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
$options = ['userid' => $author->id, 'discussion' => $discussion->id];
$count = $DB->count_records('moodleoverflow_discuss_subs', $options);
$this->assertEquals(0, $count);
@@ -292,7 +292,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
$this->assertEquals(0, $count);
// Resubscribe to the discussion so that we can check the effect of moodleoverflow-level subscriptions.
- \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
$options = ['userid' => $author->id, 'discussion' => $discussion->id];
$count = $DB->count_records('moodleoverflow_discuss_subs', $options);
$this->assertEquals(1, $count);
@@ -302,7 +302,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
// Subscribing to the moodleoverflow should have no effect on the moodleoverflow discussion
// subscription table if the user did not request the change himself.
- \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext);
+ subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(1, $count);
@@ -312,7 +312,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
// Unsubbing from the moodleoverflow should have no effect on the moodleoverflow
// discussion subscription table if the user did not request the change themself.
- \mod_moodleoverflow\subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext);
+ subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(0, $count);
@@ -322,7 +322,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
// Subscribing to the moodleoverflow should remove the per-discussion
// subscription preference if the user requested the change themself.
- \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext, true);
+ subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext, true);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(1, $count);
@@ -331,7 +331,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
$this->assertEquals(0, $count);
// Now unsubscribe from the current discussion whilst being subscribed to the moodleoverflow as a whole.
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(1, $count);
@@ -341,7 +341,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
// Unsubscribing from the moodleoverflow should remove the per-discussion
// subscription preference if the user requested the change himself.
- \mod_moodleoverflow\subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext, true);
+ subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext, true);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(0, $count);
@@ -352,7 +352,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
$this->assertEquals(0, $count);
// Subscribe to the discussion.
- \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(0, $count);
@@ -363,7 +363,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
$this->assertEquals(1, $count);
// Subscribe to the moodleoverflow without removing the discussion preferences.
- \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext);
+ subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(1, $count);
@@ -372,7 +372,7 @@ public function test_moodleoverflow_toggle_as_other(): void {
$this->assertEquals(1, $count);
// Unsubscribe from the discussion should result in a change.
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
$options = ['userid' => $author->id, 'moodleoverflow' => $moodleoverflow->id];
$count = $DB->count_records('moodleoverflow_subscriptions', $options);
$this->assertEquals(1, $count);
@@ -397,14 +397,14 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_unsub
list($author) = $this->helper_create_users($course, 1);
// Check that the user is currently not subscribed to the moodleoverflow.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $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);
unset($post);
// Check that the user is unsubscribed from the discussion too.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow,
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow,
$moodleoverflow, $discussion));
}
@@ -429,21 +429,21 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc
// Enrol the user in the moodleoverflow.
// If a subscription was added, we get the record ID.
- $this->assertIsInt(\mod_moodleoverflow\subscriptions::subscribe_user($author->id,
+ $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(\mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext));
+ $this->assertTrue(subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext));
// Check that the user is currently subscribed to the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $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);
unset($post);
// Check that the user is subscribed to the discussion too.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion));
}
@@ -467,7 +467,7 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_unsub
list($author) = $this->helper_create_users($course, 1);
// Check that the user is currently not subscribed to the moodleoverflow.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow));
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow));
// Post a discussion to the moodleoverflow.
$discussion = new \stdClass();
@@ -476,18 +476,18 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_unsub
$discussion->moodleoverflow = $moodleoverflow->id;
// Attempting to unsubscribe from the discussion should not make a change.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id,
+ $this->assertFalse(subscriptions::unsubscribe_user_from_discussion($author->id,
$discussion, $modulecontext));
// Then subscribe them to the discussion.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id,
+ $this->assertTrue(subscriptions::subscribe_user_to_discussion($author->id,
$discussion, $modulecontext));
// Check that the user is still unsubscribed from the moodleoverflow.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow));
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $moodleoverflow));
// But subscribed to the discussion.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
}
@@ -511,10 +511,10 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc
list($author) = $this->helper_create_users($course, 2);
// Enrol the student in the moodleoverflow.
- \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext);
+ subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext);
// Check that the user is currently subscribed to the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// Post a discussion to the moodleoverflow.
$discussion = new \stdClass();
@@ -523,13 +523,13 @@ public function test_moodleoverflow_discussion_subscription_moodleoverflow_subsc
$discussion->moodleoverflow = $moodleoverflow->id;
// Then unsubscribe them from the discussion.
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
// Check that the user is still subscribed to the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// But unsubscribed from the discussion.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
}
@@ -554,7 +554,7 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(
list($author) = $this->helper_create_users($course, 2);
// Enrol the student in the moodleoverflow.
- \mod_moodleoverflow\subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext);
+ subscriptions::subscribe_user($author->id, $moodleoverflow, $modulecontext);
// Post a discussion to the moodleoverflow.
$discussion = new \stdClass();
@@ -563,14 +563,14 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(
$discussion->moodleoverflow = $moodleoverflow->id;
// Check that the user is currently subscribed to the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// Check that the user is initially subscribed to that discussion.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// An attempt to subscribe again should result in a falsey return to indicate that no change was made.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id,
+ $this->assertFalse(subscriptions::subscribe_user_to_discussion($author->id,
$discussion, $modulecontext));
// And there should be no discussion subscriptions (and one moodleoverflow subscription).
@@ -582,13 +582,13 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(
$this->assertEquals(1, $count);
// Then unsubscribe them from the discussion.
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
// Check that the user is still subscribed to the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// An attempt to unsubscribe again should result in a falsey return to indicate that no change was made.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id,
+ $this->assertFalse(subscriptions::unsubscribe_user_from_discussion($author->id,
$discussion, $modulecontext));
// And there should be a discussion subscriptions (and one moodleoverflow subscription).
@@ -600,7 +600,7 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(
$this->assertEquals(1, $count);
// But unsubscribed from the discussion.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// There should be a record in the discussion subscription tracking table.
@@ -614,13 +614,13 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(
$this->assertEquals(1, $count);
// Now subscribe the user again to the discussion.
- \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
// Check that the user is still subscribed to the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// And is subscribed to the discussion again.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// And one in the moodleoverflow subscription tracking table.
@@ -634,13 +634,13 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(
$this->assertEquals(0, $count);
// And unsubscribe again.
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
// Check that the user is still subscribed to the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// But unsubscribed from the discussion.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// And one in the moodleoverflow subscription tracking table.
@@ -654,14 +654,14 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(
$this->assertEquals(1, $count);
// And subscribe the user again to the discussion.
- \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
// Check that the user is still subscribed to the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// And is subscribed to the discussion again.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// There should be no record in the discussion subscription tracking table.
@@ -675,14 +675,14 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(
$this->assertEquals(1, $count);
// And unsubscribe again.
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
// But unsubscribed from the discussion.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// Check that the user is still subscribed to the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// There should be a record in the discussion subscription tracking table.
$options = ['userid' => $author->id, 'discussion' => $discussion->id];
@@ -695,7 +695,7 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(
$this->assertEquals(1, $count);
// Now unsubscribe the user from the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext,
+ $this->assertTrue(subscriptions::unsubscribe_user($author->id, $moodleoverflow, $modulecontext,
true));
// This removes both the moodleoverflow, and the moodleoverflow records.
@@ -707,7 +707,7 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_subscribed(
$this->assertEquals(0, $count);
// And should have reset the discussion cache value.
- $result = \mod_moodleoverflow\subscriptions::fetch_discussion_subscription($moodleoverflow->id, $author->id);
+ $result = subscriptions::fetch_discussion_subscription($moodleoverflow->id, $author->id);
$this->assertIsArray($result);
$this->assertFalse(isset($result[$discussion->id]));
}
@@ -734,7 +734,7 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_unsubscribe
list($author) = $this->helper_create_users($course, 2);
// Check that the user is currently unsubscribed to the moodleoverflow.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// Post a discussion to the moodleoverflow.
$discussion = new \stdClass();
@@ -743,22 +743,22 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_unsubscribe
$discussion->moodleoverflow = $moodleoverflow->id;
// Check that the user is initially unsubscribed to that discussion.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// Then subscribe them to the discussion.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id,
+ $this->assertTrue(subscriptions::subscribe_user_to_discussion($author->id,
$discussion, $modulecontext));
// An attempt to subscribe again should result in a falsey return to indicate that no change was made.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id,
+ $this->assertFalse(subscriptions::subscribe_user_to_discussion($author->id,
$discussion, $modulecontext));
// Check that the user is still unsubscribed from the moodleoverflow.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// But subscribed to the discussion.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// There should be a record in the discussion subscription tracking table.
@@ -767,13 +767,13 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_unsubscribe
$this->assertEquals(1, $count);
// Now unsubscribe the user again from the discussion.
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
// Check that the user is still unsubscribed from the moodleoverflow.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// And is unsubscribed from the discussion again.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// There should be no record in the discussion subscription tracking table.
@@ -782,14 +782,14 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_unsubscribe
$this->assertEquals(0, $count);
// And subscribe the user again to the discussion.
- \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::subscribe_user_to_discussion($author->id, $discussion, $modulecontext);
// And is subscribed to the discussion again.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertTrue(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// Check that the user is still unsubscribed from the moodleoverflow.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// There should be a record in the discussion subscription tracking table.
$options = ['userid' => $author->id, 'discussion' => $discussion->id];
@@ -797,14 +797,14 @@ public function test_moodleoverflow_discussion_toggle_moodleoverflow_unsubscribe
$this->assertEquals(1, $count);
// And unsubscribe again.
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($author->id, $discussion, $modulecontext);
// But unsubscribed from the discussion.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext,
$discussion->id));
// Check that the user is still unsubscribed from the moodleoverflow.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
+ $this->assertFalse(subscriptions::is_subscribed($author->id, $moodleoverflow, $modulecontext));
// There should be no record in the discussion subscription tracking table.
$options = ['userid' => $author->id, 'discussion' => $discussion->id];
@@ -836,22 +836,22 @@ public function test_fetch_subscribed_users_subscriptions(): void {
$users = $this->helper_create_users($course, $usercount);
// All users should be subscribed.
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
$this->assertEquals($usercount, count($subscribers));
// Subscribe the guest user too to the moodleoverflow - they should never be returned by this function.
$this->getDataGenerator()->enrol_user($CFG->siteguest, $course->id);
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
$this->assertEquals($usercount, count($subscribers));
// Unsubscribe 2 users.
$unsubscribedcount = 2;
for ($i = 0; $i < $unsubscribedcount; $i++) {
- \mod_moodleoverflow\subscriptions::unsubscribe_user($users[$i]->id, $moodleoverflow, $modulecontext);
+ subscriptions::unsubscribe_user($users[$i]->id, $moodleoverflow, $modulecontext);
}
// The subscription count should now take into account those users who have been unsubscribed.
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
$this->assertEquals($usercount - $unsubscribedcount, count($subscribers));
}
@@ -877,7 +877,7 @@ public function test_fetch_subscribed_users_forced(): void {
$this->helper_create_users($course, $usercount);
// All users should be subscribed.
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
$this->assertEquals($usercount, count($subscribers));
}
@@ -910,20 +910,20 @@ public function test_fetch_subscribed_users_discussion_subscriptions(): void {
$discussion->moodleoverflow = $moodleoverflow->id;
// All users should be subscribed.
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
$this->assertEquals($usercount, count($subscribers));
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null,
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null,
true);
$this->assertEquals($usercount, count($subscribers));
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($users[0]->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($users[0]->id, $discussion, $modulecontext);
// All users should be subscribed.
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
$this->assertEquals($usercount, count($subscribers));
// All users should be subscribed.
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null,
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null,
true);
$this->assertEquals($usercount, count($subscribers));
@@ -936,33 +936,33 @@ public function test_fetch_subscribed_users_discussion_subscriptions(): void {
$DB->insert_record('moodleoverflow_discuss_subs', $record);
// The discussion count should not have changed.
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
$this->assertEquals($usercount, count($subscribers));
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null,
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null,
true);
$this->assertEquals($usercount, count($subscribers));
// Unsubscribe 2 users.
$unsubscribedcount = 2;
for ($i = 0; $i < $unsubscribedcount; $i++) {
- \mod_moodleoverflow\subscriptions::unsubscribe_user($users[$i]->id, $moodleoverflow, $modulecontext);
+ subscriptions::unsubscribe_user($users[$i]->id, $moodleoverflow, $modulecontext);
}
// The subscription count should now take into account those users who have been unsubscribed.
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
$this->assertEquals($usercount - $unsubscribedcount, count($subscribers));
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null,
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null,
true);
$this->assertEquals($usercount - $unsubscribedcount, count($subscribers));
// Now subscribe one of those users back to the discussion.
$subedusers = 1;
for ($i = 0; $i < $subedusers; $i++) {
- \mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($users[$i]->id, $discussion, $modulecontext);
+ subscriptions::subscribe_user_to_discussion($users[$i]->id, $discussion, $modulecontext);
}
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext);
$this->assertEquals($usercount - $unsubscribedcount, count($subscribers));
- $subscribers = \mod_moodleoverflow\subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null,
+ $subscribers = subscriptions::get_subscribed_users($moodleoverflow, $modulecontext, null,
true);
$this->assertEquals($usercount - $unsubscribedcount + $subedusers, count($subscribers));
}
@@ -992,7 +992,7 @@ public function test_force_subscribed_to_moodleoverflow(): void {
$this->getDataGenerator()->enrol_user($user->id, $course->id, $roleids['student']);
// Check that the user is currently subscribed to the moodleoverflow.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::is_subscribed($user->id, $moodleoverflow, $context));
+ $this->assertTrue(subscriptions::is_subscribed($user->id, $moodleoverflow, $context));
assign_capability('mod/moodleoverflow:allowforcesubscribe', CAP_PROHIBIT, $roleids['student'],
$context);
@@ -1018,17 +1018,17 @@ public function test_subscription_cache_prefill(): void {
$users = $this->helper_create_users($course, 20);
// Reset the subscription cache.
- \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
+ subscriptions::reset_moodleoverflow_cache();
// Filling the subscription cache should only use a single query, except for Postgres, which delegates actual reading
// to Cursors, thus tripling the amount of queries. We intend to test the cache, though, so no worries.
- $this->assertNull(\mod_moodleoverflow\subscriptions::fill_subscription_cache($moodleoverflow->id));
+ $this->assertNull(subscriptions::fill_subscription_cache($moodleoverflow->id));
$postfillcount = $DB->perf_get_reads();
// Now fetch some subscriptions from that moodleoverflow - these should use
// the cache and not perform additional queries.
foreach ($users as $user) {
- $this->assertTrue(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($moodleoverflow->id, $user->id));
+ $this->assertTrue(subscriptions::fetch_subscription_cache($moodleoverflow->id, $user->id));
}
$finalcount = $DB->perf_get_reads();
$this->assertEquals($finalcount, $postfillcount);
@@ -1053,14 +1053,14 @@ public function test_subscription_cache_fill(): void {
$users = $this->helper_create_users($course, 20);
// Reset the subscription cache.
- \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
+ subscriptions::reset_moodleoverflow_cache();
// Filling the subscription cache should only use a single query.
$startcount = $DB->perf_get_reads();
// Fetch some subscriptions from that moodleoverflow - these should not use the cache and will perform additional queries.
foreach ($users as $user) {
- $this->assertTrue(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($moodleoverflow->id, $user->id));
+ $this->assertTrue(subscriptions::fetch_subscription_cache($moodleoverflow->id, $user->id));
}
$finalcount = $DB->perf_get_reads();
$this->assertEquals(20, $finalcount - $startcount);
@@ -1091,23 +1091,23 @@ public function test_discussion_subscription_cache_fill_for_course(): void {
$user = reset($users);
// Reset the subscription caches.
- \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
+ subscriptions::reset_moodleoverflow_cache();
- $result = \mod_moodleoverflow\subscriptions::fill_subscription_cache_for_course($course->id, $user->id);
+ $result = subscriptions::fill_subscription_cache_for_course($course->id, $user->id);
$this->assertNull($result);
$postfillcount = $DB->perf_get_reads();
- $this->assertFalse(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($disallowmoodleoverflow->id, $user->id));
- $this->assertFalse(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($choosemoodleoverflow->id, $user->id));
- $this->assertTrue(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($initialmoodleoverflow->id, $user->id));
+ $this->assertFalse(subscriptions::fetch_subscription_cache($disallowmoodleoverflow->id, $user->id));
+ $this->assertFalse(subscriptions::fetch_subscription_cache($choosemoodleoverflow->id, $user->id));
+ $this->assertTrue(subscriptions::fetch_subscription_cache($initialmoodleoverflow->id, $user->id));
$finalcount = $DB->perf_get_reads();
$this->assertEquals(0, $finalcount - $postfillcount);
// Test for all users.
foreach ($users as $user) {
- $result = \mod_moodleoverflow\subscriptions::fill_subscription_cache_for_course($course->id, $user->id);
- $this->assertFalse(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($disallowmoodleoverflow->id, $user->id));
- $this->assertFalse(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($choosemoodleoverflow->id, $user->id));
- $this->assertTrue(\mod_moodleoverflow\subscriptions::fetch_subscription_cache($initialmoodleoverflow->id, $user->id));
+ $result = subscriptions::fill_subscription_cache_for_course($course->id, $user->id);
+ $this->assertFalse(subscriptions::fetch_subscription_cache($disallowmoodleoverflow->id, $user->id));
+ $this->assertFalse(subscriptions::fetch_subscription_cache($choosemoodleoverflow->id, $user->id));
+ $this->assertTrue(subscriptions::fetch_subscription_cache($initialmoodleoverflow->id, $user->id));
}
$finalcount = $DB->perf_get_reads();
$reads = $finalcount - $postfillcount;
@@ -1162,24 +1162,24 @@ public function test_discussion_subscription_cache_prefill(): void {
if ($usercount % 2) {
continue;
}
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $modulecontext);
$usercount++;
}
$moodleoverflowcount++;
}
// Reset the subscription caches.
- \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
- \mod_moodleoverflow\subscriptions::reset_discussion_cache();
+ subscriptions::reset_moodleoverflow_cache();
+ subscriptions::reset_discussion_cache();
// Filling the discussion subscription cache should only use a single query.
- $this->assertNull(\mod_moodleoverflow\subscriptions::fill_discussion_subscription_cache($moodleoverflow->id));
+ $this->assertNull(subscriptions::fill_discussion_subscription_cache($moodleoverflow->id));
$postfillcount = $DB->perf_get_reads();
// Now fetch some subscriptions from that moodleoverflow - these should use
// the cache and not perform additional queries.
foreach ($users as $user) {
- $result = \mod_moodleoverflow\subscriptions::fetch_discussion_subscription($moodleoverflow->id, $user->id);
+ $result = subscriptions::fetch_discussion_subscription($moodleoverflow->id, $user->id);
$this->assertIsArray($result);
}
$finalcount = $DB->perf_get_reads();
@@ -1229,22 +1229,22 @@ public function test_discussion_subscription_cache_fill(): void {
if ($usercount % 2) {
continue;
}
- \mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $modulecontext);
+ subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $modulecontext);
$usercount++;
}
$moodleoverflowcount++;
}
// Reset the subscription caches.
- \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
- \mod_moodleoverflow\subscriptions::reset_discussion_cache();
+ subscriptions::reset_moodleoverflow_cache();
+ subscriptions::reset_discussion_cache();
$startcount = $DB->perf_get_reads();
// Now fetch some subscriptions from that moodleoverflow - these should use
// the cache and not perform additional queries.
foreach ($users as $user) {
- $result = \mod_moodleoverflow\subscriptions::fetch_discussion_subscription($moodleoverflow->id, $user->id);
+ $result = subscriptions::fetch_discussion_subscription($moodleoverflow->id, $user->id);
$this->assertIsArray($result);
}
$finalcount = $DB->perf_get_reads();
@@ -1287,10 +1287,10 @@ public function test_moodleoverflow_subscribe_toggle_as_other_repeat_subscriptio
$discussion->moodleoverflow = $moodleoverflow->id;
// Confirm that the user is currently not subscribed to the moodleoverflow.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($user->id, $moodleoverflow, $modulecontext));
+ $this->assertFalse(subscriptions::is_subscribed($user->id, $moodleoverflow, $modulecontext));
// Confirm that the user is unsubscribed from the discussion too.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribed($user->id, $moodleoverflow, $modulecontext,
+ $this->assertFalse(subscriptions::is_subscribed($user->id, $moodleoverflow, $modulecontext,
$discussion->id));
// Confirm that we have no records in either of the subscription tables.
@@ -1305,7 +1305,7 @@ public function test_moodleoverflow_subscribe_toggle_as_other_repeat_subscriptio
// Subscribing to the moodleoverflow should create a record in the subscriptions table,
// but not the moodleoverflow discussion subscriptions table.
- \mod_moodleoverflow\subscriptions::subscribe_user($user->id, $moodleoverflow, $modulecontext);
+ subscriptions::subscribe_user($user->id, $moodleoverflow, $modulecontext);
$this->assertEquals(1, $DB->count_records('moodleoverflow_subscriptions', [
'userid' => $user->id,
'moodleoverflow' => $moodleoverflow->id,
@@ -1317,22 +1317,22 @@ public function test_moodleoverflow_subscribe_toggle_as_other_repeat_subscriptio
// Now unsubscribe from the discussion. This should return true.
$uid = $user->id;
- $this->assertTrue(\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext));
+ $this->assertTrue(subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext));
// Attempting to unsubscribe again should return false because no change was made.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext));
+ $this->assertFalse(subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext));
// Subscribing to the discussion again should return truthfully as the subscription preference was removed.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext));
+ $this->assertTrue(subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext));
// Attempting to subscribe again should return false because no change was made.
- $this->assertFalse(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext));
+ $this->assertFalse(subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext));
// Now unsubscribe from the discussion. This should return true once more.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext));
+ $this->assertTrue(subscriptions::unsubscribe_user_from_discussion($uid, $discussion, $modulecontext));
// And unsubscribing from the moodleoverflow but not as a request from the user should maintain their preference.
- \mod_moodleoverflow\subscriptions::unsubscribe_user($user->id, $moodleoverflow, $modulecontext);
+ subscriptions::unsubscribe_user($user->id, $moodleoverflow, $modulecontext);
$this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', [
'userid' => $user->id,
@@ -1344,7 +1344,7 @@ public function test_moodleoverflow_subscribe_toggle_as_other_repeat_subscriptio
]));
// Subscribing to the discussion should return truthfully because a change was made.
- $this->assertTrue(\mod_moodleoverflow\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext));
+ $this->assertTrue(subscriptions::subscribe_user_to_discussion($user->id, $discussion, $modulecontext));
$this->assertEquals(0, $DB->count_records('moodleoverflow_subscriptions', [
'userid' => $user->id,
'moodleoverflow' => $moodleoverflow->id,
@@ -1407,7 +1407,7 @@ public function test_is_subscribable_logged_out($options): void {
$options['course'] = $course->id;
$moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options);
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow,
+ $this->assertFalse(subscriptions::is_subscribable($moodleoverflow,
\context_module::instance($moodleoverflow->cmid)));
}
@@ -1432,12 +1432,12 @@ public function test_is_subscribable_is_guest($options): void {
$options['course'] = $course->id;
$moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $options);
- $this->assertFalse(\mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow,
+ $this->assertFalse(subscriptions::is_subscribable($moodleoverflow,
\context_module::instance($moodleoverflow->cmid)));
}
/**
- * Returns subscription obtions.
+ * Returns subscription options.
* @return array
*/
public function is_subscribable_loggedin_provider(): array {
@@ -1483,7 +1483,7 @@ public function test_is_subscribable_loggedin($options, $expect): void {
$this->getDataGenerator()->enrol_user($user->id, $course->id);
$this->setUser($user);
- $this->assertEquals($expect, \mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow,
+ $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 b6078f1670..bc91c3a136 100644
--- a/tests/userstats_test.php
+++ b/tests/userstats_test.php
@@ -93,8 +93,8 @@ public function setUp(): void {
*/
public function tearDown(): void {
// Clear all caches.
- \mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
- \mod_moodleoverflow\subscriptions::reset_discussion_cache();
+ subscriptions::reset_moodleoverflow_cache();
+ subscriptions::reset_discussion_cache();
}
// Begin of test functions.
@@ -221,7 +221,7 @@ public function test_partial_anonymous(): void {
}
/**
- * Test, if userstats are calculated correctly if the moodleoverflow is partially anonymous.
+ * Test, if userstats are calculated correctly if the moodleoverflow is totally anonymous.
* @covers \userstats_table
*/
public function test_total_anonymous(): void {
@@ -303,7 +303,7 @@ private function helper_course_set_up() {
* Makes the existing moodleoverflow anonymous.
* There are 2 types of anonymous moodleoverflows:
* anonymous = 1, the topic starter is anonymous
- * anonymous = 2, all users are anonym
+ * anonymous = 2, all users are anonymous
*
* @param int $anonymoussetting
*/
diff --git a/version.php b/version.php
index fe90ba0727..b8fffbb5e6 100644
--- a/version.php
+++ b/version.php
@@ -28,8 +28,8 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'mod_moodleoverflow';
-$plugin->version = 2023082500;
$plugin->release = 'v4.2-r4';
+$plugin->version = 2024031200;
$plugin->requires = 2020061500; // Requires Moodle 3.9+.
$plugin->maturity = MATURITY_STABLE;
$plugin->dependencies = [];
diff --git a/view.php b/view.php
index 9fe603d752..d30c6e6544 100644
--- a/view.php
+++ b/view.php
@@ -65,7 +65,6 @@
// Save the allowmultiplemarks setting.
$marksetting = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow->id], 'allowmultiplemarks');
-
// Require a login.
require_login($course, true, $cm);