moodleblock.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This file contains the parent class for moodle blocks, block_base.
  18. *
  19. * @package core_block
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  21. */
  22. /// Constants
  23. /**
  24. * Block type of list. Contents of block should be set as an associative array in the content object as items ($this->content->items). Optionally include footer text in $this->content->footer.
  25. */
  26. define('BLOCK_TYPE_LIST', 1);
  27. /**
  28. * Block type of text. Contents of block should be set to standard html text in the content object as items ($this->content->text). Optionally include footer text in $this->content->footer.
  29. */
  30. define('BLOCK_TYPE_TEXT', 2);
  31. /**
  32. * Block type of tree. $this->content->items is a list of tree_item objects and $this->content->footer is a string.
  33. */
  34. define('BLOCK_TYPE_TREE', 3);
  35. /**
  36. * Class for describing a moodle block, all Moodle blocks derive from this class
  37. *
  38. * @author Jon Papaioannou
  39. * @package core_block
  40. */
  41. class block_base {
  42. /**
  43. * Internal var for storing/caching translated strings
  44. * @var string $str
  45. */
  46. var $str;
  47. /**
  48. * The title of the block to be displayed in the block title area.
  49. * @var string $title
  50. */
  51. var $title = NULL;
  52. /**
  53. * The name of the block to be displayed in the block title area if the title is empty.
  54. * @var string arialabel
  55. */
  56. var $arialabel = NULL;
  57. /**
  58. * The type of content that this block creates. Currently support options - BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT
  59. * @var int $content_type
  60. */
  61. var $content_type = BLOCK_TYPE_TEXT;
  62. /**
  63. * An object to contain the information to be displayed in the block.
  64. * @var stdObject $content
  65. */
  66. var $content = NULL;
  67. /**
  68. * The initialized instance of this block object.
  69. * @var block $instance
  70. */
  71. var $instance = NULL;
  72. /**
  73. * The page that this block is appearing on.
  74. * @var moodle_page
  75. */
  76. public $page = NULL;
  77. /**
  78. * This blocks's context.
  79. * @var stdClass
  80. */
  81. public $context = NULL;
  82. /**
  83. * An object containing the instance configuration information for the current instance of this block.
  84. * @var stdObject $config
  85. */
  86. var $config = NULL;
  87. /**
  88. * How often the cronjob should run, 0 if not at all.
  89. * @var int $cron
  90. */
  91. var $cron = NULL;
  92. /// Class Functions
  93. /**
  94. * Fake constructor to keep PHP5 happy
  95. *
  96. */
  97. function __construct() {
  98. $this->init();
  99. }
  100. /**
  101. * Function that can be overridden to do extra cleanup before
  102. * the database tables are deleted. (Called once per block, not per instance!)
  103. */
  104. function before_delete() {
  105. }
  106. /**
  107. * Returns the block name, as present in the class name,
  108. * the database, the block directory, etc etc.
  109. *
  110. * @return string
  111. */
  112. function name() {
  113. // Returns the block name, as present in the class name,
  114. // the database, the block directory, etc etc.
  115. static $myname;
  116. if ($myname === NULL) {
  117. $myname = strtolower(get_class($this));
  118. $myname = substr($myname, strpos($myname, '_') + 1);
  119. }
  120. return $myname;
  121. }
  122. /**
  123. * Parent class version of this function simply returns NULL
  124. * This should be implemented by the derived class to return
  125. * the content object.
  126. *
  127. * @return stdObject
  128. */
  129. function get_content() {
  130. // This should be implemented by the derived class.
  131. return NULL;
  132. }
  133. /**
  134. * Returns the class $title var value.
  135. *
  136. * Intentionally doesn't check if a title is set.
  137. * This is already done in {@link _self_test()}
  138. *
  139. * @return string $this->title
  140. */
  141. function get_title() {
  142. // Intentionally doesn't check if a title is set. This is already done in _self_test()
  143. return $this->title;
  144. }
  145. /**
  146. * Returns the class $content_type var value.
  147. *
  148. * Intentionally doesn't check if content_type is set.
  149. * This is already done in {@link _self_test()}
  150. *
  151. * @return string $this->content_type
  152. */
  153. function get_content_type() {
  154. // Intentionally doesn't check if a content_type is set. This is already done in _self_test()
  155. return $this->content_type;
  156. }
  157. /**
  158. * Returns true or false, depending on whether this block has any content to display
  159. * and whether the user has permission to view the block
  160. *
  161. * @return boolean
  162. */
  163. function is_empty() {
  164. if ( !has_capability('moodle/block:view', $this->context) ) {
  165. return true;
  166. }
  167. $this->get_content();
  168. return(empty($this->content->text) && empty($this->content->footer));
  169. }
  170. /**
  171. * First sets the current value of $this->content to NULL
  172. * then calls the block's {@link get_content()} function
  173. * to set its value back.
  174. *
  175. * @return stdObject
  176. */
  177. function refresh_content() {
  178. // Nothing special here, depends on content()
  179. $this->content = NULL;
  180. return $this->get_content();
  181. }
  182. /**
  183. * Return a block_contents object representing the full contents of this block.
  184. *
  185. * This internally calls ->get_content(), and then adds the editing controls etc.
  186. *
  187. * You probably should not override this method, but instead override
  188. * {@link html_attributes()}, {@link formatted_contents()} or {@link get_content()},
  189. * {@link hide_header()}, {@link (get_edit_controls)}, etc.
  190. *
  191. * @return block_contents a representation of the block, for rendering.
  192. * @since Moodle 2.0.
  193. */
  194. public function get_content_for_output($output) {
  195. global $CFG;
  196. $bc = new block_contents($this->html_attributes());
  197. $bc->attributes['data-block'] = $this->name();
  198. $bc->blockinstanceid = $this->instance->id;
  199. $bc->blockpositionid = $this->instance->blockpositionid;
  200. if ($this->instance->visible) {
  201. $bc->content = $this->formatted_contents($output);
  202. if (!empty($this->content->footer)) {
  203. $bc->footer = $this->content->footer;
  204. }
  205. } else {
  206. $bc->add_class('invisible');
  207. }
  208. if (!$this->hide_header()) {
  209. $bc->title = $this->title;
  210. }
  211. if (empty($bc->title)) {
  212. $bc->arialabel = new lang_string('pluginname', get_class($this));
  213. $this->arialabel = $bc->arialabel;
  214. }
  215. if ($this->page->user_is_editing()) {
  216. $bc->controls = $this->page->blocks->edit_controls($this);
  217. } else {
  218. // we must not use is_empty on hidden blocks
  219. if ($this->is_empty() && !$bc->controls) {
  220. return null;
  221. }
  222. }
  223. if (empty($CFG->allowuserblockhiding)
  224. || (empty($bc->content) && empty($bc->footer))
  225. || !$this->instance_can_be_collapsed()) {
  226. $bc->collapsible = block_contents::NOT_HIDEABLE;
  227. } else if (get_user_preferences('block' . $bc->blockinstanceid . 'hidden', false)) {
  228. $bc->collapsible = block_contents::HIDDEN;
  229. } else {
  230. $bc->collapsible = block_contents::VISIBLE;
  231. }
  232. if ($this->instance_can_be_docked() && !$this->hide_header()) {
  233. $bc->dockable = true;
  234. }
  235. $bc->annotation = ''; // TODO MDL-19398 need to work out what to say here.
  236. return $bc;
  237. }
  238. /**
  239. * Convert the contents of the block to HTML.
  240. *
  241. * This is used by block base classes like block_list to convert the structured
  242. * $this->content->list and $this->content->icons arrays to HTML. So, in most
  243. * blocks, you probaby want to override the {@link get_contents()} method,
  244. * which generates that structured representation of the contents.
  245. *
  246. * @param $output The core_renderer to use when generating the output.
  247. * @return string the HTML that should appearn in the body of the block.
  248. * @since Moodle 2.0.
  249. */
  250. protected function formatted_contents($output) {
  251. $this->get_content();
  252. $this->get_required_javascript();
  253. if (!empty($this->content->text)) {
  254. return $this->content->text;
  255. } else {
  256. return '';
  257. }
  258. }
  259. /**
  260. * Tests if this block has been implemented correctly.
  261. * Also, $errors isn't used right now
  262. *
  263. * @return boolean
  264. */
  265. function _self_test() {
  266. // Tests if this block has been implemented correctly.
  267. // Also, $errors isn't used right now
  268. $errors = array();
  269. $correct = true;
  270. if ($this->get_title() === NULL) {
  271. $errors[] = 'title_not_set';
  272. $correct = false;
  273. }
  274. if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT, BLOCK_TYPE_TREE))) {
  275. $errors[] = 'invalid_content_type';
  276. $correct = false;
  277. }
  278. //following selftest was not working when roles&capabilities were used from block
  279. /* if ($this->get_content() === NULL) {
  280. $errors[] = 'content_not_set';
  281. $correct = false;
  282. }*/
  283. $formats = $this->applicable_formats();
  284. if (empty($formats) || array_sum($formats) === 0) {
  285. $errors[] = 'no_formats';
  286. $correct = false;
  287. }
  288. return $correct;
  289. }
  290. /**
  291. * Subclasses should override this and return true if the
  292. * subclass block has a settings.php file.
  293. *
  294. * @return boolean
  295. */
  296. function has_config() {
  297. return false;
  298. }
  299. /**
  300. * Default behavior: save all variables as $CFG properties
  301. * You don't need to override this if you 're satisfied with the above
  302. *
  303. * @deprecated since Moodle 2.9 MDL-49385 - Please use Admin Settings functionality to save block configuration.
  304. */
  305. function config_save($data) {
  306. throw new coding_exception('config_save() can not be used any more, use Admin Settings functionality to save block configuration.');
  307. }
  308. /**
  309. * Which page types this block may appear on.
  310. *
  311. * The information returned here is processed by the
  312. * {@link blocks_name_allowed_in_format()} function. Look there if you need
  313. * to know exactly how this works.
  314. *
  315. * Default case: everything except mod and tag.
  316. *
  317. * @return array page-type prefix => true/false.
  318. */
  319. function applicable_formats() {
  320. // Default case: the block can be used in courses and site index, but not in activities
  321. return array('all' => true, 'mod' => false, 'tag' => false);
  322. }
  323. /**
  324. * Default return is false - header will be shown
  325. * @return boolean
  326. */
  327. function hide_header() {
  328. return false;
  329. }
  330. /**
  331. * Return any HTML attributes that you want added to the outer <div> that
  332. * of the block when it is output.
  333. *
  334. * Because of the way certain JS events are wired it is a good idea to ensure
  335. * that the default values here still get set.
  336. * I found the easiest way to do this and still set anything you want is to
  337. * override it within your block in the following way
  338. *
  339. * <code php>
  340. * function html_attributes() {
  341. * $attributes = parent::html_attributes();
  342. * $attributes['class'] .= ' mynewclass';
  343. * return $attributes;
  344. * }
  345. * </code>
  346. *
  347. * @return array attribute name => value.
  348. */
  349. function html_attributes() {
  350. $attributes = array(
  351. 'id' => 'inst' . $this->instance->id,
  352. 'class' => 'block_' . $this->name(). ' block',
  353. 'role' => $this->get_aria_role()
  354. );
  355. if ($this->hide_header()) {
  356. $attributes['class'] .= ' no-header';
  357. }
  358. if ($this->instance_can_be_docked() && get_user_preferences('docked_block_instance_'.$this->instance->id, 0)) {
  359. $attributes['class'] .= ' dock_on_load';
  360. }
  361. return $attributes;
  362. }
  363. /**
  364. * Set up a particular instance of this class given data from the block_insances
  365. * table and the current page. (See {@link block_manager::load_blocks()}.)
  366. *
  367. * @param stdClass $instance data from block_insances, block_positions, etc.
  368. * @param moodle_page $the page this block is on.
  369. */
  370. function _load_instance($instance, $page) {
  371. if (!empty($instance->configdata)) {
  372. $this->config = unserialize(base64_decode($instance->configdata));
  373. }
  374. $this->instance = $instance;
  375. $this->context = context_block::instance($instance->id);
  376. $this->page = $page;
  377. $this->specialization();
  378. }
  379. /**
  380. * Allows the block to load any JS it requires into the page.
  381. *
  382. * By default this function simply permits the user to dock the block if it is dockable.
  383. */
  384. function get_required_javascript() {
  385. if ($this->instance_can_be_docked() && !$this->hide_header()) {
  386. user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
  387. }
  388. }
  389. /**
  390. * This function is called on your subclass right after an instance is loaded
  391. * Use this function to act on instance data just after it's loaded and before anything else is done
  392. * For instance: if your block will have different title's depending on location (site, course, blog, etc)
  393. */
  394. function specialization() {
  395. // Just to make sure that this method exists.
  396. }
  397. /**
  398. * Is each block of this type going to have instance-specific configuration?
  399. * Normally, this setting is controlled by {@link instance_allow_multiple()}: if multiple
  400. * instances are allowed, then each will surely need its own configuration. However, in some
  401. * cases it may be necessary to provide instance configuration to blocks that do not want to
  402. * allow multiple instances. In that case, make this function return true.
  403. * I stress again that this makes a difference ONLY if {@link instance_allow_multiple()} returns false.
  404. * @return boolean
  405. */
  406. function instance_allow_config() {
  407. return false;
  408. }
  409. /**
  410. * Are you going to allow multiple instances of each block?
  411. * If yes, then it is assumed that the block WILL USE per-instance configuration
  412. * @return boolean
  413. */
  414. function instance_allow_multiple() {
  415. // Are you going to allow multiple instances of each block?
  416. // If yes, then it is assumed that the block WILL USE per-instance configuration
  417. return false;
  418. }
  419. /**
  420. * Serialize and store config data
  421. */
  422. function instance_config_save($data, $nolongerused = false) {
  423. global $DB;
  424. $DB->set_field('block_instances', 'configdata', base64_encode(serialize($data)),
  425. array('id' => $this->instance->id));
  426. }
  427. /**
  428. * Replace the instance's configuration data with those currently in $this->config;
  429. */
  430. function instance_config_commit($nolongerused = false) {
  431. global $DB;
  432. $this->instance_config_save($this->config);
  433. }
  434. /**
  435. * Do any additional initialization you may need at the time a new block instance is created
  436. * @return boolean
  437. */
  438. function instance_create() {
  439. return true;
  440. }
  441. /**
  442. * Copy any block-specific data when copying to a new block instance.
  443. * @param int $fromid the id number of the block instance to copy from
  444. * @return boolean
  445. */
  446. public function instance_copy($fromid) {
  447. return true;
  448. }
  449. /**
  450. * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
  451. * @return boolean
  452. */
  453. function instance_delete() {
  454. return true;
  455. }
  456. /**
  457. * Allows the block class to have a say in the user's ability to edit (i.e., configure) blocks of this type.
  458. * The framework has first say in whether this will be allowed (e.g., no editing allowed unless in edit mode)
  459. * but if the framework does allow it, the block can still decide to refuse.
  460. * @return boolean
  461. */
  462. function user_can_edit() {
  463. global $USER;
  464. if (has_capability('moodle/block:edit', $this->context)) {
  465. return true;
  466. }
  467. // The blocks in My Moodle are a special case. We want them to inherit from the user context.
  468. if (!empty($USER->id)
  469. && $this->instance->parentcontextid == $this->page->context->id // Block belongs to this page
  470. && $this->page->context->contextlevel == CONTEXT_USER // Page belongs to a user
  471. && $this->page->context->instanceid == $USER->id) { // Page belongs to this user
  472. return has_capability('moodle/my:manageblocks', $this->page->context);
  473. }
  474. return false;
  475. }
  476. /**
  477. * Allows the block class to have a say in the user's ability to create new instances of this block.
  478. * The framework has first say in whether this will be allowed (e.g., no adding allowed unless in edit mode)
  479. * but if the framework does allow it, the block can still decide to refuse.
  480. * This function has access to the complete page object, the creation related to which is being determined.
  481. *
  482. * @param moodle_page $page
  483. * @return boolean
  484. */
  485. function user_can_addto($page) {
  486. global $USER;
  487. // The blocks in My Moodle are a special case and use a different capability.
  488. if (!empty($USER->id)
  489. && $page->context->contextlevel == CONTEXT_USER // Page belongs to a user
  490. && $page->context->instanceid == $USER->id // Page belongs to this user
  491. && $page->pagetype == 'my-index') { // Ensure we are on the My Moodle page
  492. // If the block cannot be displayed on /my it is ok if the myaddinstance capability is not defined.
  493. $formats = $this->applicable_formats();
  494. // Is 'my' explicitly forbidden?
  495. // If 'all' has not been allowed, has 'my' been explicitly allowed?
  496. if ((isset($formats['my']) && $formats['my'] == false)
  497. || (empty($formats['all']) && empty($formats['my']))) {
  498. // Block cannot be added to /my regardless of capabilities.
  499. return false;
  500. } else {
  501. $capability = 'block/' . $this->name() . ':myaddinstance';
  502. return $this->has_add_block_capability($page, $capability)
  503. && has_capability('moodle/my:manageblocks', $page->context);
  504. }
  505. }
  506. $capability = 'block/' . $this->name() . ':addinstance';
  507. if ($this->has_add_block_capability($page, $capability)
  508. && has_capability('moodle/block:edit', $page->context)) {
  509. return true;
  510. }
  511. return false;
  512. }
  513. /**
  514. * Returns true if the user can add a block to a page.
  515. *
  516. * @param moodle_page $page
  517. * @param string $capability the capability to check
  518. * @return boolean true if user can add a block, false otherwise.
  519. */
  520. private function has_add_block_capability($page, $capability) {
  521. // Check if the capability exists.
  522. if (!get_capability_info($capability)) {
  523. // Debug warning that the capability does not exist, but no more than once per page.
  524. static $warned = array();
  525. if (!isset($warned[$this->name()])) {
  526. debugging('The block ' .$this->name() . ' does not define the standard capability ' .
  527. $capability , DEBUG_DEVELOPER);
  528. $warned[$this->name()] = 1;
  529. }
  530. // If the capability does not exist, the block can always be added.
  531. return true;
  532. } else {
  533. return has_capability($capability, $page->context);
  534. }
  535. }
  536. static function get_extra_capabilities() {
  537. return array('moodle/block:view', 'moodle/block:edit');
  538. }
  539. /**
  540. * Can be overridden by the block to prevent the block from being dockable.
  541. *
  542. * @return bool
  543. */
  544. public function instance_can_be_docked() {
  545. global $CFG;
  546. return (!empty($CFG->allowblockstodock) && $this->page->theme->enable_dock);
  547. }
  548. /**
  549. * If overridden and set to false by the block it will not be hidable when
  550. * editing is turned on.
  551. *
  552. * @return bool
  553. */
  554. public function instance_can_be_hidden() {
  555. return true;
  556. }
  557. /**
  558. * If overridden and set to false by the block it will not be collapsible.
  559. *
  560. * @return bool
  561. */
  562. public function instance_can_be_collapsed() {
  563. return true;
  564. }
  565. /** @callback callback functions for comments api */
  566. public static function comment_template($options) {
  567. $ret = <<<EOD
  568. <div class="comment-userpicture">___picture___</div>
  569. <div class="comment-content">
  570. ___name___ - <span>___time___</span>
  571. <div>___content___</div>
  572. </div>
  573. EOD;
  574. return $ret;
  575. }
  576. public static function comment_permissions($options) {
  577. return array('view'=>true, 'post'=>true);
  578. }
  579. public static function comment_url($options) {
  580. return null;
  581. }
  582. public static function comment_display($comments, $options) {
  583. return $comments;
  584. }
  585. public static function comment_add(&$comments, $options) {
  586. return true;
  587. }
  588. /**
  589. * Returns the aria role attribute that best describes this block.
  590. *
  591. * Region is the default, but this should be overridden by a block is there is a region child, or even better
  592. * a landmark child.
  593. *
  594. * Options are as follows:
  595. * - landmark
  596. * - application
  597. * - banner
  598. * - complementary
  599. * - contentinfo
  600. * - form
  601. * - main
  602. * - navigation
  603. * - search
  604. *
  605. * @return string
  606. */
  607. public function get_aria_role() {
  608. return 'complementary';
  609. }
  610. }
  611. /**
  612. * Specialized class for displaying a block with a list of icons/text labels
  613. *
  614. * The get_content method should set $this->content->items and (optionally)
  615. * $this->content->icons, instead of $this->content->text.
  616. *
  617. * @author Jon Papaioannou
  618. * @package core_block
  619. */
  620. class block_list extends block_base {
  621. var $content_type = BLOCK_TYPE_LIST;
  622. function is_empty() {
  623. if ( !has_capability('moodle/block:view', $this->context) ) {
  624. return true;
  625. }
  626. $this->get_content();
  627. return (empty($this->content->items) && empty($this->content->footer));
  628. }
  629. protected function formatted_contents($output) {
  630. $this->get_content();
  631. $this->get_required_javascript();
  632. if (!empty($this->content->items)) {
  633. return $output->list_block_contents($this->content->icons, $this->content->items);
  634. } else {
  635. return '';
  636. }
  637. }
  638. function html_attributes() {
  639. $attributes = parent::html_attributes();
  640. $attributes['class'] .= ' list_block';
  641. return $attributes;
  642. }
  643. }
  644. /**
  645. * Specialized class for displaying a tree menu.
  646. *
  647. * The {@link get_content()} method involves setting the content of
  648. * <code>$this->content->items</code> with an array of {@link tree_item}
  649. * objects (these are the top-level nodes). The {@link tree_item::children}
  650. * property may contain more tree_item objects, and so on. The tree_item class
  651. * itself is abstract and not intended for use, use one of it's subclasses.
  652. *
  653. * Unlike {@link block_list}, the icons are specified as part of the items,
  654. * not in a separate array.
  655. *
  656. * @author Alan Trick
  657. * @package core_block
  658. * @internal this extends block_list so we get is_empty() for free
  659. */
  660. class block_tree extends block_list {
  661. /**
  662. * @var int specifies the manner in which contents should be added to this
  663. * block type. In this case <code>$this->content->items</code> is used with
  664. * {@link tree_item}s.
  665. */
  666. public $content_type = BLOCK_TYPE_TREE;
  667. /**
  668. * Make the formatted HTML ouput.
  669. *
  670. * Also adds the required javascript call to the page output.
  671. *
  672. * @param core_renderer $output
  673. * @return string HTML
  674. */
  675. protected function formatted_contents($output) {
  676. // based of code in admin_tree
  677. global $PAGE; // TODO change this when there is a proper way for blocks to get stuff into head.
  678. static $eventattached;
  679. if ($eventattached===null) {
  680. $eventattached = true;
  681. }
  682. if (!$this->content) {
  683. $this->content = new stdClass;
  684. $this->content->items = array();
  685. }
  686. $this->get_required_javascript();
  687. $this->get_content();
  688. $content = $output->tree_block_contents($this->content->items,array('class'=>'block_tree list'));
  689. if (isset($this->id) && !is_numeric($this->id)) {
  690. $content = $output->box($content, 'block_tree_box', $this->id);
  691. }
  692. return $content;
  693. }
  694. }