UniqueAnswerImage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * UniqueAnswerImage
  6. *
  7. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  8. */
  9. class UniqueAnswerImage extends UniqueAnswer
  10. {
  11. public static $typePicture = 'uaimg.png';
  12. public static $explanationLangVar = 'UniqueAnswerImage';
  13. /**
  14. * UniqueAnswerImage constructor.
  15. */
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->type = UNIQUE_ANSWER_IMAGE;
  20. $this->isContent = $this->getIsContent();
  21. }
  22. /**
  23. * @inheritdoc
  24. * @throws Exception
  25. */
  26. public function createAnswersForm($form)
  27. {
  28. $objExercise = Session::read('objExercise');
  29. $editorConfig = array(
  30. 'ToolbarSet' => 'TestFreeAnswer',
  31. 'Width' => '100%',
  32. 'Height' => '125'
  33. );
  34. //this line defines how many questions by default appear when creating a choice question
  35. // The previous default value was 2. See task #1759.
  36. $numberAnswers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
  37. $numberAnswers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  38. $feedbackTitle = '';
  39. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  40. //Scenario
  41. $commentTitle = '<th>'.get_lang('Comment').'</th>';
  42. $feedbackTitle = '<th>'.get_lang('Scenario').'</th>';
  43. } else {
  44. $commentTitle = '<th >'.get_lang('Comment').'</th>';
  45. }
  46. $html = '<div class="alert alert-success" role="alert">'.get_lang('UniqueAnswerImagePreferredSize200x150').'</div>';
  47. $html .= '<table class="table table-striped table-hover">
  48. <thead>
  49. <tr style="text-align: center;">
  50. <th width="10">' . get_lang('Number').'</th>
  51. <th>' . get_lang('True').'</th>
  52. <th>' . get_lang('Answer').'</th>
  53. ' . $commentTitle.'
  54. ' . $feedbackTitle.'
  55. <th width="15">' . get_lang('Weighting').'</th>
  56. </tr>
  57. </thead>
  58. <tbody>';
  59. $form->addHeader(get_lang('Answers'));
  60. $form->addHtml($html);
  61. $defaults = array();
  62. $correct = 0;
  63. if (!empty($this->id)) {
  64. $answer = new Answer($this->id);
  65. $answer->read();
  66. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  67. $numberAnswers = $answer->nbrAnswers;
  68. }
  69. }
  70. $form->addElement('hidden', 'nb_answers');
  71. //Feedback SELECT
  72. $questionList = $objExercise->selectQuestionList();
  73. $selectQuestion = array();
  74. $selectQuestion[0] = get_lang('SelectTargetQuestion');
  75. if (is_array($questionList)) {
  76. foreach ($questionList as $key => $questionid) {
  77. //To avoid warning messages
  78. if (!is_numeric($questionid)) {
  79. continue;
  80. }
  81. $question = Question::read($questionid);
  82. $selectQuestion[$questionid] = 'Q'.$key.' :'.cut(
  83. $question->selectTitle(), 20
  84. );
  85. }
  86. }
  87. $selectQuestion[-1] = get_lang('ExitTest');
  88. $list = new LearnpathList(api_get_user_id());
  89. $flatList = $list->get_flat_list();
  90. $selectLpId = array();
  91. $selectLpId[0] = get_lang('SelectTargetLP');
  92. foreach ($flatList as $id => $details) {
  93. $selectLpId[$id] = cut($details['lp_name'], 20);
  94. }
  95. $tempScenario = array();
  96. if ($numberAnswers < 1) {
  97. $numberAnswers = 1;
  98. echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  99. }
  100. for ($i = 1; $i <= $numberAnswers; ++$i) {
  101. $form->addHtml('<tr>');
  102. if (isset($answer) && is_object($answer)) {
  103. if ($answer->correct[$i]) {
  104. $correct = $i;
  105. }
  106. $defaults['answer['.$i.']'] = $answer->answer[$i];
  107. $defaults['comment['.$i.']'] = $answer->comment[$i];
  108. $defaults['weighting['.$i.']'] = float_format(
  109. $answer->weighting[$i],
  110. 1
  111. );
  112. $itemList = explode('@@', $answer->destination[$i]);
  113. $try = $itemList[0];
  114. $lp = $itemList[1];
  115. $listDestination = $itemList[2];
  116. $url = $itemList[3];
  117. $try = 0;
  118. if ($try != 0) {
  119. $tryResult = 1;
  120. }
  121. $urlResult = '';
  122. if ($url != 0) {
  123. $urlResult = $url;
  124. }
  125. $tempScenario['url'.$i] = $urlResult;
  126. $tempScenario['try'.$i] = $tryResult;
  127. $tempScenario['lp'.$i] = $lp;
  128. $tempScenario['destination'.$i] = $listDestination;
  129. } else {
  130. $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
  131. $defaults['weighting[1]'] = 10;
  132. $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
  133. $defaults['weighting[2]'] = 0;
  134. $tempScenario['destination'.$i] = array('0');
  135. $tempScenario['lp'.$i] = array('0');
  136. }
  137. $defaults['scenario'] = $tempScenario;
  138. $renderer = $form->defaultRenderer();
  139. $renderer->setElementTemplate(
  140. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  141. 'correct'
  142. );
  143. $renderer->setElementTemplate(
  144. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  145. 'counter['.$i.']'
  146. );
  147. $renderer->setElementTemplate(
  148. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  149. 'answer['.$i.']'
  150. );
  151. $renderer->setElementTemplate(
  152. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  153. 'comment['.$i.']'
  154. );
  155. $renderer->setElementTemplate(
  156. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  157. 'weighting['.$i.']'
  158. );
  159. $answerNumber = $form->addElement('text', 'counter['.$i.']', null, ' value = "'.$i.'"');
  160. $answerNumber->freeze();
  161. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox"');
  162. $form->addHtmlEditor('answer['.$i.']', null, null, true, $editorConfig);
  163. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  164. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  165. $form->addHtmlEditor(
  166. 'comment['.$i.']',
  167. null,
  168. null,
  169. false,
  170. $editorConfig
  171. );
  172. // Direct feedback
  173. // Adding extra feedback fields
  174. $group = array();
  175. $group['try'.$i] = $form->createElement('checkbox', 'try'.$i, null, get_lang('TryAgain'));
  176. $group['lp'.$i] = $form->createElement(
  177. 'select',
  178. 'lp'.$i,
  179. get_lang('SeeTheory').': ',
  180. $selectLpId
  181. );
  182. $group['destination'.$i] = $form->createElement(
  183. 'select',
  184. 'destination'.$i,
  185. get_lang('GoToQuestion').': ',
  186. $selectQuestion
  187. );
  188. $group['url'.$i] = $form->createElement(
  189. 'text', 'url'.$i,
  190. get_lang('Other').': ',
  191. array(
  192. 'class' => 'col-md-2',
  193. 'placeholder' => get_lang('Other')
  194. )
  195. );
  196. $form->addGroup($group, 'scenario');
  197. $renderer->setElementTemplate(
  198. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
  199. 'scenario'
  200. );
  201. } else {
  202. $form->addHtmlEditor('comment['.$i.']', null, null, false, $editorConfig);
  203. }
  204. $form->addText('weighting['.$i.']', null, null, array('class' => "col-md-1", 'value' => '0'));
  205. $form->addHtml('</tr>');
  206. }
  207. $form->addHtml('</tbody>');
  208. $form->addHtml('</table>');
  209. global $text;
  210. $buttonGroup = [];
  211. if ($objExercise->edit_exercise_in_lp == true) {
  212. //setting the save button here and not in the question class.php
  213. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  214. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  215. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  216. $form->addGroup($buttonGroup);
  217. }
  218. // We check the first radio button to be sure a radio button will be check
  219. if ($correct == 0) {
  220. $correct = 1;
  221. }
  222. $defaults['correct'] = $correct;
  223. if (!empty($this->id)) {
  224. $form->setDefaults($defaults);
  225. } else {
  226. if ($this->isContent == 1) {
  227. // Default sample content.
  228. $form->setDefaults($defaults);
  229. } else {
  230. $form->setDefaults(array('correct' => 1));
  231. }
  232. }
  233. $form->setConstants(array('nb_answers' => $numberAnswers));
  234. }
  235. public function processAnswersCreation($form)
  236. {
  237. $questionWeighting = $nbrGoodAnswers = 0;
  238. $correct = $form->getSubmitValue('correct');
  239. $objAnswer = new Answer($this->id);
  240. $numberAnswers = $form->getSubmitValue('nb_answers');
  241. for ($i = 1; $i <= $numberAnswers; $i++) {
  242. $answer = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('answer['.$i.']')));
  243. $comment = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('comment['.$i.']')));
  244. $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
  245. $scenario = $form->getSubmitValue('scenario');
  246. //$listDestination = $form -> getSubmitValue('destination'.$i);
  247. //$destinationStr = $form -> getSubmitValue('destination'.$i);
  248. $try = $scenario['try'.$i];
  249. $lp = $scenario['lp'.$i];
  250. $destination = $scenario['destination'.$i];
  251. $url = trim($scenario['url'.$i]);
  252. /*
  253. How we are going to parse the destination value
  254. here we parse the destination value which is a string
  255. 1@@3@@2;4;4;@@http://www.chamilo.org
  256. where: try_again@@lp_id@@selected_questions@@url
  257. try_again = is 1 || 0
  258. lp_id = id of a learning path (0 if dont select)
  259. selected_questions= ids of questions
  260. url= an url
  261. $destinationStr='';
  262. foreach ($listDestination as $destination_id)
  263. {
  264. $destinationStr.=$destination_id.';';
  265. } */
  266. $goodAnswer = $correct == $i ? true : false;
  267. if ($goodAnswer) {
  268. $nbrGoodAnswers++;
  269. $weighting = abs($weighting);
  270. if ($weighting > 0) {
  271. $questionWeighting += $weighting;
  272. }
  273. }
  274. if (empty($try)) {
  275. $try = 0;
  276. }
  277. if (empty($lp)) {
  278. $lp = 0;
  279. }
  280. if (empty($destination)) {
  281. $destination = 0;
  282. }
  283. if ($url == '') {
  284. $url = 0;
  285. }
  286. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  287. $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  288. $objAnswer->createAnswer(
  289. $answer,
  290. $goodAnswer,
  291. $comment,
  292. $weighting,
  293. $i,
  294. null,
  295. null,
  296. $dest
  297. );
  298. }
  299. // saves the answers into the data base
  300. $objAnswer->save();
  301. // sets the total weighting of the question
  302. $this->updateWeighting($questionWeighting);
  303. $this->save();
  304. }
  305. public function return_header($feedback_type = null, $counter = null, $score = null)
  306. {
  307. $header = '<table class="' . $this->question_table_class . '">
  308. <tr>
  309. <th>' . get_lang("Choice") . '</th>
  310. <th>' . get_lang("ExpectedChoice") . '</th>
  311. <th>' . get_lang("Answer") . '</th>';
  312. $header .= '<th>' . get_lang('Status') . '</th>';
  313. $header .= '<th>' . get_lang('Comment') . '</th>';
  314. $header .= '</tr>';
  315. return $header;
  316. }
  317. }