fill_blanks.class.php 54 KB

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