/**
  * Display subscribe options.
  * @param forum $forum Forum
  * @param string $text Textual note
  * @param int $subscribed
  * @param bool $button True if subscribe/unsubscribe button should be shown
  * @param bool $viewlink True if 'view subscribers' link should be shown
  * @return string HTML code for this area
  */
 public function display_subscribe_options($forum, $text, $subscribed, $button, $viewlink)
 {
     $out = '<div class="forumng-subscribe-options">' . '<h3>' . get_string('subscription', 'forumng') . '</h3>' . '<p>' . $text . '</p>';
     $cm = $forum->get_course_module();
     if ($button) {
         $outsubmit = '';
         $currentgroupid = forum::get_activity_group($cm, true);
         if ($currentgroupid == forum::NO_GROUPS) {
             $currentgroupid = 0;
         }
         if ($subscribed == forum::FULLY_SUBSCRIBED || $subscribed == forum::FULLY_SUBSCRIBED_GROUPMODE) {
             $outsubmit .= '<input type="submit" name="submitunsubscribe" value="' . get_string('unsubscribeshort', 'forumng') . '" />';
         } else {
             if ($subscribed == forum::PARTIALLY_SUBSCRIBED) {
                 //print both subscribe button and unsubscribe button
                 $outsubmit .= '<input type="submit" name="submitsubscribe" value="' . get_string('subscribelong', 'forumng') . '" />' . '<input type="submit" name="submitunsubscribe" value="' . get_string('unsubscribelong', 'forumng') . '" />';
             } else {
                 if ($subscribed == forum::NOT_SUBSCRIBED) {
                     //default unsubscribed, print subscribe button
                     $outsubmit .= '<input type="submit" name="submitsubscribe" value="' . get_string('subscribeshort', 'forumng') . '" />';
                 } else {
                     if ($subscribed == forum::THIS_GROUP_PARTIALLY_SUBSCRIBED) {
                         $outsubmit .= '<input type="submit" name="submitsubscribe_thisgroup" value="' . get_string('subscribegroup', 'forumng') . '" />' . '<input type="submit" name="submitunsubscribe_thisgroup" value="' . get_string('unsubscribegroup_partial', 'forumng') . '" />' . '<input type="hidden" name="g" value="' . $currentgroupid . '" />';
                     } else {
                         if ($subscribed == forum::THIS_GROUP_SUBSCRIBED) {
                             $outsubmit .= '<input type="submit" name="submitunsubscribe_thisgroup" value="' . get_string('unsubscribegroup', 'forumng') . '" />' . '<input type="hidden" name="g" value="' . $currentgroupid . '" />';
                         } else {
                             if ($subscribed == forum::THIS_GROUP_NOT_SUBSCRIBED) {
                                 $outsubmit .= '<input type="submit" name="submitsubscribe_thisgroup" value="' . get_string('subscribegroup', 'forumng') . '" />' . '<input type="hidden" name="g" value="' . $currentgroupid . '" />';
                             }
                         }
                     }
                 }
             }
         }
         $out .= '<form action="subscribe.php" method="post"><div>' . $forum->get_link_params(forum::PARAM_FORM) . '<input type="hidden" name="back" value="view" />' . $outsubmit . '</div></form>';
     }
     if ($viewlink) {
         $out .= ' <div class="forumng-subscribe-admin">' . '<a href="subscribers.php?' . $forum->get_link_params(forum::PARAM_HTML) . '">' . get_string('viewsubscribers', 'forumng') . '</a></div>';
     }
     $out .= '</div>';
     return $out;
 }
 /**
  * Displays the view page (usually showing a list of discussions).
  * @param forum $forum Forum
  * @param int $groupid Group ID
  */
 public function print_view_page($forum, $groupid)
 {
     global $SESSION;
     $forumid = $forum->get_id();
     $baseurl = 'view.php?' . $forum->get_link_params(forum::PARAM_PLAIN);
     if (isset($SESSION->forumng_discussionlist[$forumid]) && property_exists($SESSION->forumng_discussionlist[$forumid], 'groupid') && $SESSION->forumng_discussionlist[$forumid]->groupid != $groupid) {
         unset($SESSION->forumng_discussionlist[$forumid]->page);
         unset($SESSION->forumng_discussionlist[$forumid]->groupid);
     }
     //Remember the sort order and page number in session variables
     //Unset the page session variable when the sort links are clicked
     // or groupid has been changed (using the group dropdown box)
     $sortorder = optional_param('sort', '', PARAM_ALPHA);
     if (!$sortorder) {
         if (isset($SESSION->forumng_discussionlist[$forumid]->sort)) {
             $sortorder = $SESSION->forumng_discussionlist[$forumid]->sort;
         } else {
             $sortorder = 'd';
         }
     } else {
         if (optional_param('sortlink', '', PARAM_ALPHA)) {
             $SESSION->forumng_discussionlist[$forumid]->sort = $sortorder;
             unset($SESSION->forumng_discussionlist[$forumid]->page);
         }
     }
     $page = optional_param('page', 0, PARAM_INT);
     if (!$page) {
         if (isset($SESSION->forumng_discussionlist[$forumid]->page)) {
             $page = $SESSION->forumng_discussionlist[$forumid]->page;
         } else {
             $page = 1;
         }
     } else {
         $SESSION->forumng_discussionlist[$forumid]->page = $page;
         $SESSION->forumng_discussionlist[$forumid]->groupid = $groupid;
     }
     $baseurl .= '&page=' . $page;
     $sortchar = substr($sortorder, 0, 1);
     if (strlen($sortorder) == 2) {
         $sortreverse = substr($sortorder, 1, 1) == 'r' ? true : false;
     } else {
         $sortreverse = false;
     }
     $baseurl .= '&sort=' . $sortchar;
     $baseurl .= $sortreverse ? 'r' : '';
     $sort = forum::get_sort_code($sortchar);
     $list = $forum->get_discussion_list($groupid, $forum->can_view_hidden(), $forum->can_manage_discussions(), $page, $sort, $sortreverse);
     $sticky = $list->get_sticky_discussions();
     $normal = $list->get_normal_discussions();
     // Remove discussions from list if the forumtype thinks we can't see
     // them
     foreach ($sticky as $key => $value) {
         if (!$this->can_view_discussion($value)) {
             unset($sticky[$key]);
         }
     }
     foreach ($normal as $key => $value) {
         if (!$this->can_view_discussion($value)) {
             unset($normal[$key]);
         }
     }
     // Intro
     print $forum->display_intro();
     // Draft posts
     $drafts = $forum->get_drafts();
     if (count($drafts) > 0) {
         print $forum->display_draft_list_start();
         foreach ($drafts as $draft) {
             print $draft->display_draft_list_item($forum, $draft == end($drafts));
         }
         print $forum->display_draft_list_end();
     }
     //print info about the start and end dtates of the forum from the form setting;
     $stringend = has_capability('mod/forumng:ignorepostlimits', $forum->get_context()) ? 'capable' : '';
     $startdate = $forum->get_postingfrom();
     $enddate = $forum->get_postinguntil();
     // Before start date
     if (time() < $startdate) {
         $message = get_string('beforestartdate' . $stringend, 'forumng', forum_utils::display_date($startdate));
         print "<div class='forumng-show-dates'>{$message}</div>";
     } else {
         if (time() < $enddate) {
             $message = get_string('beforeenddate' . $stringend, 'forumng', forum_utils::display_date($enddate));
             print "<div class='forumng-show-dates'>{$message}</div>";
         }
     }
     // After end date
     if ($enddate && time() >= $enddate) {
         $message = get_string('afterenddate' . $stringend, 'forumng', forum_utils::display_date($enddate));
         print "<div class='forumng-show-dates'>{$message}</div>";
     }
     // Post button - temporarily disabled when in all-groups mode
     print $groupid == NULL ? '' : $forum->display_post_button($groupid);
     print $list->display_paging_bar($baseurl);
     if (count($sticky) + count($normal) > 0) {
         print $forum->display_discussion_list_start($groupid, $baseurl, $sort, $sortreverse);
         foreach ($sticky as $discussion) {
             print $discussion->display_discussion_list_item($groupid);
         }
         if (count($sticky) > 0 && count($normal) > 0) {
             print $forum->display_discussion_list_divider($groupid);
         }
         foreach ($normal as $discussion) {
             print $discussion->display_discussion_list_item($groupid, $discussion == end($normal));
         }
         print $forum->display_discussion_list_end($groupid);
     } else {
         print '<p class="forumng-nodiscussions">' . $this->get_string($forum, 'nodiscussions') . '</p>';
     }
     print $list->display_paging_bar($baseurl);
     print $forum->display_discussion_list_features($groupid);
     // Flagged posts
     $flagged = $forum->get_flagged_posts();
     if (count($flagged) > 0) {
         print $forum->display_flagged_list_start();
         foreach ($flagged as $post) {
             print $post->display_flagged_list_item($post === end($flagged));
         }
         print $forum->display_flagged_list_end();
     }
     // Subscribe and view subscribers links
     print $forum->display_subscribe_options();
     // Atom/RSS links
     print $forum->display_feed_links($groupid);
     // display the warning message for invalid archive setting
     print $forum->display_archive_warning();
     // Display sharing information
     print $forum->display_sharing_info();
 }
 /**
  * Updates the in-memory digest records to add a new post to the given
  * user's digests.
  * @param object $user User object (must include special ->emailtype, etc)
  * @param array $userdigests Array of user id => digest information object
  * @param forum_post $post Post object
  * @param forum_post $inreplyto Parent post
  * @param forum_discussion $discussion Discus
  * @param forum $forum
  * @param object $cm
  * @param object $course
  * @param object $context
  */
 private static function digest_add_post_for_user(&$user, &$userdigests, &$post, &$inreplyto, &$discussion, &$forum, &$cm, &$course, &$context)
 {
     global $CFG;
     // Set up digest for user if required
     if (!array_key_exists($user->id, $userdigests)) {
         $userdigests[$user->id] = new StdClass();
         $userdigests[$user->id]->discussionid = -1;
         // So we do header next
         $userdigests[$user->id]->user = $user;
         $userdigests[$user->id]->forumid = -1;
         // Get header text
         $headerdata = new object();
         $headerdata->sitename = format_string($course->fullname, true);
         $headerdata->userprefs = $CFG->wwwroot . '/user/edit.php?id=' . $user->id . '&amp;course=' . $course->id;
         $userdigests[$user->id]->text = get_string('digestmailheader', 'forumng', $headerdata) . "\n\n";
         // Get header HTML
         $html = "<head>";
         foreach ($CFG->stylesheets as $stylesheet) {
             $html .= '<link rel="stylesheet" type="text/css" href="' . $stylesheet . '" />' . "\n";
         }
         $html .= "</head>\n<body id='forumng-email'>\n";
         $headerdata->userprefs = '<a target="_blank" href="' . $headerdata->userprefs . '">' . get_string('digestmailprefs', 'forumng') . '</a>';
         $html .= '<div class="forumng-emailheader"><p>' . get_string('digestmailheader', 'forumng', $headerdata) . '</p></div><hr size="1" noshade="noshade" />';
         $userdigests[$user->id]->html = $html;
         // Get email subject
         $userdigests[$user->id]->subject = get_string('digestmailsubject', 'forumng', format_string($course->shortname, true));
     }
     // New forum?
     if ($userdigests[$user->id]->forumid != $forum->get_id()) {
         $userdigests[$user->id]->forumid = $forum->get_id();
     }
     // Is this a new discussion?
     if ($userdigests[$user->id]->discussionid != $discussion->get_id()) {
         $strforums = get_string('forums', 'forumng');
         // Per-discussion header (text mode)
         $text = "\n \n";
         $text .= '=====================================================================';
         $text .= "\n \n";
         $text .= "{$course->shortname} -> {$strforums} -> " . format_string($forum->get_name(), true);
         if ($discussion->get_subject(false) !== $forum->get_name()) {
             $text .= " -> " . format_string($discussion->get_subject(false), true);
         }
         $text .= "\n";
         // HTML mode
         $html = '<hr size="1" noshade="noshade" />';
         $html .= "<div class='forumng-breadcrumbs'>" . "<a target='_blank' href='{$CFG->wwwroot}/course/view.php?id={$course->id}'>{$course->shortname}</a> -> " . "<a target='_blank' href='{$CFG->wwwroot}/mod/forumng/index.php?id={$course->id}'>{$strforums}</a> -> " . "<a target='_blank' href='{$CFG->wwwroot}/mod/forumng/view.php?" . $forum->get_link_params(forum::PARAM_HTML) . "'>" . format_string($forum->get_name(), true) . "</a>";
         if ($discussion->get_subject(false) !== $forum->get_name()) {
             $html .= " -> <a target='_blank' href='{$CFG->wwwroot}/mod/forumng/discuss.php?" . $discussion->get_link_params(forum::PARAM_HTML) . "'>" . format_string($discussion->get_subject(false), true) . "</a>";
         }
         $html .= '</div>';
         $userdigests[$user->id]->text .= $text;
         $userdigests[$user->id]->html .= $html;
         $userdigests[$user->id]->discussionid = $discussion->get_id();
     }
     // Get both plaintext and html versions (and subject).
     // The html version will be blank if set to
     // plain text mode.
     $post->build_email($inreplyto, $subject, $text, $html, $user->emailtype & 1, $user->emailtype & 2, $user->emailtype & 4, $user->lang, $user->timezone, true);
     $userdigests[$user->id]->text .= $text;
     $userdigests[$user->id]->html .= $html;
 }