require dirname(dirname(dirname(__FILE__))) . '/init.php'; safe_require('artefact', 'blog'); $id = param_integer('id'); $limit = param_integer('limit', ArtefactTypeBlog::pagination); $offset = param_integer('offset', 0); $options = json_decode(param_variable('options')); $viewid = isset($options->viewid) ? $options->viewid : null; if ($viewid) { if (!can_view_view($viewid)) { throw new AccessDeniedException(); } } else { if (!$USER->is_logged_in()) { throw new AccessDeniedException(); } if (!$viewid && get_field('artefact', 'owner', 'id', $id) != $USER->get('id')) { throw new AccessDeniedException(); } } ($postids = get_records_sql_array("\n SELECT a.id\n FROM {artefact} a\n LEFT OUTER JOIN {artefact_blog_blogpost} bp\n ON a.id = bp.blogpost\n WHERE a.parent = ?\n AND bp.published = 1\n ORDER BY a.ctime DESC\n LIMIT ? OFFSET ?;", array($id, $limit, $offset))) || ($postids = array()); $data = array(); foreach ($postids as $postid) { $blogpost = new ArtefactTypeBlogPost($postid->id); $data[] = array('id' => $postid->id, 'content' => $blogpost->render_self((array) $options)); } $count = (int) get_field_sql("\n SELECT COUNT(*)\n FROM {artefact} a\n LEFT OUTER JOIN {artefact_blog_blogpost} bp\n ON a.id = bp.blogpost\n WHERE a.parent = ?\n AND bp.published = 1", array($id)); if (!$count) { $count = 1; $data = array(array('content' => get_string('noresults', 'artefact.blog'))); } json_reply(false, array('count' => $count, 'limit' => $limit, 'offset' => $offset, 'data' => $data));
/** * Renders a blog for a view. This involves using a tablerenderer to paginate the posts. * * This uses some legacy stuff from the old views interface, including its * dependence on javascript and the table renderer, which would be nice to * fix using the new pagination stuff some time. * * @param array Options for rendering * @return array A two key array, 'html' and 'javascript'. */ public function render_self($options) { $this->add_to_render_path($options); $smarty = smarty_core(); if (isset($options['viewid'])) { $smarty->assign('artefacttitle', '<a href="' . get_config('wwwroot') . 'view/artefact.php?artefact=' . $this->get('id') . '&view=' . $options['viewid'] . '">' . hsc($this->get('title')) . '</a>'); } else { $smarty->assign('artefacttitle', hsc($this->get('title'))); } $smarty->assign('options', $options); $smarty->assign('description', clean_html($this->get('description'))); // Remove unnecessary options for blog posts unset($options['hidetitle']); $page = isset($options['page']) ? abs(intval($options['page'])) : abs(param_integer('page', 1)); $offset = $page ? $page * self::pagination - self::pagination : 1; $postids = get_column_sql("\n SELECT a.id\n FROM {artefact} a\n LEFT JOIN {artefact_blog_blogpost} bp ON a.id = bp.blogpost\n WHERE a.parent = ?\n AND bp.published = 1\n ORDER BY a.ctime DESC\n LIMIT ? OFFSET ?", array($this->get('id'), self::pagination, $offset)); $postcount = $this->count_published_posts(); $data = array(); foreach ($postids as $postid) { $blogpost = new ArtefactTypeBlogPost($postid); $data[] = array('id' => $postid, 'content' => $blogpost->render_self($options)); } $smarty->assign('postdata', $data); // Pagination if ($postcount > self::pagination) { $baselink = get_config('wwwroot') . 'view/artefact.php?artefact=' . $this->get('id'); if (isset($options['viewid'])) { $baselink .= '&view=' . $options['viewid']; } if ($offset + self::pagination < $postcount) { $smarty->assign('olderpostslink', $baselink . '&page=' . ($page + 1)); } if ($offset > 0) { $smarty->assign('newerpostslink', $baselink . '&page=' . ($page - 1)); } } return array('html' => $smarty->fetch('blocktype:blog:blog_render_self.tpl'), 'javascript' => ''); }