unique_answer.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. $editor_config['Width'] = '250';
  55. $editor_config['Height'] = '110';
  56. $comment_title = '<th width="500px" >' . get_lang('Comment') . '</th>';
  57. $feedback_title = '<th width="350px" >' . get_lang('Scenario') . '</th>';
  58. } else {
  59. $comment_title = '<th>' . get_lang('Comment') . '</th>';
  60. }
  61. $html = '<table class="data_table">
  62. <tr style="text-align: center;">
  63. <th width="10px">
  64. ' . get_lang('Number') . '
  65. </th>
  66. <th width="10px" >
  67. ' . get_lang('True') . '
  68. </th>
  69. <th width="50%">
  70. ' . get_lang('Answer') . '
  71. </th>
  72. ' . $comment_title . '
  73. ' . $feedback_title . '
  74. <th width="50px">
  75. ' . get_lang('Weighting') . '
  76. </th>
  77. </tr>';
  78. $form->addElement(
  79. 'label',
  80. get_lang('Answers') . '<br /> <img src="../img/fill_field.png">',
  81. $html
  82. );
  83. $defaults = array();
  84. $correct = 0;
  85. if (!empty($this->id)) {
  86. $answer = new Answer($this->id);
  87. $answer->read();
  88. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  89. $nb_answers = $answer->nbrAnswers;
  90. }
  91. }
  92. $form->addElement('hidden', 'nb_answers');
  93. //Feedback SELECT
  94. $question_list = $obj_ex->selectQuestionList();
  95. $select_question = array();
  96. $select_question[0] = get_lang('SelectTargetQuestion');
  97. if (is_array($question_list)) {
  98. foreach ($question_list as $key => $questionid) {
  99. //To avoid warning messages
  100. if (!is_numeric($questionid)) {
  101. continue;
  102. }
  103. $question = Question::read($questionid);
  104. $select_question[$questionid] = 'Q' . $key . ' :' . cut(
  105. $question->selectTitle(),
  106. 20
  107. );
  108. }
  109. }
  110. $select_question[-1] = get_lang('ExitTest');
  111. $list = new LearnpathList(api_get_user_id());
  112. $flat_list = $list->get_flat_list();
  113. $select_lp_id = array();
  114. $select_lp_id[0] = get_lang('SelectTargetLP');
  115. foreach ($flat_list as $id => $details) {
  116. $select_lp_id[$id] = cut($details['lp_name'], 20);
  117. }
  118. $temp_scenario = array();
  119. if ($nb_answers < 1) {
  120. $nb_answers = 1;
  121. Display::display_normal_message(
  122. get_lang('YouHaveToCreateAtLeastOneAnswer')
  123. );
  124. }
  125. for ($i = 1; $i <= $nb_answers; ++$i) {
  126. $form->addElement('html', '<tr>');
  127. if (isset($answer) && is_object($answer)) {
  128. if ($answer->correct[$i]) {
  129. $correct = $i;
  130. }
  131. $defaults['answer[' . $i . ']'] = $answer->answer[$i];
  132. $defaults['comment[' . $i . ']'] = $answer->comment[$i];
  133. $defaults['weighting[' . $i . ']'] = float_format(
  134. $answer->weighting[$i],
  135. 1
  136. );
  137. $item_list = explode('@@', $answer->destination[$i]);
  138. $try = $item_list[0];
  139. $lp = $item_list[1];
  140. $list_dest = $item_list[2];
  141. $url = $item_list[3];
  142. if ($try == 0) {
  143. $try_result = 0;
  144. } else {
  145. $try_result = 1;
  146. }
  147. if ($url == 0) {
  148. $url_result = '';
  149. } else {
  150. $url_result = $url;
  151. }
  152. $temp_scenario['url' . $i] = $url_result;
  153. $temp_scenario['try' . $i] = $try_result;
  154. $temp_scenario['lp' . $i] = $lp;
  155. $temp_scenario['destination' . $i] = $list_dest;
  156. } else {
  157. $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
  158. $defaults['weighting[1]'] = 10;
  159. $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
  160. $defaults['weighting[2]'] = 0;
  161. $temp_scenario['destination' . $i] = array('0');
  162. $temp_scenario['lp' . $i] = array('0');
  163. }
  164. $defaults['scenario'] = $temp_scenario;
  165. $renderer = $form->defaultRenderer();
  166. $renderer->setElementTemplate(
  167. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  168. 'correct'
  169. );
  170. $renderer->setElementTemplate(
  171. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  172. 'counter[' . $i . ']'
  173. );
  174. $renderer->setElementTemplate(
  175. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  176. 'answer[' . $i . ']'
  177. );
  178. $renderer->setElementTemplate(
  179. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  180. 'comment[' . $i . ']'
  181. );
  182. $renderer->setElementTemplate(
  183. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  184. 'weighting[' . $i . ']'
  185. );
  186. $answer_number = $form->addElement(
  187. 'text',
  188. 'counter[' . $i . ']',
  189. null,
  190. ' value = "' . $i . '"'
  191. );
  192. $answer_number->freeze();
  193. $form->addElement(
  194. 'radio',
  195. 'correct',
  196. null,
  197. null,
  198. $i,
  199. 'class="checkbox" style="margin-left: 0em;"'
  200. );
  201. $form->addElement(
  202. 'html_editor',
  203. 'answer[' . $i . ']',
  204. null,
  205. 'style="vertical-align:middle"',
  206. $editor_config
  207. );
  208. $form->addRule(
  209. 'answer[' . $i . ']',
  210. get_lang('ThisFieldIsRequired'),
  211. 'required'
  212. );
  213. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  214. $form->addElement(
  215. 'html_editor',
  216. 'comment[' . $i . ']',
  217. null,
  218. 'style="vertical-align:middle"',
  219. $editor_config
  220. );
  221. // Direct feedback
  222. //Adding extra feedback fields
  223. $group = array();
  224. $group['try' . $i] = $form->createElement(
  225. 'checkbox',
  226. 'try' . $i,
  227. null,
  228. get_lang('TryAgain')
  229. );
  230. $group['lp' . $i] = $form->createElement(
  231. 'select',
  232. 'lp' . $i,
  233. get_lang('SeeTheory') . ': ',
  234. $select_lp_id
  235. );
  236. $group['destination' . $i] = $form->createElement(
  237. 'select',
  238. 'destination' . $i,
  239. get_lang('GoToQuestion') . ': ',
  240. $select_question
  241. );
  242. $group['url' . $i] = $form->createElement(
  243. 'text',
  244. 'url' . $i,
  245. get_lang('Other') . ': ',
  246. array(
  247. 'class' => 'span2',
  248. 'placeholder' => get_lang('Other')
  249. )
  250. );
  251. $form->addGroup($group, 'scenario');
  252. $renderer->setElementTemplate(
  253. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
  254. 'scenario'
  255. );
  256. } else {
  257. $form->addElement(
  258. 'html_editor',
  259. 'comment[' . $i . ']',
  260. null,
  261. 'style="vertical-align:middle"',
  262. $editor_config
  263. );
  264. }
  265. $form->addElement(
  266. 'text',
  267. 'weighting[' . $i . ']',
  268. null,
  269. array('class' => "span1", 'value' => '0')
  270. );
  271. $form->addElement('html', '</tr>');
  272. }
  273. $form->addElement('html', '</table>');
  274. $form->addElement('html', '<br />');
  275. $navigator_info = api_get_navigator();
  276. global $text, $class;
  277. //ie6 fix
  278. if ($obj_ex->edit_exercise_in_lp == true) {
  279. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  280. $form->addElement(
  281. 'submit',
  282. 'lessAnswers',
  283. get_lang('LessAnswer'),
  284. 'class="btn minus"'
  285. );
  286. $form->addElement(
  287. 'submit',
  288. 'moreAnswers',
  289. get_lang('PlusAnswer'),
  290. 'class="btn plus"'
  291. );
  292. $form->addElement(
  293. 'submit',
  294. 'submitQuestion',
  295. $text,
  296. 'class="' . $class . '"'
  297. );
  298. } else {
  299. //setting the save button here and not in the question class.php
  300. $form->addElement(
  301. 'style_submit_button',
  302. 'lessAnswers',
  303. get_lang('LessAnswer'),
  304. 'class="btn minus"'
  305. );
  306. $form->addElement(
  307. 'style_submit_button',
  308. 'moreAnswers',
  309. get_lang('PlusAnswer'),
  310. 'class="btn plus"'
  311. );
  312. $form->addElement(
  313. 'style_submit_button',
  314. 'submitQuestion',
  315. $text,
  316. 'class="' . $class . '"'
  317. );
  318. }
  319. }
  320. $renderer->setElementTemplate('{element}&nbsp;', 'submitQuestion');
  321. $renderer->setElementTemplate('{element}&nbsp;', 'lessAnswers');
  322. $renderer->setElementTemplate('{element}&nbsp;', 'moreAnswers');
  323. $form->addElement('html', '</div></div>');
  324. // We check the first radio button to be sure a radio button will be check
  325. if ($correct == 0) {
  326. $correct = 1;
  327. }
  328. $defaults['correct'] = $correct;
  329. if (!empty($this->id)) {
  330. $form->setDefaults($defaults);
  331. } else {
  332. if ($this->isContent == 1) {
  333. // Default sample content.
  334. $form->setDefaults($defaults);
  335. } else {
  336. $form->setDefaults(array('correct' => 1));
  337. }
  338. }
  339. $form->setConstants(array('nb_answers' => $nb_answers));
  340. }
  341. /**
  342. * Receives the unique answer question type creation form data and creates
  343. * or updates the answers from that question
  344. * @param FormValidator $form
  345. */
  346. public function processAnswersCreation($form)
  347. {
  348. $questionWeighting = $nbrGoodAnswers = 0;
  349. $correct = $form->getSubmitValue('correct');
  350. $objAnswer = new Answer($this->id);
  351. $nb_answers = $form->getSubmitValue('nb_answers');
  352. for ($i = 1; $i <= $nb_answers; $i++) {
  353. $answer = trim($form->getSubmitValue('answer[' . $i . ']'));
  354. $comment = trim($form->getSubmitValue('comment[' . $i . ']'));
  355. $weighting = trim($form->getSubmitValue('weighting[' . $i . ']'));
  356. $scenario = $form->getSubmitValue('scenario');
  357. //$list_destination = $form -> getSubmitValue('destination'.$i);
  358. //$destination_str = $form -> getSubmitValue('destination'.$i);
  359. $try = $scenario['try' . $i];
  360. $lp = $scenario['lp' . $i];
  361. $destination = $scenario['destination' . $i];
  362. $url = trim($scenario['url' . $i]);
  363. /*
  364. How we are going to parse the destination value
  365. here we parse the destination value which is a string
  366. 1@@3@@2;4;4;@@http://www.chamilo.org
  367. where: try_again@@lp_id@@selected_questions@@url
  368. try_again = is 1 || 0
  369. lp_id = id of a learning path (0 if dont select)
  370. selected_questions= ids of questions
  371. url= an url
  372. $destination_str='';
  373. foreach ($list_destination as $destination_id)
  374. {
  375. $destination_str.=$destination_id.';';
  376. }*/
  377. $goodAnswer = ($correct == $i) ? true : false;
  378. if ($goodAnswer) {
  379. $nbrGoodAnswers++;
  380. $weighting = abs($weighting);
  381. if ($weighting > 0) {
  382. $questionWeighting += $weighting;
  383. }
  384. }
  385. if (empty($try)) {
  386. $try = 0;
  387. }
  388. if (empty($lp)) {
  389. $lp = 0;
  390. }
  391. if (empty($destination)) {
  392. $destination = 0;
  393. }
  394. if ($url == '') {
  395. $url = 0;
  396. }
  397. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  398. $dest = $try . '@@' . $lp . '@@' . $destination . '@@' . $url;
  399. $objAnswer->createAnswer(
  400. $answer,
  401. $goodAnswer,
  402. $comment,
  403. $weighting,
  404. $i,
  405. null,
  406. null,
  407. $dest
  408. );
  409. }
  410. // saves the answers into the data base
  411. $objAnswer->save();
  412. // sets the total weighting of the question
  413. $this->updateWeighting($questionWeighting);
  414. $this->save();
  415. }
  416. /**
  417. * Helper function to print the column titles in the answers edition form
  418. * @param null $feedback_type The type of feedback influences what columns are shown to the editor
  419. * @param null $counter The number of answers to show, in case there should be pagination
  420. * @param null $score The maximum score for the question
  421. * @return string HTML string for a table header + a wrapper before it
  422. */
  423. public function return_header(
  424. $feedback_type = null,
  425. $counter = null,
  426. $score = null
  427. ) {
  428. $header = parent::return_header($feedback_type, $counter, $score);
  429. $header .= '<table class="' . $this->question_table_class . '">
  430. <tr>
  431. <th>' . get_lang("Choice") . '</th>
  432. <th>' . get_lang("ExpectedChoice") . '</th>
  433. <th>' . get_lang("Answer") . '</th>';
  434. $header .= '<th>' . get_lang("Comment") . '</th>';
  435. $header .= '</tr>';
  436. return $header;
  437. }
  438. /**
  439. * Saves one answer to the database
  440. * @param int $id The ID of the answer (has to be calculated for this course)
  441. * @param int $question_id The question ID (to which the answer is attached)
  442. * @param string $title The text of the answer
  443. * @param string $comment The feedback for the answer
  444. * @param float|null $score The score you get when picking this answer
  445. * @param int|null $correct Whether this answer is considered *the* correct one (this is the unique answer type)
  446. */
  447. public function addAnswer(
  448. $id,
  449. $question_id,
  450. $title,
  451. $comment,
  452. $score = 0.0,
  453. $correct = 0
  454. ) {
  455. $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  456. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  457. $course_id = api_get_course_int_id();
  458. $question_id = intval($question_id);
  459. $score = floatval($score);
  460. $correct = intval($correct);
  461. $title = Database::escape_string($title);
  462. $comment = Database::escape_string($comment);
  463. // Get the max position.
  464. $sql = "SELECT max(position) as max_position
  465. FROM $tbl_quiz_answer
  466. WHERE
  467. c_id = $course_id AND
  468. question_id = $question_id";
  469. $rs_max = Database::query($sql);
  470. $row_max = Database::fetch_object($rs_max);
  471. $position = $row_max->max_position + 1;
  472. // Insert a new answer
  473. $sql = "INSERT INTO $tbl_quiz_answer (
  474. c_id,
  475. id,
  476. question_id,
  477. answer,
  478. correct,
  479. comment,
  480. ponderation,
  481. position,
  482. destination
  483. ) VALUES (
  484. $course_id,
  485. $id,
  486. $question_id,
  487. '" . $title . "',
  488. $correct,
  489. '" . $comment . "',
  490. '$score', $position,
  491. '0@@0@@0@@0'
  492. )";
  493. Database::query($sql);
  494. if ($correct) {
  495. $sql = "UPDATE $tbl_quiz_question
  496. SET ponderation = (ponderation + $score)
  497. WHERE c_id = $course_id AND id = " . $question_id;
  498. Database::query($sql);
  499. }
  500. }
  501. }