session_add.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. * @todo use formvalidator for the form, remove all the select harcoded values
  6. */
  7. // name of the language file that needs to be included
  8. $language_file = 'admin';
  9. $cidReset = true;
  10. // including the global Chamilo file
  11. require_once '../inc/global.inc.php';
  12. // setting the section (for the tabs)
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. SessionManager::protect_session_edit();
  15. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('Sessions'));
  16. $interbreadcrumb[] = array('url' => 'session_list.php','name' => get_lang('SessionList'));
  17. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
  18. $htmlHeadXtra[] = '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
  19. $htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui-i18n'));
  20. $htmlHeadXtra = api_get_datetime_picker_js($htmlHeadXtra);
  21. $id = null;
  22. $url_action = api_get_self();
  23. if (isset($_GET['id'])) {
  24. $id = intval($_GET['id']);
  25. $url_action = '?id='.$id;
  26. }
  27. $add_coach = null;
  28. if ($id) {
  29. $tool_name = get_lang('EditSession');
  30. SessionManager::protect_session_edit($id);
  31. $session_info = api_get_session_info($id);
  32. if (!empty($session_info['id_coach'])) {
  33. $user_info = api_get_user_info($session_info['id_coach']);
  34. $add_coach = '$("#coach_id").trigger("addItem", [{"title": "'.$user_info['complete_name'].'", "value": "'.$session_info['id_coach'].'"}]);';
  35. }
  36. $button = get_lang('Update');
  37. } else {
  38. $tool_name = get_lang('AddSession');
  39. $button = get_lang('Add');
  40. }
  41. $url = api_get_path(WEB_AJAX_PATH).'admin.ajax.php?1=1';
  42. $htmlHeadXtra[] ='
  43. <script>
  44. function check() {
  45. $("#coach_id option:selected").each(function() {
  46. var id = $(this).val();
  47. var name = $(this).text();
  48. if (id != "" ) {
  49. $.ajax({
  50. async: false,
  51. url: "'.$url.'&a=user_exists",
  52. data: "id="+id,
  53. success: function(return_value) {
  54. if (return_value == 0 ) {
  55. alert("'.get_lang('UserDoesNotExist').'");
  56. //Deleting select option tag
  57. $("#coach_id").find("option").remove();
  58. $(".holder li").each(function () {
  59. if ($(this).attr("rel") == id) {
  60. $(this).remove();
  61. }
  62. });
  63. }
  64. },
  65. });
  66. }
  67. });
  68. }
  69. $(function() {
  70. $("#coach_id").fcbkcomplete({
  71. json_url: "'.$url.'&a=find_coaches",
  72. maxitems: 1,
  73. addontab: false,
  74. input_min_size: 1,
  75. cache: false,
  76. complete_text:"'.get_lang('StartToType').'",
  77. firstselected: false,
  78. onselect: check,
  79. filter_selected: true,
  80. newel: true
  81. });
  82. '.$add_coach.'
  83. $("#display_end_date").datetimepicker({
  84. dateFormat: "yy-mm-dd",
  85. hour: 9,
  86. onSelect: function(selectedDateTime) {
  87. var start = $(this).datetimepicker("getDate");
  88. if (!$("#access_end_date").val()) {
  89. $("#access_end_date").val(selectedDateTime);
  90. }
  91. if (!$("#coach_access_end_date").val()) {
  92. $("#coach_access_end_date").val(selectedDateTime);
  93. }
  94. }
  95. });
  96. $("#display_start_date").datetimepicker({
  97. dateFormat: "yy-mm-dd",
  98. hour: 9,
  99. onSelect: function(selectedDateTime) {
  100. var start = $(this).datetimepicker("getDate");
  101. if (!$("#access_start_date").val()) {
  102. $("#access_start_date").val(selectedDateTime);
  103. }
  104. if (!$("#coach_access_start_date").val()) {
  105. $("#coach_access_start_date").val(selectedDateTime);
  106. }
  107. }
  108. });
  109. $("#access_start_date").datetimepicker({
  110. dateFormat: "yy-mm-dd",
  111. hour: 9,
  112. onSelect: function(selectedDateTime) {
  113. var start = $(this).datetimepicker("getDate");
  114. //$("#access_end_date").val(selectedDateTime);
  115. }
  116. });
  117. access_start_date_content = $("#access_end_date").val();
  118. if (access_start_date_content.length > 0) {
  119. $("#visibility_container").show();
  120. } else {
  121. $("#visibility_container").hide();
  122. }
  123. $("#access_end_date").datetimepicker({
  124. dateFormat: "yy-mm-dd",
  125. onSelect: function(selectedDateTime) {
  126. $("#visibility_container").show();
  127. }
  128. });
  129. $("#access_end_date").on("change", function() {
  130. content = $(this).val();
  131. if (content.length > 0) {
  132. $("#visibility_container").show();
  133. } else {
  134. $("#visibility_container").hide();
  135. }
  136. });
  137. $("#coach_access_start_date").datetimepicker({
  138. dateFormat: "yy-mm-dd",
  139. hour: 9,
  140. onSelect: function(selectedDateTime) {
  141. var start = $(this).datetimepicker("getDate");
  142. //$("#coach_access_end_date").val(selectedDateTime);
  143. }
  144. });
  145. $("#coach_access_end_date").datetimepicker({
  146. dateFormat: "yy-mm-dd"
  147. });
  148. });
  149. </script>';
  150. $form = new FormValidator('add_session', 'post', $url_action);
  151. $form->addElement('header', $tool_name);
  152. //Name
  153. $form->addElement('text', 'name', get_lang('SessionName'), array('class' => 'span6'));
  154. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  155. if (empty($id)) {
  156. $form->addRule('name', get_lang('SessionNameAlreadyExists'), 'callback', 'check_session_name');
  157. } else {
  158. $form->addElement('hidden', 'id', $id);
  159. }
  160. //Coaches
  161. //$coaches = SessionManager::get_user_list();
  162. if (api_is_platform_admin()) {
  163. $form->addElement('select', 'id_coach', get_lang('CoachName'), array(), array('id' => 'coach_id'));
  164. $form->addRule('id_coach', get_lang('ThisFieldIsRequired'), 'required');
  165. } else {
  166. $form->addElement('hidden', 'id_coach', api_get_user_id());
  167. }
  168. $form->addElement('advanced_settings','<a class="btn btn-show advanced_parameters" id="advanced_params" href="javascript://">'.get_lang('AdvancedParameters').'</a>');
  169. $form->addElement('html','<div id="advanced_params_options" style="display:none">');
  170. //Dates
  171. $form->addElement('text', 'display_start_date', array(get_lang('SessionDisplayStartDate'), get_lang('SessionDisplayStartDateComment')), array('id' => 'display_start_date'));
  172. $form->addElement('text', 'display_end_date', array(get_lang('SessionDisplayEndDate'), get_lang('SessionDisplayEndDateComment')), array('id' => 'display_end_date'));
  173. $form->addRule(array('display_start_date', 'display_end_date'), get_lang('StartDateMustBeBeforeTheEndDate'), 'compare_datetime_text', '< allow_empty');
  174. $form->addElement('text', 'access_start_date', array(get_lang('SessionStartDate'), get_lang('SessionStartDateComment')), array('id' => 'access_start_date'));
  175. $form->addElement('text', 'access_end_date', array(get_lang('SessionEndDate'), get_lang('SessionEndDateComment')), array('id' => 'access_end_date'));
  176. $form->addRule(array('access_start_date', 'access_end_date'), get_lang('StartDateMustBeBeforeTheEndDate'), 'compare_datetime_text', '< allow_empty');
  177. //Visibility
  178. $visibility_list = array(SESSION_VISIBLE_READ_ONLY=>get_lang('SessionReadOnly'), SESSION_VISIBLE=>get_lang('SessionAccessible'), SESSION_INVISIBLE=>api_ucfirst(get_lang('SessionNotAccessible')));
  179. $form->addElement('html','<div id="visibility_container">');
  180. $form->addElement('select', 'visibility', get_lang('SessionVisibility'), $visibility_list, array('id' => 'visibility'));
  181. $form->addElement('html','</div>');
  182. $form->addElement('text', 'coach_access_start_date', array(get_lang('SessionCoachStartDate'), get_lang('SessionCoachStartDateComment')), array('id' => 'coach_access_start_date'));
  183. $form->addElement('text', 'coach_access_end_date', array(get_lang('SessionCoachEndDate'), get_lang('SessionCoachEndDateComment')), array('id' => 'coach_access_end_date'));
  184. $form->addRule(array('coach_access_start_date', 'coach_access_end_date'), get_lang('StartDateMustBeBeforeTheEndDate'), 'compare_datetime_text', '< allow_empty');
  185. //Session categories
  186. $categories = SessionManager::get_all_session_category();
  187. $select_categories = array();
  188. if (!empty($categories)) {
  189. $select_categories = array('0' => get_lang('None'));
  190. foreach ($categories as $row) {
  191. $select_categories[$row['id']] = $row['name'];
  192. }
  193. }
  194. $form->addElement('select', 'session_category_id', get_lang('SessionCategory'), $select_categories, array('id' => 'session_category_id_name', 'class' => null));
  195. //Extra fields
  196. $extra_field = new ExtraField('session');
  197. $extra = $extra_field->addElements($form, $id);
  198. $htmlHeadXtra[] ='<script>
  199. $(function() {
  200. '.$extra['jquery_ready_content'].'
  201. });
  202. </script>';
  203. $form->addElement('html','</div>');
  204. $form->addElement('button', 'submit', $button);
  205. if (!empty($session_info)) {
  206. $session_info['display_start_date'] = api_get_local_time($session_info['display_start_date'], null, null, true);
  207. $session_info['display_end_date'] = api_get_local_time($session_info['display_end_date'], null, null, true);
  208. $session_info['access_start_date'] = api_get_local_time($session_info['access_start_date'], null, null, true);
  209. $session_info['access_end_date'] = api_get_local_time($session_info['access_end_date'], null, null, true);
  210. $session_info['coach_access_start_date'] = api_get_local_time($session_info['coach_access_start_date'], null, null, true);
  211. $session_info['coach_access_end_date'] = api_get_local_time($session_info['coach_access_end_date'], null, null, true);
  212. $form->setDefaults($session_info);
  213. }
  214. if ($form->validate()) {
  215. $params = $form->getSubmitValues();
  216. if (isset($params['id'])) {
  217. SessionManager::update($params);
  218. header('Location: resume_session.php?id_session='.$params['id']);
  219. exit;
  220. } else {
  221. $session_id = SessionManager::add($params);
  222. if ($session_id) {
  223. // integer => no error on session creation
  224. header('Location: add_courses_to_session.php?id_session='.$session_id.'&add=true&msg=');
  225. exit;
  226. }
  227. }
  228. }
  229. function check_session_name($name) {
  230. $session = SessionManager::get_session_by_name($name);
  231. return empty($session) ? true : false;
  232. }
  233. Display::display_header($tool_name);
  234. echo '<div class="actions">';
  235. echo '<a href="../admin/index.php">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'),'',ICON_SIZE_MEDIUM).'</a>';
  236. echo '</div>';
  237. $form->display();
  238. Display::display_footer();