/** * Returns an array of forums that the current user is subscribed to and is allowed to unsubscribe from * * @return array An array of unsubscribable forums * @deprecated since Moodle 2.8 use \mod_forum\subscriptions::get_unsubscribable_forums() instead */ function forum_get_optional_subscribed_forums() { debugging("forum_get_optional_subscribed_forums() has been deprecated, please use \\mod_forum\\subscriptions::get_unsubscribable_forums() instead.", DEBUG_DEVELOPER); return \mod_forum\subscriptions::get_unsubscribable_forums(); }
/** * Test fetching unsubscribable forums. */ public function test_unsubscribable_forums() { global $DB; $this->resetAfterTest(true); // Create a course, with a forum. $course = $this->getDataGenerator()->create_course(); // Create a user enrolled in the course as a student. list($user) = $this->helper_create_users($course, 1); // Must be logged in as the current user. $this->setUser($user); // Without any subscriptions, there should be nothing returned. $result = \mod_forum\subscriptions::get_unsubscribable_forums(); $this->assertEquals(0, count($result)); // Create the forums. $options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE); $forceforum = $this->getDataGenerator()->create_module('forum', $options); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_DISALLOWSUBSCRIBE); $disallowforum = $this->getDataGenerator()->create_module('forum', $options); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); $chooseforum = $this->getDataGenerator()->create_module('forum', $options); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE); $initialforum = $this->getDataGenerator()->create_module('forum', $options); // At present the user is only subscribed to the initial forum. $result = \mod_forum\subscriptions::get_unsubscribable_forums(); $this->assertEquals(1, count($result)); // Ensure that the user is enrolled in all of the forums except force subscribed. \mod_forum\subscriptions::subscribe_user($user->id, $disallowforum); \mod_forum\subscriptions::subscribe_user($user->id, $chooseforum); $result = \mod_forum\subscriptions::get_unsubscribable_forums(); $this->assertEquals(3, count($result)); // Hide the forums. set_coursemodule_visible($forceforum->cmid, 0); set_coursemodule_visible($disallowforum->cmid, 0); set_coursemodule_visible($chooseforum->cmid, 0); set_coursemodule_visible($initialforum->cmid, 0); $result = \mod_forum\subscriptions::get_unsubscribable_forums(); $this->assertEquals(0, count($result)); // Add the moodle/course:viewhiddenactivities capability to the student user. $roleids = $DB->get_records_menu('role', null, '', 'shortname, id'); $context = \context_course::instance($course->id); assign_capability('moodle/course:viewhiddenactivities', CAP_ALLOW, $roleids['student'], $context); $context->mark_dirty(); // All of the unsubscribable forums should now be listed. $result = \mod_forum\subscriptions::get_unsubscribable_forums(); $this->assertEquals(3, count($result)); }
$strunsubscribeall = get_string('unsubscribeall', 'forum'); $PAGE->navbar->add(get_string('modulename', 'forum')); $PAGE->navbar->add($strunsubscribeall); $PAGE->set_title($strunsubscribeall); $PAGE->set_heading($COURSE->fullname); echo $OUTPUT->header(); echo $OUTPUT->heading($strunsubscribeall); if (data_submitted() and $confirm and confirm_sesskey()) { $forums = \mod_forum\subscriptions::get_unsubscribable_forums(); foreach ($forums as $forum) { \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, context_module::instance($forum->cm), true); } $DB->set_field('user', 'autosubscribe', 0, array('id' => $USER->id)); echo $OUTPUT->box(get_string('unsubscribealldone', 'forum')); echo $OUTPUT->continue_button($return); echo $OUTPUT->footer(); die; } else { $a = count(\mod_forum\subscriptions::get_unsubscribable_forums()); if ($a) { $msg = get_string('unsubscribeallconfirm', 'forum', $a); echo $OUTPUT->confirm($msg, new moodle_url('unsubscribeall.php', array('confirm' => 1)), $return); echo $OUTPUT->footer(); die; } else { echo $OUTPUT->box(get_string('unsubscribeallempty', 'forum')); echo $OUTPUT->continue_button($return); echo $OUTPUT->footer(); die; } }