course_notice_controller.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Controller for course notice. Displays activity of courses tools to which
  4. * the user is subscribed in RSS format.
  5. *
  6. * Controller of CourseNotice.
  7. *
  8. * @license see /license.txt
  9. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  10. */
  11. class CourseNoticeController
  12. {
  13. /**
  14. * @return CourseNoticeController
  15. */
  16. public static function instance()
  17. {
  18. static $result = null;
  19. if(empty($result))
  20. {
  21. $result = new self();
  22. }
  23. return $result;
  24. }
  25. protected function __construct()
  26. {
  27. ;
  28. }
  29. /**
  30. * Name of the service for the API Key.
  31. * !! 10 chars max !!
  32. */
  33. public function get_service_name()
  34. {
  35. return 'chamilorss';
  36. }
  37. /**
  38. * Returns true if security accepts to run otherwise returns false.
  39. *
  40. * @return boolean
  41. */
  42. public function accept()
  43. {
  44. $user_id = $this->get_user_id();
  45. return ! empty($user_id);
  46. }
  47. /**
  48. * Returns the url used to access the rss for a specific user.
  49. * Note that the url is protected by token
  50. *
  51. * @return string
  52. */
  53. public function get_secret_url($action = 'display', $format = 'rss')
  54. {
  55. $user_id = $this->get_user_id();
  56. $token = $this->ensure_user_token();
  57. $params = array('user_id' => $user_id, 'token' => $token, 'format' => $format, 'action' => $action);
  58. return Uri::url('main/course_notice/index.php', $params);
  59. }
  60. /**
  61. * Returns the request user id
  62. *
  63. * @return int
  64. */
  65. public function get_user_id()
  66. {
  67. return api_get_user_id();
  68. }
  69. /**
  70. * Returns the format requested. Defaults to rss
  71. *
  72. * @return string
  73. */
  74. public function get_format()
  75. {
  76. return Request::get('format', 'rss');
  77. }
  78. /**
  79. * Returns the action requested. Defaults to display
  80. *
  81. * @return string
  82. */
  83. public function get_action()
  84. {
  85. return Request::get('action', 'display');
  86. }
  87. /**
  88. * Ensure a security token is available for the user and rss service- create one if needed.
  89. *
  90. * @return string The security token
  91. */
  92. public function ensure_user_token()
  93. {
  94. $user_id = $this->get_user_id();
  95. $service = $this->service_name();
  96. $keys = UserManager::get_api_keys($user_id, $service);
  97. if (empty($keys))
  98. {
  99. UserManager::add_api_key($user_id, $service);
  100. $keys = UserManager::get_api_keys($user_id, $service);
  101. }
  102. return end($keys);
  103. }
  104. /**
  105. * Run the controller. Ensure security and execute requested action.
  106. */
  107. public function run()
  108. {
  109. if (!$this->accept())
  110. {
  111. Display::display_header();
  112. Display::display_error_message(get_lang('NotAuthorized'));
  113. Display::display_footer();
  114. die;
  115. }
  116. $action = $this->get_action();
  117. $format = $this->get_format();
  118. $f = array($this, $action . '_' . $format);
  119. if (is_callable($f))
  120. {
  121. call_user_func($f);
  122. }
  123. }
  124. /**
  125. * Display rss action. Display the notfication Rss for the user.
  126. * Result is cached. It will refresh every hour.
  127. */
  128. public function display_rss()
  129. {
  130. Header::content_type_xml();
  131. $rss = new CourseNoticeRss($this->get_user_id());
  132. $limit = (time() - 3600);
  133. if ($result = Cache::get($rss, $limit))
  134. {
  135. echo $result;
  136. exit;
  137. }
  138. $result = $rss->to_string();
  139. Cache::put($rss, $result);
  140. echo $result;
  141. }
  142. }