exercise.php 26 KB

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