start.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /**
  3. * Elgg Tasks Management
  4. *
  5. * @package ElggTasks
  6. */
  7. elgg_register_event_handler('init', 'system', 'tasks_init');
  8. /**
  9. * Initialize the tasks management plugin.
  10. *
  11. */
  12. function tasks_init() {
  13. // register a library of helper functions
  14. elgg_register_library('elgg:tasks', elgg_get_plugins_path() . 'tasks/lib/tasks.php');
  15. $item = new ElggMenuItem('tasks', elgg_echo('tasks'), 'tasks/all');
  16. elgg_register_menu_item('site', $item);
  17. // Register a page handler, so we can have nice URLs
  18. elgg_register_page_handler('tasks', 'tasks_page_handler');
  19. // Register a url handler
  20. elgg_register_entity_url_handler('object', 'task', 'tasks_url');
  21. // Register some actions
  22. $action_base = elgg_get_plugins_path() . 'tasks/actions/tasks';
  23. elgg_register_action("tasks/edit", "$action_base/edit.php");
  24. elgg_register_action("tasks/delete", "$action_base/delete.php");
  25. elgg_register_action("tasks/comments/add", "$action_base/comments/add.php");
  26. elgg_register_action("tasks/comments/delete", "$action_base/comments/delete.php");
  27. // Extend the main css and js views
  28. elgg_extend_view('css/elgg', 'tasks/css');
  29. elgg_extend_view('js/elgg', 'tasks/js');
  30. // register the blog's JavaScript
  31. elgg_register_simplecache_view('js/tasks/tasklists');
  32. elgg_register_js('elgg.tasks', elgg_get_simplecache_url('js', 'tasks/tasklists'));
  33. elgg_register_ajax_view('object/task');
  34. elgg_register_ajax_view('tasks/tasklist_graph');
  35. // Register entity type for search
  36. elgg_register_entity_type('object', 'task');
  37. // Register a different form for annotations
  38. elgg_register_plugin_hook_handler('comments', 'object', 'tasks_comments_hook');
  39. // Register granular notification for this type
  40. register_notification_object('object', 'task', elgg_echo('tasks:new'));
  41. elgg_register_plugin_hook_handler('notify:entity:message', 'object', 'tasks_notify_message');
  42. // add to groups
  43. add_group_tool_option('tasks', elgg_echo('groups:enabletasks'), true);
  44. elgg_extend_view('groups/tool_latest', 'tasks/group_module');
  45. //add a widget
  46. elgg_register_widget_type('tasks', elgg_echo('tasks:active'), elgg_echo('tasks:widget:description'));
  47. // Language short codes must be of the form "tasks:key"
  48. // where key is the array key below
  49. elgg_set_config('tasks', array(
  50. 'title' => 'text',
  51. 'description' => 'longtext',
  52. 'list_guid' => 'tasks/list',
  53. 'enddate' => 'date',
  54. 'priority' => 'tasks/priority',
  55. 'tags' => 'tags',
  56. 'access_id' => 'access',
  57. ));
  58. elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'tasks_owner_block_menu');
  59. elgg_register_event_handler('pagesetup', 'system', 'tasks_pagesetup');
  60. // write permission plugin hooks
  61. elgg_register_plugin_hook_handler('permissions_check', 'object', 'tasks_write_permission_check');
  62. elgg_register_plugin_hook_handler('container_permissions_check', 'object', 'tasks_container_permission_check');
  63. // icon url override
  64. elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'tasks_icon_url_override');
  65. // entity menu
  66. elgg_register_plugin_hook_handler('register', 'menu:entity', 'tasks_entity_menu_setup');
  67. // register ecml views to parse
  68. elgg_register_plugin_hook_handler('get_views', 'ecml', 'tasks_ecml_views_hook');
  69. elgg_register_plugin_hook_handler('format', 'friendly:time', 'tasks_get_friendly_time');
  70. elgg_register_event_handler('upgrade', 'system', 'tasks_run_upgrades');
  71. }
  72. function tasks_run_upgrades($event, $type, $details) {
  73. if (include_once(elgg_get_plugins_path() . 'upgrade-tools/lib/upgrade_tools.php')) {
  74. upgrade_module_run('tasks');
  75. }
  76. }
  77. /**
  78. * Dispatcher for tasks.
  79. * URLs take the form of
  80. * All tasks: tasks/all
  81. * User's tasks: tasks/owner/<username>
  82. * Friends' tasks: tasks/friends/<username>
  83. * View task: tasks/view/<guid>/<title>
  84. * New task: tasks/add/<guid> (container: user, group; list: tasklist)
  85. * Edit task: tasks/edit/<guid>
  86. * Group tasks: tasks/group/<guid>/all
  87. *
  88. * Title is ignored
  89. *
  90. * @param array $page
  91. */
  92. function tasks_page_handler($page) {
  93. elgg_load_library('elgg:tasks');
  94. if (!isset($page[0])) {
  95. $page[0] = 'all';
  96. }
  97. elgg_push_breadcrumb(elgg_echo('tasks'), 'tasks/all');
  98. $base_dir = elgg_get_plugins_path() . 'tasks/pages/tasks';
  99. $page_type = $page[0];
  100. switch ($page_type) {
  101. case 'owner':
  102. include "$base_dir/owner.php";
  103. break;
  104. case 'friends':
  105. include "$base_dir/friends.php";
  106. break;
  107. case 'view':
  108. set_input('guid', $page[1]);
  109. include "$base_dir/view.php";
  110. break;
  111. case 'add':
  112. set_input('guid', $page[1]);
  113. include "$base_dir/new_task.php";
  114. break;
  115. case 'edit':
  116. set_input('guid', $page[1]);
  117. include "$base_dir/edit_task.php";
  118. break;
  119. case 'group':
  120. include "$base_dir/owner.php";
  121. break;
  122. case 'all':
  123. default:
  124. include "$base_dir/world.php";
  125. break;
  126. }
  127. return;
  128. }
  129. /**
  130. * Override the task url
  131. *
  132. * @param ElggObject $entity task object
  133. * @return string
  134. */
  135. function tasks_url($entity) {
  136. $title = elgg_get_friendly_title($entity->title);
  137. return "tasks/view/$entity->guid/$title";
  138. }
  139. /**
  140. * Extend permissions checking to extend can-edit for write users.
  141. *
  142. * @param unknown_type $hook
  143. * @param unknown_type $entity_type
  144. * @param unknown_type $returnvalue
  145. * @param unknown_type $params
  146. */
  147. function tasks_write_permission_check($hook, $entity_type, $returnvalue, $params)
  148. {
  149. $entity = $params['entity'];
  150. if ($entity->getSubtype() == 'task') {
  151. $user = $params['user'];
  152. $container = $entity->getContainerEntity();
  153. if (elgg_instanceof($container, 'group')) {
  154. return $container->canWriteToContainer($user->guid, 'object', $entity->getSubtype());
  155. }
  156. }
  157. }
  158. /**
  159. * Extend container permissions checking to extend can_write_to_container for write users.
  160. *
  161. * @param unknown_type $hook
  162. * @param unknown_type $entity_type
  163. * @param unknown_type $returnvalue
  164. * @param unknown_type $params
  165. */
  166. /*
  167. function tasks_container_permission_check($hook, $entity_type, $returnvalue, $params) {
  168. if (elgg_get_context() == "tasks") {
  169. if (elgg_get_page_owner_guid()) {
  170. if (can_write_to_container(elgg_get_logged_in_user_guid(), elgg_get_page_owner_guid())) return true;
  171. }
  172. if ($page_guid = get_input('task_guid',0)) {
  173. $entity = get_entity($task_guid);
  174. } else if ($parent_guid = get_input('list_guid',0)) {
  175. $entity = get_entity($list_guid);
  176. }
  177. if ($entity instanceof ElggObject) {
  178. if (
  179. can_write_to_container(elgg_get_logged_in_user_guid(), $entity->container_guid)
  180. || in_array($entity->write_access_id,get_access_list())
  181. ) {
  182. return true;
  183. }
  184. }
  185. }
  186. }
  187. */
  188. /**
  189. * Override the default entity icon for tasks
  190. *
  191. * @return string Relative URL
  192. */
  193. function tasks_icon_url_override($hook, $type, $returnvalue, $params) {
  194. $entity = $params['entity'];
  195. $size = $params['size'];
  196. if (elgg_instanceof($entity, 'object', 'task')) {
  197. $status = $entity->status;
  198. if($status == 'unassigned' || $status == 'reopened') {
  199. $status = 'new';
  200. }
  201. if (in_array($size, array('tiny', 'small', 'medium', 'large')) &&
  202. in_array($status, array('active', 'assigned', 'closed', 'done', 'new'))){
  203. return "mod/tasks/graphics/task-icons/$status-$size.png";
  204. }
  205. }
  206. }
  207. /**
  208. * Add a menu item to the user ownerblock
  209. */
  210. function tasks_owner_block_menu($hook, $type, $return, $params) {
  211. if (elgg_instanceof($params['entity'], 'user')) {
  212. $url = "tasks/owner/{$params['entity']->username}";
  213. $item = new ElggMenuItem('tasks', elgg_echo('tasks'), $url);
  214. $return[] = $item;
  215. } else {
  216. if ($params['entity']->tasks_enable != "no") {
  217. $url = "tasks/group/{$params['entity']->guid}/all";
  218. $item = new ElggMenuItem('tasks', elgg_echo('tasks:group'), $url);
  219. $return[] = $item;
  220. }
  221. }
  222. return $return;
  223. }
  224. /**
  225. * Tasks extra menu item
  226. *
  227. */
  228. function tasks_pagesetup() {
  229. if (elgg_is_logged_in()) {
  230. $container = elgg_get_page_owner_entity();
  231. if (!$container || !elgg_instanceof($container, 'group') || $container->tasks_enable != 'yes') {
  232. return;
  233. }
  234. $address = urlencode(current_page_url());
  235. elgg_register_menu_item('extras', array(
  236. 'name' => 'task',
  237. 'text' => elgg_view_icon('checkmark'),
  238. 'href' => "tasks/add/$container->guid?referer=$address",
  239. 'title' => elgg_echo('tasks:this'),
  240. 'rel' => 'nofollow',
  241. ));
  242. }
  243. }
  244. /**
  245. * Add links/info to entity menu particular to tasks plugin
  246. */
  247. function tasks_entity_menu_setup($hook, $type, $return, $params) {
  248. if (elgg_in_context('widgets')) {
  249. return $return;
  250. }
  251. $entity = $params['entity'];
  252. $handler = elgg_extract('handler', $params, false);
  253. if ($handler != 'tasks') {
  254. return $return;
  255. }
  256. // remove delete if not owner or admin
  257. if (!elgg_is_admin_logged_in() && elgg_get_logged_in_user_guid() != $entity->getOwnerGuid()) {
  258. foreach ($return as $index => $item) {
  259. if ($item->getName() == 'delete') {
  260. unset($return[$index]);
  261. break;
  262. }
  263. }
  264. }
  265. if ($entity->status == 'active') {
  266. $options = array(
  267. 'name' => 'active',
  268. 'text' => elgg_echo('tasks:active'),
  269. 'href' => false,
  270. 'priority' => 150,
  271. );
  272. $return[] = ElggMenuItem::factory($options);
  273. }
  274. $priorities = array(
  275. '1' => 'low',
  276. '2' => 'normal',
  277. '3' => 'high',
  278. );
  279. $priority = $priorities[$entity->priority];
  280. $options = array(
  281. 'name' => 'priority',
  282. 'text' => elgg_echo("tasks:priority:$priority"),
  283. 'href' => false,
  284. 'priority' => 150,
  285. );
  286. $return[] = ElggMenuItem::factory($options);
  287. return $return;
  288. }
  289. function tasks_comments_hook($hook, $entity_type, $returnvalue, $params) {
  290. if($params['entity']->getSubtype() == 'task') {
  291. return elgg_view('tasks/page/elements/comments', $params);
  292. }
  293. return $returnvalue;
  294. }
  295. /**
  296. * Returns a more meaningful message
  297. *
  298. * @param unknown_type $hook
  299. * @param unknown_type $entity_type
  300. * @param unknown_type $returnvalue
  301. * @param unknown_type $params
  302. */
  303. function tasks_notify_message($hook, $entity_type, $returnvalue, $params) {
  304. $entity = $params['entity'];
  305. if (elgg_instanceof($entity, 'object', 'task')) {
  306. $descr = $entity->description;
  307. $title = $entity->title;
  308. $owner = $entity->getOwnerEntity();
  309. return $owner->name . ' ' . elgg_echo("tasks:via") . ': ' . $title . "\n\n" . $descr . "\n\n" . $entity->getURL();
  310. }
  311. return null;
  312. }
  313. /**
  314. * Return views to parse for tasks.
  315. *
  316. * @param unknown_type $hook
  317. * @param unknown_type $entity_type
  318. * @param unknown_type $return_value
  319. * @param unknown_type $params
  320. */
  321. function tasks_ecml_views_hook($hook, $entity_type, $return_value, $params) {
  322. $return_value['object/task'] = elgg_echo('item:object:task');
  323. return $return_value;
  324. }
  325. function tasks_get_friendly_time($hook, $type, $return_value, $params) {
  326. $time = (int) $params['time'];
  327. if ($time > time()) {
  328. $diff = $time - time();
  329. $minute = 60;
  330. $hour = $minute * 60;
  331. $day = $hour * 24;
  332. if ($diff < $minute) {
  333. return elgg_echo("friendlytime:justnow");
  334. } else if ($diff < $hour) {
  335. $diff = round($diff / $minute);
  336. if ($diff == 0) {
  337. $diff = 1;
  338. }
  339. if ($diff > 1) {
  340. return elgg_echo("friendlytime:future:minutes", array($diff));
  341. } else {
  342. return elgg_echo("friendlytime:future:minutes:singular", array($diff));
  343. }
  344. } else if ($diff < $day) {
  345. $diff = round($diff / $hour);
  346. if ($diff == 0) {
  347. $diff = 1;
  348. }
  349. if ($diff > 1) {
  350. return elgg_echo("friendlytime:future:hours", array($diff));
  351. } else {
  352. return elgg_echo("friendlytime:future:hours:singular", array($diff));
  353. }
  354. } else {
  355. $diff = round($diff / $day);
  356. if ($diff == 0) {
  357. $diff = 1;
  358. }
  359. if ($diff > 1) {
  360. return elgg_echo("friendlytime:future:days", array($diff));
  361. } else {
  362. return elgg_echo("friendlytime:future:days:singular", array($diff));
  363. }
  364. }
  365. $return_value = $time;
  366. }
  367. return $return_value;
  368. }