コード例 #1
0
function get_deleted_posts_for_forumng($forumngid, $groupid = 0, $deleteuserid = 0, $creatorid = 0)
{
    global $DB, $CFG, $USER;
    $where = ' fp.deleted != 0';
    $whereparams = array();
    if (!empty($groupid)) {
        $where .= ' AND fd.groupid = ?';
        $whereparams[] = $groupid;
    }
    if (!empty($deleteuserid)) {
        $where .= ' AND fp.deleteuserid = ?';
        $whereparams[] = $deleteuserid;
    }
    if (!empty($creatorid)) {
        $where .= ' AND fp.userid = ?';
        $whereparams[] = $creatorid;
    }
    $where .= ' AND fd.deleted = ?';
    $whereparams[] = 0;
    $where .= ' AND fd.forumngid = ?';
    $whereparams[] = $forumngid;
    $orderby = 'fd.id, fp.id';
    // See line 827 of mod_forumng_post.php.
    $result = mod_forumng_post::query_posts($where, $whereparams, $orderby, false, false, false, 0, true);
    return $result;
}
コード例 #2
0
 /**
  * Obtains the root post of the discussion. This actually requests all
  * posts from the database; the first is returned, but others are
  * accessible from methods in the first.
  * If available, cached information is used unless
  * you set $usecache to false. The cache is stored within the discussion
  * object so will not persist beyond a request unless you make the
  * discussion object persist too.
  * @param bool $usecache True to use cache if available, false to
  *    request fresh data
  * @param int $userid User ID to get user-specific data (initially, post
  *   flags) for; 0 = current
  * @return mod_forumng_post Post object
  */
 function get_root_post($usecache = true, $userid = 0)
 {
     if (!$usecache || !$this->rootpost) {
         if (!$usecache || !$this->postscache) {
             // Retrieve most posts in the discussion - even deleted
             // ones. These are necessary in case somebody deletes a post that has
             // replies. They will display as 'deleted post'. We don't retrieve
             // old versions of edited posts. Posts are retrieved in created order
             // so that the order of replies remains constant when we build the tree.
             $posts = mod_forumng_post::query_posts('fp.discussionid=? AND fp.oldversion=0', array($this->discussionfields->id), 'fp.created', $this->forum->has_ratings(), true, false, $userid);
             $this->postscache = serialize($posts);
         } else {
             $posts = unserialize($this->postscache);
         }
         // Add numbers to posts
         $i = 1;
         foreach ($posts as $post) {
             $post->number = $i++;
         }
         // Obtain post relationships
         $children = array();
         foreach ($posts as $id => $fields) {
             if (!array_key_exists($fields->parentpostid, $children)) {
                 $children[$fields->parentpostid] = array();
             }
             $children[$fields->parentpostid][] = $id;
         }
         // Recursively build posts
         $this->rootpost = $this->build_posts($posts, $children, $this->discussionfields->postid, null);
         // Update the 'next/previous' unread lists stored in posts
         if ($this->get_unread_data_user_id() != -1) {
             $linear = array();
             $this->rootpost->build_linear_children($linear);
             $nextunread = array();
             $dump = '';
             foreach ($linear as $index => $post) {
                 $nextunread[$index] = null;
                 if ($post->is_unread() && (!$post->get_deleted() || $post->can_undelete($dump))) {
                     for ($j = $index - 1; $j >= 0; $j--) {
                         if ($nextunread[$j]) {
                             break;
                         }
                         $nextunread[$j] = $post;
                     }
                 }
             }
             $previous = null;
             foreach ($linear as $index => $post) {
                 $post->set_unread_list($nextunread[$index], $previous);
                 if ($post->is_unread() && (!$post->get_deleted() || $post->can_undelete($dump))) {
                     $previous = $post;
                 }
             }
             // Update cached version to include this data
             if ($this->incache) {
                 $this->cache();
             }
         }
     }
     return $this->rootpost;
 }
