model.ajax.php 32 KB

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