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
|
<?php
require_once '../env.inc.php';
require_once $gfwww.'include/pre.php';
require_once $gfcommon.'pm/ProjectGroupFactory.class.php';
require_once $gfcommon.'pm/ProjectCategory.class.php';
require_once $gfcommon.'pm/ProjectGroup.class.php';
require_once $gfcommon.'pm/ProjectTask.class.php';
require_once($gfcommon.'include/User.class.php');
//require_once($gfcommon.'import/import_arrays.php');
class Tasks {
function __construct($trackers, $group_id, $users) {
$this->group = group_get_object($group_id);
if (!$this->group || !is_object($this->group)) {
print "error retrieving group from id";
} elseif ($this->group->isError()) {
print "error";
}
$this->hashrn=array();
$this->hashlogin=array();
//create hash table hashrn{real_name:mail} & hashlogin{id:mail}
foreach($users as $user => $infos){
$this->hashrn[$infos['real_name']] = $infos['mail'];
$this->hashlogin[$user] = $infos['mail'];
}
$this->trackers = $trackers;
}
/**
* addComments - Add followup comments to an Artifact Object
* @param object $artifact the artifact object where history should be added
* @param array $comments the artifact's data in json format (an array)
*/
function addComments($artifact, $comments){
foreach($comments as $c){
$time = strtotime($c['date']);
$uid = user_get_object_by_name($c['submitter'])->getID();
$importData = array('time' => $time, 'user' => $uid);
$artifact->addMessage($c['comment'], $importData);
}
}
/**
* addHistory - Add history of changes to an Artifact Object
* @param object $artifact the artifact object where history should be added
* @param array $history the artifact's data in json format (an array)
*/
function addHistory($artifact, $history){
foreach($history as $h){
$time = strtotime($h['date']);
$uid = user_get_object_by_name($h['by'])->getID();
$importData = array('time' => $time, 'user' => $uid);
//hack!!
$old = $h['old'];
// if($h['field']=='assigned_to'){
// if($old!='none'){
// $old = user_get_object_by_name($old)->getID();
// } else {
// $old = 100;
// }
// }
// if($h['field']=='status_id'){
// $status = array('Open' =>1, 'Closed' => 2, 'Deleted' => 3);
// $old = $status[$old];
// }
// if($h['field']=='close_date'){
// $old = strtotime($old);
// }
//end hack
$artifact->addHistory($h['field'],$old, $importData);
}
}
/**
* createTaskTracker - Create a specific tracker from data in the specified group
* @param array $data Tracker data from JSON
* @return ArtifactType the tracker created
*/
function createTaskTracker($data) {
// TaskTracker's type
$tracker = $data['type'];
// Create a tracker
db_begin();
$pg = new ProjectGroup($this->group);
if (!$pg || !is_object($pg)) {
db_rollback();
return false;
}
// include $GLOBALS['gfcommon'].'import/import_arrays.php';
// if(array_key_exists($tracker, $base_tracker_association)){
// $valueType = $base_tracker_association[$tracker];
// } else {
// $valueType = 0;
// }
if (!$pg->create($data["label"], $data["label"])) {
new dBug($pg);
db_rollback();
return false;
} else {
if (count($data['vocabulary']['category']) >1){
// Create each category
$cats = $this->createCategories($pg, $data['vocabulary']['category']);
} else {
$cats = array('None'=>100);
}
if (count($data['artifacts']) >0){
// Create each task in the TaskTracker
$output = array($pg, $data, $cats);
// $this->createTasks($pg, $data, $cats);
} else {
$output = false;
}
}
db_commit();
return $output;
}
/**
* Create each category for a single project group
* @param $pg ProjectGroup
* @param $categoriesList array
* @return array|bool
*/
function createCategories($pg, $categoriesList){
$cats = array();
foreach($categoriesList as $cat){
if($cat != 'None'){
$pc = new ProjectCategory($pg);
if ($pc){
if (!$pc->create($cat)) {
db_rollback();
return false;
} else {
$cats[$cat] = $pc->getID();
}
} else {
return false;
}
} else {
$cats['None'] = 100;
}
}
return $cats;
}
/**
* Create each task for the considered project group
* @param $pg ProjectGroup
* @param $data array
* @param $cats
* @return bool
*/
function createTasks($pg, $data, $cats){
// Stores each atid in a name:id array
$atids = array();
// Stores each artifact dependent on another artifact in a id:dependent_on_id array, with dependent tasks represented with their name
$dependent = array();
foreach($data['artifacts'] as $a){
//for each artifact
$artifact = new ProjectTask($pg);
if ($artifact) {
$start = strtotime($a['start_date']);
$end = strtotime($a['end_date']);
$assigned = array();
if (is_array($a['assigned_to[]'])) {
foreach($a['assigned_to[]'] as $realname) {
if ($realname == 'None') {
$assigned[]=100;
} else {
$assigned[] = user_get_object_by_mail($this->hashrn[$realname])->getID(); // this should be done once instead of for each artifact, TODO
}
}
} else {
if ($a['assigned_to[]']=='None') {
$assigned[]=100;
} else {
$assigned[] = user_get_object_by_mail($this->hashrn[$a['assigned_to[]']])->getID();
}
}
$uid = user_get_object_by_name($a['submitter'])->getID();
$dependentTemp = array();
// new dBug(array($a['summary'], $a['description'], $a['priority'], $a['hours'], $start, $end, $cats[$a['category']], $a['percent_complete'], &$assigned, &$dependentTemp, 0, 0, array('user' => $uid)));
if(!$artifact->create($a['summary'], $a['description'], $a['priority'], $a['hours'], $start, $end, $cats[$a['category']], $a['percent_complete'], $assigned, $dependentTemp, 0, 0, array('user' => $uid))){
return;
} else {
$atid = $artifact->getID();
$atids[$a['summary']] = $atid;
$dependent[$atid] = $a['dependent_on[]'];
$this->addComments($artifact, $a['comments']);
$this->addHistory($artifact, $a['history']);
// addTimeTracking($artifact, $a);
}
}
}
//Sets dependent tasks for each artifact, must be done after all artifacts are created
foreach($dependent as $mainId => $depNames){
if($depNames!='None'){
$artifact = new ProjectTask($pg, $mainId);
$dependentIds = array();
if(is_array($depNames)){
foreach($depNames as $taskName){
$dependentIds[$atids[$taskName]] = 'FS'; // Default to PM_LINK_DEFAULT defined as FS in ProjectTask class, it seems there is no way to use any of the other relations anyway...
}
} else {
$dependentIds[$atids[$depNames]] = 'FS';
}
$artifact->setDependentOn($dependentIds);
}
}
}
/**
* deleteTrackers - Delete all existing default trackers from a project
*/
function deleteTrackers(){
$pgf = new ProjectGroupFactory($this->group);
$pgs = $pgf->getProjectGroups();
if($pgs){
foreach($pgs as $pg){
$pg->delete(true, true);
}
}
}
function createAllTasks(){
if($this->taskCreationArray){
foreach($this->taskCreationArray as $taskCreation){
$this->createTasks($taskCreation[0],$taskCreation[1],$taskCreation[2]);
}
}
}
/**
* tasks_fill - Create trackers from an array in a given group
*/
function tasks_fill(){
//existing tracker deletion
$this->deleteTrackers();
//Tracker creation
$this->taskCreationArray = array(); // This array is used to store each projectGroup and each artifacts which will be imported later, we need to stop the script so as to update permissions again (default to Read for each new TaskTracker, and thus nobody can be assigned to a task except userid 100 which is Nobody)
foreach ($this->trackers as $data){
$output = $this->createTaskTracker($data);
if($output){
$this->taskCreationArray[]=$output;
}
}
}
}
|