ProcesTestyHelper.php 11 KB

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