admin.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. api_not_allowed();
  175. }
  176. // saves the object into the session
  177. api_session_register('objQuestion');
  178. }
  179. }
  180. // checks if the object exists
  181. if(is_object($objQuestion)) {
  182. // gets the question ID
  183. $questionId = $objQuestion->selectId();
  184. }
  185. }
  186. // if cancelling an exercise
  187. if ($cancelExercise) {
  188. // existing exercise
  189. if($exerciseId) {
  190. unset($modifyExercise);
  191. } else {
  192. // new exercise
  193. // goes back to the exercise list
  194. header('Location: exercice.php');
  195. exit();
  196. }
  197. }
  198. // if cancelling question creation/modification
  199. if ($cancelQuestion) {
  200. // if we are creating a new question from the question pool
  201. if(!$exerciseId && !$questionId) {
  202. // goes back to the question pool
  203. header('Location: question_pool.php');
  204. exit();
  205. } else {
  206. // goes back to the question viewing
  207. $editQuestion=$modifyQuestion;
  208. unset($newQuestion,$modifyQuestion);
  209. }
  210. }
  211. if (isset($clone_question) && !empty($objExercise->id)) {
  212. $old_question_obj = Question::read($clone_question);
  213. $old_question_obj->question = $old_question_obj->question.' - '.get_lang('Copy');
  214. $new_id = $old_question_obj->duplicate();
  215. $new_question_obj = Question::read($new_id);
  216. $new_question_obj->addToList($exerciseId);
  217. // This should be moved to the duplicate function
  218. $new_answer_obj = new Answer($clone_question);
  219. $new_answer_obj->read();
  220. $new_answer_obj->duplicate($new_id);
  221. header('Location: admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id);
  222. exit;
  223. }
  224. // if cancelling answer creation/modification
  225. if($cancelAnswers) {
  226. // goes back to the question viewing
  227. $editQuestion=$modifyAnswers;
  228. unset($modifyAnswers);
  229. }
  230. // modifies the query string that is used in the link of tool name
  231. if($editQuestion || $modifyQuestion || $newQuestion || $modifyAnswers) {
  232. $nameTools = get_lang('QuestionManagement');
  233. }
  234. if (isset($_SESSION['gradebook'])){
  235. $gradebook= $_SESSION['gradebook'];
  236. }
  237. if (!empty($gradebook) && $gradebook=='view') {
  238. $interbreadcrumb[]= array (
  239. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  240. 'name' => get_lang('ToolGradebook')
  241. );
  242. }
  243. $interbreadcrumb[] = array("url" => "exercice.php","name" => get_lang('Exercices'));
  244. if (isset($_GET['newQuestion']) || isset($_GET['editQuestion']) ) {
  245. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=".$objExercise->id, "name" => $objExercise->name);
  246. } else {
  247. $interbreadcrumb[] = array("url" => "#", "name" => $objExercise->name);
  248. }
  249. // shows a link to go back to the question pool
  250. if(!$exerciseId && $nameTools != get_lang('ExerciseManagement')){
  251. $interbreadcrumb[]=array("url" => "question_pool.php?fromExercise=$fromExercise","name" => get_lang('QuestionPool'));
  252. }
  253. // if the question is duplicated, disable the link of tool name
  254. if($modifyIn == 'thisExercise') {
  255. if($buttonBack) {
  256. $modifyIn='allExercises';
  257. } else {
  258. $noPHP_SELF=true;
  259. }
  260. }
  261. $htmlHeadXtra[] = api_get_jquery_ui_js();
  262. $htmlHeadXtra[] = '<script type="text/javascript">
  263. function multiple_answer_true_false_onchange(variable) {
  264. var result = variable.checked;
  265. var id = variable.id;
  266. var weight_id = "weighting_" + id;
  267. var array_result=new Array(); array_result[1]="1"; array_result[0]= "-0.50"; array_result[-1]= "0";
  268. if (result) {
  269. result = 1;
  270. } else {
  271. result = 0;
  272. }
  273. document.getElementById(weight_id).value = array_result[result];
  274. }
  275. </script>';
  276. $htmlHeadXtra[] = "<script type=\"text/javascript\" src=\"../plugin/hotspot/JavaScriptFlashGateway.js\"></script>
  277. <script src=\"../plugin/hotspot/hotspot.js\" type=\"text/javascript\"></script>
  278. <script language=\"JavaScript\" type=\"text/javascript\">
  279. <!--
  280. // -----------------------------------------------------------------------------
  281. // Globals
  282. // Major version of Flash required
  283. var requiredMajorVersion = 7;
  284. // Minor version of Flash required
  285. var requiredMinorVersion = 0;
  286. // Minor version of Flash required
  287. var requiredRevision = 0;
  288. // the version of javascript supported
  289. var jsVersion = 1.0;
  290. // -----------------------------------------------------------------------------
  291. // -->
  292. </script>
  293. <script language=\"VBScript\" type=\"text/vbscript\">
  294. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  295. Function VBGetSwfVer(i)
  296. on error resume next
  297. Dim swControl, swVersion
  298. swVersion = 0
  299. set swControl = CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" + CStr(i))
  300. if (IsObject(swControl)) then
  301. swVersion = swControl.GetVariable(\"\$version\")
  302. end if
  303. VBGetSwfVer = swVersion
  304. End Function
  305. // -->
  306. </script>
  307. <script language=\"JavaScript1.1\" type=\"text/javascript\">
  308. <!-- // Detect Client Browser type
  309. var isIE = (navigator.appVersion.indexOf(\"MSIE\") != -1) ? true : false;
  310. var isWin = (navigator.appVersion.toLowerCase().indexOf(\"win\") != -1) ? true : false;
  311. var isOpera = (navigator.userAgent.indexOf(\"Opera\") != -1) ? true : false;
  312. jsVersion = 1.1;
  313. // JavaScript helper required to detect Flash Player PlugIn version information
  314. function JSGetSwfVer(i){
  315. // NS/Opera version >= 3 check for Flash plugin in plugin array
  316. if (navigator.plugins != null && navigator.plugins.length > 0) {
  317. if (navigator.plugins[\"Shockwave Flash 2.0\"] || navigator.plugins[\"Shockwave Flash\"]) {
  318. var swVer2 = navigator.plugins[\"Shockwave Flash 2.0\"] ? \" 2.0\" : \"\";
  319. var flashDescription = navigator.plugins[\"Shockwave Flash\" + swVer2].description;
  320. descArray = flashDescription.split(\" \");
  321. tempArrayMajor = descArray[2].split(\".\");
  322. versionMajor = tempArrayMajor[0];
  323. versionMinor = tempArrayMajor[1];
  324. if ( descArray[3] != \"\" ) {
  325. tempArrayMinor = descArray[3].split(\"r\");
  326. } else {
  327. tempArrayMinor = descArray[4].split(\"r\");
  328. }
  329. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  330. flashVer = versionMajor + \".\" + versionMinor + \".\" + versionRevision;
  331. } else {
  332. flashVer = -1;
  333. }
  334. }
  335. // MSN/WebTV 2.6 supports Flash 4
  336. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.6\") != -1) flashVer = 4;
  337. // WebTV 2.5 supports Flash 3
  338. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.5\") != -1) flashVer = 3;
  339. // older WebTV supports Flash 2
  340. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv\") != -1) flashVer = 2;
  341. // Can't detect in all other cases
  342. else {
  343. flashVer = -1;
  344. }
  345. return flashVer;
  346. }
  347. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  348. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  349. {
  350. reqVer = parseFloat(reqMajorVer + \".\" + reqRevision);
  351. // loop backwards through the versions until we find the newest version
  352. for (i=25;i>0;i--) {
  353. if (isIE && isWin && !isOpera) {
  354. versionStr = VBGetSwfVer(i);
  355. } else {
  356. versionStr = JSGetSwfVer(i);
  357. }
  358. if (versionStr == -1 ) {
  359. return false;
  360. } else if (versionStr != 0) {
  361. if(isIE && isWin && !isOpera) {
  362. tempArray = versionStr.split(\" \");
  363. tempString = tempArray[1];
  364. versionArray = tempString .split(\",\");
  365. } else {
  366. versionArray = versionStr.split(\".\");
  367. }
  368. versionMajor = versionArray[0];
  369. versionMinor = versionArray[1];
  370. versionRevision = versionArray[2];
  371. versionString = versionMajor + \".\" + versionRevision; // 7.0r24 == 7.24
  372. versionNum = parseFloat(versionString);
  373. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  374. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  375. return true;
  376. } else {
  377. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  378. }
  379. }
  380. }
  381. }
  382. // -->
  383. </script>";
  384. Display::display_header($nameTools,'Exercise');
  385. $show_quiz_edition = true;
  386. if (isset($exerciseId) && !empty($exerciseId)) {
  387. $TBL_LP_ITEM = Database::get_course_table(TABLE_LP_ITEM);
  388. $sql="SELECT max_score FROM $TBL_LP_ITEM
  389. WHERE item_type = '".TOOL_QUIZ."' AND path ='".Database::escape_string($exerciseId)."'";
  390. $result = Database::query($sql);
  391. if (Database::num_rows($result) > 0) {
  392. Display::display_warning_message(get_lang('EditingExerciseCauseProblemsInLP'));
  393. $show_quiz_edition = false;
  394. }
  395. }
  396. echo '<div class="actions">';
  397. if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion']) || isset($_GET['myid']))
  398. echo '<a href="admin.php?exerciseId='.$exerciseId.'">'.Display::return_icon('back.png', get_lang('GoBackToQuestionList'),'','32').'</a>';
  399. if (!isset($_GET['hotspotadmin']) && !isset($_GET['newQuestion']) && !isset($_GET['myid']) && !isset($_GET['editQuestion'])) {
  400. echo '<a href="exercice.php?'.api_get_cidReq().'">'.Display::return_icon('back.png', get_lang('BackToExercisesList'),'','32').'</a>';
  401. }
  402. echo '<a href="overview.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'&preview=1">'.Display::return_icon('preview_view.png', get_lang('Preview'),'','32').'</a>';
  403. echo Display::url(Display::return_icon('test_results.png', get_lang('Results'),'','32'), 'exercice.php?'.api_get_cidReq().'&show=result&exerciseId='.$objExercise->id);
  404. if ($show_quiz_edition) {
  405. echo '<a href="exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.Display::return_icon('settings.png', get_lang('ModifyExercise'),'','32').'</a>';
  406. } else {
  407. echo '<a href="">'.Display::return_icon('settings_na.png', get_lang('ModifyExercise'),'','32').'</a>';
  408. }
  409. $maxScoreAllQuestions = 0;
  410. foreach ($objExercise->questionList as $q) {
  411. $oQ = Question::read($q);
  412. $maxScoreAllQuestions += $oQ->selectWeighting();
  413. }
  414. echo '<span style="float:right">'.sprintf(get_lang('XQuestionsWithTotalScoreY'),$objExercise->selectNbrQuestions(),$maxScoreAllQuestions).'</span>';
  415. echo '</div>';
  416. if (isset($_GET['message'])) {
  417. if (in_array($_GET['message'], array('ExerciseStored', 'ItemUpdated', 'ItemAdded'))) {
  418. Display::display_confirmation_message(get_lang($_GET['message']));
  419. }
  420. }
  421. if ($newQuestion || $editQuestion) {
  422. // statement management
  423. $type = Security::remove_XSS($_REQUEST['answerType']);
  424. echo '<input type="hidden" name="Type" value="'.$type.'" />';
  425. require 'question_admin.inc.php';
  426. }
  427. if (isset($_GET['hotspotadmin'])) {
  428. if (!is_object($objQuestion)) {
  429. $objQuestion = Question :: read($_GET['hotspotadmin']);
  430. }
  431. if (!$objQuestion) {
  432. api_not_allowed();
  433. }
  434. require 'hotspot_admin.inc.php';}
  435. if (!$newQuestion && !$modifyQuestion && !$editQuestion && !isset($_GET['hotspotadmin'])) {
  436. // question list management
  437. require 'question_list_admin.inc.php';
  438. }
  439. api_session_register('objExercise');
  440. api_session_register('objQuestion');
  441. api_session_register('objAnswer');
  442. Display::display_footer();