* @author Catalyst IT Ltd * @license http://www.gnu.org/copyleft/gpl.html GNU GPL * @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz * */ define('INTERNAL', 1); define('MENUITEM', 'myportfolio/blogs'); define('SECTION_PLUGINTYPE', 'artefact'); define('SECTION_PLUGINNAME', 'blog'); define('SECTION_PAGE', 'view'); require dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php'; define('TITLE', get_string('viewblog', 'artefact.blog')); safe_require('artefact', 'blog'); require_once get_config('libroot') . 'pieforms/pieform.php'; if ($publish = param_integer('publish', null)) { ArtefactTypeBlogpost::publish_form($publish); } if ($delete = param_integer('delete', null)) { ArtefactTypeBlogpost::delete_form($delete); } $id = param_integer('id', null); if (is_null($id)) { if (!($records = get_records_select_array('artefact', "artefacttype = 'blog' AND \"owner\" = ?", array($USER->get('id')), 'id ASC'))) { throw new ParameterException(); } $id = $records[0]->id; $blog = new ArtefactTypeBlog($id, $records[0]); } else { $blog = new ArtefactTypeBlog($id); } $blog->check_permission();
/** * 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', 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) { $file->icon = call_static_method(generate_artefact_class_name($file->artefacttype), 'get_icon', array('id' => $file->attachment)); $data[$file->artefact]->files[] = $file; } } if ($tags = ArtefactType::tags_from_id_list($postids)) { foreach ($tags as &$at) { $data[$at->artefact]->tags[] = $at->tag; } } // Get comment counts if (!empty($viewoptions['countcomments'])) { safe_require('artefact', 'comment'); $viewids = array(); $commentcounts = ArtefactTypeComment::count_comments($viewids, array_keys($data)); } // Format dates properly foreach ($data as &$post) { if (is_null($viewoptions)) { // My Blogs area: create forms for publishing & deleting posts. if (!$post->published) { $post->publish = ArtefactTypeBlogpost::publish_form($post->id); } $post->delete = ArtefactTypeBlogpost::delete_form($post->id); } else { $by = $post->author ? display_default_name($post->author) : $post->authorname; $post->postedby = get_string('postedbyon', 'artefact.blog', $by, format_date($post->ctime)); if (isset($commentcounts)) { $post->commentcount = isset($commentcounts[$post->id]) ? $commentcounts[$post->id]->comments : 0; } } $post->ctime = format_date($post->ctime, 'strftimedaydatetime'); $post->mtime = format_date($post->mtime); } $results['data'] = array_values($data); return $results; }