CheckPoint.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. <?php
  2. namespace CheckPoints;
  3. use CheckPointTypes\CheckPointType;
  4. class CheckPoint
  5. {
  6. public $id;
  7. public $typeId;
  8. public $priority;
  9. public $name;
  10. public $parentTaskId;
  11. public $parentCheckpointId;
  12. public $state;
  13. public $childrenCheckpoints;
  14. public $class;
  15. public $value;
  16. public $twxJson;
  17. public $checkPointType;
  18. public $parentCheckpoint;
  19. public $measurement_id;
  20. public $measurement_field;
  21. public $editable;
  22. function __construct(){}
  23. public static function CreateFromID($id, $fillchildren = true)
  24. {
  25. global $link;
  26. $query = mysqli_query($link, "select * from checkpoints where id=$id");
  27. if ($checkpoint_arr = mysqli_fetch_array($query))
  28. {
  29. return CheckPoint::CreateFromArray($checkpoint_arr, $fillchildren);
  30. }
  31. return null;
  32. }
  33. public static function CreateFromArray($checkpointArr, $fillchildren = true)
  34. {
  35. $checkpoint = new CheckPoint();
  36. $checkpoint->id = $checkpointArr['id'];
  37. $checkpoint->name = $checkpointArr['text'];
  38. $checkpoint->priority = $checkpointArr['priority'];
  39. $checkpoint->typeId = $checkpointArr['type'];
  40. $checkpoint->state = $checkpointArr['is_set'];
  41. $checkpoint->class = $checkpointArr['class'];
  42. $checkpoint->value = $checkpointArr['value'];
  43. $checkpoint->parentTaskId = $checkpointArr['parent_task_id'];
  44. $checkpoint->parentCheckpointId = intval($checkpointArr['parent_checkpoint_id']);
  45. $checkpoint->measurement_id = $checkpointArr['measurement_id'];
  46. $checkpoint->measurement_field = $checkpointArr['measurement_field'];
  47. $checkpoint->editable = $checkpointArr['editable'];
  48. $checkpoint->childrenCheckpoints = array();
  49. if ($checkpoint->parentCheckpointId == 0) {
  50. if ($fillchildren)
  51. $checkpoint->FillChildren();
  52. }
  53. else if ($checkpoint->parentCheckpoint == null)
  54. $checkpoint->parentCheckpoint = CheckPoint::CreateFromID($checkpoint->parentCheckpointId, false);
  55. $nameFromType = true;
  56. if ($checkpoint->name)
  57. $nameFromType = false;
  58. $checkpoint->SetNameAndClass($nameFromType);
  59. return $checkpoint;
  60. }
  61. public static function CreateFromType(CheckPointType $checkpointType, $parentCheckpointId = null, $parentTaskId = null)
  62. {
  63. //TODO брать все что можно из типа
  64. //TODO сначала парент, потом чайлдов
  65. global $link;
  66. $checkpoint = new CheckPoint();
  67. $checkpoint->checkPointType = $checkpointType;
  68. $checkpoint->name = $checkpointType->name;
  69. $checkpoint->priority = $checkpointType->priority;
  70. $checkpoint->typeId = $checkpointType->id;
  71. $checkpoint->state = '0';
  72. $checkpoint->class = $checkpointType->class;
  73. if ($parentCheckpointId != null)
  74. $checkpoint->parentCheckpointId = intval($parentCheckpointId);
  75. else
  76. $checkpoint->parentCheckpointId = 0;
  77. if ($parentTaskId != null)
  78. $checkpoint->parentTaskId = intval($parentTaskId);
  79. else
  80. $checkpoint->parentTaskId = 0;
  81. mysqli_query($link, "insert into checkpoints (type, parent_checkpoint_id, text, parent_task_id) values ($checkpoint->typeId,$checkpoint->parentCheckpointId, '$checkpoint->name', $checkpoint->parentTaskId)");
  82. $id = mysqli_insert_id($link);
  83. //$cptChild = $checkpointType->GetChildrenIDs();
  84. foreach ($checkpointType->childrenCheckpointTypes as $childype)
  85. {
  86. $cp = CheckPoint::CreateFromType($childype, $id, $checkpoint->parentTaskId);
  87. //$cp = CheckPoint::CreateFromType($childype, $id, $checkpoint->parentTaskId);
  88. }
  89. // $checkpoint->childrenCheckpoints = array();
  90. // $checkpoint->FillChildren();
  91. return $checkpoint;
  92. }
  93. // public static function GetallChildren
  94. public static function CreateFromScratch($class, $name, $parentTaskTypeId = null)
  95. {
  96. global $link;
  97. $checkpointType = new CheckPointType();
  98. $checkpointType->name = $name;
  99. $checkpointType->class = $class;
  100. $checkpointType->childrenCheckpointTypes = array();
  101. mysqli_query($link, "insert into checkpoint_types (class, name) values ('$class', '$name')");
  102. $checkpointType->id = mysqli_insert_id($link);
  103. if ($parentTaskTypeId != null) {
  104. mysqli_query($link, "insert into checkpoint_type_hierarchy (parent_cp_type, cp_type) values ($parentTaskTypeId, $checkpointType->id)");
  105. //mysqli_query($link, "update checkpoint_type_hierarchy set parent_cp_type=$parentTaskTypeId, cp_type=$checkpointType->id");
  106. }
  107. return $checkpointType;
  108. }
  109. public static function GetClasses()
  110. {
  111. return array(0, "DropList", "CheckBox", "MultiSelect", "SerialNumber");
  112. }
  113. public function AddRemark($echo = false, $task = null, $tasktype = null, $cpnotes = "")
  114. {
  115. if ($echo)
  116. echo "<br><br>".$this->id.": AddRemark";
  117. if ($task == null) {
  118. $task = \Task::Find($this->parentTaskId);
  119. }
  120. if ($task != null)
  121. {
  122. if ($tasktype == null)
  123. $tasktype = \Tasktype::Find($task->type);
  124. //TODO заменить настройкой исключения записи замечаний
  125. if (intval($task->type) == 741 || intval($task->type) == 743)
  126. {
  127. if ($echo)
  128. echo $this->id.": task 741";
  129. return;
  130. }
  131. $cp = $this;
  132. if ($cp != null)
  133. {
  134. if ($echo)
  135. echo $this->id." cp != null";
  136. $numdefects = 0;
  137. if ($echo)
  138. echo $this->id." state $this->state value $this->value";
  139. if ('1' == $this->state || $this->value)
  140. {
  141. $value = "";
  142. if ($tasktype != null)
  143. {
  144. $value .= "[".$tasktype->name;
  145. $value .= "] ";
  146. }
  147. if ($echo)
  148. echo $this->id." value $this->value";
  149. $children = $this->childrenCheckpoints;
  150. if ($echo)
  151. echo $this->id." children size ".(sizeof($children));
  152. $childrenvalue = "";
  153. if (sizeof($children) > 0)
  154. {
  155. foreach ($children as $child) {
  156. $cpt = $child->getCheckPointType();
  157. if ($cpt->getWriteToTWX() && $child->state == '1') {
  158. $childrenvalue .= " " . $child->name;
  159. $numdefects++;
  160. }
  161. }
  162. }
  163. /**
  164. * @var $child CheckPoint
  165. *
  166. */
  167. if ($numdefects > 0)
  168. {
  169. $value .= $cp->name . ": ";
  170. if ($this->value)
  171. $value .= $this->value." ";
  172. $value .= $childrenvalue;
  173. if ($cpnotes != "")
  174. {
  175. $value .= "$cpnotes";
  176. }
  177. // \Doctrine\Common\Util\Debug::dump($task);
  178. if ($echo) {
  179. echo "<br><br>value: ";
  180. var_dump($value);
  181. }
  182. $result = $task->AddRemark($value);
  183. // if ($echo) {
  184. // echo "<br><br>result: ";
  185. // var_dump($result);
  186. // }
  187. $cp->SetTwx($result);
  188. return $result;
  189. // return $value; //debug
  190. }
  191. }
  192. }
  193. }
  194. else {
  195. if ($echo)
  196. echo $this->id . ": Task NULL";
  197. }
  198. return null;
  199. }
  200. public function SetTwx($value)
  201. {
  202. global $link;
  203. mysqli_query($link, "update checkpoints set twx = '$value' where id=".$this->id);
  204. }
  205. public function WriteValueToMeasurement($measurement, $value, $uuid)
  206. {
  207. WriteLog("WriteValueToMeasurement $this->id $this->typeId",$value);
  208. global $link;
  209. $fieldNameToWrite = '';
  210. $valueToWrite = 0;
  211. if ($this->typeId == 0)
  212. return;
  213. if ($this->measurement_id && $this->measurement_field)
  214. {
  215. $fieldNameToWrite = $this->measurement_field;
  216. if ($fieldNameToWrite == 'measurement_comment') {
  217. $time = time();
  218. $valueToWrite = "'{\"datetime\":$time,\"author\":\"$uuid\",\"text\":\"$value\"}'";
  219. }
  220. else {
  221. if (!$value && !$this->state)
  222. return;
  223. else {
  224. if ($this->parentCheckpoint) {
  225. if ($this->parentCheckpoint->typeId == 820 && $this->typeId==293) {
  226. //отметка о выполнении
  227. if ($this->state)
  228. $valueToWrite = $this->state;
  229. }
  230. else
  231. return;
  232. }
  233. else
  234. $valueToWrite = $value;
  235. }
  236. }
  237. }
  238. else
  239. return;
  240. // if ($this->typeId == 819)
  241. // {
  242. // //measurement value
  243. // $fieldNameToWrite = 'measurement_value';
  244. // $valueToWrite = $value;
  245. // }
  246. // else if ($this->typeId == 814)
  247. // {
  248. // $fieldNameToWrite = 'measurement_comment';
  249. // $time = time();
  250. // $valueToWrite = "'{\"datetime\":$time,\"author\":\"$uuid\",\"text\":\"$value\"}'";
  251. // }
  252. // else if ($this->parentCheckpoint)
  253. // {
  254. // if ($this->parentCheckpoint->typeId == 820)
  255. // {
  256. // //соответствие норме
  257. // $fieldNameToWrite = 'value_compliance';
  258. // $valueToWrite = $this->state;
  259. // }
  260. // else
  261. // return;
  262. // }
  263. // else
  264. // return;
  265. $str = "update asusg_measurements set $fieldNameToWrite = $valueToWrite where id=$this->measurement_id ";
  266. WriteLog("checkpoint $this->typeId",$str);
  267. mysqli_query($link, $str);
  268. }
  269. public function SetValue($value)
  270. {
  271. global $link;
  272. $state = false;
  273. if ($value == "False")
  274. {$value = 0; $state = true; $this->state='0';}
  275. elseif ($value == "True")
  276. {$value = 1; $state = true; $this->state='1';}
  277. else
  278. $this->value = $value;
  279. if ($state === false)
  280. $str = "update checkpoints set value = '$value' where id=".$this->id;
  281. else
  282. $str = "update checkpoints set is_set = $value where id=".$this->id;
  283. WriteLog("checkpoint $this->id type $this->typeId",$str);
  284. mysqli_query($link, $str);
  285. }
  286. function GetClassID()
  287. {
  288. switch ($this->class)
  289. {
  290. case "DropList": return 1;
  291. case "CheckBox": return 2;
  292. case "MultiSelect": return 3;
  293. case "SerialNumber": return 4;
  294. }
  295. }
  296. function GetClassName($class)
  297. {
  298. switch ($class)
  299. {
  300. case "DropList": return 'Один из нескольких вариантов';
  301. case "CheckBox": return 'Варианты';
  302. case "MultiSelect": return 'Выбрать несколько вариантов';
  303. case "SerialNumber": return "Ввод значения";
  304. }
  305. }
  306. function FillChildren()
  307. {
  308. global $link;
  309. $query = mysqli_query($link, "select * from checkpoints where parent_checkpoint_id=".$this->id." ORDER BY priority");
  310. while ($checkarr = mysqli_fetch_array($query))
  311. {
  312. $child = CheckPoint::CreateFromArray($checkarr);
  313. if ($child->parentCheckpoint == null)
  314. $child->parentCheckpoint = $this;
  315. array_push($this->childrenCheckpoints, $child);
  316. }
  317. }
  318. function SetNameAndClass($nameFromType = true)
  319. {
  320. global $link;
  321. $query = mysqli_query($link, "select name, class from checkpoint_types where id=".$this->typeId);
  322. if ($res = mysqli_fetch_row($query)) {
  323. if ($nameFromType)
  324. $this->name = $res[0];
  325. $this->class = $res[1];
  326. }
  327. }
  328. function PackChildrenToBinary()
  329. {
  330. $buf = "";
  331. foreach ($this->childrenCheckpoints as $checkpoint)
  332. {
  333. $buf .= $checkpoint->PackToBinary();
  334. }
  335. return $buf;
  336. }
  337. function PackToBinary()
  338. {
  339. $lenname = strlen($this->name);
  340. $len = 22 + $lenname+strlen($this->value);
  341. $classid = $this->GetClassID();
  342. $buf = pack("vlCCCvllCv*",
  343. $len,
  344. $this->id,
  345. $this->priority,
  346. $this->state,
  347. $this->editable,
  348. $this->typeId,
  349. $this->parentTaskId,
  350. $this->parentCheckpointId,
  351. $classid,
  352. $lenname
  353. ); //v-ushort, c- byte, C- ubyte, s - short, L - int, l - uint
  354. $buf .= $this->name;
  355. $buf .= $this->value;
  356. return $buf;
  357. }
  358. public static function Find($parentCheckpointId = "all", $parent_task_id = null, $class = null)
  359. {
  360. //$parentCheckpointId > 0: find children
  361. //$parentCheckpointId == 0: find parents
  362. //$parentCheckpointId == -1: find all
  363. global $link;
  364. $addByTask = '';
  365. $addParentOnly = '';
  366. if ($parent_task_id) {
  367. $addByTask = " where parent_task_id=" . $parent_task_id;
  368. }
  369. $parentCheckpointId = intval($parentCheckpointId);
  370. if ($parentCheckpointId > -1) {
  371. $parentFind = "parent_checkpoint_id=$parentCheckpointId";
  372. if ($parentCheckpointId === 0)
  373. $parentFind .= " or parent_checkpoint_id is null";
  374. if ($parent_task_id) {
  375. $add = "and (";
  376. $parentFind .= ")";
  377. }
  378. else
  379. $add = "where";
  380. $addParentOnly = " $add $parentFind";
  381. }
  382. $str = "select * from checkpoints $addByTask $addParentOnly";
  383. //echo $str;
  384. $checkpoints = array(); //массив значений чекпойнтов
  385. $query = mysqli_query($link, $str);
  386. while ($checkpoint_arr = mysqli_fetch_array($query))
  387. {
  388. $check = CheckPoint::CreateFromArray($checkpoint_arr);
  389. array_push($checkpoints, $check);
  390. }
  391. return $checkpoints;
  392. }
  393. public static function FindAllTypes()
  394. {
  395. global $link;
  396. $checkpointTypes = array(); //массив значений чекпойнтов
  397. $query = mysqli_query($link, "select * from checkpoint_types");
  398. while ($checkpoint_arr = mysqli_fetch_array($query))
  399. {
  400. array_push($checkpointTypes, $checkpoint_arr);
  401. }
  402. return $checkpointTypes;
  403. }
  404. public static function FindAllByTask($parent_task_id)
  405. {
  406. global $link;
  407. $checkpoints = array(); //массив значений чекпойнтов
  408. $query = mysqli_query($link, "select * from checkpoints where parent_task_id=".$parent_task_id);
  409. while ($checkpoint_arr = mysqli_fetch_array($query))
  410. {
  411. $check = CheckPoint::CreateFromArray($checkpoint_arr);
  412. array_push($checkpoints, $check);
  413. }
  414. return $checkpoints;
  415. }
  416. public static function GetCheckPointClasses()
  417. {
  418. global $link;
  419. $class_arr = array();
  420. $query = mysqli_query($link, "SHOW COLUMNS FROM checkpoint_types WHERE Field = 'class'" );
  421. if ($class = mysqli_fetch_array($query))
  422. {
  423. $enums = $class[1];
  424. //array_push($class, $class_arr);
  425. $mch = preg_match ( "/^enum\(/" , $enums);
  426. $patterns = array();
  427. $patterns[0] = '/^enum\(/';
  428. $patterns[1] = '/\)/';
  429. $patterns[2] = '/\'/';
  430. $replacements = array();
  431. $replacements[0] = '';
  432. $replacements[1] = '';
  433. $replacements[2] = '';
  434. $str = preg_replace($patterns, $replacements, $enums);
  435. $class_arr = explode(',',$str);
  436. //echo "str $str";
  437. // var_dump ($mch);
  438. // print_r($mch);
  439. // print_r($mch);
  440. //echo "Class: $class[1]<br>";
  441. }
  442. return $class_arr;
  443. }
  444. public static function GetAllParentCheckPoints()
  445. {
  446. return self::Find( 0);
  447. }
  448. public static function GetOrphanChildren()
  449. {
  450. return self::Find( 0);
  451. }
  452. public static function EchoAllCheckPoints()
  453. {
  454. $parents = self::GetAllParentCheckPoints();
  455. foreach ($parents as $parent)
  456. {
  457. $parent->Echo();
  458. }
  459. }
  460. public static function DeleteByTask($taskid)
  461. {
  462. //echo "checkpoint DeleteByTask!!<br>";
  463. $cps = self::Find(0, $taskid);
  464. //echo "task checkpoints len ".sizeof($cps)."<br>";
  465. foreach ($cps as $cp)
  466. {
  467. //echo "CP delete id: ".$cp->id."<br>";
  468. $cp->DeleteFull();
  469. }
  470. }
  471. public function DeleteFull()
  472. {
  473. //echo "CP delete full: ".$this->id."<br>";
  474. $this->DeleteChildren();
  475. $this->Delete();
  476. }
  477. public function Delete()
  478. {
  479. //echo "CP delete: ".$this->id."<br>";
  480. global $link;
  481. mysqli_query($link, "delete from checkpoints where id=".$this->id);
  482. }
  483. public function DeleteChildren()
  484. {
  485. foreach ($this->childrenCheckpoints as $cp)
  486. {
  487. $cp->Delete();
  488. }
  489. }
  490. public function Echo()
  491. {
  492. $children = $this->childrenCheckpoints;
  493. //echo "checkpoint $cpObj->id type $cpObj->typeId";
  494. echo "<br><b>".$this->name." [$this->class]"."</b><br>";
  495. if (sizeof($children) > 0)
  496. {
  497. echo "<select>";
  498. foreach ($children as $child) {
  499. echo "<option>";
  500. $child->Echo();
  501. echo "</option>";
  502. }
  503. echo "</select><br>";
  504. }
  505. }
  506. public static function EchoClasses($classes, $myclass=null, $excl=null)
  507. {
  508. echo "<div style='float: left'>
  509. <select id='classes' data-contentsFlag='noHideContents' style='width: 400px' class='form-input content__main__form__series' name='cptype'>";
  510. $str = '<option value="0"><Выберите класс из списка></option>';
  511. foreach ($classes as $class)
  512. {
  513. // echo "found $cname $class $excl ";
  514. if ($excl && $class == $excl)
  515. continue;
  516. if ($myclass && $class != $myclass)
  517. continue;
  518. $cname = CheckPoint::GetClassName($class);
  519. $str .= "<option value='$class'>$cname</option>";
  520. }
  521. echo "$str</select></div><br>";
  522. }
  523. public static function EchoTypes($tasktype = null)
  524. {
  525. if ($tasktype != null)
  526. $types = CheckPointType::GetCheckPointTypesByTask($tasktype);
  527. else
  528. $types = self::FindAllTypes();
  529. if (sizeof($types) > 0) {
  530. //echo "cp type";
  531. //echo "<br>";
  532. //print_r($types);
  533. echo "<div class='rounded'><h5>Добавленные чекпойнты:</h5>";
  534. echo "<ul>";
  535. foreach ($types as $type) {
  536. $cpt = CheckPointType::CreateFromID($type['cp_type_id']);
  537. echo "<li> $cpt->name [" . $type['cp_type_id'] . "]</li>";
  538. // $templates = CheckPointType::CheckPointTemplate($type['cp_type_id']);
  539. // foreach ($templates as $template)
  540. // {
  541. // echo "template<br>";
  542. // echo "parent cp type ".$template['parent_cp_type']."<br>";
  543. // echo "cp type ".$template['cp_type']."<br>";
  544. // }
  545. }
  546. echo "</ul></div>";
  547. }
  548. }
  549. public function getCheckPointType()
  550. {
  551. if ($this->checkPointType != null)
  552. return $this->checkPointType;
  553. else
  554. {
  555. $cpt = CheckPointType::CreateFromID($this->typeId);
  556. $this->checkPointType = $cpt;
  557. return $cpt;
  558. }
  559. }
  560. public static function CreateCheckPointType()
  561. {
  562. //выбор типа
  563. //чекбокс = просто создание типа
  564. //листы = создание типа + заполнение его чекбоксами (создание темплейта)
  565. //добавление чекпойнта в таск через редактирование типа тасков
  566. }
  567. }