session_add.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. require_once __DIR__.'/../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. // Crop picture plugin for session images
  17. $htmlHeadXtra[] = api_get_css_asset('cropper/dist/cropper.min.css');
  18. $htmlHeadXtra[] = api_get_asset('cropper/dist/cropper.min.js');
  19. $interbreadcrumb[] = array(
  20. 'url' => 'session_list.php',
  21. 'name' => get_lang('SessionList')
  22. );
  23. function search_coachs($needle)
  24. {
  25. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  26. $xajax_response = new xajaxResponse();
  27. $return = '';
  28. if (!empty($needle)) {
  29. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
  30. // search users where username or firstname or lastname begins likes $needle
  31. $sql = 'SELECT username, lastname, firstname
  32. FROM '.$tbl_user.' user
  33. WHERE (username LIKE "'.$needle.'%"
  34. OR firstname LIKE "'.$needle.'%"
  35. OR lastname LIKE "'.$needle.'%")
  36. AND status=1'.
  37. $order_clause.
  38. ' LIMIT 10';
  39. if (api_is_multiple_url_enabled()) {
  40. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  41. $access_url_id = api_get_current_access_url_id();
  42. if ($access_url_id != -1) {
  43. $sql = 'SELECT username, lastname, firstname
  44. FROM '.$tbl_user.' user
  45. INNER JOIN '.$tbl_user_rel_access_url.' url_user
  46. ON (url_user.user_id=user.user_id)
  47. WHERE
  48. access_url_id = '.$access_url_id.' AND
  49. (
  50. username LIKE "'.$needle.'%" OR
  51. firstname LIKE "'.$needle.'%" OR
  52. lastname LIKE "'.$needle.'%"
  53. )
  54. AND status=1'.
  55. $order_clause.'
  56. LIMIT 10';
  57. }
  58. }
  59. $rs = Database::query($sql);
  60. while ($user = Database :: fetch_array($rs)) {
  61. $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 />';
  62. }
  63. }
  64. $xajax_response -> addAssign('ajax_list_coachs', 'innerHTML', api_utf8_encode($return));
  65. return $xajax_response;
  66. }
  67. $xajax->processRequests();
  68. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  69. $htmlHeadXtra[] = "
  70. <script>
  71. $(document).ready( function() {
  72. accessSwitcher(0);
  73. });
  74. function fill_coach_field (username) {
  75. document.getElementById('coach_username').value = username;
  76. document.getElementById('ajax_list_coachs').innerHTML = '';
  77. }
  78. function accessSwitcher(accessFromReady) {
  79. var access = $('#access option:selected').val();
  80. if (accessFromReady >= 0) {
  81. access = accessFromReady;
  82. }
  83. if (access == 1) {
  84. $('#duration').hide();
  85. $('#date_fields').show();
  86. } else {
  87. $('#duration').show();
  88. $('#date_fields').hide();
  89. }
  90. emptyDuration();
  91. }
  92. function emptyDuration() {
  93. if ($('#duration').val()) {
  94. $('#duration').val('');
  95. }
  96. }
  97. </script>";
  98. if (isset($_POST['formSent']) && $_POST['formSent']) {
  99. $formSent = 1;
  100. }
  101. $tool_name = get_lang('AddSession');
  102. $urlAction = api_get_self();
  103. function check_session_name($name)
  104. {
  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. $url = api_get_path(WEB_AJAX_PATH).'session.ajax.php';
  112. $urlAjaxExtraField = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1';
  113. $htmlHeadXtra[] ="
  114. <script>
  115. $(function() {
  116. ".$result['js']."
  117. $('#system_template').on('change', function() {
  118. var sessionId = $(this).find('option:selected').val();
  119. $.ajax({
  120. type: 'GET',
  121. dataType: 'json',
  122. url: '".$url."',
  123. data: 'a=session_info&load_empty_extra_fields=true&session_id=' + sessionId,
  124. success: function(data) {
  125. if (data.session_category_id > 0) {
  126. $('#session_category').val(data.session_category_id);
  127. $('#session_category').selectpicker('render');
  128. } else {
  129. $('#session_category').val(0);
  130. $('#session_category').selectpicker('render');
  131. }
  132. CKEDITOR.instances.description.setData(data.description);
  133. if (data.duration > 0) {
  134. $('#access').val(0);
  135. $('#access').selectpicker('render');
  136. $('#duration').val(data.duration);
  137. }
  138. $.each(data.extra_fields, function(i, item) {
  139. var fieldName = 'extra_'+item.variable;
  140. /*
  141. const FIELD_TYPE_TEXT = 1;
  142. const FIELD_TYPE_TEXTAREA = 2;
  143. const FIELD_TYPE_RADIO = 3;
  144. const FIELD_TYPE_SELECT = 4;
  145. const FIELD_TYPE_SELECT_MULTIPLE = 5;
  146. const FIELD_TYPE_DATE = 6;
  147. const FIELD_TYPE_DATETIME = 7;
  148. const FIELD_TYPE_DOUBLE_SELECT = 8;
  149. const FIELD_TYPE_DIVIDER = 9;
  150. const FIELD_TYPE_TAG = 10;
  151. const FIELD_TYPE_TIMEZONE = 11;
  152. const FIELD_TYPE_SOCIAL_PROFILE = 12;
  153. const FIELD_TYPE_CHECKBOX = 13;
  154. const FIELD_TYPE_MOBILE_PHONE_NUMBER = 14;
  155. const FIELD_TYPE_INTEGER = 15;
  156. const FIELD_TYPE_FILE_IMAGE = 16;
  157. const FIELD_TYPE_FLOAT = 17;
  158. const FIELD_TYPE_FILE = 18;
  159. const FIELD_TYPE_VIDEO_URL = 19;
  160. const FIELD_TYPE_LETTERS_ONLY = 20;
  161. const FIELD_TYPE_ALPHANUMERIC = 21;
  162. const FIELD_TYPE_LETTERS_SPACE = 22;
  163. const FIELD_TYPE_ALPHANUMERIC_SPACE = 23;*/
  164. switch (item.field_type) {
  165. case '1': // text
  166. case '6': // date
  167. case '7': // datetime
  168. case '15': // integer
  169. case '17': // float
  170. case '20': // letters only
  171. case '21': // alphanum
  172. $('input[name='+fieldName+']').val(item.value);
  173. break;
  174. case '2': // textarea
  175. CKEDITOR.instances[fieldName].setData(item.value);
  176. break;
  177. case '3': // radio
  178. var radio = fieldName+'['+fieldName+']';
  179. $('[name=\''+radio+'\']').val([item.value]);
  180. break;
  181. case '4': // simple select
  182. case '5': // multiple select
  183. $('#'+fieldName+'').val(item.value);
  184. $('#'+fieldName+'').selectpicker('render');
  185. break;
  186. case '8': // double
  187. var first = 'first_'+fieldName;
  188. var second = 'second_'+fieldName;
  189. // item.value has format : 85::86
  190. if (item.value) {
  191. var values = item.value.split('::');
  192. var firstFieldId = values[0];
  193. var secondFieldId = values[1];
  194. $('#'+first+'').val(firstFieldId);
  195. $('#'+first+'').selectpicker('render');
  196. // Remove all options
  197. $('#'+second+'')
  198. .find('option')
  199. .remove()
  200. .end();
  201. // Load items for this item then update:
  202. $.ajax({
  203. url: '".$urlAjaxExtraField."&a=get_second_select_options',
  204. dataType: 'json',
  205. data: 'type=session&field_id='+item.id+'&option_value_id='+firstFieldId,
  206. success: function(data) {
  207. $.each(data, function(index, value) {
  208. var my_select = $('#'+second+'');
  209. my_select.append($(\"<option/>\", {
  210. value: index,
  211. text: value
  212. }));
  213. });
  214. $('#'+second+'').selectpicker('refresh');
  215. }
  216. });
  217. $('#'+second+'').val(secondFieldId);
  218. $('#'+second+'').selectpicker('render');
  219. }
  220. break;
  221. case '10': // tags
  222. // Remove all options
  223. $('#'+fieldName+' option').each(function(i, optionItem) {
  224. $(this).remove();
  225. });
  226. $('#'+fieldName).next().find('.bit-box').each(function(i, optionItem) {
  227. $(this).remove();
  228. });
  229. // Add new options
  230. if (item.value) {
  231. $.each(item.value, function(i, tagItem) {
  232. // Select2 changes
  233. //console.log(tagItem.value);
  234. //$('#'+fieldName)[0].addItem(tagItem.value, tagItem.value);
  235. var option = new Option(tagItem.value, tagItem.value);
  236. option.selected = true;
  237. $('#'+fieldName).append(option);
  238. $('#'+fieldName).trigger(\"change\");
  239. });
  240. }
  241. break;
  242. case '13': // check
  243. var check = fieldName+'['+fieldName+']';
  244. // Default is uncheck
  245. $('[name=\''+check+'\']').prop('checked', false);
  246. if (item.value == 1) {
  247. $('[name=\''+check+'\']').prop('checked', true);
  248. }
  249. break;
  250. }
  251. });
  252. }
  253. });
  254. })
  255. })
  256. </script>";
  257. $form->addButtonNext(get_lang('NextStep'));
  258. if (!$formSent) {
  259. $formDefaults['access_start_date'] = $formDefaults['display_start_date'] = api_get_local_time();
  260. $formDefaults['coach_username'] = api_get_user_id();
  261. } else {
  262. $formDefaults['name'] = api_htmlentities($name, ENT_QUOTES, $charset);
  263. }
  264. $form->setDefaults($formDefaults);
  265. if ($form->validate()) {
  266. $params = $form->getSubmitValues();
  267. $name = $params['name'];
  268. $startDate = $params['access_start_date'];
  269. $endDate = $params['access_end_date'];
  270. $displayStartDate = $params['display_start_date'];
  271. $displayEndDate = $params['display_end_date'];
  272. $coachStartDate = $params['coach_access_start_date'];
  273. if (empty($coachStartDate)) {
  274. $coachStartDate = $displayStartDate;
  275. }
  276. $coachEndDate = $params['coach_access_end_date'];
  277. $coach_username = intval($params['coach_username']);
  278. $id_session_category = $params['session_category'];
  279. $id_visibility = $params['session_visibility'];
  280. $duration = isset($params['duration']) ? $params['duration'] : null;
  281. $description = $params['description'];
  282. $showDescription = isset($params['show_description']) ? 1 : 0;
  283. $sendSubscriptionNotification = isset($params['send_subscription_notification']);
  284. $isThisImageCropped = isset($params['picture_crop_result']);
  285. $extraFields = [];
  286. foreach ($params as $key => $value) {
  287. if (strpos($key, 'extra_') === 0) {
  288. $extraFields[$key] = $value;
  289. }
  290. }
  291. if (isset($extraFields['extra_image']) && $isThisImageCropped) {
  292. $extraFields['extra_image']['crop_parameters'] = $params['picture_crop_result'];
  293. }
  294. $return = SessionManager::create_session(
  295. $name,
  296. $startDate,
  297. $endDate,
  298. $displayStartDate,
  299. $displayEndDate,
  300. $coachStartDate,
  301. $coachEndDate,
  302. $coach_username,
  303. $id_session_category,
  304. $id_visibility,
  305. false,
  306. $duration,
  307. $description,
  308. $showDescription,
  309. $extraFields,
  310. null,
  311. $sendSubscriptionNotification
  312. );
  313. if ($return == strval(intval($return))) {
  314. // integer => no error on session creation
  315. header('Location: add_courses_to_session.php?id_session='.$return.'&add=true');
  316. exit();
  317. }
  318. }
  319. Display::display_header($tool_name);
  320. if (!empty($return)) {
  321. echo Display::return_message($return, 'error', false);
  322. }
  323. echo '<div class="actions">';
  324. echo '<a href="../session/session_list.php">'.
  325. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM).'</a>';
  326. echo '</div>';
  327. $form->display();
  328. Display::display_footer();