model.ajax.php 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. require_once __DIR__.'/../global.inc.php';
  5. // 1. Setting variables needed by jqgrid
  6. $action = $_GET['a'];
  7. $page = (int) $_REQUEST['page']; //page
  8. $limit = (int) $_REQUEST['rows']; //quantity of rows
  9. // Makes max row persistence after refreshing the grid
  10. $savedRows = Session::read('max_rows_'.$action);
  11. if (empty($savedRows)) {
  12. Session::write('max_rows_'.$action, $limit);
  13. } else {
  14. if ($limit != $savedRows) {
  15. Session::write('max_rows_'.$action, $limit);
  16. }
  17. }
  18. $sidx = $_REQUEST['sidx']; //index (field) to filter
  19. $sord = $_REQUEST['sord']; //asc or desc
  20. $exportFilename = isset($_REQUEST['export_filename']) ? $_REQUEST['export_filename'] : '';
  21. if (strpos(strtolower($sidx), 'asc') !== false) {
  22. $sidx = str_replace(['asc', ','], '', $sidx);
  23. $sord = 'asc';
  24. }
  25. if (strpos(strtolower($sidx), 'desc') !== false) {
  26. $sidx = str_replace(['desc', ','], '', $sidx);
  27. $sord = 'desc';
  28. }
  29. if (!in_array($sord, ['asc', 'desc'])) {
  30. $sord = 'desc';
  31. }
  32. // Actions allowed to other roles.
  33. if (!in_array(
  34. $action,
  35. [
  36. 'get_exercise_results',
  37. 'get_exercise_results_report',
  38. 'get_work_student_list_overview',
  39. 'get_hotpotatoes_exercise_results',
  40. 'get_work_teacher',
  41. 'get_work_student',
  42. 'get_work_user_list',
  43. 'get_work_user_list_others',
  44. 'get_work_user_list_all',
  45. 'get_timelines',
  46. 'get_user_skill_ranking',
  47. 'get_usergroups',
  48. 'get_usergroups_teacher',
  49. 'get_user_course_report_resumed',
  50. 'get_user_course_report',
  51. 'get_sessions_tracking',
  52. 'get_sessions',
  53. 'get_course_announcements',
  54. 'course_log_events',
  55. 'get_learning_path_calendars',
  56. 'get_usergroups_users',
  57. 'get_calendar_users',
  58. 'get_exercise_categories',
  59. ]
  60. ) && !isset($_REQUEST['from_course_session'])) {
  61. api_protect_admin_script(true);
  62. } elseif (isset($_REQUEST['from_course_session']) &&
  63. $_REQUEST['from_course_session'] == 1
  64. ) {
  65. api_protect_teacher_script(true);
  66. }
  67. $toRemove = ['extra_access_start_date', 'extra_access_end_date'];
  68. // Search features
  69. //@todo move this in the display_class or somewhere else
  70. /**
  71. * @param string $col
  72. * @param string $oper
  73. * @param string $val
  74. *
  75. * @return string
  76. */
  77. function getWhereClause($col, $oper, $val)
  78. {
  79. $ops = [
  80. 'eq' => '=', //equal
  81. 'ne' => '<>', //not equal
  82. 'lt' => '<', //less than
  83. 'le' => '<=', //less than or equal
  84. 'gt' => '>', //greater than
  85. 'ge' => '>=', //greater than or equal
  86. 'bw' => 'LIKE', //begins with
  87. 'bn' => 'NOT LIKE', //doesn't begin with
  88. 'in' => 'LIKE', //is in
  89. 'ni' => 'NOT LIKE', //is not in
  90. 'ew' => 'LIKE', //ends with
  91. 'en' => 'NOT LIKE', //doesn't end with
  92. 'cn' => 'LIKE', //contains
  93. 'nc' => 'NOT LIKE', //doesn't contain
  94. ];
  95. if (empty($col)) {
  96. return '';
  97. }
  98. if ($oper == 'bw' || $oper == 'bn') {
  99. $val .= '%';
  100. }
  101. if ($oper == 'ew' || $oper == 'en') {
  102. $val = '%'.$val;
  103. }
  104. if ($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') {
  105. $val = '%'.$val.'%';
  106. }
  107. $val = Database::escape_string($val);
  108. return " $col {$ops[$oper]} '$val' ";
  109. }
  110. // If there is no search request sent by jqgrid, $where should be empty
  111. $whereCondition = '';
  112. $operation = isset($_REQUEST['oper']) ? $_REQUEST['oper'] : false;
  113. $exportFormat = isset($_REQUEST['export_format']) ? $_REQUEST['export_format'] : 'csv';
  114. $searchField = isset($_REQUEST['searchField']) ? $_REQUEST['searchField'] : false;
  115. $searchOperator = isset($_REQUEST['searchOper']) ? $_REQUEST['searchOper'] : false;
  116. $searchString = isset($_REQUEST['searchString']) ? $_REQUEST['searchString'] : false;
  117. $search = isset($_REQUEST['_search']) ? $_REQUEST['_search'] : false;
  118. $forceSearch = isset($_REQUEST['_force_search']) ? $_REQUEST['_force_search'] : false;
  119. $extra_fields = [];
  120. $accessStartDate = '';
  121. $accessEndDate = '';
  122. $overwriteColumnHeaderExport = [];
  123. if (!empty($search)) {
  124. $search = 'true';
  125. }
  126. if (($search || $forceSearch) && ($search !== 'false')) {
  127. $whereCondition = ' 1 = 1 ';
  128. $whereConditionInForm = getWhereClause(
  129. $searchField,
  130. $searchOperator,
  131. $searchString
  132. );
  133. if (!empty($whereConditionInForm)) {
  134. $whereCondition .= ' AND ( ';
  135. $whereCondition .= ' ('.$whereConditionInForm.') ';
  136. }
  137. $filters = isset($_REQUEST['filters']) && !is_array($_REQUEST['filters']) ? json_decode($_REQUEST['filters']) : false;
  138. if (isset($_REQUEST['filters2'])) {
  139. $filters = json_decode($_REQUEST['filters2']);
  140. }
  141. if (!empty($filters)) {
  142. if (in_array($action, ['get_questions', 'get_sessions', 'get_sessions_tracking'])) {
  143. switch ($action) {
  144. case 'get_questions':
  145. $type = 'question';
  146. break;
  147. case 'get_sessions':
  148. case 'get_sessions_tracking':
  149. $type = 'session';
  150. break;
  151. }
  152. if (!empty($type)) {
  153. // Extra field.
  154. $extraField = new ExtraField($type);
  155. foreach ($filters->rules as $key => $data) {
  156. if (empty($data)) {
  157. continue;
  158. }
  159. if ($data->field === 'extra_access_start_date') {
  160. $accessStartDate = $data->data;
  161. }
  162. if ($data->field === 'extra_access_end_date') {
  163. $accessEndDate = $data->data;
  164. }
  165. if (in_array($data->field, $toRemove)) {
  166. unset($filters->rules[$key]);
  167. }
  168. }
  169. $result = $extraField->getExtraFieldRules($filters, 'extra_');
  170. $extra_fields = $result['extra_fields'];
  171. $condition_array = $result['condition_array'];
  172. $extraCondition = '';
  173. if (!empty($condition_array)) {
  174. $extraCondition = $filters->groupOp.' ( ';
  175. $extraCondition .= implode($filters->groupOp, $condition_array);
  176. $extraCondition .= ' ) ';
  177. }
  178. $whereCondition .= $extraCondition;
  179. // Question field
  180. $resultQuestion = $extraField->getExtraFieldRules(
  181. $filters,
  182. 'question_'
  183. );
  184. $questionFields = $resultQuestion['extra_fields'];
  185. $condition_array = $resultQuestion['condition_array'];
  186. $extraQuestionCondition = '';
  187. if (!empty($condition_array)) {
  188. $extraQuestionCondition = $filters->groupOp.' ( ';
  189. $extraQuestionCondition .= implode($filters->groupOp, $condition_array);
  190. $extraQuestionCondition .= ' ) ';
  191. // Remove conditions already added
  192. $extraQuestionCondition = str_replace(
  193. $extraCondition,
  194. '',
  195. $extraQuestionCondition
  196. );
  197. }
  198. $whereCondition .= $extraQuestionCondition;
  199. if (isset($filters->custom_dates)) {
  200. $whereCondition .= $filters->custom_dates;
  201. }
  202. }
  203. } elseif (!empty($filters->rules)) {
  204. $whereCondition .= ' AND ( ';
  205. $counter = 0;
  206. foreach ($filters->rules as $key => $rule) {
  207. $whereCondition .= getWhereClause(
  208. $rule->field,
  209. $rule->op,
  210. $rule->data
  211. );
  212. if ($counter < count($filters->rules) - 1) {
  213. $whereCondition .= $filters->groupOp;
  214. }
  215. $counter++;
  216. }
  217. $whereCondition .= ' ) ';
  218. }
  219. }
  220. if (!empty($whereConditionInForm)) {
  221. $whereCondition .= ' ) ';
  222. }
  223. }
  224. // get index row - i.e. user click to sort $sord = $_GET['sord'];
  225. // get the direction
  226. if (!$sidx) {
  227. $sidx = 1;
  228. }
  229. //2. Selecting the count FIRST
  230. //@todo rework this
  231. switch ($action) {
  232. case 'get_exercise_categories':
  233. $manager = new ExerciseCategoryManager();
  234. $courseId = isset($_REQUEST['c_id']) ? $_REQUEST['c_id'] : 0;
  235. $count = $manager->getCourseCount($courseId);
  236. break;
  237. case 'get_calendar_users':
  238. $calendarPlugin = LearningCalendarPlugin::create();
  239. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
  240. $count = $calendarPlugin->getUsersPerCalendarCount($id);
  241. break;
  242. case 'get_usergroups_users':
  243. $usergroup = new UserGroup();
  244. $usergroup->protectScript();
  245. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
  246. $count = $usergroup->getUserGroupUsers($id, true);
  247. break;
  248. case 'get_learning_path_calendars':
  249. $calendarPlugin = LearningCalendarPlugin::create();
  250. $count = $calendarPlugin->getCalendarCount();
  251. break;
  252. case 'course_log_events':
  253. $courseId = api_get_course_int_id();
  254. if (empty($courseId)) {
  255. exit;
  256. }
  257. $sessionId = api_get_session_id();
  258. if (!api_is_allowed_to_edit()) {
  259. exit;
  260. }
  261. $count = Statistics::getNumberOfActivities($courseId, $sessionId);
  262. break;
  263. case 'get_programmed_announcements':
  264. $object = new ScheduledAnnouncement();
  265. $count = $object->get_count();
  266. break;
  267. case 'get_group_reporting':
  268. $course_id = isset($_REQUEST['course_id']) ? $_REQUEST['course_id'] : null;
  269. $group_id = isset($_REQUEST['gidReq']) ? $_REQUEST['gidReq'] : null;
  270. $sessionId = isset($_REQUEST['session_id']) ? $_REQUEST['session_id'] : null;
  271. $count = Tracking::get_group_reporting(
  272. $course_id,
  273. $sessionId,
  274. $group_id,
  275. 'count'
  276. );
  277. break;
  278. case 'get_user_course_report':
  279. case 'get_user_course_report_resumed':
  280. $userNotAllowed = !api_is_student_boss() && !api_is_platform_admin(false, true);
  281. if ($userNotAllowed) {
  282. exit;
  283. }
  284. $userId = api_get_user_id();
  285. $sessionId = isset($_GET['session_id']) ? (int) $_GET['session_id'] : 0;
  286. $courseCodeList = [];
  287. $userIdList = [];
  288. $sessionIdList = [];
  289. $searchByGroups = false;
  290. if (api_is_drh()) {
  291. if (api_drh_can_access_all_session_content()) {
  292. $userList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  293. 'drh_all',
  294. api_get_user_id()
  295. );
  296. if (!empty($userList)) {
  297. foreach ($userList as $user) {
  298. $userIdList[] = $user['user_id'];
  299. }
  300. }
  301. $courseList = SessionManager::getAllCoursesFollowedByUser(
  302. api_get_user_id(),
  303. null
  304. );
  305. if (!empty($courseList)) {
  306. foreach ($courseList as $course) {
  307. $courseCodeList[] = $course['code'];
  308. }
  309. }
  310. } else {
  311. $userList = UserManager::get_users_followed_by_drh(api_get_user_id());
  312. if (!empty($userList)) {
  313. $userIdList = array_keys($userList);
  314. }
  315. $courseList = CourseManager::get_courses_followed_by_drh(api_get_user_id());
  316. if (!empty($courseList)) {
  317. $courseCodeList = array_keys($courseList);
  318. }
  319. }
  320. if (empty($userIdList) || empty($courseCodeList)) {
  321. exit;
  322. }
  323. } elseif (api_is_student_boss()) {
  324. $supervisorStudents = UserManager::getUsersFollowedByUser(
  325. api_get_user_id(),
  326. api_is_student_boss() ? null : STUDENT,
  327. false,
  328. false,
  329. false,
  330. null,
  331. null,
  332. null,
  333. null,
  334. 1,
  335. null,
  336. api_is_student_boss() ? STUDENT_BOSS : COURSEMANAGER,
  337. null
  338. );
  339. $supervisorStudents = array_column($supervisorStudents, 'user_id');
  340. //get students with course or session
  341. $userIdList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  342. 'admin',
  343. null,
  344. false,
  345. null,
  346. null,
  347. null,
  348. 'asc',
  349. null,
  350. null,
  351. null,
  352. [],
  353. $supervisorStudents,
  354. 5
  355. );
  356. $userIdList = array_column($userIdList, 'user_id');
  357. //get students session courses
  358. if ($sessionId == -1) {
  359. $sessionList = SessionManager::get_sessions_list();
  360. $sessionIdList = array_column($sessionList, 'id');
  361. $courseCodeList = [];
  362. foreach ($sessionList as $session) {
  363. $courses = SessionManager::get_course_list_by_session_id($session['id']);
  364. $courseCodeList = array_merge($courseCodeList, array_column($courses, 'code'));
  365. }
  366. }
  367. $searchByGroups = true;
  368. } elseif (api_is_platform_admin()) {
  369. // Get students with course or session
  370. $userIdList = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  371. 'admin',
  372. null,
  373. false,
  374. null,
  375. null,
  376. null,
  377. 'asc',
  378. null,
  379. null,
  380. null,
  381. [],
  382. [],
  383. 5
  384. );
  385. $userIdList = array_column($userIdList, 'user_id');
  386. //get students session courses
  387. if ($sessionId == -1) {
  388. $sessionList = SessionManager::get_sessions_list();
  389. $sessionIdList = array_column($sessionList, 'id');
  390. $courseCodeList = [];
  391. foreach ($sessionList as $session) {
  392. $courses = SessionManager::get_course_list_by_session_id(
  393. $session['id']
  394. );
  395. $courseCodeList = array_merge(
  396. $courseCodeList,
  397. array_column($courses, 'code')
  398. );
  399. }
  400. }
  401. $searchByGroups = true;
  402. }
  403. if ($searchByGroups) {
  404. $userGroup = new UserGroup();
  405. $userIdList = array_merge(
  406. $userIdList,
  407. $userGroup->getGroupUsersByUser(api_get_user_id())
  408. );
  409. }
  410. if (is_array($userIdList)) {
  411. $userIdList = array_unique($userIdList);
  412. }
  413. if (api_is_student_boss()) {
  414. $userCourses = [];
  415. foreach ($userIdList as $userId) {
  416. $userCourses = array_merge(
  417. $userCourses,
  418. CourseManager::get_courses_list_by_user_id($userId, true)
  419. );
  420. $userSessions = SessionManager::getSessionsFollowedByUser($userId);
  421. $sessionIdList = array_merge(
  422. $sessionIdList,
  423. array_column($userSessions, 'id')
  424. );
  425. }
  426. $courseCodeList = array_column($userCourses, 'code');
  427. }
  428. if (!empty($courseCodeList)) {
  429. $courseCodeList = array_unique($courseCodeList);
  430. }
  431. if (!empty($sessionIdList)) {
  432. $sessionIdList = array_unique($sessionIdList);
  433. }
  434. if (api_is_student_boss() && empty($userIdList)) {
  435. $count = 0;
  436. break;
  437. }
  438. if ($action == 'get_user_course_report') {
  439. $count = CourseManager::get_count_user_list_from_course_code(
  440. false,
  441. null,
  442. $courseCodeList,
  443. $userIdList,
  444. $sessionIdList
  445. );
  446. } else {
  447. $count = CourseManager::get_count_user_list_from_course_code(
  448. true,
  449. ['ruc'],
  450. $courseCodeList,
  451. $userIdList,
  452. $sessionIdList
  453. );
  454. }
  455. break;
  456. case 'get_course_exercise_medias':
  457. $course_id = api_get_course_int_id();
  458. $count = Question::get_count_course_medias($course_id);
  459. break;
  460. case 'get_user_skill_ranking':
  461. $skill = new Skill();
  462. $count = $skill->getUserListSkillRankingCount();
  463. break;
  464. case 'get_course_announcements':
  465. $count = AnnouncementManager::getAnnouncements(null, null, true);
  466. break;
  467. case 'get_work_teacher':
  468. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  469. $count = getWorkListTeacher(0, $limit, $sidx, $sord, $whereCondition, true);
  470. break;
  471. case 'get_work_student':
  472. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  473. $count = getWorkListStudent(0, $limit, $sidx, $sord, $whereCondition, true);
  474. break;
  475. case 'get_work_user_list_all':
  476. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  477. $work_id = $_REQUEST['work_id'];
  478. $count = get_count_work($work_id);
  479. break;
  480. case 'get_work_user_list_others':
  481. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  482. $work_id = $_REQUEST['work_id'];
  483. $count = get_count_work($work_id, api_get_user_id());
  484. break;
  485. case 'get_work_user_list':
  486. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  487. $work_id = $_REQUEST['work_id'];
  488. $courseInfo = api_get_course_info();
  489. $documents = getAllDocumentToWork($work_id, api_get_course_int_id());
  490. if (trim($whereCondition) === '1 = 1') {
  491. $whereCondition = '';
  492. }
  493. if (empty($documents)) {
  494. $whereCondition .= " AND u.user_id = ".api_get_user_id();
  495. $count = get_work_user_list(
  496. 0,
  497. $limit,
  498. $sidx,
  499. $sord,
  500. $work_id,
  501. $whereCondition,
  502. null,
  503. true
  504. );
  505. } else {
  506. $count = get_work_user_list_from_documents(
  507. 0,
  508. $limit,
  509. $sidx,
  510. $sord,
  511. $work_id,
  512. api_get_user_id(),
  513. $whereCondition,
  514. true
  515. );
  516. }
  517. break;
  518. case 'get_work_student_list_overview':
  519. if (!(api_is_allowed_to_edit() || api_is_coach())) {
  520. return 0;
  521. }
  522. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  523. $workId = isset($_GET['work_id']) ? $_GET['work_id'] : null;
  524. $count = getWorkUserListData(
  525. $workId,
  526. api_get_course_id(),
  527. api_get_session_id(),
  528. api_get_group_id(),
  529. 0,
  530. $limit,
  531. null,
  532. null,
  533. true
  534. );
  535. break;
  536. case 'get_exercise_results':
  537. $exercise_id = $_REQUEST['exerciseId'];
  538. if (isset($_GET['filter_by_user']) && !empty($_GET['filter_by_user'])) {
  539. $filter_user = (int) $_GET['filter_by_user'];
  540. if (empty($whereCondition)) {
  541. $whereCondition .= " te.exe_user_id = '$filter_user'";
  542. } else {
  543. $whereCondition .= " AND te.exe_user_id = '$filter_user'";
  544. }
  545. }
  546. if (!empty($whereCondition)) {
  547. $whereCondition = " AND $whereCondition";
  548. }
  549. $count = ExerciseLib::get_count_exam_results(
  550. $exercise_id,
  551. $whereCondition
  552. );
  553. break;
  554. case 'get_exercise_results_report':
  555. api_protect_admin_script();
  556. $exerciseId = isset($_REQUEST['exercise_id']) ? $_REQUEST['exercise_id'] : 0;
  557. $courseId = isset($_REQUEST['course_id']) ? $_REQUEST['course_id'] : 0;
  558. if (empty($exerciseId)) {
  559. exit;
  560. }
  561. if (!empty($courseId)) {
  562. $courseInfo = api_get_course_info_by_id($courseId);
  563. } else {
  564. $courseCode = isset($_REQUEST['cidReq']) ? $_REQUEST['cidReq'] : '';
  565. if (!empty($courseCode)) {
  566. $courseInfo = api_get_course_info($courseCode);
  567. }
  568. }
  569. if (empty($courseInfo)) {
  570. exit;
  571. }
  572. $startDate = Database::escape_string($_REQUEST['start_date']);
  573. $whereCondition .= " AND exe_date > '$startDate' AND te.status = '' ";
  574. $count = ExerciseLib::get_count_exam_results(
  575. $exerciseId,
  576. $whereCondition,
  577. $courseInfo['code'],
  578. true
  579. );
  580. break;
  581. case 'get_hotpotatoes_exercise_results':
  582. $hotpot_path = $_REQUEST['path'];
  583. $count = ExerciseLib::get_count_exam_hotpotatoes_results($hotpot_path);
  584. break;
  585. case 'get_sessions_tracking':
  586. $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
  587. $description = '';
  588. $setting = api_get_setting('show_session_description');
  589. if ($setting === 'true') {
  590. $description = $keyword;
  591. }
  592. if (api_is_drh() || api_is_session_admin()) {
  593. $count = SessionManager::get_sessions_followed_by_drh(
  594. api_get_user_id(),
  595. null,
  596. null,
  597. true,
  598. false,
  599. false,
  600. null,
  601. $keyword,
  602. $description,
  603. ['where' => $whereCondition, 'extra' => $extra_fields]
  604. );
  605. } else {
  606. // Sessions for the coach
  607. $count = Tracking::get_sessions_coached_by_user(
  608. api_get_user_id(),
  609. null,
  610. null,
  611. true,
  612. $keyword,
  613. $description,
  614. null,
  615. null,
  616. ['where' => $whereCondition, 'extra' => $extra_fields]
  617. );
  618. }
  619. break;
  620. case 'get_sessions':
  621. $list_type = isset($_REQUEST['list_type']) ? $_REQUEST['list_type'] : 'simple';
  622. if ($list_type === 'simple') {
  623. $count = SessionManager::formatSessionsAdminForGrid(
  624. ['where' => $whereCondition, 'extra' => $extra_fields],
  625. true
  626. );
  627. } else {
  628. $count = SessionManager::get_count_admin_complete(
  629. ['where' => $whereCondition, 'extra' => $extra_fields]
  630. );
  631. }
  632. break;
  633. case 'get_session_lp_progress':
  634. case 'get_session_progress':
  635. //@TODO replace this for a more efficient function (not retrieving the whole data)
  636. $course = api_get_course_info_by_id($_GET['course_id']);
  637. $users = CourseManager::get_student_list_from_course_code(
  638. $course['code'],
  639. true,
  640. $_GET['session_id'],
  641. $_GET['date_from'],
  642. $_GET['date_to']
  643. );
  644. $count = count($users);
  645. break;
  646. case 'get_exercise_progress':
  647. //@TODO replace this for a more efficient function (not retrieving the whole data)
  648. $records = Tracking::get_exercise_progress(
  649. $_GET['session_id'],
  650. $_GET['course_id'],
  651. $_GET['exercise_id'],
  652. $_GET['date_from'],
  653. $_GET['date_to']
  654. );
  655. $count = count($records);
  656. break;
  657. case 'get_session_access_overview':
  658. //@TODO replace this for a more efficient function (not retrieving the whole data)
  659. $records = SessionManager::get_user_data_access_tracking_overview(
  660. $_GET['session_id'],
  661. $_GET['course_id'],
  662. $_GET['student_id'],
  663. $_GET['profile'],
  664. $_GET['date_from'],
  665. $_GET['date_to'],
  666. $options
  667. );
  668. $count = count($records);
  669. break;
  670. case 'get_survey_overview':
  671. //@TODO replace this for a more efficient function (not retrieving the whole data)
  672. $records = SessionManager::get_survey_overview(
  673. $_GET['session_id'],
  674. $_GET['course_id'],
  675. $_GET['survey_id'],
  676. $_GET['date_from'],
  677. $_GET['date_to'],
  678. $options
  679. );
  680. $count = count($records);
  681. break;
  682. case 'get_exercise_grade':
  683. //@TODO replace this for a more efficient function (not retrieving the whole data)
  684. $course = api_get_course_info_by_id($_GET['course_id']);
  685. $users = CourseManager::get_student_list_from_course_code(
  686. $course['code'],
  687. true,
  688. $_GET['session_id']
  689. );
  690. $count = count($users);
  691. break;
  692. case 'get_extra_fields':
  693. $type = $_REQUEST['type'];
  694. $obj = new ExtraField($type);
  695. $count = $obj->get_count();
  696. break;
  697. case 'get_extra_field_options':
  698. $type = $_REQUEST['type'];
  699. $field_id = $_REQUEST['field_id'];
  700. $obj = new ExtraFieldOption($type);
  701. $count = $obj->get_count_by_field_id($field_id);
  702. break;
  703. case 'get_timelines':
  704. $obj = new Timeline();
  705. $count = $obj->get_count();
  706. break;
  707. case 'get_gradebooks':
  708. $obj = new Gradebook();
  709. $count = $obj->get_count();
  710. break;
  711. case 'get_event_email_template':
  712. $obj = new EventEmailTemplate();
  713. $count = $obj->get_count();
  714. break;
  715. case 'get_careers':
  716. $obj = new Career();
  717. $count = $obj->get_count();
  718. break;
  719. case 'get_promotions':
  720. $obj = new Promotion();
  721. $count = $obj->get_count();
  722. break;
  723. case 'get_mail_template':
  724. $obj = new MailTemplateManager();
  725. $count = $obj->get_count();
  726. break;
  727. case 'get_grade_models':
  728. $obj = new GradeModel();
  729. $count = $obj->get_count();
  730. break;
  731. case 'get_usergroups':
  732. $obj = new UserGroup();
  733. $obj->protectScript();
  734. $count = $obj->get_count();
  735. break;
  736. case 'get_usergroups_teacher':
  737. $obj = new UserGroup();
  738. $obj->protectScript();
  739. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'registered';
  740. $groupFilter = isset($_REQUEST['group_filter']) ? (int) $_REQUEST['group_filter'] : 0;
  741. $course_id = api_get_course_int_id();
  742. if ($type === 'registered') {
  743. $count = $obj->getUserGroupByCourseWithDataCount(
  744. $course_id,
  745. $groupFilter
  746. );
  747. } else {
  748. $count = $obj->get_count($groupFilter);
  749. }
  750. break;
  751. default:
  752. exit;
  753. }
  754. //3. Calculating first, end, etc
  755. $total_pages = 0;
  756. if ($count > 0) {
  757. if (!empty($limit)) {
  758. $total_pages = ceil((float) $count / (float) $limit);
  759. }
  760. }
  761. if ($page > $total_pages) {
  762. $page = $total_pages;
  763. }
  764. $start = $limit * $page - $limit;
  765. if ($start < 0) {
  766. $start = 0;
  767. }
  768. //4. Deleting an element if the user wants to
  769. if (isset($_REQUEST['oper']) && $_REQUEST['oper'] == 'del') {
  770. $obj->delete($_REQUEST['id']);
  771. }
  772. $is_allowedToEdit = api_is_allowed_to_edit(null, true) || api_is_allowed_to_edit(true) || api_is_drh();
  773. //5. Querying the DB for the elements
  774. $columns = [];
  775. switch ($action) {
  776. case 'get_exercise_categories':
  777. api_protect_course_script();
  778. if (!api_is_allowed_to_edit()) {
  779. api_not_allowed(true);
  780. }
  781. $columns = ['name', 'actions'];
  782. $manager = new ExerciseCategoryManager();
  783. $result = $manager->get_all([
  784. 'where' => ['c_id = ? ' => $courseId],
  785. 'order' => "$sidx $sord",
  786. 'LIMIT' => "$start , $limit",
  787. ]);
  788. break;
  789. case 'get_calendar_users':
  790. $columns = ['firstname', 'lastname', 'exam'];
  791. $result = $calendarPlugin->getUsersPerCalendar($id);
  792. break;
  793. case 'get_usergroups_users':
  794. $columns = ['name', 'actions'];
  795. if (api_get_plugin_setting('learning_calendar', 'enabled') === 'true') {
  796. $columns = [
  797. 'name',
  798. 'calendar',
  799. 'gradebook_items',
  800. 'time_spent',
  801. 'lp_day_completed',
  802. 'days_diff',
  803. 'actions',
  804. 'calendar_id',
  805. ];
  806. }
  807. $result = $usergroup->getUserGroupUsers($id);
  808. break;
  809. case 'get_learning_path_calendars':
  810. $columns = ['title', 'total_hours', 'minutes_per_day', 'actions'];
  811. $result = $calendarPlugin->getCalendars(
  812. $start,
  813. $limit,
  814. $sidx,
  815. $sord
  816. );
  817. break;
  818. case 'course_log_events':
  819. $columns = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
  820. $result = Statistics::getActivitiesData(
  821. $start,
  822. $limit,
  823. $sidx,
  824. $sord,
  825. $courseId,
  826. $sessionId
  827. );
  828. break;
  829. case 'get_programmed_announcements':
  830. $columns = ['subject', 'date', 'sent', 'actions'];
  831. $sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : 0;
  832. $result = Database::select(
  833. '*',
  834. $object->table,
  835. [
  836. 'where' => ['session_id = ? ' => $sessionId],
  837. 'order' => "$sidx $sord",
  838. 'LIMIT' => "$start , $limit", ]
  839. );
  840. if ($result) {
  841. foreach ($result as &$item) {
  842. $item['sent'] = $item['sent'] == 1 ? get_lang('Yes') : get_lang('No');
  843. $item['date'] = api_get_local_time($item['date']);
  844. }
  845. }
  846. break;
  847. case 'get_group_reporting':
  848. $columns = ['name', 'time', 'progress', 'score', 'works', 'messages', 'actions'];
  849. $result = Tracking::get_group_reporting(
  850. $course_id,
  851. $sessionId,
  852. $group_id,
  853. 'all',
  854. $start,
  855. $limit,
  856. $sidx,
  857. $sord,
  858. $whereCondition
  859. );
  860. break;
  861. case 'get_course_exercise_medias':
  862. $columns = ['question'];
  863. $result = Question::get_course_medias(
  864. $course_id,
  865. $start,
  866. $limit,
  867. $sidx,
  868. $sord,
  869. $whereCondition
  870. );
  871. break;
  872. case 'get_user_course_report_resumed':
  873. $columns = [
  874. 'extra_ruc',
  875. 'training_hours',
  876. 'count_users',
  877. 'count_users_registered',
  878. 'average_hours_per_user',
  879. 'count_certificates',
  880. ];
  881. $column_names = [
  882. get_lang('Company'),
  883. get_lang('TrainingHoursAccumulated'),
  884. get_lang('CountOfSubscriptions'),
  885. get_lang('CountOfUsers'),
  886. get_lang('AverageHoursPerStudent'),
  887. get_lang('CountCertificates'),
  888. ];
  889. $extra_fields = UserManager::get_extra_fields(
  890. 0,
  891. 100,
  892. null,
  893. null,
  894. true,
  895. true
  896. );
  897. if (!empty($extra_fields)) {
  898. foreach ($extra_fields as $extra) {
  899. if ($extra['1'] == 'ruc') {
  900. continue;
  901. }
  902. $columns[] = $extra['1'];
  903. $column_names[] = $extra['3'];
  904. }
  905. }
  906. if (!in_array($sidx, ['training_hours'])) {
  907. //$sidx = 'training_hours';
  908. }
  909. if (api_is_student_boss() && empty($userIdList)) {
  910. $result = [];
  911. break;
  912. }
  913. $result = CourseManager::get_user_list_from_course_code(
  914. null,
  915. null,
  916. "LIMIT $start, $limit",
  917. null, //" $sidx $sord",
  918. null,
  919. null,
  920. true,
  921. true,
  922. ['ruc'],
  923. $courseCodeList,
  924. $userIdList,
  925. null,
  926. $sessionIdList
  927. );
  928. $new_result = [];
  929. if (!empty($result)) {
  930. foreach ($result as $row) {
  931. $row['training_hours'] = api_time_to_hms($row['training_hours']);
  932. $row['average_hours_per_user'] = api_time_to_hms($row['average_hours_per_user']);
  933. $new_result[] = $row;
  934. }
  935. $result = $new_result;
  936. }
  937. break;
  938. case 'get_user_course_report':
  939. $columns = [
  940. 'course',
  941. 'user',
  942. 'email',
  943. 'time',
  944. 'certificate',
  945. 'progress_100',
  946. 'progress',
  947. ];
  948. $column_names = [
  949. get_lang('Course'),
  950. get_lang('User'),
  951. get_lang('Email'),
  952. get_lang('ManHours'),
  953. get_lang('CertificateGenerated'),
  954. get_lang('Approved'),
  955. get_lang('CourseAdvance'),
  956. ];
  957. $extra_fields = UserManager::get_extra_fields(
  958. 0,
  959. 100,
  960. null,
  961. null,
  962. true,
  963. true
  964. );
  965. if (!empty($extra_fields)) {
  966. foreach ($extra_fields as $extra) {
  967. $columns[] = $extra['1'];
  968. $column_names[] = $extra['3'];
  969. }
  970. }
  971. if (api_is_student_boss()) {
  972. $columns[] = 'group';
  973. $column_names[] = get_lang('Group');
  974. }
  975. if (!in_array($sidx, ['title'])) {
  976. $sidx = 'title';
  977. }
  978. if (api_is_student_boss() && empty($userIdList)) {
  979. $result = [];
  980. break;
  981. }
  982. //get sessions
  983. $arrSessions = [];
  984. if (count($sessionIdList) > 0) {
  985. $arrSessions = CourseManager::get_user_list_from_course_code(
  986. null,
  987. null,
  988. "LIMIT $start, $limit",
  989. " $sidx $sord",
  990. null,
  991. null,
  992. true,
  993. false,
  994. null,
  995. $courseCodeList,
  996. $userIdList,
  997. null,
  998. $sessionIdList
  999. );
  1000. }
  1001. //get courses
  1002. $arrCourses = CourseManager::get_user_list_from_course_code(
  1003. null,
  1004. null,
  1005. "LIMIT $start, $limit",
  1006. " $sidx $sord",
  1007. null,
  1008. null,
  1009. true,
  1010. false,
  1011. null,
  1012. [],
  1013. $userIdList,
  1014. null
  1015. );
  1016. //merge courses and sessions
  1017. $result = array_merge($arrSessions, $arrCourses);
  1018. if (api_is_student_boss()) {
  1019. $userGroup = new UserGroup();
  1020. foreach ($result as &$item) {
  1021. $userGroups = $userGroup->get_groups_by_user($item['user_id']);
  1022. $item['group'] = implode(", ", array_column($userGroups, 'name'));
  1023. unset($item['user_id']);
  1024. }
  1025. }
  1026. break;
  1027. case 'get_user_skill_ranking':
  1028. $columns = [
  1029. 'photo',
  1030. 'firstname',
  1031. 'lastname',
  1032. 'skills_acquired',
  1033. 'currently_learning',
  1034. 'rank',
  1035. ];
  1036. $result = $skill->getUserListSkillRanking(
  1037. $start,
  1038. $limit,
  1039. $sidx,
  1040. $sord,
  1041. $whereCondition
  1042. );
  1043. $result = msort($result, 'skills_acquired', 'asc');
  1044. $skills_in_course = [];
  1045. if (!empty($result)) {
  1046. foreach ($result as &$item) {
  1047. $user_info = api_get_user_info($item['user_id']);
  1048. $personal_course_list = UserManager::get_personal_session_course_list(
  1049. $item['user_id']
  1050. );
  1051. $count_skill_by_course = [];
  1052. foreach ($personal_course_list as $course_item) {
  1053. if (!isset($skills_in_course[$course_item['code']])) {
  1054. $count_skill_by_course[$course_item['code']] = $skill->getCountSkillsByCourse($course_item['code']);
  1055. $skills_in_course[$course_item['code']] = $count_skill_by_course[$course_item['code']];
  1056. } else {
  1057. $count_skill_by_course[$course_item['code']] = $skills_in_course[$course_item['code']];
  1058. }
  1059. }
  1060. $item['photo'] = Display::img($user_info['avatar_small'], $user_info['complete_name'], [], false);
  1061. $item['currently_learning'] = !empty($count_skill_by_course) ? array_sum($count_skill_by_course) : 0;
  1062. }
  1063. }
  1064. break;
  1065. case 'get_course_announcements':
  1066. $columns = [
  1067. 'title',
  1068. 'username',
  1069. 'insert_date',
  1070. 'actions',
  1071. ];
  1072. $titleToSearch = isset($_REQUEST['title_to_search']) ? $_REQUEST['title_to_search'] : '';
  1073. $userIdToSearch = isset($_REQUEST['user_id_to_search']) ? $_REQUEST['user_id_to_search'] : 0;
  1074. $result = AnnouncementManager::getAnnouncements(
  1075. null,
  1076. null,
  1077. false,
  1078. $start,
  1079. $limit,
  1080. $sidx,
  1081. $sord,
  1082. $titleToSearch,
  1083. $userIdToSearch
  1084. );
  1085. break;
  1086. case 'get_work_teacher':
  1087. $columns = [
  1088. 'type',
  1089. 'title',
  1090. 'sent_date',
  1091. 'expires_on',
  1092. 'amount',
  1093. 'actions',
  1094. ];
  1095. $result = getWorkListTeacher(
  1096. $start,
  1097. $limit,
  1098. $sidx,
  1099. $sord,
  1100. $whereCondition
  1101. );
  1102. break;
  1103. case 'get_work_student':
  1104. $columns = [
  1105. 'type',
  1106. 'title',
  1107. 'expires_on',
  1108. 'feedback',
  1109. 'last_upload',
  1110. 'others',
  1111. ];
  1112. $result = getWorkListStudent(
  1113. $start,
  1114. $limit,
  1115. $sidx,
  1116. $sord,
  1117. $whereCondition
  1118. );
  1119. break;
  1120. case 'get_work_user_list_all':
  1121. if (isset($_GET['type']) && $_GET['type'] === 'simple') {
  1122. $columns = [
  1123. 'fullname',
  1124. 'title',
  1125. 'qualification',
  1126. 'sent_date',
  1127. 'qualificator_id',
  1128. 'correction',
  1129. 'actions',
  1130. ];
  1131. } else {
  1132. $columns = [
  1133. 'fullname',
  1134. 'title',
  1135. 'qualification',
  1136. 'sent_date',
  1137. 'correction',
  1138. 'actions',
  1139. ];
  1140. }
  1141. $whereCondition = " AND $whereCondition ";
  1142. $result = get_work_user_list(
  1143. $start,
  1144. $limit,
  1145. $sidx,
  1146. $sord,
  1147. $work_id,
  1148. $whereCondition
  1149. );
  1150. break;
  1151. case 'get_work_user_list_others':
  1152. if (isset($_GET['type']) && $_GET['type'] === 'simple') {
  1153. $columns = [
  1154. 'type', 'firstname', 'lastname', 'title', 'qualification', 'sent_date', 'qualificator_id', 'actions',
  1155. ];
  1156. } else {
  1157. $columns = ['type', 'firstname', 'lastname', 'title', 'sent_date', 'actions'];
  1158. }
  1159. if (trim($whereCondition) === '1 = 1') {
  1160. $whereCondition = '';
  1161. }
  1162. $whereCondition .= " AND u.user_id <> ".api_get_user_id();
  1163. $result = get_work_user_list(
  1164. $start,
  1165. $limit,
  1166. $sidx,
  1167. $sord,
  1168. $work_id,
  1169. $whereCondition
  1170. );
  1171. break;
  1172. case 'get_work_user_list':
  1173. if (isset($_GET['type']) && $_GET['type'] == 'simple') {
  1174. $columns = [
  1175. 'type', 'title', 'qualification', 'sent_date', 'qualificator_id', 'actions',
  1176. ];
  1177. } else {
  1178. $columns = ['type', 'title', 'qualification', 'sent_date', 'actions'];
  1179. }
  1180. $documents = getAllDocumentToWork($work_id, api_get_course_int_id());
  1181. if (trim($whereCondition) === '1 = 1') {
  1182. $whereCondition = '';
  1183. }
  1184. if (empty($documents)) {
  1185. $whereCondition .= " AND u.user_id = ".api_get_user_id();
  1186. $result = get_work_user_list(
  1187. $start,
  1188. $limit,
  1189. $sidx,
  1190. $sord,
  1191. $work_id,
  1192. $whereCondition
  1193. );
  1194. } else {
  1195. $result = get_work_user_list_from_documents(
  1196. $start,
  1197. $limit,
  1198. $sidx,
  1199. $sord,
  1200. $work_id,
  1201. api_get_user_id(),
  1202. $whereCondition
  1203. );
  1204. }
  1205. break;
  1206. case 'get_exercise_results':
  1207. $course = api_get_course_info();
  1208. // Used inside ExerciseLib::get_exam_results_data()
  1209. $documentPath = api_get_path(SYS_COURSE_PATH).$course['path']."/document";
  1210. $is_allowedToEdit = api_is_allowed_to_edit(null, true) ||
  1211. api_is_drh() ||
  1212. api_is_student_boss() ||
  1213. api_is_session_admin();
  1214. if ($is_allowedToEdit || api_is_student_boss()) {
  1215. $columns = [
  1216. 'firstname',
  1217. 'lastname',
  1218. 'username',
  1219. 'group_name',
  1220. 'exe_duration',
  1221. 'start_date',
  1222. 'exe_date',
  1223. 'score',
  1224. 'user_ip',
  1225. 'status',
  1226. 'lp',
  1227. 'actions',
  1228. ];
  1229. $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
  1230. if ($officialCodeInList === 'true') {
  1231. $columns = array_merge(['official_code'], $columns);
  1232. }
  1233. }
  1234. $result = ExerciseLib::get_exam_results_data(
  1235. $start,
  1236. $limit,
  1237. $sidx,
  1238. $sord,
  1239. $exercise_id,
  1240. $whereCondition
  1241. );
  1242. break;
  1243. case 'get_exercise_results_report':
  1244. // Used inside ExerciseLib::get_exam_results_data()
  1245. $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document';
  1246. $sessionId = api_get_session_id();
  1247. $columns = [
  1248. 'firstname',
  1249. 'lastname',
  1250. 'username',
  1251. ];
  1252. $extraFieldsToAdd = [];
  1253. $extraFields = api_get_configuration_value('exercise_category_report_user_extra_fields');
  1254. $roundValues = api_get_configuration_value('exercise_category_round_score_in_export');
  1255. if (!empty($extraFields) && isset($extraFields['fields'])) {
  1256. $extraField = new ExtraField('user');
  1257. foreach ($extraFields['fields'] as $variable) {
  1258. $info = $extraField->get_handler_field_info_by_field_variable($variable);
  1259. if ($info) {
  1260. $extraFieldsToAdd[] = $variable;
  1261. }
  1262. }
  1263. }
  1264. if (!empty($extraFieldsToAdd)) {
  1265. $columns = array_merge($columns, $extraFieldsToAdd);
  1266. }
  1267. $columns[] = 'session';
  1268. $columns[] = 'session_access_start_date';
  1269. $columns[] = 'exe_date';
  1270. $columns[] = 'score';
  1271. if ($operation === 'excel') {
  1272. $columns = [
  1273. 'firstname',
  1274. 'lastname',
  1275. 'username',
  1276. ];
  1277. if (!empty($extraFieldsToAdd)) {
  1278. $columns = array_merge($columns, $extraFieldsToAdd);
  1279. }
  1280. $columns[] = 'session';
  1281. $columns[] = 'session_access_start_date';
  1282. $columns[] = 'exe_date';
  1283. $columns[] = 'score_percentage';
  1284. $columns[] = 'only_score';
  1285. $columns[] = 'total';
  1286. $overwriteColumnHeaderExport['session_access_start_date'] = get_lang('SessionStartDate');
  1287. $overwriteColumnHeaderExport['exe_date'] = get_lang('StartDate');
  1288. $overwriteColumnHeaderExport['score_percentage'] = get_lang('Score').' - '.get_lang('Percentage');
  1289. $overwriteColumnHeaderExport['only_score'] = get_lang('Score').' - '.get_lang('ScoreNote');
  1290. $overwriteColumnHeaderExport['total'] = get_lang('Score').' - '.get_lang('ScoreTest');
  1291. }
  1292. $categoryList = TestCategory::getListOfCategoriesIDForTest($exerciseId, $courseId);
  1293. if (!empty($categoryList)) {
  1294. foreach ($categoryList as $categoryInfo) {
  1295. $label = 'category_'.$categoryInfo['id'];
  1296. if ($operation == 'excel') {
  1297. $columns[] = $label.'_score_percentage';
  1298. $columns[] = $label.'_only_score';
  1299. $columns[] = $label.'_total';
  1300. $overwriteColumnHeaderExport[$label] = $categoryInfo['title'];
  1301. $overwriteColumnHeaderExport[$label.'_score_percentage'] = $categoryInfo['title'].
  1302. ' - '.get_lang('Percentage');
  1303. $overwriteColumnHeaderExport[$label.'_only_score'] = $categoryInfo['title'].
  1304. ' - '.get_lang('ScoreNote');
  1305. $overwriteColumnHeaderExport[$label.'_total'] = $categoryInfo['title'].
  1306. ' - '.get_lang('ScoreTest');
  1307. } else {
  1308. $columns[] = $label;
  1309. }
  1310. }
  1311. }
  1312. if ($operation !== 'excel') {
  1313. $columns[] = 'actions';
  1314. }
  1315. $whereCondition .= " AND te.status = '' ";
  1316. $result = ExerciseLib::get_exam_results_data(
  1317. $start,
  1318. $limit,
  1319. $sidx,
  1320. $sord,
  1321. $exerciseId,
  1322. $whereCondition,
  1323. false,
  1324. $courseInfo['code'],
  1325. true,
  1326. true,
  1327. $extraFieldsToAdd,
  1328. true,
  1329. $roundValues
  1330. );
  1331. break;
  1332. case 'get_hotpotatoes_exercise_results':
  1333. $course = api_get_course_info();
  1334. $documentPath = api_get_path(SYS_COURSE_PATH).$course['path']."/document";
  1335. if (api_is_allowed_to_edit()) {
  1336. $columns = ['firstname', 'lastname', 'username', 'group_name', 'exe_date', 'score', 'actions'];
  1337. } else {
  1338. $columns = ['exe_date', 'score', 'actions'];
  1339. }
  1340. $result = ExerciseLib::get_exam_results_hotpotatoes_data(
  1341. $start,
  1342. $limit,
  1343. $sidx,
  1344. $sord,
  1345. $hotpot_path,
  1346. $whereCondition
  1347. );
  1348. break;
  1349. case 'get_work_student_list_overview':
  1350. if (!(api_is_allowed_to_edit() || api_is_coach())) {
  1351. return [];
  1352. }
  1353. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  1354. $columns = [
  1355. 'student', 'works',
  1356. ];
  1357. $result = getWorkUserListData(
  1358. $workId,
  1359. api_get_course_id(),
  1360. api_get_session_id(),
  1361. api_get_group_id(),
  1362. $start,
  1363. $limit,
  1364. $sidx,
  1365. $sord
  1366. );
  1367. break;
  1368. case 'get_hotpotatoes_exercise_results':
  1369. $course = api_get_course_info();
  1370. $documentPath = api_get_path(SYS_COURSE_PATH).$course['path']."/document";
  1371. if (api_is_allowed_to_edit(null, true) || api_is_drh()) {
  1372. $columns = ['firstname', 'lastname', 'username', 'group_name', 'exe_date', 'score', 'actions'];
  1373. } else {
  1374. $columns = ['exe_date', 'score', 'actions'];
  1375. }
  1376. $result = ExerciseLib::get_exam_results_hotpotatoes_data(
  1377. $start,
  1378. $limit,
  1379. $sidx,
  1380. $sord,
  1381. $hotpot_path,
  1382. $whereCondition
  1383. );
  1384. break;
  1385. case 'get_sessions_tracking':
  1386. if (api_is_drh() || api_is_session_admin()) {
  1387. $orderByName = Database::escape_string($sidx);
  1388. $orderByName = in_array($orderByName, ['name', 'access_start_date']) ? $orderByName : 'name';
  1389. $orderBy = " ORDER BY $orderByName $sord";
  1390. $sessions = SessionManager::get_sessions_followed_by_drh(
  1391. api_get_user_id(),
  1392. $start,
  1393. $limit,
  1394. false,
  1395. false,
  1396. false,
  1397. $orderBy,
  1398. $keyword,
  1399. $description,
  1400. ['where' => $whereCondition, 'extra' => $extra_fields]
  1401. );
  1402. } else {
  1403. // Sessions for the coach
  1404. $sessions = Tracking::get_sessions_coached_by_user(
  1405. api_get_user_id(),
  1406. $start,
  1407. $limit,
  1408. false,
  1409. $keyword,
  1410. $description,
  1411. $sidx,
  1412. $sord,
  1413. ['where' => $whereCondition, 'extra' => $extra_fields]
  1414. );
  1415. }
  1416. $session_columns = SessionManager::getGridColumns('my_space');
  1417. $columns = $session_columns['simple_column_name'];
  1418. $result = [];
  1419. if (!empty($sessions)) {
  1420. foreach ($sessions as $session) {
  1421. if (api_drh_can_access_all_session_content()) {
  1422. $count_courses_in_session = SessionManager::get_course_list_by_session_id(
  1423. $session['id'],
  1424. '',
  1425. null,
  1426. true
  1427. );
  1428. } else {
  1429. $count_courses_in_session = count(
  1430. Tracking::get_courses_followed_by_coach(
  1431. $user_id,
  1432. $session['id']
  1433. )
  1434. );
  1435. }
  1436. $count_users_in_session = SessionManager::get_users_by_session(
  1437. $session['id'],
  1438. 0,
  1439. true
  1440. );
  1441. $session['display_start_date'] = '';
  1442. $session['display_end_date'] = '';
  1443. $session['coach_access_start_date'] = '';
  1444. $session['coach_access_end_date'] = '';
  1445. $dateData = SessionManager::parseSessionDates($session, true);
  1446. $dateToString = $dateData['access'];
  1447. $detailButtons = [];
  1448. $detailButtons[] = Display::url(
  1449. Display::return_icon('works.png', get_lang('WorksReport')),
  1450. api_get_path(WEB_CODE_PATH).'mySpace/works_in_session_report.php?session='.$session['id']
  1451. );
  1452. $detailButtons[] = Display::url(
  1453. Display::return_icon('2rightarrow.png'),
  1454. api_get_path(WEB_CODE_PATH).'mySpace/course.php?session_id='.$session['id']
  1455. );
  1456. $item = [
  1457. 'name' => Display::url(
  1458. $session['name'],
  1459. api_get_path(WEB_CODE_PATH).'mySpace/course.php?session_id='.$session['id']
  1460. ),
  1461. 'date' => $dateToString,
  1462. 'course_per_session' => $count_courses_in_session,
  1463. 'student_per_session' => $count_users_in_session,
  1464. 'actions' => implode(' ', $detailButtons),
  1465. ];
  1466. if (!empty($extra_fields)) {
  1467. foreach ($extra_fields as $extraField) {
  1468. $item[$extraField['field']] = $extraField['data'];
  1469. }
  1470. }
  1471. $result[] = $item;
  1472. }
  1473. }
  1474. break;
  1475. case 'get_sessions':
  1476. $session_columns = SessionManager::getGridColumns($list_type);
  1477. $columns = $session_columns['simple_column_name'];
  1478. if ($list_type == 'simple') {
  1479. $result = SessionManager::formatSessionsAdminForGrid(
  1480. [
  1481. 'where' => $whereCondition,
  1482. 'order' => "$sidx $sord, s.name",
  1483. 'extra' => $extra_fields,
  1484. 'limit' => "$start , $limit",
  1485. ],
  1486. false,
  1487. $session_columns
  1488. );
  1489. } else {
  1490. $result = SessionManager::get_sessions_admin_complete(
  1491. [
  1492. 'where' => $whereCondition,
  1493. 'order' => "$sidx $sord, s.name",
  1494. 'extra' => $extra_fields,
  1495. 'limit' => "$start , $limit",
  1496. ]
  1497. );
  1498. }
  1499. break;
  1500. case 'get_exercise_progress':
  1501. $sessionId = (int) $_GET['session_id'];
  1502. $courseId = (int) $_GET['course_id'];
  1503. $exerciseId = (int) $_GET['exercise_id'];
  1504. $date_from = $_GET['date_from'];
  1505. $date_to = $_GET['date_to'];
  1506. $columns = [
  1507. 'session',
  1508. 'exercise_id',
  1509. 'quiz_title',
  1510. 'username',
  1511. 'lastname',
  1512. 'firstname',
  1513. 'time',
  1514. 'question_id',
  1515. 'question',
  1516. 'description',
  1517. 'answer',
  1518. 'correct',
  1519. ];
  1520. $result = Tracking::get_exercise_progress(
  1521. $sessionId,
  1522. $courseId,
  1523. $exerciseId,
  1524. $date_from,
  1525. $date_to,
  1526. [
  1527. 'where' => $whereCondition,
  1528. 'order' => "$sidx $sord",
  1529. 'limit' => "$start , $limit",
  1530. ]
  1531. );
  1532. break;
  1533. case 'get_session_lp_progress':
  1534. $sessionId = 0;
  1535. if (!empty($_GET['session_id']) && !empty($_GET['course_id'])) {
  1536. $sessionId = (int) $_GET['session_id'];
  1537. $courseId = (int) $_GET['course_id'];
  1538. $course = api_get_course_info_by_id($courseId);
  1539. }
  1540. /**
  1541. * Add lessons of course.
  1542. */
  1543. $columns = [
  1544. 'username',
  1545. 'firstname',
  1546. 'lastname',
  1547. ];
  1548. $lessons = LearnpathList::get_course_lessons(
  1549. $course['code'],
  1550. $sessionId
  1551. );
  1552. foreach ($lessons as $lesson_id => $lesson) {
  1553. $columns[] = $lesson_id;
  1554. }
  1555. $columns[] = 'total';
  1556. $result = SessionManager::get_session_lp_progress(
  1557. $sessionId,
  1558. $courseId,
  1559. $date_from,
  1560. $date_to,
  1561. [
  1562. 'where' => $whereCondition,
  1563. 'order' => "$sidx $sord",
  1564. 'limit' => "$start , $limit",
  1565. ]
  1566. );
  1567. break;
  1568. case 'get_survey_overview':
  1569. $sessionId = 0;
  1570. if (!empty($_GET['session_id']) &&
  1571. !empty($_GET['course_id']) &&
  1572. !empty($_GET['survey_id'])
  1573. ) {
  1574. $sessionId = intval($_GET['session_id']);
  1575. $courseId = intval($_GET['course_id']);
  1576. $surveyId = intval($_GET['survey_id']);
  1577. $date_from = $_GET['date_from'];
  1578. $date_to = $_GET['date_to'];
  1579. //$course = api_get_course_info_by_id($courseId);
  1580. }
  1581. /**
  1582. * Add lessons of course.
  1583. */
  1584. $columns = [
  1585. 'username',
  1586. 'firstname',
  1587. 'lastname',
  1588. ];
  1589. $questions = SurveyManager::get_questions($surveyId, $courseId);
  1590. foreach ($questions as $question_id => $question) {
  1591. $columns[] = $question_id;
  1592. }
  1593. $result = SessionManager::get_survey_overview(
  1594. $sessionId,
  1595. $courseId,
  1596. $surveyId,
  1597. $date_from,
  1598. $date_to,
  1599. [
  1600. 'where' => $whereCondition,
  1601. 'order' => "$sidx $sord",
  1602. 'limit' => "$start , $limit",
  1603. ]
  1604. );
  1605. break;
  1606. case 'get_session_progress':
  1607. $columns = [
  1608. 'lastname',
  1609. 'firstname',
  1610. 'username',
  1611. //'profile',
  1612. 'total',
  1613. 'courses',
  1614. 'lessons',
  1615. 'exercises',
  1616. 'forums',
  1617. 'homeworks',
  1618. 'wikis',
  1619. 'surveys',
  1620. //exercises
  1621. 'lessons_total',
  1622. 'lessons_done',
  1623. 'lessons_left',
  1624. 'lessons_progress',
  1625. //exercises
  1626. 'exercises_total',
  1627. 'exercises_done',
  1628. 'exercises_left',
  1629. 'exercises_progress',
  1630. //forums
  1631. 'forums_total',
  1632. 'forums_done',
  1633. 'forums_left',
  1634. 'forums_progress',
  1635. //assignments
  1636. 'assignments_total',
  1637. 'assignments_done',
  1638. 'assignments_left',
  1639. 'assignments_progress',
  1640. //Wiki
  1641. 'wiki_total',
  1642. 'wiki_revisions',
  1643. 'wiki_read',
  1644. 'wiki_unread',
  1645. 'wiki_progress',
  1646. //surveys
  1647. 'surveys_total',
  1648. 'surveys_done',
  1649. 'surveys_left',
  1650. 'surveys_progress',
  1651. ];
  1652. $sessionId = 0;
  1653. if (!empty($_GET['course_id']) && !empty($_GET['session_id'])) {
  1654. $sessionId = intval($_GET['session_id']);
  1655. $courseId = intval($_GET['course_id']);
  1656. }
  1657. $result = SessionManager::get_session_progress(
  1658. $sessionId,
  1659. $courseId,
  1660. null,
  1661. null,
  1662. [
  1663. 'where' => $whereCondition,
  1664. 'order' => "$sidx $sord",
  1665. 'limit' => "$start , $limit",
  1666. ]
  1667. );
  1668. break;
  1669. case 'get_session_access_overview':
  1670. $columns = [
  1671. 'logindate',
  1672. 'username',
  1673. 'lastname',
  1674. 'firstname',
  1675. 'clicks',
  1676. 'ip',
  1677. 'timeLoggedIn',
  1678. 'session',
  1679. ];
  1680. $sessionId = 0;
  1681. if (!empty($_GET['course_id']) && !empty($_GET['session_id'])) {
  1682. $sessionId = intval($_GET['session_id']);
  1683. $courseId = intval($_GET['course_id']);
  1684. $studentId = intval($_GET['student_id']);
  1685. $profile = intval($_GET['profile']);
  1686. $date_from = intval($_GET['date_from']);
  1687. $date_to = intval($_GET['date_to']);
  1688. }
  1689. $result = SessionManager::get_user_data_access_tracking_overview(
  1690. $sessionId,
  1691. $courseId,
  1692. $studentId,
  1693. $profile,
  1694. $date_to,
  1695. $date_from,
  1696. [
  1697. 'where' => $whereCondition,
  1698. 'order' => "$sidx $sord",
  1699. 'limit' => "$start , $limit",
  1700. ]
  1701. );
  1702. break;
  1703. case 'get_timelines':
  1704. $columns = ['headline', 'actions'];
  1705. if (!in_array($sidx, $columns)) {
  1706. $sidx = 'headline';
  1707. }
  1708. $course_id = api_get_course_int_id();
  1709. $result = Database::select(
  1710. '*',
  1711. $obj->table,
  1712. [
  1713. 'where' => [
  1714. 'parent_id = ? AND c_id = ?' => ['0', $course_id],
  1715. ],
  1716. 'order' => "$sidx $sord",
  1717. 'LIMIT' => "$start , $limit",
  1718. ]
  1719. );
  1720. $new_result = [];
  1721. foreach ($result as $item) {
  1722. if (!$item['status']) {
  1723. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  1724. }
  1725. $item['headline'] = Display::url($item['headline'], api_get_path(WEB_CODE_PATH).'timeline/view.php?id='.$item['id']);
  1726. $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']);
  1727. $item['actions'] .= Display::url(Display::return_icon('edit.png', get_lang('Edit')), api_get_path(WEB_CODE_PATH).'timeline/?action=edit&id='.$item['id']);
  1728. $item['actions'] .= Display::url(Display::return_icon('delete.png', get_lang('Delete')), api_get_path(WEB_CODE_PATH).'timeline/?action=delete&id='.$item['id']);
  1729. $new_result[] = $item;
  1730. }
  1731. $result = $new_result;
  1732. break;
  1733. case 'get_gradebooks':
  1734. $columns = ['name', 'certificates', 'skills', 'actions', 'has_certificates'];
  1735. if (!in_array($sidx, $columns)) {
  1736. $sidx = 'name';
  1737. }
  1738. $result = Database::select(
  1739. '*',
  1740. $obj->table,
  1741. ['order' => "$sidx $sord", 'LIMIT' => "$start , $limit"]
  1742. );
  1743. $new_result = [];
  1744. foreach ($result as $item) {
  1745. if ($item['parent_id'] != 0) {
  1746. continue;
  1747. }
  1748. $skills = $obj->getSkillsByGradebook($item['id']);
  1749. //Fixes bug when gradebook doesn't have names
  1750. if (empty($item['name'])) {
  1751. $item['name'] = $item['course_code'];
  1752. }
  1753. $item['name'] = Display::url(
  1754. $item['name'],
  1755. api_get_path(WEB_CODE_PATH).'gradebook/index.php?id_session=0&cidReq='.$item['course_code']
  1756. );
  1757. if (!empty($item['certif_min_score']) && !empty($item['document_id'])) {
  1758. $item['certificates'] = Display::return_icon(
  1759. 'accept.png',
  1760. get_lang('WithCertificate'),
  1761. [],
  1762. ICON_SIZE_SMALL
  1763. );
  1764. $item['has_certificates'] = '1';
  1765. } else {
  1766. $item['certificates'] = Display::return_icon(
  1767. 'warning.png',
  1768. get_lang('NoCertificate'),
  1769. [],
  1770. ICON_SIZE_SMALL
  1771. );
  1772. $item['has_certificates'] = '0';
  1773. }
  1774. if (!empty($skills)) {
  1775. $item['skills'] = '';
  1776. foreach ($skills as $skill) {
  1777. $item['skills'] .= Display::span($skill['name'], ['class' => 'label_tag skill']);
  1778. }
  1779. }
  1780. $new_result[] = $item;
  1781. }
  1782. $result = $new_result;
  1783. break;
  1784. case 'get_event_email_template':
  1785. $columns = ['subject', 'event_type_name', 'language_id', 'activated', 'actions'];
  1786. if (!in_array($sidx, $columns)) {
  1787. $sidx = 'subject';
  1788. }
  1789. $result = Database::select(
  1790. '*',
  1791. $obj->table,
  1792. ['order' => "$sidx $sord", 'LIMIT' => "$start , $limit"]
  1793. );
  1794. $new_result = [];
  1795. foreach ($result as $item) {
  1796. $language_info = api_get_language_info($item['language_id']);
  1797. $item['language_id'] = $language_info['english_name'];
  1798. $item['actions'] = Display::url(
  1799. Display::return_icon('edit.png', get_lang('Edit')),
  1800. api_get_path(WEB_CODE_PATH).'admin/event_type.php?action=edit&event_type_name='.$item['event_type_name']
  1801. );
  1802. $item['actions'] .= Display::url(
  1803. Display::return_icon('delete.png', get_lang('Delete')),
  1804. api_get_path(WEB_CODE_PATH).'admin/event_controller.php?action=delete&id='.$item['id']
  1805. );
  1806. $new_result[] = $item;
  1807. }
  1808. $result = $new_result;
  1809. break;
  1810. case 'get_careers':
  1811. $columns = ['name', 'description', 'actions'];
  1812. if (!in_array($sidx, $columns)) {
  1813. $sidx = 'name';
  1814. }
  1815. $result = Database::select(
  1816. '*',
  1817. $obj->table,
  1818. ['order' => "$sidx $sord", 'LIMIT' => "$start , $limit"]
  1819. );
  1820. $new_result = [];
  1821. foreach ($result as $item) {
  1822. if (!$item['status']) {
  1823. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  1824. }
  1825. $new_result[] = $item;
  1826. }
  1827. $result = $new_result;
  1828. break;
  1829. case 'get_promotions':
  1830. $columns = ['name', 'career', 'description', 'actions'];
  1831. if (!in_array($sidx, $columns)) {
  1832. $sidx = 'name';
  1833. }
  1834. $result = Database::select(
  1835. 'p.id,p.name, p.description, c.name as career, p.status',
  1836. "$obj->table p LEFT JOIN ".Database::get_main_table(TABLE_CAREER)." c ON c.id = p.career_id ",
  1837. ['order' => "$sidx $sord", 'LIMIT' => "$start , $limit"]
  1838. );
  1839. $new_result = [];
  1840. foreach ($result as $item) {
  1841. if (!$item['status']) {
  1842. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  1843. }
  1844. $new_result[] = $item;
  1845. }
  1846. $result = $new_result;
  1847. break;
  1848. case 'get_mail_template':
  1849. $columns = ['name', 'type', 'default_template', 'actions'];
  1850. if (!in_array($sidx, $columns)) {
  1851. $sidx = 'name';
  1852. }
  1853. if (!in_array($sidx, $columns)) {
  1854. $sidx = 'name';
  1855. }
  1856. $result = Database::select(
  1857. '*',
  1858. $obj->table,
  1859. [
  1860. 'where' => ['url_id = ? ' => api_get_current_access_url_id()],
  1861. 'order' => "$sidx $sord",
  1862. 'LIMIT' => "$start , $limit",
  1863. ]
  1864. );
  1865. $new_result = [];
  1866. foreach ($result as $item) {
  1867. $new_result[] = $item;
  1868. }
  1869. $result = $new_result;
  1870. break;
  1871. case 'get_grade_models':
  1872. $columns = ['name', 'description', 'actions'];
  1873. if (!in_array($sidx, $columns)) {
  1874. $sidx = 'name';
  1875. }
  1876. $result = Database::select(
  1877. '*',
  1878. "$obj->table ",
  1879. ['order' => "$sidx $sord", 'LIMIT' => "$start , $limit"]
  1880. );
  1881. $new_result = [];
  1882. foreach ($result as $item) {
  1883. $new_result[] = $item;
  1884. }
  1885. $result = $new_result;
  1886. break;
  1887. case 'get_usergroups':
  1888. $obj->protectScript();
  1889. $columns = ['name', 'users', 'courses', 'sessions', 'group_type', 'actions'];
  1890. $result = $obj->getUsergroupsPagination($sidx, $sord, $start, $limit);
  1891. break;
  1892. case 'get_extra_fields':
  1893. $obj = new ExtraField($type);
  1894. $columns = [
  1895. 'display_text',
  1896. 'variable',
  1897. 'field_type',
  1898. 'changeable',
  1899. 'visible_to_self',
  1900. 'visible_to_others',
  1901. 'filter',
  1902. 'field_order',
  1903. ];
  1904. $result = $obj->getAllGrid($sidx, $sord, $start, $limit);
  1905. $new_result = [];
  1906. if (!empty($result)) {
  1907. $checkIcon = Display::return_icon(
  1908. 'check-circle.png',
  1909. get_lang('Yes')
  1910. );
  1911. $timesIcon = Display::return_icon(
  1912. 'closed-circle.png',
  1913. get_lang('No')
  1914. );
  1915. foreach ($result as $item) {
  1916. $item['display_text'] = ExtraField::translateDisplayName(
  1917. $item['variable'],
  1918. $item['displayText']
  1919. );
  1920. $item['field_type'] = $obj->get_field_type_by_id($item['fieldType']);
  1921. $item['changeable'] = $item['changeable'] ? $checkIcon : $timesIcon;
  1922. $item['visible_to_self'] = $item['visibleToSelf'] ? $checkIcon : $timesIcon;
  1923. $item['visible_to_others'] = $item['visibleToOthers'] ? $checkIcon : $timesIcon;
  1924. $item['filter'] = $item['filter'] ? $checkIcon : $timesIcon;
  1925. $new_result[] = $item;
  1926. }
  1927. $result = $new_result;
  1928. }
  1929. break;
  1930. case 'get_exercise_grade':
  1931. $objExercise = new Exercise();
  1932. $exercises = $objExercise->getExercisesByCourseSession(
  1933. $_GET['course_id'],
  1934. $_GET['session_id']
  1935. );
  1936. $cntExer = 4;
  1937. if (!empty($exercises)) {
  1938. $cntExer += count($exercises);
  1939. }
  1940. $columns = [];
  1941. //Get dynamic column names
  1942. $i = 1;
  1943. $column_names = [];
  1944. foreach (range(1, $cntExer) as $cnt) {
  1945. switch ($cnt) {
  1946. case 1:
  1947. $columns[] = 'session';
  1948. $column_names[] = get_lang('Section');
  1949. break;
  1950. case 2:
  1951. $columns[] = 'username';
  1952. $column_names[] = get_lang('Username');
  1953. break;
  1954. case 3:
  1955. $columns[] = 'name';
  1956. $column_names[] = get_lang('Name');
  1957. break;
  1958. case $cntExer:
  1959. $columns[] = 'finalScore';
  1960. $column_names[] = get_lang('FinalScore');
  1961. break;
  1962. default:
  1963. $title = '';
  1964. if (!empty($exercises[$cnt - 4]['title'])) {
  1965. $title = ucwords(strtolower(trim($exercises[$cnt - 4]['title'])));
  1966. }
  1967. $columns[] = 'exer'.$i;
  1968. $column_names[] = $title;
  1969. $i++;
  1970. break;
  1971. }
  1972. }
  1973. $quizIds = [];
  1974. if (!empty($exercises)) {
  1975. foreach ($exercises as $exercise) {
  1976. $quizIds[] = $exercise['id'];
  1977. }
  1978. }
  1979. $course = api_get_course_info_by_id($_GET['course_id']);
  1980. $listUserSess = CourseManager::get_student_list_from_course_code(
  1981. $course['code'],
  1982. true,
  1983. $_GET['session_id']
  1984. );
  1985. $usersId = array_keys($listUserSess);
  1986. $users = UserManager::get_user_list_by_ids(
  1987. $usersId,
  1988. null,
  1989. "lastname, firstname",
  1990. "$start , $limit"
  1991. );
  1992. $exeResults = $objExercise->getExerciseAndResult(
  1993. $_GET['course_id'],
  1994. $_GET['session_id'],
  1995. $quizIds
  1996. );
  1997. $arrGrade = [];
  1998. foreach ($exeResults as $exeResult) {
  1999. $arrGrade[$exeResult['exe_user_id']][$exeResult['exe_exo_id']] = $exeResult['exe_result'];
  2000. }
  2001. $result = [];
  2002. $i = 0;
  2003. foreach ($users as $user) {
  2004. $sessionInfo = SessionManager::fetch($listUserSess[$user['user_id']]['id_session']);
  2005. $result[$i]['session'] = $sessionInfo['name'];
  2006. $result[$i]['username'] = $user['username'];
  2007. $result[$i]['name'] = $user['lastname']." ".$user['firstname'];
  2008. $j = 1;
  2009. $finalScore = 0;
  2010. foreach ($quizIds as $quizID) {
  2011. $grade = '';
  2012. if (!empty($arrGrade[$user['user_id']][$quizID]) || $arrGrade[$user['user_id']][$quizID] == 0) {
  2013. $finalScore += $grade = $arrGrade[$user['user_id']][$quizID];
  2014. }
  2015. $result[$i]['exer'.$j] = $grade;
  2016. $j++;
  2017. }
  2018. if ($finalScore > 20) {
  2019. $finalScore = 20;
  2020. }
  2021. $result[$i]['finalScore'] = number_format($finalScore, 2);
  2022. $i++;
  2023. }
  2024. break;
  2025. case 'get_extra_field_options':
  2026. $obj = new ExtraFieldOption($type);
  2027. $columns = ['display_text', 'option_value', 'option_order'];
  2028. $result = $obj->get_all([
  2029. 'where' => ["field_id = ? " => $field_id],
  2030. 'order' => "$sidx $sord",
  2031. 'LIMIT' => "$start , $limit",
  2032. ]);
  2033. break;
  2034. case 'get_usergroups_teacher':
  2035. $columns = ['name', 'users', 'status', 'group_type', 'actions'];
  2036. $options = ['order' => "name $sord", 'LIMIT' => "$start , $limit"];
  2037. $options['course_id'] = $course_id;
  2038. switch ($type) {
  2039. case 'not_registered':
  2040. $options['where'] = [' (course_id IS NULL OR course_id != ?) ' => $course_id];
  2041. $result = $obj->getUserGroupNotInCourse($options, $groupFilter);
  2042. break;
  2043. case 'registered':
  2044. $options['where'] = [' usergroup.course_id = ? ' => $course_id];
  2045. $result = $obj->getUserGroupInCourse($options, $groupFilter);
  2046. break;
  2047. }
  2048. $new_result = [];
  2049. if (!empty($result)) {
  2050. $urlUserGroup = api_get_path(WEB_CODE_PATH).'admin/usergroup_users.php?'.api_get_cidreq();
  2051. foreach ($result as $group) {
  2052. $count = count($obj->get_users_by_usergroup($group['id']));
  2053. $group['users'] = $count;
  2054. if ($obj->allowTeachers()) {
  2055. $group['users'] = Display::url(
  2056. $count,
  2057. $urlUserGroup.'&id='.$group['id']
  2058. );
  2059. }
  2060. if ($obj->usergroup_was_added_in_course($group['id'], $course_id)) {
  2061. $url = 'class.php?action=remove_class_from_course&id='.$group['id'].'&'.api_get_cidreq();
  2062. $icon = Display::return_icon('delete.png', get_lang('Remove'));
  2063. } else {
  2064. $url = 'class.php?action=add_class_to_course&id='.$group['id'].'&'.api_get_cidreq().'&type=not_registered';
  2065. $icon = Display::return_icon('add.png', get_lang('Add'));
  2066. }
  2067. switch ($group['group_type']) {
  2068. case 0:
  2069. $group['group_type'] = Display::label(get_lang('Class'), 'primary');
  2070. break;
  2071. case 1:
  2072. $group['group_type'] = Display::label(get_lang('Social'), 'success');
  2073. break;
  2074. }
  2075. $role = $obj->getUserRoleToString(api_get_user_id(), $group['id']);
  2076. $group['status'] = $role;
  2077. $group['actions'] = '';
  2078. if ($obj->allowTeachers()) {
  2079. $group['actions'] .= Display::url(
  2080. Display::return_icon('statistics.png'),
  2081. $urlUserGroup.'&id='.$group['id']
  2082. ).'&nbsp;';
  2083. }
  2084. $group['actions'] .= Display::url($icon, $url);
  2085. $new_result[] = $group;
  2086. }
  2087. $result = $new_result;
  2088. }
  2089. if (!in_array($sidx, $columns)) {
  2090. $sidx = 'name';
  2091. }
  2092. // Multidimensional sort
  2093. $result = msort($result, $sidx, $sord);
  2094. break;
  2095. default:
  2096. exit;
  2097. }
  2098. $allowed_actions = [
  2099. 'get_careers',
  2100. 'get_promotions',
  2101. 'get_mail_template',
  2102. 'get_usergroups',
  2103. 'get_usergroups_teacher',
  2104. 'get_gradebooks',
  2105. 'get_sessions',
  2106. 'get_session_access_overview',
  2107. 'get_sessions_tracking',
  2108. 'get_session_lp_progress',
  2109. 'get_survey_overview',
  2110. 'get_session_progress',
  2111. 'get_exercise_progress',
  2112. 'get_exercise_results',
  2113. 'get_exercise_results_report',
  2114. 'get_work_student_list_overview',
  2115. 'get_hotpotatoes_exercise_results',
  2116. 'get_work_teacher',
  2117. 'get_work_student',
  2118. 'get_work_user_list',
  2119. 'get_work_user_list_others',
  2120. 'get_work_user_list_all',
  2121. 'get_timelines',
  2122. 'get_grade_models',
  2123. 'get_event_email_template',
  2124. 'get_user_skill_ranking',
  2125. 'get_extra_fields',
  2126. 'get_extra_field_options',
  2127. //'get_course_exercise_medias',
  2128. 'get_user_course_report',
  2129. 'get_user_course_report_resumed',
  2130. 'get_exercise_grade',
  2131. 'get_group_reporting',
  2132. 'get_course_announcements',
  2133. 'get_programmed_announcements',
  2134. 'course_log_events',
  2135. 'get_learning_path_calendars',
  2136. 'get_usergroups_users',
  2137. 'get_calendar_users',
  2138. 'get_exercise_categories',
  2139. ];
  2140. //5. Creating an obj to return a json
  2141. if (in_array($action, $allowed_actions)) {
  2142. $response = new stdClass();
  2143. $response->page = $page;
  2144. $response->total = $total_pages;
  2145. $response->records = $count;
  2146. if ($operation && $operation == 'excel') {
  2147. $j = 1;
  2148. $array = [];
  2149. if (empty($column_names)) {
  2150. $column_names = $columns;
  2151. }
  2152. // Headers
  2153. foreach ($column_names as $col) {
  2154. // Overwrite titles
  2155. if (isset($overwriteColumnHeaderExport[$col])) {
  2156. $col = $overwriteColumnHeaderExport[$col];
  2157. }
  2158. $array[0][] = $col;
  2159. }
  2160. foreach ($result as $row) {
  2161. foreach ($columns as $col) {
  2162. $array[$j][] = strip_tags($row[$col]);
  2163. }
  2164. $j++;
  2165. }
  2166. $fileName = !empty($action) ? $action : 'company_report';
  2167. if (!empty($exportFilename)) {
  2168. $fileName = $exportFilename;
  2169. }
  2170. switch ($exportFormat) {
  2171. case 'xls':
  2172. Export::arrayToXls($array, $fileName);
  2173. break;
  2174. case 'xls_html':
  2175. //TODO add date if exists
  2176. $browser = new Browser();
  2177. if ($browser->getPlatform() == Browser::PLATFORM_WINDOWS) {
  2178. Export::export_table_xls_html($array, $fileName, 'ISO-8859-15');
  2179. } else {
  2180. Export::export_table_xls_html($array, $fileName);
  2181. }
  2182. break;
  2183. case 'csv':
  2184. default:
  2185. Export::arrayToCsv($array, $fileName);
  2186. break;
  2187. }
  2188. exit;
  2189. }
  2190. $i = 0;
  2191. if (!empty($result)) {
  2192. foreach ($result as $row) {
  2193. // if results tab give not id, set id to $i otherwise id="null"
  2194. // for all <tr> of the jqgrid - ref #4235
  2195. if (!isset($row['id']) || isset($row['id']) && $row['id'] == '') {
  2196. $response->rows[$i]['id'] = $i;
  2197. } else {
  2198. $response->rows[$i]['id'] = $row['id'];
  2199. }
  2200. $array = [];
  2201. foreach ($columns as $col) {
  2202. if (in_array($col, ['correction', 'actions'])) {
  2203. $array[] = isset($row[$col]) ? $row[$col] : '';
  2204. } else {
  2205. $array[] = isset($row[$col]) ? Security::remove_XSS($row[$col]) : '';
  2206. }
  2207. }
  2208. $response->rows[$i]['cell'] = $array;
  2209. $i++;
  2210. }
  2211. }
  2212. header('Content-Type: application/json;charset=utf-8');
  2213. echo json_encode($response);
  2214. }
  2215. exit;