dokeos-auth
[ class tree: dokeos-auth ] [ index: dokeos-auth ] [ all elements ]

Source for file courses.php

Documentation is available at courses.php

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

Documentation generated on Thu, 12 Jun 2008 13:13:43 -0500 by phpDocumentor 1.4.1