session_add.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // @todo add an html element
  118. $form->addButtonNext(get_lang('NextStep'));
  119. if (!$formSent) {
  120. $formDefaults['access_start_date'] = $formDefaults['display_start_date'] = api_get_local_time();
  121. $formDefaults['coach_username'] = api_get_user_id();
  122. } else {
  123. $formDefaults['name'] = api_htmlentities($name, ENT_QUOTES, $charset);
  124. }
  125. $form->setDefaults($formDefaults);
  126. if ($form->validate()) {
  127. $params = $form->getSubmitValues();
  128. $name = $params['name'];
  129. $startDate = $params['access_start_date'];
  130. $endDate = $params['access_end_date'];
  131. $displayStartDate = $params['display_start_date'];
  132. $displayEndDate = $params['display_end_date'];
  133. $coachStartDate = $params['coach_access_start_date'];
  134. if (empty($coachStartDate)) {
  135. $coachStartDate = $displayStartDate;
  136. }
  137. $coachEndDate = $params['coach_access_end_date'];
  138. $coach_username = intval($params['coach_username']);
  139. $id_session_category = $params['session_category'];
  140. $id_visibility = $params['session_visibility'];
  141. $duration = isset($params['duration']) ? $params['duration'] : null;
  142. $description = $params['description'];
  143. $showDescription = isset($params['show_description']) ? 1 : 0;
  144. $sendSubscriptionNotification = isset($params['send_subscription_notification']);
  145. $isThisImageCropped = isset($params['picture_crop_result']);
  146. $extraFields = [];
  147. foreach ($params as $key => $value) {
  148. if (strpos($key, 'extra_') === 0) {
  149. $extraFields[$key] = $value;
  150. }
  151. }
  152. if (isset($extraFields['extra_image']) && $isThisImageCropped) {
  153. $extraFields['extra_image']['crop_parameters'] = $params['picture_crop_result'];
  154. }
  155. $return = SessionManager::create_session(
  156. $name,
  157. $startDate,
  158. $endDate,
  159. $displayStartDate,
  160. $displayEndDate,
  161. $coachStartDate,
  162. $coachEndDate,
  163. $coach_username,
  164. $id_session_category,
  165. $id_visibility,
  166. false,
  167. $duration,
  168. $description,
  169. $showDescription,
  170. $extraFields,
  171. null,
  172. $sendSubscriptionNotification
  173. );
  174. if ($return == strval(intval($return))) {
  175. // integer => no error on session creation
  176. header('Location: add_courses_to_session.php?id_session='.$return.'&add=true');
  177. exit();
  178. }
  179. }
  180. Display::display_header($tool_name);
  181. if (!empty($return)) {
  182. echo Display::return_message($return, 'error', false);
  183. }
  184. echo '<div class="actions">';
  185. echo '<a href="../session/session_list.php">'.
  186. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM).'</a>';
  187. echo '</div>';
  188. $form->display();
  189. Display::display_footer();