course_category.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. $this_section = SECTION_PLATFORM_ADMIN;
  9. api_protect_admin_script();
  10. $category = isset($_GET['category']) ? $_GET['category'] : null;
  11. $parentInfo = [];
  12. if (!empty($category)) {
  13. $parentInfo = CourseCategory::getCategory($category);
  14. }
  15. $categoryId = isset($_GET['id']) ? Security::remove_XSS($_GET['id']) : null;
  16. if (!empty($categoryId)) {
  17. $categoryInfo = CourseCategory::getCategory($categoryId);
  18. }
  19. $action = isset($_GET['action']) ? $_GET['action'] : null;
  20. $myCourseListAsCategory = api_get_configuration_value('my_courses_list_as_category');
  21. if (!empty($action)) {
  22. if ($action == 'delete') {
  23. CourseCategory::deleteNode($categoryId);
  24. Display::addFlash(Display::return_message(get_lang('Deleted')));
  25. header('Location: '.api_get_self().'?category='.Security::remove_XSS($category));
  26. exit();
  27. } elseif (($action == 'add' || $action == 'edit') && isset($_POST['formSent']) && $_POST['formSent']) {
  28. if ($action == 'add') {
  29. $ret = CourseCategory::addNode(
  30. $_POST['code'],
  31. $_POST['name'],
  32. $_POST['auth_course_child'],
  33. $categoryId
  34. );
  35. $errorMsg = Display::return_message(get_lang('Created'));
  36. } else {
  37. $ret = CourseCategory::editNode(
  38. $_POST['code'],
  39. $_POST['name'],
  40. $_POST['auth_course_child'],
  41. $categoryId
  42. );
  43. $categoryInfo = CourseCategory::getCategory($_POST['code']);
  44. $ret = $categoryInfo['id'];
  45. //Delete Picture Category
  46. $deletePicture = isset($_POST['delete_picture']) ? $_POST['delete_picture'] : '';
  47. if ($deletePicture) {
  48. CourseCategory::deletePictureCategory($ret);
  49. }
  50. $errorMsg = Display::return_message(get_lang('Update successful'));
  51. }
  52. if (!$ret) {
  53. $errorMsg = Display::return_message(get_lang('This category is already used'), 'error');
  54. } else {
  55. if ($myCourseListAsCategory) {
  56. if (isset($_FILES['image'])) {
  57. CourseCategory::saveImage($ret, $_FILES['image']);
  58. }
  59. CourseCategory::saveDescription($ret, $_POST['description']);
  60. }
  61. }
  62. Display::addFlash($errorMsg);
  63. header('Location: '.api_get_path(WEB_CODE_PATH).'admin/course_category.php');
  64. exit;
  65. } elseif ($action == 'moveUp') {
  66. CourseCategory::moveNodeUp($categoryId, $_GET['tree_pos'], $category);
  67. header('Location: '.api_get_self().'?category='.Security::remove_XSS($category));
  68. Display::addFlash(Display::return_message(get_lang('Update successful')));
  69. exit();
  70. }
  71. }
  72. $tool_name = get_lang('Courses categories');
  73. $interbreadcrumb[] = [
  74. 'url' => 'index.php',
  75. 'name' => get_lang('Administration'),
  76. ];
  77. Display::display_header($tool_name);
  78. $urlId = api_get_current_access_url_id();
  79. if ($action == 'add' || $action == 'edit') {
  80. echo '<div class="actions">';
  81. echo Display::url(
  82. Display::return_icon('folder_up.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
  83. api_get_path(WEB_CODE_PATH).'admin/course_category.php?category='.Security::remove_XSS($category)
  84. );
  85. echo '</div>';
  86. $form_title = ($action == 'add') ? get_lang('Add category') : get_lang('Edit this category');
  87. if (!empty($category)) {
  88. $form_title .= ' '.get_lang('Into').' '.Security::remove_XSS($category);
  89. }
  90. $url = api_get_self().'?action='.Security::remove_XSS($action).'&category='.Security::remove_XSS($category).'&id='.Security::remove_XSS($categoryId);
  91. $form = new FormValidator('course_category', 'post', $url);
  92. $form->addElement('header', '', $form_title);
  93. $form->addElement('hidden', 'formSent', 1);
  94. $form->addElement('text', 'code', get_lang("Category code"));
  95. if (api_get_configuration_value('save_titles_as_html')) {
  96. $form->addHtmlEditor(
  97. 'name',
  98. get_lang('Category name'),
  99. true,
  100. false,
  101. ['ToolbarSet' => 'TitleAsHtml']
  102. );
  103. } else {
  104. $form->addElement('text', 'name', get_lang("Category name"));
  105. $form->addRule('name', get_lang('Please enter a code and a name for the category'), 'required');
  106. }
  107. $form->addRule('code', get_lang('Please enter a code and a name for the category'), 'required');
  108. $group = [
  109. $form->createElement(
  110. 'radio',
  111. 'auth_course_child',
  112. get_lang('Allow adding courses in this category?'),
  113. get_lang('Yes'),
  114. 'TRUE'
  115. ),
  116. $form->createElement(
  117. 'radio',
  118. 'auth_course_child',
  119. null,
  120. get_lang('No'),
  121. 'FALSE'
  122. ),
  123. ];
  124. $form->addGroup($group, null, get_lang('Allow adding courses in this category?'));
  125. if ($myCourseListAsCategory) {
  126. $form->addHtmlEditor(
  127. 'description',
  128. get_lang('Description'),
  129. false,
  130. false,
  131. ['ToolbarSet' => 'Minimal']
  132. );
  133. $form->addFile('image', get_lang('Image'), ['id' => 'picture', 'class' => 'picture-form', 'accept' => 'image/*', 'crop_image' => true]);
  134. if ($action == 'edit' && !empty($categoryInfo['image'])) {
  135. $form->addElement('checkbox', 'delete_picture', null, get_lang('Delete picture'));
  136. $form->addHtml('
  137. <div class="form-group row">
  138. <div class="offset-md-2 col-sm-8">'.
  139. Display::img(
  140. api_get_path(WEB_UPLOAD_PATH).$categoryInfo['image'],
  141. get_lang('Image'),
  142. ['width' => 256, 'class' => 'img-thumbnail']
  143. ).'</div>
  144. </div>
  145. ');
  146. }
  147. }
  148. if (!empty($categoryInfo)) {
  149. $class = 'save';
  150. $text = get_lang('Save');
  151. $form->setDefaults($categoryInfo);
  152. $form->addButtonSave($text);
  153. } else {
  154. $class = 'add';
  155. $text = get_lang('Add category');
  156. $form->setDefaults(['auth_course_child' => 'TRUE']);
  157. $form->addButtonCreate($text);
  158. }
  159. $form->display();
  160. } else {
  161. // If multiple URLs and not main URL, prevent deletion and inform user
  162. if ($action == 'delete' && api_get_multiple_access_url() && $urlId != 1) {
  163. echo Display::return_message(get_lang('Course categories are global over multiple portals configurations. Changes are only allowed in the main administrative portal.'), 'warning');
  164. }
  165. echo '<div class="actions">';
  166. $link = null;
  167. if (!empty($parentInfo)) {
  168. $parentCode = $parentInfo['parent_id'];
  169. echo Display::url(
  170. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
  171. api_get_path(WEB_CODE_PATH).'admin/course_category.php?category='.$parentCode
  172. );
  173. }
  174. if (empty($parentInfo) || $parentInfo['auth_cat_child'] == 'TRUE') {
  175. $newCategoryLink = Display::url(
  176. Display::return_icon('new_folder.png', get_lang('Add category'), '', ICON_SIZE_MEDIUM),
  177. api_get_path(WEB_CODE_PATH).'admin/course_category.php?action=add&category='.Security::remove_XSS($category)
  178. );
  179. if (!empty($parentInfo) && $parentInfo['access_url_id'] != $urlId) {
  180. $newCategoryLink = '';
  181. }
  182. echo $newCategoryLink;
  183. }
  184. echo '</div>';
  185. if (!empty($parentInfo)) {
  186. echo Display::page_subheader($parentInfo['name'].' ('.$parentInfo['code'].')');
  187. }
  188. echo CourseCategory::listCategories($category);
  189. }
  190. Display::display_footer();