courses.php 44 KB

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