aboutsummaryrefslogtreecommitdiff
path: root/mod/tasks/start.php
blob: 822aa787f96237ffdef73681a9c4b41b401f8553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
/**
 * Elgg Tasks Management
 *
 * @package ElggTasks
 */

elgg_register_event_handler('init', 'system', 'tasks_init');

/**
 * Initialize the tasks management plugin.
 *
 */
function tasks_init() {

	// register a library of helper functions
	elgg_register_library('elgg:tasks', elgg_get_plugins_path() . 'tasks/lib/tasks.php');

	$item = new ElggMenuItem('tasks', elgg_echo('tasks'), 'tasks/all');
	elgg_register_menu_item('site', $item);

	// Register a page handler, so we can have nice URLs
	elgg_register_page_handler('tasks', 'tasks_page_handler');

	// Register a url handler
	elgg_register_entity_url_handler('object', 'task', 'tasks_url');

	// Register some actions
	$action_base = elgg_get_plugins_path() . 'tasks/actions/tasks';
	elgg_register_action("tasks/edit", "$action_base/edit.php");
	elgg_register_action("tasks/delete", "$action_base/delete.php");
	elgg_register_action("tasks/comments/add", "$action_base/comments/add.php");
	// Extend the main css and js views
	elgg_extend_view('css/elgg', 'tasks/css');
	elgg_extend_view('js/elgg', 'tasks/js');
	
	// register the blog's JavaScript
	elgg_register_simplecache_view('js/tasks/tasklists');
	elgg_register_js('elgg.tasks', elgg_get_simplecache_url('js', 'tasks/tasklists'));
	
	elgg_register_ajax_view('object/task');
	elgg_register_ajax_view('tasks/tasklist_graph');

	// Register entity type for search
	elgg_register_entity_type('object', 'task');
	
	// Register a different form for annotations
	elgg_register_plugin_hook_handler('comments', 'object', 'tasks_comments_hook');

	// Register granular notification for this type
	register_notification_object('object', 'task', elgg_echo('tasks:new'));
	elgg_register_plugin_hook_handler('notify:entity:message', 'object', 'tasks_notify_message');

	// add to groups
	add_group_tool_option('tasks', elgg_echo('groups:enabletasks'), true);
	elgg_extend_view('groups/tool_latest', 'tasks/group_module');

	//add a widget
	elgg_register_widget_type('tasks', elgg_echo('tasks:active'), elgg_echo('tasks:widget:description'));

	// Language short codes must be of the form "tasks:key"
	// where key is the array key below
	elgg_set_config('tasks', array(
		'title' => 'text',
		'description' => 'longtext',
		'list_guid' => 'tasks/list',
		'enddate' => 'date',
		'priority' => 'tasks/priority',
		'tags' => 'tags',
		'access_id' => 'access',
	));
	
	elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'tasks_owner_block_menu');

	elgg_register_event_handler('pagesetup', 'system', 'tasks_pagesetup');

	// write permission plugin hooks
	elgg_register_plugin_hook_handler('permissions_check', 'object', 'tasks_write_permission_check');
	elgg_register_plugin_hook_handler('container_permissions_check', 'object', 'tasks_container_permission_check');
	
	// icon url override
	elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'tasks_icon_url_override');

	// entity menu
	elgg_register_plugin_hook_handler('register', 'menu:entity', 'tasks_entity_menu_setup');

	// register ecml views to parse
	elgg_register_plugin_hook_handler('get_views', 'ecml', 'tasks_ecml_views_hook');
	
	elgg_register_plugin_hook_handler('format', 'friendly:time', 'tasks_get_friendly_time');
        elgg_register_event_handler('upgrade', 'system', 'tasks_run_upgrades');
}

function tasks_run_upgrades($event, $type, $details) {
        if (include_once(elgg_get_plugins_path() . 'upgrade-tools/lib/upgrade_tools.php')) {
        	upgrade_module_run('tasks');
	}
}


/**
 * Dispatcher for tasks.
 * URLs take the form of
 *  All tasks:        tasks/all
 *  User's tasks:     tasks/owner/<username>
 *  Friends' tasks:   tasks/friends/<username>
 *  View task:        tasks/view/<guid>/<title>
 *  New task:         tasks/add/<guid> (container: user, group; list: tasklist)
 *  Edit task:        tasks/edit/<guid>
 *  Group tasks:      tasks/group/<guid>/all
 *
 * Title is ignored
 *
 * @param array $page
 */
