summaryrefslogtreecommitdiff
path: root/task_lib.php
blob: 28784d646188a92fb7ad32ebe3f91329a2197945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/* Task properties:
   user, task_id, title, description, date, status, priority, completed, percentage
*/
class TaskLib extends BitBase {

	function TaskLib() {
		BitBase::BitBase();
	}
	function get_task( $pUserId,  $task_id) {
		$query = "select * from `".BIT_DB_PREFIX."tiki_user_tasks` where `user_id`=? and `task_id`=?";
		$result = $this->query($query,array( $pUserId, (int)$task_id));
		$res = $result->fetchRow();
		return $res;
	}

	function update_task_percentage( $pUserId,  $task_id, $perc) {
		$query = "update `".BIT_DB_PREFIX."tiki_user_tasks` set `percentage`=? where `user_id`=? and `task_id`=?";
		$this->query($query,array((int)$perc, $pUserId, (int)$task_id));
	}

	function open_task( $pUserId,  $task_id) {
		$query = "update `".BIT_DB_PREFIX."tiki_user_tasks` set `completed`=?, `status`=?, `percentage`=? where `user_id`=? and `task_id`=?";
		$this->query($query, array(0,'o',0, $pUserId, (int)$task_id));
	}

	function replace_task( $pUserId,  $task_id, $title, $description, $date, $status, $priority, $completed, $percentage) {
		if ($task_id != 0) {
			$query = "update `".BIT_DB_PREFIX."tiki_user_tasks` set `title` = ?, `description` = ?, `date` = ?, `status` = ?, `priority` = ?, ";
			$query.= "`percentage` = ?, `completed` = ?  where `user_id`=? and `task_id`=?";
			$this->query($query,array($title,$description,$date,$status,$priority,$percentage,$completed, $pUserId, $task_id));
			return $task_id;
		} else {
			$query = "insert into `".BIT_DB_PREFIX."tiki_user_tasks`(`user_id`,`title`,`description`,`date`,`status`,`priority`,`completed`,`percentage`) ";
			$query.= " values(?,?,?,?,?,?,?,?)";
			$this->query($query,array($pUserId,$title,$description,$date,$status,$priority,$completed,$percentage));
			$task_id = $this->getOne( "select  max(`task_id`) from `".BIT_DB_PREFIX."tiki_user_tasks` where `user_id`=? and `title`=? and `date`=?",array( $pUserId, $title,$date));
			return $task_id;
		}
	}

	function complete_task( $pUserId,  $task_id) {
		$now = date("U");
		$query = "update `".BIT_DB_PREFIX."tiki_user_tasks` set `completed`=?, `status`='c', `percentage`=100 where `user_id`=? and `task_id`=?";
		$this->query($query,array((int)$now, $pUserId, (int)$task_id));
	}

	function remove_task( $pUserId,  $task_id) {
		$query = "delete from `".BIT_DB_PREFIX."tiki_user_tasks` where `user_id`=? and `task_id`=?";
		$this->query($query,array( $pUserId, (int)$task_id));
	}

	function list_tasks( $pUserId,  $offset, $maxRecords, $sort_mode, $find, $use_date, $pdate) {
		$now = date("U");
		$bindvars=array($pUserId);
		if ($use_date == 'y') {
			$prio = " and date<=? ";
			$bindvars2=$pdate;
		} 
		else {
			$prio = '';
		}

		if ($find) {
			$findesc = '%' . strtoupper( $find ). '%';
			$mid = " and (UPPER(`title`) like ? or UPPER(`description`) like ?)";
			$bindvars[]=$findesc;
			$bindvars[]=$findesc;
		} else {
			$mid = "" ;
		}

		$mid.=$prio;
		if (isset($bindvars2)) 
			$bindvars[]=$bindvars2;

		$query = "select * from `".BIT_DB_PREFIX."tiki_user_tasks` where `user_id`=? $mid order by ".$this->convert_sortmode($sort_mode).",`task_id` desc";
		$query_cant = "select count(*) from `".BIT_DB_PREFIX."tiki_user_tasks` where `user_id`=? $mid";
		$result = $this->query($query,$bindvars,$maxRecords,$offset);
		$cant = $this->getOne($query_cant,$bindvars);
		$ret = array();

		while ($res = $result->fetchRow()) {
			$ret[] = $res;
		}

		$retval = array();
		$retval["data"] = $ret;
		$retval["cant"] = $cant;
		return $retval;
	}

}
global $tasklib;
$tasklib = new TaskLib();
?>