session_edit.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Sessions edition script.
  5. *
  6. * @package chamilo.admin
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. // setting the section (for the tabs)
  11. $this_section = SECTION_PLATFORM_ADMIN;
  12. $formSent = 0;
  13. // Crop picture plugin for session images
  14. //$htmlHeadXtra[] = api_get_css_asset('cropper/dist/cropper.min.css');
  15. //$htmlHeadXtra[] = api_get_asset('cropper/dist/cropper.min.js');
  16. // Database Table Definitions
  17. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  18. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  19. $id = (int) $_GET['id'];
  20. SessionManager::protectSession($id);
  21. $sessionInfo = SessionManager::fetch($id);
  22. // Sets to local time to show it correctly when you edit a session
  23. if (!empty($sessionInfo['display_start_date'])) {
  24. $sessionInfo['display_start_date'] = api_get_local_time($sessionInfo['display_start_date']);
  25. }
  26. if (!empty($sessionInfo['display_end_date'])) {
  27. $sessionInfo['display_end_date'] = api_get_local_time($sessionInfo['display_end_date']);
  28. }
  29. if (!empty($sessionInfo['access_start_date'])) {
  30. $sessionInfo['access_start_date'] = api_get_local_time($sessionInfo['access_start_date']);
  31. }
  32. if (!empty($sessionInfo['access_end_date'])) {
  33. $sessionInfo['access_end_date'] = api_get_local_time($sessionInfo['access_end_date']);
  34. }
  35. if (!empty($sessionInfo['coach_access_start_date'])) {
  36. $sessionInfo['coach_access_start_date'] = api_get_local_time($sessionInfo['coach_access_start_date']);
  37. }
  38. if (!empty($sessionInfo['coach_access_end_date'])) {
  39. $sessionInfo['coach_access_end_date'] = api_get_local_time($sessionInfo['coach_access_end_date']);
  40. }
  41. $tool_name = get_lang('Edit this session');
  42. $interbreadcrumb[] = ['url' => 'session_list.php', 'name' => get_lang('Session list')];
  43. $interbreadcrumb[] = ['url' => 'resume_session.php?id_session='.$id, 'name' => get_lang('Session overview')];
  44. if (isset($_POST['formSent']) && $_POST['formSent']) {
  45. $formSent = 1;
  46. }
  47. $order_clause = 'ORDER BY ';
  48. $order_clause .= api_sort_by_first_name() ? 'firstname, lastname, username' : 'lastname, firstname, username';
  49. $sql = "SELECT user_id,lastname,firstname,username
  50. FROM $tbl_user
  51. WHERE status='1'".$order_clause;
  52. if (api_is_multiple_url_enabled()) {
  53. $table_access_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  54. $access_url_id = api_get_current_access_url_id();
  55. if ($access_url_id != -1) {
  56. $sql = "SELECT DISTINCT u.user_id,lastname,firstname,username
  57. FROM $tbl_user u
  58. INNER JOIN $table_access_url_rel_user url_rel_user
  59. ON (url_rel_user.user_id = u.user_id)
  60. WHERE status='1' AND access_url_id = '$access_url_id' $order_clause";
  61. }
  62. }
  63. $result = Database::query($sql);
  64. $coaches = Database::store_result($result);
  65. $coachesOption = [
  66. '' => '----- '.get_lang('none').' -----',
  67. ];
  68. foreach ($coaches as $coach) {
  69. $personName = api_get_person_name($coach['firstname'], $coach['lastname']);
  70. $coachesOption[$coach['user_id']] = "$personName ({$coach['username']})";
  71. }
  72. $categoriesList = SessionManager::get_all_session_category();
  73. $categoriesOption = [
  74. '0' => get_lang('none'),
  75. ];
  76. if ($categoriesList != false) {
  77. foreach ($categoriesList as $categoryItem) {
  78. $categoriesOption[$categoryItem['id']] = $categoryItem['name'];
  79. }
  80. }
  81. $formAction = api_get_self().'?';
  82. $formAction .= http_build_query([
  83. 'page' => Security::remove_XSS($_GET['page']),
  84. 'id' => $id,
  85. ]);
  86. $form = new FormValidator('edit_session', 'post', $formAction);
  87. $form->addElement('header', $tool_name);
  88. $result = SessionManager::setForm($form, $sessionInfo);
  89. $htmlHeadXtra[] = '
  90. <script>
  91. $(function() {
  92. '.$result['js'].'
  93. });
  94. </script>';
  95. $form->addButtonUpdate(get_lang('Edit this session'));
  96. $formDefaults = $sessionInfo;
  97. $formDefaults['coach_username'] = $sessionInfo['id_coach'];
  98. $formDefaults['session_category'] = $sessionInfo['session_category_id'];
  99. $formDefaults['session_visibility'] = $sessionInfo['visibility'];
  100. if ($formSent) {
  101. $formDefaults['name'] = api_htmlentities($name, ENT_QUOTES, $charset);
  102. } else {
  103. $formDefaults['name'] = Security::remove_XSS($sessionInfo['name']);
  104. }
  105. $form->setDefaults($formDefaults);
  106. if ($form->validate()) {
  107. $params = $form->getSubmitValues();
  108. $name = $params['name'];
  109. $startDate = $params['access_start_date'];
  110. $endDate = $params['access_end_date'];
  111. $displayStartDate = $params['display_start_date'];
  112. $displayEndDate = $params['display_end_date'];
  113. $coachStartDate = $params['coach_access_start_date'];
  114. $coachEndDate = $params['coach_access_end_date'];
  115. $coach_username = intval($params['coach_username']);
  116. $id_session_category = $params['session_category'];
  117. $id_visibility = $params['session_visibility'];
  118. $duration = isset($params['duration']) ? $params['duration'] : null;
  119. if ($params['access'] == 1) {
  120. $duration = null;
  121. }
  122. $description = $params['description'];
  123. $showDescription = isset($params['show_description']) ? 1 : 0;
  124. $sendSubscriptionNotification = isset($params['send_subscription_notification']);
  125. $isThisImageCropped = isset($params['picture_crop_result']);
  126. $extraFields = [];
  127. foreach ($params as $key => $value) {
  128. if (strpos($key, 'extra_') === 0) {
  129. $extraFields[$key] = $value;
  130. }
  131. }
  132. if (isset($extraFields['extra_image']) && $isThisImageCropped) {
  133. $extraFields['extra_image']['crop_parameters'] = $params['picture_crop_result'];
  134. }
  135. $return = SessionManager::edit_session(
  136. $id,
  137. $name,
  138. $startDate,
  139. $endDate,
  140. $displayStartDate,
  141. $displayEndDate,
  142. $coachStartDate,
  143. $coachEndDate,
  144. $coach_username,
  145. $id_session_category,
  146. $id_visibility,
  147. $description,
  148. $showDescription,
  149. $duration,
  150. $extraFields,
  151. null,
  152. $sendSubscriptionNotification
  153. );
  154. if ($return) {
  155. Display::addFlash(Display::return_message(get_lang('Update successful')));
  156. header('Location: resume_session.php?id_session='.$return);
  157. exit();
  158. }
  159. }
  160. // display the header
  161. Display::display_header($tool_name);
  162. $form->display();
  163. ?>
  164. <script>
  165. $(function() {
  166. <?php
  167. if (!empty($sessionInfo['duration'])) {
  168. echo 'accessSwitcher(0);';
  169. } else {
  170. echo 'accessSwitcher(1);';
  171. }
  172. ?>
  173. });
  174. function accessSwitcher(accessFromReady) {
  175. var access = $('#access option:selected').val();
  176. if (accessFromReady >= 0) {
  177. access = accessFromReady;
  178. $('[name=access]').val(access);
  179. }
  180. if (access == 1) {
  181. $('#duration_div').hide();
  182. $('#date_fields').show();
  183. emptyDuration();
  184. } else {
  185. $('#duration_div').show();
  186. $('#date_fields').hide();
  187. }
  188. }
  189. function emptyDuration() {
  190. if ($('#duration').val()) {
  191. $('#duration').val('');
  192. }
  193. }
  194. $(function() {
  195. $('#show-options').on('click', function (e) {
  196. e.preventDefault();
  197. var display = $('#options').css('display');
  198. display === 'block' ? $('#options').slideUp() : $('#options').slideDown() ;
  199. });
  200. });
  201. </script>
  202. <?php
  203. Display::display_footer();