admin.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Exercise administration
  6. * This script allows to manage (create, modify) an exercise and its questions.
  7. *
  8. * Following scripts are includes for a best code understanding :
  9. *
  10. * - exercise.class.php : for the creation of an Exercise object
  11. * - question.class.php : for the creation of a Question object
  12. * - answer.class.php : for the creation of an Answer object
  13. * - exercise.lib.php : functions used in the exercise tool
  14. * - exercise_admin.inc.php : management of the exercise
  15. * - question_admin.inc.php : management of a question (statement & answers)
  16. * - statement_admin.inc.php : management of a statement
  17. * - question_list_admin.inc.php : management of the question list
  18. *
  19. * Main variables used in this script :
  20. *
  21. * - $is_allowedToEdit : set to 1 if the user is allowed to manage the exercise
  22. * - $objExercise : exercise object
  23. * - $objQuestion : question object
  24. * - $objAnswer : answer object
  25. * - $exerciseId : the exercise ID
  26. * - $picturePath : the path of question pictures
  27. * - $newQuestion : ask to create a new question
  28. * - $modifyQuestion : ID of the question to modify
  29. * - $editQuestion : ID of the question to edit
  30. * - $submitQuestion : ask to save question modifications
  31. * - $cancelQuestion : ask to cancel question modifications
  32. * - $deleteQuestion : ID of the question to delete
  33. * - $moveUp : ID of the question to move up
  34. * - $moveDown : ID of the question to move down
  35. * - $modifyExercise : ID of the exercise to modify
  36. * - $submitExercise : ask to save exercise modifications
  37. * - $cancelExercise : ask to cancel exercise modifications
  38. * - $modifyAnswers : ID of the question which we want to modify answers for
  39. * - $cancelAnswers : ask to cancel answer modifications
  40. * - $buttonBack : ask to go back to the previous page in answers of type "Fill in blanks"
  41. *
  42. * @package chamilo.exercise
  43. *
  44. * @author Olivier Brouckaert
  45. * Modified by Hubert Borderiou 21-10-2011 Question by category
  46. */
  47. require_once __DIR__.'/../inc/global.inc.php';
  48. $current_course_tool = TOOL_QUIZ;
  49. $this_section = SECTION_COURSES;
  50. // Access control
  51. api_protect_course_script(true);
  52. $is_allowedToEdit = api_is_allowed_to_edit(null, true, false, false);
  53. $sessionId = api_get_session_id();
  54. $studentViewActive = api_is_student_view_active();
  55. $showPagination = api_get_configuration_value('show_question_pagination');
  56. if (!$is_allowedToEdit) {
  57. api_not_allowed(true);
  58. }
  59. $exerciseId = isset($_GET['exerciseId']) ? (int) $_GET['exerciseId'] : 0;
  60. /* stripslashes POST data */
  61. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  62. foreach ($_POST as $key => $val) {
  63. if (is_string($val)) {
  64. $_POST[$key] = stripslashes($val);
  65. } elseif (is_array($val)) {
  66. foreach ($val as $key2 => $val2) {
  67. $_POST[$key][$key2] = stripslashes($val2);
  68. }
  69. }
  70. $GLOBALS[$key] = $_POST[$key];
  71. }
  72. }
  73. $newQuestion = isset($_GET['newQuestion']) ? $_GET['newQuestion'] : 0;
  74. $modifyAnswers = isset($_GET['modifyAnswers']) ? $_GET['modifyAnswers'] : 0;
  75. $editQuestion = isset($_GET['editQuestion']) ? $_GET['editQuestion'] : 0;
  76. $page = isset($_GET['page']) && !empty($_GET['page']) ? (int) $_GET['page'] : 1;
  77. $modifyQuestion = isset($_GET['modifyQuestion']) ? $_GET['modifyQuestion'] : 0;
  78. $deleteQuestion = isset($_GET['deleteQuestion']) ? $_GET['deleteQuestion'] : 0;
  79. $clone_question = isset($_REQUEST['clone_question']) ? $_REQUEST['clone_question'] : 0;
  80. if (empty($questionId)) {
  81. $questionId = Session::read('questionId');
  82. }
  83. if (empty($modifyExercise)) {
  84. $modifyExercise = isset($_GET['modifyExercise']) ? $_GET['modifyExercise'] : null;
  85. }
  86. $fromExercise = isset($fromExercise) ? $fromExercise : null;
  87. $cancelExercise = isset($cancelExercise) ? $cancelExercise : null;
  88. $cancelAnswers = isset($cancelAnswers) ? $cancelAnswers : null;
  89. $modifyIn = isset($modifyIn) ? $modifyIn : null;
  90. $cancelQuestion = isset($cancelQuestion) ? $cancelQuestion : null;
  91. /* Cleaning all incomplete attempts of the admin/teacher to avoid weird problems
  92. when changing the exercise settings, number of questions, etc */
  93. Event::delete_all_incomplete_attempts(
  94. api_get_user_id(),
  95. $exerciseId,
  96. api_get_course_int_id(),
  97. api_get_session_id()
  98. );
  99. // get from session
  100. $objExercise = Session::read('objExercise');
  101. $objQuestion = Session::read('objQuestion');
  102. if (isset($_REQUEST['convertAnswer'])) {
  103. $objQuestion = $objQuestion->swapSimpleAnswerTypes();
  104. Session::write('objQuestion', $objQuestion);
  105. }
  106. $objAnswer = Session::read('objAnswer');
  107. $_course = api_get_course_info();
  108. // document path
  109. $documentPath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  110. // picture path
  111. $picturePath = $documentPath.'/images';
  112. // audio path
  113. $audioPath = $documentPath.'/audio';
  114. // tables used in the exercise tool
  115. if (!empty($_GET['action']) && $_GET['action'] == 'exportqti2' && !empty($_GET['questionId'])) {
  116. require_once 'export/qti2/qti2_export.php';
  117. $export = export_question_qti($_GET['questionId'], true);
  118. $qid = (int) $_GET['questionId'];
  119. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  120. $temp_dir_short = uniqid();
  121. $temp_zip_dir = $archive_path."/".$temp_dir_short;
  122. if (!is_dir($temp_zip_dir)) {
  123. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  124. }
  125. $temp_zip_file = $temp_zip_dir."/".api_get_unique_id().".zip";
  126. $temp_xml_file = $temp_zip_dir."/qti2export_".$qid.'.xml';
  127. file_put_contents($temp_xml_file, $export);
  128. $zip_folder = new PclZip($temp_zip_file);
  129. $zip_folder->add($temp_xml_file, PCLZIP_OPT_REMOVE_ALL_PATH);
  130. $name = 'qti2_export_'.$qid.'.zip';
  131. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  132. unlink($temp_zip_file);
  133. unlink($temp_xml_file);
  134. rmdir($temp_zip_dir);
  135. exit; //otherwise following clicks may become buggy
  136. }
  137. // Exercise object creation.
  138. if (!is_object($objExercise)) {
  139. // construction of the Exercise object
  140. $objExercise = new Exercise();
  141. // creation of a new exercise if wrong or not specified exercise ID
  142. if ($exerciseId) {
  143. $parseQuestionList = $showPagination > 0 ? false : true;
  144. if ($editQuestion) {
  145. $parseQuestionList = false;
  146. $showPagination = true;
  147. }
  148. $objExercise->read($exerciseId, $parseQuestionList);
  149. }
  150. // saves the object into the session
  151. Session::write('objExercise', $objExercise);
  152. }
  153. // Exercise can be edited in their course.
  154. if ($objExercise->sessionId != $sessionId) {
  155. api_not_allowed(true);
  156. }
  157. // doesn't select the exercise ID if we come from the question pool
  158. if (!$fromExercise) {
  159. // gets the right exercise ID, and if 0 creates a new exercise
  160. if (!$exerciseId = $objExercise->selectId()) {
  161. $modifyExercise = 'yes';
  162. }
  163. }
  164. $nbrQuestions = $objExercise->getQuestionCount();
  165. // Question object creation.
  166. if ($editQuestion || $newQuestion || $modifyQuestion || $modifyAnswers) {
  167. if ($editQuestion || $newQuestion) {
  168. // reads question data
  169. if ($editQuestion) {
  170. // question not found
  171. if (!$objQuestion = Question::read($editQuestion)) {
  172. api_not_allowed(true);
  173. }
  174. // saves the object into the session
  175. Session::write('objQuestion', $objQuestion);
  176. }
  177. }
  178. // checks if the object exists
  179. if (is_object($objQuestion)) {
  180. // gets the question ID
  181. $questionId = $objQuestion->selectId();
  182. }
  183. }
  184. // if cancelling an exercise
  185. if ($cancelExercise) {
  186. // existing exercise
  187. if ($exerciseId) {
  188. unset($modifyExercise);
  189. } else {
  190. // new exercise
  191. // goes back to the exercise list
  192. header('Location: '.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq());
  193. exit();
  194. }
  195. }
  196. // if cancelling question creation/modification
  197. if ($cancelQuestion) {
  198. // if we are creating a new question from the question pool
  199. if (!$exerciseId && !$questionId) {
  200. // goes back to the question pool
  201. header('Location: question_pool.php?'.api_get_cidreq());
  202. exit();
  203. } else {
  204. // goes back to the question viewing
  205. $editQuestion = $modifyQuestion;
  206. unset($newQuestion, $modifyQuestion);
  207. }
  208. }
  209. if (!empty($clone_question) && !empty($objExercise->id)) {
  210. $old_question_obj = Question::read($clone_question);
  211. $old_question_obj->question = $old_question_obj->question.' - '.get_lang('Copy');
  212. $new_id = $old_question_obj->duplicate(api_get_course_info());
  213. $new_question_obj = Question::read($new_id);
  214. $new_question_obj->addToList($exerciseId);
  215. // Save category to the destination course
  216. if (!empty($old_question_obj->category)) {
  217. $new_question_obj->saveCategory($old_question_obj->category, api_get_course_int_id());
  218. }
  219. // This should be moved to the duplicate function
  220. $new_answer_obj = new Answer($clone_question);
  221. $new_answer_obj->read();
  222. $new_answer_obj->duplicate($new_question_obj);
  223. // Reloading tne $objExercise obj
  224. $objExercise->read($objExercise->id, false);
  225. Display::addFlash(Display::return_message(get_lang('ItemCopied')));
  226. header('Location: admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&page='.$page);
  227. exit;
  228. }
  229. // if cancelling answer creation/modification
  230. if ($cancelAnswers) {
  231. // goes back to the question viewing
  232. $editQuestion = $modifyAnswers;
  233. unset($modifyAnswers);
  234. }
  235. $nameTools = '';
  236. // modifies the query string that is used in the link of tool name
  237. if ($editQuestion || $modifyQuestion || $newQuestion || $modifyAnswers) {
  238. $nameTools = get_lang('QuestionManagement');
  239. }
  240. if (api_is_in_gradebook()) {
  241. $interbreadcrumb[] = [
  242. 'url' => Category::getUrl(),
  243. 'name' => get_lang('ToolGradebook'),
  244. ];
  245. }
  246. $interbreadcrumb[] = ['url' => 'exercise.php?'.api_get_cidreq(), 'name' => get_lang('Exercises')];
  247. if (isset($_GET['newQuestion']) || isset($_GET['editQuestion'])) {
  248. $interbreadcrumb[] = [
  249. 'url' => 'admin.php?exerciseId='.$objExercise->id.'&'.api_get_cidreq(),
  250. 'name' => $objExercise->selectTitle(true),
  251. ];
  252. } else {
  253. $interbreadcrumb[] = [
  254. 'url' => '#',
  255. 'name' => $objExercise->selectTitle(true),
  256. ];
  257. }
  258. // shows a link to go back to the question pool
  259. if (!$exerciseId && $nameTools != get_lang('ExerciseManagement')) {
  260. $interbreadcrumb[] = [
  261. 'url' => api_get_path(WEB_CODE_PATH)."exercise/question_pool.php?fromExercise=$fromExercise&".api_get_cidreq(),
  262. 'name' => get_lang('QuestionPool'),
  263. ];
  264. }
  265. // if the question is duplicated, disable the link of tool name
  266. if ($modifyIn === 'thisExercise') {
  267. if ($buttonBack) {
  268. $modifyIn = 'allExercises';
  269. }
  270. }
  271. $htmlHeadXtra[] = api_get_js('jqueryui-touch-punch/jquery.ui.touch-punch.min.js');
  272. $htmlHeadXtra[] = api_get_js('jquery.jsPlumb.all.js');
  273. $template = new Template();
  274. $templateName = $template->get_template('exercise/submit.js.tpl');
  275. $htmlHeadXtra[] = $template->fetch($templateName);
  276. $htmlHeadXtra[] = api_get_js('d3/jquery.xcolor.js');
  277. $htmlHeadXtra[] = '<link rel="stylesheet" href="'.api_get_path(WEB_LIBRARY_JS_PATH).'hotspot/css/hotspot.css">';
  278. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'hotspot/js/hotspot.js"></script>';
  279. if (isset($_GET['message'])) {
  280. if (in_array($_GET['message'], ['ExerciseStored', 'ItemUpdated', 'ItemAdded'])) {
  281. Display::addFlash(Display::return_message(get_lang($_GET['message']), 'confirmation'));
  282. }
  283. }
  284. Display::display_header($nameTools, 'Exercise');
  285. // If we are in a test
  286. $inATest = isset($exerciseId) && $exerciseId > 0;
  287. if ($inATest) {
  288. echo '<div class="actions">';
  289. if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion'])) {
  290. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/admin.php?exerciseId='.$exerciseId.'&'.api_get_cidreq().'">'.
  291. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM).'</a>';
  292. }
  293. if (!isset($_GET['hotspotadmin']) && !isset($_GET['newQuestion']) && !isset($_GET['editQuestion'])) {
  294. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">'.
  295. Display::return_icon('back.png', get_lang('BackToExercisesList'), '', ICON_SIZE_MEDIUM).'</a>';
  296. }
  297. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&preview=1">'.
  298. Display::return_icon('preview_view.png', get_lang('Preview'), '', ICON_SIZE_MEDIUM).'</a>';
  299. echo Display::url(
  300. Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_MEDIUM),
  301. api_get_path(WEB_CODE_PATH).'exercise/exercise_report.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id
  302. );
  303. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.
  304. Display::return_icon('settings.png', get_lang('ModifyExercise'), '', ICON_SIZE_MEDIUM).'</a>';
  305. $maxScoreAllQuestions = 0;
  306. if ($showPagination === false) {
  307. $questionList = $objExercise->selectQuestionList(true, true);
  308. if (!empty($questionList)) {
  309. foreach ($questionList as $questionItemId) {
  310. $question = Question::read($questionItemId);
  311. if ($question) {
  312. $maxScoreAllQuestions += $question->selectWeighting();
  313. }
  314. }
  315. }
  316. }
  317. echo '</div>';
  318. if ($objExercise->added_in_lp()) {
  319. echo Display::return_message(get_lang('AddedToLPCannotBeAccessed'), 'warning');
  320. }
  321. if ($editQuestion && $objQuestion->existsInAnotherExercise()) {
  322. echo Display::return_message(
  323. Display::returnFontAwesomeIcon('exclamation-triangle"')
  324. .get_lang('ThisQuestionExistsInAnotherExercisesWarning'),
  325. 'warning',
  326. false
  327. );
  328. }
  329. $alert = '';
  330. if ($showPagination === false) {
  331. $alert .= sprintf(
  332. get_lang('XQuestionsWithTotalScoreY'),
  333. $nbrQuestions,
  334. $maxScoreAllQuestions
  335. );
  336. }
  337. if ($objExercise->random > 0) {
  338. $alert .= '<br />'.sprintf(get_lang('OnlyXQuestionsPickedRandomly'), $objExercise->random);
  339. }
  340. echo Display::return_message($alert, 'normal', false);
  341. } elseif (isset($_GET['newQuestion'])) {
  342. // we are in create a new question from question pool not in a test
  343. echo '<div class="actions">';
  344. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/admin.php?'.api_get_cidreq().'">'.
  345. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM).'</a>';
  346. echo '</div>';
  347. } else {
  348. // If we are in question_pool but not in an test, go back to question create in pool
  349. echo '<div class="actions">';
  350. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/question_pool.php?'.api_get_cidreq().'">'.
  351. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM).
  352. '</a>';
  353. echo '</div>';
  354. }
  355. if ($newQuestion || $editQuestion) {
  356. // Question management
  357. $type = isset($_REQUEST['answerType']) ? Security::remove_XSS($_REQUEST['answerType']) : null;
  358. echo '<input type="hidden" name="Type" value="'.$type.'" />';
  359. if ($newQuestion === 'yes') {
  360. $objExercise->edit_exercise_in_lp = true;
  361. require 'question_admin.inc.php';
  362. }
  363. if ($editQuestion) {
  364. // Question preview if teacher clicked the "switch to student"
  365. if ($studentViewActive && $is_allowedToEdit) {
  366. echo '<div class="main-question">';
  367. echo Display::div($objQuestion->selectTitle(), ['class' => 'question_title']);
  368. ExerciseLib::showQuestion(
  369. $objExercise,
  370. $editQuestion,
  371. false,
  372. null,
  373. null,
  374. false,
  375. true,
  376. false,
  377. true,
  378. true
  379. );
  380. echo '</div>';
  381. } else {
  382. require 'question_admin.inc.php';
  383. }
  384. }
  385. }
  386. if (isset($_GET['hotspotadmin'])) {
  387. if (!is_object($objQuestion)) {
  388. $objQuestion = Question::read($_GET['hotspotadmin']);
  389. }
  390. if (!$objQuestion) {
  391. api_not_allowed();
  392. }
  393. require 'hotspot_admin.inc.php';
  394. }
  395. if (!$newQuestion && !$modifyQuestion && !$editQuestion && !isset($_GET['hotspotadmin'])) {
  396. // question list management
  397. require 'question_list_admin.inc.php';
  398. }
  399. // if we are in question authoring, display warning to user is feedback not shown at the end of the test -ref #6619
  400. // this test to display only message in the question authoring page and not in the question list page too
  401. if ($objExercise->getFeedbackType() == EXERCISE_FEEDBACK_TYPE_EXAM) {
  402. echo Display::return_message(get_lang('TestFeedbackNotShown'), 'normal');
  403. }
  404. Session::write('objExercise', $objExercise);
  405. Session::write('objQuestion', $objQuestion);
  406. Session::write('objAnswer', $objAnswer);
  407. Display::display_footer();