matching.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Matching
  5. * Matching questions type class
  6. *
  7. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  8. * extending the class question
  9. *
  10. * @author Eric Marguin
  11. * @package chamilo.exercise
  12. */
  13. class Matching extends Question
  14. {
  15. static $typePicture = 'matching.png';
  16. static $explanationLangVar = 'Matching';
  17. /**
  18. * Constructor
  19. */
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->type = MATCHING;
  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. $defaults = array();
  33. $nb_matches = $nb_options = 2;
  34. $matches = array();
  35. $answer = null;
  36. $counter = 1;
  37. if (isset($this->id)) {
  38. $answer = new Answer($this->id);
  39. $answer->read();
  40. if (count($answer->nbrAnswers) > 0) {
  41. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  42. $correct = $answer->isCorrect($i);
  43. if (empty($correct)) {
  44. $matches[$answer->selectAutoId($i)] = chr(64 + $counter);
  45. $counter++;
  46. }
  47. }
  48. }
  49. }
  50. if ($form->isSubmitted()) {
  51. $nb_matches = $form->getSubmitValue('nb_matches');
  52. $nb_options = $form->getSubmitValue('nb_options');
  53. if (isset($_POST['lessMatches'])) {
  54. $nb_matches--;
  55. }
  56. if (isset($_POST['moreMatches'])) {
  57. $nb_matches++;
  58. }
  59. if (isset($_POST['lessOptions'])) {
  60. $nb_options--;
  61. }
  62. if (isset($_POST['moreOptions'])) {
  63. $nb_options++;
  64. }
  65. } else if (!empty($this->id)) {
  66. if (count($answer->nbrAnswers) > 0) {
  67. $nb_matches = $nb_options = 0;
  68. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  69. if ($answer->isCorrect($i)) {
  70. $nb_matches++;
  71. $defaults['answer[' . $nb_matches . ']'] = $answer->selectAnswer($i);
  72. $defaults['weighting[' . $nb_matches . ']'] = float_format($answer->selectWeighting($i), 1);
  73. $defaults['matches[' . $nb_matches . ']'] = $answer->correct[$i];
  74. } else {
  75. $nb_options++;
  76. $defaults['option[' . $nb_options . ']'] = $answer->selectAnswer($i);
  77. }
  78. }
  79. }
  80. } else {
  81. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  82. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  83. $defaults['matches[2]'] = '2';
  84. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  85. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  86. }
  87. if (empty($matches)) {
  88. for ($i = 1; $i <= $nb_options; ++$i) {
  89. // fill the array with A, B, C.....
  90. $matches[$i] = chr(64 + $i);
  91. }
  92. } else {
  93. for ($i = $counter; $i <= $nb_options; ++$i) {
  94. // fill the array with A, B, C.....
  95. $matches[$i] = chr(64 + $i);
  96. }
  97. }
  98. $form->addElement('hidden', 'nb_matches', $nb_matches);
  99. $form->addElement('hidden', 'nb_options', $nb_options);
  100. // DISPLAY MATCHES
  101. $html = '<table class="table table-striped table-hover">
  102. <thead>
  103. <tr>
  104. <th width="10">' . get_lang('Number') . '</th>
  105. <th width="85%">' . get_lang('Answer') . '</th>
  106. <th width="15%">' . get_lang('MatchesTo') . '</th>
  107. <th width="10">' . get_lang('Weighting') . '</th>
  108. </tr>
  109. </thead>
  110. <tbody>';
  111. $form->addHeader(get_lang('MakeCorrespond'));
  112. $form->addHtml($html);
  113. if ($nb_matches < 1) {
  114. $nb_matches = 1;
  115. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  116. }
  117. for ($i = 1; $i <= $nb_matches; ++$i) {
  118. $renderer = &$form->defaultRenderer();
  119. $renderer->setElementTemplate(
  120. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  121. "answer[$i]"
  122. );
  123. $renderer->setElementTemplate(
  124. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  125. "matches[$i]"
  126. );
  127. $renderer->setElementTemplate(
  128. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  129. "weighting[$i]"
  130. );
  131. $form->addHtml('<tr>');
  132. $form->addHtml("<td>$i</td>");
  133. $form->addText("answer[$i]", null);
  134. $form->addSelect("matches[$i]", null, $matches);
  135. $form->addText("weighting[$i]", null, true, ['value' => 10]);
  136. $form->addHtml('</tr>');
  137. }
  138. $form->addHtml('</tbody></table>');
  139. $group = array();
  140. $renderer->setElementTemplate('<div class="form-group"><div class="col-sm-offset-2">{element}', 'lessMatches');
  141. $renderer->setElementTemplate('{element}</div></div>', 'moreMatches');
  142. $group[] = $form->addButtonDelete(get_lang('DelElem'), 'lessMatches', true);
  143. $group[] = $form->addButtonCreate(get_lang('AddElem'), 'moreMatches', true);
  144. $form->addGroup($group);
  145. // DISPLAY OPTIONS
  146. $html = '<table class="table table-striped table-hover">
  147. <thead>
  148. <tr>
  149. <th width="15%">' . get_lang('Number') . '</th>
  150. <th width="85%">' . get_lang('Answer') . '</th>
  151. </tr>
  152. </thead>
  153. <tbody>';
  154. $form->addHtml($html);
  155. if ($nb_options < 1) {
  156. $nb_options = 1;
  157. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  158. }
  159. for ($i = 1; $i <= $nb_options; ++$i) {
  160. $renderer = &$form->defaultRenderer();
  161. $renderer->setElementTemplate(
  162. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
  163. "option[$i]"
  164. );
  165. $form->addHtml('<tr>');
  166. $form->addHtml('<td>' . chr(64 + $i) . '</td>');
  167. $form->addText("option[$i]", null);
  168. $form->addHtml('</tr>');
  169. }
  170. $form->addHtml('</table>');
  171. $group = array();
  172. global $text;
  173. // setting the save button here and not in the question class.php
  174. $group[] = $form->addButtonDelete(get_lang('DelElem'), 'lessOptions', true);
  175. $group[] = $form->addButtonCreate(get_lang('AddElem'), 'moreOptions', true);
  176. $group[] = $form->addButtonSave($text, 'submitQuestion', true);
  177. $form->addGroup($group);
  178. if (!empty($this->id)) {
  179. $form->setDefaults($defaults);
  180. } else {
  181. if ($this->isContent == 1) {
  182. $form->setDefaults($defaults);
  183. }
  184. }
  185. $form->setConstants(
  186. array(
  187. 'nb_matches' => $nb_matches,
  188. 'nb_options' => $nb_options
  189. )
  190. );
  191. }
  192. /**
  193. * abstract function which creates the form to create / edit the answers of the question
  194. * @param FormValidator $form
  195. */
  196. public function processAnswersCreation($form)
  197. {
  198. $nb_matches = $form->getSubmitValue('nb_matches');
  199. $nb_options = $form->getSubmitValue('nb_options');
  200. $this->weighting = 0;
  201. $position = 0;
  202. $objAnswer = new Answer($this->id);
  203. // Insert the options
  204. for ($i = 1; $i <= $nb_options; ++$i) {
  205. $position++;
  206. $option = $form->getSubmitValue('option['.$i.']');
  207. $objAnswer->createAnswer($option, 0, '', 0, $position);
  208. }
  209. // Insert the answers
  210. for ($i = 1; $i <= $nb_matches; ++$i) {
  211. $position++;
  212. $answer = $form->getSubmitValue('answer['.$i.']');
  213. $matches = $form->getSubmitValue('matches['.$i.']');
  214. $weighting = $form->getSubmitValue('weighting['.$i.']');
  215. $this->weighting += $weighting;
  216. $objAnswer->createAnswer(
  217. $answer,
  218. $matches,
  219. '',
  220. $weighting,
  221. $position
  222. );
  223. }
  224. $objAnswer->save();
  225. $this->save();
  226. }
  227. /**
  228. * @param null $feedback_type
  229. * @param null $counter
  230. * @param null $score
  231. * @return string
  232. */
  233. public function return_header($feedback_type = null, $counter = null, $score = null)
  234. {
  235. $header = parent::return_header($feedback_type, $counter, $score);
  236. $header .= '<table class="'.$this->question_table_class .'">';
  237. $header .= '<tr>
  238. <th>'.get_lang('ElementList').'</th>
  239. <th>'.get_lang('CorrespondsTo').'</th>
  240. </tr>';
  241. return $header;
  242. }
  243. }