/** * Test mod_hsuforum_observer methods. */ public function test_observers() { global $DB, $CFG; require_once $CFG->dirroot . '/mod/hsuforum/lib.php'; $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_hsuforum'); $course = $this->getDataGenerator()->create_course(); $trackedrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => HSUFORUM_INITIALSUBSCRIBE); $untrackedrecord = array('course' => $course->id, 'type' => 'general'); $trackedforum = $this->getDataGenerator()->create_module('hsuforum', $trackedrecord); $untrackedforum = $this->getDataGenerator()->create_module('hsuforum', $untrackedrecord); // Used functions don't require these settings; adding // them just in case there are APIs changes in future. $user = $this->getDataGenerator()->create_user(array('maildigest' => 1, 'trackforums' => 1)); $manplugin = enrol_get_plugin('manual'); $manualenrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual')); $student = $DB->get_record('role', array('shortname' => 'student')); // The role_assign observer does it's job adding the hsuforum_subscriptions record. $manplugin->enrol_user($manualenrol, $user->id, $student->id); $record = array(); $record['course'] = $course->id; $record['forum'] = $trackedforum->id; $record['userid'] = $user->id; // Creating a discussion calls hsuforum_add_discussion which automatically adds a read record // So at this point the read count is 1. $discussion = $forumgen->create_discussion($record); $record = array(); $record['discussion'] = $discussion->id; $record['userid'] = $user->id; // Creating a post doesn't automatically add a read record. $post = $forumgen->create_post($record); // Add a read record for this post - the read record count is now 2 hsuforum_tp_add_read_record($user->id, $post->id); hsuforum_set_user_maildigest($trackedforum, 2, $user); $this->assertEquals(1, $DB->count_records('hsuforum_subscriptions', array('userid' => $user->id))); $this->assertEquals(1, $DB->count_records('hsuforum_digests', array('userid' => $user->id))); $this->assertEquals(2, $DB->count_records('hsuforum_read', array('userid' => $user->id))); // The course_module_created observer does it's job adding a subscription. $forumrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => HSUFORUM_INITIALSUBSCRIBE); $extraforum = $this->getDataGenerator()->create_module('hsuforum', $forumrecord); $this->assertEquals(2, $DB->count_records('hsuforum_subscriptions')); $manplugin->unenrol_user($manualenrol, $user->id); $this->assertEquals(0, $DB->count_records('hsuforum_digests')); $this->assertEquals(0, $DB->count_records('hsuforum_subscriptions')); $this->assertEquals(0, $DB->count_records('hsuforum_track_prefs')); $this->assertEquals(0, $DB->count_records('hsuforum_read')); }
/** * Sends several notifications to one user as: * * daily digests coming from the per-forum setting. */ public function test_cron_forum_digest_email() { global $DB, $CFG; $this->resetAfterTest(true); // Set up a basic user enrolled in a course. $userhelper = $this->helper_setup_user_in_course(); $user = $userhelper->user; $course1 = $userhelper->courses->course1; $forum1 = $userhelper->forums->forum1; $forum2 = $userhelper->forums->forum2; // Add a discussion to the forums. $record = new stdClass(); $record->course = $course1->id; $record->userid = $user->id; $record->mailnow = 1; // Add 5 discussions to forum 1. $record->forum = $forum1->id; for ($i = 0; $i < 5; $i++) { $this->getDataGenerator()->get_plugin_generator('mod_hsuforum')->create_discussion($record); } // Add 5 discussions to forum 2. $record->forum = $forum2->id; for ($i = 0; $i < 5; $i++) { $this->getDataGenerator()->get_plugin_generator('mod_hsuforum')->create_discussion($record); } // Ensure that the creation times mean that the messages will be sent. $this->helper_force_digest_mail_times(); // Set the tested user's default maildigest setting. $DB->set_field('user', 'maildigest', 0, array('id' => $user->id)); // Set the maildigest preference for forum1 to digest (complete). hsuforum_set_user_maildigest($forum1, 1, $user); // Set the maildigest preference for forum2 to digest (short). hsuforum_set_user_maildigest($forum2, 2, $user); // One digest e-mail should be sent, and no individual notifications. $this->helper_run_cron_check_count(1, 0, 1); }
$backtoindex = optional_param('backtoindex', 0, PARAM_INT); // We must have a valid session key. require_sesskey(); $forum = $DB->get_record('hsuforum', array('id' => $id)); $course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST); $cm = get_coursemodule_from_instance('hsuforum', $forum->id, $course->id, false, MUST_EXIST); $context = context_module::instance($cm->id); require_login($course, false, $cm); $url = new moodle_url('/mod/hsuforum/maildigest.php', array('id' => $id, 'maildigest' => $maildigest)); $PAGE->set_url($url); $PAGE->set_context($context); $digestoptions = hsuforum_get_user_digest_options(); $info = new stdClass(); $info->name = fullname($USER); $info->forum = format_string($forum->name); hsuforum_set_user_maildigest($forum, $maildigest); $info->maildigest = $maildigest; if ($maildigest === -1) { // Get the default maildigest options. $info->maildigest = $USER->maildigest; $info->maildigesttitle = $digestoptions[$info->maildigest]; $info->maildigestdescription = get_string('emaildigest_' . $info->maildigest, 'mod_hsuforum', $info); $updatemessage = get_string('emaildigestupdated_default', 'hsuforum', $info); } else { $info->maildigesttitle = $digestoptions[$info->maildigest]; $info->maildigestdescription = get_string('emaildigest_' . $info->maildigest, 'mod_hsuforum', $info); $updatemessage = get_string('emaildigestupdated', 'hsuforum', $info); } if ($backtoindex) { $returnto = "index.php?id={$course->id}"; } else {