Task_model::_get_task_perms()   F
last analyzed

Complexity

Conditions 17
Paths 608

Size

Total Lines 137
Code Lines 71

Duplication

Lines 20
Ratio 14.6 %

Code Coverage

Tests 66
CRAP Score 19.8442

Importance

Changes 0
Metric Value
cc 17
eloc 71
nc 608
nop 1
dl 20
loc 137
ccs 66
cts 84
cp 0.7856
crap 19.8442
rs 2.3769
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Public Model Task_Model
5
 * @package Todo
6
 */
7
class Task_model extends CI_Model {
8
9
	protected $title, $description, $category, $priority, $due,
10
			$status, $user_id, $task_id, $reminder, $reminder_time,
11
			$groups, $group_perms, $friends, $friend_perms, $share_type;
12
13
	public $form_vals;
14
15
	// --------------------------------------------------------------------------
16
17 1
	public function __construct()
18
	{
19
		// $this->output->enable_profiler(TRUE);
20 1
	}
21
22
	// --------------------------------------------------------------------------
23
24
	/**
25
	 * Get day task list
26
	 *
27
	 * Gets tasks for calendar view
28
	 * @param int $start
29
	 * @param int $end
30
	 * @param int $num_days
31
	 * @return array
32
	 */
33 1
	public function get_day_task_list($start, $end, $num_days)
34
	{
35 1
		$uid = (int) $this->session->userdata('uid');
36
37
		//Get user's tasks
38 1
		$user_sql = $this->db->select('item.title, item.id, item.due')
39 1
			->from('item')
40 1
			->where('user_id', $uid)
41 1
			->where_not_in('status', [STATUS_COMPLETED, STATUS_CANCELED])
42 1
			->where('due >=', $start)
43 1
			->where('due <=', $end)
44 1
			->get_compiled_select();
45
46
		//Get group-shared tasks
47 1
		$group_sql = $this->db->select('item.title, item.id, item.due')
48 1
			->from('user')
49 1
			->join('group_users_link', 'group_users_link.user_id=user.id', 'inner')
50 1
			->join('group_task_link', 'group_task_link.group_id=group_users_link.group_id', 'inner')
51 1
			->join('item', 'item.id=group_task_link.task_id', 'inner')
52 1
			->where('todo_user.id', $uid)
53 1
			->where_not_in('status', [STATUS_COMPLETED, STATUS_CANCELED])
54 1
			->where('due >=', $start)
55 1
			->where('due <=', $end)
56 1
			->where('todo_group_task_link.permissions !=', PERM_NO_ACCESS)
57 1
			->get_compiled_select();
58
59
		//Get friend-shared tasks
60 1
		$friend_sql = $this->db->select('item.title, item.id, item.due')
61 1
			->from('user')
62 1
			->join('user_task_link', 'user_task_link.user_id=user.id', 'inner')
63 1
			->join('item', 'item.id=user_task_link.task_id', 'inner')
64 1
			->where('todo_user.id', $uid)
65 1
			->where_not_in('status', [STATUS_COMPLETED, STATUS_CANCELED])
66 1
			->where('due >=', $start)
67 1
			->where('due <=', $end)
68 1
			->where('todo_user_task_link.permissions !=', PERM_NO_ACCESS)
69 1
			->get_compiled_select();
70
71 1
		$sql = "{$user_sql}\nUNION\n{$group_sql}\nUNION\n{$friend_sql}";
72
73 1
		$cal_query = $this->db->query($sql);
74 1
		$task_array = $cal_query->result_array();
75
76
		//Some loopy variables
77 1
		$content = array();
78 1
		$due = $start;
79 1
		$due_end = $due + 86399;
80 1
		$day = 1;
81
82 1
		while($day <= $num_days)
83
		{
84 1
			foreach ($task_array as $task)
85
			{
86 1
				if($task['due'] >= $due && $task['due'] <= $due_end)
87 1
				{
88
					//@codeCoverageIgnoreStart
89
					if(isset($content[$day]))
90
					{
91
						$content[$day] .= '<li><a href="'.site_url('task/view/'.
92
								$task['id']).'">'.$task['title'].
93
						'</a><br /> due '.date('h:i A', $task['due']).'</li>';
94
					}
95
					//@codeCoverageIgnoreEnd
96
					else
97
					{
98 1
						$content[$day] = '<li><a href="'.site_url('task/view/'.
99 1
								$task['id']).'">'.$task['title'].
100 1
						'</a><br /> due '.date('h:i A', $task['due']).'</li>';
101
					}
102 1
				}
103
104 1
			}
105
106 1
			++$day;
107 1
			$due += 86400;
108 1
			$due_end += 86400;
109 1
		}
110
111 1
		return $content;
112
	}
113
114
	// --------------------------------------------------------------------------
115
116
	/**
117
	 * Get Checklist
118
	 *
119
	 * Returns Checklist for current task
120
	 * @param int $task_id
121
	 * @return array
122
	 */
123 1
	public function get_checklist($task_id)
124
	{
125
		//Get the checklist for the current task from the database
126 1
		$chk = $this->db->select('id, task_id, description, is_checked')
127 1
			->from('checklist')
128 1
			->where('task_id', $task_id)
129 1
			->order_by('is_checked', 'asc')
130 1
			->order_by('id')
131 1
			->get();
132
133 1
		return ($chk->num_rows() > 0) ? $chk->result_array() : array();
134
135
	}
136
137
	// --------------------------------------------------------------------------
138
139
	/**
140
	 * Add Checklist Item
141
	 *
142
	 * Adds a checklist item to the current checklist
143
	 * @return mixed bool/array
144
	 */
145
	public function add_checklist_item()
146
	{
147
		$task_id = (int)$this->input->post('task_id');
148
		$desc = $this->input->post('desc', TRUE);
149
150
		//Check if the current item already exists.
151
		$exists = $this->db->select('task_id, description')
152
			->from('checklist')
153
			->where('task_id', $task_id)
154
			->where('description', $desc)
155
			->get();
156
157 View Code Duplication
		if($exists->num_rows() < 1)
158
		{
159
			//Insert the item
160
			$this->db->set('task_id', $task_id)
161
				->set('description', $desc)
162
				->insert('checklist');
163
164
			//Return the row
165
			$return = $this->db->select('id, task_id, description, is_checked')
166
				->from('checklist')
167
				->where('task_id', $task_id)
168
				->where('description', $desc)
169
				->get();
170
171
			return $return->row_array();
172
		}
173
174
		return FALSE;
175
	}
176
177
	// --------------------------------------------------------------------------
178
179
	/**
180
	 * Delete Comment
181
	 *
182
	 * Deletes a comment from a task
183
	 * @param int $c_id
184
	 * @return int
185
	 */
186
	public function delete_comment($c_id)
187
	{
188
		//Get the user group id
189
		$uid = $this->session->userdata('uid');
190
191
		//Delete the comment that matches the c_id and uid
192
		$this->db->where('id', $c_id)
193
			->where('user_id', $uid)
194
			->delete('item_comments');
195
196
		return ($this->db->affected_rows() > 0)
197
			? $this->db->affected_rows()
198
			: -1;
199
	}
200
201
	// --------------------------------------------------------------------------
202
203
	/**
204
	 * Get Task List
205
	 *
206
	 * Retrieves the user's tasks from the database
207
	 * @return mixed
208
	 */
209 1
	public function get_task_list()
210
	{
211 1
		$this->db->from('todo_task_view')
212 1
			->where('user_id', (int) $this->session->userdata('uid'))
213 1
			->where_not_in('status_id', [STATUS_COMPLETED, STATUS_CANCELED]);
214
215 1
		$res = $this->db->get();
216
217 1
		if($res->num_rows()==0) return NULL;
218
219 1
		$result_array = array();
220 1
		$i=1;
221 1
		foreach($res->result_array() as $row)
222
		{
223 1
			$result_array[$i] = $row;
224 1
			$result_array[$i]['overdue'] = ($row['due'] < time() && $row['due'] != 0 || $row['priority'] == "Immediate");
225 1
			$i++;
226 1
		}
227
228 1
		return $result_array;
229
	}
230
231
	// --------------------------------------------------------------------------
232
233
	/**
234
	 * Get archived task list
235
	 *
236
	 * Retrieves the user's archived tasks from the database
237
	 *
238
	 * @param int $page
239
	 * @param int $per_page
240
	 * @return array
241
	 */
242 1
	public function get_archived_task_list($page=0, $per_page=10)
243
	{
244 1
		$offset = ($page == 1) ? 0 : $page;
245 1
		$limit = $per_page;
246
247
		// Get the number of tasks for pagination
248 1
		$this->db->select('item.id, user_id, category_id')
249 1
			->from('item')
250 1
			->where('user_id', $this->session->userdata('uid'))
251 1
			->where_in('status', [STATUS_COMPLETED, STATUS_CANCELED])
252 1
			->order_by('modified', 'desc');
253
254 1
		$r_rows = $this->db->get();
255
256 1
		$this->db->from('todo_task_view')
257 1
			->where('user_id', $this->session->userdata('uid'))
258 1
			->where_in('status_id', [STATUS_COMPLETED, STATUS_CANCELED])
259 1
			->order_by('modified', 'desc')
260 1
			->limit($limit, $offset);
261
262 1
		$res = $this->db->get();
263
264 1
		if($res->num_rows()==0) return NULL;
265
266 1
		$result_array = array();
267 1
		$i=1;
268 1 View Code Duplication
		foreach($res->result_array() as $row)
269
		{
270 1
			$result_array[$i] = $row;
271 1
			$result_array[$i]['overdue'] = FALSE;
272 1
			$i++;
273 1
		}
274
275 1
		$result_array['num_rows'] = $r_rows->num_rows();
276
277 1
		return $result_array;
278
	}
279
280
	// --------------------------------------------------------------------------
281
282
	/**
283
	 * Get overdue task list
284
	 *
285
	 * Retrieves the user's overdue tasks from the database
286
	 * @return array
287
	 */
288 1
	public function get_overdue_task_list()
289
	{
290 1
		$this->db->select('item.id, user_id, category_id, item.priority,
291
			status, item.title, due, modified, created,
292
			category.title as category, priority.value as priority,
293 1
			status.value as status')
294 1
			->from('item')
295 1
			->join('category', 'category.id=item.category_id', 'inner')
296 1
			->join('priority', 'priority.id=item.priority', 'inner')
297 1
			->join('status', 'status.id=item.status', 'inner')
298 1
			->where('user_id', (int) $this->session->userdata('uid'))
299
300 1
			->group_start()
301 1
			->where('due <', time())
302 1
			->or_where('item.priority', 9)
303 1
			->group_end()
304
305 1
			->where_not_in('status', [STATUS_COMPLETED, STATUS_CANCELED])
306 1
			->where('due !=', 0)
307 1
			->order_by('due', 'asc')
308 1
			->order_by('item.priority', 'desc');
309
310 1
		$res = $this->db->get();
311
312 1
		if($res->num_rows()==0)
313 1
			return NULL;
314
315 1
		$result_array = array();
316 1
		$i=1;
317 1 View Code Duplication
		foreach($res->result_array() as $row)
318
		{
319 1
			$result_array[$i] = $row;
320
321
			// Overdue is set as false to cut down on visual noise.
322
			// Since every task in the list is overdue, using the
323
			// visual style is redundant
324 1
			$result_array[$i]['overdue'] = FALSE;
325 1
			$i++;
326 1
		}
327
328 1
		return $result_array;
329
	}
330
331
	/**
332
	 * Get shared task list
333
	 *
334
	 * returns a list of shared tasks
335
	 * @return array
336
	 */
337 3
	public function get_shared_task_list()
338
	{
339 3
		$user_id = (int) $this->session->userdata('uid');
340
341 3
		$user_shared_sql = $this->db->select('item.id, user.id as user_id, category_id, item.priority,
342
			status, item.title, due, modified, created,
343
			category.title as category, priority.value as priority,
344 3
			status.value as status, group_task_link.permissions as group_perms')
345 3
			->distinct()
346 3
			->from('user')
347 3
			->join('group_users_link', 'group_users_link.user_id=user.id', 'inner')
348 3
			->join('group_task_link', 'group_task_link.group_id=group_users_link.group_id', 'inner')
349 3
			->join('item', 'item.id=group_task_link.task_id', 'inner')
350 3
			->join('category', 'category.id=item.category_id', 'inner')
351 3
			->join('priority', 'priority.id=item.priority', 'inner')
352 3
			->join('status', 'status.id=item.status', 'inner')
353 3
			->where('todo_user.id', $user_id)
354 3
			->where_not_in('status', [STATUS_COMPLETED, STATUS_CANCELED])
355 3
			->where('todo_group_task_link.permissions !=', PERM_NO_ACCESS)
356 3
			->get_compiled_select();
357
358 3
		$group_shared_sql = $this->db->select('item.id, user.id as user_id, category_id, item.priority,
359
			status, item.title, due, modified, created,
360
			category.title as category, priority.value as priority,
361 3
			status.value as status, user_task_link.permissions as user_perms')
362 3
			->distinct()
363 3
			->from('user')
364 3
			->join('user_task_link', 'user_task_link.user_id=user.id', 'inner')
365 3
			->join('item', 'item.id=user_task_link.task_id', 'inner')
366 3
			->join('category', 'category.id=item.category_id', 'inner')
367 3
			->join('priority', 'priority.id=item.priority', 'inner')
368 3
			->join('status', 'status.id=item.status', 'inner')
369 3
			->where('todo_user.id', $user_id)
370 3
			->where_not_in('status', [STATUS_COMPLETED, STATUS_CANCELED])
371 3
			->where('todo_user_task_link.permissions !=', PERM_NO_ACCESS)
372 3
			->get_compiled_select();
373
374 3
		$sql = "{$user_shared_sql}\nUNION ALL\n{$group_shared_sql}";
375
376 3
		$res = $this->db->query($sql);
377
378 3
		$now = time();
379
380 3
		$result_array = array();
381 3
		$i=1;
382 3
		foreach($res->result_array() as $row)
383
		{
384 2
			$result_array[$i] = $row;
385 2
			$result_array[$i]['overdue'] = ($result_array[$i]['due'] < $now && $result_array[$i]['due'] != 0);
386 2
			$i++;
387 3
		}
388
389 3
		return $result_array;
390
	}
391
392
	// --------------------------------------------------------------------------
393
394
	/**
395
	 * Validate Task
396
	 *
397
	 * Validates a new task before database submission.
398
	 * @return mixed
399
	 */
400 6
	public function validate_task()
401
	{
402
		// Clear previous validations
403 6
		$this->form_vals = NULL;
404
405 6
		$due = $this->input->post('due', TRUE);
406 6
		$due_hour = $this->input->post('due_hour', TRUE);
407 6
		$due_minute = $this->input->post('due_minute', TRUE);
408
409 6
		$err = array();
410
411
		// Basic validation
412 6
		$valid = $this->form_validation->run('task');
413
414 6
		if ( ! $valid)
415 6
		{
416 2
			$err = array_merge($err, (array)$this->form_validation->get_error_array());
417 2
		}
418
419
		//Check due date
420 6
		if ($due != 0)
421 6
		{
422
			//Verify date format
423 5
			$valid = $this->validation_callbacks->due_date($due);
424
425 5
			if ( ! $valid)
426 5
			{
427 1
				return $err;
428
			}
429
430 4
			$due_a = explode('-', $due);
431 4
			$min = $due_minute;
432 4
			$hour = $due_hour;
433
434 4
			$due_timestamp = mktime($hour,$min,0,$due_a[1],$due_a[2],$due_a[0]);
435
436
			//Return form values
437 4
			$this->form_vals['due'] = $due_timestamp;
438 4
			$this->form_vals['due_minute'] = $due_minute;
439 4
		}
440
		else
441
		{
442 1
			$due_timestamp = 0;
443
		}
444
445
		//If there is an email reminder
446 5
		if($this->input->post('reminder') == 'rem_true')
447 5
		{
448 2
			if($due === 0)
449 2
			{
450
				$err[] = "You must set a due date in order to get a reminder.";
451
			}
452
453 2
			if(!is_numeric($this->input->post('rem_hours')) OR
454 1
			!is_numeric($this->input->post('rem_minutes')))
455 2
			{
456 1
				$err[] = "You must put numeric hours and minutes for a reminder time.";
457 1
			}
458
			else
459
			{
460 1
				$reminder_hour = (int)$this->input->post('rem_hours');
461 1
				$reminder_min = (int)$this->input->post('rem_minutes');
462
463 1
				$seconds = ($reminder_hour * 3600)+($reminder_min * 60);
464 1
				$reminder_time = $due_timestamp - $seconds;
465
466 1
				$this->reminder = TRUE;
467 1
				$this->reminder_time = $reminder_time;
468
469
				//Return form values
470 1
				$this->form_vals['reminder'] = TRUE;
471 1
				$this->form_vals['rem_hours'] = $reminder_hour;
472 1
				$this->form_vals['rem_minutes'] = $reminder_min;
473
			}
474 2
		}
475
		else
476
		{
477 3
			$this->reminder = FALSE;
478
		}
479
480 5
		$share_type = FALSE;
481
482
		//If the task is shared
483 5
		if($this->input->post('share') != FALSE)
484 5
		{
485 2
			$groups = $this->input->post('group', TRUE);
486 2
			$group_perms = $this->input->post('group_perms', TRUE);
487 2
			$friends = $this->input->post('friend', TRUE);
488 2
			$friend_perms = $this->input->post('friend_perms', TRUE);
489
490 2
			if($groups != FALSE && $group_perms != FALSE)
491 2
			{
492 1
				$share_type = 'group';
493 1
			}
494
495 2
			if($friends != FALSE && $friend_perms != FALSE)
496 2
			{
497 1
				$share_type = 'friend';
498 1
			}
499
500 2
		}
501
502
		//If there aren't any errors
503 5
		if(empty($err))
504 5
		{
505 4
			$this->groups = ( ! empty($groups)) ? $groups : FALSE;
506 4
			$this->friends = ( ! empty($friends)) ? $friends : FALSE;
507 4
			$this->share_type = $share_type;
508 4
			$this->due = $due_timestamp;
509 4
			$this->friend_perms = (isset($friend_perms)) ? $friend_perms : FALSE;
510 4
			$this->group_perms = (isset($group_perms)) ? $group_perms : FALSE;
511 4
			$this->user_id = $this->session->userdata('uid');
512 4
			$this->task_id = ($this->input->post('task_id') != FALSE)
513 4
				? $this->input->post('task_id')
514
				: NULL; //$this->db->count_all('item') + 1;
515
516
/* ?><pre><?= print_r([
517
	'class' => $this,
518
	'input' => $this->input->post()
519
], TRUE); ?><?php die(); */
520
521 4
			return TRUE;
522
		}
523
524 1
		return $err;
525
	}
526
527
	// --------------------------------------------------------------------------
528
529
	/**
530
	 * Add Task
531
	 *
532
	 * Submits new task to database
533
	 * @return bool
534
	 */
535
	public function add_task()
536
	{
537
		$title = $this->input->post('title', TRUE);
538
		$desc = $this->input->post('desc', TRUE);
539
		$category = (int) $this->input->post('category');
540
		$priority = (int) $this->input->post('priority');
541
		$status = ($this->input->post('status') == FALSE) ? 1 : $this->input->post('status');
542
		$created = time();
543
544
		$due = $this->due;
545
		$uid = $this->user_id;
546
547
		$this->db->set('user_id', $uid)
548
			->set('category_id', $category)
549
			->set('priority', $priority)
550
			->set('status', $status)
551
			->set('title', $title)
552
			->set('description', $desc)
553
			->set('due', $due)
554
			->set('created', $created)
555
			->set('modified', 0);
556
557
		$this->db->insert('item');
558
559
		if($this->db->affected_rows() < 1)
560
			return FALSE;
561
562
		//Get last inserted task
563
		$query = $this->db->select('max(id) as id')->from('item')->get();
564
			$row = $query->row();
565
			$task_id = $row->id;
566
567
		//Get groups
568 View Code Duplication
		if($this->groups != FALSE)
569
		{
570
			if($this->group_perms != FALSE)
571
			{
572
				foreach($this->groups as $group)
573
				{
574
					$this->db->set('group_id', $group)
575
						->set('task_id', $task_id)
576
						->set('permissions', $this->group_perms)
577
						->insert('group_task_link');
578
				}
579
			}
580
		}
581
582
		//Get friends
583
		if($this->friends != FALSE)
584
		{
585
			if($this->friend_perms != FALSE)
586
			{
587 View Code Duplication
				foreach($this->friends as $friend)
588
				{
589
					$this->db->set('user_id', $friend)
590
						->set('task_id', $task_id)
591
						->set('permissions', $this->friend_perms)
592
						->insert('user_task_link');
593
				}
594
			}
595
		}
596
597
598
		if($this->reminder == TRUE)
599
		{
600
			$reminder_time = $this->reminder_time;
601
			$this->_add_reminder($task_id, $reminder_time);
602
		}
603
604
		return TRUE;
605
606
	}
607
608
	// --------------------------------------------------------------------------
609
610
	/**
611
	 * Update Task
612
	 *
613
	 * Updates current task
614
	 * @return bool
615
	 */
616
	public function update_task()
617
	{
618
619
		$due_timestamp = $this->input->post('due') . ' ' .
620
			$this->input->post('due_hour') . ':' .
621
			$this->input->post('due_minute');
622
623
		$unix_due = strtotime($due_timestamp);
624
625
		$title = $this->input->post('title');;
626
		$desc = str_replace('<br>', '<br />', $this->input->post('desc'));
627
		$category = $this->input->post('category');
628
		$priority = $this->input->post('priority');
629
		$status = $this->input->post('status');
630
		$due = $unix_due;
631
		$uid = $this->user_id;
632
		$task_id = $this->task_id;
633
634
		$this->db->set('category_id', $category)
635
			->set('priority', $priority)
636
			->set('status', $status)
637
			->set('title', $title)
638
			->set('description', $desc)
639
			->set('due', (int)$due)
640
			->set('modified', time())
641
			->where('id', $task_id)
642
			->where('user_id', $uid);
643
644
		$this->db->update('item');
645
646
		//Check the status separately, to account for email reminders
647
		$this->update_status();
648
649
		if($this->reminder == TRUE)
650
		{
651
			$reminder_time = $this->reminder_time;
652
			$this->_add_reminder($task_id, $reminder_time);
653
		}
654
		else
655
		{
656
			// Delete old reminders
657
			$this->db->where('task_id', $task_id)
658
				->delete('reminder');
659
		}
660
661
		// Remove old shared permissions
662
		{
663
			// Delete existing groups and users
664
			$group_list = $this->_get_task_groups($task_id);
665
666
			// Delete groups
667
			if ( ! empty($group_list))
668
			{
669
				$this->db->where_in('group_id', $group_list)
670
					->where('task_id', $task_id)
671
					->delete('group_task_link');
672
			}
673
674
			// Delete friends
675
			$friend_list = $this->_get_task_users($task_id);
676
677
			if ( ! empty($friend_list))
678
			{
679
				$user_ids = array_merge(
680
					[(int) $this->session->userdata('uid')],
681
					$friend_list
682
				);
683
				$this->db->where_in('user_id', $user_ids)
684
					->where('task_id', $task_id)
685
					->delete('user_task_link');
686
			}
687
688
		}
689
690
		//Get groups
691 View Code Duplication
		if($this->share_type == 'group')
692
		{
693
			if($this->group_perms !== FALSE)
694
			{
695
				foreach($this->groups as $group)
696
				{
697
					$this->db->set('group_id', $group)
698
						->set('task_id', $task_id)
699
						->set('permissions', $this->group_perms)
700
						->insert('group_task_link');
701
				}
702
			}
703
		}
704
705
		//Get friends
706
		if($this->share_type == 'friend')
707
		{
708
			if($this->friend_perms !== FALSE)
709
			{
710 View Code Duplication
				foreach($this->friends as $friend)
711
				{
712
					$this->db->set('user_id', $friend)
713
						->set('task_id', $task_id)
714
						->set('permissions', $this->friend_perms)
715
						->insert('user_task_link');
716
				}
717
718
				if ($this->db->affected_rows() < 1)
719
				{
720
					return false;
721
				}
722
723
				//Set current user too
724
				$this->db->set('user_id', $this->session->userdata('uid'))
725
					->set('task_id', $task_id)
726
					->set('permissions', $this->friend_perms)
727
					->insert('user_task_link');
728
			}
729
		}
730
731
		return true;
732
	}
733
734
	// --------------------------------------------------------------------------
735
736
	/**
737
	 * Get Task By Id
738
	 *
739
	 * Retrieves task from database by task id
740
	 * @param int $task_id
741
	 * @return array
742
	 */
743 2
	public function get_task_by_id($task_id)
744
	{
745
		//Get the task
746 2
		$task = $this->db->select(
747
				'item.id,
748
				item.user_id,
749
				item.priority,
750
				item.title,
751
				item.due,
752
				item.modified,
753
				item.created,
754
				item.description,
755
				user.username,
756
				status.value as current_status,
757
				priority.value as priority,
758
				category.title as cat_name'
759 2
		)
760 2
		->from('item')
761 2
		->join('user', 'user.id=todo_item.user_id', 'inner')
762 2
		->join('category', 'category.id=todo_item.category_id', 'inner')
763 2
		->join('priority', 'priority.id=todo_item.priority', 'inner')
764 2
		->join('status', 'status.id=todo_item.status', 'inner')
765 2
		->where('todo_item.id', (int) $task_id)
766 2
		->get();
767
768 2
		$task_array = $task->row_array();
769
770
		//Get the task permissions
771 2
		$result_array = array_merge($task_array, $this->_get_task_perms($task_id));
772
773
		//Get selected groups
774 2
		$result_array['selected_groups'] = $this->_get_task_groups($task_id);
775
776
		//Get selected friends
777 2
		$result_array['selected_friends'] = $this->_get_task_users($task_id);
778
779
		//Get any related task reminders
780 2
		$query2 = $this->db->select('task_id, reminder_time')
781 2
			->from('reminder')
782 2
			->where('task_id', $task_id)
783 2
			->where('user_id', $this->session->userdata('uid'))
784 2
			->get();
785
786
		//If there aren't any reminders
787 2
		if($query2->num_rows() < 1)
788 2
		{
789 1
			$result_array['reminder'] = FALSE;
790 1
			$result_array['rem_hours'] = 0;
791 1
			$result_array['rem_minutes'] = 30;
792 1
			return $result_array;
793
		}
794
		else //There are reminders
795
		{
796 1
			$res2 = $query2->row();
797
798 1
			$result_array['reminder'] = TRUE;
799
800
			//Time until task is due, in seconds
801 1
			$until_due = $result_array['due'] - $res2->reminder_time;
802
803
			//In hours
804 1
			$until_hours = ($until_due >= 3600) ? floor((int)$until_due / 3600) : 0;
805
806
			//In additional minutes
807 1
			$until_seconds = (int)$until_due - ($until_hours * 3600);
808 1
			$until_minutes = (int)($until_seconds / 60);
809
810 1
			$result_array['rem_hours'] = $until_hours;
811 1
			$result_array['rem_minutes'] = $until_minutes;
812
813 1
			return $result_array;
814
		}
815
	}
816
817
	// --------------------------------------------------------------------------
818
819
	/**
820
	 * Get the status id for the selected task
821
	 *
822
	 * @param int $task_id
823
	 * @return int
824
	 */
825 3
	public function get_current_status_id($task_id=0)
826
	{
827
		// @codeCoverageIgnoreStart
828
		if($task_id==0)
829
		{
830
			$task_id=$this->uri->segment($this->uri->total_segments());
831
		}
832
		// @codeCoverageIgnoreEnd
833
834
		//Get the status from the task
835 3
		$task = $this->db->select('id, status')
836 3
			->from('item')
837 3
			->where('id', (int) $task_id)
838 3
			->get();
839
840 3
		$trow = $task->row();
841 3
		$status_id = $trow->status;
842
843 3
		return $status_id;
844
	}
845
846
	// --------------------------------------------------------------------------
847
848
	/**
849
	 * Get Status Select
850
	 *
851
	 * Returns select options for task status
852
	 * @param int $task_id
853
	 * @param int $status_id
854
	 * @return string
855
	 */
856 2
	public function get_status_select($task_id=0, $status_id=NULL)
857
	{
858 2
		$html = '';
859
860 2
		if (is_null($status_id))
861 2
		{
862 1
			$status_id = $this->get_current_status_id($task_id);
863 1
		}
864
865
		//Get the list of statuses
866 2
		$query = $this->db->select('id, value as desc')
867 2
				->from('status')
868 2
				->order_by('id')
869 2
				->get();
870
871 2 View Code Duplication
		foreach($query->result() as $row)
872
		{
873 2
			$html .= T5.'<option value="'.$row->id.'"';
874
			//Mark the appropriate one selected
875 2
			$html .= ($row->id == $status_id) ? ' selected="selected">': '>';
876 2
			$html .= $row->desc;
877 2
			$html .= '</option>'.NL;
878 2
		}
879
880 2
		return $html;
881
	}
882
883
	// --------------------------------------------------------------------------
884
885
	/**
886
	 * Get Priority Select
887
	 *
888
	 * Returns priority options for task status
889
	 * @param int $task_id
890
	 * @return string
891
	 */
892 2
	public function get_priority_select($task_id=0)
893
	{
894
		// @codeCoverageIgnoreStart
895
		if($task_id==0)
896
			$task_id=$this->uri->segment($this->uri->total_segments());
897
		// @codeCoverageIgnoreEnd
898
899 2
		$html = '';
900
901
		//Get the status from the task
902 2
		$task = $this->db->select('id, priority')
903 2
				->from('item')
904 2
				->where('id', $task_id)
905 2
				->order_by('id', 'asc')
906 2
				->get();
907
908 2
		$trow = $task->row();
909 2
		$pri_id = $trow->priority;
910
911
		//Get the list of statuses
912 2
		$query = $this->db->select('id, value as desc')
913 2
				->from('priority')
914 2
				->get();
915
916 2 View Code Duplication
		foreach($query->result() as $row)
917
		{
918 2
			$html .= T5.'<option value="'.$row->id.'"';
919
			//Mark the appropriate one selected
920 2
			$html .= ($row->id == $pri_id) ? ' selected="selected">': '>';
921 2
			$html .= $row->desc;
922 2
			$html .= '</option>'.NL;
923 2
		}
924
925 2
		return $html;
926
	}
927
928
	// --------------------------------------------------------------------------
929
930
	/**
931
	 * Get Category Select
932
	 *
933
	 * Returns category options for task status
934
	 * @param int $task_id
935
	 * @return string
936
	 */
937 2
	public function get_category_select($task_id=0)
938
	{
939
		// @codeCoverageIgnoreStart
940
		if($task_id==0)
941
			$task_id=$this->uri->segment($this->uri->total_segments());
942
		// @codeCoverageIgnoreEnd
943
944 2
		$html = '';
945
946
		//Get the user's category group
947 2
		$user_group_id = $this->todo->get_user_group();
948
949
		//Get the category from the task
950 2
		$task = $this->db->select('id, category_id')
951 2
				->from('item')
952 2
				->where('id', $task_id)
953 2
				->get();
954
955 2
		$trow = $task->row();
956 2
		$category_id = $trow->category_id;
957
958
		//Get the list of categories
959 2
		$query = $this->db->select('id, title as desc')
960 2
				->from('category')
961 2
				->where('group_id', 0)
962 2
				->or_where('group_id', $user_group_id)
963 2
				->order_by('title', 'asc')
964 2
				->get();
965
966 2 View Code Duplication
		foreach($query->result() as $row)
967
		{
968 2
			$html .= T5.'<option value="'.$row->id.'"';
969
			//Mark the appropriate one selected
970 2
			$html .= ($row->id == $category_id) ? ' selected="selected">': '>';
971 2
			$html .= $row->desc;
972 2
			$html .= '</option>'.NL;
973 2
		}
974
975 2
		return $html;
976
	}
977
978
	// --------------------------------------------------------------------------
979
980
	/**
981
	 * Update Status
982
	 *
983
	 * Updates task status
984
	 * @return int
985
	 */
986
	public function update_status()
987
	{
988
		$new_status = (int)$this->input->post('status');
989
		$task_id = (int)$this->input->post('task_id');
990
991
		//If you are marking it as complete
992
		if($new_status == STATUS_COMPLETED)
993
		{
994
			//Check for reminders attached to that task
995
			$rem_q = $this->db->select('id')
996
				->from('reminder')
997
				->where('task_id', $task_id)
998
				->where('sent', 0)
999
				->get();
1000
1001
			//If there are reminders attached
1002
			if($rem_q->num_rows() > 0)
1003
			{
1004
				//Go through the results, and mark each as done
1005
				foreach($rem_q->result() as $reminder)
1006
				{
1007
					$this->db->set('sent', 1)
1008
						->where('id', $reminder->id)
1009
						->update('reminder');
1010
1011
				}
1012
			}
1013
		}
1014
		else //Maybe it wasn't really complete yet
1015
		{
1016
			//Check if the task was marked complete
1017
			$stat_q = $this->db->select('status')
1018
				->from('item')
1019
				->where('id', $task_id)
1020
				->where_in('status', [STATUS_COMPLETED, STATUS_CANCELED])
1021
				->get();
1022
1023
			//if it was complete, check for associated reminders that are due in the future
1024
			if($stat_q->num_rows() > 0)
1025
			{
1026
				$now = time();
1027
1028
				$rem_q = $this->db->select('id')
1029
					->from('reminder')
1030
					->where('task_id', $task_id)
1031
					->where('reminder_time >', $now)
1032
					->get();
1033
1034
				//Update those reminders to be sent
1035
				foreach($rem_q->result() as $reminder)
1036
				{
1037
					$this->db->set('sent', 0)
1038
						->where('id', $reminder->id)
1039
						->update('reminder');
1040
				}
1041
			}
1042
		}
1043
1044
		//I guess we should actually update the status
1045
		$this->db->set('status', $new_status)
1046
				->set('modified', time())
1047
				->where('id', $task_id)
1048
				->update('item');
1049
1050
		return $this->db->affected_rows(); //'success';
1051
	}
1052
1053
	// --------------------------------------------------------------------------
1054
1055
	/**
1056
	 * Quick Update Category
1057
	 *
1058
	 * Updates task category via ajax
1059
	 * @return int
1060
	 */
1061
	public function quick_update_category()
1062
	{
1063
		$new_category = (int)$this->input->post('category');
1064
		$task_id = (int)$this->input->post('task_id');
1065
1066
		$this->db->set('category_id', $new_category)
1067
				->where('id', $task_id)
1068
				->update('item');
1069
1070
		return $this->db->affected_rows(); //'success';
1071
	}
1072
1073
	// --------------------------------------------------------------------------
1074
1075
	/**
1076
	 * Get task Comments
1077
	 *
1078
	 * Returns comments for the current task
1079
	 * @param int $task_id
1080
	 * @return array
1081
	 */
1082 1
	public function get_task_comments($task_id)
1083
	{
1084 1
		$comment_q = $this->db->select('item_comments.id, user_id, item_id, comment, time_posted, email, status.value as status')
1085 1
				->from('item_comments')
1086 1
				->join('user', 'item_comments.user_id=user.id', 'inner')
1087 1
				->join('status', 'item_comments.status=status.id', 'inner')
1088 1
				->where('item_id', (int) $task_id)
1089 1
				->order_by('time_posted', 'desc')
1090 1
				->get();
1091
1092 1
		$result_array = $comment_q->result_array();
1093 1
		return $result_array;
1094
	}
1095
1096
	// --------------------------------------------------------------------------
1097
1098
	/**
1099
	 * Add task comment
1100
	 *
1101
	 * Adds a task comment to the database
1102
	 * @param int $task_id
1103
	 * @return string
1104
	 */
1105
	public function add_task_comment($task_id)
1106
	{
1107
		$user_id = $this->session->userdata('uid');
1108
		$comment = xss_clean($this->input->post('comment'));
1109
		$status = $this->input->post('status');
1110
		$time = time();
1111
1112
		//Insert the comment
1113
		$this->db->set('item_id', $task_id)
1114
				->set('user_id', $user_id)
1115
				->set('comment', $comment)
1116
				->set('time_posted', $time)
1117
				->set('status', $status)
1118
				->insert('item_comments');
1119
1120
		return $this->db->affected_rows();
1121
	}
1122
1123
	// --------------------------------------------------------------------------
1124
1125
	/**
1126
	 * Delete Task
1127
	 *
1128
	 * Checks for permissions to delete a task
1129
	 * @param int $task_id
1130
	 * @return null
1131
	 */
1132
	public function delete_task($task_id)
1133
	{
1134
		$user = $this->session->userdata('uid');
1135
1136
		//Check if the user is task admin
1137
		$user_perms = $this->db->select('item.id')
1138
			->from('item')
1139
			->where('user_id', $user)
1140
			->get();
1141
1142
		$admin = ($user_perms->num_rows() > 0) ? TRUE : FALSE;
1143
1144
		//Check if the user has permission to delete this task
1145
		$friend_perms = $this->db->select('user.id')
1146
			->distinct()
1147
			->from('user')
1148
			->join('user_task_link', 'user_task_link.user_id=user.id', 'inner')
1149
			->join('item', 'user_task_link.task_id=item.id', 'inner')
1150
			->where('user.id', $user)
1151
			->where('task_id', $task_id)
1152
			->where('permissions', 9)
1153
			->get();
1154
1155
		$user_admin = ($friend_perms->num_rows() > 0) ? TRUE : FALSE;
1156
1157
1158
		//Check if the group this user is in has permission to delete this task
1159
		$group_perms = $this->db->select('user.id')
1160
			->distinct()
1161
			->from('user')
1162
			->join('group_users_link', 'group_users_link.user_id=user.id', 'inner')
1163
			->join('group_task_link', 'group_task_link.group_id=group_users_link.group_id', 'inner')
1164
			->where('user.id', $user)
1165
			->where('group_task_link.task_id', $task_id)
1166
			->where('permissions', 9)
1167
			->get();
1168
1169
		$group_admin = ($group_perms->num_rows() > 0) ? TRUE : FALSE;
1170
1171
1172
		//Check if the user has permission
1173
		if($admin === TRUE || $user_admin === TRUE || $group_admin === TRUE)
1174
		{
1175
			$this->_remove_task($task_id);
1176
			return;
1177
		}
1178
		else
1179
		{
1180
			show_error('You do not have permission to delete this task.');
1181
			return;
1182
		}
1183
	}
1184
1185
	// --------------------------------------------------------------------------
1186
1187
	/**
1188
	 * Update Checklist
1189
	 *
1190
	 * Updates a checklist
1191
	 *
1192
	 * @param int $check_id
1193
	 * @param int $checked
1194
	 * @return int
1195
	 */
1196
	public function update_checklist($check_id, $checked)
1197
	{
1198
		$task_id = $this->input->post('task_id');
1199
1200
		//Get the task checklist items
1201
		$clq = $this->db->select('is_checked')
1202
			->from('checklist')
1203
			->where('task_id', $task_id)
1204
			->get();
1205
1206
		$checklist = $clq->result_array();
1207
1208
		$num_items = count($checklist);
1209
		$num_checked = 0;
1210
1211
		//Count the number checked
1212
		foreach($checklist as $bit)
1213
		{
1214
			//if true, add 1, if false, add 0;
1215
			$num_checked += $bit['is_checked'];
1216
		}
1217
1218
		$unchecked = $num_items - $num_checked;
1219
1220
		if($checked == 1) //Checking another box
1221
		{
1222
			//Check if it's the first checkbox to be checked
1223
			$is_first = ($num_checked == 0) ? TRUE : FALSE;
1224
1225
			//Check if it's the last checkbox to be checked
1226
			$is_last = ($unchecked == 1) ? TRUE : FALSE;
1227
1228
			//Update the checklist item in db
1229
			$this->db->set('is_checked', 1)
1230
				->where('id', $check_id)
1231
				->update('checklist');
1232
1233
			//if the checkbox doesn't update, show error
1234
			if($this->db->affected_rows() < 1)
1235
			{
1236
				return -1;
1237
			}
1238
1239
			//If it's the first item, set the status of the task to "In progress"
1240 View Code Duplication
			if($is_first == TRUE)
1241
			{
1242
				$this->db->set('status', 3)
1243
					->where('id', $task_id)
1244
					->update('item');
1245
1246
				return ($this->db->affected_rows() > 0) ? "first" : -1;
1247
			}
1248
1249 View Code Duplication
			if($is_last == TRUE) //set status to "Completed"
1250
			{
1251
				$this->db->set('status', 2)
1252
					->where('id', $task_id)
1253
					->update('item');
1254
1255
				return ($this->db->affected_rows() > 0) ? "last" : -1;
1256
			}
1257
			else
1258
			{
1259
				return 1;
1260
			}
1261
1262
		}
1263
		else if($checked == 0) //Unchecking a checkbox
1264
		{
1265
			$is_last = ($unchecked == 0) ? TRUE : FALSE;
1266
1267
			//Update the checklist item in db
1268
			$this->db->set('is_checked', 0)
1269
				->where('id', $check_id)
1270
				->update('checklist');
1271
1272
			if($this->db->affected_rows() < 1)
1273
				return PERM_NO_ACCESS;
1274
1275
			//if unchecking the last item, set status as "In progress"
1276 View Code Duplication
			if($is_last == TRUE)
1277
			{
1278
				$this->db->set('status', 3)
1279
					->where('id', $task_id)
1280
					->update('item');
1281
1282
				return ($this->db->affected_rows() > 0) ? "first" : -1;
1283
			}
1284
		}
1285
	}
1286
1287
1288
	// --------------------------------------------------------------------------
1289
1290
	/**
1291
	 * Add Reminder
1292
	 *
1293
	 * Adds reminder to the database
1294
	 */
1295
	private function _add_reminder($task_id, $reminder_time)
1296
	{
1297
		$user_id = (int) $this->session->userdata('uid');
1298
1299
		//Check for a reminder with the current task id
1300
		$query = $this->db->select('task_id')
1301
			->from('reminder')
1302
			->where('task_id', $task_id)
1303
			->get();
1304
1305
		//Check if there is an existing reminder for this task
1306 View Code Duplication
		if($query->num_rows() < 1)
1307
		{
1308
			$this->db->set('task_id', $task_id)
1309
				->set('reminder_time', $reminder_time)
1310
				->set('user_id', $user_id)
1311
				->insert('reminder');
1312
		}
1313
		else //If there is, update it.
1314
		{
1315
1316
			$this->db->set('reminder_time', $reminder_time)
1317
				->where('task_id', $task_id)
1318
				->where('user_id', $user_id)
1319
				->update('reminder');
1320
		}
1321
	}
1322
1323
	// --------------------------------------------------------------------------
1324
1325
	/**
1326
	 * Get Task Groups
1327
	 *
1328
	 * Returns groups related to the current task
1329
	 * @param int $task_id
1330
	 * @return array
1331
	 */
1332 2 View Code Duplication
	private function _get_task_groups($task_id)
1333
	{
1334 2
		$groups = $this->db->select('group_id')
1335 2
			->from('group_task_link')
1336 2
			->where('permissions !=', PERM_NO_ACCESS)
1337 2
			->where('task_id', $task_id)
1338 2
			->get();
1339
1340 2
		$group_list = $groups->result_array();
1341 2
		$result_array = array();
1342
1343 2
		if($groups->num_rows() < 1)
1344 2
		{
1345 1
			return FALSE;
1346
		}
1347
1348 1
		foreach($group_list as $group)
1349
		{
1350 1
			$result_array[] = $group['group_id'];
1351 1
		}
1352
1353 1
		return $result_array;
1354
	}
1355
1356
	// --------------------------------------------------------------------------
1357
1358
	/**
1359
	 * Get Task Users
1360
	 *
1361
	 * Returns users related to the current task
1362
	 * @param int $task_id
1363
	 * @return array
1364
	 */
1365 2 View Code Duplication
	private function _get_task_users($task_id)
1366
	{
1367 2
		$query = $this->db->select('user_id')
1368 2
			->from('user_task_link')
1369 2
			->where('permissions !=', PERM_NO_ACCESS)
1370 2
			->where('task_id', $task_id)
1371 2
			->get();
1372
1373 2
		$friend_list = $query->result_array();
1374 2
		$result_array = array();
1375
1376 2
		if($query->num_rows() < 1)
1377 2
		{
1378 1
			return FALSE;
1379
		}
1380
1381 1
		foreach($friend_list as $friend)
1382
		{
1383 1
			$result_array[] = $friend['user_id'];
1384 1
		}
1385
1386 1
		return $result_array;
1387
	}
1388
1389
	// --------------------------------------------------------------------------
1390
1391
	/**
1392
	 * Get Task Perms
1393
	 *
1394
	 * Get the permissions of the current task
1395
	 * @param int $task_id
1396
	 * @return array
1397
	 */
1398 2
	private function _get_task_perms(int $task_id)
1399
	{
1400
		/**
1401
		 * Get the task shared permissions
1402
		 */
1403
1404
		//Get groups associated with the task and user
1405 2
		$group_perms = $this->db->select('group_task_link.permissions')
1406 2
			->from('user')
1407 2
			->join('group_users_link', 'group_users_link.user_id=user.id', 'inner')
1408 2
			->join('group_task_link', 'group_task_link.group_id=group_users_link.group_id', 'inner')
1409 2
			->join('item', 'item.id=group_task_link.task_id', 'inner')
1410 2
			->where('todo_item.id', $task_id)
1411 2
			->where('todo_group_task_link.permissions !=', PERM_NO_ACCESS)
1412 2
			->where('todo_user.id', (int) $this->session->userdata('uid'))
1413 2
			->limit(1)
1414 2
			->get();
1415
1416
		//Get friends associated with the task and user
1417 2
		$friend_perms = $this->db->select('user_task_link.permissions')
1418 2
			->from('item')
1419 2
			->join('user_task_link', 'user_task_link.task_id=item.id')
1420 2
			->where('todo_user_task_link.permissions !=', PERM_NO_ACCESS)
1421 2
			->where('todo_user_task_link.task_id', $task_id)
1422 2
			->where('todo_user_task_link.user_id', (int) $this->session->userdata('uid'))
1423 2
			->limit(1)
1424 2
			->get();
1425
1426
		//Set permissions to no access as default
1427
		$result_array = array(
1428 2
			'friend_perms' => PERM_NO_ACCESS,
1429 2
			'group_perms' => PERM_NO_ACCESS,
1430
			'user_perms' => PERM_NO_ACCESS
1431 2
		);
1432
1433 2
		$resf = ($friend_perms->num_rows() == 1) ?  $friend_perms->row_array() : array('permissions' => FALSE);
1434 2
		$resg = ($group_perms->num_rows() == 1) ? $group_perms->row_array() : array('permissions' => FALSE);
1435
1436
		//Group permissions are set
1437 2 View Code Duplication
		if($resg['permissions'] !== FALSE && $resf['permissions'] === FALSE)
1438 2
		{
1439
			//Return groups query
1440 1
			$result_array['group_perms'] = $resg['permissions'];
1441 1
			$result_array['friend_perms'] = PERM_NO_ACCESS;
1442
1443 1
		}
1444
1445
		//Group and friend permissions set
1446 2 View Code Duplication
		if($resg['permissions'] !== FALSE && $resf['permissions'] !== FALSE)
1447 2
		{
1448
			//Return groups query and friend_perms
1449
			$result_array['friend_perms'] = $resf['permissions'];
1450
			$result_array['group_perms'] = $resf['permissions'];
1451
1452
		}
1453
1454
		//Friend Permissions are set
1455 2 View Code Duplication
		if($resg['permissions'] === FALSE && $resf['permissions'] !== FALSE)
1456 2
		{
1457
			//Return user query
1458
			$result_array['friend_perms'] = $resf['permissions'];
1459
			$result_array['group_perms'] = PERM_NO_ACCESS;
1460
		}
1461
1462
		/**
1463
		 * Get the current user's permissions from the database
1464
		 */
1465
1466
		//Check group permissions
1467 2
		$upG = $this->db->select('permissions')
1468 2
			->from('user')
1469 2
			->join('group_users_link', 'group_users_link.user_id=user.id', 'inner')
1470 2
			->join('group_task_link', 'group_task_link.group_id=group_users_link.group_id', 'inner')
1471 2
			->where('todo_group_users_link.user_id', (int) $this->session->userdata('uid'))
1472 2
			->where('todo_group_task_link.task_id', $task_id)
1473 2
			->get();
1474
1475
		//Check user permissions
1476 2
		$upU = $this->db->select('permissions')
1477 2
			->from('user_task_link')
1478 2
			->where('todo_user_task_link.user_id', (int) $this->session->userdata('uid'))
1479 2
			->where('todo_user_task_link.task_id', $task_id)
1480 2
			->get();
1481
1482
		//Check if task admin
1483 2
		$upA = $this->db->select('id')
1484 2
			->from('item')
1485 2
			->where('id', $task_id)
1486 2
			->where('user_id', (int) $this->session->userdata('uid'))
1487 2
			->get();
1488
1489
		//Check for admin permissions
1490 2
		if($upA->num_rows() > 0)
1491 2
		{
1492 1
			$result_array['user_perms'] = PERM_ADMIN_ACCESS;
1493 1
			return $result_array;
1494
		}
1495
		else //User is not admin
1496
		{
1497
			//Check group permissions
1498 1
			if($upG->num_rows() > 0)
1499 1
			{
1500 1
				$upG_row = $upG->row_array();
1501 1
				$result_array['user_perms'] = $upG_row['permissions'];
1502 1
			}
1503
1504
			//Check individual user permissions
1505 1
			if($upU->num_rows() > 0)
1506 1
			{
1507
				$up_row = $upU->row_array();
1508
1509
				// Only overwrite group permissions if there are higher
1510
				// user permissions than group permissions
1511
				if (
1512
					$result_array['user_perms'] == PERM_NO_ACCESS ||
1513
					$up_row['permissions'] > $result_array['user_perms']
1514
					)
1515
				{
1516
					$result_array['user_perms'] = $up_row['permissions'];
1517
				}
1518
			}
1519
1520
			//Determine whether the current user can view and/or edit this task
1521 1
			if($result_array['user_perms'] == PERM_NO_ACCESS)
1522 1
			{
1523
				show_error('You do not have permission to view this task.');
1524
				return NULL;
1525
			}
1526 1
			else if($result_array['user_perms'] < PERM_WRITE_ACCESS && $this->uri->segment('2') == "edit")
1527 1
			{
1528
				show_error('You do not have permission to edit this task.');
1529
				return NULL;
1530
			}
1531
1532 1
			return $result_array;
1533
		}
1534
	}
1535
1536
	// --------------------------------------------------------------------------
1537
1538
	/**
1539
	 * Remove Task
1540
	 *
1541
	 * Delete a task from the database
1542
	 * @param int $task_id
1543
	 */
1544
	private function _remove_task($task_id)
1545
	{
1546
		//Delete references from reminder
1547
		$this->db->where('task_id', $task_id)
1548
			->delete('reminder');
1549
1550
		//Delete references from group_task_link
1551
		$this->db->where('task_id', $task_id)
1552
			->delete('group_task_link');
1553
1554
		//Delete references from user_task_link
1555
		$this->db->where('task_id', $task_id)
1556
			->delete('user_task_link');
1557
1558
		//Delete task comments
1559
		$this->db->where('item_id', $task_id)
1560
			->delete('item_comments');
1561
1562
		//Delete checklists
1563
		$this->db->where('task_id', $task_id)
1564
			->delete('checklist');
1565
1566
		//Delete the task
1567
		$this->db->where('id', $task_id)
1568
			->delete('item');
1569
1570
		//Redirect to the task list
1571
		$this->todo->redirect_303(site_url('task/list'));
1572
	}
1573
}
1574
// End of task_model.php