session_add.php 6.1 KB

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