/** * Returns a list of anonforum discussions as well as a summary of the discussion * in a provided list of anonforums. * * @param array $anonforumids the anonforum ids * @return array the anonforum discussion details * @since Moodle 2.5 */ public static function get_anonforum_discussions($anonforumids) { global $CFG, $DB, $USER; require_once $CFG->dirroot . "/mod/anonforum/lib.php"; // Validate the parameter. $params = self::validate_parameters(self::get_anonforum_discussions_parameters(), array('anonforumids' => $anonforumids)); $anonforumids = $params['anonforumids']; // Array to store the anonymous forum discussions to return. $arrdiscussions = array(); // Keep track of the course ids we have performed a require_course_login check on to avoid repeating. $arrcourseschecked = array(); // Store the modinfo for the anonymous forums in an individual courses. $arrcoursesanonforuminfo = array(); // Keep track of the users we have looked up in the DB. $arrusers = array(); // Loop through them. foreach ($anonforumids as $id) { // Get the anonymous forum object. $anonforum = $DB->get_record('anonforum', array('id' => $id), '*', MUST_EXIST); // Check that that user can view this course if check not performed yet. if (!in_array($anonforum->course, $arrcourseschecked)) { // Check the user can function in this context. self::validate_context(context_course::instance($anonforum->course)); // Add to the array. $arrcourseschecked[] = $anonforum->course; } // Get the modinfo for the course if we haven't already. if (!isset($arrcoursesanonforuminfo[$anonforum->course])) { $modinfo = get_fast_modinfo($anonforum->course); $arrcoursesanonforuminfo[$anonforum->course] = $modinfo->get_instances_of('anonforum'); } // Check if this anonymous forum does not exist in the modinfo array, should always be false unless DB is borked. if (empty($arrcoursesanonforuminfo[$anonforum->course][$anonforum->id])) { throw new moodle_exception('invalidmodule', 'error'); } // We now have the course module. $cm = $arrcoursesanonforuminfo[$anonforum->course][$anonforum->id]; // If the anonymous forum is not visible throw an exception. if (!$cm->uservisible) { throw new moodle_exception('nopermissiontoshow', 'error'); } // Get the module context. $modcontext = context_module::instance($cm->id); // Check they have the view anonymous forum capability. require_capability('mod/anonforum:viewdiscussion', $modcontext); // Check if they can view full names. $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext); // Get the unreads array, this takes a anonymous forum id and returns data for all discussions. $unreads = array(); if ($cantrack = anonforum_tp_can_track_anonforums($anonforum)) { if ($anonforumtracked = anonforum_tp_is_tracked($anonforum)) { $unreads = anonforum_get_discussions_unread($cm); } } // The anonforum function returns the replies for all the discussions in a given anonymous forum. $replies = anonforum_count_discussion_replies($id); // Get the discussions for this anonymous forum. if ($discussions = $DB->get_records('anonforum_discussions', array('anonforum' => $id))) { foreach ($discussions as $discussion) { // If the anonymous forum is of type qanda and the user has not posted in the discussion // we need to ensure that they have the required capability. if ($anonforum->type == 'qanda' && !anonforum_user_has_posted($discussion->anonforum, $discussion->id, $USER->id)) { require_capability('mod/anonforum:viewqandawithoutposting', $modcontext); } $usernamefields = user_picture::fields(); // If we don't have the users details then perform DB call. if (empty($arrusers[$discussion->userid])) { $arrusers[$discussion->userid] = $DB->get_record('user', array('id' => $discussion->userid), $usernamefields, MUST_EXIST); } // Get the subject. $subject = $DB->get_field('anonforum_posts', 'subject', array('id' => $discussion->firstpost), MUST_EXIST); // Create object to return. $return = new stdClass(); $return->id = (int) $discussion->id; $return->course = $discussion->course; $return->anonforum = $discussion->anonforum; $return->name = $discussion->name; $return->userid = $discussion->userid; $return->groupid = $discussion->groupid; $return->assessed = $discussion->assessed; $return->timemodified = (int) $discussion->timemodified; $return->usermodified = $discussion->usermodified; $return->timestart = $discussion->timestart; $return->timeend = $discussion->timeend; $return->firstpost = (int) $discussion->firstpost; $return->firstuserfullname = fullname($arrusers[$discussion->userid], $canviewfullname); $return->firstuserimagealt = $arrusers[$discussion->userid]->imagealt; $return->firstuserpicture = $arrusers[$discussion->userid]->picture; $return->firstuseremail = $arrusers[$discussion->userid]->email; $return->subject = $subject; $return->numunread = ''; if ($cantrack && $anonforumtracked) { if (isset($unreads[$discussion->id])) { $return->numunread = (int) $unreads[$discussion->id]; } } // Check if there are any replies to this discussion. if (!empty($replies[$discussion->id])) { $return->numreplies = (int) $replies[$discussion->id]->replies; $return->lastpost = (int) $replies[$discussion->id]->lastpostid; } else { // No replies, so the last post will be the first post. $return->numreplies = 0; $return->lastpost = (int) $discussion->firstpost; } // Get the last post as well as the user who made it. $lastpost = $DB->get_record('anonforum_posts', array('id' => $return->lastpost), '*', MUST_EXIST); if (empty($arrusers[$lastpost->userid])) { $arrusers[$lastpost->userid] = $DB->get_record('user', array('id' => $lastpost->userid), $usernamefields, MUST_EXIST); } $return->lastuserid = $lastpost->userid; $return->lastuserfullname = fullname($arrusers[$lastpost->userid], $canviewfullname); $return->lastuserimagealt = $arrusers[$lastpost->userid]->imagealt; $return->lastuserpicture = $arrusers[$lastpost->userid]->picture; $return->lastuseremail = $arrusers[$lastpost->userid]->email; // Add the discussion statistics to the array to return. $arrdiscussions[$return->id] = (array) $return; } } } return $arrdiscussions; }
/** * 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 . '&mark=read&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 . '&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}"); } }