model.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /*
  3. checks for votes on the poll
  4. @param ElggEntity $poll
  5. @param guid
  6. @return true/false
  7. */
  8. function polls_check_for_previous_vote($poll, $user_guid)
  9. {
  10. $options = array(
  11. 'guid' => $poll->guid,
  12. 'type' => "object",
  13. 'subtype' => "poll",
  14. 'annotation_name' => "vote",
  15. 'annotation_owner_guid' => $user_guid,
  16. 'limit' => 1
  17. );
  18. $votes = elgg_get_annotations($options);
  19. if ($votes) {
  20. return true;
  21. } else {
  22. return false;
  23. }
  24. }
  25. function polls_get_choices($poll) {
  26. $options = array(
  27. 'relationship' => 'poll_choice',
  28. 'relationship_guid' => $poll->guid,
  29. 'inverse_relationship' => TRUE,
  30. 'order_by_metadata' => array('name'=>'display_order','direction'=>'ASC')
  31. );
  32. return elgg_get_entities_from_relationship($options);
  33. }
  34. function polls_get_choice_array($poll) {
  35. $choices = polls_get_choices($poll);
  36. $responses = array();
  37. if ($choices) {
  38. foreach($choices as $choice) {
  39. $responses[$choice->text] = $choice->text;
  40. }
  41. }
  42. return $responses;
  43. }
  44. function polls_add_choices($poll,$choices) {
  45. $i = 0;
  46. if ($choices) {
  47. foreach($choices as $choice) {
  48. $poll_choice = new ElggObject();
  49. $poll_choice->subtype = "poll_choice";
  50. $poll_choice->text = $choice;
  51. $poll_choice->display_order = $i*10;
  52. $poll_choice->access_id = $poll->access_id;
  53. $poll_choice->save();
  54. add_entity_relationship($poll_choice->guid, 'poll_choice', $poll->guid);
  55. $i += 1;
  56. }
  57. }
  58. }
  59. function polls_delete_choices($poll) {
  60. $choices = polls_get_choices($poll);
  61. if ($choices) {
  62. foreach($choices as $choice) {
  63. $choice->delete();
  64. }
  65. }
  66. }
  67. function polls_replace_choices($poll,$new_choices) {
  68. polls_delete_choices($poll);
  69. polls_add_choices($poll, $new_choices);
  70. }
  71. function polls_activated_for_group($group) {
  72. $group_polls = elgg_get_plugin_setting('group_polls', 'polls');
  73. if ($group && ($group_polls != 'no')) {
  74. if ( ($group->polls_enable == 'yes')
  75. || ((!$group->polls_enable && ((!$group_polls) || ($group_polls == 'yes_default'))))) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. function polls_can_add_to_group($group,$user=null) {
  82. $polls_group_access = elgg_get_plugin_setting('group_access', 'polls');
  83. if (!$polls_group_access || $polls_group_access == 'admins') {
  84. return $group->canEdit();
  85. } else {
  86. if (!$user) {
  87. $user = elgg_get_logged_in_user_guid();
  88. }
  89. return $group->canEdit() || $group->isMember($user);
  90. }
  91. }
  92. function polls_get_page_edit($page_type,$guid = 0) {
  93. gatekeeper();
  94. // Get the current page's owner
  95. $page_owner = elgg_get_page_owner_entity();
  96. if ($page_owner === false || is_null($page_owner)) {
  97. $page_owner = elgg_get_logged_in_user_entity();
  98. elgg_set_page_owner_guid($page_owner->guid);
  99. }
  100. $form_vars = array('id'=>'polls-edit-form');
  101. // Get the post, if it exists
  102. if ($page_type == 'edit') {
  103. $poll = get_entity($guid);
  104. if (elgg_instanceof($poll,'object','poll')) {
  105. $container_guid = $poll->container_guid;
  106. elgg_set_page_owner_guid($container_guid);
  107. $title = elgg_echo('polls:editpost', array($poll->title));
  108. $body_vars = array(
  109. 'fd' => polls_prepare_edit_body_vars($poll),
  110. 'entity' => $poll
  111. );
  112. if ($poll->canEdit()) {
  113. $content = elgg_view_form("polls/edit", $form_vars, $body_vars);
  114. } else {
  115. $content = elgg_echo('polls:permission_error');
  116. }
  117. // set breadcrumb
  118. elgg_push_breadcrumb(elgg_echo('item:object:poll'),'polls/all');
  119. $container = get_entity($container_guid);
  120. if (elgg_instanceof($container,'group')) {
  121. elgg_push_breadcrumb($container->name, 'polls/group/' . $container->getGUID());
  122. } else {
  123. elgg_push_breadcrumb($container->name, 'polls/owner/' . $container->username);
  124. }
  125. elgg_push_breadcrumb(elgg_echo("polls:edit"));
  126. } else {
  127. $title = elgg_echo('polls:error_title');
  128. $content = elgg_echo('polls:no_such_poll');
  129. }
  130. } else {
  131. // set breadcrumb
  132. elgg_push_breadcrumb(elgg_echo('item:object:poll'),'polls/all');
  133. if ($guid) {
  134. elgg_set_page_owner_guid($guid);
  135. $container = get_entity($guid);
  136. elgg_push_breadcrumb($container->name, 'polls/group/' . $container->getGUID());
  137. } else {
  138. $user = elgg_get_logged_in_user_entity();
  139. elgg_set_page_owner_guid($user->getGUID());
  140. elgg_push_breadcrumb($user->name, 'polls/owner/' . $user->username);
  141. }
  142. elgg_push_breadcrumb(elgg_echo('polls:add'));
  143. $title = elgg_echo('polls:addpost');
  144. $body_vars = array('fd'=>polls_prepare_edit_body_vars(),'container_guid'=>$guid);
  145. $content = elgg_view_form("polls/edit",$form_vars,$body_vars);
  146. }
  147. $params = array(
  148. 'title' => $title,
  149. 'content' => $content,
  150. 'filter' => '',
  151. );
  152. $body = elgg_view_layout('content', $params);
  153. // Display page
  154. return elgg_view_page($title,$body);
  155. }
  156. /**
  157. * Pull together variables for the edit form
  158. *
  159. * @param ElggObject $poll
  160. * @return array
  161. *
  162. * TODO - put choices in sticky form as well
  163. */
  164. function polls_prepare_edit_body_vars($poll = NULL) {
  165. // input names => defaults
  166. $values = array(
  167. 'question' => NULL,
  168. 'tags' => NULL,
  169. 'front_page' => NULL,
  170. 'access_id' => ACCESS_DEFAULT,
  171. 'guid' => NULL,
  172. );
  173. if ($poll) {
  174. foreach (array_keys($values) as $field) {
  175. if (isset($poll->$field)) {
  176. $values[$field] = $poll->$field;
  177. }
  178. }
  179. }
  180. if (elgg_is_sticky_form('polls')) {
  181. $sticky_values = elgg_get_sticky_values('polls');
  182. foreach ($sticky_values as $key => $value) {
  183. $values[$key] = $value;
  184. }
  185. }
  186. elgg_clear_sticky_form('polls');
  187. return $values;
  188. }
  189. function polls_get_page_list($page_type, $container_guid = NULL) {
  190. global $autofeed;
  191. $autofeed = TRUE;
  192. $user = elgg_get_logged_in_user_entity();
  193. $params = array();
  194. $options = array(
  195. 'type'=>'object',
  196. 'subtype'=>'poll',
  197. 'full_view' => FALSE,
  198. 'limit'=>15,
  199. );
  200. // set breadcrumb
  201. elgg_push_breadcrumb(elgg_echo('item:object:poll'), 'polls/all');
  202. if ($page_type == 'group') {
  203. $group = get_entity($container_guid);
  204. if (!elgg_instanceof($group,'group') || !polls_activated_for_group($group)) {
  205. forward();
  206. }
  207. $crumbs_title = $group->name;
  208. $params['title'] = elgg_echo('polls:group_polls:listing:title', array(htmlspecialchars($crumbs_title)));
  209. $params['filter'] = "";
  210. // set breadcrumb
  211. elgg_push_breadcrumb($crumbs_title);
  212. elgg_push_context('groups');
  213. elgg_set_page_owner_guid($container_guid);
  214. group_gatekeeper();
  215. $options['container_guid'] = $container_guid;
  216. $user_guid = elgg_get_logged_in_user_guid();
  217. if (elgg_get_page_owner_entity()->canWriteToContainer($user_guid)){
  218. elgg_register_menu_item('title', array(
  219. 'name' => 'add',
  220. 'href' => "polls/add/".$container_guid,
  221. 'text' => elgg_echo('polls:add'),
  222. 'class' => 'elgg-button elgg-button-action',
  223. ));
  224. }
  225. } else {
  226. switch ($page_type) {
  227. case 'owner':
  228. $options['owner_guid'] = $container_guid;
  229. $container_entity = get_user($container_guid);
  230. elgg_push_breadcrumb($container_entity->name);
  231. if ($user->guid == $container_guid) {
  232. $params['title'] = elgg_echo('polls:your');
  233. $params['filter_context'] = 'mine';
  234. } else {
  235. $params['title'] = elgg_echo('polls:not_me',array(htmlspecialchars($container_entity->name)));
  236. $params['filter_context'] = "";
  237. }
  238. break;
  239. case 'friends':
  240. $container_entity = get_user($container_guid);
  241. $friends = get_user_friends($container_guid, ELGG_ENTITIES_ANY_VALUE, 0);
  242. $options['container_guids'] = array();
  243. foreach ($friends as $friend) {
  244. $options['container_guids'][] = $friend->getGUID();
  245. }
  246. $params['filter_context'] = 'friends';
  247. $params['title'] = elgg_echo('polls:friends');
  248. elgg_push_breadcrumb($container_entity->name, "polls/owner/{$container_entity->username}");
  249. elgg_push_breadcrumb(elgg_echo('friends'));
  250. break;
  251. case 'all':
  252. $params['filter_context'] = 'all';
  253. $params['title'] = elgg_echo('item:object:poll');
  254. break;
  255. }
  256. $polls_site_access = elgg_get_plugin_setting('site_access', 'polls');
  257. if ((elgg_is_logged_in() && ($polls_site_access != 'admins')) || elgg_is_admin_logged_in()) {
  258. elgg_register_menu_item('title', array(
  259. 'name' => 'add',
  260. 'href' => "polls/add",
  261. 'text' => elgg_echo('polls:add'),
  262. 'class' => 'elgg-button elgg-button-action',
  263. ));
  264. }
  265. }
  266. if (($page_type == 'friends') && (count($options['container_guids']) == 0)) {
  267. // this person has no friends
  268. $params['content'] = '';
  269. } else {
  270. $params['content'] = elgg_list_entities($options);
  271. }
  272. if (!$params['content']) {
  273. $params['content'] = elgg_echo('polls:none');
  274. }
  275. $body = elgg_view_layout("content", $params);
  276. return elgg_view_page($params['title'],$body);
  277. }
  278. function polls_get_page_view($guid) {
  279. elgg_load_js('elgg.polls');
  280. $poll = get_entity($guid);
  281. if (elgg_instanceof($poll,'object','poll')) {
  282. // Set the page owner
  283. $page_owner = $poll->getContainerEntity();
  284. elgg_set_page_owner_guid($page_owner->guid);
  285. $title = $poll->title;
  286. $content = elgg_view_entity($poll, array('full_view' => TRUE));
  287. //check to see if comments are on
  288. if ($poll->comments_on != 'Off') {
  289. $content .= elgg_view_comments($poll);
  290. }
  291. elgg_push_breadcrumb(elgg_echo('item:object:poll'), "polls/all");
  292. if (elgg_instanceof($page_owner,'user')) {
  293. elgg_push_breadcrumb($page_owner->name, "polls/owner/{$page_owner->username}");
  294. } else {
  295. elgg_push_breadcrumb($page_owner->name, "polls/group/{$page_owner->guid}");
  296. }
  297. elgg_push_breadcrumb($poll->title);
  298. } else {
  299. // Display the 'post not found' page instead
  300. $title = elgg_echo("polls:notfound");
  301. $content = elgg_view("polls/notfound");
  302. elgg_push_breadcrumb(elgg_echo('item:object:poll'), "polls/all");
  303. elgg_push_breadcrumb($title);
  304. }
  305. $params = array('title' =>$title,'content' => $content,'filter'=>'');
  306. $body = elgg_view_layout('content', $params);
  307. // Display page
  308. return elgg_view_page($title,$body);
  309. }
  310. function polls_get_response_count($valueToCount, $fromArray) {
  311. $count = 0;
  312. if(is_array($fromArray))
  313. {
  314. foreach($fromArray as $item)
  315. {
  316. if($item->value == $valueToCount)
  317. {
  318. $count += 1;
  319. }
  320. }
  321. }
  322. return $count;
  323. }
  324. function polls_manage_front_page($poll,$front_page) {
  325. $poll_front_page = elgg_get_plugin_setting('front_page','polls');
  326. if(elgg_is_admin_logged_in() && ($poll_front_page == 'yes')) {
  327. $options = array(
  328. 'type' => 'object',
  329. 'subtype' => 'poll',
  330. 'metadata_name_value_pairs' => array(array('name'=>'front_page','value'=>1)),
  331. 'limit' => 1,
  332. );
  333. $polls = elgg_get_entities_from_metadata($options);
  334. if ($polls) {
  335. $front_page_poll = $polls[0];
  336. if ($front_page_poll->guid == $poll->guid) {
  337. if (!$front_page) {
  338. $front_page_poll->front_page = 0;
  339. }
  340. } else {
  341. $front_page_poll->front_page = 0;
  342. $poll->front_page = 1;
  343. }
  344. } else {
  345. $poll->front_page = 1;
  346. }
  347. }
  348. }