ProcesTestyHelper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * Proces Testy helper.
  4. *
  5. * @use table `CRM_TESTY`
  6. */
  7. class ProcesTestyHelper {
  8. public static function get_kandydaci($params = array()) {
  9. $ret = array();
  10. $params['order by'] = V::get('order by', '', $params);
  11. $allowed_order_by = array();
  12. $allowed_order_by['ID']= 'DESC';
  13. $allowed_order_by['ADM_NAME']= 'ASC';
  14. if (!array_key_exists($params['order by'], $allowed_order_by)) {
  15. $params['order by'] = "";
  16. }
  17. $sql_order_by = "";
  18. if ($params['order by']) {
  19. $sql_order_by = "order by " . $params['order by'] . " " . $allowed_order_by[$params['order by']];
  20. }
  21. return array_map(function ($item) { return (object)$item; }, DB::getPDO()->fetchAll("
  22. select `ID`,`ADM_NAME`,`ADM_PHONE`,`ADM_OTHER_INFO`
  23. from `ADMIN_USERS`
  24. where `ADM_ADMIN_LEVEL`>=3
  25. and `A_STATUS`!='OFF_HARD' and `A_STATUS`!='DELETED'
  26. and `EMPLOYEE_TYPE` like '%Kandydat%'
  27. {$sql_order_by}
  28. limit 1000
  29. "));
  30. }
  31. public static function get_testy_by_kandydat($kandydat_id) {
  32. $params = array();
  33. $params['id_tester'] = $kandydat_id;
  34. $params['order_by'] = 'ID';
  35. $params['order_dir'] = 'desc';
  36. return self::get_testy($params);
  37. }
  38. public static function _get_testy_sql_where($params = array()) {
  39. $sql_where = "1=1";
  40. if (($id_tester = V::get('id_tester', 0, $params, 'int')) > 0) {
  41. $sql_where .= " and t.`ID_TESTER`='{$id_tester}'";
  42. }
  43. if ('TAK' == V::get('SHOW_IN_CERT', '', $params)) {
  44. $sql_where .= " and t.`SHOW_IN_CERT`='TAK'";
  45. }
  46. return $sql_where;
  47. }
  48. public static function get_testy_total($params = array()) {
  49. $sql_where = self::_get_testy_sql_where($params);
  50. return DB::getPDO()->fetchValue("
  51. select
  52. count(1) as cnt
  53. from `CRM_TESTY` as t
  54. where
  55. {$sql_where}
  56. ");
  57. }
  58. public static function get_testy($params = array()) {
  59. $ret = array();
  60. $sql_select = array();
  61. $sql_select[] = "t.`ID`";
  62. $sql_select[] = "t.`ID_TEST_TO_FIX`";
  63. $sql_select[] = "t.`A_STATUS`";
  64. $sql_select[] = "t.`A_RECORD_CREATE_DATE`";
  65. $sql_select[] = "t.`ID_TESTER`";
  66. $sql_select[] = "t.`ID_PROCES_INIT`";
  67. $sql_select[] = "t.`ID_STANOWISKO`";
  68. $sql_select[] = "t.`TEST_INIT`";
  69. $sql_select[] = "t.`TEST_START`";
  70. $sql_select[] = "t.`TEST_END`";
  71. $sql_select[] = "t.`UWAGI`";
  72. $sql_select[] = "t.`OCENA`";
  73. $sql_select[] = "t.`DISCOUNT`";
  74. $sql_select[] = "t.`ID_ORGANIZER`";
  75. $sql_select[] = "t.`TEST_PLACE`";
  76. //$sql_select[] = "t.`TEST_TIME`";
  77. $sql_select[] = "t.`COST`";
  78. $sql_select[] = "t.`TEST_TYPE`";
  79. $sql_select[] = "z.`DESC` as Stanowisko";
  80. $sql_select[] = "concat(u.`ADM_NAME`, ' (', u.`ADM_PHONE`, ', ', u.`ADM_OTHER_INFO`, ')') as Kandydat";
  81. $sql_select[] = "IF( u.`ID` is null, t.`ID_TESTER`, u.`ADM_NAME`) as Kandydat_Name";
  82. $sql_select[] = "t.`SHOW_IN_CERT`";
  83. $sql_join = array();
  84. $sql_order_by = "";
  85. if (($p_order_by = V::get('order_by', '', $params)) != '') {
  86. $sql_order_by = "order by {$p_order_by}";
  87. if (in_array(strtoupper($p_order_dir = V::get('order_dir', '', $params)), array('ASC','DESC'))) {
  88. $sql_order_by .= " {$p_order_dir}";
  89. }
  90. }
  91. $sql_limit = V::get('limit', 10, $params, 'int');
  92. if (($p_offset = V::get('offset', 0, $params, 'int')) > 0) {
  93. $sql_limit .= " offset {$p_offset}";
  94. }
  95. $sql_where = self::_get_testy_sql_where($params);
  96. if ((true == V::get('proces_name', '', $params))) {
  97. $sql_join[] = "left join `CRM_PROCES` as p on(p.`ID`=t.`ID_PROCES_INIT`)";
  98. $sql_select[] = "p.`DESC` as Proces_Name";
  99. }
  100. if ((true == V::get('organizer_name', '', $params))) {
  101. $sql_join[] = "left join `ADMIN_USERS` as u_org on(u_org.`ID`=t.`ID_ORGANIZER`)";
  102. $sql_select[] = "u_org.`ADM_NAME` as Organizer";
  103. }
  104. $sql_join = implode("\n\t", $sql_join);
  105. $sql_select = implode(", ", $sql_select);
  106. $listItems = DB::getPDO()->fetchAll("
  107. select {$sql_select}
  108. from `CRM_TESTY` as t
  109. left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=t.`ID_STANOWISKO`)
  110. left join `ADMIN_USERS` as u on(u.`ID`=t.`ID_TESTER`)
  111. {$sql_join}
  112. where
  113. {$sql_where}
  114. {$sql_order_by}
  115. limit {$sql_limit}
  116. ");
  117. return array_map(function ($item) {
  118. $r = (object)$item;
  119. //$sql_select[] = "IF( z.`ID` is null, t.`ID_STANOWISKO`, z.`DESC` ) as Stanowisko";
  120. //$sql_select[] = "IF( u.`ID` is null, t.`ID_TESTER`, concat(u.`ADM_NAME`, ' (', u.`ADM_PHONE`, ', ', u.`ADM_OTHER_INFO`, ')') ) as Kandydat";
  121. //$sql_select[] = "IF( u.`ID` is null, t.`ID_TESTER`, u.`ADM_NAME`) as Kandydat_Name";
  122. if (!$r->Stanowisko) {
  123. $r->Stanowisko = $r->ID_STANOWISKO;
  124. }
  125. if (!$r->Kandydat) {
  126. $r->Kandydat = $r->ID_TESTER;
  127. }
  128. if (!$r->Kandydat_Name) {
  129. $r->Kandydat_Name = $r->ID_TESTER;
  130. }
  131. return $r;
  132. }, $listItems);
  133. }
  134. /**
  135. * @return array( ID_PROCES => array( ID_PYTANIE => {ID,ODPOWIEDZ,ODP_0,ODP_1,ODP_2,ODP_3,ODP_4,OCENA} ) )
  136. */
  137. public static function get_odpowiedzi($test_id, $type = 'TEORETYCZNY') {
  138. $ret = [];
  139. $sql_type = "";
  140. $types_allowed = self::get_allowed_types();
  141. if (array_key_exists($type, $types_allowed)) {
  142. $sql_type = "and pyt.`TEST_TYPE`='{$type}'";
  143. }
  144. $listItems = DB::getPDO()->fetchAll("
  145. select
  146. odp.`ID`
  147. , odp.`ID_PYTANIE`
  148. , pyt.`ID_PROCES` as ID_PROCES
  149. , odp.`ODPOWIEDZ`
  150. , odp.`ODP_0`
  151. , odp.`ODP_1`
  152. , odp.`ODP_2`
  153. , odp.`ODP_3`
  154. , odp.`ODP_4`
  155. , odp.`OCENA`
  156. from `CRM_TESTY_ODPOWIEDZI` as odp
  157. left join `CRM_TESTY_PYTANIA` as pyt on(pyt.`ID`=odp.`ID_PYTANIE`)
  158. where
  159. pyt.`ID` is not NULL
  160. and odp.`ID_TEST`='{$test_id}'
  161. {$sql_type}
  162. ");
  163. foreach ($listItems as $item) {
  164. $r = (object)$item;
  165. $ret[$r->ID_PROCES][$r->ID_PYTANIE] = $r;
  166. }
  167. return $ret;
  168. }
  169. public static function get_pytania_by_proces_id($proces_id, $type = 'TEORETYCZNY') {
  170. $ret = array();
  171. $sql_type = "";
  172. $types_allowed = self::get_allowed_types();
  173. if (array_key_exists($type, $types_allowed)) {
  174. $sql_type = "and pyt.`TEST_TYPE`='{$type}'";
  175. }
  176. return array_map(function ($item) { return (object)$item; }, DB::getPDO()->fetchAllByKey("
  177. select pyt.*
  178. from `CRM_TESTY_PYTANIA` as pyt
  179. where
  180. pyt.`ID_PROCES`='{$proces_id}'
  181. {$sql_type}
  182. order by pyt.`ID` ASC
  183. ", 'ID'));
  184. }
  185. /**
  186. * @param $pytania_id - array if int
  187. */
  188. public static function get_pytania_by_id($pytania_id) {
  189. $ret = array();
  190. if (empty($pytania_id)) return $ret;
  191. // TODO: if int or array
  192. if (!is_array($pytania_id)) $pytania_id = array($pytania_id);
  193. return array_map(function ($item) { return (object)$item; }, DB::getPDO()->fetchAllByKey("
  194. select
  195. pyt.*
  196. from `CRM_TESTY_PYTANIA` as pyt
  197. where
  198. pyt.`ID` in(".implode(",", $pytania_id).")
  199. ", 'ID'));
  200. }
  201. /**
  202. * Updates field TEST_PYTANIE in remote table.
  203. * moved to trigger after insert/delete `CRM_TESTY_PYTANIA`
  204. */
  205. public static function update_proces_stat($remote_id) {
  206. $sql = "
  207. update `CRM_PROCES`
  208. set `TEST_PYTANIE` = (
  209. select count(1)
  210. from `CRM_TESTY_PYTANIA`
  211. where
  212. `ID_PROCES` = '{$remote_id}'
  213. )
  214. where `ID` = '{$remote_id}'
  215. limit 1;
  216. ";
  217. // update all records
  218. DB::getPDO()->execSql("
  219. update `CRM_PROCES` as p
  220. set p.`TEST_PYTANIE` = (
  221. select count(1)
  222. from `CRM_TESTY_PYTANIA` as pyt
  223. where
  224. pyt.`ID_PROCES` = p.ID
  225. )
  226. ");
  227. // $affected = $db->affected_rows();
  228. // return $affected;
  229. }
  230. public static function delete_pytanie($id, $remote_id) {
  231. DB::getPDO()->execSql("
  232. delete from `CRM_TESTY_PYTANIA`
  233. where
  234. `ID` = '{$id}'
  235. and `ID_PROCES` = '{$remote_id}'
  236. limit 1
  237. ");
  238. // $affected = $db->affected_rows();
  239. // return $affected;
  240. return 1;
  241. }
  242. /**
  243. * Ocena pytania.
  244. * @param $p - sql object pytanie
  245. * @param $selected_values - array of user selected values
  246. * @returns null if pytanie is openm ale return integer value 0 - 3 (avg)
  247. */
  248. public static function ocen_pytanie(&$p, &$selected_values) {
  249. $ret = null;
  250. /**
  251. * TODO: pokaz tylko odpowiedz udzielona przez kandydata i czy jest prawidlowa
  252. * ODP_{id_pyt}
  253. * ODP_{id_pyt}_{id_odp} (TAK/NIE)
  254. */
  255. $pytanie_otwarte = true;
  256. $avg_odp_cnt = 0;
  257. $avg_odp_ok_cnt = 0;
  258. for ($i = 0; $i < 5; $i++) {
  259. $odp_field = "ODP_".$i;
  260. if ('' != $p->$odp_field) {// jest odpowiedz do pytania
  261. $avg_odp_cnt += 1;
  262. $p_field_prawidlowa_odp = "ODP_".$i."_ODP";
  263. $pytanie_otwarte = false;
  264. if (!empty($selected_values[$i]) != '' && $p->$p_field_prawidlowa_odp == $selected_values[$i]) {
  265. $avg_odp_ok_cnt += 1;
  266. }
  267. }
  268. }
  269. if (!$pytanie_otwarte) {
  270. $ret = round((3 * floatval($avg_odp_ok_cnt)) / $avg_odp_cnt);
  271. }
  272. return $ret;
  273. }
  274. /**
  275. * CRM_TESTY_PYTANIA.TEST_TYPE
  276. * CRM_TESTY.TEST_TYPE
  277. */
  278. public static function get_allowed_types() {
  279. $types_allowed = array('TEORETYCZNY'=>"Teoretyczny", 'PRAKTYCZNY'=>"Praktyczny");
  280. return $types_allowed;
  281. }
  282. /**
  283. * Get Testy stats by user.
  284. *
  285. * @param int $idUser
  286. * @param int $max_age
  287. * @param array $proces_ids
  288. *
  289. * @usage $testy = ProcesTestyHelper::get_tetsy_stats($idUser, 30000000, $proces_ids);
  290. */
  291. public static function get_tetsy_stats($idUser, $max_age = 0, $proces_ids = array()) {
  292. $testy_stats = array();
  293. Lib::loadClass('DB');
  294. $sqlWhereAnd = array();
  295. if (!empty($proces_ids)) {
  296. $sqlWhereAnd[] = "t.`ID_PROCES_INIT` in (" . implode(",", $proces_ids) . ")";
  297. }
  298. if ($max_age > 0) {
  299. $sqlWhereAnd[] = "COALESCE(UNIX_TIMESTAMP(t.`TEST_END`), 0) > (UNIX_TIMESTAMP(NOW()) - {$max_age})";
  300. }
  301. $sqlWhereAnd = (!empty($sqlWhereAnd)) ? "and " . implode("\n and ", $sqlWhereAnd) : '';
  302. return array_map(function ($item) { return (object)$item; }, DB::getPDO()->fetchAll("
  303. select test.ID
  304. , test.ID_PROCES_INIT
  305. , test.OCENA
  306. , test.A_STATUS
  307. , test.TEST_TYPE
  308. , test.TEST_END
  309. from CRM_TESTY test
  310. where test.ID in(
  311. select MAX(t.ID) as ID
  312. from CRM_TESTY t
  313. where t.`ID_TESTER`='{$idUser}'
  314. {$sqlWhereAnd}
  315. group by t.TEST_TYPE, t.ID_PROCES_INIT
  316. )
  317. "));
  318. }
  319. /**
  320. * Remove tests which is unactual - proces or step has changed.
  321. * @param $steps_ids
  322. */
  323. public static function get_max_update_date($steps_ids) {// TODO: RMME, mved to UserAcl->getProcesMaxUpdateDate
  324. $max_update_date = DB::getPDO()->fetchValue("
  325. select max(p.`A_RECORD_UPDATE_DATE`) as max_update_date
  326. from `CRM_PROCES` as p
  327. where p.`ID` in(" . implode(", ", $steps_ids) . ")
  328. ");
  329. return ($max_update_date) ? $max_update_date : null;
  330. }
  331. public static function getLastTestEndDate($id_test, $id_tester, $id_proces_init, $id_stanowisko) {
  332. $lastTestEnd = DB::getPDO()->fetchValue("
  333. select t.`TEST_END`
  334. from `CRM_TESTY` as t
  335. where
  336. t.`ID_TESTER` = {$id_tester}
  337. and t.`ID_PROCES_INIT` = {$id_proces_init}
  338. and t.`ID_STANOWISKO` = {$id_stanowisko}
  339. and t.`ID` < {$id_test}
  340. limit 1
  341. ");
  342. }
  343. public static function getOcenaLabel($ocena) {
  344. if (!$ocena) return 'BRAK_TESTU';
  345. else if ($ocena == -1) return 'BRAK_PYTAN';
  346. else if ($ocena == 3) return 'IDEALNY';
  347. else if ($ocena < 2) return 'NIEDOSTATECZNY';
  348. else if($ocena < 2.5) return 'DOSTATECZNY';
  349. else return 'DOBRY';
  350. }
  351. }