courses.php 48 KB

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