unique_answer.class.php 19 KB

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