Ejemplo n.º 1
0
/**
 * This function return the XML rss contents about the anonforum
 * It returns false if something is wrong
 *
 * @param stdClass $anonforum the anonforum object
 * @param string $sql the SQL used to retrieve the contents from the database
 * @param array $params the SQL parameters used
 * @param object $context the context this anonforum relates to
 * @return bool|string false if the contents is empty, otherwise the contents of the feed is returned
 *
 * @Todo MDL-31129 implement post attachment handling
 */
function anonforum_rss_feed_contents($anonforum, $sql, $params, $context)
{
    global $CFG, $DB, $USER;
    $status = true;
    $recs = $DB->get_recordset_sql($sql, $params, 0, $anonforum->rssarticles);
    //set a flag. Are we displaying discussions or posts?
    $isdiscussion = true;
    if (!empty($anonforum->rsstype) && $anonforum->rsstype != 1) {
        $isdiscussion = false;
    }
    if (!($cm = get_coursemodule_from_instance('anonforum', $anonforum->id, $anonforum->course))) {
        print_error('invalidcoursemodule');
    }
    $formatoptions = new stdClass();
    $items = array();
    foreach ($recs as $rec) {
        $item = new stdClass();
        $discussion = new stdClass();
        $discussion->id = $rec->discussionid;
        $discussion->groupid = $rec->groupid;
        $discussion->timestart = $rec->timestart;
        $discussion->timeend = $rec->timeend;
        $post = null;
        if (!$isdiscussion) {
            $post = new stdClass();
            $post->id = $rec->postid;
            $post->parent = $rec->postparent;
            $post->userid = $rec->userid;
        }
        if ($isdiscussion && !anonforum_user_can_see_discussion($anonforum, $discussion, $context)) {
            // This is a discussion which the user has no permission to view
            $item->title = get_string('anonforumsubjecthidden', 'anonforum');
            $message = get_string('anonforumbodyhidden', 'anonforum');
            $item->author = get_string('anonforumauthorhidden', 'anonforum');
        } else {
            if (!$isdiscussion && !anonforum_user_can_see_post($anonforum, $discussion, $post, $USER, $cm)) {
                // This is a post which the user has no permission to view
                $item->title = get_string('anonforumsubjecthidden', 'anonforum');
                $message = get_string('anonforumbodyhidden', 'anonforum');
                $item->author = get_string('anonforumauthorhidden', 'anonforum');
            } else {
                // The user must have permission to view
                if ($isdiscussion && !empty($rec->discussionname)) {
                    $item->title = format_string($rec->discussionname);
                } else {
                    if (!empty($rec->postsubject)) {
                        $item->title = format_string($rec->postsubject);
                    } else {
                        //we should have an item title by now but if we dont somehow then substitute something somewhat meaningful
                        $item->title = format_string($anonforum->name . ' ' . userdate($rec->postcreated, get_string('strftimedatetimeshort', 'langconfig')));
                    }
                }
                if (empty($rec->anonymouspost)) {
                    $user = new stdClass();
                    $user->firstname = $rec->userfirstname;
                    $user->lastname = $rec->userlastname;
                    $item->author = fullname($user);
                } else {
                    $item->author = get_string('anonymoususer', 'anonforum');
                }
                $message = file_rewrite_pluginfile_urls($rec->postmessage, 'pluginfile.php', $context->id, 'mod_anonforum', 'post', $rec->postid);
                $formatoptions->trusted = $rec->posttrust;
            }
        }
        if ($isdiscussion) {
            $item->link = $CFG->wwwroot . "/mod/anonforum/discuss.php?d=" . $rec->discussionid;
        } else {
            $item->link = $CFG->wwwroot . "/mod/anonforum/discuss.php?d=" . $rec->discussionid . "&parent=" . $rec->postid;
        }
        $formatoptions->trusted = $rec->posttrust;
        $item->description = format_text($message, $rec->postformat, $formatoptions, $anonforum->course);
        //TODO: MDL-31129 implement post attachment handling
        /*if (!$isdiscussion) {
                        $post_file_area_name = str_replace('//', '/', "$anonforum->course/$CFG->moddata/anonforum/$anonforum->id/$rec->postid");
                        $post_files = get_directory_list("$CFG->dataroot/$post_file_area_name");
        
                        if (!empty($post_files)) {
                            $item->attachments = array();
                        }
                    }*/
        $item->pubdate = $rec->postcreated;
        $items[] = $item;
    }
    $recs->close();
    // Create the RSS header.
    $header = rss_standard_header(strip_tags(format_string($anonforum->name, true)), $CFG->wwwroot . "/mod/anonforum/view.php?f=" . $anonforum->id, format_string($anonforum->intro, true));
    // TODO: fix format
    // Now all the RSS items, if there are any.
    $articles = '';
    if (!empty($items)) {
        $articles = rss_add_items($items);
    }
    // Create the RSS footer.
    $footer = rss_standard_footer();
    return $header . $articles . $footer;
}
Ejemplo n.º 2
0
/**
 * Prints the discussion view screen for a anonforum.
 *
 * @global object
 * @global object
 * @param object $course The current course object.
 * @param object $anonforum Forum to be printed.
 * @param int $maxdiscussions .
 * @param string $displayformat The display format to use (optional).
 * @param string $sort Sort arguments for database query (optional).
 * @param int $groupmode Group mode of the anonforum (optional).
 * @param void $unused (originally current group)
 * @param int $page Page mode, page to display (optional).
 * @param int $perpage The maximum number of discussions per page(optional)
 *
 */
