edit_meeting.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. $this_section = SECTION_COURSES;
  5. if (!api_is_allowed_to_edit()) {
  6. api_not_allowed(true);
  7. }
  8. if (!api_is_allowed_to_edit()) {
  9. if (!api_is_session_general_coach() ||
  10. (!empty($_GET['survey_id']) &&
  11. !api_is_element_in_the_session(TOOL_SURVEY, $_GET['survey_id']))
  12. ) {
  13. api_not_allowed(true);
  14. }
  15. }
  16. $htmlHeadXtra[] = api_get_css_asset('jt.timepicker/jquery.timepicker.css');
  17. $htmlHeadXtra[] = api_get_asset('jt.timepicker/jquery.timepicker.js');
  18. $htmlHeadXtra[] = api_get_asset('datepair.js/dist/datepair.js');
  19. $htmlHeadXtra[] = api_get_asset('datepair.js/dist/jquery.datepair.js');
  20. $interbreadcrumb[] = [
  21. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  22. 'name' => get_lang('Survey list'),
  23. ];
  24. $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : null;
  25. $surveyData = SurveyManager::get_survey($surveyId);
  26. if (empty($surveyData)) {
  27. api_not_allowed(true);
  28. }
  29. $courseInfo = api_get_course_info();
  30. $tool_name = get_lang('Edit');
  31. $form = new FormValidator(
  32. 'survey',
  33. 'post',
  34. api_get_self().'?action=edit&'.api_get_cidreq().'&survey_id='.$surveyId
  35. );
  36. $form->addElement('header', $tool_name);
  37. $form->addHidden('anonymous', 0);
  38. $form->addHidden('survey_language', $courseInfo['language']);
  39. $form->addHidden('survey_subtitle', '');
  40. $form->addHidden('survey_thanks', '');
  41. $form->addHidden('visible_results', '0');
  42. $form->addHidden('survey_type', 3);
  43. $text = $form->addText(
  44. 'survey_title',
  45. get_lang('Title')
  46. );
  47. $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
  48. if ($allowSurveyAvailabilityDatetime) {
  49. $form->addDateTimePicker('start_date', get_lang('Start Date'));
  50. $form->addDateTimePicker('end_date', get_lang('End Date'));
  51. $form->addRule('start_date', get_lang('Invalid date'), 'datetime');
  52. $form->addRule('end_date', get_lang('Invalid date'), 'datetime');
  53. } else {
  54. $form->addElement('date_picker', 'start_date', get_lang('Start Date'));
  55. $form->addElement('date_picker', 'end_date', get_lang('End Date'));
  56. $form->addRule('start_date', get_lang('Invalid date'), 'date');
  57. $form->addRule('end_date', get_lang('Invalid date'), 'date');
  58. }
  59. $form->addRule(
  60. ['start_date', 'end_date'],
  61. get_lang('Start DateShouldBeBeforeEnd Date'),
  62. 'date_compare',
  63. 'lte'
  64. );
  65. $form->addHtmlEditor('survey_introduction', get_lang('Description'), false);
  66. $form->setRequired($text);
  67. $questions = SurveyManager::get_questions($surveyData['iid']);
  68. $currentQuestionsCount = count($questions);
  69. $counter = 1;
  70. foreach ($questions as $question) {
  71. $name = 'time_'.$counter;
  72. $parts = explode('@@', $question['question']);
  73. $surveyData[$name] = api_get_local_time($parts[0]).'@@'.api_get_local_time($parts[1]);
  74. $form->addDateTimeRangePicker($name, get_lang('Date'));
  75. $form->addHidden($name.'_question_id', $question['question_id']);
  76. $counter++;
  77. }
  78. $currentQuestionsCount++;
  79. $hideList = '';
  80. $maxEvents = $currentQuestionsCount + 10;
  81. for ($i = $currentQuestionsCount; $i <= $maxEvents; $i++) {
  82. $name = 'time_'.$i;
  83. $form->addDateTimeRangePicker($name, get_lang('Date'));
  84. $hideList .= "$('#".$name."_date_time_wrapper').hide();";
  85. }
  86. $form->addHtml('<script>
  87. $(function() {
  88. '.$hideList.'
  89. var number = "'.--$currentQuestionsCount.'";
  90. $("#add_button").on("click", function() {
  91. number++;
  92. $("#time_" + number + "_date_time_wrapper").show();
  93. $("#time_" + number + "_time_range_start").val("");
  94. $("#time_" + number + "_time_range_end").val("");
  95. $("#time_" + number + "_alt").val("");
  96. });
  97. $("#remove_button").on("click", function() {
  98. if (number > 1) {
  99. console.log("#time_" + number + "_time_range_start");
  100. $("#time_" + number + "_date_time_wrapper").hide();
  101. $("#time_" + number).val("delete");
  102. $("#time_" + number + "_alt").val("delete");
  103. $("#time_" + number + "_time_range_start").val("delete");
  104. number--;
  105. }
  106. });
  107. });
  108. </script>');
  109. $form->addLabel(
  110. '',
  111. Display::url(get_lang('Add'), 'javascript:void(0)', ['id' => 'add_button', 'class' => 'btn btn-default'])
  112. .' '.
  113. Display::url(
  114. get_lang('Remove'),
  115. 'javascript:void(0)',
  116. ['id' => 'remove_button', 'class' => 'btn btn-danger']
  117. )
  118. );
  119. $form->addButtonUpdate(get_lang('Edit'), 'submit_survey');
  120. $form->setDefaults($surveyData);
  121. // The validation or display
  122. if ($form->validate()) {
  123. // Exporting the values
  124. $values = $form->getSubmitValues();
  125. $values['survey_id'] = $surveyId;
  126. $values['survey_code'] = SurveyManager::generateSurveyCode($values['survey_title']);
  127. // Storing the survey
  128. SurveyManager::store_survey($values);
  129. $dates = [];
  130. $deleteItems = [];
  131. for ($i = 1; $i <= $maxEvents; $i++) {
  132. $name = 'time_'.$i;
  133. if (isset($values[$name]) && !empty($values[$name])) {
  134. $id = '';
  135. if (isset($values[$name.'_question_id'])) {
  136. $id = $values[$name.'_question_id'];
  137. }
  138. $date = $values[$name];
  139. if ($date === 'delete' && !empty($id)) {
  140. $deleteItems[] = $id;
  141. }
  142. if (empty($date)) {
  143. continue;
  144. }
  145. $start = $name.'_time_range_start';
  146. $end = $name.'_time_range_end';
  147. $start = $values[$start];
  148. $end = $values[$end];
  149. $part = explode('@@', $values[$name]);
  150. $firstDate = substr($part[0], 0, 10);
  151. $start = api_get_utc_datetime($firstDate.' '.$start);
  152. $end = api_get_utc_datetime($firstDate.' '.$end);
  153. if (!empty($start) && !empty($start)) {
  154. $row = [
  155. 'id' => $id,
  156. 'start' => $start,
  157. 'end' => $end,
  158. ];
  159. $dates[] = $row;
  160. }
  161. }
  162. }
  163. $questionTable = Database::get_course_table(TABLE_SURVEY_QUESTION);
  164. $counter = 1;
  165. if (!empty($surveyData['iid'])) {
  166. foreach ($dates as $date) {
  167. $formattedDate = $date['start'].'@@'.$date['end'];
  168. if (!empty($date['id'])) {
  169. $questionId = $date['id'];
  170. $sql = "UPDATE $questionTable SET survey_question = '$formattedDate'
  171. WHERE iid = $questionId";
  172. Database::query($sql);
  173. } else {
  174. $params = [
  175. 'c_id' => api_get_course_int_id(),
  176. 'survey_id' => $surveyData['iid'],
  177. 'survey_question' => $formattedDate,
  178. 'survey_question_comment' => '',
  179. 'type' => 'doodle',
  180. 'display' => 'horizontal',
  181. 'sort' => $counter,
  182. 'shared_question_id' => '0',
  183. 'max_value' => 0,
  184. ];
  185. $questionId = Database::insert($questionTable, $params);
  186. if ($questionId) {
  187. $sql = "UPDATE $questionTable SET question_id = $questionId
  188. WHERE iid = $questionId";
  189. Database::query($sql);
  190. }
  191. $counter++;
  192. }
  193. }
  194. foreach ($deleteItems as $deleteId) {
  195. SurveyManager::delete_survey_question($surveyData['iid'], $deleteId);
  196. }
  197. }
  198. // Redirecting to the survey page (whilst showing the return message)
  199. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq());
  200. exit;
  201. } else {
  202. // Displaying the header
  203. Display::display_header($tool_name);
  204. $form->display();
  205. }
  206. Display::display_footer();