model.ajax.php 54 KB

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