survey_question.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class survey_question
  6. */
  7. class survey_question
  8. {
  9. /** @var FormValidator */
  10. private $form;
  11. public $buttonList = array();
  12. /**
  13. * Generic part of any survey question: the question field
  14. * @param array $surveyData
  15. * @param array $formData
  16. *
  17. * @return FormValidator
  18. */
  19. public function createForm($surveyData, $formData)
  20. {
  21. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : null;
  22. $questionId = isset($_GET['question_id']) ? intval($_GET['question_id']) : null;
  23. $surveyId = isset($_GET['survey_id']) ? intval($_GET['survey_id']) : null;
  24. $toolName = Display::return_icon(
  25. SurveyManager::icon_question(Security::remove_XSS($_GET['type'])),
  26. get_lang(ucfirst(Security::remove_XSS($_GET['type']))),
  27. array('align' => 'middle', 'height' => '22px')
  28. ).' ';
  29. if ($action == 'add') {
  30. $toolName .= get_lang('AddQuestion');
  31. }
  32. if ($action == 'edit') {
  33. $toolName .= get_lang('EditQuestion');
  34. }
  35. if ($_GET['type'] == 'yesno') {
  36. $toolName .= ': '.get_lang('YesNo');
  37. } else if ($_GET['type'] == 'multiplechoice') {
  38. $toolName .= ': '.get_lang('UniqueSelect');
  39. } else {
  40. $toolName .= ': '.get_lang(api_ucfirst(Security::remove_XSS($_GET['type'])));
  41. }
  42. $sharedQuestionId = isset($formData['shared_question_id']) ? $formData['shared_question_id'] : null;
  43. $url = api_get_self().'?action='.$action.'&type='.Security::remove_XSS($_GET['type']).'&survey_id='.$surveyId.'&question_id='.$questionId.'&'.api_get_cidreq();
  44. $form = new FormValidator('question_form', 'post', $url);
  45. $form->addHeader($toolName);
  46. $form->addHidden('survey_id', $surveyId);
  47. $form->addHidden('question_id', $questionId);
  48. $form->addHidden('shared_question_id', Security::remove_XSS($sharedQuestionId));
  49. $form->addHidden('type', Security::remove_XSS($_GET['type']));
  50. $config = array('ToolbarSet' => 'SurveyQuestion', 'Width' => '100%', 'Height' => '120');
  51. $form->addHtmlEditor('question', get_lang('Question'), true, false, $config);
  52. // When survey type = 1??
  53. if ($surveyData['survey_type'] == 1) {
  54. $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  55. $sql = 'SELECT id,name FROM '.$table_survey_question_group.'
  56. WHERE survey_id = '.(int) $_GET['survey_id'].'
  57. ORDER BY name';
  58. $rs = Database::query($sql);
  59. $glist = null;
  60. while ($row = Database::fetch_array($rs, 'NUM')) {
  61. $glist .= '<option value="'.$row[0].'" >'.$row[1].'</option>';
  62. }
  63. $grouplist = $grouplist1 = $grouplist2 = $glist;
  64. if (!empty($formData['assigned'])) {
  65. $grouplist = str_replace('<option value="'.$formData['assigned'].'"', '<option value="'.$formData['assigned'].'" selected', $glist);
  66. }
  67. if (!empty($formData['assigned1'])) {
  68. $grouplist1 = str_replace('<option value="'.$formData['assigned1'].'"', '<option value="'.$formData['assigned1'].'" selected', $glist);
  69. }
  70. if (!empty($formData['assigned2'])) {
  71. $grouplist2 = str_replace('<option value="'.$formData['assigned2'].'"', '<option value="'.$formData['assigned2'].'" selected', $glist);
  72. }
  73. $this->html .= ' <tr><td colspan="">
  74. <fieldset style="border:1px solid black"><legend>'.get_lang('Condition').'</legend>
  75. <b>'.get_lang('Primary').'</b><br />
  76. '.'<input type="radio" name="choose" value="1" '.(($formData['choose'] == 1) ? 'checked' : '').
  77. '><select name="assigned">'.$grouplist.'</select><br />';
  78. $this->html .= '
  79. <b>'.get_lang('Secondary').'</b><br />
  80. '.'<input type="radio" name="choose" value="2" '.(($formData['choose'] == 2) ? 'checked' : '').
  81. '><select name="assigned1">'.$grouplist1.'</select> '.
  82. '<select name="assigned2">'.$grouplist2.'</select>'
  83. .'</fieldset><br />';
  84. //$form->addRadio('choose', get_lang('Primary'));
  85. //$form->addRadio('choose', get_lang('Secondary'));
  86. }
  87. $this->setForm($form);
  88. return $form;
  89. }
  90. /**
  91. * Adds submit button
  92. *
  93. */
  94. public function renderForm()
  95. {
  96. if (isset($_GET['question_id']) and !empty($_GET['question_id'])) {
  97. /**
  98. * Check if survey has answers first before update it, this is because if you update it, the question
  99. * options will delete and re-insert in database loosing the iid and question_id to verify the correct answers
  100. */
  101. $surveyId = isset($_GET['survey_id']) ? intval($_GET['survey_id']) : 0;
  102. $answersChecker = SurveyUtil::checkIfSurveyHasAnswers($surveyId);
  103. if (!$answersChecker) {
  104. $this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true);
  105. } else {
  106. $this->getForm()->addHtml('
  107. <div class="form-group">
  108. <label class="col-sm-2 control-label"></label>
  109. <div class="col-sm-8">
  110. <div class="alert alert-info">' . get_lang('YouCantNotEditThisQuestionBecauseAlreadyExistAnswers').'</div>
  111. </div>
  112. <div class="col-sm-2"></div>
  113. </div>
  114. ');
  115. }
  116. } else {
  117. $this->buttonList[] = $this->getForm()->addButtonSave(get_lang('CreateQuestionSurvey'), 'save', true);
  118. }
  119. $this->getForm()->addGroup($this->buttonList, 'buttons');
  120. }
  121. /**
  122. * @return FormValidator
  123. */
  124. public function getForm()
  125. {
  126. return $this->form;
  127. }
  128. /**
  129. * @param FormValidator $form
  130. */
  131. public function setForm($form)
  132. {
  133. $this->form = $form;
  134. }
  135. /**
  136. * @param array $formData
  137. *
  138. * @return mixed
  139. */
  140. public function preSave($formData)
  141. {
  142. $counter = Session::read('answer_count');
  143. $answerList = Session::read('answer_list');
  144. if (empty($answerList)) {
  145. $answerList = isset($formData['answers']) ? $formData['answers'] : array();
  146. Session::write('answer_list', $answerList);
  147. }
  148. if (isset($_POST['answers'])) {
  149. $formData['answers'] = $_POST['answers'];
  150. }
  151. if (empty($counter)) {
  152. $counter = count($answerList) - 1;
  153. Session::write('answer_count', $counter);
  154. }
  155. // Moving an answer up
  156. if (isset($_POST['move_up']) && $_POST['move_up']) {
  157. foreach ($_POST['move_up'] as $key => & $value) {
  158. $id1 = $key;
  159. $content1 = $formData['answers'][$id1];
  160. $id2 = $key - 1;
  161. $content2 = $formData['answers'][$id2];
  162. $formData['answers'][$id1] = $content2;
  163. $formData['answers'][$id2] = $content1;
  164. }
  165. }
  166. // Moving an answer down
  167. if (isset($_POST['move_down']) && $_POST['move_down']) {
  168. foreach ($_POST['move_down'] as $key => & $value) {
  169. $id1 = $key;
  170. $content1 = $formData['answers'][$id1];
  171. $id2 = $key + 1;
  172. $content2 = $formData['answers'][$id2];
  173. $formData['answers'][$id1] = $content2;
  174. $formData['answers'][$id2] = $content1;
  175. }
  176. }
  177. /**
  178. * This solution is a little bit strange but I could not find a different solution.
  179. */
  180. if (isset($_POST['delete_answer'])) {
  181. $deleted = false;
  182. foreach ($_POST['delete_answer'] as $key => & $value) {
  183. $deleted = $key;
  184. $counter--;
  185. Session::write('answer_count', $counter);
  186. }
  187. foreach ($formData['answers'] as $key => & $value) {
  188. if ($key > $deleted) {
  189. $formData['answers'][$key - 1] = $formData['answers'][$key];
  190. unset($formData['answers'][$key]);
  191. }
  192. }
  193. }
  194. // Adding an answer
  195. if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) {
  196. $counter++;
  197. Session::write('answer_count', $counter);
  198. }
  199. // Removing an answer
  200. if (isset($_POST['buttons']) && isset($_POST['buttons']['remove_answer'])) {
  201. $counter--;
  202. Session::write('answer_count', $counter);
  203. foreach ($formData['answers'] as $index => &$data) {
  204. if ($index > $counter) {
  205. unset($formData['answers'][$index]);
  206. }
  207. }
  208. }
  209. if (!isset($_POST['delete_answer'])) {
  210. if (isset($formData['answers'])) {
  211. foreach ($formData['answers'] as $index => $data) {
  212. if ($index > $counter) {
  213. unset($formData['answers'][$index]);
  214. }
  215. }
  216. for ($i = 0; $i <= $counter; $i++) {
  217. if (!isset($formData['answers'][$i])) {
  218. $formData['answers'][$i] = '';
  219. }
  220. }
  221. }
  222. }
  223. $formData['answers'] = isset($formData['answers']) ? $formData['answers'] : [];
  224. Session::write('answer_list', $formData['answers']);
  225. return $formData;
  226. }
  227. /**
  228. * @param array $surveyData
  229. * @param array $formData
  230. *
  231. * @return mixed
  232. */
  233. public function save($surveyData, $formData)
  234. {
  235. // Saving a question
  236. if (isset($_POST['buttons']) && isset($_POST['buttons']['save'])) {
  237. Session::erase('answer_count');
  238. Session::erase('answer_list');
  239. $message = SurveyManager::save_question(
  240. $surveyData,
  241. $formData
  242. );
  243. if ($message == 'QuestionAdded' || $message == 'QuestionUpdated') {
  244. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&message='.$message.'&'.api_get_cidreq());
  245. exit;
  246. }
  247. }
  248. return $formData;
  249. }
  250. /**
  251. * Adds two buttons. One to add an option, one to remove an option
  252. *
  253. * @param array $data
  254. *
  255. */
  256. public function addRemoveButtons($data)
  257. {
  258. $this->buttonList['remove_answer'] = $this->getForm()->createElement(
  259. 'button',
  260. 'remove_answer',
  261. get_lang('RemoveAnswer'),
  262. 'minus'
  263. );
  264. if (count($data['answers']) <= 2) {
  265. $this->buttonList['remove_answer']->updateAttributes(
  266. array('disabled' => 'disabled')
  267. );
  268. }
  269. $this->buttonList['add_answer'] = $this->getForm()->createElement(
  270. 'button',
  271. 'add_answer',
  272. get_lang('AddAnswer'),
  273. 'plus'
  274. );
  275. }
  276. /**
  277. * @param FormValidator $form
  278. * @param array $questionData
  279. * @param array $answers
  280. */
  281. public function render(FormValidator $form, $questionData = array(), $answers = array())
  282. {
  283. return null;
  284. }
  285. }