Example #1
0
/**
 * Prints a anonforum discussion
 *
 * @uses CONTEXT_MODULE
 * @uses ANONFORUM_MODE_FLATNEWEST
 * @uses ANONFORUM_MODE_FLATOLDEST
 * @uses ANONFORUM_MODE_THREADED
 * @uses ANONFORUM_MODE_NESTED
 * @param stdClass $course
 * @param stdClass $cm
 * @param stdClass $anonforum
 * @param stdClass $discussion
 * @param stdClass $post
 * @param int $mode
 * @param mixed $canreply
 * @param bool $canrate
 */
function anonforum_print_discussion($course, $cm, $anonforum, $discussion, $post, $mode, $canreply = NULL, $canrate = false)
{
    global $USER, $CFG;
    require_once $CFG->dirroot . '/rating/lib.php';
    $ownpost = isloggedin() && $USER->id == $post->userid;
    $modcontext = context_module::instance($cm->id);
    if ($canreply === NULL) {
        $reply = anonforum_user_can_post($anonforum, $discussion, $USER, $cm, $course, $modcontext);
    } else {
        $reply = $canreply;
    }
    // $cm holds general cache for anonymous forum functions
    $cm->cache = new stdClass();
    $cm->cache->groups = groups_get_all_groups($course->id, 0, $cm->groupingid);
    $cm->cache->usersgroups = array();
    $posters = array();
    // preload all posts - TODO: improve...
    if ($mode == ANONFORUM_MODE_FLATNEWEST) {
        $sort = "p.created DESC";
    } else {
        $sort = "p.created ASC";
    }
    $anonforumtracked = anonforum_tp_is_tracked($anonforum);
    $posts = anonforum_get_all_discussion_posts($discussion->id, $sort, $anonforumtracked);
    $post = $posts[$post->id];
    foreach ($posts as $pid => $p) {
        $posters[$p->userid] = $p->userid;
    }
    // preload all groups of ppl that posted in this discussion
    if ($postersgroups = groups_get_all_groups($course->id, $posters, $cm->groupingid, 'gm.id, gm.groupid, gm.userid')) {
        foreach ($postersgroups as $pg) {
            if (!isset($cm->cache->usersgroups[$pg->userid])) {
                $cm->cache->usersgroups[$pg->userid] = array();
            }
            $cm->cache->usersgroups[$pg->userid][$pg->groupid] = $pg->groupid;
        }
        unset($postersgroups);
    }
    //load ratings
    if ($anonforum->assessed != RATING_AGGREGATE_NONE) {
        $ratingoptions = new stdClass();
        $ratingoptions->context = $modcontext;
        $ratingoptions->component = 'mod_anonforum';
        $ratingoptions->ratingarea = 'post';
        $ratingoptions->items = $posts;
        $ratingoptions->aggregate = $anonforum->assessed;
        //the aggregation method
        $ratingoptions->scaleid = $anonforum->scale;
        $ratingoptions->userid = $USER->id;
        if ($anonforum->type == 'single' or !$discussion->id) {
            $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/anonforum/view.php?id={$cm->id}";
        } else {
            $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/anonforum/discuss.php?d={$discussion->id}";
        }
        $ratingoptions->assesstimestart = $anonforum->assesstimestart;
        $ratingoptions->assesstimefinish = $anonforum->assesstimefinish;
        $rm = new rating_manager();
        $posts = $rm->get_ratings($ratingoptions);
    }
    $post->anonforum = $anonforum->id;
    // Add the anonforum id to the post object, later used by anonforum_print_post
    $post->anonforumtype = $anonforum->type;
    $post->subject = format_string($post->subject);
    $postread = !empty($post->postread);
    anonforum_print_post($post, $discussion, $anonforum, $cm, $course, $ownpost, $reply, false, '', '', $postread, true, $anonforumtracked);
    switch ($mode) {
        case ANONFORUM_MODE_FLATOLDEST:
        case ANONFORUM_MODE_FLATNEWEST:
        default:
            anonforum_print_posts_flat($course, $cm, $anonforum, $discussion, $post, $mode, $reply, $anonforumtracked, $posts);
            break;
        case ANONFORUM_MODE_THREADED:
            anonforum_print_posts_threaded($course, $cm, $anonforum, $discussion, $post, 0, $reply, $anonforumtracked, $posts);
            break;
        case ANONFORUM_MODE_NESTED:
            anonforum_print_posts_nested($course, $cm, $anonforum, $discussion, $post, $reply, $anonforumtracked, $posts);
            break;
    }
}
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);
echo $OUTPUT->header();
// Make the heading have a link back to the main anonforum page.
$anonforumname = format_string($anonforum->name);
$anonforumlink = "{$CFG->wwwroot}/mod/anonforum/view.php?f={$anonforum->id}";
$linkhtml = $OUTPUT->action_link($anonforumlink, $anonforumname);
echo $OUTPUT->heading($linkhtml, 2);
/// Check to see if groups are being used in this anonymous forum
/// 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 = anonforum_user_can_post($anonforum, $discussion, $USER, $cm, $course, $modcontext);
if (!$canreply and $anonforum->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
        $canreply = enrol_selfenrol_available($course->id);
    }
}
/// Print the controls across the top
echo '<div class="discussioncontrols clearfix">';
if (!empty($CFG->enableportfolios) && has_capability('mod/anonforum:exportdiscussion', $modcontext)) {
    require_once $CFG->libdir . '/portfoliolib.php';
    $button = new portfolio_add_button();