courses.php 47 KB

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