exercise_show.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 selid = document.forms['marksform_' + m_id[i]].marks.selectedIndex;
  182. oHidn.value = document.forms['marksform_' + m_id[i]].marks.options[selid].value;
  183. f.appendChild(oHidn);
  184. }
  185. var ids = vals.split(',');
  186. for (var k = 0; k < ids.length; k++) {
  187. var oHidden = document.createElement("input");
  188. oHidden.type = "hidden";
  189. oHidden.name = "comments_" + ids[k];
  190. if (CKEDITOR.instances[oHidden.name]) {
  191. oHidden.value = CKEDITOR.instances[oHidden.name].getData();
  192. } else {
  193. oHidden.value = $("textarea[name='" + oHidden.name + "']").val();
  194. }
  195. f.appendChild(oHidden);
  196. }
  197. }
  198. </script>
  199. <?php
  200. }
  201. $show_results = true;
  202. $show_only_total_score = false;
  203. $showTotalScoreAndUserChoicesInLastAttempt = true;
  204. // Avoiding the "Score 0/0" message when the exe_id is not set
  205. if (!empty($track_exercise_info)) {
  206. // if the results_disabled of the Quiz is 1 when block the script
  207. $result_disabled = $track_exercise_info['results_disabled'];
  208. switch ($result_disabled) {
  209. case RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS:
  210. $show_results = false;
  211. break;
  212. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  213. $show_results = false;
  214. $show_only_total_score = true;
  215. if ($origin != 'learnpath') {
  216. if ($currentUserId == $student_id) {
  217. echo Display::return_message(
  218. get_lang('ThankYouForPassingTheTest'),
  219. 'warning',
  220. false
  221. );
  222. }
  223. }
  224. break;
  225. case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
  226. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  227. $attempts = Event::getExerciseResultsByUser(
  228. $currentUserId,
  229. $objExercise->id,
  230. api_get_course_int_id(),
  231. api_get_session_id(),
  232. $track_exercise_info['orig_lp_id'],
  233. $track_exercise_info['orig_lp_item_id'],
  234. 'desc'
  235. );
  236. $numberAttempts = count($attempts);
  237. if ($numberAttempts >= $track_exercise_info['max_attempt']) {
  238. $show_results = true;
  239. $show_only_total_score = true;
  240. // Attempt reach max so show score/feedback now
  241. $showTotalScoreAndUserChoicesInLastAttempt = true;
  242. } else {
  243. $show_results = true;
  244. $show_only_total_score = true;
  245. // Last attempt not reach don't show score/feedback
  246. $showTotalScoreAndUserChoicesInLastAttempt = false;
  247. }
  248. break;
  249. }
  250. } else {
  251. echo Display::return_message(get_lang('CantViewResults'), 'warning');
  252. $show_results = false;
  253. }
  254. if ($origin == 'learnpath' && !isset($_GET['fb_type'])) {
  255. $show_results = false;
  256. }
  257. if ($is_allowedToEdit && in_array($action, ['qualify', 'edit', 'export'])) {
  258. $show_results = true;
  259. }
  260. if ($action == 'export') {
  261. ob_start();
  262. }
  263. $user_info = api_get_user_info($student_id);
  264. if ($show_results || $show_only_total_score || $showTotalScoreAndUserChoicesInLastAttempt) {
  265. // Shows exercise header
  266. echo $objExercise->showExerciseResultHeader(
  267. $user_info,
  268. $track_exercise_info
  269. );
  270. }
  271. $i = $totalScore = $totalWeighting = 0;
  272. $arrques = [];
  273. $arrans = [];
  274. $user_restriction = $is_allowedToEdit ? '' : " AND user_id= $student_id ";
  275. $sql = "SELECT attempts.question_id, answer
  276. FROM $TBL_TRACK_ATTEMPT as attempts
  277. INNER JOIN $TBL_TRACK_EXERCISES AS stats_exercises
  278. ON stats_exercises.exe_id = attempts.exe_id
  279. INNER JOIN $TBL_EXERCISE_QUESTION AS quizz_rel_questions
  280. ON
  281. quizz_rel_questions.exercice_id=stats_exercises.exe_exo_id AND
  282. quizz_rel_questions.question_id = attempts.question_id AND
  283. quizz_rel_questions.c_id=".api_get_course_int_id()."
  284. INNER JOIN $TBL_QUESTIONS AS questions
  285. ON
  286. questions.id = quizz_rel_questions.question_id AND
  287. questions.c_id = ".api_get_course_int_id()."
  288. WHERE
  289. attempts.exe_id = $id $user_restriction
  290. GROUP BY quizz_rel_questions.question_order, attempts.question_id";
  291. $result = Database::query($sql);
  292. $question_list_from_database = [];
  293. $exerciseResult = [];
  294. while ($row = Database::fetch_array($result)) {
  295. $question_list_from_database[] = $row['question_id'];
  296. $exerciseResult[$row['question_id']] = $row['answer'];
  297. }
  298. // Fixing #2073 Fixing order of questions
  299. if (!empty($track_exercise_info['data_tracking'])) {
  300. $temp_question_list = explode(',', $track_exercise_info['data_tracking']);
  301. // Getting question list from data_tracking
  302. if (!empty($temp_question_list)) {
  303. $questionList = $temp_question_list;
  304. }
  305. // If for some reason data_tracking is empty we select the question list from db
  306. if (empty($questionList)) {
  307. $questionList = $question_list_from_database;
  308. }
  309. } else {
  310. $questionList = $question_list_from_database;
  311. }
  312. // Display the text when finished message if we are on a LP #4227
  313. $end_of_message = $objExercise->getTextWhenFinished();
  314. if (!empty($end_of_message) && ($origin === 'learnpath')) {
  315. echo Display::return_message($end_of_message, 'normal', false);
  316. echo "<div class='clear'>&nbsp;</div>";
  317. }
  318. // for each question
  319. $total_weighting = 0;
  320. foreach ($questionList as $questionId) {
  321. $objQuestionTmp = Question::read($questionId);
  322. if ($objQuestionTmp) {
  323. $total_weighting += $objQuestionTmp->selectWeighting();
  324. }
  325. }
  326. $counter = 1;
  327. $exercise_content = '';
  328. $category_list = [];
  329. $useAdvancedEditor = true;
  330. if (!empty($maxEditors) && count($questionList) > $maxEditors) {
  331. $useAdvancedEditor = false;
  332. }
  333. $objExercise->export = $action === 'export';
  334. $arrid = [];
  335. $arrmarks = [];
  336. $strids = '';
  337. $marksid = '';
  338. $countPendingQuestions = 0;
  339. foreach ($questionList as $questionId) {
  340. $choice = isset($exerciseResult[$questionId]) ? $exerciseResult[$questionId] : '';
  341. // destruction of the Question object
  342. unset($objQuestionTmp);
  343. $questionWeighting = 0;
  344. $answerType = 0;
  345. $questionScore = 0;
  346. // Creates a temporary Question object
  347. $objQuestionTmp = Question::read($questionId);
  348. if (empty($objQuestionTmp)) {
  349. continue;
  350. }
  351. $questionWeighting = $objQuestionTmp->selectWeighting();
  352. $answerType = $objQuestionTmp->selectType();
  353. // Start buffer
  354. ob_start();
  355. if ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  356. $choice = [];
  357. }
  358. $relPath = api_get_path(WEB_CODE_PATH);
  359. switch ($answerType) {
  360. case MULTIPLE_ANSWER_COMBINATION:
  361. case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
  362. case UNIQUE_ANSWER:
  363. case UNIQUE_ANSWER_NO_OPTION:
  364. case UNIQUE_ANSWER_IMAGE:
  365. case MULTIPLE_ANSWER:
  366. case MULTIPLE_ANSWER_TRUE_FALSE:
  367. case FILL_IN_BLANKS:
  368. case CALCULATED_ANSWER:
  369. case GLOBAL_MULTIPLE_ANSWER:
  370. case FREE_ANSWER:
  371. case ORAL_EXPRESSION:
  372. case MATCHING:
  373. case DRAGGABLE:
  374. case READING_COMPREHENSION:
  375. case MATCHING_DRAGGABLE:
  376. $question_result = $objExercise->manage_answer(
  377. $id,
  378. $questionId,
  379. $choice,
  380. 'exercise_show',
  381. [],
  382. false,
  383. true,
  384. $show_results,
  385. $objExercise->selectPropagateNeg(),
  386. [],
  387. $showTotalScoreAndUserChoicesInLastAttempt
  388. );
  389. $questionScore = $question_result['score'];
  390. $totalScore += $question_result['score'];
  391. break;
  392. case MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY:
  393. $choiceTmp = [];
  394. $choiceTmp['choice'] = $choice;
  395. $choiceTmp['choiceDegreeCertainty'] = $choiceDegreeCertainty;
  396. $questionResult = $objExercise->manage_answer(
  397. $id,
  398. $questionId,
  399. $choiceTmp,
  400. 'exercise_show',
  401. [],
  402. false,
  403. true,
  404. $show_results,
  405. $objExercise->selectPropagateNeg()
  406. );
  407. $questionScore = $questionResult['score'];
  408. $totalScore += $questionResult['score'];
  409. break;
  410. case HOT_SPOT:
  411. if ($show_results || $showTotalScoreAndUserChoicesInLastAttempt) {
  412. echo '<table width="500" border="0"><tr>
  413. <td valign="top" align="center" style="padding-left:0px;" >
  414. <table border="1" bordercolor="#A4A4A4" style="border-collapse: collapse;" width="552">';
  415. }
  416. $question_result = $objExercise->manage_answer(
  417. $id,
  418. $questionId,
  419. $choice,
  420. 'exercise_show',
  421. [],
  422. false,
  423. true,
  424. $show_results,
  425. $objExercise->selectPropagateNeg(),
  426. [],
  427. $showTotalScoreAndUserChoicesInLastAttempt
  428. );
  429. $questionScore = $question_result['score'];
  430. $totalScore += $question_result['score'];
  431. if ($show_results) {
  432. echo '</table></td></tr>';
  433. echo "
  434. <tr>
  435. <td colspan=\"2\">
  436. <div id=\"hotspot-solution-$questionId-$id\"></div>
  437. <script>
  438. $(function() {
  439. new HotspotQuestion({
  440. questionId: $questionId,
  441. exerciseId: {$objExercise->id},
  442. exeId: $id,
  443. selector: '#hotspot-solution-$questionId-$id',
  444. for: 'solution',
  445. relPath: '$relPath'
  446. });
  447. });
  448. </script>
  449. </td>
  450. </tr>
  451. </table>
  452. <br>
  453. ";
  454. }
  455. break;
  456. case HOT_SPOT_DELINEATION:
  457. $question_result = $objExercise->manage_answer(
  458. $id,
  459. $questionId,
  460. $choice,
  461. 'exercise_show',
  462. [],
  463. false,
  464. true,
  465. $show_results,
  466. $objExercise->selectPropagateNeg(),
  467. 'database',
  468. [],
  469. $showTotalScoreAndUserChoicesInLastAttempt
  470. );
  471. $questionScore = $question_result['score'];
  472. $totalScore += $question_result['score'];
  473. //$organs_at_risk_hit
  474. echo $objExercise->getDelineationResult($objQuestionTmp, $questionId, $show_results, $question_result);
  475. break;
  476. case ANNOTATION:
  477. $question_result = $objExercise->manage_answer(
  478. $id,
  479. $questionId,
  480. $choice,
  481. 'exercise_show',
  482. [],
  483. false,
  484. true,
  485. $show_results,
  486. $objExercise->selectPropagateNeg(),
  487. [],
  488. $showTotalScoreAndUserChoicesInLastAttempt
  489. );
  490. $questionScore = $question_result['score'];
  491. $totalScore += $question_result['score'];
  492. if ($show_results) {
  493. echo '
  494. <div id="annotation-canvas-'.$questionId.'"></div>
  495. <script>
  496. AnnotationQuestion({
  497. questionId: '.(int) $questionId.',
  498. exerciseId: '.(int) $id.',
  499. relPath: \''.$relPath.'\',
  500. courseId: '.(int) $courseInfo['real_id'].'
  501. });
  502. </script>
  503. ';
  504. }
  505. break;
  506. }
  507. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  508. echo '</table>';
  509. }
  510. if ($show_results && $answerType != HOT_SPOT) {
  511. echo '</table>';
  512. }
  513. $comnt = null;
  514. if ($show_results) {
  515. if ($is_allowedToEdit && $locked == false && !api_is_drh() && $isCoachAllowedToEdit) {
  516. $isFeedbackAllowed = true;
  517. } elseif (!$isCoachAllowedToEdit && $allowCoachFeedbackExercises) {
  518. $isFeedbackAllowed = true;
  519. }
  520. // Boss cannot edit exercise result
  521. if ($isBossOfStudent) {
  522. $isFeedbackAllowed = false;
  523. }
  524. $marksname = '';
  525. if ($isFeedbackAllowed && $action != 'export') {
  526. $name = 'fckdiv'.$questionId;
  527. $marksname = 'marksName'.$questionId;
  528. if (in_array($answerType, [FREE_ANSWER, ORAL_EXPRESSION, ANNOTATION])) {
  529. $url_name = get_lang('EditCommentsAndMarks');
  530. } else {
  531. $url_name = get_lang('AddComments');
  532. if ($action === 'edit') {
  533. $url_name = get_lang('EditIndividualComment');
  534. }
  535. }
  536. echo '<p>';
  537. echo Display::button(
  538. 'show_ck',
  539. $url_name,
  540. [
  541. 'type' => 'button',
  542. 'class' => 'btn btn-default',
  543. 'onclick' => "showfck('".$name."', '".$marksname."');",
  544. ]
  545. );
  546. echo '</p>';
  547. echo '<div id="feedback_'.$name.'" class="show">';
  548. $comnt = Event::get_comments($id, $questionId);
  549. if (!empty($comnt)) {
  550. echo ExerciseLib::getFeedbackText($comnt);
  551. }
  552. echo ExerciseLib::getOralFeedbackAudio($id, $questionId, $student_id);
  553. echo '</div>';
  554. echo '<div id="'.$name.'" class="row hidden">';
  555. echo '<div class="col-sm-'.($allowTeacherCommentAudio ? 7 : 12).'">';
  556. $arrid[] = $questionId;
  557. $feedback_form = new FormValidator('frmcomments'.$questionId);
  558. $renderer = &$feedback_form->defaultRenderer();
  559. $renderer->setFormTemplate('<form{attributes}><div>{content}</div></form>');
  560. $renderer->setCustomElementTemplate('<div>{element}</div>');
  561. $comnt = Event::get_comments($id, $questionId);
  562. $default = ['comments_'.$questionId => $comnt];
  563. if ($useAdvancedEditor) {
  564. $feedback_form->addElement(
  565. 'html_editor',
  566. 'comments_'.$questionId,
  567. null,
  568. null,
  569. [
  570. 'ToolbarSet' => 'TestAnswerFeedback',
  571. 'Width' => '100%',
  572. 'Height' => '120',
  573. ]
  574. );
  575. } else {
  576. $feedback_form->addElement('textarea', 'comments_'.$questionId);
  577. }
  578. $feedback_form->setDefaults($default);
  579. $feedback_form->display();
  580. echo '</div>';
  581. if ($allowRecordAudio && $allowTeacherCommentAudio) {
  582. echo '<div class="col-sm-5">';
  583. echo ExerciseLib::getOralFeedbackForm($id, $questionId, $student_id);
  584. echo '</div>';
  585. }
  586. echo '</div>';
  587. } else {
  588. $comnt = Event::get_comments($id, $questionId);
  589. echo '<br />';
  590. if (!empty($comnt)) {
  591. echo '<b>'.get_lang('Feedback').'</b>';
  592. echo ExerciseLib::getFeedbackText($comnt);
  593. echo ExerciseLib::getOralFeedbackAudio($id, $questionId, $student_id);
  594. }
  595. }
  596. if ($is_allowedToEdit && $isFeedbackAllowed && $action != 'export') {
  597. if (in_array($answerType, [FREE_ANSWER, ORAL_EXPRESSION, ANNOTATION])) {
  598. $marksname = 'marksName'.$questionId;
  599. $arrmarks[] = $questionId;
  600. echo '<div id="'.$marksname.'" class="hidden">';
  601. $formMark = new FormValidator('marksform_'.$questionId, 'post');
  602. $formMark->addHeader(get_lang('AssignMarks'));
  603. $select = $formMark->addSelect('marks', get_lang('AssignMarks'), [], ['disable_js' => true]);
  604. $model = ExerciseLib::getCourseScoreModel();
  605. if (empty($model)) {
  606. for ($i = 0; $i <= $questionWeighting; $i++) {
  607. $attributes = [];
  608. if ($questionScore == $i) {
  609. $attributes['selected'] = 'selected';
  610. }
  611. $select->addOption($i, $i, $attributes);
  612. }
  613. } else {
  614. foreach ($model['score_list'] as $item) {
  615. $i = api_number_format($item['score_to_qualify'] / 100 * $questionWeighting, 2);
  616. $model = ExerciseLib::getModelStyle($item, $i);
  617. $attributes = ['class' => $item['css_class']];
  618. if ($questionScore == $i) {
  619. $attributes['selected'] = 'selected';
  620. }
  621. $select->addOption($model, $i, $attributes);
  622. }
  623. $select->updateSelectWithSelectedOption($formMark);
  624. }
  625. $formMark->display();
  626. echo '</div>';
  627. if ($questionScore == -1) {
  628. $questionScore = 0;
  629. echo ExerciseLib::getNotCorrectedYetText();
  630. }
  631. } else {
  632. $arrmarks[] = $questionId;
  633. echo '
  634. <div id="'.$marksname.'" class="hidden">
  635. <form name="marksform_'.$questionId.'" method="post" action="">
  636. <select name="marks" id="select_marks_'.$questionId.'" style="display:none;" class="exercise_mark_select">
  637. <option value="'.$questionScore.'" >'.$questionScore.'</option>
  638. </select>
  639. </form>
  640. <br/>
  641. </div>
  642. ';
  643. }
  644. } else {
  645. if ($questionScore == -1) {
  646. $questionScore = 0;
  647. }
  648. }
  649. }
  650. $my_total_score = $questionScore;
  651. $my_total_weight = $questionWeighting;
  652. $totalWeighting += $questionWeighting;
  653. $category_was_added_for_this_test = false;
  654. if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
  655. if (!isset($category_list[$objQuestionTmp->category]['score'])) {
  656. $category_list[$objQuestionTmp->category]['score'] = 0;
  657. }
  658. if (!isset($category_list[$objQuestionTmp->category]['total'])) {
  659. $category_list[$objQuestionTmp->category]['total'] = 0;
  660. }
  661. $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
  662. $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
  663. $category_was_added_for_this_test = true;
  664. }
  665. if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
  666. foreach ($objQuestionTmp->category_list as $category_id) {
  667. $category_list[$category_id]['score'] += $my_total_score;
  668. $category_list[$category_id]['total'] += $my_total_weight;
  669. $category_was_added_for_this_test = true;
  670. }
  671. }
  672. // No category for this question!
  673. if (!isset($category_list['none']['score'])) {
  674. $category_list['none']['score'] = 0;
  675. }
  676. if (!isset($category_list['none']['total'])) {
  677. $category_list['none']['total'] = 0;
  678. }
  679. if ($category_was_added_for_this_test == false) {
  680. $category_list['none']['score'] += $my_total_score;
  681. $category_list['none']['total'] += $my_total_weight;
  682. }
  683. if ($objExercise->selectPropagateNeg() == 0 && $my_total_score < 0) {
  684. $my_total_score = 0;
  685. }
  686. $score = [];
  687. if ($show_results) {
  688. $scorePassed = $my_total_score >= $my_total_weight;
  689. if (function_exists('bccomp')) {
  690. $compareResult = bccomp($my_total_score, $my_total_weight, 3);
  691. $scorePassed = $compareResult === 1 || $compareResult === 0;
  692. }
  693. $score['result'] = ExerciseLib::show_score(
  694. $my_total_score,
  695. $my_total_weight,
  696. false,
  697. false
  698. );
  699. $score['pass'] = $scorePassed;
  700. $score['type'] = $answerType;
  701. $score['score'] = $my_total_score;
  702. $score['weight'] = $my_total_weight;
  703. $score['comments'] = isset($comnt) ? $comnt : null;
  704. if (isset($question_result['user_answered'])) {
  705. $score['user_answered'] = $question_result['user_answered'];
  706. }
  707. }
  708. if (in_array($objQuestionTmp->type, [FREE_ANSWER, ORAL_EXPRESSION, ANNOTATION])) {
  709. $scoreToReview = [
  710. 'score' => $my_total_score,
  711. 'comments' => isset($comnt) ? $comnt : null,
  712. ];
  713. $check = $objQuestionTmp->isQuestionWaitingReview($scoreToReview);
  714. if ($check === false) {
  715. $countPendingQuestions++;
  716. }
  717. }
  718. unset($objAnswerTmp);
  719. $i++;
  720. $contents = ob_get_clean();
  721. $question_content = '<div class="question_row">';
  722. if ($show_results && $objQuestionTmp) {
  723. $objQuestionTmp->export = $action == 'export';
  724. // Shows question title an description
  725. $question_content .= $objQuestionTmp->return_header(
  726. $objExercise,
  727. $counter,
  728. $score
  729. );
  730. }
  731. $counter++;
  732. $question_content .= $contents;
  733. $question_content .= '</div>';
  734. $exercise_content .= Display::panel($question_content);
  735. } // end of large foreach on questions
  736. $totalScoreText = '';
  737. if ($answerType != MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  738. $pluginEvaluation = QuestionOptionsEvaluationPlugin::create();
  739. if ('true' === $pluginEvaluation->get(QuestionOptionsEvaluationPlugin::SETTING_ENABLE)) {
  740. $formula = $pluginEvaluation->getFormulaForExercise($objExercise->selectId());
  741. if (!empty($formula)) {
  742. $totalScore = $pluginEvaluation->getResultWithFormula($id, $formula);
  743. $totalWeighting = $pluginEvaluation->getMaxScore();
  744. }
  745. }
  746. }
  747. // Total score
  748. $myTotalScoreTemp = $totalScore;
  749. if ($origin != 'learnpath' || ($origin == 'learnpath' && isset($_GET['fb_type']))) {
  750. if ($show_results || $show_only_total_score || $showTotalScoreAndUserChoicesInLastAttempt) {
  751. $totalScoreText .= '<div class="question_row">';
  752. if ($objExercise->selectPropagateNeg() == 0 && $myTotalScoreTemp < 0) {
  753. $myTotalScoreTemp = 0;
  754. }
  755. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  756. $totalScoreText .= ExerciseLib::getQuestionDiagnosisRibbon(
  757. $objExercise,
  758. $myTotalScoreTemp,
  759. $totalWeighting,
  760. true
  761. );
  762. } else {
  763. $totalScoreText .= ExerciseLib::getTotalScoreRibbon(
  764. $objExercise,
  765. $myTotalScoreTemp,
  766. $totalWeighting,
  767. true,
  768. $countPendingQuestions
  769. );
  770. }
  771. $totalScoreText .= '</div>';
  772. }
  773. }
  774. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  775. $chartMultiAnswer = MultipleAnswerTrueFalseDegreeCertainty::displayStudentsChartResults($id, $objExercise);
  776. echo $chartMultiAnswer;
  777. }
  778. if (!empty($category_list) && ($show_results || $show_only_total_score || $showTotalScoreAndUserChoicesInLastAttempt)) {
  779. // Adding total
  780. $category_list['total'] = [
  781. 'score' => $myTotalScoreTemp,
  782. 'total' => $totalWeighting,
  783. ];
  784. echo TestCategory::get_stats_table_by_attempt(
  785. $objExercise->id,
  786. $category_list
  787. );
  788. }
  789. if (in_array(
  790. $track_exercise_info['results_disabled'],
  791. [RESULT_DISABLE_RANKING, RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING]
  792. )) {
  793. echo Display::page_header(get_lang('Ranking'), null, 'h4');
  794. echo ExerciseLib::displayResultsInRanking(
  795. $objExercise->iId,
  796. $student_id,
  797. $courseInfo['real_id'],
  798. $sessionId
  799. );
  800. }
  801. echo $totalScoreText;
  802. echo $exercise_content;
  803. // only show "score" in bottom of page if there's exercise content
  804. if ($show_results) {
  805. echo $totalScoreText;
  806. }
  807. if ($action === 'export') {
  808. $content = ob_get_clean();
  809. // needed in order to mpdf to work
  810. ob_clean();
  811. $params = [
  812. 'filename' => api_replace_dangerous_char(
  813. $objExercise->name.' '.
  814. $user_info['complete_name'].' '.
  815. api_get_local_time()
  816. ),
  817. 'course_code' => api_get_course_id(),
  818. 'session_info' => api_get_session_info(api_get_session_id()),
  819. 'course_info' => '',
  820. 'pdf_date' => '',
  821. 'show_real_course_teachers' => false,
  822. 'show_teacher_as_myself' => false,
  823. 'orientation' => 'P',
  824. ];
  825. $pdf = new PDF('A4', $params['orientation'], $params);
  826. $pdf->html_to_pdf_with_template($content, false, false, true);
  827. exit;
  828. }
  829. if ($isFeedbackAllowed) {
  830. if (is_array($arrid) && is_array($arrmarks)) {
  831. $strids = implode(',', $arrid);
  832. $marksid = implode(',', $arrmarks);
  833. }
  834. }
  835. if ($isFeedbackAllowed && $origin != 'learnpath' && $origin != 'student_progress') {
  836. if (in_array($origin, ['tracking_course', 'user_course', 'correct_exercise_in_lp'])) {
  837. $formUrl = api_get_path(WEB_CODE_PATH).'exercise/exercise_report.php?'.api_get_cidreq().'&';
  838. $formUrl .= http_build_query([
  839. 'exerciseId' => $exercise_id,
  840. 'filter' => 2,
  841. 'comments' => 'update',
  842. 'exeid' => $id,
  843. 'origin' => $origin,
  844. 'details' => 'true',
  845. 'course' => Security::remove_XSS($_GET['cidReq']),
  846. ]);
  847. $emailForm = new FormValidator('form-email', 'post', $formUrl, '', ['id' => 'form-email']);
  848. $emailForm->addHidden('lp_item_id', $learnpath_id);
  849. $emailForm->addHidden('lp_item_view_id', $lp_item_view_id);
  850. $emailForm->addHidden('student_id', $student_id);
  851. $emailForm->addHidden('total_score', $totalScore);
  852. $emailForm->addHidden('my_exe_exo_id', $exercise_id);
  853. } else {
  854. $formUrl = api_get_path(WEB_CODE_PATH).'exercise/exercise_report.php?'.api_get_cidreq().'&';
  855. $formUrl .= http_build_query([
  856. 'exerciseId' => $exercise_id,
  857. 'filter' => 1,
  858. 'comments' => 'update',
  859. 'exeid' => $id,
  860. ]);
  861. $emailForm = new FormValidator(
  862. 'form-email',
  863. 'post',
  864. $formUrl,
  865. '',
  866. ['id' => 'form-email']
  867. );
  868. }
  869. if ($objExercise->results_disabled != RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS) {
  870. $emailForm->addCheckBox(
  871. 'send_notification',
  872. get_lang('SendEmail'),
  873. get_lang('SendEmail'),
  874. ['onclick' => 'openEmailWrapper();']
  875. );
  876. $emailForm->addHtml('<div id="email_content_wrapper" style="display:none; margin-bottom: 20px;">');
  877. $emailForm->addHtmlEditor(
  878. 'notification_content',
  879. get_lang('Content'),
  880. false
  881. );
  882. $emailForm->addHtml('</div>');
  883. }
  884. if (empty($track_exercise_info['orig_lp_id']) || empty($track_exercise_info['orig_lp_item_id'])) {
  885. // Default url
  886. $url = api_get_path(WEB_CODE_PATH).'exercise/result.php?id='.$track_exercise_info['exe_id'].'&'.api_get_cidreq()
  887. .'&show_headers=1&id_session='.api_get_session_id();
  888. } else {
  889. $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?action=view&item_id='
  890. .$track_exercise_info['orig_lp_item_id'].'&lp_id='.$track_exercise_info['orig_lp_id'].'&'.api_get_cidreq()
  891. .'&id_session='.api_get_session_id();
  892. }
  893. Skill::addSkillsToUserForm(
  894. $emailForm,
  895. ITEM_TYPE_EXERCISE,
  896. $exercise_id,
  897. $student_id,
  898. $track_exercise_info['exe_id'],
  899. true
  900. );
  901. $content = ExerciseLib::getEmailNotification(
  902. $currentUserId,
  903. api_get_course_info(),
  904. $track_exercise_info['title'],
  905. $url
  906. );
  907. $emailForm->setDefaults(['notification_content' => $content]);
  908. $emailForm->addButtonSend(
  909. get_lang('CorrectTest'),
  910. 'submit',
  911. false,
  912. ['onclick' => "getFCK('$strids', '$marksid')"]
  913. );
  914. echo $emailForm->returnForm();
  915. }
  916. //Came from lpstats in a lp
  917. if ($origin == 'student_progress') {
  918. ?>
  919. <button type="button" class="back" onclick="window.history.go(-1);" value="<?php echo get_lang('Back'); ?>">
  920. <?php echo get_lang('Back'); ?>
  921. </button>
  922. <?php
  923. } elseif ($origin == 'myprogress') {
  924. ?>
  925. <button type="button" class="save"
  926. onclick="top.location.href='../auth/my_progress.php?course=<?php echo api_get_course_id(); ?>'"
  927. value="<?php echo get_lang('Finish'); ?>">
  928. <?php echo get_lang('Finish'); ?>
  929. </button>
  930. <?php
  931. }
  932. if ($origin != 'learnpath') {
  933. //we are not in learnpath tool
  934. Display::display_footer();
  935. } else {
  936. if (!isset($_GET['fb_type'])) {
  937. $lp_mode = Session::read('lp_mode');
  938. $url = '../lp/lp_controller.php?'.api_get_cidreq().'&';
  939. $url .= http_build_query([
  940. 'action' => 'view',
  941. 'lp_id' => $learnpath_id,
  942. 'lp_item_id' => $learnpath_item_id,
  943. 'exeId' => $id,
  944. 'fb_type' => $feedback_type,
  945. ]);
  946. $href = ($lp_mode == 'fullscreen')
  947. ? ' window.opener.location.href="'.$url.'" '
  948. : ' top.location.href="'.$url.'" ';
  949. echo '<script type="text/javascript">'.$href.'</script>';
  950. // Record the results in the learning path, using the SCORM interface (API)
  951. echo "<script>window.parent.API.void_save_asset('$totalScore', '$totalWeighting', 0, 'completed'); </script>";
  952. echo '</body></html>';
  953. } else {
  954. echo Display::return_message(
  955. get_lang('ExerciseFinished').' '.get_lang('ToContinueUseMenu'),
  956. 'normal'
  957. );
  958. echo '<br />';
  959. }
  960. }
  961. // Destroying the session
  962. Session::erase('questionList');
  963. unset($questionList);
  964. Session::erase('exerciseResult');
  965. unset($exerciseResult);
  966. Session::erase('calculatedAnswerId');