Example #1
0
/**
 * Print a forum post
 * 
 * @param object $post The post to print.
 * @param integer $courseid The course this post belongs to.
 * @param boolean $ownpost Whether this post belongs to the current user.
 * @param boolean $reply Whether to print a 'reply' link at the bottom of the message. 
 * @param boolean $link Just print a shortened version of the post as a link to the full post.
 * @param object $ratings -- I don't really know -- 
 * @param string $footer Extra stuff to print after the message.
 * @param string $highlight Space-separated list of terms to highlight.
 * @param int $post_read true, false or -99. If we already know whether this user
 *          has read this post, pass that in, otherwise, pass in -99, and this
 *          function will work it out.
 * @param boolean $dummyifcantsee When forum_user_can_see_post says that
 *          the current user can't see this post, if this argument is true 
 *          (the default) then print a dummy 'you can't see this post' post.
 *          If false, don't output anything at all.
 */
function forum_print_post(&$post, $courseid, $ownpost = false, $reply = false, $link = false, $ratings = NULL, $footer = "", $highlight = "", $post_read = -99, $dummyifcantsee = true)
{
    global $USER, $CFG;
    static $stredit, $strdelete, $strreply, $strparent, $strprune;
    static $strpruneheading, $displaymode;
    static $strmarkread, $strmarkunread, $istracked;
    if (empty($post->modcontext)) {
        // Have to generate it, which is expensive!  Should always be set.
        if (empty($post->forum)) {
            $discussion = get_record('forum_discussions', 'id', $post->discussion);
            $post->forum = $discussion->forum;
        }
        if (!($cm = get_coursemodule_from_instance('forum', $post->forum))) {
            error('Course Module ID was incorrect');
        }
        $post->modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
    }
    if (!forum_user_can_see_post($post->forum, $post->discussion, $post)) {
        if (!$dummyifcantsee) {
            return;
        }
        echo '<a id="p' . $post->id . '"></a>';
        echo '<table cellspacing="0" class="forumpost">';
        echo '<tr class="header"><td class="picture left">';
        //        print_user_picture($post->userid, $courseid, $post->picture);
        echo '</td>';
        if ($post->parent) {
            echo '<td class="topic">';
        } else {
            echo '<td class="topic starter">';
        }
        echo '<div class="subject">' . get_string('forumsubjecthidden', 'forum') . '</div>';
        echo '<div class="author">';
        print_string('forumauthorhidden', 'forum');
        echo '</div></td></tr>';
        echo '<tr><td class="left side">';
        echo '&nbsp;';
        // Actual content
        echo '</td><td class="content">' . "\n";
        echo get_string('forumbodyhidden', 'forum');
        echo '</td></tr></table>';
        return;
    }
    if (empty($stredit)) {
        $stredit = get_string('edit', 'forum');
        $strdelete = get_string('delete', 'forum');
        $strreply = get_string('reply', 'forum');
        $strparent = get_string('parent', 'forum');
        $strpruneheading = get_string('pruneheading', 'forum');
        $strprune = get_string('prune', 'forum');
        $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
        $strmarkread = get_string('markread', 'forum');
        $strmarkunread = get_string('markunread', 'forum');
        if (!empty($post->forum)) {
            $istracked = forum_tp_can_track_forums($post->forum) && forum_tp_is_tracked($post->forum);
        } else {
            $istracked = false;
        }
    }
    if ($istracked) {
        if ($post_read == -99) {
            // If we don't know yet...
            // The front page can display a news item post to non-logged in users. This should
            // always appear as 'read'.
            $post_read = empty($USER) || forum_tp_is_post_read($USER->id, $post);
        }
        if ($post_read) {
            $read_style = ' read';
        } else {
            $read_style = ' unread';
            echo '<a name="unread"></a>';
        }
    } else {
        $read_style = '';
    }
    echo '<a id="p' . $post->id . '"></a>';
    echo '<table cellspacing="0" class="forumpost' . $read_style . '">';
    // Picture
    $postuser = new object();
    $postuser->id = $post->userid;
    $postuser->firstname = $post->firstname;
    $postuser->lastname = $post->lastname;
    $postuser->imagealt = $post->imagealt;
    $postuser->picture = $post->picture;
    echo '<tr class="header"><td class="picture left">';
    print_user_picture($postuser, $courseid);
    echo '</td>';
    if ($post->parent) {
        echo '<td class="topic">';
    } else {
        echo '<td class="topic starter">';
    }
    if (!empty($post->subjectnoformat)) {
        echo '<div class="subject">' . $post->subject . '</div>';
    } else {
        echo '<div class="subject">' . format_string($post->subject) . '</div>';
    }
    echo '<div class="author">';
    $fullname = fullname($post, has_capability('moodle/site:viewfullnames', $post->modcontext));
    $by = new object();
    $by->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $post->userid . '&amp;course=' . $courseid . '">' . $fullname . '</a>';
    $by->date = userdate($post->modified);
    print_string('bynameondate', 'forum', $by);
    echo '</div></td></tr>';
    echo '<tr><td class="left side">';
    if ($group = groups_get_all_groups($courseid, $post->userid)) {
        print_group_picture($group, $courseid, false, false, true);
    } else {
        echo '&nbsp;';
    }
    // Actual content
    echo '</td><td class="content">' . "\n";
    if ($post->attachment) {
        $post->course = $courseid;
        $post->forum = get_field('forum_discussions', 'forum', 'id', $post->discussion);
        echo '<div class="attachments">';
        $attachedimages = forum_print_attachments($post);
        echo '</div>';
    } else {
        $attachedimages = '';
    }
    $options = new Object();
    $options->para = false;
    $options->trusttext = true;
    if ($link and strlen(strip_tags($post->message)) > $CFG->forum_longpost) {
        // Print shortened version
        echo format_text(forum_shorten_post($post->message), $post->format, $options, $courseid);
        $numwords = count_words(strip_tags($post->message));
        echo '<p><a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '">';
        echo get_string('readtherest', 'forum');
        echo '</a> (' . get_string('numwords', '', $numwords) . ')...</p>';
    } else {
        // Print whole message
        if ($highlight) {
            echo highlight($highlight, format_text($post->message, $post->format, $options, $courseid));
        } else {
            echo format_text($post->message, $post->format, $options, $courseid);
        }
        echo $attachedimages;
    }
    // Commands
    $commands = array();
    if ($istracked) {
        // SPECIAL CASE: The front page can display a news item post to non-logged in users.
        // Don't display the mark read / unread controls in this case.
        if ($CFG->forum_usermarksread && !empty($USER)) {
            if ($post_read) {
                $mcmd = '&amp;mark=unread&amp;postid=' . $post->id;
                $mtxt = $strmarkunread;
            } else {
                $mcmd = '&amp;mark=read&amp;postid=' . $post->id;
                $mtxt = $strmarkread;
            }
            if ($displaymode == FORUM_MODE_THREADED) {
                $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '&amp;parent=' . $post->id . $mcmd . '">' . $mtxt . '</a>';
            } else {
                $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . $mcmd . '#p' . $post->id . '">' . $mtxt . '</a>';
            }
        }
    }
    if ($post->parent) {
        // Zoom in to the parent specifically
        if ($displaymode == FORUM_MODE_THREADED) {
            $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '&amp;parent=' . $post->parent . '">' . $strparent . '</a>';
        } else {
            $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '#p' . $post->parent . '">' . $strparent . '</a>';
        }
    }
    if (!isset($post->forumtype)) {
        $post->forumtype = get_field('forum', 'type', 'id', $post->forum);
    }
    $age = time() - $post->created;
    // Hack for allow to edit news posts those are not displayed yet until they are displayed
    if (!$post->parent && $post->forumtype == 'news' && get_field_sql("SELECT id FROM {$CFG->prefix}forum_discussions WHERE id = {$post->discussion} AND timestart > " . time())) {
        $age = 0;
    }
    $editanypost = has_capability('mod/forum:editanypost', $post->modcontext);
    if ($ownpost or $editanypost) {
        if ($age < $CFG->maxeditingtime or $editanypost) {
            $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?edit=' . $post->id . '">' . $stredit . '</a>';
        }
    }
    if (has_capability('mod/forum:splitdiscussions', $post->modcontext) && $post->parent && $post->forumtype != 'single') {
        $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?prune=' . $post->id . '" title="' . $strpruneheading . '">' . $strprune . '</a>';
    }
    if ($ownpost and $age < $CFG->maxeditingtime and has_capability('mod/forum:deleteownpost', $post->modcontext) or has_capability('mod/forum:deleteanypost', $post->modcontext)) {
        $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?delete=' . $post->id . '">' . $strdelete . '</a>';
    }
    if ($reply) {
        $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?reply=' . $post->id . '">' . $strreply . '</a>';
    }
    echo '<div class="commands">';
    echo implode(' | ', $commands);
    echo '</div>';
    // Ratings
    $ratingsmenuused = false;
    if (!empty($ratings) and !empty($USER->id)) {
        echo '<div class="ratings">';
        $useratings = true;
        if ($ratings->assesstimestart and $ratings->assesstimefinish) {
            if ($post->created < $ratings->assesstimestart or $post->created > $ratings->assesstimefinish) {
                $useratings = false;
            }
        }
        if ($useratings) {
            $mypost = $USER->id == $post->userid;
            $canviewallratings = has_capability('mod/forum:viewanyrating', $post->modcontext);
            if ($canviewallratings and !$mypost) {
                forum_print_ratings_mean($post->id, $ratings->scale, $canviewallratings);
                if (!empty($ratings->allow)) {
                    echo '&nbsp;';
                    forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
                    $ratingsmenuused = true;
                }
            } else {
                if ($mypost) {
                    forum_print_ratings_mean($post->id, $ratings->scale, true);
                } else {
                    if (!empty($ratings->allow)) {
                        forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
                        $ratingsmenuused = true;
                    }
                }
            }
        }
        echo '</div>';
    }
    // Link to post if required
    if ($link) {
        echo '<div class="link">';
        if ($post->replies == 1) {
            $replystring = get_string('repliesone', 'forum', $post->replies);
        } else {
            $replystring = get_string('repliesmany', 'forum', $post->replies);
        }
        echo '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '">' . get_string('discussthistopic', 'forum') . '</a>&nbsp;(' . $replystring . ')';
        echo '</div>';
    }
    if ($footer) {
        echo '<div class="footer">' . $footer . '</div>';
    }
    echo '</td></tr></table>' . "\n\n";
    if ($istracked && !$CFG->forum_usermarksread && !empty($post->forum)) {
        forum_tp_mark_post_read($USER->id, $post, $post->forum);
    }
    return $ratingsmenuused;
}
Example #2
0
File: lib.php Project: r007/PMoodle
/**
 * Print a forum post
 *
 * @param object $post The post to print.
 * @param integer $courseid The course this post belongs to.
 * @param boolean $ownpost Whether this post belongs to the current user.
 * @param boolean $reply Whether to print a 'reply' link at the bottom of the message.
 * @param boolean $link Just print a shortened version of the post as a link to the full post.
 * @param object $ratings -- I don't really know --
 * @param string $footer Extra stuff to print after the message.
 * @param string $highlight Space-separated list of terms to highlight.
 * @param int $post_read true, false or -99. If we already know whether this user
 *          has read this post, pass that in, otherwise, pass in -99, and this
 *          function will work it out.
 * @param boolean $dummyifcantsee When forum_user_can_see_post says that
 *          the current user can't see this post, if this argument is true
 *          (the default) then print a dummy 'you can't see this post' post.
 *          If false, don't output anything at all.
 */
