session_add.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. require_once __DIR__.'/../inc/global.inc.php';
  8. $xajax = new xajax();
  9. $xajax->registerFunction('search_coachs');
  10. // setting the section (for the tabs)
  11. $this_section = SECTION_PLATFORM_ADMIN;
  12. SessionManager::protectSession(null, false);
  13. api_protect_limit_for_session_admin();
  14. $formSent = 0;
  15. $errorMsg = '';
  16. // Crop picture plugin for session images
  17. $htmlHeadXtra[] = api_get_css_asset('cropper/dist/cropper.min.css');
  18. $htmlHeadXtra[] = api_get_asset('cropper/dist/cropper.min.js');
  19. $interbreadcrumb[] = array(
  20. 'url' => 'session_list.php',
  21. 'name' => get_lang('SessionList')
  22. );
  23. function search_coachs($needle)
  24. {
  25. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  26. $xajax_response = new xajaxResponse();
  27. $return = '';
  28. if (!empty($needle)) {
  29. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
  30. // search users where username or firstname or lastname begins likes $needle
  31. $sql = 'SELECT username, lastname, firstname
  32. FROM '.$tbl_user.' user
  33. WHERE (username LIKE "'.$needle.'%"
  34. OR firstname LIKE "'.$needle.'%"
  35. OR lastname LIKE "'.$needle.'%")
  36. AND status=1'.
  37. $order_clause.
  38. ' LIMIT 10';
  39. if (api_is_multiple_url_enabled()) {
  40. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  41. $access_url_id = api_get_current_access_url_id();
  42. if ($access_url_id != -1) {
  43. $sql = 'SELECT username, lastname, firstname
  44. FROM '.$tbl_user.' user
  45. INNER JOIN '.$tbl_user_rel_access_url.' url_user
  46. ON (url_user.user_id=user.user_id)
  47. WHERE
  48. access_url_id = '.$access_url_id.' AND
  49. (
  50. username LIKE "'.$needle.'%" OR
  51. firstname LIKE "'.$needle.'%" OR
  52. lastname LIKE "'.$needle.'%"
  53. )
  54. AND status=1'.
  55. $order_clause.'
  56. LIMIT 10';
  57. }
  58. }
  59. $rs = Database::query($sql);
  60. while ($user = Database :: fetch_array($rs)) {
  61. $return .= '<a href="javascript: void(0);" onclick="javascript: fill_coach_field(\''.$user['username'].'\')">'.api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')</a><br />';
  62. }
  63. }
  64. $xajax_response -> addAssign('ajax_list_coachs', 'innerHTML', api_utf8_encode($return));
  65. return $xajax_response;
  66. }
  67. $xajax -> processRequests();
  68. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  69. $htmlHeadXtra[] = "
  70. <script>
  71. $(document).ready( function() {
  72. accessSwitcher(0);
  73. });
  74. function fill_coach_field (username) {
  75. document.getElementById('coach_username').value = username;
  76. document.getElementById('ajax_list_coachs').innerHTML = '';
  77. }
  78. function accessSwitcher(accessFromReady) {
  79. var access = $('#access option:selected').val();
  80. if (accessFromReady >= 0) {
  81. access = accessFromReady;
  82. }
  83. if (access == 1) {
  84. $('#duration').hide();
  85. $('#date_fields').show();
  86. } else {
  87. $('#duration').show();
  88. $('#date_fields').hide();
  89. }
  90. emptyDuration();
  91. }
  92. function emptyDuration() {
  93. if ($('#duration').val()) {
  94. $('#duration').val('');
  95. }
  96. }
  97. </script>";
  98. if (isset($_POST['formSent']) && $_POST['formSent']) {
  99. $formSent = 1;
  100. }
  101. $tool_name = get_lang('AddSession');
  102. $urlAction = api_get_self();
  103. function check_session_name($name)
  104. {
  105. $session = SessionManager::get_session_by_name($name);
  106. return empty($session) ? true : false;
  107. }
  108. $form = new FormValidator('add_session', 'post', $urlAction);
  109. $form->addElement('header', $tool_name);
  110. $result = SessionManager::setForm($form);
  111. $htmlHeadXtra[] = '
  112. <script>
  113. $(function() {
  114. '.$result['js'].'
  115. });
  116. </script>';
  117. $form->addButtonNext(get_lang('NextStep'));
  118. if (!$formSent) {
  119. $formDefaults['access_start_date'] = $formDefaults['display_start_date'] = api_get_local_time();
  120. $formDefaults['coach_username'] = api_get_user_id();
  121. } else {
  122. $formDefaults['name'] = api_htmlentities($name, ENT_QUOTES, $charset);
  123. }
  124. $form->setDefaults($formDefaults);
  125. if ($form->validate()) {
  126. $params = $form->getSubmitValues();
  127. $name = $params['name'];
  128. $startDate = $params['access_start_date'];
  129. $endDate = $params['access_end_date'];
  130. $displayStartDate = $params['display_start_date'];
  131. $displayEndDate = $params['display_end_date'];
  132. $coachStartDate = $params['coach_access_start_date'];
  133. if (empty($coachStartDate)) {
  134. $coachStartDate = $displayStartDate;
  135. }
  136. $coachEndDate = $params['coach_access_end_date'];
  137. $coach_username = intval($params['coach_username']);
  138. $id_session_category = $params['session_category'];
  139. $id_visibility = $params['session_visibility'];
  140. $duration = isset($params['duration']) ? $params['duration'] : null;
  141. $description = $params['description'];
  142. $showDescription = isset($params['show_description']) ? 1 : 0;
  143. $sendSubscriptionNotification = isset($params['send_subscription_notification']);
  144. $isThisImageCropped = isset($params['picture_crop_result']);
  145. $extraFields = [];
  146. foreach ($params as $key => $value) {
  147. if (strpos($key, 'extra_') === 0) {
  148. $extraFields[$key] = $value;
  149. }
  150. }
  151. if (isset($extraFields['extra_image']) && $isThisImageCropped) {
  152. $extraFields['extra_image']['crop_parameters'] = $params['picture_crop_result'];
  153. }
  154. $return = SessionManager::create_session(
  155. $name,
  156. $startDate,
  157. $endDate,
  158. $displayStartDate,
  159. $displayEndDate,
  160. $coachStartDate,
  161. $coachEndDate,
  162. $coach_username,
  163. $id_session_category,
  164. $id_visibility,
  165. false,
  166. $duration,
  167. $description,
  168. $showDescription,
  169. $extraFields,
  170. null,
  171. $sendSubscriptionNotification
  172. );
  173. if ($return == strval(intval($return))) {
  174. // integer => no error on session creation
  175. header('Location: add_courses_to_session.php?id_session='.$return.'&add=true&msg=');
  176. exit();
  177. }
  178. }
  179. Display::display_header($tool_name);
  180. if (!empty($return)) {
  181. echo Display::return_message($return, 'error', false);
  182. }
  183. echo '<div class="actions">';
  184. echo '<a href="../session/session_list.php">'.
  185. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM).'</a>';
  186. echo '</div>';
  187. $form->display();
  188. Display::display_footer();