ProcesTestyHelper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. */
  227. public static function update_proces_stat($remote_id) {
  228. $db = DB::getDB();
  229. $sql = "update `CRM_PROCES`
  230. set `TEST_PYTANIE`=(
  231. select count(1)
  232. from `CRM_TESTY_PYTANIA`
  233. where
  234. `ID_PROCES`='{$remote_id}'
  235. )
  236. where `ID`='{$remote_id}'
  237. limit 1;
  238. ";
  239. // update all records
  240. $sql = "update `CRM_PROCES` as p
  241. set p.`TEST_PYTANIE`=(
  242. select count(1)
  243. from `CRM_TESTY_PYTANIA` as pyt
  244. where
  245. pyt.`ID_PROCES`=p.ID
  246. )
  247. ";
  248. $res = $db->query($sql);
  249. $affected = $db->affected_rows();
  250. return $affected;
  251. }
  252. public static function delete_pytanie($id, $remote_id) {
  253. $db = DB::getDB();
  254. $sql = "delete from `CRM_TESTY_PYTANIA`
  255. where
  256. `ID`='{$id}'
  257. and `ID_PROCES`='{$remote_id}'
  258. limit 1
  259. ";
  260. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">sql delete = ';print_r($sql);echo'</pre>';
  261. $db->query($sql);
  262. $affected = $db->affected_rows();
  263. self::update_proces_stat($remote_id);
  264. return $affected;
  265. }
  266. /**
  267. * Ocena pytania.
  268. * @param $p - sql object pytanie
  269. * @param $selected_values - array of user selected values
  270. * @returns null if pytanie is openm ale return integer value 0 - 3 (avg)
  271. */
  272. public static function ocen_pytanie(&$p, &$selected_values) {
  273. $ret = null;
  274. /**
  275. * TODO: pokaz tylko odpowiedz udzielona przez kandydata i czy jest prawidlowa
  276. * ODP_{id_pyt}
  277. * ODP_{id_pyt}_{id_odp} (TAK/NIE)
  278. */
  279. $pytanie_otwarte = true;
  280. $avg_odp_cnt = 0;
  281. $avg_odp_ok_cnt = 0;
  282. for ($i = 0; $i < 5; $i++) {
  283. $odp_field = "ODP_".$i;
  284. if (!empty($p->$odp_field)) {// jest odpowiedz do pytania
  285. $avg_odp_cnt += 1;
  286. $p_field_prawidlowa_odp = "ODP_".$i."_ODP";
  287. $pytanie_otwarte = false;
  288. if (!empty($selected_values[$i]) != '' && $p->$p_field_prawidlowa_odp == $selected_values[$i]) {
  289. $avg_odp_ok_cnt += 1;
  290. }
  291. }
  292. }
  293. if (!$pytanie_otwarte) {
  294. $ret = round((3 * floatval($avg_odp_ok_cnt)) / $avg_odp_cnt);
  295. }
  296. return $ret;
  297. }
  298. /**
  299. * CRM_TESTY_PYTANIA.TEST_TYPE
  300. * CRM_TESTY.TEST_TYPE
  301. */
  302. public static function get_allowed_types() {
  303. $types_allowed = array('TEORETYCZNY'=>"Teoretyczny", 'PRAKTYCZNY'=>"Praktyczny");
  304. return $types_allowed;
  305. }
  306. /**
  307. * Get Testy stats by user.
  308. *
  309. * @param int $user_id
  310. * @param int $max_age
  311. * @param array $proces_ids
  312. *
  313. * @usage $testy = ProcesTestyHelper::get_tetsy_stats($user_id, 30000000, $proces_ids);
  314. */
  315. public static function get_tetsy_stats($user_id, $max_age = 0, $proces_ids = array()) {
  316. $testy_stats = array();
  317. Lib::loadClass('DB');
  318. $db = DB::getDB();
  319. $sql_where_arr = array();
  320. $sql_where_arr[] = "t.`ID_TESTER`='{$user_id}'";
  321. if (!empty($proces_ids)) {
  322. $sql_where_arr[] = "t.`ID_PROCES_INIT` in (" . implode(",", $proces_ids) . ")";
  323. }
  324. if ($max_age > 0) {
  325. $sql_where_arr[] = "UNIX_TIMESTAMP(t.`TEST_END`) > (UNIX_TIMESTAMP(NOW()) - $max_age)";
  326. }
  327. $sql = "select MAX(tbl.`ID`) as `ID`
  328. , tbl.`ID_PROCES_INIT`
  329. , tbl.`OCENA`
  330. , tbl.`A_STATUS`
  331. , tbl.`TEST_TYPE`
  332. , tbl.`TEST_END`
  333. from ( select t.`ID`, t.`ID_PROCES_INIT`, t.`OCENA`, t.`A_STATUS`, t.`TEST_TYPE`, t.`TEST_END`
  334. from `CRM_TESTY` as t
  335. where " . implode("\n and ", $sql_where_arr) . "
  336. order by t.`ID` DESC
  337. ) AS tbl
  338. group by tbl.`TEST_TYPE`, tbl.`ID_PROCES_INIT`
  339. ";
  340. $res = $db->query($sql);
  341. while ($r = $db->fetch($res)) {
  342. $testy_stats[] = $r;
  343. }
  344. return $testy_stats;
  345. }
  346. /**
  347. * Remove tests which is unactual - proces or step has changed.
  348. * @param $steps_ids
  349. */
  350. public static function get_max_update_date($steps_ids) {
  351. $max_update_date = null;
  352. $db = DB::getDB();
  353. $sql = "select max(p.`A_RECORD_UPDATE_DATE`) as max_update_date
  354. from `CRM_PROCES` as p
  355. where p.`ID` in(" . implode(", ", $steps_ids) . ")
  356. ";
  357. $res = $db->query($sql);
  358. if ($r = $db->fetch($res)) {
  359. $max_update_date = $r->max_update_date;
  360. }
  361. return $max_update_date;
  362. }
  363. public static function getLastTestEndDate($id_test, $id_tester, $id_proces_init, $id_stanowisko) {
  364. $lastTestEnd = null;
  365. $db = DB::getDB();
  366. $sql = "select t.`TEST_END`
  367. from `CRM_TESTY` as t
  368. where
  369. t.`ID_TESTER` = {$id_tester}
  370. and t.`ID_PROCES_INIT` = {$id_proces_init}
  371. and t.`ID_STANOWISKO` = {$id_stanowisko}
  372. and t.`ID` < {$id_test}
  373. limit 1
  374. ";
  375. $res = $db->query($sql);
  376. if ($r = $db->fetch($res)) {
  377. $lastTestEnd = $r->TEST_END;
  378. }
  379. return $lastTestEnd;
  380. }
  381. }