matching.class.php 8.8 KB

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