courses.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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. display_search_courses();
  184. $user_courses = get_courses_of_user($_user['user_id']);
  185. display_courses($_user['user_id'], true, $user_courses);
  186. break;
  187. }
  188. echo '</div>';
  189. Display :: display_footer();
  190. /*
  191. ==============================================================================
  192. FUNCTIONS
  193. ==============================================================================
  194. */
  195. /**
  196. * Subscribe the user to a given course
  197. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  198. * @param string $course_code the code of the course the user wants to subscribe to
  199. * @return string we return the message that is displayed when the action is succesfull
  200. */
  201. function subscribe_user($course_code) {
  202. global $_user, $stok;
  203. $all_course_information = CourseManager::get_course_information($course_code);
  204. if ($all_course_information['registration_code'] == '' || $_POST['course_registration_code'] == $all_course_information['registration_code']) {
  205. if (api_is_platform_admin()) {
  206. $status_user_in_new_course = COURSEMANAGER;
  207. } else {
  208. $status_user_in_new_course=null;
  209. }
  210. if (CourseManager::add_user_to_course($_user['user_id'], $course_code, $status_user_in_new_course)) {
  211. $send = api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $course_code);
  212. if ($send == 1) {
  213. CourseManager::email_to_tutor($_user['user_id'], $course_code, $send_to_tutor_also = false);
  214. } else if ($send == 2){
  215. CourseManager::email_to_tutor($_user['user_id'], $course_code, $send_to_tutor_also = true);
  216. }
  217. return get_lang('EnrollToCourseSuccessful');
  218. } else {
  219. return get_lang('ErrorContactPlatformAdmin');
  220. }
  221. } else {
  222. $return = '';
  223. if (isset($_POST['course_registration_code']) && $_POST['course_registration_code'] != $all_course_information['registration_code']) {
  224. Display::display_error_message(get_lang('CourseRegistrationCodeIncorrect'));
  225. }
  226. $return .= get_lang('CourseRequiresPassword').'<br />';
  227. $return .= $all_course_information['visual_code'].' - '.$all_course_information['title'];
  228. $return .= "<form action=\"".$_SERVER["REQUEST_URI"]."\" method=\"post\">";
  229. $return .= '<input type="hidden" name="sec_token" value="'.$stok.'" />';
  230. $return .= "<input type=\"hidden\" name=\"subscribe\" value=\"".$all_course_information['code']."\" />";
  231. $return .= "<input type=\"text\" name=\"course_registration_code\" value=\"".$_POST['course_registration_code']."\" />";
  232. $return .= "<input type=\"submit\" name=\"submit_course_registration_code\" value=\"OK\" alt=\"".get_lang('SubmitRegistrationCode')."\" /></form>";
  233. return $return;
  234. }
  235. }
  236. /**
  237. * unsubscribe the user from a given course
  238. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  239. * @param int $user_id The user id of the user that is performing the unsubscribe action
  240. * @param string $course_code the course code of the course the user wants to unsubscribe from
  241. * @return string we return the message that is displayed when the action is succesfull
  242. */
  243. function remove_user_from_course($user_id, $course_code) {
  244. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  245. // we check (once again) if the user is not course administrator
  246. // because the course administrator cannot unsubscribe himself
  247. // (s)he can only delete the course
  248. $sql_check = "SELECT * FROM $tbl_course_user WHERE user_id='".$user_id."' AND course_code='".$course_code."' AND status='1'";
  249. $result_check = Database::query($sql_check, __FILE__, __LINE__);
  250. $number_of_rows = Database::num_rows($result_check);
  251. if ($number_of_rows > 0) {
  252. return false;
  253. }
  254. CourseManager::unsubscribe_user($user_id, $course_code);
  255. return get_lang('YouAreNowUnsubscribed');
  256. }
  257. /**
  258. * handles the display of the courses to which the user can subscribe
  259. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  260. */
  261. function courses_subscribing() {
  262. browse_courses();
  263. display_search_courses();
  264. }
  265. /**
  266. * Allows you to browse through the course categories (faculties) and subscribe to the courses of
  267. * this category (faculty)
  268. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  269. */
  270. function browse_courses() {
  271. browse_course_categories();
  272. if (!isset($_POST['search_course'])) {
  273. browse_courses_in_category();
  274. }
  275. }
  276. /**
  277. * Counts the number of courses in a given course category
  278. */
  279. function count_courses_in_category($category) {
  280. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  281. $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD);
  282. $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  283. // get course list auto-register
  284. $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON " .
  285. " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  286. $special_course_result = Database::query($sql, __FILE__, __LINE__);
  287. if(Database::num_rows($special_course_result)>0) {
  288. $special_course_list = array();
  289. while ($result_row = Database::fetch_array($special_course_result)) {
  290. $special_course_list[] = '"'.$result_row['course_code'].'"';
  291. }
  292. }
  293. $without_special_courses = '';
  294. if (!empty($special_course_list)) {
  295. $without_special_courses = ' AND course.code NOT IN ('.implode(',',$special_course_list).')';
  296. }
  297. $sql = "SELECT * FROM $tbl_course WHERE category_code".(empty($category) ? " IS NULL" : "='".$category."'").$without_special_courses;
  298. // Showing only the courses of the current Dokeos access_url_id.
  299. global $_configuration;
  300. if ($_configuration['multiple_access_urls']) {
  301. $url_access_id = api_get_current_access_url_id();
  302. if ($url_access_id != -1) {
  303. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  304. $sql = "SELECT * FROM $tbl_course as course INNER JOIN $tbl_url_rel_course as url_rel_course
  305. ON (url_rel_course.course_code=course.code)
  306. WHERE access_url_id = $url_access_id AND category_code".(empty($category) ? " IS NULL" : "='".$category."'").$without_special_courses;
  307. }
  308. }
  309. return Database::num_rows(Database::query($sql, __FILE__, __LINE__));
  310. }
  311. /**
  312. * displays the browsing of the course categories (faculties)
  313. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  314. * @return HTML code containing a list with all the categories and subcategories and the navigation to go one category up(if needed)
  315. */
  316. function browse_course_categories() {
  317. global $stok;
  318. $tbl_courses_nodes = Database::get_main_table(TABLE_MAIN_CATEGORY);
  319. $category = Database::escape_string($_GET['category']);
  320. $safe_url_categ = Security::remove_XSS($_GET['category']);
  321. echo "<p><strong>".get_lang('CourseCategories')."</strong>";
  322. $sql = "SELECT * FROM $tbl_courses_nodes WHERE parent_id ".(empty($category) ? "IS NULL" : "='".$category."'")." GROUP BY code, parent_id ORDER BY tree_pos ASC";
  323. $result = Database::query($sql, __FILE__, __LINE__);
  324. echo "<ul>";
  325. while ($row = Database::fetch_array($result)) {
  326. $count_courses_in_categ = count_courses_in_category($row['code']);
  327. if ($row['children_count'] > 0 || $count_courses_in_categ > 0) {
  328. 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>".
  329. " (".$count_courses_in_categ.")</li>";
  330. } elseif ($row['nbChilds'] > 0) {
  331. 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>";
  332. } else {
  333. echo "<li>".$row['name']."</li>";
  334. }
  335. }
  336. echo "</ul>";
  337. if ($_GET['category']) {
  338. 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>";
  339. }
  340. }
  341. /**
  342. * Display all the courses in the given course category. I could have used a parameter here
  343. * but instead I use the already available $_GET['category']
  344. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  345. * @return HTML code: a table with all the courses in a given category (title, code, tutor) and a subscription icon if applicable)
  346. */
  347. function browse_courses_in_category() {
  348. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  349. $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD);
  350. $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  351. // get course list auto-register
  352. $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON " .
  353. " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  354. $special_course_result = Database::query($sql, __FILE__, __LINE__);
  355. if(Database::num_rows($special_course_result)>0) {
  356. $special_course_list = array();
  357. while ($result_row = Database::fetch_array($special_course_result)) {
  358. $special_course_list[] = '"'.$result_row['course_code'].'"';
  359. }
  360. }
  361. $without_special_courses = '';
  362. if (!empty($special_course_list)) {
  363. $without_special_courses = ' AND course.code NOT IN ('.implode(',',$special_course_list).')';
  364. }
  365. $category = Database::escape_string($_GET['category']);
  366. echo "<p><strong>".get_lang('CoursesInCategory')."</strong>";
  367. $my_category = (empty($category) ? " IS NULL" : "='".$category."'");
  368. $sql = "SELECT * FROM $tbl_course WHERE category_code".$my_category.$without_special_courses.' ORDER BY title, visual_code';
  369. //showing only the courses of the current Dokeos access_url_id
  370. global $_configuration;
  371. if ($_configuration['multiple_access_urls']) {
  372. $url_access_id = api_get_current_access_url_id();
  373. if ($url_access_id != -1) {
  374. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  375. $sql = "SELECT * FROM $tbl_course as course INNER JOIN $tbl_url_rel_course as url_rel_course
  376. ON (url_rel_course.course_code=course.code)
  377. WHERE access_url_id = $url_access_id AND category_code".$my_category.$without_special_courses.' ORDER BY title, visual_code';
  378. }
  379. }
  380. $result = Database::query($sql, __FILE__, __LINE__);
  381. while ($row = Database::fetch_array($result)) {
  382. $row['registration_code'] = !empty($row['registration_code']);
  383. $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);
  384. }
  385. display_subscribe_to_courses($courses);
  386. }
  387. /**
  388. * displays the form for searching for a course and the results if a query has been submitted.
  389. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  390. * @return HTML code: the form for searching for a course
  391. */
  392. function display_search_courses() {
  393. global $_user, $stok;
  394. echo "<p><strong>".get_lang('SearchCourse')."</strong><br />";
  395. echo "<form class=\"course_list\" method=\"post\" action=\"".api_get_self()."?action=subscribe\">",
  396. '<input type="hidden" name="sec_token" value="'.$stok.'">',
  397. "<input type=\"hidden\" name=\"search_course\" value=\"1\" />",
  398. "<input type=\"text\" name=\"search_term\" value=\"".(empty($_POST['search_term']) ? '' : Security::remove_XSS($_POST['search_term']))."\" />",
  399. "&nbsp;<button class=\"search\" type=\"submit\">",get_lang('_search'),"</button>",
  400. "</form>";
  401. if (isset($_POST['search_course'])) {
  402. echo "<p><strong>".get_lang('SearchResultsFor')." ".api_htmlentities($_POST['search_term'], ENT_QUOTES, api_get_system_encoding())."</strong><br />";
  403. $result_search_courses_array = search_courses($_POST['search_term']);
  404. display_subscribe_to_courses($result_search_courses_array);
  405. }
  406. }
  407. /**
  408. * This function displays the list of course that can be subscribed to.
  409. * This list can come from the search results or from the browsing of the platform course categories
  410. */
  411. function display_subscribe_to_courses($courses) {
  412. global $_user;
  413. // getting all the courses to which the user is subscribed to
  414. $user_courses = get_courses_of_user($_user['user_id']);
  415. $user_coursecodes = array();
  416. // we need only the course codes as these will be used to match against the courses of the category
  417. if ($user_courses != '') {
  418. foreach ($user_courses as $key => $value) {
  419. $user_coursecodes[] = $value['code'];
  420. }
  421. }
  422. if ($courses == 0) {
  423. return false;
  424. }
  425. echo "<table cellpadding=\"4\">\n";
  426. foreach ($courses as $key=>$course) {
  427. // displaying the course title, visual code and teacher/teaching staff
  428. echo "\t<tr>\n";
  429. if (api_get_setting('show_courses_descriptions_in_catalog') == 'true') {
  430. // block course description
  431. echo "\t\t<td>";
  432. $icon_title = get_lang('CourseDetails') . ' - ' . $course['title'];
  433. 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>";
  434. echo "\t\t</td>";
  435. }
  436. echo "\t\t<td>\n";
  437. echo "<strong>".$course['title']."</strong><br />";
  438. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  439. echo $course['visual_code'];
  440. }
  441. if (api_get_setting('display_coursecode_in_courselist') == 'true' && api_get_setting('display_teacher_in_courselist') == 'true') {
  442. echo " - ";
  443. }
  444. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  445. echo $course['tutor'];
  446. }
  447. echo "\t\t</td>\n";
  448. echo "\t\t<td>\n";
  449. if ($course['registration_code']) {
  450. Display::display_icon('passwordprotected.png', '', array('style' => 'float:left;'));
  451. }
  452. display_subscribe_icon($course, $user_coursecodes);
  453. echo "\t\t</td>\n";
  454. echo "</tr>";
  455. }
  456. echo "</table>";
  457. }
  458. /**
  459. * Search the courses database for a course that matches the search term.
  460. * The search is done on the code, title and tutor field of the course table.
  461. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  462. * @param string $search_term: the string that the user submitted, what we are looking for
  463. * @return array an array containing a list of all the courses (the code, directory, dabase, visual_code, title, ... )
  464. * matching the the search term.
  465. */
  466. function search_courses($search_term) {
  467. $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
  468. $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD);
  469. $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  470. // get course list auto-register
  471. $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON " .
  472. " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  473. $special_course_result = Database::query($sql, __FILE__, __LINE__);
  474. if(Database::num_rows($special_course_result)>0) {
  475. $special_course_list = array();
  476. while ($result_row = Database::fetch_array($special_course_result)) {
  477. $special_course_list[] = '"'.$result_row['course_code'].'"';
  478. }
  479. }
  480. $without_special_courses = '';
  481. if (!empty($special_course_list)) {
  482. $without_special_courses = ' AND course.code NOT IN ('.implode(',',$special_course_list).')';
  483. }
  484. $search_term_safe = Database::escape_string($search_term);
  485. $sql_find = "SELECT * FROM $TABLECOURS WHERE (code LIKE '%".$search_term_safe."%' OR title LIKE '%".$search_term_safe."%' OR tutor_name LIKE '%".$search_term_safe."%') $without_special_courses ORDER BY title, visual_code ASC";
  486. global $_configuration;
  487. if ($_configuration['multiple_access_urls']) {
  488. $url_access_id = api_get_current_access_url_id();
  489. if ($url_access_id != -1) {
  490. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  491. $sql_find = "SELECT * FROM $TABLECOURS as course INNER JOIN $tbl_url_rel_course as url_rel_course
  492. ON (url_rel_course.course_code=course.code)
  493. 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."%' ) $without_special_courses ORDER BY title, visual_code ASC ";
  494. }
  495. }
  496. $result_find = Database::query($sql_find, __FILE__, __LINE__);
  497. while ($row = Database::fetch_array($result_find)) {
  498. $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']);
  499. }
  500. return $courses;
  501. }
  502. /**
  503. * deletes a course category and moves all the courses that were in this category to main category
  504. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  505. * @param int $id: the id of the user_course_category
  506. * @return string a language variable saying that the deletion went OK.
  507. */
  508. function delete_course_category($id) {
  509. global $_user, $_configuration;
  510. $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  511. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  512. $id = intval($id);
  513. $sql_delete = "DELETE FROM $tucc WHERE id='".$id."' and user_id='".$_user['user_id']."'";
  514. $sql_update = "UPDATE $TABLECOURSUSER SET user_course_cat='0' WHERE user_course_cat='".$id."' AND user_id='".$_user['user_id']."'";
  515. Database::query($sql_delete, __FILE__, __LINE__);
  516. Database::query($sql_update, __FILE__, __LINE__);
  517. return get_lang('CourseCategoryDeleted');
  518. }
  519. /**
  520. * stores the user course category in the dokeos_user database
  521. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  522. * @return string a language variable saying that the user course category was stored
  523. */
  524. function store_course_category() {
  525. global $_user, $_configuration;
  526. $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  527. // step 1: we determine the max value of the user defined course categories
  528. $sql = "SELECT sort FROM $tucc WHERE user_id='".$_user['user_id']."' ORDER BY sort DESC";
  529. $result = Database::query($sql, __FILE__, __LINE__);
  530. $maxsort = Database::fetch_array($result);
  531. $nextsort = $maxsort['sort'] + 1;
  532. // step 2: we check if there is already a category with this name, if not we store it, else we give an error.
  533. $sql = "SELECT * FROM $tucc WHERE user_id='".$_user['user_id']."' AND title='".Database::escape_string($_POST['title_course_category'])."'ORDER BY sort DESC";
  534. $result = Database::query($sql, __FILE__, __LINE__);
  535. if (Database::num_rows($result) == 0) {
  536. $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."')";
  537. Database::query($sql_insert, __FILE__, __LINE__);
  538. Display::display_confirmation_message(get_lang("CourseCategoryStored"));
  539. } else {
  540. Display::display_error_message(get_lang('ACourseCategoryWithThisNameAlreadyExists'));
  541. }
  542. }
  543. /**
  544. * displays the form that is needed to create a course category.
  545. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  546. * @return HTML the form (input field + submit button) to create a user course category
  547. */
  548. function display_create_course_category_form()
  549. {
  550. global $_user, $_configuration, $stok;
  551. echo "<form name=\"create_course_category\" method=\"post\" action=\"".api_get_self()."?action=sortmycourses\">\n";
  552. echo '<input type="hidden" name="sec_token" value="'.$stok.'">';
  553. echo "<input type=\"text\" name=\"title_course_category\" />\n";
  554. echo "<button type=\"submit\" class=\"save\" name=\"create_course_category\">".get_lang('Ok')."</button>\n";
  555. echo "</form>\n";
  556. echo get_lang('ExistingCourseCategories');
  557. $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  558. $sql = "SELECT * FROM $tucc WHERE user_id='".$_user['user_id']."'";
  559. $result = Database::query($sql, __LINE__, __FILE__);
  560. if (Database::num_rows($result) > 0) {
  561. echo "<ul>\n";
  562. while ($row = Database::fetch_array($result)) {
  563. echo "\t<li>".$row['title']."</li>\n";
  564. }
  565. echo "</ul>\n";
  566. }
  567. }
  568. // ***************************************************************************
  569. // this function stores the changes in a course category
  570. //
  571. // ***************************************************************************
  572. /**
  573. * stores the changes in a course category (moving a course to a different course category)
  574. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  575. * @param string $course_code : the course_code of the course we are moving
  576. * int $newcategory : the id of the user course category we are moving the course to.
  577. * @return string a language variable saying that the course was moved.
  578. */
  579. function store_changecoursecategory($course_code, $newcategory) {
  580. global $_user;
  581. $course_code = Database::escape_string($course_code);
  582. $newcategory = Database::escape_string($newcategory);
  583. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  584. $max_sort_value = api_max_sort_value($newcategory, $_user['user_id']); // max_sort_value($newcategory);
  585. $sql = "UPDATE $TABLECOURSUSER SET user_course_cat='".$newcategory."', sort='".($max_sort_value + 1)."' WHERE course_code='".$course_code."' AND user_id='".$_user['user_id']."'";
  586. $result = Database::query($sql, __FILE__, __LINE__);
  587. return get_lang('EditCourseCategorySucces');
  588. }
  589. /**
  590. * moves the course one place up or down
  591. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  592. * @param string $direction : the direction we are moving the course to (up or down)
  593. * string $course2move : the course we are moving
  594. * @return string a language variable saying that the course was moved.
  595. */
  596. function move_course($direction, $course2move, $category) {
  597. global $_user;
  598. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  599. $all_user_courses = get_courses_of_user($_user['user_id']);
  600. // we need only the courses of the category we are moving in
  601. $user_courses = array();
  602. foreach ($all_user_courses as $key => $course) {
  603. if ($course['user_course_category'] == $category) {
  604. $user_courses[] = $course;
  605. }
  606. }
  607. foreach ($user_courses as $key => $course) {
  608. if ($course2move == $course['code']) {
  609. // source_course is the course where we clicked the up or down icon
  610. $source_course = $course;
  611. // target_course is the course before/after the source_course (depending on the up/down icon)
  612. if ($direction == 'up') {
  613. $target_course = $user_courses[$key - 1];
  614. } else {
  615. $target_course = $user_courses[$key + 1];
  616. }
  617. } // if ($course2move == $course['code'])
  618. }
  619. if (count($target_course) > 0 && count($source_course) > 0) {
  620. $sql_update1 = "UPDATE $TABLECOURSUSER SET sort='".$target_course['sort']."' WHERE course_code='".$source_course['code']."' AND user_id='".$_user['user_id']."'";
  621. $sql_update2 = "UPDATE $TABLECOURSUSER SET sort='".$source_course['sort']."' WHERE course_code='".$target_course['code']."' AND user_id='".$_user['user_id']."'";
  622. Database::query($sql_update2, __FILE__, __LINE__);
  623. Database::query($sql_update1, __FILE__, __LINE__);
  624. return get_lang('CourseSortingDone');
  625. }
  626. return '';
  627. }
  628. /**
  629. * Moves the course one place up or down
  630. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  631. * @param string $direction : the direction we are moving the course to (up or down)
  632. * string $course2move : the course we are moving
  633. * @return string a language variable saying that the course was moved.
  634. */
  635. function move_category($direction, $category2move) {
  636. global $_user;
  637. // the database definition of the table that stores the user defined course categories
  638. $table_user_defined_category = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  639. $user_coursecategories = get_user_course_categories();
  640. $user_course_categories_info = get_user_course_categories_info();
  641. foreach ($user_coursecategories as $key => $category_id) {
  642. if ($category2move == $category_id) {
  643. // source_course is the course where we clicked the up or down icon
  644. //$source_category=get_user_course_category($category2move);
  645. $source_category = $user_course_categories_info[$category2move];
  646. // target_course is the course before/after the source_course (depending on the up/down icon)
  647. if ($direction == 'up') {
  648. $target_category = $user_course_categories_info[$user_coursecategories[$key - 1]];
  649. } else {
  650. $target_category = $user_course_categories_info[$user_coursecategories[$key + 1]];
  651. }
  652. } // if ($course2move == $course['code'])
  653. } // foreach ($user_courses as $key => $course)
  654. if (count($target_category) > 0 && count($source_category) > 0) {
  655. $sql_update1="UPDATE $table_user_defined_category SET sort='".$target_category['sort']."' WHERE id='".$source_category['id']."' AND user_id='".$_user['user_id']."'";
  656. $sql_update2="UPDATE $table_user_defined_category SET sort='".$source_category['sort']."' WHERE id='".$target_category['id']."' AND user_id='".$_user['user_id']."'";
  657. Database::query($sql_update2, __FILE__, __LINE__);
  658. Database::query($sql_update1, __FILE__, __LINE__);
  659. return get_lang('CategorySortingDone');
  660. }
  661. return '';
  662. }
  663. /**
  664. * displays everything that is needed when the user wants to manage his current courses (sorting, subscribing, unsubscribing, ...)
  665. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  666. * @param int $user_id: the user_id of the current user
  667. * string $parameter: determines weither we are displaying for the sorting, subscribing or unsubscribin
  668. array $user_courses: the courses to which the user is subscribed
  669. * @return html a table containing courses and the appropriate icons (sub/unsub/move)
  670. */
  671. function display_courses($user_id, $show_course_icons, $user_courses) {
  672. global $_user, $_configuration;
  673. echo "<table cellpadding=\"4\">\n";
  674. // building an array that contains all the id's of the user defined course categories
  675. // initially this was inside the display_courses_in_category function but when we do it here we have fewer
  676. // sql executions = performance increase.
  677. $all_user_categories = get_user_course_categories();
  678. // step 0: we display the course without a user category
  679. display_courses_in_category(0, 'true');
  680. // Step 1: We get all the categories of the user.
  681. $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  682. $sql = "SELECT * FROM $tucc WHERE user_id='".$_user['user_id']."' ORDER BY sort ASC";
  683. $result = Database::query($sql, __FILE__, __LINE__);
  684. while ($row = Database::fetch_array($result)) {
  685. if ($show_course_icons) {
  686. // The edit link is clicked.
  687. if (isset($_GET['categoryid']) && $_GET['categoryid'] == $row['id']) {
  688. // We display the edit form for the category.
  689. echo "<tr><td colspan=\"2\" class=\"user_course_category\">";
  690. echo '<a name="category'.$row['id'].'"></a>'; // display an internal anchor.
  691. display_edit_course_category_form($row['id']);
  692. } else {
  693. // We simply display the title of the category.
  694. echo "<tr><td colspan=\"2\" class=\"user_course_category\">";
  695. echo '<a name="category'.$row['id'].'"></a>'; // display an internal anchor.
  696. echo $row['title'];
  697. }
  698. echo "</td><td class=\"user_course_category\">";
  699. display_category_icons($row['id'], $all_user_categories);
  700. echo "</td></tr>";
  701. }
  702. // Step 2: Show the courses inside this category.
  703. display_courses_in_category($row['id'], $show_course_icons);
  704. }
  705. echo "</table>\n";
  706. }
  707. /**
  708. * This function displays all the courses in the particular user category;
  709. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  710. * @param int id: the id of the user defined course category
  711. * @return string: the name of the user defined course category
  712. */
  713. function display_courses_in_category($user_category_id, $showicons) {
  714. global $_user;
  715. // table definitions
  716. $TABLECOURS=Database::get_main_table(TABLE_MAIN_COURSE);
  717. $TABLECOURSUSER=Database::get_main_table(TABLE_MAIN_COURSE_USER);
  718. $TABLE_USER_COURSE_CATEGORY = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  719. $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD);
  720. $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  721. // get course list auto-register
  722. $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON " .
  723. " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  724. $special_course_result = Database::query($sql, __FILE__, __LINE__);
  725. if(Database::num_rows($special_course_result)>0) {
  726. $special_course_list = array();
  727. while ($result_row = Database::fetch_array($special_course_result)) {
  728. $special_course_list[] = '"'.$result_row['course_code'].'"';
  729. }
  730. }
  731. $without_special_courses = '';
  732. if (!empty($special_course_list)) {
  733. $without_special_courses = ' AND course.code NOT IN ('.implode(',',$special_course_list).')';
  734. }
  735. $sql_select_courses = "SELECT course.code, course.visual_code, course.subscribe subscr, course.unsubscribe unsubscr,
  736. course.title title, course.tutor_name tutor, course.db_name, course.directory, course_rel_user.status status,
  737. course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
  738. FROM $TABLECOURS course,
  739. $TABLECOURSUSER course_rel_user
  740. WHERE course.code = course_rel_user.course_code
  741. AND course_rel_user.user_id = '".$_user['user_id']."'
  742. AND course_rel_user.user_course_cat='".$user_category_id."' $without_special_courses
  743. ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC";
  744. $result = Database::query($sql_select_courses,__FILE__,__LINE__);
  745. $number_of_courses = Database::num_rows($result);
  746. $key = 0;
  747. while ($course = Database::fetch_array($result)) {
  748. echo "\t<tr>\n";
  749. if (api_get_setting('show_courses_descriptions_in_catalog') == 'true') {
  750. // block course description
  751. echo "\t\t<td>";
  752. $icon_title = get_lang('CourseDetails') . ' - ' . $course['title'];
  753. 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>";
  754. echo "\t\t</td>";
  755. }
  756. echo "\t\t<td>\n";
  757. echo '<a name="course'.$course['code'].'"></a>'; // display an internal anchor.
  758. echo "<strong>".$course['title']."</strong><br />";
  759. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  760. echo $course['visual_code'];
  761. }
  762. if (api_get_setting('display_coursecode_in_courselist') == 'true' && api_get_setting('display_teacher_in_courselist') == 'true') {
  763. echo " - ";
  764. }
  765. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  766. echo $course['tutor'];
  767. }
  768. echo "\t\t</td>\n";
  769. // displaying the up/down/edit icons when we are sorting courses
  770. echo "\t\t<td valign=\"top\">\n";
  771. //if ($parameter == 'sorting')
  772. //{
  773. display_course_icons($key, $number_of_courses, $course);
  774. //}
  775. // displaying the delete icon when we are unsubscribing from courses
  776. //if($parameter == 'deleting')
  777. //{
  778. // display_unsubscribe_icons($course);
  779. //}
  780. // display the subscribing icon when we are to courses.
  781. //if ($parameter == 'subscribing')
  782. //{
  783. // display_subscribe_icon($course);
  784. //}
  785. echo "\t\t</td>\n";
  786. echo "\t</tr>\n";
  787. $key++;
  788. }
  789. }
  790. /**
  791. * gets the title of the user course category
  792. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  793. * @param int id: the id of the user defined course category
  794. * @return string: the name of the user defined course category
  795. */
  796. function get_user_course_category($id) {
  797. global $_user, $_configuration;
  798. $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  799. $id = intval($id);
  800. return Database::fetch_array(Database::query("SELECT * FROM $tucc WHERE user_id='".$_user['user_id']."' AND id='$id'", __FILE__, __LINE__));
  801. }
  802. /**
  803. * displays the subscribe icon if the subscribing is allowed and if the user is not yet
  804. * subscribe to this course
  805. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  806. * @param string $current_course: the course code of the course we need to display the subscribe icon for
  807. * @return string a subscribe icon or the text that subscribing is not allowed or the user is already subscribed
  808. */
  809. function display_subscribe_icon($current_course, $user_coursecodes) {
  810. global $stok;
  811. // we display the icon to subscribe or the text already subscribed
  812. if (in_array($current_course['code'], $user_coursecodes)) {
  813. Display::display_icon('enroll_na.gif', get_lang('AlreadySubscribed'));
  814. } else {
  815. if ($current_course['subscribe'] == SUBSCRIBE_ALLOWED) {
  816. echo "<form action=\"".$_SERVER["REQUEST_URI"]."\" method=\"post\">";
  817. echo '<input type="hidden" name="sec_token" value="'.$stok.'">';
  818. echo "<input type=\"hidden\" name=\"subscribe\" value=\"".$current_course['code']."\" />";
  819. if (!empty($_POST['search_term'])) {
  820. echo '<input type="hidden" name="search_course" value="1" />';
  821. echo '<input type="hidden" name="search_term" value="'.Security::remove_XSS($_POST['search_term']).'" />';
  822. }
  823. 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>";
  824. } else {
  825. // echo get_lang('SubscribingNotAllowed');
  826. Display::display_icon('enroll_na.gif', get_lang('SubscribingNotAllowed'));
  827. }
  828. }
  829. }
  830. /**
  831. * Displays the subscribe icon if the subscribing is allowed and if the user is not yet
  832. * subscribed to this course
  833. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  834. * @param $key:
  835. * $number_of_courses
  836. * $course
  837. * $user_courses
  838. * @return html a small table containing the up/down icons and the edit icon (for moving to a different user course category)
  839. * @todo complete the comments on this function: the parameter section
  840. */
  841. function display_course_icons($key, $number_of_courses, $course) {
  842. global $safe, $stok;
  843. echo "<table><tr><td>";
  844. // the up icon
  845. if ($key > 0) {
  846. 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."\">";
  847. Display::display_icon('up.gif', get_lang('Up'));
  848. echo '</a>';
  849. }
  850. echo "</td>";
  851. // the edit icon OR the edit dropdown list
  852. if (isset($_GET['edit']) && $course['code'] == $_GET['edit']) {
  853. echo "<td rowspan=\"2\" valign=\"top\">".display_change_course_category_form($_GET['edit'])."</td>";
  854. } else {
  855. echo "<td rowspan=\"2\" valign=\"middle\"><a href=\"courses.php?action=".$safe['action']."&amp;edit=".$course['code']."&amp;sec_token=".$stok."\">";
  856. Display::display_icon('edit.gif', get_lang('Edit'));
  857. echo "</a></td>";
  858. }
  859. echo "<td rowspan=\"2\" valign=\"top\" class=\"invisible\">";
  860. if ($course['status'] != 1) {
  861. if ($course['unsubscr'] == 1) {
  862. // changed link to submit to avoid action by the search tool indexer
  863. 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;\">";
  864. echo '<input type="hidden" name="sec_token" value="'.$stok.'">';
  865. echo "<input type=\"hidden\" name=\"unsubscribe\" value=\"".$course['code']."\" />";
  866. 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>';
  867. } else {
  868. display_info_text(get_lang('UnsubscribeNotAllowed'));
  869. }
  870. } else {
  871. display_info_text(get_lang('CourseAdminUnsubscribeNotAllowed'));
  872. }
  873. echo "</td>";
  874. echo "</tr><tr><td>";
  875. if ($key < $number_of_courses - 1) {
  876. 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."\">";
  877. Display::display_icon('down.gif', get_lang('Down'));
  878. echo '</a>';
  879. }
  880. echo "</td></tr></table>";
  881. }
  882. /**
  883. * displays the relevant icons for the category (if applicable):move up, move down, edit, delete
  884. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  885. * @param $current_category the id of the current category
  886. * $allcategories an associative array containing all the categories.
  887. * @return html: a small table containing the up/down icons and the edit icon (for moving to a different user course category)
  888. * @todo complete the comments on this function: the parameter section
  889. */
  890. function display_category_icons($current_category, $all_user_categories) {
  891. global $safe, $stok;
  892. $max_category_key = count($all_user_categories);
  893. if ($safe['action'] != 'unsubscribe') { // we are in the unsubscribe section then we do not show the icons.
  894. echo "<table>";
  895. echo "<tr>";
  896. echo "<td>";
  897. if ($current_category != $all_user_categories[0]) {
  898. echo "<a href=\"courses.php?action=".$safe['action']."&amp;move=up&amp;category=".$current_category."&amp;sec_token=".$stok."\">";
  899. echo Display::return_icon('up.gif', get_lang('Up')).'</a>';
  900. }
  901. echo "</td>";
  902. echo " <td rowspan=\"2\">";
  903. echo " <a href=\"courses.php?action=sortmycourses&amp;categoryid=".$current_category."&amp;sec_token=".$stok."#category".$current_category."\">";
  904. Display::display_icon('edit.gif', get_lang('Edit'));
  905. echo "</a>";
  906. echo "</td>";
  907. echo "<td rowspan=\"2\">";
  908. echo " <a href=\"courses.php?action=deletecoursecategory&amp;id=".$current_category."&amp;sec_token=".$stok."\">";
  909. 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;"));
  910. echo "</a>";
  911. echo "</td>";
  912. echo "</tr>";
  913. echo "<tr>";
  914. echo " <td>";
  915. if ($current_category != $all_user_categories[$max_category_key - 1]) {
  916. echo "<a href=\"courses.php?action=".$safe['action']."&amp;move=down&amp;category=".$current_category."&amp;sec_token=".$stok."\">";
  917. echo Display::return_icon('down.gif', get_lang('Down')).'</a>';
  918. }
  919. echo "</td>";
  920. echo " </tr>";
  921. echo "</table>";
  922. }
  923. }
  924. /**
  925. * This function displays the form (dropdown list) to move a course to a
  926. * different course_category (after the edit icon has been changed)
  927. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  928. * @param string $edit_course:
  929. * @return html a dropdown list containing all the user defined course categories and a submit button
  930. * @todo when editing (moving) a course inside a user defined course category to a different user defined category
  931. * the dropdown list should have the current course category selected.
  932. */
  933. function display_change_course_category_form($edit_course) {
  934. global $_user, $_configuration, $safe, $stok;
  935. $edit_course = Security::remove_XSS($edit_course);
  936. $DATABASE_USER_TOOLS = $_configuration['user_personal_database'];
  937. $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  938. $sql = "SELECT * FROM $tucc WHERE user_id='".$_user['user_id']."'";
  939. $result = Database::query($sql, __FILE__, __LINE__);
  940. $output = "<form name=\"edit_course_category\" method=\"post\" action=\"courses.php?action=".$safe['action']."\">\n";
  941. $output .= '<input type="hidden" name="sec_token" value="'.$stok.'">';
  942. $output .= "<input type=\"hidden\" name=\"course_2_edit_category\" value=\"".$edit_course."\" />";
  943. $output .= "\t<select name=\"course_categories\">\n";
  944. $output .= "\t\t<option value=\"0\">".get_lang("NoCourseCategory")."</option>";
  945. while ($row = Database::fetch_array($result)) {
  946. $output.="\t\t<option value=\"".$row['id']."\">".$row['title']."</option>";
  947. }
  948. $output .= "\t</select>\n";
  949. $output .= "\t<button class=\"save\" type=\"submit\" name=\"submit_change_course_category\">".get_lang('Ok')."</button>\n";
  950. $output .= "</form>";
  951. return $output;
  952. }
  953. /**
  954. * This function displays the unsubscribe part which can be
  955. * 1. the unsubscribe link
  956. * 2. text: you are course admin of this course (=> unsubscription is not possible
  957. * 3. text: you are not allowed to unsubscribe from this course
  958. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  959. * @param array $course: the array with all the course & course_rel_user information
  960. * @return html a delete icon or a text that unsubscribing is not possible (course admin) or not allowed.
  961. */
  962. function display_unsubscribe_icons($course) {
  963. global $stok;
  964. if ($course['status'] != 1) {
  965. if ($course['unsubscribe'] == 1) {
  966. // Changed link to submit to avoid action by the search tool indexer.
  967. 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;\">";
  968. echo '<input type="hidden" name="sec_token" value="'.$stok.'">';
  969. echo "<input type=\"hidden\" name=\"unsubscribe\" value=\"".$course['code']."\" />";
  970. echo "<input type=\"image\" name=\"unsub\" src=\"".api_get_path(WEB_IMG_PATH)."delete.gif\" alt=\"".get_lang('_unsubscribe')."\" /></form>";
  971. } else {
  972. display_info_text(get_lang('UnsubscribeNotAllowed'));
  973. }
  974. } else {
  975. display_info_text(get_lang('CourseAdminUnsubscribeNotAllowed'));
  976. }
  977. }
  978. /**
  979. * retrieves all the courses that the user has already subscribed to
  980. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  981. * @param int $user_id: the id of the user
  982. * @return array an array containing all the information of the courses of the given user
  983. */
  984. function get_courses_of_user($user_id) {
  985. $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
  986. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  987. $TABLE_COURSE_FIELD = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD);
  988. $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  989. // get course list auto-register
  990. $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON " .
  991. " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  992. $special_course_result = Database::query($sql, __FILE__, __LINE__);
  993. if(Database::num_rows($special_course_result)>0) {
  994. $special_course_list = array();
  995. while ($result_row = Database::fetch_array($special_course_result)) {
  996. $special_course_list[] = '"'.$result_row['course_code'].'"';
  997. }
  998. }
  999. $without_special_courses = '';
  1000. if (!empty($special_course_list)) {
  1001. $without_special_courses = ' AND course.code NOT IN ('.implode(',',$special_course_list).')';
  1002. }
  1003. // Secondly we select the courses that are in a category (user_course_cat<>0) and sort these according to the sort of the category
  1004. $user_id = intval($user_id);
  1005. $sql_select_courses = "SELECT course.code k, course.visual_code vc, course.subscribe subscr, course.unsubscribe unsubscr,
  1006. course.title i, course.tutor_name t, course.db_name db, course.directory dir, course_rel_user.status status,
  1007. course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
  1008. FROM $TABLECOURS course,
  1009. $TABLECOURSUSER course_rel_user
  1010. WHERE course.code = course_rel_user.course_code
  1011. AND course_rel_user.user_id = '".$user_id."' $without_special_courses
  1012. ORDER BY course_rel_user.sort ASC";
  1013. $result = Database::query($sql_select_courses,__FILE__,__LINE__);
  1014. while ($row = Database::fetch_array($result)) {
  1015. // we only need the database name of the course
  1016. $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']);
  1017. }
  1018. return $courses;
  1019. }
  1020. /**
  1021. * retrieves the user defined course categories
  1022. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1023. * @return array containing all the IDs of the user defined courses categories, sorted by the "sort" field
  1024. */
  1025. function get_user_course_categories() {
  1026. global $_user;
  1027. $table_category = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  1028. $sql = "SELECT * FROM ".$table_category." WHERE user_id='".$_user['user_id']."' ORDER BY sort ASC";
  1029. $result = Database::query($sql, __FILE__, __LINE__);
  1030. while ($row = Database::fetch_array($result)) {
  1031. $output[] = $row['id'];
  1032. }
  1033. return $output;
  1034. }
  1035. /**
  1036. * Retrieves the user defined course categories and all the info that goes with it
  1037. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1038. * @return array containing all the info of the user defined courses categories with the id as key of the array
  1039. */
  1040. function get_user_course_categories_info() {
  1041. global $_user;
  1042. $table_category = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  1043. $sql = "SELECT * FROM ".$table_category." WHERE user_id='".$_user['user_id']."' ORDER BY sort ASC";
  1044. $result = Database::query($sql, __FILE__, __LINE__);
  1045. while ($row = Database::fetch_array($result)) {
  1046. $output[$row['id']] = $row;
  1047. }
  1048. return $output;
  1049. }
  1050. /**
  1051. * @author unknown
  1052. * @param string $text: the text that has to be written in grey
  1053. * @return string: the text with the grey formatting
  1054. * @todo move this to a stylesheet
  1055. * Added id grey to CSS
  1056. */
  1057. function display_info_text($text) {
  1058. //echo "<font color=\"#808080\">" . $text . "</font>\n";
  1059. echo $text;
  1060. }
  1061. /**
  1062. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1063. * @param string $edit_course:
  1064. * @return html output: the form
  1065. */
  1066. function display_edit_course_category_form($edit_course_category) {
  1067. global $safe, $stok;
  1068. echo "<form name=\"edit_course_category\" method=\"post\" action=\"courses.php?action=".$safe['action']."\">\n";
  1069. echo "\t<input type=\"hidden\" name=\"edit_course_category\" value=\"".$edit_course_category."\" />\n";
  1070. echo '<input type="hidden" name="sec_token" value="'.$stok.'">';
  1071. $info_this_user_course_category = get_user_course_category($edit_course_category);
  1072. echo "\t<input type=\"text\" name=\"title_course_category\" value=\"".$info_this_user_course_category['title']."\" />";
  1073. echo "\t<button class=\"save\" type=\"submit\" name=\"submit_edit_course_category\">".get_lang('Ok')."</button>\n";
  1074. echo "</form>";
  1075. }
  1076. /**
  1077. * Updates the user course category in the dokeos_user database
  1078. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1079. * @return string a language variable saying that the user course category was stored
  1080. */
  1081. function store_edit_course_category() {
  1082. global $_user, $_configuration;
  1083. $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  1084. $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']."'";
  1085. Database::query($sql_update, __FILE__, __LINE__);
  1086. return get_lang('CourseCategoryEditStored');
  1087. }