예제 #1
0
    /// Updating rate
} else {
    if ($oldrating = $DB->get_record('forum_ratings', array('userid' => $USER->id, 'post' => $post->id))) {
        if ($rate != $oldrating->rating) {
            $oldrating->rating = $rate;
            $oldrating->time = time();
            $DB->update_record('forum_ratings', $oldrating);
        }
        /// Inserting rate
    } else {
        $newrating = new object();
        $newrating->userid = $USER->id;
        $newrating->time = time();
        $newrating->post = $post->id;
        $newrating->rating = $rate;
        $DB->insert_record('forum_ratings', $newrating);
    }
}
/// Update grades
forum_update_grades($forum, $post->userid);
/// Check user can see any rate
$canviewanyrating = has_capability('mod/forum:viewanyrating', $context);
/// Decide if rates info is displayed
$rateinfo = '';
if ($canviewanyrating) {
    $rateinfo = forum_print_ratings($postid, $scale_values, $forum->assessed, true, NULL, true);
}
/// Calculate response
$response['status'] = 'Ok';
$response['message'] = $rateinfo;
echo json_encode($response);
예제 #2
0
파일: lib.php 프로젝트: 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;
}