print_error('invalidcoursemodule'); } if (!($cm = get_coursemodule_from_instance("twf", $twf->id, $course->id))) { print_error('invalidcoursemodule'); } require_login($course, false, $cm); $returnto = twf_go_back_to($returnpage . '?id=' . $course->id . '&f=' . $twf->id); if (!twf_tp_can_track_twfs($twf)) { redirect($returnto); } $info = new stdClass(); $info->name = fullname($USER); $info->twf = format_string($twf->name); $eventparams = array('context' => context_module::instance($cm->id), 'relateduserid' => $USER->id, 'other' => array('twfid' => $twf->id)); if (twf_tp_is_tracked($twf)) { if (twf_tp_stop_tracking($twf->id)) { $event = \mod_twf\event\readtracking_disabled::create($eventparams); $event->trigger(); redirect($returnto, get_string("nownottracking", "twf", $info), 1); } else { print_error('cannottrack', '', get_local_referer(false)); } } else { // subscribe if (twf_tp_start_tracking($twf->id)) { $event = \mod_twf\event\readtracking_enabled::create($eventparams); $event->trigger(); redirect($returnto, get_string("nowtracking", "twf", $info), 1); } else { print_error('cannottrack', '', get_local_referer(false)); }
/** * 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')); }
/** * Test the logic in the test_twf_tp_get_untracked_twfs() function. */ public function test_twf_tp_get_untracked_twfs() { global $CFG; $this->resetAfterTest(); $useron = $this->getDataGenerator()->create_user(array('tracktwfs' => 1)); $useroff = $this->getDataGenerator()->create_user(array('tracktwfs' => 0)); $course = $this->getDataGenerator()->create_course(); $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OFF); // Off. $twfoff = $this->getDataGenerator()->create_module('twf', $options); $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED); // On. $twfforce = $this->getDataGenerator()->create_module('twf', $options); $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL); // Optional. $twfoptional = $this->getDataGenerator()->create_module('twf', $options); // Allow force. $CFG->twf_allowforcedreadtracking = 1; // On user with force on. $result = twf_tp_get_untracked_twfs($useron->id, $course->id); $this->assertEquals(1, count($result)); $this->assertEquals(true, isset($result[$twfoff->id])); // Off user with force on. $result = twf_tp_get_untracked_twfs($useroff->id, $course->id); $this->assertEquals(2, count($result)); $this->assertEquals(true, isset($result[$twfoff->id])); $this->assertEquals(true, isset($result[$twfoptional->id])); // Don't allow force. $CFG->twf_allowforcedreadtracking = 0; // On user with force off. $result = twf_tp_get_untracked_twfs($useron->id, $course->id); $this->assertEquals(1, count($result)); $this->assertEquals(true, isset($result[$twfoff->id])); // Off user with force off. $result = twf_tp_get_untracked_twfs($useroff->id, $course->id); $this->assertEquals(3, count($result)); $this->assertEquals(true, isset($result[$twfoff->id])); $this->assertEquals(true, isset($result[$twfoptional->id])); $this->assertEquals(true, isset($result[$twfforce->id])); // Stop tracking so we can test again. twf_tp_stop_tracking($twfforce->id, $useron->id); twf_tp_stop_tracking($twfoptional->id, $useron->id); twf_tp_stop_tracking($twfforce->id, $useroff->id); twf_tp_stop_tracking($twfoptional->id, $useroff->id); // Allow force. $CFG->twf_allowforcedreadtracking = 1; // On user with force on. $result = twf_tp_get_untracked_twfs($useron->id, $course->id); $this->assertEquals(2, count($result)); $this->assertEquals(true, isset($result[$twfoff->id])); $this->assertEquals(true, isset($result[$twfoptional->id])); // Off user with force on. $result = twf_tp_get_untracked_twfs($useroff->id, $course->id); $this->assertEquals(2, count($result)); $this->assertEquals(true, isset($result[$twfoff->id])); $this->assertEquals(true, isset($result[$twfoptional->id])); // Don't allow force. $CFG->twf_allowforcedreadtracking = 0; // On user with force off. $result = twf_tp_get_untracked_twfs($useron->id, $course->id); $this->assertEquals(3, count($result)); $this->assertEquals(true, isset($result[$twfoff->id])); $this->assertEquals(true, isset($result[$twfoptional->id])); $this->assertEquals(true, isset($result[$twfforce->id])); // Off user with force off. $result = twf_tp_get_untracked_twfs($useroff->id, $course->id); $this->assertEquals(3, count($result)); $this->assertEquals(true, isset($result[$twfoff->id])); $this->assertEquals(true, isset($result[$twfoptional->id])); $this->assertEquals(true, isset($result[$twfforce->id])); }