course_category.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php // $Id: course_category.php 14214 2008-01-31 03:31:49Z yannoo $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * @package dokeos.admin
  22. ==============================================================================
  23. */
  24. // name of the language file that needs to be included
  25. $language_file='admin';
  26. $cidReset=true;
  27. require('../inc/global.inc.php');
  28. $this_section=SECTION_PLATFORM_ADMIN;
  29. api_protect_admin_script();
  30. $category=$_GET['category'];
  31. $action=$_GET['action'];
  32. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  33. $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
  34. $errorMsg='';
  35. if(!empty($action))
  36. {
  37. if($action == 'delete')
  38. {
  39. deleteNode($_GET['id']);
  40. header('Location: '.api_get_self().'?category='.Security::remove_XSS($category));
  41. exit();
  42. }
  43. elseif(($action == 'add' || $action == 'edit') && $_POST['formSent'])
  44. {
  45. $_POST['categoryCode']=trim($_POST['categoryCode']);
  46. $_POST['categoryName']=trim($_POST['categoryName']);
  47. if(!empty($_POST['categoryCode']) && !empty($_POST['categoryName']))
  48. {
  49. if($action == 'add')
  50. {
  51. $ret=addNode($_POST['categoryCode'],$_POST['categoryName'],$_POST['canHaveCourses'],$category);
  52. }
  53. else
  54. {
  55. $ret=editNode($_POST['categoryCode'],$_POST['categoryName'],$_POST['canHaveCourses'],$_GET['id']);
  56. }
  57. if($ret)
  58. {
  59. $action='';
  60. }
  61. else
  62. {
  63. $errorMsg=get_lang('CatCodeAlreadyUsed');
  64. }
  65. }
  66. else
  67. {
  68. $errorMsg=get_lang('PleaseEnterCategoryInfo');
  69. }
  70. }
  71. elseif($action == 'edit')
  72. {
  73. $categoryCode=Database::escape_string($_GET['id']);
  74. $result=api_sql_query("SELECT name,auth_course_child FROM $tbl_category WHERE code='$categoryCode'",__FILE__,__LINE__);
  75. list($categoryName,$canHaveCourses)=Database::fetch_row($result);
  76. $canHaveCourses=($canHaveCourses == 'FALSE')?0:1;
  77. }
  78. elseif($action == 'moveUp')
  79. {
  80. moveNodeUp($_GET['id'],$_GET['tree_pos'],$category);
  81. header('Location: '.api_get_self().'?category='.Security::remove_XSS($category));
  82. exit();
  83. }
  84. }
  85. $tool_name=get_lang('AdminCategories');
  86. $interbreadcrumb[]=array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  87. Display::display_header($tool_name);
  88. //api_display_tool_title($tool_name);
  89. if(!empty($category))
  90. {
  91. $myquery = "SELECT * FROM $tbl_category WHERE code ='$category'";
  92. $result = api_sql_query($myquery,__FILE__,__LINE__);
  93. if(Database::num_rows($result)==0)
  94. {
  95. $category = '';
  96. }
  97. }
  98. if(empty($action))
  99. {
  100. $myquery="SELECT t1.name,t1.code,t1.parent_id,t1.tree_pos,t1.children_count,COUNT(DISTINCT t3.code) AS nbr_courses 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 WHERE t1.parent_id ".(empty($category)?"IS NULL":"='$category'")." GROUP BY t1.name,t1.code,t1.parent_id,t1.tree_pos,t1.children_count ORDER BY t1.tree_pos";
  101. $result=api_sql_query($myquery,__FILE__,__LINE__);
  102. $Categories=api_store_result($result);
  103. }
  104. if(!empty($category) && empty($action))
  105. {
  106. $myquery = "SELECT parent_id FROM $tbl_category WHERE code='$category'";
  107. $result=api_sql_query($myquery,__FILE__,__LINE__);
  108. $parent_id = 0;
  109. if(Database::num_rows($result)>0){
  110. $parent_id=Database::fetch_row($result);
  111. }
  112. ?>
  113. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($parent_id); ?>">&lt;&lt; <?php echo get_lang("Back"); if(!empty($parent_id)) echo ' ('.$parent_id.')'; ?></a>
  114. <?php
  115. }
  116. if($action == 'add' || $action == 'edit')
  117. {
  118. ?>
  119. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($category); ?>">&lt;&lt; <?php echo get_lang("Back"); if(!empty($category)) echo ' ('.Security::remove_XSS($category).')'; ?></a>
  120. <h3><?php echo ($action == 'add')?get_lang('AddACategory'):get_lang('EditNode'); if(!empty($category)) echo ' '.get_lang('Into').' '.Security::remove_XSS($category); ?></h3>
  121. <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 Security::remove_XSS($_GET['id']); ?>">
  122. <input type="hidden" name="formSent" value="1" />
  123. <table border="0" cellpadding="5" cellspacing="0">
  124. <?php
  125. if(!empty($errorMsg))
  126. {
  127. ?>
  128. <tr>
  129. <td colspan="2">
  130. <?php
  131. Display::display_normal_message($errorMsg); //main API
  132. ?>
  133. </td>
  134. </tr>
  135. <?php
  136. }
  137. ?>
  138. <tr>
  139. <td nowrap="nowrap"><?php echo get_lang("CategoryCode"); ?> :</td>
  140. <td><input type="text" name="categoryCode" size="20" maxlength="20" value="<?php echo htmlentities(stripslashes($categoryCode),ENT_QUOTES,$charset); ?>" /></td>
  141. </tr>
  142. <tr>
  143. <td nowrap="nowrap"><?php echo get_lang("CategoryName"); ?> :</td>
  144. <td><input type="text" name="categoryName" size="20" maxlength="100" value="<?php echo htmlentities(stripslashes($categoryName),ENT_QUOTES,$charset); ?>" /></td>
  145. </tr>
  146. <tr>
  147. <td nowrap="nowrap"><?php echo get_lang("AllowCoursesInCategory"); ?></td>
  148. <td>
  149. <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"); ?>
  150. <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"); ?>
  151. </td>
  152. </tr>
  153. <tr>
  154. <td>&nbsp;</td>
  155. <td><input type="submit" value="<?php echo get_lang("Ok"); ?>" /></td>
  156. </tr>
  157. </table>
  158. </form>
  159. <?php
  160. }
  161. else
  162. {
  163. ?>
  164. <ul>
  165. <?php
  166. if(count($Categories)>0)
  167. {
  168. foreach($Categories as $enreg)
  169. {
  170. ?>
  171. <li>
  172. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($enreg['code']); ?>"><img src="../img/folder_document.gif" border="0" title="<?php echo get_lang("OpenNode"); ?>" alt="<?php echo get_lang("OpenNode"); ?>" align="absbottom" /></a>
  173. <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']); ?>"><img src="../img/edit.gif" border="0" title="<?php echo get_lang("EditNode"); ?>" alt ="<?php echo get_lang("EditNode"); ?>" /></a>
  174. <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(htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)); ?>')) return false;"><img src="../img/delete.gif" border="0" title="<?php echo get_lang("DeleteNode"); ?>" alt="<?php echo get_lang("DeleteNode"); ?>" /></a>
  175. <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']; ?>"><img src="../img/up.gif" border="0" title="<?php echo get_lang("UpInSameLevel"); ?>" alt="<?php echo get_lang("UpInSameLevel"); ?>" /></a>
  176. <?php echo $enreg['name']; ?>
  177. (<?php echo $enreg['children_count']; ?> <?php echo get_lang("Categories"); ?> - <?php echo $enreg['nbr_courses']; ?> <?php echo get_lang("Courses"); ?>)
  178. </li>
  179. <?php
  180. }
  181. unset($Categories);
  182. }
  183. else
  184. {
  185. echo get_lang("NoCategories");
  186. }
  187. ?>
  188. </ul>
  189. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($category); ?>&amp;action=add"><?php echo get_lang("AddACategory"); if(!empty($category)) echo ' '.get_lang('Into').' '.Security::remove_XSS($category); ?></a>
  190. <?php
  191. }
  192. /*
  193. ==============================================================================
  194. FOOTER
  195. ==============================================================================
  196. */
  197. Display::display_footer();
  198. /******** Functions ********/
  199. function deleteNode($node)
  200. {
  201. global $tbl_category, $tbl_course;
  202. $node = Database::escape_string($node);
  203. $result=api_sql_query("SELECT parent_id,tree_pos FROM $tbl_category WHERE code='$node'",__FILE__,__LINE__);
  204. if($row=Database::fetch_array($result))
  205. {
  206. if(!empty($row['parent_id']))
  207. {
  208. api_sql_query("UPDATE $tbl_course SET category_code='".$row['parent_id']."' WHERE category_code='$node'",__FILE__,__LINE__);
  209. api_sql_query("UPDATE $tbl_category SET parent_id='".$row['parent_id']."' WHERE parent_id='$node'",__FILE__,__LINE__);
  210. }
  211. else
  212. {
  213. api_sql_query("UPDATE $tbl_course SET category_code='' WHERE category_code='$node'",__FILE__,__LINE__);
  214. api_sql_query("UPDATE $tbl_category SET parent_id=NULL WHERE parent_id='$node'",__FILE__,__LINE__);
  215. }
  216. api_sql_query("UPDATE $tbl_category SET tree_pos=tree_pos-1 WHERE tree_pos > '".$row['tree_pos']."'",__FILE__,__LINE__);
  217. api_sql_query("DELETE FROM $tbl_category WHERE code='$node'",__FILE__,__LINE__);
  218. if(!empty($row['parent_id']))
  219. {
  220. updateFils($row['parent_id']);
  221. }
  222. }
  223. }
  224. function addNode($code,$name,$canHaveCourses,$parent_id)
  225. {
  226. global $tbl_category;
  227. $canHaveCourses=$canHaveCourses?'TRUE':'FALSE';
  228. $code = Database::escape_string($code);
  229. $name = Database::escape_string($name);
  230. $parent_id = Database::escape_string($parent_id);
  231. $result=api_sql_query("SELECT 1 FROM $tbl_category WHERE code='$code'",__FILE__,__LINE__);
  232. if(Database::num_rows($result))
  233. {
  234. return false;
  235. }
  236. $result=api_sql_query("SELECT MAX(tree_pos) AS maxTreePos FROM $tbl_category",__FILE__,__LINE__);
  237. $row=Database::fetch_array($result);
  238. $tree_pos=$row['maxTreePos']+1;
  239. api_sql_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')",__FILE__,__LINE__);
  240. updateFils($parent_id);
  241. return true;
  242. }
  243. function editNode($code,$name,$canHaveCourses,$old_code)
  244. {
  245. global $tbl_category;
  246. $canHaveCourses=$canHaveCourses?'TRUE':'FALSE';
  247. $code = Database::escape_string($code);
  248. $name = Database::escape_string($name);
  249. $old_code = Database::escape_string($old_code);
  250. if($code != $old_code)
  251. {
  252. $result=api_sql_query("SELECT 1 FROM $tbl_category WHERE code='$code'",__FILE__,__LINE__);
  253. if(Database::num_rows($result))
  254. {
  255. return false;
  256. }
  257. }
  258. api_sql_query("UPDATE $tbl_category SET name='$name',code='$code',auth_course_child='$canHaveCourses' WHERE code='$old_code'",__FILE__,__LINE__);
  259. return true;
  260. }
  261. function moveNodeUp($code,$tree_pos,$parent_id)
  262. {
  263. global $tbl_category;
  264. $code = Database::escape_string($code);
  265. $tree_pos = Database::escape_string($tree_pos);
  266. $parent_id = Database::escape_string($parent_id);
  267. $result=api_sql_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",__FILE__,__LINE__);
  268. if(!$row=Database::fetch_array($result))
  269. {
  270. $result=api_sql_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",__FILE__,__LINE__);
  271. if(!$row=Database::fetch_array($result))
  272. {
  273. return false;
  274. }
  275. }
  276. api_sql_query("UPDATE $tbl_category SET tree_pos='".$row['tree_pos']."' WHERE code='$code'",__FILE__,__LINE__);
  277. api_sql_query("UPDATE $tbl_category SET tree_pos='$tree_pos' WHERE code='$row[code]'",__FILE__,__LINE__);
  278. }
  279. function updateFils($category)
  280. {
  281. global $tbl_category;
  282. $category = Database::escape_string($category);
  283. $result=api_sql_query("SELECT parent_id FROM $tbl_category WHERE code='$category'",__FILE__,__LINE__);
  284. if($row=Database::fetch_array($result))
  285. {
  286. updateFils($row['parent_id']);
  287. }
  288. $children_count=compterFils($category,0)-1;
  289. api_sql_query("UPDATE $tbl_category SET children_count='$children_count' WHERE code='$category'",__FILE__,__LINE__);
  290. }
  291. function compterFils($pere,$cpt)
  292. {
  293. global $tbl_category;
  294. $pere = Database::escape_string($pere);
  295. $result=api_sql_query("SELECT code FROM $tbl_category WHERE parent_id='$pere'",__FILE__,__LINE__);
  296. while($row=Database::fetch_array($result))
  297. {
  298. $cpt=compterFils($row['code'],$cpt);
  299. }
  300. return ($cpt+1);
  301. }
  302. ?>