ProcesTestyHelper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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`='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. 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.`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. public static 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. public static function &get_pytania_by_proces_id( $proces_id, $type = 'TEORETYCZNY' ) {
  182. $ret = array();
  183. $sql_type = "";
  184. $types_allowed = self::get_allowed_types();
  185. if (array_key_exists($type, $types_allowed)) {
  186. $sql_type = "and pyt.`TEST_TYPE`='".$type."'";
  187. }
  188. $db = DB::getDB();
  189. $sql = "select pyt.*
  190. from `CRM_TESTY_PYTANIA` as pyt
  191. where
  192. pyt.`ID_PROCES`='".$proces_id."'
  193. " . $sql_type . "
  194. order by pyt.`ID` ASC
  195. ";
  196. $res = $db->query($sql);
  197. while ($r = $db->fetch($res)) {
  198. $ret[$r->ID] = $r;
  199. }
  200. return $ret;
  201. }
  202. /**
  203. * @param $pytania_id - array if int
  204. */
  205. public static function &get_pytania_by_id( $pytania_id ) {
  206. $ret = array();
  207. if (empty($pytania_id)) return $ret;
  208. // TODO: if int or array
  209. if (!is_array($pytania_id)) $pytania_id = array($pytania_id);
  210. $db = DB::getDB();
  211. $sql = "select
  212. pyt.*
  213. from `CRM_TESTY_PYTANIA` as pyt
  214. where
  215. pyt.`ID` in(".implode(",", $pytania_id).")
  216. ";
  217. $res = $db->query($sql);
  218. while ($r = $db->fetch($res)) {
  219. $ret[$r->ID] = $r;
  220. }
  221. return $ret;
  222. }
  223. /**
  224. * Updates field TEST_PYTANIE in remote table.
  225. */
  226. public static function update_proces_stat($remote_id) {
  227. $db = DB::getDB();
  228. $sql = "update `CRM_PROCES`
  229. set `TEST_PYTANIE`=(
  230. select count(1)
  231. from `CRM_TESTY_PYTANIA`
  232. where
  233. `ID_PROCES`='".$remote_id."'
  234. )
  235. where `ID`='".$remote_id."'
  236. limit 1;
  237. ";
  238. // update all records
  239. $sql = "update `CRM_PROCES` as p
  240. set p.`TEST_PYTANIE`=(
  241. select count(1)
  242. from `CRM_TESTY_PYTANIA` as pyt
  243. where
  244. pyt.`ID_PROCES`=p.ID
  245. )
  246. ";
  247. $res = $db->query($sql);
  248. $affected = $db->affected_rows();
  249. return $affected;
  250. }
  251. public static function delete_pytanie($id, $remote_id) {
  252. $db = DB::getDB();
  253. $sql = "delete from `CRM_TESTY_PYTANIA`
  254. where
  255. `ID`='".$id."'
  256. and `ID_PROCES`='".$remote_id."'
  257. limit 1
  258. ";
  259. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">sql delete = ';print_r($sql);echo'</pre>';
  260. $db->query($sql);
  261. $affected = $db->affected_rows();
  262. self::update_proces_stat($remote_id);
  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 (!empty($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 tbl.*
  327. from ( select t.`ID_PROCES_INIT`, t.`OCENA`, t.`A_STATUS`, t.`TEST_TYPE`, t.`TEST_END`
  328. from `CRM_TESTY` as t
  329. where " . implode("\n and ", $sql_where_arr) . "
  330. order by t.`ID` DESC
  331. ) AS tbl
  332. group by tbl.TEST_TYPE, tbl.ID_PROCES_INIT
  333. ";
  334. $res = $db->query($sql);
  335. while ($r = $db->fetch($res)) {
  336. $testy_stats []= $r;
  337. }
  338. return $testy_stats;
  339. }
  340. /**
  341. * Remove tests which is unactual - proces or step has changed.
  342. * @param $steps_ids
  343. */
  344. public static function get_max_update_date($steps_ids) {
  345. $max_update_date = null;
  346. $db = DB::getDB();
  347. $sql = "select max(p.`A_RECORD_UPDATE_DATE`) as max_update_date
  348. from `CRM_PROCES` as p
  349. where p.`ID` in(" . implode(", ", $steps_ids) . ")
  350. ";
  351. $res = $db->query($sql);
  352. if ($r = $db->fetch($res)) {
  353. $max_update_date = $r->max_update_date;
  354. }
  355. return $max_update_date;
  356. }
  357. public static function getLastTestEndDate($id_test, $id_tester, $id_proces_init, $id_stanowisko) {
  358. $lastTestEnd = null;
  359. $db = DB::getDB();
  360. $sql = "select t.`TEST_END`
  361. from `CRM_TESTY` as t
  362. where
  363. t.`ID_TESTER`={$id_tester}
  364. and t.`ID_PROCES_INIT`={$id_proces_init}
  365. and t.`ID_STANOWISKO`={$id_stanowisko}
  366. and t.`ID`<{$id_test}
  367. limit 1
  368. ";
  369. $res = $db->query($sql);
  370. if ($r = $db->fetch($res)) {
  371. $lastTestEnd = $r->TEST_END;
  372. }
  373. return $lastTestEnd;
  374. }
  375. }