survey_question.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. $this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true);
  98. } else {
  99. $this->buttonList[] = $this->getForm()->addButtonSave(get_lang('CreateQuestionSurvey'), 'save', true);
  100. }
  101. $this->getForm()->addGroup($this->buttonList, 'buttons');
  102. }
  103. /**
  104. * @return FormValidator
  105. */
  106. public function getForm()
  107. {
  108. return $this->form;
  109. }
  110. /**
  111. * @param FormValidator $form
  112. */
  113. public function setForm($form)
  114. {
  115. $this->form = $form;
  116. }
  117. /**
  118. * @param array $formData
  119. *
  120. * @return mixed
  121. */
  122. public function preSave($formData)
  123. {
  124. $counter = Session::read('answer_count');
  125. $answerList = Session::read('answer_list');
  126. if (empty($answerList)) {
  127. $answerList = isset($formData['answers']) ? $formData['answers'] : array();
  128. Session::write('answer_list', $answerList);
  129. }
  130. if (isset($_POST['answers'])) {
  131. $formData['answers'] = $_POST['answers'];
  132. }
  133. if (empty($counter)) {
  134. $counter = count($answerList) - 1;
  135. Session::write('answer_count', $counter);
  136. }
  137. // Moving an answer up
  138. if (isset($_POST['move_up']) && $_POST['move_up']) {
  139. foreach ($_POST['move_up'] as $key => & $value) {
  140. $id1 = $key;
  141. $content1 = $formData['answers'][$id1];
  142. $id2 = $key - 1;
  143. $content2 = $formData['answers'][$id2];
  144. $formData['answers'][$id1] = $content2;
  145. $formData['answers'][$id2] = $content1;
  146. }
  147. }
  148. // Moving an answer down
  149. if (isset($_POST['move_down']) && $_POST['move_down']) {
  150. foreach ($_POST['move_down'] as $key => & $value) {
  151. $id1 = $key;
  152. $content1 = $formData['answers'][$id1];
  153. $id2 = $key + 1;
  154. $content2 = $formData['answers'][$id2];
  155. $formData['answers'][$id1] = $content2;
  156. $formData['answers'][$id2] = $content1;
  157. }
  158. }
  159. /**
  160. * This solution is a little bit strange but I could not find a different solution.
  161. */
  162. if (isset($_POST['delete_answer'])) {
  163. $deleted = false;
  164. foreach ($_POST['delete_answer'] as $key => & $value) {
  165. $deleted = $key;
  166. $counter--;
  167. Session::write('answer_count', $counter);
  168. }
  169. foreach ($formData['answers'] as $key => & $value) {
  170. if ($key > $deleted) {
  171. $formData['answers'][$key - 1] = $formData['answers'][$key];
  172. unset($formData['answers'][$key]);
  173. }
  174. }
  175. }
  176. // Adding an answer
  177. if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) {
  178. $counter++;
  179. Session::write('answer_count', $counter);
  180. }
  181. // Removing an answer
  182. if (isset($_POST['buttons']) && isset($_POST['buttons']['remove_answer'])) {
  183. $counter--;
  184. Session::write('answer_count', $counter);
  185. foreach ($formData['answers'] as $index => &$data) {
  186. if ($index > $counter) {
  187. unset($formData['answers'][$index]);
  188. }
  189. }
  190. }
  191. if (!isset($_POST['delete_answer'])) {
  192. if (isset($formData['answers'])) {
  193. foreach ($formData['answers'] as $index => $data) {
  194. if ($index > $counter) {
  195. unset($formData['answers'][$index]);
  196. }
  197. }
  198. for ($i = 0; $i <= $counter; $i++) {
  199. if (!isset($formData['answers'][$i])) {
  200. $formData['answers'][$i] = '';
  201. }
  202. }
  203. }
  204. }
  205. $formData['answers'] = isset($formData['answers']) ? $formData['answers'] : [];
  206. Session::write('answer_list', $formData['answers']);
  207. return $formData;
  208. }
  209. /**
  210. * @param array $surveyData
  211. * @param array $formData
  212. *
  213. * @return mixed
  214. */
  215. public function save($surveyData, $formData)
  216. {
  217. // Saving a question
  218. if (isset($_POST['buttons']) && isset($_POST['buttons']['save'])) {
  219. Session::erase('answer_count');
  220. Session::erase('answer_list');
  221. $message = SurveyManager::save_question(
  222. $surveyData,
  223. $formData
  224. );
  225. if ($message == 'QuestionAdded' || $message == 'QuestionUpdated') {
  226. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&message='.$message.'&'.api_get_cidreq());
  227. exit;
  228. }
  229. }
  230. return $formData;
  231. }
  232. /**
  233. * Adds two buttons. One to add an option, one to remove an option
  234. *
  235. * @param array $data
  236. *
  237. */
  238. public function addRemoveButtons($data)
  239. {
  240. $this->buttonList['remove_answer'] = $this->getForm()->createElement(
  241. 'button',
  242. 'remove_answer',
  243. get_lang('RemoveAnswer'),
  244. 'minus'
  245. );
  246. if (count($data['answers']) <= 2) {
  247. $this->buttonList['remove_answer']->updateAttributes(
  248. array('disabled' => 'disabled')
  249. );
  250. }
  251. $this->buttonList['add_answer'] = $this->getForm()->createElement(
  252. 'button',
  253. 'add_answer',
  254. get_lang('AddAnswer'),
  255. 'plus'
  256. );
  257. }
  258. /**
  259. * @param FormValidator $form
  260. * @param array $questionData
  261. * @param array $answers
  262. */
  263. public function render(FormValidator $form, $questionData = array(), $answers = array())
  264. {
  265. return null;
  266. }
  267. }