Do you wish to register an account?
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

1545 líneas
38 KiB

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