123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- class CourseNoticeController
- {
-
- public static function instance()
- {
- static $result = null;
- if(empty($result))
- {
- $result = new self();
- }
- return $result;
- }
-
- protected function __construct()
- {
- ;
- }
-
- public function get_service_name()
- {
- return 'chamilorss';
- }
-
- public function accept()
- {
- $user_id = $this->get_user_id();
- return ! empty($user_id);
- }
-
- public function get_secret_url($action = 'display', $format = 'rss')
- {
- $user_id = $this->get_user_id();
- $token = $this->ensure_user_token();
- $params = array('user_id' => $user_id, 'token' => $token, 'format' => $format, 'action' => $action);
- return Uri::url('main/course_notice/index.php', $params);
- }
-
- public function get_user_id()
- {
- return api_get_user_id();
- }
-
- public function get_format()
- {
- return Request::get('format', 'rss');
- }
-
- public function get_action()
- {
- return Request::get('action', 'display');
- }
-
- public function ensure_user_token()
- {
- $user_id = $this->get_user_id();
- $service = $this->service_name();
- $keys = UserManager::get_api_keys($user_id, $service);
- if (empty($keys))
- {
- UserManager::add_api_key($user_id, $service);
- $keys = UserManager::get_api_keys($user_id, $service);
- }
- return end($keys);
- }
-
- public function run()
- {
- if (!$this->accept())
- {
- Display::display_header();
- Display::display_error_message(get_lang('NotAuthorized'));
- Display::display_footer();
- die;
- }
- $action = $this->get_action();
- $format = $this->get_format();
- $f = array($this, $action . '_' . $format);
- if (is_callable($f))
- {
- call_user_func($f);
- }
- }
-
- public function display_rss()
- {
- Header::content_type_xml();
- $rss = new CourseNoticeRss($this->get_user_id());
- $limit = (time() - 3600);
- if ($result = Cache::get($rss, $limit))
- {
- echo $result;
- exit;
- }
- $result = $rss->to_string();
- Cache::put($rss, $result);
- echo $result;
- }
- }
|