unique_answer.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * File containing the UNIQUE_ANSWER class.
  5. * @package chamilo.exercise
  6. * @author Eric Marguin
  7. */
  8. /**
  9. * Code
  10. */
  11. if(!class_exists('UniqueAnswer')):
  12. /**
  13. CLASS UNIQUE_ANSWER
  14. *
  15. * This class allows to instantiate an object of type UNIQUE_ANSWER (MULTIPLE CHOICE, UNIQUE ANSWER),
  16. * extending the class question
  17. *
  18. * @author Eric Marguin
  19. * @author Julio Montoya
  20. * @package chamilo.exercise
  21. **/
  22. class UniqueAnswer extends Question {
  23. static $typePicture = 'mcua.gif';
  24. static $explanationLangVar = 'UniqueSelect';
  25. /**
  26. * Constructor
  27. */
  28. function UniqueAnswer() {
  29. //this is highly important
  30. parent::question();
  31. $this -> type = UNIQUE_ANSWER;
  32. $this -> isContent = $this-> getIsContent();
  33. }
  34. /**
  35. * function which redifines Question::createAnswersForm
  36. * @param the formvalidator instance
  37. * @param the answers number to display
  38. */
  39. function createAnswersForm ($form) {
  40. // Getting the exercise list
  41. $obj_ex = $_SESSION['objExercise'];
  42. $editor_config = array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '125');
  43. //this line define how many question by default appear when creating a choice question
  44. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4; // The previous default value was 2. See task #1759.
  45. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  46. /*
  47. Types of Feedback
  48. $feedback_option[0]=get_lang('Feedback');
  49. $feedback_option[1]=get_lang('DirectFeedback');
  50. $feedback_option[2]=get_lang('NoFeedback');
  51. */
  52. $feedback_title='';
  53. $comment_title='';
  54. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  55. $comment_title = '<th>'.get_lang('Comment').'</th>';
  56. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  57. //Scenario
  58. $editor_config['Width'] = '250';
  59. $editor_config['Height'] = '110';
  60. $comment_title = '<th width="500px" >'.get_lang('Comment').'</th>';
  61. $feedback_title = '<th width="350px" >'.get_lang('Scenario').'</th>';
  62. }
  63. $html='<table class="data_table">
  64. <tr style="text-align: center;">
  65. <th width="10px">
  66. '.get_lang('Number').'
  67. </th>
  68. <th width="10px" >
  69. '.get_lang('True').'
  70. </th>
  71. <th width="50%">
  72. '.get_lang('Answer').'
  73. </th>
  74. '.$comment_title.'
  75. '.$feedback_title.'
  76. <th width="50px">
  77. '.get_lang('Weighting').'
  78. </th>
  79. </tr>';
  80. $form -> addElement ('label', get_lang('Answers').'<br /> <img src="../img/fill_field.png">', $html);
  81. $defaults = array();
  82. $correct = 0;
  83. if(!empty($this -> id)) {
  84. $answer = new Answer($this -> id);
  85. $answer -> read();
  86. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted()) {
  87. $nb_answers = $answer->nbrAnswers;
  88. }
  89. }
  90. $form -> addElement('hidden', 'nb_answers');
  91. //Feedback SELECT
  92. $question_list = $obj_ex->selectQuestionList();
  93. $select_question=array();
  94. $select_question[0] = get_lang('SelectTargetQuestion');
  95. require_once '../newscorm/learnpathList.class.php';
  96. if (is_array($question_list)) {
  97. foreach ($question_list as $key=>$questionid) {
  98. //To avoid warning messages
  99. if (!is_numeric($questionid)) {
  100. continue;
  101. }
  102. $question = Question::read($questionid);
  103. if($question) {
  104. $select_question[$questionid]='Q'.$key.' :'.Text::cut($question->selectTitle(),20);
  105. }
  106. }
  107. }
  108. $select_question[-1] = get_lang('ExitTest');
  109. $list = new LearnpathList(api_get_user_id());
  110. $flat_list = $list->get_flat_list();
  111. $select_lp_id=array();
  112. $select_lp_id[0]=get_lang('SelectTargetLP');
  113. foreach ($flat_list as $id => $details) {
  114. $select_lp_id[$id] = Text::cut($details['lp_name'],20);
  115. }
  116. $temp_scenario = array();
  117. if ($nb_answers < 1) {
  118. $nb_answers = 1;
  119. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  120. }
  121. for ($i = 1 ; $i <= $nb_answers ; ++$i) {
  122. $form -> addElement ('html', '<tr>');
  123. if (isset($answer) && is_object($answer)) {
  124. if ($answer -> correct[$i]) {
  125. $correct = $i;
  126. }
  127. $defaults['answer['.$i.']'] = $answer->answer[$i];
  128. $defaults['comment['.$i.']'] = $answer->comment[$i];
  129. $defaults['weighting['.$i.']'] = Text::float_format($answer->weighting[$i], 1);
  130. $item_list=explode('@@',$answer -> destination[$i]);
  131. $try = $item_list[0];
  132. $lp = $item_list[1];
  133. $list_dest = $item_list[2];
  134. $url = $item_list[3];
  135. if ($try==0) {
  136. $try_result = 0;
  137. } else {
  138. $try_result = 1;
  139. }
  140. if ($url==0) {
  141. $url_result='';
  142. } else {
  143. $url_result = $url;
  144. }
  145. $temp_scenario['url'.$i] = $url_result;
  146. $temp_scenario['try'.$i] = $try_result;
  147. $temp_scenario['lp'.$i] = $lp;
  148. $temp_scenario['destination'.$i]= $list_dest;
  149. } else {
  150. $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
  151. $defaults['weighting[1]'] = 10;
  152. $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
  153. $defaults['weighting[2]'] = 0;
  154. $temp_scenario['destination'.$i] = array('0');
  155. $temp_scenario['lp'.$i] = array('0');
  156. }
  157. $defaults['scenario'] = $temp_scenario;
  158. $renderer = $form->defaultRenderer();
  159. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct');
  160. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  161. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  162. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  163. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'weighting['.$i.']');
  164. $answer_number = $form->addElement('text', 'counter['.$i.']',null,' value = "'.$i.'"');
  165. $answer_number->freeze();
  166. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  167. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  168. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  169. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  170. // feedback
  171. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  172. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  173. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  174. // Direct feedback
  175. //Adding extra feedback fields
  176. $group = array();
  177. $group['try'.$i] = $form->createElement('checkbox', 'try'.$i, null , get_lang('TryAgain'));
  178. $group['lp'.$i] = $form->createElement('select', 'lp'.$i, get_lang('SeeTheory').': ', $select_lp_id);
  179. $group['destination'.$i]= $form->createElement('select', 'destination'.$i, get_lang('GoToQuestion').': ' , $select_question);
  180. $group['url'.$i] = $form->createElement('text', 'url'.$i, get_lang('Other').': ', array('class'=>'span2', 'placeholder' => get_lang('Other')));
  181. $form->addGroup($group, 'scenario');
  182. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}', 'scenario');
  183. }
  184. $form->addElement('text', 'weighting['.$i.']', null, array('class' => "span1", 'value' => '0'));
  185. $form->addElement ('html', '</tr>');
  186. }
  187. $form -> addElement ('html', '</table>');
  188. $form -> addElement ('html', '<br />');
  189. $navigator_info = api_get_navigator();
  190. global $text, $class;
  191. //ie6 fix
  192. if ($obj_ex->edit_exercise_in_lp == true) {
  193. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  194. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  195. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  196. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  197. } else {
  198. //setting the save button here and not in the question class.php
  199. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  200. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  201. $form->addElement('style_submit_button', 'submitQuestion',$text, 'class="'.$class.'"');
  202. }
  203. }
  204. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  205. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  206. $renderer->setElementTemplate('{element}&nbsp;','moreAnswers');
  207. $form->addElement('html', '</div></div>');
  208. //We check the first radio button to be sure a radio button will be check
  209. if ($correct==0) {
  210. $correct=1;
  211. }
  212. $defaults['correct'] = $correct;
  213. if (!empty($this -> id)) {
  214. $form -> setDefaults($defaults);
  215. } else {
  216. if ($this -> isContent == 1) {
  217. $form -> setDefaults($defaults);
  218. }
  219. }
  220. $form->setConstants(array('nb_answers' => $nb_answers));
  221. }
  222. /**
  223. * abstract function which creates the form to create / edit the answers of the question
  224. * @param the formvalidator instance
  225. * @param the answers number to display
  226. */
  227. function processAnswersCreation($form) {
  228. $questionWeighting = $nbrGoodAnswers = 0;
  229. $correct = $form -> getSubmitValue('correct');
  230. $objAnswer = new Answer($this->id);
  231. $nb_answers = $form->getSubmitValue('nb_answers');
  232. for ($i=1 ; $i <= $nb_answers ; $i++) {
  233. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  234. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  235. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  236. $scenario = $form -> getSubmitValue('scenario');
  237. //$list_destination = $form -> getSubmitValue('destination'.$i);
  238. //$destination_str = $form -> getSubmitValue('destination'.$i);
  239. $try = $scenario['try'.$i];
  240. $lp = $scenario['lp'.$i];
  241. $destination = $scenario['destination'.$i];
  242. $url = trim($scenario['url'.$i]);
  243. /*
  244. How we are going to parse the destination value
  245. here we parse the destination value which is a string
  246. 1@@3@@2;4;4;@@http://www.chamilo.org
  247. where: try_again@@lp_id@@selected_questions@@url
  248. try_again = is 1 || 0
  249. lp_id = id of a learning path (0 if dont select)
  250. selected_questions= ids of questions
  251. url= an url
  252. $destination_str='';
  253. foreach ($list_destination as $destination_id)
  254. {
  255. $destination_str.=$destination_id.';';
  256. }*/
  257. $goodAnswer = ($correct == $i) ? true : false;
  258. if ($goodAnswer) {
  259. $nbrGoodAnswers++;
  260. $weighting = abs($weighting);
  261. if($weighting > 0) {
  262. $questionWeighting += $weighting;
  263. }
  264. }
  265. if (empty($try)) {
  266. $try = 0;
  267. }
  268. if (empty($lp)) {
  269. $lp = 0;
  270. }
  271. if (empty($destination)) {
  272. $destination=0;
  273. }
  274. if ($url=='') {
  275. $url=0;
  276. }
  277. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  278. $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  279. $objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i, null, null, $dest);
  280. }
  281. // saves the answers into the data base
  282. $objAnswer->save();
  283. // sets the total weighting of the question
  284. $this->updateWeighting($questionWeighting);
  285. $this->save();
  286. }
  287. function return_header($feedback_type = null, $counter = null, $score = null, $show_media = false) {
  288. $header = parent::return_header($feedback_type, $counter, $score, $show_media);
  289. $header .= '<table class="'.$this->question_table_class .'">
  290. <tr>
  291. <th>'.get_lang("Choice").'</th>
  292. <th>'. get_lang("ExpectedChoice").'</th>
  293. <th>'. get_lang("Answer").'</th>';
  294. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  295. $header .= '<th>'.get_lang("Comment").'</th>';
  296. } else {
  297. $header .= '<th>&nbsp;</th>';
  298. }
  299. $header .= '</tr>';
  300. return $header;
  301. }
  302. /**
  303. * Create database record for the given answer
  304. * @param int The answer ID (technically 1 is OK if you don't know)
  305. * @param int The question this answer is attached to
  306. * @param string The answer itself
  307. * @param string The answer comment (shown as feedback if enabled)
  308. * @param float Score given by this answer if selected (can be negative)
  309. * @param int Whether this answer is considered correct (1) or not (0)
  310. * @param int The course ID - if not provided, will be guessed from the context
  311. * @return void
  312. * @assert (1,null,'a','',1,1,null) === false
  313. * @assert (1,1,'','',1,1,null) === false
  314. */
  315. function create_answer($id=1, $question_id, $answer_title, $comment = '', $score = 0, $correct = 0, $course_id = null) {
  316. if (empty($question_id) or empty($answer_title)) { return false; }
  317. $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  318. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  319. if (empty($course_id)) {
  320. $course_id = api_get_course_int_id();
  321. }
  322. $position = 1;
  323. $question_id = filter_var($question_id,FILTER_SANITIZE_NUMBER_INT);
  324. $score = filter_var($score,FILTER_SANITIZE_NUMBER_FLOAT);
  325. $correct = filter_var($correct,FILTER_SANITIZE_NUMBER_INT);
  326. // Get the max position
  327. $sql = "SELECT max(position) as max_position FROM $tbl_quiz_answer "
  328. ." WHERE c_id = $course_id AND question_id = $question_id";
  329. $rs_max = Database::query($sql);
  330. $row_max = Database::fetch_object($rs_max);
  331. $position = $row_max->max_position + 1;
  332. // Insert a new answer
  333. $sql = "INSERT INTO $tbl_quiz_answer "
  334. ."(c_id, id, question_id,answer,correct,comment,ponderation,position,destination)"
  335. ."VALUES ($course_id, $id,$question_id,'".Database::escape_string($answer_title)."',"
  336. ."$correct,'".Database::escape_string($comment)."',$score,$position, "
  337. ." '0@@0@@0@@0')";
  338. $rs = Database::query($sql);
  339. if ($correct) {
  340. $sql = "UPDATE $tbl_quiz_question "
  341. ." SET ponderation = (ponderation + $score) WHERE c_id = $course_id AND id = ".$question_id;
  342. $rs = Database::query($sql);
  343. return $rs;
  344. }
  345. }
  346. }
  347. endif;