JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
really made it so you can put tasks on hold. contractor can switch logins multiple...
[contractor-progress.git] / tasks.php
1 <?php
2
3 #  Copyright (C) 2008  Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU Affero General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU Affero General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Affero General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 require_once('code/tasks.php');
19
20 $GLOBALS['tasks_form_recipient'] = "fixme@example.com";
21
22
23
24 require_once('code/wfpl/template.php');
25 require_once('code/wfpl/format.php');
26 require_once('code/wfpl/messages.php');
27 require_once('code/wfpl/email.php');
28 require_once('code/db_connect.php');
29
30 function description_has_fixmes($description) {
31         return (strpos($description, 'FIXME') !== false);
32 }
33
34 function tasks_get_fields() {
35         $title = format_oneline($_REQUEST['title']);
36         $url = format_oneline($_REQUEST['url']);
37         $description = format_unix($_REQUEST['description']);
38         $price = format_decimal($_REQUEST['price']);
39
40         tasks_tem_sets($title, $url, $description, $price);
41
42         return array($title, $url, $description, $price);
43 }
44
45 function tasks_tem_sets($title, $url, $description, $price) {
46         tem_set('title', $title);
47         tem_set('url', $url);
48         tem_set('description', $description);
49         tem_set('price', $price);
50 }
51
52 function tasks_main() {
53         if(!logged_in()) {
54                 $GLOBALS['url'] = this_url();
55                 return 'login';
56         }
57
58         if(isset($_REQUEST['tasks_id'])) {
59                 $ret = tasks_display_main();
60                 if($ret) {
61                         return $ret;
62                 }
63                 tem_show('display_body');
64         } else {
65                 $ret = tasks_edit_main();
66                 if($ret) {
67                         return $ret;
68                 }
69                 tem_show('edit_body');
70         }
71
72         tem_show('main_body');
73 }
74
75 function tasks_display_main() {
76         $task_id = format_int($_REQUEST['tasks_id']);;
77         $client_id = logged_in();
78         if(logged_in_as_contractor()) {
79                 $row = db_get_row('tasks', 'title,url,description,state,price,client_id,paid,finished_at,tested_at', 'where id=%i', $task_id);
80         } else {
81                 $row = db_get_row('tasks', 'title,url,description,state,price,client_id,paid,finished_at,tested_at', 'where id=%i && client_id=%i', $task_id, $client_id);
82         }
83         if($row) {
84                 list($title, $url, $description, $state, $price, $owner_id, $paid, $finished_at, $tested_at) = $row;
85                 tem_set('task_id', $task_id);
86                 tem_set('title', $title);
87                 tem_set('url', $url);
88                 tem_set('description', $description);
89                 tem_set('state', task_state_pretty($state));
90                 tem_set('price', $price);
91                 if($finished_at) {
92                         tem_set('finished_at', $finished_at);
93                         tem_show('finished_at_section');
94                 }
95                 if($tested_at) {
96                         tem_set('tested_at', $tested_at);
97                         tem_show('tested_at_section');
98                 }
99                 if($state == TASK_BUG) {
100                         tem_show('bug_title');
101                 } else {
102                         tem_show('normal_title');
103                 }
104                 if(logged_in_as_contractor()) {
105                         switch($state) {
106                                 case TASK_DRAFT:
107                                 case TASK_NEEDS_CLARIFICATION:
108                                 case TASK_NEEDS_QUOTE:
109                                 case TASK_BUG:
110                                         tem_show('normal_edit_link');
111                                 break;
112                                 case TASK_NEEDS_GO_AHEAD:
113                                         tem_show('approve_price_link');
114                                         tem_show('normal_edit_link');
115                                         tem_show('price_row');
116                                 break;
117                                 case TASK_QUEUED:
118                                         tem_show('normal_edit_link');
119                                         tem_show('working_link');
120                                         tem_show('price_row');
121                                 break;
122                                 case TASK_WORKING:
123                                         tem_show('price_row');
124                                         tem_show('needs_testing_link');
125                                 break;
126                                 case TASK_NEEDS_TESTING:
127                                         if($owner_id == logged_in()) {
128                                                 tem_show('finished_link');
129                                         }
130                                         # FALL THROUGH
131                                 case TASK_FINISHED:
132                                         if($paid) {
133                                                 tem_show('marked_paid');
134                                         } else {
135                                                 tem_show('mark_paid_link');
136                                         }
137                                         tem_show('price_row');
138                                 break;
139                         }
140                 } else {
141                         switch($state) {
142                                 case TASK_DRAFT:
143                                 case TASK_NEEDS_CLARIFICATION:
144                                 case TASK_BUG:
145                                 case TASK_ON_HOLD:
146                                         tem_show('normal_edit_link');
147                                 break;
148                                 case TASK_NEEDS_QUOTE:
149                                         tem_show('hold_link');
150                                         tem_show('normal_edit_link');
151                                 break;
152                                 case TASK_NEEDS_GO_AHEAD:
153                                         tem_show('price_row');
154                                         tem_show('approve_price_link');
155                                         tem_show('normal_edit_link');
156                                 break;
157                                 case TASK_QUEUED:
158                                         tem_show('price_row');
159                                         tem_show('warning_edit_link');
160                                         tem_show('hold_link');
161                                 break;
162                                 case TASK_WORKING:
163                                         tem_show('price_row');
164                                 break;
165                                 case TASK_NEEDS_TESTING:
166                                         tem_show('price_row');
167                                         tem_show('finished_link');
168                                 break;
169                                 case TASK_FINISHED:
170                                         tem_show('price_row');
171                                 break;
172                         }
173                 }
174         } else {
175                 message("Task #$task_id not found");
176                 return './';
177         }
178 }
179
180 define('MAX_PRIORITY', 2000000000);
181 define('MIN_PRIORITY', 0);
182 define('MID_PRIORITY', floor((MAX_PRIORITY - MIN_PRIORITY) / 2));
183
184 function new_lowest_priority($client_id) {
185         $lowest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
186         if($lowest_ord === false) {
187                 return MID_PRIORITY;
188         }
189         if($lowest_ord == MIN_PRIORITY) {
190                 reprioritize_tasks($client_id); # make room
191                 $lowest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
192         }
193         return MIN_PRIORITY + floor(($lowest_ord - MIN_PRIORITY) / 2);
194 }
195
196 # keep everything in the same order, but space them out so there's room to squeeze things in anywhere
197 function reprioritize_tasks($client_id) {
198         $ids = db_get_column('tasks', 'id', 'where client_id=%i && state=%i order by ord desc, id desc', $client_id, TASK_QUEUED);
199         $step = floor((MAX_PRIORITY - MIN_PRIORITY) / (count($ids) + 1));
200         $cur = MAX_PRIORITY;
201         foreach($ids as $id) {
202                 $cur -= $step;
203                 db_update('tasks', 'ord', $cur, 'where id=%i', $id);
204         }
205 }
206
207
208 # pass the task id and one of (up,down,top,bottom)
209 function prioritize_task($id, $change) {
210         $row = db_get_row('tasks', 'client_id,ord', 'where id=%i', $id);
211         if(!$row) {
212                 message('Database error #2242');
213                 return;
214         }
215         list($client_id, $ord) = $row;
216         switch($change) {
217                 case 'top':
218                         list($highest_id, $highest_ord) = db_get_row('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord desc limit 1', $client_id, TASK_QUEUED);
219                         if($highest_id == $id) {
220                                 message('Already highest priority');
221                                 return;
222                         }
223
224                         if($highest_ord == MAX_PRIORITY) {
225                                 reprioritize_tasks($client_id); # make room
226                                 $highest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord desc limit 1', $client_id, TASK_QUEUED);
227                         }
228
229                         $new_ord = MAX_PRIORITY - floor((MAX_PRIORITY - $highest_ord) / 2);
230                         db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
231                         return;
232                 case 'bottom':
233                         list($lowest_id, $lowest_ord) = db_get_row('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
234                         if($lowest_id == $id) {
235                                 message('Already lowest priority');
236                                 return $lowest_ord;
237                         }
238
239                         if($lowest_ord == MIN_PRIORITY) {
240                                 reprioritize_tasks($client_id); # make room
241                                 $lowest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
242                         }
243
244                         $new_ord = MIN_PRIORITY + floor(($lowest_ord - MIN_PRIORITY) / 2);
245                         db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
246                         return;
247                 case 'up':
248                 case 'down':
249                         if($change == 'up') {
250                                 $rows = db_get_rows('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord desc, id desc', $client_id, TASK_QUEUED);
251                                 if($rows[0][0] == $id) {
252                                         message('Already highest priority');
253                                         return;
254                                 }
255                                 if($rows[1][0] == $id) {
256                                         prioritize_task($id, 'top');
257                                         return;
258                                 }
259                         } else {
260                                 $rows = db_get_rows('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord asc, id asc', $client_id, TASK_QUEUED);
261                                 if($rows[0][0] == $id) {
262                                         message('Already lowest priority');
263                                         return;
264                                 }
265                                 if($rows[1][0] == $id) {
266                                         prioritize_task($id, 'bottom');
267                                         return;
268                                 }
269                         }
270                         # find the one we're moving
271                         $cur_index = 0;
272                         $done = count($rows);
273                         for($i = 2; $i < $done ; ++$i) {
274                                 if($rows[$i][0] == $id) {
275                                         $cur_index = $i;
276                                         break;
277                                 }
278                         }
279                         $before_ord = $rows[$cur_index - 1][1];
280                         $before_before_ord = $rows[$cur_index - 2][1];
281                         if(abs($before_before_ord - $before_ord) < 2) {
282                                 reprioritize_tasks($client_id);
283                                 $before_ord = db_get_value('tasks', 'ord', 'where id=%i', $rows[$cur_index - 1][0]);
284                                 $before_before_ord = db_get_value('tasks', 'ord', 'where id=%i', $rows[$cur_index - 2][0]);
285                                 if($before_before_ord == $before_ord) {
286                                         message('Programmer error #8592');
287                                         return;
288                                 }
289                         }
290                         $new_ord = $before_ord + floor(($before_before_ord - $before_ord) / 2);
291                         db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
292                         return;
293                 default:
294                         message('invalid change');
295                         return;
296         }
297
298 }
299
300 function tasks_edit_main() {
301         $state = TASK_DRAFT; # will be overwritten
302         $client_id = logged_in(); # fixed shortly if we're contractor
303         $edit_id = format_int($_REQUEST['tasks_edit_id']);
304         unset($_REQUEST['tasks_edit_id']);
305         if($edit_id) {
306                 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $edit_id);
307                 if(logged_in_as_contractor()) {
308                         $client_id = $owner;
309                 } elseif($owner != $client_id) {
310                         message('Sorry, that task was entered by/for another client.');
311                         return './';
312                 }
313
314                 # add hidden field for database id of row we're editing
315                 tem_set('tasks_edit_id', $edit_id);
316                 tem_show('editing');
317
318                 $state = db_get_value('tasks', 'state', 'where id=%i', $edit_id);
319         }
320
321         if(isset($_REQUEST['bump'])) {
322                 switch($_REQUEST['bump']) {
323                         case 'up':
324                         case 'down':
325                         case 'top':
326                         case 'bottom':
327                                 prioritize_task($edit_id, $_REQUEST['bump']);
328                                 return './';
329                 }
330         }
331
332         if(isset($_REQUEST['tasks_new_bug'])) {
333                 $state = TASK_BUG;
334         }
335
336         if(isset($_REQUEST['tasks_hold_id'])) {
337                 $id = $_REQUEST['tasks_hold_id'];
338                 db_update('tasks', 'state', TASK_ON_HOLD, 'where id=%i', $id);
339                 message("Task removed from Jason's to-do list.");
340                 return './';
341         }
342
343         if(isset($_REQUEST['tasks_mark_paid_id'])) {
344                 if(!logged_in_as_contractor()) {
345                         message("Error: only Jason can mark tasks as paid.");
346                         return './';
347                 }
348                 $id = $_REQUEST['tasks_mark_paid_id'];
349                 db_update('tasks', 'paid', 1, 'where id=%i', $id);
350                 message('Marked as paid.');
351                 return './';
352         }
353
354         if(isset($_REQUEST['tasks_approve_price_id'])) {
355                 $id = $_REQUEST['tasks_approve_price_id'];
356                 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);
357                 if(logged_in() != $owner) {
358                         message("Error: can't approve a task entered by/for another client.");
359                         return './';
360                 }
361                 $ord = new_lowest_priority($owner);
362                 db_update('tasks', 'state,ord', TASK_QUEUED, $ord, 'where id=%i', $id);
363                 message('Price approved.');
364                 return './';
365         }
366
367         if(isset($_REQUEST['tasks_working_id'])) {
368                 $id = $_REQUEST['tasks_working_id'];
369                 if(!logged_in_as_contractor()) {
370                         message("Error: only Jason can say what he's working on.");
371                         return './';
372                 }
373                 db_update('tasks', 'state', TASK_WORKING, 'where id=%i', $id);
374                 message('Task marked as "in progress".');
375                 return './tasks?tasks_id=' . $id;
376         }
377
378         if(isset($_REQUEST['tasks_needs_testing_id'])) {
379                 $id = $_REQUEST['tasks_needs_testing_id'];
380                 if(!logged_in_as_contractor()) {
381                         message("Error: only Jason can say when he's done.");
382                         return './';
383                 }
384                 db_update('tasks', 'state,finished_at', TASK_NEEDS_TESTING, date('Y-m-d'), 'where id=%i', $id);
385                 message('Task awaits testing.');
386                 return './';
387         }
388
389         if(isset($_REQUEST['tasks_finished_id'])) {
390                 $id = $_REQUEST['tasks_finished_id'];
391                 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);;
392                 if(logged_in() != $owner) {
393                         message("Error: can't test a task entered by/for another client.");
394                         return './';
395                 }
396                 db_update('tasks', 'state,tested_at', TASK_FINISHED, date('Y-m-d'), 'where id=%i', $id);
397                 message('Task marked as finished.');
398                 # FIXME also mark it as paid if client's balance can cover it
399                 return './';
400         }
401
402         $delete_id = format_int($_REQUEST['tasks_delete_id']);
403         unset($_REQUEST['tasks_delete_id']);
404         if($delete_id) {
405                 db_delete('tasks', 'where id=%i', $delete_id);
406                 message('Task deleted.');
407
408                 return './tasks.html';
409         }
410
411         if(isset($_REQUEST['title'])) {
412                 list($title, $url, $description, $price) = tasks_get_fields();
413                 $queuing = false;
414
415                 # FIXME
416                 if(isset($_REQUEST['save_draft'])) {
417                         $state = TASK_DRAFT;
418                 } elseif(isset($_REQUEST['save_bug'])) {
419                         $state = TASK_BUG;
420                 } elseif(isset($_REQUEST['save_price_no_tiny']) && logged_in_as_contractor()) {
421                         $state = TASK_NEEDS_GO_AHEAD;
422                 } elseif(isset($_REQUEST['save_price']) && logged_in_as_contractor()) {
423                         $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
424                         if($price <= $tiny_agreement) {
425                                 $state = TASK_QUEUED;
426                                 $queuing = true;
427                         } else {
428                                 $state = TASK_NEEDS_GO_AHEAD;
429                         }
430                 } elseif(isset($_REQUEST['needs_clarification'])) {
431                         $state = TASK_NEEDS_CLARIFICATION;
432                 } else { # better be "request_price"
433                         if(description_has_fixmes($description)) {
434                                 $state = TASK_NEEDS_CLARIFICATION;
435                                 message("Error: Not requesting price. To get this task priced, you'll need to edit the description so it no longer contains \"FIXME\".");
436                         } else {
437                                 $state = TASK_NEEDS_QUOTE;
438                         }
439                 }
440
441                 if(!logged_in_as_contractor() || $edit_id || $_REQUEST['client_id']) {
442                         if($edit_id) {
443                                 $tables = 'title,url,description,state';
444                                 $values = array($title, $url, $description, $state);
445                                 if(isset($_REQUEST['price']) && logged_in_as_contractor()) {
446                                         $tables .= ',price';
447                                         array_push($values, $price);
448                                 }
449                                 if($queuing) {
450                                         $client_id = db_get_value('tasks', 'client_id', 'where id=%i', $edit_id);
451                                         $tables .= ',ord';
452                                         array_push($values, new_lowest_priority($client_id));
453                                 }
454                                 db_update('tasks', $tables, $values, 'where id=%i', $edit_id);
455                                 message('Changes saved.');
456                         } else {
457                                 # new task
458                                 $paid = 0;
459                                 $client_id = logged_in();
460                                 if(logged_in_as_contractor() && $_REQUEST['client_id']) {
461                                         $client_id = format_int($_REQUEST['client_id']);
462                                         $client_name = db_get_value('people', 'name', 'where id=%i', $client_id);
463                                 } else {
464                                         # if client entered the task, no price is set
465                                         $price = 0;
466                                 }
467                                 if($state == TASK_QUEUED) {
468                                         $ord = new_lowest_priority($client_id);
469                                 } else {
470                                         $ord = 0;
471                                 }
472                                 db_insert('tasks', 'client_id,title,url,description,state,paid,price,ord', $client_id, $title, $url, $description, $state, $paid, $price, $ord);
473                                 if(logged_in_as_contractor()) {
474                                         message("Task saved for $client_name.");
475                                 } else {
476                                         message('Task saved.');
477                                 }
478                         }
479                         if($GLOBALS['tasks_form_recipient'] != "fixme@example.com") {
480                                 $to = $GLOBALS['tasks_form_recipient'];
481                                 $from = $to;
482                                 $reply_to = '';
483                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
484                                         $reply_to = $_REQUEST['email'];
485                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
486                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
487                                         }
488                                 }
489                                 $subject = 'tasks form submitted';
490                                 $message = tem_run('tasks.email.txt');
491                                 $cc = '';
492                                 $bcc = '';
493                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
494                                         message('Due to an internal error, your message could not be sent. Please try again later.');
495                                         $error = true;
496                                 }
497                         }
498                         if($error !== true) {
499                                 return './';
500                         }
501                 } else {
502                         message('Error: you must select a client for the task');
503                 }
504                 # otherwise, we display the form again. tasks_get_fields() has
505                 # already put the posted values back into the template engine, so they will
506                 # show up in the form fields. You should add some message asking people to
507                 # fix their entry in whatever way you require.
508         } elseif($edit_id) {
509                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
510                 list($title, $url, $description, $state, $price) = db_get_row('tasks', 'title,url,description,state,price', 'where id=%i', $edit_id);
511                 tasks_tem_sets($title, $url, $description, $price);
512         } else {
513                 # form not submitted, you can set default values like so:
514                 #tem_set('client_id', 'Yes');
515         }
516
517         # display header
518         if($edit_id) {
519                 tem_show('edit_msg');
520         } elseif($state == TASK_BUG) {
521                 tem_show('bug_msg');
522         } else {
523                 tem_show('new_msg');
524                 if(logged_in_as_contractor()) {
525                         pulldown('client_id', db_get_rows('people', 'id,name', 'where id > 1'), PULLDOWN_2D);
526                         tem_set('client_id', format_int($_REQUEST['client_id']));
527                         tem_show('client_row');
528                 }
529         }
530
531         # display instructions
532         if($state == TASK_BUG) {
533                 tem_show('bug_instructions');
534                 if(logged_in_as_contractor()) {
535                         tem_show('price_field');
536                         tem_show('contractor_submits');
537                 } else {
538                         tem_show('bug_submit');
539                 }
540         } elseif($state == TASK_NEEDS_QUOTE && logged_in_as_contractor()) {
541                 tem_show('set_price_instructions');
542                 tem_show('price_field');
543                 tem_show('contractor_submits');
544         } else {
545                 if(description_has_fixmes($description)) {
546                         tem_show('fixme_instructions');
547                 } else {
548                         tem_show('normal_instructions');
549                 }
550                 if(logged_in_as_contractor()) {
551                         tem_show('contractor_submits');
552                         switch($state) {
553                                 case TASK_DRAFT:
554                                 case TASK_NEEDS_CLARIFICATION:
555                                 case TASK_NEEDS_QUOTE:
556                                 case TASK_NEEDS_GO_AHEAD:
557                                 case TASK_QUEUED:
558                                 case TASK_BUG:
559                                 tem_show('price_field');
560                         }
561                 } else {
562                         tem_show('normal_submits');
563                 }
564         }
565 }
566
567 ?>