exercise_submit.php 60 KB

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