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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
|
<?php
/**
* GForge Project Management Facility
*
* Copyright 2002 GForge, LLC
* http://gforge.org/
*
* @version $Id: ProjectTask.class 5361 2006-03-09 17:21:22Z danper $
*
* This file is part of GForge.
*
* GForge is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GForge is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GForge; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
*/
/*
Project/Task Manager
By Tim Perdue, Sourceforge, 11/99
Heavy rewrite by Tim Perdue April 2000
Total rewrite in OO and GForge coding guidelines 12/2002 by Tim Perdue
*/
require_once('common/include/Error.class');
require_once('common/include/Validator.class');
function &projecttask_get_object($project_task_id,$data=false) {
global $PROJECTTASK_OBJ;
if (!isset($PROJECTTASK_OBJ["_".$project_task_id."_"])) {
if ($data) {
//the db result handle was passed in
} else {
$res=db_query("SELECT * FROM project_task_vw
WHERE project_task_id='$project_task_id'");
if (db_numrows($res) <1 ) {
$PROJECTTASK_OBJ["_".$project_task_id."_"]=false;
return false;
}
$data =& db_fetch_array($res);
}
$ProjectGroup =& projectgroup_get_object($data["group_project_id"]);
$PROJECTTASK_OBJ["_".$project_task_id."_"]= new ProjectTask($ProjectGroup,$project_task_id,$data);
}
return $PROJECTTASK_OBJ["_".$project_task_id."_"];
}
/*
Types of task dependencies
*/
define('PM_LINK_DEFAULT','FS');
define('PM_LINK_START_START','SS');
define('PM_LINK_START_FINISH','SF');
define('PM_LINK_FINISH_START','FS');
define('PM_LINK_FINISH_FINISH','FF');
class ProjectTask extends Error {
/**
* Associative array of data from db.
*
* @var array $data_array.
*/
var $data_array;
/**
* The ProjectGroup object.
*
* @var object $ProjectGroup.
*/
var $ProjectGroup;
var $dependon;
var $assignedto;
var $relatedartifacts;
/**
* Constructor.
*
* @param object The ProjectGroup object to which this ProjectTask is associated.
* @param int The project_task_id.
* @param array The associative array of data.
* @return boolean success.
*/
function ProjectTask(&$ProjectGroup, $project_task_id=false, $arr=false) {
$this->Error();
if (!$ProjectGroup || !is_object($ProjectGroup)) {
$this->setError('ProjectTask:: No Valid ProjectGroup Object');
return false;
}
if ($ProjectGroup->isError()) {
$this->setError('ProjectTask:: '.$ProjectGroup->getErrorMessage());
return false;
}
$this->ProjectGroup =& $ProjectGroup;
if ($project_task_id) {
if (!$arr || !is_array($arr)) {
if (!$this->fetchData($project_task_id)) {
return false;
}
} else {
$this->data_array =& $arr;
//
// Verify this message truly belongs to this ProjectGroup
//
if ($this->data_array['group_project_id'] != $this->ProjectGroup->getID()) {
$this->setError('Group_project_id in db result does not match ProjectGroup Object');
return false;
}
}
}
return true;
}
/**
* create - create a new ProjectTask in the database.
*
* @param string The summary of this task.
* @param string The detailed description of this task.
* @param int The Priority of this task.
* @param int The Hours estimated to complete this task.
* @param int The (unix) start date of this task.
* @param int The (unix) end date of this task.
* @param int The category_id of this task.
* @param int The percentage of completion in integer format of this task.
* @param array An array of user_id's that are assigned this task.
* @param array An array of project_task_id's that this task depends on.
* @param int The duration of the task in days.
* @param int The id of the parent task, if any.
* @return boolean success.
*/
function create($summary,$details,$priority,$hours,$start_date,$end_date,
$category_id,$percent_complete,&$assigned_arr,&$depend_arr,$duration=0,$parent_id=0) {
$v = new Validator();
$v->check($summary, "summary");
$v->check($details, "details");
$v->check($priority, "priority");
$v->check($hours, "hours");
$v->check($start_date, "start date");
$v->check($end_date, "end date");
$v->check($category_id, "category");
if (!$v->isClean()) {
$this->setError($v->formErrorMsg("Must include "));
return false;
}
if (!$parent_id) {
$parent_id=0;
}
if (!$duration) {
$duration=0;
}
if (!$this->ProjectGroup->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
db_begin();
$res=db_query("SELECT nextval('project_task_pk_seq') AS id");
if (!$project_task_id=db_result($res,0,'id')) {
$this->setError('Could Not Get Next ID');
db_rollback();
return false;
} else {
$this->data_array['project_task_id']=$project_task_id;
if (!$this->setDependentOn($depend_arr)) {
db_rollback();
return false;
} elseif (!$this->setAssignedTo($assigned_arr)) {
db_rollback();
return false;
} else {
$sql="INSERT INTO project_task (project_task_id,group_project_id,created_by,summary,
details,start_date,end_date,status_id,category_id,priority,percent_complete,hours,duration,parent_id)
VALUES ('$project_task_id','". $this->ProjectGroup->getID() ."', '".user_getid()."', '". htmlspecialchars($summary) ."',
'". htmlspecialchars($details) ."','$start_date','$end_date','1','$category_id','$priority','$percent_complete','$hours','$duration','$parent_id')";
$result=db_query($sql);
if (!$result || db_affected_rows($result) < 1) {
$this->setError('ProjectTask::create() Posting Failed '.db_error().$sql);
db_rollback();
return false;
} else {
if (!$this->fetchData($project_task_id)) {
db_rollback();
return false;
} else {
$this->sendNotice(1);
db_commit();
return true;
}
}
}
}
}
/**
* fetchData - re-fetch the data for this ProjectTask from the database.
*
* @param int The project_task_id.
* @return boolean success.
*/
function fetchData($project_task_id) {
$res=db_query("SELECT * FROM project_task_vw
WHERE project_task_id='$project_task_id'
AND group_project_id='". $this->ProjectGroup->getID() ."'");
if (!$res || db_numrows($res) < 1) {
$this->setError('ProjectTask::fetchData() Invalid Task ID'.db_error());
return false;
}
$this->data_array =& db_fetch_array($res);
db_free_result($res);
return true;
}
/**
* getProjectGroup - get the ProjectGroup object this ProjectTask is associated with.
*
* @return Object The ProjectGroup object.
*/
function &getProjectGroup() {
return $this->ProjectGroup;
}
/**
* getID - get this project_task_id.
*
* @return int The project_task_id.
*/
function getID() {
return $this->data_array['project_task_id'];
}
/**
* getSubmittedRealName - get the real name of the person who created this task.
*
* @return string The real name person who created this task.
*/
function getSubmittedRealName() {
return $this->data_array['realname'];
}
/**
* getDuration - the duration of the task.
*
* @return int The number of days of duration.
*/
function getDuration() {
return $this->data_array['duration'];
}
/**
* getParentID - the task_id of the parent task, if any.
*
* @return string The real name person who created this task.
*/
function getParentID() {
return $this->data_array['parent_id'];
}
/**
* getSubmittedUnixName - get the unix name of the person who created this task.
*
* @return string The unix name of the person who created this task.
*/
function getSubmittedUnixName() {
return $this->data_array['user_name'];
}
/**
* getSummary - get the subject/summary of this task.
*
* @return string The summary.
*/
function getSummary() {
return $this->data_array['summary'];
}
/**
* getDetails - get the body/details of this task.
*
* @return string The body/details.
*/
function getDetails() {
return $this->data_array['details'];
}
/**
* getPercentComplete - an integer between 0 and 100.
*
* @return int The percentage of completion of this task.
*/
function getPercentComplete() {
return $this->data_array['percent_complete'];
}
/**
* getPriority - the priority, between 1 and 9 of this task.
*
* @return int The priority.
*/
function getPriority() {
return $this->data_array['priority'];
}
/**
* getHours - the hours this task is expected to take.
*
* @return int The hours.
*/
function getHours() {
return $this->data_array['hours'];
}
/**
* getStartDate - the unix time that this task will start.
*
* @return int The unix start time of this task.
*/
function getStartDate() {
return $this->data_array['start_date'];
}
/**
* getEndDate - the unix time that this task will end.
*
* @return int The unix end time of this task.
*/
function getEndDate() {
return $this->data_array['end_date'];
}
/**
* getStatusID - the integer of the status of this task.
*
* @return int the status_id.
*/
function getStatusID() {
return $this->data_array['status_id'];
}
/**
* getStatusName - the string of the status of this task.
*
* @return string the status_name.
*/
function getStatusName() {
return $this->data_array['status_name'];
}
/**
* getCategoryID - the category_id of this task.
*
* @return int the category_id.
*/
function getCategoryID() {
return $this->data_array['category_id'];
}
/**
* getCategoryName - the category_name of this task.
*
* @return int the category_name.
*/
function getCategoryName() {
return $this->data_array['category_name'];
}
/**
* getLastModifiedDate - the last_modified_date of this task.
*
* @return int the last_modified_date.
*/
function getLastModifiedDate() {
return $this->data_array['last_modified_date'];
}
/**
* setExternalID - set a row in project_task_external_order which stores
* an id, for example an ID generated by MS Project, which needs to be restored later
*/
function setExternalID($id) {
$res=db_query("UPDATE project_task_external_order SET external_id='$id'
WHERE project_task_id='".$this->getID()."'");
if (db_affected_rows($res) < 1) {
$res=db_query("INSERT INTO project_task_external_order (project_task_id,external_id)
VALUES ('".$this->getID()."','$id')");
}
}
/**
* getExternalID - get the ID that MS Project uses to sort tasks
*
* @return int the id.
*/
function getExternalID() {
return $this->data_array['external_id'];
}
/**
* getRelatedArtifacts - Return a result set of artifacts which are related to this task.
*
* @returns Database result set.
*/
function getRelatedArtifacts() {
if (!$this->relatedartifacts) {
$this->relatedartifacts=
db_query("SELECT agl.group_id,agl.name,agl.group_artifact_id,a.artifact_id,a.open_date,a.summary
FROM artifact_group_list agl, artifact a
WHERE a.group_artifact_id=agl.group_artifact_id
AND EXISTS (SELECT artifact_id FROM project_task_artifact
WHERE artifact_id=a.artifact_id
AND project_task_id='". $this->getID() ."')");
}
return $this->relatedartifacts;
}
/**
* addRelatedArtifacts - take an array of artifact_id's and build relationships.
*
* @param array An array of artifact_id's to be attached to this task.
* @return boolean success.
*/
function addRelatedArtifacts($art_array) {
if (!$this->ProjectGroup->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
//
// SHOULD REALLY INSTANTIATE THIS ARTIFACT OBJECT TO ENSURE PROPER SECURITY - FUTURE
//
// new ArtifactFromID($id)
//
for ($i=0; $i<count($art_array); $i++) {
if ($art_array[$i] < 1) {
continue;
}
$res=db_query("INSERT INTO project_task_artifact (project_task_id,artifact_id)
VALUES ('".$this->getID()."','".$art_array[$i]."')");
if (!$res) {
$this->setError('Error inserting artifact relationship: '.db_error());
return false;
}
}
return true;
}
/**
* removeRelatedArtifacts - take an array of artifact_id's and delete relationships.
*
* @param array An array of artifact_id's to be removed from this task.
* @return boolean success.
*/
function removeRelatedArtifacts($art_array) {
if (!$this->ProjectGroup->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
for ($i=0; $i<count($art_array); $i++) {
$res=db_query("DELETE FROM project_task_artifact
WHERE project_task_id='".$this->getID()."'
AND artifact_id='".$art_array[$i]."'");
if (!$res) {
$this->setError('Error deleting artifact relationship: '.db_error());
return false;
}
}
return true;
}
/**
* delete - delete this tracker and all its related data.
*
* @param bool I'm Sure.
* @return bool true/false;
*/
function delete($sure) {
if (!$sure) {
$this->setMissingParamsError();
return false;
}
if (!$this->ProjectGroup->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
db_begin();
$res = db_query("DELETE FROM project_assigned_to WHERE project_task_id='".$this->getID()."'");
if (!$res) {
$this->setError('Error deleting assigned users relationship: '.db_error());
db_rollback();
return false;
}
$res = db_query("DELETE FROM project_dependencies WHERE project_task_id='".$this->getID()."'");
if (!$res) {
$this->setError('Error deleting dependencies: '.db_error());
db_rollback();
return false;
}
$res = db_query("DELETE FROM project_history WHERE project_task_id='".$this->getID()."'");
if (!$res) {
$this->setError('Error deleting history: '.db_error());
db_rollback();
return false;
}
$res = db_query("DELETE FROM project_messages WHERE project_task_id='".$this->getID()."'");
if (!$res) {
$this->setError('Error deleting messages: '.db_error());
db_rollback();
return false;
}
$res = db_query("DELETE FROM project_task_artifact WHERE project_task_id='".$this->getID()."'");
if (!$res) {
$this->setError('Error deleting artifacts: '.db_error());
db_rollback();
return false;
}
$res = db_query("DELETE FROM rep_time_tracking WHERE project_task_id='".$this->getID()."'");
if (!$res) {
$this->setError('Error deleting time tracking report: '.db_error());
db_rollback();
return false;
}
$res = db_query("DELETE FROM project_task WHERE project_task_id='".$this->getID()."'");
if (!$res) {
$this->setError('Error deleting task: '.db_error());
db_rollback();
return false;
}
db_commit();
return true;
}
/**
* getOtherTasks - Return a result set of tasks in this subproject that do not equal
* the current task_id.
*
* @returns Database result set.
*/
function getOtherTasks () {
//
// May not yet have an ID, if we are creating a NEW task
//
if ($this->getID()) {
$addstr=" AND project_task_id <> '". $this->getID() ."' ";
}
$sql="SELECT project_task_id,summary
FROM project_task
WHERE group_project_id='". $this->ProjectGroup->getID() ."'
$addstr ORDER BY project_task_id DESC";
return db_query($sql);
}
/**
* getHistory - returns a result set of audit trail for this ProjectTask.
*
* @return database result set.
*/
function getHistory() {
$sql="SELECT *
FROM project_history_user_vw
WHERE project_task_id='". $this->getID() ."'
ORDER BY mod_date DESC";
return db_query($sql);
}
/**
* getMessages - get the list of messages attached to this ProjectTask.
*
* @return database result set.
*/
function getMessages() {
$sql="select *
FROM project_message_user_vw
WHERE project_task_id='". $this->getID() ."' ORDER BY postdate DESC";
return db_query($sql);
}
/**
* addMessage - Handle the addition of a followup message to this task.
*
* @param string The message.
* @returns boolean success.
*/
function addMessage($message) {
//prevent posting the same message
if ($this->getDetails() == htmlspecialchars($message)) {
return true;
}
$res=db_query("SELECT * FROM project_messages
WHERE project_task_id='".$this->getID()."'
AND body='". htmlspecialchars($message) ."'");
if (!$res || db_numrows($res) < 1) {
$sql="INSERT INTO project_messages (project_task_id,body,posted_by,postdate)
VALUES ('". $this->getID() ."','". htmlspecialchars($message) ."','".user_getid()."','". time() ."')";
$res=db_query($sql);
if (!$res || db_affected_rows($res) < 1) {
$this->setError('AddMessage():: '.db_error());
return false;
} else {
return true;
}
} else {
return true;
}
}
/**
* addHistory - Handle the insertion of history for these parameters.
*
* @param string The field name.
* @param string The old value.
* @returns boolean success.
*/
function addHistory ($field_name,$old_value) {
$sql="insert into project_history(project_task_id,field_name,old_value,mod_by,mod_date)
VALUES ('". $this->getID() ."','$field_name','$old_value','".user_getid()."','".time()."')";
$result=db_query($sql);
if (!$result) {
$this->setError('ERROR IN AUDIT TRAIL - '.db_error());
return false;
} else {
return true;
}
}
/**
* checkCircular - recursive function calls itself to look at all tasks you are dependent on.
*
* @param int The project_task_id you are dependent on.
* @param int The project_task_id you are checking circular dependencies for.
* @returns boolean success.
*/
function checkCircular($depend_on_id, $original_id) {
//for msproject users - ms project has more complex logic than gforge
return true;
global $Language;
if ($depend_on_id == $original_id) {
$this->setError($Language->getText('pm_projecttask','circular_dependency'));
return false;
}
$res=db_query("SELECT is_dependent_on_task_id AS id
FROM project_dependencies
WHERE project_task_id='$depend_on_id'");
$rows=db_numrows($res);
for ($i=0; $i<$rows; $i++) {
if (!$this->checkCircular(db_result($res,$i,'id'), $original_id)) {
return false;
}
}
return true;
}
/**
* setDependentOn - takes an array of project_task_id's and builds dependencies.
*
* @param array The array of project_task_id's.
* @returns boolean success.
*/
function setDependentOn(&$arr_) {
//printr($arr_,'setDependentOn entry');
//
// IMPORTANT - MUST VERIFY NO CIRCULAR DEPENDENCY!!
//
if (!$arr_ || empty($arr_)) {
$arr_=array('100'=>PM_LINK_DEFAULT);
}
$arr =& array_keys($arr_);
//get existing dependencies to diff against
$arr2 =& array_keys($this->getDependentOn());
if (count($arr) || count($arr2)) {
$add_arr = array_values (array_diff ($arr, $arr2));
//echo "add arr: ".print_r($add_arr);
$del_arr = array_values (array_diff ($arr2, $arr));
//echo "del arr: ".print_r($del_arr);
for ($i=0; $i<count($del_arr); $i++) {
db_query("DELETE FROM project_dependencies
WHERE project_task_id='".$this->getID()."'
AND is_dependent_on_task_id='". $del_arr[$i] ."'");
if (db_error()) {
$this->setError('setDependentOn()-1:: '.db_error());
return false;
}
}
for ($i=0; $i<count($add_arr); $i++) {
//
// Check task for circular dependencies
//
if (!$this->checkCircular($add_arr[$i],$this->getID())) {
return false;
}
$lnk = $arr_[$add_arr[$i]];
if (!$lnk) {
$lnk=PM_LINK_DEFAULT;
}
$sql="INSERT INTO project_dependencies (project_task_id,is_dependent_on_task_id,link_type)
VALUES ('".$this->getID()."','". $add_arr[$i] ."','$lnk')";
db_query($sql);
if (db_error()) {
$this->setError('setDependentOn()-2:: '.db_error().$sql);
return false;
}
}
return true;
} else {
return true;
}
}
/**
* convertDependentOn - converts a regular array of dependencies, such
* as from a multiple-select-box to an associative array with default
* link types. Should be called from web code as part of the create/update calls.
* Here we are converting an array like array(1,5,9,77) to array(1=>SS,5=>SF,9=>FS,77=>SS)
*/
function &convertDependentOn($arr) {
//echo "<p>Convert Dep0: ".print_r($arr);
$deps =& $this->getDependentOn();
for ($i=0; $i<count($arr); $i++) {
if ($deps[$arr[$i]]) {
//use existing link_type if it exists
$new[$arr[$i]]=$deps[$arr[$i]];
} else {
//else create with default link type
$new[$arr[$i]]=PM_LINK_DEFAULT;
}
}
//echo "<p>Convert Dep1: ".print_r($new);
return $new;
}
/**
* getDependentOn - get an array of project_task_id's that you are dependent on.
*
* @return array The array of project_task_id's in this format:
* array($id=>$link_type,id2=>link_type2).
*/
function &getDependentOn() {
if (!$this->getID()) {
return array();
}
if (!$this->dependon) {
$res=db_query("SELECT is_dependent_on_task_id,link_type
FROM project_dependencies
WHERE project_task_id='".$this->getID()."'");
for ($i=0; $i<db_numrows($res); $i++) {
$this->dependon[db_result($res,$i,'is_dependent_on_task_id')] = db_result($res,$i,'link_type');
}
}
/* fix bug 319: if dependentlist is emtpy, set it to 100 (none) */
if (!$this->dependon) {
$this->dependon[100]=PM_LINK_DEFAULT;
}
return $this->dependon;
}
/**
* setAssignedTo - takes an array of user_id's and builds assignments.
*
* @param array The array of user_id's.
* @returns boolean success.
*/
function setAssignedTo(&$arr) {
$arr2 =& $this->getAssignedTo();
$this->assignedto =& $arr;
//If no one is assigned, then assign it to "100" - NOBODY
if (count($arr) < 1 || ((count($arr)==1) && ($arr[0]==''))) {
$arr=array('100');
}
if (count($arr) || count($arr2)) {
$add_arr = array_values(array_diff ($arr, $arr2));
$del_arr = array_values(array_diff ($arr2, $arr));
for ($i=0; $i<count($del_arr); $i++) {
db_query("DELETE FROM project_assigned_to
WHERE project_task_id='".$this->getID()."'
AND assigned_to_id='". $del_arr[$i] ."'");
if (db_error()) {
$this->setError('setAssignedTo()-1:: '.db_error());
return false;
}
}
for ($i=0; $i<count($add_arr); $i++) {
db_query("INSERT INTO project_assigned_to (project_task_id,assigned_to_id)
VALUES ('".$this->getID()."','". $add_arr[$i] ."')");
if (db_error()) {
$this->setError('setAssignedTo()-2:: '.db_error());
return false;
}
}
return true;
} else {
return true;
}
}
/**
* getAssignedTo - get an array of user_id's that you are assigned to.
*
* @return array The array of user_id's.
*/
function &getAssignedTo() {
if (!$this->getID()) {
return array();
}
if (!$this->assignedto) {
$this->assignedto =& util_result_column_to_array(db_query("SELECT assigned_to_id
FROM project_assigned_to
WHERE project_task_id='".$this->getID()."'"));
}
return $this->assignedto;
}
/**
* update - update this ProjectTask in the database.
*
* @param string The summary of this task.
* @param string The detailed description of this task.
* @param int The Priority of this task.
* @param int The Hours estimated to complete this task.
* @param int The (unix) start date of this task.
* @param int The (unix) end date of this task.
* @param int The status_id of this task.
* @param int The category_id of this task.
* @param int The percentage of completion in integer format of this task.
* @param array An array of user_id's that are assigned this task.
* @param array An array of project_task_id's that this task depends on.
* @param int The GroupProjectID of a new subproject that you want to move this Task to.
* @param int The duration of the task in days.
* @param int The id of the parent task, if any.
* @return boolean success.
*/
function update($summary,$details,$priority,$hours,$start_date,$end_date,
$status_id,$category_id,$percent_complete,&$assigned_arr,&$depend_arr,
$new_group_project_id,$duration=0,$parent_id=0) {
$has_changes = false; // if any of the values passed is different from
$v = new Validator();
$v->check($summary, "summary");
$v->check($priority, "priority");
$v->check($hours, "hours");
$v->check($start_date, "start date");
$v->check($end_date, "end date");
$v->check($status_id, "status");
$v->check($category_id, "category");
if (!$v->isClean()) {
$this->setError($v->formErrorMsg("Must include "));
return false;
}
if (!$parent_id) {
$parent_id=0;
}
if ( ($this->getParentID()) != $parent_id ) {
$has_changes = true;
}
if (!$duration) {
$duration=0;
}
if ( ($this->getDuration()) != $duration ) {
$has_changes = true;
}
if (!$this->ProjectGroup->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
/*if ( ($this->getSummary() != $summary) || ($this->getDetails() != $details) ||
($this->getPriority() != $priority) || ($this->getHours() != $hours) ||
($this->getStartDate() != $start_date) || ($this->getEndDate() != $end_date) ||
($this->getStatusID() != $status_id) || ($this->getCategoryID() != $category_id) ||
($this->getPercentComplete() != $percent_complete) ) {
$has_changes = true;
}*/
db_begin();
//
// Attempt to move this Task to a new Subproject
// need to instantiate new ProjectGroup obj and test if it works
//
$group_project_id = $this->ProjectGroup->getID();
if ($new_group_project_id != $group_project_id) {
$newProjectGroup= new ProjectGroup($this->ProjectGroup->getGroup(), $new_group_project_id);
if (!is_object($newProjectGroup) || $newProjectGroup->isError()) {
$this->setError('ProjectTask: Could not move to new ProjectGroup'. $newProjectGroup->getErrorMessage());
db_rollback();
return false;
}
/* do they have perms for new ArtifactType?
if (!$newArtifactType->userIsAdmin()) {
$this->setPermissionDeniedError();
db_rollback();
return false;
}*/
//
// Now set ProjectGroup, Category, and Assigned to 100 in the new ProjectGroup
//
$status_id=1;
$category_id='100';
unset($assigned_to);
$assigned_to=array('100');
$this->ProjectGroup =& $newProjectGroup;
$this->addHistory ('group_project_id',$group_project_id);
$has_changes = true;
}
if ($details) {
$has_changes = true;
if (!$this->addMessage($details)) {
db_rollback();
return false;
}
}
if ($this->getStatusID() != $status_id) {
$this->addHistory ('status_id',$this->getStatusID());
$has_changes = true;
}
if ($this->getCategoryID() != $category_id) {
$this->addHistory ('category_id',$this->getCategoryID());
$has_changes = true;
}
if ($this->getPriority() != $priority) {
$this->addHistory ('priority',$this->getPriority());
$has_changes = true;
}
if ($this->getSummary() != htmlspecialchars(stripslashes($summary))) {
$this->addHistory ('summary',addslashes($this->getSummary()));
$has_changes = true;
}
if ($this->getPercentComplete() != $percent_complete) {
$this->addHistory ('percent_complete',$this->getPercentComplete());
$has_changes = true;
}
if ($this->getHours() != $hours) {
$this->addHistory ('hours',$this->getHours());
$has_changes = true;
}
if ($this->getStartDate() != $start_date) {
$this->addHistory ('start_date',$this->getStartDate());
$has_changes = true;
}
if ($this->getEndDate() != $end_date) {
$this->addHistory ('end_date',$this->getEndDate());
$has_changes = true;
}
$old_assigned = &$this->getAssignedTo();
$diff_assigned_array=array_diff($old_assigned, $assigned_arr);
if (count($diff_assigned_array)>0) {
for ($tmp=0;$tmp<count($old_assigned);$tmp++) {
$this->addHistory('assigned_to_id',$old_assigned[$tmp]);
}
$has_changes = true;
}
$old_array =& array_keys($this->getDependentOn());
$diff_array=array_diff($old_array,array_keys($depend_arr));
if (count($diff_array)>0) {
for ($tmp=0;$tmp<count($old_array);$tmp++) {
$this->addHistory('dependent_on_id', $old_array[$tmp]);
}
$has_changes = true;
}
if (!$this->setDependentOn($depend_arr)) {
db_rollback();
return false;
} elseif (!$this->setAssignedTo($assigned_arr)) {
db_rollback();
return false;
} else {
$sql="UPDATE project_task SET
summary='".htmlspecialchars($summary)."',
priority='$priority',
hours='$hours',
start_date='$start_date',
end_date='$end_date',
status_id='$status_id',
percent_complete='$percent_complete',
category_id='$category_id',
group_project_id='$new_group_project_id',
duration='$duration',
parent_id='$parent_id'
WHERE group_project_id='$group_project_id'
AND project_task_id='".$this->getID()."'";
$res=db_query($sql);
if (!$res || db_affected_rows($res) < 1) {
$this->setError('Error On ProjectTask::update-5: '.db_error().$sql);
db_rollback();
return false;
} else {
if (!$this->fetchData($this->getID())) {
$this->setError('Error On ProjectTask::update-6: '.db_error());
db_rollback();
return false;
} else {
if ($has_changes) { //only send email if there was any change
$this->sendNotice();
}
db_commit();
return true;
}
}
}
}
/**
* sendNotice - contains the logic for sending email/jabber updates.
*
* @return boolean success.
*/
function sendNotice($first=false) {
global $send_task_email;
if ($send_task_email===false) {
return true;
}
$ids =& $this->getAssignedTo();
//
// See if there is anyone to send messages to
//
if (count($ids) < 1 && !$this->ProjectGroup->getSendAllPostsTo()) {
return true;
}
$body = "Task #". $this->getID() ." has been updated. ".
"\n\nProject: ". $this->ProjectGroup->Group->getPublicName() .
"\nSubproject: ". $this->ProjectGroup->getName() .
"\nSummary: ".util_unconvert_htmlspecialchars( $this->getSummary() ).
"\nComplete: ". $this->getPercentComplete() ."%".
"\nStatus: ". $this->getStatusName() .
"\n\nDescription: ". util_unconvert_htmlspecialchars( $this->getDetails() );
/*
Now get the followups to this task
*/
$result2=$this->getMessages();
$rows=db_numrows($result2);
if ($result2 && $rows > 0) {
$body .= "\n\nFollow-Ups:";
for ($i=0; $i<$rows;$i++) {
$body .= "\n\n-------------------------------------------------------";
$body .= "\nDate: ". date($GLOBALS['sys_datefmt'],db_result($result2,$i,'postdate'));
$body .= "\nBy: ".db_result($result2,$i,'user_name');
$body .= "\n\nComment:\n".util_unconvert_htmlspecialchars(db_result($result2,$i,'body'));
}
}
$body .= "\n\n-------------------------------------------------------".
"\nFor more info, visit:".
"\n\nhttp://".$GLOBALS['sys_default_domain']."/pm/task.php?func=detailtask&project_task_id=".
$this->getID() ."&group_id=".
$this->ProjectGroup->Group->getID() ."&group_project_id=". $this->ProjectGroup->getID();
$subject="[Task #". $this->getID() .'] '.
util_unconvert_htmlspecialchars( $this->getSummary() );
util_handle_message(array_unique($ids),$subject,$body,$this->ProjectGroup->getSendAllPostsTo());
return true;
}
}
?>
|