UniqueAnswerImage.php 13 KB

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