function anonforum_print_latest_discussions($course, $anonforum, $maxdiscussions = -1, $displayformat = 'plain', $sort = '', $currentgroup = -1, $groupmode = -1, $page = -1, $perpage = 100, $cm = NULL)
{
    global $CFG, $USER, $OUTPUT;
    if (!$cm) {
        if (!($cm = get_coursemodule_from_instance('anonforum', $anonforum->id, $anonforum->course))) {
            print_error('invalidcoursemodule');
        }
    }
    $context = context_module::instance($cm->id);
    if (empty($sort)) {
        $sort = "d.timemodified DESC";
    }
    $olddiscussionlink = false;
    // Sort out some defaults
    if ($perpage <= 0) {
        $perpage = 0;
        $page = -1;
    }
    if ($maxdiscussions == 0) {
        // all discussions - backwards compatibility
        $page = -1;
        $perpage = 0;
        if ($displayformat == 'plain') {
            $displayformat = 'header';
            // Abbreviate display by default
        }
    } else {
        if ($maxdiscussions > 0) {
            $page = -1;
            $perpage = $maxdiscussions;
        }
    }
    $fullpost = false;
    if ($displayformat == 'plain') {
        $fullpost = true;
    }
    // Decide if current user is allowed to see ALL the current discussions or not
    // First check the group stuff
    if ($currentgroup == -1 or $groupmode == -1) {
        $groupmode = groups_get_activity_groupmode($cm, $course);
        $currentgroup = groups_get_activity_group($cm);
    }
    $groups = array();
    //cache
    // If the user can post discussions, then this is a good place to put the
    // button for it. We do not show the button if we are showing site news
    // and the current user is a guest.
    $canstart = anonforum_user_can_post_discussion($anonforum, $currentgroup, $groupmode, $cm, $context);
    if (!$canstart and $anonforum->type !== 'news') {
        if (isguestuser() or !isloggedin()) {
            $canstart = true;
        }
        if (!is_enrolled($context) and !is_viewing($context)) {
            // allow guests and not-logged-in to see the button - they are prompted to log in after clicking the link
            // normal users with temporary guest access see this button too, they are asked to enrol instead
            // do not show the button to users with suspended enrolments here
            $canstart = enrol_selfenrol_available($course->id);
        }
    }
    if ($canstart) {
        echo '<div class="singlebutton anonforumaddnew">';
        echo "<form id=\"newdiscussionform\" method=\"get\" action=\"{$CFG->wwwroot}/mod/anonforum/post.php\">";
        echo '<div>';
        echo "<input type=\"hidden\" name=\"anonforum\" value=\"{$anonforum->id}\" />";
        switch ($anonforum->type) {
            case 'news':
            case 'blog':
                $buttonadd = get_string('addanewtopic', 'anonforum');
                break;
            case 'qanda':
                $buttonadd = get_string('addanewquestion', 'anonforum');
                break;
            default:
                $buttonadd = get_string('addanewdiscussion', 'anonforum');
                break;
        }
        echo '<input type="submit" value="' . $buttonadd . '" />';
        echo '</div>';
        echo '</form>';
        echo "</div>\n";
    } else {
        if (isguestuser() or !isloggedin() or $anonforum->type == 'news') {
            // no button and no info
        } else {
            if ($groupmode and has_capability('mod/anonforum:startdiscussion', $context)) {
                // inform users why they can not post new discussion
                if ($currentgroup) {
                    echo $OUTPUT->notification(get_string('cannotadddiscussion', 'anonforum'));
                } else {
                    echo $OUTPUT->notification(get_string('cannotadddiscussionall', 'anonforum'));
                }
            }
        }
    }
    // Get all the recent discussions we're allowed to see
    $getuserlastmodified = $displayformat == 'header';
    if (!($discussions = anonforum_get_discussions($cm, $sort, $fullpost, null, $maxdiscussions, $getuserlastmodified, $page, $perpage))) {
        echo '<div class="anonforumnodiscuss">';
        if ($anonforum->type == 'news') {
            echo '(' . get_string('nonews', 'anonforum') . ')';
        } else {
            if ($anonforum->type == 'qanda') {
                echo '(' . get_string('noquestions', 'anonforum') . ')';
            } else {
                echo '(' . get_string('nodiscussions', 'anonforum') . ')';
            }
        }
        echo "</div>\n";
        return;
    }
    // If we want paging
    if ($page != -1) {
        ///Get the number of discussions found
        $numdiscussions = anonforum_get_discussions_count($cm);
        ///Show the paging bar
        echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f={$anonforum->id}");
        if ($numdiscussions > 1000) {
            // saves some memory on sites with very large anonymous forums
            $replies = anonforum_count_discussion_replies($anonforum->id, $sort, $maxdiscussions, $page, $perpage);
        } else {
            $replies = anonforum_count_discussion_replies($anonforum->id);
        }
    } else {
        $replies = anonforum_count_discussion_replies($anonforum->id);
        if ($maxdiscussions > 0 and $maxdiscussions <= count($discussions)) {
            $olddiscussionlink = true;
        }
    }
    $canviewparticipants = has_capability('moodle/course:viewparticipants', $context);
    $strdatestring = get_string('strftimerecentfull');
    // Check if the anonymous forum is tracked.
    if ($cantrack = anonforum_tp_can_track_anonforums($anonforum)) {
        $anonforumtracked = anonforum_tp_is_tracked($anonforum);
    } else {
        $anonforumtracked = false;
    }
    if ($anonforumtracked) {
        $unreads = anonforum_get_discussions_unread($cm);
    } else {
        $unreads = array();
    }
    if ($displayformat == 'header') {
        echo '<table cellspacing="0" class="anonforumheaderlist">';
        echo '<thead>';
        echo '<tr>';
        echo '<th class="header topic" scope="col">' . get_string('discussion', 'anonforum') . '</th>';
        echo '<th class="header author" colspan="2" scope="col">' . get_string('startedby', 'anonforum') . '</th>';
        if ($groupmode > 0) {
            echo '<th class="header group" scope="col">' . get_string('group') . '</th>';
        }
        if (has_capability('mod/anonforum:viewdiscussion', $context)) {
            echo '<th class="header replies" scope="col">' . get_string('replies', 'anonforum') . '</th>';
            // If the anonymous forum can be tracked, display the unread column.
            if ($cantrack) {
                echo '<th class="header replies" scope="col">' . get_string('unread', 'anonforum');
                if ($anonforumtracked) {
                    echo '<a title="' . get_string('markallread', 'anonforum') . '" href="' . $CFG->wwwroot . '/mod/anonforum/markposts.php?f=' . $anonforum->id . '&amp;mark=read&amp;returnpage=view.php">' . '<img src="' . $OUTPUT->pix_url('t/markasread') . '" class="iconsmall" alt="' . get_string('markallread', 'anonforum') . '" /></a>';
                }
                echo '</th>';
            }
        }
        echo '<th class="header lastpost" scope="col">' . get_string('lastpost', 'anonforum') . '</th>';
        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';
    }
    foreach ($discussions as $discussion) {
        if (!empty($replies[$discussion->discussion])) {
            $discussion->replies = $replies[$discussion->discussion]->replies;
            $discussion->lastpostid = $replies[$discussion->discussion]->lastpostid;
        } else {
            $discussion->replies = 0;
        }
        // SPECIAL CASE: The front page can display a news item post to non-logged in users.
        // All posts are read in this case.
        if (!$anonforumtracked) {
            $discussion->unread = '-';
        } else {
            if (empty($USER)) {
                $discussion->unread = 0;
            } else {
                if (empty($unreads[$discussion->discussion])) {
                    $discussion->unread = 0;
                } else {
                    $discussion->unread = $unreads[$discussion->discussion];
                }
            }
        }
        if (isloggedin()) {
            $ownpost = $discussion->userid == $USER->id;
        } else {
            $ownpost = false;
        }
        // Use discussion name instead of subject of first post
        $discussion->subject = $discussion->name;
        switch ($displayformat) {
            case 'header':
                if ($groupmode > 0) {
                    if (isset($groups[$discussion->groupid])) {
                        $group = $groups[$discussion->groupid];
                    } else {
                        $group = $groups[$discussion->groupid] = groups_get_group($discussion->groupid);
                    }
                } else {
                    $group = -1;
                }
                anonforum_print_discussion_header($discussion, $anonforum, $group, $strdatestring, $cantrack, $anonforumtracked, $canviewparticipants, $context);
                break;
            default:
                $link = false;
                if ($discussion->replies) {
                    $link = true;
                } else {
                    $modcontext = context_module::instance($cm->id);
                    $link = anonforum_user_can_see_discussion($anonforum, $discussion, $modcontext, $USER);
                }
                $discussion->anonforum = $anonforum->id;
                anonforum_print_post($discussion, $discussion, $anonforum, $cm, $course, $ownpost, 0, $link, false, '', null, true, $anonforumtracked);
                break;
        }
    }
    if ($displayformat == "header") {
        echo '</tbody>';
        echo '</table>';
    }
    if ($olddiscussionlink) {
        if ($anonforum->type == 'news') {
            $strolder = get_string('oldertopics', 'anonforum');
        } else {
            $strolder = get_string('olderdiscussions', 'anonforum');
        }
        echo '<div class="anonforumolddiscuss">';
        echo '<a href="' . $CFG->wwwroot . '/mod/anonforum/view.php?f=' . $anonforum->id . '&amp;showall=1">';
        echo $strolder . '</a> ...</div>';
    }
    if ($page != -1) {
        ///Show the paging bar
        echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f={$anonforum->id}");
    }
}