コード例 #3
0
 /**
  * Obtains the root post of the discussion. This actually requests all
  * posts from the database; the first is returned, but others are
  * accessible from methods in the first.
  * If available, cached information is used unless
  * you set $usecache to false. The cache is stored within the discussion
  * object so will not persist beyond a request unless you make the
  * discussion object persist too.
  * @param bool $usecache True to use cache if available, false to
  *    request fresh data
  * @param int $userid User ID to get user-specific data (initially, post
  *   flags) for; 0 = current
  * @return mod_forumng_post Post object
  */
 public function get_root_post($usecache = true, $userid = 0)
 {
     global $CFG, $USER;
     require_once $CFG->dirroot . '/rating/lib.php';
     if (!$usecache || !$this->rootpost) {
         if (!$usecache || !$this->postscache) {
             $read = !mod_forumng::mark_read_automatically($userid);
             // Retrieve most posts in the discussion - even deleted
             // ones. These are necessary in case somebody deletes a post that has
             // replies. They will display as 'deleted post'. We don't retrieve
             // old versions of edited posts. Posts are retrieved in created order
             // so that the order of replies remains constant when we build the tree.
             $posts = mod_forumng_post::query_posts('fp.discussionid=? AND fp.oldversion=0', array($this->discussionfields->id), 'fp.created', $this->forum->has_ratings(), true, false, $userid, false, false, '', '', $read);
             // Load standard ratings.
             if ($this->get_forum()->get_enableratings() == mod_forumng::FORUMNG_STANDARD_RATING) {
                 // If grading is 'No grading' or 'Teacher grades students'.
                 if ($this->get_forum()->get_grading() == mod_forumng::GRADING_NONE || $this->get_forum()->get_grading() == mod_forumng::GRADING_MANUAL) {
                     // Set the aggregation method.
                     if ($this->get_forum()->get_rating_scale() > 0) {
                         $aggregate = RATING_AGGREGATE_AVERAGE;
                     } else {
                         $aggregate = RATING_AGGREGATE_COUNT;
                     }
                 } else {
                     $aggregate = $this->get_forum()->get_grading();
                 }
                 $ratingoptions = new stdClass();
                 $ratingoptions->context = $this->get_forum()->get_context();
                 $ratingoptions->component = 'mod_forumng';
                 $ratingoptions->ratingarea = 'post';
                 $ratingoptions->items = $posts;
                 $ratingoptions->aggregate = $aggregate;
                 $ratingoptions->scaleid = $this->get_forum()->get_rating_scale();
                 $ratingoptions->userid = $USER->id;
                 $ratingoptions->assesstimestart = $this->forum->get_ratingfrom();
                 $ratingoptions->assesstimefinish = $this->forum->get_ratinguntil();
                 $ratingoptions->returnurl = $this->get_moodle_url();
                 $rm = new rating_manager();
                 $posts = $rm->get_ratings($ratingoptions);
             }
             $this->postscache = serialize($posts);
         } else {
             $posts = unserialize($this->postscache);
         }
         // Add numbers to posts
         $i = 1;
         foreach ($posts as $post) {
             $post->number = $i++;
         }
         // Obtain post relationships
         $children = array();
         foreach ($posts as $id => $fields) {
             if (!array_key_exists($fields->parentpostid, $children)) {
                 $children[$fields->parentpostid] = array();
             }
             $children[$fields->parentpostid][] = $id;
         }
         // Recursively build posts
         $this->rootpost = $this->build_posts($posts, $children, $this->discussionfields->postid, null);
         // Update the 'next/previous' unread lists stored in posts
         if ($this->get_unread_data_user_id() != -1) {
             $linear = array();
             $this->rootpost->build_linear_children($linear);
             $nextunread = array();
             $dump = '';
             foreach ($linear as $index => $post) {
                 $nextunread[$index] = null;
                 if ($post->is_unread() && (!$post->get_deleted() || $post->can_undelete($dump))) {
                     for ($j = $index - 1; $j >= 0; $j--) {
                         if ($nextunread[$j]) {
                             break;
                         }
                         $nextunread[$j] = $post;
                     }
                 }
             }
             $previous = null;
             foreach ($linear as $index => $post) {
                 $post->set_unread_list($nextunread[$index], $previous);
                 if ($post->is_unread() && (!$post->get_deleted() || $post->can_undelete($dump))) {
                     $previous = $post;
                 }
             }
             // Update cached version to include this data
             if ($this->incache) {
                 $this->cache();
             }
         }
     }
     return $this->rootpost;
 }