function tasks_page_handler($page) {

	elgg_load_library('elgg:tasks');

	if (!isset($page[0])) {
		$page[0] = 'all';
	}

	elgg_push_breadcrumb(elgg_echo('tasks'), 'tasks/all');

	$base_dir = elgg_get_plugins_path() . 'tasks/pages/tasks';

	$page_type = $page[0];
	switch ($page_type) {
		case 'owner':
			include "$base_dir/owner.php";
			break;
		case 'friends':
			include "$base_dir/friends.php";
			break;
		case 'view':
			set_input('guid', $page[1]);
			include "$base_dir/view.php";
			break;
		case 'add':
			set_input('guid', $page[1]);
			include "$base_dir/new_task.php";
			break;
		case 'edit':
			set_input('guid', $page[1]);
			include "$base_dir/edit_task.php";
			break;
		case 'group':
			include "$base_dir/owner.php";
			break;
		case 'all':
		default:
			include "$base_dir/world.php";
			break;
	}

	return;
}

/**
 * Override the task url
 * 
 * @param ElggObject $entity task object
 * @return string
 */
function tasks_url($entity) {
	$title = elgg_get_friendly_title($entity->title);
	return "tasks/view/$entity->guid/$title";
}

/**
 * Extend permissions checking to extend can-edit for write users.
 *
 * @param unknown_type $hook
 * @param unknown_type $entity_type
 * @param unknown_type $returnvalue
 * @param unknown_type $params
 */
function tasks_write_permission_check($hook, $entity_type, $returnvalue, $params)
{
	$entity = $params['entity'];
	if ($entity->getSubtype() == 'task') {

		$user = $params['user'];
		$container = $entity->getContainerEntity();
		if (elgg_instanceof($container, 'group')) {
			return $container->canWriteToContainer($user->guid, 'object', $entity->getSubtype());
		}
	}
}

/**
 * Extend container permissions checking to extend can_write_to_container for write users.
 *
 * @param unknown_type $hook
 * @param unknown_type $entity_type
 * @param unknown_type $returnvalue
 * @param unknown_type $params
 */
/*
function tasks_container_permission_check($hook, $entity_type, $returnvalue, $params) {

	if (elgg_get_context() == "tasks") {
		if (elgg_get_page_owner_guid()) {
			if (can_write_to_container(elgg_get_logged_in_user_guid(), elgg_get_page_owner_guid())) return true;
		}
		if ($page_guid = get_input('task_guid',0)) {
			$entity = get_entity($task_guid);
		} else if ($parent_guid = get_input('list_guid',0)) {
			$entity = get_entity($list_guid);
		}
		if ($entity instanceof ElggObject) {
			if (
					can_write_to_container(elgg_get_logged_in_user_guid(), $entity->container_guid)
					|| in_array($entity->write_access_id,get_access_list())
				) {
					return true;
			}
		}
	}

}
*/

/**
 * Override the default entity icon for tasks
 *
 * @return string Relative URL
 */
function tasks_icon_url_override($hook, $type, $returnvalue, $params) {
	$entity = $params['entity'];
	$size = $params['size'];
	if (elgg_instanceof($entity, 'object', 'task')) {
		$status = $entity->status;
		if($status == 'unassigned' || $status == 'reopened') {
			$status = 'new';
		}
		if (in_array($size, array('tiny', 'small', 'medium', 'large')) &&
			in_array($status, array('active', 'assigned', 'closed', 'done', 'new'))){
			return "mod/tasks/graphics/task-icons/$status-$size.png";
		}
	}
}

/**
 * Add a menu item to the user ownerblock
 */
function tasks_owner_block_menu($hook, $type, $return, $params) {
	if (elgg_instanceof($params['entity'], 'user')) {
		$url = "tasks/owner/{$params['entity']->username}";
		$item = new ElggMenuItem('tasks', elgg_echo('tasks'), $url);
		$return[] = $item;
	} else {
		if ($params['entity']->tasks_enable != "no") {
			$url = "tasks/group/{$params['entity']->guid}/all";
			$item = new ElggMenuItem('tasks', elgg_echo('tasks:group'), $url);
			$return[] = $item;
		}
	}

	return $return;
}

/**
 * Tasks extra menu item
 *
 */
