tasks.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * Prepare the add/edit form variables
  4. *
  5. * @param ElggObject $tasklist
  6. * @return array
  7. */
  8. function tasklist_prepare_form_vars($tasklist = null, $list_guid = 0) {
  9. // input names => defaults
  10. $values = array(
  11. 'title' => '',
  12. 'description' => '',
  13. 'enddate' => '',
  14. 'access_id' => ACCESS_DEFAULT,
  15. 'write_access_id' => ACCESS_DEFAULT,
  16. 'tags' => '',
  17. 'container_guid' => elgg_get_page_owner_guid(),
  18. 'guid' => null,
  19. 'entity' => $tasklist,
  20. 'list_guid' => $list_guid,
  21. );
  22. if ($tasklist) {
  23. foreach (array_keys($values) as $field) {
  24. if (isset($tasklist->$field)) {
  25. $values[$field] = $tasklist->$field;
  26. }
  27. }
  28. }
  29. if (elgg_is_sticky_form('tasklist')) {
  30. $sticky_values = elgg_get_sticky_values('tasklist');
  31. foreach ($sticky_values as $key => $value) {
  32. $values[$key] = $value;
  33. }
  34. }
  35. elgg_clear_sticky_form('tasklist');
  36. return $values;
  37. }
  38. /**
  39. * Prepare the add/edit form variables
  40. *
  41. * @param ElggObject $task
  42. * @return array
  43. */
  44. function task_prepare_form_vars($task = null, $list_guid = 0) {
  45. // input names => defaults
  46. $values = array(
  47. 'title' => get_input('title', ''),
  48. 'description' => '',
  49. 'priority' => '',
  50. 'access_id' => ACCESS_DEFAULT,
  51. 'write_access_id' => ACCESS_DEFAULT,
  52. 'tags' => '',
  53. 'container_guid' => elgg_get_page_owner_guid(),
  54. 'guid' => null,
  55. 'entity' => $task,
  56. 'list_guid' => $list_guid,
  57. 'referer_guid' => get_input('referer_guid', ''),
  58. );
  59. if ($task) {
  60. foreach (array_keys($values) as $field) {
  61. if (isset($task->$field)) {
  62. $values[$field] = $task->$field;
  63. }
  64. }
  65. }
  66. if (elgg_is_sticky_form('task')) {
  67. $sticky_values = elgg_get_sticky_values('task');
  68. foreach ($sticky_values as $key => $value) {
  69. $values[$key] = $value;
  70. }
  71. }
  72. elgg_clear_sticky_form('task');
  73. if ($referer = get_input('referer')) {
  74. $link = elgg_view('output/url', array(
  75. 'href' => $referer,
  76. 'text' => elgg_echo('tasks:this:moreinfo:here'),
  77. ));
  78. $values['description'] .= "<p>" . elgg_echo('tasks:this:moreinfo', array($link)) . "</p>";
  79. }
  80. return $values;
  81. }
  82. function tasks_get_entities($options) {
  83. $default = array(
  84. 'type' => 'object',
  85. 'subtype' => 'task',
  86. );
  87. if (!isset($options['metadata_name_value_pairs']) || !is_array($options['metadata_name_value_pairs'])) {
  88. $options['metadata_name_value_pairs'] = array();
  89. }
  90. if (isset($options['list_guid'])) {
  91. $options['metadata_name_value_pairs'][] = array(
  92. 'name' => 'list_guid',
  93. 'value' => $options['list_guid'],
  94. );
  95. unset($options['parent_guid']);
  96. }
  97. if (isset($options['status'])) {
  98. $options['metadata_name_value_pairs'][] = array(
  99. 'name' => 'status',
  100. 'value' => $options['status'],
  101. );
  102. unset($options['status']);
  103. }
  104. $options = array_merge($default, $options);
  105. return elgg_get_entities_from_metadata($options);
  106. }
  107. function tasks_get_actions_from_state($state){
  108. switch($state) {
  109. case 'new':
  110. case 'unassigned':
  111. case 'reopened':
  112. $actions = array(
  113. 'assign',
  114. 'assign_and_activate',
  115. 'mark_as_done',
  116. 'close',
  117. );
  118. break;
  119. case 'assigned':
  120. $actions = array(
  121. 'activate',
  122. 'leave',
  123. 'mark_as_done',
  124. 'close',
  125. );
  126. break;
  127. case 'active':
  128. $actions = array(
  129. 'deactivate',
  130. 'leave',
  131. 'mark_as_done',
  132. 'close',
  133. );
  134. break;
  135. case 'done':
  136. case 'closed':
  137. $actions = array(
  138. 'reopen',
  139. );
  140. break;
  141. }
  142. return $actions;
  143. }
  144. function tasks_get_possible_actions($task, $user_guid = 0) {
  145. if(!$user_guid) {
  146. $user_guid = elgg_get_logged_in_user_guid();
  147. }
  148. $is_doing = check_entity_relationship($user_guid, 'is_doing', $task->guid);
  149. $subscribes = check_entity_relationship($user_guid, 'subscribes', $task->guid);
  150. if ($task->status == 'active' && $is_doing) {
  151. $status = 'active';
  152. } elseif ($task->status == 'assigned' && $subscribes) {
  153. $status = 'assigned';
  154. } elseif ($task->status == 'active' || $task->status == 'assigned') {
  155. $status = 'unassigned';
  156. } else {
  157. $status = $task->status;
  158. }
  159. return tasks_get_actions_from_state($status);
  160. }
  161. function tasks_prepare_radio_options($task) {
  162. $actions = tasks_get_possible_actions($task);
  163. $actions_labels = array(
  164. elgg_echo("tasks:state:action:noaction", array($task->status)) => '',
  165. );
  166. foreach($actions as $action) {
  167. $actions_labels[elgg_echo("tasks:state:action:$action")] = $action;
  168. }
  169. return $actions_labels;
  170. }
  171. function tasks_register_actions_menu($task) {
  172. foreach (tasks_get_possible_actions($task) as $action) {
  173. $action_url = "action/tasks/comments/add?" . http_build_query(array(
  174. 'entity_guid' => $task->guid,
  175. 'state_action' => $action,
  176. ), '', '&');
  177. $action_url = elgg_add_action_tokens_to_url($action_url);
  178. $item = new ElggMenuItem($action, elgg_echo("tasks:state:action:$action"), $action_url);
  179. elgg_register_menu_item('tasks_hover', $item);
  180. }
  181. }
  182. function tasks_reset_actions_menu() {
  183. global $CONFIG;
  184. if (isset($CONFIG->menus['tasks_hover'])) {
  185. unset($CONFIG->menus['tasks_hover']);
  186. }
  187. }
  188. function tasks_get_state_from_action($action){
  189. $actions_states = array(
  190. 'assign' => 'assigned',
  191. 'leave' => 'unassigned',
  192. 'activate' => 'active',
  193. 'deactivate' => 'assigned',
  194. 'assign_and_activate' => 'active',
  195. 'mark_as_done' => 'done',
  196. 'close' => 'closed',
  197. 'reopen' => 'reopened',
  198. );
  199. return $actions_states[$action];
  200. }
  201. function tasks_get_user_active_task ($user_guid) {
  202. return array_shift(elgg_get_entities_from_metadata(array(
  203. 'metadata_name' => 'status',
  204. 'metadata_value' => 'active',
  205. 'owner_guid' => $user_guid,
  206. )));
  207. }
  208. /**
  209. * Register the navigation menu
  210. *
  211. * @param ElggEntity $container Container entity for the tasks
  212. */
  213. function tasks_register_navigation_tree($container) {
  214. if (!$container) {
  215. return;
  216. }
  217. $tasklists_top = elgg_get_entities_from_metadata(array(
  218. 'type' => 'object',
  219. 'subtype' => 'task',
  220. 'container_guid' => $container->getGUID(),
  221. 'metadata_name' => 'list_guid',
  222. 'metadata_value' => 0,
  223. 'limit' => 0,
  224. ));
  225. if (!$tasklists_top) {
  226. return;
  227. }
  228. foreach ($tasklists_top as $tasklist) {
  229. elgg_register_menu_item('tasks_nav', array(
  230. 'name' => $tasklist->getGUID(),
  231. 'text' => $tasklist->title,
  232. 'href' => $tasklist->getURL(),
  233. ));
  234. $stack = array();
  235. array_push($stack, $tasklist);
  236. while (count($stack) > 0) {
  237. $list = array_pop($stack);
  238. $tasklists = elgg_get_entities_from_metadata(array(
  239. 'type' => 'object',
  240. 'subtype' => 'task',
  241. 'metadata_name' => 'list_guid',
  242. 'metadata_value' => $list->guid,
  243. 'container_guid' => $container->getGUID(),
  244. 'limit' => 0,
  245. ));
  246. if ($tasklists) {
  247. foreach ($tasklists as $tasklist) {
  248. elgg_register_menu_item('tasks_nav', array(
  249. 'name' => $tasklist->getGUID(),
  250. 'text' => $tasklist->title,
  251. 'href' => $tasklist->getURL(),
  252. 'parent_name' => $list->getGUID(),
  253. ));
  254. array_push($stack, $tasklist);
  255. }
  256. }
  257. }
  258. }
  259. }