create_meeting.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $htmlHeadXtra[] = api_get_css_asset('jt.timepicker/jquery.timepicker.css');
  9. $htmlHeadXtra[] = api_get_asset('jt.timepicker/jquery.timepicker.js');
  10. $htmlHeadXtra[] = api_get_asset('datepair.js/dist/datepair.js');
  11. $htmlHeadXtra[] = api_get_asset('datepair.js/dist/jquery.datepair.js');
  12. $interbreadcrumb[] = [
  13. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  14. 'name' => get_lang('Survey list'),
  15. ];
  16. $courseInfo = api_get_course_info();
  17. $tool_name = get_lang('Create meeting poll');
  18. $form = new FormValidator(
  19. 'survey',
  20. 'post',
  21. api_get_self().'?action=add&'.api_get_cidreq()
  22. );
  23. $form->addElement('header', $tool_name);
  24. $form->addHidden('anonymous', 0);
  25. $form->addHidden('survey_language', $courseInfo['language']);
  26. $form->addHidden('survey_subtitle', '');
  27. $form->addHidden('survey_thanks', '');
  28. $form->addHidden('visible_results', '0');
  29. $form->addHidden('survey_type', 3);
  30. $text = $form->addText(
  31. 'survey_title',
  32. get_lang('Title')
  33. );
  34. $allowSurveyAvailabilityDatetime = api_get_configuration_value('allow_survey_availability_datetime');
  35. if ($allowSurveyAvailabilityDatetime) {
  36. $startDateElement = $form->addDateTimePicker('start_date', get_lang('Start Date'));
  37. $endDateElement = $form->addDateTimePicker('end_date', get_lang('End Date'));
  38. $form->addRule('start_date', get_lang('Invalid date'), 'datetime');
  39. $form->addRule('end_date', get_lang('Invalid date'), 'datetime');
  40. } else {
  41. $startDateElement = $form->addElement('date_picker', 'start_date', get_lang('Start Date'));
  42. $endDateElement = $form->addElement('date_picker', 'end_date', get_lang('End Date'));
  43. $form->addRule('start_date', get_lang('Invalid date'), 'date');
  44. $form->addRule('end_date', get_lang('Invalid date'), 'date');
  45. }
  46. $form->addRule(
  47. ['start_date', 'end_date'],
  48. get_lang('Start DateShouldBeBeforeEnd Date'),
  49. 'date_compare',
  50. 'lte'
  51. );
  52. $form->setRequired($startDateElement);
  53. $form->setRequired($endDateElement);
  54. $form->addHtmlEditor('survey_introduction', get_lang('Description'), false);
  55. $form->setRequired($text);
  56. $hideList = '';
  57. $maxEvents = 20;
  58. for ($i = 1; $i <= $maxEvents; $i++) {
  59. $name = 'time_'.$i;
  60. $form->addDateTimeRangePicker($name, get_lang('Date'));
  61. if ($i > 3) {
  62. $hideList .= "$('#".$name."_date_time_wrapper').hide();";
  63. }
  64. }
  65. $form->addHtml('<script>
  66. $(function() {
  67. '.$hideList.'
  68. var number = 3;
  69. $("#add_button").on("click", function() {
  70. number++;
  71. $("#time_" + number + "_date_time_wrapper").show();
  72. });
  73. $("#remove_button").on("click", function() {
  74. if (number > 1) {
  75. $("#time_" + number + "_date_time_wrapper").hide();
  76. number--;
  77. }
  78. });
  79. });
  80. </script>');
  81. $form->addLabel(
  82. '',
  83. Display::url(get_lang('Add'), 'javascript:void(0)', ['id' => 'add_button', 'class' => 'btn btn-default'])
  84. .' '.
  85. Display::url(
  86. get_lang('Remove'),
  87. 'javascript:void(0)',
  88. ['id' => 'remove_button', 'class' => 'btn btn-danger']
  89. )
  90. );
  91. $form->addButtonCreate(get_lang('Create survey'), 'submit_survey');
  92. $defaults = [];
  93. $form->setDefaults($defaults);
  94. // The validation or display
  95. if ($form->validate()) {
  96. // Exporting the values
  97. $values = $form->getSubmitValues();
  98. $values['survey_code'] = SurveyManager::generateSurveyCode($values['survey_title']);
  99. // Storing the survey
  100. $surveyData = SurveyManager::store_survey($values);
  101. $dates = [];
  102. for ($i = 1; $i <= $maxEvents; $i++) {
  103. $name = 'time_'.$i;
  104. if (isset($values[$name]) && !empty($values[$name])) {
  105. $date = $values[$name];
  106. if (empty($date)) {
  107. continue;
  108. }
  109. $start = $name.'_time_range_start';
  110. $end = $name.'_time_range_end';
  111. $start = $values[$start];
  112. $end = $values[$end];
  113. $start = api_get_utc_datetime($values[$name].' '.$start, true);
  114. $end = api_get_utc_datetime($values[$name].' '.$end, true);
  115. if (!empty($start) && !empty($start)) {
  116. $row = [
  117. 'start' => $start,
  118. 'end' => $end,
  119. ];
  120. $dates[] = $row;
  121. }
  122. }
  123. }
  124. $questionTable = Database::get_course_table(TABLE_SURVEY_QUESTION);
  125. $counter = 1;
  126. if (!empty($surveyData['id'])) {
  127. foreach ($dates as $date) {
  128. $params = [
  129. 'c_id' => api_get_course_int_id(),
  130. 'survey_id' => $surveyData['id'],
  131. 'survey_question' => $date['start'].'@@'.$date['end'],
  132. 'survey_question_comment' => '',
  133. 'type' => 'doodle',
  134. 'display' => 'horizontal',
  135. 'sort' => $counter,
  136. 'shared_question_id' => '0',
  137. 'max_value' => 0,
  138. ];
  139. $questionId = Database::insert($questionTable, $params);
  140. if ($questionId) {
  141. $sql = "UPDATE $questionTable SET question_id = $questionId
  142. WHERE iid = $questionId";
  143. Database::query($sql);
  144. }
  145. $counter++;
  146. }
  147. }
  148. // Redirecting to the survey page (whilst showing the return message)
  149. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq());
  150. exit;
  151. } else {
  152. // Displaying the header
  153. Display::display_header($tool_name);
  154. $form->display();
  155. }
  156. Display::display_footer();