function tasks_pagesetup() {
	if (elgg_is_logged_in()) {
		
		$container = elgg_get_page_owner_entity();
		if (!$container || !elgg_instanceof($container, 'group') || $container->tasks_enable != 'yes') {
			return;
		}
		
		$address = urlencode(current_page_url());
		
		elgg_register_menu_item('extras', array(
			'name' => 'task',
			'text' => elgg_view_icon('checkmark'),
			'href' => "tasks/add/$container->guid?referer=$address",
			'title' => elgg_echo('tasks:this'),
			'rel' => 'nofollow',
		));
	}
}

/**
 * Add links/info to entity menu particular to tasks plugin
 */
function tasks_entity_menu_setup($hook, $type, $return, $params) {
	if (elgg_in_context('widgets')) {
		return $return;
	}

	$entity = $params['entity'];
	$handler = elgg_extract('handler', $params, false);
	if ($handler != 'tasks') {
		return $return;
	}

	// remove delete if not owner or admin
	if (!elgg_is_admin_logged_in() && elgg_get_logged_in_user_guid() != $entity->getOwnerGuid()) {
		foreach ($return as $index => $item) {
			if ($item->getName() == 'delete') {
				unset($return[$index]);
				break;
			}
		}
	}
	
	if ($entity->status == 'active') {
		$options = array(
			'name' => 'active',
			'text' => elgg_echo('tasks:active'),
			'href' => false,
			'priority' => 150,
		);
		$return[] = ElggMenuItem::factory($options);
	}
	
	$priorities = array(
		'1' => 'low',
		'2' => 'normal',
		'3' => 'high',
	);
	
	$priority = $priorities[$entity->priority];
	
	$options = array(
		'name' => 'priority',
		'text' => elgg_echo("tasks:priority:$priority"),
		'href' => false,
		'priority' => 150,
	);
	
	$return[] = ElggMenuItem::factory($options);
	
	return $return;
}

function tasks_comments_hook($hook, $entity_type, $returnvalue, $params) {
	if($params['entity']->getSubtype() == 'task') {
		return elgg_view('tasks/page/elements/comments', $params);
	}
	return $returnvalue;
}

/**
* Returns a more meaningful message
*
* @param unknown_type $hook
* @param unknown_type $entity_type
* @param unknown_type $returnvalue
* @param unknown_type $params
*/
function tasks_notify_message($hook, $entity_type, $returnvalue, $params) {
	$entity = $params['entity'];
	if (elgg_instanceof($entity, 'object', 'task')) {
		$descr = $entity->description;
		$title = $entity->title;
		$owner = $entity->getOwnerEntity();
		return $owner->name . ' ' . elgg_echo("tasks:via") . ': ' . $title . "\n\n" . $descr . "\n\n" . $entity->getURL();
	}
	return null;
}




/**
 * Return views to parse for tasks.
 *
 * @param unknown_type $hook
 * @param unknown_type $entity_type
 * @param unknown_type $return_value
 * @param unknown_type $params
 */
function tasks_ecml_views_hook($hook, $entity_type, $return_value, $params) {
	$return_value['object/task'] = elgg_echo('item:object:task');

	return $return_value;
}

function tasks_get_friendly_time($hook, $type, $return_value, $params) {
	$time = (int) $params['time'];
	if ($time > time()) {
		$diff = $time - time();

		$minute = 60;
		$hour = $minute * 60;
		$day = $hour * 24;

		if ($diff < $minute) {
				return elgg_echo("friendlytime:justnow");
		} else if ($diff < $hour) {
			$diff = round($diff / $minute);
			if ($diff == 0) {
				$diff = 1;
			}

			if ($diff > 1) {
				return elgg_echo("friendlytime:future:minutes", array($diff));
			} else {
				return elgg_echo("friendlytime:future:minutes:singular", array($diff));
			}
		} else if ($diff < $day) {
			$diff = round($diff / $hour);
			if ($diff == 0) {
				$diff = 1;
			}

			if ($diff > 1) {
				return elgg_echo("friendlytime:future:hours", array($diff));
			} else {
				return elgg_echo("friendlytime:future:hours:singular", array($diff));
			}
		} else {
			$diff = round($diff / $day);
			if ($diff == 0) {
				$diff = 1;
			}

			if ($diff > 1) {
				return elgg_echo("friendlytime:future:days", array($diff));
			} else {
				return elgg_echo("friendlytime:future:days:singular", array($diff));
			}
		}
		$return_value = $time;
	}
	return $return_value;
}