admin.php 16 KB

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