function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost = false, $reply = false, $link = false, $ratings = NULL, $footer = "", $highlight = "", $post_read = null, $dummyifcantsee = true, $istracked = null)
{
    global $USER, $CFG;
    static $stredit, $strdelete, $strreply, $strparent, $strprune;
    static $strpruneheading, $displaymode;
    static $strmarkread, $strmarkunread;
    $post->course = $course->id;
    $post->forum = $forum->id;
    // caching
    if (!isset($cm->cache)) {
        $cm->cache = new object();
    }
    if (!isset($cm->cache->caps)) {
        $cm->cache->caps = array();
        $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
        $cm->cache->caps['mod/forum:viewdiscussion'] = has_capability('mod/forum:viewdiscussion', $modcontext);
        $cm->cache->caps['moodle/site:viewfullnames'] = has_capability('moodle/site:viewfullnames', $modcontext);
        $cm->cache->caps['mod/forum:editanypost'] = has_capability('mod/forum:editanypost', $modcontext);
        $cm->cache->caps['mod/forum:splitdiscussions'] = has_capability('mod/forum:splitdiscussions', $modcontext);
        $cm->cache->caps['mod/forum:deleteownpost'] = has_capability('mod/forum:deleteownpost', $modcontext);
        $cm->cache->caps['mod/forum:deleteanypost'] = has_capability('mod/forum:deleteanypost', $modcontext);
        $cm->cache->caps['mod/forum:viewanyrating'] = has_capability('mod/forum:viewanyrating', $modcontext);
    }
    if (!isset($cm->uservisible)) {
        $cm->uservisible = coursemodule_visible_for_user($cm);
    }
    if (!forum_user_can_see_post($forum, $discussion, $post, NULL, $cm)) {
        if (!$dummyifcantsee) {
            return;
        }
        echo '<a id="p' . $post->id . '"></a>';
        echo '<table cellspacing="0" class="forumpost">';
        echo '<tr class="header"><td class="picture left">';
        //        print_user_picture($post->userid, $courseid, $post->picture);
        echo '</td>';
        if ($post->parent) {
            echo '<td class="topic">';
        } else {
            echo '<td class="topic starter">';
        }
        echo '<div class="subject">' . get_string('forumsubjecthidden', 'forum') . '</div>';
        echo '<div class="author">';
        print_string('forumauthorhidden', 'forum');
        echo '</div></td></tr>';
        echo '<tr><td class="left side">';
        echo '&nbsp;';
        // Actual content
        echo '</td><td class="content">' . "\n";
        echo get_string('forumbodyhidden', 'forum');
        echo '</td></tr></table>';
        return;
    }
    if (empty($stredit)) {
        $stredit = get_string('edit', 'forum');
        $strdelete = get_string('delete', 'forum');
        $strreply = get_string('reply', 'forum');
        $strparent = get_string('parent', 'forum');
        $strpruneheading = get_string('pruneheading', 'forum');
        $strprune = get_string('prune', 'forum');
        $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
        $strmarkread = get_string('markread', 'forum');
        $strmarkunread = get_string('markunread', 'forum');
    }
    $read_style = '';
    // ignore trackign status if not tracked or tracked param missing
    if ($istracked) {
        if (is_null($post_read)) {
            debugging('fetching post_read info');
            $post_read = forum_tp_is_post_read($USER->id, $post);
        }
        if ($post_read) {
            $read_style = ' read';
        } else {
            $read_style = ' unread';
            echo '<a name="unread"></a>';
        }
    }
    echo '<a id="p' . $post->id . '"></a>';
    echo '<table cellspacing="0" class="forumpost' . $read_style . '">';
    // Picture
    $postuser = new object();
    $postuser->id = $post->userid;
    $postuser->firstname = $post->firstname;
    $postuser->lastname = $post->lastname;
    $postuser->imagealt = $post->imagealt;
    $postuser->picture = $post->picture;
    echo '<tr class="header"><td class="picture left">';
    print_user_picture($postuser, $course->id);
    echo '</td>';
    if ($post->parent) {
        echo '<td class="topic">';
    } else {
        echo '<td class="topic starter">';
    }
    if (!empty($post->subjectnoformat)) {
        echo '<div class="subject">' . $post->subject . '</div>';
    } else {
        echo '<div class="subject">' . format_string($post->subject) . '</div>';
    }
    echo '<div class="author">';
    $fullname = fullname($postuser, $cm->cache->caps['moodle/site:viewfullnames']);
    $by = new object();
    $by->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $post->userid . '&amp;course=' . $course->id . '">' . $fullname . '</a>';
    $by->date = userdate($post->modified);
    print_string('bynameondate', 'forum', $by);
    echo '</div></td></tr>';
    echo '<tr><td class="left side">';
    if (isset($cm->cache->usersgroups)) {
        $groups = array();
        if (isset($cm->cache->usersgroups[$post->userid])) {
            foreach ($cm->cache->usersgroups[$post->userid] as $gid) {
                $groups[$gid] = $cm->cache->groups[$gid];
            }
        }
    } else {
        $groups = groups_get_all_groups($course->id, $post->userid, $cm->groupingid);
    }
    if ($groups) {
        print_group_picture($groups, $course->id, false, false, true);
    } else {
        echo '&nbsp;';
    }
    // Actual content
    echo '</td><td class="content">' . "\n";
    if ($post->attachment) {
        echo '<div class="attachments">';
        $attachedimages = forum_print_attachments($post);
        echo '</div>';
    } else {
        $attachedimages = '';
    }
    $options = new object();
    $options->para = false;
    $options->trusttext = true;
    if ($link and strlen(strip_tags($post->message)) > $CFG->forum_longpost) {
        // Print shortened version
        echo format_text(forum_shorten_post($post->message), $post->format, $options, $course->id);
        $numwords = count_words(strip_tags($post->message));
        echo '<p><a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '">';
        echo get_string('readtherest', 'forum');
        echo '</a> (' . get_string('numwords', '', $numwords) . ')...</p>';
    } else {
        // Print whole message
        if ($highlight) {
            echo highlight($highlight, format_text($post->message, $post->format, $options, $course->id));
        } else {
            echo format_text($post->message, $post->format, $options, $course->id);
        }
        echo $attachedimages;
    }
    // Commands
    $commands = array();
    if ($istracked) {
        // SPECIAL CASE: The front page can display a news item post to non-logged in users.
        // Don't display the mark read / unread controls in this case.
        if ($CFG->forum_usermarksread and isloggedin()) {
            if ($post_read) {
                $mcmd = '&amp;mark=unread&amp;postid=' . $post->id;
                $mtxt = $strmarkunread;
            } else {
                $mcmd = '&amp;mark=read&amp;postid=' . $post->id;
                $mtxt = $strmarkread;
            }
            if ($displaymode == FORUM_MODE_THREADED) {
                $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '&amp;parent=' . $post->id . $mcmd . '">' . $mtxt . '</a>';
            } else {
                $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . $mcmd . '#p' . $post->id . '">' . $mtxt . '</a>';
            }
        }
    }
    if ($post->parent) {
        // Zoom in to the parent specifically
        if ($displaymode == FORUM_MODE_THREADED) {
            $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '&amp;parent=' . $post->parent . '">' . $strparent . '</a>';
        } else {
            $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '#p' . $post->parent . '">' . $strparent . '</a>';
        }
    }
    $age = time() - $post->created;
    // Hack for allow to edit news posts those are not displayed yet until they are displayed
    if (!$post->parent and $forum->type == 'news' and $discussion->timestart > time()) {
        $age = 0;
    }
    $editanypost = $cm->cache->caps['mod/forum:editanypost'];
    if ($ownpost or $editanypost) {
        if ($age < $CFG->maxeditingtime or $editanypost) {
            $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?edit=' . $post->id . '">' . $stredit . '</a>';
        }
    }
    if ($cm->cache->caps['mod/forum:splitdiscussions'] && $post->parent && $forum->type != 'single') {
        $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?prune=' . $post->id . '" title="' . $strpruneheading . '">' . $strprune . '</a>';
    }
    if ($ownpost and $age < $CFG->maxeditingtime and $cm->cache->caps['mod/forum:deleteownpost'] or $cm->cache->caps['mod/forum:deleteanypost']) {
        $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?delete=' . $post->id . '">' . $strdelete . '</a>';
    }
    if ($reply) {
        $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?reply=' . $post->id . '">' . $strreply . '</a>';
    }
    echo '<div class="commands">';
    echo implode(' | ', $commands);
    echo '</div>';
    // Ratings
    $ratingsmenuused = false;
    if (!empty($ratings) and isloggedin()) {
        echo '<div class="ratings">';
        $useratings = true;
        if ($ratings->assesstimestart and $ratings->assesstimefinish) {
            if ($post->created < $ratings->assesstimestart or $post->created > $ratings->assesstimefinish) {
                $useratings = false;
            }
        }
        if ($useratings) {
            $mypost = $USER->id == $post->userid;
            $canviewallratings = $cm->cache->caps['mod/forum:viewanyrating'];
            if (isset($cm->cache->ratings)) {
                if (isset($cm->cache->ratings[$post->id])) {
                    $allratings = $cm->cache->ratings[$post->id];
                } else {
                    $allratings = array();
                    // no reatings present yet
                }
            } else {
                $allratings = NULL;
                // not preloaded
            }
            if (isset($cm->cache->myratings)) {
                if (isset($cm->cache->myratings[$post->id])) {
                    $myrating = $cm->cache->myratings[$post->id];
                } else {
                    $myrating = FORUM_UNSET_POST_RATING;
                    // no reatings present yet
                }
            } else {
                $myrating = NULL;
                // not preloaded
            }
            if ($canviewallratings and !$mypost) {
                forum_print_ratings($post->id, $ratings->scale, $forum->assessed, $canviewallratings, $allratings);
                if (!empty($ratings->allow)) {
                    echo '&nbsp;';
                    forum_print_rating_menu($post->id, $USER->id, $ratings->scale, $myrating);
                    $ratingsmenuused = true;
                }
            } else {
                if ($mypost) {
                    forum_print_ratings($post->id, $ratings->scale, $forum->assessed, true, $allratings);
                } else {
                    if (!empty($ratings->allow)) {
                        forum_print_rating_menu($post->id, $USER->id, $ratings->scale, $myrating);
                        $ratingsmenuused = true;
                    }
                }
            }
        }
        echo '</div>';
    }
    // Link to post if required
    if ($link) {
        echo '<div class="link">';
        if ($post->replies == 1) {
            $replystring = get_string('repliesone', 'forum', $post->replies);
        } else {
            $replystring = get_string('repliesmany', 'forum', $post->replies);
        }
        echo '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '">' . get_string('discussthistopic', 'forum') . '</a>&nbsp;(' . $replystring . ')';
        echo '</div>';
    }
    if ($footer) {
        echo '<div class="footer">' . $footer . '</div>';
    }
    echo '</td></tr></table>' . "\n\n";
    if ($istracked && !$CFG->forum_usermarksread && !$post_read) {
        forum_tp_mark_post_read($USER->id, $post, $forum->id);
    }
    return $ratingsmenuused;
}
Example #3
0
/**
 * TODO document
 */
