courses.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. <?php // $Id: courses.php 12467 2007-05-25 21:35:58Z 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) various contributors
  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.auth
  22. * @todo check if unsubscribing from a course WITH group memberships works as it should
  23. * @todo constants are in uppercase, variables aren't
  24. ==============================================================================
  25. */
  26. /*
  27. ==============================================================================
  28. INIT SECTION
  29. ==============================================================================
  30. */
  31. // name of the language file that needs to be included
  32. $language_file = 'courses';
  33. // including the global file
  34. include('../inc/global.inc.php');
  35. // section for the tabs
  36. $this_section=SECTION_COURSES;
  37. // acces rights: anonymous users can't do anything usefull here
  38. api_block_anonymous_users();
  39. // include additional libraries
  40. include_once(api_get_path(LIBRARY_PATH) . 'debug.lib.inc.php');
  41. include_once(api_get_path(LIBRARY_PATH) . 'course.lib.php');
  42. // Database table definitions
  43. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  44. $tbl_courses_nodes = Database::get_main_table(TABLE_MAIN_CATEGORY);
  45. $tbl_courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  46. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  47. //filter
  48. $safe = array();
  49. $safe['action'] = '';
  50. $actions = array('sortmycourses','createcoursecategory','subscribe','deletecoursecategory','unsubscribe');
  51. if(in_array(htmlentities($_GET['action']),$actions))
  52. {
  53. $safe['action'] = htmlentities($_GET['action']);
  54. }
  55. // title of the page
  56. if ($safe['action'] == 'sortmycourses' OR !isset($safe['action']))
  57. {
  58. $nameTools = get_lang("SortMyCourses");
  59. }
  60. if ($safe['action'] == 'createcoursecategory')
  61. {
  62. $nameTools = get_lang('CreateCourseCategory');
  63. }
  64. if ($safe['action'] == 'subscribe')
  65. {
  66. $nameTools = get_lang("SubscribeToCourse");
  67. }
  68. // breadcrumbs
  69. $interbreadcrumb[] = array('name'=> get_lang('CourseManagement'), 'url'=>'courses.php');
  70. // Displaying the header
  71. Display::display_header($nameTools);
  72. // Diplaying the tool title
  73. // api_display_tool_title($nameTools);
  74. /*
  75. ==============================================================================
  76. COMMANDS SECTION
  77. ==============================================================================
  78. */
  79. unset($message);
  80. // we are moving a course or category of the user up/down the list (=Sort My Courses)
  81. if (isset($_GET['move']))
  82. {
  83. if (isset($_GET['course']))
  84. {
  85. $message=move_course($_GET['move'], $_GET['course'],$_GET['category']);
  86. }
  87. if (isset($_GET['category']) and !$_GET['course'])
  88. {
  89. $message=move_category($_GET['move'], $_GET['category']);
  90. }
  91. }
  92. // we are moving the course of the user to a different user defined course category (=Sort My Courses)
  93. if (isset($_POST['submit_change_course_category']))
  94. {
  95. $message=store_changecoursecategory($_POST['course_2_edit_category'], $_POST['course_categories']);
  96. }
  97. // we are creating a new user defined course category (= Create Course Category)
  98. if (isset($_POST['create_course_category']) AND isset($_POST['title_course_category']) AND strlen(trim($_POST['title_course_category'])) > 0)
  99. {
  100. $message=store_course_category();
  101. }
  102. if (isset($_POST['submit_edit_course_category']) AND isset($_POST['title_course_category']) AND strlen(trim($_POST['title_course_category'])) > 0)
  103. {
  104. $message=store_edit_course_category();
  105. }
  106. // we are subcribing to a course (=Subscribe to course)
  107. if (isset($_POST['subscribe']))
  108. {
  109. $message = subscribe_user($_POST['subscribe']);
  110. }
  111. // we are unsubscribing from a course (=Unsubscribe from course)
  112. if (isset($_POST['unsubscribe']))
  113. {
  114. $message=remove_user_from_course($_user['user_id'], $_POST['unsubscribe']);
  115. }
  116. // we are deleting a course category
  117. if ($safe['action']=='deletecoursecategory' AND isset($_GET['id']))
  118. {
  119. $message=delete_course_category($_GET['id']);
  120. }
  121. // we are displaying any result messages;
  122. if (isset($message))
  123. {
  124. Display::display_confirmation_message($message, false);
  125. }
  126. /*
  127. ==============================================================================
  128. DISPLAY SECTION
  129. ==============================================================================
  130. */
  131. // The menu with the different options in the course management
  132. echo "<div id=\"actions\">\n";
  133. if ($safe['action'] <> 'sortmycourses' AND isset($safe['action']))
  134. {
  135. echo "<a href=\"".api_get_self()."?action=sortmycourses\">".Display::return_icon('deplacer_fichier.gif').' '.get_lang("SortMyCourses")."</a>";
  136. }
  137. else
  138. {
  139. echo '<b>'.Display::return_icon('deplacer_fichier.gif').' '.get_lang('SortMyCourses').'</b>';
  140. }
  141. echo '&nbsp;&nbsp;';
  142. if ($safe['action']<>'createcoursecategory')
  143. {
  144. echo "<a href=\"".api_get_self()."?action=createcoursecategory\">".Display::return_icon('folder_new.gif').' '.get_lang("CreateCourseCategory")."</a>\n";
  145. }
  146. else
  147. {
  148. echo '<b>'.Display::return_icon('folder_new.gif').' '.get_lang('CreateCourseCategory').'</b>';
  149. }
  150. echo '&nbsp;&nbsp;';
  151. if ($safe['action']<>'subscribe')
  152. {
  153. echo "\t\t<a href=\"".api_get_self()."?action=subscribe\">".Display::return_icon('view_more_stats.gif').' '.get_lang("SubscribeToCourse")."</a>\n";
  154. }
  155. else
  156. {
  157. echo '<b>'.Display::return_icon('view_more_stats.gif').' '.get_lang("SubscribeToCourse").'</b>';
  158. }
  159. echo "</div>";
  160. echo "<div>";
  161. switch ($safe['action'])
  162. {
  163. case 'subscribe':
  164. //api_display_tool_title(get_lang('SubscribeToCourse'));
  165. courses_subscribing();
  166. break;
  167. case 'unsubscribe':
  168. //api_display_tool_title(get_lang('UnsubscribeFromCourse'));
  169. $user_courses=get_courses_of_user($_user['user_id']);
  170. display_courses($_user['user_id'], true, $user_courses);
  171. break;
  172. case 'createcoursecategory':
  173. //api_display_tool_title(get_lang('CreateCourseCategory'));
  174. display_create_course_category_form();
  175. break;
  176. case 'deletecoursecategory':
  177. case 'sortmycourses':
  178. default:
  179. //api_display_tool_title(get_lang('SortMyCourses'));
  180. $user_courses=get_courses_of_user($_user['user_id']);
  181. display_courses($_user['user_id'], true, $user_courses);
  182. break;
  183. }
  184. echo '</div>';
  185. Display :: display_footer();
  186. /*
  187. ==============================================================================
  188. FUNCTIONS
  189. ==============================================================================
  190. */
  191. /**
  192. * Subscribe the user to a given course
  193. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  194. * @param string $course_code the code of the course the user wants to subscribe to
  195. * @return string we return the message that is displayed when the action is succesfull
  196. */
  197. function subscribe_user($course_code)
  198. {
  199. global $_user;
  200. $all_course_information = CourseManager::get_course_information($course_code);
  201. if ($all_course_information['registration_code']=='' OR $_POST['course_registration_code']==$all_course_information['registration_code'])
  202. {
  203. if (CourseManager::add_user_to_course($_user['user_id'], $course_code))
  204. {
  205. return get_lang('EnrollToCourseSuccessful');
  206. }
  207. else
  208. {
  209. return get_lang('ErrorContactPlatformAdmin');
  210. }
  211. }
  212. else
  213. {
  214. $return='';
  215. if (isset($_POST['course_registration_code']) AND $_POST['course_registration_code']<>$all_course_information['registration_code'])
  216. {
  217. Display::display_error_message(get_lang('CourseRegistrationCodeIncorrect'));
  218. }
  219. $return.=get_lang('CourseRequiresPassword').'<br/>';
  220. $return.=$all_course_information['visual_code'].' - '.$all_course_information['title'];
  221. $return.="<form action=\"".$_SERVER["REQUEST_URI"]."\" method=\"post\">";
  222. $return.="<input type=\"hidden\" name=\"subscribe\" value=\"".$all_course_information['code']."\" />";
  223. $return.="<input type=\"text\" name=\"course_registration_code\" value=\"".$_POST['course_registration_code']."\" />";
  224. $return.="<input type=\"Submit\" name=\"submit_course_registration_code\" value=\"OK\" alt=\"".get_lang("SubmitRegistrationCode")."\" /></form>";
  225. return $return;
  226. }
  227. }
  228. /**
  229. * unsubscribe the user from a given course
  230. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  231. * @param int $user_id The user id of the user that is performing the unsubscribe action
  232. * @param string $course_code the course code of the course the user wants to unsubscribe from
  233. * @return string we return the message that is displayed when the action is succesfull
  234. */
  235. function remove_user_from_course($user_id, $course_code)
  236. {
  237. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  238. // we check (once again) if the user is not course administrator
  239. // because the course administrator cannot unsubscribe himself
  240. // (s)he can only delete the course
  241. $sql_check="SELECT * FROM $tbl_course_user WHERE user_id='".$user_id."' AND course_code='".$course_code."' AND status='1'";
  242. $result_check=api_sql_query($sql_check);
  243. $number_of_rows=mysql_num_rows($result_check);
  244. if ($number_of_rows>0)
  245. {return false;}
  246. else
  247. {
  248. CourseManager::unsubscribe_user($user_id,$course_code);
  249. return get_lang("YouAreNowUnsubscribed");
  250. }
  251. }
  252. /**
  253. * handles the display of the courses to which the user can subscribe
  254. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  255. */
  256. function courses_subscribing()
  257. {
  258. browse_courses();
  259. display_search_courses();
  260. }
  261. /**
  262. * allows you to browse through the course categories (faculties) and subscribe to the courses of
  263. * this category (faculty)
  264. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  265. */
  266. function browse_courses()
  267. {
  268. browse_course_categories();
  269. if (!isset($_POST['search_course']))
  270. {
  271. browse_courses_in_category();
  272. }
  273. }
  274. /**
  275. * counts the number of courses in a given course category
  276. */
  277. function count_courses_in_category($category)
  278. {
  279. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  280. $sql="SELECT * FROM $tbl_course WHERE category_code".(empty($category)?" IS NULL":"='".$category."'");
  281. $result=api_sql_query($sql);
  282. return mysql_num_rows($result);
  283. }
  284. /**
  285. * displays the browsing of the course categories (faculties)
  286. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  287. * @return HTML code containing a list with all the categories and subcategories and the navigation to go one category up(if needed)
  288. */
  289. function browse_course_categories()
  290. {
  291. $tbl_courses_nodes = Database::get_main_table(TABLE_MAIN_CATEGORY);
  292. $category = Database::escape_string($_GET['category']);
  293. echo "<p><b>".get_lang('CourseCategories')."</b>";
  294. $sql= "SELECT * FROM $tbl_courses_nodes WHERE parent_id ".(empty($category)?"IS NULL":"='".$category."'")." GROUP BY code, parent_id ORDER BY tree_pos ASC";
  295. $result=mysql_query($sql);
  296. echo "<ul>";
  297. while ($row=mysql_fetch_array($result))
  298. {
  299. if ($row['children_count'] > 0 OR count_courses_in_category($row['code'])>0)
  300. {
  301. echo "<li><a href=\"".api_get_self()."?action=subscribe&amp;category=".$row['code']."&amp;up=".Security::remove_XSS($_GET['category'])."\">".$row['name']."</a>".
  302. " (".count_courses_in_category($row['code']).")</li>";
  303. }
  304. elseif ($row['nbChilds'] > 0)
  305. {
  306. echo "<li><a href=\"".api_get_self()."?action=subscribe&amp;category=".$row['code']."&amp;up=".Security::remove_XSS($_GET['category'])."\">".$row['name']."</a></li>";
  307. }
  308. else
  309. {
  310. echo "<li>".$row['name']."</li>";
  311. }
  312. }
  313. echo "</ul>";
  314. if ($_GET['category'])
  315. {
  316. echo "<a href=\"".api_get_self()."?action=subscribe&amp;category=".Security::remove_XSS($_GET['up'])."\">&lt; ".get_lang('UpOneCategory')."</a>";
  317. }
  318. }
  319. /**
  320. * Display all the courses in the given course category. I could have used a parameter here
  321. * but instead I use the already available $_GET['category']
  322. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  323. * @return HTML code: a table with all the courses in a given category (title, code, tutor) and a subscription icon if applicable)
  324. */
  325. function browse_courses_in_category()
  326. {
  327. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  328. $category = Database::escape_string($_GET['category']);
  329. echo "<p><b>".get_lang('CoursesInCategory')."</b>";
  330. $my_category = (empty($category)?" IS NULL":"='".$category."'");
  331. $sql="SELECT * FROM $tbl_course WHERE category_code".$my_category;
  332. $result=api_sql_query($sql);
  333. while ($row=mysql_fetch_array($result))
  334. {
  335. if ($row['registration_code']=='')
  336. {
  337. $registration_code=false;
  338. }
  339. else
  340. {
  341. $registration_code=true;
  342. }
  343. $courses[]=array("code" => $row['code'], "directory" => $row['directory'], "db"=> $row['db_name'], "visual_code" => $row['visual_code'], "title" => $row['title'], "tutor" => $row['tutor_name'], "subscribe" => $row['subscribe'], "unsubscribe" => $row['unsubscribe'], 'registration_code'=> $registration_code);
  344. }
  345. display_subscribe_to_courses($courses);
  346. }
  347. /**
  348. * displays the form for searching for a course and the results if a query has been submitted.
  349. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  350. * @return HTML code: the form for searching for a course
  351. */
  352. function display_search_courses()
  353. {
  354. global $_user;
  355. echo "<p><b>".get_lang("SearchCourse")."</b><br />";
  356. echo "<form class=\"course_list\" method=\"post\" action=\"".api_get_self()."?action=subscribe\">",
  357. "<input type=\"hidden\" name=\"search_course\" value=\"1\" />",
  358. "<input type=\"text\" name=\"search_term\" />",
  359. "&nbsp;<input type=\"submit\" value=\"",get_lang("_search"),"\" />",
  360. "</form>";
  361. if (isset($_POST['search_course']))
  362. {
  363. echo "<p><b>".get_lang("SearchResultsFor")." ".htmlentities($_POST['search_term'])."</b><br />";
  364. $result_search_courses_array=search_courses($_POST['search_term']);
  365. display_subscribe_to_courses($result_search_courses_array);
  366. }
  367. }
  368. /**
  369. * This function displays the list of course that can be subscribed to.
  370. * This list can come from the search results or from the browsing of the platform course categories
  371. */
  372. function display_subscribe_to_courses($courses)
  373. {
  374. global $_user;
  375. // getting all the courses to which the user is subscribed to
  376. $user_courses=get_courses_of_user($_user['user_id']);
  377. $user_coursecodes=array();
  378. // we need only the course codes as these will be used to match against the courses of the category
  379. if ($user_courses<>"")
  380. {
  381. foreach ($user_courses as $key=>$value)
  382. {
  383. $user_coursecodes[]=$value['code'];
  384. }
  385. }
  386. if ($courses==0)
  387. {
  388. return false;
  389. }
  390. echo "<table cellpadding=\"4\">\n";
  391. foreach ($courses as $key=>$course)
  392. {
  393. // displaying the course title, visual code and teacher/teaching staff
  394. echo "\t<tr>\n";
  395. echo "\t\t<td>\n";
  396. echo "<b>".$course['title']."</b><br />";
  397. if (get_setting("display_coursecode_in_courselist") == "true")
  398. {
  399. echo $course['visual_code'];
  400. }
  401. if (get_setting("display_coursecode_in_courselist") == "true" AND get_setting("display_teacher_in_courselist") == "true")
  402. {
  403. echo " - ";
  404. }
  405. if (get_setting("display_teacher_in_courselist") == "true")
  406. {
  407. echo $course['tutor'];
  408. }
  409. echo "\t\t</td>\n";
  410. echo "\t\t<td>\n";
  411. if ($course['registration_code'])
  412. {
  413. Display::display_icon('passwordprotected.png','',array('style'=>'float:left;'));
  414. }
  415. display_subscribe_icon($course, $user_coursecodes);
  416. echo "\t\t</td>\n";
  417. echo "</tr>";
  418. }
  419. echo "</table>";
  420. }
  421. /**
  422. * This function filters dangerous stuff out. It does addslashes when the php.ini setting
  423. * magic_quotes is set to off.
  424. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  425. * @param string something that possibly needs addslashing
  426. * @return string the addslahed version of the input
  427. */
  428. function escape($s)
  429. {
  430. if(!get_magic_quotes_gpc())
  431. return addslashes($s);
  432. else return $s;
  433. }
  434. /**
  435. * Search the courses database for a course that matches the search term.
  436. * The search is done on the code, title and tutor field of the course table.
  437. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  438. * @param string $search_term: the string that the user submitted, what we are looking for
  439. * @return array an array containing a list of all the courses (the code, directory, dabase, visual_code, title, ... )
  440. * matching the the search term.
  441. */
  442. function search_courses($search_term)
  443. {
  444. $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
  445. $search_term_safe=escape($search_term);
  446. $sql_find="SELECT * FROM $TABLECOURS WHERE code LIKE '%".$search_term_safe."%' OR title LIKE '%".$search_term_safe."%' OR tutor_name LIKE '%".$search_term_safe."%'";
  447. $result_find=mysql_query($sql_find) or die(mysql_error());
  448. while ($row=mysql_fetch_array($result_find))
  449. {
  450. $courses[]=array("code" => $row['code'], "directory" => $row['directory'], "db"=> $row['db_name'], "visual_code" => $row['visual_code'], "title" => $row['title'], "tutor" => $row['tutor_name'], "subscribe" => $row['subscribe'], "unsubscribe" => $row['unsubscribe']);
  451. }
  452. return $courses;
  453. }
  454. /**
  455. * deletes a course category and moves all the courses that were in this category to main category
  456. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  457. * @param int $id: the id of the user_course_category
  458. * @return string a language variable saying that the deletion went OK.
  459. */
  460. function delete_course_category($id)
  461. {
  462. global $_user, $_configuration;
  463. $DATABASE_USER_TOOLS = $_configuration['user_personal_database'];
  464. $TABLE_USER_COURSE_CATEGORY = $DATABASE_USER_TOOLS."`.`user_course_category";
  465. $TABLECOURSUSER=Database::get_main_table(TABLE_MAIN_COURSE_USER);
  466. $id = intval($id);
  467. $sql_delete="DELETE FROM `$TABLE_USER_COURSE_CATEGORY` WHERE id='".$id."' and user_id='".$_user['user_id']."'";
  468. $sql_update="UPDATE $TABLECOURSUSER SET user_course_cat='0' WHERE user_course_cat='".$id."' AND user_id='".$_user['user_id']."'";
  469. mysql_query($sql_delete) or die(mysql_error());
  470. mysql_query($sql_update) or die(mysql_error());
  471. return get_lang("CourseCategoryDeleted");
  472. }
  473. /**
  474. * stores the user course category in the dokeos_user database
  475. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  476. * @return string a language variable saying that the user course category was stored
  477. */
  478. function store_course_category()
  479. {
  480. global $_user, $_configuration;
  481. $DATABASE_USER_TOOLS = $_configuration['user_personal_database'];
  482. $TABLE_USER_COURSE_CATEGORY = $DATABASE_USER_TOOLS."`.`user_course_category";
  483. // step 1: we determine the max value of the user defined course categories
  484. $sql="SELECT sort FROM `$TABLE_USER_COURSE_CATEGORY` WHERE user_id='".$_user['user_id']."' ORDER BY sort DESC";
  485. $result=api_sql_query($sql);
  486. $maxsort=mysql_fetch_array($result);
  487. $nextsort=$maxsort['sort']+1;
  488. // step 2: we check if there is already a category with this name, if not we store it, else we give an error.
  489. $sql="SELECT * FROM `$TABLE_USER_COURSE_CATEGORY` WHERE user_id='".$_user['user_id']."' AND title='".Database::escape_string($_POST['title_course_category'])."'ORDER BY sort DESC";
  490. $result=api_sql_query($sql);
  491. if (mysql_numrows($result) == 0)
  492. {
  493. $sql_insert="INSERT INTO `$TABLE_USER_COURSE_CATEGORY` (user_id, title,sort) VALUES ('".$_user['user_id']."', '".htmlentities($_POST['title_course_category'])."', '".$nextsort."')";
  494. api_sql_query($sql_insert);
  495. Display::display_confirmation_message(get_lang("CourseCategoryStored"));
  496. }
  497. else
  498. {
  499. Display::display_error_message(get_lang('ACourseCategoryWithThisNameAlreadyExists'));
  500. }
  501. }
  502. /**
  503. * displays the form that is needed to create a course category.
  504. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  505. * @return HTML the form (input field + submit button) to create a user course category
  506. */
  507. function display_create_course_category_form()
  508. {
  509. global $_user, $_configuration;
  510. echo "<form name=\"create_course_category\" method=\"post\" action=\"".api_get_self()."?action=sortmycourses\">\n";
  511. echo "<input type=\"text\" name=\"title_course_category\" />\n";
  512. echo "<input type=\"submit\" name=\"create_course_category\" value=\"".get_lang("Ok")."\" />\n";
  513. echo "</form>\n";
  514. echo get_lang("ExistingCourseCategories");
  515. $DATABASE_USER_TOOLS = $_configuration['user_personal_database'];
  516. $TABLE_USER_COURSE_CATEGORY = $DATABASE_USER_TOOLS."`.`user_course_category";
  517. $sql="SELECT * FROM `$TABLE_USER_COURSE_CATEGORY` WHERE user_id='".$_user['user_id']."'";
  518. $result=api_sql_query($sql, __LINE__, __FILE__);
  519. if (mysql_num_rows($result)>0)
  520. {
  521. echo "<ul>\n";
  522. while ($row=mysql_fetch_array($result))
  523. {
  524. echo "\t<li>".$row['title']."</li>\n";
  525. }
  526. echo "</ul>\n";
  527. }
  528. }
  529. // ***************************************************************************
  530. // this function stores the changes in a course category
  531. //
  532. // ***************************************************************************
  533. /**
  534. * stores the changes in a course category (moving a course to a different course category)
  535. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  536. * @param string $course_code : the course_code of the course we are moving
  537. * int $newcategory : the id of the user course category we are moving the course to.
  538. * @return string a language variable saying that the course was moved.
  539. */
  540. function store_changecoursecategory($course_code, $newcategory)
  541. {
  542. global $_user;
  543. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  544. $max_sort_value=api_max_sort_value($newcategory,$_user['user_id']); //max_sort_value($newcategory);
  545. $new_category = htmlentities($newcategory);
  546. $sql="UPDATE $TABLECOURSUSER SET user_course_cat='".$newcategory."', sort='".($max_sort_value+1)."' WHERE course_code='".$course_code."' AND user_id='".$_user['user_id']."'";
  547. $result=api_sql_query($sql);
  548. return get_lang("EditCourseCategorySucces");
  549. }
  550. /**
  551. * moves the course one place up or down
  552. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  553. * @param string $direction : the direction we are moving the course to (up or down)
  554. * string $course2move : the course we are moving
  555. * @return string a language variable saying that the course was moved.
  556. */
  557. function move_course($direction, $course2move, $category)
  558. {
  559. global $_user;
  560. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  561. $all_user_courses=get_courses_of_user($_user['user_id']);
  562. // we need only the courses of the category we are moving in
  563. foreach ($all_user_courses as $key=>$course)
  564. {
  565. if ($course['user_course_category']==$category)
  566. {
  567. $user_courses[]=$course;
  568. }
  569. }
  570. foreach ($user_courses as $key=>$course)
  571. {
  572. if ($course2move==$course['code'])
  573. {
  574. // source_course is the course where we clicked the up or down icon
  575. $source_course=$course;
  576. // target_course is the course before/after the source_course (depending on the up/down icon)
  577. if ($direction=="up")
  578. {$target_course=$user_courses[$key-1];}
  579. else
  580. {$target_course=$user_courses[$key+1];}
  581. } // if ($course2move==$course['code'])
  582. }
  583. $sql_update1="UPDATE $TABLECOURSUSER SET sort='".$target_course['sort']."' WHERE course_code='".$source_course['code']."' AND user_id='".$_user['user_id']."'";
  584. $sql_update2="UPDATE $TABLECOURSUSER SET sort='".$source_course['sort']."' WHERE course_code='".$target_course['code']."' AND user_id='".$_user['user_id']."'";
  585. mysql_query($sql_update2);
  586. mysql_query($sql_update1);
  587. return get_lang("CourseSortingDone");
  588. }
  589. /**
  590. * moves the course one place up or down
  591. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  592. * @param string $direction : the direction we are moving the course to (up or down)
  593. * string $course2move : the course we are moving
  594. * @return string a language variable saying that the course was moved.
  595. */
  596. function move_category($direction, $category2move)
  597. {
  598. global $_user;
  599. // the database definition of the table that stores the user defined course categories
  600. $table_user_defined_category = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  601. $user_coursecategories=get_user_course_categories();
  602. foreach ($user_coursecategories as $key=>$categoryid)
  603. {
  604. if ($category2move==$categoryid)
  605. {
  606. // source_course is the course where we clicked the up or down icon
  607. $source_category=get_user_course_category($category2move);
  608. // target_course is the course before/after the source_course (depending on the up/down icon)
  609. if ($direction=="up")
  610. {$target_category=get_user_course_category($user_coursecategories[$key-1]);}
  611. else
  612. {$target_category=get_user_course_category($user_coursecategories[$key+1]);}
  613. } // if ($course2move==$course['code'])
  614. } // foreach ($user_courses as $key=>$course)
  615. $sql_update1="UPDATE $table_user_defined_category SET sort='".$target_category['sort']."' WHERE id='".$source_category['id']."' AND user_id='".$_user['user_id']."'";
  616. $sql_update2="UPDATE $table_user_defined_category SET sort='".$source_category['sort']."' WHERE id='".$target_category['id']."' AND user_id='".$_user['user_id']."'";
  617. mysql_query($sql_update2);
  618. mysql_query($sql_update1);
  619. return get_lang("CategorySortingDone");
  620. }
  621. /**
  622. * displays everything that is needed when the user wants to manage his current courses (sorting, subscribing, unsubscribing, ...)
  623. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  624. * @param int $user_id: the user_id of the current user
  625. * string $parameter: determines weither we are displaying for the sorting, subscribing or unsubscribin
  626. array $user_courses: the courses to which the user is subscribed
  627. * @return html a table containing courses and the appropriate icons (sub/unsub/move)
  628. */
  629. function display_courses($user_id, $show_course_icons, $user_courses)
  630. {
  631. global $_user, $_configuration;
  632. echo "<table cellpadding=\"4\">\n";
  633. // building an array that contains all the id's of the user defined course categories
  634. // initially this was inside the display_courses_in_category function but when we do it here we have fewer
  635. // sql executions = performance increase.
  636. $all_user_categories=get_user_course_categories();
  637. // step 0: we display the course without a user category
  638. display_courses_in_category(0,'true');
  639. // Step 1: we get all the categories of the user
  640. $DATABASE_USER_TOOLS = $_configuration['user_personal_database'];
  641. $TABLE_USER_COURSE_CATEGORY = $DATABASE_USER_TOOLS."`.`user_course_category";
  642. $sql="SELECT * FROM `$TABLE_USER_COURSE_CATEGORY` WHERE user_id='".$_user['user_id']."' ORDER BY sort ASC";
  643. $result=api_sql_query($sql);
  644. while ($row=mysql_fetch_array($result))
  645. {
  646. if ($show_course_icons=true)
  647. {
  648. // the edit link is clicked: we display the edit form for the category
  649. if (isset($_GET['categoryid']) AND $_GET['categoryid']==$row['id'])
  650. {
  651. echo "<tr><td colspan=\"2\" class=\"user_course_category\">";
  652. echo '<a name="category'.$row['id'].'"></a>'; // display an internal anchor.
  653. display_edit_course_category_form($row['id']);
  654. }
  655. // we simply display the title of the catgory
  656. else
  657. {
  658. $info_this_user_course_category=get_user_course_category($row['id']);
  659. echo "<tr><td colspan=\"2\" class=\"user_course_category\">";
  660. echo '<a name="category'.$row['id'].'"></a>'; // display an internal anchor.
  661. echo $info_this_user_course_category['title'];
  662. }
  663. echo "</td><td class=\"user_course_category\">";
  664. display_category_icons($row['id'],$all_user_categories);
  665. echo "</td></tr>";
  666. }
  667. // Step 2: show the courses inside this category
  668. display_courses_in_category($row['id'], $show_course_icons);
  669. }
  670. echo "</table>\n";
  671. }
  672. /**
  673. * This function displays all the courses in the particular user category;
  674. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  675. * @param int id: the id of the user defined course category
  676. * @return string: the name of the user defined course category
  677. */
  678. function display_courses_in_category($user_category_id, $showicons)
  679. {
  680. global $_user;
  681. // table definitions
  682. $TABLECOURS=Database::get_main_table(TABLE_MAIN_COURSE);
  683. $TABLECOURSUSER=Database::get_main_table(TABLE_MAIN_COURSE_USER);
  684. $TABLE_USER_COURSE_CATEGORY = "`".Database::get_user_personal_database()."`.`user_course_category`";
  685. $sql_select_courses="SELECT course.code, course.visual_code, course.subscribe subscr, course.unsubscribe unsubscr,
  686. course.title title, course.tutor_name tutor, course.db_name, course.directory, course_rel_user.status status,
  687. course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
  688. FROM $TABLECOURS course,
  689. $TABLECOURSUSER course_rel_user
  690. WHERE course.code = course_rel_user.course_code
  691. AND course_rel_user.user_id = '".$_user['user_id']."'
  692. AND course_rel_user.user_course_cat='".$user_category_id."'
  693. ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC";
  694. $result = api_sql_query($sql_select_courses) or die(mysql_error());
  695. $number_of_courses=mysql_num_rows($result);
  696. $key=0;
  697. while ($course=mysql_fetch_array($result))
  698. {
  699. echo "\t<tr>\n";
  700. echo "\t\t<td>\n";
  701. echo '<a name="course'.$course['code'].'"></a>'; // display an internal anchor.
  702. echo "<b>".$course['title']."</b><br />";
  703. if (get_setting("display_coursecode_in_courselist") == "true")
  704. {
  705. echo $course['visual_code'];
  706. }
  707. if (get_setting("display_coursecode_in_courselist") == "true" AND get_setting("display_teacher_in_courselist") == "true")
  708. {
  709. echo " - ";
  710. }
  711. if (get_setting("display_teacher_in_courselist") == "true")
  712. {
  713. echo $course['tutor'];
  714. }
  715. echo "\t\t</td>\n";
  716. // displaying the up/down/edit icons when we are sorting courses
  717. echo "\t\t<td valign=\"top\">\n";
  718. //if ($parameter=="sorting")
  719. //{
  720. display_course_icons($key, $number_of_courses, $course, $user_courses);
  721. //}
  722. // displaying the delete icon when we are unsubscribing from courses
  723. //if($parameter=="deleting")
  724. //{
  725. // display_unsubscribe_icons($course);
  726. //}
  727. // display the subscribing icon when we are to courses.
  728. //if ($parameter=="subscribing")
  729. //{
  730. // display_subscribe_icon($course);
  731. //}
  732. echo "\t\t</td>\n";
  733. echo "\t</tr>\n";
  734. $key++;
  735. }
  736. }
  737. /**
  738. * gets the title of the user course category
  739. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  740. * @param int id: the id of the user defined course category
  741. * @return string: the name of the user defined course category
  742. */
  743. function get_user_course_category($id)
  744. {
  745. global $_user, $_configuration;
  746. $DATABASE_USER_TOOLS = $_configuration['user_personal_database'];
  747. $TABLE_USER_COURSE_CATEGORY = $DATABASE_USER_TOOLS."`.`user_course_category";
  748. $id = intval($id);
  749. $sql="SELECT * FROM `".$TABLE_USER_COURSE_CATEGORY."` WHERE user_id='".$_user['user_id']."' AND id='$id'";
  750. $result=mysql_query($sql) or die(mysql_error());
  751. $row=mysql_fetch_array($result);
  752. return $row;
  753. }
  754. /**
  755. * displays the subscribe icon if the subscribing is allowed and if the user is not yet
  756. * subscribe to this course
  757. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  758. * @param string $current_course: the course code of the course we need to display the subscribe icon for
  759. * @return string a subscribe icon or the text that subscribing is not allowed or the user is already subscribed
  760. */
  761. function display_subscribe_icon($current_course, $user_coursecodes)
  762. {
  763. // we display the icon to subscribe or the text already subscribed
  764. if (in_array($current_course['code'],$user_coursecodes))
  765. {
  766. echo get_lang("AlreadySubscribed");
  767. }
  768. else
  769. {
  770. if ($current_course['subscribe'] == SUBSCRIBE_ALLOWED)
  771. {
  772. echo "<form action=\"".$_SERVER["REQUEST_URI"]."\" method=\"post\">";
  773. echo "<input type=\"hidden\" name=\"subscribe\" value=\"".$current_course['code']."\" />";
  774. echo "<input type=\"image\" name=\"unsub\" src=\"../img/enroll.gif\" alt=\"".get_lang("Subscribe")."\" />".get_lang("Subscribe")."</form>";
  775. }
  776. else
  777. {
  778. echo get_lang("SubscribingNotAllowed");
  779. }
  780. }
  781. }
  782. /**
  783. * displays the subscribe icon if the subscribing is allowed and if the user is not yet
  784. * subscribed to this course
  785. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  786. * @param $key:
  787. * $number_of_courses
  788. * $course
  789. * $user_courses
  790. * @return html a small table containing the up/down icons and the edit icon (for moving to a different user course category)
  791. * @todo complete the comments on this function: the parameter section
  792. */
  793. function display_course_icons($key, $number_of_courses, $course, $user_courses)
  794. {
  795. //print_r($course);
  796. global $safe;
  797. echo "<table><tr><td>";
  798. // the up icon
  799. if ($key>0 AND $user_courses[$key-1]['user_course_category']==$course['user_course_category'])
  800. {
  801. echo "<a href=\"courses.php?action=".$safe['action']."&amp;move=up&amp;course=".$course['code']."&amp;category=".$course['user_course_cat']."\">";
  802. Display::display_icon('up.gif', get_lang('Up'));
  803. echo '</a>';
  804. }
  805. echo "</td>";
  806. // the edit icon OR the edit dropdown list
  807. if (isset($_GET['edit']) and $course['code']==$_GET['edit'])
  808. {
  809. echo "<td rowspan=\"2\" valign=\"top\">".display_change_course_category_form($_GET['edit'])."</td>";
  810. }
  811. else
  812. {
  813. echo "<td rowspan=\"2\" valign=\"middle\"><a href=\"courses.php?action=".$safe['action']."&amp;edit=".$course['code']."\">";
  814. Display::display_icon('edit.gif',get_lang('Edit'));
  815. echo "</a></td>";
  816. }
  817. echo "<td rowspan=\"2\" valign=\"top\" class=\"invisible\">";
  818. if ($course['status'] != 1)
  819. {
  820. if ($course['unsubscr'] == 1)
  821. { // changed link to submit to avoid action by the search tool indexer
  822. echo "<form action=\"".api_get_self()."\" method=\"post\" onsubmit=\"javascript:if(!confirm('".addslashes(htmlentities(get_lang("ConfirmUnsubscribeFromCourse")))."')) return false;\">";
  823. echo "<input type=\"hidden\" name=\"unsubscribe\" value=\"".$course['code']."\" />";
  824. echo "<input type=\"image\" name=\"unsub\" src=\"../img/delete.gif\" alt=\"".get_lang("_unsubscribe")."\" /></form>";
  825. }
  826. else
  827. {display_info_text(get_lang("UnsubscribeNotAllowed"));}
  828. }
  829. else
  830. {
  831. display_info_text(get_lang("CourseAdminUnsubscribeNotAllowed"));
  832. }
  833. echo "</td>";
  834. echo "</tr><tr><td>";
  835. if ($key<$number_of_courses-1 AND $user_courses[$key+1]['user_course_category']==$course['user_course_category'])
  836. {
  837. echo "<a href=\"courses.php?action=".$safe['action']."&amp;move=down&amp;course=".$course['code']."&amp;category=".$course['user_course_cat']."\">";
  838. Display::display_icon('down.gif', get_lang('Down'));
  839. echo '</a>';
  840. }
  841. echo "</td></tr></table>";
  842. }
  843. /**
  844. * displays the relevant icons for the category (if applicable):move up, move down, edit, delete
  845. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  846. * @param $current_category the id of the current category
  847. * $allcategories an associative array containing all the categories.
  848. * @return html: a small table containing the up/down icons and the edit icon (for moving to a different user course category)
  849. * @todo complete the comments on this function: the parameter section
  850. */
  851. function display_category_icons($current_category, $all_user_categories)
  852. {
  853. global $safe;
  854. $max_category_key=count($all_user_categories);
  855. if ($safe['action']<>'unsubscribe') // we are in the unsubscribe section then we do not show the icons.
  856. {
  857. echo "<table>";
  858. echo "<tr>";
  859. echo "<td>";
  860. if ($current_category<>$all_user_categories[0])
  861. {
  862. echo "<a href=\"courses.php?action=".$safe['action']."&amp;move=up&amp;category=".$current_category."\">";
  863. echo "<img src=\"../img/up.gif\" alt=\"".htmlentities(get_lang("Up"))."\"></a>";
  864. }
  865. echo "</td>";
  866. echo " <td rowspan=\"2\">";
  867. echo " <a href=\"courses.php?action=sortmycourses&amp;categoryid=".$current_category."#category".$current_category."\">";
  868. Display::display_icon('edit.gif',get_lang('Edit'));
  869. echo "</a>";
  870. echo "</td>";
  871. echo "<td rowspan=\"2\">";
  872. echo " <a href=\"courses.php?action=deletecoursecategory&amp;id=".$current_category."\">";
  873. Display::display_icon('delete.gif',get_lang('Edit'),array('onclick'=>"javascript:if(!confirm('".addslashes(htmlentities(get_lang("CourseCategoryAbout2bedeleted")))."')) return false;"));
  874. echo "</a>";
  875. echo "</td>";
  876. echo "</tr>";
  877. echo "<tr>";
  878. echo " <td>";
  879. if ($current_category<>$all_user_categories[$max_category_key-1])
  880. {
  881. echo "<a href=\"courses.php?action=".$safe['action']."&amp;move=down&amp;category=".$current_category."\">";
  882. echo "<img src=\"../img/down.gif\" alt=\"".htmlentities(get_lang("Down"))."\"></a>";
  883. }
  884. echo "</td>";
  885. echo " </tr>";
  886. echo "</table>";
  887. }
  888. }
  889. /**
  890. * This function displays the form (dropdown list) to move a course to a
  891. * different course_category (after the edit icon has been changed)
  892. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  893. * @param string $edit_course:
  894. * @return html a dropdown list containing all the user defined course categories and a submit button
  895. * @todo when editing (moving) a course inside a user defined course category to a different user defined category
  896. * the dropdown list should have the current course category selected.
  897. */
  898. function display_change_course_category_form($edit_course)
  899. {
  900. global $_user, $_configuration, $safe;
  901. $edit_course = Security::remove_XSS($edit_course);
  902. $DATABASE_USER_TOOLS = $_configuration['user_personal_database'];
  903. $TABLE_USER_COURSE_CATEGORY = $DATABASE_USER_TOOLS."`.`user_course_category";
  904. $sql="SELECT * FROM `$TABLE_USER_COURSE_CATEGORY` WHERE user_id='".$_user['user_id']."'";
  905. $result=api_sql_query($sql);
  906. $output="<form name=\"edit_course_category\" method=\"post\" action=\"courses.php?action=".$safe['action']."\">\n";
  907. $output.="<input type=\"hidden\" name=\"course_2_edit_category\" value=\"".$edit_course."\" />";
  908. $output.="\t<select name=\"course_categories\">\n";
  909. $output.="\t\t<option value=\"0\">".get_lang("NoCourseCategory")."</option>";
  910. while ($row=mysql_fetch_array($result))
  911. {$output.="\t\t<option value=\"".$row['id']."\">".$row['title']."</option>";}
  912. $output.="\t</select>\n";
  913. $output.="\t<input type=\"submit\" name=\"submit_change_course_category\" value=\"".get_lang("Ok")."\" />\n";
  914. $output.="</form>";
  915. return $output;
  916. }
  917. /**
  918. * This function displays the unsubscribe part which can be
  919. * 1. the unsubscribe link
  920. * 2. text: you are course admin of this course (=> unsubscription is not possible
  921. * 3. text: you are not allowed to unsubscribe from this course
  922. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  923. * @param array $course: the array with all the course & course_rel_user information
  924. * @return html a delete icon or a text that unsubscribing is not possible (course admin) or not allowed.
  925. */
  926. function display_unsubscribe_icons($course)
  927. {
  928. if ($course['status'] != 1)
  929. {
  930. if ($course['unsubscribe'] == 1)
  931. { // changed link to submit to avoid action by the search tool indexer
  932. echo "<form action=\"".api_get_self()."\" method=\"post\" onsubmit=\"javascript:if(!confirm('".addslashes(htmlentities(get_lang("ConfirmUnsubscribeFromCourse")))."')) return false;\">";
  933. echo "<input type=\"hidden\" name=\"unsubscribe\" value=\"".$course['code']."\" />";
  934. echo "<input type=\"image\" name=\"unsub\" src=\"../img/delete.gif\" alt=\"".get_lang("_unsubscribe")."\" /></form>";
  935. }
  936. else
  937. {display_info_text(get_lang("UnsubscribeNotAllowed"));}
  938. }
  939. else
  940. {
  941. display_info_text(get_lang("CourseAdminUnsubscribeNotAllowed"));
  942. }
  943. }
  944. /**
  945. * retrieves all the courses that the user has already subscribed to
  946. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  947. * @param int $user_id: the id of the user
  948. * @return array an array containing all the information of the courses of the given user
  949. */
  950. function get_courses_of_user($user_id)
  951. {
  952. $TABLECOURS=Database::get_main_table(TABLE_MAIN_COURSE);
  953. $TABLECOURSUSER=Database::get_main_table(TABLE_MAIN_COURSE_USER);
  954. // Secondly we select the courses that are in a category (user_course_cat<>0) and sort these according to the sort of the category
  955. $user_id = intval($user_id);
  956. $sql_select_courses="SELECT course.code k, course.visual_code vc, course.subscribe subscr, course.unsubscribe unsubscr,
  957. course.title i, course.tutor_name t, course.db_name db, course.directory dir, course_rel_user.status status,
  958. course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
  959. FROM $TABLECOURS course,
  960. $TABLECOURSUSER course_rel_user
  961. WHERE course.code = course_rel_user.course_code
  962. AND course_rel_user.user_id = '".$user_id."'
  963. ORDER BY course_rel_user.sort ASC";
  964. $result = api_sql_query($sql_select_courses) or die(mysql_error());
  965. while ($row=mysql_fetch_array($result))
  966. {
  967. // we only need the database name of the course
  968. $courses[]=array("db"=> $row['db'], "code" => $row['k'], "visual_code" => $row['vc'], "title" => $row['i'], "directory" => $row['dir'], "status" => $row['status'], "tutor" => $row['t'], "subscribe" => $row['subscr'], "unsubscribe" => $row['unsubscr'], "sort" => $row['sort'], "user_course_category" => $row['user_course_cat']);
  969. }
  970. return $courses;
  971. }
  972. /**
  973. * retrieves the user defined course categories
  974. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  975. * @return array containing all the titles of the user defined courses with the id as key of the array
  976. */
  977. function get_user_course_categories()
  978. {
  979. global $_user;
  980. $table_category = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  981. $sql = "SELECT * FROM ".$table_category." WHERE user_id='".$_user['user_id']."' ORDER BY sort ASC";
  982. $result = api_sql_query($sql,__FILE__,__LINE__);
  983. while ($row = mysql_fetch_array($result))
  984. {
  985. $output[] = $row['id'];
  986. }
  987. return $output;
  988. }
  989. /**
  990. * @author unknown
  991. * @param string $text: the text that has to be written in grey
  992. * @return string: the text with the grey formatting
  993. * @todo move this to a stylesheet
  994. * Added id grey to CSS
  995. */
  996. function display_info_text($text)
  997. {
  998. //echo "<font color=\"#808080\">" . $text . "</font>\n";
  999. echo $text;
  1000. }
  1001. /**
  1002. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1003. * @param string $edit_course:
  1004. * @return html output: the form
  1005. */
  1006. function display_edit_course_category_form($edit_course_category)
  1007. {
  1008. global $safe;
  1009. echo "<form name=\"edit_course_category\" method=\"post\" action=\"courses.php?action=".$safe['action']."\">\n";
  1010. echo "\t<input type=\"hidden\" name=\"edit_course_category\" value=\"".$edit_course_category."\" />\n";
  1011. $info_this_user_course_category=get_user_course_category($edit_course_category);
  1012. echo "\t<input type=\"text\" name=\"title_course_category\" value=\"".$info_this_user_course_category['title']."\" />";
  1013. echo "\t<input type=\"submit\" name=\"submit_edit_course_category\" value=\"".get_lang("Ok")."\" />\n";
  1014. echo "</form>";
  1015. }
  1016. /**
  1017. * Updates the user course category in the dokeos_user database
  1018. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1019. * @return string a language variable saying that the user course category was stored
  1020. */
  1021. function store_edit_course_category()
  1022. {
  1023. global $_user, $_configuration;
  1024. $DATABASE_USER_TOOLS = $_configuration['user_personal_database'];
  1025. $TABLE_USER_COURSE_CATEGORY = $DATABASE_USER_TOOLS."`.`user_course_category";
  1026. $sql_update="UPDATE `$TABLE_USER_COURSE_CATEGORY` SET title='".htmlentities($_POST['title_course_category'])."' WHERE id='".(int)$_POST['edit_course_category']."'";
  1027. mysql_query($sql_update) or die(mysql_error());
  1028. //api_sql_query(sql_update);
  1029. return get_lang("CourseCategoryEditStored");
  1030. }
  1031. ?>