fill_blanks.class.php 44 KB

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