admin.php 17 KB

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