model.ajax.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. // @todo this could be integrated in the inc/lib/model.lib.php + try to clean this file
  4. // @todo this file was rewrote in the model ajax controller
  5. $language_file = array('admin', 'exercice', 'gradebook', 'tracking');
  6. require_once '../global.inc.php';
  7. $libpath = api_get_path(LIBRARY_PATH);
  8. // 1. Setting variables needed by jqgrid
  9. $action = $_GET['a'];
  10. $page = intval($_REQUEST['page']); //page
  11. $limit = intval($_REQUEST['rows']); //quantity of rows
  12. $sidx = $_REQUEST['sidx']; //index (field) to filter
  13. $sord = $_REQUEST['sord']; //asc or desc
  14. if (strpos(strtolower($sidx), 'asc') !== false) {
  15. $sidx = str_replace(array('asc', ','), '', $sidx);
  16. $sord = 'asc';
  17. }
  18. if (strpos(strtolower($sidx), 'desc') !== false) {
  19. $sidx = str_replace(array('desc', ','), '', $sidx);
  20. $sord = 'desc';
  21. }
  22. if (!in_array($sord, array('asc','desc'))) {
  23. $sord = 'desc';
  24. }
  25. if (!in_array($action,
  26. array(
  27. 'get_exercise_results',
  28. 'get_hotpotatoes_exercise_results',
  29. 'get_work_user_list',
  30. 'get_timelines',
  31. 'get_user_skill_ranking',
  32. 'get_usergroups_teacher',
  33. 'get_question_list',
  34. 'get_user_list_plugin_widescale'
  35. )
  36. ))
  37. {
  38. api_protect_admin_script(true);
  39. }
  40. if ($action == 'get_user_list_plugin_widescale') {
  41. $allowed = api_is_drh() || api_is_platform_admin();
  42. if (!$allowed) {
  43. api_not_allowed();
  44. }
  45. }
  46. //Search features
  47. $ops = array(
  48. 'eq' => '=', //equal
  49. 'ne' => '<>', //not equal
  50. 'lt' => '<', //less than
  51. 'le' => '<=', //less than or equal
  52. 'gt' => '>', //greater than
  53. 'ge' => '>=', //greater than or equal
  54. 'bw' => 'LIKE', //begins with
  55. 'bn' => 'NOT LIKE', //doesn't begin with
  56. 'in' => 'LIKE', //is in
  57. 'ni' => 'NOT LIKE', //is not in
  58. 'ew' => 'LIKE', //ends with
  59. 'en' => 'NOT LIKE', //doesn't end with
  60. 'cn' => 'LIKE', //contains
  61. 'nc' => 'NOT LIKE' //doesn't contain
  62. );
  63. //@todo move this in the display_class or somewhere else
  64. /**
  65. * @param $col
  66. * @param $oper
  67. * @param $val
  68. * @return string
  69. */
  70. function get_where_clause($col, $oper, $val) {
  71. global $ops;
  72. if (empty($col)) {
  73. return '';
  74. }
  75. if ($oper == 'bw' || $oper == 'bn') {
  76. $val .= '%';
  77. }
  78. if ($oper == 'ew' || $oper == 'en') {
  79. $val = '%'.$val;
  80. }
  81. if ($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') {
  82. $val = '%'.$val.'%';
  83. }
  84. $val = Database::escape_string($val);
  85. return " $col {$ops[$oper]} '$val' ";
  86. }
  87. $where_condition = ""; //if there is no search request sent by jqgrid, $where should be empty
  88. $operation = isset($_REQUEST['oper']) ? $_REQUEST['oper'] : false;
  89. $export_format = isset($_REQUEST['export_format']) ? $_REQUEST['export_format'] : 'csv';
  90. $search_field = isset($_REQUEST['searchField']) ? $_REQUEST['searchField'] : false;
  91. $search_oper = isset($_REQUEST['searchOper']) ? $_REQUEST['searchOper'] : false;
  92. $search_string = isset($_REQUEST['searchString']) ? $_REQUEST['searchString'] : false;
  93. $extra_fields = array();
  94. $questionFields = array();
  95. if ($_REQUEST['_search'] == 'true') {
  96. $where_condition = ' 1 = 1 ';
  97. $where_condition_in_form = get_where_clause($search_field, $search_oper, $search_string);
  98. if (!empty($where_condition_in_form)) {
  99. $where_condition .= ' AND '.$where_condition_in_form;
  100. }
  101. $filters = isset($_REQUEST['filters']) ? json_decode($_REQUEST['filters']) : false;
  102. // for now
  103. if (!empty($filters)) {
  104. switch($action) {
  105. case 'get_questions':
  106. $type = 'question';
  107. break;
  108. case 'get_sessions':
  109. $type = 'session';
  110. break;
  111. }
  112. // Extra field.
  113. $extraField = new ExtraField($type);
  114. $result = $extraField->getExtraFieldRules($filters, 'extra_');
  115. $extra_fields = $result['extra_fields'];
  116. $condition_array = $result['condition_array'];
  117. if (!empty($condition_array)) {
  118. $where_condition .= ' AND ( ';
  119. $where_condition .= implode($filters->groupOp, $condition_array);
  120. $where_condition .= ' ) ';
  121. }
  122. // Question field
  123. $resultQuestion = $extraField->getExtraFieldRules($filters, 'question_');
  124. $questionFields = $resultQuestion['extra_fields'];
  125. $condition_array = $resultQuestion['condition_array'];
  126. if (!empty($condition_array)) {
  127. $where_condition .= ' AND ( ';
  128. $where_condition .= implode($filters->groupOp, $condition_array);
  129. $where_condition .= ' ) ';
  130. }
  131. }
  132. }
  133. // get index row - i.e. user click to sort $sord = $_GET['sord'];
  134. // get the direction
  135. if (!$sidx) {
  136. $sidx = 1;
  137. }
  138. //2. Selecting the count FIRST
  139. //@todo rework this
  140. switch ($action) {
  141. case 'get_user_list_plugin_widescale':
  142. $count = UserManager::get_user_data(null, null, null, null, true);
  143. break;
  144. case 'get_question_list':
  145. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  146. $exerciseId = isset($_REQUEST['exerciseId']) ? $_REQUEST['exerciseId'] : null;
  147. $exercise = new Exercise(api_get_course_int_id());
  148. $exercise->read($exerciseId, false);
  149. $count = $exercise->getQuestionCount();
  150. break;
  151. case 'get_group_reporting':
  152. $course_id = isset($_REQUEST['course_id']) ? $_REQUEST['course_id'] : null;
  153. $group_id = isset($_REQUEST['gidReq']) ? $_REQUEST['gidReq'] : null;
  154. $count = Tracking::get_group_reporting($course_id, $group_id, 'count');
  155. break;
  156. case 'get_user_course_report_resumed':
  157. $count = CourseManager::get_count_user_list_from_course_code(true, 'ruc');
  158. break;
  159. case 'get_user_course_report':
  160. $count = CourseManager::get_count_user_list_from_course_code(false);
  161. break;
  162. case 'get_course_exercise_medias':
  163. $course_id = api_get_course_int_id();
  164. $count = Question::get_count_course_medias($course_id);
  165. break;
  166. case 'get_user_skill_ranking':
  167. $skill = new Skill();
  168. $count = $skill->get_user_list_skill_ranking_count();
  169. break;
  170. case 'get_work_user_list':
  171. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  172. $work_id = $_REQUEST['work_id'];
  173. $count = get_count_work($work_id);
  174. break;
  175. case 'get_exercise_results':
  176. $exercise_id = $_REQUEST['exerciseId'];
  177. if (isset($_GET['filter_by_user']) && !empty($_GET['filter_by_user'])) {
  178. $filter_user = intval($_GET['filter_by_user']);
  179. if ($where_condition == "") {
  180. $where_condition .= " te.exe_user_id = '$filter_user'" ;
  181. } else {
  182. $where_condition .= " AND te.exe_user_id = '$filter_user'";
  183. }
  184. }
  185. $count = ExerciseLib::get_count_exam_results($exercise_id, $where_condition);
  186. break;
  187. case 'get_hotpotatoes_exercise_results':
  188. $hotpot_path = $_REQUEST['path'];
  189. $count = ExerciseLib::get_count_exam_hotpotatoes_results($hotpot_path);
  190. break;
  191. case 'get_sessions':
  192. $list_type = isset($_REQUEST['list_type']) ? $_REQUEST['list_type'] : 'simple';
  193. if ($list_type == 'simple') {
  194. $count = SessionManager::get_sessions_admin(array('where'=> $where_condition, 'extra' => $extra_fields), true);
  195. } else {
  196. $count = SessionManager::get_count_admin_complete(array('where'=> $where_condition, 'extra' => $extra_fields));
  197. }
  198. break;
  199. case 'get_extra_fields':
  200. $type = $_REQUEST['type'];
  201. $obj = new ExtraField($type);
  202. $count = $obj->get_count();
  203. break;
  204. case 'get_extra_field_options':
  205. $type = $_REQUEST['type'];
  206. $field_id = $_REQUEST['field_id'];
  207. $obj = new ExtraFieldOption($type);
  208. $count = $obj->get_count_by_field_id($field_id);
  209. break;
  210. case 'get_timelines':
  211. require_once $libpath.'timeline.lib.php';
  212. $obj = new Timeline();
  213. $count = $obj->get_count();
  214. break;
  215. case 'get_gradebooks':
  216. require_once $libpath.'gradebook.lib.php';
  217. $obj = new Gradebook();
  218. $count = $obj->get_count();
  219. break;
  220. case 'get_event_email_template':
  221. $obj = new EventEmailTemplate();
  222. $count = $obj->get_count();
  223. break;
  224. case 'get_careers':
  225. $obj = new Career();
  226. $count = $obj->get_count();
  227. break;
  228. case 'get_promotions':
  229. $obj = new Promotion();
  230. $count = $obj->get_count();
  231. break;
  232. case 'get_grade_models':
  233. $obj = new GradeModel();
  234. $count = $obj->get_count();
  235. break;
  236. case 'get_usergroups':
  237. $obj = new UserGroup();
  238. $count = $obj->get_count();
  239. break;
  240. case 'get_usergroups_teacher':
  241. $obj = new UserGroup();
  242. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'registered';
  243. $course_id = api_get_course_int_id();
  244. if ($type == 'registered') {
  245. $count = $obj->get_usergroup_by_course_with_data_count($course_id);
  246. } else {
  247. $count = $obj->get_count();
  248. }
  249. break;
  250. default:
  251. exit;
  252. }
  253. //3. Calculating first, end, etc
  254. $total_pages = 0;
  255. if ($count > 0) {
  256. if (!empty($limit)) {
  257. $total_pages = ceil($count/$limit);
  258. }
  259. }
  260. if ($page > $total_pages) {
  261. $page = $total_pages;
  262. }
  263. $start = $limit * $page - $limit;
  264. if ($start < 0) {
  265. $start = 0;
  266. }
  267. //4. Deleting an element if the user wants to
  268. if (isset($_REQUEST['oper']) && $_REQUEST['oper'] == 'del') {
  269. $obj->delete($_REQUEST['id']);
  270. }
  271. $is_allowedToEdit = api_is_allowed_to_edit(null, true) || api_is_allowed_to_edit(true) || api_is_drh();
  272. //5. Querying the DB for the elements
  273. $columns = array();
  274. switch ($action) {
  275. case 'get_user_list_plugin_widescale':
  276. $columns = array('username', 'firstname', 'lastname', 'exam_password');
  277. $column_names = array(get_lang('Username'), get_lang('Firstname'), get_lang('Lastname'), get_lang('Password'));
  278. $result = UserManager::get_user_data($start, $limit, $sidx, $sord);
  279. break;
  280. case 'get_question_list':
  281. if (isset($exercise) && !empty($exercise)) {
  282. $extraField = new ExtraField('question');
  283. $extraFields = $extraField->get_all(array('field_filter = ?' => 1));
  284. $columns = array('question', 'type', 'category', 'level', 'score');
  285. if (!empty($extraFields)) {
  286. foreach ($extraFields as $extraField) {
  287. $columns[] = $extraField['field_variable'];
  288. }
  289. }
  290. $columns[] = 'actions';
  291. $result = $exercise->getQuestionListPagination($start, $limit, $sidx, $sord, $where_condition, $extraFields);
  292. }
  293. break;
  294. case 'get_group_reporting':
  295. $columns = array('name', 'time', 'progress', 'score', 'works', 'messages', 'actions');
  296. $result = Tracking::get_group_reporting($course_id, $group_id, 'all', $start, $limit, $sidx, $sord, $where_condition);
  297. break;
  298. case 'get_course_exercise_medias':
  299. $columns = array('question');
  300. $result = Question::get_course_medias($course_id, $start, $limit, $sidx, $sord, $where_condition);
  301. if (!empty($result)) {
  302. foreach ($result as &$media) {
  303. $media['id'] = $media['iid'];
  304. }
  305. }
  306. break;
  307. case 'get_user_course_report_resumed':
  308. $columns = array('extra_ruc', 'training_hours', 'count_users', 'count_users_registered', 'average_hours_per_user', 'count_certificates');
  309. $column_names = array(get_lang('Company'), get_lang('TrainingHoursAccumulated'), get_lang('CountOfSubscriptions'), get_lang('CountOfUsers'), get_lang('AverageHoursPerStudent'), get_lang('CountCertificates'));
  310. $result = CourseManager::get_user_list_from_course_code(null, null, "LIMIT $start, $limit", " $sidx $sord", null, null, true, true, 'ruc');
  311. $new_result = array();
  312. if (!empty($result)) {
  313. foreach ($result as $row) {
  314. $row['training_hours'] = api_time_to_hms($row['training_hours']);
  315. $row['average_hours_per_user'] = api_time_to_hms($row['average_hours_per_user']);
  316. $new_result[] = $row;
  317. }
  318. $result = $new_result;
  319. }
  320. break;
  321. case 'get_user_course_report':
  322. $columns = array('course', 'user', 'time', 'certificate', 'progress_100', 'progress');
  323. $column_names = array(get_lang('Course'), get_lang('User'), get_lang('ManHours'), get_lang('CertificateGenerated'), get_lang('Approved'), get_lang('CourseAdvance'));
  324. $extra_fields = UserManager::get_extra_fields(0, 100, null, null, true, true);
  325. if (!empty($extra_fields)) {
  326. foreach($extra_fields as $extra) {
  327. $columns[] = $extra['1'];
  328. $column_names[] = $extra['3'];
  329. }
  330. }
  331. $result = CourseManager::get_user_list_from_course_code(null, null, "LIMIT $start, $limit", " $sidx $sord", null, null, true);
  332. break;
  333. case 'get_user_skill_ranking':
  334. $columns = array('photo', 'firstname', 'lastname', 'skills_acquired', 'currently_learning', 'rank');
  335. $result = $skill->get_user_list_skill_ranking($start, $limit, $sidx, $sord, $where_condition);
  336. $result = ArrayClass::msort($result, 'skills_acquired', 'asc');
  337. $skills_in_course = array();
  338. if (!empty($result)) {
  339. //$counter = 1;
  340. foreach ($result as &$item) {
  341. $user_info = api_get_user_info($item['user_id']);
  342. $personal_course_list = UserManager::get_personal_session_course_list($item['user_id']);
  343. $count_skill_by_course = array();
  344. foreach ($personal_course_list as $course_item) {
  345. if (!isset($skills_in_course[$course_item['code']])) {
  346. $count_skill_by_course[$course_item['code']] = $skill->get_count_skills_by_course($course_item['code']);
  347. $skills_in_course[$course_item['code']] = $count_skill_by_course[$course_item['code']];
  348. } else {
  349. $count_skill_by_course[$course_item['code']] = $skills_in_course[$course_item['code']];
  350. }
  351. }
  352. $item['photo'] = Display::img($user_info['avatar_small']);
  353. $item['currently_learning'] = !empty($count_skill_by_course) ? array_sum($count_skill_by_course) : 0;
  354. }
  355. }
  356. break;
  357. case 'get_work_user_list':
  358. if (isset($_GET['type']) && $_GET['type'] == 'simple') {
  359. $columns = array('type', 'firstname', 'lastname', 'username', 'title', 'qualification', 'sent_date', 'qualificator_id', 'actions');
  360. } else {
  361. $columns = array('type', 'firstname', 'lastname', 'username', 'title', 'sent_date', 'actions');
  362. }
  363. $result = get_work_user_list($start, $limit, $sidx, $sord, $work_id, $where_condition);
  364. break;
  365. case 'get_exercise_results':
  366. $course = api_get_course_info();
  367. //used inside get_exam_results_data()
  368. $documentPath = api_get_path(SYS_COURSE_PATH) . $course['path'] . "/document";
  369. if ($is_allowedToEdit) {
  370. $columns = array('firstname', 'lastname', 'username', 'group_name', 'exe_duration', 'start_date', 'exe_date', 'score', 'status', 'lp', 'actions');
  371. } else {
  372. //$columns = array('exe_duration', 'start_date', 'exe_date', 'score', 'status', 'actions');
  373. }
  374. $result = ExerciseLib::get_exam_results_data($start, $limit, $sidx, $sord, $exercise_id, $where_condition);
  375. break;
  376. case 'get_hotpotatoes_exercise_results':
  377. $course = api_get_course_info();
  378. //used inside get_exam_results_data()
  379. $documentPath = api_get_path(SYS_COURSE_PATH) . $course['path'] . "/document";
  380. $columns = array('firstname', 'lastname', 'username', 'group_name', 'exe_date', 'score', 'actions');
  381. $result = ExerciseLib::get_exam_results_hotpotatoes_data($start, $limit, $sidx, $sord, $hotpot_path, $where_condition); //get_exam_results_data($start, $limit, $sidx, $sord, $exercise_id, $where_condition);
  382. break;
  383. case 'get_sessions':
  384. $session_columns = SessionManager::get_session_columns($list_type);
  385. $columns = $session_columns['simple_column_name'];
  386. if ($list_type == 'simple') {
  387. $result = SessionManager::get_sessions_admin(array('where'=> $where_condition, 'order'=>"$sidx $sord", 'extra' => $extra_fields, 'limit'=> "$start , $limit"), false);
  388. } else {
  389. $result = SessionManager::get_sessions_admin_complete(array('where'=> $where_condition, 'order'=>"$sidx $sord", 'extra' => $extra_fields, 'limit'=> "$start , $limit"));
  390. }
  391. break;
  392. case 'get_timelines':
  393. $columns = array('headline', 'actions');
  394. //$columns = array('headline', 'type', 'start_date', 'end_date', 'text', 'media', 'media_credit', 'media_caption', 'title_slide', 'parent_id');
  395. if(!in_array($sidx, $columns)) {
  396. $sidx = 'headline';
  397. }
  398. $course_id = api_get_course_int_id();
  399. $result = Database::select('*', $obj->table, array('where' => array('parent_id = ? AND c_id = ?' => array('0', $course_id)), 'order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  400. $new_result = array();
  401. foreach ($result as $item) {
  402. if (!$item['status']) {
  403. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  404. }
  405. $item['headline'] = Display::url($item['headline'], api_get_path(WEB_CODE_PATH).'timeline/view.php?id='.$item['id']);
  406. $item['actions'] = Display::url(Display::return_icon('add.png', get_lang('AddItems')), api_get_path(WEB_CODE_PATH).'timeline/?action=add_item&parent_id='.$item['id']);
  407. $item['actions'] .= Display::url(Display::return_icon('edit.png', get_lang('Edit')), api_get_path(WEB_CODE_PATH).'timeline/?action=edit&id='.$item['id']);
  408. $item['actions'] .= Display::url(Display::return_icon('delete.png', get_lang('Delete')), api_get_path(WEB_CODE_PATH).'timeline/?action=delete&id='.$item['id']);
  409. $new_result[] = $item;
  410. }
  411. $result = $new_result;
  412. break;
  413. case 'get_gradebooks':
  414. $columns = array('name', 'certificates','skills', 'actions', 'has_certificates');
  415. if (!in_array($sidx, $columns)) {
  416. $sidx = 'name';
  417. }
  418. $result = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  419. $new_result = array();
  420. foreach($result as $item) {
  421. if ($item['parent_id'] != 0) {
  422. continue;
  423. }
  424. $skills = $obj->get_skills_by_gradebook($item['id']);
  425. //Fixes bug when gradebook doesn't have names
  426. if (empty($item['name'])) {
  427. $item['name'] = $item['course_code'];
  428. } else {
  429. //$item['name'] = $item['name'].' ['.$item['course_code'].']';
  430. }
  431. $item['name'] = Display::url($item['name'], api_get_path(WEB_CODE_PATH).'gradebook/index.php?id_session=0&cidReq='.$item['course_code']);
  432. if (!empty($item['certif_min_score']) && !empty($item['document_id'])) {
  433. $item['certificates'] = Display::return_icon('accept.png', get_lang('WithCertificate'), array(), ICON_SIZE_SMALL);
  434. $item['has_certificates'] = '1';
  435. } else {
  436. $item['certificates'] = Display::return_icon('warning.png', get_lang('NoCertificate'), array(), ICON_SIZE_SMALL);
  437. $item['has_certificates'] = '0';
  438. }
  439. if (!empty($skills)) {
  440. foreach($skills as $skill) {
  441. $item['skills'] .= Display::span($skill['name'], array('class' => 'label_tag skill'));
  442. }
  443. }
  444. $new_result[] = $item;
  445. }
  446. $result = $new_result;
  447. break;
  448. case 'get_event_email_template':
  449. $columns = array('subject', 'event_type_name', 'language_id', 'activated', 'actions');
  450. if(!in_array($sidx, $columns)) {
  451. $sidx = 'subject';
  452. }
  453. $result = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  454. $new_result = array();
  455. foreach ($result as $item) {
  456. $language_info = api_get_language_info($item['language_id']);
  457. $item['language_id'] = $language_info['english_name'];
  458. $item['actions'] = Display::url(Display::return_icon('edit.png', get_lang('Edit')), api_get_path(WEB_CODE_PATH).'admin/event_type.php?action=edit&event_type_name='.$item['event_type_name']);
  459. $item['actions'] .= Display::url(Display::return_icon('delete.png', get_lang('Delete')), api_get_path(WEB_CODE_PATH).'admin/event_controller.php?action=delete&id='.$item['id']);
  460. /*if (!$item['status']) {
  461. $item['name'] = '<font style="color:#AAA">'.$item['subject'].'</font>';
  462. }*/
  463. $new_result[] = $item;
  464. }
  465. $result = $new_result;
  466. break;
  467. case 'get_careers':
  468. $columns = array('name', 'description', 'actions');
  469. if(!in_array($sidx, $columns)) {
  470. $sidx = 'name';
  471. }
  472. $result = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  473. $new_result = array();
  474. foreach ($result as $item) {
  475. if (!$item['status']) {
  476. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  477. }
  478. $new_result[] = $item;
  479. }
  480. $result = $new_result;
  481. break;
  482. case 'get_promotions':
  483. $columns = array('name', 'career', 'description', 'actions');
  484. if(!in_array($sidx, $columns)) {
  485. $sidx = 'name';
  486. }
  487. $result = Database::select('p.id,p.name, p.description, c.name as career, p.status', "$obj->table p LEFT JOIN ".Database::get_main_table(TABLE_CAREER)." c ON c.id = p.career_id ", array('order' =>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  488. $new_result = array();
  489. foreach($result as $item) {
  490. if (!$item['status']) {
  491. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  492. }
  493. $new_result[] = $item;
  494. }
  495. $result = $new_result;
  496. break;
  497. case 'get_grade_models':
  498. $columns = array('name', 'description', 'actions');
  499. if (!in_array($sidx, $columns)) {
  500. $sidx = 'name';
  501. }
  502. $result = Database::select('*', "$obj->table ", array('order' =>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  503. $new_result = array();
  504. foreach($result as $item) {
  505. $new_result[] = $item;
  506. }
  507. $result = $new_result;
  508. break;
  509. case 'get_usergroups':
  510. $columns = array('name', 'users', 'courses', 'sessions', 'group_type', 'actions');
  511. $result = Database::select('*', $obj->table, array('order'=>"name $sord", 'LIMIT'=> "$start , $limit"));
  512. $new_result = array();
  513. if (!empty($result)) {
  514. foreach ($result as $group) {
  515. $group['sessions'] = count($obj->get_sessions_by_usergroup($group['id']));
  516. $group['courses'] = count($obj->get_courses_by_usergroup($group['id']));
  517. $group['users'] = count($obj->get_users_by_usergroup($group['id']));
  518. switch ($group['group_type']) {
  519. case '0':
  520. $group['group_type'] = Display::label(get_lang('Class'), 'info');
  521. break;
  522. case '1' :
  523. $group['group_type'] = Display::label(get_lang('Social'), 'success');
  524. break;
  525. }
  526. $new_result[] = $group;
  527. }
  528. $result = $new_result;
  529. }
  530. $columns = array('name', 'users', 'courses','sessions', 'group_type');
  531. if(!in_array($sidx, $columns)) {
  532. $sidx = 'name';
  533. }
  534. //Multidimensional sort
  535. ArrayClass::msort($result, $sidx);
  536. break;
  537. case 'get_extra_fields':
  538. $obj = new ExtraField($type);
  539. $columns = array('field_display_text', 'field_variable', 'field_type', 'field_changeable', 'field_visible', 'field_filter', 'field_order');
  540. $result = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  541. $new_result = array();
  542. if (!empty($result)) {
  543. foreach ($result as $item) {
  544. $item['field_type'] = $obj->get_field_type_by_id($item['field_type']);
  545. $item['field_changeable'] = $item['field_changeable'] ? Display::return_icon('right.gif') : Display::return_icon('wrong.gif');
  546. $item['field_visible'] = $item['field_visible'] ? Display::return_icon('right.gif') : Display::return_icon('wrong.gif');
  547. $item['field_filter'] = $item['field_filter'] ? Display::return_icon('right.gif') : Display::return_icon('wrong.gif');
  548. $new_result[] = $item;
  549. }
  550. $result = $new_result;
  551. }
  552. break;
  553. case 'get_extra_field_options':
  554. $obj = new ExtraFieldOption($type);
  555. $columns = array('option_display_text', 'option_value', 'option_order');
  556. $result = Database::select('*', $obj->table, array('where' => array("field_id = ? " => $field_id),'order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  557. /*$new_result = array();
  558. if (!empty($result)) {
  559. foreach ($result as $item) {
  560. $item['field_type'] = $obj->get_field_type_by_id($item['field_type']);
  561. $item['field_changeable'] = $item['field_changeable'] ? Display::return_icon('right.gif') : Display::return_icon('wrong.gif');
  562. $item['field_visible'] = $item['field_visible'] ? Display::return_icon('right.gif') : Display::return_icon('wrong.gif');
  563. $item['field_filter'] = $item['field_filter'] ? Display::return_icon('right.gif') : Display::return_icon('wrong.gif');
  564. $new_result[] = $item;
  565. }
  566. $result = $new_result;
  567. }*/
  568. break;
  569. case 'get_usergroups_teacher':
  570. $columns = array('name', 'users', 'actions');
  571. $options = array('order'=>"name $sord", 'LIMIT'=> "$start , $limit");
  572. $options['course_id'] = $course_id;
  573. switch ($type) {
  574. case 'not_registered':
  575. $options['where'] = array(" (course_id IS NULL OR course_id != ?) " => $course_id);
  576. $result = $obj->get_usergroup_not_in_course($options);
  577. break;
  578. case 'registered':
  579. $options['where'] = array(" usergroup.course_id = ? " => $course_id);
  580. $result = $obj->get_usergroup_in_course($options);
  581. break;
  582. }
  583. $new_result = array();
  584. if (!empty($result)) {
  585. foreach ($result as $group) {
  586. $group['users'] = count($obj->get_users_by_usergroup($group['id']));
  587. if ($obj->usergroup_was_added_in_course($group['id'], $course_id)) {
  588. $url = 'class.php?action=remove_class_from_course&id='.$group['id'];
  589. $icon = Display::return_icon('delete.png', get_lang('Remove'));
  590. } else {
  591. $url = 'class.php?action=add_class_to_course&id='.$group['id'];
  592. $icon = Display::return_icon('add.png', get_lang('Add'));
  593. }
  594. $group['actions'] = Display::url($icon, $url);
  595. $new_result[] = $group;
  596. }
  597. $result = $new_result;
  598. }
  599. if (!in_array($sidx, $columns)) {
  600. $sidx = 'name';
  601. }
  602. //Multidimensional sort
  603. ArrayClass::msort($result, $sidx);
  604. break;
  605. default:
  606. exit;
  607. }
  608. $allowed_actions = array(
  609. 'get_careers',
  610. 'get_promotions',
  611. 'get_usergroups',
  612. 'get_usergroups_teacher',
  613. 'get_gradebooks',
  614. 'get_sessions',
  615. 'get_exercise_results',
  616. 'get_hotpotatoes_exercise_results',
  617. 'get_work_user_list',
  618. 'get_timelines',
  619. 'get_grade_models',
  620. 'get_event_email_template',
  621. 'get_user_skill_ranking',
  622. 'get_extra_fields',
  623. 'get_extra_field_options',
  624. 'get_course_exercise_medias',
  625. 'get_user_course_report',
  626. 'get_user_course_report_resumed',
  627. 'get_group_reporting',
  628. 'get_question_list',
  629. 'get_user_list_plugin_widescale'
  630. );
  631. //5. Creating an obj to return a json
  632. if (in_array($action, $allowed_actions)) {
  633. $response = new stdClass();
  634. $response->page = $page;
  635. $response->total = $total_pages;
  636. $response->records = $count;
  637. if ($operation && $operation == 'excel') {
  638. $j = 1;
  639. $array = array();
  640. if (empty($column_names)) {
  641. $column_names = $columns;
  642. }
  643. //Headers
  644. foreach ($column_names as $col) {
  645. $array[0][] = $col;
  646. }
  647. foreach ($result as $row) {
  648. foreach ($columns as $col) {
  649. $array[$j][] = strip_tags($row[$col]);
  650. }
  651. $j++;
  652. }
  653. switch ($export_format) {
  654. case 'xls':
  655. Export::export_table_xls($array, 'company_report');
  656. break;
  657. case 'csv':
  658. default:
  659. Export::export_table_csv($array, 'company_report');
  660. break;
  661. }
  662. exit;
  663. }
  664. $i = 0;
  665. if (!empty($result)) {
  666. foreach ($result as $row) {
  667. //print_r($row);
  668. // if results tab give not id, set id to $i otherwise id="null" for all <tr> of the jqgrid - ref #4235
  669. if (!isset($row['id']) || isset($row['id']) && $row['id'] == "") {
  670. $response->rows[$i]['id']=$i;
  671. } else {
  672. $response->rows[$i]['id']=$row['id'];
  673. }
  674. $array = array();
  675. foreach ($columns as $col) {
  676. $array[] = isset($row[$col]) ? $row[$col] : null;
  677. }
  678. $response->rows[$i]['cell']=$array;
  679. $i++;
  680. }
  681. }
  682. echo json_encode($response);
  683. }
  684. exit;