courses.php 48 KB

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