exercise_show.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Shows the exercise results.
  6. *
  7. * @author Julio Montoya - Added switchable fill in blank option added
  8. *
  9. * @version $Id: exercise_show.php 22256 2009-07-20 17:40:20Z ivantcholakov $
  10. *
  11. * @package chamilo.exercise
  12. *
  13. * @todo remove the debug code and use the general debug library
  14. * @todo small letters for table variables
  15. */
  16. require_once __DIR__.'/../inc/global.inc.php';
  17. $origin = api_get_origin();
  18. $currentUserId = api_get_user_id();
  19. $printHeaders = $origin === 'learnpath';
  20. $id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0; //exe id
  21. if (empty($id)) {
  22. api_not_allowed(true);
  23. }
  24. // Getting results from the exe_id. This variable also contain all the information about the exercise
  25. $track_exercise_info = ExerciseLib::get_exercise_track_exercise_info($id);
  26. //No track info
  27. if (empty($track_exercise_info)) {
  28. api_not_allowed($printHeaders);
  29. }
  30. $exercise_id = $track_exercise_info['id'];
  31. $student_id = $track_exercise_info['exe_user_id'];
  32. $learnpath_id = $track_exercise_info['orig_lp_id'];
  33. $learnpath_item_id = $track_exercise_info['orig_lp_item_id'];
  34. $lp_item_view_id = $track_exercise_info['orig_lp_item_view_id'];
  35. $isBossOfStudent = false;
  36. if (api_is_student_boss()) {
  37. // Check if boss has access to user info.
  38. if (UserManager::userIsBossOfStudent($currentUserId, $student_id)) {
  39. $isBossOfStudent = true;
  40. } else {
  41. api_not_allowed($printHeaders);
  42. }
  43. } else {
  44. api_protect_course_script($printHeaders, false, true);
  45. }
  46. // Database table definitions
  47. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  48. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  49. $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  50. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  51. if (empty($formSent)) {
  52. $formSent = isset($_REQUEST['formSent']) ? $_REQUEST['formSent'] : null;
  53. }
  54. if (empty($exerciseResult)) {
  55. $exerciseResult = Session::read('exerciseResult');
  56. }
  57. if (empty($choiceDegreeCertainty)) {
  58. $choiceDegreeCertainty = isset($_REQUEST['choiceDegreeCertainty']) ? $_REQUEST['choiceDegreeCertainty'] : null;
  59. }
  60. $questionId = isset($_REQUEST['questionId']) ? (int) $_REQUEST['questionId'] : null;
  61. if (empty($choice)) {
  62. $choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
  63. }
  64. if (empty($questionNum)) {
  65. $questionNum = isset($_REQUEST['num']) ? $_REQUEST['num'] : null;
  66. }
  67. if (empty($nbrQuestions)) {
  68. $nbrQuestions = isset($_REQUEST['nbrQuestions']) ? $_REQUEST['nbrQuestions'] : null;
  69. }
  70. if (empty($questionList)) {
  71. $questionList = Session::read('questionList');
  72. }
  73. if (empty($objExercise)) {
  74. $objExercise = Session::read('objExercise');
  75. }
  76. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  77. $courseInfo = api_get_course_info();
  78. $sessionId = api_get_session_id();
  79. $is_allowedToEdit =
  80. api_is_allowed_to_edit(null, true) ||
  81. api_is_course_tutor() ||
  82. api_is_session_admin() ||
  83. api_is_drh() ||
  84. api_is_student_boss();
  85. if (!empty($sessionId) && !$is_allowedToEdit) {
  86. if (api_is_course_session_coach(
  87. $currentUserId,
  88. api_get_course_int_id(),
  89. $sessionId
  90. )) {
  91. if (!api_coach_can_edit_view_results(api_get_course_int_id(), $sessionId)) {
  92. api_not_allowed($printHeaders);
  93. }
  94. }
  95. } else {
  96. if (!$is_allowedToEdit) {
  97. api_not_allowed($printHeaders);
  98. }
  99. }
  100. $allowCoachFeedbackExercises = api_get_setting('allow_coach_feedback_exercises') === 'true';
  101. $maxEditors = (int) api_get_setting('exercise_max_ckeditors_in_page');
  102. $isCoachAllowedToEdit = api_is_allowed_to_edit(false, true);
  103. $isFeedbackAllowed = false;
  104. if (api_is_excluded_user_type(true, $student_id)) {
  105. api_not_allowed($printHeaders);
  106. }
  107. $locked = api_resource_is_locked_by_gradebook($exercise_id, LINK_EXERCISE);
  108. if (empty($objExercise)) {
  109. $objExercise = new Exercise();
  110. $objExercise->read($exercise_id);
  111. }
  112. $feedback_type = $objExercise->getFeedbackType();
  113. // Only users can see their own results
  114. if (!$is_allowedToEdit) {
  115. if ($student_id != $currentUserId) {
  116. api_not_allowed($printHeaders);
  117. }
  118. }
  119. $allowRecordAudio = api_get_setting('enable_record_audio') === 'true';
  120. $allowTeacherCommentAudio = api_get_configuration_value('allow_teacher_comment_audio') === true;
  121. $js = '<script>'.api_get_language_translate_html().'</script>';
  122. $htmlHeadXtra[] = $js;
  123. if (api_is_in_gradebook()) {
  124. $interbreadcrumb[] = [
  125. 'url' => Category::getUrl(),
  126. 'name' => get_lang('ToolGradebook'),
  127. ];
  128. }
  129. $interbreadcrumb[] = [
  130. 'url' => 'exercise.php?'.api_get_cidreq(),
  131. 'name' => get_lang('Exercises'),
  132. ];
  133. $interbreadcrumb[] = [
  134. 'url' => 'overview.php?exerciseId='.$exercise_id.'&'.api_get_cidreq(),
  135. 'name' => $objExercise->selectTitle(true),
  136. ];
  137. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Result')];
  138. $this_section = SECTION_COURSES;
  139. $htmlHeadXtra[] = '<link rel="stylesheet" href="'.api_get_path(WEB_LIBRARY_JS_PATH).'hotspot/css/hotspot.css">';
  140. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'hotspot/js/hotspot.js"></script>';
  141. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'annotation/js/annotation.js"></script>';
  142. if ($allowRecordAudio && $allowTeacherCommentAudio) {
  143. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'rtc/RecordRTC.js"></script>';
  144. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'wami-recorder/recorder.js"></script>';
  145. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'wami-recorder/gui.js"></script>';
  146. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_PATH).'swfobject/swfobject.js"></script>';
  147. $htmlHeadXtra[] = api_get_js('record_audio/record_audio.js');
  148. }
  149. if ($action != 'export') {
  150. $scoreJsCode = ExerciseLib::getJsCode();
  151. if ($origin != 'learnpath') {
  152. Display::display_header('');
  153. } else {
  154. $htmlHeadXtra[] = "<style>body { background: none; } </style>";
  155. Display::display_reduced_header();
  156. }
  157. echo Display::toolbarAction('toolbar', [
  158. Display::url(
  159. Display::return_icon('pdf.png', get_lang('Export')),
  160. api_get_self().'?'.api_get_cidreq().'&id='.$id.'&action=export&'
  161. ),
  162. ]); ?>
  163. <script>
  164. <?php echo $scoreJsCode; ?>
  165. var maxEditors = <?php echo $maxEditors; ?>;
  166. function showfck(sid, marksid) {
  167. $('#' + sid).toggleClass('hidden');
  168. $('#' + marksid).toggleClass('hidden');
  169. $('#feedback_' + sid).toggleClass('hidden', !$('#' + sid).is('.hidden'));
  170. }
  171. function openEmailWrapper() {
  172. $('#email_content_wrapper').toggle();
  173. }
  174. function getFCK(vals, marksid) {
  175. var f = document.getElementById('form-email');
  176. var m_id = marksid.split(',');
  177. for (var i = 0; i < m_id.length; i++) {
  178. var oHidn = document.createElement("input");
  179. oHidn.type = "hidden";
  180. var selname = oHidn.name = "marks_" + m_id[i];
  181. var elMarks = document.forms['marksform_' + m_id[i]].marks;
  182. if (elMarks.tagName.toLowerCase() === 'select') {
  183. var selid = elMarks.selectedIndex;
  184. oHidn.value = elMarks.options[selid].value;
  185. } else if (elMarks.tagName.toLowerCase() === 'input') {
  186. oHidn.value = elMarks.value;
  187. }
  188. f.appendChild(oHidn);
  189. }
  190. var ids = vals.split(',');
  191. for (var k = 0; k < ids.length; k++) {
  192. var oHidden = document.createElement("input");
  193. oHidden.type = "hidden";
  194. oHidden.name = "comments_" + ids[k];
  195. if (CKEDITOR.instances[oHidden.name]) {
  196. oHidden.value = CKEDITOR.instances[oHidden.name].getData();
  197. } else {
  198. oHidden.value = $("textarea[name='" + oHidden.name + "']").val();
  199. }
  200. f.appendChild(oHidden);
  201. }
  202. }
  203. </script>
  204. <?php
  205. }
  206. $show_results = true;
  207. $show_only_total_score = false;
  208. $showTotalScoreAndUserChoicesInLastAttempt = true;
  209. // Avoiding the "Score 0/0" message when the exe_id is not set
  210. if (!empty($track_exercise_info)) {
  211. // if the results_disabled of the Quiz is 1 when block the script
  212. $result_disabled = $track_exercise_info['results_disabled'];
  213. switch ($result_disabled) {
  214. case RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS:
  215. $show_results = false;
  216. break;
  217. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  218. $show_results = false;
  219. $show_only_total_score = true;
  220. if ($origin != 'learnpath') {
  221. if ($currentUserId == $student_id) {
  222. echo Display::return_message(
  223. get_lang('ThankYouForPassingTheTest'),
  224. 'warning',
  225. false
  226. );
  227. }
  228. }
  229. break;
  230. case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
  231. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  232. $attempts = Event::getExerciseResultsByUser(
  233. $currentUserId,
  234. $objExercise->id,
  235. api_get_course_int_id(),
  236. api_get_session_id(),
  237. $track_exercise_info['orig_lp_id'],
  238. $track_exercise_info['orig_lp_item_id'],
  239. 'desc'
  240. );
  241. $numberAttempts = count($attempts);
  242. if ($numberAttempts >= $track_exercise_info['max_attempt']) {
  243. $show_results = true;
  244. $show_only_total_score = true;
  245. // Attempt reach max so show score/feedback now
  246. $showTotalScoreAndUserChoicesInLastAttempt = true;
  247. } else {
  248. $show_results = true;
  249. $show_only_total_score = true;
  250. // Last attempt not reach don't show score/feedback
  251. $showTotalScoreAndUserChoicesInLastAttempt = false;
  252. }
  253. break;
  254. }
  255. } else {
  256. echo Display::return_message(get_lang('CantViewResults'), 'warning');
  257. $show_results = false;
  258. }
  259. if ($origin == 'learnpath' && !isset($_GET['fb_type'])) {
  260. $show_results = false;
  261. }
  262. if ($is_allowedToEdit && in_array($action, ['qualify', 'edit', 'export'])) {
  263. $show_results = true;
  264. }
  265. if ($action == 'export') {
  266. ob_start();
  267. }
  268. $user_info = api_get_user_info($student_id);
  269. if ($show_results || $show_only_total_score || $showTotalScoreAndUserChoicesInLastAttempt) {
  270. // Shows exercise header
  271. echo $objExercise->showExerciseResultHeader(
  272. $user_info,
  273. $track_exercise_info
  274. );
  275. }
  276. $i = $totalScore = $totalWeighting = 0;
  277. $arrques = [];
  278. $arrans = [];
  279. $user_restriction = $is_allowedToEdit ? '' : " AND user_id= $student_id ";
  280. $sql = "SELECT attempts.question_id, answer
  281. FROM $TBL_TRACK_ATTEMPT as attempts
  282. INNER JOIN $TBL_TRACK_EXERCISES AS stats_exercises
  283. ON stats_exercises.exe_id = attempts.exe_id
  284. INNER JOIN $TBL_EXERCISE_QUESTION AS quizz_rel_questions
  285. ON
  286. quizz_rel_questions.exercice_id=stats_exercises.exe_exo_id AND
  287. quizz_rel_questions.question_id = attempts.question_id AND
  288. quizz_rel_questions.c_id=".api_get_course_int_id()."
  289. INNER JOIN $TBL_QUESTIONS AS questions
  290. ON
  291. questions.id = quizz_rel_questions.question_id AND
  292. questions.c_id = ".api_get_course_int_id()."
  293. WHERE
  294. attempts.exe_id = $id $user_restriction
  295. GROUP BY quizz_rel_questions.question_order, attempts.question_id";
  296. $result = Database::query($sql);
  297. $question_list_from_database = [];
  298. $exerciseResult = [];
  299. while ($row = Database::fetch_array($result)) {
  300. $question_list_from_database[] = $row['question_id'];
  301. $exerciseResult[$row['question_id']] = $row['answer'];
  302. }
  303. // Fixing #2073 Fixing order of questions
  304. if (!empty($track_exercise_info['data_tracking'])) {
  305. $temp_question_list = explode(',', $track_exercise_info['data_tracking']);
  306. // Getting question list from data_tracking
  307. if (!empty($temp_question_list)) {
  308. $questionList = $temp_question_list;
  309. }
  310. // If for some reason data_tracking is empty we select the question list from db
  311. if (empty($questionList)) {
  312. $questionList = $question_list_from_database;
  313. }
  314. } else {
  315. $questionList = $question_list_from_database;
  316. }
  317. // Display the text when finished message if we are on a LP #4227
  318. $end_of_message = $objExercise->getTextWhenFinished();
  319. if (!empty($end_of_message) && ($origin === 'learnpath')) {
  320. echo Display::return_message($end_of_message, 'normal', false);
  321. echo "<div class='clear'>&nbsp;</div>";
  322. }
  323. // for each question
  324. $total_weighting = 0;
  325. foreach ($questionList as $questionId) {
  326. $objQuestionTmp = Question::read($questionId);
  327. if ($objQuestionTmp) {
  328. $total_weighting += $objQuestionTmp->selectWeighting();
  329. }
  330. }
  331. $counter = 1;
  332. $exercise_content = '';
  333. $category_list = [];
  334. $useAdvancedEditor = true;
  335. if (!empty($maxEditors) && count($questionList) > $maxEditors) {
  336. $useAdvancedEditor = false;
  337. }
  338. $objExercise->export = $action === 'export';
  339. $arrid = [];
  340. $arrmarks = [];
  341. $strids = '';
  342. $marksid = '';
  343. $countPendingQuestions = 0;
  344. foreach ($questionList as $questionId) {
  345. $choice = isset($exerciseResult[$questionId]) ? $exerciseResult[$questionId] : '';
  346. // destruction of the Question object
  347. unset($objQuestionTmp);
  348. $questionWeighting = 0;
  349. $answerType = 0;
  350. $questionScore = 0;
  351. // Creates a temporary Question object
  352. $objQuestionTmp = Question::read($questionId);
  353. if (empty($objQuestionTmp)) {
  354. continue;
  355. }
  356. $questionWeighting = $objQuestionTmp->selectWeighting();
  357. $answerType = $objQuestionTmp->selectType();
  358. // Start buffer
  359. ob_start();
  360. if ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  361. $choice = [];
  362. }
  363. $relPath = api_get_path(WEB_CODE_PATH);
  364. switch ($answerType) {
  365. case MULTIPLE_ANSWER_COMBINATION:
  366. case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
  367. case UNIQUE_ANSWER:
  368. case UNIQUE_ANSWER_NO_OPTION:
  369. case UNIQUE_ANSWER_IMAGE:
  370. case MULTIPLE_ANSWER:
  371. case MULTIPLE_ANSWER_TRUE_FALSE:
  372. case FILL_IN_BLANKS:
  373. case CALCULATED_ANSWER:
  374. case GLOBAL_MULTIPLE_ANSWER:
  375. case FREE_ANSWER:
  376. case ORAL_EXPRESSION:
  377. case MATCHING:
  378. case DRAGGABLE:
  379. case READING_COMPREHENSION:
  380. case MATCHING_DRAGGABLE:
  381. $question_result = $objExercise->manage_answer(
  382. $id,
  383. $questionId,
  384. $choice,
  385. 'exercise_show',
  386. [],
  387. false,
  388. true,
  389. $show_results,
  390. $objExercise->selectPropagateNeg(),
  391. [],
  392. $showTotalScoreAndUserChoicesInLastAttempt
  393. );
  394. $questionScore = $question_result['score'];
  395. $totalScore += $question_result['score'];
  396. break;
  397. case MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY:
  398. $choiceTmp = [];
  399. $choiceTmp['choice'] = $choice;
  400. $choiceTmp['choiceDegreeCertainty'] = $choiceDegreeCertainty;
  401. $questionResult = $objExercise->manage_answer(
  402. $id,
  403. $questionId,
  404. $choiceTmp,
  405. 'exercise_show',
  406. [],
  407. false,
  408. true,
  409. $show_results,
  410. $objExercise->selectPropagateNeg()
  411. );
  412. $questionScore = $questionResult['score'];
  413. $totalScore += $questionResult['score'];
  414. break;
  415. case HOT_SPOT:
  416. if ($show_results || $showTotalScoreAndUserChoicesInLastAttempt) {
  417. echo '<table width="500" border="0"><tr>
  418. <td valign="top" align="center" style="padding-left:0px;" >
  419. <table border="1" bordercolor="#A4A4A4" style="border-collapse: collapse;" width="552">';
  420. }
  421. $question_result = $objExercise->manage_answer(
  422. $id,
  423. $questionId,
  424. $choice,
  425. 'exercise_show',
  426. [],
  427. false,
  428. true,
  429. $show_results,
  430. $objExercise->selectPropagateNeg(),
  431. [],
  432. $showTotalScoreAndUserChoicesInLastAttempt
  433. );
  434. $questionScore = $question_result['score'];
  435. $totalScore += $question_result['score'];
  436. if ($show_results) {
  437. echo '</table></td></tr>';
  438. echo "
  439. <tr>
  440. <td colspan=\"2\">
  441. <div id=\"hotspot-solution-$questionId-$id\"></div>
  442. <script>
  443. $(function() {
  444. new HotspotQuestion({
  445. questionId: $questionId,
  446. exerciseId: {$objExercise->id},
  447. exeId: $id,
  448. selector: '#hotspot-solution-$questionId-$id',
  449. for: 'solution',
  450. relPath: '$relPath'
  451. });
  452. });
  453. </script>
  454. </td>
  455. </tr>
  456. </table>
  457. <br>
  458. ";
  459. }
  460. break;
  461. case HOT_SPOT_DELINEATION:
  462. $question_result = $objExercise->manage_answer(
  463. $id,
  464. $questionId,
  465. $choice,
  466. 'exercise_show',
  467. [],
  468. false,
  469. true,
  470. $show_results,
  471. $objExercise->selectPropagateNeg(),
  472. 'database',
  473. [],
  474. $showTotalScoreAndUserChoicesInLastAttempt
  475. );
  476. $questionScore = $question_result['score'];
  477. $totalScore += $question_result['score'];
  478. //$organs_at_risk_hit
  479. echo $objExercise->getDelineationResult($objQuestionTmp, $questionId, $show_results, $question_result);
  480. break;
  481. case ANNOTATION:
  482. $question_result = $objExercise->manage_answer(
  483. $id,
  484. $questionId,
  485. $choice,
  486. 'exercise_show',
  487. [],
  488. false,
  489. true,
  490. $show_results,
  491. $objExercise->selectPropagateNeg(),
  492. [],
  493. $showTotalScoreAndUserChoicesInLastAttempt
  494. );
  495. $questionScore = $question_result['score'];
  496. $totalScore += $question_result['score'];
  497. if ($show_results) {
  498. echo '
  499. <div id="annotation-canvas-'.$questionId.'"></div>
  500. <script>
  501. AnnotationQuestion({
  502. questionId: '.(int) $questionId.',
  503. exerciseId: '.(int) $id.',
  504. relPath: \''.$relPath.'\',
  505. courseId: '.(int) $courseInfo['real_id'].'
  506. });
  507. </script>
  508. ';
  509. }
  510. break;
  511. }
  512. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  513. echo '</table>';
  514. }
  515. if ($show_results && $answerType != HOT_SPOT) {
  516. echo '</table>';
  517. }
  518. $comnt = null;
  519. if ($show_results) {
  520. if ($is_allowedToEdit && $locked == false && !api_is_drh() && $isCoachAllowedToEdit) {
  521. $isFeedbackAllowed = true;
  522. } elseif (!$isCoachAllowedToEdit && $allowCoachFeedbackExercises) {
  523. $isFeedbackAllowed = true;
  524. }
  525. // Boss cannot edit exercise result
  526. if ($isBossOfStudent) {
  527. $isFeedbackAllowed = false;
  528. }
  529. $marksname = '';
  530. if ($isFeedbackAllowed && $action != 'export') {
  531. $name = 'fckdiv'.$questionId;
  532. $marksname = 'marksName'.$questionId;
  533. if (in_array($answerType, [FREE_ANSWER, ORAL_EXPRESSION, ANNOTATION])) {
  534. $url_name = get_lang('EditCommentsAndMarks');
  535. } else {
  536. $url_name = get_lang('AddComments');
  537. if ($action === 'edit') {
  538. $url_name = get_lang('EditIndividualComment');
  539. }
  540. }
  541. echo '<p>';
  542. echo Display::button(
  543. 'show_ck',
  544. $url_name,
  545. [
  546. 'type' => 'button',
  547. 'class' => 'btn btn-default',
  548. 'onclick' => "showfck('".$name."', '".$marksname."');",
  549. ]
  550. );
  551. echo '</p>';
  552. echo '<div id="feedback_'.$name.'" class="show">';
  553. $comnt = Event::get_comments($id, $questionId);
  554. if (!empty($comnt)) {
  555. echo ExerciseLib::getFeedbackText($comnt);
  556. }
  557. echo ExerciseLib::getOralFeedbackAudio($id, $questionId, $student_id);
  558. echo '</div>';
  559. echo '<div id="'.$name.'" class="row hidden">';
  560. echo '<div class="col-sm-'.($allowTeacherCommentAudio ? 7 : 12).'">';
  561. $arrid[] = $questionId;
  562. $feedback_form = new FormValidator('frmcomments'.$questionId);
  563. $renderer = &$feedback_form->defaultRenderer();
  564. $renderer->setFormTemplate('<form{attributes}><div>{content}</div></form>');
  565. $renderer->setCustomElementTemplate('<div>{element}</div>');
  566. $comnt = Event::get_comments($id, $questionId);
  567. $textareaId = 'comments_'.$questionId;
  568. $default = [$textareaId => $comnt];
  569. if ($useAdvancedEditor) {
  570. $feedback_form->addElement(
  571. 'html_editor',
  572. $textareaId,
  573. null,
  574. ['id' => $textareaId],
  575. [
  576. 'ToolbarSet' => 'TestAnswerFeedback',
  577. 'Width' => '100%',
  578. 'Height' => '120',
  579. ]
  580. );
  581. } else {
  582. $feedback_form->addElement('textarea', $textareaId, ['id' => $textareaId]);
  583. }
  584. $feedback_form->setDefaults($default);
  585. $feedback_form->display();
  586. echo '</div>';
  587. if ($allowRecordAudio && $allowTeacherCommentAudio) {
  588. echo '<div class="col-sm-5">';
  589. echo ExerciseLib::getOralFeedbackForm($id, $questionId, $student_id);
  590. echo '</div>';
  591. }
  592. echo '</div>';
  593. } else {
  594. $comnt = Event::get_comments($id, $questionId);
  595. echo '<br />';
  596. if (!empty($comnt)) {
  597. echo '<b>'.get_lang('Feedback').'</b>';
  598. echo ExerciseLib::getFeedbackText($comnt);
  599. echo ExerciseLib::getOralFeedbackAudio($id, $questionId, $student_id);
  600. }
  601. }
  602. if ($is_allowedToEdit && $isFeedbackAllowed && $action != 'export') {
  603. if (in_array($answerType, [FREE_ANSWER, ORAL_EXPRESSION, ANNOTATION])) {
  604. $marksname = 'marksName'.$questionId;
  605. $arrmarks[] = $questionId;
  606. echo '<div id="'.$marksname.'" class="hidden">';
  607. $allowDecimalScore = api_get_configuration_value('quiz_open_question_decimal_score');
  608. $formMark = new FormValidator('marksform_'.$questionId, 'post');
  609. $formMark->addHeader(get_lang('AssignMarks'));
  610. $model = ExerciseLib::getCourseScoreModel();
  611. if ($allowDecimalScore && empty($model)) {
  612. $formMark->addElement(
  613. 'number',
  614. 'marks',
  615. get_lang('AssignMarks'),
  616. [
  617. 'step' => 0.01,
  618. 'min' => 0,
  619. 'max' => $questionWeighting,
  620. 'placeholder' => 0,
  621. 'class' => 'grade_select',
  622. 'id' => "select_marks_$questionId",
  623. ]
  624. );
  625. $formMark->setDefaults(['marks' => $questionScore]);
  626. $formMark->applyFilter('marks', 'stripslashes');
  627. $formMark->applyFilter('marks', 'trim');
  628. $formMark->applyFilter('marks', 'floatval');
  629. $formMark->addRule('marks', get_lang('Numeric'), 'numeric');
  630. $formMark->addRule('marks', get_lang('ValueTooSmall'), 'min_numeric_length', 0);
  631. $formMark->addRule('marks', get_lang('ValueTooBig'), 'max_numeric_length', $questionWeighting);
  632. } else {
  633. $select = $formMark->addSelect(
  634. 'marks',
  635. get_lang('AssignMarks'),
  636. [],
  637. ['disable_js' => true, 'extra_class' => 'grade_select']
  638. );
  639. if (empty($model)) {
  640. for ($i = 0; $i <= $questionWeighting; $i++) {
  641. $attributes = [];
  642. if ($questionScore == $i) {
  643. $attributes['selected'] = 'selected';
  644. }
  645. $select->addOption($i, $i, $attributes);
  646. }
  647. } else {
  648. foreach ($model['score_list'] as $item) {
  649. $i = api_number_format($item['score_to_qualify'] / 100 * $questionWeighting, 2);
  650. $model = ExerciseLib::getModelStyle($item, $i);
  651. $attributes = ['class' => $item['css_class']];
  652. if ($questionScore == $i) {
  653. $attributes['selected'] = 'selected';
  654. }
  655. $select->addOption($model, $i, $attributes);
  656. }
  657. $select->updateSelectWithSelectedOption($formMark);
  658. }
  659. }
  660. $formMark->display();
  661. echo '</div>';
  662. if ($questionScore == -1) {
  663. $questionScore = 0;
  664. echo ExerciseLib::getNotCorrectedYetText();
  665. }
  666. } else {
  667. $arrmarks[] = $questionId;
  668. echo '
  669. <div id="'.$marksname.'" class="hidden">
  670. <form name="marksform_'.$questionId.'" method="post" action="">
  671. <select name="marks" id="select_marks_'.$questionId.'" style="display:none;" class="exercise_mark_select">
  672. <option value="'.$questionScore.'" >'.$questionScore.'</option>
  673. </select>
  674. </form>
  675. <br/>
  676. </div>
  677. ';
  678. }
  679. } else {
  680. if ($questionScore == -1) {
  681. $questionScore = 0;
  682. }
  683. }
  684. }
  685. $my_total_score = $questionScore;
  686. $my_total_weight = $questionWeighting;
  687. $totalWeighting += $questionWeighting;
  688. $category_was_added_for_this_test = false;
  689. if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
  690. if (!isset($category_list[$objQuestionTmp->category]['score'])) {
  691. $category_list[$objQuestionTmp->category]['score'] = 0;
  692. }
  693. if (!isset($category_list[$objQuestionTmp->category]['total'])) {
  694. $category_list[$objQuestionTmp->category]['total'] = 0;
  695. }
  696. $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
  697. $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
  698. $category_was_added_for_this_test = true;
  699. }
  700. if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
  701. foreach ($objQuestionTmp->category_list as $category_id) {
  702. $category_list[$category_id]['score'] += $my_total_score;
  703. $category_list[$category_id]['total'] += $my_total_weight;
  704. $category_was_added_for_this_test = true;
  705. }
  706. }
  707. // No category for this question!
  708. if (!isset($category_list['none']['score'])) {
  709. $category_list['none']['score'] = 0;
  710. }
  711. if (!isset($category_list['none']['total'])) {
  712. $category_list['none']['total'] = 0;
  713. }
  714. if ($category_was_added_for_this_test == false) {
  715. $category_list['none']['score'] += $my_total_score;
  716. $category_list['none']['total'] += $my_total_weight;
  717. }
  718. if ($objExercise->selectPropagateNeg() == 0 && $my_total_score < 0) {
  719. $my_total_score = 0;
  720. }
  721. $score = [];
  722. if ($show_results) {
  723. $scorePassed = $my_total_score >= $my_total_weight;
  724. if (function_exists('bccomp')) {
  725. $compareResult = bccomp($my_total_score, $my_total_weight, 3);
  726. $scorePassed = $compareResult === 1 || $compareResult === 0;
  727. }
  728. $score['result'] = ExerciseLib::show_score(
  729. $my_total_score,
  730. $my_total_weight,
  731. false,
  732. false
  733. );
  734. $score['pass'] = $scorePassed;
  735. $score['type'] = $answerType;
  736. $score['score'] = $my_total_score;
  737. $score['weight'] = $my_total_weight;
  738. $score['comments'] = isset($comnt) ? $comnt : null;
  739. if (isset($question_result['user_answered'])) {
  740. $score['user_answered'] = $question_result['user_answered'];
  741. }
  742. }
  743. if (in_array($objQuestionTmp->type, [FREE_ANSWER, ORAL_EXPRESSION, ANNOTATION])) {
  744. $scoreToReview = [
  745. 'score' => $my_total_score,
  746. 'comments' => isset($comnt) ? $comnt : null,
  747. ];
  748. $check = $objQuestionTmp->isQuestionWaitingReview($scoreToReview);
  749. if ($check === false) {
  750. $countPendingQuestions++;
  751. }
  752. }
  753. unset($objAnswerTmp);
  754. $i++;
  755. $contents = ob_get_clean();
  756. $question_content = '<div class="question_row">';
  757. if ($show_results && $objQuestionTmp) {
  758. $objQuestionTmp->export = $action == 'export';
  759. // Shows question title an description
  760. $question_content .= $objQuestionTmp->return_header(
  761. $objExercise,
  762. $counter,
  763. $score
  764. );
  765. }
  766. $counter++;
  767. $question_content .= $contents;
  768. $question_content .= '</div>';
  769. $exercise_content .= Display::panel($question_content);
  770. } // end of large foreach on questions
  771. $totalScoreText = '';
  772. if ($answerType != MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  773. $pluginEvaluation = QuestionOptionsEvaluationPlugin::create();
  774. if ('true' === $pluginEvaluation->get(QuestionOptionsEvaluationPlugin::SETTING_ENABLE)) {
  775. $formula = $pluginEvaluation->getFormulaForExercise($objExercise->selectId());
  776. if (!empty($formula)) {
  777. $totalScore = $pluginEvaluation->getResultWithFormula($id, $formula);
  778. $totalWeighting = $pluginEvaluation->getMaxScore();
  779. }
  780. }
  781. }
  782. // Total score
  783. $myTotalScoreTemp = $totalScore;
  784. if ($origin != 'learnpath' || ($origin == 'learnpath' && isset($_GET['fb_type']))) {
  785. if ($show_results || $show_only_total_score || $showTotalScoreAndUserChoicesInLastAttempt) {
  786. $totalScoreText .= '<div class="question_row">';
  787. if ($objExercise->selectPropagateNeg() == 0 && $myTotalScoreTemp < 0) {
  788. $myTotalScoreTemp = 0;
  789. }
  790. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  791. $totalScoreText .= ExerciseLib::getQuestionDiagnosisRibbon(
  792. $objExercise,
  793. $myTotalScoreTemp,
  794. $totalWeighting,
  795. true
  796. );
  797. } else {
  798. $totalScoreText .= ExerciseLib::getTotalScoreRibbon(
  799. $objExercise,
  800. $myTotalScoreTemp,
  801. $totalWeighting,
  802. true,
  803. $countPendingQuestions
  804. );
  805. }
  806. $totalScoreText .= '</div>';
  807. }
  808. }
  809. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  810. $chartMultiAnswer = MultipleAnswerTrueFalseDegreeCertainty::displayStudentsChartResults($id, $objExercise);
  811. echo $chartMultiAnswer;
  812. }
  813. if (!empty($category_list) && ($show_results || $show_only_total_score || $showTotalScoreAndUserChoicesInLastAttempt)) {
  814. // Adding total
  815. $category_list['total'] = [
  816. 'score' => $myTotalScoreTemp,
  817. 'total' => $totalWeighting,
  818. ];
  819. echo TestCategory::get_stats_table_by_attempt(
  820. $objExercise->id,
  821. $category_list
  822. );
  823. }
  824. if (in_array(
  825. $track_exercise_info['results_disabled'],
  826. [RESULT_DISABLE_RANKING, RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING]
  827. )) {
  828. echo Display::page_header(get_lang('Ranking'), null, 'h4');
  829. echo ExerciseLib::displayResultsInRanking(
  830. $objExercise->iId,
  831. $student_id,
  832. $courseInfo['real_id'],
  833. $sessionId
  834. );
  835. }
  836. echo $totalScoreText;
  837. echo $exercise_content;
  838. // only show "score" in bottom of page if there's exercise content
  839. if ($show_results) {
  840. echo $totalScoreText;
  841. }
  842. if ($action === 'export') {
  843. $content = ob_get_clean();
  844. // needed in order to mpdf to work
  845. ob_clean();
  846. $params = [
  847. 'filename' => api_replace_dangerous_char(
  848. $objExercise->name.' '.
  849. $user_info['complete_name'].' '.
  850. api_get_local_time()
  851. ),
  852. 'course_code' => api_get_course_id(),
  853. 'session_info' => api_get_session_info(api_get_session_id()),
  854. 'course_info' => '',
  855. 'pdf_date' => '',
  856. 'show_real_course_teachers' => false,
  857. 'show_teacher_as_myself' => false,
  858. 'orientation' => 'P',
  859. ];
  860. $pdf = new PDF('A4', $params['orientation'], $params);
  861. $pdf->html_to_pdf_with_template($content, false, false, true);
  862. exit;
  863. }
  864. if ($isFeedbackAllowed) {
  865. if (is_array($arrid) && is_array($arrmarks)) {
  866. $strids = implode(',', $arrid);
  867. $marksid = implode(',', $arrmarks);
  868. }
  869. }
  870. if ($isFeedbackAllowed && $origin != 'learnpath' && $origin != 'student_progress') {
  871. if (in_array($origin, ['tracking_course', 'user_course', 'correct_exercise_in_lp'])) {
  872. $formUrl = api_get_path(WEB_CODE_PATH).'exercise/exercise_report.php?'.api_get_cidreq().'&';
  873. $formUrl .= http_build_query([
  874. 'exerciseId' => $exercise_id,
  875. 'filter' => 2,
  876. 'comments' => 'update',
  877. 'exeid' => $id,
  878. 'origin' => $origin,
  879. 'details' => 'true',
  880. 'course' => Security::remove_XSS($_GET['cidReq']),
  881. ]);
  882. $emailForm = new FormValidator('form-email', 'post', $formUrl, '', ['id' => 'form-email']);
  883. $emailForm->addHidden('lp_item_id', $learnpath_id);
  884. $emailForm->addHidden('lp_item_view_id', $lp_item_view_id);
  885. $emailForm->addHidden('student_id', $student_id);
  886. $emailForm->addHidden('total_score', $totalScore);
  887. $emailForm->addHidden('my_exe_exo_id', $exercise_id);
  888. } else {
  889. $formUrl = api_get_path(WEB_CODE_PATH).'exercise/exercise_report.php?'.api_get_cidreq().'&';
  890. $formUrl .= http_build_query([
  891. 'exerciseId' => $exercise_id,
  892. 'filter' => 1,
  893. 'comments' => 'update',
  894. 'exeid' => $id,
  895. ]);
  896. $emailForm = new FormValidator(
  897. 'form-email',
  898. 'post',
  899. $formUrl,
  900. '',
  901. ['id' => 'form-email']
  902. );
  903. }
  904. if ($objExercise->results_disabled != RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS) {
  905. $emailForm->addCheckBox(
  906. 'send_notification',
  907. get_lang('SendEmail'),
  908. get_lang('SendEmail'),
  909. ['onclick' => 'openEmailWrapper();']
  910. );
  911. $emailForm->addHtml('<div id="email_content_wrapper" style="display:none; margin-bottom: 20px;">');
  912. $emailForm->addHtmlEditor(
  913. 'notification_content',
  914. get_lang('Content'),
  915. false
  916. );
  917. $emailForm->addHtml('</div>');
  918. }
  919. if (empty($track_exercise_info['orig_lp_id']) || empty($track_exercise_info['orig_lp_item_id'])) {
  920. // Default url
  921. $url = api_get_path(WEB_CODE_PATH).'exercise/result.php?id='.$track_exercise_info['exe_id'].'&'.api_get_cidreq()
  922. .'&show_headers=1&id_session='.api_get_session_id();
  923. } else {
  924. $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?action=view&item_id='
  925. .$track_exercise_info['orig_lp_item_id'].'&lp_id='.$track_exercise_info['orig_lp_id'].'&'.api_get_cidreq()
  926. .'&id_session='.api_get_session_id();
  927. }
  928. Skill::addSkillsToUserForm(
  929. $emailForm,
  930. ITEM_TYPE_EXERCISE,
  931. $exercise_id,
  932. $student_id,
  933. $track_exercise_info['exe_id'],
  934. true
  935. );
  936. $content = ExerciseLib::getEmailNotification(
  937. $currentUserId,
  938. api_get_course_info(),
  939. $track_exercise_info['title'],
  940. $url
  941. );
  942. $emailForm->setDefaults(['notification_content' => $content]);
  943. $emailForm->addButtonSend(
  944. get_lang('CorrectTest'),
  945. 'submit',
  946. false,
  947. ['onclick' => "getFCK('$strids', '$marksid')"]
  948. );
  949. echo $emailForm->returnForm();
  950. }
  951. //Came from lpstats in a lp
  952. if ($origin == 'student_progress') {
  953. ?>
  954. <button type="button" class="back" onclick="window.history.go(-1);" value="<?php echo get_lang('Back'); ?>">
  955. <?php echo get_lang('Back'); ?>
  956. </button>
  957. <?php
  958. } elseif ($origin == 'myprogress') {
  959. ?>
  960. <button type="button" class="save"
  961. onclick="top.location.href='../auth/my_progress.php?course=<?php echo api_get_course_id(); ?>'"
  962. value="<?php echo get_lang('Finish'); ?>">
  963. <?php echo get_lang('Finish'); ?>
  964. </button>
  965. <?php
  966. }
  967. if ($origin != 'learnpath') {
  968. //we are not in learnpath tool
  969. Display::display_footer();
  970. } else {
  971. if (!isset($_GET['fb_type'])) {
  972. $lp_mode = Session::read('lp_mode');
  973. $url = '../lp/lp_controller.php?'.api_get_cidreq().'&';
  974. $url .= http_build_query([
  975. 'action' => 'view',
  976. 'lp_id' => $learnpath_id,
  977. 'lp_item_id' => $learnpath_item_id,
  978. 'exeId' => $id,
  979. 'fb_type' => $feedback_type,
  980. ]);
  981. $href = ($lp_mode == 'fullscreen')
  982. ? ' window.opener.location.href="'.$url.'" '
  983. : ' top.location.href="'.$url.'" ';
  984. echo '<script type="text/javascript">'.$href.'</script>';
  985. // Record the results in the learning path, using the SCORM interface (API)
  986. echo "<script>window.parent.API.void_save_asset('$totalScore', '$totalWeighting', 0, 'completed'); </script>";
  987. echo '</body></html>';
  988. } else {
  989. echo Display::return_message(
  990. get_lang('ExerciseFinished').' '.get_lang('ToContinueUseMenu'),
  991. 'normal'
  992. );
  993. echo '<br />';
  994. }
  995. }
  996. // Destroying the session
  997. Session::erase('questionList');
  998. unset($questionList);
  999. Session::erase('exerciseResult');
  1000. unset($exerciseResult);
  1001. Session::erase('calculatedAnswerId');