draggable.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * CLASS Draggable
  5. *
  6. * This class allows to instantiate an object of type DRAGGABLE,
  7. * extending the class question
  8. *
  9. * @author Julio Montoya
  10. * @package chamilo.exercise
  11. **/
  12. /**
  13. * Code
  14. */
  15. class Draggable extends Matching
  16. {
  17. static $typePicture = 'matching.gif';
  18. static $explanationLangVar = 'Draggable';
  19. /**
  20. * Constructor
  21. */
  22. public function Draggable()
  23. {
  24. parent::question();
  25. $this->type = DRAGGABLE;
  26. $this->isContent = $this->getIsContent();
  27. }
  28. /**
  29. * Function which redefines Question::createAnswersForm
  30. * @param FormValidator instance
  31. */
  32. public function createAnswersForm($form)
  33. {
  34. $defaults = array();
  35. $nb_matches = $nb_options = 2;
  36. if ($form->isSubmitted()) {
  37. $nb_matches = $form->getSubmitValue('nb_matches');
  38. $nb_options = $form->getSubmitValue('nb_options');
  39. if (isset($_POST['lessMatches'])) {
  40. $nb_matches--;
  41. }
  42. if (isset($_POST['moreMatches'])) {
  43. $nb_matches++;
  44. }
  45. if (isset($_POST['lessOptions'])) {
  46. $nb_options--;
  47. }
  48. if (isset($_POST['moreOptions'])) {
  49. $nb_options++;
  50. }
  51. } else {
  52. if (!empty($this->id)) {
  53. $answer = new Answer($this->id, api_get_course_int_id());
  54. $answer->read();
  55. if (count($answer->nbrAnswers) > 0) {
  56. $nb_matches = $nb_options = 0;
  57. //for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  58. foreach ($answer->answer as $answerId => $answer_item) {
  59. //$answer_id = $answer->getRealAnswerIdFromList($answerId);
  60. if ($answer->isCorrect($answerId)) {
  61. $nb_matches++;
  62. $defaults['answer['.$nb_matches.']'] = $answer->selectAnswer($answerId);
  63. $defaults['weighting['.$nb_matches.']'] = Text::float_format($answer->selectWeighting($answerId), 1);
  64. $defaults['matches['.$nb_matches.']'] = $answer->correct[$answerId];//$nb_matches;
  65. } else {
  66. $nb_options++;
  67. $defaults['option['.$nb_options.']'] = $nb_options;
  68. }
  69. }
  70. }
  71. } else {
  72. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  73. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  74. $defaults['matches[2]'] = '2';
  75. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  76. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  77. }
  78. }
  79. $a_matches = array();
  80. for ($i = 1; $i <= $nb_matches; ++$i) {
  81. $a_matches[$i] = $i; // fill the array with A, B, C.....
  82. }
  83. $form->addElement('hidden', 'nb_matches', $nb_matches);
  84. $form->addElement('hidden', 'nb_options', $nb_options);
  85. // DISPLAY MATCHES
  86. $html = '<table class="data_table">
  87. <tr>
  88. <th width="40%">
  89. '.get_lang('Answer').'
  90. </th>
  91. <th width="40%">
  92. '.get_lang('MatchesTo').'
  93. </th>
  94. <th width="50px">
  95. '.get_lang('Weighting').'
  96. </th>
  97. </tr>';
  98. $form->addElement('label', get_lang('MakeCorrespond').'<br /> '.Display::return_icon('fill_field.png'), $html);
  99. if ($nb_matches < 1) {
  100. $nb_matches = 1;
  101. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  102. }
  103. for ($i = 1; $i <= $nb_matches; ++$i) {
  104. $form->addElement('html', '<tr><td>');
  105. $group = array();
  106. $group[] = $form->createElement('text', 'answer['.$i.']', null, ' size="60" style="margin-left: 0em;"');
  107. $group[] = $form->createElement('select', 'matches['.$i.']', null, $a_matches);
  108. $group[] = $form->createElement(
  109. 'text',
  110. 'weighting['.$i.']',
  111. null,
  112. array('class' => 'span1', 'value' => 10, )
  113. );
  114. $form->addGroup($group, null, null, '</td><td>');
  115. $form->addElement('html', '</td></tr>');
  116. $defaults['option['.$i.']'] = $i;
  117. }
  118. $form->addElement('html', '</table></div></div>');
  119. $group = array();
  120. $group[] = $form->createElement(
  121. 'style_submit_button',
  122. 'moreMatches',
  123. get_lang('AddElem'),
  124. 'class="btn plus"'
  125. );
  126. $group[] = $form->createElement(
  127. 'style_submit_button',
  128. 'lessMatches',
  129. get_lang('DelElem'),
  130. 'class="btn minus"'
  131. );
  132. $group[] = $form->createElement('style_submit_button', 'submitQuestion', $this->submitText, 'class="'.$this->submitClass.'"');
  133. $form->addGroup($group);
  134. $form->addElement('html', '</table></div></div>');
  135. if (!empty($this->id)) {
  136. $form->setDefaults($defaults);
  137. } else {
  138. if ($this->isContent == 1) {
  139. $form->setDefaults($defaults);
  140. }
  141. }
  142. $form->setConstants(array('nb_matches' => $nb_matches, 'nb_options' => $nb_options));
  143. }
  144. /**
  145. * abstract function which creates the form to create / edit the answers of the question
  146. * @param FormValidator instance
  147. */
  148. public function processAnswersCreation($form)
  149. {
  150. $nb_matches = $form->getSubmitValue('nb_matches');
  151. $this->weighting = 0;
  152. $objAnswer = new Answer($this->id);
  153. $position = 0;
  154. // insert the options
  155. for ($i = 1; $i <= $nb_matches; ++$i) {
  156. $position++;
  157. //$option = $form->getSubmitValue('option['.$i.']');
  158. $objAnswer->createAnswer($position, 0, '', 0, $position);
  159. }
  160. // insert the answers
  161. for ($i = 1; $i <= $nb_matches; ++$i) {
  162. $position++;
  163. $answer = $form->getSubmitValue('answer['.$i.']');
  164. $matches = $form->getSubmitValue('matches['.$i.']');
  165. $weighting = $form->getSubmitValue('weighting['.$i.']');
  166. $this->weighting += $weighting;
  167. $objAnswer->createAnswer($answer, $matches, '', $weighting, $position);
  168. }
  169. $objAnswer->save();
  170. $this->save();
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function return_header($feedback_type = null, $counter = null, $score = null, $show_media = false, $hideTitle = 0)
  176. {
  177. $header = parent::return_header($feedback_type, $counter, $score, $show_media, $hideTitle);
  178. $header .= '<table class="'.$this->question_table_class.'">';
  179. $header .= '<tr>
  180. <th>'.get_lang('ElementList').'</th>
  181. <th>'.get_lang('Status').'</th>
  182. </tr>';
  183. return $header;
  184. }
  185. }