unique_answer_no_option.class.php 15 KB

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