unique_answer_no_option.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '125');
  34. //this line define how many question by default appear when creating a choice question
  35. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 3; // The previous default value was 2. See task #1759.
  36. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  37. /*
  38. Types of Feedback
  39. $feedback_option[0]=get_lang('Feedback');
  40. $feedback_option[1]=get_lang('DirectFeedback');
  41. $feedback_option[2]=get_lang('NoFeedback');
  42. */
  43. $feedback_title = '';
  44. $comment_title = '';
  45. if ($obj_ex->selectFeedbackType() == 1) {
  46. $editor_config['Width'] = '250';
  47. $editor_config['Height'] = '110';
  48. $comment_title = '<th width="50%" >'.get_lang('Comment').'</th>';
  49. $feedback_title = '<th width="50%" >'.get_lang('Scenario').'</th>';
  50. } else {
  51. $comment_title = '<th width="50%">'.get_lang('Comment').'</th>';
  52. }
  53. $html = '<table class="table table-striped table-hover">';
  54. $html .= '<thead>';
  55. $html .= '<tr>';
  56. $html .= '<th>'.get_lang('Number').'</th>';
  57. $html .= '<th>'.get_lang('True').'</th>';
  58. $html .= '<th width="50%">'.get_lang('Answer').'</th>';
  59. $html .= $comment_title.$feedback_title;
  60. $html .= '<th>'.get_lang('Weighting').'</th>';
  61. $html .= '</tr>';
  62. $html .= '</thead>';
  63. $html .= '<tbody>';
  64. $form->addHeader(get_lang('Answers'));
  65. $form->addHtml($html);
  66. $defaults = array();
  67. $correct = 0;
  68. $answer = false;
  69. if (!empty($this->id)) {
  70. $answer = new Answer($this->id);
  71. $answer->read();
  72. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  73. $nb_answers = $answer->nbrAnswers;
  74. }
  75. }
  76. $temp_scenario = array();
  77. if ($nb_answers < 1) {
  78. $nb_answers = 1;
  79. echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  80. }
  81. $editQuestion = isset($_GET['editQuestion']) ? $_GET['editQuestion'] : false;
  82. if ($editQuestion) {
  83. //fixing $nb_answers
  84. $new_list = array();
  85. $count = 1;
  86. if (isset($_POST['lessAnswers'])) {
  87. if (!isset($_SESSION['less_answer'])) {
  88. $_SESSION['less_answer'] = $this->id;
  89. $nb_answers--;
  90. }
  91. }
  92. for ($k = 1; $k <= $nb_answers; ++$k) {
  93. if ($answer->position[$k] != '666') {
  94. $new_list[$count] = $count;
  95. $count++;
  96. }
  97. }
  98. } else {
  99. for ($k = 1; $k <= $nb_answers; ++$k) {
  100. $new_list[$k] = $k;
  101. }
  102. }
  103. $i = 1;
  104. //for ($k = 1 ; $k <= $real_nb_answers; $k++) {
  105. foreach ($new_list as $key) {
  106. $i = $key;
  107. $form->addElement('html', '<tr>');
  108. if (is_object($answer)) {
  109. if ($answer->position[$i] == 666) {
  110. //we set nothing
  111. } else {
  112. if ($answer->correct[$i]) {
  113. $correct = $i;
  114. }
  115. $answer_result = $answer->answer[$i];
  116. $weight_result = float_format($answer->weighting[$i], 1);
  117. if ($nb_answers == $i) {
  118. $weight_result = '0';
  119. }
  120. $defaults['answer['.$i.']'] = $answer_result;
  121. $defaults['comment['.$i.']'] = $answer->comment[$i];
  122. $defaults['weighting['.$i.']'] = $weight_result;
  123. $item_list = explode('@@', $answer->destination[$i]);
  124. $try = $item_list[0];
  125. $lp = $item_list[1];
  126. $list_dest = $item_list[2];
  127. $url = $item_list[3];
  128. if ($try == 0) {
  129. $try_result = 0;
  130. } else {
  131. $try_result = 1;
  132. }
  133. if ($url == 0) {
  134. $url_result = '';
  135. } else {
  136. $url_result = $url;
  137. }
  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('style' => 'width: 60px;', '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["counter[$i]"] = '-';
  183. $defaults['answer['.$i.']'] = get_lang('DontKnow');
  184. $defaults['weighting['.$i.']'] = '0';
  185. $defaults['scenario'] = $temp_scenario;
  186. $renderer = & $form->defaultRenderer();
  187. $renderer->setElementTemplate(
  188. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  189. 'correct'
  190. );
  191. $renderer->setElementTemplate(
  192. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  193. 'counter['.$i.']'
  194. );
  195. $renderer->setElementTemplate(
  196. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  197. 'answer['.$i.']'
  198. );
  199. $renderer->setElementTemplate(
  200. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  201. 'comment['.$i.']'
  202. );
  203. $renderer->setElementTemplate(
  204. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  205. 'weighting['.$i.']'
  206. );
  207. $form
  208. ->addElement('text', 'counter['.$i.']', null)
  209. ->freeze();
  210. $form->addElement('hidden', 'position['.$i.']', '666');
  211. $form->addElement('radio', 'correct', null, null, $i, ['class' => 'checkbox', 'disabled' => true]);
  212. $form->addElement('html_editor', 'answer['.$i.']', null, array(), $editor_config);
  213. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  214. $form->addElement('html_editor', 'comment['.$i.']', null, array(), $editor_config);
  215. //$form->addElement('select', 'destination'.$i, get_lang('SelectQuestion').' : ',$select_question,'multiple');
  216. $form
  217. ->addElement('text', "weighting[$i]", null)
  218. ->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. //setting the save button here and not in the question class.php
  226. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  227. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  228. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  229. $form->addGroup($buttonGroup);
  230. }
  231. //We check the first radio button to be sure a radio button will be check
  232. if ($correct == 0) {
  233. $correct = 1;
  234. }
  235. $defaults['correct'] = $correct;
  236. if (!empty($this->id)) {
  237. $form->setDefaults($defaults);
  238. } else {
  239. $form->setDefaults($defaults);
  240. }
  241. $form->addElement('hidden', 'nb_answers');
  242. $form->setConstants(array('nb_answers' => $nb_answers));
  243. }
  244. /**
  245. * Function which creates the form to create / edit the answers of the question
  246. * @param the formvalidator instance
  247. * @param the answers number to display
  248. */
  249. function processAnswersCreation($form)
  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();
  342. }
  343. function return_header($feedback_type = null, $counter = null, $score = null)
  344. {
  345. $header = parent::return_header($feedback_type, $counter, $score);
  346. $header .= '<table class="'.$this->question_table_class.'">
  347. <tr>
  348. <th>'.get_lang("Choice").'</th>
  349. <th>'. get_lang("ExpectedChoice").'</th>
  350. <th>'. get_lang("Answers").'</th>';
  351. $header .= '<th>'.get_lang('Status').'</th>';
  352. $header .= '<th>'.get_lang('Comment').'</th>';
  353. $header .= '</tr>';
  354. return $header;
  355. }
  356. }