public function test_subscription_modes() { global $DB; $this->resetAfterTest(true); // Create a course, with a twf. $course = $this->getDataGenerator()->create_course(); $options = array('course' => $course->id); $twf = $this->getDataGenerator()->create_module('twf', $options); \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_FORCESUBSCRIBE); $twf = $DB->get_record('twf', array('id' => $twf->id)); $this->assertEquals(FORUM_FORCESUBSCRIBE, \mod_twf\subscriptions::get_subscription_mode($twf)); $this->assertTrue(\mod_twf\subscriptions::is_forcesubscribed($twf)); $this->assertFalse(\mod_twf\subscriptions::is_subscribable($twf)); $this->assertFalse(\mod_twf\subscriptions::subscription_disabled($twf)); \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_DISALLOWSUBSCRIBE); $twf = $DB->get_record('twf', array('id' => $twf->id)); $this->assertEquals(FORUM_DISALLOWSUBSCRIBE, \mod_twf\subscriptions::get_subscription_mode($twf)); $this->assertTrue(\mod_twf\subscriptions::subscription_disabled($twf)); $this->assertFalse(\mod_twf\subscriptions::is_subscribable($twf)); $this->assertFalse(\mod_twf\subscriptions::is_forcesubscribed($twf)); \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_INITIALSUBSCRIBE); $twf = $DB->get_record('twf', array('id' => $twf->id)); $this->assertEquals(FORUM_INITIALSUBSCRIBE, \mod_twf\subscriptions::get_subscription_mode($twf)); $this->assertTrue(\mod_twf\subscriptions::is_subscribable($twf)); $this->assertFalse(\mod_twf\subscriptions::subscription_disabled($twf)); $this->assertFalse(\mod_twf\subscriptions::is_forcesubscribed($twf)); \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_CHOOSESUBSCRIBE); $twf = $DB->get_record('twf', array('id' => $twf->id)); $this->assertEquals(FORUM_CHOOSESUBSCRIBE, \mod_twf\subscriptions::get_subscription_mode($twf)); $this->assertTrue(\mod_twf\subscriptions::is_subscribable($twf)); $this->assertFalse(\mod_twf\subscriptions::subscription_disabled($twf)); $this->assertFalse(\mod_twf\subscriptions::is_forcesubscribed($twf)); }
} // Can proceed now, the user is not guest and is enrolled foreach ($modinfo->get_instances_of('twf') as $twfid => $cm) { $twf = $twfs[$twfid]; $modcontext = context_module::instance($cm->id); $cansub = false; if (has_capability('mod/twf:viewdiscussion', $modcontext)) { $cansub = true; } if ($cansub && $cm->visible == 0 && !has_capability('mod/twf:managesubscriptions', $modcontext)) { $cansub = false; } if (!\mod_twf\subscriptions::is_forcesubscribed($twf)) { $subscribed = \mod_twf\subscriptions::is_subscribed($USER->id, $twf, null, $cm); $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id); if (($canmanageactivities || \mod_twf\subscriptions::is_subscribable($twf)) && $subscribe && !$subscribed && $cansub) { \mod_twf\subscriptions::subscribe_user($USER->id, $twf, $modcontext, true); } else { if (!$subscribe && $subscribed) { \mod_twf\subscriptions::unsubscribe_user($USER->id, $twf, $modcontext, true); } } } } $returnto = twf_go_back_to("index.php?id={$course->id}"); $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id))); if ($subscribe) { redirect($returnto, get_string('nowallsubscribed', 'twf', $shortname), 1); } else { redirect($returnto, get_string('nowallunsubscribed', 'twf', $shortname), 1); }
$node->display = false; if ($node && $post->id != $discussion->firstpost) { $node->add(format_string($post->subject), $PAGE->url); } $PAGE->set_title("{$course->shortname}: " . format_string($discussion->name)); $PAGE->set_heading($course->fullname); $PAGE->set_button($searchform); $renderer = $PAGE->get_renderer('mod_twf'); echo $OUTPUT->header(); echo $OUTPUT->heading(format_string($twf->name), 2); echo $OUTPUT->heading(format_string($discussion->name), 3, 'discussionname'); // is_guest should be used here as this also checks whether the user is a guest in the current course. // Guests and visitors cannot subscribe - only enrolled users. if (!is_guest($modcontext, $USER) && isloggedin() && has_capability('mod/twf:viewdiscussion', $modcontext)) { // Discussion subscription. if (\mod_twf\subscriptions::is_subscribable($twf)) { echo html_writer::div(twf_get_discussion_subscription_icon($twf, $post->discussion, null, true), 'discussionsubscription'); echo twf_get_discussion_subscription_icon_preloaders(); } } /// Check to see if groups are being used in this twf /// If so, make sure the current person is allowed to see this discussion /// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons $canreply = twf_user_can_post($twf, $discussion, $USER, $cm, $course, $modcontext); if (!$canreply and $twf->type !== 'news') { if (isguestuser() or !isloggedin()) { $canreply = true; } if (!is_enrolled($modcontext) and !is_viewing($modcontext)) { // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link // normal users with temporary guest access see this link too, they are asked to enrol instead
/** * Prints the discussion view screen for a twf. * * @global object * @global object * @param object $course The current course object. * @param object $twf Forum to be printed. * @param int $maxdiscussions . * @param string $displayformat The display format to use (optional). * @param string $sort Sort arguments for database query (optional). * @param int $groupmode Group mode of the twf (optional). * @param void $unused (originally current group) * @param int $page Page mode, page to display (optional). * @param int $perpage The maximum number of discussions per page(optional) * @param boolean $subscriptionstatus Whether the user is currently subscribed to the discussion in some fashion. * */ function twf_print_latest_discussions($course, $twf, $maxdiscussions = -1, $displayformat = 'plain', $sort = '', $currentgroup = -1, $groupmode = -1, $page = -1, $perpage = 100, $cm = null) { global $CFG, $USER, $OUTPUT; //return; if (!$cm) { if (!($cm = get_coursemodule_from_instance('twf', $twf->id, $twf->course))) { print_error('invalidcoursemodule'); } } $context = context_module::instance($cm->id); if (empty($sort)) { $sort = "d.timemodified DESC"; } $olddiscussionlink = false; // Sort out some defaults if ($perpage <= 0) { $perpage = 0; $page = -1; } if ($maxdiscussions == 0) { // all discussions - backwards compatibility $page = -1; $perpage = 0; if ($displayformat == 'plain') { $displayformat = 'header'; // Abbreviate display by default } } else { if ($maxdiscussions > 0) { $page = -1; $perpage = $maxdiscussions; } } $fullpost = false; if ($displayformat == 'plain') { $fullpost = true; } // Decide if current user is allowed to see ALL the current discussions or not // First check the group stuff if ($currentgroup == -1 or $groupmode == -1) { $groupmode = groups_get_activity_groupmode($cm, $course); $currentgroup = groups_get_activity_group($cm); } $groups = array(); //cache // If the user can post discussions, then this is a good place to put the // button for it. We do not show the button if we are showing site news // and the current user is a guest. $canstart = twf_user_can_post_discussion($twf, $currentgroup, $groupmode, $cm, $context); if (!$canstart and $twf->type !== 'news') { if (isguestuser() or !isloggedin()) { $canstart = true; } if (!is_enrolled($context) and !is_viewing($context)) { // allow guests and not-logged-in to see the button - they are prompted to log in after clicking the link // normal users with temporary guest access see this button too, they are asked to enrol instead // do not show the button to users with suspended enrolments here $canstart = enrol_selfenrol_available($course->id); } } if ($canstart) { echo '<div class="singlebutton twfaddnew">'; echo "<form id=\"newdiscussionform\" method=\"get\" action=\"{$CFG->wwwroot}/mod/twf/post.php\">"; echo '<div>'; echo "<input type=\"hidden\" name=\"twf\" value=\"{$twf->id}\" />"; switch ($twf->type) { case 'news': case 'blog': $buttonadd = get_string('addanewtopic', 'twf'); break; case 'qanda': $buttonadd = get_string('addanewquestion', 'twf'); break; default: $buttonadd = get_string('addanewdiscussion', 'twf'); break; } echo '<input type="submit" value="' . $buttonadd . '" />'; echo '</div>'; echo '</form>'; echo "</div>\n"; } else { if (isguestuser() or !isloggedin() or $twf->type == 'news' or $twf->type == 'qanda' and !has_capability('mod/twf:addquestion', $context) or $twf->type != 'qanda' and !has_capability('mod/twf:startdiscussion', $context)) { // no button and no info } else { if ($groupmode and !has_capability('moodle/site:accessallgroups', $context)) { // inform users why they can not post new discussion if (!$currentgroup) { echo $OUTPUT->notification(get_string('cannotadddiscussionall', 'twf')); } else { if (!groups_is_member($currentgroup)) { echo $OUTPUT->notification(get_string('cannotadddiscussion', 'twf')); } } } } } // Get all the recent discussions we're allowed to see $getuserlastmodified = $displayformat == 'header'; if (!($discussions = twf_get_discussions($cm, $sort, $fullpost, null, $maxdiscussions, $getuserlastmodified, $page, $perpage))) { echo '<div class="twfnodiscuss">'; if ($twf->type == 'news') { echo '(' . get_string('nonews', 'twf') . ')'; } else { if ($twf->type == 'qanda') { echo '(' . get_string('noquestions', 'twf') . ')'; } else { echo '(' . get_string('nodiscussions', 'twf') . ')'; } } echo "</div>\n"; return; } // If we want paging if ($page != -1) { ///Get the number of discussions found $numdiscussions = twf_get_discussions_count($cm); ///Show the paging bar echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f={$twf->id}"); if ($numdiscussions > 1000) { // saves some memory on sites with very large twfs $replies = twf_count_discussion_replies($twf->id, $sort, $maxdiscussions, $page, $perpage); } else { $replies = twf_count_discussion_replies($twf->id); } } else { $replies = twf_count_discussion_replies($twf->id); if ($maxdiscussions > 0 and $maxdiscussions <= count($discussions)) { $olddiscussionlink = true; } } $canviewparticipants = has_capability('moodle/course:viewparticipants', $context); $strdatestring = get_string('strftimerecentfull'); // Check if the twf is tracked. if ($cantrack = twf_tp_can_track_twfs($twf)) { $twftracked = twf_tp_is_tracked($twf); } else { $twftracked = false; } if ($twftracked) { $unreads = twf_get_discussions_unread($cm); } else { $unreads = array(); } if ($displayformat == 'header') { echo '<table cellspacing="0" class="forumheaderlist">'; echo '<thead>'; echo '<tr>'; echo '<th class="header topic" scope="col">' . get_string('discussion', 'twf') . '</th>'; echo '<th class="header author" colspan="2" scope="col">' . get_string('startedby', 'twf') . '</th>'; if ($groupmode > 0) { echo '<th class="header group" scope="col">' . get_string('group') . '</th>'; } if (has_capability('mod/twf:viewdiscussion', $context)) { echo '<th class="header replies" scope="col">' . get_string('replies', 'twf') . '</th>'; // If the twf can be tracked, display the unread column. if ($cantrack) { echo '<th class="header replies" scope="col">' . get_string('unread', 'twf'); if ($twftracked) { echo '<a title="' . get_string('markallread', 'twf') . '" href="' . $CFG->wwwroot . '/mod/twf/markposts.php?f=' . $twf->id . '&mark=read&returnpage=view.php">' . '<img src="' . $OUTPUT->pix_url('t/markasread') . '" class="iconsmall" alt="' . get_string('markallread', 'twf') . '" /></a>'; } echo '</th>'; } } echo '<th class="header lastpost" scope="col">' . get_string('lastpost', 'twf') . '</th>'; if (!is_guest($context, $USER) && isloggedin() && has_capability('mod/twf:viewdiscussion', $context)) { if (\mod_twf\subscriptions::is_subscribable($twf)) { echo '<th class="header discussionsubscription" scope="col">'; echo twf_get_discussion_subscription_icon_preloaders(); echo '</th>'; } } echo '</tr>'; echo '</thead>'; echo '<tbody>'; } foreach ($discussions as $discussion) { if ($twf->type == 'qanda' && !has_capability('mod/twf:viewqandawithoutposting', $context) && !twf_user_has_posted($twf->id, $discussion->discussion, $USER->id)) { $canviewparticipants = false; } if (!empty($replies[$discussion->discussion])) { $discussion->replies = $replies[$discussion->discussion]->replies; $discussion->lastpostid = $replies[$discussion->discussion]->lastpostid; } else { $discussion->replies = 0; } // SPECIAL CASE: The front page can display a news item post to non-logged in users. // All posts are read in this case. if (!$twftracked) { $discussion->unread = '-'; } else { if (empty($USER)) { $discussion->unread = 0; } else { if (empty($unreads[$discussion->discussion])) { $discussion->unread = 0; } else { $discussion->unread = $unreads[$discussion->discussion]; } } } if (isloggedin()) { $ownpost = $discussion->userid == $USER->id; } else { $ownpost = false; } // Use discussion name instead of subject of first post $discussion->subject = $discussion->name; switch ($displayformat) { case 'header': if ($groupmode > 0) { if (isset($groups[$discussion->groupid])) { $group = $groups[$discussion->groupid]; } else { $group = $groups[$discussion->groupid] = groups_get_group($discussion->groupid); } } else { $group = -1; } twf_print_discussion_header($discussion, $twf, $group, $strdatestring, $cantrack, $twftracked, $canviewparticipants, $context); break; default: $link = false; if ($discussion->replies) { $link = true; } else { $modcontext = context_module::instance($cm->id); $link = twf_user_can_see_discussion($twf, $discussion, $modcontext, $USER); } $discussion->twf = $twf->id; twf_print_post($discussion, $discussion, $twf, $cm, $course, $ownpost, 0, $link, false, '', null, true, $twftracked); break; } } if ($displayformat == "header") { echo '</tbody>'; echo '</table>'; } if ($olddiscussionlink) { if ($twf->type == 'news') { $strolder = get_string('oldertopics', 'twf'); } else { $strolder = get_string('olderdiscussions', 'twf'); } echo '<div class="forumolddiscuss">'; echo '<a href="' . $CFG->wwwroot . '/mod/twf/view.php?f=' . $twf->id . '&showall=1">'; echo $strolder . '</a> ...</div>'; } if ($page != -1) { ///Show the paging bar echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f={$twf->id}"); } }