exercise.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Exercise list: This script shows the list of exercises for administrators and students.
  6. *
  7. * @package chamilo.exercise
  8. *
  9. * @author Olivier Brouckaert, original author
  10. * @author Denes Nagy, HotPotatoes integration
  11. * @author Wolfgang Schneider, code/html cleanup
  12. * @author Julio Montoya <gugli100@gmail.com>, lots of cleanup + several improvements
  13. * Modified by hubert.borderiou (question category)
  14. */
  15. require_once __DIR__.'/../inc/global.inc.php';
  16. $current_course_tool = TOOL_QUIZ;
  17. // Setting the tabs
  18. $this_section = SECTION_COURSES;
  19. $htmlHeadXtra[] = api_get_asset('qtip2/jquery.qtip.min.js');
  20. $htmlHeadXtra[] = api_get_css_asset('qtip2/jquery.qtip.min.css');
  21. api_protect_course_script(true);
  22. $limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access');
  23. $check = Security::get_existing_token('get');
  24. $currentUrl = api_get_self().'?'.api_get_cidreq();
  25. require_once 'hotpotatoes.lib.php';
  26. /* Constants and variables */
  27. $is_allowedToEdit = api_is_allowed_to_edit(null, true);
  28. $is_tutor = api_is_allowed_to_edit(true);
  29. $is_tutor_course = api_is_course_tutor();
  30. $courseInfo = api_get_course_info();
  31. $courseId = $courseInfo['real_id'];
  32. $userInfo = api_get_user_info();
  33. $userId = $userInfo['id'];
  34. $sessionId = api_get_session_id();
  35. $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
  36. $userId,
  37. $courseInfo
  38. );
  39. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  40. $TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  41. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  42. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  43. $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  44. // document path
  45. $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document';
  46. // picture path
  47. $picturePath = $documentPath.'/images';
  48. // audio path
  49. $audioPath = $documentPath.'/audio';
  50. // hot potatoes
  51. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  52. $exercisePath = api_get_self();
  53. $exfile = explode('/', $exercisePath);
  54. $exfile = strtolower($exfile[count($exfile) - 1]);
  55. $exercisePath = substr($exercisePath, 0, strpos($exercisePath, $exfile));
  56. $exercisePath = $exercisePath.'exercise.php';
  57. // Clear the exercise session
  58. Session::erase('objExercise');
  59. Session::erase('objQuestion');
  60. Session::erase('objAnswer');
  61. Session::erase('questionList');
  62. Session::erase('exerciseResult');
  63. Session::erase('firstTime');
  64. Session::erase('calculatedAnswerId');
  65. Session::erase('duration_time_previous');
  66. Session::erase('duration_time');
  67. //General POST/GET/SESSION/COOKIES parameters recovery
  68. $origin = api_get_origin();
  69. $choice = isset($_REQUEST['choice']) ? Security::remove_XSS($_REQUEST['choice']) : null;
  70. $hpchoice = isset($_REQUEST['hpchoice']) ? Security::remove_XSS($_REQUEST['hpchoice']) : null;
  71. $exerciseId = isset($_REQUEST['exerciseId']) ? (int) $_REQUEST['exerciseId'] : null;
  72. $file = isset($_REQUEST['file']) ? Database::escape_string($_REQUEST['file']) : null;
  73. $learnpath_id = isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : null;
  74. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? (int) $_REQUEST['learnpath_item_id'] : null;
  75. $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : null;
  76. $categoryId = isset($_REQUEST['category_id']) ? (int) $_REQUEST['category_id'] : 0;
  77. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  78. $keyword = isset($_REQUEST['keyword']) ? Security::remove_XSS($_REQUEST['keyword']) : '';
  79. if ($page < 0) {
  80. $page = 1;
  81. }
  82. if (api_is_in_gradebook()) {
  83. $interbreadcrumb[] = [
  84. 'url' => Category::getUrl(),
  85. 'name' => get_lang('ToolGradebook'),
  86. ];
  87. }
  88. $nameTools = get_lang('Exercises');
  89. // Simple actions
  90. if ($is_allowedToEdit) {
  91. switch ($action) {
  92. case 'clean_all_test':
  93. if ($check) {
  94. if ($limitTeacherAccess && !api_is_platform_admin()) {
  95. api_not_allowed(true);
  96. }
  97. // list of exercises in a course/session
  98. // we got variable $courseId $courseInfo session api_get_session_id()
  99. $exerciseList = ExerciseLib::get_all_exercises_for_course_id(
  100. $courseInfo,
  101. $sessionId,
  102. $courseId,
  103. false
  104. );
  105. $quantity_results_deleted = 0;
  106. foreach ($exerciseList as $exeItem) {
  107. // delete result for test, if not in a gradebook
  108. $exercise_action_locked = api_resource_is_locked_by_gradebook($exeItem['id'], LINK_EXERCISE);
  109. if ($exercise_action_locked == false) {
  110. $objExerciseTmp = new Exercise();
  111. if ($objExerciseTmp->read($exeItem['id'])) {
  112. $quantity_results_deleted += $objExerciseTmp->cleanResults(true);
  113. }
  114. }
  115. }
  116. Display::addFlash(Display::return_message(
  117. sprintf(
  118. get_lang('XResultsCleaned'),
  119. $quantity_results_deleted
  120. ),
  121. 'confirm'
  122. ));
  123. header('Location: '.$currentUrl);
  124. exit;
  125. }
  126. break;
  127. case 'exportqti2':
  128. if ($limitTeacherAccess && !api_is_platform_admin()) {
  129. api_not_allowed(true);
  130. }
  131. require_once api_get_path(SYS_CODE_PATH).'exercise/export/qti2/qti2_export.php';
  132. $export = export_exercise_to_qti($exerciseId, true);
  133. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  134. $temp_dir_short = api_get_unique_id();
  135. $temp_zip_dir = $archive_path.$temp_dir_short;
  136. if (!is_dir($temp_zip_dir)) {
  137. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  138. }
  139. $temp_zip_file = $temp_zip_dir.'/'.api_get_unique_id().'.zip';
  140. $temp_xml_file = $temp_zip_dir.'/qti2export_'.$exerciseId.'.xml';
  141. file_put_contents($temp_xml_file, $export);
  142. $xmlReader = new XMLReader();
  143. $xmlReader->open($temp_xml_file);
  144. $xmlReader->setParserProperty(XMLReader::VALIDATE, true);
  145. $isValid = $xmlReader->isValid();
  146. if ($isValid) {
  147. $zip_folder = new PclZip($temp_zip_file);
  148. $zip_folder->add($temp_xml_file, PCLZIP_OPT_REMOVE_ALL_PATH);
  149. $name = 'qti2_export_'.$exerciseId.'.zip';
  150. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  151. unlink($temp_zip_file);
  152. unlink($temp_xml_file);
  153. rmdir($temp_zip_dir);
  154. exit; // otherwise following clicks may become buggy
  155. } else {
  156. Display::addFlash(Display::return_message(get_lang('ErrorWritingXMLFile'), 'error'));
  157. header('Location: '.$currentUrl);
  158. exit;
  159. }
  160. break;
  161. case 'up_category':
  162. case 'down_category':
  163. $categoryIdFromGet = isset($_REQUEST['category_id_edit']) ? $_REQUEST['category_id_edit'] : 0;
  164. $em = Database::getManager();
  165. $repo = $em->getRepository('ChamiloCourseBundle:CExerciseCategory');
  166. $category = $repo->find($categoryIdFromGet);
  167. $currentPosition = $category->getPosition();
  168. if ($action === 'up_category') {
  169. $currentPosition--;
  170. } else {
  171. $currentPosition++;
  172. }
  173. $category->setPosition($currentPosition);
  174. $em->persist($category);
  175. $em->flush();
  176. Display::addFlash(Display::return_message(get_lang('Updated')));
  177. header('Location: '.$currentUrl);
  178. exit;
  179. break;
  180. }
  181. }
  182. // Mass actions
  183. if (!empty($action) && $is_allowedToEdit) {
  184. $exerciseListToEdit = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
  185. if (!empty($exerciseListToEdit)) {
  186. foreach ($exerciseListToEdit as $exerciseIdToEdit) {
  187. $objExerciseTmp = new Exercise();
  188. $result = $objExerciseTmp->read($exerciseIdToEdit);
  189. if (empty($result)) {
  190. continue;
  191. }
  192. switch ($action) {
  193. case 'delete':
  194. $objExerciseTmp->delete();
  195. break;
  196. case 'visible':
  197. if ($limitTeacherAccess && !api_is_platform_admin()) {
  198. // Teacher change exercise
  199. break;
  200. }
  201. // enables an exercise
  202. if (empty($sessionId)) {
  203. $objExerciseTmp->enable();
  204. $objExerciseTmp->save();
  205. } else {
  206. if (!empty($objExerciseTmp->sessionId)) {
  207. $objExerciseTmp->enable();
  208. $objExerciseTmp->save();
  209. }
  210. }
  211. api_item_property_update(
  212. $courseInfo,
  213. TOOL_QUIZ,
  214. $objExerciseTmp->id,
  215. 'visible',
  216. $userId
  217. );
  218. break;
  219. case 'invisible':
  220. if ($limitTeacherAccess && !api_is_platform_admin()) {
  221. // Teacher change exercise
  222. break;
  223. }
  224. // enables an exercise
  225. if (empty($sessionId)) {
  226. $objExerciseTmp->disable();
  227. $objExerciseTmp->save();
  228. } else {
  229. if (!empty($objExerciseTmp->sessionId)) {
  230. $objExerciseTmp->disable();
  231. $objExerciseTmp->save();
  232. }
  233. }
  234. api_item_property_update(
  235. $courseInfo,
  236. TOOL_QUIZ,
  237. $objExerciseTmp->id,
  238. 'visible',
  239. $userId
  240. );
  241. break;
  242. }
  243. }
  244. Display::addFlash(Display::return_message(get_lang('Updated')));
  245. header('Location: '.$currentUrl);
  246. exit;
  247. }
  248. }
  249. Event::event_access_tool(TOOL_QUIZ);
  250. $logInfo = [
  251. 'tool' => TOOL_QUIZ,
  252. 'tool_id' => (int) $exerciseId,
  253. 'tool_id_detail' => 0,
  254. 'action' => isset($_REQUEST['learnpath_id']) ? 'learnpath_id' : '',
  255. 'action_details' => isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : '',
  256. ];
  257. Event::registerLog($logInfo);
  258. HotPotGCt($documentPath, 1, $userId);
  259. // Only for administrator
  260. if ($is_allowedToEdit) {
  261. if (!empty($choice)) {
  262. // single exercise choice
  263. // construction of Exercise
  264. $objExerciseTmp = new Exercise();
  265. $exercise_action_locked = api_resource_is_locked_by_gradebook(
  266. $exerciseId,
  267. LINK_EXERCISE
  268. );
  269. if ($objExerciseTmp->read($exerciseId)) {
  270. if ($check) {
  271. switch ($choice) {
  272. case 'enable_launch':
  273. $objExerciseTmp->cleanCourseLaunchSettings();
  274. $objExerciseTmp->enableAutoLaunch();
  275. Display::addFlash(Display::return_message(get_lang('Updated')));
  276. break;
  277. case 'disable_launch':
  278. $objExerciseTmp->cleanCourseLaunchSettings();
  279. break;
  280. case 'delete':
  281. // deletes an exercise
  282. $result = $objExerciseTmp->delete();
  283. if ($result) {
  284. Display::addFlash(Display::return_message(get_lang('ExerciseDeleted'), 'confirmation'));
  285. }
  286. break;
  287. case 'enable':
  288. if ($limitTeacherAccess && !api_is_platform_admin()) {
  289. // Teacher change exercise
  290. break;
  291. }
  292. // Enables an exercise
  293. if (empty($sessionId)) {
  294. $objExerciseTmp->enable();
  295. $objExerciseTmp->save();
  296. } else {
  297. if (!empty($objExerciseTmp->sessionId)) {
  298. $objExerciseTmp->enable();
  299. $objExerciseTmp->save();
  300. }
  301. }
  302. api_item_property_update(
  303. $courseInfo,
  304. TOOL_QUIZ,
  305. $objExerciseTmp->id,
  306. 'visible',
  307. $userId
  308. );
  309. Display::addFlash(Display::return_message(get_lang('VisibilityChanged'), 'confirmation'));
  310. break;
  311. case 'disable':
  312. if ($limitTeacherAccess && !api_is_platform_admin()) {
  313. // Teacher change exercise
  314. break;
  315. }
  316. // disables an exercise
  317. if (empty($sessionId)) {
  318. $objExerciseTmp->disable();
  319. $objExerciseTmp->save();
  320. } else {
  321. // Only change active if it belongs to a session
  322. if (!empty($objExerciseTmp->sessionId)) {
  323. $objExerciseTmp->disable();
  324. $objExerciseTmp->save();
  325. }
  326. }
  327. api_item_property_update(
  328. $courseInfo,
  329. TOOL_QUIZ,
  330. $objExerciseTmp->id,
  331. 'invisible',
  332. $userId
  333. );
  334. Display::addFlash(Display::return_message(get_lang('VisibilityChanged'), 'confirmation'));
  335. break;
  336. case 'disable_results':
  337. //disable the results for the learners
  338. $objExerciseTmp->disable_results();
  339. $objExerciseTmp->save();
  340. Display::addFlash(Display::return_message(get_lang('ResultsDisabled'), 'confirmation'));
  341. break;
  342. case 'enable_results':
  343. //disable the results for the learners
  344. $objExerciseTmp->enable_results();
  345. $objExerciseTmp->save();
  346. Display::addFlash(Display::return_message(get_lang('ResultsEnabled'), 'confirmation'));
  347. break;
  348. case 'clean_results':
  349. if ($limitTeacherAccess && !api_is_platform_admin()) {
  350. // Teacher change exercise
  351. break;
  352. }
  353. // Clean student results
  354. if ($exercise_action_locked == false) {
  355. $quantity_results_deleted = $objExerciseTmp->cleanResults(true);
  356. $title = $objExerciseTmp->selectTitle();
  357. Display::addFlash(
  358. Display::return_message(
  359. $title.': '.sprintf(
  360. get_lang('XResultsCleaned'),
  361. $quantity_results_deleted
  362. ),
  363. 'confirmation'
  364. )
  365. );
  366. }
  367. break;
  368. case 'copy_exercise': //copy an exercise
  369. api_set_more_memory_and_time_limits();
  370. $objExerciseTmp->copyExercise();
  371. Display::addFlash(Display::return_message(
  372. get_lang('ExerciseCopied'),
  373. 'confirmation'
  374. ));
  375. break;
  376. }
  377. header('Location: '.$currentUrl);
  378. exit;
  379. }
  380. }
  381. // destruction of Exercise
  382. unset($objExerciseTmp);
  383. Security::clear_token();
  384. }
  385. if (!empty($hpchoice)) {
  386. switch ($hpchoice) {
  387. case 'delete':
  388. if ($limitTeacherAccess && !api_is_platform_admin()) {
  389. // Teacher change exercise
  390. break;
  391. }
  392. // deletes an exercise
  393. $imgparams = [];
  394. $imgcount = 0;
  395. GetImgParams($file, $documentPath, $imgparams, $imgcount);
  396. $fld = GetFolderName($file);
  397. for ($i = 0; $i < $imgcount; $i++) {
  398. my_delete($documentPath.$uploadPath."/".$fld."/".$imgparams[$i]);
  399. DocumentManager::updateDbInfo("delete", $uploadPath."/".$fld."/".$imgparams[$i]);
  400. }
  401. if (!is_dir($documentPath.$uploadPath."/".$fld."/")) {
  402. my_delete($documentPath.$file);
  403. DocumentManager::updateDbInfo("delete", $file);
  404. } else {
  405. if (my_delete($documentPath.$file)) {
  406. DocumentManager::updateDbInfo("delete", $file);
  407. }
  408. }
  409. /* hotpotatoes folder may contains several tests so
  410. don't delete folder if not empty :
  411. http://support.chamilo.org/issues/2165
  412. */
  413. if (!(strstr($uploadPath, DIR_HOTPOTATOES) &&
  414. !folder_is_empty($documentPath.$uploadPath."/".$fld."/"))
  415. ) {
  416. my_delete($documentPath.$uploadPath."/".$fld."/");
  417. }
  418. break;
  419. case 'enable': // enables an exercise
  420. if ($limitTeacherAccess && !api_is_platform_admin()) {
  421. // Teacher change exercise
  422. break;
  423. }
  424. $newVisibilityStatus = '1'; //"visible"
  425. $query = "SELECT id FROM $TBL_DOCUMENT
  426. WHERE c_id = $courseId AND path='".Database::escape_string($file)."'";
  427. $res = Database::query($query);
  428. $row = Database :: fetch_array($res, 'ASSOC');
  429. api_item_property_update(
  430. $courseInfo,
  431. TOOL_DOCUMENT,
  432. $row['id'],
  433. 'visible',
  434. $userId
  435. );
  436. Display::addFlash(Display::return_message(get_lang('Updated')));
  437. break;
  438. case 'disable': // disables an exercise
  439. if ($limitTeacherAccess && !api_is_platform_admin()) {
  440. // Teacher change exercise
  441. break;
  442. }
  443. $newVisibilityStatus = '0'; //"invisible"
  444. $query = "SELECT id FROM $TBL_DOCUMENT
  445. WHERE c_id = $courseId AND path='".Database::escape_string($file)."'";
  446. $res = Database::query($query);
  447. $row = Database :: fetch_array($res, 'ASSOC');
  448. api_item_property_update(
  449. $courseInfo,
  450. TOOL_DOCUMENT,
  451. $row['id'],
  452. 'invisible',
  453. $userId
  454. );
  455. break;
  456. default:
  457. break;
  458. }
  459. header('Location: '.$currentUrl);
  460. exit;
  461. }
  462. }
  463. if ($origin !== 'learnpath') {
  464. //so we are not in learnpath tool
  465. Display::display_header($nameTools, get_lang('Exercise'));
  466. if (isset($_GET['message']) && in_array($_GET['message'], ['ExerciseEdited'])) {
  467. echo Display::return_message(get_lang('ExerciseEdited'), 'confirmation');
  468. }
  469. } else {
  470. Display::display_reduced_header();
  471. }
  472. Display::display_introduction_section(TOOL_QUIZ);
  473. // Selects $limit exercises at the same time
  474. // maximum number of exercises on a same page
  475. $limit = 50;
  476. // Display the next and previous link if needed
  477. $from = $page * $limit;
  478. HotPotGCt($documentPath, 1, $userId);
  479. $token = Security::get_token();
  480. if ($is_allowedToEdit && $origin !== 'learnpath') {
  481. $actionsLeft = '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise_admin.php?'.api_get_cidreq().'">'.
  482. Display::return_icon('new_exercice.png', get_lang('NewEx'), '', ICON_SIZE_MEDIUM).'</a>';
  483. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/question_create.php?'.api_get_cidreq().'">'.
  484. Display::return_icon('new_question.png', get_lang('AddQ'), '', ICON_SIZE_MEDIUM).'</a>';
  485. if (api_get_configuration_value('allow_exercise_categories')) {
  486. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/category.php?'.api_get_cidreq().'">';
  487. $actionsLeft .= Display::return_icon('folder.png', get_lang('Category'), '', ICON_SIZE_MEDIUM);
  488. $actionsLeft .= '</a>';
  489. }
  490. // Question category
  491. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/tests_category.php?'.api_get_cidreq().'">';
  492. $actionsLeft .= Display::return_icon('green_open.png', get_lang('QuestionCategory'), '', ICON_SIZE_MEDIUM);
  493. $actionsLeft .= '</a>';
  494. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/question_pool.php?'.api_get_cidreq().'">';
  495. $actionsLeft .= Display::return_icon('database.png', get_lang('QuestionPool'), '', ICON_SIZE_MEDIUM);
  496. $actionsLeft .= '</a>';
  497. //echo Display::url(Display::return_icon('looknfeel.png', get_lang('Media')), 'media.php?' . api_get_cidreq());
  498. // end question category
  499. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/hotpotatoes.php?'.api_get_cidreq().'">'.
  500. Display::return_icon('import_hotpotatoes.png', get_lang('ImportHotPotatoesQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  501. // link to import qti2 ...
  502. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/qti2.php?'.api_get_cidreq().'">'.
  503. Display::return_icon('import_qti2.png', get_lang('ImportQtiQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  504. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/aiken.php?'.api_get_cidreq().'">'.
  505. Display::return_icon('import_aiken.png', get_lang('ImportAikenQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  506. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/upload_exercise.php?'.api_get_cidreq().'">'.
  507. Display::return_icon('import_excel.png', get_lang('ImportExcelQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  508. $cleanAll = Display::url(
  509. Display::return_icon(
  510. 'clean_all.png',
  511. get_lang('CleanAllStudentsResultsForAllTests'),
  512. '',
  513. ICON_SIZE_MEDIUM
  514. ),
  515. '#',
  516. [
  517. 'data-item-question' => addslashes(get_lang('AreYouSureToEmptyAllTestResults')),
  518. 'data-href' => api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'&action=clean_all_test&sec_token='.$token,
  519. 'data-toggle' => 'modal',
  520. 'data-target' => '#confirm-delete',
  521. ]
  522. );
  523. if ($limitTeacherAccess) {
  524. if (api_is_platform_admin()) {
  525. $actionsLeft .= $cleanAll;
  526. }
  527. } else {
  528. $actionsLeft .= $cleanAll;
  529. }
  530. // Create a search-box
  531. $form = new FormValidator('search_simple', 'get', $currentUrl, null, null, FormValidator::LAYOUT_INLINE);
  532. $form->addCourseHiddenParams();
  533. if (api_get_configuration_value('allow_exercise_categories')) {
  534. $manager = new ExerciseCategoryManager();
  535. $options = $manager->getCategoriesForSelect(api_get_course_int_id());
  536. if (!empty($options)) {
  537. $form->addSelect(
  538. 'category_id',
  539. get_lang('Category'),
  540. $options,
  541. ['placeholder' => get_lang('SelectAnOption'), 'disable_js' => true]
  542. );
  543. }
  544. }
  545. $form->addText(
  546. 'keyword',
  547. get_lang('Search'),
  548. false,
  549. [
  550. 'aria-label' => get_lang('Search'),
  551. ]
  552. );
  553. $form->addButtonSearch(get_lang('Search'));
  554. $actionsRight = $form->returnForm();
  555. }
  556. if ($is_allowedToEdit) {
  557. echo Display::toolbarAction(
  558. 'toolbarUser',
  559. [$actionsLeft, '', $actionsRight],
  560. [6, 1, 5]
  561. );
  562. }
  563. if (api_get_configuration_value('allow_exercise_categories') === false) {
  564. echo Exercise::exerciseGrid(0, $page, $from, $limit, $keyword);
  565. } else {
  566. if (empty($categoryId)) {
  567. echo Display::page_subheader(get_lang('NoCategory'));
  568. echo Exercise::exerciseGrid(0, $page, $from, $limit, $keyword);
  569. $counter = 0;
  570. $manager = new ExerciseCategoryManager();
  571. $categories = $manager->getCategories($courseId);
  572. $modifyUrl = api_get_self().'?'.api_get_cidreq();
  573. $total = count($categories);
  574. $upIcon = Display::return_icon('up.png', get_lang('MoveUp'));
  575. $downIcon = Display::return_icon('down.png', get_lang('MoveDown'));
  576. /** @var \Chamilo\CourseBundle\Entity\CExerciseCategory $category */
  577. foreach ($categories as $category) {
  578. $categoryIdItem = $category->getId();
  579. $up = '';
  580. $down = '';
  581. if ($is_allowedToEdit) {
  582. $up = Display::url($upIcon, $modifyUrl.'&action=up_category&category_id_edit='.$categoryIdItem);
  583. if ($counter === 0) {
  584. $up = Display::url(Display::return_icon('up_na.png'), '#');
  585. }
  586. $down = Display::url($downIcon, $modifyUrl.'&action=down_category&category_id_edit='.$categoryIdItem);
  587. $counter++;
  588. if ($total === $counter) {
  589. $down = Display::url(Display::return_icon('down_na.png'), '#');
  590. }
  591. }
  592. echo Display::page_subheader($category->getName().$up.$down);
  593. echo Exercise::exerciseGrid($category->getId(), $page, $from, $limit, $keyword);
  594. }
  595. } else {
  596. $manager = new ExerciseCategoryManager();
  597. $category = $manager->get($categoryId);
  598. echo Display::page_subheader($category['name']);
  599. echo Exercise::exerciseGrid($category['id'], $page, $from, $limit, $keyword);
  600. }
  601. }
  602. if ($origin !== 'learnpath') {
  603. // We are not in learnpath tool
  604. Display::display_footer();
  605. }