session_add.php 6.1 KB

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