start.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. elgg_register_event_handler('init','system','polls_init');
  3. function polls_init() {
  4. elgg_register_library('elgg:polls', elgg_get_plugins_path() . 'polls/models/model.php');
  5. // Set up menu for logged in users
  6. if (elgg_is_logged_in()) {
  7. $item = new ElggMenuItem('polls', elgg_echo('polls'), 'polls/owner/' . elgg_get_logged_in_user_entity()->username);
  8. } else {
  9. $item = new ElggMenuItem('polls', elgg_echo('poll'), 'polls/all');
  10. }
  11. elgg_register_menu_item('site', $item);
  12. // Extend system CSS with our own styles, which are defined in the polls/css view
  13. elgg_extend_view('css/elgg','polls/css');
  14. // Extend hover-over menu
  15. elgg_extend_view('profile/menu/links','polls/menu');
  16. // Register a page handler, so we can have nice URLs
  17. elgg_register_page_handler('polls','polls_page_handler');
  18. // Register a URL handler for poll posts
  19. elgg_register_entity_url_handler('object','poll','polls_url');
  20. // notifications
  21. $elgg_version = explode('.',get_version(true));
  22. if ($elgg_version[1] > 8) {
  23. $send_notification = elgg_get_plugin_setting('send_notification', 'polls');
  24. if (!$send_notification || $send_notification != 'no') {
  25. elgg_register_notification_event('object', 'poll');
  26. elgg_register_plugin_hook_handler('prepare', 'notification:create:object:poll', 'polls_prepare_notification');
  27. }
  28. }
  29. // add link to owner block
  30. elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'polls_owner_block_menu');
  31. elgg_register_plugin_hook_handler('widget_url', 'widget_manager', 'polls_widget_url_handler');
  32. // Register entity type
  33. elgg_register_entity_type('object','poll');
  34. // register the JavaScript
  35. $js = elgg_get_simplecache_url('js', 'polls/js');
  36. elgg_register_simplecache_view('js/polls/js');
  37. elgg_register_js('elgg.polls', $js);
  38. // add group widget
  39. $group_polls = elgg_get_plugin_setting('group_polls', 'polls');
  40. if (!$group_polls || $group_polls != 'no') {
  41. elgg_extend_view('groups/tool_latest', 'polls/group_module');
  42. }
  43. if (!$group_polls || ($group_polls == 'yes_default')) {
  44. add_group_tool_option('polls',elgg_echo('polls:enable_polls'),TRUE);
  45. } else if ($group_polls == 'yes_not_default') {
  46. add_group_tool_option('polls',elgg_echo('polls:enable_polls'),FALSE);
  47. }
  48. //add widgets
  49. // elgg_register_widget_type('poll',elgg_echo('polls:my_widget_title'),elgg_echo('polls:my_widget_description'), "profile,groups");
  50. elgg_register_widget_type('latestPolls',elgg_echo('polls:latest_widget_title'),elgg_echo('polls:latest_widget_description'), "index,profile,dashboard,groups");
  51. elgg_register_widget_type('poll_individual',elgg_echo('polls:individual'),elgg_echo('poll_individual_group:widget:description'), "index,profile,dashboard,groups");
  52. // Register actions
  53. $action_path = elgg_get_plugins_path() . 'polls/actions/polls';
  54. elgg_register_action("polls/add","$action_path/add.php");
  55. elgg_register_action("polls/edit","$action_path/edit.php");
  56. elgg_register_action("polls/delete","$action_path/delete.php");
  57. elgg_register_action("polls/vote","$action_path/vote.php");
  58. }
  59. /**
  60. * poll page handler; allows the use of fancy URLs
  61. *
  62. * @param array $page From the page_handler function
  63. * @return true|false Depending on success
  64. */
  65. function polls_page_handler($page) {
  66. elgg_load_library('elgg:polls');
  67. $page_type = $page[0];
  68. switch($page_type) {
  69. case "view":
  70. echo polls_get_page_view($page[1]);
  71. break;
  72. case "all":
  73. echo polls_get_page_list($page_type);
  74. break;
  75. case "add":
  76. case "edit":
  77. $container = null;
  78. if(isset($page[1])){
  79. $container = $page[1];
  80. }
  81. echo polls_get_page_edit($page_type,$container);
  82. break;
  83. case "friends":
  84. case "owner":
  85. $username = $page[1];
  86. $user = get_user_by_username($username);
  87. $user_guid = $user->guid;
  88. echo polls_get_page_list($page_type,$user_guid);
  89. break;
  90. case "group":
  91. echo polls_get_page_list($page_type,$page[1]);
  92. break;
  93. default:
  94. return FALSE;
  95. break;
  96. }
  97. return TRUE;
  98. }
  99. /**
  100. * Populates the ->getUrl() method for poll objects
  101. *
  102. * @param ElggEntity $pollpost poll post entity
  103. * @return string poll post URL
  104. */
  105. function polls_url($poll) {
  106. $title = elgg_get_friendly_title($poll->title);
  107. return "polls/view/" . $poll->guid . "/" . $title;
  108. }
  109. /**
  110. * Add a menu item to an owner block
  111. */
  112. function polls_owner_block_menu($hook, $type, $return, $params) {
  113. if (elgg_instanceof($params['entity'], 'user')) {
  114. $url = "polls/owner/{$params['entity']->username}";
  115. $item = new ElggMenuItem('polls', elgg_echo('polls'), $url);
  116. $return[] = $item;
  117. } else {
  118. elgg_load_library('elgg:polls');
  119. if (polls_activated_for_group($params['entity'])) {
  120. $url = "polls/group/{$params['entity']->guid}/all";
  121. $item = new ElggMenuItem('polls', elgg_echo('polls:group_polls'), $url);
  122. $return[] = $item;
  123. }
  124. }
  125. return $return;
  126. }
  127. /**
  128. * Prepare a notification message about a created poll
  129. *
  130. * @param string $hook Hook name
  131. * @param string $type Hook type
  132. * @param Elgg_Notifications_Notification $notification The notification to prepare
  133. * @param array $params Hook parameters
  134. * @return Elgg_Notifications_Notification
  135. */
  136. function polls_prepare_notification($hook, $type, $notification, $params) {
  137. $entity = $params['event']->getObject();
  138. $owner = $params['event']->getActor();
  139. $recipient = $params['recipient'];
  140. $language = $params['language'];
  141. $method = $params['method'];
  142. $notification->subject = elgg_echo('polls:add', array(), $language);
  143. $notification->body = elgg_echo('polls:notification', array(
  144. $owner->name,
  145. $entity->title,
  146. $entity->getURL()
  147. ), $language);
  148. $notification->summary = elgg_echo('polls:notify:summary', array($entity->title), $language);
  149. return $notification;
  150. }
  151. function polls_widget_url_handler($hook, $type, $return, $params){
  152. $result = $return;
  153. if(empty($result) && !empty($params) && is_array($params)){
  154. $widget = elgg_extract("entity", $params);
  155. if(!empty($widget) && elgg_instanceof($widget, "object", "widget")){
  156. switch($widget->handler){
  157. case "latestPolls":
  158. if($widget->context == "groups"){
  159. $result = "polls/group/" . $widget->getOwnerGUID() . "/all";
  160. } else {
  161. $result = "polls/all";
  162. }
  163. break;
  164. case "poll":
  165. if($widget->context == "groups"){
  166. $result = "polls/group/" . $widget->getOwnerGUID() . "/all";
  167. } else {
  168. $result = "polls/owner/" . $widget->getOwnerEntity()->username . "/all";
  169. }
  170. break;
  171. case "poll_individual":
  172. break;
  173. }
  174. }
  175. }
  176. return $result;
  177. }