3 * FusionForge project manager
5 * Copyright 1999-2000, Tim Perdue/Sourceforge
6 * Copyright 2002, Tim Perdue/GForge, LLC
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 require_once $gfcommon.'pm/ProjectTaskFactory.class.php';
26 function &pm_import_tasks($group_project_id,&$tasks) {
29 $pg = projectgroup_get_object($group_project_id);
30 if (!$pg || !is_object($pg)) {
31 $array['success']=false;
32 $array['errormessage']='Could Not Get ProjectGroup';
33 } elseif ($pg->isError()) {
34 $array['success']=false;
35 $array['errormessage']='Could Not Get ProjectGroup: '.$pg->getErrorMessage();
39 // Build hash list of technicians so we can get their ID for assigning tasks
41 $engine = RBACEngine::getInstance () ;
42 $techs = $engine->getUsersByAllowedAction ('pm', $pg->getID(), 'tech') ;
43 $tcount=count($techs);
44 for ($i=0; $i<$tcount; $i++) {
45 $tarr[strtolower($techs[$i]->getUnixName())]=$techs[$i]->getID();
46 $tarr[strtolower($techs[$i]->getRealName())]=$techs[$i]->getID();
48 $invalid_names=array();
50 // Create a linked list based on the msproj_id
52 for ($i=0; $i<$count; $i++) {
53 $msprojid[$tasks[$i]['msproj_id']] =& $tasks[$i];
54 $resrc = $tasks[$i]['resources'];
55 for ($j=0; $j<count($resrc); $j++) {
56 //validate user - see if they really exist as techs in this subproject
57 if (!util_ifsetor($tarr[strtolower($resrc[$j]['user_name'])])) {
58 //create list of bogus names to send back
59 if (array_search(strtolower($resrc[$j]['user_name']),$invalid_names) === false) {
60 $invalid_names[]=$resrc[$j]['user_name'];
67 // invalid assignees - send it back for remapping
69 if (count($invalid_names)) {
70 $array['success']=false;
71 $array['errormessage']='Invalid Resource Name:';
72 foreach ($invalid_names as $i) {
73 $array['errormessage'] .= ' ' . $i;
75 $array['resourcename']=$invalid_names;
76 for ($i=0; $i<$tcount; $i++) {
77 $array['usernames'][$techs[$i]->getID()]=$techs[$i]->getUnixName();
82 // Begin inserting/updating the tasks
84 for ($i=0; $i<$count; $i++) {
88 //no task_id so it must be new - create it
89 if (!$tasks[$i]['id']) {
90 if (!$tasks[$i]['notes']) {
91 $tasks[$i]['notes']='None Provided';
94 $pt = new ProjectTask($pg);
95 if (!$pt || !is_object($pt)) {
96 $array['success']=false;
98 $array['errormessage']='Could Not Get ProjectTask';
99 } elseif ($pt->isError()) {
100 $array['success']=false;
102 $array['errormessage']='Could Not Get ProjectTask: '.$pt->getErrorMessage();
104 //remap priority names=>numbers
105 $priority=$tasks[$i]['priority'];
106 if (!$priority || $priority < 1 || $priority > 5) {
111 $resrc = $tasks[$i]['resources'];
112 for ($ucount=0; $ucount< count($resrc); $ucount++) {
113 //get their user_id from the $tarr we created earlier
114 $assignees[]=$tarr[strtolower($resrc[$ucount]['user_name'])];
116 //don't do anything with dependencies yet - we may only have
117 //the MSprojid from dependent items
118 $hours = $tasks[$i]['work'];
122 $percent_complete= intval($tasks[$i]['percent_complete']);
123 if (!$percent_complete) {
125 } elseif ($percent_complete > 100) {
126 $percent_complete=100;
129 // Convert category name to category_id if given.
130 if (isset($tasks[$i]['category'])) {
131 if ($tasks[$i]['category'] == 'None') {
134 $res = db_query_params('SELECT category_id FROM project_category WHERE group_project_id=$1 AND category_name=$2',
135 array($group_project_id, $tasks[$i]['category']));
136 $category_id = db_result($res, 0, 'category_id');
139 $array['errormessage']='Error No category named : '.$tasks[$i]['category'];
144 $category_id = $pt->getCategoryID();
152 strtotime($tasks[$i]['start_date']),
153 strtotime($tasks[$i]['end_date']),
158 $tasks[$i]['duration'],
159 $tasks[$i]['parent_id'])) {
160 $array['success']=false;
162 $array['errormessage']='Error Creating ProjectTask: '.$pt->getErrorMessage();
167 $tasks[$i]['id'] = $pt->getID();
168 $tasks[$i]['obj'] = $pt;
169 $pt->setExternalID($tasks[$i]['msproj_id']);
175 //update existing task
177 $pt = projecttask_get_object($tasks[$i]['id']);
178 if (!$pt || !is_object($pt)) {
179 } elseif ($pt->isError()) {
180 $array['success']=false;
182 $array['errormessage']='Could Not Get ProjectTask: '.$pt->getErrorMessage();
184 //remap priority names=>numbers
185 $priority=$tasks[$i]['priority'];
186 if (!$priority || $priority < 1 || $priority > 5) {
192 $resrc = $tasks[$i]['resources'];
193 for ($ucount=0; $ucount<count($resrc); $ucount++) {
194 //get their user_id from the $tarr we created earlier
195 $assignees[]=$tarr[strtolower($resrc[$ucount]['user_name'])];
198 //don't do anything with dependencies yet - we may only have the
199 //MSprojid from dependent items
200 $hours = $tasks[$i]['work'];
204 $percent_complete= intval($tasks[$i]['percent_complete']);
205 if (!$percent_complete) {
207 } elseif ($percent_complete > 100) {
208 $percent_complete=100;
211 // Convert category name to category_id if given.
212 if (isset($tasks[$i]['category'])) {
213 if ($tasks[$i]['category'] == 'None') {
216 $res = db_query_params('SELECT category_id FROM project_category WHERE group_project_id=$1 AND category_name=$2',
217 array($group_project_id, $tasks[$i]['category']));
218 $category_id = db_result($res, 0, 'category_id');
221 $array['errormessage']='Error No category named : '.$tasks[$i]['category'];
226 $category_id = $pt->getCategoryID();
234 strtotime($tasks[$i]['start_date']),
235 strtotime($tasks[$i]['end_date']),
240 $pt->getDependentOn(),
242 $tasks[$i]['duration'],
243 $tasks[$i]['parent_id'])) {
244 $array['success']=false;
246 $array['errormessage']='Error Updating ProjectTask: '.$pt->getErrorMessage();
252 $tasks[$i]['id'] = $pt->getID();
253 $tasks[$i]['obj'] = $pt;
254 $pt->setExternalID($tasks[$i]['msproj_id']);
258 } //if task->iserror()
260 //accumulate list of completed tasks
261 //any task not in this list will be deleted
262 $completed[$tasks[$i]['id']]=true;
266 // Do task dependencies
271 for ($i=0; $i<$count; $i++) {
272 $darr=$tasks[$i]['dependenton'];
275 //iterate each dependency in a task
276 for ($dcount=0; $dcount<count($darr); $dcount++) {
277 //get the id of the task we're dependent on -
278 // may have to get it from msprojid linked list
279 $id=$darr[$dcount]['task_id'];
281 $id=$msprojid[$darr[$dcount]['msproj_id']]['id'];
283 //prevent task from being dependent on itself
284 if ($id == $tasks[$i]['id']) {
287 $deps[$id]=$darr[$dcount]['link_type'];
289 if (isset($tasks[$i]['obj']) && is_object($tasks[$i]['obj'])) {
290 if (!$tasks[$i]['obj']->setDependentOn($deps)) {
292 $array['success']=false;
296 // $array['success']=false;
299 } //iterates tasks to do dependencies
304 // Delete unreferenced tasks
307 $ptf = new ProjectTaskFactory($pg);
308 $pt_arr=& $ptf->getTasks();
309 for ($i=0; $i<count($pt_arr); $i++) {
310 if (is_object($pt_arr[$i])) {
311 if (!util_ifsetor($completed[$pt_arr[$i]->getID()])) {
312 if (!$pt_arr[$i]->delete(true)) {
313 echo $pt_arr[$i]->getErrorMessage();
323 $array['success']=true;
331 // c-file-style: "bsd"