admin.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php // $Id: admin.php 21662 2009-06-29 14:55:09Z iflorespaz $
  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 dokeos.exercise
  44. * @author Olivier Brouckaert
  45. * @version $Id: admin.php 21662 2009-06-29 14:55:09Z iflorespaz $
  46. */
  47. require_once 'exercise.class.php';
  48. require_once 'question.class.php';
  49. require_once 'answer.class.php';
  50. // name of the language file that needs to be included
  51. $language_file='exercice';
  52. require_once '../inc/global.inc.php';
  53. require_once 'exercise.lib.php';
  54. $this_section=SECTION_COURSES;
  55. $is_allowedToEdit=api_is_allowed_to_edit(null,true);
  56. if (!$is_allowedToEdit) {
  57. api_not_allowed(true);
  58. }
  59. // allows script inclusions
  60. define(ALLOWED_TO_INCLUDE,1);
  61. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  62. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  63. /****************************/
  64. /* stripslashes POST data */
  65. /****************************/
  66. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  67. foreach($_POST as $key=>$val) {
  68. if(is_string($val))
  69. {
  70. $_POST[$key]=stripslashes($val);
  71. }
  72. elseif(is_array($val))
  73. {
  74. foreach($val as $key2=>$val2)
  75. {
  76. $_POST[$key][$key2]=stripslashes($val2);
  77. }
  78. }
  79. $GLOBALS[$key]=$_POST[$key];
  80. }
  81. }
  82. // get vars from GET
  83. if ( empty ( $exerciseId ) ) {
  84. $exerciseId = $_GET['exerciseId'];
  85. }
  86. if ( empty ( $newQuestion ) )
  87. {
  88. $newQuestion = $_GET['newQuestion'];
  89. }
  90. if ( empty ( $modifyAnswers ) )
  91. {
  92. $modifyAnswers = $_GET['modifyAnswers'];
  93. }
  94. if ( empty ( $editQuestion ) )
  95. {
  96. $editQuestion = $_GET['editQuestion'];
  97. }
  98. if ( empty ( $modifyQuestion ) )
  99. {
  100. $modifyQuestion = $_GET['modifyQuestion'];
  101. }
  102. if ( empty ( $deleteQuestion ) )
  103. {
  104. $deleteQuestion = $_GET['deleteQuestion'];
  105. }
  106. if ( empty ( $questionId ) )
  107. {
  108. $questionId = $_SESSION['questionId'];
  109. }
  110. if ( empty ( $modifyExercise ) )
  111. {
  112. $modifyExercise = $_GET['modifyExercise'];
  113. }
  114. // get from session
  115. $objExercise = $_SESSION['objExercise'];
  116. $objQuestion = $_SESSION['objQuestion'];
  117. $objAnswer = $_SESSION['objAnswer'];
  118. // document path
  119. $documentPath=api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  120. // picture path
  121. $picturePath=$documentPath.'/images';
  122. // audio path
  123. $audioPath=$documentPath.'/audio';
  124. // the 5 types of answers
  125. $aType=array(get_lang('UniqueSelect'),get_lang('MultipleSelect'),get_lang('FillBlanks'),get_lang('Matching'),get_lang('freeAnswer'));
  126. // tables used in the exercise tool
  127. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  128. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  129. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  130. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  131. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  132. if($_GET['action'] == 'exportqti2' && !empty($_GET['questionId'])) {
  133. require_once('export/qti2/qti2_export.php');
  134. $export = export_question((int)$_GET['questionId'],true);
  135. $qid = (int)$_GET['questionId'];
  136. require_once(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  137. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  138. $temp_dir_short = uniqid();
  139. $temp_zip_dir = $archive_path."/".$temp_dir_short;
  140. if(!is_dir($temp_zip_dir)) mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  141. $temp_zip_file = $temp_zip_dir."/".md5(time()).".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. // intializes the Exercise object
  155. if(!is_object($objExercise))
  156. {
  157. // construction of the Exercise object
  158. $objExercise=new Exercise();
  159. // creation of a new exercise if wrong or not specified exercise ID
  160. if($exerciseId)
  161. {
  162. $objExercise->read($exerciseId);
  163. }
  164. // saves the object into the session
  165. api_session_register('objExercise');
  166. }
  167. // doesn't select the exercise ID if we come from the question pool
  168. if(!$fromExercise)
  169. {
  170. // gets the right exercise ID, and if 0 creates a new exercise
  171. if(!$exerciseId=$objExercise->selectId())
  172. {
  173. $modifyExercise='yes';
  174. }
  175. }
  176. $nbrQuestions=$objExercise->selectNbrQuestions();
  177. // intializes the Question object
  178. if($editQuestion || $newQuestion || $modifyQuestion || $modifyAnswers)
  179. {
  180. if($editQuestion || $newQuestion)
  181. {
  182. // reads question data
  183. if($editQuestion)
  184. {
  185. // question not found
  186. if(!$objQuestion = Question::read($editQuestion))
  187. {
  188. die(get_lang('QuestionNotFound'));
  189. }
  190. // saves the object into the session
  191. api_session_register('objQuestion');
  192. }
  193. }
  194. // checks if the object exists
  195. if(is_object($objQuestion))
  196. {
  197. // gets the question ID
  198. $questionId=$objQuestion->selectId();
  199. }
  200. }
  201. // if cancelling an exercise
  202. if($cancelExercise)
  203. {
  204. // existing exercise
  205. if($exerciseId)
  206. {
  207. unset($modifyExercise);
  208. }
  209. // new exercise
  210. else
  211. {
  212. // goes back to the exercise list
  213. header('Location: exercice.php');
  214. exit();
  215. }
  216. }
  217. // if cancelling question creation/modification
  218. if($cancelQuestion)
  219. {
  220. // if we are creating a new question from the question pool
  221. if(!$exerciseId && !$questionId)
  222. {
  223. // goes back to the question pool
  224. header('Location: question_pool.php');
  225. exit();
  226. }
  227. else
  228. {
  229. // goes back to the question viewing
  230. $editQuestion=$modifyQuestion;
  231. unset($newQuestion,$modifyQuestion);
  232. }
  233. }
  234. // if cancelling answer creation/modification
  235. if($cancelAnswers)
  236. {
  237. // goes back to the question viewing
  238. $editQuestion=$modifyAnswers;
  239. unset($modifyAnswers);
  240. }
  241. // modifies the query string that is used in the link of tool name
  242. if($editQuestion || $modifyQuestion || $newQuestion || $modifyAnswers) {
  243. $nameTools=get_lang('QuestionManagement');
  244. }
  245. if (isset($_SESSION['gradebook'])){
  246. $gradebook= $_SESSION['gradebook'];
  247. }
  248. if (!empty($gradebook) && $gradebook=='view') {
  249. $interbreadcrumb[]= array (
  250. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  251. 'name' => get_lang('ToolGradebook')
  252. );
  253. }
  254. $interbreadcrumb[]=array("url" => "exercice.php","name" => get_lang('Exercices'));
  255. $interbreadcrumb[]=array("url" => "admin.php?exerciseId=".$objExercise->id,"name" => $objExercise->exercise);
  256. // shows a link to go back to the question pool
  257. if(!$exerciseId && $nameTools != get_lang('ExerciseManagement')){
  258. $interbreadcrumb[]=array("url" => "question_pool.php?fromExercise=$fromExercise","name" => get_lang('QuestionPool'));
  259. }
  260. // if the question is duplicated, disable the link of tool name
  261. if($modifyIn == 'thisExercise') {
  262. if($buttonBack) {
  263. $modifyIn='allExercises';
  264. } else {
  265. $noPHP_SELF=true;
  266. }
  267. }
  268. $htmlHeadXtra[] = "<script type=\"text/javascript\" src=\"../plugin/hotspot/JavaScriptFlashGateway.js\"></script>
  269. <script src=\"../plugin/hotspot/hotspot.js\" type=\"text/javascript\"></script>
  270. <script language=\"JavaScript\" type=\"text/javascript\">
  271. <!--
  272. // -----------------------------------------------------------------------------
  273. // Globals
  274. // Major version of Flash required
  275. var requiredMajorVersion = 7;
  276. // Minor version of Flash required
  277. var requiredMinorVersion = 0;
  278. // Minor version of Flash required
  279. var requiredRevision = 0;
  280. // the version of javascript supported
  281. var jsVersion = 1.0;
  282. // -----------------------------------------------------------------------------
  283. // -->
  284. </script>
  285. <script language=\"VBScript\" type=\"text/vbscript\">
  286. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  287. Function VBGetSwfVer(i)
  288. on error resume next
  289. Dim swControl, swVersion
  290. swVersion = 0
  291. set swControl = CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" + CStr(i))
  292. if (IsObject(swControl)) then
  293. swVersion = swControl.GetVariable(\"\$version\")
  294. end if
  295. VBGetSwfVer = swVersion
  296. End Function
  297. // -->
  298. </script>
  299. <script language=\"JavaScript1.1\" type=\"text/javascript\">
  300. <!-- // Detect Client Browser type
  301. var isIE = (navigator.appVersion.indexOf(\"MSIE\") != -1) ? true : false;
  302. var isWin = (navigator.appVersion.toLowerCase().indexOf(\"win\") != -1) ? true : false;
  303. var isOpera = (navigator.userAgent.indexOf(\"Opera\") != -1) ? true : false;
  304. jsVersion = 1.1;
  305. // JavaScript helper required to detect Flash Player PlugIn version information
  306. function JSGetSwfVer(i){
  307. // NS/Opera version >= 3 check for Flash plugin in plugin array
  308. if (navigator.plugins != null && navigator.plugins.length > 0) {
  309. if (navigator.plugins[\"Shockwave Flash 2.0\"] || navigator.plugins[\"Shockwave Flash\"]) {
  310. var swVer2 = navigator.plugins[\"Shockwave Flash 2.0\"] ? \" 2.0\" : \"\";
  311. var flashDescription = navigator.plugins[\"Shockwave Flash\" + swVer2].description;
  312. descArray = flashDescription.split(\" \");
  313. tempArrayMajor = descArray[2].split(\".\");
  314. versionMajor = tempArrayMajor[0];
  315. versionMinor = tempArrayMajor[1];
  316. if ( descArray[3] != \"\" ) {
  317. tempArrayMinor = descArray[3].split(\"r\");
  318. } else {
  319. tempArrayMinor = descArray[4].split(\"r\");
  320. }
  321. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  322. flashVer = versionMajor + \".\" + versionMinor + \".\" + versionRevision;
  323. } else {
  324. flashVer = -1;
  325. }
  326. }
  327. // MSN/WebTV 2.6 supports Flash 4
  328. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.6\") != -1) flashVer = 4;
  329. // WebTV 2.5 supports Flash 3
  330. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.5\") != -1) flashVer = 3;
  331. // older WebTV supports Flash 2
  332. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv\") != -1) flashVer = 2;
  333. // Can't detect in all other cases
  334. else {
  335. flashVer = -1;
  336. }
  337. return flashVer;
  338. }
  339. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  340. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  341. {
  342. reqVer = parseFloat(reqMajorVer + \".\" + reqRevision);
  343. // loop backwards through the versions until we find the newest version
  344. for (i=25;i>0;i--) {
  345. if (isIE && isWin && !isOpera) {
  346. versionStr = VBGetSwfVer(i);
  347. } else {
  348. versionStr = JSGetSwfVer(i);
  349. }
  350. if (versionStr == -1 ) {
  351. return false;
  352. } else if (versionStr != 0) {
  353. if(isIE && isWin && !isOpera) {
  354. tempArray = versionStr.split(\" \");
  355. tempString = tempArray[1];
  356. versionArray = tempString .split(\",\");
  357. } else {
  358. versionArray = versionStr.split(\".\");
  359. }
  360. versionMajor = versionArray[0];
  361. versionMinor = versionArray[1];
  362. versionRevision = versionArray[2];
  363. versionString = versionMajor + \".\" + versionRevision; // 7.0r24 == 7.24
  364. versionNum = parseFloat(versionString);
  365. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  366. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  367. return true;
  368. } else {
  369. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  370. }
  371. }
  372. }
  373. }
  374. // -->
  375. </script>";
  376. Display::display_header($nameTools,'Exercise');
  377. $show_quiz_edition = true;
  378. if (isset($exerciseId) && !empty($exerciseId)) {
  379. $TBL_LP_ITEM = Database::get_course_table(TABLE_LP_ITEM);
  380. $sql="SELECT max_score FROM $TBL_LP_ITEM
  381. WHERE item_type = '".TOOL_QUIZ."' AND path ='".Database::escape_string($exerciseId)."'";
  382. $result = Database::query($sql);
  383. if (Database::num_rows($result) > 0) {
  384. Display::display_warning_message(get_lang('EditingExerciseCauseProblemsInLP'));
  385. $show_quiz_edition = false;
  386. }
  387. }
  388. echo '<div class="actions">';
  389. echo Display::return_icon('preview.gif', get_lang('Preview')).'<a href="exercice_submit.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'">'.get_lang('Preview').'</a>';
  390. if ($show_quiz_edition) {
  391. echo Display::return_icon('edit.gif', get_lang('ModifyExercise')).'<a href="exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.get_lang('ModifyExercise').'</a>';
  392. } else {
  393. echo Display::return_icon('edit_na.gif', get_lang('ModifyExercise')).'<a href="#">'.get_lang('ModifyExercise').'</a>';
  394. }
  395. if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion']) || isset($_GET['myid']))
  396. echo Display::return_icon('message_reply_forum.png', get_lang('GoBackToQuestionList')).' '.'<a href="admin.php?">'.get_lang('GoBackToQuestionList').'</a><br/>';
  397. echo '</div>';
  398. if(isset($_GET['message']))
  399. {
  400. if (in_array($_GET['message'], array('ExerciseStored')))
  401. {
  402. Display::display_confirmation_message(get_lang($_GET['message']));
  403. }
  404. }
  405. /*
  406. $description = $objExercise->selectDescription();
  407. echo '<div class="sectiontitle">'.$objExercise->selectTitle().'</div>';
  408. if(!empty($description))
  409. {
  410. echo '<div class="sectioncomment">'.stripslashes($description).'</div>';
  411. }
  412. */
  413. if($newQuestion || $editQuestion)
  414. {
  415. // statement management
  416. $type = $_REQUEST['answerType'];
  417. ?><input type="hidden" name="Type" value="<?php echo $type; ?>" />
  418. <?php
  419. include('question_admin.inc.php');
  420. }
  421. if(isset($_GET['hotspotadmin']))
  422. {
  423. include('hotspot_admin.inc.php');
  424. }
  425. if(!$newQuestion && !$modifyQuestion && !$editQuestion && !isset($_GET['hotspotadmin']))
  426. {
  427. include_once(api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  428. $form = new FormValidator('exercise_admin', 'post', api_get_self().'?exerciseId='.$_GET['exerciseId']);
  429. $form -> addElement ('hidden','edit','true');
  430. //$objExercise -> createForm ($form,'simple');
  431. if($form -> validate()) {
  432. $objExercise -> processCreation($form,'simple');
  433. if($form -> getSubmitValue('edit') == 'true')
  434. Display::display_confirmation_message(get_lang('ExerciseEdited'));
  435. }
  436. if(api_get_setting('search_enabled')=='true' && !extension_loaded('xapian')) {
  437. Display::display_error_message(get_lang('SearchXapianModuleNotInstaled'));
  438. }
  439. $form -> display ();
  440. echo '<br />';
  441. // question list management
  442. include('question_list_admin.inc.php');
  443. }
  444. api_session_register('objExercise');
  445. api_session_register('objQuestion');
  446. api_session_register('objAnswer');
  447. Display::display_footer();
  448. ?>