course_category.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php // $Id: course_category.php 20648 2009-05-14 17:34:11Z cvargas1 $
  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. * @todo use formvalidator for the form
  23. ==============================================================================
  24. */
  25. // name of the language file that needs to be included
  26. $language_file='admin';
  27. $cidReset=true;
  28. require('../inc/global.inc.php');
  29. $this_section=SECTION_PLATFORM_ADMIN;
  30. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  31. api_protect_admin_script();
  32. $category=$_GET['category'];
  33. $action=$_GET['action'];
  34. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  35. $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
  36. $errorMsg='';
  37. if(!empty($action))
  38. {
  39. if($action == 'delete')
  40. {
  41. deleteNode($_GET['id']);
  42. header('Location: '.api_get_self().'?category='.Security::remove_XSS($category));
  43. exit();
  44. }
  45. elseif(($action == 'add' || $action == 'edit') && $_POST['formSent'])
  46. {
  47. $_POST['categoryCode']=trim($_POST['categoryCode']);
  48. $_POST['categoryName']=trim($_POST['categoryName']);
  49. if(!empty($_POST['categoryCode']) && !empty($_POST['categoryName']))
  50. {
  51. if($action == 'add')
  52. {
  53. $ret=addNode($_POST['categoryCode'],$_POST['categoryName'],$_POST['canHaveCourses'],$category);
  54. }
  55. else
  56. {
  57. $ret=editNode($_POST['categoryCode'],$_POST['categoryName'],$_POST['canHaveCourses'],$_GET['id']);
  58. }
  59. if($ret)
  60. {
  61. $action='';
  62. }
  63. else
  64. {
  65. $errorMsg=get_lang('CatCodeAlreadyUsed');
  66. }
  67. }
  68. else
  69. {
  70. $errorMsg=get_lang('PleaseEnterCategoryInfo');
  71. }
  72. }
  73. elseif($action == 'edit')
  74. {
  75. $categoryCode=Database::escape_string($_GET['id']);
  76. $result=api_sql_query("SELECT name,auth_course_child FROM $tbl_category WHERE code='$categoryCode'",__FILE__,__LINE__);
  77. list($categoryName,$canHaveCourses)=Database::fetch_row($result);
  78. $canHaveCourses=($canHaveCourses == 'FALSE')?0:1;
  79. }
  80. elseif($action == 'moveUp')
  81. {
  82. moveNodeUp($_GET['id'],$_GET['tree_pos'],$category);
  83. header('Location: '.api_get_self().'?category='.Security::remove_XSS($category));
  84. exit();
  85. }
  86. }
  87. $tool_name=get_lang('AdminCategories');
  88. $interbreadcrumb[]=array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  89. //$interbreadcrumb[]=array('url' => 'configure_homepage.php',"name" => get_lang('ConfigureHomePage'));
  90. Display::display_header($tool_name);
  91. //api_display_tool_title($tool_name);
  92. if(!empty($category))
  93. {
  94. $myquery = "SELECT * FROM $tbl_category WHERE code ='$category'";
  95. $result = api_sql_query($myquery,__FILE__,__LINE__);
  96. if(Database::num_rows($result)==0)
  97. {
  98. $category = '';
  99. }
  100. }
  101. if(empty($action))
  102. {
  103. $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";
  104. $result=api_sql_query($myquery,__FILE__,__LINE__);
  105. $Categories=api_store_result($result);
  106. }
  107. if($action == 'add' || $action == 'edit')
  108. {
  109. ?>
  110. <div class="actions">
  111. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($category); ?>"><?php echo Display::return_icon('folder_up.gif').get_lang("Back"); if(!empty($category)) echo ' ('.Security::remove_XSS($category).')'; ?></a>
  112. </div>
  113. <?php
  114. $form_title = ($action == 'add')?get_lang('AddACategory'):get_lang('EditNode');
  115. if(!empty($category))
  116. {
  117. $form_title .= ' '.get_lang('Into').' '.Security::remove_XSS($category);
  118. }
  119. $form = new FormValidator('course_category');
  120. $form->addElement('header', '', $form_title);
  121. $form->display();
  122. ?>
  123. <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']); ?>">
  124. <input type="hidden" name="formSent" value="1" />
  125. <table border="0" cellpadding="5" cellspacing="0">
  126. <?php
  127. if(!empty($errorMsg))
  128. {
  129. ?>
  130. <tr>
  131. <td colspan="2">
  132. <?php
  133. Display::display_normal_message($errorMsg); //main API
  134. ?>
  135. </td>
  136. </tr>
  137. <?php
  138. }
  139. ?>
  140. <tr>
  141. <td nowrap="nowrap"><?php echo get_lang("CategoryCode"); ?> :</td>
  142. <td><input type="text" name="categoryCode" size="20" maxlength="20" value="<?php echo api_htmlentities(stripslashes($categoryCode),ENT_QUOTES,$charset); ?>" /></td>
  143. </tr>
  144. <tr>
  145. <td nowrap="nowrap"><?php echo get_lang("CategoryName"); ?> :</td>
  146. <td><input type="text" name="categoryName" size="20" maxlength="100" value="<?php echo api_htmlentities(stripslashes($categoryName),ENT_QUOTES,$charset); ?>" /></td>
  147. </tr>
  148. <tr>
  149. <td nowrap="nowrap"><?php echo get_lang("AllowCoursesInCategory"); ?></td>
  150. <td>
  151. <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"); ?>
  152. <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"); ?>
  153. </td>
  154. </tr>
  155. <tr>
  156. <td>&nbsp;</td>
  157. <?php
  158. if(isset($_GET['id']) && !empty($_GET['id'])) {
  159. $class="save";
  160. $text=get_lang('CategoryMod');
  161. } else {
  162. $class="add";
  163. $text=get_lang('AddCategory');
  164. }
  165. ?>
  166. <td><button type="submit" class="<?php echo $class; ?>" value="<?php echo $text; ?>" ><?php echo $text; ?></button></td>
  167. </tr>
  168. </table>
  169. </form>
  170. <?php
  171. }
  172. else
  173. {
  174. ?>
  175. <div class="actions">
  176. <?php
  177. if(!empty($category) && empty($action))
  178. {
  179. $myquery = "SELECT parent_id FROM $tbl_category WHERE code='$category'";
  180. $result=api_sql_query($myquery,__FILE__,__LINE__);
  181. $parent_id = 0;
  182. if(Database::num_rows($result)>0){
  183. $parent_id=Database::fetch_array($result);
  184. }
  185. $parent_id['parent_id']?$link=' ('.$parent_id['parent_id'].')':$link='';
  186. ?>
  187. <a href="<?php echo api_get_self(); ?>?category=<?php echo $parent_id['parent_id']; ?>"><?php echo Display::return_icon('folder_up.gif').get_lang("Back"); if(!empty($parent_id)) echo $link ?></a>
  188. <?php
  189. }
  190. ?>
  191. <a href="<?php echo api_get_self(); ?>?category=<?php echo Security::remove_XSS($category); ?>&amp;action=add"><?php echo Display::return_icon('folder_new.gif').get_lang("AddACategory"); if(!empty($category)) echo ' '.get_lang('Into').' '.Security::remove_XSS($category); ?></a>
  192. </div>
  193. <ul>
  194. <?php
  195. if(count($Categories)>0)
  196. {
  197. foreach($Categories as $enreg)
  198. {
  199. ?>
  200. <li>
  201. <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>
  202. <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>
  203. <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(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)); ?>')) return false;"><?php Display::display_icon('delete.gif', get_lang('DeleteNode'));?></a>
  204. <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>
  205. <?php echo $enreg['name']; ?>
  206. (<?php echo $enreg['children_count']; ?> <?php echo get_lang("Categories"); ?> - <?php echo $enreg['nbr_courses']; ?> <?php echo get_lang("Courses"); ?>)
  207. </li>
  208. <?php
  209. }
  210. unset($Categories);
  211. }
  212. else
  213. {
  214. echo get_lang("NoCategories");
  215. }
  216. ?>
  217. </ul>
  218. <?php
  219. }
  220. /*
  221. ==============================================================================
  222. FOOTER
  223. ==============================================================================
  224. */
  225. Display::display_footer();
  226. /******** Functions ********/
  227. function deleteNode($node)
  228. {
  229. global $tbl_category, $tbl_course;
  230. $node = Database::escape_string($node);
  231. $result=api_sql_query("SELECT parent_id,tree_pos FROM $tbl_category WHERE code='$node'",__FILE__,__LINE__);
  232. if($row=Database::fetch_array($result))
  233. {
  234. if(!empty($row['parent_id']))
  235. {
  236. api_sql_query("UPDATE $tbl_course SET category_code='".$row['parent_id']."' WHERE category_code='$node'",__FILE__,__LINE__);
  237. api_sql_query("UPDATE $tbl_category SET parent_id='".$row['parent_id']."' WHERE parent_id='$node'",__FILE__,__LINE__);
  238. }
  239. else
  240. {
  241. api_sql_query("UPDATE $tbl_course SET category_code='' WHERE category_code='$node'",__FILE__,__LINE__);
  242. api_sql_query("UPDATE $tbl_category SET parent_id=NULL WHERE parent_id='$node'",__FILE__,__LINE__);
  243. }
  244. api_sql_query("UPDATE $tbl_category SET tree_pos=tree_pos-1 WHERE tree_pos > '".$row['tree_pos']."'",__FILE__,__LINE__);
  245. api_sql_query("DELETE FROM $tbl_category WHERE code='$node'",__FILE__,__LINE__);
  246. if(!empty($row['parent_id']))
  247. {
  248. updateFils($row['parent_id']);
  249. }
  250. }
  251. }
  252. function addNode($code,$name,$canHaveCourses,$parent_id)
  253. {
  254. global $tbl_category;
  255. $canHaveCourses=$canHaveCourses?'TRUE':'FALSE';
  256. $code = Database::escape_string($code);
  257. $name = Database::escape_string($name);
  258. $parent_id = Database::escape_string($parent_id);
  259. $result=api_sql_query("SELECT 1 FROM $tbl_category WHERE code='$code'",__FILE__,__LINE__);
  260. if(Database::num_rows($result))
  261. {
  262. return false;
  263. }
  264. $result=api_sql_query("SELECT MAX(tree_pos) AS maxTreePos FROM $tbl_category",__FILE__,__LINE__);
  265. $row=Database::fetch_array($result);
  266. $tree_pos=$row['maxTreePos']+1;
  267. 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__);
  268. updateFils($parent_id);
  269. return true;
  270. }
  271. function editNode($code,$name,$canHaveCourses,$old_code)
  272. {
  273. global $tbl_category;
  274. $canHaveCourses=$canHaveCourses?'TRUE':'FALSE';
  275. $code = Database::escape_string($code);
  276. $name = Database::escape_string($name);
  277. $old_code = Database::escape_string($old_code);
  278. if($code != $old_code)
  279. {
  280. $result=api_sql_query("SELECT 1 FROM $tbl_category WHERE code='$code'",__FILE__,__LINE__);
  281. if(Database::num_rows($result))
  282. {
  283. return false;
  284. }
  285. }
  286. api_sql_query("UPDATE $tbl_category SET name='$name',code='$code',auth_course_child='$canHaveCourses' WHERE code='$old_code'",__FILE__,__LINE__);
  287. return true;
  288. }
  289. function moveNodeUp($code,$tree_pos,$parent_id)
  290. {
  291. global $tbl_category;
  292. $code = Database::escape_string($code);
  293. $tree_pos = Database::escape_string($tree_pos);
  294. $parent_id = Database::escape_string($parent_id);
  295. $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__);
  296. if(!$row=Database::fetch_array($result))
  297. {
  298. $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__);
  299. if(!$row=Database::fetch_array($result))
  300. {
  301. return false;
  302. }
  303. }
  304. api_sql_query("UPDATE $tbl_category SET tree_pos='".$row['tree_pos']."' WHERE code='$code'",__FILE__,__LINE__);
  305. api_sql_query("UPDATE $tbl_category SET tree_pos='$tree_pos' WHERE code='$row[code]'",__FILE__,__LINE__);
  306. }
  307. function updateFils($category)
  308. {
  309. global $tbl_category;
  310. $category = Database::escape_string($category);
  311. $result=api_sql_query("SELECT parent_id FROM $tbl_category WHERE code='$category'",__FILE__,__LINE__);
  312. if($row=Database::fetch_array($result))
  313. {
  314. updateFils($row['parent_id']);
  315. }
  316. $children_count=compterFils($category,0)-1;
  317. api_sql_query("UPDATE $tbl_category SET children_count='$children_count' WHERE code='$category'",__FILE__,__LINE__);
  318. }
  319. function compterFils($pere,$cpt)
  320. {
  321. global $tbl_category;
  322. $pere = Database::escape_string($pere);
  323. $result=api_sql_query("SELECT code FROM $tbl_category WHERE parent_id='$pere'",__FILE__,__LINE__);
  324. while($row=Database::fetch_array($result))
  325. {
  326. $cpt=compterFils($row['code'],$cpt);
  327. }
  328. return ($cpt+1);
  329. }
  330. ?>