コード例 #4
0
 /**
  * Returns all posts in this forum by the given user within the given group.
  * @param object $forum
  * @param int $userid
  * @param int $groupid
  * @param int $ratedstart
  * @param int $ratedend
  * @param string $order Sort order; the default is fp.id - note this is preferable
  *   to fp.timecreated because it works correctly if there are two posts in
  *   the same second
  * @param bool $hasrating if true only returns posts which ahve been rated
  * @return array Array of mod_forumng_post objects
  */
 public function get_rated_posts_by_user($forum, $userid, $groupid, $order = 'fp.id', $ratedstart = null, $ratedend = null, $start = null, $end = null)
 {
     global $CFG, $USER;
     if ($forum->get_enableratings() != mod_forumng::FORUMNG_STANDARD_RATING) {
         return array();
     }
     $where = 'fd.forumngid = ? AND fp.userid <> ? AND fp.oldversion = 0 AND fp.deleted = 0';
     $whereparams = array($this->get_id(), $userid);
     if ($groupid != self::NO_GROUPS && $groupid != self::ALL_GROUPS) {
         $where .= ' AND (fd.groupid = ? OR fd.groupid IS NULL)';
         $whereparams[] = $groupid;
     }
     if (!empty($start)) {
         $where .= ' AND fp.created >= ?';
         $whereparams[] = $start;
     }
     if (!empty($end)) {
         $where .= ' AND fp.created <= ?';
         $whereparams[] = $end;
     }
     $sqlselectstring = 'SELECT r.itemid FROM {rating} r WHERE r.itemid = fp.id AND r.ratingarea = \'post\'
             AND r.contextid = ? AND r.userid = ?';
     $extraparams = array();
     if (!empty($ratedstart)) {
         $sqlselectstring .= ' AND r.timemodified >= ?';
         $extraparams[] = $ratedstart;
     }
     if (!empty($ratedend)) {
         $sqlselectstring .= ' AND r.timemodified <= ?';
         $extraparams[] = $ratedend;
     }
     $where .= ' AND ' . self::select_exists($sqlselectstring);
     $whereparams[] = $this->get_context(true)->id;
     $whereparams[] = $userid;
     $whereparams = array_merge($whereparams, $extraparams);
     $result = array();
     $posts = mod_forumng_post::query_posts($where, $whereparams, $order, false, false, true, 0, true, true);
     // Add standard ratings if enabled.
     if ($this->get_enableratings() == mod_forumng::FORUMNG_STANDARD_RATING) {
         require_once $CFG->dirroot . '/rating/lib.php';
         // If grading is 'No grading' or 'Teacher grades students'.
         if ($this->get_grading() == mod_forumng::GRADING_NONE || $this->get_grading() == mod_forumng::GRADING_MANUAL) {
             // Set the aggregation method.
             if ($this->get_rating_scale() > 0) {
                 $aggregate = RATING_AGGREGATE_AVERAGE;
             } else {
                 $aggregate = RATING_AGGREGATE_COUNT;
             }
         } else {
             $aggregate = $this->get_grading();
         }
         $ratingoptions = new stdClass();
         $ratingoptions->context = $this->get_context(true);
         $ratingoptions->component = 'mod_forumng';
         $ratingoptions->ratingarea = 'post';
         $ratingoptions->items = $posts;
         $ratingoptions->aggregate = $aggregate;
         $ratingoptions->scaleid = $this->get_rating_scale();
         $ratingoptions->userid = $USER->id;
         $ratingoptions->assesstimestart = $this->get_ratingfrom();
         $ratingoptions->assesstimefinish = $this->get_ratinguntil();
         $rm = new rating_manager();
         $posts = $rm->get_ratings($ratingoptions);
     }
     foreach ($posts as $fields) {
         $discussionfields = mod_forumng_utils::extract_subobject($fields, 'fd_');
         $discussion = new mod_forumng_discussion($this, $discussionfields, false, -1);
         $result[$fields->id] = new mod_forumng_post($discussion, $fields);
     }
     return $result;
 }
コード例 #5
0
 /**
  * Returns all posts in this forum by the given user within the given group.
  * @param int $userid
  * @param int $groupid
  * @param string $order Sort order; the default is fp.id - note this is preferable
  *   to fp.timecreated because it works correctly if there are two posts in
  *   the same second
  * @return array Array of mod_forumng_post objects
  */
 public function get_all_posts_by_user($userid, $groupid, $order = 'fp.id')
 {
     $where = 'fd.forumngid = ? AND fp.userid = ? AND fp.oldversion = 0 AND fp.deleted = 0';
     $whereparams = array($this->get_id(), $userid);
     if ($groupid != self::NO_GROUPS && $groupid != self::ALL_GROUPS) {
         $where .= ' AND (fd.groupid = ? OR fd.groupid IS NULL)';
         $whereparams[] = $groupid;
     }
     $result = array();
     $posts = mod_forumng_post::query_posts($where, $whereparams, $order, false, false, true, 0, true, true);
     foreach ($posts as $fields) {
         $discussionfields = mod_forumng_utils::extract_subobject($fields, 'fd_');
         $discussion = new mod_forumng_discussion($this, $discussionfields, false, -1);
         $result[$fields->id] = new mod_forumng_post($discussion, $fields);
     }
     return $result;
 }