course_category.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. $category
  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. $errorMsg = Display::return_message(get_lang('Updated'));
  46. }
  47. if (!$ret) {
  48. $errorMsg = Display::return_message(get_lang('CatCodeAlreadyUsed'), 'error');
  49. } else {
  50. if ($myCourseListAsCategory) {
  51. if (isset($_FILES['image'])) {
  52. CourseCategory::saveImage($ret, $_FILES['image']);
  53. }
  54. CourseCategory::saveDescription($ret, $_POST['description']);
  55. }
  56. }
  57. Display::addFlash($errorMsg);
  58. header('Location: '.api_get_path(WEB_CODE_PATH).'admin/course_category.php');
  59. exit;
  60. } elseif ($action == 'moveUp') {
  61. CourseCategory::moveNodeUp($categoryId, $_GET['tree_pos'], $category);
  62. header('Location: '.api_get_self().'?category='.Security::remove_XSS($category));
  63. Display::addFlash(Display::return_message(get_lang('Updated')));
  64. exit();
  65. }
  66. }
  67. $tool_name = get_lang('AdminCategories');
  68. $interbreadcrumb[] = [
  69. 'url' => 'index.php',
  70. 'name' => get_lang('PlatformAdmin'),
  71. ];
  72. Display::display_header($tool_name);
  73. $urlId = api_get_current_access_url_id();
  74. if ($action == 'add' || $action == 'edit') {
  75. echo '<div class="actions">';
  76. echo Display::url(
  77. Display::return_icon('folder_up.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
  78. api_get_path(WEB_CODE_PATH).'admin/course_category.php?category='.Security::remove_XSS($category)
  79. );
  80. echo '</div>';
  81. $form_title = ($action == 'add') ? get_lang('AddACategory') : get_lang('EditNode');
  82. if (!empty($category)) {
  83. $form_title .= ' '.get_lang('Into').' '.Security::remove_XSS($category);
  84. }
  85. $url = api_get_self().'?action='.Security::remove_XSS($action).'&category='.Security::remove_XSS($category).'&id='.Security::remove_XSS($categoryId);
  86. $form = new FormValidator('course_category', 'post', $url);
  87. $form->addElement('header', '', $form_title);
  88. $form->addElement('hidden', 'formSent', 1);
  89. $form->addElement('text', 'code', get_lang("CategoryCode"));
  90. if (api_get_configuration_value('save_titles_as_html')) {
  91. $form->addHtmlEditor(
  92. 'name',
  93. get_lang('CategoryName'),
  94. true,
  95. false,
  96. ['ToolbarSet' => 'Minimal']
  97. );
  98. } else {
  99. $form->addElement('text', 'name', get_lang("CategoryName"));
  100. $form->addRule('name', get_lang('PleaseEnterCategoryInfo'), 'required');
  101. }
  102. $form->addRule('code', get_lang('PleaseEnterCategoryInfo'), 'required');
  103. $group = [
  104. $form->createElement(
  105. 'radio',
  106. 'auth_course_child',
  107. get_lang('AllowCoursesInCategory'),
  108. get_lang('Yes'),
  109. 'TRUE'
  110. ),
  111. $form->createElement(
  112. 'radio',
  113. 'auth_course_child',
  114. null,
  115. get_lang('No'),
  116. 'FALSE'
  117. ),
  118. ];
  119. $form->addGroup($group, null, get_lang('AllowCoursesInCategory'));
  120. if ($myCourseListAsCategory) {
  121. $form->addHtmlEditor(
  122. 'description',
  123. get_lang('Description'),
  124. false,
  125. false,
  126. ['ToolbarSet' => 'Minimal']
  127. );
  128. $form->addFile('image', get_lang('Image'), ['accept' => 'image/*']);
  129. if ($action == 'edit' && !empty($categoryInfo['image'])) {
  130. $form->addHtml('
  131. <div class="form-group">
  132. <div class="col-sm-offset-2 col-sm-8">'.
  133. Display::img(
  134. api_get_path(WEB_UPLOAD_PATH).$categoryInfo['image'],
  135. get_lang('Image'),
  136. ['width' => 256]
  137. ).'</div>
  138. </div>
  139. ');
  140. }
  141. }
  142. if (!empty($categoryInfo)) {
  143. $class = 'save';
  144. $text = get_lang('Save');
  145. $form->setDefaults($categoryInfo);
  146. $form->addButtonSave($text);
  147. } else {
  148. $class = 'add';
  149. $text = get_lang('AddCategory');
  150. $form->setDefaults(['auth_course_child' => 'TRUE']);
  151. $form->addButtonCreate($text);
  152. }
  153. $form->display();
  154. } else {
  155. // If multiple URLs and not main URL, prevent deletion and inform user
  156. if ($action == 'delete' && api_get_multiple_access_url() && $urlId != 1) {
  157. echo Display::return_message(get_lang('CourseCategoriesAreGlobal'), 'warning');
  158. }
  159. echo '<div class="actions">';
  160. $link = null;
  161. if (!empty($parentInfo)) {
  162. $parentCode = $parentInfo['parent_id'];
  163. echo Display::url(
  164. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
  165. api_get_path(WEB_CODE_PATH).'admin/course_category.php?category='.$parentCode
  166. );
  167. }
  168. if (empty($parentInfo) || $parentInfo['auth_cat_child'] == 'TRUE') {
  169. $newCategoryLink = Display::url(
  170. Display::return_icon('new_folder.png', get_lang('AddACategory'), '', ICON_SIZE_MEDIUM),
  171. api_get_path(WEB_CODE_PATH).'admin/course_category.php?action=add&category='.Security::remove_XSS($category)
  172. );
  173. if (!empty($parentInfo) && $parentInfo['access_url_id'] != $urlId) {
  174. $newCategoryLink = '';
  175. }
  176. echo $newCategoryLink;
  177. }
  178. echo '</div>';
  179. if (!empty($parentInfo)) {
  180. echo Display::page_subheader($parentInfo['name'].' ('.$parentInfo['code'].')');
  181. }
  182. echo CourseCategory::listCategories($category);
  183. }
  184. Display::display_footer();