admin.php 17 KB

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