Example #1
0
 /**
  * Sends several notifications to one user as:
  * * daily digests coming from the per-twf setting.
  */
 public function test_cron_twf_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;
     $twf1 = $userhelper->twfs->twf1;
     $twf2 = $userhelper->twfs->twf2;
     // Add a discussion to the twfs.
     $record = new stdClass();
     $record->course = $course1->id;
     $record->userid = $user->id;
     $record->mailnow = 1;
     // Add 5 discussions to twf 1.
     $record->twf = $twf1->id;
     for ($i = 0; $i < 5; $i++) {
         $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_discussion($record);
     }
     // Add 5 discussions to twf 2.
     $record->twf = $twf2->id;
     for ($i = 0; $i < 5; $i++) {
         $this->getDataGenerator()->get_plugin_generator('mod_twf')->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 twf1 to digest (complete).
     twf_set_user_maildigest($twf1, 1, $user);
     // Set the maildigest preference for twf2 to digest (short).
     twf_set_user_maildigest($twf2, 2, $user);
     // One digest e-mail should be sent, and no individual notifications.
     $this->helper_run_cron_check_count(1, 0, 1);
 }
Example #2
0
$backtoindex = optional_param('backtoindex', 0, PARAM_INT);
// We must have a valid session key.
require_sesskey();
$twf = $DB->get_record('twf', array('id' => $id));
$course = $DB->get_record('course', array('id' => $twf->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('twf', $twf->id, $course->id, false, MUST_EXIST);
$context = context_module::instance($cm->id);
require_login($course, false, $cm);
$url = new moodle_url('/mod/twf/maildigest.php', array('id' => $id, 'maildigest' => $maildigest));
$PAGE->set_url($url);
$PAGE->set_context($context);
$digestoptions = twf_get_user_digest_options();
$info = new stdClass();
$info->name = fullname($USER);
$info->twf = format_string($twf->name);
twf_set_user_maildigest($twf, $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_twf', $info);
    $updatemessage = get_string('emaildigestupdated_default', 'twf', $info);
} else {
    $info->maildigesttitle = $digestoptions[$info->maildigest];
    $info->maildigestdescription = get_string('emaildigest_' . $info->maildigest, 'mod_twf', $info);
    $updatemessage = get_string('emaildigestupdated', 'twf', $info);
}
if ($backtoindex) {
    $returnto = "index.php?id={$course->id}";
} else {
Example #3
0
 /**
  * Test mod_twf_observer methods.
  */
 public function test_observers()
 {
     global $DB, $CFG;
     require_once $CFG->dirroot . '/mod/twf/lib.php';
     $twfgen = $this->getDataGenerator()->get_plugin_generator('mod_twf');
     $course = $this->getDataGenerator()->create_course();
     $trackedrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
     $untrackedrecord = array('course' => $course->id, 'type' => 'general');
     $trackedtwf = $this->getDataGenerator()->create_module('twf', $trackedrecord);
     $untrackedtwf = $this->getDataGenerator()->create_module('twf', $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, 'tracktwfs' => 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 twf_subscriptions record.
     $manplugin->enrol_user($manualenrol, $user->id, $student->id);
     // They are not required, but in a real environment they are supposed to be required;
     // adding them just in case there are APIs changes in future.
     set_config('twf_trackingtype', 1);
     set_config('twf_trackreadposts', 1);
     $record = array();
     $record['course'] = $course->id;
     $record['twf'] = $trackedtwf->id;
     $record['userid'] = $user->id;
     $discussion = $twfgen->create_discussion($record);
     $record = array();
     $record['discussion'] = $discussion->id;
     $record['userid'] = $user->id;
     $post = $twfgen->create_post($record);
     twf_tp_add_read_record($user->id, $post->id);
     twf_set_user_maildigest($trackedtwf, 2, $user);
     twf_tp_stop_tracking($untrackedtwf->id, $user->id);
     $this->assertEquals(1, $DB->count_records('twf_subscriptions'));
     $this->assertEquals(1, $DB->count_records('twf_digests'));
     $this->assertEquals(1, $DB->count_records('twf_track_prefs'));
     $this->assertEquals(1, $DB->count_records('twf_read'));
     // The course_module_created observer does it's job adding a subscription.
     $twfrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
     $extratwf = $this->getDataGenerator()->create_module('twf', $twfrecord);
     $this->assertEquals(2, $DB->count_records('twf_subscriptions'));
     $manplugin->unenrol_user($manualenrol, $user->id);
     $this->assertEquals(0, $DB->count_records('twf_digests'));
     $this->assertEquals(0, $DB->count_records('twf_subscriptions'));
     $this->assertEquals(0, $DB->count_records('twf_track_prefs'));
     $this->assertEquals(0, $DB->count_records('twf_read'));
 }