session_add.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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[] = [
  20. 'url' => 'session_list.php',
  21. 'name' => get_lang('Session list'),
  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. $(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_div').hide();
  85. $('#date_fields').show();
  86. } else {
  87. $('#duration_div').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('Add a training session');
  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. $urlUpload = api_get_path(WEB_UPLOAD_PATH);
  113. $sysUploadPath = api_get_path(SYS_UPLOAD_PATH);
  114. $urlAjaxExtraField = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1';
  115. $htmlHeadXtra[] = "
  116. <script>
  117. $(function() {
  118. ".$result['js']."
  119. $('#system_template').on('change', function() {
  120. var sessionId = $(this).find('option:selected').val();
  121. $.ajax({
  122. type: 'GET',
  123. dataType: 'json',
  124. url: '".$url."',
  125. data: 'a=session_info&load_empty_extra_fields=true&session_id=' + sessionId,
  126. success: function(data) {
  127. if (data.session_category_id > 0) {
  128. $('#session_category').val(data.session_category_id);
  129. $('#session_category').selectpicker('render');
  130. } else {
  131. $('#session_category').val(0);
  132. $('#session_category').selectpicker('render');
  133. }
  134. CKEDITOR.instances.description.setData(data.description);
  135. if (data.duration > 0) {
  136. $('#access').val(0);
  137. $('#access').selectpicker('render');
  138. accessSwitcher(0);
  139. $('#duration').val(parseInt(data.duration));
  140. } else {
  141. $('#access').val(1);
  142. $('#access').selectpicker('render');
  143. accessSwitcher(1);
  144. var variables = [
  145. 'display_start_date',
  146. 'access_start_date',
  147. 'coach_access_start_date',
  148. 'display_end_date',
  149. 'access_end_date',
  150. 'coach_access_end_date'
  151. ];
  152. variables.forEach(function(variable) {
  153. var variableName = variable + '_to_local_time';
  154. if (data[variableName]) {
  155. var parsedDate = $.datepicker.parseDateTime(
  156. 'yy-mm-dd',
  157. 'hh:mm:ss',
  158. data[variableName]
  159. );
  160. if (parsedDate) {
  161. $('#'+variable).datetimepicker('setDate', parsedDate);
  162. }
  163. }
  164. });
  165. }
  166. $('[name=\'show_description\']').prop('checked', false);
  167. if (data.show_description) {
  168. $('[name=\'show_description\']').prop('checked', true);
  169. }
  170. $('[name=\'send_subscription_notification\']').prop('checked', false);
  171. if (data.send_subscription_notification) {
  172. $('[name=\'send_subscription_notification\']').prop('checked', true);
  173. }
  174. $.each(data.extra_fields, function(i, item) {
  175. var fieldName = 'extra_'+item.variable;
  176. /*
  177. const FIELD_TYPE_TEXT = 1;
  178. const FIELD_TYPE_TEXTAREA = 2;
  179. const FIELD_TYPE_RADIO = 3;
  180. const FIELD_TYPE_SELECT = 4;
  181. const FIELD_TYPE_SELECT_MULTIPLE = 5;
  182. const FIELD_TYPE_DATE = 6;
  183. const FIELD_TYPE_DATETIME = 7;
  184. const FIELD_TYPE_DOUBLE_SELECT = 8;
  185. const FIELD_TYPE_DIVIDER = 9;
  186. const FIELD_TYPE_TAG = 10;
  187. const FIELD_TYPE_TIMEZONE = 11;
  188. const FIELD_TYPE_SOCIAL_PROFILE = 12;
  189. const FIELD_TYPE_CHECKBOX = 13;
  190. const FIELD_TYPE_MOBILE_PHONE_NUMBER = 14;
  191. const FIELD_TYPE_INTEGER = 15;
  192. const FIELD_TYPE_FILE_IMAGE = 16;
  193. const FIELD_TYPE_FLOAT = 17;
  194. const FIELD_TYPE_FILE = 18;
  195. const FIELD_TYPE_VIDEO_URL = 19;
  196. const FIELD_TYPE_LETTERS_ONLY = 20;
  197. const FIELD_TYPE_ALPHANUMERIC = 21;
  198. const FIELD_TYPE_LETTERS_SPACE = 22;
  199. const FIELD_TYPE_ALPHANUMERIC_SPACE = 23;*/
  200. switch (item.field_type) {
  201. case '1': // text
  202. case '6': // date
  203. case '7': // datetime
  204. case '15': // integer
  205. case '17': // float
  206. case '20': // letters only
  207. case '21': // alphanum
  208. $('input[name='+fieldName+']').val(item.value);
  209. break;
  210. case '2': // textarea
  211. CKEDITOR.instances[fieldName].setData(item.value);
  212. break;
  213. case '3': // radio
  214. var radio = fieldName+'['+fieldName+']';
  215. $('[name=\''+radio+'\']').val([item.value]);
  216. break;
  217. case '4': // simple select
  218. case '5': // multiple select
  219. var options = item.value.split(';');
  220. $('#'+fieldName+'').val(options);
  221. $('#'+fieldName+'').selectpicker('render');
  222. break;
  223. case '8': // double
  224. var first = 'first_'+fieldName;
  225. var second = 'second_'+fieldName;
  226. // item.value has format : 85::86
  227. if (item.value) {
  228. var values = item.value.split('::');
  229. var firstFieldId = values[0];
  230. var secondFieldId = values[1];
  231. $('#'+first+'').val(firstFieldId);
  232. $('#'+first+'').selectpicker('render');
  233. // Remove all options
  234. $('#'+second+'')
  235. .find('option')
  236. .remove()
  237. .end();
  238. // Load items for this item then update:
  239. $.ajax({
  240. url: '".$urlAjaxExtraField."&a=get_second_select_options',
  241. dataType: 'json',
  242. data: 'type=session&field_id='+item.id+'&option_value_id='+firstFieldId,
  243. success: function(data) {
  244. $.each(data, function(index, value) {
  245. var my_select = $('#'+second+'');
  246. my_select.append($(\"<option/>\", {
  247. value: index,
  248. text: value
  249. }));
  250. });
  251. $('#'+second+'').selectpicker('refresh');
  252. }
  253. });
  254. $('#'+second+'').val(secondFieldId);
  255. $('#'+second+'').selectpicker('render');
  256. }
  257. break;
  258. case '10': // tags
  259. // Remove all options
  260. $('#'+fieldName+' option').each(function(i, optionItem) {
  261. $(this).remove();
  262. });
  263. $('#'+fieldName).next().find('.bit-box').each(function(i, optionItem) {
  264. $(this).remove();
  265. });
  266. // Add new options
  267. if (item.value) {
  268. $.each(item.value, function(i, tagItem) {
  269. // Select2 changes
  270. //console.log(tagItem.value);
  271. //$('#'+fieldName)[0].addItem(tagItem.value, tagItem.value);
  272. var option = new Option(tagItem.value, tagItem.value);
  273. option.selected = true;
  274. $('#'+fieldName).append(option);
  275. $('#'+fieldName).trigger(\"change\");
  276. });
  277. }
  278. break;
  279. case '13': // check
  280. var check = fieldName+'['+fieldName+']';
  281. // Default is uncheck
  282. $('[name=\''+check+'\']').prop('checked', false);
  283. if (item.value == 1) {
  284. $('[name=\''+check+'\']').prop('checked', true);
  285. }
  286. break;
  287. case '16':
  288. if (item.value) {
  289. // $('input[name='+fieldName+']').val(item.value);
  290. var url = '".$urlUpload."';
  291. url = url + item.value;
  292. var divFormGroup = fieldName + '-form-group';
  293. var divWrapper = fieldName + '_crop_image';
  294. var divPreview = fieldName + '_preview_image';
  295. var divCropButton = fieldName + '_crop_button';
  296. var cropResult = fieldName + '_crop_result';
  297. $('[name=\''+cropResult+'\']').val('import_file_from_session::' + sessionId);
  298. $('#' + divFormGroup).show();
  299. $('#' + divWrapper).show();
  300. $('#' + divCropButton).hide();
  301. $('#' + divPreview).attr('src', url);
  302. //$('[name=\''+fieldName+'\']')
  303. }
  304. break;
  305. }
  306. });
  307. }
  308. });
  309. })
  310. })
  311. </script>";
  312. $form->addButtonNext(get_lang('Next step'));
  313. if (!$formSent) {
  314. $formDefaults['access_start_date'] = $formDefaults['display_start_date'] = api_get_local_time();
  315. $formDefaults['coach_username'] = api_get_user_id();
  316. } else {
  317. $formDefaults['name'] = api_htmlentities($name, ENT_QUOTES, $charset);
  318. }
  319. $form->setDefaults($formDefaults);
  320. if ($form->validate()) {
  321. $params = $form->getSubmitValues();
  322. $name = $params['name'];
  323. $startDate = $params['access_start_date'];
  324. $endDate = $params['access_end_date'];
  325. $displayStartDate = $params['display_start_date'];
  326. $displayEndDate = $params['display_end_date'];
  327. $coachStartDate = $params['coach_access_start_date'];
  328. if (empty($coachStartDate)) {
  329. $coachStartDate = $displayStartDate;
  330. }
  331. $coachEndDate = $params['coach_access_end_date'];
  332. $coach_username = intval($params['coach_username']);
  333. $id_session_category = $params['session_category'];
  334. $id_visibility = $params['session_visibility'];
  335. $duration = isset($params['duration']) ? $params['duration'] : null;
  336. $description = $params['description'];
  337. $showDescription = isset($params['show_description']) ? 1 : 0;
  338. $sendSubscriptionNotification = isset($params['send_subscription_notification']);
  339. $isThisImageCropped = isset($params['picture_crop_result']);
  340. $extraFields = [];
  341. foreach ($params as $key => $value) {
  342. if (strpos($key, 'extra_') === 0) {
  343. $extraFields[$key] = $value;
  344. }
  345. }
  346. if (isset($extraFields['extra_image']) && !empty($extraFields['extra_image']['name']) && $isThisImageCropped) {
  347. $extraFields['extra_image']['crop_parameters'] = $params['picture_crop_result'];
  348. }
  349. // Check if the session image will be copied from the template
  350. $importImageFromSession = false;
  351. $sessionIdToImport = explode('::', $params['extra_image_crop_result']);
  352. $sessionIdToImport = isset($sessionIdToImport[1]) ? (int) $sessionIdToImport[1] : 0;
  353. if (!empty($sessionIdToImport)) {
  354. $extraField = new ExtraField('session');
  355. $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable('image');
  356. $extraFieldValue = new ExtraFieldValue('session');
  357. $extraFieldValueData = $extraFieldValue->get_values_by_handler_and_field_id(
  358. $sessionIdToImport,
  359. $extraFieldInfo['id']
  360. );
  361. if ($extraFieldValueData && file_exists($sysUploadPath.$extraFieldValueData['value'])) {
  362. $extraFields['extra_image']['name'] = basename($extraFieldValueData['value']);
  363. $extraFields['extra_image']['tmp_name'] = $sysUploadPath.$extraFieldValueData['value'];
  364. $extraFields['extra_image']['type'] = 'image/png';
  365. $extraFields['extra_image']['error'] = 0;
  366. $extraFields['extra_image']['size'] = filesize($sysUploadPath.$extraFieldValueData['value']);
  367. }
  368. }
  369. $return = SessionManager::create_session(
  370. $name,
  371. $startDate,
  372. $endDate,
  373. $displayStartDate,
  374. $displayEndDate,
  375. $coachStartDate,
  376. $coachEndDate,
  377. $coach_username,
  378. $id_session_category,
  379. $id_visibility,
  380. false,
  381. $duration,
  382. $description,
  383. $showDescription,
  384. $extraFields,
  385. null,
  386. $sendSubscriptionNotification
  387. );
  388. if ($return == strval(intval($return))) {
  389. // integer => no error on session creation
  390. header('Location: add_courses_to_session.php?id_session='.$return.'&add=true');
  391. exit();
  392. }
  393. }
  394. Display::display_header($tool_name);
  395. if (!empty($return)) {
  396. echo Display::return_message($return, 'error', false);
  397. }
  398. echo '<div class="actions">';
  399. echo '<a href="../session/session_list.php">'.
  400. Display::return_icon('back.png', get_lang('Back to').' '.get_lang('Administration'), '', ICON_SIZE_MEDIUM).'</a>';
  401. echo '</div>';
  402. $form->display();
  403. Display::display_footer();