model.ajax.php 28 KB

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