course_category.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. * @todo use formvalidator for the form
  6. */
  7. /**
  8. * Code
  9. */
  10. // name of the language file that needs to be included
  11. $language_file = 'admin';
  12. $cidReset = true;
  13. require_once '../inc/global.inc.php';
  14. $this_section = SECTION_PLATFORM_ADMIN;
  15. api_protect_admin_script();
  16. $category = Database::escape_string($_GET['category']);
  17. $action = isset($_GET['action']) ? $_GET['action'] : null;
  18. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  19. $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
  20. $errorMsg = '';
  21. $id = isset($_GET['id']) ? $_GET['id'] : null;
  22. $categoryCode = isset($_POST['categoryCode']) ? $_POST['categoryCode'] : null;
  23. $categoryName = isset($_POST['categoryName']) ? $_POST['categoryName'] : null;
  24. $formSent = isset($_POST['formSent']) ? $_POST['formSent'] : false;
  25. $canHaveCourses = 0;
  26. if (!empty($action)) {
  27. if ($action == 'delete') {
  28. if (api_get_multiple_access_url()) {
  29. if (api_get_current_access_url_id() == 1) {
  30. deleteNode($id);
  31. header('Location: ' . api_get_self() . '?category=' . Security::remove_XSS($category));
  32. exit();
  33. } else {
  34. $delError = 1;
  35. }
  36. } else {
  37. deleteNode($_GET['id']);
  38. header('Location: ' . api_get_self() . '?category=' . Security::remove_XSS($category));
  39. exit();
  40. }
  41. } elseif (($action == 'add' || $action == 'edit') && $_POST['formSent']) {
  42. $_POST['categoryCode'] = trim($_POST['categoryCode']);
  43. $_POST['categoryName'] = trim($_POST['categoryName']);
  44. if (!empty($_POST['categoryCode']) && !empty($_POST['categoryName'])) {
  45. if ($action == 'add') {
  46. $ret = addNode($_POST['categoryCode'], $_POST['categoryName'], $_POST['canHaveCourses'], $category);
  47. } else {
  48. $ret = editNode($_POST['categoryCode'], $_POST['categoryName'], $_POST['canHaveCourses'], $id);
  49. }
  50. if ($ret) {
  51. $action = '';
  52. } else {
  53. $errorMsg = get_lang('CatCodeAlreadyUsed');
  54. }
  55. } else {
  56. $errorMsg = get_lang('PleaseEnterCategoryInfo');
  57. }
  58. } elseif ($action == 'edit') {
  59. if (!empty($id)) {
  60. $categoryCode = $id;
  61. $sql = "SELECT name, auth_course_child FROM $tbl_category WHERE code='$id'";
  62. $result = Database::query($sql);
  63. list($categoryName, $canHaveCourses) = Database::fetch_row($result);
  64. $canHaveCourses = ($canHaveCourses == 'FALSE') ? 0 : 1;
  65. }
  66. } elseif ($action == 'moveUp') {
  67. moveNodeUp($id, $_GET['tree_pos'], $category);
  68. header('Location: ' . api_get_self() . '?category=' . Security::remove_XSS($category));
  69. exit();
  70. }
  71. }
  72. $tool_name = get_lang('AdminCategories');
  73. $interbreadcrumb[] = array('url' => 'index.php', "name" => get_lang('PlatformAdmin'));
  74. //$interbreadcrumb[]=array('url' => 'configure_homepage.php',"name" => get_lang('ConfigureHomePage'));
  75. Display::display_header($tool_name);
  76. if (!empty($category)) {
  77. $myquery = "SELECT * FROM $tbl_category WHERE code ='$category'";
  78. $result = Database::query($myquery);
  79. if (Database::num_rows($result) == 0) {
  80. $category = '';
  81. }
  82. }
  83. if (empty($action)) {
  84. $myquery = "SELECT t1.name,t1.code,t1.parent_id,t1.tree_pos,t1.children_count,COUNT(DISTINCT t3.code) AS nbr_courses
  85. FROM $tbl_category t1 LEFT JOIN $tbl_category t2 ON t1.code=t2.parent_id LEFT JOIN $tbl_course t3 ON t3.category_code=t1.code
  86. WHERE t1.parent_id " . (empty($category) ? "IS NULL" : "='$category'") . "
  87. GROUP BY t1.name,t1.code,t1.parent_id,t1.tree_pos,t1.children_count ORDER BY t1.tree_pos";
  88. $result = Database::query($myquery);
  89. $Categories = Database::store_result($result);
  90. }
  91. if ($action == 'add' || $action == 'edit') {
  92. if ((api_get_multiple_access_url() && api_get_current_access_url_id() == 1) || !api_get_multiple_access_url() ) { ?>
  93. <div class="actions">
  94. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($category); ?>"><?php echo Display::return_icon('folder_up.png', get_lang("Back"), '', ICON_SIZE_MEDIUM);
  95. if (!empty($category)) echo ' (' . Security::remove_XSS($category) . ')'; ?></a>
  96. </div>
  97. <?php
  98. $form_title = ($action == 'add') ? get_lang('AddACategory') : get_lang('EditNode');
  99. if (!empty($category)) {
  100. $form_title .= ' ' . get_lang('Into') . ' ' . Security::remove_XSS($category);
  101. }
  102. $form = new FormValidator('course_category');
  103. $form->addElement('header', '', $form_title);
  104. $form->display();
  105. ?>
  106. <form method="post" action="<?php echo api_get_self(); ?>?action=<?php echo Security::remove_XSS($action); ?>&category=<?php echo Security::remove_XSS($category); ?>&amp;id=<?php echo $id; ?>">
  107. <input type="hidden" name="formSent" value="1" />
  108. <table border="0" cellpadding="5" cellspacing="0">
  109. <?php
  110. if (!empty($errorMsg)) {
  111. ?>
  112. <tr>
  113. <td colspan="2">
  114. <?php
  115. Display::display_normal_message($errorMsg); //main API
  116. ?>
  117. </td>
  118. </tr>
  119. <?php
  120. }
  121. ?>
  122. <tr>
  123. <td nowrap="nowrap"><?php echo get_lang("CategoryCode"); ?> :</td>
  124. <td><input type="text" name="categoryCode" size="20" maxlength="20" value="<?php echo api_htmlentities(stripslashes($categoryCode), ENT_QUOTES, $charset); ?>" /></td>
  125. </tr>
  126. <tr>
  127. <td nowrap="nowrap"><?php echo get_lang("CategoryName"); ?> :</td>
  128. <td><input type="text" name="categoryName" size="20" maxlength="100" value="<?php echo api_htmlentities(stripslashes($categoryName), ENT_QUOTES, $charset); ?>" /></td>
  129. </tr>
  130. <tr>
  131. <td nowrap="nowrap"><?php echo get_lang("AllowCoursesInCategory"); ?></td>
  132. <td>
  133. <input class="checkbox" type="radio" name="canHaveCourses" value="0" <?php if (($action == 'edit' && !$canHaveCourses) || ($action == 'add' && $formSent && !$canHaveCourses)) echo 'checked="checked"'; ?> /><?php echo get_lang("No"); ?>
  134. <input class="checkbox" type="radio" name="canHaveCourses" value="1" <?php if (($action == 'edit' && $canHaveCourses) || ($action == 'add' && !$formSent || $canHaveCourses)) echo 'checked="checked"'; ?> /><?php echo get_lang("Yes"); ?>
  135. </td>
  136. </tr>
  137. <tr>
  138. <td>&nbsp;</td>
  139. <?php
  140. if (!empty($id)) {
  141. $class = "save";
  142. $text = get_lang('CategoryMod');
  143. } else {
  144. $class = "add";
  145. $text = get_lang('AddCategory');
  146. }
  147. ?>
  148. <td><button type="submit" class="<?php echo $class; ?>" value="<?php echo $text; ?>" ><?php echo $text; ?></button></td>
  149. </tr>
  150. </table>
  151. </form>
  152. <?php } elseif (api_get_multiple_access_url() && api_get_current_access_url_id() != 1) {
  153. Display :: display_error_message(get_lang('CourseCategoriesAreGlobal'));
  154. }
  155. } else {
  156. if ($delError == 0) { ?>
  157. <div class="actions">
  158. <?php
  159. if (!empty($category) && empty($action)) {
  160. $myquery = "SELECT parent_id FROM $tbl_category WHERE code='$category'";
  161. $result = Database::query($myquery);
  162. $parent_id = 0;
  163. if (Database::num_rows($result) > 0) {
  164. $parent_id = Database::fetch_array($result);
  165. }
  166. $parent_id['parent_id'] ? $link = ' (' . $parent_id['parent_id'] . ')' : $link = '';
  167. ?>
  168. <a href="<?php echo api_get_self(); ?>?category=<?php echo $parent_id['parent_id']; ?>"><?php echo Display::return_icon('folder_up.png', get_lang("Back"), '', ICON_SIZE_MEDIUM);
  169. if (!empty($parent_id)) echo $link ?></a>
  170. <?php
  171. }
  172. ?>
  173. <?php
  174. if (!empty($category)) {
  175. $CategoryInto = ' ' . get_lang('Into') . ' ' . Security::remove_XSS($category);
  176. } else {
  177. $CategoryInto = '';
  178. }
  179. ?>
  180. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($category); ?>&amp;action=add"><?php echo Display::return_icon('new_folder.png', get_lang("AddACategory") . $CategoryInto, '', ICON_SIZE_MEDIUM); ?></a>
  181. </div>
  182. <ul>
  183. <?php
  184. if (count($Categories) > 0) {
  185. foreach ($Categories as $enreg) {
  186. ?>
  187. <li>
  188. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($enreg['code']); ?>"><?php Display::display_icon('folder_document.gif', get_lang('OpenNode')); ?></a>
  189. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($category); ?>&amp;action=edit&amp;id=<?php echo Security::remove_XSS($enreg['code']); ?>"><?php Display::display_icon('edit.gif', get_lang('EditNode')); ?></a>
  190. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($category); ?>&amp;action=delete&amp;id=<?php echo Security::remove_XSS($enreg['code']); ?>" onclick="javascript:if (!confirm('<?php echo addslashes(get_lang('ConfirmYourChoice')); ?>'))
  191. return false;"><?php Display::display_icon('delete.gif', get_lang('DeleteNode')); ?></a>
  192. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($category); ?>&amp;action=moveUp&amp;id=<?php echo Security::remove_XSS($enreg['code']); ?>&amp;tree_pos=<?php echo $enreg['tree_pos']; ?>"><?php Display::display_icon('up.gif', get_lang('UpInSameLevel')); ?></a>
  193. <?php echo $enreg['name']; ?>
  194. (<?php echo $enreg['children_count']; ?> <?php echo get_lang('CategoriesNumber'); ?> - <?php echo $enreg['nbr_courses']; ?> <?php echo get_lang('Courses'); ?>)
  195. </li>
  196. <?php
  197. }
  198. unset($Categories);
  199. } else {
  200. echo get_lang("NoCategories");
  201. }
  202. } else {
  203. Display :: display_error_message(get_lang('CourseCategoriesAreGlobal'));
  204. }?>
  205. </ul>
  206. <?php
  207. }
  208. Display::display_footer();
  209. function deleteNode($node) {
  210. global $tbl_category, $tbl_course;
  211. $node = Database::escape_string($node);
  212. $result = Database::query("SELECT parent_id,tree_pos FROM $tbl_category WHERE code='$node'");
  213. if ($row = Database::fetch_array($result)) {
  214. if (!empty($row['parent_id'])) {
  215. Database::query("UPDATE $tbl_course SET category_code='" . $row['parent_id'] . "' WHERE category_code='$node'");
  216. Database::query("UPDATE $tbl_category SET parent_id='" . $row['parent_id'] . "' WHERE parent_id='$node'");
  217. } else {
  218. Database::query("UPDATE $tbl_course SET category_code='' WHERE category_code='$node'");
  219. Database::query("UPDATE $tbl_category SET parent_id=NULL WHERE parent_id='$node'");
  220. }
  221. Database::query("UPDATE $tbl_category SET tree_pos=tree_pos-1 WHERE tree_pos > '" . $row['tree_pos'] . "'");
  222. Database::query("DELETE FROM $tbl_category WHERE code='$node'");
  223. if (!empty($row['parent_id'])) {
  224. updateFils($row['parent_id']);
  225. }
  226. }
  227. }
  228. function addNode($code, $name, $canHaveCourses, $parent_id) {
  229. global $tbl_category;
  230. $canHaveCourses = $canHaveCourses ? 'TRUE' : 'FALSE';
  231. $code = Database::escape_string($code);
  232. $name = Database::escape_string($name);
  233. $parent_id = Database::escape_string($parent_id);
  234. $result = Database::query("SELECT 1 FROM $tbl_category WHERE code='$code'");
  235. if (Database::num_rows($result)) {
  236. return false;
  237. }
  238. $result = Database::query("SELECT MAX(tree_pos) AS maxTreePos FROM $tbl_category");
  239. $row = Database::fetch_array($result);
  240. $tree_pos = $row['maxTreePos'] + 1;
  241. $code = CourseManager::generate_course_code($code);
  242. Database::query("INSERT INTO $tbl_category(name,code,parent_id,tree_pos,children_count,auth_course_child) VALUES('$name','$code'," . (empty($parent_id) ? "NULL" : "'$parent_id'") . ",'$tree_pos','0','$canHaveCourses')");
  243. updateFils($parent_id);
  244. return true;
  245. }
  246. function editNode($code, $name, $canHaveCourses, $old_code) {
  247. global $tbl_category, $tbl_course;
  248. $canHaveCourses = $canHaveCourses ? 'TRUE' : 'FALSE';
  249. $code = Database::escape_string($code);
  250. $name = Database::escape_string($name);
  251. $old_code = Database::escape_string($old_code);
  252. if ($code != $old_code) {
  253. $result = Database::query("SELECT 1 FROM $tbl_category WHERE code='$code'");
  254. if (Database::num_rows($result)) {
  255. return false;
  256. }
  257. }
  258. $code = CourseManager::generate_course_code($code);
  259. Database::query("UPDATE $tbl_category SET name='$name', code='$code',auth_course_child='$canHaveCourses' WHERE code='$old_code'");
  260. $sql = "UPDATE $tbl_course SET category_code = '$code' WHERE category_code = '$old_code' ";
  261. Database::query($sql);
  262. return true;
  263. }
  264. function moveNodeUp($code, $tree_pos, $parent_id) {
  265. global $tbl_category;
  266. $code = Database::escape_string($code);
  267. $tree_pos = Database::escape_string($tree_pos);
  268. $parent_id = Database::escape_string($parent_id);
  269. $result = Database::query("SELECT code,tree_pos FROM $tbl_category WHERE parent_id " . (empty($parent_id) ? "IS NULL" : "='$parent_id'") . " AND tree_pos<'$tree_pos' ORDER BY tree_pos DESC LIMIT 0,1");
  270. if (!$row = Database::fetch_array($result)) {
  271. $result = Database::query("SELECT code,tree_pos FROM $tbl_category WHERE parent_id " . (empty($parent_id) ? "IS NULL" : "='$parent_id'") . " AND tree_pos>'$tree_pos' ORDER BY tree_pos DESC LIMIT 0,1");
  272. if (!$row = Database::fetch_array($result)) {
  273. return false;
  274. }
  275. }
  276. Database::query("UPDATE $tbl_category SET tree_pos='" . $row['tree_pos'] . "' WHERE code='$code'");
  277. Database::query("UPDATE $tbl_category SET tree_pos='$tree_pos' WHERE code='$row[code]'");
  278. }
  279. function updateFils($category) {
  280. global $tbl_category;
  281. $category = Database::escape_string($category);
  282. $result = Database::query("SELECT parent_id FROM $tbl_category WHERE code='$category'");
  283. if ($row = Database::fetch_array($result)) {
  284. updateFils($row['parent_id']);
  285. }
  286. $children_count = compterFils($category, 0) - 1;
  287. Database::query("UPDATE $tbl_category SET children_count='$children_count' WHERE code='$category'");
  288. }
  289. function compterFils($pere, $cpt) {
  290. global $tbl_category;
  291. $pere = Database::escape_string($pere);
  292. $result = Database::query("SELECT code FROM $tbl_category WHERE parent_id='$pere'");
  293. while ($row = Database::fetch_array($result)) {
  294. $cpt = compterFils($row['code'], $cpt);
  295. }
  296. return ($cpt + 1);
  297. }