admin.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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[] = ["url" => "admin.php?exerciseId=".$objExercise->id, "name" => $objExercise->selectTitle(true)];
  256. } else {
  257. $interbreadcrumb[] = ["url" => "#", "name" => $objExercise->selectTitle(true)];
  258. }
  259. // shows a link to go back to the question pool
  260. if (!$exerciseId && $nameTools != get_lang('ExerciseManagement')) {
  261. $interbreadcrumb[] = array(
  262. "url" => api_get_path(WEB_CODE_PATH)."exercise/question_pool.php?fromExercise=$fromExercise&".api_get_cidreq(),
  263. "name" => get_lang('QuestionPool')
  264. );
  265. }
  266. // if the question is duplicated, disable the link of tool name
  267. if ($modifyIn == 'thisExercise') {
  268. if ($buttonBack) {
  269. $modifyIn = 'allExercises';
  270. } else {
  271. $noPHP_SELF = true;
  272. }
  273. }
  274. $htmlHeadXtra[] = '<script>
  275. function multiple_answer_true_false_onchange(variable) {
  276. var result = variable.checked;
  277. var id = variable.id;
  278. var weight_id = "weighting_" + id;
  279. var array_result=new Array();
  280. array_result[1]="1";
  281. array_result[0]= "-0.50";
  282. array_result[-1]= "0";
  283. if (result) {
  284. result = 1;
  285. } else {
  286. result = 0;
  287. }
  288. document.getElementById(weight_id).value = array_result[result];
  289. }
  290. </script>';
  291. $htmlHeadXtra[] = api_get_js('jqueryui-touch-punch/jquery.ui.touch-punch.min.js');
  292. $htmlHeadXtra[] = api_get_js('jquery.jsPlumb.all.js');
  293. $template = new Template();
  294. $templateName = $template->get_template('exercise/submit.js.tpl');
  295. $htmlHeadXtra[] = $template->fetch($templateName);
  296. $htmlHeadXtra[] = api_get_js('d3/jquery.xcolor.js');
  297. $htmlHeadXtra[] = '<link rel="stylesheet" href="'.api_get_path(WEB_LIBRARY_JS_PATH).'hotspot/css/hotspot.css">';
  298. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'hotspot/js/hotspot.js"></script>';
  299. if (isset($_GET['message'])) {
  300. if (in_array($_GET['message'], array('ExerciseStored', 'ItemUpdated', 'ItemAdded'))) {
  301. Display::addFlash(Display::return_message(get_lang($_GET['message']), 'confirmation'));
  302. }
  303. }
  304. Display::display_header($nameTools, 'Exercise');
  305. /*
  306. if ($objExercise->exercise_was_added_in_lp) {
  307. if ($objExercise->force_edit_exercise_in_lp == true) {
  308. Display::addFlash(Display::return_message(get_lang('ForceEditingExerciseInLPWarning'), 'warning'));
  309. } else {
  310. Display::addFlash(Display::return_message(get_lang('EditingExerciseCauseProblemsInLP'), 'warning'));
  311. }
  312. }*/
  313. // If we are in a test
  314. $inATest = isset($exerciseId) && $exerciseId > 0;
  315. if ($inATest) {
  316. echo '<div class="actions">';
  317. if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion']) || isset($_GET['myid']))
  318. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/admin.php?exerciseId='.$exerciseId.'&'.api_get_cidreq().'">'.
  319. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM).'</a>';
  320. if (!isset($_GET['hotspotadmin']) && !isset($_GET['newQuestion']) && !isset($_GET['myid']) && !isset($_GET['editQuestion'])) {
  321. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">'.
  322. Display::return_icon('back.png', get_lang('BackToExercisesList'), '', ICON_SIZE_MEDIUM).'</a>';
  323. }
  324. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&preview=1">'.
  325. Display::return_icon('preview_view.png', get_lang('Preview'), '', ICON_SIZE_MEDIUM).'</a>';
  326. echo Display::url(
  327. Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_MEDIUM),
  328. api_get_path(WEB_CODE_PATH).'exercise/exercise_report.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id
  329. );
  330. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.
  331. Display::return_icon('settings.png', get_lang('ModifyExercise'), '', ICON_SIZE_MEDIUM).'</a>';
  332. $maxScoreAllQuestions = 0;
  333. //$questionList = $objExercise->getQuestionList();
  334. $questionList = $objExercise->selectQuestionList(true, true);
  335. if (!empty($questionList)) {
  336. foreach ($questionList as $q) {
  337. $question = Question::read($q);
  338. if ($question) {
  339. $maxScoreAllQuestions += $question->selectWeighting();
  340. }
  341. }
  342. }
  343. echo '</div>';
  344. if ($objExercise->added_in_lp()) {
  345. echo Display::return_message(get_lang('AddedToLPCannotBeAccessed'), 'warning');
  346. }
  347. echo '<div class="alert alert-info">';
  348. echo sprintf(
  349. get_lang('XQuestionsWithTotalScoreY'),
  350. $objExercise->selectNbrQuestions(),
  351. $maxScoreAllQuestions
  352. );
  353. if ($objExercise->random > 0) {
  354. echo '<br />'.
  355. sprintf(get_lang('OnlyXQuestionsPickedRandomly'), $objExercise->random);
  356. }
  357. echo '</div>';
  358. } else if (isset($_GET['newQuestion'])) {
  359. // we are in create a new question from question pool not in a test
  360. echo '<div class="actions">';
  361. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/admin.php?'.api_get_cidreq().'">'.
  362. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM).'</a>';
  363. echo '</div>';
  364. } else {
  365. // If we are in question_pool but not in an test, go back to question create in pool
  366. echo '<div class="actions">';
  367. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/question_pool.php?'.api_get_cidreq().'">'.
  368. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM).
  369. '</a>';
  370. echo '</div>';
  371. }
  372. if ($newQuestion || $editQuestion) {
  373. // Question management
  374. $type = isset($_REQUEST['answerType']) ? Security::remove_XSS($_REQUEST['answerType']) : null;
  375. echo '<input type="hidden" name="Type" value="'.$type.'" />';
  376. if ($newQuestion === 'yes') {
  377. $objExercise->edit_exercise_in_lp = true;
  378. require 'question_admin.inc.php';
  379. }
  380. if ($editQuestion) {
  381. // Question preview if teacher clicked the "switch to student"
  382. if ($studentViewActive && $is_allowedToEdit) {
  383. echo '<div class="main-question">';
  384. echo Display::div($objQuestion->selectTitle(), array('class' => 'question_title'));
  385. ExerciseLib::showQuestion(
  386. $editQuestion,
  387. false,
  388. null,
  389. null,
  390. false,
  391. true,
  392. false,
  393. true,
  394. $objExercise->feedback_type,
  395. true
  396. );
  397. echo '</div>';
  398. } else {
  399. require 'question_admin.inc.php';
  400. }
  401. }
  402. }
  403. if (isset($_GET['hotspotadmin'])) {
  404. if (!is_object($objQuestion)) {
  405. $objQuestion = Question :: read($_GET['hotspotadmin']);
  406. }
  407. if (!$objQuestion) {
  408. api_not_allowed();
  409. }
  410. require 'hotspot_admin.inc.php';
  411. }
  412. if (!$newQuestion && !$modifyQuestion && !$editQuestion && !isset($_GET['hotspotadmin'])) {
  413. // question list management
  414. require 'question_list_admin.inc.php';
  415. }
  416. // if we are in question authoring, display warning to user is feedback not shown at the end of the test -ref #6619
  417. // this test to display only message in the question authoring page and not in the question list page too
  418. // if (is_object($objQuestion) && $objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_EXAM && ($newQuestion || $modifyQuestion || $editQuestion)) {
  419. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_EXAM) {
  420. echo Display::return_message(get_lang('TestFeedbackNotShown'), 'normal');
  421. }
  422. Session::write('objExercise', $objExercise);
  423. Session::write('objQuestion', $objQuestion);
  424. Session::write('objAnswer', $objAnswer);
  425. Display::display_footer();