feed.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * Contains class block_rss_client\output\feed
  18. *
  19. * @package block_rss_client
  20. * @copyright 2015 Howard County Public School System
  21. * @author Brendan Anderson <brendan_anderson@hcpss.org>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. namespace block_rss_client\output;
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * Class to help display an RSS Feed
  28. *
  29. * @package block_rss_client
  30. * @copyright 2015 Howard County Public School System
  31. * @author Brendan Anderson <brendan_anderson@hcpss.org>
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class feed implements \renderable, \templatable {
  35. /**
  36. * The feed's title
  37. *
  38. * @var string
  39. */
  40. protected $title = null;
  41. /**
  42. * An array of renderable feed items
  43. *
  44. * @var array
  45. */
  46. protected $items = array();
  47. /**
  48. * The channel image
  49. *
  50. * @var channel_image
  51. */
  52. protected $image = null;
  53. /**
  54. * Whether or not to show the title
  55. *
  56. * @var boolean
  57. */
  58. protected $showtitle;
  59. /**
  60. * Whether or not to show the channel image
  61. *
  62. * @var boolean
  63. */
  64. protected $showimage;
  65. /**
  66. * Contructor
  67. *
  68. * @param string $title The title of the RSS feed
  69. * @param boolean $showtitle Whether to show the title
  70. * @param boolean $showimage Whether to show the channel image
  71. */
  72. public function __construct($title, $showtitle = true, $showimage = true) {
  73. $this->title = $title;
  74. $this->showtitle = $showtitle;
  75. $this->showimage = $showimage;
  76. }
  77. /**
  78. * Export this for use in a mustache template context.
  79. *
  80. * @see templatable::export_for_template()
  81. * @param renderer_base $output
  82. * @return stdClass
  83. */
  84. public function export_for_template(\renderer_base $output) {
  85. $data = array(
  86. 'title' => $this->showtitle ? $this->title : null,
  87. 'image' => null,
  88. 'items' => array(),
  89. );
  90. if ($this->showimage && $this->image) {
  91. $data['image'] = $this->image->export_for_template($output);
  92. }
  93. foreach ($this->items as $item) {
  94. $data['items'][] = $item->export_for_template($output);
  95. }
  96. return $data;
  97. }
  98. /**
  99. * Set the feed title
  100. *
  101. * @param string $title
  102. * @return \block_rss_client\output\feed
  103. */
  104. public function set_title($title) {
  105. $this->title = $title;
  106. return $this;
  107. }
  108. /**
  109. * Get the feed title
  110. *
  111. * @return string
  112. */
  113. public function get_title() {
  114. return $this->title;
  115. }
  116. /**
  117. * Add an RSS item
  118. *
  119. * @param \block_rss_client\output\item $item
  120. */
  121. public function add_item(item $item) {
  122. $this->items[] = $item;
  123. return $this;
  124. }
  125. /**
  126. * Set the RSS items
  127. *
  128. * @param array $items An array of renderable RSS items
  129. */
  130. public function set_items(array $items) {
  131. $this->items = $items;
  132. return $this;
  133. }
  134. /**
  135. * Get the RSS items
  136. *
  137. * @return array An array of renderable RSS items
  138. */
  139. public function get_items() {
  140. return $this->items;
  141. }
  142. /**
  143. * Set the channel image
  144. *
  145. * @param \block_rss_client\output\channel_image $image
  146. */
  147. public function set_image(channel_image $image) {
  148. $this->image = $image;
  149. }
  150. /**
  151. * Get the channel image
  152. *
  153. * @return channel_image
  154. */
  155. public function get_image() {
  156. return $this->image;
  157. }
  158. /**
  159. * Set showtitle
  160. *
  161. * @param boolean $showtitle
  162. * @return \block_rss_client\output\feed
  163. */
  164. public function set_showtitle($showtitle) {
  165. $this->showtitle = boolval($showtitle);
  166. return $this;
  167. }
  168. /**
  169. * Get showtitle
  170. *
  171. * @return boolean
  172. */
  173. public function get_showtitle() {
  174. return $this->showtitle;
  175. }
  176. /**
  177. * Set showimage
  178. *
  179. * @param boolean $showimage
  180. * @return \block_rss_client\output\feed
  181. */
  182. public function set_showimage($showimage) {
  183. $this->showimage = boolval($showimage);
  184. return $this;
  185. }
  186. /**
  187. * Get showimage
  188. *
  189. * @return boolean
  190. */
  191. public function get_showimage() {
  192. return $this->showimage;
  193. }
  194. }