PageRenderTime 61ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/artefact/blog/blocktype/blogpost/lib.php

https://github.com/richardmansfield/richardms-mahara
PHP | 200 lines | 105 code | 27 blank | 68 comment | 11 complexity | 09396067dedc239d0e4cd3aebe4f5905 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * Mahara: Electronic portfolio, weblog, resume builder and social networking
  4. * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
  5. * http://wiki.mahara.org/Contributors
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package mahara
  21. * @subpackage blocktype-blogpost
  22. * @author Catalyst IT Ltd
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
  24. * @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
  25. *
  26. */
  27. defined('INTERNAL') || die();
  28. class PluginBlocktypeBlogpost extends PluginBlocktype {
  29. public static function get_title() {
  30. return get_string('title', 'blocktype.blog/blogpost');
  31. }
  32. /**
  33. * Optional method. If exists, allows this class to decide the title for
  34. * all blockinstances of this type
  35. */
  36. public static function get_instance_title(BlockInstance $bi) {
  37. $configdata = $bi->get('configdata');
  38. if (!empty($configdata['artefactid'])) {
  39. require_once(get_config('docroot') . 'artefact/lib.php');
  40. $blogpost = artefact_instance_from_id($configdata['artefactid']);
  41. return $blogpost->get('title');
  42. }
  43. return '';
  44. }
  45. public static function get_description() {
  46. return get_string('description', 'blocktype.blog/blogpost');
  47. }
  48. public static function get_categories() {
  49. return array('blog');
  50. }
  51. public static function render_instance(BlockInstance $instance, $editing=false) {
  52. $configdata = $instance->get('configdata');
  53. $result = '';
  54. if (!empty($configdata['artefactid'])) {
  55. require_once(get_config('docroot') . 'artefact/lib.php');
  56. $blogpost = $instance->get_artefact_instance($configdata['artefactid']);
  57. $configdata['hidetitle'] = true;
  58. $configdata['viewid'] = $instance->get('view');
  59. $result = $blogpost->render_self($configdata);
  60. $result = $result['html'];
  61. }
  62. return $result;
  63. }
  64. /**
  65. * Returns a list of artefact IDs that are in this blockinstance.
  66. *
  67. * Normally this would just include the blogpost ID itself (children such
  68. * as attachments don't need to be included here, they're handled by the
  69. * artefact parent cache). But people might just link to artefacts without
  70. * using the attachment facility. There's nothing wrong with them doing
  71. * that, so if they do we should scrape the post looking for such links and
  72. * include those artefacts as being part of this blockinstance.
  73. *
  74. * @return array List of artefact IDs that are 'in' this blogpost - all
  75. * the blogpost ID plus links to other artefacts that are
  76. * part of the blogpost text. Note that proper artefact
  77. * children, such as blog post attachments, aren't included -
  78. * the artefact parent cache is used for them
  79. * @see PluginBlocktypeBlog::get_artefacts()
  80. */
  81. public static function get_artefacts(BlockInstance $instance) {
  82. $configdata = $instance->get('configdata');
  83. $artefacts = array();
  84. if (isset($configdata['artefactid'])) {
  85. $artefacts[] = $configdata['artefactid'];
  86. // Add all artefacts found in the blogpost text
  87. $blogpost = $instance->get_artefact_instance($configdata['artefactid']);
  88. $artefacts = array_unique(array_merge($artefacts, $blogpost->get_referenced_artefacts_from_postbody()));
  89. }
  90. return $artefacts;
  91. }
  92. public static function has_instance_config() {
  93. return true;
  94. }
  95. public static function instance_config_form($instance) {
  96. global $USER;
  97. safe_require('artefact', 'blog');
  98. $configdata = $instance->get('configdata');
  99. if (!empty($configdata['artefactid'])) {
  100. $blog = $instance->get_artefact_instance($configdata['artefactid']);
  101. }
  102. $elements = array();
  103. // If the blog post in this block is owned by the owner of the View,
  104. // then the block can be configured. Otherwise, the blog post was
  105. // copied in from another View. We won't confuse users by asking them to
  106. // choose a blog post to put in this block, when the one that is
  107. // currently in it isn't choosable.
  108. //
  109. // Note: the owner check will have to change when we do group/site
  110. // blogs
  111. if (empty($configdata['artefactid']) || $blog->get('owner') == $USER->get('id')) {
  112. $elements[] = self::artefactchooser_element((isset($configdata['artefactid'])) ? $configdata['artefactid'] : null);
  113. $elements[] = PluginArtefactBlog::block_advanced_options_element($configdata, 'blogpost');
  114. }
  115. else {
  116. $elements[] = array(
  117. 'type' => 'html',
  118. 'name' => 'notice',
  119. 'value' => '<div class="message">' . get_string('blogcopiedfromanotherview', 'artefact.blog', get_string('blogpost', 'artefact.blog')) . '</div>',
  120. );
  121. }
  122. return $elements;
  123. }
  124. public static function artefactchooser_element($default=null) {
  125. $extrajoin = ' JOIN {artefact_blog_blogpost} ON {artefact_blog_blogpost}.blogpost = a.id ';
  126. $element = array(
  127. 'name' => 'artefactid',
  128. 'type' => 'artefactchooser',
  129. 'title' => get_string('blogpost', 'artefact.blog'),
  130. 'defaultvalue' => $default,
  131. 'blocktype' => 'blogpost',
  132. 'limit' => 10,
  133. 'selectone' => true,
  134. 'artefacttypes' => array('blogpost'),
  135. 'extrajoin' => $extrajoin,
  136. 'extracols' => '1 - {artefact_blog_blogpost}.published AS draft',
  137. 'template' => 'artefact:blog:artefactchooser-element.tpl',
  138. );
  139. return $element;
  140. }
  141. /**
  142. * Optional method. If specified, allows the blocktype class to munge the
  143. * artefactchooser element data before it's templated
  144. */
  145. public static function artefactchooser_get_element_data($artefact) {
  146. static $blognames = array();
  147. if (!isset($blognames[$artefact->parent])) {
  148. $blognames[$artefact->parent] = get_field('artefact', 'title', 'id', $artefact->parent);
  149. }
  150. $artefact->blog = $blognames[$artefact->parent];
  151. $artefact->description = str_shorten_html($artefact->description, 50, true);
  152. return $artefact;
  153. }
  154. /**
  155. * Optional method. If specified, changes the order in which the artefacts are sorted in the artefact chooser.
  156. *
  157. * This is a valid SQL string for the ORDER BY clause. Fields you can sort on are as per the artefact table
  158. */
  159. public static function artefactchooser_get_sort_order() {
  160. return 'parent, ctime DESC';
  161. }
  162. public static function default_copy_type() {
  163. return 'nocopy';
  164. }
  165. /**
  166. * Blogpost blocktype is only allowed in personal views, because currently
  167. * there's no such thing as group/site blogs
  168. */
  169. public static function allowed_in_view(View $view) {
  170. return $view->get('owner') != null;
  171. }
  172. }
  173. ?>