admin.php 15 KB

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