session_add.php 11 KB

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