course_notice_rss.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Formats in RSS the courses notices returned by CourseNoticeQuery.
  4. *
  5. * View of CourseNotice
  6. *
  7. * @license see /license.txt
  8. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  9. */
  10. class CourseNoticeRss
  11. {
  12. protected $query;
  13. function __construct($user_id = null, $limit = 20)
  14. {
  15. $this->query = CourseNoticeQuery::create($user_id, $limit);
  16. }
  17. /**
  18. * unique id used by the cache
  19. */
  20. function get_unique_id()
  21. {
  22. return strtolower(__CLASS__) . $this->get_query()->get_user_id();
  23. }
  24. /**
  25. *
  26. * @return CourseNoticeQuery
  27. */
  28. function get_query()
  29. {
  30. return $this->query;
  31. }
  32. function get_title()
  33. {
  34. return get_lang('CourseRssTitle');
  35. }
  36. function get_description()
  37. {
  38. return get_lang('CourseRssDescription');
  39. }
  40. function to_string()
  41. {
  42. return (string)$this;
  43. }
  44. function __toString()
  45. {
  46. ob_start();
  47. $this->display();
  48. $result = ob_get_clean();
  49. return $result;
  50. }
  51. function display()
  52. {
  53. $channel = $this->channel();
  54. echo <<<EOT
  55. <?xml version="1.0" encoding="UTF-8"?>
  56. <rss version="2.0"
  57. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  58. xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  59. xmlns:dc="http://purl.org/dc/elements/1.1/"
  60. xmlns:atom="http://www.w3.org/2005/Atom"
  61. xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  62. xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  63. >
  64. <channel>
  65. <title>{$channel->title}</title>
  66. <atom:link href="" rel="self" type="application/rss+xml" />
  67. <link>{$channel->link}</link>
  68. <description>{$channel->description}</description>
  69. <lastBuildDate>{$channel->last_build_date}</lastBuildDate>
  70. <language>{$channel->language}</language>
  71. <sy:updatePeriod>{$channel->update_period}</sy:updatePeriod>
  72. <sy:updateFrequency>{$channel->update_frequency}</sy:updateFrequency>
  73. <generator>{$channel->generator}</generator>
  74. EOT;
  75. foreach ($channel->items as $item)
  76. {
  77. echo <<<EOT
  78. <item>
  79. <title>{$item->title}</title>
  80. <link>{$item->link}</link>
  81. <pubDate>{$item->date}</pubDate>
  82. <dc:creator>{$item->author}</dc:creator>
  83. <category><![CDATA[{$item->course_title}]]></category>
  84. <description><![CDATA[{$item->description}]]></description>
  85. </item>
  86. EOT;
  87. }
  88. echo '</channel></rss>';
  89. }
  90. function channel()
  91. {
  92. $result = (object) array();
  93. $result->title = $this->get_title();
  94. $result->description = $this->get_description();
  95. $result->link = Uri::www();
  96. $result->last_build_date = time();
  97. $result->language = api_get_language_isocode();
  98. $result->update_period = 'hourly';
  99. $result->update_frequency = 1;
  100. $result->generator = Uri::chamilo();
  101. $items = $this->get_query()->get_items();
  102. $items = $this->format($items);
  103. $result->items = $items;
  104. return $result;
  105. }
  106. protected function format($items)
  107. {
  108. $result = array();
  109. foreach ($items as $item)
  110. {
  111. $result[] = $this->format_item($item);
  112. }
  113. return $result;
  114. }
  115. protected function format_item($item)
  116. {
  117. $result = (object) array();
  118. $item = (object) $item;
  119. $author = (object) UserManager::get_user_info_by_id($item->lastedit_user_id);
  120. $result->title = $item->title;
  121. $result->description = $item->description;
  122. $result->description .= $result->description ? '<br/>' : '';
  123. $result->description .= '<i>' . $item->course_title . ' &gt; ' . $this->get_tool_lang($item->tool) . ' &gt; ' . $item->title . '</i>';
  124. $result->date = date('r', strtotime($item->lastedit_date));
  125. $result->author = htmlentities($author->firstname . ' ' . $author->lastname . ' <' . $author->email . '>');
  126. $result->author_email = $author->email;
  127. $result->tool = $item->tool;
  128. $result->course_code = $item->code;
  129. $result->course_title = $item->course_title;
  130. $result->course_description = $item->course_description;
  131. $result->course_id = $item->c_id;
  132. $tool = $item->tool;
  133. $f = array($this, "format_$tool");
  134. if (is_callable($f))
  135. {
  136. call_user_func($f, $result, $item);
  137. }
  138. return $result;
  139. }
  140. protected function get_tool_lang($tool_name)
  141. {
  142. if ($tool_name = TOOL_CALENDAR_EVENT)
  143. {
  144. return get_lang('Agenda');
  145. }
  146. else if ($tool_name = TOOL_DOCUMENT)
  147. {
  148. return get_lang('Document');
  149. }
  150. else if ($tool_name = TOOL_LINK)
  151. {
  152. return get_lang('Link');
  153. }
  154. else if ($tool_name = TOOL_ANNOUNCEMENT)
  155. {
  156. return get_lang('Announcement');
  157. }
  158. }
  159. protected function format_document($result, $item)
  160. {
  161. $params = Uri::course_params($item->code, $item->session_id, $item->to_group_id);
  162. $params['id'] = $item->ref;
  163. $params['action'] = 'download';
  164. $result->link = Uri::url('main/document/document.php', $params);
  165. }
  166. protected function format_announcement($result, $item)
  167. {
  168. $params = Uri::course_params($item->code, $item->session_id, $item->to_group_id);
  169. $params['id'] = $item->ref;
  170. $params['action'] = 'view';
  171. $result->link = Uri::url('main/announcements/announcements.php', $params);
  172. }
  173. protected function format_link($result, $item)
  174. {
  175. $result->link = $item->url;
  176. }
  177. protected function format_calendar_event($result, $item)
  178. {
  179. $params = Uri::course_params($item->code, $item->session_id, $item->to_group_id);
  180. // . 'calendar/agenda.php?cidReq=' . $item->code . '#' . $item->id;
  181. $result->link = Uri::url('main/calendar/agenda.php', $params);
  182. //$result->description .= '<br/><i>' . $course->title . ' &gt; ' . get_lang('Agenda') . ' &gt; ' . $item->title . '</i>';
  183. }
  184. }