draggable.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /**
  16. * Matching questions type class
  17. * @package chamilo.exercise
  18. */
  19. class Draggable extends Matching
  20. {
  21. static $typePicture = 'matching.gif';
  22. static $explanationLangVar = 'Draggable';
  23. /**
  24. * Constructor
  25. */
  26. function Draggable()
  27. {
  28. parent::question();
  29. $this->type = DRAGGABLE;
  30. $this->isContent = $this->getIsContent();
  31. }
  32. /**
  33. * function which redifines Question::createAnswersForm
  34. * @param the formvalidator instance
  35. */
  36. public function createAnswersForm($form)
  37. {
  38. $defaults = array();
  39. $navigator_info = api_get_navigator();
  40. $nb_matches = $nb_options = 2;
  41. if ($form->isSubmitted()) {
  42. $nb_matches = $form->getSubmitValue('nb_matches');
  43. $nb_options = $form->getSubmitValue('nb_options');
  44. if (isset($_POST['lessMatches'])) {
  45. $nb_matches--;
  46. }
  47. if (isset($_POST['moreMatches'])) {
  48. $nb_matches++;
  49. }
  50. if (isset($_POST['lessOptions'])) {
  51. $nb_options--;
  52. }
  53. if (isset($_POST['moreOptions'])) {
  54. $nb_options++;
  55. }
  56. } else {
  57. if (!empty($this->id)) {
  58. $answer = new Answer($this->id);
  59. $answer->read();
  60. if (count($answer->nbrAnswers) > 0) {
  61. $a_matches = $a_options = array();
  62. $nb_matches = $nb_options = 0;
  63. for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
  64. if ($answer->isCorrect($i)) {
  65. $nb_matches++;
  66. $defaults['answer['.$nb_matches.']'] = $answer->selectAnswer($i);
  67. $defaults['weighting['.$nb_matches.']'] = Text::float_format($answer->selectWeighting($i), 1);
  68. $defaults['matches['.$nb_matches.']'] = $answer->correct[$i];//$nb_matches;
  69. } else {
  70. $nb_options++;
  71. $defaults['option['.$nb_options.']'] = $nb_options;
  72. }
  73. }
  74. }
  75. } else {
  76. $defaults['answer[1]'] = get_lang('DefaultMakeCorrespond1');
  77. $defaults['answer[2]'] = get_lang('DefaultMakeCorrespond2');
  78. $defaults['matches[2]'] = '2';
  79. $defaults['option[1]'] = get_lang('DefaultMatchingOptA');
  80. $defaults['option[2]'] = get_lang('DefaultMatchingOptB');
  81. }
  82. }
  83. $a_matches = array();
  84. for ($i = 1; $i <= $nb_matches; ++$i) {
  85. $a_matches[$i] = $i; // fill the array with A, B, C.....
  86. }
  87. $form->addElement('hidden', 'nb_matches', $nb_matches);
  88. $form->addElement('hidden', 'nb_options', $nb_options);
  89. // DISPLAY MATCHES
  90. $html = '<table class="data_table">
  91. <tr>
  92. <th width="40%">
  93. '.get_lang('Answer').'
  94. </th>
  95. <th width="40%">
  96. '.get_lang('MatchesTo').'
  97. </th>
  98. <th width="50px">
  99. '.get_lang('Weighting').'
  100. </th>
  101. </tr>';
  102. $form->addElement('label', get_lang('MakeCorrespond').'<br /> <img src="../img/fill_field.png">', $html);
  103. if ($nb_matches < 1) {
  104. $nb_matches = 1;
  105. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  106. }
  107. for ($i = 1; $i <= $nb_matches; ++$i) {
  108. $form->addElement('html', '<tr><td>');
  109. $group = array();
  110. /*$puce = $form->createElement('text', null, null, 'value="'.$i.'"');
  111. $puce->freeze();
  112. $group[] = $puce;*/
  113. $group[] = $form->createElement('text', 'answer['.$i.']', null, ' size="60" style="margin-left: 0em;"');
  114. $group[] = $form->createElement('select', 'matches['.$i.']', null, $a_matches);
  115. $group[] = $form->createElement(
  116. 'text',
  117. 'weighting['.$i.']',
  118. null,
  119. array('class' => 'span1', 'value' => 10, )
  120. );
  121. $form->addGroup($group, null, null, '</td><td>');
  122. $form->addElement('html', '</td></tr>');
  123. $defaults['option['.$i.']'] = $i;
  124. //$defaults['matches['.$i.']'] = $i;
  125. }
  126. $form->addElement('html', '</table></div></div>');
  127. $group = array();
  128. $group[] = $form->createElement(
  129. 'style_submit_button',
  130. 'moreMatches',
  131. get_lang('AddElem'),
  132. 'class="btn plus"'
  133. );
  134. $group[] = $form->createElement(
  135. 'style_submit_button',
  136. 'lessMatches',
  137. get_lang('DelElem'),
  138. 'class="btn minus"'
  139. );
  140. global $text, $class;
  141. $group[] = $form->createElement('style_submit_button', 'submitQuestion', $text, 'class="'.$class.'"');
  142. $form->addGroup($group);
  143. // DISPLAY OPTIONS
  144. /*
  145. $html = '<table class="data_table">
  146. <tr style="text-align: center;">
  147. <th width="10px">
  148. '.get_lang('Number').'
  149. </th>
  150. <th width="90%"
  151. '.get_lang('Answer').'
  152. </th>
  153. </tr>';
  154. //$form -> addElement ('html', $html);
  155. $form->addElement('label', null, $html);
  156. if ($nb_options < 1)
  157. {
  158. $nb_options = 1;
  159. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  160. }
  161. for ($i = 1; $i <= $nb_matches; ++$i) {
  162. $form->addElement('html', '<tr><td>');
  163. $group = array();
  164. $puce = $form->createElement('text', null, null, 'value="'.chr(64 + $i).'"');
  165. $puce->freeze();
  166. $group[] = $puce;
  167. $group[] = $form->createElement('text', 'option['.$i.']', null, array('class' => 'span6'));
  168. $form->addGroup($group, null, null, '</td><td>');
  169. $form->addElement('html', '</td></tr>');
  170. }*/
  171. $form->addElement('html', '</table></div></div>');
  172. if (!empty($this->id)) {
  173. $form->setDefaults($defaults);
  174. } else {
  175. if ($this->isContent == 1) {
  176. $form->setDefaults($defaults);
  177. }
  178. }
  179. $form->setConstants(array('nb_matches' => $nb_matches, 'nb_options' => $nb_options));
  180. }
  181. /**
  182. * abstract function which creates the form to create / edit the answers of the question
  183. * @param the formvalidator instance
  184. */
  185. public function processAnswersCreation($form)
  186. {
  187. $nb_matches = $form->getSubmitValue('nb_matches');
  188. $this->weighting = 0;
  189. $objAnswer = new Answer($this->id);
  190. $position = 0;
  191. // insert the options
  192. for ($i = 1; $i <= $nb_matches; ++$i) {
  193. $position++;
  194. //$option = $form->getSubmitValue('option['.$i.']');
  195. $objAnswer->createAnswer($position, 0, '', 0, $position);
  196. }
  197. // insert the answers
  198. for ($i = 1; $i <= $nb_matches; ++$i) {
  199. $position++;
  200. $answer = $form->getSubmitValue('answer['.$i.']');
  201. $matches = $form->getSubmitValue('matches['.$i.']');
  202. $weighting = $form->getSubmitValue('weighting['.$i.']');
  203. $this->weighting += $weighting;
  204. $objAnswer->createAnswer($answer, $matches, '', $weighting, $position);
  205. }
  206. $objAnswer->save();
  207. $this->save();
  208. }
  209. /**
  210. * @param null $feedback_type
  211. * @param null $counter
  212. * @param null $score
  213. * @param bool $show_media
  214. * @return string
  215. */
  216. public function return_header($feedback_type = null, $counter = null, $score = null, $show_media = false)
  217. {
  218. $header = parent::return_header($feedback_type, $counter, $score, $show_media);
  219. $header .= '<table class="'.$this->question_table_class.'">';
  220. $header .= '<tr>
  221. <th>'.get_lang('ElementList').'</th>
  222. <th>'.get_lang('Status').'</th>
  223. </tr>';
  224. return $header;
  225. }
  226. }