unique_answer_no_option.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class UniqueAnswerNoOption
  5. * Allows to instantiate an object of type UNIQUE_ANSWER (MULTIPLE CHOICE, UNIQUE ANSWER),
  6. * extending the class question
  7. * @author Eric Marguin
  8. * @author Julio Montoya
  9. * @package chamilo.exercise
  10. *
  11. */
  12. class UniqueAnswerNoOption extends Question
  13. {
  14. static $typePicture = 'mcuao.png';
  15. static $explanationLangVar = 'UniqueAnswerNoOption';
  16. /**
  17. * Constructor
  18. */
  19. public function __construct()
  20. {
  21. //this is highly important
  22. parent::__construct();
  23. $this -> type = UNIQUE_ANSWER_NO_OPTION;
  24. $this -> isContent = $this-> getIsContent();
  25. }
  26. /**
  27. * function which redifines Question::createAnswersForm
  28. * @param the formvalidator instance
  29. * @param the answers number to display
  30. */
  31. function createAnswersForm($form)
  32. {
  33. // getting the exercise list
  34. $obj_ex = $_SESSION['objExercise'];
  35. $editor_config = array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '125');
  36. //this line define how many question by default appear when creating a choice question
  37. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 3; // The previous default value was 2. See task #1759.
  38. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  39. /*
  40. Types of Feedback
  41. $feedback_option[0]=get_lang('Feedback');
  42. $feedback_option[1]=get_lang('DirectFeedback');
  43. $feedback_option[2]=get_lang('NoFeedback');
  44. */
  45. $feedback_title = '';
  46. $comment_title = '';
  47. if ($obj_ex->selectFeedbackType() == 1) {
  48. $editor_config['Width'] = '250';
  49. $editor_config['Height'] = '110';
  50. $comment_title = '<th width="50%" >' . get_lang('Comment') . '</th>';
  51. $feedback_title = '<th width="50%" >' . get_lang('Scenario') . '</th>';
  52. } else {
  53. $comment_title = '<th width="50%">' . get_lang('Comment') . '</th>';
  54. }
  55. $html = '<table class="table table-striped table-hover">';
  56. $html .= '<thead>';
  57. $html .= '<tr>';
  58. $html .= '<th>' . get_lang('Number') . '</th>';
  59. $html .= '<th>' . get_lang('True') . '</th>';
  60. $html .= '<th width="50%">' . get_lang('Answer') . '</th>';
  61. $html .= $comment_title . $feedback_title;
  62. $html .= '<th>' . get_lang('Weighting') . '</th>';
  63. $html .= '</tr>';
  64. $html .= '</thead>';
  65. $html .= '<tbody>';
  66. $form->addHeader(get_lang('Answers'));
  67. $form->addHtml($html);
  68. $defaults = array();
  69. $correct = 0;
  70. $answer = false;
  71. if (!empty($this->id)) {
  72. $answer = new Answer($this->id);
  73. $answer->read();
  74. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  75. $nb_answers = $answer->nbrAnswers;
  76. }
  77. }
  78. $temp_scenario = array();
  79. if ($nb_answers < 1) {
  80. $nb_answers = 1;
  81. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  82. }
  83. $editQuestion = isset($_GET['editQuestion']) ? $_GET['editQuestion'] : false;
  84. if ($editQuestion) {
  85. //fixing $nb_answers
  86. $new_list = array();
  87. $count = 1;
  88. if (isset($_POST['lessAnswers'])) {
  89. if (!isset($_SESSION['less_answer'])) {
  90. $_SESSION['less_answer'] = $this->id;
  91. $nb_answers--;
  92. }
  93. }
  94. for ($k = 1; $k <= $nb_answers; ++$k) {
  95. if ($answer->position[$k] != '666') {
  96. $new_list[$count] = $count;
  97. $count++;
  98. }
  99. }
  100. } else {
  101. for ($k = 1; $k <= $nb_answers; ++$k) {
  102. $new_list[$k] = $k;
  103. }
  104. }
  105. $i = 1;
  106. //for ($k = 1 ; $k <= $real_nb_answers; $k++) {
  107. foreach ($new_list as $key) {
  108. $i = $key;
  109. $form->addElement('html', '<tr>');
  110. if (is_object($answer)) {
  111. if ($answer->position[$i] == 666) {
  112. //we set nothing
  113. } else {
  114. if ($answer->correct[$i]) {
  115. $correct = $i;
  116. }
  117. $answer_result = $answer->answer[$i];
  118. $weight_result = float_format($answer->weighting[$i], 1);
  119. if ($nb_answers == $i) {
  120. $weight_result = '0';
  121. }
  122. $defaults['answer[' . $i . ']'] = $answer_result;
  123. $defaults['comment[' . $i . ']'] = $answer->comment[$i];
  124. $defaults['weighting[' . $i . ']'] = $weight_result;
  125. $item_list = explode('@@', $answer->destination[$i]);
  126. $try = $item_list[0];
  127. $lp = $item_list[1];
  128. $list_dest = $item_list[2];
  129. $url = $item_list[3];
  130. if ($try == 0)
  131. $try_result = 0;
  132. else
  133. $try_result = 1;
  134. if ($url == 0)
  135. $url_result = '';
  136. else
  137. $url_result = $url;
  138. $temp_scenario['url' . $i] = $url_result;
  139. $temp_scenario['try' . $i] = $try_result;
  140. $temp_scenario['lp' . $i] = $lp;
  141. $temp_scenario['destination' . $i] = $list_dest;
  142. }
  143. }
  144. $defaults['scenario'] = $temp_scenario;
  145. $renderer = & $form->defaultRenderer();
  146. $renderer->setElementTemplate(
  147. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  148. 'correct'
  149. );
  150. $renderer->setElementTemplate(
  151. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  152. 'counter[' . $i . ']'
  153. );
  154. $renderer->setElementTemplate(
  155. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  156. 'answer[' . $i . ']'
  157. );
  158. $renderer->setElementTemplate(
  159. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  160. 'comment[' . $i . ']'
  161. );
  162. $renderer->setElementTemplate(
  163. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  164. 'weighting[' . $i . ']'
  165. );
  166. $answer_number = $form->addElement('text', 'counter[' . $i . ']', null, 'value="' . $i . '"');
  167. $answer_number->freeze();
  168. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  169. $form->addElement('html_editor', 'answer[' . $i . ']', null, array(), $editor_config);
  170. $form->addElement('html_editor', 'comment[' . $i . ']', null, array(), $editor_config);
  171. $form->addElement('text', 'weighting[' . $i . ']', null, array('class' => "span1", 'value' => '0'));
  172. $form->addElement('html', '</tr>');
  173. $i++;
  174. }
  175. if (empty($this->id)) {
  176. $form->addElement('hidden', 'new_question', 1);
  177. }
  178. //Adding the "I don't know" question answer
  179. //if (empty($this -> id)) {
  180. $i = 666;
  181. $form->addHtml('<tr>');
  182. $defaults['answer[' . $i . ']'] = get_lang('DontKnow');
  183. $defaults['weighting[' . $i . ']'] = 0;
  184. $defaults['scenario'] = $temp_scenario;
  185. $renderer = & $form->defaultRenderer();
  186. $renderer->setElementTemplate(
  187. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  188. 'correct'
  189. );
  190. $renderer->setElementTemplate(
  191. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  192. 'counter[' . $i . ']'
  193. );
  194. $renderer->setElementTemplate(
  195. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  196. 'answer[' . $i . ']'
  197. );
  198. $renderer->setElementTemplate(
  199. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  200. 'comment[' . $i . ']'
  201. );
  202. $renderer->setElementTemplate(
  203. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  204. 'weighting[' . $i . ']'
  205. );
  206. $answer_number = $form->addElement('text', 'counter[' . $i . ']', null, 'value="-"');
  207. $answer_number->freeze();
  208. $form->addElement('hidden', 'position[' . $i . ']', '666');
  209. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  210. $form->addElement('html_editor', 'answer[' . $i . ']', null, array(), $editor_config);
  211. $form->addRule('answer[' . $i . ']', get_lang('ThisFieldIsRequired'), 'required');
  212. $form->addElement('html_editor', 'comment[' . $i . ']', null, array(), $editor_config);
  213. //$form->addElement('select', 'destination'.$i, get_lang('SelectQuestion').' : ',$select_question,'multiple');
  214. $form->addText("weighting[$i]", null, false, ['value' => 0, 'readonly' => 'readonly']);
  215. $form->addHTml('</tr>');
  216. $form->addHtml('</tbody></table>');
  217. $buttonGroup = [];
  218. global $text, $class;
  219. //ie6 fix
  220. if ($obj_ex->edit_exercise_in_lp == true) {
  221. //setting the save button here and not in the question class.php
  222. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  223. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  224. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  225. $form->addGroup($buttonGroup);
  226. }
  227. //We check the first radio button to be sure a radio button will be check
  228. if ($correct == 0) {
  229. $correct = 1;
  230. }
  231. $defaults['correct'] = $correct;
  232. if (!empty($this->id)) {
  233. $form->setDefaults($defaults);
  234. } else {
  235. $form->setDefaults($defaults);
  236. }
  237. $form->addElement('hidden', 'nb_answers');
  238. $form->setConstants(array('nb_answers' => $nb_answers));
  239. }
  240. /**
  241. * Function which creates the form to create / edit the answers of the question
  242. * @param the formvalidator instance
  243. * @param the answers number to display
  244. */
  245. function processAnswersCreation($form)
  246. {
  247. $questionWeighting = $nbrGoodAnswers = 0;
  248. $correct = $form -> getSubmitValue('correct');
  249. $objAnswer = new Answer($this->id);
  250. $nb_answers = $form -> getSubmitValue('nb_answers');
  251. $minus = 1;
  252. if ($form -> getSubmitValue('new_question')) {
  253. $minus = 0;
  254. }
  255. for ($i=1 ; $i <= $nb_answers - $minus; $i++) {
  256. $position = trim($form -> getSubmitValue('position['.$i.']'));
  257. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  258. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  259. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  260. $scenario = $form -> getSubmitValue('scenario');
  261. //$list_destination = $form -> getSubmitValue('destination'.$i);
  262. //$destination_str = $form -> getSubmitValue('destination'.$i);
  263. $try = $scenario['try'.$i];
  264. $lp = $scenario['lp'.$i];
  265. $destination = $scenario['destination'.$i];
  266. $url = trim($scenario['url'.$i]);
  267. /*
  268. How we are going to parse the destination value
  269. here we parse the destination value which is a string
  270. 1@@3@@2;4;4;@@http://www.chamilo.org
  271. where: try_again@@lp_id@@selected_questions@@url
  272. try_again = is 1 || 0
  273. lp_id = id of a learning path (0 if dont select)
  274. selected_questions= ids of questions
  275. url= an url
  276. */
  277. /*
  278. $destination_str='';
  279. foreach ($list_destination as $destination_id)
  280. {
  281. $destination_str.=$destination_id.';';
  282. }*/
  283. $goodAnswer= ($correct == $i) ? true : false;
  284. if ($goodAnswer) {
  285. $nbrGoodAnswers++;
  286. $weighting = abs($weighting);
  287. if($weighting > 0) {
  288. $questionWeighting += $weighting;
  289. }
  290. }
  291. if (empty($try))
  292. $try=0;
  293. if (empty($lp)) {
  294. $lp=0;
  295. }
  296. if (empty($destination)) {
  297. $destination=0;
  298. }
  299. if ($url=='') {
  300. $url=0;
  301. }
  302. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  303. $dest= $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  304. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  305. }
  306. //Create 666 answer
  307. $i = 666;
  308. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  309. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  310. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  311. $goodAnswer= ($correct == $i) ? true : false;
  312. $dest = '';
  313. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  314. // saves the answers into the data base
  315. $objAnswer -> save();
  316. // sets the total weighting of the question
  317. $this -> updateWeighting($questionWeighting);
  318. $this -> save();
  319. }
  320. function return_header($feedback_type = null, $counter = null, $score = null)
  321. {
  322. $header = parent::return_header($feedback_type, $counter, $score);
  323. $header .= '<table class="'.$this->question_table_class .'">
  324. <tr>
  325. <th>'.get_lang("Choice").'</th>
  326. <th>'. get_lang("ExpectedChoice").'</th>
  327. <th>'. get_lang("Answer").'</th>';
  328. $header .= '<th>'.get_lang("Comment").'</th>';
  329. $header .= '</tr>';
  330. return $header;
  331. }
  332. }