admin.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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).'document.lib.php';
  67. /* stripslashes POST data */
  68. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  69. foreach ($_POST as $key => $val) {
  70. if (is_string($val)) {
  71. $_POST[$key] = stripslashes($val);
  72. } elseif (is_array($val)) {
  73. foreach ($val as $key2 => $val2) {
  74. $_POST[$key][$key2] = stripslashes($val2);
  75. }
  76. }
  77. $GLOBALS[$key] = $_POST[$key];
  78. }
  79. }
  80. // get vars from GET
  81. if (empty($exerciseId)) {
  82. $exerciseId = isset($_GET['exerciseId'])?intval($_GET['exerciseId']):'0';
  83. }
  84. if (empty($newQuestion)) {
  85. $newQuestion = isset($_GET['newQuestion'])?$_GET['newQuestion']:0;
  86. }
  87. if (empty($modifyAnswers)) {
  88. $modifyAnswers = isset($_GET['modifyAnswers'])?$_GET['modifyAnswers']:0;
  89. }
  90. if (empty($editQuestion)) {
  91. $editQuestion = isset($_GET['editQuestion'])?$_GET['editQuestion']:0;
  92. }
  93. if (empty($modifyQuestion)) {
  94. $modifyQuestion = isset($_GET['modifyQuestion'])?$_GET['modifyQuestion']:0;
  95. }
  96. if (empty($deleteQuestion)) {
  97. $deleteQuestion = isset($_GET['deleteQuestion'])?$_GET['deleteQuestion']:0;
  98. }
  99. if (empty($clone_question)) {
  100. $clone_question = isset($_GET['clone_question'])?$_GET['clone_question']:0;
  101. }
  102. if (empty($questionId)) {
  103. $questionId = isset($_SESSION['questionId'])?$_SESSION['questionId']:0;
  104. }
  105. if (empty($modifyExercise)) {
  106. $modifyExercise = isset($_GET['modifyExercise'])?$_GET['modifyExercise']:0;
  107. }
  108. //Cleaning all incomplete attempts of the admin/teacher to avoid weird problems when changing the exercise settings, number of questions, etc
  109. delete_all_incomplete_attempts(api_get_user_id(), $exerciseId, api_get_course_id(), api_get_session_id());
  110. // get from session
  111. $objExercise = isset($_SESSION['objExercise'])?$_SESSION['objExercise']:0;
  112. $objQuestion = isset($_SESSION['objQuestion'])?$_SESSION['objQuestion']:0;
  113. $objAnswer = isset($_SESSION['objAnswer'])?$_SESSION['objAnswer']:0;
  114. // document path
  115. $documentPath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  116. // picture path
  117. $picturePath = $documentPath.'/images';
  118. // audio path
  119. $audioPath = $documentPath.'/audio';
  120. // the 5 types of answers
  121. $aType = array(
  122. get_lang('UniqueSelect'),
  123. get_lang('MultipleSelect'),
  124. get_lang('FillBlanks'),
  125. get_lang('Matching'),
  126. get_lang('FreeAnswer')
  127. );
  128. // tables used in the exercise tool
  129. if (!empty($_GET['action']) && $_GET['action'] == 'exportqti2' && !empty($_GET['questionId'])) {
  130. require_once 'export/qti2/qti2_export.php';
  131. $export = export_question($_GET['questionId'], true);
  132. $qid = (int)$_GET['questionId'];
  133. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  134. $temp_dir_short = uniqid();
  135. $temp_zip_dir = $archive_path."/".$temp_dir_short;
  136. if (!is_dir($temp_zip_dir)) {
  137. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  138. }
  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 (!isset($fromExercise) or !$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 (!empty($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 (!empty($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 (!empty($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 (!empty($cancelAnswers)) {
  232. // goes back to the question viewing
  233. $editQuestion = $modifyAnswers;
  234. unset($modifyAnswers);
  235. }
  236. $nameTools = get_lang('ExerciseManagement');
  237. // modifies the query string that is used in the link of tool name
  238. if ($editQuestion || $modifyQuestion || $newQuestion || $modifyAnswers) {
  239. $nameTools = get_lang('QuestionManagement');
  240. }
  241. if (isset($_SESSION['gradebook'])) {
  242. $gradebook = $_SESSION['gradebook'];
  243. }
  244. if (!empty($gradebook) && $gradebook == 'view') {
  245. $interbreadcrumb[] = array(
  246. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  247. 'name' => get_lang('ToolGradebook')
  248. );
  249. }
  250. $interbreadcrumb[] = array("url" => "exercice.php", "name" => get_lang('Exercices'));
  251. if (isset($_GET['newQuestion']) || isset($_GET['editQuestion'])) {
  252. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=".$objExercise->id, "name" => $objExercise->name);
  253. } else {
  254. $interbreadcrumb[] = array("url" => "#", "name" => $objExercise->name);
  255. }
  256. // shows a link to go back to the question pool
  257. if (!$exerciseId && $nameTools != get_lang('ExerciseManagement')) {
  258. $interbreadcrumb[] = array(
  259. "url" => "question_pool.php?fromExercise=$fromExercise",
  260. "name" => get_lang('QuestionPool')
  261. );
  262. }
  263. // if the question is duplicated, disable the link of tool name
  264. if (!empty($modifyIn) && $modifyIn == 'thisExercise') {
  265. if ($buttonBack) {
  266. $modifyIn = 'allExercises';
  267. } else {
  268. $noPHP_SELF = true;
  269. }
  270. }
  271. $htmlHeadXtra[] = '<script>
  272. function multiple_answer_true_false_onchange(variable) {
  273. var result = variable.checked;
  274. var id = variable.id;
  275. var weight_id = "weighting_" + id;
  276. var array_result=new Array(); array_result[1]="1"; array_result[0]= "-0.50"; array_result[-1]= "0";
  277. if (result) {
  278. result = 1;
  279. } else {
  280. result = 0;
  281. }
  282. document.getElementById(weight_id).value = array_result[result];
  283. }
  284. </script>';
  285. $htmlHeadXtra[] = "<script type=\"text/javascript\" src=\"../plugin/hotspot/JavaScriptFlashGateway.js\"></script>
  286. <script src=\"../plugin/hotspot/hotspot.js\" type=\"text/javascript\"></script>
  287. <script language=\"JavaScript\" type=\"text/javascript\">
  288. <!--
  289. // Globals
  290. // Major version of Flash required
  291. var requiredMajorVersion = 7;
  292. // Minor version of Flash required
  293. var requiredMinorVersion = 0;
  294. // Minor version of Flash required
  295. var requiredRevision = 0;
  296. // the version of javascript supported
  297. var jsVersion = 1.0;
  298. // -->
  299. </script>
  300. <script language=\"VBScript\" type=\"text/vbscript\">
  301. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  302. Function VBGetSwfVer(i)
  303. on error resume next
  304. Dim swControl, swVersion
  305. swVersion = 0
  306. set swControl = CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" + CStr(i))
  307. if (IsObject(swControl)) then
  308. swVersion = swControl.GetVariable(\"\$version\")
  309. end if
  310. VBGetSwfVer = swVersion
  311. End Function
  312. // -->
  313. </script>
  314. <script language=\"JavaScript1.1\" type=\"text/javascript\">
  315. <!-- // Detect Client Browser type
  316. var isIE = (navigator.appVersion.indexOf(\"MSIE\") != -1) ? true : false;
  317. var isWin = (navigator.appVersion.toLowerCase().indexOf(\"win\") != -1) ? true : false;
  318. var isOpera = (navigator.userAgent.indexOf(\"Opera\") != -1) ? true : false;
  319. jsVersion = 1.1;
  320. // JavaScript helper required to detect Flash Player PlugIn version information
  321. function JSGetSwfVer(i){
  322. // NS/Opera version >= 3 check for Flash plugin in plugin array
  323. if (navigator.plugins != null && navigator.plugins.length > 0) {
  324. if (navigator.plugins[\"Shockwave Flash 2.0\"] || navigator.plugins[\"Shockwave Flash\"]) {
  325. var swVer2 = navigator.plugins[\"Shockwave Flash 2.0\"] ? \" 2.0\" : \"\";
  326. var flashDescription = navigator.plugins[\"Shockwave Flash\" + swVer2].description;
  327. descArray = flashDescription.split(\" \");
  328. tempArrayMajor = descArray[2].split(\".\");
  329. versionMajor = tempArrayMajor[0];
  330. versionMinor = tempArrayMajor[1];
  331. if ( descArray[3] != \"\" ) {
  332. tempArrayMinor = descArray[3].split(\"r\");
  333. } else {
  334. tempArrayMinor = descArray[4].split(\"r\");
  335. }
  336. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  337. flashVer = versionMajor + \".\" + versionMinor + \".\" + versionRevision;
  338. } else {
  339. flashVer = -1;
  340. }
  341. }
  342. // MSN/WebTV 2.6 supports Flash 4
  343. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.6\") != -1) flashVer = 4;
  344. // WebTV 2.5 supports Flash 3
  345. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.5\") != -1) flashVer = 3;
  346. // older WebTV supports Flash 2
  347. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv\") != -1) flashVer = 2;
  348. // Can't detect in all other cases
  349. else {
  350. flashVer = -1;
  351. }
  352. return flashVer;
  353. }
  354. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  355. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  356. {
  357. reqVer = parseFloat(reqMajorVer + \".\" + reqRevision);
  358. // loop backwards through the versions until we find the newest version
  359. for (i=25;i>0;i--) {
  360. if (isIE && isWin && !isOpera) {
  361. versionStr = VBGetSwfVer(i);
  362. } else {
  363. versionStr = JSGetSwfVer(i);
  364. }
  365. if (versionStr == -1 ) {
  366. return false;
  367. } else if (versionStr != 0) {
  368. if(isIE && isWin && !isOpera) {
  369. tempArray = versionStr.split(\" \");
  370. tempString = tempArray[1];
  371. versionArray = tempString .split(\",\");
  372. } else {
  373. versionArray = versionStr.split(\".\");
  374. }
  375. versionMajor = versionArray[0];
  376. versionMinor = versionArray[1];
  377. versionRevision = versionArray[2];
  378. versionString = versionMajor + \".\" + versionRevision; // 7.0r24 == 7.24
  379. versionNum = parseFloat(versionString);
  380. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  381. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  382. return true;
  383. } else {
  384. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  385. }
  386. }
  387. }
  388. }
  389. // -->
  390. </script>";
  391. Display::display_header($nameTools, 'Exercise');
  392. if ($objExercise->exercise_was_added_in_lp) {
  393. if ($objExercise->force_edit_exercise_in_lp == true) {
  394. Display::display_warning_message(get_lang('ForceEditingExerciseInLPWarning'));
  395. } else {
  396. Display::display_warning_message(get_lang('EditingExerciseCauseProblemsInLP'));
  397. }
  398. }
  399. // If we are in a test
  400. $inATest = isset($exerciseId) && $exerciseId > 0;
  401. if ($inATest) {
  402. echo '<div class="actions">';
  403. if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion']) || isset($_GET['myid'])) {
  404. echo '<a href="admin.php?exerciseId='.$exerciseId.'">'.Display::return_icon(
  405. 'back.png',
  406. get_lang('GoBackToQuestionList'),
  407. '',
  408. ICON_SIZE_MEDIUM
  409. ).'</a>';
  410. }
  411. if (!isset($_GET['hotspotadmin']) && !isset($_GET['newQuestion']) && !isset($_GET['myid']) && !isset($_GET['editQuestion'])) {
  412. echo '<a href="exercice.php?'.api_get_cidReq().'">'.Display::return_icon(
  413. 'back.png',
  414. get_lang('BackToExercisesList'),
  415. '',
  416. ICON_SIZE_MEDIUM
  417. ).'</a>';
  418. }
  419. echo '<a href="overview.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&preview=1">'.Display::return_icon(
  420. 'preview_view.png',
  421. get_lang('Preview'),
  422. '',
  423. ICON_SIZE_MEDIUM
  424. ).'</a>';
  425. echo Display::url(
  426. Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_MEDIUM),
  427. 'exercise_report.php?'.api_get_cidReq().'&exerciseId='.$objExercise->id
  428. );
  429. if ($objExercise->edit_exercise_in_lp == false) {
  430. echo '<a href="">'.Display::return_icon(
  431. 'settings_na.png',
  432. get_lang('ModifyExercise'),
  433. '',
  434. ICON_SIZE_MEDIUM
  435. ).'</a>';
  436. } else {
  437. echo '<a href="exercise_admin.php?'.api_get_cidreq(
  438. ).'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.Display::return_icon(
  439. 'settings.png',
  440. get_lang('ModifyExercise'),
  441. '',
  442. ICON_SIZE_MEDIUM
  443. ).'</a>';
  444. }
  445. $maxScoreAllQuestions = 0;
  446. if (!empty($objExercise->questionList)) {
  447. foreach ($objExercise->questionList as $q) {
  448. $question = Question::read($q);
  449. if ($question) {
  450. $maxScoreAllQuestions += $question->selectWeighting();
  451. }
  452. }
  453. }
  454. echo '<span style="float:right">'.sprintf(
  455. get_lang('XQuestionsWithTotalScoreY'),
  456. $objExercise->selectNbrQuestions(),
  457. $maxScoreAllQuestions
  458. ).'</span>';
  459. echo '</div>';
  460. } else {
  461. if (isset($_GET['newQuestion'])) {
  462. // we are in create a new question from question pool not in a test
  463. echo '<div class="actions">';
  464. echo '<a href="admin.php?'.api_get_cidreq().'">.'.Display::return_icon(
  465. 'back.png',
  466. get_lang('GoBackToQuestionList'),
  467. '',
  468. ICON_SIZE_MEDIUM
  469. ).'</a>';
  470. echo '</div>';
  471. } else {
  472. // If we are in question_poolbut not in an test, go back to question create in pool
  473. echo '<div class="actions">';
  474. echo '<a href="question_pool.php">'.Display::return_icon(
  475. 'back.png',
  476. get_lang('GoBackToQuestionList'),
  477. '',
  478. ICON_SIZE_MEDIUM
  479. ).'</a>';
  480. echo '</div>';
  481. }
  482. }
  483. if (isset($_GET['message'])) {
  484. if (in_array($_GET['message'], array('ExerciseStored', 'ItemUpdated', 'ItemAdded'))) {
  485. Display::display_confirmation_message(get_lang($_GET['message']));
  486. }
  487. }
  488. if ($newQuestion || $editQuestion) {
  489. // statement management
  490. $type = Security::remove_XSS($_REQUEST['answerType']);
  491. echo '<input type="hidden" name="Type" value="'.$type.'" />';
  492. require 'question_admin.inc.php';
  493. }
  494. if (isset($_GET['hotspotadmin'])) {
  495. if (!is_object($objQuestion)) {
  496. $objQuestion = Question :: read($_GET['hotspotadmin']);
  497. }
  498. if (!$objQuestion) {
  499. api_not_allowed();
  500. }
  501. require 'hotspot_admin.inc.php';
  502. }
  503. if (!$newQuestion && !$modifyQuestion && !$editQuestion && !isset($_GET['hotspotadmin'])) {
  504. // question list management
  505. require 'question_list_admin.inc.php';
  506. }
  507. Session::write('objExercise', $objExercise);
  508. Session::write('objQuestion', $objQuestion);
  509. Session::write('objAnswer', $objAnswer);
  510. Display::display_footer();