exercise_show.php 39 KB

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