admin.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. /* stripslashes POST data */
  67. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  68. foreach ($_POST as $key => $val) {
  69. if (is_string($val)) {
  70. $_POST[$key] = stripslashes($val);
  71. } elseif (is_array($val)) {
  72. foreach ($val as $key2 => $val2) {
  73. $_POST[$key][$key2] = stripslashes($val2);
  74. }
  75. }
  76. $GLOBALS[$key] = $_POST[$key];
  77. }
  78. }
  79. // get vars from GET
  80. if (empty($exerciseId)) {
  81. $exerciseId = isset($_GET['exerciseId'])?intval($_GET['exerciseId']):'0';
  82. }
  83. if (empty($newQuestion)) {
  84. $newQuestion = isset($_GET['newQuestion'])?$_GET['newQuestion']:0;
  85. }
  86. if (empty($modifyAnswers)) {
  87. $modifyAnswers = isset($_GET['modifyAnswers'])?$_GET['modifyAnswers']:0;
  88. }
  89. if (empty($editQuestion)) {
  90. $editQuestion = isset($_GET['editQuestion'])?$_GET['editQuestion']:0;
  91. }
  92. if (empty($modifyQuestion)) {
  93. $modifyQuestion = isset($_GET['modifyQuestion'])?$_GET['modifyQuestion']:0;
  94. }
  95. if (empty($deleteQuestion)) {
  96. $deleteQuestion = isset($_GET['deleteQuestion'])?$_GET['deleteQuestion']:0;
  97. }
  98. if (empty($clone_question)) {
  99. $clone_question = isset($_GET['clone_question'])?$_GET['clone_question']:0;
  100. }
  101. if (empty($questionId)) {
  102. $questionId = isset($_SESSION['questionId'])?$_SESSION['questionId']:0;
  103. }
  104. if (empty($modifyExercise)) {
  105. $modifyExercise = isset($_GET['modifyExercise'])?$_GET['modifyExercise']:0;
  106. }
  107. //Cleaning all incomplete attempts of the admin/teacher to avoid weird problems when changing the exercise settings, number of questions, etc
  108. delete_all_incomplete_attempts(api_get_user_id(), $exerciseId, api_get_course_id(), api_get_session_id());
  109. // get from session
  110. $objExercise = isset($_SESSION['objExercise'])?$_SESSION['objExercise']:0;
  111. $objQuestion = isset($_SESSION['objQuestion'])?$_SESSION['objQuestion']:0;
  112. $objAnswer = isset($_SESSION['objAnswer'])?$_SESSION['objAnswer']:0;
  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($_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. // intializes the Exercise object
  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. // doesn't select the exercise ID if we come from the question pool
  163. if (!isset($fromExercise) or !$fromExercise) {
  164. // gets the right exercise ID, and if 0 creates a new exercise
  165. if (!$exerciseId = $objExercise->selectId()) {
  166. $modifyExercise = 'yes';
  167. }
  168. }
  169. $nbrQuestions = $objExercise->selectNbrQuestions();
  170. // intializes the Question object
  171. if ($editQuestion || $newQuestion || $modifyQuestion || $modifyAnswers) {
  172. if ($editQuestion || $newQuestion) {
  173. // reads question data
  174. if ($editQuestion) {
  175. // question not found
  176. if (!$objQuestion = Question::read($editQuestion)) {
  177. api_not_allowed();
  178. }
  179. // saves the object into the session
  180. Session::write('objQuestion', $objQuestion);
  181. }
  182. }
  183. // checks if the object exists
  184. if (is_object($objQuestion)) {
  185. // gets the question ID
  186. $questionId = $objQuestion->selectId();
  187. }
  188. }
  189. // if cancelling an exercise
  190. if (!empty($cancelExercise)) {
  191. // existing exercise
  192. if ($exerciseId) {
  193. unset($modifyExercise);
  194. } else {
  195. // new exercise
  196. // goes back to the exercise list
  197. header('Location: exercice.php');
  198. exit();
  199. }
  200. }
  201. // if cancelling question creation/modification
  202. if (!empty($cancelQuestion)) {
  203. // if we are creating a new question from the question pool
  204. if (!$exerciseId && !$questionId) {
  205. // goes back to the question pool
  206. header('Location: question_pool.php');
  207. exit();
  208. } else {
  209. // goes back to the question viewing
  210. $editQuestion = $modifyQuestion;
  211. unset($newQuestion, $modifyQuestion);
  212. }
  213. }
  214. if (!empty($clone_question) && !empty($objExercise->id)) {
  215. $old_question_obj = Question::read($clone_question);
  216. $old_question_obj->question = $old_question_obj->question.' - '.get_lang('Copy');
  217. $new_id = $old_question_obj->duplicate();
  218. $new_question_obj = Question::read($new_id);
  219. $new_question_obj->addToList($exerciseId);
  220. // This should be moved to the duplicate function
  221. $new_answer_obj = new Answer($clone_question);
  222. $new_answer_obj->read();
  223. $new_answer_obj->duplicate($new_id);
  224. //Reloading tne $objExercise obj
  225. $objExercise->read($objExercise->id);
  226. header('Location: admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id);
  227. exit;
  228. }
  229. // if cancelling answer creation/modification
  230. if (!empty($cancelAnswers)) {
  231. // goes back to the question viewing
  232. $editQuestion = $modifyAnswers;
  233. unset($modifyAnswers);
  234. }
  235. $nameTools = get_lang('ExerciseManagement');
  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(
  258. "url" => "question_pool.php?fromExercise=$fromExercise",
  259. "name" => get_lang('QuestionPool')
  260. );
  261. }
  262. // if the question is duplicated, disable the link of tool name
  263. if (!empty($modifyIn) && $modifyIn == 'thisExercise') {
  264. if ($buttonBack) {
  265. $modifyIn = 'allExercises';
  266. } else {
  267. $noPHP_SELF = true;
  268. }
  269. }
  270. $htmlHeadXtra[] = '<script>
  271. function multiple_answer_true_false_onchange(variable) {
  272. var result = variable.checked;
  273. var id = variable.id;
  274. var weight_id = "weighting_" + id;
  275. var array_result=new Array(); array_result[1]="1"; array_result[0]= "-0.50"; array_result[-1]= "0";
  276. if (result) {
  277. result = 1;
  278. } else {
  279. result = 0;
  280. }
  281. document.getElementById(weight_id).value = array_result[result];
  282. }
  283. </script>';
  284. $htmlHeadXtra[] = "<script type=\"text/javascript\" src=\"../plugin/hotspot/JavaScriptFlashGateway.js\"></script>
  285. <script src=\"../plugin/hotspot/hotspot.js\" type=\"text/javascript\"></script>
  286. <script language=\"JavaScript\" type=\"text/javascript\">
  287. <!--
  288. // Globals
  289. // Major version of Flash required
  290. var requiredMajorVersion = 7;
  291. // Minor version of Flash required
  292. var requiredMinorVersion = 0;
  293. // Minor version of Flash required
  294. var requiredRevision = 0;
  295. // the version of javascript supported
  296. var jsVersion = 1.0;
  297. // -->
  298. </script>
  299. <script language=\"VBScript\" type=\"text/vbscript\">
  300. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  301. Function VBGetSwfVer(i)
  302. on error resume next
  303. Dim swControl, swVersion
  304. swVersion = 0
  305. set swControl = CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" + CStr(i))
  306. if (IsObject(swControl)) then
  307. swVersion = swControl.GetVariable(\"\$version\")
  308. end if
  309. VBGetSwfVer = swVersion
  310. End Function
  311. // -->
  312. </script>
  313. <script language=\"JavaScript1.1\" type=\"text/javascript\">
  314. <!-- // Detect Client Browser type
  315. var isIE = (navigator.appVersion.indexOf(\"MSIE\") != -1) ? true : false;
  316. var isWin = (navigator.appVersion.toLowerCase().indexOf(\"win\") != -1) ? true : false;
  317. var isOpera = (navigator.userAgent.indexOf(\"Opera\") != -1) ? true : false;
  318. jsVersion = 1.1;
  319. // JavaScript helper required to detect Flash Player PlugIn version information
  320. function JSGetSwfVer(i){
  321. // NS/Opera version >= 3 check for Flash plugin in plugin array
  322. if (navigator.plugins != null && navigator.plugins.length > 0) {
  323. if (navigator.plugins[\"Shockwave Flash 2.0\"] || navigator.plugins[\"Shockwave Flash\"]) {
  324. var swVer2 = navigator.plugins[\"Shockwave Flash 2.0\"] ? \" 2.0\" : \"\";
  325. var flashDescription = navigator.plugins[\"Shockwave Flash\" + swVer2].description;
  326. descArray = flashDescription.split(\" \");
  327. tempArrayMajor = descArray[2].split(\".\");
  328. versionMajor = tempArrayMajor[0];
  329. versionMinor = tempArrayMajor[1];
  330. if ( descArray[3] != \"\" ) {
  331. tempArrayMinor = descArray[3].split(\"r\");
  332. } else {
  333. tempArrayMinor = descArray[4].split(\"r\");
  334. }
  335. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  336. flashVer = versionMajor + \".\" + versionMinor + \".\" + versionRevision;
  337. } else {
  338. flashVer = -1;
  339. }
  340. }
  341. // MSN/WebTV 2.6 supports Flash 4
  342. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.6\") != -1) flashVer = 4;
  343. // WebTV 2.5 supports Flash 3
  344. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.5\") != -1) flashVer = 3;
  345. // older WebTV supports Flash 2
  346. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv\") != -1) flashVer = 2;
  347. // Can't detect in all other cases
  348. else {
  349. flashVer = -1;
  350. }
  351. return flashVer;
  352. }
  353. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  354. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  355. {
  356. reqVer = parseFloat(reqMajorVer + \".\" + reqRevision);
  357. // loop backwards through the versions until we find the newest version
  358. for (i=25;i>0;i--) {
  359. if (isIE && isWin && !isOpera) {
  360. versionStr = VBGetSwfVer(i);
  361. } else {
  362. versionStr = JSGetSwfVer(i);
  363. }
  364. if (versionStr == -1 ) {
  365. return false;
  366. } else if (versionStr != 0) {
  367. if(isIE && isWin && !isOpera) {
  368. tempArray = versionStr.split(\" \");
  369. tempString = tempArray[1];
  370. versionArray = tempString .split(\",\");
  371. } else {
  372. versionArray = versionStr.split(\".\");
  373. }
  374. versionMajor = versionArray[0];
  375. versionMinor = versionArray[1];
  376. versionRevision = versionArray[2];
  377. versionString = versionMajor + \".\" + versionRevision; // 7.0r24 == 7.24
  378. versionNum = parseFloat(versionString);
  379. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  380. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  381. return true;
  382. } else {
  383. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  384. }
  385. }
  386. }
  387. }
  388. // -->
  389. </script>";
  390. Display::display_header($nameTools, 'Exercise');
  391. if ($objExercise->exercise_was_added_in_lp) {
  392. if ($objExercise->force_edit_exercise_in_lp == true) {
  393. Display::display_warning_message(get_lang('ForceEditingExerciseInLPWarning'));
  394. } else {
  395. Display::display_warning_message(get_lang('EditingExerciseCauseProblemsInLP'));
  396. }
  397. }
  398. // If we are in a test
  399. $inATest = isset($exerciseId) && $exerciseId > 0;
  400. if ($inATest) {
  401. echo '<div class="actions">';
  402. if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion']) || isset($_GET['myid'])) {
  403. echo '<a href="admin.php?exerciseId='.$exerciseId.'">'.Display::return_icon(
  404. 'back.png',
  405. get_lang('GoBackToQuestionList'),
  406. '',
  407. ICON_SIZE_MEDIUM
  408. ).'</a>';
  409. }
  410. if (!isset($_GET['hotspotadmin']) && !isset($_GET['newQuestion']) && !isset($_GET['myid']) && !isset($_GET['editQuestion'])) {
  411. echo '<a href="exercice.php?'.api_get_cidReq().'">'.Display::return_icon(
  412. 'back.png',
  413. get_lang('BackToExercisesList'),
  414. '',
  415. ICON_SIZE_MEDIUM
  416. ).'</a>';
  417. }
  418. echo '<a href="overview.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&preview=1">'.Display::return_icon(
  419. 'preview_view.png',
  420. get_lang('Preview'),
  421. '',
  422. ICON_SIZE_MEDIUM
  423. ).'</a>';
  424. echo Display::url(
  425. Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_MEDIUM),
  426. 'exercise_report.php?'.api_get_cidReq().'&exerciseId='.$objExercise->id
  427. );
  428. if ($objExercise->edit_exercise_in_lp == false) {
  429. echo '<a href="">'.Display::return_icon(
  430. 'settings_na.png',
  431. get_lang('ModifyExercise'),
  432. '',
  433. ICON_SIZE_MEDIUM
  434. ).'</a>';
  435. } else {
  436. echo '<a href="exercise_admin.php?'.api_get_cidreq(
  437. ).'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.Display::return_icon(
  438. 'settings.png',
  439. get_lang('ModifyExercise'),
  440. '',
  441. ICON_SIZE_MEDIUM
  442. ).'</a>';
  443. }
  444. $maxScoreAllQuestions = 0;
  445. if (!empty($objExercise->questionList)) {
  446. foreach ($objExercise->questionList as $q) {
  447. $question = Question::read($q);
  448. if ($question) {
  449. $maxScoreAllQuestions += $question->selectWeighting();
  450. }
  451. }
  452. }
  453. echo '<span style="float:right">'.sprintf(
  454. get_lang('XQuestionsWithTotalScoreY'),
  455. $objExercise->selectNbrQuestions(),
  456. $maxScoreAllQuestions
  457. ).'</span>';
  458. echo '</div>';
  459. } else {
  460. if (isset($_GET['newQuestion'])) {
  461. // we are in create a new question from question pool not in a test
  462. echo '<div class="actions">';
  463. echo '<a href="admin.php?'.api_get_cidreq().'">.'.Display::return_icon(
  464. 'back.png',
  465. get_lang('GoBackToQuestionList'),
  466. '',
  467. ICON_SIZE_MEDIUM
  468. ).'</a>';
  469. echo '</div>';
  470. } else {
  471. // If we are in question_poolbut not in an test, go back to question create in pool
  472. echo '<div class="actions">';
  473. echo '<a href="question_pool.php">'.Display::return_icon(
  474. 'back.png',
  475. get_lang('GoBackToQuestionList'),
  476. '',
  477. ICON_SIZE_MEDIUM
  478. ).'</a>';
  479. echo '</div>';
  480. }
  481. }
  482. if (isset($_GET['message'])) {
  483. if (in_array($_GET['message'], array('ExerciseStored', 'ItemUpdated', 'ItemAdded'))) {
  484. Display::display_confirmation_message(get_lang($_GET['message']));
  485. }
  486. }
  487. if ($newQuestion || $editQuestion) {
  488. // statement management
  489. $type = Security::remove_XSS($_REQUEST['answerType']);
  490. echo '<input type="hidden" name="Type" value="'.$type.'" />';
  491. require 'question_admin.inc.php';
  492. }
  493. if (isset($_GET['hotspotadmin'])) {
  494. if (!is_object($objQuestion)) {
  495. $objQuestion = Question :: read($_GET['hotspotadmin']);
  496. }
  497. if (!$objQuestion) {
  498. api_not_allowed();
  499. }
  500. require 'hotspot_admin.inc.php';
  501. }
  502. if (!$newQuestion && !$modifyQuestion && !$editQuestion && !isset($_GET['hotspotadmin'])) {
  503. // question list management
  504. require 'question_list_admin.inc.php';
  505. }
  506. Session::write('objExercise', $objExercise);
  507. Session::write('objQuestion', $objQuestion);
  508. Session::write('objAnswer', $objAnswer);
  509. Display::display_footer();