UniqueAnswerImage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. Display::display_normal_message(
  99. get_lang('YouHaveToCreateAtLeastOneAnswer')
  100. );
  101. }
  102. for ($i = 1; $i <= $numberAnswers; ++$i) {
  103. $form->addHtml('<tr>');
  104. if (isset($answer) && is_object($answer)) {
  105. if ($answer->correct[$i]) {
  106. $correct = $i;
  107. }
  108. $defaults['answer['.$i.']'] = $answer->answer[$i];
  109. $defaults['comment['.$i.']'] = $answer->comment[$i];
  110. $defaults['weighting['.$i.']'] = float_format(
  111. $answer->weighting[$i],
  112. 1
  113. );
  114. $itemList = explode('@@', $answer->destination[$i]);
  115. $try = $itemList[0];
  116. $lp = $itemList[1];
  117. $listDestination = $itemList[2];
  118. $url = $itemList[3];
  119. $try = 0;
  120. if ($try != 0) {
  121. $tryResult = 1;
  122. }
  123. $urlResult = '';
  124. if ($url != 0) {
  125. $urlResult = $url;
  126. }
  127. $tempScenario['url' . $i] = $urlResult;
  128. $tempScenario['try' . $i] = $tryResult;
  129. $tempScenario['lp' . $i] = $lp;
  130. $tempScenario['destination' . $i] = $listDestination;
  131. } else {
  132. $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
  133. $defaults['weighting[1]'] = 10;
  134. $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
  135. $defaults['weighting[2]'] = 0;
  136. $tempScenario['destination' . $i] = array('0');
  137. $tempScenario['lp' . $i] = array('0');
  138. }
  139. $defaults['scenario'] = $tempScenario;
  140. $renderer = $form->defaultRenderer();
  141. $renderer->setElementTemplate(
  142. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  143. 'correct'
  144. );
  145. $renderer->setElementTemplate(
  146. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  147. 'counter[' . $i . ']'
  148. );
  149. $renderer->setElementTemplate(
  150. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  151. 'answer[' . $i . ']'
  152. );
  153. $renderer->setElementTemplate(
  154. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  155. 'comment[' . $i . ']'
  156. );
  157. $renderer->setElementTemplate(
  158. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  159. 'weighting[' . $i . ']'
  160. );
  161. $answerNumber = $form->addElement('text', 'counter[' . $i . ']', null, ' value = "' . $i . '"');
  162. $answerNumber->freeze();
  163. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox"');
  164. $form->addHtmlEditor('answer[' . $i . ']', null, null, true, $editorConfig);
  165. $form->addRule('answer[' . $i . ']', get_lang('ThisFieldIsRequired'), 'required');
  166. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  167. $form->addHtmlEditor(
  168. 'comment['.$i.']',
  169. null,
  170. null,
  171. false,
  172. $editorConfig
  173. );
  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(
  179. 'select',
  180. 'lp' . $i,
  181. get_lang('SeeTheory') . ': ',
  182. $selectLpId
  183. );
  184. $group['destination' . $i] = $form->createElement(
  185. 'select',
  186. 'destination' . $i,
  187. get_lang('GoToQuestion') . ': ',
  188. $selectQuestion
  189. );
  190. $group['url' . $i] = $form->createElement(
  191. 'text', 'url' . $i,
  192. get_lang('Other') . ': ',
  193. array(
  194. 'class' => 'col-md-2',
  195. 'placeholder' => get_lang('Other')
  196. )
  197. );
  198. $form->addGroup($group, 'scenario');
  199. $renderer->setElementTemplate(
  200. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
  201. 'scenario'
  202. );
  203. } else {
  204. $form->addHtmlEditor('comment[' . $i . ']', null, null, false, $editorConfig);
  205. }
  206. $form->addText('weighting[' . $i . ']', null, null, array('class' => "col-md-1", 'value' => '0'));
  207. $form->addHtml('</tr>');
  208. }
  209. $form->addHtml('</tbody>');
  210. $form->addHtml('</table>');
  211. global $text;
  212. $buttonGroup = [];
  213. if ($objExercise->edit_exercise_in_lp == true) {
  214. //setting the save button here and not in the question class.php
  215. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  216. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  217. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  218. $form->addGroup($buttonGroup);
  219. }
  220. // We check the first radio button to be sure a radio button will be check
  221. if ($correct == 0) {
  222. $correct = 1;
  223. }
  224. $defaults['correct'] = $correct;
  225. if (!empty($this->id)) {
  226. $form->setDefaults($defaults);
  227. } else {
  228. if ($this->isContent == 1) {
  229. // Default sample content.
  230. $form->setDefaults($defaults);
  231. } else {
  232. $form->setDefaults(array('correct' => 1));
  233. }
  234. }
  235. $form->setConstants(array('nb_answers' => $numberAnswers));
  236. }
  237. public function processAnswersCreation($form)
  238. {
  239. $questionWeighting = $nbrGoodAnswers = 0;
  240. $correct = $form->getSubmitValue('correct');
  241. $objAnswer = new Answer($this->id);
  242. $numberAnswers = $form->getSubmitValue('nb_answers');
  243. for ($i = 1; $i <= $numberAnswers; $i++) {
  244. $answer = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('answer[' . $i . ']')));
  245. $comment = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('comment[' . $i . ']')));
  246. $weighting = trim($form->getSubmitValue('weighting[' . $i . ']'));
  247. $scenario = $form->getSubmitValue('scenario');
  248. //$listDestination = $form -> getSubmitValue('destination'.$i);
  249. //$destinationStr = $form -> getSubmitValue('destination'.$i);
  250. $try = $scenario['try' . $i];
  251. $lp = $scenario['lp' . $i];
  252. $destination = $scenario['destination' . $i];
  253. $url = trim($scenario['url' . $i]);
  254. /*
  255. How we are going to parse the destination value
  256. here we parse the destination value which is a string
  257. 1@@3@@2;4;4;@@http://www.chamilo.org
  258. where: try_again@@lp_id@@selected_questions@@url
  259. try_again = is 1 || 0
  260. lp_id = id of a learning path (0 if dont select)
  261. selected_questions= ids of questions
  262. url= an url
  263. $destinationStr='';
  264. foreach ($listDestination as $destination_id)
  265. {
  266. $destinationStr.=$destination_id.';';
  267. } */
  268. $goodAnswer = $correct == $i ? true : false;
  269. if ($goodAnswer) {
  270. $nbrGoodAnswers++;
  271. $weighting = abs($weighting);
  272. if ($weighting > 0) {
  273. $questionWeighting += $weighting;
  274. }
  275. }
  276. if (empty($try)) {
  277. $try = 0;
  278. }
  279. if (empty($lp)) {
  280. $lp = 0;
  281. }
  282. if (empty($destination)) {
  283. $destination = 0;
  284. }
  285. if ($url == '') {
  286. $url = 0;
  287. }
  288. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  289. $dest = $try . '@@' . $lp . '@@' . $destination . '@@' . $url;
  290. $objAnswer->createAnswer(
  291. $answer,
  292. $goodAnswer,
  293. $comment,
  294. $weighting,
  295. $i,
  296. null,
  297. null,
  298. $dest
  299. );
  300. }
  301. // saves the answers into the data base
  302. $objAnswer->save();
  303. // sets the total weighting of the question
  304. $this->updateWeighting($questionWeighting);
  305. $this->save();
  306. }
  307. public function return_header($feedback_type = null, $counter = null, $score = null)
  308. {
  309. return parent::return_header($feedback_type, $counter, $score);
  310. }
  311. }