session_edit.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Sessions edition script
  5. * @package chamilo.admin
  6. */
  7. // name of the language file that needs to be included
  8. $language_file ='admin';
  9. $cidReset = true;
  10. require_once '../inc/global.inc.php';
  11. // setting the section (for the tabs)
  12. $this_section = SECTION_PLATFORM_ADMIN;
  13. $formSent = 0;
  14. // Database Table Definitions
  15. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  16. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  17. $id = intval($_GET['id']);
  18. SessionManager::protect_session_edit($id);
  19. $infos = SessionManager::fetch($id);
  20. $id_coach = $infos['id_coach'];
  21. $tool_name = get_lang('EditSession');
  22. $interbreadcrumb[] = array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  23. $interbreadcrumb[] = array('url' => "session_list.php","name" => get_lang('SessionList'));
  24. $interbreadcrumb[] = array('url' => "resume_session.php?id_session=".$id,"name" => get_lang('SessionOverview'));
  25. list($year_start, $month_start, $day_start) = explode('-', $infos['date_start']);
  26. list($year_end, $month_end, $day_end) = explode('-', $infos['date_end']);
  27. $end_year_disabled = $end_month_disabled = $end_day_disabled = '';
  28. if (isset($_POST['formSent']) && $_POST['formSent']) {
  29. $formSent = 1;
  30. }
  31. $order_clause = 'ORDER BY ';
  32. $order_clause .= api_sort_by_first_name() ? 'firstname, lastname, username' : 'lastname, firstname, username';
  33. $sql="SELECT user_id,lastname,firstname,username FROM $tbl_user WHERE status='1'".$order_clause;
  34. if (api_is_multiple_url_enabled()) {
  35. $table_access_url_rel_user= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  36. $access_url_id = api_get_current_access_url_id();
  37. if ($access_url_id != -1) {
  38. $sql="SELECT DISTINCT u.user_id,lastname,firstname,username FROM $tbl_user u INNER JOIN $table_access_url_rel_user url_rel_user ON (url_rel_user.user_id = u.user_id)
  39. WHERE status='1' AND access_url_id = '$access_url_id' $order_clause";
  40. }
  41. }
  42. $result = Database::query($sql);
  43. $coaches = Database::store_result($result);
  44. $thisYear = date('Y');
  45. $daysOption = array();
  46. for ($i = 1; $i <= 31; $i++) {
  47. $day = sprintf("%02d", $i);
  48. $daysOption[$day] = $day;
  49. }
  50. $monthsOption = array();
  51. for ($i = 1; $i <= 12; $i++) {
  52. $month = sprintf("%02d", $i);
  53. $monthsOption[$month] = $month;
  54. }
  55. $yearsOption = array();
  56. for ($i = $thisYear - 5; $i <= ($thisYear + 5); $i++) {
  57. $yearsOption[$i] = $i;
  58. }
  59. $coachesOption = array(
  60. '' => '----- ' . get_lang('None') . ' -----'
  61. );
  62. foreach ($coaches as $coach) {
  63. $personName = api_get_person_name($coach['firstname'], $coach['lastname']);
  64. $coachesOption[$coach['user_id']] = "$personName ({$coach['username']})";
  65. }
  66. $categoriesList = SessionManager::get_all_session_category();
  67. $categoriesOption = array(
  68. '0' => get_lang('None')
  69. );
  70. if ($categoriesList != false) {
  71. foreach ($categoriesList as $categoryItem) {
  72. $categoriesOption[$categoryItem['id']] = $categoryItem['name'];
  73. }
  74. }
  75. $formAction = api_get_self() . '?';
  76. $formAction .= http_build_query(array(
  77. 'page' => Security::remove_XSS($_GET['page']),
  78. 'id' => $id
  79. ));
  80. $form = new FormValidator('edit_session', 'post', $formAction);
  81. $form->addElement('header', $tool_name);
  82. $form->addElement('text', 'name', get_lang('SessionName'), array(
  83. 'class' => 'span4',
  84. 'maxlength' => 50,
  85. 'value' => $formSent ? api_htmlentities($name,ENT_QUOTES,$charset) : ''
  86. ));
  87. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  88. $form->addRule('name', get_lang('SessionNameAlreadyExists'), 'callback', 'check_session_name');
  89. $form->addElement('select', 'id_coach', get_lang('CoachName'), $coachesOption, array(
  90. 'id' => 'coach_username',
  91. 'class' => 'chzn-select',
  92. 'style' => 'width:370px;',
  93. 'title' => get_lang('Choose')
  94. ));
  95. $form->addRule('id_coach', get_lang('ThisFieldIsRequired'), 'required');
  96. $form->addSelect('session_category', get_lang('SessionCategory'), $categoriesOption, array(
  97. 'id' => 'session_category',
  98. 'class' => 'chzn-select',
  99. 'style' => 'width:370px;'
  100. ));
  101. $form->addElement('advanced_settings','<a class="btn-show" id="show-options" href="#">'.get_lang('DefineSessionOptions').'</a>');
  102. if ($infos['nb_days_access_before_beginning'] != 0 || $infos['nb_days_access_after_end'] != 0) {
  103. $form->addElement('html','<div id="options" style="display:block;">');
  104. } else {
  105. $form->addElement('html','<div id="options" style="display:none;">');
  106. }
  107. $form->addElement('text', 'nb_days_access_before', array('', '', get_lang('DaysBefore')), array(
  108. 'style' => 'width: 30px;'
  109. ));
  110. $form->addElement('text', 'nb_days_access_after', array('', '', get_lang('DaysAfter')), array(
  111. 'style' => 'width: 30px;'
  112. ));
  113. $form->addElement('html','</div>');
  114. if ($year_start!="0000") {
  115. $form->addElement('checkbox', 'start_limit', '', get_lang('DateStartSession'), array(
  116. 'onchange' => 'disable_starttime(this)',
  117. 'id' => 'start_limit',
  118. 'checked' => ''
  119. ));
  120. $form->addElement('html','<div id="start_date" style="display:block">');
  121. } else {
  122. $form->addElement('checkbox', 'start_limit', '', get_lang('DateStartSession'), array(
  123. 'onchange' => 'disable_starttime(this)',
  124. 'id' => 'start_limit'
  125. ));
  126. $form->addElement('html','<div id="start_date" style="display:none">');
  127. }
  128. $form->addElement('date_picker', 'date_start');
  129. $form->addElement('html','</div>');
  130. if ($year_end != "0000") {
  131. $form->addElement('checkbox', 'end_limit', '', get_lang('DateEndSession'), array(
  132. 'onchange' => 'disable_endtime(this)',
  133. 'id' => 'end_limit',
  134. 'checked' => ''
  135. ));
  136. $form->addElement('html','<div id="end_date" style="display:block">');
  137. } else {
  138. $form->addElement('checkbox', 'end_limit', '', get_lang('DateEndSession'), array(
  139. 'onchange' => 'disable_endtime(this)',
  140. 'id' => 'end_limit'
  141. ));
  142. $form->addElement('html','<div id="end_date" style="display:none">');
  143. }
  144. $form->addElement('date_picker', 'date_end');
  145. $visibilityGroup = array();
  146. $visibilityGroup[] = $form->createElement('advanced_settings', get_lang('SessionVisibility'));
  147. $visibilityGroup[] = $form->createElement('select', 'session_visibility', null, array(
  148. SESSION_VISIBLE_READ_ONLY => get_lang('SessionReadOnly'),
  149. SESSION_VISIBLE => get_lang('SessionAccessible'),
  150. SESSION_INVISIBLE => api_ucfirst(get_lang('SessionNotAccessible'))
  151. ), array(
  152. 'style' => 'width:250px;'
  153. ));
  154. $form->addGroup($visibilityGroup, 'visibility_group', null, null, false);
  155. $form->addElement('html','</div>');
  156. $form->addElement(
  157. 'textarea',
  158. 'description',
  159. get_lang('Description'),
  160. array(
  161. 'class' => 'span4',
  162. 'rows' => 3
  163. )
  164. );
  165. $chkDescriptionAttributes = array();
  166. if (!empty($infos['show_description'])) {
  167. $chkDescriptionAttributes['checked'] = '';
  168. }
  169. $form->addElement('checkbox', 'show_description', null, get_lang('ShowDescription'), $chkDescriptionAttributes);
  170. $duration = empty($infos['duration']) ? null : $infos['duration'];
  171. $form->addElement(
  172. 'text',
  173. 'duration',
  174. array(
  175. get_lang('SessionDurationTitle'),
  176. get_lang('SessionDurationDescription')
  177. ),
  178. array(
  179. 'class' => 'span1',
  180. 'maxlength' => 50
  181. )
  182. );
  183. //Extra fields
  184. $extra_field = new ExtraField('session');
  185. $extra = $extra_field->addElements($form, $id);
  186. $htmlHeadXtra[] ='
  187. <script>
  188. $(function() {
  189. '.$extra['jquery_ready_content'].'
  190. });
  191. </script>';
  192. $form->addButtonUpdate(get_lang('ModifyThisSession'));
  193. $formDefaults = array(
  194. 'id_coach' => $infos['id_coach'],
  195. 'session_category' => $infos['session_category_id'],
  196. 'date_start' => $infos['date_start'],
  197. 'date_end' => $infos['date_end'],
  198. 'session_visibility' => $infos['visibility'],
  199. 'description' => $infos['description']
  200. );
  201. if ($formSent) {
  202. $formDefaults['name'] = api_htmlentities($name,ENT_QUOTES,$charset);
  203. $formDefaults['nb_days_access_before'] = api_htmlentities($nb_days_access_before,ENT_QUOTES,$charset);
  204. $formDefaults['nb_days_access_after'] = api_htmlentities($nb_days_access_after,ENT_QUOTES,$charset);
  205. $formDefaults['duration'] = Security::remove_XSS($duration);
  206. } else {
  207. $formDefaults['name'] = Security::remove_XSS($infos['name']);
  208. $formDefaults['nb_days_access_before'] = api_htmlentities($infos['nb_days_access_before_beginning'],ENT_QUOTES,$charset);
  209. $formDefaults['nb_days_access_after'] = api_htmlentities($infos['nb_days_access_after_end'],ENT_QUOTES,$charset);
  210. $formDefaults['duration'] = $duration;
  211. }
  212. $form->setDefaults($formDefaults);
  213. if ($form->validate()) {
  214. $params = $form->getSubmitValues();
  215. $name = $params['name'];
  216. $startDate = $params['date_start'];
  217. $endDate = $params['date_end'];
  218. $nb_days_acess_before = $params['nb_days_access_before'];
  219. $nb_days_acess_after = $params['nb_days_access_after'];
  220. $id_coach = $params['id_coach'];
  221. $id_session_category = $params['session_category'];
  222. $id_visibility = $params['session_visibility'];
  223. $duration = isset($params['duration']) ? $params['duration'] : null;
  224. $description = $params['description'];
  225. $showDescription = isset($params['show_description']) ? 1: 0;
  226. $end_limit = isset($params['end_limit']);
  227. $start_limit = isset($params['start_limit']);
  228. if (!$end_limit && !$start_limit) {
  229. $nolimit = 1;
  230. } else {
  231. $nolimit = null;
  232. }
  233. $extraFields = array();
  234. foreach ($params as $key => $value) {
  235. if (strpos($key, 'extra_') === 0) {
  236. $extraFields[$key] = $value;
  237. }
  238. }
  239. $return = SessionManager::edit_session(
  240. $id,
  241. $name,
  242. $startDate,
  243. $endDate,
  244. $nb_days_acess_before,
  245. $nb_days_acess_after,
  246. $nolimit,
  247. $id_coach,
  248. $id_session_category,
  249. $id_visibility,
  250. $start_limit,
  251. $end_limit,
  252. $description,
  253. $showDescription,
  254. $duration,
  255. $extraFields
  256. );
  257. if ($return == strval(intval($return))) {
  258. header('Location: resume_session.php?id_session=' . $return);
  259. exit();
  260. }
  261. }
  262. // display the header
  263. Display::display_header($tool_name);
  264. if (!empty($return)) {
  265. Display::display_error_message($return,false);
  266. }
  267. $form->display();
  268. ?>
  269. <script type="text/javascript">
  270. <?php
  271. //if($year_start=="0000") echo "setDisable(document.form.nolimit);\r\n";
  272. ?>
  273. function setDisable(select) {
  274. document.forms['edit_session'].elements['session_visibility'].disabled = (select.checked) ? true : false;
  275. document.forms['edit_session'].elements['session_visibility'].selectedIndex = 0;
  276. document.forms['edit_session'].elements['start_limit'].disabled = (select.checked) ? true : false;
  277. document.forms['edit_session'].elements['start_limit'].checked = false;
  278. document.forms['edit_session'].elements['end_limit'].disabled = (select.checked) ? true : false;
  279. document.forms['edit_session'].elements['end_limit'].checked = false;
  280. var end_div = document.getElementById('end_date');
  281. end_div.style.display = 'none';
  282. var start_div = document.getElementById('start_date');
  283. start_div.style.display = 'none';
  284. }
  285. function disable_endtime(select) {
  286. var end_div = document.getElementById('end_date');
  287. if (end_div.style.display == 'none')
  288. end_div.style.display = 'block';
  289. else
  290. end_div.style.display = 'none';
  291. emptyDuration();
  292. }
  293. function disable_starttime(select) {
  294. var start_div = document.getElementById('start_date');
  295. if (start_div.style.display == 'none')
  296. start_div.style.display = 'block';
  297. else
  298. start_div.style.display = 'none';
  299. emptyDuration();
  300. }
  301. function emptyDuration() {
  302. if ($('#duration').val()) {
  303. $('#duration').val('');
  304. }
  305. }
  306. $(document).on('ready', function (){
  307. $('#show-options').on('click', function (e) {
  308. e.preventDefault();
  309. var display = $('#options').css('display');
  310. display === 'block' ? $('#options').slideUp() : $('#options').slideDown() ;
  311. });
  312. });
  313. </script>
  314. <?php
  315. Display::display_footer();