ProcesTestyHelper.php 11 KB

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