model.ajax.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  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;