unique_answer.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class UniqueAnswer
  5. *
  6. * This class allows to instantiate an object of type UNIQUE_ANSWER
  7. * (MULTIPLE CHOICE, UNIQUE ANSWER),
  8. * extending the class question
  9. *
  10. * @author Eric Marguin
  11. * @author Julio Montoya
  12. * @package chamilo.exercise
  13. **/
  14. class UniqueAnswer extends Question
  15. {
  16. static $typePicture = 'mcua.png';
  17. static $explanationLangVar = 'UniqueSelect';
  18. /**
  19. * Constructor
  20. */
  21. public function UniqueAnswer()
  22. {
  23. //this is highly important
  24. parent::question();
  25. $this->type = UNIQUE_ANSWER;
  26. $this->isContent = $this->getIsContent();
  27. }
  28. /**
  29. * function which redefines Question::createAnswersForm
  30. * @param FormValidator $form
  31. */
  32. public function createAnswersForm($form)
  33. {
  34. // Getting the exercise list
  35. $obj_ex = $_SESSION['objExercise'];
  36. $editor_config = array(
  37. 'ToolbarSet' => 'TestProposedAnswer',
  38. 'Width' => '100%',
  39. 'Height' => '125'
  40. );
  41. //this line defines how many questions by default appear when creating a choice question
  42. // The previous default value was 2. See task #1759.
  43. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
  44. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  45. /*
  46. Types of Feedback
  47. $feedback_option[0]=get_lang('Feedback');
  48. $feedback_option[1]=get_lang('DirectFeedback');
  49. $feedback_option[2]=get_lang('NoFeedback');
  50. */
  51. $feedback_title = '';
  52. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  53. //Scenario
  54. $comment_title = '<th>' . get_lang('Comment') . '</th>';
  55. $feedback_title = '<th>' . get_lang('Scenario') . '</th>';
  56. } else {
  57. $comment_title = '<th >' . get_lang('Comment') . '</th>';
  58. }
  59. $html = '<table class="table table-striped table-hover">
  60. <thead>
  61. <tr style="text-align: center;">
  62. <th width="10">' . get_lang('Number') . '</th>
  63. <th>' . get_lang('True') . '</th>
  64. <th>' . get_lang('Answer') . '</th>
  65. ' . $comment_title . '
  66. ' . $feedback_title . '
  67. <th width="10">' . get_lang('Weighting') . '</th>
  68. </tr>
  69. </thead>
  70. <tbody>';
  71. $form->addHeader(get_lang('Answers'));
  72. $form->addHtml($html);
  73. $defaults = array();
  74. $correct = 0;
  75. if (!empty($this->id)) {
  76. $answer = new Answer($this->id);
  77. $answer->read();
  78. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  79. $nb_answers = $answer->nbrAnswers;
  80. }
  81. }
  82. $form->addElement('hidden', 'nb_answers');
  83. //Feedback SELECT
  84. $question_list = $obj_ex->selectQuestionList();
  85. $select_question = array();
  86. $select_question[0] = get_lang('SelectTargetQuestion');
  87. if (is_array($question_list)) {
  88. foreach ($question_list as $key => $questionid) {
  89. //To avoid warning messages
  90. if (!is_numeric($questionid)) {
  91. continue;
  92. }
  93. $question = Question::read($questionid);
  94. $select_question[$questionid] = 'Q' . $key . ' :' . cut(
  95. $question->selectTitle(), 20
  96. );
  97. }
  98. }
  99. $select_question[-1] = get_lang('ExitTest');
  100. $list = new LearnpathList(api_get_user_id());
  101. $flat_list = $list->get_flat_list();
  102. $select_lp_id = array();
  103. $select_lp_id[0] = get_lang('SelectTargetLP');
  104. foreach ($flat_list as $id => $details) {
  105. $select_lp_id[$id] = cut($details['lp_name'], 20);
  106. }
  107. $temp_scenario = array();
  108. if ($nb_answers < 1) {
  109. $nb_answers = 1;
  110. Display::display_normal_message(
  111. get_lang('YouHaveToCreateAtLeastOneAnswer')
  112. );
  113. }
  114. for ($i = 1; $i <= $nb_answers; ++$i) {
  115. $form->addHtml('<tr>');
  116. if (isset($answer) && is_object($answer)) {
  117. if ($answer->correct[$i]) {
  118. $correct = $i;
  119. }
  120. $defaults['answer[' . $i . ']'] = $answer->answer[$i];
  121. $defaults['comment[' . $i . ']'] = $answer->comment[$i];
  122. $defaults['weighting[' . $i . ']'] = float_format(
  123. $answer->weighting[$i],
  124. 1
  125. );
  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. } else {
  146. $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
  147. $defaults['weighting[1]'] = 10;
  148. $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
  149. $defaults['weighting[2]'] = 0;
  150. $temp_scenario['destination' . $i] = array('0');
  151. $temp_scenario['lp' . $i] = array('0');
  152. }
  153. $defaults['scenario'] = $temp_scenario;
  154. $renderer = $form->defaultRenderer();
  155. $renderer->setElementTemplate(
  156. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  157. 'correct'
  158. );
  159. $renderer->setElementTemplate(
  160. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  161. 'counter[' . $i . ']'
  162. );
  163. $renderer->setElementTemplate(
  164. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  165. 'answer[' . $i . ']'
  166. );
  167. $renderer->setElementTemplate(
  168. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  169. 'comment[' . $i . ']'
  170. );
  171. $renderer->setElementTemplate(
  172. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  173. 'weighting[' . $i . ']'
  174. );
  175. $answer_number = $form->addElement(
  176. 'text', 'counter[' . $i . ']', null, ' value = "' . $i . '"'
  177. );
  178. $answer_number->freeze();
  179. $form->addElement(
  180. 'radio', 'correct', null, null, $i, 'class="checkbox"'
  181. );
  182. $form->addHtmlEditor('answer[' . $i . ']', null, null, true, $editor_config);
  183. $form->addRule(
  184. 'answer[' . $i . ']', get_lang('ThisFieldIsRequired'), 'required'
  185. );
  186. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  187. $form->addHtmlEditor('comment[' . $i . ']', null, null, false, $editor_config);
  188. // Direct feedback
  189. //Adding extra feedback fields
  190. $group = array();
  191. $group['try' . $i] = $form->createElement(
  192. 'checkbox',
  193. 'try' . $i,
  194. null,
  195. get_lang('TryAgain')
  196. );
  197. $group['lp' . $i] = $form->createElement(
  198. 'select',
  199. 'lp' . $i,
  200. get_lang('SeeTheory') . ': ',
  201. $select_lp_id
  202. );
  203. $group['destination' . $i] = $form->createElement(
  204. 'select',
  205. 'destination' . $i,
  206. get_lang('GoToQuestion') . ': ',
  207. $select_question
  208. );
  209. $group['url' . $i] = $form->createElement(
  210. 'text',
  211. 'url' . $i,
  212. get_lang('Other') . ': ',
  213. array(
  214. 'class' => 'col-md-2',
  215. 'placeholder' => get_lang('Other')
  216. )
  217. );
  218. $form->addGroup($group, 'scenario');
  219. $renderer->setElementTemplate(
  220. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
  221. 'scenario'
  222. );
  223. } else {
  224. $form->addHtmlEditor('comment[' . $i . ']', null, null, false, $editor_config);
  225. }
  226. $form->addText('weighting[' . $i . ']', null, null, array('class' => "col-md-1", 'value' => '0'));
  227. $form->addHtml('</tr>');
  228. }
  229. $form->addHtml('</tbody>');
  230. $form->addHtml('</table>');
  231. $navigator_info = api_get_navigator();
  232. global $text, $class;
  233. $buttonGroup = [];
  234. //ie6 fix
  235. if ($obj_ex->edit_exercise_in_lp == true) {
  236. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  237. $buttonGroup[] = $form->createElement(
  238. 'submit',
  239. 'lessAnswers',
  240. get_lang('LessAnswer'),
  241. 'class="btn btn-primary"'
  242. );
  243. $buttonGroup[] = $form->createElement(
  244. 'submit',
  245. 'moreAnswers',
  246. get_lang('PlusAnswer'),
  247. 'class="btn btn-primary"'
  248. );
  249. $buttonGroup[] = $form->createElement('submit', 'submitQuestion', $text, 'class="' . $class . '"');
  250. } else {
  251. //setting the save button here and not in the question class.php
  252. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  253. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  254. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  255. }
  256. $form->addGroup($buttonGroup);
  257. }
  258. // We check the first radio button to be sure a radio button will be check
  259. if ($correct == 0) {
  260. $correct = 1;
  261. }
  262. $defaults['correct'] = $correct;
  263. if (!empty($this->id)) {
  264. $form->setDefaults($defaults);
  265. } else {
  266. if ($this->isContent == 1) {
  267. // Default sample content.
  268. $form->setDefaults($defaults);
  269. } else {
  270. $form->setDefaults(array('correct' => 1));
  271. }
  272. }
  273. $form->setConstants(array('nb_answers' => $nb_answers));
  274. }
  275. /**
  276. * Receives the unique answer question type creation form data and creates
  277. * or updates the answers from that question
  278. * @param FormValidator $form
  279. */
  280. public function processAnswersCreation($form)
  281. {
  282. $questionWeighting = $nbrGoodAnswers = 0;
  283. $correct = $form->getSubmitValue('correct');
  284. $objAnswer = new Answer($this->id);
  285. $nb_answers = $form->getSubmitValue('nb_answers');
  286. for ($i = 1; $i <= $nb_answers; $i++) {
  287. $answer = trim($form->getSubmitValue('answer[' . $i . ']'));
  288. $comment = trim($form->getSubmitValue('comment[' . $i . ']'));
  289. $weighting = trim($form->getSubmitValue('weighting[' . $i . ']'));
  290. $scenario = $form->getSubmitValue('scenario');
  291. //$list_destination = $form -> getSubmitValue('destination'.$i);
  292. //$destination_str = $form -> getSubmitValue('destination'.$i);
  293. $try = $scenario['try' . $i];
  294. $lp = $scenario['lp' . $i];
  295. $destination = $scenario['destination' . $i];
  296. $url = trim($scenario['url' . $i]);
  297. /*
  298. How we are going to parse the destination value
  299. here we parse the destination value which is a string
  300. 1@@3@@2;4;4;@@http://www.chamilo.org
  301. where: try_again@@lp_id@@selected_questions@@url
  302. try_again = is 1 || 0
  303. lp_id = id of a learning path (0 if dont select)
  304. selected_questions= ids of questions
  305. url= an url
  306. $destination_str='';
  307. foreach ($list_destination as $destination_id)
  308. {
  309. $destination_str.=$destination_id.';';
  310. }*/
  311. $goodAnswer = ($correct == $i) ? true : false;
  312. if ($goodAnswer) {
  313. $nbrGoodAnswers++;
  314. $weighting = abs($weighting);
  315. if ($weighting > 0) {
  316. $questionWeighting += $weighting;
  317. }
  318. }
  319. if (empty($try)) {
  320. $try = 0;
  321. }
  322. if (empty($lp)) {
  323. $lp = 0;
  324. }
  325. if (empty($destination)) {
  326. $destination = 0;
  327. }
  328. if ($url == '') {
  329. $url = 0;
  330. }
  331. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  332. $dest = $try . '@@' . $lp . '@@' . $destination . '@@' . $url;
  333. $objAnswer->createAnswer(
  334. $answer,
  335. $goodAnswer,
  336. $comment,
  337. $weighting,
  338. $i,
  339. null,
  340. null,
  341. $dest
  342. );
  343. }
  344. // saves the answers into the data base
  345. $objAnswer->save();
  346. // sets the total weighting of the question
  347. $this->updateWeighting($questionWeighting);
  348. $this->save();
  349. }
  350. /**
  351. * Helper function to print the column titles in the answers edition form
  352. * @param null $feedback_type The type of feedback influences what columns are shown to the editor
  353. * @param null $counter The number of answers to show, in case there should be pagination
  354. * @param null $score The maximum score for the question
  355. * @return string HTML string for a table header + a wrapper before it
  356. */
  357. public function return_header(
  358. $feedback_type = null,
  359. $counter = null,
  360. $score = null
  361. ) {
  362. $header = parent::return_header($feedback_type, $counter, $score);
  363. $header .= '<table class="' . $this->question_table_class . '">
  364. <tr>
  365. <th>' . get_lang("Choice") . '</th>
  366. <th>' . get_lang("ExpectedChoice") . '</th>
  367. <th>' . get_lang("Answer") . '</th>';
  368. $header .= '<th>' . get_lang("Comment") . '</th>';
  369. $header .= '</tr>';
  370. return $header;
  371. }
  372. /**
  373. * Saves one answer to the database
  374. * @param int $id The ID of the answer (has to be calculated for this course)
  375. * @param int $question_id The question ID (to which the answer is attached)
  376. * @param string $title The text of the answer
  377. * @param string $comment The feedback for the answer
  378. * @param float|null $score The score you get when picking this answer
  379. * @param int|null $correct Whether this answer is considered *the* correct one (this is the unique answer type)
  380. */
  381. public function addAnswer(
  382. $id,
  383. $question_id,
  384. $title,
  385. $comment,
  386. $score = 0.0,
  387. $correct = 0
  388. ) {
  389. $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  390. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  391. $course_id = api_get_course_int_id();
  392. $question_id = intval($question_id);
  393. $score = floatval($score);
  394. $correct = intval($correct);
  395. $title = Database::escape_string($title);
  396. $comment = Database::escape_string($comment);
  397. // Get the max position.
  398. $sql = "SELECT max(position) as max_position
  399. FROM $tbl_quiz_answer
  400. WHERE
  401. c_id = $course_id AND
  402. question_id = $question_id";
  403. $rs_max = Database::query($sql);
  404. $row_max = Database::fetch_object($rs_max);
  405. $position = $row_max->max_position + 1;
  406. // Insert a new answer
  407. $sql = "INSERT INTO $tbl_quiz_answer (
  408. c_id,
  409. id,
  410. question_id,
  411. answer,
  412. correct,
  413. comment,
  414. ponderation,
  415. position,
  416. destination
  417. ) VALUES (
  418. $course_id,
  419. $id,
  420. $question_id,
  421. '" . $title . "',
  422. $correct,
  423. '" . $comment . "',
  424. '$score', $position,
  425. '0@@0@@0@@0'
  426. )";
  427. Database::query($sql);
  428. if ($correct) {
  429. $sql = "UPDATE $tbl_quiz_question
  430. SET ponderation = (ponderation + $score)
  431. WHERE c_id = $course_id AND id = " . $question_id;
  432. Database::query($sql);
  433. }
  434. }
  435. }