Пример #1
0
 /**
  * This function returns a list of posts in a given blog.
  *
  * @param integer
  * @param integer
  * @param integer
  * @param array
  */
 public static function get_posts($id, $limit, $offset, $viewoptions = null)
 {
     $results = array('limit' => $limit, 'offset' => $offset);
     // If viewoptions is null, we're getting posts for the my blogs area,
     // and we should get all posts & show drafts first.  Otherwise it's a
     // blog in a view, and we should only get published posts.
     $from = "\n            FROM {artefact} a LEFT JOIN {artefact_blog_blogpost} bp ON a.id = bp.blogpost\n            WHERE a.artefacttype = 'blogpost' AND a.parent = ?";
     if (!is_null($viewoptions)) {
         if (isset($viewoptions['before'])) {
             $from .= " AND a.ctime < '{$viewoptions['before']}'";
         }
         $from .= ' AND bp.published = 1';
     }
     $results['count'] = count_records_sql('SELECT COUNT(*) ' . $from, array($id));
     $data = get_records_sql_assoc('
         SELECT
             a.id, a.title, a.description, a.author, a.authorname, ' . db_format_tsfield('a.ctime', 'ctime') . ', ' . db_format_tsfield('a.mtime', 'mtime') . ',
             a.locked, bp.published, a.allowcomments ' . $from . '
         ORDER BY bp.published ASC, a.ctime DESC, a.id DESC', array($id), $offset, $limit);
     if (!$data) {
         $results['data'] = array();
         return $results;
     }
     // Get the attached files.
     $postids = array_map(create_function('$a', 'return $a->id;'), $data);
     $files = ArtefactType::attachments_from_id_list($postids);
     if ($files) {
         safe_require('artefact', 'file');
         foreach ($files as &$file) {
             $params = array('id' => $file->attachment);
             if (!empty($viewoptions['viewid'])) {
                 $params['viewid'] = $viewoptions['viewid'];
             }
             $file->icon = call_static_method(generate_artefact_class_name($file->artefacttype), 'get_icon', $params);
             $data[$file->artefact]->files[] = $file;
         }
     }
     if ($tags = ArtefactType::tags_from_id_list($postids)) {
         foreach ($tags as &$at) {
             $data[$at->artefact]->tags[] = $at->tag;
         }
     }
     foreach ($data as &$post) {
         // Format dates properly
         if (is_null($viewoptions)) {
             // My Blogs area: create forms for changing post status & deleting posts.
             $post->changepoststatus = ArtefactTypeBlogpost::changepoststatus_form($post->id, $post->published);
             $post->delete = ArtefactTypeBlogpost::delete_form($post->id, $post->title);
         } else {
             $by = $post->author ? display_default_name($post->author) : $post->authorname;
             $post->postedby = get_string('postedbyon', 'artefact.blog', $by, format_date($post->ctime));
             // Get comment counts
             if (!empty($viewoptions['countcomments'])) {
                 safe_require('artefact', 'comment');
                 require_once get_config('docroot') . 'lib/view.php';
                 $view = new View($viewoptions['viewid']);
                 $artefact = artefact_instance_from_id($post->id);
                 list($commentcount, $comments) = ArtefactTypeComment::get_artefact_comments_for_view($artefact, $view, null, false);
                 $post->commentcount = $commentcount;
                 $post->comments = $comments;
             }
         }
         $post->ctime = format_date($post->ctime, 'strftimedaydatetime');
         $post->mtime = format_date($post->mtime);
         // Ensure images in the post have the right viewid associated with them
         if (!empty($viewoptions['viewid'])) {
             safe_require('artefact', 'file');
             $post->description = ArtefactTypeFolder::append_view_url($post->description, $viewoptions['viewid']);
         }
     }
     $results['data'] = array_values($data);
     return $results;
 }