course_category.php 6.8 KB

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