unique_answer_no_option.class.php 14 KB

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