admin.php 16 KB

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