model.ajax.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  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. require_once '../global.inc.php';
  5. $libpath = api_get_path(LIBRARY_PATH);
  6. // 1. Setting variables needed by jqgrid
  7. $action = $_GET['a'];
  8. $page = intval($_REQUEST['page']); //page
  9. $limit = intval($_REQUEST['rows']); //quantity of rows
  10. $sidx = $_REQUEST['sidx']; //index (field) to filter
  11. $sord = $_REQUEST['sord']; //asc or desc
  12. if (strpos(strtolower($sidx), 'asc') !== false) {
  13. $sidx = str_replace(array('asc', ','), '', $sidx);
  14. $sord = 'asc';
  15. }
  16. if (strpos(strtolower($sidx), 'desc') !== false) {
  17. $sidx = str_replace(array('desc', ','), '', $sidx);
  18. $sord = 'desc';
  19. }
  20. if (!in_array($sord, array('asc','desc'))) {
  21. $sord = 'desc';
  22. }
  23. // Actions allowed to other roles.
  24. if (!in_array(
  25. $action,
  26. array(
  27. 'get_exercise_results',
  28. 'get_work_student_list_overview',
  29. 'get_hotpotatoes_exercise_results',
  30. 'get_work_teacher',
  31. 'get_work_student',
  32. 'get_work_user_list',
  33. 'get_work_user_list_others',
  34. 'get_work_user_list_all',
  35. 'get_timelines',
  36. 'get_user_skill_ranking',
  37. 'get_usergroups_teacher',
  38. 'get_user_course_report_resumed',
  39. 'get_user_course_report',
  40. 'get_sessions_tracking',
  41. 'get_sessions'
  42. )
  43. ) && !isset($_REQUEST['from_course_session'])) {
  44. api_protect_admin_script(true);
  45. } elseif (isset($_REQUEST['from_course_session']) &&
  46. $_REQUEST['from_course_session'] == 1
  47. ) {
  48. api_protect_teacher_script(true);
  49. }
  50. // Search features
  51. //@todo move this in the display_class or somewhere else
  52. function getWhereClause($col, $oper, $val)
  53. {
  54. $ops = array(
  55. 'eq' => '=', //equal
  56. 'ne' => '<>', //not equal
  57. 'lt' => '<', //less than
  58. 'le' => '<=', //less than or equal
  59. 'gt' => '>', //greater than
  60. 'ge' => '>=', //greater than or equal
  61. 'bw' => 'LIKE', //begins with
  62. 'bn' => 'NOT LIKE', //doesn't begin with
  63. 'in' => 'LIKE', //is in
  64. 'ni' => 'NOT LIKE', //is not in
  65. 'ew' => 'LIKE', //ends with
  66. 'en' => 'NOT LIKE', //doesn't end with
  67. 'cn' => 'LIKE', //contains
  68. 'nc' => 'NOT LIKE' //doesn't contain
  69. );
  70. if (empty($col)) {
  71. return '';
  72. }
  73. if ($oper == 'bw' || $oper == 'bn') {
  74. $val .= '%';
  75. }
  76. if ($oper == 'ew' || $oper == 'en') {
  77. $val = '%'.$val;
  78. }
  79. if ($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') {
  80. $val = '%'.$val.'%';
  81. }
  82. $val = Database::escape_string($val);
  83. return " $col {$ops[$oper]} '$val' ";
  84. }
  85. // If there is no search request sent by jqgrid, $where should be empty
  86. $whereCondition = null;
  87. $operation = isset($_REQUEST['oper']) ? $_REQUEST['oper'] : false;
  88. $exportFormat = isset($_REQUEST['export_format']) ? $_REQUEST['export_format'] : 'csv';
  89. $searchField = isset($_REQUEST['searchField']) ? $_REQUEST['searchField'] : false;
  90. $searchOperator = isset($_REQUEST['searchOper']) ? $_REQUEST['searchOper'] : false;
  91. $searchString = isset($_REQUEST['searchString']) ? $_REQUEST['searchString'] : false;
  92. $search = isset($_REQUEST['_search']) ? $_REQUEST['_search'] : false;
  93. $forceSearch = isset($_REQUEST['_force_search']) ? $_REQUEST['_force_search'] : false;
  94. $extra_fields = array();
  95. if (($search || $forceSearch) && ($search !== 'false')) {
  96. $whereCondition = ' 1 = 1 ';
  97. $whereConditionInForm = getWhereClause($searchField, $searchOperator, $searchString);
  98. if (!empty($whereConditionInForm)) {
  99. $whereCondition .= ' AND '.$whereConditionInForm;
  100. }
  101. $filters = isset($_REQUEST['filters']) && !is_array($_REQUEST['filters']) ? json_decode($_REQUEST['filters']) : false;
  102. if (!empty($filters)) {
  103. if (in_array($action, ['get_questions', 'get_sessions'])) {
  104. switch ($action) {
  105. case 'get_questions':
  106. $type = 'question';
  107. break;
  108. case 'get_sessions':
  109. $type = 'session';
  110. break;
  111. }
  112. if (!empty($type)) {
  113. // Extra field.
  114. $extraField = new ExtraField($type);
  115. $result = $extraField->getExtraFieldRules($filters, 'extra_');
  116. $extra_fields = $result['extra_fields'];
  117. $condition_array = $result['condition_array'];
  118. $extraCondition = '';
  119. if (!empty($condition_array)) {
  120. $extraCondition = ' AND ( ';
  121. $extraCondition .= implode($filters->groupOp, $condition_array);
  122. $extraCondition .= ' ) ';
  123. }
  124. $whereCondition .= $extraCondition;
  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. $extraQuestionCondition = ' AND ( ';
  131. $extraQuestionCondition .= implode($filters->groupOp, $condition_array);
  132. $extraQuestionCondition .= ' ) ';
  133. // Remove conditions already added
  134. $extraQuestionCondition = str_replace($extraCondition, '', $extraQuestionCondition);
  135. }
  136. $whereCondition .= $extraQuestionCondition;
  137. }
  138. } elseif (!empty($filters->rules)) {
  139. $whereCondition .= ' AND ( ';
  140. $counter = 0;
  141. foreach ($filters->rules as $key => $rule) {
  142. $whereCondition .= getWhereClause($rule->field, $rule->op, $rule->data);
  143. if ($counter < count($filters->rules) -1) {
  144. $whereCondition .= $filters->groupOp;
  145. }
  146. $counter++;
  147. }
  148. $whereCondition .= ' ) ';
  149. }
  150. }
  151. }
  152. // get index row - i.e. user click to sort $sord = $_GET['sord'];
  153. // get the direction
  154. if (!$sidx) {
  155. $sidx = 1;
  156. }
  157. //2. Selecting the count FIRST
  158. //@todo rework this
  159. switch ($action) {
  160. case 'get_group_reporting':
  161. $course_id = isset($_REQUEST['course_id']) ? $_REQUEST['course_id'] : null;
  162. $group_id = isset($_REQUEST['gidReq']) ? $_REQUEST['gidReq'] : null;
  163. $count = Tracking::get_group_reporting($course_id, $group_id, 'count');
  164. break;
  165. case 'get_user_course_report':
  166. case 'get_user_course_report_resumed':
  167. $userId = api_get_user_id();
  168. if (!(api_is_platform_admin(false, true))) {
  169. //exit;
  170. }
  171. $sessionId = isset($_GET['session_id']) ? intval($_GET['session_id']) : 0;
  172. $courseCodeList = array();
  173. $userIdList = array();
  174. $sessionIdList = [];
  175. $searchByGroups = false;
  176. if (api_is_drh()) {
  177. if (api_drh_can_access_all_session_content()) {
  178. $userList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  179. 'drh_all',
  180. api_get_user_id()
  181. );
  182. if (!empty($userList)) {
  183. foreach ($userList as $user) {
  184. $userIdList[] = $user['user_id'];
  185. }
  186. }
  187. $courseList = SessionManager::getAllCoursesFollowedByUser(
  188. api_get_user_id(),
  189. null
  190. );
  191. if (!empty($courseList)) {
  192. foreach ($courseList as $course) {
  193. $courseCodeList[] = $course['code'];
  194. }
  195. }
  196. } else {
  197. $userList = UserManager::get_users_followed_by_drh(api_get_user_id());
  198. if (!empty($userList)) {
  199. $userIdList = array_keys($userList);
  200. }
  201. $courseList = CourseManager::get_courses_followed_by_drh(api_get_user_id());
  202. if (!empty($courseList)) {
  203. $courseCodeList = array_keys($courseList);
  204. }
  205. }
  206. if (empty($userIdList) || empty($courseCodeList)) {
  207. exit;
  208. }
  209. } elseif (api_is_student_boss()) {
  210. $searchByGroups = true;
  211. } elseif (api_is_platform_admin()) {
  212. if ($sessionId == -1) {
  213. $userIdList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  214. 'admin',
  215. null
  216. );
  217. $userIdList = array_column($userIdList, 'user_id');
  218. $sessionList = SessionManager::get_sessions_list();
  219. $sessionIdList = array_column($sessionList, 'id');
  220. $courseCodeList = array();
  221. foreach ($sessionList as $session) {
  222. $courses = SessionManager::get_course_list_by_session_id($session['id']);
  223. $courseCodeList = array_merge($courseCodeList, array_column($courses, 'code'));
  224. }
  225. }
  226. $searchByGroups = true;
  227. }
  228. if ($searchByGroups) {
  229. $userGroup = new UserGroup();
  230. $userIdList = array_merge(
  231. $userIdList,
  232. $userGroup->getGroupUsersByUser(api_get_user_id())
  233. );
  234. }
  235. if (is_array($userIdList)) {
  236. $userIdList = array_unique($userIdList);
  237. }
  238. if (api_is_student_boss()) {
  239. $userCourses = [];
  240. foreach ($userIdList as $userId) {
  241. $userCourses = array_merge(
  242. $userCourses,
  243. CourseManager::get_courses_list_by_user_id($userId, true)
  244. );
  245. $userSessions = SessionManager::getSessionsFollowedByUser($userId);
  246. $sessionIdList = array_merge(
  247. $sessionIdList,
  248. array_column($userSessions, 'id')
  249. );
  250. }
  251. $courseCodeList = array_column($userCourses, 'code');
  252. }
  253. if (!empty($courseCodeList)) {
  254. $courseCodeList = array_unique($courseCodeList);
  255. }
  256. if (!empty($sessionIdList)) {
  257. $sessionIdList = array_unique($sessionIdList);
  258. }
  259. if (api_is_student_boss() && empty($userIdList)) {
  260. $count = 0;
  261. break;
  262. }
  263. if ($action == 'get_user_course_report') {
  264. $count = CourseManager::get_count_user_list_from_course_code(
  265. false,
  266. null,
  267. $courseCodeList,
  268. $userIdList,
  269. $sessionIdList
  270. );
  271. } else {
  272. $count = CourseManager::get_count_user_list_from_course_code(
  273. true,
  274. array('ruc'),
  275. $courseCodeList,
  276. $userIdList,
  277. $sessionIdList
  278. );
  279. }
  280. break;
  281. case 'get_course_exercise_medias':
  282. $course_id = api_get_course_int_id();
  283. $count = Question::get_count_course_medias($course_id);
  284. break;
  285. case 'get_user_skill_ranking':
  286. $skill = new Skill();
  287. $count = $skill->get_user_list_skill_ranking_count();
  288. break;
  289. case 'get_work_teacher':
  290. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  291. $count = getWorkListTeacher(0, $limit, $sidx, $sord, $whereCondition, true);
  292. break;
  293. case 'get_work_student':
  294. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  295. $count = getWorkListStudent(0, $limit, $sidx, $sord, $whereCondition, true);
  296. break;
  297. case 'get_work_user_list_all':
  298. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  299. $work_id = $_REQUEST['work_id'];
  300. $count = get_count_work($work_id);
  301. break;
  302. case 'get_work_user_list_others':
  303. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  304. $work_id = $_REQUEST['work_id'];
  305. $count = get_count_work($work_id, api_get_user_id());
  306. break;
  307. case 'get_work_user_list':
  308. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  309. $work_id = $_REQUEST['work_id'];
  310. $courseInfo = api_get_course_info();
  311. $documents = getAllDocumentToWork($work_id, api_get_course_int_id());
  312. if (empty($documents)) {
  313. $whereCondition .= " AND u.user_id = ".api_get_user_id();
  314. $count = get_work_user_list(
  315. 0,
  316. $limit,
  317. $sidx,
  318. $sord,
  319. $work_id,
  320. $whereCondition,
  321. null,
  322. true
  323. );
  324. } else {
  325. $count = get_work_user_list_from_documents(
  326. 0,
  327. $limit,
  328. $sidx,
  329. $sord,
  330. $work_id,
  331. api_get_user_id(),
  332. $whereCondition,
  333. true
  334. );
  335. }
  336. break;
  337. case 'get_work_student_list_overview':
  338. if (!(api_is_allowed_to_edit() || api_is_coach())) {
  339. return 0;
  340. }
  341. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  342. $workId = isset($_GET['work_id']) ? $_GET['work_id'] : null;
  343. $count = getWorkUserListData(
  344. $workId,
  345. api_get_course_id(),
  346. api_get_session_id(),
  347. api_get_group_id(),
  348. 0,
  349. $limit,
  350. null,
  351. null,
  352. true
  353. );
  354. break;
  355. case 'get_exercise_results':
  356. $exercise_id = $_REQUEST['exerciseId'];
  357. if (isset($_GET['filter_by_user']) && !empty($_GET['filter_by_user'])) {
  358. $filter_user = intval($_GET['filter_by_user']);
  359. $whereCondition .= " AND te.exe_user_id = '$filter_user'";
  360. }
  361. if (!empty($whereCondition)) {
  362. $whereCondition = " AND $whereCondition";
  363. }
  364. $count = ExerciseLib::get_count_exam_results($exercise_id, $whereCondition);
  365. break;
  366. case 'get_hotpotatoes_exercise_results':
  367. $hotpot_path = $_REQUEST['path'];
  368. $count = ExerciseLib::get_count_exam_hotpotatoes_results($hotpot_path);
  369. break;
  370. case 'get_sessions_tracking':
  371. $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
  372. $description = '';
  373. $setting = api_get_setting('show_session_description');
  374. if ($setting === 'true') {
  375. $description = $keyword;
  376. }
  377. if (api_is_drh()) {
  378. $count = SessionManager::get_sessions_followed_by_drh(
  379. api_get_user_id(),
  380. null,
  381. null,
  382. true,
  383. false,
  384. false,
  385. null,
  386. $keyword,
  387. $description
  388. );
  389. } else {
  390. // Sessions for the coach
  391. $count = Tracking::get_sessions_coached_by_user(
  392. api_get_user_id(),
  393. null,
  394. null,
  395. true,
  396. $keyword,
  397. $description
  398. );
  399. }
  400. break;
  401. case 'get_sessions':
  402. $list_type = isset($_REQUEST['list_type']) ? $_REQUEST['list_type'] : 'simple';
  403. if ($list_type == 'simple') {
  404. $count = SessionManager::get_sessions_admin(
  405. array('where' => $whereCondition, 'extra' => $extra_fields),
  406. true
  407. );
  408. } else {
  409. $count = SessionManager::get_count_admin_complete(
  410. array('where' => $whereCondition, 'extra' => $extra_fields)
  411. );
  412. }
  413. break;
  414. case 'get_session_lp_progress':
  415. case 'get_session_progress':
  416. //@TODO replace this for a more efficient function (not retrieving the whole data)
  417. $course = api_get_course_info_by_id($_GET['course_id']);
  418. $users = CourseManager::get_student_list_from_course_code($course['code'], true, $_GET['session_id'], $_GET['date_from'], $_GET['date_to']);
  419. $count = count($users);
  420. break;
  421. case 'get_exercise_progress':
  422. //@TODO replace this for a more efficient function (not retrieving the whole data)
  423. $records = Tracking::get_exercise_progress($_GET['session_id'], $_GET['course_id'], $_GET['exercise_id'], $_GET['date_from'], $_GET['date_to']);
  424. $count = count($records);
  425. break;
  426. case 'get_session_access_overview':
  427. //@TODO replace this for a more efficient function (not retrieving the whole data)
  428. $records = SessionManager::get_user_data_access_tracking_overview($_GET['session_id'], $_GET['course_id'], $_GET['student_id'], $_GET['profile'], $_GET['date_from'], $_GET['date_to'], $options);
  429. $count = count($records);
  430. break;
  431. case 'get_survey_overview':
  432. //@TODO replace this for a more efficient function (not retrieving the whole data)
  433. $records = SessionManager::get_survey_overview($_GET['session_id'], $_GET['course_id'], $_GET['survey_id'], $_GET['date_from'], $_GET['date_to'], $options);
  434. $count = count($records);
  435. break;
  436. case 'get_exercise_grade':
  437. //@TODO replace this for a more efficient function (not retrieving the whole data)
  438. $course = api_get_course_info_by_id($_GET['course_id']);
  439. $users = CourseManager::get_student_list_from_course_code($course['code'], true, $_GET['session_id']);
  440. $count = count($users);
  441. break;
  442. case 'get_extra_fields':
  443. $type = $_REQUEST['type'];
  444. $obj = new ExtraField($type);
  445. $count = $obj->get_count();
  446. break;
  447. case 'get_extra_field_options':
  448. $type = $_REQUEST['type'];
  449. $field_id = $_REQUEST['field_id'];
  450. $obj = new ExtraFieldOption($type);
  451. $count = $obj->get_count_by_field_id($field_id);
  452. break;
  453. case 'get_timelines':
  454. require_once $libpath.'timeline.lib.php';
  455. $obj = new Timeline();
  456. $count = $obj->get_count();
  457. break;
  458. case 'get_gradebooks':
  459. require_once $libpath.'gradebook.lib.php';
  460. $obj = new Gradebook();
  461. $count = $obj->get_count();
  462. break;
  463. case 'get_event_email_template':
  464. $obj = new EventEmailTemplate();
  465. $count = $obj->get_count();
  466. break;
  467. case 'get_careers':
  468. $obj = new Career();
  469. $count = $obj->get_count();
  470. break;
  471. case 'get_promotions':
  472. $obj = new Promotion();
  473. $count = $obj->get_count();
  474. break;
  475. case 'get_grade_models':
  476. $obj = new GradeModel();
  477. $count = $obj->get_count();
  478. break;
  479. case 'get_usergroups':
  480. $obj = new UserGroup();
  481. $count = $obj->get_count();
  482. break;
  483. case 'get_usergroups_teacher':
  484. $obj = new UserGroup();
  485. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'registered';
  486. $groupFilter = isset($_REQUEST['group_filter']) ? intval($_REQUEST['group_filter']) : 0;
  487. $course_id = api_get_course_int_id();
  488. if ($type == 'registered') {
  489. $count = $obj->getUserGroupByCourseWithDataCount($course_id, $groupFilter);
  490. } else {
  491. $count = $obj->get_count($groupFilter);
  492. }
  493. break;
  494. default:
  495. exit;
  496. }
  497. //3. Calculating first, end, etc
  498. $total_pages = 0;
  499. if ($count > 0) {
  500. if (!empty($limit)) {
  501. $total_pages = ceil((float)$count/(float)$limit);
  502. }
  503. }
  504. if ($page > $total_pages) {
  505. $page = $total_pages;
  506. }
  507. $start = $limit * $page - $limit;
  508. if ($start < 0) {
  509. $start = 0;
  510. }
  511. //4. Deleting an element if the user wants to
  512. if (isset($_REQUEST['oper']) && $_REQUEST['oper'] == 'del') {
  513. $obj->delete($_REQUEST['id']);
  514. }
  515. $is_allowedToEdit = api_is_allowed_to_edit(null, true) || api_is_allowed_to_edit(true) || api_is_drh();
  516. //5. Querying the DB for the elements
  517. $columns = array();
  518. switch ($action) {
  519. case 'get_group_reporting':
  520. $columns = array('name', 'time', 'progress', 'score', 'works', 'messages', 'actions');
  521. $result = Tracking::get_group_reporting(
  522. $course_id,
  523. $group_id,
  524. 'all',
  525. $start,
  526. $limit,
  527. $sidx,
  528. $sord,
  529. $whereCondition
  530. );
  531. break;
  532. case 'get_course_exercise_medias':
  533. $columns = array('question');
  534. $result = Question::get_course_medias(
  535. $course_id,
  536. $start,
  537. $limit,
  538. $sidx,
  539. $sord,
  540. $whereCondition
  541. );
  542. break;
  543. case 'get_user_course_report_resumed':
  544. $columns = array(
  545. 'extra_ruc',
  546. 'training_hours',
  547. 'count_users',
  548. 'count_users_registered',
  549. 'average_hours_per_user',
  550. 'count_certificates'
  551. );
  552. $column_names = array(
  553. get_lang('Company'),
  554. get_lang('TrainingHoursAccumulated'),
  555. get_lang('CountOfSubscriptions'),
  556. get_lang('CountOfUsers'),
  557. get_lang('AverageHoursPerStudent'),
  558. get_lang('CountCertificates')
  559. );
  560. $extra_fields = UserManager::get_extra_fields(0, 100, null, null, true, true);
  561. if (!empty($extra_fields)) {
  562. foreach ($extra_fields as $extra) {
  563. if ($extra['1'] == 'ruc') {
  564. continue;
  565. }
  566. $columns[] = $extra['1'];
  567. $column_names[] = $extra['3'];
  568. }
  569. }
  570. if (!in_array($sidx, array('training_hours'))) {
  571. //$sidx = 'training_hours';
  572. }
  573. if (api_is_student_boss() && empty($userIdList)) {
  574. $result = [];
  575. break;
  576. }
  577. $result = CourseManager::get_user_list_from_course_code(
  578. null,
  579. null,
  580. "LIMIT $start, $limit",
  581. null, //" $sidx $sord",
  582. null,
  583. null,
  584. true,
  585. true,
  586. array('ruc'),
  587. $courseCodeList,
  588. $userIdList,
  589. null,
  590. $sessionIdList
  591. );
  592. $new_result = array();
  593. if (!empty($result)) {
  594. foreach ($result as $row) {
  595. $row['training_hours'] = api_time_to_hms($row['training_hours']);
  596. $row['average_hours_per_user'] = api_time_to_hms($row['average_hours_per_user']);
  597. $new_result[] = $row;
  598. }
  599. $result = $new_result;
  600. }
  601. break;
  602. case 'get_user_course_report':
  603. $columns = array('course', 'user', 'time', 'certificate', 'progress_100', 'progress');
  604. $column_names = array(
  605. get_lang('Course'),
  606. get_lang('User'),
  607. get_lang('ManHours'),
  608. get_lang('CertificateGenerated'),
  609. get_lang('Approved'),
  610. get_lang('CourseAdvance')
  611. );
  612. $extra_fields = UserManager::get_extra_fields(0, 100, null, null, true, true);
  613. if (!empty($extra_fields)) {
  614. foreach ($extra_fields as $extra) {
  615. $columns[] = $extra['1'];
  616. $column_names[] = $extra['3'];
  617. }
  618. }
  619. if (api_is_student_boss()) {
  620. $columns[] = 'group';
  621. $column_names[] = get_lang('Group');
  622. }
  623. if (!in_array($sidx, array('title'))) {
  624. $sidx = 'title';
  625. }
  626. if (api_is_student_boss() && empty($userIdList)) {
  627. $result = [];
  628. break;
  629. }
  630. $result = CourseManager::get_user_list_from_course_code(
  631. null,
  632. null,
  633. "LIMIT $start, $limit",
  634. " $sidx $sord",
  635. null,
  636. null,
  637. true,
  638. false,
  639. null,
  640. $courseCodeList,
  641. $userIdList,
  642. null,
  643. $sessionIdList
  644. );
  645. if (api_is_student_boss()) {
  646. $userGroup = new UserGroup();
  647. foreach ($result as &$item) {
  648. $userGroups = $userGroup->get_groups_by_user($item['user_id']);
  649. $item['group'] = implode(", ", array_column($userGroups, 'name'));
  650. unset($item['user_id']);
  651. }
  652. }
  653. break;
  654. case 'get_user_skill_ranking':
  655. $columns = array('photo', 'firstname', 'lastname', 'skills_acquired', 'currently_learning', 'rank');
  656. $result = $skill->get_user_list_skill_ranking($start, $limit, $sidx, $sord, $whereCondition);
  657. $result = msort($result, 'skills_acquired', 'asc');
  658. $skills_in_course = array();
  659. if (!empty($result)) {
  660. foreach ($result as &$item) {
  661. $user_info = api_get_user_info($item['user_id']);
  662. $personal_course_list = UserManager::get_personal_session_course_list($item['user_id']);
  663. $count_skill_by_course = array();
  664. foreach ($personal_course_list as $course_item) {
  665. if (!isset($skills_in_course[$course_item['code']])) {
  666. $count_skill_by_course[$course_item['code']] = $skill->get_count_skills_by_course($course_item['code']);
  667. $skills_in_course[$course_item['code']] = $count_skill_by_course[$course_item['code']];
  668. } else {
  669. $count_skill_by_course[$course_item['code']] = $skills_in_course[$course_item['code']];
  670. }
  671. }
  672. $item['photo'] = Display::img($user_info['avatar_small']);
  673. $item['currently_learning'] = !empty($count_skill_by_course) ? array_sum($count_skill_by_course) : 0;
  674. }
  675. }
  676. break;
  677. case 'get_work_teacher':
  678. $columns = array(
  679. 'type',
  680. 'title',
  681. 'sent_date',
  682. 'expires_on',
  683. 'amount',
  684. 'actions'
  685. );
  686. $result = getWorkListTeacher($start, $limit, $sidx, $sord, $whereCondition);
  687. break;
  688. case 'get_work_student':
  689. $columns = array(
  690. 'type',
  691. 'title',
  692. 'expires_on',
  693. 'feedback',
  694. 'last_upload',
  695. 'others'
  696. );
  697. $result = getWorkListStudent($start, $limit, $sidx, $sord, $whereCondition);
  698. break;
  699. case 'get_work_user_list_all':
  700. if (isset($_GET['type']) && $_GET['type'] == 'simple') {
  701. $columns = array(
  702. //'type',
  703. 'firstname',
  704. 'lastname',
  705. 'title',
  706. 'qualification',
  707. 'sent_date',
  708. 'qualificator_id',
  709. 'correction',
  710. 'actions'
  711. );
  712. } else {
  713. $columns = array(
  714. //'type',
  715. 'firstname',
  716. 'lastname',
  717. 'title',
  718. 'qualification',
  719. 'sent_date',
  720. 'correction',
  721. 'actions'
  722. );
  723. }
  724. $result = get_work_user_list($start, $limit, $sidx, $sord, $work_id, $whereCondition);
  725. break;
  726. case 'get_work_user_list_others':
  727. if (isset($_GET['type']) && $_GET['type'] == 'simple') {
  728. $columns = array(
  729. 'type', 'firstname', 'lastname', 'title', 'qualification', 'sent_date', 'qualificator_id', 'actions'
  730. );
  731. } else {
  732. $columns = array('type', 'firstname', 'lastname', 'title', 'sent_date', 'actions');
  733. }
  734. $whereCondition .= " AND u.user_id <> ".api_get_user_id();
  735. $result = get_work_user_list($start, $limit, $sidx, $sord, $work_id, $whereCondition);
  736. break;
  737. case 'get_work_user_list':
  738. if (isset($_GET['type']) && $_GET['type'] == 'simple') {
  739. $columns = array(
  740. 'type', 'title', 'qualification', 'sent_date', 'qualificator_id', 'actions'
  741. );
  742. } else {
  743. $columns = array('type', 'title', 'qualification', 'sent_date', 'actions');
  744. }
  745. $documents = getAllDocumentToWork($work_id, api_get_course_int_id());
  746. if (empty($documents)) {
  747. $whereCondition .= " AND u.user_id = ".api_get_user_id();
  748. $result = get_work_user_list($start, $limit, $sidx, $sord, $work_id, $whereCondition);
  749. } else {
  750. $result = get_work_user_list_from_documents(
  751. $start,
  752. $limit,
  753. $sidx,
  754. $sord,
  755. $work_id,
  756. api_get_user_id(),
  757. $whereCondition
  758. );
  759. }
  760. break;
  761. case 'get_exercise_results':
  762. $course = api_get_course_info();
  763. // Used inside ExerciseLib::get_exam_results_data()
  764. $documentPath = api_get_path(SYS_COURSE_PATH) . $course['path'] . "/document";
  765. if ($is_allowedToEdit || api_is_student_boss()) {
  766. $columns = array(
  767. 'firstname', 'lastname', 'username', 'group_name', 'exe_duration', 'start_date', 'exe_date', 'score', 'user_ip', 'status', 'lp', 'actions'
  768. );
  769. $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
  770. if ($officialCodeInList === 'true') {
  771. $columns = array_merge(array('official_code'), $columns);
  772. }
  773. }
  774. $result = ExerciseLib::get_exam_results_data($start, $limit, $sidx, $sord, $exercise_id, $whereCondition);
  775. break;
  776. case 'get_hotpotatoes_exercise_results':
  777. $course = api_get_course_info();
  778. $documentPath = api_get_path(SYS_COURSE_PATH) . $course['path'] . "/document";
  779. if (api_is_allowed_to_edit()) {
  780. $columns = array('firstname', 'lastname', 'username', 'group_name', 'exe_date', 'score', 'actions');
  781. } else {
  782. $columns = array('exe_date', 'score', 'actions');
  783. }
  784. $result = ExerciseLib::get_exam_results_hotpotatoes_data($start, $limit, $sidx, $sord, $hotpot_path, $whereCondition);
  785. break;
  786. case 'get_work_student_list_overview':
  787. if (!(api_is_allowed_to_edit() || api_is_coach())) {
  788. return array();
  789. }
  790. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  791. $columns = array(
  792. 'student', 'works'
  793. );
  794. $result = getWorkUserListData(
  795. $workId,
  796. api_get_course_id(),
  797. api_get_session_id(),
  798. api_get_group_id(),
  799. $start,
  800. $limit,
  801. $sidx,
  802. $sord
  803. );
  804. break;
  805. case 'get_hotpotatoes_exercise_results':
  806. $course = api_get_course_info();
  807. $documentPath = api_get_path(SYS_COURSE_PATH) . $course['path'] . "/document";
  808. if (api_is_allowed_to_edit(null, true) || api_is_drh()) {
  809. $columns = array('firstname', 'lastname', 'username', 'group_name', 'exe_date', 'score', 'actions');
  810. } else {
  811. $columns = array('exe_date', 'score', 'actions');
  812. }
  813. $result = ExerciseLib::get_exam_results_hotpotatoes_data($start, $limit, $sidx, $sord, $hotpot_path, $whereCondition);
  814. break;
  815. case 'get_sessions_tracking':
  816. if (api_is_drh()) {
  817. $sessions = SessionManager::get_sessions_followed_by_drh(
  818. api_get_user_id(),
  819. $start,
  820. $limit,
  821. false,
  822. false,
  823. false,
  824. null,
  825. $keyword,
  826. $description
  827. );
  828. } else {
  829. // Sessions for the coach
  830. $sessions = Tracking::get_sessions_coached_by_user(
  831. api_get_user_id(),
  832. $start,
  833. $limit,
  834. false,
  835. $keyword,
  836. $description
  837. );
  838. }
  839. $columns = array(
  840. 'name',
  841. 'date',
  842. 'course_per_session',
  843. 'student_per_session',
  844. 'details'
  845. );
  846. $result = array();
  847. if (!empty($sessions)) {
  848. foreach ($sessions as $session) {
  849. if (api_drh_can_access_all_session_content()) {
  850. $count_courses_in_session = count(SessionManager::get_course_list_by_session_id($session['id']));
  851. } else {
  852. $count_courses_in_session = count(Tracking::get_courses_followed_by_coach($user_id, $session['id']));
  853. }
  854. $count_users_in_session = count(SessionManager::get_users_by_session($session['id'], 0));
  855. $session_date = array();
  856. if (!empty($session['access_start_date']) && $session['access_start_date'] != '0000-00-00') {
  857. $session_date[] = get_lang('From').' '.api_format_date($session['access_start_date'], DATE_FORMAT_SHORT);
  858. }
  859. if (!empty($session['access_end_date']) && $session['access_end_date'] != '0000-00-00') {
  860. $session_date[] = get_lang('Until').' '.api_format_date($session['access_end_date'], DATE_FORMAT_SHORT);
  861. }
  862. if (empty($session_date)) {
  863. $session_date_string = '-';
  864. } else {
  865. $session_date_string = implode(' ', $session_date);
  866. }
  867. $sessionUrl = api_get_path(WEB_CODE_PATH).'mySpace/course.php?session_id='.$session['id'];
  868. $result[] = array(
  869. 'name' => $session['name'],
  870. 'date' => $session_date_string,
  871. 'course_per_session' => $count_courses_in_session,
  872. 'student_per_session' => $count_users_in_session,
  873. 'details' => Display::url(Display::return_icon('2rightarrow.gif'), $sessionUrl)
  874. );
  875. }
  876. }
  877. break;
  878. case 'get_sessions':
  879. $session_columns = SessionManager::getGridColumns($list_type);
  880. $columns = $session_columns['simple_column_name'];
  881. if ($list_type == 'simple') {
  882. $result = SessionManager::get_sessions_admin(
  883. array(
  884. 'where' => $whereCondition,
  885. 'order' => "$sidx $sord",
  886. 'extra' => $extra_fields,
  887. 'limit' => "$start , $limit",
  888. ),
  889. false
  890. );
  891. } else {
  892. $result = SessionManager::get_sessions_admin_complete(
  893. array(
  894. 'where' => $whereCondition,
  895. 'order' => "$sidx $sord",
  896. 'extra' => $extra_fields,
  897. 'limit' => "$start , $limit",
  898. )
  899. );
  900. }
  901. break;
  902. /*
  903. $columns = array(
  904. 'name',
  905. 'nbr_courses',
  906. 'nbr_users',
  907. 'category_name',
  908. 'access_start_date',
  909. 'access_end_date',
  910. 'coach_name',
  911. 'session_active',
  912. 'visibility'
  913. );
  914. if (SessionManager::allowToManageSessions()) {
  915. if (SessionManager::allowOnlyMySessions()) {
  916. $whereCondition .= ' AND s.id_coach = '.api_get_user_id();
  917. }
  918. // Rename Category_name
  919. $whereCondition = str_replace(
  920. 'category_name',
  921. 'sc.name',
  922. $whereCondition
  923. );
  924. $result = SessionManager::get_sessions_admin(
  925. array(
  926. 'where' => $whereCondition,
  927. 'order' => "$sidx $sord",
  928. 'limit' => "$start , $limit"
  929. )
  930. );
  931. }
  932. */
  933. break;
  934. case 'get_exercise_progress':
  935. $sessionId = intval($_GET['session_id']);
  936. $courseId = intval($_GET['course_id']);
  937. $exerciseId = intval($_GET['exercise_id']);
  938. $date_from = $_GET['date_from'];
  939. $date_to = $_GET['date_to'];
  940. $columns = array(
  941. 'session',
  942. 'exercise_id',
  943. 'quiz_title',
  944. 'username',
  945. 'lastname',
  946. 'firstname',
  947. 'time',
  948. 'question_id',
  949. 'question',
  950. 'description',
  951. 'answer',
  952. 'correct',
  953. );
  954. $result = Tracking::get_exercise_progress(
  955. $sessionId,
  956. $courseId,
  957. $exerciseId,
  958. $date_from,
  959. $date_to,
  960. array(
  961. 'where' => $whereCondition,
  962. 'order' => "$sidx $sord",
  963. 'limit'=> "$start , $limit"
  964. )
  965. );
  966. break;
  967. case 'get_session_lp_progress':
  968. $sessionId = 0;
  969. if (!empty($_GET['session_id']) && !empty($_GET['course_id'])) {
  970. $sessionId = intval($_GET['session_id']);
  971. $courseId = intval($_GET['course_id']);
  972. $course = api_get_course_info_by_id($courseId);
  973. }
  974. /**
  975. * Add lessons of course
  976. *
  977. */
  978. $columns = array(
  979. 'username',
  980. 'firstname',
  981. 'lastname',
  982. );
  983. $lessons = LearnpathList::get_course_lessons($course['code'], $sessionId);
  984. foreach ($lessons as $lesson_id => $lesson) {
  985. $columns[] = $lesson_id;
  986. }
  987. $columns[] = 'total';
  988. $result = SessionManager::get_session_lp_progress($sessionId, $courseId, $date_from, $date_to,
  989. array(
  990. 'where' => $whereCondition,
  991. 'order' => "$sidx $sord",
  992. 'limit'=> "$start , $limit"
  993. )
  994. );
  995. break;
  996. case 'get_survey_overview':
  997. $sessionId = 0;
  998. if (!empty($_GET['session_id']) &&
  999. !empty($_GET['course_id']) &&
  1000. !empty($_GET['survey_id'])
  1001. ) {
  1002. $sessionId = intval($_GET['session_id']);
  1003. $courseId = intval($_GET['course_id']);
  1004. $surveyId = intval($_GET['survey_id']);
  1005. $date_from = $_GET['date_from'];
  1006. $date_to = $_GET['date_to'];
  1007. //$course = api_get_course_info_by_id($courseId);
  1008. }
  1009. /**
  1010. * Add lessons of course
  1011. */
  1012. $columns = array(
  1013. 'username',
  1014. 'firstname',
  1015. 'lastname',
  1016. );
  1017. $questions = SurveyManager::get_questions($surveyId, $courseId);
  1018. foreach ($questions as $question_id => $question)
  1019. {
  1020. $columns[] = $question_id;
  1021. }
  1022. $result = SessionManager::get_survey_overview($sessionId, $courseId, $surveyId, $date_from, $date_to,
  1023. array(
  1024. 'where' => $whereCondition,
  1025. 'order' => "$sidx $sord",
  1026. 'limit'=> "$start , $limit"
  1027. )
  1028. );
  1029. break;
  1030. case 'get_session_progress':
  1031. $columns = array(
  1032. 'lastname',
  1033. 'firstname',
  1034. 'username',
  1035. #'profile',
  1036. 'total',
  1037. 'courses',
  1038. 'lessons',
  1039. 'exercises',
  1040. 'forums',
  1041. 'homeworks',
  1042. 'wikis',
  1043. 'surveys',
  1044. //exercises
  1045. 'lessons_total' ,
  1046. 'lessons_done' ,
  1047. 'lessons_left' ,
  1048. 'lessons_progress',
  1049. //exercises
  1050. 'exercises_total' ,
  1051. 'exercises_done' ,
  1052. 'exercises_left' ,
  1053. 'exercises_progress' ,
  1054. //forums
  1055. 'forums_total' ,
  1056. 'forums_done' ,
  1057. 'forums_left' ,
  1058. 'forums_progress' ,
  1059. //assignments
  1060. 'assignments_total' ,
  1061. 'assignments_done' ,
  1062. 'assignments_left' ,
  1063. 'assignments_progress' ,
  1064. //Wiki
  1065. 'wiki_total',
  1066. 'wiki_revisions',
  1067. 'wiki_read',
  1068. 'wiki_unread',
  1069. 'wiki_progress',
  1070. //surveys
  1071. 'surveys_total' ,
  1072. 'surveys_done' ,
  1073. 'surveys_left' ,
  1074. 'surveys_progress' ,
  1075. );
  1076. $sessionId = 0;
  1077. if (!empty($_GET['course_id']) && !empty($_GET['session_id'])) {
  1078. $sessionId = intval($_GET['session_id']);
  1079. $courseId = intval($_GET['course_id']);
  1080. }
  1081. $result = SessionManager::get_session_progress(
  1082. $sessionId,
  1083. $courseId,
  1084. null,
  1085. null,
  1086. array(
  1087. 'where' => $whereCondition,
  1088. 'order' => "$sidx $sord",
  1089. 'limit'=> "$start , $limit"
  1090. )
  1091. );
  1092. break;
  1093. case 'get_session_access_overview':
  1094. $columns = array(
  1095. 'logindate',
  1096. 'username',
  1097. 'lastname',
  1098. 'firstname',
  1099. 'clicks',
  1100. 'ip',
  1101. 'timeLoggedIn',
  1102. 'session'
  1103. );
  1104. $sessionId = 0;
  1105. if (!empty($_GET['course_id']) && !empty($_GET['session_id'])) {
  1106. $sessionId = intval($_GET['session_id']);
  1107. $courseId = intval($_GET['course_id']);
  1108. $studentId = intval($_GET['student_id']);
  1109. $profile = intval($_GET['profile']);
  1110. $date_from = intval($_GET['date_from']);
  1111. $date_to = intval($_GET['date_to']);
  1112. }
  1113. $result = SessionManager::get_user_data_access_tracking_overview(
  1114. $sessionId,
  1115. $courseId,
  1116. $studentId,
  1117. $profile,
  1118. $date_to,
  1119. $date_from,
  1120. array(
  1121. 'where' => $whereCondition,
  1122. 'order' => "$sidx $sord",
  1123. 'limit'=> "$start , $limit"
  1124. )
  1125. );
  1126. break;
  1127. case 'get_timelines':
  1128. $columns = array('headline', 'actions');
  1129. if (!in_array($sidx, $columns)) {
  1130. $sidx = 'headline';
  1131. }
  1132. $course_id = api_get_course_int_id();
  1133. $result = Database::select(
  1134. '*',
  1135. $obj->table,
  1136. array(
  1137. 'where' => array(
  1138. 'parent_id = ? AND c_id = ?' => array('0', $course_id)
  1139. ),
  1140. 'order'=>"$sidx $sord",
  1141. 'LIMIT'=> "$start , $limit"
  1142. )
  1143. );
  1144. $new_result = array();
  1145. foreach ($result as $item) {
  1146. if (!$item['status']) {
  1147. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  1148. }
  1149. $item['headline'] = Display::url($item['headline'], api_get_path(WEB_CODE_PATH).'timeline/view.php?id='.$item['id']);
  1150. $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']);
  1151. $item['actions'] .= Display::url(Display::return_icon('edit.png', get_lang('Edit')), api_get_path(WEB_CODE_PATH).'timeline/?action=edit&id='.$item['id']);
  1152. $item['actions'] .= Display::url(Display::return_icon('delete.png', get_lang('Delete')), api_get_path(WEB_CODE_PATH).'timeline/?action=delete&id='.$item['id']);
  1153. $new_result[] = $item;
  1154. }
  1155. $result = $new_result;
  1156. break;
  1157. case 'get_gradebooks':
  1158. $columns = array('name', 'certificates','skills', 'actions', 'has_certificates');
  1159. if (!in_array($sidx, $columns)) {
  1160. $sidx = 'name';
  1161. }
  1162. $result = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  1163. $new_result = array();
  1164. foreach ($result as $item) {
  1165. if ($item['parent_id'] != 0) {
  1166. continue;
  1167. }
  1168. $skills = $obj->get_skills_by_gradebook($item['id']);
  1169. //Fixes bug when gradebook doesn't have names
  1170. if (empty($item['name'])) {
  1171. $item['name'] = $item['course_code'];
  1172. }
  1173. $item['name'] = Display::url($item['name'], api_get_path(WEB_CODE_PATH).'gradebook/index.php?id_session=0&cidReq='.$item['course_code']);
  1174. if (!empty($item['certif_min_score']) && !empty($item['document_id'])) {
  1175. $item['certificates'] = Display::return_icon('accept.png', get_lang('WithCertificate'), array(), ICON_SIZE_SMALL);
  1176. $item['has_certificates'] = '1';
  1177. } else {
  1178. $item['certificates'] = Display::return_icon('warning.png', get_lang('NoCertificate'), array(), ICON_SIZE_SMALL);
  1179. $item['has_certificates'] = '0';
  1180. }
  1181. if (!empty($skills)) {
  1182. $item['skills'] = '';
  1183. foreach ($skills as $skill) {
  1184. $item['skills'] .= Display::span($skill['name'], array('class' => 'label_tag skill'));
  1185. }
  1186. }
  1187. $new_result[] = $item;
  1188. }
  1189. $result = $new_result;
  1190. break;
  1191. case 'get_event_email_template':
  1192. $columns = array('subject', 'event_type_name', 'language_id', 'activated', 'actions');
  1193. if (!in_array($sidx, $columns)) {
  1194. $sidx = 'subject';
  1195. }
  1196. $result = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  1197. $new_result = array();
  1198. foreach ($result as $item) {
  1199. $language_info = api_get_language_info($item['language_id']);
  1200. $item['language_id'] = $language_info['english_name'];
  1201. $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']);
  1202. $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']);
  1203. $new_result[] = $item;
  1204. }
  1205. $result = $new_result;
  1206. break;
  1207. case 'get_careers':
  1208. $columns = array('name', 'description', 'actions');
  1209. if (!in_array($sidx, $columns)) {
  1210. $sidx = 'name';
  1211. }
  1212. $result = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  1213. $new_result = array();
  1214. foreach ($result as $item) {
  1215. if (!$item['status']) {
  1216. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  1217. }
  1218. $new_result[] = $item;
  1219. }
  1220. $result = $new_result;
  1221. break;
  1222. case 'get_promotions':
  1223. $columns = array('name', 'career', 'description', 'actions');
  1224. if (!in_array($sidx, $columns)) {
  1225. $sidx = 'name';
  1226. }
  1227. $result = Database::select(
  1228. 'p.id,p.name, p.description, c.name as career, p.status',
  1229. "$obj->table p LEFT JOIN ".Database::get_main_table(TABLE_CAREER)." c ON c.id = p.career_id ",
  1230. array('order' => "$sidx $sord", 'LIMIT'=> "$start , $limit")
  1231. );
  1232. $new_result = array();
  1233. foreach ($result as $item) {
  1234. if (!$item['status']) {
  1235. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  1236. }
  1237. $new_result[] = $item;
  1238. }
  1239. $result = $new_result;
  1240. break;
  1241. case 'get_grade_models':
  1242. $columns = array('name', 'description', 'actions');
  1243. if (!in_array($sidx, $columns)) {
  1244. $sidx = 'name';
  1245. }
  1246. $result = Database::select('*', "$obj->table ", array('order' =>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
  1247. $new_result = array();
  1248. foreach ($result as $item) {
  1249. $new_result[] = $item;
  1250. }
  1251. $result = $new_result;
  1252. break;
  1253. case 'get_usergroups':
  1254. $columns = array('name', 'users', 'courses', 'sessions', 'group_type', 'actions');
  1255. $result = $obj->getUsergroupsPagination($sidx, $sord, $start, $limit);
  1256. break;
  1257. case 'get_extra_fields':
  1258. $obj = new ExtraField($type);
  1259. $columns = array(
  1260. 'display_text',
  1261. 'variable',
  1262. 'field_type',
  1263. 'changeable',
  1264. 'visible',
  1265. 'filter',
  1266. 'field_order',
  1267. );
  1268. $result = $obj->getAllGrid($sidx, $sord, $start, $limit);
  1269. /*$result = Database::select(
  1270. '*',
  1271. $obj->table,
  1272. array('order' => "$sidx $sord", 'LIMIT' => "$start , $limit")
  1273. );*/
  1274. $new_result = array();
  1275. if (!empty($result)) {
  1276. foreach ($result as $item) {
  1277. $item['display_text'] = $item['displayText'];
  1278. $item['field_type'] = $obj->get_field_type_by_id($item['fieldType']);
  1279. $item['changeable'] = $item['changeable'] ? Display::return_icon('right.gif') : Display::return_icon('wrong.gif');
  1280. $item['visible'] = $item['visible'] ? Display::return_icon('right.gif') : Display::return_icon('wrong.gif');
  1281. $item['filter'] = $item['filter'] ? Display::return_icon('right.gif') : Display::return_icon('wrong.gif');
  1282. $new_result[] = $item;
  1283. }
  1284. $result = $new_result;
  1285. }
  1286. break;
  1287. case 'get_exercise_grade':
  1288. $objExercise = new Exercise();
  1289. $exercises = $objExercise->getExercisesByCouseSession($_GET['course_id'], $_GET['session_id']);
  1290. $cntExer = 4;
  1291. if (!empty($exercises)) {
  1292. $cntExer += count($exercises);
  1293. }
  1294. $columns = array();
  1295. //Get dynamic column names
  1296. $i = 1;
  1297. $column_names = array();
  1298. foreach (range(1, $cntExer) as $cnt) {
  1299. switch ($cnt) {
  1300. case 1:
  1301. $columns[] = 'session';
  1302. $column_names[] = get_lang('Section');
  1303. break;
  1304. case 2:
  1305. $columns[] = 'username';
  1306. $column_names[] = get_lang('Username');
  1307. break;
  1308. case 3:
  1309. $columns[] = 'name';
  1310. $column_names[] = get_lang('Name');
  1311. break;
  1312. case $cntExer:
  1313. $columns[] = 'finalScore';
  1314. $column_names[] = get_lang('FinalScore');
  1315. break;
  1316. default:
  1317. $title = "";
  1318. if (!empty($exercises[$cnt - 4]['title'])) {
  1319. $title = ucwords(strtolower(trim($exercises[$cnt - 4]['title'])));
  1320. }
  1321. $columns[] = 'exer' . $i;
  1322. $column_names[] = $title;
  1323. $i++;
  1324. break;
  1325. }
  1326. }
  1327. $quizIds = array();
  1328. if (!empty($exercises)) {
  1329. foreach($exercises as $exercise) {
  1330. $quizIds[] = $exercise['id'];
  1331. }
  1332. }
  1333. $course = api_get_course_info_by_id($_GET['course_id']);
  1334. $listUserSess = CourseManager::get_student_list_from_course_code($course['code'], true, $_GET['session_id']);
  1335. $usersId = array_keys($listUserSess);
  1336. $users = UserManager::get_user_list_by_ids($usersId, null, "lastname, firstname", "$start , $limit");
  1337. $exeResults = $objExercise->getExerciseAndResult($_GET['course_id'], $_GET['session_id'], $quizIds);
  1338. $arrGrade = array();
  1339. foreach ($exeResults as $exeResult) {
  1340. $arrGrade[$exeResult['exe_user_id']][$exeResult['exe_exo_id']] = $exeResult['exe_result'];
  1341. }
  1342. $result = array();
  1343. $i = 0;
  1344. foreach ($users as $user) {
  1345. $sessionInfo = SessionManager::fetch($listUserSess[$user['user_id']]['id_session']);
  1346. $result[$i]['session'] = $sessionInfo['name'];
  1347. $result[$i]['username'] = $user['username'];
  1348. $result[$i]['name'] = $user['lastname'] . " " . $user['firstname'];
  1349. $j = 1;
  1350. $finalScore = 0;
  1351. foreach ($quizIds as $quizID) {
  1352. $grade = "";
  1353. if (!empty($arrGrade [$user['user_id']][$quizID]) || $arrGrade [$user['user_id']][$quizID] == 0) {
  1354. $finalScore += $grade = $arrGrade [$user['user_id']][$quizID];
  1355. }
  1356. $result[$i]['exer' . $j] = $grade;
  1357. $j++;
  1358. }
  1359. if ($finalScore > 20) {
  1360. $finalScore = 20;
  1361. }
  1362. $result[$i]['finalScore'] = number_format($finalScore, 2);
  1363. $i++;
  1364. }
  1365. break;
  1366. case 'get_extra_field_options':
  1367. $obj = new ExtraFieldOption($type);
  1368. $columns = array('display_text', 'option_value', 'option_order');
  1369. $result = Database::select(
  1370. '*',
  1371. $obj->table,
  1372. array(
  1373. 'where' => array("field_id = ? " => $field_id),
  1374. 'order' => "$sidx $sord",
  1375. 'LIMIT' => "$start , $limit",
  1376. )
  1377. );
  1378. break;
  1379. case 'get_usergroups_teacher':
  1380. $columns = array('name', 'users', 'status', 'group_type', 'actions');
  1381. $options = array('order'=>"name $sord", 'LIMIT'=> "$start , $limit");
  1382. $options['course_id'] = $course_id;
  1383. switch ($type) {
  1384. case 'not_registered':
  1385. $options['where'] = array(" (course_id IS NULL OR course_id != ?) " => $course_id);
  1386. $result = $obj->getUserGroupNotInCourse($options, $groupFilter);
  1387. break;
  1388. case 'registered':
  1389. $options['where'] = array(" usergroup.course_id = ? " => $course_id);
  1390. $result = $obj->getUserGroupInCourse($options, $groupFilter);
  1391. break;
  1392. }
  1393. $new_result = array();
  1394. if (!empty($result)) {
  1395. foreach ($result as $group) {
  1396. $group['users'] = count($obj->get_users_by_usergroup($group['id']));
  1397. if ($obj->usergroup_was_added_in_course($group['id'], $course_id)) {
  1398. $url = 'class.php?action=remove_class_from_course&id='.$group['id'].'&'.api_get_cidreq();
  1399. //$icon = Display::return_icon('delete.png', get_lang('Remove'));
  1400. $class = 'btn btn-danger';
  1401. $text = get_lang('Remove');
  1402. } else {
  1403. $url = 'class.php?action=add_class_to_course&id='.$group['id'].'&'.api_get_cidreq();
  1404. $class = 'btn btn-primary';
  1405. //$icon = Display::return_icon('add.png', get_lang('Add'));
  1406. $text = get_lang('Add');
  1407. }
  1408. switch ($group['group_type']) {
  1409. case 0:
  1410. $group['group_type'] = Display::label(get_lang('Class'), 'info');
  1411. break;
  1412. case 1:
  1413. $group['group_type'] = Display::label(get_lang('Social'), 'success');
  1414. break;
  1415. }
  1416. $role = $obj->getUserRoleToString(api_get_user_id(), $group['id']);
  1417. $group['status'] = $role;
  1418. $group['actions'] = Display::url($text, $url, ['class' => $class]);
  1419. $new_result[] = $group;
  1420. }
  1421. $result = $new_result;
  1422. }
  1423. if (!in_array($sidx, $columns)) {
  1424. $sidx = 'name';
  1425. }
  1426. // Multidimensional sort
  1427. $result = msort($result, $sidx, $sord);
  1428. break;
  1429. default:
  1430. exit;
  1431. }
  1432. $allowed_actions = array(
  1433. 'get_careers',
  1434. 'get_promotions',
  1435. 'get_usergroups',
  1436. 'get_usergroups_teacher',
  1437. 'get_gradebooks',
  1438. 'get_sessions',
  1439. 'get_session_access_overview',
  1440. 'get_sessions_tracking',
  1441. 'get_session_lp_progress',
  1442. 'get_survey_overview',
  1443. 'get_session_progress',
  1444. 'get_exercise_progress',
  1445. 'get_exercise_results',
  1446. 'get_work_student_list_overview',
  1447. 'get_hotpotatoes_exercise_results',
  1448. 'get_work_teacher',
  1449. 'get_work_student',
  1450. 'get_work_user_list',
  1451. 'get_work_user_list_others',
  1452. 'get_work_user_list_all',
  1453. 'get_timelines',
  1454. 'get_grade_models',
  1455. 'get_event_email_template',
  1456. 'get_user_skill_ranking',
  1457. 'get_extra_fields',
  1458. 'get_extra_field_options',
  1459. //'get_course_exercise_medias',
  1460. 'get_user_course_report',
  1461. 'get_user_course_report_resumed',
  1462. 'get_exercise_grade',
  1463. 'get_group_reporting'
  1464. );
  1465. //5. Creating an obj to return a json
  1466. if (in_array($action, $allowed_actions)) {
  1467. $response = new stdClass();
  1468. $response->page = $page;
  1469. $response->total = $total_pages;
  1470. $response->records = $count;
  1471. if ($operation && $operation == 'excel') {
  1472. $j = 1;
  1473. $array = array();
  1474. if (empty($column_names)) {
  1475. $column_names = $columns;
  1476. }
  1477. //Headers
  1478. foreach ($column_names as $col) {
  1479. $array[0][] = $col;
  1480. }
  1481. foreach ($result as $row) {
  1482. foreach ($columns as $col) {
  1483. $array[$j][] = strip_tags($row[$col]);
  1484. }
  1485. $j++;
  1486. }
  1487. switch ($exportFormat) {
  1488. case 'xls':
  1489. //TODO add date if exists
  1490. $file_name = (!empty($action)) ? $action : 'company_report';
  1491. $browser = new Browser();
  1492. if ($browser->getPlatform() == Browser::PLATFORM_WINDOWS) {
  1493. Export::export_table_xls_html($array, $file_name, 'ISO-8859-15');
  1494. } else {
  1495. Export::export_table_xls_html($array, $file_name);
  1496. }
  1497. break;
  1498. case 'csv':
  1499. default:
  1500. //TODO add date if exists
  1501. $file_name = (!empty($action)) ? $action : 'company_report';
  1502. Export::arrayToCsv($array, $file_name);
  1503. break;
  1504. }
  1505. exit;
  1506. }
  1507. $i = 0;
  1508. if (!empty($result)) {
  1509. foreach ($result as $row) {
  1510. // if results tab give not id, set id to $i otherwise id="null" for all <tr> of the jqgrid - ref #4235
  1511. if (!isset($row['id']) || isset($row['id']) && $row['id'] == '') {
  1512. $response->rows[$i]['id']= $i;
  1513. } else {
  1514. $response->rows[$i]['id']= $row['id'];
  1515. }
  1516. $array = array();
  1517. foreach ($columns as $col) {
  1518. $array[] = isset($row[$col]) ? $row[$col] : null;
  1519. }
  1520. $response->rows[$i]['cell']=$array;
  1521. $i++;
  1522. }
  1523. }
  1524. echo json_encode($response);
  1525. }
  1526. exit;