model.ajax.php 64 KB

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