courses.php 50 KB

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