admin.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exercise administration
  5. * This script allows to manage (create, modify) an exercise and its questions
  6. *
  7. * Following scripts are includes for a best code understanding :
  8. *
  9. * - exercise.class.php : for the creation of an Exercise object
  10. * - question.class.php : for the creation of a Question object
  11. * - answer.class.php : for the creation of an Answer object
  12. * - exercise.lib.php : functions used in the exercise tool
  13. * - exercise_admin.inc.php : management of the exercise
  14. * - question_admin.inc.php : management of a question (statement & answers)
  15. * - statement_admin.inc.php : management of a statement
  16. * - question_list_admin.inc.php : management of the question list
  17. *
  18. * Main variables used in this script :
  19. *
  20. * - $is_allowedToEdit : set to 1 if the user is allowed to manage the exercise
  21. * - $objExercise : exercise object
  22. * - $objQuestion : question object
  23. * - $objAnswer : answer object
  24. * - $aType : array with answer types
  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. use ChamiloSession as Session;
  47. require_once '../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);
  53. $sessionId = api_get_session_id();
  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. if (empty($newQuestion)) {
  74. $newQuestion = isset($_GET['newQuestion']) ? $_GET['newQuestion'] : 0;
  75. }
  76. if (empty($modifyAnswers)) {
  77. $modifyAnswers = isset($_GET['modifyAnswers']) ? $_GET['modifyAnswers'] : 0;
  78. }
  79. if (empty($editQuestion)) {
  80. $editQuestion = isset($_GET['editQuestion']) ? $_GET['editQuestion'] : 0;
  81. }
  82. if (empty($modifyQuestion)) {
  83. $modifyQuestion = isset($_GET['modifyQuestion']) ? $_GET['modifyQuestion'] : 0;
  84. }
  85. if (empty($deleteQuestion)) {
  86. $deleteQuestion = isset($_GET['deleteQuestion']) ? $_GET['deleteQuestion'] : 0;
  87. }
  88. $clone_question = isset($_REQUEST['clone_question']) ? $_REQUEST['clone_question'] : 0;
  89. if (empty($questionId)) {
  90. $questionId = isset($_SESSION['questionId']) ? $_SESSION['questionId'] : 0;
  91. }
  92. if (empty($modifyExercise)) {
  93. $modifyExercise = isset($_GET['modifyExercise']) ? $_GET['modifyExercise'] : null;
  94. }
  95. $fromExercise = isset($fromExercise) ? $fromExercise : null;
  96. $cancelExercise = isset($cancelExercise) ? $cancelExercise : null;
  97. $cancelAnswers = isset($cancelAnswers) ? $cancelAnswers : null;
  98. $modifyIn = isset($modifyIn) ? $modifyIn : null;
  99. $cancelQuestion = isset($cancelQuestion) ? $cancelQuestion : null;
  100. /* Cleaning all incomplete attempts of the admin/teacher to avoid weird problems
  101. when changing the exercise settings, number of questions, etc */
  102. Event::delete_all_incomplete_attempts(
  103. api_get_user_id(),
  104. $exerciseId,
  105. api_get_course_int_id(),
  106. api_get_session_id()
  107. );
  108. // get from session
  109. $objExercise = isset($_SESSION['objExercise']) ? $_SESSION['objExercise'] : null;
  110. $objQuestion = isset($_SESSION['objQuestion']) ? $_SESSION['objQuestion'] : null;
  111. if (isset($_REQUEST['convertAnswer'])) {
  112. $objQuestion = $objQuestion->swapSimpleAnswerTypes();
  113. $_SESSION['objQuestion'] = $objQuestion;
  114. }
  115. $objAnswer = isset($_SESSION['objAnswer']) ? $_SESSION['objAnswer'] : null;
  116. // document path
  117. $documentPath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  118. // picture path
  119. $picturePath = $documentPath.'/images';
  120. // audio path
  121. $audioPath = $documentPath.'/audio';
  122. // the 5 types of answers
  123. $aType = array(
  124. get_lang('UniqueSelect'),
  125. get_lang('MultipleSelect'),
  126. get_lang('FillBlanks'),
  127. get_lang('Matching'),
  128. get_lang('FreeAnswer')
  129. );
  130. // tables used in the exercise tool
  131. if (!empty($_GET['action']) && $_GET['action'] == 'exportqti2' && !empty($_GET['questionId'])) {
  132. require_once 'export/qti2/qti2_export.php';
  133. $export = export_question_qti($_GET['questionId'], true);
  134. $qid = (int)$_GET['questionId'];
  135. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  136. $temp_dir_short = uniqid();
  137. $temp_zip_dir = $archive_path."/".$temp_dir_short;
  138. if (!is_dir($temp_zip_dir)) {
  139. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  140. }
  141. $temp_zip_file = $temp_zip_dir."/".api_get_unique_id().".zip";
  142. $temp_xml_file = $temp_zip_dir."/qti2export_".$qid.'.xml';
  143. file_put_contents($temp_xml_file, $export);
  144. $zip_folder = new PclZip($temp_zip_file);
  145. $zip_folder->add($temp_xml_file, PCLZIP_OPT_REMOVE_ALL_PATH);
  146. $name = 'qti2_export_'.$qid.'.zip';
  147. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  148. unlink($temp_zip_file);
  149. unlink($temp_xml_file);
  150. rmdir($temp_zip_dir);
  151. //DocumentManager::string_send_for_download($export,true,'qti2export_q'.$_GET['questionId'].'.xml');
  152. exit; //otherwise following clicks may become buggy
  153. }
  154. // Exercise object creation.
  155. if (!is_object($objExercise)) {
  156. // construction of the Exercise object
  157. $objExercise = new Exercise();
  158. // creation of a new exercise if wrong or not specified exercise ID
  159. if ($exerciseId) {
  160. $objExercise->read($exerciseId);
  161. }
  162. // saves the object into the session
  163. Session::write('objExercise', $objExercise);
  164. }
  165. // Exercise can be edited in their course.
  166. if ($objExercise->sessionId != $sessionId) {
  167. api_not_allowed(true);
  168. /*header('Location: '.api_get_path(WEB_CODE_PATH).'exercice/exercise.php?'.api_get_cidreq());
  169. exit;*/
  170. }
  171. // doesn't select the exercise ID if we come from the question pool
  172. if (!$fromExercise) {
  173. // gets the right exercise ID, and if 0 creates a new exercise
  174. if (!$exerciseId = $objExercise->selectId()) {
  175. $modifyExercise='yes';
  176. }
  177. }
  178. $nbrQuestions = $objExercise->selectNbrQuestions();
  179. // Question object creation.
  180. if ($editQuestion || $newQuestion || $modifyQuestion || $modifyAnswers) {
  181. if ($editQuestion || $newQuestion) {
  182. // reads question data
  183. if ($editQuestion) {
  184. // question not found
  185. if (!$objQuestion = Question::read($editQuestion)) {
  186. api_not_allowed();
  187. }
  188. // saves the object into the session
  189. Session::write('objQuestion', $objQuestion);
  190. }
  191. }
  192. // checks if the object exists
  193. if (is_object($objQuestion)) {
  194. // gets the question ID
  195. $questionId = $objQuestion->selectId();
  196. }
  197. }
  198. // if cancelling an exercise
  199. if ($cancelExercise) {
  200. // existing exercise
  201. if ($exerciseId) {
  202. unset($modifyExercise);
  203. } else {
  204. // new exercise
  205. // goes back to the exercise list
  206. header('Location: '.api_get_path(WEB_CODE_PATH).'exercice/exercise.php?'.api_get_cidreq());
  207. exit();
  208. }
  209. }
  210. // if cancelling question creation/modification
  211. if ($cancelQuestion) {
  212. // if we are creating a new question from the question pool
  213. if (!$exerciseId && !$questionId) {
  214. // goes back to the question pool
  215. header('Location: question_pool.php');
  216. exit();
  217. } else {
  218. // goes back to the question viewing
  219. $editQuestion=$modifyQuestion;
  220. unset($newQuestion,$modifyQuestion);
  221. }
  222. }
  223. if (!empty($clone_question) && !empty($objExercise->id)) {
  224. $old_question_obj = Question::read($clone_question);
  225. $old_question_obj->question = $old_question_obj->question.' - '.get_lang('Copy');
  226. $new_id = $old_question_obj->duplicate();
  227. $new_question_obj = Question::read($new_id);
  228. $new_question_obj->addToList($exerciseId);
  229. // This should be moved to the duplicate function
  230. $new_answer_obj = new Answer($clone_question);
  231. $new_answer_obj->read();
  232. $new_answer_obj->duplicate($new_question_obj);
  233. //Reloading tne $objExercise obj
  234. $objExercise->read($objExercise->id);
  235. header('Location: admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id);
  236. exit;
  237. }
  238. // if cancelling answer creation/modification
  239. if ($cancelAnswers) {
  240. // goes back to the question viewing
  241. $editQuestion=$modifyAnswers;
  242. unset($modifyAnswers);
  243. }
  244. $nameTools = null;
  245. // modifies the query string that is used in the link of tool name
  246. if ($editQuestion || $modifyQuestion || $newQuestion || $modifyAnswers) {
  247. $nameTools = get_lang('QuestionManagement');
  248. }
  249. if (isset($_SESSION['gradebook'])){
  250. $gradebook= $_SESSION['gradebook'];
  251. }
  252. if (!empty($gradebook) && $gradebook=='view') {
  253. $interbreadcrumb[]= array(
  254. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  255. 'name' => get_lang('ToolGradebook')
  256. );
  257. }
  258. $interbreadcrumb[] = array("url" => "exercise.php","name" => get_lang('Exercises'));
  259. if (isset($_GET['newQuestion']) || isset($_GET['editQuestion']) ) {
  260. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=".$objExercise->id, "name" => $objExercise->name);
  261. } else {
  262. $interbreadcrumb[] = array("url" => "#", "name" => $objExercise->name);
  263. }
  264. // shows a link to go back to the question pool
  265. if (!$exerciseId && $nameTools != get_lang('ExerciseManagement')){
  266. $interbreadcrumb[]=array(
  267. "url" => api_get_path(WEB_CODE_PATH)."exercice/question_pool.php?fromExercise=$fromExercise&".api_get_cidreq(),
  268. "name" => get_lang('QuestionPool')
  269. );
  270. }
  271. // if the question is duplicated, disable the link of tool name
  272. if ($modifyIn == 'thisExercise') {
  273. if($buttonBack) {
  274. $modifyIn='allExercises';
  275. } else {
  276. $noPHP_SELF=true;
  277. }
  278. }
  279. $htmlHeadXtra[] = '<script>
  280. function multiple_answer_true_false_onchange(variable) {
  281. var result = variable.checked;
  282. var id = variable.id;
  283. var weight_id = "weighting_" + id;
  284. var array_result=new Array();
  285. array_result[1]="1";
  286. array_result[0]= "-0.50";
  287. array_result[-1]= "0";
  288. if (result) {
  289. result = 1;
  290. } else {
  291. result = 0;
  292. }
  293. document.getElementById(weight_id).value = array_result[result];
  294. }
  295. </script>';
  296. $htmlHeadXtra[] = api_get_js('jqueryui-touch-punch/jquery.ui.touch-punch.min.js');
  297. $htmlHeadXtra[] = api_get_js('jquery.jsPlumb.all.js');
  298. $template = new Template();
  299. $htmlHeadXtra[] = $template->fetch('default/exercise/submit.js.tpl');
  300. $htmlHeadXtra[] = api_get_js('d3/jquery.xcolor.js');
  301. $htmlHeadXtra[] = '<link rel="stylesheet" href="' . api_get_path(WEB_LIBRARY_JS_PATH) . 'hotspot/css/hotspot.css">';
  302. $htmlHeadXtra[] = '<script src="' . api_get_path(WEB_LIBRARY_JS_PATH) . 'hotspot/js/hotspot.js"></script>';
  303. Display::display_header($nameTools,'Exercise');
  304. /*
  305. if ($objExercise->exercise_was_added_in_lp) {
  306. if ($objExercise->force_edit_exercise_in_lp == true) {
  307. Display::display_warning_message(get_lang('ForceEditingExerciseInLPWarning'));
  308. } else {
  309. Display::display_warning_message(get_lang('EditingExerciseCauseProblemsInLP'));
  310. }
  311. }*/
  312. // If we are in a test
  313. $inATest = isset($exerciseId) && $exerciseId > 0;
  314. if ($inATest) {
  315. echo '<div class="actions">';
  316. if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion']) || isset($_GET['myid']))
  317. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercice/admin.php?exerciseId='.$exerciseId.'&'.api_get_cidreq().'">'.
  318. Display::return_icon('back.png', get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).'</a>';
  319. if (!isset($_GET['hotspotadmin']) && !isset($_GET['newQuestion']) && !isset($_GET['myid']) && !isset($_GET['editQuestion'])) {
  320. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercice/exercise.php?'.api_get_cidreq().'">'.
  321. Display::return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
  322. }
  323. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercice/overview.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&preview=1">'.
  324. Display::return_icon('preview_view.png', get_lang('Preview'),'',ICON_SIZE_MEDIUM).'</a>';
  325. echo Display::url(
  326. Display::return_icon('test_results.png', get_lang('Results'),'',ICON_SIZE_MEDIUM),
  327. api_get_path(WEB_CODE_PATH).'exercice/exercise_report.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id
  328. );
  329. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercice/exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.
  330. Display::return_icon('settings.png', get_lang('ModifyExercise'),'',ICON_SIZE_MEDIUM).'</a>';
  331. $maxScoreAllQuestions = 0;
  332. if (!empty($objExercise->questionList)) {
  333. foreach ($objExercise->questionList as $q) {
  334. $question = Question::read($q);
  335. if ($question) {
  336. $maxScoreAllQuestions += $question->selectWeighting();
  337. }
  338. }
  339. }
  340. echo '</div>';
  341. echo '<div class="alert alert-info">'.sprintf(get_lang('XQuestionsWithTotalScoreY'), $objExercise->selectNbrQuestions(), $maxScoreAllQuestions).'</div>';
  342. } else if (isset($_GET['newQuestion'])) {
  343. // we are in create a new question from question pool not in a test
  344. echo '<div class="actions">';
  345. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercice/admin.php?'.api_get_cidreq().'">'.
  346. Display::return_icon('back.png', get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).'</a>';
  347. echo '</div>';
  348. } else {
  349. // If we are in question_pool but not in an test, go back to question create in pool
  350. echo '<div class="actions">';
  351. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercice/question_pool.php?'.api_get_cidreq().'">'.
  352. Display::return_icon('back.png', get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).
  353. '</a>';
  354. echo '</div>';
  355. }
  356. if (isset($_GET['message'])) {
  357. if (in_array($_GET['message'], array('ExerciseStored', 'ItemUpdated', 'ItemAdded'))) {
  358. Display::display_confirmation_message(get_lang($_GET['message']));
  359. }
  360. }
  361. if ($newQuestion || $editQuestion) {
  362. // Question management
  363. $type = isset($_REQUEST['answerType']) ? Security::remove_XSS($_REQUEST['answerType']) : null;
  364. echo '<input type="hidden" name="Type" value="'.$type.'" />';
  365. if ($newQuestion == 'yes') {
  366. $objExercise->edit_exercise_in_lp = true;
  367. }
  368. require 'question_admin.inc.php';
  369. }
  370. if (isset($_GET['hotspotadmin'])) {
  371. if (!is_object($objQuestion)) {
  372. $objQuestion = Question :: read($_GET['hotspotadmin']);
  373. }
  374. if (!$objQuestion) {
  375. api_not_allowed();
  376. }
  377. require 'hotspot_admin.inc.php';
  378. }
  379. if (!$newQuestion && !$modifyQuestion && !$editQuestion && !isset($_GET['hotspotadmin'])) {
  380. // question list management
  381. require 'question_list_admin.inc.php';
  382. }
  383. // if we are in question authoring, display warning to user is feedback not shown at the end of the test -ref #6619
  384. // this test to display only message in the question authoring page and not in the question list page too
  385. // if (is_object($objQuestion) && $objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_EXAM && ($newQuestion || $modifyQuestion || $editQuestion)) {
  386. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_EXAM) {
  387. Display::display_normal_message(get_lang("TestFeedbackNotShown"));
  388. }
  389. Session::write('objExercise', $objExercise);
  390. Session::write('objQuestion', $objQuestion);
  391. Session::write('objAnswer', $objAnswer);
  392. Display::display_footer();