session_add.php 6.1 KB

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