admin.php 18 KB

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