exercise_submit.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exercise submission
  5. * This script allows to run an exercise. According to the exercise type, questions
  6. * can be on an unique page, or one per page with a Next button.
  7. *
  8. * One exercise may contain different types of answers (unique or multiple selection,
  9. * matching, fill in blanks, free answer, hot-spot).
  10. *
  11. * Questions are selected randomly or not.
  12. *
  13. * When the user has answered all questions and clicks on the button "Ok",
  14. * it goes to exercise_result.php
  15. *
  16. * @package chamilo.exercise
  17. * @author Olivier Brouckaert
  18. * @author Julio Montoya <gugli100@gmail.com>
  19. * Fill in blank option added (2008)
  20. * Cleaning exercises (2010),
  21. * Adding hotspot delineation support (2011)
  22. * Adding reminder + ajax support (2011)
  23. * Adding adding medias, local and global categories (2013)
  24. * Modified by hubert.borderiou (2011-10-21 question category)
  25. */
  26. /**
  27. * Code
  28. */
  29. use \ChamiloSession as Session;
  30. require_once 'exercise.class.php';
  31. require_once 'question.class.php';
  32. require_once 'answer.class.php';
  33. // name of the language file that needs to be included
  34. $language_file = 'exercice';
  35. require_once '../inc/global.inc.php';
  36. $current_course_tool = TOOL_QUIZ;
  37. $nameTools = get_lang('Quiz');
  38. $this_section = SECTION_COURSES;
  39. if ($debug) { error_log('--- Enter to the exercise_submit.php ---- '); error_log('0. POST variables : '.print_r($_POST,1)); }
  40. // Notice for unauthorized people.
  41. api_protect_course_script(true);
  42. $is_allowedToEdit = api_is_allowed_to_edit(null,true);
  43. if (api_get_setting('show_glossary_in_extra_tools') == 'true') {
  44. $htmlHeadXtra[] = api_get_js('glossary.js'); //Glossary
  45. $htmlHeadXtra[] = api_get_js('jquery. highlight.js'); //highlight
  46. }
  47. // Matching question
  48. $htmlHeadXtra[] = api_get_js('jquery.jsPlumb.all.js');
  49. // This library is necessary for the time control feature
  50. $htmlHeadXtra[]= api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/stylesheet/jquery.epiclock.css');
  51. $htmlHeadXtra[]= api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/renderers/minute/epiclock.minute.css');
  52. $htmlHeadXtra[]= api_get_js('epiclock/javascript/jquery.dateformat.min.js');
  53. $htmlHeadXtra[]= api_get_js('epiclock/javascript/jquery.epiclock.min.js');
  54. $htmlHeadXtra[]= api_get_js('epiclock/renderers/minute/epiclock.minute.js');
  55. $htmlHeadXtra[]= api_get_js('d3/jquery.xcolor.js');
  56. $htmlHeadXtra[]= '<script>
  57. var recycle_icon = "<a href=\'#\' class=\'ui-icon ui-icon-refresh\'>Recycle image</a>";
  58. var trash_icon = "<a href=\'#\' class=\'ui-icon ui-icon-trash\'>Delete image</a>";
  59. function deleteItem($item, $insertHere) {
  60. if ($insertHere.find(".question_draggable").length > 0) {
  61. return false;
  62. }
  63. $item.fadeOut(function() {
  64. $list = $( "<div class=\'gallery ui-helper-reset\'/>" ).appendTo($insertHere);
  65. $item.find( "a.ui-icon-trash" ).remove();
  66. var droppedId = $item.attr("id");
  67. var dropedOnId = $insertHere.attr("id");
  68. var originSelectId = "window_"+droppedId+"_select";
  69. value = dropedOnId.split("_")[2];
  70. $("#"+originSelectId +" option").filter(function() {
  71. return $(this).val() == value;
  72. }).attr("selected", true);
  73. $item.append( recycle_icon ).appendTo( $list ).fadeIn(function() {
  74. //$item.animate({ width: "48px" }).find( "img" ).animate({ height: "36px" });
  75. });
  76. });
  77. }
  78. // Draggable js script in order to use jsplumb
  79. var oldPositionArray;
  80. $(function() {
  81. var $gallery = $( ".drag_question" );
  82. var $trash = $( ".droppable" );
  83. // let the questions items be draggable
  84. $("li", $gallery).draggable({
  85. cancel: "a.ui-icon", // clicking an icon wont initiate dragging
  86. revert: "invalid", // when not dropped, the item will revert back to its initial position
  87. containment: "document",
  88. helper: "clone",
  89. cursor: "move"
  90. });
  91. // let the "droppable" be droppable, accepting the questions items
  92. $trash.droppable({
  93. accept: ".drag_question > li",
  94. hoverClass: "ui-state-active",
  95. drop: function(event, ui) {
  96. deleteItem(ui.draggable, $(this));
  97. }
  98. });
  99. // let the question handler be droppable as well, accepting items from the trash
  100. $gallery.droppable({
  101. // accept: ".droppable",
  102. // activeClass: "custom-state-active",
  103. hoverClass: "ui-state-active",
  104. drop: function( event, ui ) {
  105. recycleItem( ui.draggable, $(this));
  106. }
  107. });
  108. function recycleItem( $item, droppedOn) {
  109. //console.log("recycleItem");
  110. $item.fadeOut(function() {
  111. $item
  112. .find( "a.ui-icon-refresh" )
  113. .remove()
  114. .end()
  115. .css( "width", "96px")
  116. //.append( trash_icon )
  117. .find( "img" )
  118. .css( "height", "72px" )
  119. .end()
  120. .appendTo( $gallery )
  121. .fadeIn();
  122. });
  123. var droppedId = $item.attr("id");
  124. var originSelectId = "window_"+droppedId+"_select";
  125. $("#"+originSelectId+" option:first").attr("selected","selected");
  126. }
  127. $( "ul.drag_question > li" ).click(function( event ) {
  128. var $item = $( this ),
  129. $target = $( event.target );
  130. if ( $target.is( "a.ui-icon-trash" ) ) {
  131. deleteItem( $item );
  132. } else if ( $target.is( "a.ui-icon-zoomin" ) ) {
  133. } else if ( $target.is( "a.ui-icon-refresh" ) ) {
  134. recycleItem( $item );
  135. }
  136. return false;
  137. });
  138. });
  139. // Matching js script in order to use jsplumb
  140. var colorDestination = "#316b31";
  141. var curvinessValue = 0;
  142. var connectorType = "Straight";
  143. ;(function() {
  144. window.jsPlumbDemo = {
  145. init : function(questionId) {
  146. var windowQuestion = null;
  147. if (questionId) {
  148. windowQuestion = ".window"+ questionId+"_question";
  149. }
  150. var countConnections = $(windowQuestion).size();
  151. if (countConnections && countConnections > 0) {
  152. var colorArray = $.xcolor.analogous("#da0", countConnections);
  153. var colorArrayDestination = $.xcolor.analogous("#51a351", countConnections);
  154. } else {
  155. var colorArray = $.xcolor.analogous("#da0", 10);
  156. var colorArrayDestination = $.xcolor.analogous("#51a351", 10);
  157. }
  158. jsPlumb.importDefaults({
  159. DragOptions : { cursor: "pointer", zIndex:2000 },
  160. PaintStyle : { strokeStyle:"#000" },
  161. EndpointStyle : { strokeStyle:"#316b31" },
  162. Endpoint : "Rectangle",
  163. Anchors : ["TopCenter", "TopCenter"]
  164. });
  165. var exampleDropOptions = {
  166. tolerance: "touch",
  167. hoverClass: "dropHover",
  168. activeClass: "dragActive"
  169. };
  170. var destinationEndPoint = {
  171. endpoint:["Dot", { radius: 15 }],
  172. paintStyle:{ fillStyle:colorDestination },
  173. isSource:false,
  174. connectorStyle:{ strokeStyle:colorDestination, lineWidth:8 },
  175. connector: [connectorType, { curviness: curvinessValue } ],
  176. maxConnections: 1000,
  177. isTarget:true,
  178. dropOptions : exampleDropOptions,
  179. beforeDrop:function(params) {
  180. var connections = jsPlumb.getConnections({source: params.sourceId});
  181. jsPlumb.select({source:params.sourceId}).each(function(connection) {
  182. jsPlumb.detach(connection);
  183. });
  184. var selectId = params.sourceId + "_select";
  185. var value = params.targetId.split("_")[2];
  186. console.log(selectId);
  187. console.log(value);
  188. $("#" +selectId +" option").filter(function() {
  189. return $(this).val() == value;
  190. }).attr("selected", true);
  191. return true;
  192. }
  193. };
  194. var count = 0;
  195. var sourceDestinationArray = Array;
  196. $(windowQuestion).each(function(index) {
  197. var windowId = $(this).attr("id");
  198. var scope = windowId + "scope";
  199. var destinationColor = colorArray[count].getHex();
  200. var sourceEndPoint = {
  201. endpoint:["Dot", { radius:15 }],
  202. paintStyle:{ fillStyle: destinationColor },
  203. isSource:true,
  204. connectorStyle:{ strokeStyle:"#8a8888" , lineWidth:8 },
  205. connector: [connectorType, { curviness: curvinessValue } ],
  206. maxConnections: 1,
  207. isTarget:false,
  208. dropOptions : exampleDropOptions,
  209. scope: scope
  210. };
  211. sourceDestinationArray[count+1] = sourceEndPoint;
  212. count++;
  213. jsPlumb.addEndpoint(windowId, { anchor:[ "RightMiddle","RightMiddle","RightMiddle","RightMiddle" ] }, sourceEndPoint);
  214. var destinationCount = 0;
  215. $(windowQuestion).each(function( index ) {
  216. var windowDestinationId = $(this).attr("id");
  217. destinationEndPoint.scope = scope;
  218. destinationEndPoint.paintStyle.fillStyle = colorArrayDestination[destinationCount].getHex();
  219. destinationCount++;
  220. jsPlumb.addEndpoint(windowDestinationId+"_answer", { anchor:[ "LeftMiddle","LeftMiddle","LeftMiddle","LeftMiddle" ] }, destinationEndPoint);
  221. });
  222. });
  223. //var divsWithWindowClass = jsPlumb.CurrentLibrary.getSelector("#"+questionId+" .window");
  224. //jsPlumb.draggable(divsWithWindowClass);
  225. jsPlumbDemo.attachBehaviour();
  226. }
  227. }
  228. })();
  229. ;(function() {
  230. var _initialised = false;
  231. jsPlumbDemo.attachBehaviour = function() {
  232. if (!_initialised) {
  233. _initialised = true;
  234. }
  235. };
  236. })();
  237. jsPlumb.ready(function() {
  238. if ($(".drag_question").length > 0) {
  239. jsPlumbDemo.init();
  240. $(document).scroll(function() {
  241. jsPlumb.repaintEverything();
  242. });
  243. $(window).resize(function() {
  244. jsPlumb.repaintEverything();
  245. });
  246. }
  247. });
  248. $(function(){
  249. $(".highlight_image").on("click", function() {
  250. $(this).parent().find(".highlight_image").each(function(index){
  251. $(this).removeClass("highlight_image_selected");
  252. $(this).addClass("highlight_image_default");
  253. $(this).find("label").find("input").attr("checked", false);
  254. });
  255. $(this).removeClass("highlight_image_default");
  256. $(this).addClass("highlight_image_selected");
  257. $(this).find("label").find("input").attr("checked", "checked");
  258. });
  259. });
  260. </script>';
  261. // General parameters passed via POST/GET
  262. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : 0;
  263. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : 0;
  264. $learnpath_item_view_id = isset($_REQUEST['learnpath_item_view_id']) ? intval($_REQUEST['learnpath_item_view_id']) : 0;
  265. $origin = isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : '';
  266. $reminder = isset($_REQUEST['reminder']) ? intval($_REQUEST['reminder']) : 0;
  267. $remind_question_id = isset($_REQUEST['remind_question_id']) ? intval($_REQUEST['remind_question_id']) : 0;
  268. $exerciseId = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : 0;
  269. $formSent = isset($_REQUEST['formSent']) ? $_REQUEST['formSent'] : null;
  270. $exerciseResult = isset($_REQUEST['exerciseResult']) ? $_REQUEST['exerciseResult'] : null;
  271. $choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
  272. $choice = empty($choice) ? isset($_REQUEST['choice2']) ? $_REQUEST['choice2'] : null : null;
  273. $exerciseResultCoordinates = isset($_REQUEST['exerciseResultCoordinates']) ? $_REQUEST['exerciseResultCoordinates'] : null;
  274. $current_question = isset($_REQUEST['num']) ? intval($_REQUEST['num']) : null;
  275. //Error message
  276. $error = '';
  277. /* Teacher takes an exam and want to see a preview, we delete the objExercise from the session in order to get the latest changes in the exercise */
  278. if (api_is_allowed_to_edit(null,true) && isset($_GET['preview']) && $_GET['preview'] == 1) {
  279. Session::erase('objExercise');
  280. }
  281. // 1. Loading the $objExercise variable
  282. if (!isset($_SESSION['objExercise']) || isset($_SESSION['objExercise']) && ($_SESSION['objExercise']->id != $_REQUEST['exerciseId'])) {
  283. // Construction of Exercise
  284. /** @var Exercise $objExercise */
  285. $objExercise = new Exercise();
  286. if ($debug) {error_log('1. Setting the $objExercise variable'); };
  287. unset($_SESSION['questionList']);
  288. // if the specified exercise doesn't exist or is disabled
  289. if (!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !$is_allowedToEdit && ($origin != 'learnpath'))) {
  290. if ($debug) { error_log('1.1. Error while reading the exercise'); };
  291. unset ($objExercise);
  292. $error = get_lang('ExerciseNotFound');
  293. } else {
  294. // Saves the object into the session
  295. Session::write('objExercise', $objExercise);
  296. if ($debug) { error_log('1.1. $_SESSION[objExercise] was unset - set now - end'); };
  297. }
  298. }
  299. // $objExercise = new Exercise(); $objExercise->read($exerciseId);
  300. //2. Checking if $objExercise is set
  301. if (!isset($objExercise) && isset($_SESSION['objExercise'])) {
  302. if ($debug) { error_log('2. Loading $objExercise from session'); };
  303. $objExercise = $_SESSION['objExercise'];
  304. }
  305. //3. $objExercise is not set, then return to the exercise list
  306. if (!is_object($objExercise)) {
  307. if ($debug) {error_log('3. $objExercise was not set, kill the script'); };
  308. header('Location: exercice.php');
  309. exit;
  310. }
  311. // If reminder ends we jump to the exercise_reminder
  312. if ($objExercise->review_answers) {
  313. if ($remind_question_id == -1) {
  314. $paramsReminder = "exerciseId=$exerciseId&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id&".api_get_cidreq();
  315. header('Location: exercise_reminder.php?'.$paramsReminder);
  316. exit;
  317. }
  318. }
  319. $current_timestamp = time();
  320. $my_remind_list = array();
  321. $time_control = false;
  322. if ($objExercise->expired_time != 0) {
  323. $time_control = true;
  324. }
  325. //Generating the time control key for the user
  326. $current_expired_time_key = ExerciseLib::get_time_control_key($objExercise->id, $learnpath_id, $learnpath_item_id);
  327. if ($debug) {
  328. error_log("4. current_expired_time_key: $current_expired_time_key ");
  329. }
  330. $_SESSION['duration_time'][$current_expired_time_key] = $current_timestamp;
  331. if ($time_control) {
  332. // Get the expired time of the current exercise in track_e_exercices.
  333. $total_seconds = $objExercise->expired_time * 60;
  334. }
  335. $exercise_title = $objExercise->selectTitle();
  336. $exercise_sound = $objExercise->selectSound();
  337. $show_clock = true;
  338. $user_id = api_get_user_id();
  339. if ($objExercise->selectAttempts() > 0) {
  340. $attempt_html = null;
  341. $attempt_count = get_attempt_count($user_id, $exerciseId, $learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  342. if ($attempt_count >= $objExercise->selectAttempts()) {
  343. $show_clock = false;
  344. if (!api_is_allowed_to_edit(null,true)) {
  345. if ($objExercise->results_disabled == 0 && $origin != 'learnpath') {
  346. //Showing latest attempt according with task BT#1628
  347. $exercise_stat_info = getExerciseResultsByUser($user_id, $exerciseId, api_get_course_int_id(), api_get_session_id());
  348. if (!empty($exercise_stat_info)) {
  349. $max_exe_id = max(array_keys($exercise_stat_info));
  350. $last_attempt_info = $exercise_stat_info[$max_exe_id];
  351. $attempt_html .= Display::div(get_lang('Date').': '.api_get_local_time($last_attempt_info['exe_date']), array('id'=>''));
  352. $attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  353. if (!empty($last_attempt_info['question_list'])) {
  354. foreach($last_attempt_info['question_list'] as $question_data) {
  355. $question_id = $question_data['question_id'];
  356. $marks = $question_data['marks'];
  357. $question_info = Question::read($question_id);
  358. $attempt_html .= Display::div($question_info->question, array('class'=>'question_title'));
  359. $attempt_html .= Display::div(get_lang('Score').' '.$marks, array('id'=>'question_score'));
  360. }
  361. }
  362. $score = ExerciseLib::show_score($last_attempt_info['exe_result'], $last_attempt_info['exe_weighting']);
  363. $attempt_html .= Display::div(get_lang('YourTotalScore').' '.$score, array('id'=>'question_score'));
  364. } else {
  365. $attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  366. }
  367. } else {
  368. $attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  369. }
  370. } else {
  371. $attempt_html .= Display :: return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  372. }
  373. if ($origin == 'learnpath') {
  374. Display :: display_reduced_header();
  375. } else {
  376. Display :: display_header($nameTools,'Exercises');
  377. }
  378. echo $attempt_html;
  379. if ($origin != 'learnpath') {
  380. Display :: display_footer();
  381. }
  382. exit;
  383. }
  384. }
  385. // 5. Getting user exercise info (if the user took the exam before) - generating exe_id
  386. $exercise_stat_info = $objExercise->getStatTrackExerciseInfo($learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  387. //if (1) {
  388. if (!isset($_SESSION['questionList'])) {
  389. // Selects the list of question ID
  390. $questionList = $objExercise->getQuestionList();
  391. // Media questions.
  392. $media_is_activated = $objExercise->mediaIsActivated();
  393. //Getting order from random
  394. if ($media_is_activated == false && $objExercise->isRandom() && isset($exercise_stat_info) && !empty($exercise_stat_info['data_tracking'])) {
  395. $questionList = explode(',', $exercise_stat_info['data_tracking']);
  396. }
  397. Session::write('questionList', $questionList);
  398. if ($debug > 0) { error_log('$_SESSION[questionList] was set'); }
  399. } else {
  400. if (isset($objExercise) && isset($_SESSION['objExercise'])) {
  401. $questionList = Session::read('questionList');
  402. }
  403. }
  404. //Fix in order to get the correct question list
  405. $questionListUncompressed = $objExercise->getQuestionListWithMediasUncompressed();
  406. Session::write('question_list_uncompressed', $questionListUncompressed);
  407. $clock_expired_time = null;
  408. if (empty($exercise_stat_info)) {
  409. if ($debug) error_log('5 $exercise_stat_info is empty ');
  410. $total_weight = 0;
  411. foreach ($questionListUncompressed as $question_id) {
  412. $objQuestionTmp = Question::read($question_id);
  413. $total_weight += floatval($objQuestionTmp->weighting);
  414. }
  415. if ($time_control) {
  416. $expected_time = $current_timestamp + $total_seconds;
  417. if ($debug) error_log('5.1. $current_timestamp '.$current_timestamp);
  418. if ($debug) error_log('5.2. $expected_time '.$expected_time);
  419. $clock_expired_time = api_get_utc_datetime($expected_time);
  420. if ($debug) error_log('5.3. $expected_time '.$clock_expired_time);
  421. //Sessions that contain the expired time
  422. $_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
  423. if ($debug) { error_log('5.4. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
  424. }
  425. $exe_id = $objExercise->save_stat_track_exercise_info($clock_expired_time, $learnpath_id, $learnpath_item_id, $learnpath_item_view_id, $questionListUncompressed, $total_weight);
  426. $exercise_stat_info = $objExercise->getStatTrackExerciseInfo($learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  427. if ($debug) error_log("5.5 Creating a new attempt exercise_stat_info[] exe_id : $exe_id");
  428. } else {
  429. $exe_id = $exercise_stat_info['exe_id'];
  430. if ($debug) error_log("5 exercise_stat_info[] exists getting exe_id: $exe_id ");
  431. }
  432. // Array to check in order to block the chat
  433. ExerciseLib::create_chat_exercise_session($exe_id);
  434. if ($debug) { error_log('6. $objExercise->getStatTrackExerciseInfo function called:: '.print_r($exercise_stat_info, 1)); };
  435. if (!empty($exercise_stat_info['questions_to_check'])) {
  436. $my_remind_list = $exercise_stat_info['questions_to_check'];
  437. $my_remind_list = explode(',', $my_remind_list);
  438. $my_remind_list = array_filter($my_remind_list);
  439. }
  440. if ($debug) { error_log("6.0 my_remind_list array: ".print_r($my_remind_list, 1)); }
  441. $params = "exe_id=$exe_id&exerciseId=$exerciseId&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id&".api_get_cidreq();
  442. if ($debug) { error_log("6.1 params: $params"); };
  443. if ($reminder == 2 && empty($my_remind_list)) {
  444. if ($debug) { error_log("6.2 calling the exercise_reminder.php "); };
  445. header('Location: exercise_reminder.php?'.$params);
  446. exit;
  447. }
  448. /*
  449. * 7. Loading Time control parameters
  450. * If the expired time is major that zero(0) then the expired time is compute on this time.
  451. */
  452. if ($time_control) {
  453. if ($debug) error_log('7.1. Time control is enabled');
  454. if ($debug) error_log('7.2. $current_expired_time_key '.$current_expired_time_key);
  455. if ($debug) error_log('7.3. $_SESSION[expired_time][$current_expired_time_key] '.$_SESSION['expired_time'][$current_expired_time_key]);
  456. if (!isset($_SESSION['expired_time'][$current_expired_time_key])) {
  457. //Timer - Get expired_time for a student
  458. if (!empty($exercise_stat_info)) {
  459. if ($debug) {error_log('7.4 Seems that the session ends and the user want to retake the exam'); };
  460. $expired_time_of_this_attempt = $exercise_stat_info['expired_time_control'];
  461. if ($debug) {error_log('7.5 $expired_time_of_this_attempt: '.$expired_time_of_this_attempt); }
  462. //Get the last attempt of an exercice
  463. $last_attempt_date = getLastAttemptDateOfExercise($exercise_stat_info['exe_id']);
  464. //This means that the user enters the exam but do not answer the first question we get the date from the track_e_exercises not from the track_et_attempt see #2069
  465. if (empty($last_attempt_date)) {
  466. $diff = $current_timestamp - api_strtotime($exercise_stat_info['start_date'], 'UTC');
  467. $last_attempt_date = api_get_utc_datetime(api_strtotime($exercise_stat_info['start_date'],'UTC') + $diff);
  468. } else {
  469. //Recalculate the time control due #2069
  470. $diff = $current_timestamp - api_strtotime($last_attempt_date,'UTC');
  471. $last_attempt_date = api_get_utc_datetime(api_strtotime($last_attempt_date,'UTC') + $diff);
  472. }
  473. if ($debug) {error_log('7.6. $last_attempt_date: '.$last_attempt_date); }
  474. //New expired time - it is due to the possible closure of session
  475. $new_expired_time_in_seconds = api_strtotime($expired_time_of_this_attempt, 'UTC') - api_strtotime($last_attempt_date,'UTC');
  476. if ($debug) {error_log('7.7. $new_expired_time_in_seconds: '.$new_expired_time_in_seconds); }
  477. $expected_time = $current_timestamp + $new_expired_time_in_seconds;
  478. if ($debug) {error_log('7.8. $expected_time1: '.$expected_time); }
  479. $clock_expired_time = api_get_utc_datetime($expected_time);
  480. if ($debug) {error_log('7.9. $clock_expired_time: '.$clock_expired_time); }
  481. // First we update the attempt to today
  482. // How the expired time is changed into "track_e_exercices" table,then the last attempt for this student should be changed too,so
  483. ExerciseLib::update_attempt_date($exercise_stat_info['exe_id'], $last_attempt_date);
  484. //Sessions that contain the expired time
  485. $_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
  486. if ($debug) {error_log('7.11. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
  487. }
  488. } else {
  489. $clock_expired_time = $_SESSION['expired_time'][$current_expired_time_key];
  490. }
  491. } else {
  492. if ($debug) { error_log("7. No time control"); };
  493. }
  494. // Get time left for expiring time
  495. $time_left = api_strtotime($clock_expired_time,'UTC') - time();
  496. /*
  497. * The time control feature is enable here - this feature is enable for a jquery plugin called epiclock
  498. * for more details of how it works see this link : http://eric.garside.name/docs.html?p=epiclock
  499. */
  500. if ($time_control) {
  501. //Sends the exercise form when the expired time is finished
  502. $htmlHeadXtra[] = $objExercise->show_time_control_js($time_left);
  503. }
  504. if ($debug) error_log('8. Question list loaded '.print_r($questionList, 1));
  505. $question_count = $objExercise->getCountCompressedQuestionList();
  506. $urlMainExercise = api_get_path(WEB_CODE_PATH).'exercice/';
  507. if ($formSent && isset($_POST)) {
  508. if ($debug) { error_log('9. $formSent was sent'); }
  509. // Initializing
  510. if (!is_array($exerciseResult)) {
  511. $exerciseResult = array();
  512. $exerciseResultCoordinates = array();
  513. }
  514. // Only for hot spot
  515. if (!isset($choice) && isset($_REQUEST['hidden_hotspot_id'])) {
  516. $hotspot_id = (int)($_REQUEST['hidden_hotspot_id']);
  517. $choice = array($hotspot_id => '');
  518. }
  519. // Filling array exercise result
  520. // if the user has answered at least one question
  521. if (is_array($choice)) {
  522. if ($debug) { error_log('9.1. $choice is an array '.print_r($choice, 1)); }
  523. // Also store hot spot spots in the session ($exerciseResultCoordinates
  524. // will be stored in the session at the end of this script)
  525. if (isset($_POST['hotspot'])) {
  526. $exerciseResultCoordinates = $_POST['hotspot'];
  527. if ($debug) { error_log('9.2. $_POST[hotspot] data '.print_r($exerciseResultCoordinates, 1)); }
  528. }
  529. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  530. // $exerciseResult receives the content of the form.
  531. // Each choice of the student is stored into the array $choice
  532. $exerciseResult = $choice;
  533. } else {
  534. // gets the question ID from $choice. It is the key of the array
  535. list ($key) = array_keys($choice);
  536. // if the user didn't already answer this question
  537. if (!isset($exerciseResult[$key])) {
  538. // stores the user answer into the array
  539. $exerciseResult[$key] = $choice[$key];
  540. //saving each question
  541. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  542. $questionId = $key;
  543. // gets the student choice for this question
  544. $choice = $exerciseResult[$questionId];
  545. if (isset($exe_id)) {
  546. //Manage the question and answer attempts
  547. if ($debug) { error_log('8.3. manageAnswers exe_id: '.$exe_id.' - $questionId: '.$questionId.' Choice'.print_r($choice,1)); }
  548. $objExercise->manageAnswers($exe_id, $questionId, $choice,'exercise_show',$exerciseResultCoordinates, true, false,false);
  549. }
  550. //END of saving and qualifying
  551. }
  552. }
  553. }
  554. if ($debug) { error_log('9.3. $choice is an array - end'); }
  555. if ($debug) { error_log('9.4. $exerciseResult '.print_r($exerciseResult,1)); }
  556. }
  557. // the script "exercise_result.php" will take the variable $exerciseResult from the session
  558. Session::write('exerciseResult',$exerciseResult);
  559. //Session::write('remind_list', $remind_list);
  560. Session::write('exerciseResultCoordinates',$exerciseResultCoordinates);
  561. // if all questions on one page OR if it is the last question (only for an exercise with one question per page)
  562. if (($objExercise->type == ALL_ON_ONE_PAGE || $current_question >= $question_count)) {
  563. if (api_is_allowed_to_session_edit()) {
  564. // goes to the script that will show the result of the exercise
  565. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  566. if ($debug) { error_log('10. Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php'); }
  567. //We check if the user attempts before sending to the exercise_result.php
  568. if ($objExercise->selectAttempts() > 0) {
  569. $attempt_count = get_attempt_count(api_get_user_id(), $exerciseId, $learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  570. if ($attempt_count >= $objExercise->selectAttempts()) {
  571. Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), false);
  572. if ($origin != 'learnpath') {
  573. //so we are not in learnpath tool
  574. echo '</div>'; //End glossary div
  575. Display :: display_footer();
  576. } else {
  577. echo '</body></html>';
  578. }
  579. }
  580. }
  581. header("Location: ".$urlMainExercise."exercise_result.php?".api_get_cidreq()."&exe_id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  582. exit;
  583. } else {
  584. //Time control is only enabled for ONE PER PAGE
  585. if (!empty($exe_id) && is_numeric($exe_id)) {
  586. //Verify if the current test is fraudulent
  587. if (ExerciseLib::exercise_time_control_is_valid($exerciseId, $learnpath_id, $learnpath_item_id)) {
  588. $sql_exe_result = "";
  589. if ($debug) { error_log('exercise_time_control_is_valid is valid'); }
  590. } else {
  591. $sql_exe_result = ", exe_result = 0";
  592. if ($debug) { error_log('exercise_time_control_is_valid is NOT valid then exe_result = 0 '); }
  593. }
  594. }
  595. if ($debug) { error_log('10. Redirecting to exercise_show.php'); }
  596. header("Location: ".$urlMainExercise."exercise_result.php?".api_get_cidreq()."&exe_id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  597. exit;
  598. }
  599. } else {
  600. if ($debug) { error_log('10. Redirecting to exercise_submit.php'); }
  601. header("Location: exercise_submit.php?".api_get_cidreq()."&exerciseId=$exerciseId&origin=$origin");
  602. exit;
  603. }
  604. }
  605. if ($debug) { error_log('11. $formSent was set - end'); }
  606. } else {
  607. if ($debug) { error_log('9. $formSent was NOT sent'); }
  608. }
  609. // Getting the latest questionId
  610. $latestQuestionId = getLatestQuestionIdFromAttempt($exe_id);
  611. if (is_null($current_question)) {
  612. $current_question = 1;
  613. if ($latestQuestionId) {
  614. $current_question = $objExercise->getPositionInCompressedQuestionList($latestQuestionId);
  615. }
  616. } else {
  617. $current_question++;
  618. }
  619. if ($question_count != 0) {
  620. if (($objExercise->type == ALL_ON_ONE_PAGE || $current_question > $question_count)) {
  621. if (api_is_allowed_to_session_edit()) {
  622. // goes to the script that will show the result of the exercise
  623. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  624. if ($debug) { error_log('12. Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php'); }
  625. //We check if the user attempts before sending to the exercise_result.php
  626. if ($objExercise->selectAttempts() > 0) {
  627. $attempt_count = get_attempt_count(api_get_user_id(), $exerciseId, $learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  628. if ($attempt_count >= $objExercise->selectAttempts()) {
  629. Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), false);
  630. if ($origin != 'learnpath') {
  631. //so we are not in learnpath tool
  632. echo '</div>'; //End glossary div
  633. Display :: display_footer();
  634. } else {
  635. echo '</body></html>';
  636. }
  637. exit;
  638. }
  639. }
  640. } else {
  641. //Time control is only enabled for ONE PER PAGE
  642. if (!empty($exe_id) && is_numeric($exe_id)) {
  643. //Verify if the current test is fraudulent
  644. $check = ExerciseLib::exercise_time_control_is_valid($exerciseId, $learnpath_id, $learnpath_item_id);
  645. if ($check) {
  646. $sql_exe_result = "";
  647. if ($debug) { error_log('12. exercise_time_control_is_valid is valid'); }
  648. } else {
  649. $sql_exe_result = ", exe_result = 0";
  650. if ($debug) { error_log('12. exercise_time_control_is_valid is NOT valid then exe_result = 0 '); }
  651. }
  652. }
  653. if ($objExercise->review_answers) {
  654. header('Location: '.$urlMainExercise.'exercise_reminder.php?'.$params);
  655. exit;
  656. } else {
  657. header("Location: ".$urlMainExercise."exercise_result.php?".api_get_cidreq()."&exe_id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  658. exit;
  659. }
  660. }
  661. } else {
  662. if ($debug) { error_log('Redirecting to exercise_submit.php'); }
  663. exit;
  664. }
  665. }
  666. } else {
  667. $error = get_lang('ThereAreNoQuestionsForThisExercise');
  668. // if we are in the case where user select random by category, but didn't choose the number of random question
  669. if ($objExercise->selectRandomByCat() > 0 && $objExercise->random <= 0) {
  670. $error .= "<br/>".get_lang('PleaseSelectSomeRandomQuestion');
  671. }
  672. }
  673. if (!empty ($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  674. $_SESSION['gradebook'] = Security :: remove_XSS($_GET['gradebook']);
  675. $gradebook = $_SESSION['gradebook'];
  676. } elseif (empty ($_GET['gradebook'])) {
  677. unset ($_SESSION['gradebook']);
  678. $gradebook = '';
  679. }
  680. if (!empty ($gradebook) && $gradebook == 'view') {
  681. $interbreadcrumb[]= array ('url' => '../gradebook/' . Security::remove_XSS($_SESSION['gradebook_dest']),'name' => get_lang('ToolGradebook'));
  682. }
  683. $interbreadcrumb[]= array ("url" => "exercice.php?gradebook=$gradebook", "name" => get_lang('Exercices'));
  684. $interbreadcrumb[]= array ("url" => "#","name" => $objExercise->name);
  685. if ($origin != 'learnpath') { //so we are not in learnpath tool
  686. Display :: display_header($nameTools,'Exercises');
  687. if (!api_is_allowed_to_session_edit()) {
  688. Display :: display_warning_message(get_lang('SessionIsReadOnly'));
  689. }
  690. } else {
  691. Display::display_reduced_header();
  692. echo '<div style="height:10px">&nbsp;</div>';
  693. }
  694. $show_quiz_edition = $objExercise->added_in_lp();
  695. // I'm in a preview mode
  696. if (api_is_course_admin() && $origin != 'learnpath') {
  697. echo '<div class="actions">';
  698. if ($show_quiz_edition == false) {
  699. echo '<a href="exercise_admin.php?' . api_get_cidreq() . '&modifyExercise=yes&exerciseId=' . $objExercise->id . '">'.Display :: return_icon('settings.png', get_lang('ModifyExercise'),'',ICON_SIZE_MEDIUM).'</a>';
  700. } else {
  701. echo '<a href="#">'.Display::return_icon('settings_na.png', get_lang('ModifyExercise'),'',ICON_SIZE_MEDIUM).'</a>';
  702. }
  703. echo '</div>';
  704. }
  705. if ($objExercise->type == ONE_PER_PAGE) {
  706. echo $objExercise->getProgressPagination(
  707. $exe_id,
  708. $questionList,
  709. $questionListUncompressed,
  710. $my_remind_list,
  711. $reminder,
  712. $remind_question_id,
  713. api_get_self().'?'.$params,
  714. $current_question
  715. );
  716. }
  717. $is_visible_return = $objExercise->is_visible($learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  718. if ($is_visible_return['value'] == false) {
  719. echo $is_visible_return['message'];
  720. if ($origin != 'learnpath') {
  721. Display :: display_footer();
  722. }
  723. exit;
  724. }
  725. $limit_time_exists = (($objExercise->start_time != '0000-00-00 00:00:00') || ($objExercise->end_time != '0000-00-00 00:00:00')) ? true : false;
  726. if ($limit_time_exists) {
  727. $exercise_start_time = api_strtotime($objExercise->start_time, 'UTC');
  728. $exercise_end_time = api_strtotime($objExercise->end_time, 'UTC');
  729. $time_now = time();
  730. if ($objExercise->start_time != '0000-00-00 00:00:00') {
  731. $permission_to_start = (($time_now - $exercise_start_time) > 0) ? true : false;
  732. } else {
  733. $permission_to_start = true;
  734. }
  735. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  736. if ($objExercise->end_time != '0000-00-00 00:00:00') {
  737. $exercise_timeover = (($time_now - $exercise_end_time) > 0) ? true : false;
  738. } else {
  739. $exercise_timeover = false;
  740. }
  741. }
  742. if (!$permission_to_start || $exercise_timeover) {
  743. if (!api_is_allowed_to_edit(null,true)) {
  744. $message_warning = $permission_to_start ? get_lang('ReachedTimeLimit') : get_lang('ExerciseNoStartedYet');
  745. Display :: display_warning_message(sprintf($message_warning, $exercise_title, $objExercise->selectAttempts()));
  746. if ($origin != 'learnpath') {
  747. Display :: display_footer();
  748. }
  749. exit;
  750. } else {
  751. $message_warning = $permission_to_start ? get_lang('ReachedTimeLimitAdmin') : get_lang('ExerciseNoStartedAdmin');
  752. Display :: display_warning_message(sprintf($message_warning, $exercise_title, $objExercise->selectAttempts()));
  753. }
  754. }
  755. }
  756. // Blocking empty start times see BT#2800
  757. global $_custom;
  758. if (isset($_custom['exercises_hidden_when_no_start_date']) && $_custom['exercises_hidden_when_no_start_date']) {
  759. if (empty($objExercise->start_time) || $objExercise->start_time == '0000-00-00 00:00:00') {
  760. Display :: display_warning_message(sprintf(get_lang('ExerciseNoStartedYet'), $exercise_title, $objExercise->selectAttempts()));
  761. if ($origin != 'learnpath') {
  762. Display :: display_footer();
  763. }
  764. }
  765. }
  766. //Timer control
  767. if ($time_control) {
  768. echo $objExercise->returnTimeLeftDiv();
  769. echo '<div style="display:none" class="warning-message" id="expired-message-id">'.get_lang('ExerciceExpiredTimeMessage').'</div>';
  770. }
  771. if (!empty($objExercise->description)) {
  772. echo '<br />';
  773. echo Display::generate_accordion(array(array('title' => get_lang('ExerciseDescriptionLabel'), 'content' => $objExercise->description)), 'jquery', 'description_content');
  774. }
  775. if ($origin != 'learnpath') {
  776. echo '<div id="highlight-plugin" class="glossary-content">';
  777. }
  778. if ($reminder == 2) {
  779. if ($debug) { error_log(' $reminder == 2'); }
  780. $data_tracking = $exercise_stat_info['data_tracking'];
  781. $data_tracking = explode(',', $data_tracking);
  782. // Set by default the 1st question
  783. $current_question = 1;
  784. if (!empty($my_remind_list)) {
  785. //Checking which questions we are going to call from the remind list
  786. for ($i = 0; $i < count($data_tracking); $i++) {
  787. for($j = 0; $j < count($my_remind_list); $j++) {
  788. if (!empty($remind_question_id)) {
  789. if ($remind_question_id == $my_remind_list[$j]) {
  790. if ($remind_question_id == $data_tracking[$i]) {
  791. if (isset($my_remind_list[$j+1])) {
  792. $remind_question_id = $my_remind_list[$j+1];
  793. $current_question = $i + 1;
  794. } else {
  795. $remind_question_id = -1; //We end the remind list we go to the exercise_reminder.php please
  796. $current_question = $i + 1; // last question
  797. }
  798. break 2;
  799. }
  800. }
  801. } else {
  802. if ($my_remind_list[$j] == $data_tracking[$i]) {
  803. if (isset($my_remind_list[$j+1])) {
  804. $remind_question_id = $my_remind_list[$j+1];
  805. $current_question = $i + 1; // last question
  806. } else {
  807. $remind_question_id = -1; //We end the remind list we go to the exercise_reminder.php please
  808. $current_question = $i + 1; // last question
  809. }
  810. break 2;
  811. }
  812. }
  813. }
  814. }
  815. } else {
  816. if ($objExercise->review_answers) {
  817. if ($debug) { error_log('. redirecting to exercise_reminder.php '); }
  818. header("Location: exercise_reminder.php?$params");
  819. exit;
  820. }
  821. }
  822. }
  823. if ($objExercise->review_answers) {
  824. $script_php = $urlMainExercise.'exercise_reminder.php';
  825. } else {
  826. $script_php = $urlMainExercise.'exercise_result.php';
  827. }
  828. if (!empty($error)) {
  829. Display :: display_error_message($error, false);
  830. } else {
  831. if (!empty($exercise_sound)) {
  832. echo "<a href=\"../document/download.php?doc_url=%2Faudio%2F".Security::remove_XSS($exercise_sound)."\" target=\"_blank\">", "<img src=\"../img/sound.gif\" border=\"0\" align=\"absmiddle\" alt=", get_lang('Sound') . "\" /></a>";
  833. }
  834. // Get number of hotspot questions for javascript validation
  835. $number_of_hotspot_questions = 0;
  836. $onsubmit = '';
  837. $i = 0;
  838. if (!empty($questionList)) {
  839. foreach ($questionList as $questionId) {
  840. // for sequential exercises
  841. if ($objExercise->type == ONE_PER_PAGE) {
  842. // if it is not the right question, goes to the next loop iteration
  843. if ($current_question != $i) {
  844. continue;
  845. } else {
  846. $objQuestionTmp = Question::read($questionId);
  847. if ($objQuestionTmp->selectType() == HOT_SPOT || $objQuestionTmp->selectType() == HOT_SPOT_DELINEATION) {
  848. $number_of_hotspot_questions++;
  849. }
  850. break;
  851. }
  852. } else {
  853. $objQuestionTmp = Question::read($questionId);
  854. if ($objQuestionTmp->selectType() == HOT_SPOT || $objQuestionTmp->selectType() == HOT_SPOT_DELINEATION) {
  855. $number_of_hotspot_questions++;
  856. }
  857. }
  858. $i++;
  859. }
  860. }
  861. if ($number_of_hotspot_questions > 0) {
  862. $onsubmit = "onsubmit=\"return validateFlashVar('" . $number_of_hotspot_questions . "', '" . get_lang('HotspotValidateError1') . "', '" . get_lang('HotspotValidateError2') . "');\"";
  863. }
  864. echo '<script>
  865. $(function() {
  866. $(".main_question").mouseover(function() {
  867. //$(this).find(".exercise_save_now_button").show();
  868. //$(this).addClass("question_highlight");
  869. });
  870. $(".main_question").mouseout(function() {
  871. //$(this).find(".exercise_save_now_button").hide();
  872. $(this).removeClass("question_highlight");
  873. });
  874. $(".no_remind_highlight").hide();
  875. });
  876. function previous_question(question_num) {
  877. url = "'.$urlMainExercise.'exercise_submit.php?'.$params.'&num="+question_num;
  878. window.location = url;
  879. }
  880. function previous_question_and_save(previous_question_id, question_id_to_save) {
  881. url = "'.$urlMainExercise.'exercise_submit.php?'.$params.'&num="+previous_question_id;
  882. //Save the current question
  883. save_now(question_id_to_save, url);
  884. }
  885. function save_question_list(question_list) {
  886. $.each(question_list, function(key, question_id) {
  887. save_now(question_id, null, false);
  888. });
  889. var url = "";
  890. if ('.$reminder.' == 1 ) {
  891. url = "exercise_reminder.php?'.$params.'&num='.$current_question.'";
  892. } else if ('.$reminder.' == 2 ) {
  893. url = "'.$urlMainExercise.'exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'&reminder=2";
  894. } else {
  895. url = "'.$urlMainExercise.'exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'";
  896. }
  897. //$("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('save.png', get_lang('Saved'), array(), ICON_SIZE_SMALL)).'");
  898. window.location = url;
  899. }
  900. function save_now(question_id, url_extra, redirect) {
  901. if (redirect == undefined) {
  902. redirect = true;
  903. }
  904. //1. Normal choice inputs
  905. var my_choice = $(\'*[name*="choice[\'+question_id+\']"]\').serialize();
  906. //2. Reminder checkbox
  907. var remind_list = $(\'*[name*="remind_list"]\').serialize();
  908. //3. Hotspots
  909. var hotspot = $(\'*[name*="hotspot[\'+question_id+\']"]\').serialize();
  910. var ckeditor = CKEDITOR.instances[\'choice[\'+question_id+\']\'];
  911. if (ckeditor) {
  912. value = ckeditor.getData();
  913. my_choice = {};
  914. my_choice["choice["+question_id+"]"] = value;
  915. my_choice = $.param(my_choice);
  916. }
  917. if ($(\'input[name="remind_list[\'+question_id+\']"]\').is(\':checked\')) {
  918. $("#question_div_"+question_id).addClass("remind_highlight");
  919. } else {
  920. $("#question_div_"+question_id).removeClass("remind_highlight");
  921. }
  922. // Only for the first time
  923. $("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('loading1.gif')).'");
  924. $.ajax({
  925. type : "post",
  926. async: false,
  927. url: "'.api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?a=save_exercise_by_now",
  928. data: "'.$params.'&type=simple&question_id="+question_id+"&"+my_choice+"&"+hotspot+"&"+remind_list,
  929. success: function(return_value) {
  930. if (return_value == "ok") {
  931. $("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('save.png', get_lang('Saved'), array(), ICON_SIZE_SMALL)).'");
  932. } else if (return_value == "error") {
  933. $("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('error.png', get_lang('Error'), array(), ICON_SIZE_SMALL)).'");
  934. } else if (return_value == "one_per_page") {
  935. var url = "";
  936. if ('.$reminder.' == 1 ) {
  937. url = "exercise_reminder.php?'.$params.'&num='.$current_question.'";
  938. } else if ('.$reminder.' == 2 ) {
  939. url = "'.$urlMainExercise.'exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'&reminder=2";
  940. } else {
  941. url = "'.$urlMainExercise.'exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'";
  942. }
  943. if (url_extra) {
  944. url = url_extra;
  945. }
  946. $("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('save.png', get_lang('Saved'), array(), ICON_SIZE_SMALL)).'");
  947. if (redirect) {
  948. window.location = url;
  949. }
  950. }
  951. },
  952. error: function() {
  953. $("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('error.png', get_lang('Error'), array(), ICON_SIZE_SMALL)).'");
  954. }
  955. });
  956. return false;
  957. }
  958. function save_now_all(validate) {
  959. //1. Input choice
  960. var my_choice = $(\'*[name*="choice"]\').serialize();
  961. //2. Reminder
  962. var remind_list = $(\'*[name*="remind_list"]\').serialize();
  963. //3. Hotspots
  964. var hotspot = $(\'*[name*="hotspot"]\').serialize();
  965. //Question list
  966. var question_list = ['.implode(',', $questionList).'];
  967. var free_answers = {};
  968. $.each(question_list, function(index, my_question_id) {
  969. //Checking FCK
  970. if (typeof(FCKeditorAPI) !== "undefined") {
  971. var oEditor = FCKeditorAPI.GetInstance("choice["+my_question_id+"]") ;
  972. var fck_content = "";
  973. if (oEditor) {
  974. fck_content = oEditor.GetHTML();
  975. //alert(index + " " +my_question_id + " " +fck_content);
  976. free_answers["free_choice["+my_question_id+"]"] = fck_content;
  977. }
  978. }
  979. });
  980. free_answers = $.param(free_answers);
  981. $("#save_all_reponse").html("'.addslashes(Display::return_icon('loading1.gif')).'");
  982. $.ajax({
  983. type:"post",
  984. async: false,
  985. url: "'.api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?a=save_exercise_by_now",
  986. data: "'.$params.'&type=all&"+my_choice+"&"+hotspot+"&"+free_answers+"&"+remind_list,
  987. success: function(return_value) {
  988. if (return_value == "ok") {
  989. //$("#save_all_reponse").html("'.addslashes(Display::return_icon('accept.png')).'");
  990. if (validate == "validate") {
  991. window.location = "'.$script_php.'?'.$params.'";
  992. } else {
  993. $("#save_all_reponse").html("'.addslashes(Display::return_icon('accept.png')).'");
  994. }
  995. } else {
  996. $("#save_all_reponse").html("'.addslashes(Display::return_icon('wrong.gif')).'");
  997. }
  998. }
  999. });
  1000. return false;
  1001. }
  1002. function validate_all() {
  1003. save_now_all("validate");
  1004. return false;
  1005. }
  1006. </script>';
  1007. // Show list of questions.
  1008. $attempt_list = array();
  1009. if (isset($exe_id)) {
  1010. $attempt_list = getAllExerciseEventByExeId($exe_id);
  1011. }
  1012. $remind_list = array();
  1013. if (isset($exercise_stat_info['questions_to_check']) && !empty($exercise_stat_info['questions_to_check'])) {
  1014. $remind_list = explode(',', $exercise_stat_info['questions_to_check']);
  1015. }
  1016. echo '<form id="exercise_form" method="post" action="'.api_get_path(WEB_CODE_PATH).'exercice/exercise_submit.php?'.api_get_cidreq().'&autocomplete=off&gradebook='.$gradebook."&exerciseId=".$exerciseId .'" name="frm_exercise" '.$onsubmit.'>
  1017. <input type="hidden" name="formSent" value="1" />
  1018. <input type="hidden" name="exerciseId" value="'.$exerciseId . '" />
  1019. <input type="hidden" name="num" value="'.$current_question.'" id="num_current_id" />
  1020. <input type="hidden" name="exe_id" value="'.$exe_id . '" />
  1021. <input type="hidden" name="origin" value="'.$origin . '" />
  1022. <input type="hidden" name="learnpath_id" value="'.$learnpath_id . '" />
  1023. <input type="hidden" name="learnpath_item_id" value="'.$learnpath_item_id . '" />
  1024. <input type="hidden" name="learnpath_item_view_id" value="'.$learnpath_item_view_id . '" />';
  1025. $objExercise->renderQuestionList($questionList, $current_question, $exerciseResult, $attempt_list, $remind_list);
  1026. echo '</form>';
  1027. }
  1028. if ($origin != 'learnpath') {
  1029. //so we are not in learnpath tool
  1030. echo '</div>'; //End glossary div
  1031. Display :: display_footer();
  1032. } else {
  1033. echo '</body></html>';
  1034. }