コード例 #1
0
 /**
  * Queries for all forums on a course, including additional data about unread
  * posts etc.
  * NOTE: If shared forums are in use, this will usually return the CLONE
  * forum object, which doesn't hold any data about the actual forum;
  * the exception is that unread data will be obtained from the real forum.
  * If you would like to obtain the real forum instead, please make sure
  * $realforums is set to true. This has a performance cost.
  * @param object $course Moodle course object
  * @param int $userid User ID, 0 = current user, -1 = no unread data is needed
  * @param bool $unreadasbinary If true, unread data MAY BE binary (1/0)
  *   instead of containing the full number; this improves performance but
  *   only works on some databases
  * @param array $specificids If array has no entries, returns all forums
  *   on the course; if it has at least one entry, returns only those forums
  *   with course-module ID listed in the array
  * @param bool $realforums Set this to true to obtain real forums
  *   if any are clones; has a performance cost if shared forums are used
  * @return array Array of forum objects (keys are forum IDs; in the case of
  *   shared forums, the id is of the clone not the forum, even if
  *   $realforums is set)
  */
 public static function get_course_forums($course, $userid = 0, $unread = self::UNREAD_DISCUSSIONS, $specificids = array(), $realforums = false)
 {
     global $USER, $DB;
     $userid = mod_forumng_utils::get_real_userid($userid);
     $result = array();
     // Added $userid parameter to obtain modinfo for specific user rather than current user.
     $modinfo = get_fast_modinfo($course, $userid);
     // Obtains extra information needed only when acquiring unread data
     $aagforums = array();
     $viewhiddenforums = array();
     $groups = array();
     $contexts = array();
     if ($unread != self::UNREAD_NONE) {
         foreach ($modinfo->cms as $cmid => $cm) {
             if (count($specificids) && !in_array($cmid, $specificids)) {
                 continue;
             }
             if ($cm->modname == 'forumng') {
                 $context = context_module::instance($cmid);
                 $contexts[$cmid] = $context;
                 if (has_capability('moodle/site:accessallgroups', $context, $userid)) {
                     $aagforums[] = $cm->instance;
                 }
                 if (has_capability('mod/forumng:viewallposts', $context, $userid)) {
                     $viewhiddenforums[] = $cm->instance;
                 }
             }
         }
         if ($userid == $USER->id && isset($USER->groupmember)) {
             if (array_key_exists($course->id, $USER->groupmember)) {
                 $groups = $USER->groupmember[$course->id];
             }
             // Else do nothing - groups list should be empty
         } else {
             $rs = $DB->get_recordset_sql("\nSELECT\n    g.id\nFROM\n    {groups} g\n    INNER JOIN {groups_members} gm ON g.id = gm.groupid\nWHERE\n    g.courseid = ?\n    AND gm.userid = ?", array($course->id, $userid));
             foreach ($rs as $rec) {
                 $groups[] = $rec->id;
             }
             $rs->close();
         }
     }
     $rows = self::query_forums($specificids, $course, $userid, $unread, $groups, $aagforums, $viewhiddenforums);
     foreach ($rows as $rec) {
         // Check course-module exists
         if (!array_key_exists($rec->cm_id, $modinfo->cms)) {
             continue;
         }
         $cm = $modinfo->cms[$rec->cm_id];
         if ($cm->modname != 'forumng') {
             continue;
         }
         // Mess about with binary setting to ensure result is same, whatever
         // the database
         if ($unread == self::UNREAD_BINARY) {
             // Set binary to 0/1 even if database returns 't'/'f'
             if ($rec->f_hasunreaddiscussions === 'f') {
                 $rec->f_hasunreaddiscussions = 0;
             } else {
                 if ($rec->f_hasunreaddiscussions) {
                     $rec->f_hasunreaddiscussions = 1;
                 } else {
                     $rec->f_hasunreaddiscussions = 0;
                 }
             }
         }
         // Get context if we didn't already get it.
         if (!empty($contexts[$rec->cm_id])) {
             $context = $contexts[$rec->cm_id];
         } else {
             $context = context_module::instance($rec->cm_id);
         }
         // Create a new forum object from the database details
         $forumfields = mod_forumng_utils::extract_subobject($rec, 'f_');
         $forum = new mod_forumng($course, $cm, $context, $forumfields);
         $result[$forumfields->id] = $forum;
         if ($forum->is_shared()) {
             $forum->set_clone_reference(self::CLONE_DIRECT);
         }
         // For clone forums (only pointers to genuine shared forums)
         if ($forum->is_clone()) {
             // If we are retrieving the real forum, get it individually
             if ($realforums) {
                 $othercourse = $DB->get_record_sql("\nSELECT\n    c.*\nFROM\n    {course_modules} cm\n    INNER JOIN {course} c ON c.id = cm.course\nWHERE\n    cm.id = ?", array($forumfields->originalcmid), '*', MUST_EXIST);
                 $extra = self::get_course_forums($othercourse, $userid, $unread, array($forumfields->originalcmid));
                 if (count($extra) != 1) {
                     throw new coding_exception('Unable to find shared forum ' . $forumfields->originalcmid);
                 }
                 foreach ($extra as $extraforum) {
                     $extraforum->set_clone_reference($cm->id);
                     $result[$forumfields->id] = $extraforum;
                 }
             } else {
                 if ($unread != self::UNREAD_NONE) {
                     // Even if not retrieving the real forum, we still use
                     // its undead data when unread data is on
                     $forum->init_unread_from_original($unread);
                 }
             }
         }
     }
     return $result;
 }