model.php 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973
  1. <?php
  2. /**
  3. * The model file of task module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package task
  9. * @version $Id: model.php 5154 2013-07-16 05:51:02Z chencongzhi520@gmail.com $
  10. * @link https://www.zentao.net
  11. */
  12. class taskModel extends model
  13. {
  14. /**
  15. * 激活任务。
  16. * Activate task.
  17. *
  18. * @param object $task
  19. * @param string $comment
  20. * @param object $teamData
  21. * @param mixed[] $drag
  22. * @access public
  23. * @return bool
  24. */
  25. public function activate($task, $comment, $teamData, $drag = array())
  26. {
  27. $taskID = $task->id;
  28. if(strpos($this->config->task->activate->requiredFields, 'comment') !== false && !$comment)
  29. {
  30. dao::$errors['comment'] = sprintf($this->lang->error->notempty, $this->lang->comment);
  31. return false;
  32. }
  33. $oldTask = $this->getById($taskID);
  34. if($oldTask->isParent) $this->config->task->activate->requiredFields = '';
  35. $this->dao->update(TABLE_TASKTEAM)->set('status')->eq('wait')->where('task')->eq($task->id)->andWhere('consumed')->eq(0)->andWhere('left')->gt('0')->exec();
  36. $this->dao->update(TABLE_TASKTEAM)->set('status')->eq('doing')->where('task')->eq($task->id)->andWhere('consumed')->gt(0)->andWhere('left')->gt(0)->exec();
  37. $this->dao->update(TABLE_TASKTEAM)->set('status')->eq('done')->where('task')->eq($task->id)->andWhere('consumed')->gt(0)->andWhere('left')->eq('0')->exec();
  38. if(!empty($oldTask->team))
  39. {
  40. /* When activate and assigned to a team member, then update his left data in teamData. */
  41. $valueCount = array_count_values($teamData->team);
  42. if(isset($valueCount[$task->assignedTo]) && $valueCount[$task->assignedTo] == 1)
  43. {
  44. $teamIndex = zget(array_flip($teamData->team), $task->assignedTo, '');
  45. $teamData->teamLeft[$teamIndex] = $task->left;
  46. }
  47. $this->manageTaskTeam($oldTask->mode, $task, $teamData);
  48. $task = $this->computeMultipleHours($oldTask, $task);
  49. if(!empty($task->assignedTo) && $task->assignedTo == 'closed') $task->assignedTo = '';
  50. }
  51. if(isset($task->left)) $task->left = round((float)$task->left, 2);
  52. $this->dao->update(TABLE_TASK)->data($task)
  53. ->autoCheck()
  54. ->batchCheck($this->config->task->activate->requiredFields, 'notempty')
  55. ->checkFlow()
  56. ->where('id')->eq((int)$taskID)
  57. ->exec();
  58. if(dao::isError()) return false;
  59. if($task->left != $oldTask->left) $this->loadModel('program')->refreshProjectStats($oldTask->project);
  60. if($oldTask->parent > 0) $this->updateParentStatus($taskID);
  61. if($oldTask->story) $this->loadModel('story')->setStage($oldTask->story);
  62. $changes = common::createChanges($oldTask, $task);
  63. if($this->post->comment != '' || !empty($changes))
  64. {
  65. $actionID = $this->loadModel('action')->create('task', $taskID, 'Activated', $this->post->comment);
  66. $this->action->logHistory($actionID, $changes);
  67. }
  68. if($this->config->edition != 'open' && $oldTask->feedback) $this->loadModel('feedback')->updateStatus('task', $oldTask->feedback, $task->status, $oldTask->status, $taskID);
  69. $this->updateKanbanCell($taskID, $drag, $oldTask->execution);
  70. return !dao::isError();
  71. }
  72. /**
  73. * 添加一条工时记录。
  74. * Add task effort.
  75. *
  76. * @param object $data
  77. * @access public
  78. * @return int
  79. */
  80. public function addTaskEffort($data)
  81. {
  82. $oldTask = $this->getById($data->task);
  83. $relation = $this->loadModel('action')->getRelatedFields('task', $data->task);
  84. $effort = new stdclass();
  85. $effort->objectType = 'task';
  86. $effort->objectID = $data->task;
  87. $effort->execution = $oldTask->execution;
  88. $effort->product = $relation['product'];
  89. $effort->project = (int)$relation['project'];
  90. $effort->account = $data->account;
  91. $effort->date = $data->date;
  92. $effort->consumed = round((float)$data->consumed, 2);
  93. $effort->left = round((float)$data->left, 2);
  94. $effort->work = isset($data->work) ? $data->work : '';
  95. $effort->vision = $this->config->vision;
  96. $effort->order = isset($data->order) ? $data->order : 0;
  97. $this->dao->insert(TABLE_EFFORT)->data($effort)->autoCheck()->exec();
  98. $effortID = $this->dao->lastInsertID();
  99. $this->loadModel('program')->refreshProjectStats($effort->project);
  100. return $effortID;
  101. }
  102. /**
  103. * 批量创建任务后的其他数据处理。
  104. * other data process after task batch create.
  105. *
  106. * @param array $taskIdList
  107. * @param object $parent
  108. * @access public
  109. * @return bool
  110. */
  111. public function afterBatchCreate($taskIdList, $parent = null)
  112. {
  113. /* Process other data after split task. */
  114. if($parent && !empty($taskIdList))
  115. {
  116. $this->afterSplitTask($parent, $taskIdList);
  117. }
  118. return !dao::isError();
  119. }
  120. /**
  121. * 批量编辑任务后的其他数据处理。
  122. * other data process after task batch edit.
  123. *
  124. * @param object[] $tasks
  125. * @param object[] $oldTasks
  126. * @access public
  127. * @return bool
  128. */
  129. public function afterBatchUpdate($tasks, $oldTasks = array())
  130. {
  131. $this->loadModel('story');
  132. $this->loadModel('kanban');
  133. $this->loadModel('feedback');
  134. $today = helper::today();
  135. $currentAccount = $this->app->user->account;
  136. $syncStatus = false;
  137. foreach($tasks as $taskID => $task)
  138. {
  139. $oldTask = zget($oldTasks, $taskID);
  140. /* Record effort. */
  141. if(!empty($task->consumed) && $task->consumed > $oldTask->consumed)
  142. {
  143. $record = new stdclass();
  144. $record->account = $currentAccount;
  145. $record->task = $taskID;
  146. $record->date = $today;
  147. $record->left = $task->left;
  148. $record->consumed = $task->consumed - $oldTask->consumed;
  149. $this->addTaskEffort($record);
  150. }
  151. /* Update Kanban and story stage. */
  152. if($oldTask->story) $this->story->setStage($oldTask->story);
  153. if($task->status != $oldTask->status) $this->kanban->updateLane($oldTask->execution, 'task', $taskID);
  154. /* Update parent task's status, date and hour. */
  155. if($oldTask->parent > 0)
  156. {
  157. $this->updateParentStatus($taskID);
  158. $this->computeBeginAndEnd($oldTask->parent);
  159. }
  160. if($this->config->edition != 'open' && $oldTask->feedback && !isset($feedbacks[$oldTask->feedback]))
  161. {
  162. $feedbacks[$oldTask->feedback] = $oldTask->feedback;
  163. $this->feedback->updateStatus('task', $oldTask->feedback, $task->status, $oldTask->status, $taskID);
  164. }
  165. if(!empty($task->story) && !empty($oldTask->isParent) && $this->post->syncChildren) $this->syncStoryToChildren($task);
  166. if(!$syncStatus && $oldTask->status == 'wait' && $task->status == 'doing')
  167. {
  168. $syncStatus = true;
  169. $this->loadModel('common')->syncPPEStatus($oldTask->id);
  170. }
  171. if($this->config->edition != 'open' && $task->story != $oldTask->story)
  172. {
  173. if($oldTask->story > 0)
  174. {
  175. $this->dao->delete()->from(TABLE_RELATION)
  176. ->where('relation')->eq('generated')
  177. ->andWhere('AID')->eq($oldTask->story)
  178. ->andWhere('AType')->eq('story')
  179. ->andWhere('BID')->eq($oldTask->id)
  180. ->andWhere('BType')->eq('task')
  181. ->exec();
  182. }
  183. if($task->story > 0)
  184. {
  185. $relation = new stdClass();
  186. $relation->relation = 'generated';
  187. $relation->AID = $task->story;
  188. $relation->AType = 'story';
  189. $relation->BID = $oldTask->id;
  190. $relation->BType = 'task';
  191. $relation->product = 0;
  192. $this->dao->replace(TABLE_RELATION)->data($relation)->exec();
  193. }
  194. }
  195. }
  196. return !dao::isError();
  197. }
  198. /**
  199. * 处理状态变化之后的操作。
  200. * Process other data after task status changed.
  201. *
  202. * @param object $task
  203. * @param array $changes
  204. * @param string $action Finished|Closed|Started
  205. * @param array $output
  206. * @access public
  207. * @return bool
  208. */
  209. public function afterChangeStatus($task, $changes, $action, $output)
  210. {
  211. /* Process other data. */
  212. if($task->parent > 0) $this->updateParentStatus($task->id);
  213. if($task->isParent) $this->updateChildrenStatus($task->id, $task->status);
  214. if($task->story) $this->loadModel('story')->setStage($task->story);
  215. if(!empty($output)) $this->updateKanbanCell($task->id, $output, $task->execution);
  216. $files = $this->loadModel('file')->saveUpload('task', $task->id);
  217. if($changes || $this->post->comment)
  218. {
  219. $fileAction = !empty($files) ? $this->lang->addFiles . implode(',', $files) . "\n" : '';
  220. $actionID = $this->loadModel('action')->create('task', $task->id, $action, $fileAction . $this->post->comment);
  221. $this->action->logHistory($actionID, $changes);
  222. }
  223. return !dao::isError();
  224. }
  225. /**
  226. * 创建任务后的其他数据处理。
  227. * Other data process after task create.
  228. *
  229. * @param object $task
  230. * @param array $taskIdList
  231. * @param int $bugID
  232. * @param int $todoID
  233. * @access public
  234. * @return bool
  235. */
  236. public function afterCreate($task, $taskIdList, $bugID, $todoID)
  237. {
  238. $this->loadModel('file');
  239. $this->setTaskFiles($taskIdList); // Set attachments for tasks.
  240. foreach($taskIdList as $taskID)
  241. {
  242. /* If the task comes from a bug, update the task and bug information. */
  243. if($bugID > 0)
  244. {
  245. $this->dao->update(TABLE_TASK)->set('fromBug')->eq($bugID)->where('id')->eq($taskID)->exec();
  246. $this->dao->update(TABLE_BUG)->set('toTask')->eq($taskID)->where('id')->eq($bugID)->exec();
  247. $this->loadModel('action')->create('bug', $bugID, 'converttotask', '', $taskID);
  248. }
  249. /* If the task comes from a todo, update the todo information. */
  250. if($todoID > 0)
  251. {
  252. $this->dao->update(TABLE_TODO)->set('status')->eq('done')->where('id')->eq($todoID)->exec();
  253. $this->loadModel('action')->create('todo', $todoID, 'finished', '', "TASK:$taskID");
  254. /* If the todo comes from a feedback, update the feedback information. */
  255. $todo = $this->dao->findByID($todoID)->from(TABLE_TODO)->fetch();
  256. if($this->config->edition != 'open' && $todo->type == 'feedback' && $todo->objectID) $this->loadModel('feedback')->updateStatus('todo', $todo->objectID, 'done', '', $todoID);
  257. }
  258. /* If the task comes from a design, update the task information. */
  259. if(!empty($task->design))
  260. {
  261. $design = $this->loadModel('design')->getByID($task->design);
  262. $this->dao->update(TABLE_TASK)->set('designVersion')->eq($design->version)->where('id')->eq($taskID)->exec();
  263. }
  264. /* If the task comes from a story, update the stage of the story. */
  265. if($task->story) $this->loadModel('story')->setStage($task->story);
  266. }
  267. return !dao::isError();
  268. }
  269. /**
  270. * 拆分任务后更新其他数据。
  271. * Process other data after split task.
  272. *
  273. * @param object $oldParentTask
  274. * @param array $childrenIdList
  275. * @access public
  276. * @return bool
  277. */
  278. public function afterSplitTask($oldParentTask, $childrenIdList = array())
  279. {
  280. $parentID = (int)$oldParentTask->id;
  281. /* When a normal task is consumed, create the subtask and update the parent task status. */
  282. if($oldParentTask->isParent == 0 && $oldParentTask->consumed > 0)
  283. {
  284. $copyTaskID = $this->taskTao->copyTaskData($oldParentTask);
  285. if(dao::isError()) return false;
  286. $this->loadModel('action')->create('task', $copyTaskID, 'Opened', '');
  287. $childrenIdList = arrayUnion(array($copyTaskID), $childrenIdList);
  288. }
  289. $parentTask = new stdclass();
  290. $parentTask->isParent = 1;
  291. $parentTask->lastEditedBy = $this->app->user->account;
  292. $parentTask->lastEditedDate = helper::now();
  293. $this->dao->update(TABLE_TASK)->data($parentTask)->where('id')->eq($parentID)->exec();
  294. $this->updateParentStatus(current($childrenIdList));
  295. $this->computeBeginAndEnd($parentID);
  296. $this->loadModel('program')->refreshProjectStats($oldParentTask->project);
  297. /* Create a action. */
  298. $extra = implode(',', $childrenIdList);
  299. $actionID = $this->loadModel('action')->create('task', $parentID, 'createChildren', '', trim($extra, ','));
  300. /* Create a log history. */
  301. $newParentTask = $this->getByID($parentID);
  302. $changes = common::createChanges($oldParentTask, $newParentTask);
  303. if(!empty($changes)) $this->action->logHistory($actionID, $changes);
  304. return !dao::isError();
  305. }
  306. /**
  307. * 开始任务后的其他数据处理。
  308. * Other data process after task start.
  309. *
  310. * @param object $oldTask
  311. * @param array $changes
  312. * @param float $left
  313. * @param array $output
  314. * @param string $message
  315. * @access public
  316. * @return array|bool
  317. */
  318. public function afterStart($oldTask, $changes, $left, $output = array(), $message = '')
  319. {
  320. /* Update the data of the parent task. */
  321. if($oldTask->parent > 0) $this->computeBeginAndEnd($oldTask->parent);
  322. /* Create related dynamic and record. */
  323. $action = 'Started';
  324. if($oldTask->status == 'pause') $action = 'Restarted';
  325. if($left == 0) $action = 'Finished';
  326. $this->afterChangeStatus($oldTask, $changes, $action, $output);
  327. /* Send Webhook notifications and synchronize status to execution, project and program. */
  328. $oldExecution = $this->loadModel('execution')->fetchByID($oldTask->execution);
  329. $this->loadModel('common')->syncPPEStatus($oldTask->id);
  330. /* Remind whether to update status of the bug, if task which from that bug has been finished. */
  331. if($changes && $this->needUpdateBugStatus($oldTask))
  332. {
  333. $response = $this->taskTao->getRemindBugLink($oldTask, $changes);
  334. if($response) return $response;
  335. }
  336. if($oldExecution->status == 'wait')
  337. {
  338. $inLiteKanban = $this->config->vision == 'lite' && $this->app->tab == 'project' && $this->session->kanbanview == 'kanban';
  339. if(($this->app->tab == 'execution' || $inLiteKanban) && $oldExecution->type == 'kanban') return array('result' => 'success', 'message' => $message, 'load' => true);
  340. }
  341. return true;
  342. }
  343. /**
  344. * 编辑任务后的其他数据处理:记录分数、更改需求阶段、处理父任务变更、更改反馈状态等。
  345. * Additional data processing after updating tasks: record scores, change story stage, handle parent task changes, change feedback status.
  346. *
  347. * @param object $oldTask
  348. * @param object $task
  349. * @access public
  350. * @return void
  351. */
  352. public function afterUpdate($oldTask, $task)
  353. {
  354. /* Update children task. */
  355. if(isset($task->execution) && $task->execution != $oldTask->execution)
  356. {
  357. $newExecution = $this->loadModel('execution')->fetchByID((int)$task->execution);
  358. $task->project = $newExecution->project;
  359. $this->dao->update(TABLE_TASK)->set('execution')->eq($task->execution)->set('module')->eq($task->module)->set('project')->eq($task->project)->where('parent')->eq($task->id)->exec();
  360. }
  361. /* Multi-task change to normal task. */
  362. if($task->mode == 'single') $this->dao->delete()->from(TABLE_TASKTEAM)->where('task')->eq($task->id)->exec();
  363. if(isset($task->version) && $task->version > $oldTask->version) $this->recordTaskVersion($task);
  364. /* Compute task's story stage. */
  365. $this->loadModel('story')->setStage($task->story);
  366. if($task->story != $oldTask->story) $this->story->setStage($oldTask->story);
  367. /* Create score. */
  368. if($task->status == 'done') $this->loadModel('score')->create('task', 'finish', $task->id);
  369. if($task->status == 'closed') $this->loadModel('score')->create('task', 'close', $task->id);
  370. if($task->status != $oldTask->status)
  371. {
  372. $executionInfo = !empty($newExecution) ? $newExecution : $this->loadModel('execution')->fetchByID((int)$oldTask->execution);
  373. if($executionInfo->type == 'kanban') $this->loadModel('kanban')->updateLane($task->execution, 'task', $task->id);
  374. }
  375. $isParentChanged = $task->parent != $oldTask->parent;
  376. /* If there is a parent task before updating the task, update the parent. */
  377. $this->updateParent($task, $isParentChanged);
  378. if($isParentChanged && $oldTask->parent > 0)
  379. {
  380. $oldParentTask = $this->dao->select('*')->from(TABLE_TASK)->where('id')->eq($oldTask->parent)->fetch();
  381. $this->updateParentStatus($task->id, $oldTask->parent, !$isParentChanged);
  382. $this->computeBeginAndEnd($oldTask->parent);
  383. $oldChildCount = $this->dao->select('COUNT(1) AS count')->from(TABLE_TASK)->where('parent')->eq($oldTask->parent)->fetch('count');
  384. if(!$oldChildCount) $this->dao->update(TABLE_TASK)->set('isParent')->eq(0)->where('id')->eq($oldTask->parent)->exec();
  385. $this->dao->update(TABLE_TASK)->set('lastEditedBy')->eq($this->app->user->account)->set('lastEditedDate')->eq(helper::now())->where('id')->eq($oldTask->parent)->exec();
  386. $newParentTask = $this->dao->select('*')->from(TABLE_TASK)->where('id')->eq($oldTask->parent)->fetch();
  387. $changes = common::createChanges($oldParentTask, $newParentTask);
  388. $this->loadModel('action')->create('task', $task->id, 'unlinkParentTask', '', $oldTask->parent, '', false);
  389. $actionID = $this->action->create('task', $oldTask->parent, 'unLinkChildrenTask', '', $task->id, '', false);
  390. if(!empty($changes)) $this->action->logHistory($actionID, $changes);
  391. }
  392. if($this->config->edition != 'open' && $oldTask->feedback) $this->loadModel('feedback')->updateStatus('task', $oldTask->feedback, $task->status, $oldTask->status, $oldTask->id);
  393. if(!empty($oldTask->mode) && empty($task->mode)) $this->dao->delete()->from(TABLE_TASKTEAM)->where('task')->eq($task->id)->exec();
  394. if(!empty($task->story) && $this->post->syncChildren) $this->syncStoryToChildren($task);
  395. if(!empty($task->mode) && $task->story != $oldTask->story && $task->storyVersion > $oldTask->storyVersion) $this->dao->update(TABLE_TASKTEAM)->set('storyVersion')->eq($task->storyVersion)->where('task')->eq($task->id)->exec();
  396. }
  397. /**
  398. * 在任务信息中追加泳道名称。
  399. * Append the lane name to the task information.
  400. *
  401. * @param array $tasks
  402. * @access public
  403. * @return object[]
  404. */
  405. public function appendLane($tasks)
  406. {
  407. $executionIdList = array();
  408. foreach($tasks as $task)
  409. {
  410. $task->lane = '';
  411. if(!isset($executionIdList[$task->execution])) $executionIdList[$task->execution] = $task->execution;
  412. }
  413. $lanes = $this->dao->select('t1.kanban,t1.lane,t2.name,t1.cards')->from(TABLE_KANBANCELL)->alias('t1')
  414. ->leftJoin(TABLE_KANBANLANE)->alias('t2')->on('t1.lane = t2.id')
  415. ->where('t1.kanban')->in($executionIdList)
  416. ->andWhere('t2.deleted')->eq(0)
  417. ->andWhere('t1.type')->eq('task')
  418. ->andWhere("t1.cards")->ne('')
  419. ->fetchAll();
  420. if(empty($lanes)) return $tasks;
  421. foreach($tasks as $task)
  422. {
  423. foreach($lanes as $lane)
  424. {
  425. if($lane->kanban != $task->execution) continue;
  426. if(strpos(",{$lane->cards},", ",{$task->id},") === false) continue;
  427. $task->lane = $lane->name;
  428. break;
  429. }
  430. }
  431. return $tasks;
  432. }
  433. /**
  434. * 任务指派用户。
  435. * Assign a task to a user again.
  436. *
  437. * @param object $task
  438. * @param int $taskID
  439. * @access public
  440. * @return array|false
  441. */
  442. public function assign($task)
  443. {
  444. $oldTask = $this->getById($task->id);
  445. /* Check task left. */
  446. if($oldTask->isParent == '0' && !in_array($oldTask->status, array('done', 'closed')) && isset($task->left) && $task->left == 0)
  447. {
  448. dao::$errors['left'] = sprintf($this->lang->error->notempty, $this->lang->task->left);
  449. return false;
  450. }
  451. if(!empty($task->left) && $task->left < 0)
  452. {
  453. dao::$errors['left'] = sprintf($this->lang->task->error->recordMinus, $this->lang->task->left);
  454. return false;
  455. }
  456. /* Update parent task status. */
  457. if($oldTask->parent > 0) $this->updateParentStatus($task->id);
  458. $this->dao->update(TABLE_TASK)
  459. ->data($task)
  460. ->autoCheck()
  461. ->checkFlow()
  462. ->where('id')->eq($task->id)
  463. ->exec();
  464. if(dao::isError()) return false;
  465. $changes = common::createChanges($oldTask, $task);
  466. /* Record log. */
  467. $actionID = $this->loadModel('action')->create('task', $task->id, 'Assigned', $this->post->comment, $task->assignedTo);
  468. if($actionID) $this->action->logHistory($actionID, $changes);
  469. return $changes;
  470. }
  471. /**
  472. * 批量创建任务。
  473. * Batch create tasks.
  474. *
  475. * @param array $tasks
  476. * @param array $output
  477. * @access public
  478. * @return array|false
  479. */
  480. public function batchCreate($tasks, $output)
  481. {
  482. $this->loadModel('story');
  483. $requireEstimate = strpos($this->config->task->create->requiredFields, 'estimate') !== false;
  484. $executionID = !empty($tasks) ? current($tasks)->execution : 0;
  485. $taskIdList = array();
  486. $parents = array();
  487. foreach($tasks as $rowIndex => $task)
  488. {
  489. /* Get the lane and column of the current task. */
  490. $laneID = $task->lane;
  491. $columnID = $task->column;
  492. $level = $task->level;
  493. unset($task->lane, $task->column, $task->level);
  494. if($requireEstimate)
  495. {
  496. $requiredFields = ',' . $this->config->task->create->requiredFields . ',';
  497. if($this->post->isParent[$rowIndex] == '1')
  498. {
  499. $requiredFields = str_replace(',estimate,', ',', $requiredFields);
  500. }
  501. elseif(strpos($requiredFields, ',estimate,') === false)
  502. {
  503. $requiredFields .= 'estimate';
  504. }
  505. $this->config->task->create->requiredFields = trim(',', $requiredFields);
  506. }
  507. /* Create a task. */
  508. $taskID = $this->create($task);
  509. if(!$taskID) return false;
  510. $parents[$level] = $taskID;
  511. if($level > 0)
  512. {
  513. $task->id = $taskID;
  514. $task->parent = $parents[$level - 1];
  515. $this->dao->update(TABLE_TASK)->set('parent')->eq($task->parent)->where('id')->eq($taskID)->exec();
  516. $this->updateParent($task, false);
  517. }
  518. /* Update Kanban and story stage. */
  519. if(!empty($task->story))
  520. {
  521. $this->story->setStage($task->story);
  522. if($this->config->edition != 'open')
  523. {
  524. $relation = new stdClass();
  525. $relation->relation = 'generated';
  526. $relation->AID = $task->story;
  527. $relation->AType = 'story';
  528. $relation->BID = $taskID;
  529. $relation->BType = 'task';
  530. $relation->product = 0;
  531. $this->dao->replace(TABLE_RELATION)->data($relation)->exec();
  532. }
  533. }
  534. $this->updateKanbanForBatchCreate($taskID, $executionID, $laneID, (int)$columnID);
  535. $this->executeHooks($taskID);
  536. $taskIdList[$taskID] = $taskID;
  537. }
  538. return $taskIdList;
  539. }
  540. /**
  541. * 批量更改任务所属模块。
  542. * Batch change the module of task.
  543. *
  544. * @param array $taskIdList
  545. * @param int $moduleID
  546. * @access public
  547. * @return bool
  548. */
  549. public function batchChangeModule($taskIdList, $moduleID)
  550. {
  551. $now = helper::now();
  552. $oldTasks = $this->getByIdList($taskIdList);
  553. $this->loadModel('action');
  554. foreach($taskIdList as $taskID)
  555. {
  556. $oldTask = zget($oldTasks, $taskID, null);
  557. if(!$oldTask || $moduleID == $oldTask->module) continue;
  558. $task = new stdclass();
  559. $task->lastEditedBy = $this->app->user->account;
  560. $task->lastEditedDate = $now;
  561. $task->module = $moduleID;
  562. $this->dao->update(TABLE_TASK)->data($task)
  563. ->autoCheck()
  564. ->check('module', 'ge', 0)
  565. ->where('id')->eq((int)$taskID)
  566. ->exec();
  567. if(dao::isError()) return false;
  568. $changes = common::createChanges($oldTask, $task);
  569. $actionID = $this->action->create('task', (int)$taskID, 'Edited');
  570. if($actionID) $this->action->logHistory($actionID, $changes);
  571. }
  572. return true;
  573. }
  574. /**
  575. * 批量更新任务。
  576. * Batch update tasks.
  577. *
  578. * @param array $taskData
  579. * @access public
  580. * @return array[]|false
  581. */
  582. public function batchUpdate($taskData)
  583. {
  584. $this->loadModel('action');
  585. $this->loadModel('score');
  586. if($this->config->edition != 'open')
  587. {
  588. $fieldList = $this->loadModel('workflowfield')->getList('task');
  589. foreach($fieldList as $field)
  590. {
  591. if($field->type == 'date' || $field->type == 'datetime') $this->config->task->dateFields[] = $field->field;
  592. }
  593. }
  594. $allChanges = array();
  595. $oldTasks = $taskData ? $this->getByIdList(array_keys($taskData)) : array();
  596. foreach($taskData as $taskID => $task)
  597. {
  598. foreach($this->config->task->dateFields as $field)
  599. {
  600. if(in_array($field, explode(',', $this->config->task->batchedit->requiredFields))) continue;
  601. if(isset($task->$field) && helper::isZeroDate($task->$field)) $task->$field = null;
  602. }
  603. /* Format hours. */
  604. if(isset($task->estimate)) $task->estimate = round((float)$task->estimate, 2);
  605. if(isset($task->consumed)) $task->consumed = round((float)$task->consumed, 2);
  606. if(isset($task->left)) $task->left = round((float)$task->left, 2);
  607. /* Update a task.*/
  608. $this->dao->update(TABLE_TASK)->data($task)
  609. ->autoCheck()
  610. ->batchCheck($this->config->task->batchedit->requiredFields, 'notempty')
  611. ->checkFlow()
  612. ->where('id')->eq($taskID)
  613. ->exec();
  614. if(dao::isError())
  615. {
  616. foreach(dao::getError() as $field => $error) dao::$errors["{$field}[{$taskID}]"] = $error;
  617. return false;
  618. }
  619. $this->executeHooks($taskID);
  620. /* Create the task description of the current version in the database. */
  621. $oldTask = zget($oldTasks, $taskID);
  622. if($task->version > $oldTask->version)
  623. {
  624. $taskSpec = new stdclass();
  625. $taskSpec->task = $taskID;
  626. $taskSpec->version = $task->version;
  627. $taskSpec->name = $task->name;
  628. $taskSpec->estStarted = isset($task->estStarted) ? $task->estStarted : null;
  629. $taskSpec->deadline = isset($task->deadline) ? $task->deadline : null;
  630. $this->dao->insert(TABLE_TASKSPEC)->data($taskSpec)->autoCheck()->exec();
  631. }
  632. if(!empty($oldTask->mode) && $task->story != $oldTask->story)
  633. {
  634. $storyVersion = $this->dao->select('version')->from(TABLE_STORY)->where('id')->eq($task->story)->limit(1)->fetch('version');
  635. $this->dao->update(TABLE_TASKTEAM)->set('storyVersion')->eq($storyVersion)->where('task')->eq($task->id)->exec();
  636. }
  637. if($task->status == 'done') $this->score->create('task', 'finish', $taskID);
  638. if($task->status == 'closed') $this->score->create('task', 'close', $taskID);
  639. $changes = common::createChanges($oldTask, $task);
  640. if(!empty($changes))
  641. {
  642. $extra = '';
  643. array_map(function($field) use (&$extra) { if($field['field'] == 'status') $extra = 'statuschanged'; }, $changes);
  644. $actionID = $this->action->create('task', $taskID, 'Edited', '', $extra);
  645. if($actionID) $this->action->logHistory($actionID, $changes);
  646. $allChanges[$taskID] = $changes;
  647. }
  648. }
  649. $this->score->create('ajax', 'batchEdit');
  650. return $allChanges;
  651. }
  652. /**
  653. * 取消一个任务。
  654. * Cancel a task.
  655. *
  656. * @param object $oldTask
  657. * @param object $task
  658. * @param array $output
  659. * @access public
  660. * @return bool
  661. */
  662. public function cancel($oldTask, $task, $output = array())
  663. {
  664. $this->dao->update(TABLE_TASK)->data($task)->autoCheck()->checkFlow()->where('id')->eq($oldTask->id)->exec();
  665. if(dao::isError()) return false;
  666. if(!empty($oldTask->mode)) $this->dao->update(TABLE_TASKTEAM)->set('status')->eq($task->status)->where('task')->eq($oldTask->id)->exec();
  667. if(!empty($oldTask->fromBug)) $this->dao->update(TABLE_BUG)->set('toTask')->eq(0)->where('id')->eq($oldTask->fromBug)->exec();
  668. /* Cancel a parent task. */
  669. $changes = common::createChanges($oldTask, $task);
  670. $this->afterChangeStatus($oldTask, $changes, 'Canceled', $output);
  671. return true;
  672. }
  673. /**
  674. * 检查当前登录用户是否可以操作日志。
  675. * Check if the current user can operate effort.
  676. *
  677. * @param object $task
  678. * @param object $effort
  679. * @access public
  680. * @return bool
  681. */
  682. public function canOperateEffort($task, $effort = null)
  683. {
  684. if(empty($task->team))
  685. {
  686. if($effort === null) $effort = new stdclass();
  687. return $this->loadModel('common')->canOperateEffort($effort);
  688. }
  689. /* Check for add effort. */
  690. if(empty($effort))
  691. {
  692. $members = array_column($task->team, 'account');
  693. if(!in_array($this->app->user->account, $members)) return false;
  694. if($task->mode == 'linear' && $this->app->user->account != $task->assignedTo) return false;
  695. return true;
  696. }
  697. /* Check for edit and delete effort. */
  698. if($task->mode == 'linear')
  699. {
  700. if(in_array($task->status, array('pause', 'cancel', 'closed'))) return false;
  701. if($task->status == 'doing') return $effort->account == $this->app->user->account;
  702. }
  703. return $this->loadModel('common')->canOperateEffort($effort);
  704. }
  705. /**
  706. * 检查开始日期和截止日期。
  707. * Check estStarted and deadline date.
  708. *
  709. * @param int $executionID
  710. * @param string $estStarted
  711. * @param string $deadline
  712. * @param int|null $rowID
  713. * @access public
  714. * @return false|void
  715. */
  716. public function checkEstStartedAndDeadline($executionID, $estStarted, $deadline, $rowID = null)
  717. {
  718. $beginIndex = $rowID === null ? 'estStarted' : "estStarted[$rowID]";
  719. $endIndex = $rowID === null ? 'deadline' : "deadline[$rowID]";
  720. $execution = $this->loadModel('execution')->getByID($executionID);
  721. if(empty($execution) || empty($this->config->limitTaskDate)) return false;
  722. if(empty($execution->multiple)) $this->lang->execution->common = $this->lang->project->common;
  723. if(!empty($estStarted) && !helper::isZeroDate($estStarted))
  724. {
  725. if($estStarted < $execution->begin) dao::$errors[$beginIndex] = sprintf($this->lang->task->error->beginLtExecution, $this->lang->execution->common, $execution->begin);
  726. if($estStarted > $execution->end) dao::$errors[$beginIndex] = sprintf($this->lang->task->error->beginGtExecution, $this->lang->execution->common, $execution->end);
  727. }
  728. if(!empty($deadline) && !helper::isZeroDate($deadline))
  729. {
  730. if($deadline > $execution->end) dao::$errors[$endIndex] = sprintf($this->lang->task->error->endGtExecution, $this->lang->execution->common, $execution->end);
  731. if($deadline < $execution->begin) dao::$errors[$endIndex] = sprintf($this->lang->task->error->endLtExecution, $this->lang->execution->common, $execution->begin);
  732. }
  733. }
  734. /**
  735. * 关闭任务。
  736. * Close a task.
  737. *
  738. * @param object $oldTask
  739. * @param object $task
  740. * @param mixed[] $output
  741. * @access public
  742. * @return bool|array
  743. */
  744. public function close($oldTask, $task, $output = array())
  745. {
  746. $this->dao->update(TABLE_TASK)->data($task)->autoCheck()->checkFlow()->where('id')->eq((int)$oldTask->id)->exec();
  747. if(dao::isError()) return false;
  748. if(!empty($oldTask->mode)) $this->dao->update(TABLE_TASKTEAM)->set('`status`')->eq($task->status)->where('task')->eq($task->id)->exec();
  749. $changes = common::createChanges($oldTask, $task);
  750. $this->afterChangeStatus($oldTask, $changes, 'Closed', $output);
  751. $this->loadModel('score')->create('task', 'close', $task->id);
  752. /* Confirm need update issue status. */
  753. if(isset($oldTask->fromIssue) && $oldTask->fromIssue > 0)
  754. {
  755. $fromIssue = $this->loadModel('issue')->getByID($oldTask->fromIssue);
  756. if($fromIssue->status != 'closed')
  757. {
  758. $confirmURL = $this->createLink('issue', 'close', "id=$oldTask->fromIssue");
  759. $cancelURL = $this->createLink('task', 'view', "taskID=$oldTask->id");
  760. return array('result' => 'success', 'load' => array('confirm' => sprintf($this->lang->task->remindIssue, $oldTask->fromIssue), 'confirmed' => $confirmURL, 'canceled' => $cancelURL));
  761. }
  762. }
  763. return true;
  764. }
  765. /**
  766. * 根据父任务ID计算父任务的预计开始 实际开始 截止日期。
  767. * Compute begin and end for parent task.
  768. *
  769. * @param int $taskID
  770. * @access public
  771. * @return bool
  772. */
  773. public function computeBeginAndEnd($taskID)
  774. {
  775. /* Get estStarted realStarted and deadline of the sub-tasks. */
  776. $tasks = $this->dao->select('estStarted, realStarted, deadline')->from(TABLE_TASK)->where('parent')->eq($taskID)->andWhere('status')->ne('cancel')->andWhere('deleted')->eq(0)->fetchAll();
  777. if(empty($tasks)) return !dao::isError();
  778. /* Initialize task data and update it. */
  779. $parent = $this->fetchById($taskID);
  780. $taskDateLimit = $this->dao->select('taskDateLimit')->from(TABLE_PROJECT)->where('id')->eq($parent->project)->fetch('taskDateLimit');
  781. if($taskDateLimit == 'limit') return !dao::isError();
  782. /* Compute the earliest estStarted, the earliest realStarted and the latest deadline. */
  783. $earliestEstStarted = '';
  784. $earliestRealStarted = '';
  785. $latestDeadline = '';
  786. foreach($tasks as $task)
  787. {
  788. if(!helper::isZeroDate($task->estStarted) && (empty($earliestEstStarted) || $earliestEstStarted > $task->estStarted)) $earliestEstStarted = $task->estStarted;
  789. if(!helper::isZeroDate($task->realStarted) && (empty($earliestRealStarted) || $earliestRealStarted > $task->realStarted)) $earliestRealStarted = $task->realStarted;
  790. if(!helper::isZeroDate($task->deadline) && (empty($latestDeadline) || $latestDeadline < $task->deadline)) $latestDeadline = $task->deadline;
  791. }
  792. $newTask = array();
  793. if(!empty($earliestEstStarted) && !helper::isZeroDate($parent->estStarted) && $parent->estStarted > $earliestEstStarted) $newTask['estStarted'] = $earliestEstStarted;
  794. if(!empty($earliestRealStarted) && !helper::isZeroDate($parent->realStarted) && $parent->realStarted > $earliestRealStarted) $newTask['realStarted'] = $earliestRealStarted;
  795. if(!empty($latestDeadline) && !helper::isZeroDate($parent->deadline) && $parent->deadline < $latestDeadline) $newTask['deadline'] = $latestDeadline;
  796. if(!empty($newTask)) $this->dao->update(TABLE_TASK)->data($newTask)->autoCheck()->where('id')->eq($taskID)->exec();
  797. if($parent->parent) $this->computeBeginAndEnd($parent->parent);
  798. return !dao::isError();
  799. }
  800. /**
  801. * 计算多人任务工时。
  802. * Compute hours for multiple task.
  803. *
  804. * @param object $oldTask
  805. * @param object $task
  806. * @param array $team
  807. * @param bool $autoStatus
  808. * @access public
  809. * @return object|bool
  810. */
  811. public function computeMultipleHours($oldTask, $task = null, $team = array(), $autoStatus = true)
  812. {
  813. if(!$oldTask) return false;
  814. if(empty($team)) $team = $this->dao->select('*')->from(TABLE_TASKTEAM)->where('task')->eq($oldTask->id)->orderBy('order')->fetchAll('id', false); // If the team is empty, get the team from the task team table.
  815. /* If the team is not empty, compute the team hours. */
  816. if(!empty($team))
  817. {
  818. /* Get members, old team and current task. */
  819. $members = array_column($team, 'account');
  820. $oldTeam = zget($oldTask, 'team', array());
  821. $currentTask = !empty($task) ? clone $task : new stdclass();
  822. if(!isset($currentTask->status)) $currentTask->status = $oldTask->status;
  823. $oldTask->team = $team;
  824. if(isset($task->mode)) $oldTask->mode = $task->mode;
  825. /* If the assignedTo is not empty, the current task assignedTo is assignedTo. */
  826. if(!empty($_POST['assignedTo']) && is_string($_POST['assignedTo']))
  827. {
  828. $currentTask->assignedTo = $this->post->assignedTo;
  829. if(!in_array($currentTask->assignedTo, $members)) $currentTask->assignedTo = $this->getAssignedTo4Multi($members, $oldTask);
  830. }
  831. /* If assignedTo is empty, get the assignedTo for the multiply linear task. */
  832. else
  833. {
  834. $currentTask->assignedTo = $this->getAssignedTo4Multi($members, $oldTask);
  835. }
  836. if(isset($task->mode) && $task->mode == 'multi' && strpos(',wait,doing,pause,', ",{$task->status},") !== false) $currentTask->assignedTo = '';
  837. if($oldTask->assignedTo != $currentTask->assignedTo) $currentTask->assignedDate = helper::now();
  838. $oldTask->team = $oldTeam;
  839. /* Compute estimate and left. */
  840. $currentTask->estimate = 0;
  841. $currentTask->left = 0;
  842. foreach($team as $member)
  843. {
  844. $currentTask->estimate += (float)$member->estimate;
  845. $currentTask->left += (float)$member->left;
  846. }
  847. /* Get task efforts, and compute consumed. */
  848. $efforts = $this->getTaskEfforts($oldTask->id);
  849. $currentTask->consumed = 0;
  850. foreach($efforts as $effort) $currentTask->consumed += (float)$effort->consumed;
  851. /* If task is not empty, the task status is computed and the task is returned. */
  852. if(!empty($task)) return $this->taskTao->computeTaskStatus($currentTask, $oldTask, $task, $autoStatus, !empty($efforts), $members);
  853. /* If task is empty, update the current task. */
  854. $currentTask->consumed = round((float)$currentTask->consumed, 2);
  855. $currentTask->left = round((float)$currentTask->left, 2);
  856. $this->dao->update(TABLE_TASK)->data($currentTask)->autoCheck()->where('id')->eq($oldTask->id)->exec();
  857. }
  858. return !dao::isError();
  859. }
  860. /**
  861. * 根据父任务ID计算父任务的预计、消耗和剩余工时。
  862. * Compute parent task working hours.
  863. *
  864. * @param int|bool $taskID
  865. * @access public
  866. * @return bool
  867. */
  868. public function computeWorkingHours($taskID)
  869. {
  870. if(!$taskID) return true;
  871. /* Get sub-tasks. */
  872. $tasks = $this->dao->select('`id`,`estimate`,`consumed`,`left`, status')->from(TABLE_TASK)->where('parent')->eq($taskID)->andWhere('status')->ne('cancel')->andWhere('deleted')->eq(0)->fetchAll('id');
  873. /* If task doesn't have sub-tasks, clear out the consumed hours. */
  874. if(empty($tasks))
  875. {
  876. $this->dao->update(TABLE_TASK)->set('consumed')->eq(0)->where('id')->eq($taskID)->exec();
  877. return !dao::isError();
  878. }
  879. /* Compute task estimate, consumed and left through sub-tasks. */
  880. $estimate = 0;
  881. $consumed = 0;
  882. $left = 0;
  883. foreach($tasks as $task)
  884. {
  885. $estimate += $task->estimate;
  886. $consumed += $task->consumed;
  887. if($task->status != 'closed') $left += $task->left;
  888. }
  889. /* Initialize task data to update. */
  890. $newTask = new stdclass();
  891. $newTask->estimate = $estimate;
  892. $newTask->consumed = $consumed;
  893. $newTask->left = $left;
  894. $newTask->lastEditedBy = $this->app->user->account;
  895. $newTask->lastEditedDate = helper::now();
  896. /* Update task data. */
  897. $this->dao->update(TABLE_TASK)->data($newTask)->autoCheck()->where('id')->eq($taskID)->exec();
  898. return !dao::isError();
  899. }
  900. /**
  901. * 创建一个任务。
  902. * Create a task.
  903. *
  904. * @param object $task
  905. * @param bool $createAction
  906. * @access public
  907. * @return false|int
  908. */
  909. public function create($task, $createAction = true)
  910. {
  911. /* If the lifetime if the execution is ops and the attribute of execution is request or review, remove story from required fields. */
  912. $execution = $this->dao->findByID($task->execution)->from(TABLE_PROJECT)->fetch();
  913. $requiredFields = ',' . $this->config->task->create->requiredFields . ',';
  914. if($execution && $this->isNoStoryExecution($execution)) $requiredFields = str_replace(',story,', ',', $requiredFields);
  915. /* Insert task data. */
  916. if(!empty($execution->isTpl)) $task->isTpl = $execution->isTpl;
  917. if(empty($task->assignedTo)) unset($task->assignedDate);
  918. if(isset($task->estimate)) $task->estimate = round((float)$task->estimate, 2);
  919. if(isset($task->left)) $task->left = round((float)$task->left, 2);
  920. $this->dao->insert(TABLE_TASK)->data($task, 'docVersions')
  921. ->checkIF($task->estimate != '', 'estimate', 'float')
  922. ->autoCheck()
  923. ->batchCheck($requiredFields, 'notempty')
  924. ->checkFlow()
  925. ->exec();
  926. if(dao::isError()) return false;
  927. /* Get task id. */
  928. $taskID = $this->dao->lastInsertID();
  929. if(!empty($task->parent))
  930. {
  931. $task->id = $taskID;
  932. $this->updateParent($task, false);
  933. unset($task->id);
  934. }
  935. else
  936. {
  937. $this->dao->update(TABLE_TASK)->set('path')->eq(",$taskID,")->where('id')->eq($taskID)->exec();
  938. }
  939. /* Insert task desc data. */
  940. $taskSpec = new stdclass();
  941. $taskSpec->task = $taskID;
  942. $taskSpec->version = $task->version;
  943. $taskSpec->name = $task->name;
  944. if(!empty($task->estStarted)) $taskSpec->estStarted = $task->estStarted;
  945. if(!empty($task->deadline)) $taskSpec->deadline = $task->deadline;
  946. $this->dao->insert(TABLE_TASKSPEC)->data($taskSpec)->autoCheck()->exec();
  947. if(dao::isError()) return false;
  948. $this->dao->commit();
  949. if($createAction)
  950. {
  951. $this->loadModel('action')->create('task', $taskID, 'Opened', '');
  952. if(!empty($task->assignedTo)) $this->action->create('task', $taskID, 'Assigned', '', $task->assignedTo);
  953. }
  954. $this->loadModel('file')->updateObjectID($this->post->uid, $taskID, 'task');
  955. $this->loadModel('score')->create('task', 'create', $taskID);
  956. if(dao::isError()) return false;
  957. return $taskID;
  958. }
  959. /**
  960. * 从GitLab议题创建任务。
  961. * Create task from gitlab issue.
  962. *
  963. * @param object $task
  964. * @param int $executionID
  965. * @access public
  966. * @return int
  967. */
  968. public function createTaskFromGitlabIssue($task, $executionID)
  969. {
  970. $task->version = 1;
  971. $task->openedBy = $this->app->user->account;
  972. $task->lastEditedBy = $this->app->user->account;
  973. $task->assignedDate = isset($task->assignedTo) ? helper::now() : null;
  974. $task->story = 0;
  975. $task->module = 0;
  976. $task->estimate = 0;
  977. $task->deadline = $task->deadline ?? null;
  978. $task->estStarted = null;
  979. $task->left = 0;
  980. $task->pri = 3;
  981. $task->type = 'devel';
  982. $task->execution = $task->execution ?? $executionID;
  983. $task->project = $this->dao->select('project')->from(TABLE_PROJECT)->where('id')->eq($executionID)->fetch('project');
  984. /* Set project of the task to 0 if the project of execution is not exist. */
  985. if(empty($task->project)) $task->project = 0;
  986. $this->dao->insert(TABLE_TASK)->data($task, 'id,product')
  987. ->autoCheck()
  988. ->batchCheck($this->config->task->create->requiredFields, 'notempty')
  989. ->checkIF(!helper::isZeroDate($task->deadline), 'deadline', 'ge', $task->estStarted)
  990. ->exec();
  991. if(dao::isError()) return false;
  992. return $this->dao->lastInsertID();
  993. }
  994. /**
  995. * 创建事务类型的任务。
  996. * Create a task of affair type.
  997. *
  998. * @param object $task
  999. * @param array $assignedToList
  1000. * @access public
  1001. * @return false|array
  1002. */
  1003. public function createTaskOfAffair($task, $assignedToList)
  1004. {
  1005. $taskIdList = array();
  1006. foreach($assignedToList as $assignedTo)
  1007. {
  1008. /* If the type of task is affair and assignedTo is empty, skip it. */
  1009. if(count($assignedToList) > 1 && empty($assignedTo)) continue;
  1010. $task->assignedTo = $assignedTo;
  1011. $taskID = $this->create($task);
  1012. if(!$taskID) return false;
  1013. $taskIdList[] = $taskID;
  1014. }
  1015. if(dao::isError()) return false;
  1016. return $taskIdList;
  1017. }
  1018. /**
  1019. * 创建测试类型的任务。
  1020. * Create a test type task.
  1021. *
  1022. * @param object $task
  1023. * @param array $testTasks
  1024. * @access public
  1025. * @return false|int
  1026. */
  1027. public function createTaskOfTest($task, $testTasks)
  1028. {
  1029. $this->config->task->create->requiredFields = str_replace(array(',estimate,', ',story,', ',estStarted,', ',deadline,', ',module,'), ',', ",{$this->config->task->create->requiredFields},");
  1030. $taskID = $this->create($task);
  1031. if(!$taskID) return false;
  1032. /* If the current task has test subtasks, create test subtasks and update the task information. */
  1033. if(!empty($testTasks))
  1034. {
  1035. $this->createTestChildTasks($taskID, $testTasks);
  1036. $this->computeWorkingHours($taskID);
  1037. $this->computeBeginAndEnd($taskID);
  1038. $this->dao->update(TABLE_TASK)->set('isParent')->eq(1)->where('id')->eq($taskID)->exec();
  1039. }
  1040. if(dao::isError()) return false;
  1041. return $taskID;
  1042. }
  1043. /**
  1044. * 创建关联需求的测试类型的子任务。
  1045. * Create a subtask for the test type story with the story.
  1046. *
  1047. * @param int $taskID
  1048. * @param object[] $testTasks
  1049. * @access public
  1050. * @return void
  1051. */
  1052. public function createTestChildTasks($taskID, $testTasks)
  1053. {
  1054. $this->loadModel('action');
  1055. /* Get the stories of the test tasks. */
  1056. $testStoryIdList = array();
  1057. foreach($testTasks as $task)
  1058. {
  1059. if(!isset($task->story)) continue;
  1060. $testStoryIdList[$task->story] = $task->story;
  1061. }
  1062. $testStories = $this->dao->select('id,title,version,module')->from(TABLE_STORY)->where('id')->in($testStoryIdList)->fetchAll('id');
  1063. $parentTask = $this->fetchById($taskID);
  1064. foreach($testTasks as $task)
  1065. {
  1066. /* If the story id is not exist, skip it. */
  1067. $storyID = isset($task->story) ? $task->story : 0;
  1068. if(!isset($testStories[$storyID])) continue;
  1069. /* Construct a task and create it. */
  1070. $task->parent = $taskID;
  1071. $task->storyVersion = $testStories[$storyID]->version;
  1072. $task->name = $this->lang->task->lblTestStory . " #{$storyID} " . $testStories[$storyID]->title;
  1073. $task->module = $testStories[$storyID]->module;
  1074. $this->dao->insert(TABLE_TASK)->data($task)->exec();
  1075. /* Get task id and create a action. */
  1076. $childTaskID = $this->dao->lastInsertID();
  1077. $this->action->create('task', $childTaskID, 'Opened');
  1078. if(!empty($parentTask)) $this->dao->update(TABLE_TASK)->set('path')->eq("{$parentTask->path}{$childTaskID},")->where('id')->eq($childTaskID)->exec();
  1079. if($this->config->edition != 'open')
  1080. {
  1081. $relation = new stdClass();
  1082. $relation->relation = 'generated';
  1083. $relation->AID = $task->story;
  1084. $relation->AType = 'story';
  1085. $relation->BID = $childTaskID;
  1086. $relation->BType = 'task';
  1087. $relation->product = 0;
  1088. $this->dao->replace(TABLE_RELATION)->data($relation)->exec();
  1089. }
  1090. $this->taskTao->updateRelation($childTaskID, (int)$taskID);
  1091. }
  1092. }
  1093. /**
  1094. * 创建多人任务。
  1095. * Create a multiplayer task.
  1096. *
  1097. * @param object $task
  1098. * @param object $teamData
  1099. * @access public
  1100. * @return false|int
  1101. */
  1102. public function createMultiTask($task, $teamData)
  1103. {
  1104. $task->assignedTo = '';
  1105. $taskID = $this->create($task, false);
  1106. if(!$taskID) return false;
  1107. if(count(array_filter($teamData->team)) < 2) return $taskID;
  1108. /* Manage the team of task and calculate the team hours. */
  1109. $task->id = $taskID;
  1110. $teams = $this->manageTaskTeam($task->mode, $task, $teamData);
  1111. if($teams)
  1112. {
  1113. $this->computeMultipleHours($task);
  1114. $this->loadModel('program')->refreshProjectStats($task->project);
  1115. }
  1116. if(!empty($task->story))
  1117. {
  1118. $storyVersion = $this->dao->select('version')->from(TABLE_STORY)->where('id')->eq($task->story)->limit(1)->fetch('version');
  1119. $this->dao->update(TABLE_TASKTEAM)->set('storyVersion')->eq($storyVersion)->where('task')->eq($task->id)->exec();
  1120. }
  1121. /* Send mail after created team. */
  1122. $this->loadModel('action')->create('task', $taskID, 'Opened', '');
  1123. return $taskID;
  1124. }
  1125. /**
  1126. * 删除工时并更新任务。 Delete the work hour from the task.
  1127. *
  1128. * @param int $effortID
  1129. * @access public
  1130. * @return array|false
  1131. */
  1132. public function deleteWorkhour($effortID)
  1133. {
  1134. $effort = $this->getEffortByID($effortID);
  1135. if(empty($effort))
  1136. {
  1137. dao::$errors[] = $this->lang->notFound;
  1138. return false;
  1139. }
  1140. $task = $this->getById($effort->objectID);
  1141. $data = $this->taskTao->getTaskAfterDeleteWorkhour($effort, $task);
  1142. /* 删除工时;如果任务是多人任务,则重新计算工时。*/
  1143. $this->dao->update(TABLE_EFFORT)->set('deleted')->eq('1')->where('id')->eq($effort->id)->exec();
  1144. if(!empty($task->team)) $data = $this->computeMultipleHours($task, $data) ?? $data;
  1145. /* 更新任务、父任务状态、需求阶段。*/
  1146. $this->dao->update(TABLE_TASK)->data($data) ->where('id')->eq($effort->objectID)->exec();
  1147. if($task->consumed != $data->consumed || $task->left != $data->left) $this->loadModel('program')->refreshProjectStats($task->project);
  1148. if($task->parent > 0) $this->updateParentStatus($task->id);
  1149. if($task->story) $this->loadModel('story')->setStage($task->story);
  1150. /* 计算此工时所对应任务的变更。*/
  1151. $oldTask = new stdclass();
  1152. $oldTask->consumed = $task->consumed;
  1153. $oldTask->left = $task->left;
  1154. $oldTask->status = $task->status;
  1155. $newTask = new stdclass();
  1156. $newTask->consumed = $data->consumed;
  1157. $newTask->left = $data->left;
  1158. $newTask->status = $data->status;
  1159. if(!dao::isError()) return common::createChanges($oldTask, $newTask);
  1160. }
  1161. /**
  1162. * 完成任务。
  1163. * Finish a task.
  1164. *
  1165. * @param object $oldTask
  1166. * @param object $task
  1167. * @access public
  1168. * @return bool|array
  1169. */
  1170. public function finish($oldTask, $task)
  1171. {
  1172. $currentTeam = !empty($oldTask->team) ? $this->getTeamByAccount($oldTask->team) : array();
  1173. if($currentTeam)
  1174. {
  1175. $consumed = $currentTeam->consumed + (float)$this->post->currentConsumed;
  1176. $consumed = round((float)$consumed, 2);
  1177. $this->dao->update(TABLE_TASKTEAM)->set('left')->eq(0)->set('consumed')->eq($consumed)->set('status')->eq('done')->where('id')->eq($currentTeam->id)->exec();
  1178. $task = $this->computeMultipleHours($oldTask, $task);
  1179. }
  1180. if(isset($task->consumed)) $task->consumed = round((float)$task->consumed, 2);
  1181. $this->dao->update(TABLE_TASK)->data($task)->autoCheck()->checkFlow()->where('id')->eq((int)$oldTask->id)->exec();
  1182. if(dao::isError()) return false;
  1183. if($task->consumed != $oldTask->consumed || $task->left != $oldTask->left) $this->loadModel('program')->refreshProjectStats($oldTask->project);
  1184. return common::createChanges($oldTask, $task);
  1185. }
  1186. /**
  1187. * 获取多人串行任务的指派人。
  1188. * Get the assignedTo for the multiply linear task.
  1189. *
  1190. * @param string|array $members
  1191. * @param object $task
  1192. * @param string $type current|next
  1193. * @access public
  1194. * @return string
  1195. */
  1196. public function getAssignedTo4Multi($members, $task, $type = 'current')
  1197. {
  1198. if(!$members || empty($task->team) || $task->mode != 'linear') return $task->assignedTo;
  1199. /* Format task team members. */
  1200. if(!is_array($members)) $members = explode(',', trim($members, ','));
  1201. $members = array_values($members);
  1202. if(is_object($members[0])) $members = array_column($members, 'account');
  1203. /* Get the member of the first unfinished task. */
  1204. $teamHours = array_values($task->team);
  1205. foreach($members as $i => $account)
  1206. {
  1207. if(isset($teamHours[$i]) && $teamHours[$i]->status == 'done') continue;
  1208. if($type == 'current') return $account;
  1209. break;
  1210. }
  1211. /* Get the member of the second unfinished task. */
  1212. if($type == 'next' && isset($members[$i + 1])) return $members[$i + 1];
  1213. return $task->openedBy;
  1214. }
  1215. /**
  1216. * 通过任务ID获取任务的信息。
  1217. * Get task info by ID.
  1218. *
  1219. * @param int $taskID
  1220. * @param bool $setImgSize
  1221. * @access public
  1222. * @return false|object
  1223. */
  1224. public function getByID($taskID, $setImgSize = false, $vision = '')
  1225. {
  1226. if(common::isTutorialMode()) return $this->loadModel('tutorial')->getTask();
  1227. if($vision == '') $vision = $this->config->vision; // TODO: $vision is for compatibling with viewing drill data.
  1228. $task = $this->dao->select('t1.*, t2.id AS storyID, t2.title AS storyTitle, t2.version AS latestStoryVersion, t2.status AS storyStatus, t2.deleted AS storyDeleted')
  1229. ->from(TABLE_TASK)->alias('t1')
  1230. ->leftJoin(TABLE_STORY)->alias('t2')->on('t1.story = t2.id')
  1231. ->where('t1.id')->eq($taskID)
  1232. ->beginIf($vision != 'all')->andWhere('t1.vision')->eq($this->config->vision)->fi()
  1233. ->fetch();
  1234. if(!$task) return false;
  1235. /* Format data. */
  1236. $task->openedDate = !empty($task->openedDate) ? substr($task->openedDate, 0, 19) : null;
  1237. $task->finishedDate = !empty($task->finishedDate) ? substr($task->finishedDate, 0, 19) : null;
  1238. $task->canceledDate = !empty($task->canceledDate) ? substr($task->canceledDate, 0, 19) : null;
  1239. $task->closedDate = !empty($task->closedDate) ? substr($task->closedDate, 0, 19) : null;
  1240. $task->lastEditedDate = !empty($task->lastEditedDate) ? substr($task->lastEditedDate, 0, 19) : null;
  1241. $task->realStarted = !empty($task->realStarted) ? substr($task->realStarted, 0, 19) : null;
  1242. /* Get the child tasks of the parent task. */
  1243. $childIdList = $this->getAllChildId($taskID, false);
  1244. $children = $this->dao->select('t1.*, t2.id AS storyID, t2.title AS storyTitle, t2.version AS latestStoryVersion, t2.status AS storyStatus')
  1245. ->from(TABLE_TASK)->alias('t1')
  1246. ->leftJoin(TABLE_STORY)->alias('t2')->on('t1.story = t2.id')
  1247. ->where('t1.id')->in($childIdList)
  1248. ->beginIf($vision != 'all')->andWhere('t1.vision')->eq($this->config->vision)->fi()
  1249. ->fetchAll('id', false);
  1250. foreach($children as $child)
  1251. {
  1252. $child->team = array();
  1253. $child->members = array();
  1254. }
  1255. $task->children = $this->processTasks($children);
  1256. if($task->parent > 0) $task->parentName = $this->dao->findById($task->parent)->from(TABLE_TASK)->fetch('name');
  1257. /* Get task team and team members. */
  1258. if(!empty($task->mode))
  1259. {
  1260. $task->members = array();
  1261. $task->team = $this->getTeamByTask($taskID);
  1262. foreach($task->team as $member)
  1263. {
  1264. $task->members[$member->account] = $member->account;
  1265. if($member->account == $this->app->user->account) $task->storyVersion = $member->storyVersion;
  1266. }
  1267. }
  1268. else
  1269. {
  1270. $task->members = array();
  1271. $task->team = false;
  1272. }
  1273. $task = $this->loadModel('file')->replaceImgURL($task, 'desc');
  1274. $task->files = $this->file->getByObject('task', $taskID);
  1275. if($setImgSize && $task->desc) $task->desc = $this->file->setImgSize($task->desc);
  1276. /* Process a task, compute its progress and get its related information. */
  1277. return $this->processTask($task, false);
  1278. }
  1279. /**
  1280. * 通过任务ID列表批量获取任务信息。
  1281. * Get the task information from the task ID list.
  1282. *
  1283. * @param array $taskIdList
  1284. * @access public
  1285. * @return object[]
  1286. */
  1287. public function getByIdList($taskIdList = array())
  1288. {
  1289. if(empty($taskIdList)) return array();
  1290. return $this->dao->select('*')->from(TABLE_TASK)
  1291. ->where('deleted')->eq(0)
  1292. ->andWhere('id')->in($taskIdList)
  1293. ->fetchAll('id', false);
  1294. }
  1295. /**
  1296. * 获取指定的任务 id name 键值对。
  1297. * Get task pairs by task ID list.
  1298. *
  1299. * @param array $taskIdList
  1300. * @access public
  1301. * @return array
  1302. */
  1303. public function getPairsByIdList($taskIdList = array())
  1304. {
  1305. $taskPairs = $this->dao->select('id, name')->from(TABLE_TASK)->where('deleted')->eq('0')->andWhere('id')->in($taskIdList)->fetchPairs();
  1306. return array(0 => '') + $taskPairs;
  1307. }
  1308. /**
  1309. * 获取按每天完成统计的报表数据。
  1310. * Get report data of finished tasks per day.
  1311. *
  1312. * @access public
  1313. * @return object[]
  1314. */
  1315. public function getDataOfFinishedTasksPerDay()
  1316. {
  1317. $tasks = $this->dao->select("id, DATE_FORMAT(`finishedDate`, '%Y-%m-%d') AS `date`")->from(TABLE_TASK)
  1318. ->where($this->reportCondition())
  1319. ->andWhere('finishedDate')->notZeroDatetime()
  1320. ->orderBy('finishedDate asc')
  1321. ->fetchAll('id');
  1322. if(!$tasks) return array();
  1323. return $this->processData4Report($tasks, array(), 'date');
  1324. }
  1325. /**
  1326. * 获取按指派给统计的报表数据。
  1327. * Get report data of tasks per assignedTo.
  1328. *
  1329. * @access public
  1330. * @return object[]
  1331. */
  1332. public function getDataOfTasksPerAssignedTo()
  1333. {
  1334. $tasks = $this->dao->select('id,assignedTo')->from(TABLE_TASK)->alias('t1')
  1335. ->where($this->reportCondition())
  1336. ->fetchAll('id');
  1337. if(!$tasks) return array();
  1338. $dataList = $this->processData4Report($tasks, array(), 'assignedTo');
  1339. /* Get user's realname. */
  1340. if(!isset($this->users)) $this->users = $this->loadModel('user')->getPairs('noletter');
  1341. foreach($dataList as $account => $data)
  1342. {
  1343. if(isset($this->users[$account])) $data->name = $this->users[$account];
  1344. }
  1345. return $dataList;
  1346. }
  1347. /**
  1348. * 获取按关闭原因统计的报表数据。
  1349. * Get report data of tasks per closed reason.
  1350. *
  1351. * @access public
  1352. * @return object[]
  1353. */
  1354. public function getDataOfTasksPerClosedReason()
  1355. {
  1356. $tasks = $this->dao->select('id,closedReason')->from(TABLE_TASK)->alias('t1')
  1357. ->where($this->reportCondition())
  1358. ->andWhere('closedReason')->ne('')
  1359. ->fetchAll('id');
  1360. if(!$tasks) return array();
  1361. $dataList = $this->processData4Report($tasks, array(), 'closedReason');
  1362. foreach($dataList as $closedReason => $data)
  1363. {
  1364. if(isset($this->lang->task->reasonList[$closedReason])) $data->name = $this->lang->task->reasonList[$closedReason];
  1365. }
  1366. return $dataList;
  1367. }
  1368. /**
  1369. * 获取按消耗时间统计的报表数据。
  1370. * Get report data of tasks per consumed.
  1371. *
  1372. * @access public
  1373. * @return object[]
  1374. */
  1375. public function getDataOfTasksPerConsumed()
  1376. {
  1377. $tasks = $this->dao->select('id,consumed')->from(TABLE_TASK)->alias('t1')
  1378. ->where($this->reportCondition())
  1379. ->fetchAll('id');
  1380. if(!$tasks) return array();
  1381. $children = $this->dao->select('id,parent,consumed')->from(TABLE_TASK)->where('parent')->in(array_keys($tasks))->fetchAll('id');
  1382. return $this->processData4Report($tasks, $children, 'consumed');
  1383. }
  1384. /**
  1385. * 获取按照截止日期统计的报表数据。
  1386. * Get report data of tasks per deadline.
  1387. *
  1388. * @access public
  1389. * @return object[]
  1390. */
  1391. public function getDataOfTasksPerDeadline()
  1392. {
  1393. $tasks = $this->dao->select('id,deadline')->from(TABLE_TASK)->alias('t1')
  1394. ->where($this->reportCondition())
  1395. ->orderBy('deadline asc')
  1396. ->fetchAll('id');
  1397. if(!$tasks) return array();
  1398. return $this->processData4Report($tasks, array(), 'deadline');
  1399. }
  1400. /**
  1401. * 获取按预计时间统计的报表数据。
  1402. * Get report data of tasks per estimate.
  1403. *
  1404. * @access public
  1405. * @return object[]
  1406. */
  1407. public function getDataOfTasksPerEstimate()
  1408. {
  1409. $tasks = $this->dao->select('id,estimate')->from(TABLE_TASK)->alias('t1')
  1410. ->where($this->reportCondition())
  1411. ->fetchAll('id');
  1412. if(!$tasks) return array();
  1413. $children = $this->dao->select('id,parent,estimate')->from(TABLE_TASK)->where('parent')->in(array_keys($tasks))->fetchAll('id');
  1414. return $this->processData4Report($tasks, $children, 'estimate');
  1415. }
  1416. /**
  1417. * 获取按执行任务数统计的报表数据。
  1418. * Get report data of tasks per execution.
  1419. *
  1420. * @access public
  1421. * @return object[]
  1422. */
  1423. public function getDataOfTasksPerExecution()
  1424. {
  1425. $tasks = $this->taskTao->getListByReportCondition('execution', $this->reportCondition());
  1426. if(!$tasks) return array();
  1427. $dataList = $this->processData4Report($tasks, array(), 'execution');
  1428. /* Get execution names for these tasks. */
  1429. $executions = $this->loadModel('execution')->fetchPairs(0, 'all', true, true);
  1430. foreach($dataList as $executionID => $data) $data->name = isset($executions[$executionID]) ? $executions[$executionID] : $this->lang->report->undefined;
  1431. return $dataList;
  1432. }
  1433. /**
  1434. * 获取按由谁完成统计的报表数据。
  1435. * Get report data of tasks per finishedBy.
  1436. *
  1437. * @access public
  1438. * @return object[]
  1439. */
  1440. public function getDataOfTasksPerFinishedBy()
  1441. {
  1442. $tasks = $this->dao->select('id,finishedBy')->from(TABLE_TASK)->alias('t1')
  1443. ->where($this->reportCondition())
  1444. ->andWhere('finishedBy')->ne('')
  1445. ->fetchAll('id');
  1446. if(!$tasks) return array();
  1447. $dataList = $this->processData4Report($tasks, array(), 'finishedBy');
  1448. /* Get user's realname. */
  1449. if(!isset($this->users)) $this->users = $this->loadModel('user')->getPairs('noletter');
  1450. foreach($dataList as $account => $data)
  1451. {
  1452. if(isset($this->users[$account])) $data->name = $this->users[$account];
  1453. }
  1454. return $dataList;
  1455. }
  1456. /**
  1457. * 获取按剩余时间统计的报表数据。
  1458. * Get report data of tasks per left.
  1459. *
  1460. * @access public
  1461. * @return object[]
  1462. */
  1463. public function getDataOfTasksPerLeft()
  1464. {
  1465. $tasks = $this->dao->select('id,`left`')->from(TABLE_TASK)->alias('t1')
  1466. ->where($this->reportCondition())
  1467. ->fetchAll('id');
  1468. if(!$tasks) return array();
  1469. $children = $this->dao->select('id,parent,`left`')->from(TABLE_TASK)->where('parent')->in(array_keys($tasks))->fetchAll('id');
  1470. return $this->processData4Report($tasks, $children, 'left');
  1471. }
  1472. /**
  1473. * 获取按模块任务数统计的报表数据。
  1474. * Get report data of tasks per module.
  1475. *
  1476. * @access public
  1477. * @return object[]
  1478. */
  1479. public function getDataOfTasksPerModule()
  1480. {
  1481. $tasks = $this->dao->select('id,module')->from(TABLE_TASK)->alias('t1')
  1482. ->where($this->reportCondition())
  1483. ->fetchAll('id');
  1484. if(!$tasks) return array();
  1485. $dataList = $this->processData4Report($tasks, array(), 'module');
  1486. /* Get modules name. */
  1487. $modules = $this->loadModel('tree')->getModulesName(array_keys($dataList), true, true);
  1488. foreach($dataList as $moduleID => $data)
  1489. {
  1490. $data->name = isset($modules[$moduleID]) ? $modules[$moduleID] : '/';
  1491. }
  1492. return $dataList;
  1493. }
  1494. /**
  1495. * 获取按照优先级统计的报表数据。
  1496. * Get report data of tasks per priority.
  1497. *
  1498. * @access public
  1499. * @return object[]
  1500. */
  1501. public function getDataOfTasksPerPri()
  1502. {
  1503. $tasks = $this->dao->select('id,pri')->from(TABLE_TASK)->alias('t1')
  1504. ->where($this->reportCondition())
  1505. ->fetchAll('id');
  1506. if(!$tasks) return array();
  1507. $dataList = $this->processData4Report($tasks, array(), 'pri');
  1508. foreach($dataList as $pri) $pri->name = $this->lang->task->priList[$pri->name];
  1509. return $dataList;
  1510. }
  1511. /**
  1512. * 获取按任务类型统计的报表数据。
  1513. * Get report data of tasks per type.
  1514. *
  1515. * @access public
  1516. * @return object[]
  1517. */
  1518. public function getDataOfTasksPerType()
  1519. {
  1520. $tasks = $this->dao->select('id,type')->from(TABLE_TASK)->alias('t1')
  1521. ->where($this->reportCondition())
  1522. ->fetchAll('id');
  1523. if(!$tasks) return array();
  1524. $dataList = $this->processData4Report($tasks, array(), 'type');
  1525. foreach($dataList as $type => $data)
  1526. {
  1527. if(isset($this->lang->task->typeList[$type])) $data->name = $this->lang->task->typeList[$type];
  1528. }
  1529. return $dataList;
  1530. }
  1531. /**
  1532. * 按照任务状态统计的报表数据。
  1533. * Get report data of status.
  1534. *
  1535. * @access public
  1536. * @return object[]
  1537. */
  1538. public function getDataOfTasksPerStatus()
  1539. {
  1540. $tasks = $this->dao->select('id,status')->from(TABLE_TASK)->alias('t1')
  1541. ->where($this->reportCondition())
  1542. ->fetchAll('id');
  1543. if(!$tasks) return array();
  1544. $dataList = $this->processData4Report($tasks, array(), 'status');
  1545. foreach($dataList as $status => $data) $data->name = $this->lang->task->statusList[$status];
  1546. return $dataList;
  1547. }
  1548. /**
  1549. * 根据日志ID获取日志信息和是否最后一次日志。
  1550. * Get effort data and check last by id.
  1551. *
  1552. * @param int $effortID
  1553. * @access public
  1554. * @return false|object
  1555. */
  1556. public function getEffortByID($effortID)
  1557. {
  1558. $effort = $this->dao->select('*')->from(TABLE_EFFORT)
  1559. ->where('id')->eq($effortID)
  1560. ->fetch();
  1561. if(!$effort ) return false;
  1562. /* If the estimate is the last of its task, status of task will be checked. */
  1563. $lastID = $this->dao->select('id')->from(TABLE_EFFORT)
  1564. ->where('objectID')->eq($effort->objectID)
  1565. ->andWhere('objectType')->eq('task')
  1566. ->andWhere('deleted')->eq('0')
  1567. ->orderBy('date_desc,id_desc')->limit(1)->fetch('id');
  1568. $effort->isLast = $lastID == $effort->id;
  1569. return $effort;
  1570. }
  1571. /**
  1572. * 获取执行下的任务列表信息。
  1573. * Get the task list under a execution.
  1574. *
  1575. * @param int|array $executionID
  1576. * @param int $productID
  1577. * @param string|array $type all|assignedbyme|myinvolved|undone|needconfirm|assignedtome|finishedbyme|delayed|review|wait|doing|done|pause|cancel|closed|array('wait','doing','done','pause','cancel','closed')
  1578. * @param array $modules
  1579. * @param string $orderBy
  1580. * @param object $pager
  1581. * @access public
  1582. * @return array
  1583. */
  1584. public function getExecutionTasks($executionID, $productID = 0, $type = 'all', $modules = array(), $orderBy = 'status_asc, id_desc', $pager = null)
  1585. {
  1586. $tasks = $this->taskTao->fetchExecutionTasks($executionID, $productID, $type, $modules, $orderBy, $pager);
  1587. if(empty($tasks)) return array();
  1588. $taskTeam = $this->taskTao->getTeamMembersByIdList(array_keys($tasks));
  1589. foreach($tasks as $task)
  1590. {
  1591. if(isset($taskTeam[$task->id])) $tasks[$task->id]->team = $taskTeam[$task->id];
  1592. }
  1593. if($this->config->vision == 'lite') $tasks = $this->appendLane($tasks);
  1594. $userList = $this->loadModel('user')->getPairs('noletter|noclosed');
  1595. $parentIdList = array();
  1596. foreach($tasks as &$task)
  1597. {
  1598. $task->assignedToRealName = zget($userList, $task->assignedTo);
  1599. if($task->parent <= 0 || isset($tasks[$task->parent]) || isset($parentIdList[$task->parent])) continue;
  1600. $parentIdList[$task->parent] = $task->parent;
  1601. }
  1602. return $this->processTasks($tasks);
  1603. }
  1604. /**
  1605. * 获取任务 id:name 的数组。
  1606. * Get an array of task id:name.
  1607. *
  1608. * @param int $executionID
  1609. * @param string $status
  1610. * @param string $orderBy
  1611. * @access public
  1612. * @return array
  1613. */
  1614. public function getExecutionTaskPairs($executionID, $status = 'all', $orderBy = 'finishedBy, id_desc')
  1615. {
  1616. $tasks = $this->dao->select('t1.id,t1.name,t1.parent,t2.realname AS finishedByRealName')
  1617. ->from(TABLE_TASK)->alias('t1')
  1618. ->leftJoin(TABLE_USER)->alias('t2')->on('t1.finishedBy = t2.account')
  1619. ->where('t1.execution')->eq((int)$executionID)
  1620. ->andWhere('t1.deleted')->eq(0)
  1621. ->beginIF($status != 'all')->andWhere('t1.status')->in($status)->fi()
  1622. ->orderBy($orderBy)
  1623. ->fetchAll('id');
  1624. $taskPairs = array();
  1625. foreach($tasks as $taskID => $task)
  1626. {
  1627. $prefix = $task->parent > 0 ? "[{$this->lang->task->childrenAB}] " : '';
  1628. $taskPairs[$taskID] = $prefix . "$task->id:" . (empty($task->finishedByRealName) ? '' : "$task->finishedByRealName:") . "$task->name";;
  1629. }
  1630. return $taskPairs;
  1631. }
  1632. /**
  1633. * 获取项目下的任务列表。
  1634. * Get project task list.
  1635. *
  1636. * @param int $projectID
  1637. * @param bool $isPairs
  1638. * @param int $executionID
  1639. * @access public
  1640. * @return array
  1641. */
  1642. public function getProjectTaskList($projectID, $isPairs = false, $executionID = 0)
  1643. {
  1644. $taskList = $this->dao->select('*')->from(TABLE_TASK)
  1645. ->where('deleted')->eq(0)
  1646. ->beginIF($this->config->vision)->andWhere('vision')->eq($this->config->vision)->fi()
  1647. ->andWhere('project')->eq($projectID)
  1648. ->beginIF(!$this->app->user->admin)->andWhere('execution')->in($this->app->user->view->sprints)->fi()
  1649. ->fetchAll('id');
  1650. $taskPairs = array();
  1651. foreach($taskList as $taskID => $task)
  1652. {
  1653. $prefix = $task->parent > 0 ? "[{$this->lang->task->childrenAB}] " : '';
  1654. $finishedBy = !empty($task->finishedByRealName) ? "{$task->finishedByRealName}:" : '';
  1655. $task->rawName = !empty($task->rawName) ? $task->rawName : $task->name;
  1656. $task->name = $task->parent > 0 ? "{$prefix}{$task->id}:{$finishedBy}{$task->rawName}" : $task->rawName;
  1657. if(!empty($executionID) && $task->execution != $executionID) $task->name = $task->name . " [{$this->lang->task->otherExecution}]";
  1658. if(!empty($taskList[$task->parent]))
  1659. {
  1660. $parent = $taskList[$task->parent];
  1661. $parent->rawName = !empty($parent->rawName) ? $parent->rawName : $parent->name;
  1662. $parent->name = "[{$this->lang->task->parentAB}] " . "{$parent->id}:" . (empty($parent->finishedByRealName) ? '' : "$parent->finishedByRealName:") . $parent->rawName;
  1663. }
  1664. }
  1665. foreach($taskList as $taskID => $task) $taskPairs[$taskID] = $task->name;
  1666. return $isPairs ? $taskPairs : $taskList;
  1667. }
  1668. /**
  1669. * 获取导出的任务数据。
  1670. * Get export task information.
  1671. *
  1672. * @param string $orderBy
  1673. * @access public
  1674. * @return object[]
  1675. */
  1676. public function getExportTasks($orderBy)
  1677. {
  1678. $sort = common::appendOrder($orderBy);
  1679. $tasks = array();
  1680. if($this->session->taskOnlyCondition)
  1681. {
  1682. $tasks = $this->dao->select('*')->from(TABLE_TASK)->alias('t1')->where($this->session->taskQueryCondition)
  1683. ->beginIF($this->post->exportType == 'selected')->andWhere('t1.id')->in($this->cookie->checkedItem)->fi()
  1684. ->orderBy($sort)
  1685. ->fetchAll('id', false);
  1686. foreach($tasks as $key => $task)
  1687. {
  1688. /* Compute task progress. */
  1689. if($task->consumed == 0 && $task->left == 0)
  1690. {
  1691. $task->progress = 0;
  1692. }
  1693. elseif($task->consumed != 0 && $task->left == 0)
  1694. {
  1695. $task->progress = 100;
  1696. }
  1697. else
  1698. {
  1699. $task->progress = round($task->consumed / ($task->consumed + $task->left), 2) * 100;
  1700. }
  1701. $task->progress .= '%';
  1702. }
  1703. }
  1704. elseif($this->session->taskQueryCondition)
  1705. {
  1706. $stmt = $this->dbh->query($this->session->taskQueryCondition . ($this->post->exportType == 'selected' ? " AND t1.id IN({$this->cookie->checkedItem})" : '') . " ORDER BY " . strtr($orderBy, '_', ' '));
  1707. while($row = $stmt->fetch()) $tasks[$row->id] = $row;
  1708. }
  1709. return $this->processExportTasks($tasks);
  1710. }
  1711. /**
  1712. * 通过给定条件获取任务列表信息。
  1713. * Get task list by condition.
  1714. *
  1715. * @param object $condition
  1716. * @param string $orderBy
  1717. * @param object|null $pager
  1718. * @access public
  1719. * @return object[]
  1720. */
  1721. public function getListByCondition($condition, $orderBy = 'id_desc', $pager = null)
  1722. {
  1723. $defaultValueList = array('priList' => array(), 'assignedToList' => array(), 'statusList' => array(), 'idList' => array(), 'taskName' => '');
  1724. foreach($defaultValueList as $key => $defaultValue)
  1725. {
  1726. if(!isset($condition->$key))
  1727. {
  1728. $condition->$key = $defaultValue;
  1729. continue;
  1730. }
  1731. if(strripos($key, 'list') === strlen($key) - 4 && !is_array($condition->$key)) $condition->$key = array_filter(explode(',', $condition->$key));
  1732. }
  1733. return $this->dao->select('*')->from(TABLE_TASK)
  1734. ->where('deleted')->eq(0)
  1735. ->beginIF(!empty($condition->priList))->andWhere('pri')->in(zget($condition, 'priList', array()))->fi()
  1736. ->beginIF(!empty($condition->assignedToList))->andWhere('assignedTo')->in(zget($condition, 'assignedToList', array()))->fi()
  1737. ->beginIF(!empty($condition->statusList))->andWhere('status')->in(zget($condition, 'statusList', array()))->fi()
  1738. ->beginIF(!empty($condition->idList))->andWhere('id')->in(zget($condition, 'idList', array()))->fi()
  1739. ->beginIF(!empty($condition->taskName))->andWhere('name')->like("%{$condition->taskName}%")->fi()
  1740. ->beginIF(!empty($condition->executionList))->andWhere('execution')->in(zget($condition, 'executionList', array()))->fi()
  1741. ->beginIF(!$this->app->user->admin)->andWhere('execution')->in($this->app->user->view->sprints)->fi()
  1742. ->orderBy($orderBy)
  1743. ->page($pager)
  1744. ->fetchAll('id', false);
  1745. }
  1746. /**
  1747. * 通过需求列表获取对应的任务列表,任务只取部分属性。
  1748. * Get the list of task information by the list of stories.
  1749. *
  1750. * @param int[] $storyIdList
  1751. * @param int $executionID
  1752. * @param int $projectID
  1753. * @access public
  1754. * @return object[]
  1755. */
  1756. public function getListByStories($storyIdList, $executionID = 0, $projectID = 0)
  1757. {
  1758. return $this->dao->select('id, story, parent, name, assignedTo, pri, status, estimate, consumed, closedReason, `left`')
  1759. ->from(TABLE_TASK)
  1760. ->where('story')->in($storyIdList)
  1761. ->andWhere('deleted')->eq('0')
  1762. ->beginIF($executionID)->andWhere('execution')->eq($executionID)->fi()
  1763. ->beginIF($projectID)->andWhere('project')->eq($projectID)->fi()
  1764. ->fetchAll('id');
  1765. }
  1766. /**
  1767. * 通过需求获取对应的任务列表,任务只取部分属性。
  1768. * Get the task list by a story.
  1769. *
  1770. * @param int $storyID
  1771. * @param int $executionID
  1772. * @param int $projectID
  1773. * @access public
  1774. * @return object[]
  1775. */
  1776. public function getListByStory($storyID, $executionID = 0, $projectID = 0)
  1777. {
  1778. $tasks = $this->dao->select('id, parent, name, assignedTo, pri, status, isParent, estimate, consumed, closedReason, `left`')
  1779. ->from(TABLE_TASK)
  1780. ->where('story')->eq($storyID)
  1781. ->andWhere('deleted')->eq('0')
  1782. ->beginIF($executionID)->andWhere('execution')->eq($executionID)->fi()
  1783. ->beginIF($projectID)->andWhere('project')->eq($projectID)->fi()
  1784. ->fetchAll('id');
  1785. $parentIdList = array();
  1786. foreach($tasks as $task)
  1787. {
  1788. /* 如果任务不是父任务,或者父任务已经在任务列表中,或者父任务已经在当前列表中,则跳过处理。*/
  1789. if($task->parent <= 0 || isset($parentIdList[$task->parent])) continue;
  1790. $parentIdList[$task->parent] = $task->parent;
  1791. }
  1792. $parentTasks = $this->getByIdList($parentIdList);
  1793. $tasks = $this->taskTao->buildTaskTree($tasks, $parentTasks); /* 将子任务放到父任务里,或者将父任务的名字放到子任务里。*/
  1794. return $this->taskTao->batchComputeProgress($tasks); /* 通过任务的消耗和剩余工时计算任务及其子任务的进度,结果以百分比的数字部分显示。*/
  1795. }
  1796. /**
  1797. * 获取任务的团队成员。
  1798. * Get task's team member pairs.
  1799. *
  1800. * @param object $task
  1801. * @access public
  1802. * @return array
  1803. */
  1804. public function getMemberPairs($task)
  1805. {
  1806. if(!is_array($task->team)) return array();
  1807. $users = $this->loadModel('user')->getTeamMemberPairs($task->execution, 'execution', 'nodeleted');
  1808. $members = array();
  1809. foreach($task->team as $member)
  1810. {
  1811. if(isset($users[$member->account])) $members[$member->account] = $users[$member->account];
  1812. }
  1813. return $members;
  1814. }
  1815. /**
  1816. * 获取并行任务的团队成员。
  1817. * Get parallel task's members.
  1818. *
  1819. * @param int $taskID
  1820. * @access public
  1821. * @return array
  1822. */
  1823. public function getMultiTaskMembers($taskID)
  1824. {
  1825. $taskType = $this->dao->select('mode')->from(TABLE_TASK)->where('id')->eq($taskID)->fetch('mode');
  1826. if($taskType != 'multi') return array();
  1827. $teamMembers = $this->dao->select('account')->from(TABLE_TASKTEAM)->where('task')->eq($taskID)->fetchPairs();
  1828. return empty($teamMembers) ? $teamMembers : array_keys($teamMembers);
  1829. }
  1830. /**
  1831. * 获取父任务 id:name 的数组。
  1832. * Get an array of parent task id:name.
  1833. *
  1834. * @param int $executionID
  1835. * @param string $appendIdList
  1836. * @param int $taskID
  1837. * @access public
  1838. * @return array
  1839. */
  1840. public function getParentTaskPairs($executionID, $appendIdList = '', $taskID = 0)
  1841. {
  1842. /*
  1843. 过滤多人任务。
  1844. 过滤已经记录工时的任务。
  1845. 过滤已取消、已关闭的任务。
  1846. 过滤自己和后代任务。
  1847. */
  1848. $children = $this->getAllChildId($taskID);
  1849. $taskList = $this->dao->select('id, name, isParent, consumed')->from(TABLE_TASK)
  1850. ->where('deleted')->eq(0)
  1851. ->andWhere('status')->notin('cancel,closed')
  1852. ->andWhere('parent')->eq('0')
  1853. ->andWhere('execution')->eq($executionID)
  1854. ->andWhere('mode')->eq('')
  1855. ->beginIF($children)->andWhere('id')->notin($children)->fi()
  1856. ->beginIF($appendIdList)->orWhere('id')->in($appendIdList)->fi()
  1857. ->fetchAll();
  1858. $taskPairs = array();
  1859. foreach($taskList as $task)
  1860. {
  1861. if(!$task->isParent && $task->consumed > 0) continue;
  1862. $taskPairs[$task->id] = $task->name;
  1863. }
  1864. return $taskPairs;
  1865. }
  1866. /**
  1867. * 给任务的下拉列表增加标签。
  1868. * Add label to task dropdown list.
  1869. *
  1870. * @param array $tasks
  1871. * @access public
  1872. * @return array
  1873. */
  1874. public function addTaskLabel($tasks)
  1875. {
  1876. $taskList = $this->getByIdList(array_keys($tasks));
  1877. $options = array();
  1878. foreach($taskList as $task)
  1879. {
  1880. if($task->isParent)
  1881. {
  1882. $options[] = array('text' => array('html' => "<span class='label rounded-xl ring-0 inverse bg-opacity-10 text-inherit mr-1 size-sm'>{$this->lang->task->parentAB}</span> #{$task->id} {$task->name}"), 'hint' => $task->name, 'value' => $task->id, 'keys' => $task->name);
  1883. }
  1884. elseif($task->parent > 0)
  1885. {
  1886. $options[] = array('text' => array('html' => "<span class='label rounded-xl ring-0 inverse bg-opacity-10 text-inherit mr-1 size-sm'>{$this->lang->task->childrenAB}</span> #{$task->id} {$task->name}"), 'hint' => $task->name, 'value' => $task->id, 'keys' => $task->name);
  1887. }
  1888. else
  1889. {
  1890. $options[] = array('text' => "#{$task->id} {$task->name}", 'hint' => $task->name, 'value' => $task->id, 'keys' => $task->name);
  1891. }
  1892. }
  1893. return $options;
  1894. }
  1895. /**
  1896. * 获取任务所有子任务id。
  1897. * Get all child task id.
  1898. *
  1899. * @param int $taskID
  1900. * @param bool $includeSelf
  1901. * @access public
  1902. * @return array
  1903. */
  1904. public function getAllChildId($taskID, $includeSelf = true)
  1905. {
  1906. if(!$taskID) return [];
  1907. $task = $this->fetchByID($taskID);
  1908. if(!$task) return [];
  1909. return $this->dao->select('id')->from(TABLE_TASK)
  1910. ->where('path')->like($task->path . '%') // 去除左侧的模糊查询以利用索引提高性能。Remove the left fuzzy query to use index to improve performance.
  1911. ->andWhere('deleted')->eq(0)
  1912. ->beginIF(!$includeSelf)->andWhere('id')->ne($taskID)->fi()
  1913. ->fetchPairs();
  1914. }
  1915. /**
  1916. * 获取关联需求的任务数。
  1917. * Get the number of tasks linked with the story.
  1918. *
  1919. * @param array $stories
  1920. * @param int $executionID
  1921. * @access public
  1922. * @return array
  1923. */
  1924. public function getStoryTaskCounts($stories, $executionID = 0)
  1925. {
  1926. if(empty($stories)) return array();
  1927. $taskCounts = $this->dao->select('story, COUNT(id) AS tasks')
  1928. ->from(TABLE_TASK)
  1929. ->where('story')->in($stories)
  1930. ->andWhere('deleted')->eq(0)
  1931. ->beginIF($executionID)->andWhere('execution')->eq($executionID)->fi()
  1932. ->groupBy('story')
  1933. ->fetchPairs();
  1934. foreach($stories as $storyID)
  1935. {
  1936. if(!isset($taskCounts[$storyID])) $taskCounts[$storyID] = 0;
  1937. }
  1938. return $taskCounts;
  1939. }
  1940. /**
  1941. * 根据任务、人员和日志ID查询并排序任务的日志列表。
  1942. * Get task efforts by taskID, account or effortID.
  1943. *
  1944. * @param int|array $taskIdList
  1945. * @param string $account
  1946. * @param int $effortID
  1947. * @param string $orderBy
  1948. * @access public
  1949. * @return array
  1950. */
  1951. public function getTaskEfforts($taskIdList, $account = '', $effortID = 0, $orderBy = 'date,id')
  1952. {
  1953. return $this->dao->select('*')->from(TABLE_EFFORT)
  1954. ->where('objectType')->eq('task')
  1955. ->andWhere('objectID')->in($taskIdList)
  1956. ->andWhere('deleted')->eq('0')
  1957. ->beginIF($account)->andWhere('account')->eq($account)->fi()
  1958. ->beginIF($effortID)->orWhere('id')->eq($effortID)->fi()
  1959. ->orderBy($orderBy)
  1960. ->fetchAll('id', false);
  1961. }
  1962. /**
  1963. * 获取用户所在多人任务的团队工序。
  1964. * Get the team process of the multi-task by account.
  1965. *
  1966. * @param array $teams
  1967. * @param string $account
  1968. * @param array $extra
  1969. * @access public
  1970. * @return object|bool
  1971. */
  1972. public function getTeamByAccount($teams, $account = '', $extra = array('filter' => 'done'))
  1973. {
  1974. if(empty($account)) $account = $this->app->user->account;
  1975. $filterStatus = zget($extra, 'filter', '');
  1976. $effortID = zget($extra, 'effortID', '');
  1977. $repeatUsers = array();
  1978. $taskID = 0;
  1979. foreach($teams as $team)
  1980. {
  1981. if(isset($extra['order']) && $team->order == $extra['order'] && $team->account == $account) return $team;
  1982. if(empty($taskID) && $effortID) $taskID = $team->task;
  1983. if(isset($repeatUsers[$team->account])) $repeatUsers[$team->account] = 1;
  1984. if(!isset($repeatUsers[$team->account])) $repeatUsers[$team->account] = 0;
  1985. }
  1986. /*
  1987. * 1. No repeat account or account is not repeat account;
  1988. * 2. Not by effort;
  1989. * Then direct get team by account.
  1990. */
  1991. if(empty($repeatUsers[$account]) || empty($effortID))
  1992. {
  1993. foreach($teams as $team)
  1994. {
  1995. if(empty($effortID) && $filterStatus && $team->status == $filterStatus) continue;
  1996. if($team->account == $account) return $team;
  1997. }
  1998. }
  1999. elseif($effortID)
  2000. {
  2001. $efforts = $this->getTaskEfforts($taskID, '', $effortID);
  2002. $prevTeam = current($teams);
  2003. foreach($efforts as $effort)
  2004. {
  2005. $currentTeam = reset($teams);
  2006. if($effort->id == $effortID)
  2007. {
  2008. if($effort->account == $currentTeam->account) return $currentTeam;
  2009. if($effort->account == $prevTeam->account) return $prevTeam;
  2010. return false;
  2011. }
  2012. if($effort->left == 0 && $currentTeam->account == $effort->account) $prevTeam = array_shift($teams);
  2013. }
  2014. }
  2015. return false;
  2016. }
  2017. /**
  2018. * 获取指派用户和抄送给用户列表。
  2019. * Get toList and ccList.
  2020. *
  2021. * @param object $task
  2022. * @param string $action
  2023. * @access public
  2024. * @return array|false
  2025. */
  2026. public function getToAndCcList($task, $action)
  2027. {
  2028. /* Set assignedTo and mailto. */
  2029. $assignedTo = $task->assignedTo;
  2030. $mailto = empty($task->mailto) ? array() : explode(',', $task->mailto);
  2031. if($task->mode == 'multi')
  2032. {
  2033. $teamList = $this->getMultiTaskMembers($task->id);
  2034. $teamList = implode(',', $teamList);
  2035. $assignedTo = $teamList;
  2036. }
  2037. /* If the assignor is closed, treat the completion person as the assignor. */
  2038. if(strtolower($assignedTo) == 'closed') $assignedTo = empty($task->finishedBy) ? $task->openedBy : $task->finishedBy;
  2039. /* If the child task is paused, closed or canceled, append the mailto from parent task. */
  2040. if($task->parent > 0)
  2041. {
  2042. $appendParent = false;
  2043. if(in_array($action, array('paused', 'closed', 'canceled')))
  2044. {
  2045. $actionExtra = $this->dao->select('id,extra')->from(TABLE_ACTION)->where('objectType')->eq('task')->andWhere('objectID')->eq($task->id)->andWhere('action')->eq($action)->orderBy('id_desc')->limit(1)->fetch('extra');
  2046. if($actionExtra != 'autobyparent') $appendParent = true;
  2047. }
  2048. elseif($action == 'edited')
  2049. {
  2050. $actionExtra = $this->dao->select('id,extra')->from(TABLE_ACTION)->where('objectType')->eq('task')->andWhere('objectID')->eq($task->id)->andWhere('action')->eq($action)->orderBy('id_desc')->limit(1)->fetch('extra');
  2051. if($actionExtra == 'statuschanged' && in_array($task->status, array('pause', 'close', 'cancel'))) $appendParent = true;
  2052. }
  2053. if($appendParent)
  2054. {
  2055. $parentTasks = $this->dao->select('id,assignedTo,openedBy,finishedBy,mailto')->from(TABLE_TASK)->where('id')->in($task->path)->andWhere('id')->ne($task->id)->fetchAll('id');
  2056. foreach($parentTasks as $parentTask)
  2057. {
  2058. $parentAssignedTo = $parentTask->assignedTo;
  2059. if(strtolower($parentAssignedTo) == 'closed') $parentAssignedTo = empty($parentTask->finishedBy) ? $parentTask->openedBy : $parentTask->finishedBy;
  2060. $mailto[] = $parentAssignedTo;
  2061. $mailto = array_merge($mailto, empty($parentTask->mailto) ? array() : explode(',', $parentTask->mailto));
  2062. }
  2063. }
  2064. }
  2065. $mailto = array_unique(array_filter(array_map(function($account) use($assignedTo){ return $account == $assignedTo ? '' : $account;}, $mailto)));
  2066. if(empty($assignedTo) && empty($mailto)) return false;
  2067. /* If the assignor is empty, consider the first one with the cc as the assignor. */
  2068. if(empty($assignedTo)) $assignedTo = array_shift($mailto);
  2069. if(in_array($action, array('paused', 'closed', 'canceled')) && $task->parent > 0)
  2070. {
  2071. $parentTasks = $this->dao->select('id,assignedTo,finishedBy,mailto')->from(TABLE_TASK)->where('id')->in($task->path)->fetchAll('id');
  2072. foreach($parentTasks as $parentTask)
  2073. {
  2074. $mailto[] = (strtolower($parentTask->assignedTo) == 'closed') ? $parentTask->finishedBy : $parentTask->assignedTo;
  2075. $mailto += is_null($parentTask->mailto) ? array() : explode(',', trim($parentTask->mailto, ','));
  2076. }
  2077. }
  2078. $mailto = array_unique(array_filter(array_map(function($account) use($assignedTo){ return $account == $assignedTo ? '' : $account;}, $mailto)));
  2079. return array($assignedTo, implode(',', $mailto));
  2080. }
  2081. /**
  2082. * 获取未完成的任务。
  2083. * Get the unfinish tasks.
  2084. *
  2085. * @param int $executionID
  2086. * @access public
  2087. * @return object[]
  2088. */
  2089. public function getUnfinishTasks($executionID)
  2090. {
  2091. return $this->dao->select('*')->from(TABLE_TASK)
  2092. ->where('execution')->eq($executionID)
  2093. ->andWhere('deleted')->eq(0)
  2094. ->andWhere('status')->in('wait,doing,pause')
  2095. ->fetchAll('id', false);
  2096. }
  2097. /**
  2098. * 获取暂停的项目和执行下,指派给指定用户的任务信息。
  2099. * Get tasks assigned to the user for the suspended project and execution.
  2100. *
  2101. * @param string $account
  2102. * @access public
  2103. * @return object[]
  2104. */
  2105. public function getUserSuspendedTasks($account)
  2106. {
  2107. return $this->dao->select('t1.*')
  2108. ->from(TABLE_TASK)->alias('t1')
  2109. ->leftJoin(TABLE_EXECUTION)->alias('t2')->on("t1.execution = t2.id")
  2110. ->leftJoin(TABLE_PROJECT)->alias('t3')->on("t1.project = t3.id")
  2111. ->where('t1.assignedTo')->eq($account)
  2112. ->andWhere('(t2.status')->eq('suspended')
  2113. ->orWhere('t3.status')->eq('suspended')
  2114. ->markRight(1)
  2115. ->andWhere('t1.deleted')->eq(0)
  2116. ->beginIF($this->config->vision)->andWhere('t1.vision')->eq($this->config->vision)->fi()
  2117. ->andWhere('t2.deleted')->eq(0)
  2118. ->andWhere('t3.deleted')->eq(0)
  2119. ->fetchAll('id', false);
  2120. }
  2121. /**
  2122. * 通过任务类型获取用户的任务。
  2123. * Get user tasks by type.
  2124. *
  2125. * @param string $account
  2126. * @param string $type
  2127. * @param int $limit
  2128. * @param object $pager
  2129. * @param string $orderBy
  2130. * @param int $projectID
  2131. * @access public
  2132. * @return object[]
  2133. */
  2134. public function getUserTasks($account, $type = 'assignedTo', $limit = 0, $pager = null, $orderBy = 'id_desc', $projectID = 0)
  2135. {
  2136. if($type != 'myInvolved' && !$this->loadModel('common')->checkField(TABLE_TASK, $type)) return array();
  2137. $tasks = $this->taskTao->fetchUserTasksByType($account, $type, $orderBy, $projectID, $limit, $pager);
  2138. if(empty($tasks)) return $tasks;
  2139. $this->loadModel('common')->saveQueryCondition($this->dao->get(), 'task', false);
  2140. $taskTeam = $this->taskTao->getTeamMembersByIdList(array_keys($tasks));
  2141. foreach($taskTeam as $taskID => $team) $tasks[$taskID]->team = $team;
  2142. return $this->processTasks($tasks);
  2143. }
  2144. /**
  2145. * 获取指派给用户的任务 id:name 数组。
  2146. * Get the task id:name array assigned to the user.
  2147. *
  2148. * @param string $account
  2149. * @param string $status
  2150. * @param array $skipExecutionIDList
  2151. * @param array $appendTaskID
  2152. * @access public
  2153. * @return array
  2154. */
  2155. public function getUserTaskPairs($account, $status = 'all', $skipExecutionIDList = array(), $appendTaskID = array())
  2156. {
  2157. $tasks = $this->dao->select('t1.id, t1.name, t2.name as executionName')
  2158. ->from(TABLE_TASK)->alias('t1')
  2159. ->leftJoin(TABLE_PROJECT)->alias('t2')->on('t1.execution = t2.id')
  2160. ->where('t1.assignedTo')->eq($account)
  2161. ->andWhere('t1.deleted')->eq(0)
  2162. ->andWhere('t2.deleted')->eq(0)
  2163. ->beginIF($this->config->vision)->andWhere('t1.vision')->eq($this->config->vision)->fi()
  2164. ->beginIF($status != 'all')->andWhere('t1.status')->in($status)->fi()
  2165. ->beginIF(!empty($skipExecutionIDList))->andWhere('t1.execution')->notin($skipExecutionIDList)->fi()
  2166. ->beginIF(!empty($appendTaskID))->orWhere('t1.id')->in($appendTaskID)->fi()
  2167. ->fetchAll('id');
  2168. $taskPairs = array();
  2169. foreach($tasks as $task) $taskPairs[$task->id] = $task->executionName . ' / ' . $task->name;
  2170. return $taskPairs;
  2171. }
  2172. /**
  2173. * 判断任务操作是否可以点击。
  2174. * Judge an action is clickable or not.
  2175. *
  2176. * @param object $task
  2177. * @param string $action
  2178. * @access public
  2179. * @return bool
  2180. */
  2181. public static function isClickable($task, $action)
  2182. {
  2183. $action = strtolower($action);
  2184. /* 任务不可修改的话,则无法进行操作。 */
  2185. if(!common::canModify('task', $task)) return false;
  2186. /* IPD任务当关联的需求池需求撤销/移除移除时,任务需要点击确认。*/
  2187. /* IPD Task when the associated demand is retraec / unlink, the task needs to confirm . */
  2188. if($action == 'confirmdemandretract') return !empty($task->confirmeActionType) && $task->confirmeActionType == 'confirmedretract';
  2189. if($action == 'confirmdemandunlink') return !empty($task->confirmeActionType) && $task->confirmeActionType == 'confirmedunlink';
  2190. /* 如果是转任务,直接返回 true。 */
  2191. if($action == 'totask') return true;
  2192. /* 父任务只能编辑、创建子任务和指派。 Parent task only can edit task, create children and assign to somebody. */
  2193. if((!empty($task->isParent)) && !in_array($action, array('edit', 'batchcreate', 'cancel', 'assignto', 'pause', 'close', 'restart', 'confirmstorychange', 'delete'))) return false;
  2194. /* 子任务、多人任务、已取消已关闭的任务不能创建子任务。Multi task and child task and canceled/closed task cannot create children. */
  2195. if($action == 'batchcreate' && (!empty($task->team) || !empty($task->mode) || !empty($task->rawParent) || in_array($task->status, array('closed', 'cancel')))) return false;
  2196. /* 如果是IPD串行项目下的任务,则只有当前阶段开始以后才能开始/关闭任务。*/
  2197. /* If it is a task under an IPD serial project, the task can be started / closed only after the current phase begins. */
  2198. $executionInfo = zget($task, 'executionInfo', array());
  2199. if(!empty($executionInfo->canStartExecution['disabled']) && in_array($action, array('start', 'finish', 'recordworkhour'))) return false;
  2200. if(!empty($task->team))
  2201. {
  2202. global $app;
  2203. $myself = new self();
  2204. if($task->mode == 'linear')
  2205. {
  2206. if($action == 'assignto' && !in_array($task->status, array('done', 'cancel', 'closed'))) return false;
  2207. if($action == 'start' && in_array($task->status, array('wait', 'doing')))
  2208. {
  2209. if($task->assignedTo != $app->user->account) return false;
  2210. $currentTeam = $myself->getTeamByAccount($task->team, $app->user->account);
  2211. if($currentTeam && $currentTeam->status == 'wait') return true;
  2212. }
  2213. if($action == 'finish' && $task->assignedTo != $app->user->account) return false;
  2214. }
  2215. elseif($task->mode == 'multi')
  2216. {
  2217. $currentTeam = $myself->getTeamByAccount($task->team, $app->user->account);
  2218. if($action == 'start' && in_array($task->status, array('wait', 'doing')) && $currentTeam && $currentTeam->status == 'wait') return true;
  2219. if($action == 'finish' && (empty($currentTeam) || $currentTeam->status == 'done')) return false;
  2220. }
  2221. }
  2222. /* 根据状态判断是否可以点击。 Check clickable by status. */
  2223. if($action == 'batchcreate') return (empty($task->team) || empty($task->children)) && zget($executionInfo, 'type') != 'kanban';
  2224. if($action == 'start') return $task->status == 'wait';
  2225. if($action == 'restart') return $task->status == 'pause';
  2226. if($action == 'pause') return $task->status == 'doing';
  2227. if($action == 'assignto') return !in_array($task->status, array('closed', 'cancel'));
  2228. if($action == 'close') return $task->status == 'done' || $task->status == 'cancel' || ($task->isParent && $task->status != 'closed');
  2229. if($action == 'activate') return $task->status == 'done' || $task->status == 'closed' || $task->status == 'cancel';
  2230. if($action == 'finish') return $task->status != 'done' && $task->status != 'closed' && $task->status != 'cancel';
  2231. if($action == 'cancel') return $task->status != 'done' && $task->status != 'closed' && $task->status != 'cancel';
  2232. if($action == 'confirmstorychange') return !in_array($task->status, array('cancel', 'closed')) && !empty($task->needConfirm);
  2233. return true;
  2234. }
  2235. /**
  2236. * 检查执行是否有需求列表。
  2237. * Check whether execution has story list.
  2238. *
  2239. * @param object $execution
  2240. * @access public
  2241. * @return bool
  2242. */
  2243. public function isNoStoryExecution($execution)
  2244. {
  2245. if(empty($execution)) return false;
  2246. return $execution->lifetime == 'ops';
  2247. }
  2248. /**
  2249. * 维护多人任务的团队信息。
  2250. * Manage multi task team.
  2251. *
  2252. * @param string $mode
  2253. * @param object $task
  2254. * @param object $teamData
  2255. * @access public
  2256. * @return array|false
  2257. */
  2258. public function manageTaskTeam($mode, $task, $teamData)
  2259. {
  2260. /* Get old team member, and delete old task team. */
  2261. $oldTeamData = $this->dao->select('*')->from(TABLE_TASKTEAM)->where('task')->eq($task->id)->fetchAll('account', false);
  2262. $oldTeams = array_values($oldTeamData);
  2263. $oldMembers = array_column($oldTeams, 'account');
  2264. $this->dao->delete()->from(TABLE_TASKTEAM)->where('task')->eq($task->id)->exec();
  2265. /* Set effort left = 0 when linear task members be changed. */
  2266. $changeUsers = array();
  2267. $teamData->team = array_filter($teamData->team);
  2268. foreach($teamData->team as $index => $account)
  2269. {
  2270. if($mode == 'linear' && isset($oldTeams[$index]) && $oldTeams[$index]->account != $account) $changeUsers[] = $oldTeams[$index]->account;
  2271. }
  2272. /* Manage task team member. */
  2273. $teams = $this->manageTaskTeamMember($mode, $task, $teamData, $oldTeamData);
  2274. if(dao::isError()) return false;
  2275. /* Set effort left = 0 when multi task members be removed. */
  2276. if($mode == 'multi' && $oldMembers)
  2277. {
  2278. $removedMembers = array_diff($oldMembers, $teams);
  2279. $changeUsers = array_merge($changeUsers, $removedMembers);
  2280. }
  2281. if($changeUsers) $this->taskTao->resetEffortLeft($task->id, $changeUsers);
  2282. return $teams;
  2283. }
  2284. /**
  2285. * 维护多人任务团队成员信息。
  2286. * Manage multi-task team member information.
  2287. *
  2288. * @param string $mode
  2289. * @param object $task
  2290. * @param object $teamData
  2291. * @param array $oldTeamData
  2292. * @access public
  2293. * @return false|array
  2294. */
  2295. public function manageTaskTeamMember($mode, $task, $teamData, $oldTeamData = array())
  2296. {
  2297. /* If status of the task is doing, get the person who did not complete the task. */
  2298. $undoneUsers = array();
  2299. if($task->status == 'doing')
  2300. {
  2301. $efforts = $this->getTaskEfforts($task->id);
  2302. foreach($efforts as $effort)
  2303. {
  2304. if($effort->left != 0) $undoneUsers[$effort->account] = $effort->account;
  2305. else unset($undoneUsers[$effort->account]);
  2306. }
  2307. }
  2308. $minStatus = 'done';
  2309. $teamList = array_filter($teamData->team);
  2310. $teams = array();
  2311. $order = 1;
  2312. foreach($teamList as $index => $account)
  2313. {
  2314. /* Set member information. */
  2315. $member = new stdclass();
  2316. $member->task = $task->id;
  2317. $member->order = $order;
  2318. $member->account = $account;
  2319. $member->estimate = isset($teamData->teamEstimate) ? (float)zget($teamData->teamEstimate, $index, 0.00) : 0.00;
  2320. $member->consumed = isset($teamData->teamConsumed) ? (float)zget($teamData->teamConsumed, $index, 0.00) : 0.00;
  2321. $member->left = isset($teamData->teamLeft) ? (float)zget($teamData->teamLeft, $index, 0.00) : 0.00;
  2322. $member->status = isset($oldTeamData[$account]->status) ? $oldTeamData[$account]->status : 'wait';
  2323. if($task->status == 'wait' && $member->estimate > 0 && $member->left == 0) $member->left = $member->estimate;
  2324. if($task->status == 'done') $member->left = 0;
  2325. if($member->status == 'done' && !empty($member->left)) $member->status = 'doing';
  2326. /* Compute task status of member. */
  2327. if($member->left == 0 && $member->consumed > 0)
  2328. {
  2329. $member->status = 'done';
  2330. }
  2331. elseif($task->status == 'doing')
  2332. {
  2333. $teamSource = zget($teamData->teamSource, $index);
  2334. if(!empty($teamSource) && $teamSource != $account && isset($undoneUsers[$teamSource])) $member->transfer = $teamSource;
  2335. if(isset($undoneUsers[$account]) && ($mode == 'multi' || ($mode == 'linear' && $minStatus != 'wait'))) $member->status = 'doing';
  2336. }
  2337. /* Compute multi-task status, and in a linear task, there is only one doing status. */
  2338. if(($mode == 'linear' && $member->status == 'doing') || $member->status == 'wait') $minStatus = 'wait';
  2339. if($minStatus != 'wait' && $member->status == 'doing') $minStatus = 'doing';
  2340. /* Insert or update team member. */
  2341. $this->taskTao->setTeamMember($member, $mode, isset($teams[$account]));
  2342. if(dao::isError()) return false;
  2343. $teams[$account] = $account;
  2344. $order ++;
  2345. }
  2346. return $teams;
  2347. }
  2348. /**
  2349. * 将默认图表设置与当前图表设置合并。
  2350. * Merge the default chart settings and the settings of current chart.
  2351. *
  2352. * @param string $chartType
  2353. * @access public
  2354. * @return void
  2355. */
  2356. public function mergeChartOption($chartType)
  2357. {
  2358. $chartOption = $this->lang->task->report->$chartType;
  2359. $commonOption = $this->lang->task->report->options;
  2360. $chartOption->graph->caption = $this->lang->task->report->charts[$chartType];
  2361. if(!isset($chartOption->type)) $chartOption->type = $commonOption->type;
  2362. if(!isset($chartOption->width)) $chartOption->width = $commonOption->width;
  2363. if(!isset($chartOption->height)) $chartOption->height = $commonOption->height;
  2364. /* merge configuration */
  2365. foreach($commonOption->graph as $key => $value)
  2366. {
  2367. if(isset($chartOption->graph->$key)) continue;
  2368. $chartOption->graph->$key = $value;
  2369. }
  2370. }
  2371. /**
  2372. * 通过缺陷(Bug)导入的任务在完成时检查是否需要更新对应缺陷的状态。
  2373. * When task is finished, check whether need update status of bug.
  2374. *
  2375. * @param object $task
  2376. * @access public
  2377. * @return bool
  2378. */
  2379. public function needUpdateBugStatus($task)
  2380. {
  2381. /* If the task is not imported from a bug, return false. */
  2382. if($task->fromBug == 0) return false;
  2383. /* If the bug has been resolved, return false. */
  2384. $bug = $this->loadModel('bug')->getByID($task->fromBug);
  2385. if($bug->status == 'resolved') return false;
  2386. return true;
  2387. }
  2388. /**
  2389. * 暂停一个任务。
  2390. * Pause a task.
  2391. *
  2392. * @param object $task
  2393. * @param array $output
  2394. * @access public
  2395. * @return array|bool
  2396. */
  2397. public function pause($task, $output = array())
  2398. {
  2399. /* Get old task. */
  2400. $oldTask = $this->getById($task->id);
  2401. /* Update kanban status. */
  2402. $this->dao->update(TABLE_TASK)->data($task)->autoCheck()->checkFlow()->where('id')->eq($task->id)->exec();
  2403. /* If task has parent task, update status of the parent task by the child task. */
  2404. if($oldTask->isParent) $this->updateChildrenStatus($task->id);
  2405. /* If output is not empty, update kanban cell. */
  2406. $this->updateKanbanCell($task->id, $output, $oldTask->execution);
  2407. if(dao::isError()) return false;
  2408. return common::createChanges($oldTask, $task);
  2409. }
  2410. /**
  2411. * 处理报表统计数据。
  2412. * Process data for report.
  2413. *
  2414. * @param array $tasks
  2415. * @param array $children
  2416. * @param string $field execution|module|assignedTo|type|pri|deadline|estimate|left|consumed|finishedBy|closedReason|status|date
  2417. * @access public
  2418. * @return array
  2419. */
  2420. public function processData4Report($tasks, $children, $field)
  2421. {
  2422. if(!empty($children))
  2423. {
  2424. /* Remove the parent task from the tasks. */
  2425. foreach($children as $childTask) unset($tasks[$childTask->parent]);
  2426. }
  2427. $fields = array();
  2428. $dataList = array();
  2429. foreach($tasks as $task)
  2430. {
  2431. $key = strpos(',estimate,consumed,left,', ",{$field},") !== false ? helper::formatHours($task->$field) : (string)$task->$field;
  2432. if(!isset($fields[$key])) $fields[$key] = 0;
  2433. $fields[$key] ++;
  2434. }
  2435. /* Process table statistics data. */
  2436. if($field != 'date' && $field != 'deadline') asort($fields);
  2437. foreach($fields as $field => $count)
  2438. {
  2439. $data = new stdclass();
  2440. $data->name = $field;
  2441. $data->value = $count;
  2442. $dataList[$field] = $data;
  2443. }
  2444. return $dataList;
  2445. }
  2446. /**
  2447. * 处理导出的任务信息。
  2448. * Process export task information.
  2449. *
  2450. * @param array $tasks
  2451. * @access protected
  2452. * @return object[]
  2453. */
  2454. protected function processExportTasks($tasks)
  2455. {
  2456. /* Process multiple task info. */
  2457. $taskTeam = $this->dao->select('*')->from(TABLE_TASKTEAM)->where('task')->in(array_keys($tasks))->fetchGroup('task');
  2458. $users = $this->loadModel('user')->getPairs('noletter');
  2459. if(!empty($taskTeam))
  2460. {
  2461. foreach($taskTeam as $taskID => $team)
  2462. {
  2463. $tasks[$taskID]->team = $team;
  2464. $tasks[$taskID]->estimate = '';
  2465. $tasks[$taskID]->left = '';
  2466. $tasks[$taskID]->consumed = '';
  2467. foreach($team as $userInfo)
  2468. {
  2469. $tasks[$taskID]->estimate .= zget($users, $userInfo->account) . ':' . $userInfo->estimate . "\n";
  2470. $tasks[$taskID]->left .= zget($users, $userInfo->account) . ':' . $userInfo->left . "\n";
  2471. $tasks[$taskID]->consumed .= zget($users, $userInfo->account) . ':' . $userInfo->consumed . "\n";
  2472. }
  2473. }
  2474. }
  2475. /* Process parent-child task info. */
  2476. $parentIdList = array();
  2477. foreach($tasks as $task)
  2478. {
  2479. if($task->parent > 0) $parentIdList[] = $task->parent;
  2480. }
  2481. $parents = $this->dao->select('id, name')->from(TABLE_TASK)->where('id')->in($parentIdList)->fetchPairs();
  2482. foreach($tasks as $task)
  2483. {
  2484. $task->parentId = $task->parent;
  2485. $task->parent = zget($parents, $task->parent, '');
  2486. }
  2487. return $tasks;
  2488. }
  2489. /**
  2490. * 处理任务,计算进度、获取相关信息。
  2491. * Process a task, compute its progress and get its relates.
  2492. *
  2493. * @param object $task
  2494. * @param bool $convertParent
  2495. * @param array $workingDays
  2496. * @access public
  2497. * @return object
  2498. */
  2499. public function processTask($task, $convertParent = true, $workingDays = array())
  2500. {
  2501. $today = helper::today();
  2502. /* Delayed or not?. */
  2503. $this->loadModel('holiday');
  2504. $isNotCancel = !in_array($task->status, array('cancel', 'closed')) || ($task->status == 'closed' && !helper::isZeroDate($task->finishedDate) && $task->closedReason != 'cancel');
  2505. $isComputeDelay = $isNotCancel && !helper::isZeroDate($task->deadline);
  2506. $workingDays = empty($workingDays) && $isComputeDelay ? $this->holiday->getActualWorkingDays($task->deadline, $today) : $workingDays;
  2507. if($isComputeDelay) $task = $this->computeDelay($task, $task->deadline, $workingDays);
  2508. /* Story changed or not. */
  2509. $task->needConfirm = false;
  2510. if(!empty($task->storyStatus) && !in_array($task->status, array('cancel', 'closed')) && $task->storyStatus == 'active' && $task->latestStoryVersion > $task->storyVersion)
  2511. {
  2512. $task->needConfirm = true;
  2513. $task->rawStatus = $task->status;
  2514. $task->status = 'changed';
  2515. }
  2516. /* Set product type for task. */
  2517. if(!empty($task->product))
  2518. {
  2519. $product = $this->loadModel('product')->fetchByID($task->product);
  2520. if($product) $task->productType = $product->type;
  2521. }
  2522. /* Get related test cases. */
  2523. if($task->story) $task->cases = $this->dao->select('id, title')->from(TABLE_CASE)->where('story')->eq($task->story)->andWhere('storyVersion')->eq($task->storyVersion)->andWhere('deleted')->eq('0')->fetchPairs();
  2524. /* Set realname to task.*/
  2525. if($task->assignedTo != 'closed') $task->assignedToRealName = $this->dao->select('realname')->from(TABLE_USER)->where('account')->eq($task->assignedTo)->fetch('realname');
  2526. /* Set closed realname. */
  2527. if($task->assignedTo == 'closed') $task->assignedToRealName = 'Closed';
  2528. $task->progress = $this->taskTao->computeTaskProgress($task);
  2529. if($task->mode == 'multi')
  2530. {
  2531. $teamMembers = $this->dao->select('t1.realname')->from(TABLE_USER)->alias('t1')
  2532. ->leftJoin(TABLE_TASKTEAM)->alias('t2')
  2533. ->on('t1.account = t2.account')
  2534. ->where('t2.task')->eq($task->id)
  2535. ->fetchPairs();
  2536. $task->teamMembers = implode(',', array_keys($teamMembers));
  2537. }
  2538. if($this->config->edition != 'open')
  2539. {
  2540. $fieldList = $this->loadModel('workflowfield')->getList('task');
  2541. foreach($fieldList as $field)
  2542. {
  2543. if($field->type == 'date' || $field->type == 'datetime') $this->config->task->dateFields[] = $field->field;
  2544. }
  2545. }
  2546. foreach($task as $field => $value)
  2547. {
  2548. if(in_array($field, $this->config->task->dateFields) && helper::isZeroDate($value)) $task->$field = null;
  2549. }
  2550. $task->rawParent = $task->parent;
  2551. if($convertParent)
  2552. {
  2553. $task->parent = array();
  2554. foreach(explode(',', trim((string)$task->path, ',')) as $parentID)
  2555. {
  2556. if(!$parentID) continue;
  2557. if($parentID == $task->id) continue;
  2558. $task->parent[] = (int)$parentID;
  2559. }
  2560. }
  2561. return $task;
  2562. }
  2563. /**
  2564. * 批量处理任务,计算进度、获取相关信息。
  2565. * Batch process tasks, compute their progress and get their relates.
  2566. *
  2567. * @param array $tasks
  2568. * @access public
  2569. * @return array
  2570. */
  2571. public function processTasks($tasks)
  2572. {
  2573. $begin = $end = helper::today();
  2574. foreach($tasks as $task)
  2575. {
  2576. if(helper::isZeroDate($task->deadline)) continue;
  2577. $begin = $task->deadline < $begin ? $task->deadline : $begin;
  2578. if(!empty($task->children))
  2579. {
  2580. foreach($task->children as $child)
  2581. {
  2582. if(helper::isZeroDate($child->deadline)) continue;
  2583. $begin = $child->deadline < $begin ? $child->deadline : $begin;
  2584. }
  2585. }
  2586. }
  2587. $workingDays = $this->loadModel('holiday')->getActualWorkingDays($begin, $end);
  2588. $storyVersionPairs = $this->getTeamStoryVersion(array_keys($tasks));
  2589. foreach($tasks as &$task)
  2590. {
  2591. $task->storyVersion = zget($storyVersionPairs, $task->id, $task->storyVersion);
  2592. $task = $this->processTask($task, false, $workingDays);
  2593. if(!empty($task->children))
  2594. {
  2595. foreach($task->children as &$child) $child = $this->processTask($child, false, $workingDays);
  2596. }
  2597. }
  2598. return $tasks;
  2599. }
  2600. /**
  2601. * 记录工时。
  2602. * Record workhour and left of task.
  2603. *
  2604. * @param int $taskID
  2605. * @param array $workhour the form data from POST
  2606. * @access public
  2607. * @return array
  2608. */
  2609. public function recordWorkhour($taskID, $workhour)
  2610. {
  2611. $task = $this->fetchByID($taskID);
  2612. $task = $this->taskTao->formatDatetime($task);
  2613. $task->team = $this->dao->select('*')->from(TABLE_TASKTEAM)->where('task')->eq($taskID)->orderBy('order')->fetchAll('id', false);
  2614. /* Check if field is valid. */
  2615. $workhour = $this->checkWorkhour($task, $workhour);
  2616. if(!$workhour || dao::isError()) return array();
  2617. /* Add field to workhour. */
  2618. $workhour = $this->taskTao->buildWorkhour($taskID, $workhour);
  2619. if(empty($workhour)) return array();
  2620. $allChanges = array();
  2621. $oldStatus = $task->status;
  2622. $lastDate = $this->dao->select('date')->from(TABLE_EFFORT)->where('objectID')->eq($taskID)->andWhere('objectType')->eq('task')->andWhere('deleted')->eq('0')->orderBy('date_desc,id_desc')->limit(1)->fetch('date');
  2623. $currentTeam = !empty($task->team) ? $this->getTeamByAccount($task->team, $this->app->user->account, array()) : array();
  2624. foreach($workhour as $record)
  2625. {
  2626. if(isset($record->consumed)) $record->consumed = round((float)$record->consumed, 2);
  2627. if(isset($record->left)) $record->left = round((float)$record->left, 2);
  2628. $this->addTaskEffort($record);
  2629. $effortID = $this->dao->lastInsertID();
  2630. $isFinishTask = (empty($currentTeam) && in_array($task->status, $this->config->task->unfinishedStatus)) || (!empty($currentTeam) && $currentTeam->status != 'done');
  2631. /* Change the workhour and status of tasks through effort. */
  2632. list($newTask, $actionID) = $this->taskTao->buildTaskForEffort($record, $task, (string)$lastDate, $isFinishTask);
  2633. if($lastDate <= $record->date) $lastDate = $record->date;
  2634. /* Process multi-person task. Update consumed on team table. */
  2635. if(!empty($currentTeam))
  2636. {
  2637. $currentTeam->status = $record->left == 0 ? 'done' : 'doing';
  2638. $this->taskTao->updateTeamByEffort($effortID, $record, $currentTeam, $task, (string)$lastDate);
  2639. $newTask = $this->computeMultipleHours($task, $newTask);
  2640. }
  2641. $changes = common::createChanges($task, $newTask, 'task');
  2642. if($changes && $actionID) $this->loadModel('action')->logHistory($actionID, $changes);
  2643. if($changes) $allChanges = array_merge($allChanges, $changes);
  2644. $task = $newTask;
  2645. }
  2646. /* Update task and do other operations. */
  2647. if($allChanges)
  2648. {
  2649. if($this->config->edition != 'open')
  2650. {
  2651. $fieldList = $this->loadModel('workflowfield')->getList('task');
  2652. foreach($fieldList as $field)
  2653. {
  2654. if($field->type == 'date' || $field->type == 'datetime') $this->config->task->dateFields[] = $field->field;
  2655. }
  2656. }
  2657. foreach($this->config->task->dateFields as $field)
  2658. {
  2659. if(isset($task->$field) && helper::isZeroDate($task->$field)) $task->$field = null;
  2660. }
  2661. $this->dao->update(TABLE_TASK)->data($task, 'team')->where('id')->eq($taskID)->exec();
  2662. if($task->parent > 0) $this->updateParentStatus($task->id);
  2663. if($task->story) $this->loadModel('story')->setStage($task->story);
  2664. if($task->status != $oldStatus)
  2665. {
  2666. $this->loadModel('kanban')->updateLane($task->execution, 'task', $taskID);
  2667. if($this->config->edition != 'open' && $task->feedback) $this->loadModel('feedback')->updateStatus('task', $task->feedback, $task->status, $oldStatus, $task->id);
  2668. }
  2669. if($task->status == 'done' && !dao::isError()) $this->loadModel('score')->create('task', 'finish', $taskID);
  2670. }
  2671. $this->loadModel('program')->refreshProjectStats($task->project);
  2672. return $allChanges;
  2673. }
  2674. /**
  2675. * 检查录入日志的字段必填性及日志记录人要在多人任务的团队中。
  2676. * Check that the required fields of the effort must be filled in and the effort recorder must be in the multi-task team.
  2677. *
  2678. * @param object $task
  2679. * @param array $workhour
  2680. * @access public
  2681. * @return false|array
  2682. */
  2683. public function checkWorkhour($task, $workhour)
  2684. {
  2685. foreach($workhour as $id => $record)
  2686. {
  2687. if(!$record->work && !$record->consumed && !$record->left)
  2688. {
  2689. unset($workhour[$id]);
  2690. continue;
  2691. }
  2692. $date = $record->date;
  2693. $consumed = $record->consumed;
  2694. $left = $record->left;
  2695. /* Check the date of workhour. */
  2696. if(helper::isZeroDate($date)) dao::$errors["date[$id]"] = $this->lang->task->error->dateEmpty;
  2697. if($date > helper::today()) dao::$errors["date[$id]"] = 'ID #' . $id . ' ' . $this->lang->task->error->date;
  2698. /* Check consumed hours. */
  2699. if(!$consumed)
  2700. {
  2701. dao::$errors["consumed[$id]"] = $this->lang->task->error->consumedThisTime;
  2702. }
  2703. elseif(!is_numeric($consumed) && !empty($consumed))
  2704. {
  2705. dao::$errors["consumed[$id]"] = 'ID #' . $id . ' ' . $this->lang->task->error->totalNumber;
  2706. }
  2707. elseif(is_numeric($consumed) && $consumed <= 0)
  2708. {
  2709. dao::$errors["consumed[$id]"] = sprintf($this->lang->error->gt, 'ID #' . $id . ' ' . $this->lang->task->record, '0');
  2710. }
  2711. elseif(!$record->work && $this->config->edition != 'open')
  2712. {
  2713. dao::$errors["work[$id]"] = sprintf($this->lang->error->notempty, $this->lang->task->work);
  2714. }
  2715. /* Check left hours. */
  2716. if($left === '') dao::$errors["left[$id]"] = $this->lang->task->error->left;
  2717. if(!is_numeric($left)) dao::$errors["left[$id]"] = 'ID #' . $id . ' ' . $this->lang->task->error->leftNumber;
  2718. if(is_numeric($left) && $left < 0) dao::$errors["left[$id]"] = sprintf($this->lang->error->gt, 'ID #' . $id . ' ' . $this->lang->task->left, '0');
  2719. }
  2720. if(dao::isError()) return false;
  2721. $inTeam = $this->dao->select('id')->from(TABLE_TASKTEAM)->where('task')->eq($task->id)->andWhere('account')->eq($this->app->user->account)->fetch('id');
  2722. if($task->team && !$inTeam) return false;
  2723. return $workhour;
  2724. }
  2725. /**
  2726. * 设置任务的附件。
  2727. * Set attachments for tasks.
  2728. *
  2729. * @param mixed[] $taskIdList
  2730. * @access public
  2731. * @return bool
  2732. */
  2733. public function setTaskFiles($taskIdList)
  2734. {
  2735. if(empty($taskIdList)) return true;
  2736. $taskID = array_shift($taskIdList);
  2737. $taskFilePairs = $this->loadModel('file')->saveUpload('task', $taskID);
  2738. if(!empty($taskIdList))
  2739. {
  2740. $taskFiles = $taskFilePairs ? $this->dao->select('*')->from(TABLE_FILE)->where('id')->in(array_keys($taskFilePairs))->fetchAll('id', false) : array();
  2741. foreach($taskIdList as $objectID)
  2742. {
  2743. foreach($taskFiles as $taskFile)
  2744. {
  2745. $taskFile->objectID = $objectID;
  2746. unset($taskFile->id);
  2747. $this->dao->insert(TABLE_FILE)->data($taskFile)->exec();
  2748. }
  2749. }
  2750. }
  2751. return !dao::isError();
  2752. }
  2753. /**
  2754. * 获取报表的查询语句。
  2755. * Get report condition from session.
  2756. *
  2757. * @access public
  2758. * @return string
  2759. */
  2760. public function reportCondition()
  2761. {
  2762. if(!empty($_SESSION['taskQueryCondition']))
  2763. {
  2764. if(!$this->session->taskOnlyCondition) return 'id in (' . preg_replace('/SELECT .* FROM/', 'SELECT t1.id FROM', (string)$this->session->taskQueryCondition) . ')';
  2765. return $this->session->taskQueryCondition;
  2766. }
  2767. return '1=1';
  2768. }
  2769. /**
  2770. * 开始一个任务。
  2771. * Start a task.
  2772. *
  2773. * @param object $oldTask
  2774. * @param object $task
  2775. * @access public
  2776. * @return false|array
  2777. */
  2778. public function start($oldTask, $task)
  2779. {
  2780. /* Process data for multiple tasks. */
  2781. $account = $this->app->user->account;
  2782. $currentTeam = !empty($oldTask->team) ? $this->getTeamByAccount($oldTask->team, $account, array()) : '';
  2783. if($currentTeam)
  2784. {
  2785. /* Update task team. */
  2786. $team = new stdclass();
  2787. $team->consumed = round((float)$task->consumed, 2);
  2788. $team->left = round((float)$task->left, 2);
  2789. $team->status = empty($team->left) ? 'done' : 'doing';
  2790. $this->dao->update(TABLE_TASKTEAM)->data($team)->where('id')->eq($currentTeam->id)->exec();
  2791. /* Compute hours for multiple task. */
  2792. $task = $this->computeMultipleHours($oldTask, $task);
  2793. /* Set the assigner for the task. */
  2794. $now = helper::now();
  2795. if($team->status == 'done')
  2796. {
  2797. if(isset($oldTask->team[$currentTeam->id])) $oldTask->team[$currentTeam->id]->status = $team->status; // 通过开始直接完成的情况,status会变成done
  2798. $task->assignedTo = $this->getAssignedTo4Multi($oldTask->team, $oldTask, 'current');
  2799. $task->assignedDate = $now;
  2800. }
  2801. /* Set the task finisher. */
  2802. $finishedUsers = $this->taskTao->getFinishedUsers($oldTask->id, array_keys($oldTask->members));
  2803. if(count($finishedUsers) == count($oldTask->team))
  2804. {
  2805. $task->status = 'done';
  2806. $task->finishedBy = $account;
  2807. $task->finishedDate = $now;
  2808. }
  2809. }
  2810. if(isset($task->consumed)) $task->consumed = round((float)$task->consumed, 2);
  2811. if(isset($task->left)) $task->left = round((float)$task->left, 2);
  2812. $this->dao->update(TABLE_TASK)->data($task)->autoCheck()->checkFlow()->where('id')->eq($oldTask->id)->exec();
  2813. if(dao::isError()) return false;
  2814. if($task->consumed != $oldTask->consumed || $task->left != $oldTask->left) $this->loadModel('program')->refreshProjectStats($oldTask->project);
  2815. return common::createChanges($oldTask, $task);
  2816. }
  2817. /**
  2818. * 更新一个任务。
  2819. * Update a task.
  2820. *
  2821. * @param object $task
  2822. * @param object $teamData
  2823. * @access public
  2824. * @return array|string|false
  2825. */
  2826. public function update($task, $teamData = null)
  2827. {
  2828. $taskID = $task->id;
  2829. $oldTask = $this->getByID($taskID);
  2830. $task->project = $oldTask->project;
  2831. if($task->consumed < $oldTask->consumed)
  2832. {
  2833. dao::$errors[] = $this->lang->task->error->consumedSmall;
  2834. return false;
  2835. }
  2836. /* Compute hours and manage team for multi-task. */
  2837. if($teamData && !empty($teamData->team) && count(array_filter($teamData->team)) > 1)
  2838. {
  2839. $teams = $this->manageTaskTeam($task->mode, $task, $teamData);
  2840. if(!empty($teams)) $task = $this->computeMultipleHours($oldTask, $task, array(), false);
  2841. }
  2842. $requiredFields = $this->taskTao->getRequiredFields4Edit($task);
  2843. if(dao::isError()) return false;
  2844. $this->taskTao->doUpdate($task, $oldTask, $requiredFields);
  2845. if(dao::isError()) return false;
  2846. if($task->estimate != $oldTask->estimate || $task->consumed != $oldTask->consumed || $task->left != $oldTask->left) $this->loadModel('program')->refreshProjectStats($oldTask->project);
  2847. $this->afterUpdate($oldTask, $task);
  2848. unset($oldTask->parent, $task->parent);
  2849. /* Logging history when multi-task team members have changed. */
  2850. if(!empty($oldTask->team) && !empty($teamData->team)) list($oldTask, $task) = $this->taskTao->createChangesForTeam($oldTask, $task);
  2851. $this->loadModel('file')->processFileDiffsForObject('task', $oldTask, $task);
  2852. $task = $this->file->replaceImgURL($task, 'desc');
  2853. $changes = common::createChanges($oldTask, $task);
  2854. /* Record log. */
  2855. if($this->post->comment != '' || !empty($changes))
  2856. {
  2857. $extra = '';
  2858. array_map(function($field) use (&$extra) { if($field['field'] == 'status') $extra = 'statuschanged'; }, $changes);
  2859. $action = !empty($changes) ? 'Edited' : 'Commented';
  2860. $actionID = $this->loadModel('action')->create('task', $taskID, $action, $this->post->comment, $extra);
  2861. if(!empty($changes) && $actionID) $this->action->logHistory($actionID, $changes);
  2862. }
  2863. return $changes;
  2864. }
  2865. /**
  2866. * 编辑任务的日志。
  2867. * Update effort of task.
  2868. *
  2869. * @param object $effort
  2870. * @access public
  2871. * @return array|false
  2872. */
  2873. public function updateEffort($effort)
  2874. {
  2875. $oldEffort = $this->getEffortByID($effort->id);
  2876. if(!$this->taskTao->checkEffort($effort)) return false;
  2877. if(isset($effort->consumed)) $effort->consumed = round((float)$effort->consumed, 2);
  2878. if(isset($effort->left)) $effort->left = round((float)$effort->left, 2);
  2879. $this->dao->update(TABLE_EFFORT)->data($effort)
  2880. ->autoCheck()
  2881. ->where('id')->eq((int)$effort->id)
  2882. ->exec();
  2883. $this->loadModel('program')->refreshProjectStats($oldEffort->project);
  2884. $task = $this->getById($oldEffort->objectID);
  2885. $data = $this->buildTaskForUpdateEffort($task, $oldEffort, $effort);
  2886. /* Process multi-task by effort. */
  2887. if(!empty($task->team))
  2888. {
  2889. $currentTeam = $this->getTeamByAccount($task->team, $oldEffort->account, array('order' => $oldEffort->order));
  2890. if($currentTeam)
  2891. {
  2892. $newTeamInfo = new stdclass();
  2893. $newTeamInfo->consumed = $currentTeam->consumed + $effort->consumed - $oldEffort->consumed;
  2894. if($currentTeam->status != 'done') $newTeamInfo->left = $data->left;
  2895. if($currentTeam->status != 'done' && $newTeamInfo->consumed > 0 && $data->left == 0) $newTeamInfo->status = 'done';
  2896. $this->dao->update(TABLE_TASKTEAM)->data($newTeamInfo)->where('id')->eq($currentTeam->id)->exec();
  2897. $data = $this->computeMultipleHours($task, $data);
  2898. }
  2899. }
  2900. $this->dao->update(TABLE_TASK)->data($data)->where('id')->eq($task->id)->exec();
  2901. if(dao::isError()) return false;
  2902. if($task->consumed != $data->consumed || $task->left != $data->left) $this->loadModel('program')->refreshProjectStats($task->project);
  2903. if($task->parent > 0) $this->updateParentStatus($task->id);
  2904. if($task->story) $this->loadModel('story')->setStage($task->story);
  2905. $oldTask = new stdclass();
  2906. $oldTask->consumed = $task->consumed;
  2907. $oldTask->left = $task->left;
  2908. $oldTask->status = $task->status;
  2909. $oldTask->date = $oldEffort->date;
  2910. $newTask = new stdclass();
  2911. $newTask->consumed = $data->consumed;
  2912. $newTask->left = $data->left;
  2913. $newTask->status = $data->status;
  2914. $newTask->date = $effort->date;
  2915. return common::createChanges($oldTask, $newTask);
  2916. }
  2917. /**
  2918. * 更新串行任务工时日志的排序。
  2919. * Update effort order for linear task team.
  2920. *
  2921. * @param int $effortID
  2922. * @param int $order
  2923. * @access public
  2924. * @return bool
  2925. */
  2926. public function updateEffortOrder($effortID, $order)
  2927. {
  2928. $this->dao->update(TABLE_EFFORT)->set('`order`')->eq($order)->where('id')->eq($effortID)->exec();
  2929. return !dao::isError();
  2930. }
  2931. /**
  2932. * 更新预计开始和结束日期。
  2933. * Update estimate date by gantt.
  2934. *
  2935. * @param object $postData
  2936. * @access public
  2937. * @return bool
  2938. */
  2939. public function updateEsDateByGantt($postData)
  2940. {
  2941. $this->app->loadLang('project');
  2942. $postData->endDate = date('Y-m-d', strtotime('-1 day', strtotime($postData->endDate)));
  2943. $changeTable = $postData->type == 'task' ? TABLE_TASK : TABLE_PROJECT;
  2944. $actionType = $postData->type == 'task' ? 'task' : 'execution';
  2945. $oldObject = $this->dao->select('*')->from($changeTable)->where('id')->eq($postData->id)->fetch();
  2946. if($postData->type == 'task')
  2947. {
  2948. $project = $this->dao->select('id,taskDateLimit')->from(TABLE_PROJECT)->where('id')->eq($oldObject->project)->fetch();
  2949. if($project->taskDateLimit == 'limit')
  2950. {
  2951. $parentTasks = $this->dao->select('id,estStarted,deadline')->from(TABLE_TASK)->where('id')->in($oldObject->path)->andWhere('id')->ne($oldObject->id)->fetchAll('id');
  2952. foreach(array_reverse(array_filter(explode(',', $oldObject->path))) as $taskID)
  2953. {
  2954. if(!isset($parentTasks[$taskID])) continue;
  2955. $parentTask = $parentTasks[$taskID];
  2956. if(!helper::isZeroDate($parentTask->estStarted) && $parentTask->estStarted > $postData->startDate) dao::$errors[] = sprintf($this->lang->task->overParentEsStarted, $parentTask->estStarted);
  2957. if(!helper::isZeroDate($parentTask->deadline) && $parentTask->deadline < $postData->endDate) dao::$errors[] = sprintf($this->lang->task->overParentDeadline, $parentTask->deadline);
  2958. if(dao::isError()) return false;
  2959. }
  2960. }
  2961. $oldObject->estStarted = $postData->startDate;
  2962. $oldObject->deadline = $postData->endDate;
  2963. unset($oldObject->openedDate);
  2964. unset($oldObject->assignedDate);
  2965. unset($oldObject->realStarted);
  2966. unset($oldObject->finishedDate);
  2967. unset($oldObject->canceledDate);
  2968. unset($oldObject->closedDate);
  2969. unset($oldObject->lastEditedDate);
  2970. unset($oldObject->activatedDate);
  2971. $this->loadModel('task')->update($oldObject);
  2972. }
  2973. elseif($postData->type == 'plan')
  2974. {
  2975. $this->updateExecutionEsDateByGantt($postData);
  2976. }
  2977. if(dao::isError()) return false;
  2978. $newObject = $this->dao->select('*')->from($changeTable)->where('id')->eq($postData->id)->fetch();
  2979. $changes = common::createChanges($oldObject, $newObject);
  2980. $actionID = $this->loadModel('action')->create($actionType, $postData->id, 'edited');
  2981. if(!empty($changes) && $actionID) $this->loadModel('action')->logHistory($actionID, $changes);
  2982. return true;
  2983. }
  2984. /**
  2985. * 通过甘特图更新阶段的预计日期。
  2986. * Update Execution estimate date by gantt.
  2987. *
  2988. * @param object $postData
  2989. * @access public
  2990. * @return bool
  2991. */
  2992. public function updateExecutionEsDateByGantt($postData)
  2993. {
  2994. $stage = $this->dao->select('project,parent,frozen')->from(TABLE_EXECUTION)->where('id')->eq($postData->id)->fetch();
  2995. if(!empty($stage->frozen))
  2996. {
  2997. $this->app->loadLang('execution');
  2998. dao::$errors[] = sprintf($this->lang->execution->stageFrozenTip, $this->lang->execution->ganttDrag);
  2999. return false;
  3000. }
  3001. /* Get parent information. */
  3002. $parentID = $stage->project != $stage->parent ? $stage->parent : 0;
  3003. $parentData = $this->dao->select('begin,end')->from(TABLE_PROJECT)->where('id')->eq($parentID ? $parentID : $stage->project)->fetch();
  3004. $begin = helper::isZeroDate($parentData->begin) ? '' : $parentData->begin;
  3005. $end = helper::isZeroDate($parentData->end) ? '' : $parentData->end;
  3006. $this->app->loadLang('programplan');
  3007. $typeLang = $parentID ? $this->lang->programplan->parent : $this->lang->project->common;
  3008. if(helper::diffDate($begin, $postData->startDate) > 0) dao::$errors[] = sprintf($this->lang->task->overEsStartDate, $typeLang, $typeLang);
  3009. if(helper::diffDate($end, $postData->endDate) < 0) dao::$errors[] = sprintf($this->lang->task->overEsEndDate, $typeLang, $typeLang);
  3010. if(dao::isError()) return false;
  3011. $this->dao->update(TABLE_PROJECT)
  3012. ->set('begin')->eq($postData->startDate)
  3013. ->set('end')->eq($postData->endDate)
  3014. ->set('lastEditedBy')->eq($this->app->user->account)
  3015. ->where('id')->eq($postData->id)
  3016. ->exec();
  3017. return !dao::isError();
  3018. }
  3019. /**
  3020. * 更新看板单元格。
  3021. * Update kanban cell.
  3022. *
  3023. * @param int $taskID
  3024. * @param array $output
  3025. * @param int $executionID
  3026. * @access public
  3027. * @return void
  3028. */
  3029. public function updateKanbanCell($taskID, $output, $executionID)
  3030. {
  3031. if(!isset($output['toColID'])) $this->loadModel('kanban')->updateLane($executionID, 'task', $taskID);
  3032. if(isset($output['toColID'])) $this->loadModel('kanban')->moveCard($taskID, (int)$output['fromColID'], (int)$output['toColID'], (int)$output['fromLaneID'], (int)$output['toLaneID']);
  3033. }
  3034. /**
  3035. * 更新看板中的任务泳道数据。
  3036. * Update the task lane data in Kanban.
  3037. *
  3038. * @param int $executionID
  3039. * @param array $taskIdList
  3040. * @param int $laneID
  3041. * @param int $oldColumnID
  3042. * @access public
  3043. * @return bool
  3044. */
  3045. public function updateKanbanData($executionID, $taskIdList, $laneID, $oldColumnID)
  3046. {
  3047. if(!$executionID) return false;
  3048. $this->loadModel('kanban');
  3049. /* Get kanban id, lane id and column id. */
  3050. $laneID = !empty($laneID) ? $laneID : 0;
  3051. $columnID = $this->kanban->getColumnIDByLaneID($laneID, 'wait');
  3052. if(empty($columnID)) $columnID = $oldColumnID;
  3053. /* If both of lane id and column id are not empty, add task to the kanban cell. */
  3054. if($laneID && $columnID)
  3055. {
  3056. foreach($taskIdList as $taskID) $this->kanban->addKanbanCell($executionID, $laneID, $columnID, 'task', (string)$taskID);
  3057. }
  3058. /* If lane id or column id is empty, update the task type lane of the kanban. */
  3059. if(!$laneID || !$columnID) $this->kanban->updateLane($executionID, 'task');
  3060. return true;
  3061. }
  3062. /**
  3063. * 批量创建任务后的看板数据处理。
  3064. * Kanban data processing after batch create tasks.
  3065. *
  3066. * @param int $taskID
  3067. * @param int $executionID
  3068. * @param int $laneID
  3069. * @param int $columnID
  3070. * @access public
  3071. * @return bool
  3072. */
  3073. public function updateKanbanForBatchCreate($taskID, $executionID, $laneID, $columnID)
  3074. {
  3075. $this->loadModel('kanban');
  3076. if($this->config->vision == 'lite')
  3077. {
  3078. $this->kanban->addKanbanCell($executionID, $laneID, $columnID, 'task', (string)$taskID);
  3079. }
  3080. else
  3081. {
  3082. $columnID = $this->kanban->getColumnIDByLaneID($laneID, 'wait');
  3083. if(!empty($laneID) && !empty($columnID)) $this->kanban->addKanbanCell($executionID, $laneID, $columnID, 'task', (string)$taskID);
  3084. }
  3085. return !dao::isError();
  3086. }
  3087. /**
  3088. * 拖动甘特图更新任务的顺序。
  3089. * Update order by gantt.
  3090. *
  3091. * @param object $postData
  3092. * @access public
  3093. * @return bool
  3094. */
  3095. public function updateOrderByGantt($postData)
  3096. {
  3097. $order = 1;
  3098. foreach($postData->tasks as $task)
  3099. {
  3100. $idList = explode('-', $task);
  3101. $taskID = $idList[1];
  3102. $this->dao->update(TABLE_TASK)->set('`order`')->eq($order)->where('id')->eq($taskID)->exec();
  3103. $order ++;
  3104. }
  3105. return !dao::isError();
  3106. }
  3107. /**
  3108. * 编辑任务时更新父任务的信息。
  3109. * Update parent of a task.
  3110. *
  3111. * @param object $task
  3112. * @param bool $isParentChanged
  3113. * @access public
  3114. * @return void
  3115. */
  3116. public function updateParent($task, $isParentChanged = false)
  3117. {
  3118. $oldTask = $this->fetchByID($task->id);
  3119. $parentTask = empty($task->parent) ? null : $this->fetchByID((int)$task->parent);
  3120. $parentPath = $parentTask ? $parentTask->path : ',';
  3121. $path = $parentPath . $task->id . ',';
  3122. $this->dao->update(TABLE_TASK)->set('path')->eq($path)->where('id')->eq($task->id)->exec();
  3123. if($parentTask && !$parentTask->isParent) $this->dao->update(TABLE_TASK)->set('isParent')->eq(1)->where('id')->eq((int)$task->parent)->exec();
  3124. /* 更新所有子任务的path. */
  3125. $childIdList = $this->getAllChildId($task->id, false);
  3126. if($childIdList)
  3127. {
  3128. $children = $this->getByIdList($childIdList);
  3129. foreach($children as $child)
  3130. {
  3131. $newChildPath = str_replace($oldTask->path, $path, $child->path);
  3132. $this->dao->update(TABLE_TASK)->set('path')->eq($newChildPath)->where('id')->eq($child->id)->exec();
  3133. }
  3134. }
  3135. $this->updateParentStatus($task->id, $task->parent, !$isParentChanged);
  3136. if($task->parent) $this->computeBeginAndEnd($task->parent);
  3137. $this->taskTao->updateRelation((int)$task->id, (int)$task->parent);
  3138. if($isParentChanged && $task->parent)
  3139. {
  3140. $this->loadModel('action')->create('task', $task->id, 'linkParentTask', '', $task->parent, '', false);
  3141. $actionID = $this->action->create('task', $task->parent, 'linkChildTask', '', $task->id, '', false);
  3142. $newParentTask = $this->fetchByID($task->parent);
  3143. $changes = common::createChanges($parentTask, $newParentTask);
  3144. if(!empty($changes) && $actionID) $this->action->logHistory($actionID, $changes);
  3145. }
  3146. }
  3147. /**
  3148. * 更新父任务的状态.
  3149. * Update parent status by taskID of the child task.
  3150. *
  3151. * @param int $taskID
  3152. * @param int $parentID
  3153. * @param bool $createAction
  3154. * @access public
  3155. * @return void
  3156. */
  3157. public function updateParentStatus($taskID, $parentID = 0, $createAction = true)
  3158. {
  3159. /* Get child task info. */
  3160. $childTask = $this->dao->select('id,assignedTo,parent,path,realStarted')->from(TABLE_TASK)->where('id')->eq($taskID)->fetch();
  3161. if(empty($childTask)) return;
  3162. $taskIdList = $childTask->path;
  3163. if($parentID && $childTask->parent != $parentID) $taskIdList = $this->dao->select('id,assignedTo,parent,path')->from(TABLE_TASK)->where('id')->eq($parentID)->fetch('path');
  3164. $taskIdList = array_unique(array_filter(explode(',', $taskIdList)));
  3165. $parentTasks = $this->dao->select('*')->from(TABLE_TASK)->where('id')->in($taskIdList)->andWhere('id')->ne($taskID)->orderBy('path_desc')->fetchAll('id', false);
  3166. if(empty($parentTasks)) return;
  3167. $this->loadModel('story');
  3168. foreach($parentTasks as $parentID => $parentTask)
  3169. {
  3170. /* Compute parent task hours and status. */
  3171. $this->computeWorkingHours($parentID);
  3172. $status = $this->taskTao->getParentStatusById($parentID);
  3173. if(empty($status))
  3174. {
  3175. $this->dao->update(TABLE_TASK)->set('isParent')->eq(0)->where('id')->eq($parentID)->exec();
  3176. continue;
  3177. }
  3178. /* 只有子任务是进行中和已完成的时候才更新父任务的状态。*/
  3179. /* Update parent task status only child status is 'doing' or 'done'. */
  3180. if(!in_array($status, array('doing', 'done'))) continue;
  3181. if($parentTask->status == $status) continue;
  3182. /* Update task status. */
  3183. $this->taskTao->autoUpdateTaskByStatus($parentTask, $childTask, $status);
  3184. if(dao::isError() || !$createAction) return;
  3185. if($parentTask->story) $this->story->setStage($parentTask->story);
  3186. /* Create action record. */
  3187. $this->taskTao->createAutoUpdateTaskAction($parentTask, 'child');
  3188. if($this->config->edition != 'open' && $parentTask->feedback) $this->loadModel('feedback')->updateStatus('task', $parentTask->feedback, $status, $parentTask->status, $parentID);
  3189. }
  3190. }
  3191. /**
  3192. * 更新子任务的状态.
  3193. * Update children status by taskID.
  3194. *
  3195. * @param int $taskID
  3196. * @param string $oldParentStatus
  3197. * @access public
  3198. * @return void
  3199. */
  3200. public function updateChildrenStatus($taskID, $oldParentStatus = '')
  3201. {
  3202. /* Get child task info. */
  3203. $parentTask = $this->dao->select('id,status,isParent,parent,path')->from(TABLE_TASK)->where('id')->eq($taskID)->fetch();
  3204. if(empty($parentTask)) return;
  3205. $parentStatus = $parentTask->status;
  3206. if(!in_array($parentStatus, array('doing', 'pause', 'cancel', 'closed'))) return;
  3207. $childrenTasks = $this->dao->select('*')->from(TABLE_TASK)->where('path')->like("{$parentTask->path}%")->andWhere('id')->ne($taskID)->fetchAll('id', false);
  3208. if(empty($childrenTasks)) return;
  3209. $autoActions = array();
  3210. if($parentTask->status == 'doing' && $oldParentStatus == 'pause') $autoActions = $this->dao->select('objectID,extra')->from(TABLE_ACTION)->where('objectType')->eq("task")->andWhere('objectID')->in(array_keys($childrenTasks))->andWhere('action')->eq('paused')->orderBy('date')->fetchAll('objectID');
  3211. $this->loadModel('story');
  3212. foreach($childrenTasks as $childID => $childTask)
  3213. {
  3214. if($childTask->status == $parentStatus) continue;
  3215. if($parentStatus == 'pause' && $childTask->status != 'doing') continue;
  3216. if($parentStatus == 'cancel' && in_array($childTask->status, array('done', 'closed'))) continue;
  3217. if($parentStatus == 'doing' && $childTask->status != 'pause') continue;
  3218. if(isset($autoActions[$childID]) && $autoActions[$childID]->extra != 'autobyparent') continue;
  3219. $this->taskTao->autoUpdateTaskByStatus($childTask, null, $parentStatus);
  3220. if(dao::isError()) return;
  3221. if($childTask->story) $this->story->setStage($childTask->story);
  3222. /* Create action record. */
  3223. $this->taskTao->createAutoUpdateTaskAction($childTask, 'parent');
  3224. if($this->config->edition != 'open' && $childTask->feedback) $this->loadModel('feedback')->updateStatus('task', $childTask->feedback, $status, $childTask->status, $childID);
  3225. }
  3226. }
  3227. /**
  3228. * 更新团队信息。
  3229. * Update team.
  3230. *
  3231. * @param object $task
  3232. * @param object $postData
  3233. * @access public
  3234. * @return array|false
  3235. */
  3236. public function updateTeam($task, $postData)
  3237. {
  3238. $taskID = $task->id;
  3239. $oldTask = $this->getById($taskID);
  3240. /* Check team data. */
  3241. $team = array_filter($postData->team);
  3242. foreach($team as $i => $account)
  3243. {
  3244. if($postData->teamConsumed[$i] == 0 && $postData->teamLeft[$i] == 0)
  3245. {
  3246. dao::$errors[] = $this->lang->task->noticeTaskStart;
  3247. return false;
  3248. }
  3249. }
  3250. if(count($team) <= 1)
  3251. {
  3252. dao::$errors[] = $this->lang->task->error->teamMember;
  3253. return false;
  3254. }
  3255. /* Manage the team and calculate task work information. */
  3256. $teams = $this->manageTaskTeam($oldTask->mode, $task, $postData);
  3257. !empty($teams) ? $task = $this->computeMultipleHours($oldTask, $task) : $task->mode = '';
  3258. /* Update parent task status. */
  3259. if($oldTask->parent > 0) $this->updateParentStatus($taskID);
  3260. $this->dao->update(TABLE_TASK)
  3261. ->data($task)
  3262. ->autoCheck()
  3263. ->checkFlow()
  3264. ->where('id')->eq($taskID)
  3265. ->exec();
  3266. if(dao::isError()) return false;
  3267. if($task->estimate != $oldTask->estimate || $task->consumed != $oldTask->consumed || $task->left != $oldTask->left) $this->loadModel('program')->refreshProjectStats($oldTask->project);
  3268. return common::createChanges($oldTask, $task);
  3269. }
  3270. /**
  3271. * 通过任务ID获取任务团队信息。
  3272. * Get the task team information through the task ID.
  3273. *
  3274. * @param int $taskID
  3275. * @param string $orderBy
  3276. * @access protected
  3277. * @return object[]
  3278. */
  3279. public function getTeamByTask($taskID, $orderBy = 'order_asc')
  3280. {
  3281. return $this->dao->select('*')->from(TABLE_TASKTEAM)->where('task')->eq($taskID)->orderBy($orderBy)->fetchAll('id', false);
  3282. }
  3283. /**
  3284. * 更新任务关联的代码提交记录。
  3285. * Update the commit logs linked with the tasks.
  3286. *
  3287. * @param int $taskID
  3288. * @param int $repoID
  3289. * @param array $revisions
  3290. * @access public
  3291. * @return bool
  3292. */
  3293. public function updateLinkedCommits($taskID, $repoID, $revisions)
  3294. {
  3295. if(!$taskID || !$repoID || empty($revisions)) return true;
  3296. $task = $this->dao->select('project')->from(TABLE_TASK)->where('id')->eq($taskID)->fetch();
  3297. if(!$task) return true;
  3298. foreach($revisions as $revision)
  3299. {
  3300. $data = new stdclass();
  3301. $data->product = 0;
  3302. $data->project = $task->project;
  3303. $data->AType = 'task';
  3304. $data->AID = $taskID;
  3305. $data->BType = 'commit';
  3306. $data->BID = $revision;
  3307. $data->relation = 'completedin';
  3308. $data->extra = $repoID;
  3309. $this->dao->replace(TABLE_RELATION)->data($data)->autoCheck()->exec();
  3310. $data->AType = 'commit';
  3311. $data->AID = $revision;
  3312. $data->BType = 'task';
  3313. $data->BID = $taskID;
  3314. $data->relation = 'completedfrom';
  3315. $this->dao->replace(TABLE_RELATION)->data($data)->autoCheck()->exec();
  3316. }
  3317. return !dao::isError();
  3318. }
  3319. /**
  3320. * 获取Task关联的提交数据。
  3321. * Get the commit data for the associated bugs
  3322. * @param int $repoID
  3323. * @param array $revisions
  3324. * @access public
  3325. * @return bool
  3326. */
  3327. public function getLinkedCommits($repoID, $revisions)
  3328. {
  3329. return $this->dao->select('t1.revision,t3.id AS id,t3.name AS name')
  3330. ->from(TABLE_REPOHISTORY)->alias('t1')
  3331. ->leftJoin(TABLE_RELATION)->alias('t2')->on("t2.relation='completedin' AND t2.BType='commit' AND t2.BID=t1.id")
  3332. ->leftJoin(TABLE_TASK)->alias('t3')->on("t2.AType='task' AND t2.AID=t3.id")
  3333. ->where('t1.revision')->in($revisions)
  3334. ->andWhere('t1.repo')->eq($repoID)
  3335. ->andWhere('t3.id')->notNULL()
  3336. ->fetchGroup('revision', 'id');
  3337. }
  3338. /**
  3339. * 获取执行未关闭的任务
  3340. * Get unclosed tasks by execution
  3341. *
  3342. * @param mixed[]|int $execution
  3343. * @access public
  3344. * @return array|false
  3345. */
  3346. public function getUnclosedTasksByExecution($execution)
  3347. {
  3348. if(is_array($execution)) return $this->dao->select('id,execution')->from(TABLE_TASK)->where('execution')->in($execution)->andWhere('status')->ne('closed')->andWhere('deleted')->eq('0')->fetchGroup('execution');
  3349. return $this->dao->select('id,name')->from(TABLE_TASK)->where('execution')->eq($execution)->andWhere('status')->ne('closed')->andWhere('deleted')->eq('0')->fetchPairs();
  3350. }
  3351. /**
  3352. * 同步父任务的需求到子任务
  3353. * Sync parent story to children
  3354. *
  3355. * @param object $task
  3356. * @access public
  3357. * @return bool
  3358. */
  3359. public function syncStoryToChildren($task)
  3360. {
  3361. $nonStoryTasks = $this->dao->select('id,story')->from(TABLE_TASK)
  3362. ->where('path')->like("%,$task->id,%")
  3363. ->andWhere('deleted')->eq('0')
  3364. ->andWhere('story')->eq(0)
  3365. ->andWhere('id')->ne($task->id)
  3366. ->fetchPairs();
  3367. $taskStory = $this->loadModel('story')->fetchByID($task->story);
  3368. $this->loadModel('action');
  3369. foreach($nonStoryTasks as $id => $story)
  3370. {
  3371. $this->dao->update(TABLE_TASK)->set('story')->eq($task->story)->set('storyVersion')->eq($taskStory->version)->where('id')->eq($id)->exec();
  3372. $changes = array(array('field' => 'story', 'old' => $story, 'new' => $task->story, 'diff' => ''));
  3373. $actionID = $this->action->create('task', $id, 'syncStoryByParentTask');
  3374. $this->action->logHistory($actionID, $changes);
  3375. }
  3376. return !dao::isError();
  3377. }
  3378. /**
  3379. * 获取子任务
  3380. * Get child tasks
  3381. *
  3382. * @param array $taskIdList
  3383. * @access public
  3384. * @return array|false
  3385. */
  3386. public function getChildTasksByList($taskIdList)
  3387. {
  3388. $childTasks = $this->dao->select('id,parent,path,estStarted,deadline')->from(TABLE_TASK)->where('parent')->in($taskIdList)->andWhere('deleted')->eq('0')->fetchGroup('parent', 'id');
  3389. $nonStoryChildTasks = $this->dao->select('id,parent')->from(TABLE_TASK)->where('parent')->in($taskIdList)->andWhere('story')->eq('0')->andWhere('deleted')->eq('0')->fetchGroup('parent', 'id');
  3390. return array($childTasks, $nonStoryChildTasks);
  3391. }
  3392. /**
  3393. * 确认需求变更。
  3394. * Confirm story change.
  3395. *
  3396. * @param int $taskID
  3397. * @access public
  3398. * @return bool
  3399. */
  3400. public function confirmStoryChange($taskID)
  3401. {
  3402. $task = $this->getByID($taskID);
  3403. if($task->storyVersion == $task->latestStoryVersion) return true;
  3404. $this->dao->update(TABLE_TASK)->set('storyVersion')->eq($task->latestStoryVersion)->where('id')->eq($taskID)->exec();
  3405. $this->dao->update(TABLE_TASKTEAM)->set('storyVersion')->eq($task->latestStoryVersion)->where('task')->eq($taskID)->andWhere('account')->eq($this->app->user->account)->exec();
  3406. if(dao::isError()) return false;
  3407. $this->loadModel('action')->create('task', $taskID, 'confirmed', '', $task->latestStoryVersion);
  3408. return !dao::isError();
  3409. }
  3410. /**
  3411. * 获取多人任务当前登录用户的需求版本。
  3412. * Get team story version by current login user.
  3413. *
  3414. * @param int|array|string $taskIdList
  3415. * @access public
  3416. * @return array
  3417. */
  3418. public function getTeamStoryVersion($taskIdList)
  3419. {
  3420. return $this->dao->select('task,storyVersion')->from(TABLE_TASKTEAM)->where('task')->in($taskIdList)->andWhere('account')->eq($this->app->user->account)->fetchPairs();
  3421. }
  3422. /**
  3423. * 处理需求确认变更按钮。
  3424. * Process confirm story change button.
  3425. *
  3426. * @param object $task
  3427. * @access public
  3428. * @return object
  3429. */
  3430. public function processConfirmStoryChange($task)
  3431. {
  3432. if(empty($task->actions[0]['name']) || $task->actions[0]['name'] != 'confirmStoryChange') return $task;
  3433. if(empty($task->team) && (empty($task->assignedTo) || $task->assignedTo == $this->app->user->account)) return $task;
  3434. if(!empty($task->team))
  3435. {
  3436. $taskMembers = !empty($task->team) ? array_column($task->team, 'account') : array();
  3437. $disabled = !in_array($this->app->user->account, $taskMembers);
  3438. $task->actions[0]['disabled'] = $disabled;
  3439. if($disabled) $task->actions[0]['hint'] = $this->lang->task->disabledHint->memberConfirmStoryChange;
  3440. }
  3441. else
  3442. {
  3443. $task->actions[0]['disabled'] = true;
  3444. $task->actions[0]['hint'] = $this->lang->task->disabledHint->assignedConfirmStoryChange;
  3445. }
  3446. return $task;
  3447. }
  3448. /**
  3449. * 计算任务延期。
  3450. * Compute task delay.
  3451. *
  3452. * @param object $task
  3453. * @param string $deadline
  3454. * @param array $workingDays
  3455. * @access public
  3456. * @return object
  3457. */
  3458. public function computeDelay($task, $deadline = '', $workingDays = array())
  3459. {
  3460. if(helper::isZeroDate($deadline)) return $task;
  3461. $today = helper::today();
  3462. $endDate = helper::isZeroDate($task->finishedDate) ? $today : $task->finishedDate;
  3463. $betweenDays = $this->loadModel('holiday')->getDaysBetween($deadline, $endDate);
  3464. if($betweenDays)
  3465. {
  3466. $delayDays = array_intersect($betweenDays, $workingDays);
  3467. $delay = !empty($delayDays) ? count($delayDays) - 1: 0;
  3468. if($delay > 0) $task->delay = $delay;
  3469. }
  3470. return $task;
  3471. }
  3472. }