exercise.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. 'tool_id_detail' => 0,
  242. 'action' => isset($_REQUEST['learnpath_id']) ? 'learnpath_id' : '',
  243. 'action_details' => isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : '',
  244. ];
  245. Event::registerLog($logInfo);
  246. HotPotGCt($documentPath, 1, $userId);
  247. // Only for administrator
  248. if ($is_allowedToEdit) {
  249. if (!empty($choice)) {
  250. // single exercise choice
  251. // construction of Exercise
  252. $objExerciseTmp = new Exercise();
  253. $exercise_action_locked = api_resource_is_locked_by_gradebook(
  254. $exerciseId,
  255. LINK_EXERCISE
  256. );
  257. if ($objExerciseTmp->read($exerciseId)) {
  258. if ($check) {
  259. switch ($choice) {
  260. case 'enable_launch':
  261. $objExerciseTmp->cleanCourseLaunchSettings();
  262. $objExerciseTmp->enableAutoLaunch();
  263. Display::addFlash(Display::return_message(get_lang('Updated')));
  264. break;
  265. case 'disable_launch':
  266. $objExerciseTmp->cleanCourseLaunchSettings();
  267. break;
  268. case 'delete':
  269. // deletes an exercise
  270. $result = $objExerciseTmp->delete();
  271. if ($result) {
  272. Display::addFlash(Display::return_message(get_lang('ExerciseDeleted'), 'confirmation'));
  273. }
  274. break;
  275. case 'enable':
  276. if ($limitTeacherAccess && !api_is_platform_admin()) {
  277. // Teacher change exercise
  278. break;
  279. }
  280. // Enables an exercise
  281. if (empty($sessionId)) {
  282. $objExerciseTmp->enable();
  283. $objExerciseTmp->save();
  284. } else {
  285. if (!empty($objExerciseTmp->sessionId)) {
  286. $objExerciseTmp->enable();
  287. $objExerciseTmp->save();
  288. }
  289. }
  290. api_item_property_update(
  291. $courseInfo,
  292. TOOL_QUIZ,
  293. $objExerciseTmp->id,
  294. 'visible',
  295. $userId
  296. );
  297. Display::addFlash(Display::return_message(get_lang('VisibilityChanged'), 'confirmation'));
  298. break;
  299. case 'disable':
  300. if ($limitTeacherAccess && !api_is_platform_admin()) {
  301. // Teacher change exercise
  302. break;
  303. }
  304. // disables an exercise
  305. if (empty($sessionId)) {
  306. $objExerciseTmp->disable();
  307. $objExerciseTmp->save();
  308. } else {
  309. // Only change active if it belongs to a session
  310. if (!empty($objExerciseTmp->sessionId)) {
  311. $objExerciseTmp->disable();
  312. $objExerciseTmp->save();
  313. }
  314. }
  315. api_item_property_update(
  316. $courseInfo,
  317. TOOL_QUIZ,
  318. $objExerciseTmp->id,
  319. 'invisible',
  320. $userId
  321. );
  322. Display::addFlash(Display::return_message(get_lang('VisibilityChanged'), 'confirmation'));
  323. break;
  324. case 'disable_results':
  325. //disable the results for the learners
  326. $objExerciseTmp->disable_results();
  327. $objExerciseTmp->save();
  328. Display::addFlash(Display::return_message(get_lang('ResultsDisabled'), 'confirmation'));
  329. break;
  330. case 'enable_results':
  331. //disable the results for the learners
  332. $objExerciseTmp->enable_results();
  333. $objExerciseTmp->save();
  334. Display::addFlash(Display::return_message(get_lang('ResultsEnabled'), 'confirmation'));
  335. break;
  336. case 'clean_results':
  337. if ($limitTeacherAccess && !api_is_platform_admin()) {
  338. // Teacher change exercise
  339. break;
  340. }
  341. // Clean student results
  342. if ($exercise_action_locked == false) {
  343. $quantity_results_deleted = $objExerciseTmp->cleanResults(true);
  344. $title = $objExerciseTmp->selectTitle();
  345. Display::addFlash(
  346. Display::return_message(
  347. $title.': '.sprintf(
  348. get_lang('XResultsCleaned'),
  349. $quantity_results_deleted
  350. ),
  351. 'confirmation'
  352. )
  353. );
  354. }
  355. break;
  356. case 'copy_exercise': //copy an exercise
  357. api_set_more_memory_and_time_limits();
  358. $objExerciseTmp->copyExercise();
  359. Display::addFlash(Display::return_message(
  360. get_lang('ExerciseCopied'),
  361. 'confirmation'
  362. ));
  363. break;
  364. }
  365. header('Location: '.$currentUrl);
  366. exit;
  367. }
  368. }
  369. // destruction of Exercise
  370. unset($objExerciseTmp);
  371. Security::clear_token();
  372. }
  373. if (!empty($hpchoice)) {
  374. switch ($hpchoice) {
  375. case 'delete':
  376. if ($limitTeacherAccess && !api_is_platform_admin()) {
  377. // Teacher change exercise
  378. break;
  379. }
  380. // deletes an exercise
  381. $imgparams = [];
  382. $imgcount = 0;
  383. GetImgParams($file, $documentPath, $imgparams, $imgcount);
  384. $fld = GetFolderName($file);
  385. for ($i = 0; $i < $imgcount; $i++) {
  386. my_delete($documentPath.$uploadPath."/".$fld."/".$imgparams[$i]);
  387. DocumentManager::updateDbInfo("delete", $uploadPath."/".$fld."/".$imgparams[$i]);
  388. }
  389. if (!is_dir($documentPath.$uploadPath."/".$fld."/")) {
  390. my_delete($documentPath.$file);
  391. DocumentManager::updateDbInfo("delete", $file);
  392. } else {
  393. if (my_delete($documentPath.$file)) {
  394. DocumentManager::updateDbInfo("delete", $file);
  395. }
  396. }
  397. /* hotpotatoes folder may contains several tests so
  398. don't delete folder if not empty :
  399. http://support.chamilo.org/issues/2165
  400. */
  401. if (!(strstr($uploadPath, DIR_HOTPOTATOES) &&
  402. !folder_is_empty($documentPath.$uploadPath."/".$fld."/"))
  403. ) {
  404. my_delete($documentPath.$uploadPath."/".$fld."/");
  405. }
  406. break;
  407. case 'enable': // enables an exercise
  408. if ($limitTeacherAccess && !api_is_platform_admin()) {
  409. // Teacher change exercise
  410. break;
  411. }
  412. $newVisibilityStatus = '1'; //"visible"
  413. $query = "SELECT id FROM $TBL_DOCUMENT
  414. WHERE c_id = $courseId AND path='".Database::escape_string($file)."'";
  415. $res = Database::query($query);
  416. $row = Database :: fetch_array($res, 'ASSOC');
  417. api_item_property_update(
  418. $courseInfo,
  419. TOOL_DOCUMENT,
  420. $row['id'],
  421. 'visible',
  422. $userId
  423. );
  424. Display::addFlash(Display::return_message(get_lang('Updated')));
  425. break;
  426. case 'disable': // disables an exercise
  427. if ($limitTeacherAccess && !api_is_platform_admin()) {
  428. // Teacher change exercise
  429. break;
  430. }
  431. $newVisibilityStatus = '0'; //"invisible"
  432. $query = "SELECT id FROM $TBL_DOCUMENT
  433. WHERE c_id = $courseId AND path='".Database::escape_string($file)."'";
  434. $res = Database::query($query);
  435. $row = Database :: fetch_array($res, 'ASSOC');
  436. api_item_property_update(
  437. $courseInfo,
  438. TOOL_DOCUMENT,
  439. $row['id'],
  440. 'invisible',
  441. $userId
  442. );
  443. break;
  444. default:
  445. break;
  446. }
  447. header('Location: '.$currentUrl);
  448. exit;
  449. }
  450. }
  451. if ($origin !== 'learnpath') {
  452. //so we are not in learnpath tool
  453. Display::display_header($nameTools, get_lang('Exercise'));
  454. if (isset($_GET['message']) && in_array($_GET['message'], ['ExerciseEdited'])) {
  455. echo Display::return_message(get_lang('ExerciseEdited'), 'confirmation');
  456. }
  457. } else {
  458. Display::display_reduced_header();
  459. }
  460. Display::display_introduction_section(TOOL_QUIZ);
  461. HotPotGCt($documentPath, 1, $userId);
  462. $token = Security::get_token();
  463. if ($is_allowedToEdit && $origin !== 'learnpath') {
  464. $actionsLeft = '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise_admin.php?'.api_get_cidreq().'">'.
  465. Display::return_icon('new_exercice.png', get_lang('NewEx'), '', ICON_SIZE_MEDIUM).'</a>';
  466. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/question_create.php?'.api_get_cidreq().'">'.
  467. Display::return_icon('new_question.png', get_lang('AddQ'), '', ICON_SIZE_MEDIUM).'</a>';
  468. if (api_get_configuration_value('allow_exercise_categories')) {
  469. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/category.php?'.api_get_cidreq().'">';
  470. $actionsLeft .= Display::return_icon('folder.png', get_lang('Category'), '', ICON_SIZE_MEDIUM);
  471. $actionsLeft .= '</a>';
  472. }
  473. // Question category
  474. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/tests_category.php?'.api_get_cidreq().'">';
  475. $actionsLeft .= Display::return_icon('green_open.png', get_lang('QuestionCategory'), '', ICON_SIZE_MEDIUM);
  476. $actionsLeft .= '</a>';
  477. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/question_pool.php?'.api_get_cidreq().'">';
  478. $actionsLeft .= Display::return_icon('database.png', get_lang('QuestionPool'), '', ICON_SIZE_MEDIUM);
  479. $actionsLeft .= '</a>';
  480. //echo Display::url(Display::return_icon('looknfeel.png', get_lang('Media')), 'media.php?' . api_get_cidreq());
  481. // end question category
  482. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/hotpotatoes.php?'.api_get_cidreq().'">'.
  483. Display::return_icon('import_hotpotatoes.png', get_lang('ImportHotPotatoesQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  484. // link to import qti2 ...
  485. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/qti2.php?'.api_get_cidreq().'">'.
  486. Display::return_icon('import_qti2.png', get_lang('ImportQtiQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  487. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/aiken.php?'.api_get_cidreq().'">'.
  488. Display::return_icon('import_aiken.png', get_lang('ImportAikenQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  489. $actionsLeft .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/upload_exercise.php?'.api_get_cidreq().'">'.
  490. Display::return_icon('import_excel.png', get_lang('ImportExcelQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  491. $cleanAll = Display::url(
  492. Display::return_icon(
  493. 'clean_all.png',
  494. get_lang('CleanAllStudentsResultsForAllTests'),
  495. '',
  496. ICON_SIZE_MEDIUM
  497. ),
  498. '#',
  499. [
  500. 'data-item-question' => addslashes(get_lang('AreYouSureToEmptyAllTestResults')),
  501. 'data-href' => api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'&action=clean_all_test&sec_token='.$token,
  502. 'data-toggle' => 'modal',
  503. 'data-target' => '#confirm-delete',
  504. ]
  505. );
  506. if ($limitTeacherAccess) {
  507. if (api_is_platform_admin()) {
  508. $actionsLeft .= $cleanAll;
  509. }
  510. } else {
  511. $actionsLeft .= $cleanAll;
  512. }
  513. // Create a search-box
  514. $form = new FormValidator('search_simple', 'get', $currentUrl, null, null, FormValidator::LAYOUT_INLINE);
  515. $form->addCourseHiddenParams();
  516. if (api_get_configuration_value('allow_exercise_categories')) {
  517. $manager = new ExerciseCategoryManager();
  518. $options = $manager->getCategoriesForSelect(api_get_course_int_id());
  519. if (!empty($options)) {
  520. $form->addSelect(
  521. 'category_id',
  522. get_lang('Category'),
  523. $options,
  524. ['placeholder' => get_lang('SelectAnOption'), 'disable_js' => true]
  525. );
  526. }
  527. }
  528. $form->addText(
  529. 'keyword',
  530. get_lang('Search'),
  531. false,
  532. [
  533. 'aria-label' => get_lang('Search'),
  534. ]
  535. );
  536. $form->addButtonSearch(get_lang('Search'));
  537. $actionsRight = $form->returnForm();
  538. }
  539. if ($is_allowedToEdit) {
  540. echo Display::toolbarAction(
  541. 'toolbarUser',
  542. [$actionsLeft, '', $actionsRight],
  543. [6, 1, 5]
  544. );
  545. }
  546. if (api_get_configuration_value('allow_exercise_categories') === false) {
  547. echo Exercise::exerciseGrid(0, $keyword);
  548. } else {
  549. if (empty($categoryId)) {
  550. echo Display::page_subheader(get_lang('NoCategory'));
  551. echo Exercise::exerciseGrid(0, $keyword);
  552. $counter = 0;
  553. $manager = new ExerciseCategoryManager();
  554. $categories = $manager->getCategories($courseId);
  555. $modifyUrl = api_get_self().'?'.api_get_cidreq();
  556. $total = count($categories);
  557. $upIcon = Display::return_icon('up.png', get_lang('MoveUp'));
  558. $downIcon = Display::return_icon('down.png', get_lang('MoveDown'));
  559. /** @var \Chamilo\CourseBundle\Entity\CExerciseCategory $category */
  560. foreach ($categories as $category) {
  561. $categoryIdItem = $category->getId();
  562. $up = '';
  563. $down = '';
  564. if ($is_allowedToEdit) {
  565. $up = Display::url($upIcon, $modifyUrl.'&action=up_category&category_id_edit='.$categoryIdItem);
  566. if ($counter === 0) {
  567. $up = Display::url(Display::return_icon('up_na.png'), '#');
  568. }
  569. $down = Display::url($downIcon, $modifyUrl.'&action=down_category&category_id_edit='.$categoryIdItem);
  570. $counter++;
  571. if ($total === $counter) {
  572. $down = Display::url(Display::return_icon('down_na.png'), '#');
  573. }
  574. }
  575. echo Display::page_subheader($category->getName().$up.$down);
  576. echo Exercise::exerciseGrid($category->getId(), $keyword);
  577. }
  578. } else {
  579. $manager = new ExerciseCategoryManager();
  580. $category = $manager->get($categoryId);
  581. echo Display::page_subheader($category['name']);
  582. echo Exercise::exerciseGrid($category['id'], $keyword);
  583. }
  584. }
  585. if ($origin !== 'learnpath') {
  586. // We are not in learnpath tool
  587. Display::display_footer();
  588. }