Completed
Push — master ( b4e470...5ef2ca )
by Timothy
01:49
created

Task_model::validate_task()   F

Complexity

Conditions 19
Paths 3302

Size

Total Lines 123
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 75
CRAP Score 19.0063

Importance

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