fill_blanks.class.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class FillBlanks
  5. *
  6. * @author Eric Marguin
  7. * @author Julio Montoya multiple fill in blank option added
  8. * @package chamilo.exercise
  9. **/
  10. class FillBlanks extends Question
  11. {
  12. public static $typePicture = 'fill_in_blanks.png';
  13. public static $explanationLangVar = 'FillBlanks';
  14. const FILL_THE_BLANK_STANDARD = 0;
  15. const FILL_THE_BLANK_MENU = 1;
  16. const FILL_THE_BLANK_SEVERAL_ANSWER = 2;
  17. /**
  18. * Constructor
  19. */
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->type = FILL_IN_BLANKS;
  24. $this->isContent = $this->getIsContent();
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. public function createAnswersForm($form)
  30. {
  31. $defaults = array();
  32. if (!empty($this->id)) {
  33. $objectAnswer = new Answer($this->id);
  34. $answer = $objectAnswer->selectAnswer(1);
  35. $listAnswersInfo = self::getAnswerInfo($answer);
  36. if ($listAnswersInfo['switchable']) {
  37. $defaults['multiple_answer'] = 1;
  38. } else {
  39. $defaults['multiple_answer'] = 0;
  40. }
  41. //take the complete string except after the last '::'
  42. $defaults['answer'] = $listAnswersInfo['text'];
  43. $defaults['select_separator'] = $listAnswersInfo['blankseparatornumber'];
  44. $blankSeparatorNumber = $listAnswersInfo['blankseparatornumber'];
  45. } else {
  46. $defaults['answer'] = get_lang('DefaultTextInBlanks');
  47. $defaults['select_separator'] = 0;
  48. $blankSeparatorNumber = 0;
  49. }
  50. $blankSeparatorStart = self::getStartSeparator($blankSeparatorNumber);
  51. $blankSeparatorEnd = self::getEndSeparator($blankSeparatorNumber);
  52. $setWeightAndSize = '';
  53. if (isset($listAnswersInfo) && count($listAnswersInfo['tabweighting']) > 0) {
  54. foreach ($listAnswersInfo['tabweighting'] as $i => $weighting) {
  55. $setWeightAndSize .= 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";';
  56. }
  57. foreach ($listAnswersInfo['tabinputsize'] as $i => $sizeOfInput) {
  58. $setWeightAndSize .= 'document.getElementById("sizeofinput['.$i.']").value = "'.$sizeOfInput.'";';
  59. $setWeightAndSize .= 'document.getElementById("samplesize['.$i.']").style.width = "'.$sizeOfInput.'px";';
  60. }
  61. }
  62. echo '<script>
  63. var firstTime = true;
  64. var originalOrder = new Array();
  65. var blankSeparatorStart = "'.$blankSeparatorStart.'";
  66. var blankSeparatorEnd = "'.$blankSeparatorEnd.'";
  67. var blankSeparatorStartRegexp = getBlankSeparatorRegexp(blankSeparatorStart);
  68. var blankSeparatorEndRegexp = getBlankSeparatorRegexp(blankSeparatorEnd);
  69. var blanksRegexp = "/"+blankSeparatorStartRegexp+"[^"+blankSeparatorStartRegexp+"]*"+blankSeparatorEndRegexp+"/g";
  70. CKEDITOR.on("instanceCreated", function(e) {
  71. if (e.editor.name === "answer") {
  72. //e.editor.on("change", updateBlanks);
  73. e.editor.on("change", function(){
  74. updateBlanks();
  75. });
  76. }
  77. });
  78. function updateBlanks()
  79. {
  80. var answer;
  81. if (firstTime) {
  82. var field = document.getElementById("answer");
  83. answer = field.value;
  84. } else {
  85. answer = CKEDITOR.instances["answer"].getData();
  86. }
  87. // disable the save button, if not blanks have been created
  88. $("button").attr("disabled", "disabled");
  89. $("#defineoneblank").show();
  90. var blanks = answer.match(eval(blanksRegexp));
  91. var fields = "<div class=\"form-group \">";
  92. fields += "<label class=\"col-sm-2 control-label\">'.get_lang('Weighting').'</label>";
  93. fields += "<div class=\"col-sm-8\">";
  94. fields += "<table>";
  95. fields += "<tr><th style=\"padding:0 20px\">'.get_lang("WordTofind").'</th><th style=\"padding:0 20px\">'.get_lang("QuestionWeighting").'</th><th style=\"padding:0 20px\">'.get_lang("BlankInputSize").'</th></tr>";
  96. if (blanks != null) {
  97. for (var i=0; i < blanks.length; i++) {
  98. // remove forbidden characters that causes bugs
  99. blanks[i] = removeForbiddenChars(blanks[i]);
  100. // trim blanks between brackets
  101. blanks[i] = trimBlanksBetweenSeparator(blanks[i], blankSeparatorStart, blankSeparatorEnd);
  102. // if the word is empty []
  103. if (blanks[i] == blankSeparatorStartRegexp+blankSeparatorEndRegexp) {
  104. break;
  105. }
  106. // get input size
  107. var inputSize = 100;
  108. var textValue = blanks[i].substr(1, blanks[i].length - 2);
  109. var btoaValue = textValue.hashCode();
  110. if (firstTime == false) {
  111. var element = document.getElementById("samplesize["+i+"]");
  112. if (element) {
  113. inputSize = document.getElementById("sizeofinput["+i+"]").value;
  114. }
  115. }
  116. if (document.getElementById("weighting["+i+"]")) {
  117. var value = document.getElementById("weighting["+i+"]").value;
  118. } else {
  119. var value = "1";
  120. }
  121. fields += "<tr>";
  122. fields += "<td>"+blanks[i]+"</td>";
  123. fields += "<td><input style=\"width:35px\" value=\""+value+"\" type=\"text\" id=\"weighting["+i+"]\" name=\"weighting["+i+"]\" /></td>";
  124. fields += "<td>";
  125. fields += "<input class=\"btn btn-default\" type=\"button\" value=\"-\" onclick=\"changeInputSize(-1, "+i+")\">&nbsp;";
  126. fields += "<input class=\"btn btn-default\" type=\"button\" value=\"+\" onclick=\"changeInputSize(1, "+i+")\">&nbsp;";
  127. fields += "<input class=\"sample\" id=\"samplesize["+i+"]\" data-btoa=\""+btoaValue+"\" type=\"text\" value=\""+textValue+"\" style=\"width:"+inputSize+"px\" disabled=disabled />";
  128. fields += "<input id=\"sizeofinput["+i+"]\" type=\"hidden\" value=\""+inputSize+"\" name=\"sizeofinput["+i+"]\" />";
  129. fields += "</td>";
  130. fields += "</tr>";
  131. // enable the save button
  132. $("button").removeAttr("disabled");
  133. $("#defineoneblank").hide();
  134. }
  135. }
  136. document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  137. $(originalOrder).each(function(i, data) {
  138. if (firstTime == false) {
  139. value = data.value;
  140. var d = $("input.sample[data-btoa=\'"+value+"\']");
  141. var id = d.attr("id");
  142. if (id) {
  143. var sizeInputId = id.replace("samplesize", "sizeofinput");
  144. var sizeInputId = sizeInputId.replace("[", "\\\[");
  145. var sizeInputId = sizeInputId.replace("]", "\\\]");
  146. $("#"+sizeInputId).val(data.width);
  147. d.outerWidth(data.width+"px");
  148. }
  149. }
  150. });
  151. updateOrder(blanks);
  152. if (firstTime) {
  153. firstTime = false;
  154. '.$setWeightAndSize.'
  155. }
  156. }
  157. window.onload = updateBlanks;
  158. String.prototype.hashCode = function() {
  159. var hash = 0, i, chr, len;
  160. if (this.length === 0) return hash;
  161. for (i = 0, len = this.length; i < len; i++) {
  162. chr = this.charCodeAt(i);
  163. hash = ((hash << 5) - hash) + chr;
  164. hash |= 0; // Convert to 32bit integer
  165. }
  166. return hash;
  167. };
  168. function updateOrder(blanks)
  169. {
  170. originalOrder = new Array();
  171. if (blanks != null) {
  172. for (var i=0; i < blanks.length; i++) {
  173. // remove forbidden characters that causes bugs
  174. blanks[i] = removeForbiddenChars(blanks[i]);
  175. // trim blanks between brackets
  176. blanks[i] = trimBlanksBetweenSeparator(blanks[i], blankSeparatorStart, blankSeparatorEnd);
  177. // if the word is empty []
  178. if (blanks[i] == blankSeparatorStartRegexp+blankSeparatorEndRegexp) {
  179. break;
  180. }
  181. var textValue = blanks[i].substr(1, blanks[i].length - 2);
  182. var btoaValue = textValue.hashCode();
  183. if (firstTime == false) {
  184. var element = document.getElementById("samplesize["+i+"]");
  185. if (element) {
  186. inputSize = document.getElementById("sizeofinput["+i+"]").value;
  187. originalOrder.push({ "width" : inputSize, "value": btoaValue });
  188. }
  189. }
  190. }
  191. }
  192. }
  193. function changeInputSize(coef, inIdNum)
  194. {
  195. if (firstTime) {
  196. var field = document.getElementById("answer");
  197. answer = field.value;
  198. } else {
  199. answer = CKEDITOR.instances["answer"].getData();
  200. }
  201. var blanks = answer.match(eval(blanksRegexp));
  202. var currentWidth = $("#samplesize\\\["+inIdNum+"\\\]").width();
  203. var newWidth = currentWidth + coef * 20;
  204. newWidth = Math.max(20, newWidth);
  205. newWidth = Math.min(newWidth, 600);
  206. $("#samplesize\\\["+inIdNum+"\\\]").outerWidth(newWidth);
  207. $("#sizeofinput\\\["+inIdNum+"\\\]").attr("value", newWidth);
  208. updateOrder(blanks);
  209. }
  210. function removeForbiddenChars(inTxt)
  211. {
  212. outTxt = inTxt;
  213. outTxt = outTxt.replace(/&quot;/g, ""); // remove the char
  214. outTxt = outTxt.replace(/\x22/g, ""); // remove the char
  215. outTxt = outTxt.replace(/"/g, ""); // remove the char
  216. outTxt = outTxt.replace(/\\\\/g, ""); // remove the \ char
  217. outTxt = outTxt.replace(/&nbsp;/g, " ");
  218. outTxt = outTxt.replace(/^ +/, "");
  219. outTxt = outTxt.replace(/ +$/, "");
  220. return outTxt;
  221. }
  222. function changeBlankSeparator()
  223. {
  224. var separatorNumber = $("#select_separator").val();
  225. var tabSeparator = getSeparatorFromNumber(separatorNumber);
  226. blankSeparatorStart = tabSeparator[0];
  227. blankSeparatorEnd = tabSeparator[1];
  228. blankSeparatorStartRegexp = getBlankSeparatorRegexp(blankSeparatorStart);
  229. blankSeparatorEndRegexp = getBlankSeparatorRegexp(blankSeparatorEnd);
  230. blanksRegexp = "/"+blankSeparatorStartRegexp+"[^"+blankSeparatorStartRegexp+"]*"+blankSeparatorEndRegexp+"/g";
  231. updateBlanks();
  232. }
  233. // this function is the same than the PHP one
  234. // if modify it modify the php one escapeForRegexp
  235. function getBlankSeparatorRegexp(inTxt)
  236. {
  237. var tabSpecialChar = new Array(".", "+", "*", "?", "[", "^", "]", "$", "(", ")",
  238. "{", "}", "=", "!", "<", ">", "|", ":", "-", ")");
  239. for (var i=0; i < tabSpecialChar.length; i++) {
  240. if (inTxt == tabSpecialChar[i]) {
  241. return "\\\"+inTxt;
  242. }
  243. }
  244. return inTxt;
  245. }
  246. // this function is the same than the PHP one
  247. // if modify it modify the php one getAllowedSeparator
  248. function getSeparatorFromNumber(innumber)
  249. {
  250. tabSeparator = new Array();
  251. tabSeparator[0] = new Array("[", "]");
  252. tabSeparator[1] = new Array("{", "}");
  253. tabSeparator[2] = new Array("(", ")");
  254. tabSeparator[3] = new Array("*", "*");
  255. tabSeparator[4] = new Array("#", "#");
  256. tabSeparator[5] = new Array("%", "%");
  257. tabSeparator[6] = new Array("$", "$");
  258. return tabSeparator[innumber];
  259. }
  260. function trimBlanksBetweenSeparator(inTxt, inSeparatorStart, inSeparatorEnd)
  261. {
  262. var result = inTxt
  263. result = result.replace(inSeparatorStart, "");
  264. result = result.replace(inSeparatorEnd, "");
  265. result = result.trim();
  266. return inSeparatorStart+result+inSeparatorEnd;
  267. }
  268. </script>';
  269. // answer
  270. $form->addLabel(
  271. null,
  272. get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank')
  273. );
  274. $form->addElement(
  275. 'html_editor',
  276. 'answer',
  277. Display::return_icon('fill_field.png'),
  278. ['id' => 'answer'],
  279. array('ToolbarSet' => 'TestQuestionDescription')
  280. );
  281. $form->addRule('answer', get_lang('GiveText'), 'required');
  282. //added multiple answers
  283. $form->addElement('checkbox', 'multiple_answer', '', get_lang('FillInBlankSwitchable'));
  284. $form->addElement(
  285. 'select',
  286. 'select_separator',
  287. get_lang("SelectFillTheBlankSeparator"),
  288. self::getAllowedSeparatorForSelect(),
  289. ' id="select_separator" style="width:150px" onchange="changeBlankSeparator()" '
  290. );
  291. $form->addLabel(
  292. null,
  293. '<input type="button" onclick="updateBlanks()" value="'.get_lang('RefreshBlanks').'" class="btn btn-default" />'
  294. );
  295. $form->addHtml('<div id="blanks_weighting"></div>');
  296. global $text;
  297. // setting the save button here and not in the question class.php
  298. $form->addHtml('<div id="defineoneblank" style="color:#D04A66; margin-left:160px">'.get_lang('DefineBlanks').'</div>');
  299. $form->addButtonSave($text, 'submitQuestion');
  300. if (!empty($this->id)) {
  301. $form->setDefaults($defaults);
  302. } else {
  303. if ($this->isContent == 1) {
  304. $form->setDefaults($defaults);
  305. }
  306. }
  307. }
  308. /**
  309. * @inheritdoc
  310. */
  311. public function processAnswersCreation($form, $exercise)
  312. {
  313. $answer = $form->getSubmitValue('answer');
  314. // Due the ckeditor transform the elements to their HTML value
  315. //$answer = api_html_entity_decode($answer, ENT_QUOTES, $charset);
  316. //$answer = htmlentities(api_utf8_encode($answer));
  317. // remove the "::" eventually written by the user
  318. $answer = str_replace('::', '', $answer);
  319. // remove starting and ending space and &nbsp;
  320. $answer = api_preg_replace("/\xc2\xa0/", " ", $answer);
  321. // start and end separator
  322. $blankStartSeparator = self::getStartSeparator($form->getSubmitValue('select_separator'));
  323. $blankEndSeparator = self::getEndSeparator($form->getSubmitValue('select_separator'));
  324. $blankStartSeparatorRegexp = self::escapeForRegexp($blankStartSeparator);
  325. $blankEndSeparatorRegexp = self::escapeForRegexp($blankEndSeparator);
  326. // remove spaces at the beginning and the end of text in square brackets
  327. $answer = preg_replace_callback(
  328. "/".$blankStartSeparatorRegexp."[^]]+".$blankEndSeparatorRegexp."/",
  329. function ($matches) use ($blankStartSeparator, $blankEndSeparator) {
  330. $matchingResult = $matches[0];
  331. $matchingResult = trim($matchingResult, $blankStartSeparator);
  332. $matchingResult = trim($matchingResult, $blankEndSeparator);
  333. $matchingResult = trim($matchingResult);
  334. // remove forbidden chars
  335. $matchingResult = str_replace("/\\/", "", $matchingResult);
  336. $matchingResult = str_replace('/"/', "", $matchingResult);
  337. return $blankStartSeparator.$matchingResult.$blankEndSeparator;
  338. },
  339. $answer
  340. );
  341. // get the blanks weightings
  342. $nb = preg_match_all(
  343. '/'.$blankStartSeparatorRegexp.'[^'.$blankStartSeparatorRegexp.']*'.$blankEndSeparatorRegexp.'/',
  344. $answer,
  345. $blanks
  346. );
  347. if (isset($_GET['editQuestion'])) {
  348. $this->weighting = 0;
  349. }
  350. /* if we have some [tobefound] in the text
  351. build the string to save the following in the answers table
  352. <p>I use a [computer] and a [pen].</p>
  353. becomes
  354. <p>I use a [computer] and a [pen].</p>::100,50:100,50@1
  355. ++++++++-------**
  356. --- -- --- -- -
  357. A B (C) (D)(E)
  358. +++++++ : required, weighting of each words
  359. ------- : optional, input width to display, 200 if not present
  360. ** : equal @1 if "Allow answers order switches" has been checked, @ otherwise
  361. A : weighting for the word [computer]
  362. B : weighting for the word [pen]
  363. C : input width for the word [computer]
  364. D : input width for the word [pen]
  365. E : equal @1 if "Allow answers order switches" has been checked, @ otherwise
  366. */
  367. if ($nb > 0) {
  368. $answer .= '::';
  369. // weighting
  370. for ($i = 0; $i < $nb; ++$i) {
  371. // enter the weighting of word $i
  372. $answer .= $form->getSubmitValue('weighting['.$i.']');
  373. // not the last word, add ","
  374. if ($i != $nb - 1) {
  375. $answer .= ",";
  376. }
  377. // calculate the global weighting for the question
  378. $this -> weighting += $form->getSubmitValue('weighting['.$i.']');
  379. }
  380. // input width
  381. $answer .= ":";
  382. for ($i = 0; $i < $nb; ++$i) {
  383. // enter the width of input for word $i
  384. $answer .= $form->getSubmitValue('sizeofinput['.$i.']');
  385. // not the last word, add ","
  386. if ($i != $nb - 1) {
  387. $answer .= ",";
  388. }
  389. }
  390. }
  391. // write the blank separator code number
  392. // see function getAllowedSeparator
  393. /*
  394. 0 [...]
  395. 1 {...}
  396. 2 (...)
  397. 3 *...*
  398. 4 #...#
  399. 5 %...%
  400. 6 $...$
  401. */
  402. $answer .= ":".$form->getSubmitValue('select_separator');
  403. // Allow answers order switches
  404. $is_multiple = $form -> getSubmitValue('multiple_answer');
  405. $answer .= '@'.$is_multiple;
  406. $this->save($exercise);
  407. $objAnswer = new Answer($this->id);
  408. $objAnswer->createAnswer($answer, 0, '', 0, 1);
  409. $objAnswer->save();
  410. }
  411. /**
  412. * @inheritdoc
  413. */
  414. public function return_header($exercise, $counter = null, $score = null)
  415. {
  416. $header = parent::return_header($exercise, $counter, $score);
  417. $header .= '<table class="'.$this->question_table_class.'">
  418. <tr>
  419. <th>'.get_lang("Answer").'</th>
  420. </tr>';
  421. return $header;
  422. }
  423. /**
  424. * @param int $currentQuestion
  425. * @param int $questionId
  426. * @param string $correctItem
  427. * @param array $attributes
  428. * @param string $answer
  429. * @param array $listAnswersInfo
  430. * @param boolean $displayForStudent
  431. * @param int $inBlankNumber
  432. * @return string
  433. */
  434. public static function getFillTheBlankHtml(
  435. $currentQuestion,
  436. $questionId,
  437. $correctItem,
  438. $attributes,
  439. $answer,
  440. $listAnswersInfo,
  441. $displayForStudent,
  442. $inBlankNumber
  443. ) {
  444. $result = '';
  445. $inTabTeacherSolution = $listAnswersInfo['tabwords'];
  446. $inTeacherSolution = $inTabTeacherSolution[$inBlankNumber];
  447. switch (self::getFillTheBlankAnswerType($inTeacherSolution)) {
  448. case self::FILL_THE_BLANK_MENU:
  449. $selected = '';
  450. // the blank menu
  451. // display a menu from answer separated with |
  452. // if display for student, shuffle the correct answer menu
  453. $listMenu = self::getFillTheBlankMenuAnswers($inTeacherSolution, $displayForStudent);
  454. $resultOptions = ['' => '--'];
  455. foreach ($listMenu as $item) {
  456. $item = self::trimOption($item);
  457. $resultOptions[$item] = $item;
  458. }
  459. for ($k = 0; $k < count($listMenu); $k++) {
  460. if ($correctItem == $listMenu[$k]) {
  461. $selected = $k;
  462. break;
  463. }
  464. // if in teacher view, display the first item by default, which is the right answer
  465. if ($k == 0 && !$displayForStudent) {
  466. $selected = $k;
  467. break;
  468. }
  469. }
  470. $result = Display::select(
  471. "choice[$questionId][]",
  472. $resultOptions,
  473. $selected,
  474. ['class' => 'selectpicker'],
  475. false
  476. );
  477. break;
  478. case self::FILL_THE_BLANK_SEVERAL_ANSWER:
  479. //no break
  480. case self::FILL_THE_BLANK_STANDARD:
  481. default:
  482. $attributes['id'] = 'choice_id_'.$currentQuestion.'_'.$inBlankNumber;
  483. $result = Display::input(
  484. 'text',
  485. "choice[$questionId][]",
  486. $correctItem,
  487. $attributes
  488. );
  489. break;
  490. }
  491. return $result;
  492. }
  493. private static function trimOption($text)
  494. {
  495. $converted = strtr($text, array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)));
  496. $trimmed = trim($converted, chr(0xC2).chr(0xA0).' ');
  497. return $trimmed;
  498. }
  499. /**
  500. * Return an array with the different choices available
  501. * when the answers between bracket show as a menu
  502. * @param string $correctAnswer
  503. * @param bool $displayForStudent true if we want to shuffle the choices of the menu for students
  504. *
  505. * @return array
  506. */
  507. public static function getFillTheBlankMenuAnswers($correctAnswer, $displayForStudent)
  508. {
  509. $list = api_preg_split("/\|/", $correctAnswer);
  510. if ($displayForStudent) {
  511. shuffle($list);
  512. }
  513. return $list;
  514. }
  515. /**
  516. * Return the array index of the student answer
  517. * @param string $correctAnswer the menu Choice1|Choice2|Choice3
  518. * @param string $studentAnswer the student answer must be Choice1 or Choice2 or Choice3
  519. *
  520. * @return int in the example 0 1 or 2 depending of the choice of the student
  521. */
  522. public static function getFillTheBlankMenuAnswerNum($correctAnswer, $studentAnswer)
  523. {
  524. $listChoices = self::getFillTheBlankMenuAnswers($correctAnswer, false);
  525. foreach ($listChoices as $num => $value) {
  526. if ($value == $studentAnswer) {
  527. return $num;
  528. }
  529. }
  530. // should not happened, because student choose the answer in a menu of possible answers
  531. return -1;
  532. }
  533. /**
  534. * Return the possible answer if the answer between brackets is a multiple choice menu
  535. * @param string $correctAnswer
  536. *
  537. * @return array
  538. */
  539. public static function getFillTheBlankSeveralAnswers($correctAnswer)
  540. {
  541. // is answer||Answer||response||Response , mean answer or Answer ...
  542. $listSeveral = api_preg_split("/\|\|/", $correctAnswer);
  543. return $listSeveral;
  544. }
  545. /**
  546. * Return true if student answer is right according to the correctAnswer
  547. * it is not as simple as equality, because of the type of Fill The Blank question
  548. * eg : studentAnswer = 'Un' and correctAnswer = 'Un||1||un'
  549. * @param string $studentAnswer [studentanswer] of the info array of the answer field
  550. * @param string $correctAnswer [tabwords] of the info array of the answer field
  551. *
  552. * @return bool
  553. */
  554. public static function isGoodStudentAnswer($studentAnswer, $correctAnswer)
  555. {
  556. switch (self::getFillTheBlankAnswerType($correctAnswer)) {
  557. case self::FILL_THE_BLANK_MENU:
  558. $listMenu = self::getFillTheBlankMenuAnswers($correctAnswer, false);
  559. $result = self::trimOption($listMenu[0]) == $studentAnswer;
  560. break;
  561. case self::FILL_THE_BLANK_SEVERAL_ANSWER:
  562. // the answer must be one of the choice made
  563. $listSeveral = self::getFillTheBlankSeveralAnswers($correctAnswer);
  564. $listSeveral = array_map(function($item) {
  565. return self::trimOption($item);
  566. }, $listSeveral);
  567. $result = in_array($studentAnswer, $listSeveral);
  568. break;
  569. case self::FILL_THE_BLANK_STANDARD:
  570. default:
  571. $result = $studentAnswer == self::trimOption($correctAnswer);
  572. break;
  573. }
  574. return $result;
  575. }
  576. /**
  577. * @param string $correctAnswer
  578. *
  579. * @return int
  580. */
  581. public static function getFillTheBlankAnswerType($correctAnswer)
  582. {
  583. if (api_strpos($correctAnswer, "|") && !api_strpos($correctAnswer, "||")) {
  584. return self::FILL_THE_BLANK_MENU;
  585. } elseif (api_strpos($correctAnswer, "||")) {
  586. return self::FILL_THE_BLANK_SEVERAL_ANSWER;
  587. } else {
  588. return self::FILL_THE_BLANK_STANDARD;
  589. }
  590. }
  591. /**
  592. * Return information about the answer
  593. * @param string $userAnswer the text of the answer of the question
  594. * @param bool $isStudentAnswer true if it's a student answer false the empty question model
  595. *
  596. * @return array of information about the answer
  597. */
  598. public static function getAnswerInfo($userAnswer = "", $isStudentAnswer = false)
  599. {
  600. $listAnswerResults = array();
  601. $listAnswerResults['text'] = '';
  602. $listAnswerResults['wordsCount'] = 0;
  603. $listAnswerResults['tabwordsbracket'] = array();
  604. $listAnswerResults['tabwords'] = array();
  605. $listAnswerResults['tabweighting'] = array();
  606. $listAnswerResults['tabinputsize'] = array();
  607. $listAnswerResults['switchable'] = '';
  608. $listAnswerResults['studentanswer'] = array();
  609. $listAnswerResults['studentscore'] = array();
  610. $listAnswerResults['blankseparatornumber'] = 0;
  611. $listDoubleColon = array();
  612. api_preg_match("/(.*)::(.*)$/s", $userAnswer, $listResult);
  613. if (count($listResult) < 2) {
  614. $listDoubleColon[] = '';
  615. $listDoubleColon[] = '';
  616. } else {
  617. $listDoubleColon[] = $listResult[1];
  618. $listDoubleColon[] = $listResult[2];
  619. }
  620. $listAnswerResults['systemstring'] = $listDoubleColon[1];
  621. // make sure we only take the last bit to find special marks
  622. $listArobaseSplit = explode('@', $listDoubleColon[1]);
  623. if (count($listArobaseSplit) < 2) {
  624. $listArobaseSplit[1] = '';
  625. }
  626. // take the complete string except after the last '::'
  627. $listDetails = explode(":", $listArobaseSplit[0]);
  628. // < number of item after the ::[score]:[size]:[separator_id]@ , here there are 3
  629. if (count($listDetails) < 3) {
  630. $listWeightings = explode(',', $listDetails[0]);
  631. $listSizeOfInput = array();
  632. for ($i = 0; $i < count($listWeightings); $i++) {
  633. $listSizeOfInput[] = 200;
  634. }
  635. $blankSeparatorNumber = 0; // 0 is [...]
  636. } else {
  637. $listWeightings = explode(',', $listDetails[0]);
  638. $listSizeOfInput = explode(',', $listDetails[1]);
  639. $blankSeparatorNumber = $listDetails[2];
  640. }
  641. $listAnswerResults['text'] = $listDoubleColon[0];
  642. $listAnswerResults['tabweighting'] = $listWeightings;
  643. $listAnswerResults['tabinputsize'] = $listSizeOfInput;
  644. $listAnswerResults['switchable'] = $listArobaseSplit[1];
  645. $listAnswerResults['blankseparatorstart'] = self::getStartSeparator($blankSeparatorNumber);
  646. $listAnswerResults['blankseparatorend'] = self::getEndSeparator($blankSeparatorNumber);
  647. $listAnswerResults['blankseparatornumber'] = $blankSeparatorNumber;
  648. $blankCharStart = self::getStartSeparator($blankSeparatorNumber);
  649. $blankCharEnd = self::getEndSeparator($blankSeparatorNumber);
  650. $blankCharStartForRegexp = self::escapeForRegexp($blankCharStart);
  651. $blankCharEndForRegexp = self::escapeForRegexp($blankCharEnd);
  652. // get all blanks words
  653. $listAnswerResults['wordsCount'] = api_preg_match_all(
  654. '/'.$blankCharStartForRegexp.'[^'.$blankCharEndForRegexp.']*'.$blankCharEndForRegexp.'/',
  655. $listDoubleColon[0],
  656. $listWords
  657. );
  658. if ($listAnswerResults['wordsCount'] > 0) {
  659. $listAnswerResults['tabwordsbracket'] = $listWords[0];
  660. // remove [ and ] in string
  661. array_walk(
  662. $listWords[0],
  663. function(&$value, $key, $tabBlankChar) {
  664. $trimChars = '';
  665. for ($i = 0; $i < count($tabBlankChar); $i++) {
  666. $trimChars .= $tabBlankChar[$i];
  667. }
  668. $value = trim($value, $trimChars);
  669. },
  670. array($blankCharStart, $blankCharEnd)
  671. );
  672. $listAnswerResults['tabwords'] = $listWords[0];
  673. }
  674. // get all common words
  675. $commonWords = api_preg_replace(
  676. '/'.$blankCharStartForRegexp.'[^'.$blankCharEndForRegexp.']*'.$blankCharEndForRegexp.'/',
  677. "::",
  678. $listDoubleColon[0]
  679. );
  680. // if student answer, the second [] is the student answer,
  681. // the third is if student scored or not
  682. $listBrackets = array();
  683. $listWords = array();
  684. if ($isStudentAnswer) {
  685. for ($i = 0; $i < count($listAnswerResults['tabwords']); $i++) {
  686. $listBrackets[] = $listAnswerResults['tabwordsbracket'][$i];
  687. $listWords[] = $listAnswerResults['tabwords'][$i];
  688. if ($i + 1 < count($listAnswerResults['tabwords'])) {
  689. // should always be
  690. $i++;
  691. }
  692. $listAnswerResults['studentanswer'][] = $listAnswerResults['tabwords'][$i];
  693. if ($i + 1 < count($listAnswerResults['tabwords'])) {
  694. // should always be
  695. $i++;
  696. }
  697. $listAnswerResults['studentscore'][] = $listAnswerResults['tabwords'][$i];
  698. }
  699. $listAnswerResults['tabwords'] = $listWords;
  700. $listAnswerResults['tabwordsbracket'] = $listBrackets;
  701. // if we are in student view, we've got 3 times :::::: for common words
  702. $commonWords = api_preg_replace("/::::::/", "::", $commonWords);
  703. }
  704. $listAnswerResults['commonwords'] = explode("::", $commonWords);
  705. return $listAnswerResults;
  706. }
  707. /**
  708. * Return an array of student state answers for fill the blank questions
  709. * for each students that answered the question
  710. * -2 : didn't answer
  711. * -1 : student answer is wrong
  712. * 0 : student answer is correct
  713. * >0 : for fill the blank question with choice menu, is the index of the student answer (right answer indice is 0)
  714. *
  715. * @param integer $testId
  716. * @param integer $questionId
  717. * @param $studentsIdList
  718. * @param string $startDate
  719. * @param string $endDate
  720. * @param bool $useLastAnsweredAttempt
  721. * @return array
  722. * (
  723. * [student_id] => Array
  724. * (
  725. * [first fill the blank for question] => -1
  726. * [second fill the blank for question] => 2
  727. * [third fill the blank for question] => -1
  728. * )
  729. * )
  730. */
  731. public static function getFillTheBlankTabResult(
  732. $testId,
  733. $questionId,
  734. $studentsIdList,
  735. $startDate,
  736. $endDate,
  737. $useLastAnsweredAttempt = true
  738. ) {
  739. $tblTrackEAttempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  740. $tblTrackEExercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  741. $courseId = api_get_course_int_id();
  742. // If no user has answered questions, no need to go further. Return empty array.
  743. if (empty($studentsIdList)) {
  744. return array();
  745. }
  746. // request to have all the answers of student for this question
  747. // student may have doing it several time
  748. // student may have not answered the bracket id, in this case, is result of the answer is empty
  749. // we got the less recent attempt first
  750. $sql = 'SELECT * FROM '.$tblTrackEAttempt.' tea
  751. LEFT JOIN '.$tblTrackEExercise.' tee
  752. ON
  753. tee.exe_id = tea.exe_id AND
  754. tea.c_id = '.$courseId.' AND
  755. exe_exo_id = '.$testId.'
  756. WHERE
  757. tee.c_id = '.$courseId.' AND
  758. question_id = '.$questionId.' AND
  759. tea.user_id IN ('.implode(',', $studentsIdList).') AND
  760. tea.tms >= "'.$startDate.'" AND
  761. tea.tms <= "'.$endDate.'"
  762. ORDER BY user_id, tea.exe_id;
  763. ';
  764. $res = Database::query($sql);
  765. $tabUserResult = array();
  766. // foreach attempts for all students starting with his older attempt
  767. while ($data = Database::fetch_array($res)) {
  768. $tabAnswer = self::getAnswerInfo($data['answer'], true);
  769. // for each bracket to find in this question
  770. foreach ($tabAnswer['studentanswer'] as $bracketNumber => $studentAnswer) {
  771. if ($tabAnswer['studentanswer'][$bracketNumber] != '') {
  772. // student has answered this bracket, cool
  773. switch (self::getFillTheBlankAnswerType($tabAnswer['tabwords'][$bracketNumber])) {
  774. case self::FILL_THE_BLANK_MENU:
  775. // get the indice of the choosen answer in the menu
  776. // we know that the right answer is the first entry of the menu, ie 0
  777. // (remember, menu entries are shuffled when taking the test)
  778. $tabUserResult[$data['user_id']][$bracketNumber] = self::getFillTheBlankMenuAnswerNum(
  779. $tabAnswer['tabwords'][$bracketNumber],
  780. $tabAnswer['studentanswer'][$bracketNumber]
  781. );
  782. break;
  783. default:
  784. if (self::isGoodStudentAnswer(
  785. $tabAnswer['studentanswer'][$bracketNumber],
  786. $tabAnswer['tabwords'][$bracketNumber]
  787. )
  788. ) {
  789. $tabUserResult[$data['user_id']][$bracketNumber] = 0; // right answer
  790. } else {
  791. $tabUserResult[$data['user_id']][$bracketNumber] = -1; // wrong answer
  792. }
  793. }
  794. } else {
  795. // student didn't answer this bracket
  796. if ($useLastAnsweredAttempt) {
  797. // if we take into account the last answered attempt
  798. if (!isset($tabUserResult[$data['user_id']][$bracketNumber])) {
  799. $tabUserResult[$data['user_id']][$bracketNumber] = -2; // not answered
  800. }
  801. } else {
  802. // we take the last attempt, even if the student answer the question before
  803. $tabUserResult[$data['user_id']][$bracketNumber] = -2; // not answered
  804. }
  805. }
  806. }
  807. }
  808. return $tabUserResult;
  809. }
  810. /**
  811. * Return the number of student that give at leat an answer in the fill the blank test
  812. * @param array $resultList
  813. * @return int
  814. */
  815. public static function getNbResultFillBlankAll($resultList)
  816. {
  817. $outRes = 0;
  818. // for each student in group
  819. foreach ($resultList as $userId => $tabValue) {
  820. $found = false;
  821. // for each bracket, if student has at least one answer ( choice > -2) then he pass the question
  822. foreach ($tabValue as $i => $choice) {
  823. if ($choice > -2 && !$found) {
  824. $outRes++;
  825. $found = true;
  826. }
  827. }
  828. }
  829. return $outRes;
  830. }
  831. /**
  832. * Replace the occurrence of blank word with [correct answer][student answer][answer is correct]
  833. * @param array $listWithStudentAnswer
  834. *
  835. * @return string
  836. */
  837. public static function getAnswerInStudentAttempt($listWithStudentAnswer)
  838. {
  839. $separatorStart = $listWithStudentAnswer['blankseparatorstart'];
  840. $separatorEnd = $listWithStudentAnswer['blankseparatorend'];
  841. // lets rebuild the sentence with [correct answer][student answer][answer is correct]
  842. $result = '';
  843. for ($i = 0; $i < count($listWithStudentAnswer['commonwords']) - 1; $i++) {
  844. $result .= $listWithStudentAnswer['commonwords'][$i];
  845. $result .= $listWithStudentAnswer['tabwordsbracket'][$i];
  846. $result .= $separatorStart.$listWithStudentAnswer['studentanswer'][$i].$separatorEnd;
  847. $result .= $separatorStart.$listWithStudentAnswer['studentscore'][$i].$separatorEnd;
  848. }
  849. $result .= $listWithStudentAnswer['commonwords'][$i];
  850. $result .= "::";
  851. // add the system string
  852. $result .= $listWithStudentAnswer['systemstring'];
  853. return $result;
  854. }
  855. /**
  856. * This function is the same than the js one above getBlankSeparatorRegexp
  857. * @param string $inChar
  858. *
  859. * @return string
  860. */
  861. public static function escapeForRegexp($inChar)
  862. {
  863. $listChars = [
  864. ".",
  865. "+",
  866. "*",
  867. "?",
  868. "[",
  869. "^",
  870. "]",
  871. "$",
  872. "(",
  873. ")",
  874. "{",
  875. "}",
  876. "=",
  877. "!",
  878. ">",
  879. "|",
  880. ":",
  881. "-",
  882. ")",
  883. ];
  884. if (in_array($inChar, $listChars)) {
  885. return "\\".$inChar;
  886. } else {
  887. return $inChar;
  888. }
  889. }
  890. /**
  891. * return $text protected for use in regexp
  892. * @param string $text
  893. *
  894. * @return string
  895. */
  896. public static function getRegexpProtected($text)
  897. {
  898. $listRegexpCharacters = [
  899. "/",
  900. ".",
  901. "+",
  902. "*",
  903. "?",
  904. "[",
  905. "^",
  906. "]",
  907. "$",
  908. "(",
  909. ")",
  910. "{",
  911. "}",
  912. "=",
  913. "!",
  914. ">",
  915. "|",
  916. ":",
  917. "-",
  918. ")",
  919. ];
  920. $result = $text;
  921. for ($i = 0; $i < count($listRegexpCharacters); $i++) {
  922. $result = str_replace($listRegexpCharacters[$i], "\\".$listRegexpCharacters[$i], $result);
  923. }
  924. return $result;
  925. }
  926. /**
  927. * This function must be the same than the js one getSeparatorFromNumber above
  928. * @return array
  929. */
  930. public static function getAllowedSeparator()
  931. {
  932. $fillBlanksAllowedSeparator = array(
  933. array('[', ']'),
  934. array('{', '}'),
  935. array('(', ')'),
  936. array('*', '*'),
  937. array('#', '#'),
  938. array('%', '%'),
  939. array('$', '$'),
  940. );
  941. return $fillBlanksAllowedSeparator;
  942. }
  943. /**
  944. * return the start separator for answer
  945. * @param string $number
  946. *
  947. * @return string
  948. */
  949. public static function getStartSeparator($number)
  950. {
  951. $listSeparators = self::getAllowedSeparator();
  952. return $listSeparators[$number][0];
  953. }
  954. /**
  955. * return the end separator for answer
  956. * @param string $number
  957. *
  958. * @return string
  959. */
  960. public static function getEndSeparator($number)
  961. {
  962. $listSeparators = self::getAllowedSeparator();
  963. return $listSeparators[$number][1];
  964. }
  965. /**
  966. * Return as a description text, array of allowed separators for question
  967. * eg: array("[...]", "(...)")
  968. * @return array
  969. */
  970. public static function getAllowedSeparatorForSelect()
  971. {
  972. $listResults = array();
  973. $fillBlanksAllowedSeparator = self::getAllowedSeparator();
  974. for ($i = 0; $i < count($fillBlanksAllowedSeparator); $i++) {
  975. $listResults[] = $fillBlanksAllowedSeparator[$i][0]."...".$fillBlanksAllowedSeparator[$i][1];
  976. }
  977. return $listResults;
  978. }
  979. /**
  980. * return the code number of the separator for the question
  981. * @param string $startSeparator
  982. * @param string $endSeparator
  983. *
  984. * @return int
  985. */
  986. public function getDefaultSeparatorNumber($startSeparator, $endSeparator)
  987. {
  988. $listSeparators = self::getAllowedSeparator();
  989. $result = 0;
  990. for ($i = 0; $i < count($listSeparators); $i++) {
  991. if ($listSeparators[$i][0] == $startSeparator &&
  992. $listSeparators[$i][1] == $endSeparator
  993. ) {
  994. $result = $i;
  995. }
  996. }
  997. return $result;
  998. }
  999. /**
  1000. * return the HTML display of the answer
  1001. * @param string $answer
  1002. * @param int $feedbackType
  1003. * @param bool $resultsDisabled
  1004. * @param bool $showTotalScoreAndUserChoices
  1005. * @return string
  1006. */
  1007. public static function getHtmlDisplayForAnswer(
  1008. $answer,
  1009. $feedbackType,
  1010. $resultsDisabled = false,
  1011. $showTotalScoreAndUserChoices = false
  1012. ) {
  1013. $result = '';
  1014. $listStudentAnswerInfo = self::getAnswerInfo($answer, true);
  1015. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  1016. if ($showTotalScoreAndUserChoices) {
  1017. $resultsDisabled = false;
  1018. } else {
  1019. $resultsDisabled = true;
  1020. }
  1021. }
  1022. // rebuild the answer with good HTML style
  1023. // this is the student answer, right or wrong
  1024. for ($i = 0; $i < count($listStudentAnswerInfo['studentanswer']); $i++) {
  1025. if ($listStudentAnswerInfo['studentscore'][$i] == 1) {
  1026. $listStudentAnswerInfo['studentanswer'][$i] = self::getHtmlRightAnswer(
  1027. $listStudentAnswerInfo['studentanswer'][$i],
  1028. $listStudentAnswerInfo['tabwords'][$i],
  1029. $feedbackType,
  1030. $resultsDisabled,
  1031. $showTotalScoreAndUserChoices
  1032. );
  1033. } else {
  1034. $listStudentAnswerInfo['studentanswer'][$i] = self::getHtmlWrongAnswer(
  1035. $listStudentAnswerInfo['studentanswer'][$i],
  1036. $listStudentAnswerInfo['tabwords'][$i],
  1037. $feedbackType,
  1038. $resultsDisabled,
  1039. $showTotalScoreAndUserChoices
  1040. );
  1041. }
  1042. }
  1043. // rebuild the sentence with student answer inserted
  1044. for ($i = 0; $i < count($listStudentAnswerInfo['commonwords']); $i++) {
  1045. $result .= isset($listStudentAnswerInfo['commonwords'][$i]) ? $listStudentAnswerInfo['commonwords'][$i] : '';
  1046. $result .= isset($listStudentAnswerInfo['studentanswer'][$i]) ? $listStudentAnswerInfo['studentanswer'][$i] : '';
  1047. }
  1048. // the last common word (should be </p>)
  1049. $result .= isset($listStudentAnswerInfo['commonwords'][$i]) ? $listStudentAnswerInfo['commonwords'][$i] : '';
  1050. return $result;
  1051. }
  1052. /**
  1053. * return the HTML code of answer for correct and wrong answer
  1054. * @param string $answer
  1055. * @param string $correct
  1056. * @param string $right
  1057. * @param int $feedbackType
  1058. * @param bool $resultsDisabled
  1059. * @param bool $showTotalScoreAndUserChoices
  1060. * @return string
  1061. */
  1062. public static function getHtmlAnswer(
  1063. $answer,
  1064. $correct,
  1065. $right,
  1066. $feedbackType,
  1067. $resultsDisabled = false,
  1068. $showTotalScoreAndUserChoices = false
  1069. ) {
  1070. $hideExpectedAnswer = false;
  1071. if ($feedbackType == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  1072. $hideExpectedAnswer = true;
  1073. }
  1074. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  1075. if ($showTotalScoreAndUserChoices) {
  1076. $hideExpectedAnswer = false;
  1077. } else {
  1078. $hideExpectedAnswer = true;
  1079. }
  1080. }
  1081. $style = "color: green";
  1082. if (!$right) {
  1083. $style = "color: red; text-decoration: line-through;";
  1084. }
  1085. $type = self::getFillTheBlankAnswerType($correct);
  1086. switch ($type) {
  1087. case self::FILL_THE_BLANK_MENU:
  1088. $correctAnswerHtml = '';
  1089. $listPossibleAnswers = self::getFillTheBlankMenuAnswers($correct, false);
  1090. $correctAnswerHtml .= "<span style='color: green'>".$listPossibleAnswers[0]."</span>";
  1091. $correctAnswerHtml .= " <span style='font-weight:normal'>(";
  1092. for ($i = 1; $i < count($listPossibleAnswers); $i++) {
  1093. $correctAnswerHtml .= $listPossibleAnswers[$i];
  1094. if ($i != count($listPossibleAnswers) - 1) {
  1095. $correctAnswerHtml .= " | ";
  1096. }
  1097. }
  1098. $correctAnswerHtml .= ")</span>";
  1099. break;
  1100. case self::FILL_THE_BLANK_SEVERAL_ANSWER:
  1101. $listCorrects = explode("||", $correct);
  1102. $firstCorrect = $correct;
  1103. if (count($listCorrects) > 0) {
  1104. $firstCorrect = $listCorrects[0];
  1105. }
  1106. $correctAnswerHtml = "<span style='color: green'>".$firstCorrect."</span>";
  1107. break;
  1108. case self::FILL_THE_BLANK_STANDARD:
  1109. default:
  1110. $correctAnswerHtml = "<span style='color: green'>".$correct."</span>";
  1111. }
  1112. if ($hideExpectedAnswer) {
  1113. $correctAnswerHtml = "<span title='".get_lang("ExerciseWithFeedbackWithoutCorrectionComment")."'> - </span>";
  1114. }
  1115. $result = "<span style='border:1px solid black; border-radius:5px; padding:2px; font-weight:bold;'>";
  1116. $result .= "<span style='$style'>".$answer."</span>";
  1117. $result .= "&nbsp;<span style='font-size:120%;'>/</span>&nbsp;";
  1118. $result .= $correctAnswerHtml;
  1119. $result .= "</span>";
  1120. return $result;
  1121. }
  1122. /**
  1123. * return HTML code for correct answer
  1124. * @param string $answer
  1125. * @param string $correct
  1126. * @param bool $resultsDisabled
  1127. *
  1128. * @return string
  1129. */
  1130. public static function getHtmlRightAnswer(
  1131. $answer,
  1132. $correct,
  1133. $feedbackType,
  1134. $resultsDisabled = false,
  1135. $showTotalScoreAndUserChoices = false
  1136. ) {
  1137. return self::getHtmlAnswer(
  1138. $answer,
  1139. $correct,
  1140. true,
  1141. $feedbackType,
  1142. $resultsDisabled,
  1143. $showTotalScoreAndUserChoices
  1144. );
  1145. }
  1146. /**
  1147. * return HTML code for wrong answer
  1148. * @param string $answer
  1149. * @param string $correct
  1150. * @param bool $resultsDisabled
  1151. *
  1152. * @return string
  1153. */
  1154. public static function getHtmlWrongAnswer(
  1155. $answer,
  1156. $correct,
  1157. $feedbackType,
  1158. $resultsDisabled = false,
  1159. $showTotalScoreAndUserChoices = false
  1160. ) {
  1161. return self::getHtmlAnswer(
  1162. $answer,
  1163. $correct,
  1164. false,
  1165. $feedbackType,
  1166. $resultsDisabled,
  1167. $showTotalScoreAndUserChoices
  1168. );
  1169. }
  1170. /**
  1171. * Check if a answer is correct by its text
  1172. * @param string $answerText
  1173. * @return bool
  1174. */
  1175. public static function isCorrect($answerText)
  1176. {
  1177. $answerInfo = self::getAnswerInfo($answerText, true);
  1178. $correctAnswerList = $answerInfo['tabwords'];
  1179. $studentAnswer = $answerInfo['studentanswer'];
  1180. $isCorrect = true;
  1181. foreach ($correctAnswerList as $i => $correctAnswer) {
  1182. $isGoodStudentAnswer = self::isGoodStudentAnswer($studentAnswer[$i], $correctAnswer);
  1183. $isCorrect = $isCorrect && $isGoodStudentAnswer;
  1184. }
  1185. return $isCorrect;
  1186. }
  1187. /**
  1188. * Clear the answer entered by student
  1189. * @param string $answer
  1190. * @return string
  1191. */
  1192. public static function clearStudentAnswer($answer)
  1193. {
  1194. $answer = htmlentities(api_utf8_encode($answer), ENT_QUOTES);
  1195. $answer = str_replace('&#039;', '&#39;', $answer); // fix apostrophe
  1196. $answer = api_preg_replace('/\s\s+/', ' ', $answer); // replace excess white spaces
  1197. $answer = strtr($answer, array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)));
  1198. return trim($answer);
  1199. }
  1200. }