Esempio n. 1
0
/**
 * Given a course and a date, prints a summary of all the new
 * messages posted in the course since that date as HTML media objects
 *
 * @global object $CFG
 * @global object $USER
 * @global object $DB
 * @global object $OUTPUT
 * @uses CONTEXT_MODULE
 * @uses VISIBLEGROUPS
 * @param object $course
 * @param bool $viewfullnames capability
 * @param int $timestart
 * @return string recent activity
 */
function hsuforum_recent_activity($course, $viewfullnames, $timestart, $forumid = null)
{
    global $CFG, $USER, $DB, $OUTPUT;
    $limitfrom = 0;
    $limitnum = 0;
    $andforumid = '';
    if ($forumid !== null) {
        $andforumid = 'AND d.forum = ?';
        $limitnum = 6;
    }
    $allnamefields = user_picture::fields('u', null, 'duserid');
    $sql = "SELECT p.*, f.anonymous as forumanonymous, f.type AS forumtype,\n                   d.forum, d.groupid, d.timestart, d.timeend, {$allnamefields}\n              FROM {hsuforum_posts} p\n              JOIN {hsuforum_discussions} d ON d.id = p.discussion\n              JOIN {hsuforum} f             ON f.id = d.forum\n              JOIN {user} u                 ON u.id = p.userid\n             WHERE p.created > ?\n                   AND f.course = ?\n                   AND (p.privatereply = 0 OR p.privatereply = ? OR p.userid = ?)\n                   {$andforumid}\n          ORDER BY p.created DESC\n    ";
    $params = array($timestart, $course->id, $USER->id, $USER->id, $forumid);
    if (!($posts = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum))) {
        return '';
    }
    $modinfo = get_fast_modinfo($course);
    $config = get_config('hsuforum');
    $groupmodes = array();
    $cms = array();
    $out = '';
    foreach ($posts as $post) {
        if (!isset($modinfo->instances['hsuforum'][$post->forum])) {
            // not visible
            continue;
        }
        $cm = $modinfo->instances['hsuforum'][$post->forum];
        if (!$cm->uservisible) {
            continue;
        }
        $context = context_module::instance($cm->id);
        if (!has_capability('mod/hsuforum:viewdiscussion', $context)) {
            continue;
        }
        if (!empty($config->enabletimedposts) and $USER->id != $post->duserid and ($post->timestart > 0 and $post->timestart > time() or $post->timeend > 0 and $post->timeend < time())) {
            if (!has_capability('mod/hsuforum:viewhiddentimedposts', $context)) {
                continue;
            }
        }
        // Check that the user can see the discussion.
        if (hsuforum_is_user_group_discussion($cm, $post->groupid)) {
            $postuser = hsuforum_extract_postuser($post, hsuforum_get_cm_forum($cm), context_module::instance($cm->id));
            $userpicture = new user_picture($postuser);
            $userpicture->link = false;
            $userpicture->alttext = false;
            $userpicture->size = 100;
            $picture = $OUTPUT->render($userpicture);
            $url = $CFG->wwwroot . '/mod/hsuforum/discuss.php?d=' . $post->discussion;
            if (!empty($post->parent)) {
                $url .= '#p' . $post->id;
            }
            $postusername = fullname($postuser, $viewfullnames);
            $postsubject = break_up_long_words(format_string($post->subject, true));
            $posttime = hsuforum_relative_time($post->modified);
            $out .= hsuforum_media_object($url, $picture, $postusername, $posttime, $postsubject);
        }
    }
    if ($out) {
        $out = "<h3 class='hsuforum-recent-heading'>" . get_string('newforumposts', 'hsuforum') . "</h3>" . $out;
    }
    return $out;
}
    /**
     * Return html for individual post
     *
     * 3 use cases:
     *  1. Standard post
     *  2. Reply to user
     *  3. Private reply to user
     *
     * @param object $p
     * @return string
     */
    public function post_template($p)
    {
        global $PAGE;
        $byuser = $p->fullname;
        if (!empty($p->userurl)) {
            $byuser = html_writer::link($p->userurl, $p->fullname);
        }
        $byline = get_string('postbyx', 'hsuforum', $byuser);
        if ($p->isreply) {
            $parent = $p->parentfullname;
            if (!empty($p->parentuserurl)) {
                $parent = html_writer::link($p->parentuserurl, $p->parentfullname);
            }
            if (empty($p->parentuserpic)) {
                $byline = get_string('replybyx', 'hsuforum', $byuser);
            } else {
                $byline = get_string('postbyxinreplytox', 'hsuforum', array('parent' => $p->parentuserpic . $parent, 'author' => $byuser, 'parentpost' => "<a title='" . get_string('parentofthispost', 'hsuforum') . "' class='hsuforum-parent-post-link disable-router' href='{$p->parenturl}'><span class='accesshide'>" . get_string('parentofthispost', 'hsuforum') . "</span>↑</a>"));
            }
            if (!empty($p->privatereply)) {
                if (empty($p->parentuserpic)) {
                    $byline = get_string('privatereplybyx', 'hsuforum', $byuser);
                } else {
                    $byline = get_string('postbyxinprivatereplytox', 'hsuforum', array('author' => $byuser, 'parent' => $p->parentuserpic . $parent));
                }
            }
        } else {
            if (!empty($p->privatereply)) {
                $byline = get_string('privatereplybyx', 'hsuforum', $byuser);
            }
        }
        $author = s(strip_tags($p->fullname));
        $unread = '';
        $unreadclass = '';
        if ($p->unread) {
            $unread = "<span class='hsuforum-unreadcount'>" . get_string('unread', 'hsuforum') . "</span>";
            $unreadclass = "hsuforum-post-unread";
        }
        $options = get_string('options', 'hsuforum');
        $datecreated = hsuforum_relative_time($p->rawcreated, array('class' => 'hsuforum-post-pubdate'));
        $postreplies = '';
        if ($p->replycount) {
            $postreplies = "<div class='post-reply-count accesshide'>{$p->replycount}</div>";
        }
        $newwindow = '';
        if ($PAGE->pagetype === 'local-joulegrader-view') {
            $newwindow = ' target="_blank"';
        }
        $revealed = "";
        if ($p->revealed) {
            $nonanonymous = get_string('nonanonymous', 'mod_hsuforum');
            $revealed = '<span class="label label-danger">' . $nonanonymous . '</span>';
        }
        return <<<HTML
<div class="hsuforum-post-wrapper hsuforum-post-target clearfix {$unreadclass}" id="p{$p->id}" data-postid="{$p->id}" data-discussionid="{$p->discussionid}" data-author="{$author}" data-ispost="true" tabindex="-1">

    <div class="hsuforum-post-figure">

        <img class="userpicture" src="{$p->imagesrc}" alt="">

    </div>
    <div class="hsuforum-post-body">
        <h6 role="heading" aria-level="6" class="hsuforum-post-byline" id="hsuforum-post-{$p->id}">
            {$unread} {$byline} {$revealed}
        </h6>
        <small class='hsuform-post-date'><a href="{$p->permalink}" class="disable-router"{$newwindow}>{$datecreated}</a></small>

        <div class="hsuforum-post-content">
            <div class="hsuforum-post-title">{$p->subject}</div>
                {$p->message}
        </div>
        <div role="region" class='hsuforum-tools' aria-label='{$options}'>
            <div class="hsuforum-postflagging">{$p->postflags}</div>
            {$p->tools}
        </div>
        {$postreplies}
    </div>
</div>
HTML;
    }