admin.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. * - answer_admin.inc.php : management of answers
  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. /**
  48. * Code
  49. */
  50. use \ChamiloSession as Session;
  51. require_once 'exercise.class.php';
  52. require_once 'question.class.php';
  53. require_once 'answer.class.php';
  54. // Name of the language file that needs to be included
  55. $language_file = 'exercice';
  56. require_once '../inc/global.inc.php';
  57. require_once 'exercise.lib.php';
  58. $current_course_tool = TOOL_QUIZ;
  59. $this_section = SECTION_COURSES;
  60. // Access control
  61. api_protect_course_script(true);
  62. $is_allowedToEdit = api_is_allowed_to_edit(null, true);
  63. if (!$is_allowedToEdit) {
  64. api_not_allowed(true);
  65. }
  66. require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
  67. require_once api_get_path(LIBRARY_PATH) . 'document.lib.php';
  68. /* stripslashes POST data */
  69. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  70. foreach ($_POST as $key => $val) {
  71. if (is_string($val)) {
  72. $_POST[$key] = stripslashes($val);
  73. } elseif (is_array($val)) {
  74. foreach ($val as $key2 => $val2) {
  75. $_POST[$key][$key2] = stripslashes($val2);
  76. }
  77. }
  78. $GLOBALS[$key] = $_POST[$key];
  79. }
  80. }
  81. // get vars from GET
  82. if (empty($exerciseId)) {
  83. $exerciseId = intval($_GET['exerciseId']);
  84. }
  85. if (empty($newQuestion)) {
  86. $newQuestion = $_GET['newQuestion'];
  87. }
  88. if (empty($modifyAnswers)) {
  89. $modifyAnswers = $_GET['modifyAnswers'];
  90. }
  91. if (empty($editQuestion)) {
  92. $editQuestion = $_GET['editQuestion'];
  93. }
  94. if (empty($modifyQuestion)) {
  95. $modifyQuestion = $_GET['modifyQuestion'];
  96. }
  97. if (empty($deleteQuestion)) {
  98. $deleteQuestion = $_GET['deleteQuestion'];
  99. }
  100. if (empty($clone_question)) {
  101. $clone_question = $_GET['clone_question'];
  102. }
  103. if (empty($questionId)) {
  104. $questionId = $_SESSION['questionId'];
  105. }
  106. if (empty($modifyExercise)) {
  107. $modifyExercise = $_GET['modifyExercise'];
  108. }
  109. //Cleaning all incomplete attempts of the admin/teacher to avoid weird problems when changing the exercise settings, number of questions, etc
  110. delete_all_incomplete_attempts(api_get_user_id(), $exerciseId, api_get_course_id(), api_get_session_id());
  111. // get from session
  112. $objExercise = $_SESSION['objExercise'];
  113. $objQuestion = $_SESSION['objQuestion'];
  114. $objAnswer = $_SESSION['objAnswer'];
  115. // document path
  116. $documentPath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document';
  117. // picture path
  118. $picturePath = $documentPath . '/images';
  119. // audio path
  120. $audioPath = $documentPath . '/audio';
  121. // the 5 types of answers
  122. $aType = array(get_lang('UniqueSelect'), get_lang('MultipleSelect'), get_lang('FillBlanks'), get_lang('Matching'), get_lang('FreeAnswer'));
  123. // tables used in the exercise tool
  124. //@todo remove if this declarations are not used
  125. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  126. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  127. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  128. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  129. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  130. if ($_GET['action'] == 'exportqti2' && !empty($_GET['questionId'])) {
  131. require_once 'export/qti2/qti2_export.php';
  132. $export = export_question($_GET['questionId'], true);
  133. $qid = (int) $_GET['questionId'];
  134. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  135. $temp_dir_short = uniqid();
  136. $temp_zip_dir = $archive_path . "/" . $temp_dir_short;
  137. if (!is_dir($temp_zip_dir))
  138. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  139. $temp_zip_file = $temp_zip_dir . "/" . api_get_unique_id() . ".zip";
  140. $temp_xml_file = $temp_zip_dir . "/qti2export_" . $qid . '.xml';
  141. file_put_contents($temp_xml_file, $export);
  142. $zip_folder = new PclZip($temp_zip_file);
  143. $zip_folder->add($temp_xml_file, PCLZIP_OPT_REMOVE_ALL_PATH);
  144. $name = 'qti2_export_' . $qid . '.zip';
  145. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  146. unlink($temp_zip_file);
  147. unlink($temp_xml_file);
  148. rmdir($temp_zip_dir);
  149. //DocumentManager::string_send_for_download($export,true,'qti2export_q'.$_GET['questionId'].'.xml');
  150. exit; //otherwise following clicks may become buggy
  151. }
  152. // intializes the Exercise object
  153. if (!is_object($objExercise)) {
  154. // construction of the Exercise object
  155. $objExercise = new Exercise();
  156. // creation of a new exercise if wrong or not specified exercise ID
  157. if ($exerciseId) {
  158. $objExercise->read($exerciseId);
  159. }
  160. // saves the object into the session
  161. Session::write('objExercise', $objExercise);
  162. }
  163. // doesn't select the exercise ID if we come from the question pool
  164. if (!$fromExercise) {
  165. // gets the right exercise ID, and if 0 creates a new exercise
  166. if (!$exerciseId = $objExercise->selectId()) {
  167. $modifyExercise = 'yes';
  168. }
  169. }
  170. $nbrQuestions = $objExercise->selectNbrQuestions();
  171. // intializes the Question object
  172. if ($editQuestion || $newQuestion || $modifyQuestion || $modifyAnswers) {
  173. if ($editQuestion || $newQuestion) {
  174. // reads question data
  175. if ($editQuestion) {
  176. // question not found
  177. if (!$objQuestion = Question::read($editQuestion)) {
  178. api_not_allowed();
  179. }
  180. // saves the object into the session
  181. Session::write('objQuestion', $objQuestion);
  182. }
  183. }
  184. // checks if the object exists
  185. if (is_object($objQuestion)) {
  186. // gets the question ID
  187. $questionId = $objQuestion->selectId();
  188. }
  189. }
  190. // if cancelling an exercise
  191. if ($cancelExercise) {
  192. // existing exercise
  193. if ($exerciseId) {
  194. unset($modifyExercise);
  195. } else {
  196. // new exercise
  197. // goes back to the exercise list
  198. header('Location: exercice.php');
  199. exit();
  200. }
  201. }
  202. // if cancelling question creation/modification
  203. if ($cancelQuestion) {
  204. // if we are creating a new question from the question pool
  205. if (!$exerciseId && !$questionId) {
  206. // goes back to the question pool
  207. header('Location: question_pool.php');
  208. exit();
  209. } else {
  210. // goes back to the question viewing
  211. $editQuestion = $modifyQuestion;
  212. unset($newQuestion, $modifyQuestion);
  213. }
  214. }
  215. if (isset($clone_question) && !empty($objExercise->id)) {
  216. $old_question_obj = Question::read($clone_question);
  217. $old_question_obj->question = $old_question_obj->question . ' - ' . get_lang('Copy');
  218. $new_id = $old_question_obj->duplicate();
  219. $new_question_obj = Question::read($new_id);
  220. $new_question_obj->addToList($exerciseId);
  221. // This should be moved to the duplicate function
  222. $new_answer_obj = new Answer($clone_question);
  223. $new_answer_obj->read();
  224. $new_answer_obj->duplicate($new_id);
  225. //Reloading tne $objExercise obj
  226. $objExercise->read($objExercise->id);
  227. header('Location: admin.php?' . api_get_cidreq() . '&exerciseId=' . $objExercise->id);
  228. exit;
  229. }
  230. // if cancelling answer creation/modification
  231. if ($cancelAnswers) {
  232. // goes back to the question viewing
  233. $editQuestion = $modifyAnswers;
  234. unset($modifyAnswers);
  235. }
  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 (isset($_SESSION['gradebook'])) {
  241. $gradebook = $_SESSION['gradebook'];
  242. }
  243. if (!empty($gradebook) && $gradebook == 'view') {
  244. $interbreadcrumb[] = array(
  245. 'url' => '../gradebook/' . $_SESSION['gradebook_dest'],
  246. 'name' => get_lang('ToolGradebook')
  247. );
  248. }
  249. $interbreadcrumb[] = array("url" => "exercice.php", "name" => get_lang('Exercices'));
  250. if (isset($_GET['newQuestion']) || isset($_GET['editQuestion'])) {
  251. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=" . $objExercise->id, "name" => $objExercise->name);
  252. } else {
  253. $interbreadcrumb[] = array("url" => "#", "name" => $objExercise->name);
  254. }
  255. // shows a link to go back to the question pool
  256. if (!$exerciseId && $nameTools != get_lang('ExerciseManagement')) {
  257. $interbreadcrumb[] = array("url" => "question_pool.php?fromExercise=$fromExercise", "name" => get_lang('QuestionPool'));
  258. }
  259. // if the question is duplicated, disable the link of tool name
  260. if ($modifyIn == 'thisExercise') {
  261. if ($buttonBack) {
  262. $modifyIn = 'allExercises';
  263. } else {
  264. $noPHP_SELF = true;
  265. }
  266. }
  267. $htmlHeadXtra[] = '<script>
  268. function multiple_answer_true_false_onchange(variable) {
  269. var result = variable.checked;
  270. var id = variable.id;
  271. var weight_id = "weighting_" + id;
  272. var array_result=new Array(); array_result[1]="1"; array_result[0]= "-0.50"; array_result[-1]= "0";
  273. if (result) {
  274. result = 1;
  275. } else {
  276. result = 0;
  277. }
  278. document.getElementById(weight_id).value = array_result[result];
  279. }
  280. </script>';
  281. $htmlHeadXtra[] = "<script type=\"text/javascript\" src=\"../plugin/hotspot/JavaScriptFlashGateway.js\"></script>
  282. <script src=\"../plugin/hotspot/hotspot.js\" type=\"text/javascript\"></script>
  283. <script language=\"JavaScript\" type=\"text/javascript\">
  284. <!--
  285. // Globals
  286. // Major version of Flash required
  287. var requiredMajorVersion = 7;
  288. // Minor version of Flash required
  289. var requiredMinorVersion = 0;
  290. // Minor version of Flash required
  291. var requiredRevision = 0;
  292. // the version of javascript supported
  293. var jsVersion = 1.0;
  294. // -->
  295. </script>
  296. <script language=\"VBScript\" type=\"text/vbscript\">
  297. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  298. Function VBGetSwfVer(i)
  299. on error resume next
  300. Dim swControl, swVersion
  301. swVersion = 0
  302. set swControl = CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" + CStr(i))
  303. if (IsObject(swControl)) then
  304. swVersion = swControl.GetVariable(\"\$version\")
  305. end if
  306. VBGetSwfVer = swVersion
  307. End Function
  308. // -->
  309. </script>
  310. <script language=\"JavaScript1.1\" type=\"text/javascript\">
  311. <!-- // Detect Client Browser type
  312. var isIE = (navigator.appVersion.indexOf(\"MSIE\") != -1) ? true : false;
  313. var isWin = (navigator.appVersion.toLowerCase().indexOf(\"win\") != -1) ? true : false;
  314. var isOpera = (navigator.userAgent.indexOf(\"Opera\") != -1) ? true : false;
  315. jsVersion = 1.1;
  316. // JavaScript helper required to detect Flash Player PlugIn version information
  317. function JSGetSwfVer(i){
  318. // NS/Opera version >= 3 check for Flash plugin in plugin array
  319. if (navigator.plugins != null && navigator.plugins.length > 0) {
  320. if (navigator.plugins[\"Shockwave Flash 2.0\"] || navigator.plugins[\"Shockwave Flash\"]) {
  321. var swVer2 = navigator.plugins[\"Shockwave Flash 2.0\"] ? \" 2.0\" : \"\";
  322. var flashDescription = navigator.plugins[\"Shockwave Flash\" + swVer2].description;
  323. descArray = flashDescription.split(\" \");
  324. tempArrayMajor = descArray[2].split(\".\");
  325. versionMajor = tempArrayMajor[0];
  326. versionMinor = tempArrayMajor[1];
  327. if ( descArray[3] != \"\" ) {
  328. tempArrayMinor = descArray[3].split(\"r\");
  329. } else {
  330. tempArrayMinor = descArray[4].split(\"r\");
  331. }
  332. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  333. flashVer = versionMajor + \".\" + versionMinor + \".\" + versionRevision;
  334. } else {
  335. flashVer = -1;
  336. }
  337. }
  338. // MSN/WebTV 2.6 supports Flash 4
  339. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.6\") != -1) flashVer = 4;
  340. // WebTV 2.5 supports Flash 3
  341. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.5\") != -1) flashVer = 3;
  342. // older WebTV supports Flash 2
  343. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv\") != -1) flashVer = 2;
  344. // Can't detect in all other cases
  345. else {
  346. flashVer = -1;
  347. }
  348. return flashVer;
  349. }
  350. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  351. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  352. {
  353. reqVer = parseFloat(reqMajorVer + \".\" + reqRevision);
  354. // loop backwards through the versions until we find the newest version
  355. for (i=25;i>0;i--) {
  356. if (isIE && isWin && !isOpera) {
  357. versionStr = VBGetSwfVer(i);
  358. } else {
  359. versionStr = JSGetSwfVer(i);
  360. }
  361. if (versionStr == -1 ) {
  362. return false;
  363. } else if (versionStr != 0) {
  364. if(isIE && isWin && !isOpera) {
  365. tempArray = versionStr.split(\" \");
  366. tempString = tempArray[1];
  367. versionArray = tempString .split(\",\");
  368. } else {
  369. versionArray = versionStr.split(\".\");
  370. }
  371. versionMajor = versionArray[0];
  372. versionMinor = versionArray[1];
  373. versionRevision = versionArray[2];
  374. versionString = versionMajor + \".\" + versionRevision; // 7.0r24 == 7.24
  375. versionNum = parseFloat(versionString);
  376. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  377. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  378. return true;
  379. } else {
  380. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  381. }
  382. }
  383. }
  384. }
  385. // -->
  386. </script>";
  387. Display::display_header($nameTools, 'Exercise');
  388. if ($objExercise->edit_exercise_in_lp == false) {
  389. Display::display_warning_message(get_lang('EditingExerciseCauseProblemsInLP'));
  390. }
  391. // If we are in a test
  392. $inATest = isset($exerciseId) && $exerciseId > 0;
  393. if ($inATest) {
  394. echo '<div class="actions">';
  395. if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion']) || isset($_GET['myid']))
  396. echo '<a href="admin.php?exerciseId=' . $exerciseId . '">' . Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM) . '</a>';
  397. if (!isset($_GET['hotspotadmin']) && !isset($_GET['newQuestion']) && !isset($_GET['myid']) && !isset($_GET['editQuestion'])) {
  398. echo '<a href="exercice.php?' . api_get_cidReq() . '">' . Display::return_icon('back.png', get_lang('BackToExercisesList'), '', ICON_SIZE_MEDIUM) . '</a>';
  399. }
  400. echo '<a href="overview.php?' . api_get_cidreq() . '&exerciseId=' . $objExercise->id . '&preview=1">' . Display::return_icon('preview_view.png', get_lang('Preview'), '', ICON_SIZE_MEDIUM) . '</a>';
  401. echo Display::url(Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_MEDIUM), 'exercise_report.php?' . api_get_cidReq() . '&exerciseId=' . $objExercise->id);
  402. if ($objExercise->edit_exercise_in_lp == false) {
  403. echo '<a href="">' . Display::return_icon('settings_na.png', get_lang('ModifyExercise'), '', ICON_SIZE_MEDIUM) . '</a>';
  404. } else {
  405. echo '<a href="exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.Display::return_icon('settings.png', get_lang('ModifyExercise'),'',ICON_SIZE_MEDIUM).'</a>';
  406. }
  407. $maxScoreAllQuestions = 0;
  408. if (!empty($objExercise->questionList)) {
  409. foreach ($objExercise->questionList as $q) {
  410. $question = Question::read($q);
  411. if ($question) {
  412. $maxScoreAllQuestions += $question->selectWeighting();
  413. }
  414. }
  415. }
  416. echo '<span style="float:right">' . sprintf(get_lang('XQuestionsWithTotalScoreY'), $objExercise->selectNbrQuestions(), $maxScoreAllQuestions) . '</span>';
  417. echo '</div>';
  418. } else if (isset($_GET['newQuestion'])) {
  419. // we are in create a new question from question pool not in a test
  420. echo '<div class="actions">';
  421. echo '<a href="admin.php?' . api_get_cidreq() . '">.' . Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM) . '</a>';
  422. echo '</div>';
  423. } else {
  424. // If we are in question_poolbut not in an test, go back to question create in pool
  425. echo '<div class="actions">';
  426. echo '<a href="question_pool.php">' . Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM) . '</a>';
  427. echo '</div>';
  428. }
  429. if (isset($_GET['message'])) {
  430. if (in_array($_GET['message'], array('ExerciseStored', 'ItemUpdated', 'ItemAdded'))) {
  431. Display::display_confirmation_message(get_lang($_GET['message']));
  432. }
  433. }
  434. if ($newQuestion || $editQuestion) {
  435. // statement management
  436. $type = Security::remove_XSS($_REQUEST['answerType']);
  437. echo '<input type="hidden" name="Type" value="' . $type . '" />';
  438. require 'question_admin.inc.php';
  439. }
  440. if (isset($_GET['hotspotadmin'])) {
  441. if (!is_object($objQuestion)) {
  442. $objQuestion = Question :: read($_GET['hotspotadmin']);
  443. }
  444. if (!$objQuestion) {
  445. api_not_allowed();
  446. }
  447. require 'hotspot_admin.inc.php';
  448. }
  449. if (!$newQuestion && !$modifyQuestion && !$editQuestion && !isset($_GET['hotspotadmin'])) {
  450. // question list management
  451. require 'question_list_admin.inc.php';
  452. }
  453. Session::write('objExercise', $objExercise);
  454. Session::write('objQuestion', $objQuestion);
  455. Session::write('objAnswer', $objAnswer);
  456. Display::display_footer();