Simple status box for the sidebar.
[elgg_plugins.git] / tasks / lib.php
blob97361e5279f2d4690b402b9a02ec75f30a02905b
1 <?php
3 function tasks_pagesetup() {
4 // register links --
5 global $tasks_id;
6 global $PAGE;
7 global $CFG;
9 if (isloggedin()) {
10 if (defined("context") && context == "tasks" && page_owner() == $_SESSION['userid']) {
11 $PAGE->menu[] = array( 'name' => 'tasks',
12 'html' => '<li><a href="'.$CFG->wwwroot.'mod/tasks/" class="selected">'.gettext("Your Tasks").'</a></li>');
13 } else {
14 $PAGE->menu[] = array( 'name' => 'tasks',
15 'html' => '<li><a href="'.$CFG->wwwroot.'mod/tasks/">'.gettext("Your Tasks").'</a></li>');
20 function tasks_init() {
22 global $function;
23 global $CFG;
24 global $db;
26 $function['init'][] = $CFG->dirroot . "mod/tasks/init.php";
27 $function['display:sidebar'][] = $CFG->dirroot . "mod/tasks/sidebarlink.php";
29 $tables = $db->Metatables();
30 if (!in_array($CFG->prefix . "tasks",$tables)) {
31 modify_database($CFG->dirroot . "mod/tasks/table.sql");
32 print_continue("index.php");
33 exit;
38 function tasks_permissions_check ($object) {
39 global page_owner();
41 if ($object === "tasks" && (page_owner() == $_SESSION['userid'] || user_flag_get("admin", $_SESSION['userid']))) {
42 return true;
44 return false;
47 function tasks_display ($tasklist_id, $owner_id, $title, $assigned_to = false, $keyword = null) {
49 global $CFG;
50 $return = "";
52 $where = run("users:access_level_sql_where",$_SESSION['userid']);
53 if ($tasklist_id > 0) {
54 $tasklist_where = " and tasklist = $tasklist_id ";
55 } else if ($tasklist_id < 0) {
56 $tasklist_where = " and tasklist != " . (0 - $tasklist_id);
57 } else {
58 $tasklist_where = "";
61 if ($keyword == null) {
62 if ($owner_id != -1) {
63 $sql = "select * from ".$CFG->prefix."tasks where ($where) and owner = ".$owner_id." $tasklist_where order by deadline asc";
64 } else {
65 $sql = "select * from ".$CFG->prefix."tasks where ($where) and owner != ".$_SESSION['userid']." $tasklist_where order by deadline asc";
67 } else {
68 $where = str_replace("owner","ts.owner",$where);
69 $where = str_replace("access","ts.access",$where);
70 $keyword = mysql_real_escape_string($keyword);
71 if ($owner_id != -1) {
72 $sql = "select ts.* from ".$CFG->prefix."tags tg left join ".$CFG->prefix."tasks ts on ts.ident = tg.ref where ($where) and tg.tag = '$keyword' and ts.owner = ".$owner_id." $tasklist_where order by deadline asc";
73 } else {
74 $sql = "select ts.* from ".$CFG->prefix."tags tg left join ".$CFG->prefix."tasks ts on ts.ident = tg.ref where ($where) and tg.tag = '$keyword' and ts.owner != ".$_SESSION['userid']." $tasklist_where order by deadline asc";
78 $tasks = get_records_sql($sql);
80 if ($tasklist_id < 0) {
81 $tasklist_id = 0 - $tasklist_id;
84 if (is_array($tasks) && !empty($tasks)) {
86 $return = "<h2>" . $title . "</h2>";
87 foreach($tasks as $task) {
88 if (run("users:access_level_check", $task->access)) {
90 $return .= "<table width=\"100%\" border=\"0\" class=\"task\">";
92 $cells = 1;
94 if ($task->deadline < time() && $task->deadline > 0) {
95 $class = "task_late";
96 } else if ($task->deadline < (time() + 86400) && $task->deadline > 0) {
97 $class = "task_expiring";
98 } else {
99 $class = "task_normal";
101 $return .= "<tr class=\"$class\"><td class=\"task_title\">";
103 if ($task->tasklist != page_owner()) {
104 $return .= "<a href=\"".$CFG->wwwroot."mod/tasks/?owner=".$task->tasklist."\">";
107 $return .= $task->title;
109 if ($task->tasklist != page_owner()) {
110 $return .= " @ " . user_info("name",$task->tasklist) . "</a>";
113 if ($task->deadline > 0) {
114 $cells++;
115 $return .= "</td><td class=\"task_deadline\">" . gettext("Due: ") . strftime("%B %d, %Y %H:%M",$task->deadline);
117 $return .= "</td>";
118 if ($task->owner == $_SESSION['userid'] || run("permissions:check", "profile")) {
119 $cells++;
120 $return .= "<td class=\"task_buttons\">Edit | <a href=\"".$CFG->wwwroot."mod/tasks/?owner=".$task->tasklist."&action=task:delete&task_id=".$task->ident."\">Delete</a></td>";
122 if ($assigned_to) {
123 $return .= "<tr class=\"$class\"><td colspan=\"$cells\" class=\"task_assigned\">";
124 $return .= gettext("Assigned to: ");
126 if (run("permissions:check", "weblog") && logged_on) {
128 $sql = "select u.ident, u.name from ".$CFG->prefix."friends f join ".$CFG->prefix."users u on u.ident = f.owner where f.friend = ".$task->tasklist;
129 $users = get_records_sql($sql);
131 $return .= "<form action=\"\" method=\"post\">";
133 if (is_array($users) && !empty($users)) {
135 $return .= "<select name=\"task_assigned\">";
136 $haduser = false;
137 foreach($users as $user) {
138 if ($user->ident == $task->owner) {
139 $selected = "selected = \"selected\"";
140 $haduser = true;
141 } else {
142 $selected = "";
144 $return .= "<option value=\"".$user->ident."\" $selected>" . $user->name . "</option>";
146 if (!$haduser) {
147 $user = get_record_sql("select * from ".$CFG->prefix."users where ident = ".$task->owner);
148 $return .= "<option value=\"".$user->ident."\" selected=\"selected\">" . $user->name . "</option>";
150 $return .= "</select> ";
151 $return .= "<input type=\"hidden\" name=\"action\" value=\"task:reassign\" />";
152 $return .= "<input type=\"hidden\" name=\"task_id\" value=\"".$task->ident."\" />";
153 $return .= "<input type=\"submit\" value=\"&gt;\" />";
157 $return .= "</form>";
159 } else {
160 $return .= user_info("name",$task->owner);
163 $return .= "</td></tr>";
165 $return .= "<tr><td colspan=\"$cells\" class=\"task_description\">" . nl2br($task->description) . "</td></tr>";
167 $tags = "";
168 $db_tags = get_records_sql("select * from ".$CFG->prefix."tags where tagtype = 'task' and ref = ".$task->ident);
170 if (is_array($db_tags) && !empty($db_tags)) {
171 $i=0;
172 foreach($db_tags as $tag) {
173 $tags .= "<a href=\"" . $CFG->wwwroot . "mod/tasks/search.php?tag=".urlencode($tag->tag)."&amp;tasklist=$tasklist_id\">";
174 $tags .= $tag->tag;
175 $tags .= "</a>";
176 if ($i < sizeof($db_tags) - 1) {
177 $tags .= ", ";
179 $i++;
183 $return .= "<tr><td colspan=\"$cells\" class=\"task_tags\">" . gettext("Tags:") . " " . $tags . "</td>";
184 $return .= "</tr>\n";
186 $return .= "</table>";
193 return $return;
197 function tasks_form($tasklist_id) {
199 $return = "";
201 if (run("permissions:check", "weblog") && logged_on) {
203 if (user_type($tasklist_id) == "person") {
204 $access = "user" . $tasklist_id;
205 } else if (user_type($tasklist_id) == "community") {
206 $access = "community" . $tasklist_id;
207 } else {
208 $access = "PRIVATE";
211 $return .= "<h2>" . gettext("Add new task") . "</h2>";
212 $return .= "<form action=\"\" method=\"post\">";
213 $return .= "<p>" . gettext("Task title") . "<br /><input type=\"text\" name=\"task_title\" value=\"\" /></p>";
214 $return .= "<p>" . gettext("Task description") . "<br /><textarea name=\"task_description\" ></textarea></p>";
215 $return .= "<p>" . gettext("Task tags") . "<br /><input type=\"text\" name=\"task_tags\" value=\"\" /></p>";
216 $return .= "<p>" . gettext("Task expiry time if any<br />(eg, 'next thursday 15:00', 'december 1st 2007', '+1 week', etc)") . "<br /><input type=\"text\" name=\"task_deadline\" value=\"\"></textarea></p>";
217 $return .= "<p>" . gettext("Task access restriction") . "<br />". run("display:access_level_select",array("task_access",$access)) ."</p>";
218 $return .= "<p><input type=\"hidden\" name=\"action\" value=\"task:add\" /><input type=\"hidden\" name=\"tasklist_id\" value=\"$tasklist_id\" /><input type=\"submit\" value=\"" . gettext("Add new task") . "\" /></p>";
219 $return .= "</form>";
223 return $return;
227 function tasks_actions() {
229 global $CFG;
230 global $messages;
232 $redirect = false;
234 $action = optional_param('action', "");
236 if (logged_on && run("permissions:check", "weblog") && !empty($action)) {
238 switch($action) {
240 case "task:add":
241 $task = new stdClass;
243 $task->title = optional_param('task_title',"");
244 $task->description = optional_param('task_description',"");
245 $task->access = optional_param('task_access',"");
246 $task->tasklist = optional_param('tasklist_id',page_owner(),PARAM_INT);
247 $task->owner = $_SESSION['userid'];
248 $task->posted = time();
250 $deadline = optional_param('task_deadline',"");
251 if (empty($deadline)) {
252 $task->deadline = 0;
253 } else {
254 $task->deadline = strtotime($deadline);
257 $insert_id = insert_record('tasks',$task);
259 $tags = optional_param('task_tags',"");
260 insert_tags_from_string (trim($tags), 'task', $insert_id, $task->access, $task->owner);
262 $messages[] = gettext("Task '". $task->title ."' was added to the tasklist.");
263 $redirect = true;
264 break;
266 case "task:delete":
268 $task_id = optional_param('task_id',-1,PARAM_INT);
269 if ($task_id != -1) {
270 $task = get_record_sql("select * from ".$CFG->prefix."tasks where ident = $task_id");
271 if ($task->owner == $_SESSION['userid'] || run("permissions:check", "weblog")) {
273 delete_records('tasks','ident',$task_id);
274 delete_records('tags','tagtype','task','ref',$task_id);
275 $messages[] = gettext("Task '".$task->title."' was deleted.");
280 $redirect = true;
281 break;
282 case "task:reassign":
284 $task_id = optional_param('task_id',-1,PARAM_INT);
285 $task_owner = optional_param('task_assigned',-1,PARAM_INT);
286 if ($task_id != -1 && $task_owner != -1) {
287 $task = get_record_sql("select * from ".$CFG->prefix."tasks where ident = $task_id");
288 if ($task->owner == $_SESSION['userid'] || run("permissions:check", "weblog")) {
290 $task->owner = $task_owner;
291 update_record('tasks',$task);
292 $messages[] = gettext("Task '".$task->title."' was reassigned.");
296 $redirect = true;
297 break;
301 if ($redirect) {
302 define('redirect_url',$CFG->wwwroot . "mod/tasks/?owner=" . $task->tasklist);
303 $_SESSION['messages'] = $messages;
304 header("Location: " . redirect_url);
305 exit;