model.ajax.php 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400
  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. $plagiarismColumns = [];
  1122. if (api_get_configuration_value('allow_compilatio_tool')) {
  1123. $plagiarismColumns = ['compilatio'];
  1124. }
  1125. if (isset($_GET['type']) && $_GET['type'] === 'simple') {
  1126. $columns = [
  1127. 'fullname',
  1128. 'title',
  1129. 'qualification',
  1130. 'sent_date',
  1131. 'qualificator_id',
  1132. 'correction',
  1133. ];
  1134. $columns = array_merge($columns, $plagiarismColumns);
  1135. $columns[] = 'actions';
  1136. } else {
  1137. $columns = [
  1138. 'fullname',
  1139. 'title',
  1140. 'qualification',
  1141. 'sent_date',
  1142. 'correction',
  1143. ];
  1144. $columns = array_merge($columns, $plagiarismColumns);
  1145. $columns[] = 'actions';
  1146. }
  1147. $whereCondition = " AND $whereCondition ";
  1148. $result = get_work_user_list(
  1149. $start,
  1150. $limit,
  1151. $sidx,
  1152. $sord,
  1153. $work_id,
  1154. $whereCondition
  1155. );
  1156. break;
  1157. case 'get_work_user_list_others':
  1158. $plagiarismColumns = [];
  1159. if (api_get_configuration_value('allow_compilatio_tool')) {
  1160. $plagiarismColumns = ['compilatio'];
  1161. }
  1162. if (isset($_GET['type']) && $_GET['type'] === 'simple') {
  1163. $columns = [
  1164. 'type', 'firstname', 'lastname', 'title', 'qualification', 'sent_date', 'qualificator_id',
  1165. ];
  1166. $columns = array_merge($columns, $plagiarismColumns);
  1167. $columns[] = 'actions';
  1168. } else {
  1169. $columns = ['type', 'firstname', 'lastname', 'title', 'sent_date'];
  1170. $columns = array_merge($columns, $plagiarismColumns);
  1171. $columns[] = 'actions';
  1172. }
  1173. if (trim($whereCondition) === '1 = 1') {
  1174. $whereCondition = '';
  1175. }
  1176. $whereCondition .= " AND u.user_id <> ".api_get_user_id();
  1177. $result = get_work_user_list(
  1178. $start,
  1179. $limit,
  1180. $sidx,
  1181. $sord,
  1182. $work_id,
  1183. $whereCondition
  1184. );
  1185. break;
  1186. case 'get_work_user_list':
  1187. $plagiarismColumns = [];
  1188. if (api_get_configuration_value('allow_compilatio_tool')) {
  1189. $plagiarismColumns = ['compilatio'];
  1190. }
  1191. if (isset($_GET['type']) && $_GET['type'] == 'simple') {
  1192. $columns = [
  1193. 'type', 'title', 'qualification', 'sent_date', 'qualificator_id',
  1194. ];
  1195. $columns = array_merge($columns, $plagiarismColumns);
  1196. $columns[] = 'actions';
  1197. } else {
  1198. $columns = ['type', 'title', 'qualification', 'sent_date'];
  1199. $columns = array_merge($columns, $plagiarismColumns);
  1200. $columns[] = 'actions';
  1201. }
  1202. $documents = getAllDocumentToWork($work_id, api_get_course_int_id());
  1203. if (trim($whereCondition) === '1 = 1') {
  1204. $whereCondition = '';
  1205. }
  1206. if (empty($documents)) {
  1207. $whereCondition .= ' AND u.user_id = '.api_get_user_id();
  1208. $result = get_work_user_list(
  1209. $start,
  1210. $limit,
  1211. $sidx,
  1212. $sord,
  1213. $work_id,
  1214. $whereCondition
  1215. );
  1216. } else {
  1217. $result = get_work_user_list_from_documents(
  1218. $start,
  1219. $limit,
  1220. $sidx,
  1221. $sord,
  1222. $work_id,
  1223. api_get_user_id(),
  1224. $whereCondition
  1225. );
  1226. }
  1227. break;
  1228. case 'get_exercise_results':
  1229. $is_allowedToEdit = api_is_allowed_to_edit(null, true) ||
  1230. api_is_drh() ||
  1231. api_is_student_boss() ||
  1232. api_is_session_admin();
  1233. if ($is_allowedToEdit || api_is_student_boss()) {
  1234. $columns = [
  1235. 'firstname',
  1236. 'lastname',
  1237. 'username',
  1238. 'group_name',
  1239. 'exe_duration',
  1240. 'start_date',
  1241. 'exe_date',
  1242. 'score',
  1243. 'user_ip',
  1244. 'status',
  1245. 'lp',
  1246. 'actions',
  1247. ];
  1248. $officialCodeInList = api_get_setting('show_official_code_exercise_result_list');
  1249. if ($officialCodeInList === 'true') {
  1250. $columns = array_merge(['official_code'], $columns);
  1251. }
  1252. }
  1253. $result = ExerciseLib::get_exam_results_data(
  1254. $start,
  1255. $limit,
  1256. $sidx,
  1257. $sord,
  1258. $exercise_id,
  1259. $whereCondition
  1260. );
  1261. break;
  1262. case 'get_exercise_results_report':
  1263. $columns = [
  1264. 'firstname',
  1265. 'lastname',
  1266. 'username',
  1267. ];
  1268. $extraFieldsToAdd = [];
  1269. $extraFields = api_get_configuration_value('exercise_category_report_user_extra_fields');
  1270. $roundValues = api_get_configuration_value('exercise_category_round_score_in_export');
  1271. if (!empty($extraFields) && isset($extraFields['fields'])) {
  1272. $extraField = new ExtraField('user');
  1273. foreach ($extraFields['fields'] as $variable) {
  1274. $info = $extraField->get_handler_field_info_by_field_variable($variable);
  1275. if ($info) {
  1276. $extraFieldsToAdd[] = $variable;
  1277. }
  1278. }
  1279. }
  1280. if (!empty($extraFieldsToAdd)) {
  1281. $columns = array_merge($columns, $extraFieldsToAdd);
  1282. }
  1283. $columns[] = 'session';
  1284. $columns[] = 'session_access_start_date';
  1285. $columns[] = 'exe_date';
  1286. $columns[] = 'score';
  1287. if ($operation === 'excel') {
  1288. $columns = [
  1289. 'firstname',
  1290. 'lastname',
  1291. 'username',
  1292. ];
  1293. if (!empty($extraFieldsToAdd)) {
  1294. $columns = array_merge($columns, $extraFieldsToAdd);
  1295. }
  1296. $columns[] = 'session';
  1297. $columns[] = 'session_access_start_date';
  1298. $columns[] = 'exe_date';
  1299. $columns[] = 'score_percentage';
  1300. $columns[] = 'only_score';
  1301. $columns[] = 'total';
  1302. $overwriteColumnHeaderExport['session_access_start_date'] = get_lang('SessionStartDate');
  1303. $overwriteColumnHeaderExport['exe_date'] = get_lang('StartDate');
  1304. $overwriteColumnHeaderExport['score_percentage'] = get_lang('Score').' - '.get_lang('Percentage');
  1305. $overwriteColumnHeaderExport['only_score'] = get_lang('Score').' - '.get_lang('ScoreNote');
  1306. $overwriteColumnHeaderExport['total'] = get_lang('Score').' - '.get_lang('ScoreTest');
  1307. }
  1308. $categoryList = TestCategory::getListOfCategoriesIDForTest($exerciseId, $courseId);
  1309. if (!empty($categoryList)) {
  1310. foreach ($categoryList as $categoryInfo) {
  1311. $label = 'category_'.$categoryInfo['id'];
  1312. if ($operation == 'excel') {
  1313. $columns[] = $label.'_score_percentage';
  1314. $columns[] = $label.'_only_score';
  1315. $columns[] = $label.'_total';
  1316. $overwriteColumnHeaderExport[$label] = $categoryInfo['title'];
  1317. $overwriteColumnHeaderExport[$label.'_score_percentage'] = $categoryInfo['title'].
  1318. ' - '.get_lang('Percentage');
  1319. $overwriteColumnHeaderExport[$label.'_only_score'] = $categoryInfo['title'].
  1320. ' - '.get_lang('ScoreNote');
  1321. $overwriteColumnHeaderExport[$label.'_total'] = $categoryInfo['title'].
  1322. ' - '.get_lang('ScoreTest');
  1323. } else {
  1324. $columns[] = $label;
  1325. }
  1326. }
  1327. }
  1328. if ($operation !== 'excel') {
  1329. $columns[] = 'actions';
  1330. }
  1331. $whereCondition .= " AND te.status = '' ";
  1332. $result = ExerciseLib::get_exam_results_data(
  1333. $start,
  1334. $limit,
  1335. $sidx,
  1336. $sord,
  1337. $exerciseId,
  1338. $whereCondition,
  1339. false,
  1340. $courseInfo['code'],
  1341. true,
  1342. true,
  1343. $extraFieldsToAdd,
  1344. true,
  1345. $roundValues
  1346. );
  1347. break;
  1348. case 'get_hotpotatoes_exercise_results':
  1349. $course = api_get_course_info();
  1350. $documentPath = api_get_path(SYS_COURSE_PATH).$course['path']."/document";
  1351. if (api_is_allowed_to_edit()) {
  1352. $columns = ['firstname', 'lastname', 'username', 'group_name', 'exe_date', 'score', 'actions'];
  1353. } else {
  1354. $columns = ['exe_date', 'score', 'actions'];
  1355. }
  1356. $result = ExerciseLib::get_exam_results_hotpotatoes_data(
  1357. $start,
  1358. $limit,
  1359. $sidx,
  1360. $sord,
  1361. $hotpot_path,
  1362. $whereCondition
  1363. );
  1364. break;
  1365. case 'get_work_student_list_overview':
  1366. if (!(api_is_allowed_to_edit() || api_is_coach())) {
  1367. return [];
  1368. }
  1369. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  1370. $columns = [
  1371. 'student', 'works',
  1372. ];
  1373. $result = getWorkUserListData(
  1374. $workId,
  1375. api_get_course_id(),
  1376. api_get_session_id(),
  1377. api_get_group_id(),
  1378. $start,
  1379. $limit,
  1380. $sidx,
  1381. $sord
  1382. );
  1383. break;
  1384. case 'get_hotpotatoes_exercise_results':
  1385. $course = api_get_course_info();
  1386. $documentPath = api_get_path(SYS_COURSE_PATH).$course['path']."/document";
  1387. if (api_is_allowed_to_edit(null, true) || api_is_drh()) {
  1388. $columns = ['firstname', 'lastname', 'username', 'group_name', 'exe_date', 'score', 'actions'];
  1389. } else {
  1390. $columns = ['exe_date', 'score', 'actions'];
  1391. }
  1392. $result = ExerciseLib::get_exam_results_hotpotatoes_data(
  1393. $start,
  1394. $limit,
  1395. $sidx,
  1396. $sord,
  1397. $hotpot_path,
  1398. $whereCondition
  1399. );
  1400. break;
  1401. case 'get_sessions_tracking':
  1402. if (api_is_drh() || api_is_session_admin()) {
  1403. $orderByName = Database::escape_string($sidx);
  1404. $orderByName = in_array($orderByName, ['name', 'access_start_date']) ? $orderByName : 'name';
  1405. $orderBy = " ORDER BY $orderByName $sord";
  1406. $sessions = SessionManager::get_sessions_followed_by_drh(
  1407. api_get_user_id(),
  1408. $start,
  1409. $limit,
  1410. false,
  1411. false,
  1412. false,
  1413. $orderBy,
  1414. $keyword,
  1415. $description,
  1416. ['where' => $whereCondition, 'extra' => $extra_fields]
  1417. );
  1418. } else {
  1419. // Sessions for the coach
  1420. $sessions = Tracking::get_sessions_coached_by_user(
  1421. api_get_user_id(),
  1422. $start,
  1423. $limit,
  1424. false,
  1425. $keyword,
  1426. $description,
  1427. $sidx,
  1428. $sord,
  1429. ['where' => $whereCondition, 'extra' => $extra_fields]
  1430. );
  1431. }
  1432. $session_columns = SessionManager::getGridColumns('my_space');
  1433. $columns = $session_columns['simple_column_name'];
  1434. $result = [];
  1435. if (!empty($sessions)) {
  1436. foreach ($sessions as $session) {
  1437. if (api_drh_can_access_all_session_content()) {
  1438. $count_courses_in_session = SessionManager::get_course_list_by_session_id(
  1439. $session['id'],
  1440. '',
  1441. null,
  1442. true
  1443. );
  1444. } else {
  1445. $count_courses_in_session = count(
  1446. Tracking::get_courses_followed_by_coach(
  1447. $user_id,
  1448. $session['id']
  1449. )
  1450. );
  1451. }
  1452. $count_users_in_session = SessionManager::get_users_by_session(
  1453. $session['id'],
  1454. 0,
  1455. true
  1456. );
  1457. $session['display_start_date'] = '';
  1458. $session['display_end_date'] = '';
  1459. $session['coach_access_start_date'] = '';
  1460. $session['coach_access_end_date'] = '';
  1461. $dateData = SessionManager::parseSessionDates($session, true);
  1462. $dateToString = $dateData['access'];
  1463. $detailButtons = [];
  1464. $detailButtons[] = Display::url(
  1465. Display::return_icon('works.png', get_lang('WorksReport')),
  1466. api_get_path(WEB_CODE_PATH).'mySpace/works_in_session_report.php?session='.$session['id']
  1467. );
  1468. $detailButtons[] = Display::url(
  1469. Display::return_icon('2rightarrow.png'),
  1470. api_get_path(WEB_CODE_PATH).'mySpace/course.php?session_id='.$session['id']
  1471. );
  1472. $item = [
  1473. 'name' => Display::url(
  1474. $session['name'],
  1475. api_get_path(WEB_CODE_PATH).'mySpace/course.php?session_id='.$session['id']
  1476. ),
  1477. 'date' => $dateToString,
  1478. 'course_per_session' => $count_courses_in_session,
  1479. 'student_per_session' => $count_users_in_session,
  1480. 'actions' => implode(' ', $detailButtons),
  1481. ];
  1482. if (!empty($extra_fields)) {
  1483. foreach ($extra_fields as $extraField) {
  1484. $item[$extraField['field']] = $extraField['data'];
  1485. }
  1486. }
  1487. $result[] = $item;
  1488. }
  1489. }
  1490. break;
  1491. case 'get_sessions':
  1492. $session_columns = SessionManager::getGridColumns($list_type);
  1493. $columns = $session_columns['simple_column_name'];
  1494. if ($list_type == 'simple') {
  1495. $result = SessionManager::formatSessionsAdminForGrid(
  1496. [
  1497. 'where' => $whereCondition,
  1498. 'order' => "$sidx $sord, s.name",
  1499. 'extra' => $extra_fields,
  1500. 'limit' => "$start , $limit",
  1501. ],
  1502. false,
  1503. $session_columns
  1504. );
  1505. } else {
  1506. $result = SessionManager::get_sessions_admin_complete(
  1507. [
  1508. 'where' => $whereCondition,
  1509. 'order' => "$sidx $sord, s.name",
  1510. 'extra' => $extra_fields,
  1511. 'limit' => "$start , $limit",
  1512. ]
  1513. );
  1514. }
  1515. break;
  1516. case 'get_exercise_progress':
  1517. $sessionId = (int) $_GET['session_id'];
  1518. $courseId = (int) $_GET['course_id'];
  1519. $exerciseId = (int) $_GET['exercise_id'];
  1520. $date_from = $_GET['date_from'];
  1521. $date_to = $_GET['date_to'];
  1522. $columns = [
  1523. 'session',
  1524. 'exercise_id',
  1525. 'quiz_title',
  1526. 'username',
  1527. 'lastname',
  1528. 'firstname',
  1529. 'time',
  1530. 'question_id',
  1531. 'question',
  1532. 'description',
  1533. 'answer',
  1534. 'correct',
  1535. ];
  1536. $result = Tracking::get_exercise_progress(
  1537. $sessionId,
  1538. $courseId,
  1539. $exerciseId,
  1540. $date_from,
  1541. $date_to,
  1542. [
  1543. 'where' => $whereCondition,
  1544. 'order' => "$sidx $sord",
  1545. 'limit' => "$start , $limit",
  1546. ]
  1547. );
  1548. break;
  1549. case 'get_session_lp_progress':
  1550. $sessionId = 0;
  1551. if (!empty($_GET['session_id']) && !empty($_GET['course_id'])) {
  1552. $sessionId = (int) $_GET['session_id'];
  1553. $courseId = (int) $_GET['course_id'];
  1554. $course = api_get_course_info_by_id($courseId);
  1555. }
  1556. /**
  1557. * Add lessons of course.
  1558. */
  1559. $columns = [
  1560. 'username',
  1561. 'firstname',
  1562. 'lastname',
  1563. ];
  1564. $lessons = LearnpathList::get_course_lessons(
  1565. $course['code'],
  1566. $sessionId
  1567. );
  1568. foreach ($lessons as $lesson_id => $lesson) {
  1569. $columns[] = $lesson_id;
  1570. }
  1571. $columns[] = 'total';
  1572. $result = SessionManager::get_session_lp_progress(
  1573. $sessionId,
  1574. $courseId,
  1575. $date_from,
  1576. $date_to,
  1577. [
  1578. 'where' => $whereCondition,
  1579. 'order' => "$sidx $sord",
  1580. 'limit' => "$start , $limit",
  1581. ]
  1582. );
  1583. break;
  1584. case 'get_survey_overview':
  1585. $sessionId = 0;
  1586. if (!empty($_GET['session_id']) &&
  1587. !empty($_GET['course_id']) &&
  1588. !empty($_GET['survey_id'])
  1589. ) {
  1590. $sessionId = intval($_GET['session_id']);
  1591. $courseId = intval($_GET['course_id']);
  1592. $surveyId = intval($_GET['survey_id']);
  1593. $date_from = $_GET['date_from'];
  1594. $date_to = $_GET['date_to'];
  1595. //$course = api_get_course_info_by_id($courseId);
  1596. }
  1597. /**
  1598. * Add lessons of course.
  1599. */
  1600. $columns = [
  1601. 'username',
  1602. 'firstname',
  1603. 'lastname',
  1604. ];
  1605. $questions = SurveyManager::get_questions($surveyId, $courseId);
  1606. foreach ($questions as $question_id => $question) {
  1607. $columns[] = $question_id;
  1608. }
  1609. $result = SessionManager::get_survey_overview(
  1610. $sessionId,
  1611. $courseId,
  1612. $surveyId,
  1613. $date_from,
  1614. $date_to,
  1615. [
  1616. 'where' => $whereCondition,
  1617. 'order' => "$sidx $sord",
  1618. 'limit' => "$start , $limit",
  1619. ]
  1620. );
  1621. break;
  1622. case 'get_session_progress':
  1623. $columns = [
  1624. 'lastname',
  1625. 'firstname',
  1626. 'username',
  1627. //'profile',
  1628. 'total',
  1629. 'courses',
  1630. 'lessons',
  1631. 'exercises',
  1632. 'forums',
  1633. 'homeworks',
  1634. 'wikis',
  1635. 'surveys',
  1636. //exercises
  1637. 'lessons_total',
  1638. 'lessons_done',
  1639. 'lessons_left',
  1640. 'lessons_progress',
  1641. //exercises
  1642. 'exercises_total',
  1643. 'exercises_done',
  1644. 'exercises_left',
  1645. 'exercises_progress',
  1646. //forums
  1647. 'forums_total',
  1648. 'forums_done',
  1649. 'forums_left',
  1650. 'forums_progress',
  1651. //assignments
  1652. 'assignments_total',
  1653. 'assignments_done',
  1654. 'assignments_left',
  1655. 'assignments_progress',
  1656. //Wiki
  1657. 'wiki_total',
  1658. 'wiki_revisions',
  1659. 'wiki_read',
  1660. 'wiki_unread',
  1661. 'wiki_progress',
  1662. //surveys
  1663. 'surveys_total',
  1664. 'surveys_done',
  1665. 'surveys_left',
  1666. 'surveys_progress',
  1667. ];
  1668. $sessionId = 0;
  1669. if (!empty($_GET['course_id']) && !empty($_GET['session_id'])) {
  1670. $sessionId = intval($_GET['session_id']);
  1671. $courseId = intval($_GET['course_id']);
  1672. }
  1673. $result = SessionManager::get_session_progress(
  1674. $sessionId,
  1675. $courseId,
  1676. null,
  1677. null,
  1678. [
  1679. 'where' => $whereCondition,
  1680. 'order' => "$sidx $sord",
  1681. 'limit' => "$start , $limit",
  1682. ]
  1683. );
  1684. break;
  1685. case 'get_session_access_overview':
  1686. $columns = [
  1687. 'logindate',
  1688. 'username',
  1689. 'lastname',
  1690. 'firstname',
  1691. 'clicks',
  1692. 'ip',
  1693. 'timeLoggedIn',
  1694. 'session',
  1695. ];
  1696. $sessionId = 0;
  1697. if (!empty($_GET['course_id']) && !empty($_GET['session_id'])) {
  1698. $sessionId = intval($_GET['session_id']);
  1699. $courseId = intval($_GET['course_id']);
  1700. $studentId = intval($_GET['student_id']);
  1701. $profile = intval($_GET['profile']);
  1702. $date_from = intval($_GET['date_from']);
  1703. $date_to = intval($_GET['date_to']);
  1704. }
  1705. $result = SessionManager::get_user_data_access_tracking_overview(
  1706. $sessionId,
  1707. $courseId,
  1708. $studentId,
  1709. $profile,
  1710. $date_to,
  1711. $date_from,
  1712. [
  1713. 'where' => $whereCondition,
  1714. 'order' => "$sidx $sord",
  1715. 'limit' => "$start , $limit",
  1716. ]
  1717. );
  1718. break;
  1719. case 'get_timelines':
  1720. $columns = ['headline', 'actions'];
  1721. if (!in_array($sidx, $columns)) {
  1722. $sidx = 'headline';
  1723. }
  1724. $course_id = api_get_course_int_id();
  1725. $result = Database::select(
  1726. '*',
  1727. $obj->table,
  1728. [
  1729. 'where' => [
  1730. 'parent_id = ? AND c_id = ?' => ['0', $course_id],
  1731. ],
  1732. 'order' => "$sidx $sord",
  1733. 'LIMIT' => "$start , $limit",
  1734. ]
  1735. );
  1736. $new_result = [];
  1737. foreach ($result as $item) {
  1738. if (!$item['status']) {
  1739. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  1740. }
  1741. $item['headline'] = Display::url($item['headline'], api_get_path(WEB_CODE_PATH).'timeline/view.php?id='.$item['id']);
  1742. $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']);
  1743. $item['actions'] .= Display::url(Display::return_icon('edit.png', get_lang('Edit')), api_get_path(WEB_CODE_PATH).'timeline/?action=edit&id='.$item['id']);
  1744. $item['actions'] .= Display::url(Display::return_icon('delete.png', get_lang('Delete')), api_get_path(WEB_CODE_PATH).'timeline/?action=delete&id='.$item['id']);
  1745. $new_result[] = $item;
  1746. }
  1747. $result = $new_result;
  1748. break;
  1749. case 'get_gradebooks':
  1750. $columns = ['name', 'certificates', 'skills', 'actions', 'has_certificates'];
  1751. if (!in_array($sidx, $columns)) {
  1752. $sidx = 'name';
  1753. }
  1754. $result = Database::select(
  1755. '*',
  1756. $obj->table,
  1757. ['order' => "$sidx $sord", 'LIMIT' => "$start , $limit"]
  1758. );
  1759. $new_result = [];
  1760. foreach ($result as $item) {
  1761. if ($item['parent_id'] != 0) {
  1762. continue;
  1763. }
  1764. $skills = $obj->getSkillsByGradebook($item['id']);
  1765. //Fixes bug when gradebook doesn't have names
  1766. if (empty($item['name'])) {
  1767. $item['name'] = $item['course_code'];
  1768. }
  1769. $item['name'] = Display::url(
  1770. $item['name'],
  1771. api_get_path(WEB_CODE_PATH).'gradebook/index.php?id_session=0&cidReq='.$item['course_code']
  1772. );
  1773. if (!empty($item['certif_min_score']) && !empty($item['document_id'])) {
  1774. $item['certificates'] = Display::return_icon(
  1775. 'accept.png',
  1776. get_lang('WithCertificate'),
  1777. [],
  1778. ICON_SIZE_SMALL
  1779. );
  1780. $item['has_certificates'] = '1';
  1781. } else {
  1782. $item['certificates'] = Display::return_icon(
  1783. 'warning.png',
  1784. get_lang('NoCertificate'),
  1785. [],
  1786. ICON_SIZE_SMALL
  1787. );
  1788. $item['has_certificates'] = '0';
  1789. }
  1790. if (!empty($skills)) {
  1791. $item['skills'] = '';
  1792. foreach ($skills as $skill) {
  1793. $item['skills'] .= Display::span($skill['name'], ['class' => 'label_tag skill']);
  1794. }
  1795. }
  1796. $new_result[] = $item;
  1797. }
  1798. $result = $new_result;
  1799. break;
  1800. case 'get_event_email_template':
  1801. $columns = ['subject', 'event_type_name', 'language_id', 'activated', 'actions'];
  1802. if (!in_array($sidx, $columns)) {
  1803. $sidx = 'subject';
  1804. }
  1805. $result = Database::select(
  1806. '*',
  1807. $obj->table,
  1808. ['order' => "$sidx $sord", 'LIMIT' => "$start , $limit"]
  1809. );
  1810. $new_result = [];
  1811. foreach ($result as $item) {
  1812. $language_info = api_get_language_info($item['language_id']);
  1813. $item['language_id'] = $language_info['english_name'];
  1814. $item['actions'] = Display::url(
  1815. Display::return_icon('edit.png', get_lang('Edit')),
  1816. api_get_path(WEB_CODE_PATH).'admin/event_type.php?action=edit&event_type_name='.$item['event_type_name']
  1817. );
  1818. $item['actions'] .= Display::url(
  1819. Display::return_icon('delete.png', get_lang('Delete')),
  1820. api_get_path(WEB_CODE_PATH).'admin/event_controller.php?action=delete&id='.$item['id']
  1821. );
  1822. $new_result[] = $item;
  1823. }
  1824. $result = $new_result;
  1825. break;
  1826. case 'get_careers':
  1827. $columns = ['name', 'description', 'actions'];
  1828. if (!in_array($sidx, $columns)) {
  1829. $sidx = 'name';
  1830. }
  1831. $result = Database::select(
  1832. '*',
  1833. $obj->table,
  1834. ['order' => "$sidx $sord", 'LIMIT' => "$start , $limit"]
  1835. );
  1836. $new_result = [];
  1837. foreach ($result as $item) {
  1838. if (!$item['status']) {
  1839. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  1840. }
  1841. $new_result[] = $item;
  1842. }
  1843. $result = $new_result;
  1844. break;
  1845. case 'get_promotions':
  1846. $columns = ['name', 'career', 'description', 'actions'];
  1847. if (!in_array($sidx, $columns)) {
  1848. $sidx = 'name';
  1849. }
  1850. $result = Database::select(
  1851. 'p.id,p.name, p.description, c.name as career, p.status',
  1852. "$obj->table p LEFT JOIN ".Database::get_main_table(TABLE_CAREER)." c ON c.id = p.career_id ",
  1853. ['order' => "$sidx $sord", 'LIMIT' => "$start , $limit"]
  1854. );
  1855. $new_result = [];
  1856. foreach ($result as $item) {
  1857. if (!$item['status']) {
  1858. $item['name'] = '<font style="color:#AAA">'.$item['name'].'</font>';
  1859. }
  1860. $new_result[] = $item;
  1861. }
  1862. $result = $new_result;
  1863. break;
  1864. case 'get_mail_template':
  1865. $columns = ['name', 'type', 'default_template', 'actions'];
  1866. if (!in_array($sidx, $columns)) {
  1867. $sidx = 'name';
  1868. }
  1869. if (!in_array($sidx, $columns)) {
  1870. $sidx = 'name';
  1871. }
  1872. $result = Database::select(
  1873. '*',
  1874. $obj->table,
  1875. [
  1876. 'where' => ['url_id = ? ' => api_get_current_access_url_id()],
  1877. 'order' => "$sidx $sord",
  1878. 'LIMIT' => "$start , $limit",
  1879. ]
  1880. );
  1881. $new_result = [];
  1882. foreach ($result as $item) {
  1883. $new_result[] = $item;
  1884. }
  1885. $result = $new_result;
  1886. break;
  1887. case 'get_grade_models':
  1888. $columns = ['name', 'description', 'actions'];
  1889. if (!in_array($sidx, $columns)) {
  1890. $sidx = 'name';
  1891. }
  1892. $result = Database::select(
  1893. '*',
  1894. "$obj->table ",
  1895. ['order' => "$sidx $sord", 'LIMIT' => "$start , $limit"]
  1896. );
  1897. $new_result = [];
  1898. foreach ($result as $item) {
  1899. $new_result[] = $item;
  1900. }
  1901. $result = $new_result;
  1902. break;
  1903. case 'get_usergroups':
  1904. $obj->protectScript();
  1905. $columns = ['name', 'users', 'courses', 'sessions', 'group_type', 'actions'];
  1906. $result = $obj->getUsergroupsPagination($sidx, $sord, $start, $limit);
  1907. break;
  1908. case 'get_extra_fields':
  1909. $obj = new ExtraField($type);
  1910. $columns = [
  1911. 'display_text',
  1912. 'variable',
  1913. 'field_type',
  1914. 'changeable',
  1915. 'visible_to_self',
  1916. 'visible_to_others',
  1917. 'filter',
  1918. 'field_order',
  1919. ];
  1920. $result = $obj->getAllGrid($sidx, $sord, $start, $limit);
  1921. $new_result = [];
  1922. if (!empty($result)) {
  1923. $checkIcon = Display::return_icon(
  1924. 'check-circle.png',
  1925. get_lang('Yes')
  1926. );
  1927. $timesIcon = Display::return_icon(
  1928. 'closed-circle.png',
  1929. get_lang('No')
  1930. );
  1931. foreach ($result as $item) {
  1932. $item['display_text'] = ExtraField::translateDisplayName(
  1933. $item['variable'],
  1934. $item['displayText']
  1935. );
  1936. $item['field_type'] = $obj->get_field_type_by_id($item['fieldType']);
  1937. $item['changeable'] = $item['changeable'] ? $checkIcon : $timesIcon;
  1938. $item['visible_to_self'] = $item['visibleToSelf'] ? $checkIcon : $timesIcon;
  1939. $item['visible_to_others'] = $item['visibleToOthers'] ? $checkIcon : $timesIcon;
  1940. $item['filter'] = $item['filter'] ? $checkIcon : $timesIcon;
  1941. $new_result[] = $item;
  1942. }
  1943. $result = $new_result;
  1944. }
  1945. break;
  1946. case 'get_exercise_grade':
  1947. $objExercise = new Exercise();
  1948. $exercises = $objExercise->getExercisesByCourseSession(
  1949. $_GET['course_id'],
  1950. $_GET['session_id']
  1951. );
  1952. $cntExer = 4;
  1953. if (!empty($exercises)) {
  1954. $cntExer += count($exercises);
  1955. }
  1956. $columns = [];
  1957. //Get dynamic column names
  1958. $i = 1;
  1959. $column_names = [];
  1960. foreach (range(1, $cntExer) as $cnt) {
  1961. switch ($cnt) {
  1962. case 1:
  1963. $columns[] = 'session';
  1964. $column_names[] = get_lang('Section');
  1965. break;
  1966. case 2:
  1967. $columns[] = 'username';
  1968. $column_names[] = get_lang('Username');
  1969. break;
  1970. case 3:
  1971. $columns[] = 'name';
  1972. $column_names[] = get_lang('Name');
  1973. break;
  1974. case $cntExer:
  1975. $columns[] = 'finalScore';
  1976. $column_names[] = get_lang('FinalScore');
  1977. break;
  1978. default:
  1979. $title = '';
  1980. if (!empty($exercises[$cnt - 4]['title'])) {
  1981. $title = ucwords(strtolower(trim($exercises[$cnt - 4]['title'])));
  1982. }
  1983. $columns[] = 'exer'.$i;
  1984. $column_names[] = $title;
  1985. $i++;
  1986. break;
  1987. }
  1988. }
  1989. $quizIds = [];
  1990. if (!empty($exercises)) {
  1991. foreach ($exercises as $exercise) {
  1992. $quizIds[] = $exercise['id'];
  1993. }
  1994. }
  1995. $course = api_get_course_info_by_id($_GET['course_id']);
  1996. $listUserSess = CourseManager::get_student_list_from_course_code(
  1997. $course['code'],
  1998. true,
  1999. $_GET['session_id']
  2000. );
  2001. $usersId = array_keys($listUserSess);
  2002. $users = UserManager::get_user_list_by_ids(
  2003. $usersId,
  2004. null,
  2005. "lastname, firstname",
  2006. "$start , $limit"
  2007. );
  2008. $exeResults = $objExercise->getExerciseAndResult(
  2009. $_GET['course_id'],
  2010. $_GET['session_id'],
  2011. $quizIds
  2012. );
  2013. $arrGrade = [];
  2014. foreach ($exeResults as $exeResult) {
  2015. $arrGrade[$exeResult['exe_user_id']][$exeResult['exe_exo_id']] = $exeResult['exe_result'];
  2016. }
  2017. $result = [];
  2018. $i = 0;
  2019. foreach ($users as $user) {
  2020. $sessionInfo = SessionManager::fetch($listUserSess[$user['user_id']]['id_session']);
  2021. $result[$i]['session'] = $sessionInfo['name'];
  2022. $result[$i]['username'] = $user['username'];
  2023. $result[$i]['name'] = $user['lastname']." ".$user['firstname'];
  2024. $j = 1;
  2025. $finalScore = 0;
  2026. foreach ($quizIds as $quizID) {
  2027. $grade = '';
  2028. if (!empty($arrGrade[$user['user_id']][$quizID]) || $arrGrade[$user['user_id']][$quizID] == 0) {
  2029. $finalScore += $grade = $arrGrade[$user['user_id']][$quizID];
  2030. }
  2031. $result[$i]['exer'.$j] = $grade;
  2032. $j++;
  2033. }
  2034. if ($finalScore > 20) {
  2035. $finalScore = 20;
  2036. }
  2037. $result[$i]['finalScore'] = number_format($finalScore, 2);
  2038. $i++;
  2039. }
  2040. break;
  2041. case 'get_extra_field_options':
  2042. $obj = new ExtraFieldOption($type);
  2043. $columns = ['display_text', 'option_value', 'option_order'];
  2044. $result = $obj->get_all([
  2045. 'where' => ['field_id = ? ' => $field_id],
  2046. 'order' => "$sidx $sord",
  2047. 'LIMIT' => "$start , $limit",
  2048. ]);
  2049. break;
  2050. case 'get_usergroups_teacher':
  2051. $columns = ['name', 'users', 'status', 'group_type', 'actions'];
  2052. $options = ['order' => "name $sord", 'LIMIT' => "$start , $limit"];
  2053. $options['course_id'] = $course_id;
  2054. switch ($type) {
  2055. case 'not_registered':
  2056. $options['where'] = [' (course_id IS NULL OR course_id != ?) ' => $course_id];
  2057. $result = $obj->getUserGroupNotInCourse($options, $groupFilter);
  2058. break;
  2059. case 'registered':
  2060. $options['where'] = [' usergroup.course_id = ? ' => $course_id];
  2061. $result = $obj->getUserGroupInCourse($options, $groupFilter);
  2062. break;
  2063. }
  2064. $new_result = [];
  2065. if (!empty($result)) {
  2066. $urlUserGroup = api_get_path(WEB_CODE_PATH).'admin/usergroup_users.php?'.api_get_cidreq();
  2067. foreach ($result as $group) {
  2068. $countUsers = count($obj->get_users_by_usergroup($group['id']));
  2069. $group['users'] = $countUsers;
  2070. if ($obj->allowTeachers()) {
  2071. $group['users'] = Display::url(
  2072. $countUsers,
  2073. $urlUserGroup.'&id='.$group['id']
  2074. );
  2075. }
  2076. if ($obj->usergroup_was_added_in_course($group['id'], $course_id)) {
  2077. $url = 'class.php?action=remove_class_from_course&id='.$group['id'].'&'.api_get_cidreq();
  2078. $icon = Display::return_icon('delete.png', get_lang('Remove'));
  2079. } else {
  2080. $url = 'class.php?action=add_class_to_course&id='.$group['id'].'&'.api_get_cidreq().'&type=not_registered';
  2081. $icon = Display::return_icon('add.png', get_lang('Add'));
  2082. }
  2083. switch ($group['group_type']) {
  2084. case 0:
  2085. $group['group_type'] = Display::label(get_lang('Class'), 'primary');
  2086. break;
  2087. case 1:
  2088. $group['group_type'] = Display::label(get_lang('Social'), 'success');
  2089. break;
  2090. }
  2091. $role = $obj->getUserRoleToString(api_get_user_id(), $group['id']);
  2092. $group['status'] = $role;
  2093. $group['actions'] = '';
  2094. if ($obj->allowTeachers()) {
  2095. $group['actions'] .= Display::url(
  2096. Display::return_icon('statistics.png'),
  2097. $urlUserGroup.'&id='.$group['id']
  2098. ).'&nbsp;';
  2099. }
  2100. $group['actions'] .= Display::url($icon, $url);
  2101. $new_result[] = $group;
  2102. }
  2103. $result = $new_result;
  2104. }
  2105. if (!in_array($sidx, $columns)) {
  2106. $sidx = 'name';
  2107. }
  2108. // Multidimensional sort
  2109. $result = msort($result, $sidx, $sord);
  2110. break;
  2111. default:
  2112. exit;
  2113. }
  2114. $allowed_actions = [
  2115. 'get_careers',
  2116. 'get_promotions',
  2117. 'get_mail_template',
  2118. 'get_usergroups',
  2119. 'get_usergroups_teacher',
  2120. 'get_gradebooks',
  2121. 'get_sessions',
  2122. 'get_session_access_overview',
  2123. 'get_sessions_tracking',
  2124. 'get_session_lp_progress',
  2125. 'get_survey_overview',
  2126. 'get_session_progress',
  2127. 'get_exercise_progress',
  2128. 'get_exercise_results',
  2129. 'get_exercise_results_report',
  2130. 'get_work_student_list_overview',
  2131. 'get_hotpotatoes_exercise_results',
  2132. 'get_work_teacher',
  2133. 'get_work_student',
  2134. 'get_work_user_list',
  2135. 'get_work_user_list_others',
  2136. 'get_work_user_list_all',
  2137. 'get_timelines',
  2138. 'get_grade_models',
  2139. 'get_event_email_template',
  2140. 'get_user_skill_ranking',
  2141. 'get_extra_fields',
  2142. 'get_extra_field_options',
  2143. //'get_course_exercise_medias',
  2144. 'get_user_course_report',
  2145. 'get_user_course_report_resumed',
  2146. 'get_exercise_grade',
  2147. 'get_group_reporting',
  2148. 'get_course_announcements',
  2149. 'get_programmed_announcements',
  2150. 'course_log_events',
  2151. 'get_learning_path_calendars',
  2152. 'get_usergroups_users',
  2153. 'get_calendar_users',
  2154. 'get_exercise_categories',
  2155. ];
  2156. // 5. Creating an obj to return a json
  2157. if (in_array($action, $allowed_actions)) {
  2158. $response = new stdClass();
  2159. $response->page = $page;
  2160. $response->total = $total_pages;
  2161. $response->records = $count;
  2162. if ($operation && $operation == 'excel') {
  2163. $j = 1;
  2164. $array = [];
  2165. if (empty($column_names)) {
  2166. $column_names = $columns;
  2167. }
  2168. // Headers
  2169. foreach ($column_names as $col) {
  2170. // Overwrite titles
  2171. if (isset($overwriteColumnHeaderExport[$col])) {
  2172. $col = $overwriteColumnHeaderExport[$col];
  2173. }
  2174. $array[0][] = $col;
  2175. }
  2176. foreach ($result as $row) {
  2177. foreach ($columns as $col) {
  2178. $array[$j][] = strip_tags($row[$col]);
  2179. }
  2180. $j++;
  2181. }
  2182. $fileName = !empty($action) ? $action : 'company_report';
  2183. if (!empty($exportFilename)) {
  2184. $fileName = $exportFilename;
  2185. }
  2186. switch ($exportFormat) {
  2187. case 'xls':
  2188. Export::arrayToXls($array, $fileName);
  2189. break;
  2190. case 'xls_html':
  2191. //TODO add date if exists
  2192. $browser = new Browser();
  2193. if ($browser->getPlatform() == Browser::PLATFORM_WINDOWS) {
  2194. Export::export_table_xls_html($array, $fileName, 'ISO-8859-15');
  2195. } else {
  2196. Export::export_table_xls_html($array, $fileName);
  2197. }
  2198. break;
  2199. case 'csv':
  2200. default:
  2201. Export::arrayToCsv($array, $fileName);
  2202. break;
  2203. }
  2204. exit;
  2205. }
  2206. $i = 0;
  2207. if (!empty($result)) {
  2208. foreach ($result as $row) {
  2209. // if results tab give not id, set id to $i otherwise id="null"
  2210. // for all <tr> of the jqgrid - ref #4235
  2211. if (!isset($row['id']) || isset($row['id']) && $row['id'] == '') {
  2212. $response->rows[$i]['id'] = $i;
  2213. } else {
  2214. $response->rows[$i]['id'] = $row['id'];
  2215. }
  2216. $array = [];
  2217. foreach ($columns as $col) {
  2218. if (in_array($col, ['correction', 'actions'])) {
  2219. $array[] = isset($row[$col]) ? $row[$col] : '';
  2220. } else {
  2221. $array[] = isset($row[$col]) ? Security::remove_XSS($row[$col]) : '';
  2222. }
  2223. }
  2224. $response->rows[$i]['cell'] = $array;
  2225. $i++;
  2226. }
  2227. }
  2228. header('Content-Type: application/json;charset=utf-8');
  2229. echo json_encode($response);
  2230. }
  2231. exit;