function forum_print_post(&$post, $courseid, $ownpost = false, $reply = false, $link = false, $ratings = NULL, $footer = "", $highlight = "", $post_read = -99)
{
    global $USER, $CFG, $SESSION;
    static $stredit, $strdelete, $strreply, $strparent, $strprune;
    static $strpruneheading, $threadedmode;
    static $strmarkread, $strmarkunread, $istracked;
    if (empty($post->modcontext)) {
        // Have to generate it, which is expensive!  Should always be set.
        if (empty($post->forum)) {
            $discussion = get_record('forum_discussions', 'id', $post->discussion);
            $post->forum = $discussion->forum;
        }
        if (!($cm = get_coursemodule_from_instance('forum', $post->forum))) {
            error('Course Module ID was incorrect');
        }
        $post->modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
    }
    if (!forum_user_can_see_post($post->forum, $post->discussion, $post)) {
        if (empty($SESSION->forum_search)) {
            // just viewing, return
            return;
        }
        echo '<a id="p' . $post->id . '"></a>';
        echo '<table cellspacing="0" class="forumpost">';
        echo '<tr class="header"><td class="picture left">';
        //        print_user_picture($post->userid, $courseid, $post->picture);
        echo '</td>';
        if ($post->parent) {
            echo '<td class="topic">';
        } else {
            echo '<td class="topic starter">';
        }
        echo '<div class="subject">' . get_string('forumsubjecthidden', 'forum') . '</div>';
        echo '<div class="author">';
        print_string('forumauthorhidden', 'forum');
        echo '</div></td></tr>';
        echo '<tr><td class="left side">';
        echo '&nbsp;';
        // Actual content
        echo '</td><td class="content">' . "\n";
        echo get_string('forumbodyhidden', 'forum');
        echo '</td></tr></table>';
        return;
    }
    if (empty($stredit)) {
        $stredit = get_string('edit', 'forum');
        $strdelete = get_string('delete', 'forum');
        $strreply = get_string('reply', 'forum');
        $strparent = get_string('parent', 'forum');
        $strpruneheading = get_string('pruneheading', 'forum');
        $strprune = get_string('prune', 'forum');
        $threadedmode = (!empty($USER->mode) and $USER->mode == FORUM_MODE_THREADED);
        $strmarkread = get_string('markread', 'forum');
        $strmarkunread = get_string('markunread', 'forum');
        if (!empty($post->forum)) {
            $istracked = forum_tp_can_track_forums($post->forum) && forum_tp_is_tracked($post->forum);
        } else {
            $istracked = false;
        }
    }
    if ($istracked) {
        if ($post_read == -99) {
            // If we don't know yet...
            // The front page can display a news item post to non-logged in users. This should
            // always appear as 'read'.
            $post_read = empty($USER) || forum_tp_is_post_read($USER->id, $post);
        }
        if ($post_read) {
            $read_style = ' read';
        } else {
            $read_style = ' unread';
            echo '<a name="unread"></a>';
        }
    } else {
        $read_style = '';
    }
    echo '<a id="p' . $post->id . '"></a>';
    echo '<table cellspacing="0" class="forumpost' . $read_style . ' ' . PM_POST_CLASS . '" id="m' . $post->id . '">';
    // Picture
    $postuser = new object();
    $postuser->id = $post->userid;
    $postuser->firstname = $post->firstname;
    $postuser->lastname = $post->lastname;
    $postuser->imagealt = $post->imagealt;
    $postuser->picture = $post->picture;
    echo '<tr class="header"><td class="picture left">';
    print_user_picture($postuser, $courseid);
    echo '</td>';
    if ($post->parent) {
        echo '<td class="topic" colspan="3">';
    } else {
        echo '<td class="topic starter" colspan="3">';
    }
    echo '<div class="subject ' . PM_TITLE_CLASS . '">' . format_string($post->subject) . '</div>';
    // #GEOF# Annotations must use a unique value for the user ID
    // This should be a string, and it should be human-readable and suitable for
    // outside consumption.  I'm therefore using username rather than ID.
    // If Moodle displayed these values, the fields wouldn'n need to be hidden.
    $rootpath = $CFG->wwwroot;
    //parse_url( $CFG->wwwroot );
    //$rootpath = $rootpath[ 'path' ];
    $refurl = "{$rootpath}/mod/forum/permalink.php?p={$post->id}";
    $discussurl = "{$rootpath}/mod/forum/discuss.php?d={$post->discussion}";
    // used for summary link
    // This class author, used by Marginalia, must preceed the one below, used by Moodle.  (Well, not in Moodle 1.8)
    // Unfortunately I can't use that one - it's marked author but it is not, in fact, the author
    // - it also includes "by", the date, etc.  (grrr) #geof#
    echo "<span style='display:none' class='" . PM_AUTHOR_CLASS . "' title='" . htmlspecialchars($post->username) . "'>" . htmlspecialchars($post->firstname . ' ' . $post->lastname) . "</span>\n";
    echo "<abbr style='display:none' class='" . PM_DATE_CLASS . "' title='" . date('Ymd', $post->modified) . 'T' . date('HiO', $post->modified) . "'></abbr>\n";
    echo "<a style='display:none' rel='" . PM_URL_REL . "' href='{$refurl}'></a>\n";
    echo '<div class="author">';
    $fullname = fullname($post, has_capability('moodle/site:viewfullnames', $post->modcontext));
    $by->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $post->userid . '&amp;course=' . $courseid . '">' . $fullname . '</a>';
    $by->date = userdate($post->modified);
    // This, I presume, is to localize string display.  Unfortunately it's
    // unstructured text, so I can't insert the necessary <abbr> element with the date.
    // Instead that's done in a hidden tag above.
    print_string('bynameondate', 'forum', $by);
    echo '</div></td></tr>';
    echo '<tr><td class="left side">';
    if ($group = user_group($courseid, $post->userid)) {
        print_group_picture($group, $courseid, false, false, true);
    } else {
        echo '&nbsp;';
    }
    // Actual content
    echo '</td><td class="content ' . PM_CONTENT_CLASS . '" valign="top">' . "\n";
    if ($post->attachment) {
        $post->course = $courseid;
        $post->forum = get_field('forum_discussions', 'forum', 'id', $post->discussion);
        echo '<div class="attachments">';
        $attachedimages = forum_print_attachments($post);
        echo '</div>';
    } else {
        $attachedimages = '';
    }
    $options = new Object();
    $options->para = false;
    $options->trusttext = true;
    if ($link and strlen(strip_tags($post->message)) > $CFG->forum_longpost) {
        // Print shortened version
        echo format_text(forum_shorten_post($post->message), $post->format, $options, $courseid);
        $numwords = count_words(strip_tags($post->message));
        echo '<p><a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '">';
        echo get_string('readtherest', 'forum');
        echo '</a> (' . get_string('numwords', '', $numwords) . ')...</p>';
    } else {
        // Print whole message
        if ($highlight) {
            echo highlight($highlight, format_text($post->message, $post->format, $options, $courseid));
        } else {
            echo format_text($post->message, $post->format, $options, $courseid);
        }
        echo $attachedimages;
    }
    // Commands
    $commands = array();
    if ($istracked) {
        // SPECIAL CASE: The front page can display a news item post to non-logged in users.
        // Don't display the mark read / unread controls in this case.
        if ($CFG->forum_usermarksread && !empty($USER)) {
            if ($post_read) {
                $mcmd = '&amp;mark=unread&amp;postid=' . $post->id;
                $mtxt = $strmarkunread;
            } else {
                $mcmd = '&amp;mark=read&amp;postid=' . $post->id;
                $mtxt = $strmarkread;
            }
            if ($threadedmode) {
                $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '&amp;parent=' . $post->id . $mcmd . '">' . $mtxt . '</a>';
            } else {
                $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . $mcmd . '#p' . $post->id . '">' . $mtxt . '</a>';
            }
        }
    }
    if ($post->parent) {
        if ($threadedmode) {
            $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '&amp;parent=' . $post->parent . '">' . $strparent . '</a>';
        } else {
            $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '#p' . $post->parent . '">' . $strparent . '</a>';
        }
    }
    if (!isset($post->forumtype)) {
        $post->forumtype = get_field('forum', 'type', 'id', $post->forum);
    }
    $age = time() - $post->created;
    // Hack for allow to edit news posts those are not displayed yet until they are displayed
    if (!$post->parent && $post->forumtype == 'news' && get_field_sql("SELECT id FROM {$CFG->prefix}forum_discussions WHERE id = {$post->discussion} AND timestart > " . time())) {
        $age = 0;
    }
    $editanypost = has_capability('mod/forum:editanypost', $post->modcontext);
    if ($ownpost or $editanypost) {
        if ($age < $CFG->maxeditingtime or $editanypost) {
            $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?edit=' . $post->id . '">' . $stredit . '</a>';
        }
    }
    if (has_capability('mod/forum:splitdiscussions', $post->modcontext) && $post->parent && $post->forumtype != 'single') {
        $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?prune=' . $post->id . '" title="' . $strpruneheading . '">' . $strprune . '</a>';
    }
    if ($ownpost and $age < $CFG->maxeditingtime and has_capability('mod/forum:deleteownpost', $post->modcontext) or has_capability('mod/forum:deleteanypost', $post->modcontext)) {
        $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?delete=' . $post->id . '">' . $strdelete . '</a>';
    }
    // The span below is to allow text-decoration:underline, which doesn't seem
    // to work otherwise (Firefox bug?)
    $commands[] = "<button class='smartquote'><span>Quote</span></button>";
    if ($reply) {
        $commands[] = '<a href="' . $CFG->wwwroot . '/mod/forum/post.php?reply=' . $post->id . '">' . $strreply . '</a>';
    }
    echo '<div class="commands">';
    echo implode(' | ', $commands);
    echo '</div>';
    // Ratings
    $ratingsmenuused = false;
    if (!empty($ratings) and !empty($USER->id)) {
        echo '<div class="ratings">';
        $useratings = true;
        if ($ratings->assesstimestart and $ratings->assesstimefinish) {
            if ($post->created < $ratings->assesstimestart or $post->created > $ratings->assesstimefinish) {
                $useratings = false;
            }
        }
        if ($useratings) {
            $mypost = $USER->id == $post->userid;
            $canviewallratings = has_capability('mod/forum:viewanyrating', $post->modcontext);
            if ($canviewallratings and !$mypost) {
                forum_print_ratings_mean($post->id, $ratings->scale, $canviewallratings);
                if (!empty($ratings->allow)) {
                    echo '&nbsp;';
                    forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
                    $ratingsmenuused = true;
                }
            } else {
                if ($mypost) {
                    forum_print_ratings_mean($post->id, $ratings->scale, true);
                } else {
                    if (!empty($ratings->allow)) {
                        forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
                        $ratingsmenuused = true;
                    }
                }
            }
        }
        echo '</div>';
    }
    // Link to post if required
    if ($link) {
        echo '<div class="link">';
        if ($post->replies == 1) {
            $replystring = get_string('repliesone', 'forum', $post->replies);
        } else {
            $replystring = get_string('repliesmany', 'forum', $post->replies);
        }
        echo '<a href="' . $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '">' . get_string('discussthistopic', 'forum') . '</a>&nbsp;(' . $replystring . ')';
        echo '</div>';
    }
    if ($footer) {
        echo '<div class="footer">' . $footer . '</div>';
    }
    //	if ( $canannotate )
    //	{
    echo "</td><td class='control-margin'><div><button type='button' " . " title='" . htmlspecialchars(get_string('create_button', ANNOTATION_STRINGS)) . "'" . " onclick='window.moodleMarginalia.createAnnotation(event,\"m{$post->id}\")'><span>&gt;</span></button></div></td>\n";
    echo "<td class='" . AN_NOTES_CLASS . "' valign='top'><div>";
    echo "<ol></ol>\n";
    echo "</div></td>\n";
    //	}
    //	else
    //		echo "<td></td><td></td>";
    echo '</tr></table>' . "\n\n";
    if ($istracked && !$CFG->forum_usermarksread && !empty($post->forum)) {
        forum_tp_mark_post_read($USER->id, $post, $post->forum);
    }
    return $ratingsmenuused;
}