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. if ($_GET['action'] == 'exportqti2' && !empty($_GET['questionId'])) {
  125. require_once 'export/qti2/qti2_export.php';
  126. $export = export_question($_GET['questionId'],true);
  127. $qid = (int)$_GET['questionId'];
  128. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  129. $temp_dir_short = uniqid();
  130. $temp_zip_dir = $archive_path."/".$temp_dir_short;
  131. if(!is_dir($temp_zip_dir)) mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  132. $temp_zip_file = $temp_zip_dir."/".api_get_unique_id().".zip";
  133. $temp_xml_file = $temp_zip_dir."/qti2export_".$qid.'.xml';
  134. file_put_contents($temp_xml_file,$export);
  135. $zip_folder=new PclZip($temp_zip_file);
  136. $zip_folder->add($temp_xml_file, PCLZIP_OPT_REMOVE_ALL_PATH);
  137. $name = 'qti2_export_'.$qid.'.zip';
  138. DocumentManager::file_send_for_download($temp_zip_file,true,$name);
  139. unlink($temp_zip_file);
  140. unlink($temp_xml_file);
  141. rmdir($temp_zip_dir);
  142. //DocumentManager::string_send_for_download($export,true,'qti2export_q'.$_GET['questionId'].'.xml');
  143. exit; //otherwise following clicks may become buggy
  144. }
  145. // intializes the Exercise object
  146. if (!is_object($objExercise)) {
  147. // construction of the Exercise object
  148. $objExercise = new Exercise();
  149. // creation of a new exercise if wrong or not specified exercise ID
  150. if ($exerciseId) {
  151. $objExercise->read($exerciseId);
  152. }
  153. // saves the object into the session
  154. Session::write('objExercise',$objExercise);
  155. }
  156. // doesn't select the exercise ID if we come from the question pool
  157. if(!$fromExercise) {
  158. // gets the right exercise ID, and if 0 creates a new exercise
  159. if(!$exerciseId = $objExercise->selectId()) {
  160. $modifyExercise='yes';
  161. }
  162. }
  163. $nbrQuestions = $objExercise->selectNbrQuestions();
  164. // intializes the Question object
  165. if ($editQuestion || $newQuestion || $modifyQuestion || $modifyAnswers) {
  166. if ($editQuestion || $newQuestion) {
  167. // reads question data
  168. if ($editQuestion) {
  169. // question not found
  170. if (!$objQuestion = Question::read($editQuestion)) {
  171. api_not_allowed();
  172. }
  173. // saves the object into the session
  174. Session::write('objQuestion',$objQuestion);
  175. }
  176. }
  177. // checks if the object exists
  178. if(is_object($objQuestion)) {
  179. // gets the question ID
  180. $questionId = $objQuestion->selectId();
  181. }
  182. }
  183. // if cancelling an exercise
  184. if ($cancelExercise) {
  185. // existing exercise
  186. if($exerciseId) {
  187. unset($modifyExercise);
  188. } else {
  189. // new exercise
  190. // goes back to the exercise list
  191. header('Location: exercice.php');
  192. exit();
  193. }
  194. }
  195. // if cancelling question creation/modification
  196. if ($cancelQuestion) {
  197. // if we are creating a new question from the question pool
  198. if(!$exerciseId && !$questionId) {
  199. // goes back to the question pool
  200. header('Location: question_pool.php');
  201. exit();
  202. } else {
  203. // goes back to the question viewing
  204. $editQuestion=$modifyQuestion;
  205. unset($newQuestion,$modifyQuestion);
  206. }
  207. }
  208. if (isset($clone_question) && !empty($objExercise->id)) {
  209. $old_question_obj = Question::read($clone_question);
  210. $old_question_obj->question = $old_question_obj->question.' - '.get_lang('Copy');
  211. $new_id = $old_question_obj->duplicate();
  212. $new_question_obj = Question::read($new_id);
  213. $new_question_obj->addToList($exerciseId);
  214. // This should be moved to the duplicate function
  215. $new_answer_obj = new Answer($clone_question);
  216. $new_answer_obj->read();
  217. $new_answer_obj->duplicate($new_id);
  218. //Reloading tne $objExercise obj
  219. $objExercise->read($objExercise->id);
  220. header('Location: admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id);
  221. exit;
  222. }
  223. // if cancelling answer creation/modification
  224. if ($cancelAnswers) {
  225. // goes back to the question viewing
  226. $editQuestion=$modifyAnswers;
  227. unset($modifyAnswers);
  228. }
  229. // modifies the query string that is used in the link of tool name
  230. if($editQuestion || $modifyQuestion || $newQuestion || $modifyAnswers) {
  231. $nameTools = get_lang('QuestionManagement');
  232. }
  233. if (isset($_SESSION['gradebook'])){
  234. $gradebook= $_SESSION['gradebook'];
  235. }
  236. if (!empty($gradebook) && $gradebook=='view') {
  237. $interbreadcrumb[]= array (
  238. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  239. 'name' => get_lang('ToolGradebook')
  240. );
  241. }
  242. $interbreadcrumb[] = array("url" => "exercice.php","name" => get_lang('Exercices'));
  243. if (isset($_GET['newQuestion']) || isset($_GET['editQuestion']) ) {
  244. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=".$objExercise->id, "name" => $objExercise->name);
  245. } else {
  246. $interbreadcrumb[] = array("url" => "#", "name" => $objExercise->name);
  247. }
  248. // shows a link to go back to the question pool
  249. if (!$exerciseId && $nameTools != get_lang('ExerciseManagement')){
  250. $interbreadcrumb[]=array("url" => "question_pool.php?fromExercise=$fromExercise","name" => get_lang('QuestionPool'));
  251. }
  252. // if the question is duplicated, disable the link of tool name
  253. if ($modifyIn == 'thisExercise') {
  254. if($buttonBack) {
  255. $modifyIn='allExercises';
  256. } else {
  257. $noPHP_SELF=true;
  258. }
  259. }
  260. $htmlHeadXtra[] = '<script>
  261. function multiple_answer_true_false_onchange(variable) {
  262. var result = variable.checked;
  263. var id = variable.id;
  264. var weight_id = "weighting_" + id;
  265. var array_result=new Array(); array_result[1]="1"; array_result[0]= "-0.50"; array_result[-1]= "0";
  266. if (result) {
  267. result = 1;
  268. } else {
  269. result = 0;
  270. }
  271. document.getElementById(weight_id).value = array_result[result];
  272. }
  273. </script>';
  274. $htmlHeadXtra[] = "<script type=\"text/javascript\" src=\"../plugin/hotspot/JavaScriptFlashGateway.js\"></script>
  275. <script src=\"../plugin/hotspot/hotspot.js\" type=\"text/javascript\"></script>
  276. <script language=\"JavaScript\" type=\"text/javascript\">
  277. <!--
  278. // -----------------------------------------------------------------------------
  279. // Globals
  280. // Major version of Flash required
  281. var requiredMajorVersion = 7;
  282. // Minor version of Flash required
  283. var requiredMinorVersion = 0;
  284. // Minor version of Flash required
  285. var requiredRevision = 0;
  286. // the version of javascript supported
  287. var jsVersion = 1.0;
  288. // -----------------------------------------------------------------------------
  289. // -->
  290. </script>
  291. <script language=\"VBScript\" type=\"text/vbscript\">
  292. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  293. Function VBGetSwfVer(i)
  294. on error resume next
  295. Dim swControl, swVersion
  296. swVersion = 0
  297. set swControl = CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" + CStr(i))
  298. if (IsObject(swControl)) then
  299. swVersion = swControl.GetVariable(\"\$version\")
  300. end if
  301. VBGetSwfVer = swVersion
  302. End Function
  303. // -->
  304. </script>
  305. <script language=\"JavaScript1.1\" type=\"text/javascript\">
  306. <!-- // Detect Client Browser type
  307. var isIE = (navigator.appVersion.indexOf(\"MSIE\") != -1) ? true : false;
  308. var isWin = (navigator.appVersion.toLowerCase().indexOf(\"win\") != -1) ? true : false;
  309. var isOpera = (navigator.userAgent.indexOf(\"Opera\") != -1) ? true : false;
  310. jsVersion = 1.1;
  311. // JavaScript helper required to detect Flash Player PlugIn version information
  312. function JSGetSwfVer(i){
  313. // NS/Opera version >= 3 check for Flash plugin in plugin array
  314. if (navigator.plugins != null && navigator.plugins.length > 0) {
  315. if (navigator.plugins[\"Shockwave Flash 2.0\"] || navigator.plugins[\"Shockwave Flash\"]) {
  316. var swVer2 = navigator.plugins[\"Shockwave Flash 2.0\"] ? \" 2.0\" : \"\";
  317. var flashDescription = navigator.plugins[\"Shockwave Flash\" + swVer2].description;
  318. descArray = flashDescription.split(\" \");
  319. tempArrayMajor = descArray[2].split(\".\");
  320. versionMajor = tempArrayMajor[0];
  321. versionMinor = tempArrayMajor[1];
  322. if ( descArray[3] != \"\" ) {
  323. tempArrayMinor = descArray[3].split(\"r\");
  324. } else {
  325. tempArrayMinor = descArray[4].split(\"r\");
  326. }
  327. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  328. flashVer = versionMajor + \".\" + versionMinor + \".\" + versionRevision;
  329. } else {
  330. flashVer = -1;
  331. }
  332. }
  333. // MSN/WebTV 2.6 supports Flash 4
  334. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.6\") != -1) flashVer = 4;
  335. // WebTV 2.5 supports Flash 3
  336. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.5\") != -1) flashVer = 3;
  337. // older WebTV supports Flash 2
  338. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv\") != -1) flashVer = 2;
  339. // Can't detect in all other cases
  340. else {
  341. flashVer = -1;
  342. }
  343. return flashVer;
  344. }
  345. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  346. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  347. {
  348. reqVer = parseFloat(reqMajorVer + \".\" + reqRevision);
  349. // loop backwards through the versions until we find the newest version
  350. for (i=25;i>0;i--) {
  351. if (isIE && isWin && !isOpera) {
  352. versionStr = VBGetSwfVer(i);
  353. } else {
  354. versionStr = JSGetSwfVer(i);
  355. }
  356. if (versionStr == -1 ) {
  357. return false;
  358. } else if (versionStr != 0) {
  359. if(isIE && isWin && !isOpera) {
  360. tempArray = versionStr.split(\" \");
  361. tempString = tempArray[1];
  362. versionArray = tempString .split(\",\");
  363. } else {
  364. versionArray = versionStr.split(\".\");
  365. }
  366. versionMajor = versionArray[0];
  367. versionMinor = versionArray[1];
  368. versionRevision = versionArray[2];
  369. versionString = versionMajor + \".\" + versionRevision; // 7.0r24 == 7.24
  370. versionNum = parseFloat(versionString);
  371. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  372. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  373. return true;
  374. } else {
  375. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  376. }
  377. }
  378. }
  379. }
  380. // -->
  381. </script>";
  382. Display::display_header($nameTools,'Exercise');
  383. if ($objExercise->exercise_was_added_in_lp) {
  384. if ($objExercise->force_edit_exercise_in_lp == true) {
  385. Display::display_warning_message(get_lang('ForceEditingExerciseInLPWarning'));
  386. } else {
  387. //if ($objExercise->edit_exercise_in_lp) {
  388. Display::display_warning_message(get_lang('EditingExerciseCauseProblemsInLP'));
  389. //}
  390. }
  391. }
  392. // If we are in a test
  393. $inATest = isset($exerciseId) && $exerciseId > 0;
  394. if ($inATest) {
  395. echo '<div class="actions">';
  396. if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion']) || isset($_GET['myid']))
  397. echo '<a href="admin.php?exerciseId='.$exerciseId.'">'.Display::return_icon('back.png', get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).'</a>';
  398. if (!isset($_GET['hotspotadmin']) && !isset($_GET['newQuestion']) && !isset($_GET['myid']) && !isset($_GET['editQuestion'])) {
  399. echo '<a href="exercice.php?'.api_get_cidReq().'">'.Display::return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
  400. }
  401. 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>';
  402. echo Display::url(Display::return_icon('test_results.png', get_lang('Results'),'',ICON_SIZE_MEDIUM), 'exercise_report.php?'.api_get_cidReq().'&exerciseId='.$objExercise->id);
  403. if ($objExercise->edit_exercise_in_lp == false) {
  404. echo '<a href="">'.Display::return_icon('settings_na.png', get_lang('ModifyExercise'),'',ICON_SIZE_MEDIUM).'</a>';
  405. } else {
  406. 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>';
  407. }
  408. $maxScoreAllQuestions = 0;
  409. if (!empty($objExercise->questionList)) {
  410. foreach ($objExercise->questionList as $q) {
  411. $question = Question::read($q);
  412. if ($question) {
  413. $maxScoreAllQuestions += $question->selectWeighting();
  414. }
  415. }
  416. }
  417. echo '<span style="float:right">'.sprintf(get_lang('XQuestionsWithTotalScoreY'), $objExercise->selectNbrQuestions(), $maxScoreAllQuestions).'</span>';
  418. echo '</div>';
  419. } else if (isset($_GET['newQuestion'])) {
  420. // we are in create a new question from question pool not in a test
  421. echo '<div class="actions">';
  422. echo '<a href="admin.php?'.api_get_cidreq().'">.'.Display::return_icon('back.png', get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).'</a>';
  423. echo '</div>';
  424. } else {
  425. // If we are in question_poolbut not in an test, go back to question create in pool
  426. echo '<div class="actions">';
  427. echo '<a href="question_pool.php">'.Display::return_icon('back.png', get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).'</a>';
  428. echo '</div>';
  429. }
  430. if (isset($_GET['message'])) {
  431. if (in_array($_GET['message'], array('ExerciseStored', 'ItemUpdated', 'ItemAdded'))) {
  432. Display::display_confirmation_message(get_lang($_GET['message']));
  433. }
  434. }
  435. if ($newQuestion || $editQuestion) {
  436. // statement management
  437. $type = Security::remove_XSS($_REQUEST['answerType']);
  438. echo '<input type="hidden" name="Type" value="'.$type.'" />';
  439. require 'question_admin.inc.php';
  440. }
  441. if (isset($_GET['hotspotadmin'])) {
  442. if (!is_object($objQuestion)) {
  443. $objQuestion = Question :: read($_GET['hotspotadmin']);
  444. }
  445. if (!$objQuestion) {
  446. api_not_allowed();
  447. }
  448. require 'hotspot_admin.inc.php';
  449. }
  450. if (!$newQuestion && !$modifyQuestion && !$editQuestion && !isset($_GET['hotspotadmin'])) {
  451. // question list management
  452. require 'question_list_admin.inc.php';
  453. }
  454. Session::write('objExercise', $objExercise);
  455. Session::write('objQuestion', $objQuestion);
  456. Session::write('objAnswer', $objAnswer);
  457. Display::display_footer();