MultipleAnswerTrueFalseDegreeCertainty.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class MultipleAnswerTrueFalseDegreeCertainty
  6. * This class allows to instantiate an object of type MULTIPLE_ANSWER
  7. * (MULTIPLE CHOICE, MULTIPLE ANSWER), extending the class question.
  8. *
  9. * @package chamilo.exercise
  10. */
  11. class MultipleAnswerTrueFalseDegreeCertainty extends Question
  12. {
  13. const LEVEL_DARKGREEN = 1;
  14. const LEVEL_LIGHTGREEN = 2;
  15. const LEVEL_WHITE = 3;
  16. const LEVEL_LIGHTRED = 4;
  17. const LEVEL_DARKRED = 5;
  18. public $typePicture = 'mccert.png';
  19. public $explanationLangVar = 'MultipleAnswerTrueFalseDegreeCertainty';
  20. public $optionsTitle;
  21. public $options;
  22. /**
  23. * Constructor.
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->type = MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY;
  29. $this->isContent = $this->getIsContent();
  30. $this->optionsTitle = [1 => 'Answers', 2 => 'DegreeOfCertaintyThatMyAnswerIsCorrect'];
  31. $this->options = [
  32. 1 => 'True',
  33. 2 => 'False',
  34. 3 => '50%',
  35. 4 => '60%',
  36. 5 => '70%',
  37. 6 => '80%',
  38. 7 => '90%',
  39. 8 => '100%',
  40. ];
  41. }
  42. /**
  43. * Redefines Question::createAnswersForm: creates the HTML form to answer the question.
  44. *
  45. * @uses \globals $text and $class, defined in the calling script
  46. *
  47. * @param FormValidator $form
  48. *
  49. * @throws Exception
  50. */
  51. public function createAnswersForm($form)
  52. {
  53. global $text;
  54. $nbAnswers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
  55. // The previous default value was 2. See task #1759.
  56. $nbAnswers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  57. $courseId = api_get_course_int_id();
  58. $objEx = Session::read('objExercise');
  59. $renderer = &$form->defaultRenderer();
  60. $defaults = [];
  61. $html = '<table class="data_table"><tr style="text-align: center;"><th>'
  62. .get_lang('N°')
  63. .'</th><th>'
  64. .get_lang('True')
  65. .'</th><th>'
  66. .get_lang('False')
  67. .'</th><th>'
  68. .get_lang('Answer')
  69. .'</th>';
  70. // show column comment when feedback is enable
  71. if ($objEx->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  72. $html .= '<th>'.get_lang('Comment').'</th>';
  73. }
  74. $html .= '</tr>';
  75. $form->addElement('label', get_lang('Answers').'<br /> <img src="../img/fill_field.png">', $html);
  76. $correct = 0;
  77. $answer = null;
  78. if (!empty($this->id)) {
  79. $answer = new Answer($this->id);
  80. $answer->read();
  81. if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
  82. $nbAnswers = $answer->nbrAnswers;
  83. }
  84. }
  85. $form->addElement('hidden', 'nb_answers');
  86. $boxesNames = [];
  87. if ($nbAnswers < 1) {
  88. $nbAnswers = 1;
  89. echo Display::return_message(get_lang('You have to create at least one answer'));
  90. }
  91. // Can be more options
  92. $optionData = Question::readQuestionOption($this->id, $courseId);
  93. for ($i = 1; $i <= $nbAnswers; $i++) {
  94. $renderer->setElementTemplate(
  95. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  96. 'correct['.$i.']'
  97. );
  98. $renderer->setElementTemplate(
  99. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  100. 'counter['.$i.']'
  101. );
  102. $renderer->setElementTemplate(
  103. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  104. 'answer['.$i.']'
  105. );
  106. $renderer->setElementTemplate(
  107. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  108. 'comment['.$i.']'
  109. );
  110. $answerNumber = $form->addElement('text', 'counter['.$i.']', null, 'value="'.$i.'"');
  111. $answerNumber->freeze();
  112. $defaults['answer['.$i.']'] = '';
  113. $defaults['comment['.$i.']'] = '';
  114. $defaults['correct['.$i.']'] = '';
  115. if (is_object($answer)) {
  116. $defaults['answer['.$i.']'] = isset($answer->answer[$i]) ? $answer->answer[$i] : '';
  117. if (isset($_POST['answer']) && isset($_POST['answer'][$i])) {
  118. $defaults['answer['.$i.']'] = Security::remove_XSS($_POST['answer'][$i]);
  119. }
  120. $defaults['comment['.$i.']'] = isset($answer->comment[$i]) ? $answer->comment[$i] : '';
  121. if (isset($_POST['comment']) && isset($_POST['comment'][$i])) {
  122. $defaults['comment['.$i.']'] = Security::remove_XSS($_POST['comment'][$i]);
  123. }
  124. $defaults['weighting['.$i.']'] = isset($answer->weighting[$i]) ? float_format($answer->weighting[$i], 1) : '';
  125. $correct = isset($answer->correct[$i]) ? $answer->correct[$i] : '';
  126. $defaults['correct['.$i.']'] = $correct;
  127. if (isset($_POST['correct']) && isset($_POST['correct'][$i])) {
  128. $defaults['correct['.$i.']'] = Security::remove_XSS($_POST['correct'][$i]);
  129. }
  130. $j = 1;
  131. if (!empty($optionData)) {
  132. foreach ($optionData as $id => $data) {
  133. $form->addElement('radio', 'correct['.$i.']', null, null, $id);
  134. $j++;
  135. if ($j == 3) {
  136. break;
  137. }
  138. }
  139. }
  140. } else {
  141. $form->addElement('radio', 'correct['.$i.']', null, null, 1);
  142. $form->addElement('radio', 'correct['.$i.']', null, null, 2);
  143. }
  144. $boxesNames[] = 'correct['.$i.']';
  145. $form->addElement(
  146. 'html_editor',
  147. 'answer['.$i.']',
  148. null,
  149. ['style' => 'vertical-align:middle;'],
  150. ['ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100']
  151. );
  152. $form->addRule('answer['.$i.']', get_lang('Required field'), 'required');
  153. // show comment when feedback is enable
  154. if ($objEx->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  155. $form->addElement(
  156. 'html_editor',
  157. 'comment['.$i.']',
  158. null,
  159. ['style' => 'vertical-align:middle;'],
  160. ['ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100']
  161. );
  162. }
  163. $form->addElement('html', '</tr>');
  164. }
  165. $form->addElement('html', '</table>');
  166. $form->addElement('html', '<br />');
  167. // 3 scores
  168. $form->addElement('text', 'option[1]', get_lang('Correct'), ['class' => 'span1', 'value' => '1']);
  169. $form->addElement('text', 'option[2]', get_lang('Wrong'), ['class' => 'span1', 'value' => '-0.5']);
  170. $form->addElement('hidden', 'option[3]', 0);
  171. $form->addRule('option[1]', get_lang('Required field'), 'required');
  172. $form->addRule('option[2]', get_lang('Required field'), 'required');
  173. $form->addElement('html', '</tr><table>');
  174. $form->addElement('hidden', 'options_count', 3);
  175. $form->addElement('html', '</table><br /><br />');
  176. //Extra values True, false, Dont known
  177. if (!empty($this->extra)) {
  178. $scores = explode(':', $this->extra);
  179. if (!empty($scores)) {
  180. for ($i = 1; $i <= 3; $i++) {
  181. $defaults['option['.$i.']'] = $scores[$i - 1];
  182. }
  183. }
  184. }
  185. if ($objEx->edit_exercise_in_lp === true ||
  186. (empty($this->exerciseList) && empty($objEx->id))
  187. ) {
  188. $form->addElement('submit', 'lessAnswers', get_lang('Remove answer option'), 'class="btn btn-danger minus"');
  189. $form->addElement('submit', 'moreAnswers', get_lang('Add answer option'), 'class="btn btn-primary plus"');
  190. //$text and $class defined in calling script
  191. $form->addElement('submit', 'submitQuestion', $text, 'class = "btn btn-primary"');
  192. }
  193. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  194. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  195. $renderer->setElementTemplate('{element}&nbsp;', 'moreAnswers');
  196. $form->addElement('html', '</div></div>');
  197. $defaults['correct'] = $correct;
  198. if (!empty($this->id)) {
  199. $form->setDefaults($defaults);
  200. } else {
  201. $form->setDefaults($defaults);
  202. }
  203. $form->setConstants(['nb_answers' => $nbAnswers]);
  204. }
  205. /**
  206. * abstract function which creates the form to create / edit the answers of the question.
  207. *
  208. * @param FormValidator $form
  209. * @param Exercise $exercise
  210. */
  211. public function processAnswersCreation($form, $exercise)
  212. {
  213. $questionWeighting = 0;
  214. $objAnswer = new Answer($this->id);
  215. $nbAnswers = $form->getSubmitValue('nb_answers');
  216. $courseId = api_get_course_int_id();
  217. $correct = [];
  218. $options = Question::readQuestionOption($this->id, $courseId);
  219. if (!empty($options)) {
  220. foreach ($options as $optionData) {
  221. $id = $optionData['id'];
  222. unset($optionData['id']);
  223. Question::updateQuestionOption($id, $optionData, $courseId);
  224. }
  225. } else {
  226. for ($i = 1; $i <= 8; $i++) {
  227. $lastId = Question::saveQuestionOption($this->id, $this->options[$i], $courseId, $i);
  228. $correct[$i] = $lastId;
  229. }
  230. }
  231. /* Getting quiz_question_options (true, false, doubt) because
  232. it's possible that there are more options in the future */
  233. $newOptions = Question::readQuestionOption($this->id, $courseId);
  234. $sortedByPosition = [];
  235. foreach ($newOptions as $item) {
  236. $sortedByPosition[$item['position']] = $item;
  237. }
  238. /* Saving quiz_question.extra values that has the correct scores of
  239. the true, false, doubt options registered in this format
  240. XX:YY:ZZZ where XX is a float score value. */
  241. $extraValues = [];
  242. for ($i = 1; $i <= 3; $i++) {
  243. $score = trim($form->getSubmitValue('option['.$i.']'));
  244. $extraValues[] = $score;
  245. }
  246. $this->setExtra(implode(':', $extraValues));
  247. for ($i = 1; $i <= $nbAnswers; $i++) {
  248. $answer = trim($form->getSubmitValue('answer['.$i.']'));
  249. $comment = trim($form->getSubmitValue('comment['.$i.']'));
  250. $goodAnswer = trim($form->getSubmitValue('correct['.$i.']'));
  251. if (empty($options)) {
  252. // If this is the first time that the question is created then change
  253. // the default values from the form 1 and 2 by the correct "option id" registered
  254. $goodAnswer = $sortedByPosition[$goodAnswer]['id'];
  255. }
  256. $questionWeighting += $extraValues[0]; //By default 0 has the correct answers
  257. $objAnswer->createAnswer($answer, $goodAnswer, $comment, '', $i);
  258. }
  259. // saves the answers into the data base
  260. $objAnswer->save();
  261. // sets the total weighting of the question
  262. $this->updateWeighting($questionWeighting);
  263. $this->save($exercise);
  264. }
  265. /**
  266. * {@inheritdoc}
  267. */
  268. public function return_header(Exercise $exercise, $counter = null, $score = [])
  269. {
  270. $header = parent::return_header($exercise, $counter, $score);
  271. $header .= '<table class="'.$this->question_table_class.'"><tr>';
  272. $header .= '<th>'.get_lang('Your choice').'</th>';
  273. if ($exercise->showExpectedChoiceColumn()) {
  274. $header .= '<th>'.get_lang('ExpectedYour choice').'</th>';
  275. }
  276. $header .= '<th>'
  277. .get_lang('Answer')
  278. .'</th><th colspan="2" style="text-align:center;">'
  279. .get_lang('Your degree of certainty')
  280. .'</th>'
  281. ;
  282. if ($exercise->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) {
  283. $header .= '<th>'.get_lang('Comment').'</th>';
  284. } else {
  285. $header .= '<th>&nbsp;</th>';
  286. }
  287. $header .= '</tr>';
  288. return $header;
  289. }
  290. /**
  291. * Get color code, status, label and description for the current answer.
  292. *
  293. * @param string $studentAnswer
  294. * @param string $expectedAnswer
  295. * @param int $studentDegreeChoicePosition
  296. *
  297. * @return array An array with indexes 'color', 'background-color', 'status', 'label' and 'description'
  298. */
  299. public function getResponseDegreeInfo($studentAnswer, $expectedAnswer, $studentDegreeChoicePosition)
  300. {
  301. $result = [];
  302. if ($studentDegreeChoicePosition == 3) {
  303. $result = [
  304. 'color' => '#000000',
  305. 'background-color' => '#F6BA2A',
  306. 'status' => self::LEVEL_WHITE,
  307. 'label' => get_lang('Declared ignorance'),
  308. 'description' => get_lang('Declared ignoranceDescription'),
  309. ];
  310. } else {
  311. $checkResult = $studentAnswer == $expectedAnswer ? true : false;
  312. if ($checkResult) {
  313. if ($studentDegreeChoicePosition >= 6) {
  314. $result = [
  315. 'color' => '#FFFFFF',
  316. 'background-color' => '#1E9C55',
  317. 'status' => self::LEVEL_DARKGREEN,
  318. 'label' => get_lang('Very sure'),
  319. 'description' => get_lang('Very sureDescription'),
  320. ];
  321. } elseif ($studentDegreeChoicePosition >= 4 && $studentDegreeChoicePosition <= 5) {
  322. $result = [
  323. 'color' => '#000000',
  324. 'background-color' => '#B1E183',
  325. 'status' => self::LEVEL_LIGHTGREEN,
  326. 'label' => get_lang('Pretty sure'),
  327. 'description' => get_lang('Pretty sureDescription'),
  328. ];
  329. }
  330. } else {
  331. if ($studentDegreeChoicePosition >= 6) {
  332. $result = [
  333. 'color' => '#FFFFFF',
  334. 'background-color' => '#ED4040',
  335. 'status' => self::LEVEL_DARKRED,
  336. 'label' => get_lang('Very unsure'),
  337. 'description' => get_lang('Very unsureDescription'),
  338. ];
  339. } elseif ($studentDegreeChoicePosition >= 4 && $studentDegreeChoicePosition <= 5) {
  340. $result = [
  341. 'color' => '#000000',
  342. 'background-color' => '#F79B88',
  343. 'status' => self::LEVEL_LIGHTRED,
  344. 'label' => get_lang('Unsure'),
  345. 'description' => get_lang('UnsureDescription'),
  346. ];
  347. }
  348. }
  349. }
  350. return $result;
  351. }
  352. /**
  353. * Method to show the code color and his meaning for the test result.
  354. */
  355. public static function showColorCodes()
  356. {
  357. ?>
  358. <table class="fc-border-separate" cellspacing="0" style="width:600px;
  359. margin: auto; border: 3px solid #A39E9E;" >
  360. <tr style="border-bottom: 1px solid #A39E9E;">
  361. <td style="width:15%; height:30px; background-color: #088A08; border-right: 1px solid #A39E9E;">
  362. &nbsp;
  363. </td>
  364. <td style="padding-left:10px;">
  365. <b><?php echo get_lang('Very sure'); ?> :</b>
  366. <?php echo get_lang('Very sureDescription'); ?>
  367. </td>
  368. </tr>
  369. <tr style="border-bottom: 1px solid #A39E9E;">
  370. <td style="width:15%; height:30px; background-color: #A9F5A9; border-right: 1px solid #A39E9E;">
  371. &nbsp;
  372. </td>
  373. <td style="padding-left:10px;">
  374. <b><?php echo get_lang('Pretty sure'); ?> :</b>
  375. <?php echo get_lang('Pretty sureDescription'); ?>
  376. </td>
  377. </tr>
  378. <tr style="border: 1px solid #A39E9E;">
  379. <td style="width:15%; height:30px; background-color: #FFFFFF; border-right: 1px solid #A39E9E;">
  380. &nbsp;
  381. </td>
  382. <td style="padding-left:10px;">
  383. <b><?php echo get_lang('Declared ignorance'); ?> :</b>
  384. <?php echo get_lang('Declared ignoranceDescription'); ?>
  385. </td>
  386. </tr>
  387. <tr style="border: 1px solid #A39E9E;">
  388. <td style="width:15%; height:30px; background-color: #F6CECE; border-right: 1px solid #A39E9E;">
  389. &nbsp;
  390. </td>
  391. <td style="padding-left:10px;">
  392. <b><?php echo get_lang('Unsure'); ?> :</b>
  393. <?php echo get_lang('UnsureDescription'); ?>
  394. </td>
  395. </tr>
  396. <tr style="border-bottom: 1px solid #A39E9E;">
  397. <td style="width:15%; height:30px; background-color: #FE2E2E; border-right: 1px solid #A39E9E;">
  398. &nbsp;
  399. </td>
  400. <td style="padding-left:10px;">
  401. <b><?php echo get_lang('Very unsure'); ?> :</b>
  402. <?php echo get_lang('Very unsureDescription'); ?>
  403. </td>
  404. </tr>
  405. </table><br/>
  406. <?php
  407. }
  408. /**
  409. * Display basic bar charts of results by category of questions.
  410. *
  411. * @param array $scoreListAll
  412. * @param string $title The block title
  413. * @param int $sizeRatio
  414. *
  415. * @return string The HTML/CSS code for the charts block
  416. */
  417. public static function displayDegreeChartByCategory($scoreListAll, $title, $sizeRatio = 1)
  418. {
  419. $maxHeight = 0;
  420. $groupCategoriesByBracket = false;
  421. if ($groupCategoriesByBracket) {
  422. $scoreList = [];
  423. $categoryPrefixList = [];
  424. // categoryPrefix['Math'] = firstCategoryId for this prefix
  425. // rebuild $scoreList factorizing data with category prefix
  426. foreach ($scoreListAll as $categoryId => $scoreListForCategory) {
  427. $objCategory = new Testcategory();
  428. $objCategoryNum = $objCategory->getCategory($categoryId);
  429. preg_match("/^\[([^]]+)\]/", $objCategoryNum->name, $matches);
  430. if (count($matches) > 1) {
  431. // check if we have already see this prefix
  432. if (array_key_exists($matches[1], $categoryPrefixList)) {
  433. // add the result color for this entry
  434. $scoreList[$categoryPrefixList[$matches[1]]][self::LEVEL_DARKGREEN] +=
  435. $scoreListForCategory[self::LEVEL_DARKGREEN];
  436. $scoreList[$categoryPrefixList[$matches[1]]][self::LEVEL_LIGHTGREEN] +=
  437. $scoreListForCategory[self::LEVEL_LIGHTGREEN];
  438. $scoreList[$categoryPrefixList[$matches[1]]][self::LEVEL_WHITE] +=
  439. $scoreListForCategory[self::LEVEL_WHITE];
  440. $scoreList[$categoryPrefixList[$matches[1]]][self::LEVEL_LIGHTRED] +=
  441. $scoreListForCategory[self::LEVEL_LIGHTRED];
  442. $scoreList[$categoryPrefixList[$matches[1]]][self::LEVEL_DARKRED] +=
  443. $scoreListForCategory[self::LEVEL_DARKRED];
  444. } else {
  445. $categoryPrefixList[$matches[1]] = $categoryId;
  446. $scoreList[$categoryId] = $scoreListAll[$categoryId];
  447. }
  448. } else {
  449. // doesn't match the prefix '[math] Math category'
  450. $scoreList[$categoryId] = $scoreListAll[$categoryId];
  451. }
  452. }
  453. } else {
  454. $scoreList = $scoreListAll;
  455. }
  456. // get the max height of item to have each table the same height if displayed side by side
  457. $testCategory = new TestCategory();
  458. foreach ($scoreList as $categoryId => $scoreListForCategory) {
  459. $category = $testCategory->getCategory($categoryId);
  460. if ($category) {
  461. $categoryQuestionName = $category->name;
  462. }
  463. list($noValue, $height) = self::displayDegreeChartChildren(
  464. $scoreListForCategory,
  465. 300,
  466. '',
  467. 1,
  468. 0,
  469. false,
  470. true,
  471. 0
  472. );
  473. if ($height > $maxHeight) {
  474. $maxHeight = $height;
  475. }
  476. }
  477. if (count($scoreList) > 1) {
  478. $boxWidth = $sizeRatio * 300 * 2 + 54;
  479. } else {
  480. $boxWidth = $sizeRatio * 300 + 54;
  481. }
  482. $html = '<div class="row-chart">';
  483. $html .= '<h4 class="chart-title">'.$title.'</h4>';
  484. $legendTitle = [
  485. 'Very unsure',
  486. 'Unsure',
  487. 'Declared ignorance',
  488. 'Pretty sure',
  489. 'Very sure',
  490. ];
  491. $html .= '<ul class="chart-legend">';
  492. foreach ($legendTitle as $i => $item) {
  493. $html .= '<li><i class="fa fa-square square_color'.$i.'" aria-hidden="true"></i> '.get_lang($item).'</li>';
  494. }
  495. $html .= '</ul>';
  496. // get the html of items
  497. $i = 0;
  498. $testCategory = new Testcategory();
  499. foreach ($scoreList as $categoryId => $scoreListForCategory) {
  500. $category = $testCategory->getCategory($categoryId);
  501. $categoryQuestionName = '';
  502. if ($category) {
  503. $categoryQuestionName = $category->name;
  504. }
  505. if ($categoryQuestionName === '') {
  506. $categoryName = get_lang('Without category');
  507. } else {
  508. $categoryName = $categoryQuestionName;
  509. }
  510. $html .= '<div class="col-md-4">';
  511. $html .= self::displayDegreeChartChildren(
  512. $scoreListForCategory,
  513. 300,
  514. $categoryName,
  515. 1,
  516. $maxHeight,
  517. false,
  518. false,
  519. $groupCategoriesByBracket
  520. );
  521. $html .= '</div>';
  522. if ($i == 2) {
  523. $html .= '<div style="clear:both; height: 10px;">&nbsp;</div>';
  524. $i = 0;
  525. } else {
  526. $i++;
  527. }
  528. }
  529. $html .= '</div>';
  530. return $html.'<div style="clear:both; height: 10px;" >&nbsp;</div>';
  531. }
  532. /**
  533. * Return HTML code for the $scoreList of MultipleAnswerTrueFalseDegreeCertainty questions.
  534. *
  535. * @param $scoreList
  536. * @param $widthTable
  537. * @param string $title
  538. * @param int $sizeRatio
  539. * @param int $minHeight
  540. * @param bool $displayExplanationText
  541. * @param bool $returnHeight
  542. * @param bool $groupCategoriesByBracket
  543. * @param int $numberOfQuestions
  544. *
  545. * @return array|string
  546. */
  547. public static function displayDegreeChart(
  548. $scoreList,
  549. $widthTable,
  550. $title = '',
  551. $sizeRatio = 1,
  552. $minHeight = 0,
  553. $displayExplanationText = true,
  554. $returnHeight = false,
  555. $groupCategoriesByBracket = false,
  556. $numberOfQuestions = 0
  557. ) {
  558. $topAndBottomMargin = 10;
  559. $colorList = [
  560. self::LEVEL_DARKRED,
  561. self::LEVEL_LIGHTRED,
  562. self::LEVEL_WHITE,
  563. self::LEVEL_LIGHTGREEN,
  564. self::LEVEL_DARKGREEN,
  565. ];
  566. // get total attempt number
  567. $highterColorHeight = 0;
  568. foreach ($scoreList as $color => $number) {
  569. if ($number > $highterColorHeight) {
  570. $highterColorHeight = $number;
  571. }
  572. }
  573. $totalAttemptNumber = $numberOfQuestions;
  574. $verticalLineHeight = $highterColorHeight * $sizeRatio * 2 + 122 + $topAndBottomMargin * 2;
  575. if ($verticalLineHeight < $minHeight) {
  576. $minHeightCorrection = $minHeight - $verticalLineHeight;
  577. $verticalLineHeight += $minHeightCorrection;
  578. }
  579. // draw chart
  580. $html = '';
  581. if ($groupCategoriesByBracket) {
  582. $title = api_preg_replace("/[^]]*$/", '', $title);
  583. $title = ucfirst(api_preg_replace("/[\[\]]/", '', $title));
  584. }
  585. $titleDisplay = (strpos($title, "ensemble") > 0) ?
  586. $title."<br/>($totalAttemptNumber questions)" :
  587. $title;
  588. $textSize = (
  589. strpos($title, 'ensemble') > 0 ||
  590. strpos($title, 'votre dernier résultat à ce test') > 0
  591. ) ? 100 : 80;
  592. $html .= '<div class="row-chart">';
  593. $html .= '<h4 class="chart-title">'.$titleDisplay.'</h4>';
  594. $nbResponsesInc = 0;
  595. if (isset($scoreList[4])) {
  596. $nbResponsesInc += (int) $scoreList[4];
  597. }
  598. if (isset($scoreList[5])) {
  599. $nbResponsesInc += (int) $scoreList[5];
  600. }
  601. $nbResponsesIng = isset($scoreList[3]) ? $scoreList[3] : 0;
  602. $nbResponsesCor = 0;
  603. if (isset($scoreList[1])) {
  604. $nbResponsesCor += (int) $scoreList[1];
  605. }
  606. if (isset($scoreList[2])) {
  607. $nbResponsesCor += (int) $scoreList[2];
  608. }
  609. $IncorrectAnswers = sprintf(get_lang('Incorrect answers: %s'), $nbResponsesInc);
  610. $IgnoranceAnswers = sprintf(get_lang('Ignorance: %s'), $nbResponsesIng);
  611. $CorrectAnswers = sprintf(get_lang('Correct answers: %s'), $nbResponsesCor);
  612. $html .= '<div class="chart-grid">';
  613. $explainHistoList = null;
  614. if ($displayExplanationText) {
  615. // Display of histogram text
  616. $explainHistoList = [
  617. 'Very unsure',
  618. 'Unsure',
  619. 'Declared ignorance',
  620. 'Pretty sure',
  621. 'Very sure',
  622. ];
  623. }
  624. foreach ($colorList as $i => $color) {
  625. if (array_key_exists($color, $scoreList)) {
  626. $scoreOnBottom = $scoreList[$color]; // height of the colored area on the bottom
  627. } else {
  628. $scoreOnBottom = 0;
  629. }
  630. $sizeBar = ($scoreOnBottom * $sizeRatio * 2).'px;';
  631. if ($i == 0) {
  632. $html .= '<div class="item">';
  633. $html .= '<div class="panel-certaint" style="min-height:'.$verticalLineHeight.'px; position: relative;">';
  634. $html .= '<div class="answers-title">'.$IncorrectAnswers.'</div>';
  635. $html .= '<ul class="certaint-list-two">';
  636. } elseif ($i == 3) {
  637. $html .= '<div class="item">';
  638. $html .= '<div class="panel-certaint" style="height:'.$verticalLineHeight.'px; position: relative;">';
  639. $html .= '<div class="answers-title">'.$CorrectAnswers.'</div>';
  640. $html .= '<ul class="certaint-list-two">';
  641. } elseif ($i == 2) {
  642. $html .= '<div class="item">';
  643. $html .= '<div class="panel-certaint" style="height:'.$verticalLineHeight.'px; position: relative;">';
  644. $html .= '<div class="answers-title">'.$IgnoranceAnswers.'</div>';
  645. $html .= '<ul class="certaint-list">';
  646. }
  647. $html .= '<li>';
  648. $html .= '<div class="certaint-score">';
  649. $html .= $scoreOnBottom;
  650. $html .= '</div>';
  651. $html .= '<div class="levelbar_'.$color.'" style="height:'.$sizeBar.'">&nbsp;</div>';
  652. $html .= '<div class="certaint-text">'.get_lang($explainHistoList[$i]).'</div>';
  653. $html .= '</li>';
  654. if ($i == 1 || $i == 2 || $i == 4) {
  655. $html .= '</ul>';
  656. $html .= '</div>';
  657. $html .= '</div>';
  658. }
  659. }
  660. $html .= '</div>';
  661. $html .= '</div>';
  662. if ($returnHeight) {
  663. return [$html, $verticalLineHeight];
  664. } else {
  665. return $html;
  666. }
  667. }
  668. /**
  669. * Return HTML code for the $scoreList of MultipleAnswerTrueFalseDegreeCertainty questions.
  670. *
  671. * @param $scoreList
  672. * @param $widthTable
  673. * @param string $title
  674. * @param int $sizeRatio
  675. * @param int $minHeight
  676. * @param bool $displayExplanationText
  677. * @param bool $returnHeight
  678. * @param bool $groupCategoriesByBracket
  679. * @param int $numberOfQuestions
  680. *
  681. * @return array|string
  682. */
  683. public static function displayDegreeChartChildren(
  684. $scoreList,
  685. $widthTable,
  686. $title = '',
  687. $sizeRatio = 1,
  688. $minHeight = 0,
  689. $displayExplanationText = true,
  690. $returnHeight = false,
  691. $groupCategoriesByBracket = false,
  692. $numberOfQuestions = 0
  693. ) {
  694. $topAndBottomMargin = 10;
  695. $colorList = [
  696. self::LEVEL_DARKRED,
  697. self::LEVEL_LIGHTRED,
  698. self::LEVEL_WHITE,
  699. self::LEVEL_LIGHTGREEN,
  700. self::LEVEL_DARKGREEN,
  701. ];
  702. // get total attempt number
  703. $highterColorHeight = 0;
  704. foreach ($scoreList as $color => $number) {
  705. if ($number > $highterColorHeight) {
  706. $highterColorHeight = $number;
  707. }
  708. }
  709. $totalAttemptNumber = $numberOfQuestions;
  710. $verticalLineHeight = $highterColorHeight * $sizeRatio * 2 + 122 + $topAndBottomMargin * 2;
  711. if ($verticalLineHeight < $minHeight) {
  712. $minHeightCorrection = $minHeight - $verticalLineHeight;
  713. $verticalLineHeight += $minHeightCorrection;
  714. }
  715. // draw chart
  716. $html = '';
  717. if ($groupCategoriesByBracket) {
  718. $title = api_preg_replace("/[^]]*$/", '', $title);
  719. $title = ucfirst(api_preg_replace("/[\[\]]/", '', $title));
  720. }
  721. $textSize = 80;
  722. $classGlobalChart = '';
  723. if ($displayExplanationText) {
  724. // global chart
  725. $classGlobalChart = 'globalChart';
  726. }
  727. $html .= '<table class="certaintyTable" style="height :'.$verticalLineHeight.'px; margin-bottom: 10px;" >';
  728. $html .= '<tr><th colspan="5" class="'.$classGlobalChart.'">'
  729. .$title
  730. .'</th><tr>'
  731. ;
  732. $nbResponsesInc = 0;
  733. if (isset($scoreList[4])) {
  734. $nbResponsesInc += (int) $scoreList[4];
  735. }
  736. if (isset($scoreList[5])) {
  737. $nbResponsesInc += (int) $scoreList[5];
  738. }
  739. $nbResponsesIng = isset($scoreList[3]) ? $scoreList[3] : 0;
  740. $nbResponsesCor = 0;
  741. if (isset($scoreList[1])) {
  742. $nbResponsesCor += (int) $scoreList[1];
  743. }
  744. if (isset($scoreList[2])) {
  745. $nbResponsesCor += (int) $scoreList[2];
  746. }
  747. $colWidth = $widthTable / 5;
  748. $html .= '<tr>
  749. <td class="firstLine borderRight '.$classGlobalChart.'"
  750. colspan="2"
  751. style="width:'.($colWidth * 2).'px; line-height: 15px; font-size:'.$textSize.'%;">'.
  752. sprintf(get_lang('Incorrect answers: %s'), $nbResponsesInc).'
  753. </td>
  754. <td class="firstLine borderRight '.$classGlobalChart.'"
  755. style="width:'.$colWidth.'px; line-height: 15px; font-size :'.$textSize.'%;">'.
  756. sprintf(get_lang('Ignorance: %s'), $nbResponsesIng).'
  757. </td>
  758. <td class="firstLine '.$classGlobalChart.'"
  759. colspan="2"
  760. style="width:'.($colWidth * 2).'px; line-height: 15px; font-size:'.$textSize.'%;">'.
  761. sprintf(get_lang('Correct answers: %s'), $nbResponsesCor).'
  762. </td>
  763. </tr>';
  764. $html .= '<tr>';
  765. foreach ($colorList as $i => $color) {
  766. if (array_key_exists($color, $scoreList)) {
  767. $scoreOnBottom = $scoreList[$color]; // height of the colored area on the bottom
  768. } else {
  769. $scoreOnBottom = 0;
  770. }
  771. $sizeOnBottom = $scoreOnBottom * $sizeRatio * 2;
  772. if ($i == 1 || $i == 2) {
  773. $html .= '<td width="'
  774. .$colWidth
  775. .'px" style="border-right: 1px dotted #7FC5FF; vertical-align: bottom;font-size: '
  776. .$textSize
  777. .'%;">'
  778. ;
  779. } else {
  780. $html .= '<td width="'
  781. .$colWidth
  782. .'px" style="vertical-align: bottom;font-size: '
  783. .$textSize
  784. .'%;">'
  785. ;
  786. }
  787. $html .= '<div class="certaint-score">'
  788. .$scoreOnBottom
  789. .'</div><div class="levelbar_'
  790. .$color
  791. .'" style="height: '
  792. .$sizeOnBottom
  793. .'px;">&nbsp;</div>'
  794. ;
  795. $html .= '</td>';
  796. }
  797. $html .= '</tr>';
  798. if ($displayExplanationText) {
  799. // Display of histogram text
  800. $explainHistoList = [
  801. 'Very unsure',
  802. 'Unsure',
  803. 'Declared ignorance',
  804. 'Pretty sure',
  805. 'Very sure',
  806. ];
  807. $html .= '<tr>';
  808. $i = 0;
  809. foreach ($explainHistoList as $explain) {
  810. if ($i == 1 || $i == 2) {
  811. $class = 'borderRight';
  812. } else {
  813. $class = '';
  814. }
  815. $html .= '<td class="firstLine '
  816. .$class
  817. .' '
  818. .$classGlobalChart
  819. .'" style="width="'
  820. .$colWidth
  821. .'px; font-size:'
  822. .$textSize
  823. .'%;">'
  824. ;
  825. $html .= get_lang($explain);
  826. $html .= '</td>';
  827. $i++;
  828. }
  829. $html .= '</tr>';
  830. }
  831. $html .= '</table></center>';
  832. if ($returnHeight) {
  833. return [$html, $verticalLineHeight];
  834. } else {
  835. return $html;
  836. }
  837. }
  838. /**
  839. * return previous attempt id for this test for student, 0 if no previous attempt.
  840. *
  841. * @param $exeId
  842. *
  843. * @return int
  844. */
  845. public static function getPreviousAttemptId($exeId)
  846. {
  847. $tblTrackEExercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  848. $exeId = (int) $exeId;
  849. $sql = "SELECT * FROM $tblTrackEExercise
  850. WHERE exe_id = ".$exeId;
  851. $res = Database::query($sql);
  852. if (empty(Database::num_rows($res))) {
  853. // if we cannot find the exe_id
  854. return 0;
  855. }
  856. $data = Database::fetch_assoc($res);
  857. $courseCode = $data['c_id'];
  858. $exerciseId = $data['exe_exo_id'];
  859. $userId = $data['exe_user_id'];
  860. $attemptDate = $data['exe_date'];
  861. if ($attemptDate == '0000-00-00 00:00:00') {
  862. // incomplete attempt, close it before continue
  863. return 0;
  864. }
  865. // look for previous attempt
  866. $exerciseId = (int) $exerciseId;
  867. $userId = (int) $userId;
  868. $sql = "SELECT *
  869. FROM $tblTrackEExercise
  870. WHERE c_id = '$courseCode'
  871. AND exe_exo_id = $exerciseId
  872. AND exe_user_id = $userId
  873. AND status = ''
  874. AND exe_date > '0000-00-00 00:00:00'
  875. AND exe_date < '$attemptDate'
  876. ORDER BY exe_date DESC";
  877. $res = Database::query($sql);
  878. if (Database::num_rows($res) == 0) {
  879. // no previous attempt
  880. return 0;
  881. }
  882. $data = Database::fetch_assoc($res);
  883. return $data['exe_id'];
  884. }
  885. /**
  886. * return an array of number of answer color for exe attempt
  887. * for question type = MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY
  888. * e.g.
  889. * [LEVEL_DARKGREEN => 3, LEVEL_LIGHTGREEN => 0, LEVEL_WHITE => 5, LEVEL_LIGHTRED => 12, LEVEL_DARKTRED => 0].
  890. *
  891. * @param $exeId
  892. *
  893. * @return array
  894. */
  895. public static function getColorNumberListForAttempt($exeId)
  896. {
  897. $result = [
  898. self::LEVEL_DARKGREEN => 0,
  899. self::LEVEL_LIGHTGREEN => 0,
  900. self::LEVEL_WHITE => 0,
  901. self::LEVEL_LIGHTRED => 0,
  902. self::LEVEL_DARKRED => 0,
  903. ];
  904. $attemptInfoList = self::getExerciseAttemptInfo($exeId);
  905. foreach ($attemptInfoList as $attemptInfo) {
  906. $oQuestion = new MultipleAnswerTrueFalseDegreeCertainty();
  907. $oQuestion->read($attemptInfo['question_id']);
  908. if ($oQuestion->type == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  909. $answerColor = self::getAnswerColor($exeId, $attemptInfo['question_id'], $attemptInfo['position']);
  910. if ($answerColor) {
  911. $result[$answerColor]++;
  912. }
  913. }
  914. }
  915. return $result;
  916. }
  917. /**
  918. * return an array of number of color for question type = MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY
  919. * for each question category.
  920. *
  921. * e.g.
  922. * [
  923. * (categoryId=)5 => [LEVEL_DARKGREEN => 3, LEVEL_WHITE => 5, LEVEL_LIGHTRED => 12]
  924. * (categoryId=)2 => [LEVEL_DARKGREEN => 8, LEVEL_LIGHTRED => 2, LEVEL_DARKTRED => 8]
  925. * (categoryId=)0 => [LEVEL_DARKGREEN => 1,
  926. * LEVEL_LIGHTGREEN => 2,
  927. * LEVEL_WHITE => 6,
  928. * LEVEL_LIGHTRED => 1,
  929. * LEVEL_DARKTRED => 9]
  930. * ]
  931. *
  932. * @param int $exeId
  933. *
  934. * @return array
  935. */
  936. public static function getColorNumberListForAttemptByCategory($exeId)
  937. {
  938. $result = [];
  939. $attemptInfoList = self::getExerciseAttemptInfo($exeId);
  940. foreach ($attemptInfoList as $attemptInfo) {
  941. $oQuestion = new MultipleAnswerTrueFalseDegreeCertainty();
  942. $oQuestion->read($attemptInfo['question_id']);
  943. if ($oQuestion->type == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  944. $questionCategory = Testcategory::getCategoryForQuestion($attemptInfo['question_id']);
  945. if (!array_key_exists($questionCategory, $result)) {
  946. $result[$questionCategory] = [];
  947. }
  948. $answerColor = self::getAnswerColor($exeId, $attemptInfo['question_id'], $attemptInfo['position']);
  949. if ($answerColor && isset($result[$questionCategory])) {
  950. if (!isset($result[$questionCategory][$answerColor])) {
  951. $result[$questionCategory][$answerColor] = 0;
  952. }
  953. $result[$questionCategory][$answerColor]++;
  954. }
  955. }
  956. }
  957. return $result;
  958. }
  959. /**
  960. * Return true if answer of $exeId, $questionId, $position is correct, otherwise return false.
  961. *
  962. * @param $exeId
  963. * @param $questionId
  964. * @param $position
  965. *
  966. * @return int
  967. */
  968. public static function getAnswerColor($exeId, $questionId, $position)
  969. {
  970. $attemptInfoList = self::getExerciseAttemptInfo($exeId, $questionId, $position);
  971. if (count($attemptInfoList) != 1) {
  972. // havent got the answer
  973. return 0;
  974. }
  975. $answerCodes = $attemptInfoList[0]['answer'];
  976. // student answer
  977. $splitAnswer = preg_split("/:/", $answerCodes);
  978. // get correct answer option id
  979. $correctAnswerOptionId = self::getCorrectAnswerOptionId($splitAnswer[0]);
  980. if ($correctAnswerOptionId == 0) {
  981. // error returning the correct answer option id
  982. return 0;
  983. }
  984. // get student answer option id
  985. $studentAnswerOptionId = isset($splitAnswer[1]) ? $splitAnswer[1] : null;
  986. // we got the correct answer option id, let's compare ti with the student answer
  987. $percentage = null;
  988. if (isset($splitAnswer[2])) {
  989. $percentage = self::getPercentagePosition($splitAnswer[2]);
  990. }
  991. if ($studentAnswerOptionId == $correctAnswerOptionId) {
  992. // yeah, student got correct answer
  993. switch ($percentage) {
  994. case 3:
  995. return self::LEVEL_WHITE;
  996. case 4:
  997. case 5:
  998. return self::LEVEL_LIGHTGREEN;
  999. case 6:
  1000. case 7:
  1001. case 8:
  1002. return self::LEVEL_DARKGREEN;
  1003. default:
  1004. return 0;
  1005. }
  1006. } else {
  1007. // bummer, wrong answer dude
  1008. switch ($percentage) {
  1009. case 3:
  1010. return self::LEVEL_WHITE;
  1011. case 4:
  1012. case 5:
  1013. return self::LEVEL_LIGHTRED;
  1014. case 6:
  1015. case 7:
  1016. case 8:
  1017. return self::LEVEL_DARKRED;
  1018. default:
  1019. return 0;
  1020. }
  1021. }
  1022. }
  1023. /**
  1024. * Return the position of certitude %age choose by student.
  1025. *
  1026. * @param $optionId
  1027. *
  1028. * @return int
  1029. */
  1030. public static function getPercentagePosition($optionId)
  1031. {
  1032. $tblAnswerOption = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1033. $courseId = api_get_course_int_id();
  1034. $optionId = (int) $optionId;
  1035. $sql = "SELECT position
  1036. FROM $tblAnswerOption
  1037. WHERE c_id = $courseId AND id = $optionId";
  1038. $res = Database::query($sql);
  1039. if (Database::num_rows($res) == 0) {
  1040. return 0;
  1041. }
  1042. $data = Database::fetch_assoc($res);
  1043. return $data['position'];
  1044. }
  1045. /**
  1046. * return the correct id from c_quiz_question_option for question idAuto.
  1047. *
  1048. * @param $idAuto
  1049. *
  1050. * @return int
  1051. */
  1052. public static function getCorrectAnswerOptionId($idAuto)
  1053. {
  1054. $tblAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  1055. $courseId = api_get_course_int_id();
  1056. $idAuto = (int) $idAuto;
  1057. $sql = "SELECT correct FROM $tblAnswer
  1058. WHERE c_id = $courseId AND id_auto = $idAuto";
  1059. $res = Database::query($sql);
  1060. $data = Database::fetch_assoc($res);
  1061. if (Database::num_rows($res) > 0) {
  1062. return $data['correct'];
  1063. } else {
  1064. return 0;
  1065. }
  1066. }
  1067. /**
  1068. * return an array of exe info from track_e_attempt.
  1069. *
  1070. * @param int $exeId
  1071. * @param int $questionId
  1072. * @param int $position
  1073. *
  1074. * @return array
  1075. */
  1076. public static function getExerciseAttemptInfo($exeId, $questionId = -1, $position = -1)
  1077. {
  1078. $result = [];
  1079. $and = '';
  1080. $questionId = (int) $questionId;
  1081. $position = (int) $position;
  1082. $exeId = (int) $exeId;
  1083. if ($questionId >= 0) {
  1084. $and .= " AND question_id = $questionId";
  1085. }
  1086. if ($position >= 0) {
  1087. $and .= " AND position = $position";
  1088. }
  1089. $tblExeAttempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1090. $cId = api_get_course_int_id();
  1091. $sql = "SELECT * FROM $tblExeAttempt
  1092. WHERE c_id = $cId AND exe_id = $exeId $and";
  1093. $res = Database::query($sql);
  1094. while ($data = Database::fetch_assoc($res)) {
  1095. $result[] = $data;
  1096. }
  1097. return $result;
  1098. }
  1099. /**
  1100. * @param int $exeId
  1101. *
  1102. * @return int
  1103. */
  1104. public static function getNumberOfQuestionsForExeId($exeId)
  1105. {
  1106. $tableTrackEExercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1107. $exeId = (int) $exeId;
  1108. $sql = "SELECT exe_exo_id
  1109. FROM $tableTrackEExercise
  1110. WHERE exe_id=".$exeId;
  1111. $res = Database::query($sql);
  1112. $data = Database::fetch_assoc($res);
  1113. if ($data) {
  1114. $exerciseId = $data['exe_exo_id'];
  1115. $objectExercise = new Exercise();
  1116. $objectExercise->read($exerciseId);
  1117. return $objectExercise->getQuestionCount();
  1118. }
  1119. return 0;
  1120. }
  1121. /**
  1122. * Display student chart results for these question types.
  1123. *
  1124. * @param int $exeId
  1125. * @param Exercise $objExercice
  1126. *
  1127. * @return string
  1128. */
  1129. public static function displayStudentsChartResults($exeId, $objExercice)
  1130. {
  1131. $numberOfQuestions = self::getNumberOfQuestionsForExeId($exeId);
  1132. $globalScoreList = self::getColorNumberListForAttempt($exeId);
  1133. $html = self::displayDegreeChart(
  1134. $globalScoreList,
  1135. 600,
  1136. get_lang('Your overall results for the test'),
  1137. 2,
  1138. 0,
  1139. true,
  1140. false,
  1141. false,
  1142. $numberOfQuestions
  1143. );
  1144. $html .= '<br/>';
  1145. $previousAttemptId = self::getPreviousAttemptId($exeId);
  1146. if ($previousAttemptId > 0) {
  1147. $previousAttemptScoreList = self::getColorNumberListForAttempt(
  1148. $previousAttemptId
  1149. );
  1150. $html .= self::displayDegreeChart(
  1151. $previousAttemptScoreList,
  1152. 600,
  1153. get_lang('In comparison, your latest results for this test'),
  1154. 2
  1155. );
  1156. $html .= '<br/>';
  1157. }
  1158. $list = self::getColorNumberListForAttemptByCategory($exeId);
  1159. $html .= self::displayDegreeChartByCategory(
  1160. $list,
  1161. get_lang('Your results by discipline'),
  1162. 1,
  1163. $objExercice
  1164. );
  1165. $html .= '<br/>';
  1166. return $html;
  1167. }
  1168. /**
  1169. * send mail to student with degre certainty result test.
  1170. *
  1171. * @param int $userId
  1172. * @param Exercise $objExercise
  1173. * @param int $exeId
  1174. */
  1175. public static function sendQuestionCertaintyNotification($userId, $objExercise, $exeId)
  1176. {
  1177. $userInfo = api_get_user_info($userId);
  1178. $recipientName = api_get_person_name($userInfo['firstname'],
  1179. $userInfo['lastname'],
  1180. null,
  1181. PERSON_NAME_EMAIL_ADDRESS
  1182. );
  1183. $subject = "[".get_lang('Please do not reply')."] "
  1184. .html_entity_decode(get_lang('Results for the accomplished test')." \"".$objExercise->title."\"");
  1185. // message sended to the student
  1186. $message = get_lang('Dear').' '.$recipientName.",<br /><br />";
  1187. $exerciseLink = "<a href='".api_get_path(WEB_CODE_PATH)."/exercise/result.php?show_headers=1&"
  1188. .api_get_cidreq()
  1189. ."&id=$exeId'>";
  1190. $exerciseTitle = $objExercise->title;
  1191. $message .= sprintf(
  1192. get_lang('Please follow the instructions below to check your results for test %s.<br /><br />'),
  1193. $exerciseTitle,
  1194. api_get_path(WEB_PATH),
  1195. $exerciseLink
  1196. );
  1197. // show histogram
  1198. $message .= self::displayStudentsChartResults($exeId, $objExercise);
  1199. $message .= get_lang('Kind regards,');
  1200. $message = api_preg_replace("/\\\n/", '', $message);
  1201. MessageManager::send_message_simple($userId, $subject, $message);
  1202. }
  1203. }