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

Source for file course.lib.php

Documentation is available at course.lib.php

  1. <?php
  2. /*
  3. ==============================================================================
  4.     Dokeos - elearning and course management software
  5.  
  6.     Copyright (c) 2004-2008 Dokeos SPRL
  7.     Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8.     Copyright (c) Roan Embrechts, Vrije Universiteit Brussel
  9.     Copyright (c) Bart Mollet, Hogeschool Gent
  10.     Copyright (c) Yannick Warnier, Dokeos S.A.
  11.  
  12.     For a full list of contributors, see "credits.txt".
  13.     The full license can be read in "license.txt".
  14.  
  15.     This program is free software; you can redistribute it and/or
  16.     modify it under the terms of the GNU General Public License
  17.     as published by the Free Software Foundation; either version 2
  18.     of the License, or (at your option) any later version.
  19.  
  20.     See the GNU General Public License for more details.
  21.  
  22.     Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  23.     Mail: info@dokeos.com
  24. ==============================================================================
  25. */
  26. /**
  27. ==============================================================================
  28. * This is the course library for Dokeos.
  29. *
  30. * All main course functions should be placed here.
  31.  
  32. * Many functions of this library deal with providing support for
  33. * virtual/linked/combined courses (this was already used in several universities
  34. * but not available in standard Dokeos).
  35. *
  36. * The implementation changed, initially a course was a real course
  37. * if target_course_code was 0 , this was changed to NULL.
  38. * There are probably some places left with the wrong code.
  39. *
  40. @package dokeos.library
  41. ==============================================================================
  42. */
  43. /*
  44. ==============================================================================
  45.     DOCUMENTATION
  46.     (list not up to date, you can auto generate documentation with phpDocumentor)
  47.  
  48.     CourseManager::get_real_course_code_select_html($element_name, $has_size=true, $only_current_user_courses=true)
  49.     CourseManager::check_parameter($parameter, $error_message)
  50.     CourseManager::check_parameter_or_fail($parameter, $error_message)
  51.     CourseManager::is_existing_course_code($wanted_course_code)
  52.     CourseManager::get_real_course_list()
  53.     CourseManager::get_virtual_course_list()
  54.  
  55.     GENERAL COURSE FUNCTIONS
  56.     CourseManager::get_access_settings($course_code)
  57.     CourseManager::set_course_tool_visibility($tool_table_id, $visibility)
  58.     CourseManager::get_user_in_course_status($user_id, $course_code)
  59.     CourseManager::add_user_to_course($user_id, $course_code)
  60.     CourseManager::get_virtual_course_info($real_course_code)
  61.     CourseManager::is_virtual_course_from_visual_code($visual_code)
  62.     CourseManager::is_virtual_course_from_system_code($system_code)
  63.     CourseManager::get_virtual_courses_linked_to_real_course($real_course_code)
  64.     CourseManager::get_list_of_virtual_courses_for_specific_user_and_real_course($user_id, $real_course_code)
  65.     CourseManager::has_virtual_courses_from_code($real_course_code, $user_id)
  66.     CourseManager::get_target_of_linked_course($virtual_course_code)
  67.  
  68.     TITLE AND CODE FUNCTIONS
  69.     CourseManager::determine_course_title_from_course_info($user_id, $course_info)
  70.     CourseManager::create_combined_name($user_is_registered_in_real_course, $real_course_name, $virtual_course_list)
  71.     CourseManager::create_combined_code($user_is_registered_in_real_course, $real_course_code, $virtual_course_list)
  72.  
  73.     USER FUNCTIONS
  74.     CourseManager::get_real_course_list_of_user_as_course_admin($user_id)
  75.     CourseManager::get_course_list_of_user_as_course_admin($user_id)
  76.  
  77.     CourseManager::is_user_subscribed_in_course($user_id, $course_code)
  78.     CourseManager::is_user_subscribed_in_real_or_linked_course($user_id, $course_code)
  79.     CourseManager::get_user_list_from_course_code($course_code)
  80.     CourseManager::get_real_and_linked_user_list($course_code);
  81.  
  82.     GROUP FUNCTIONS
  83.     CourseManager::get_group_list_of_course($course_code)
  84.  
  85.     CREATION FUNCTIONS
  86.     CourseManager::attempt_create_virtual_course($real_course_code, $course_title, $wanted_course_code, $course_language, $course_category)
  87. ==============================================================================
  88. */
  89.  
  90. /*
  91. ==============================================================================
  92.         INIT SECTION
  93. ==============================================================================
  94. */
  95.  
  96. /*
  97. -----------------------------------------------------------
  98.         Configuration files
  99. -----------------------------------------------------------
  100. */
  101. include_once (api_get_path(CONFIGURATION_PATH).'add_course.conf.php');
  102.  
  103. /*
  104. -----------------------------------------------------------
  105.         Libraries
  106.         we assume main_api is also included...
  107. -----------------------------------------------------------
  108. */
  109.  
  110. include_once (api_get_path(LIBRARY_PATH).'database.lib.php');
  111. include_once (api_get_path(LIBRARY_PATH).'add_course.lib.inc.php');
  112.  
  113. /*
  114. -----------------------------------------------------------
  115.         Constants
  116. -----------------------------------------------------------
  117. */
  118.  
  119. //LOGIC: course visibility and registration settings
  120. /*
  121.     COURSE VISIBILITY
  122.  
  123.     MAPPING OLD SETTINGS TO NEW SETTINGS
  124.     -----------------------
  125.  
  126.     NOT_VISIBLE_NO_SUBSCRIPTION_ALLOWED
  127.     --> COURSE_VISIBILITY_REGISTERED, SUBSCRIBE_NOT_ALLOWED
  128.     NOT_VISIBLE_SUBSCRIPTION_ALLOWED
  129.     --> COURSE_VISIBILITY_REGISTERED, SUBSCRIBE_ALLOWED
  130.     VISIBLE_SUBSCRIPTION_ALLOWED
  131.     --> COURSE_VISIBILITY_OPEN_PLATFORM, SUBSCRIBE_ALLOWED
  132.     VISIBLE_NO_SUBSCRIPTION_ALLOWED
  133.     --> COURSE_VISIBILITY_OPEN_PLATFORM, SUBSCRIBE_NOT_ALLOWED
  134. */
  135. //OLD SETTINGS
  136. define("NOT_VISIBLE_NO_SUBSCRIPTION_ALLOWED"0);
  137. define("NOT_VISIBLE_SUBSCRIPTION_ALLOWED"1);
  138. define("VISIBLE_SUBSCRIPTION_ALLOWED"2);
  139. define("VISIBLE_NO_SUBSCRIPTION_ALLOWED"3);
  140.  
  141. //NEW SETTINGS
  142. //these are now defined in the main_api.lib.php
  143. /*
  144.     COURSE_VISIBILITY_CLOSED
  145.     COURSE_VISIBILITY_REGISTERED
  146.     COURSE_VISIBILITY_OPEN_PLATFORM
  147.     COURSE_VISIBILITY_OPEN_WORLD
  148.  
  149.     SUBSCRIBE_ALLOWED
  150.     SUBSCRIBE_NOT_ALLOWED
  151.     UNSUBSCRIBE_ALLOWED
  152.     UNSUBSCRIBE_NOT_ALLOWED
  153. */
  154.  
  155. /*
  156. -----------------------------------------------------------
  157.     Variables
  158. -----------------------------------------------------------
  159. */
  160.  
  161. $TABLECOURSE Database :: get_main_table(TABLE_MAIN_COURSE);
  162. $TABLECOURSDOMAIN Database :: get_main_table(TABLE_MAIN_CATEGORY);
  163. $TABLEANNOUNCEMENTS "announcement";
  164. $coursesRepositories $_configuration['root_sys'];
  165.  
  166. /*
  167. ==============================================================================
  168.         CourseManager CLASS
  169. ==============================================================================
  170. */
  171.  
  172. /**
  173.  *    @package dokeos.library
  174.  */
  175. {
  176.     /**
  177.     * Returns all the information of a given coursecode
  178.     * @param string $course_code, the course code
  179.     * @return an array with all the fields of the course table
  180.     * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  181.     */
  182.     function get_course_information($course_code)
  183.     {
  184.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  185.         $sql="SELECT * FROM ".$course_table." WHERE code='".$course_code."'";
  186.         $sql_result api_sql_query($sql__FILE____LINE__);
  187.         $result Database::fetch_array($sql_result);
  188.         return $result;
  189.     }
  190.     /**
  191.      * Returns a list of courses. Should work with quickform syntax
  192.      * @param    integer    Offset (from the 7th = '6'). Optional.
  193.      * @param    integer    Number of results we want. Optional.
  194.      * @param    string    The column we want to order it by. Optional, defaults to first column.
  195.      * @param    string    The direction of the order (ASC or DESC). Optional, defaults to ASC.
  196.      * @param    string    The visibility of the course, or all by default.
  197.      * @param    string    If defined, only return results for which the course *title* begins with this string
  198.      */
  199.     function get_courses_list($from=0,$howmany=0,$orderby=1,$orderdirection='ASC',$visibility=-1,$startwith='')
  200.     {
  201.         $tbl_course Database::get_main_table(TABLE_MAIN_COURSE);
  202.         $sql "SELECT code, title " .
  203.                 "FROM $tbl_course ";
  204.         if(!empty($startwith))
  205.         {
  206.             $sql .= "WHERE LIKE title '".Database::escape_string($startwith)."%' ";
  207.         }
  208.         else
  209.         {
  210.             $sql .= "WHERE 1 ";
  211.         }
  212.         if(!empty($orderby))
  213.         {
  214.             $sql .= " ORDER BY ".Database::escape_string($orderby)." ";
  215.         }
  216.         else
  217.         {
  218.             $sql .= " ORDER BY 1 ";
  219.         }
  220.         if(!empty($orderdirection))
  221.         {
  222.             $sql .= Database::escape_string($orderdirection);
  223.         }
  224.         else
  225.         {
  226.             $sql .= 'ASC';
  227.         }
  228.         if(!empty($howmanyand is_int($howmanyand $howmany>0)
  229.         {
  230.             $sql .= ' LIMIT '.Database::escape_string($howmany);
  231.         }
  232.         else
  233.         {
  234.             $sql .= ' LIMIT 1000000'//virtually no limit
  235.         }
  236.         if(!empty($from))
  237.         {
  238.             $sql .= ' OFFSET '.Database::escape_string($from);
  239.         }
  240.         else
  241.         {
  242.             $sql .= ' OFFSET 0';
  243.         }
  244.         $res api_sql_query($sql,__FILE__,__LINE__);
  245.         return api_store_result($res);
  246.     }
  247.  
  248.  
  249.     /**
  250.     * Returns the access settings of the course:
  251.     * which visibility;
  252.     * wether subscribing is allowed;
  253.     * wether unsubscribing is allowed.
  254.     *
  255.     * @param string $course_code, the course code
  256.     * @todo for more consistency: use course_info call from database API
  257.     * @return an array with int fields "visibility", "subscribe", "unsubscribe"
  258.     */
  259.     function get_access_settings($course_code)
  260.     {
  261.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  262.         $sql "SELECT `visibility`, `subscribe`, `unsubscribe` from ".$course_table." where `code` = '".$course_code."'";
  263.         $sql_result api_sql_query($sql__FILE____LINE__);
  264.         $result Database::fetch_array($sql_result);
  265.         return $result;
  266.     }
  267.  
  268.     /**
  269.     * Returns the status of a user in a course, which is COURSEMANAGER or STUDENT.
  270.     *
  271.     * @return int the status of the user in that course
  272.     */
  273.     function get_user_in_course_status($user_id$course_code)
  274.     {
  275.         $course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  276.         $sql_query "SELECT * FROM $course_user_table WHERE `course_code` = '$course_code' AND `user_id` = '$user_id'";
  277.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  278.         $result Database::fetch_array($sql_result);
  279.         return $result["status"];
  280.     }
  281.  
  282.  
  283.     /**
  284.      * Unsubscribe one or more users from a course
  285.      * @param int|array$user_id 
  286.      * @param string $course_code 
  287.      */
  288.     function unsubscribe_user($user_id$course_code)
  289.     {
  290.         if(!is_array($user_id))
  291.         {
  292.             $user_id array($user_id);
  293.         }
  294.         if(count($user_id== 0)
  295.         {
  296.             return;
  297.         }
  298.         $user_ids implode(','$user_id);
  299.         $table_course_user Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  300.         $table_course Database :: get_main_table(TABLE_MAIN_COURSE);
  301.         // Unsubscribe user from all groups in the course
  302.         $sql "SELECT * FROM $table_course WHERE code = '".$course_code."'";
  303.         $res api_sql_query($sql__FILE____LINE__);
  304.         $course Database::fetch_object($res);
  305.         $table_group Database :: get_course_table(TABLE_GROUP_USER$course->db_name);
  306.         $sql "DELETE FROM $table_group WHERE user_id IN (".$user_ids.")";
  307.         api_sql_query($sql__FILE____LINE__);
  308.  
  309.         // Unsubscribe user from all blogs in the course
  310.         $table_blog_user Database::get_course_table(TABLE_BLOGS_REL_USER$course->db_name);
  311.         $sql "DELETE FROM  ".$table_blog_user." WHERE user_id IN (".$user_ids.")";
  312.         api_sql_query($sql,__FILE__,__LINE__);
  313.         $table_blogtask_user Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER$course->db_name);
  314.         $sql "DELETE FROM  ".$table_blogtask_user." WHERE user_id IN (".$user_ids.")";
  315.         api_sql_query($sql,__FILE__,__LINE__);
  316.  
  317.         // Unsubscribe user from the course
  318.         $sql "DELETE FROM $table_course_user WHERE user_id IN (".$user_ids.") AND course_code = '".$course_code."'";
  319.         api_sql_query($sql__FILE____LINE__);
  320.     }
  321.  
  322.  
  323.     /**
  324.      * Subscribe a user to a course. No checks are performed here to see if
  325.      * course subscription is allowed.
  326.      * @see add_user_to_course
  327.      */
  328.     function subscribe_user($user_id$course_code$status STUDENT)
  329.     {
  330.         $user_table Database :: get_main_table(TABLE_MAIN_USER);
  331.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  332.         $course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  333.         $location_table Database :: get_main_table(MAIN_LOCATION_TABLE);
  334.         $user_role_table Database :: get_main_table(MAIN_USER_ROLE_TABLE);
  335.  
  336.         $status ($status == STUDENT || $status == COURSEMANAGER$status STUDENT;
  337.         $role_id ($status == COURSEMANAGERCOURSE_ADMIN NORMAL_COURSE_MEMBER;
  338.  
  339.         if (empty ($user_id|| empty ($course_code))
  340.         {
  341.             return false;
  342.         }
  343.         else
  344.         {
  345.             // previously check if the user are already registered on the platform
  346.  
  347.             $handle api_sql_query("SELECT status FROM ".$user_table."
  348.                                                         WHERE `user_id` = '$user_id"__FILE____LINE__);
  349.             if (Database::num_rows($handle== 0)
  350.             {
  351.                 return false// the user isn't registered to the platform
  352.             }
  353.             else
  354.             {
  355.                 //check if user isn't already subscribed to the course
  356.                 $handle api_sql_query("SELECT * FROM ".$course_user_table."
  357.                                                                     WHERE `user_id` = '$user_id'
  358.                                                                     AND `course_code` ='$course_code'"__FILE____LINE__);
  359.                 if (Database::num_rows($handle0)
  360.                 {
  361.                     return false// the user is already subscribed to the course
  362.                 }
  363.                 else
  364.                 {
  365.                     $course_sort CourseManager :: userCourseSort($user_id,$course_code);
  366.                     $add_course_user_entry_sql "INSERT INTO ".$course_user_table."
  367.                                         SET `course_code` = '$course_code',
  368.                                         `user_id`    = '$user_id',
  369.                                             `status`    = '".$status."',
  370.                                             `sort`  =   '"($course_sort)."'";
  371.                     $result api_sql_query($add_course_user_entry_sql__FILE____LINE__);
  372.                     if ($result)
  373.                     {
  374.                         return true;
  375.                     }
  376.                     else
  377.                     {
  378.                         return false;
  379.                     }
  380.                 }
  381.             }
  382.         }
  383.     }
  384.  
  385.     /**
  386.     * Subscribe a user $user_id to a course $course_code.
  387.     * @author Hugues Peeters
  388.     * @author Roan Embrechts
  389.     *
  390.     * @param  int $user_id the id of the user
  391.     * @param  string $course_code the course code
  392.     * @param string $status (optional) The user's status in the course
  393.     *
  394.     * @return boolean true if subscription succeeds, boolean false otherwise.
  395.     * @todo script has ugly ifelseifelseifelseif structure, improve
  396.     */
  397.     function add_user_to_course($user_id$course_code$status STUDENT)
  398.     {
  399.         $user_table Database :: get_main_table(TABLE_MAIN_USER);
  400.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  401.         $course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  402.  
  403.         $status ($status == STUDENT || $status == COURSEMANAGER$status STUDENT;
  404.         if (empty($user_id|| empty ($course_code))
  405.         {
  406.             return false;
  407.         }
  408.         else
  409.         {
  410.             // previously check if the user are already registered on the platform
  411.  
  412.             $handle api_sql_query("SELECT status FROM ".$user_table."
  413.                                 WHERE `user_id` = '$user_id"__FILE____LINE__);
  414.             if (Database::num_rows($handle== 0)
  415.             {
  416.                 return false// the user isn't registered to the platform
  417.             }
  418.             else
  419.             {
  420.                 //check if user isn't already subscribed to the course
  421.                 $handle api_sql_query("SELECT * FROM ".$course_user_table."
  422.                                                                     WHERE `user_id` = '$user_id'
  423.                                                                     AND `course_code` ='$course_code'"__FILE____LINE__);
  424.                 if (Database::num_rows($handle0)
  425.                 {
  426.                     return false// the user is already subscribed to the course
  427.                 }
  428.                 else
  429.                 {
  430.                     // previously check if subscription is allowed for this course
  431.  
  432.                     $handle api_sql_query("SELECT code, visibility FROM ".$course_table."
  433.                                             WHERE  `code` = '$course_code'
  434.                                             AND  `subscribe` = '".SUBSCRIBE_NOT_ALLOWED."'"__FILE____LINE__);
  435.  
  436.                     if (Database::num_rows($handle0)
  437.                     {
  438.                         return false// subscription not allowed for this course
  439.                     }
  440.                     else
  441.                     {
  442.                         $max_sort api_max_sort_value('0'$user_id);
  443.                         $add_course_user_entry_sql "INSERT INTO ".$course_user_table."
  444.                                             SET `course_code` = '$course_code',
  445.                                                 `user_id`    = '$user_id',
  446.                                                 `status`    = '".$status."',
  447.                                                 `sort`  =   '"($max_sort +1)."'";
  448.                         $result=api_sql_query($add_course_user_entry_sql__FILE____LINE__);
  449.                         if ($result)
  450.                         {
  451.                             return true;
  452.                         }
  453.                         else
  454.                         {
  455.                             return false;
  456.                         }
  457.                     }
  458.                 }
  459.             }
  460.         }
  461.     }
  462.  
  463.     /**
  464.     *    This code creates a select form element to let the user
  465.     *    choose a real course to link to.
  466.     *
  467.     *    A good non-display library should not use echo statements, but just return text/html
  468.     *    so users of the library can choose when to display.
  469.     *
  470.     *    We display the course code, but internally store the course id.
  471.     *
  472.     *    @param boolean $has_size, true the select tag gets a size element, false it stays a dropdownmenu
  473.     *    @param boolean $only_current_user_courses, true only the real courses of which the
  474.     *     current user is course admin are displayed, false all real courses are shown.
  475.     *    @param string $element_name the name of the select element
  476.     *    @return string containing html code for a form select element.
  477.     * @deprecated Function not in use
  478.     */
  479.     function get_real_course_code_select_html($element_name$has_size true$only_current_user_courses true$user_id)
  480.     {
  481.         if ($only_current_user_courses == true)
  482.         {
  483.             $real_course_list CourseManager :: get_real_course_list_of_user_as_course_admin($user_id);
  484.         }
  485.         else
  486.         {
  487.             $real_course_list CourseManager :: get_real_course_list();
  488.         }
  489.  
  490.         if ($has_size == true)
  491.         {
  492.             $size_element "size=\"".SELECT_BOX_SIZE."\"";
  493.         }
  494.         else
  495.         {
  496.             $size_element "";
  497.         }
  498.         $html_code "<select name=\"$element_name\" $size_element >\n";
  499.         foreach ($real_course_list as $real_course)
  500.         {
  501.             $course_code $real_course["code"];
  502.             $html_code .= "<option value=\"".$course_code."\">";
  503.             $html_code .= $course_code;
  504.             $html_code .= "</option>\n";
  505.         }
  506.         $html_code .= "</select>\n";
  507.  
  508.         return $html_code;
  509.     }
  510.  
  511.     /**
  512.     *    Checks wether a parameter exists.
  513.     *    If it doesn't, the function displays an error message.
  514.     *
  515.     *    @return true if parameter is set and not empty, false otherwise
  516.     *    @todo move function to better place, main_api ?
  517.     */
  518.     function check_parameter($parameter$error_message)
  519.     {
  520.         if (!isset ($parameter|| empty ($parameter))
  521.         {
  522.             Display :: display_normal_message($error_message);
  523.             return false;
  524.         }
  525.         return true;
  526.     }
  527.  
  528.     /**
  529.     *    Lets the script die when a parameter check fails.
  530.     *    @todo move function to better place, main_api ?
  531.     */
  532.     function check_parameter_or_fail($parameter$error_message)
  533.     {
  534.         if (!CourseManager :: check_parameter($parameter$error_message))
  535.             die();
  536.     }
  537.  
  538.     /**
  539.     *    @return true if there already are one or more courses
  540.     *     with the same code OR visual_code (visualcode), false otherwise
  541.     */
  542.     function is_existing_course_code($wanted_course_code)
  543.     {
  544.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  545.  
  546.         $sql_query "SELECT COUNT(*) as number FROM ".$course_table."WHERE `code` = '$wanted_course_code' OR `visual_code` = '$wanted_course_code";
  547.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  548.         $result Database::fetch_array($sql_result);
  549.  
  550.         if ($result["number"0)
  551.         {
  552.             return true;
  553.         }
  554.         else
  555.         {
  556.             return false;
  557.         }
  558.     }
  559.  
  560.     /**
  561.     *    @return an array with the course info of all real courses on the platform
  562.     */
  563.     function get_real_course_list()
  564.     {
  565.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  566.         $sql_query "SELECT * FROM $course_table WHERE `target_course_code` IS NULL";
  567.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  568.  
  569.         while ($result Database::fetch_array($sql_result))
  570.         {
  571.             $real_course_list[$result;
  572.         }
  573.  
  574.         return $real_course_list;
  575.     }
  576.  
  577.     /**
  578.     *    @return an array with the course info of all virtual courses on the platform
  579.     */
  580.     function get_virtual_course_list()
  581.     {
  582.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  583.         $sql_query "SELECT * FROM $course_table WHERE `target_course_code` IS NOT NULL";
  584.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  585.  
  586.         while ($result Database::fetch_array($sql_result))
  587.         {
  588.             $virtual_course_list[$result;
  589.         }
  590.         return $virtual_course_list;
  591.     }
  592.  
  593.     /**
  594.     *    @return an array with the course info of the real courses of which
  595.     *     the current user is course admin
  596.     */
  597.     {
  598.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  599.         $course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  600.  
  601.         $sql_query "    SELECT *
  602.                                         FROM $course_table course
  603.                                         LEFT JOIN $course_user_table course_user
  604.                                         ON course.`code` = course_user.`course_code`
  605.                                         WHERE course.`target_course_code` IS NULL
  606.                                             AND course_user.`user_id` = '$user_id'
  607.                                             AND course_user.`status` = '1'";
  608.  
  609.         //api_display_debug_info($sql_query);
  610.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  611.  
  612.         while ($result Database::fetch_array($sql_result))
  613.         {
  614.             $result_array[$result;
  615.         }
  616.  
  617.         return $result_array;
  618.     }
  619.  
  620.     /**
  621.     *    @return an array with the course info of all the courses (real and virtual) of which
  622.     *     the current user is course admin
  623.     */
  624.     function get_course_list_of_user_as_course_admin($user_id)
  625.     {
  626.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  627.         $course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  628.  
  629.         $sql_query "    SELECT *
  630.                                         FROM $course_table course
  631.                                         LEFT JOIN $course_user_table course_user
  632.                                         ON course.`code` = course_user.`course_code`
  633.                                         WHERE course_user.`user_id` = '$user_id'
  634.                                             AND course_user.`status` = '1'";
  635.  
  636.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  637.  
  638.         while ($result Database::fetch_array($sql_result))
  639.         {
  640.             $result_array[$result;
  641.         }
  642.  
  643.         return $result_array;
  644.     }
  645.  
  646.     /**
  647.     * Find out for which courses the user is registered and determine a visual course code and course title from that.
  648.     * Takes virtual courses into account
  649.     *
  650.     * Default case: the name and code stay what they are.
  651.     *
  652.     * Scenarios:
  653.     * - User is registered in real course and virtual courses; name / code become a mix of all
  654.     * - User is registered in real course only: name stays that of real course
  655.     * - User is registered in virtual course only: name becomes that of virtual course
  656.     * - user is not registered to any of the real/virtual courses: name stays that of real course
  657.     * (I'm not sure about the last case, but this seems not too bad)
  658.     *
  659.     * @author Roan Embrechts
  660.     * @param $user_id, the id of the user
  661.     * @param $course_info, an array with course info that you get using Database::get_course_info($course_system_code);
  662.     * @return an array with indices
  663.     *     $return_result["title"] - the course title of the combined courses
  664.     *     $return_result["code"]  - the course code of the combined courses
  665.     */
  666.     function determine_course_title_from_course_info($user_id$course_info)
  667.     {
  668.         $real_course_id $course_info['system_code'];
  669.         $real_course_info Database :: get_course_info($real_course_id);
  670.         $real_course_name $real_course_info["title"];
  671.         $real_course_visual_code $real_course_info["visual_code"];
  672.         $real_course_real_code $course_info['system_code'];
  673.  
  674.         //is the user registered in the real course?
  675.         $table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  676.         $sql_query "SELECT * FROM $table WHERE `user_id` = '$user_id' AND `course_code` = '$real_course_real_code'";
  677.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  678.         $result Database::fetch_array($sql_result);
  679.  
  680.         if (!isset ($result|| empty ($result))
  681.         {
  682.             $user_is_registered_in_real_course false;
  683.         }
  684.         else
  685.         {
  686.             $user_is_registered_in_real_course true;
  687.         }
  688.         //get a list of virtual courses linked to the current real course
  689.         //and to which the current user is subscribed
  690.  
  691.         $user_subscribed_virtual_course_list CourseManager :: get_list_of_virtual_courses_for_specific_user_and_real_course($user_id$real_course_id);
  692.  
  693.         if (count($user_subscribed_virtual_course_list0)
  694.         {
  695.             $virtual_courses_exist true;
  696.         }
  697.         else
  698.         {
  699.             $virtual_courses_exist false;
  700.         }
  701.  
  702.         //now determine course code and name
  703.  
  704.         if ($user_is_registered_in_real_course && $virtual_courses_exist)
  705.         {
  706.             $course_info["name"CourseManager :: create_combined_name($user_is_registered_in_real_course$real_course_name$user_subscribed_virtual_course_list);
  707.             $course_info['official_code'CourseManager :: create_combined_code($user_is_registered_in_real_course$real_course_visual_code$user_subscribed_virtual_course_list);
  708.         }
  709.         else
  710.             if ($user_is_registered_in_real_course)
  711.             {
  712.                 //course name remains real course name
  713.                 $course_info["name"$real_course_name;
  714.                 $course_info['official_code'$real_course_visual_code;
  715.             }
  716.             else
  717.                 if ($virtual_courses_exist)
  718.                 {
  719.                     $course_info["name"CourseManager :: create_combined_name($user_is_registered_in_real_course$real_course_name$user_subscribed_virtual_course_list);
  720.                     $course_info['official_code'CourseManager :: create_combined_code($user_is_registered_in_real_course$real_course_visual_code$user_subscribed_virtual_course_list);
  721.                 }
  722.                 else
  723.                 {
  724.                     //course name remains real course name
  725.                     $course_info["name"$real_course_name;
  726.                     $course_info['official_code'$real_course_visual_code;
  727.                 }
  728.  
  729.         $return_result["title"$course_info["name"];
  730.         $return_result["code"$course_info['official_code'];
  731.         return $return_result;
  732.     }
  733.  
  734.     /**
  735.     * Create a course title based on all real and virtual courses the user is registered in.
  736.     * @param boolean $user_is_registered_in_real_course 
  737.     * @param string $real_course_name, the title of the real course
  738.     * @param array $virtual_course_list, the list of virtual courses
  739.     */
  740.     function create_combined_name($user_is_registered_in_real_course$real_course_name$virtual_course_list)
  741.     {
  742.         if ($user_is_registered_in_real_course || count($virtual_course_list1)
  743.         {
  744.             $complete_course_name_before get_lang("CombinedCourse")." "//from course_home lang file
  745.         }
  746.  
  747.         if ($user_is_registered_in_real_course)
  748.         {
  749.             //add real name to result
  750.             $complete_course_name[$real_course_name;
  751.         }
  752.  
  753.         //add course titles of all virtual courses to the list
  754.         foreach ($virtual_course_list as $current_course)
  755.         {
  756.             $complete_course_name[$current_course["title"];
  757.         }
  758.  
  759.         $complete_course_name $complete_course_name_before.implode(' &amp; '$complete_course_name);
  760.  
  761.         return $complete_course_name;
  762.     }
  763.  
  764.     /**
  765.     *    Create a course code based on all real and virtual courses the user is registered in.
  766.     */
  767.     function create_combined_code($user_is_registered_in_real_course$real_course_code$virtual_course_list)
  768.     {
  769.         $complete_course_code .= "";
  770.  
  771.         if ($user_is_registered_in_real_course)
  772.         {
  773.             //add real name to result
  774.             $complete_course_code[$real_course_code;
  775.         }
  776.  
  777.         //add course titles of all virtual courses to the list
  778.         foreach ($virtual_course_list as $current_course)
  779.         {
  780.             $complete_course_code[$current_course["visual_code"];
  781.         }
  782.  
  783.         $complete_course_code implode(' &amp; '$complete_course_code);
  784.  
  785.         return $complete_course_code;
  786.     }
  787.  
  788.     /**
  789.     *    Return course info array of virtual course
  790.     *
  791.     *    Note this is different from getting information about a real course!
  792.     *
  793.     *    @param $real_course_code, the id of the real course which the virtual course is linked to
  794.     */
  795.     function get_virtual_course_info($real_course_code)
  796.     {
  797.         $table Database :: get_main_table(TABLE_MAIN_COURSE);
  798.         $sql_query "SELECT * FROM $table WHERE `target_course_code` = '$real_course_code'";
  799.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  800.         $result array ();
  801.         while ($virtual_course Database::fetch_array($sql_result))
  802.         {
  803.             $result[$virtual_course;
  804.         }
  805.         return $result;
  806.     }
  807.  
  808.     /**
  809.     *    @param string $system_code, the system code of the course
  810.     *    @return true if the course is a virtual course, false otherwise
  811.     */
  812.     function is_virtual_course_from_system_code($system_code)
  813.     {
  814.         $table Database :: get_main_table(TABLE_MAIN_COURSE);
  815.         $sql_query "SELECT * FROM $table WHERE `code` = '$system_code'";
  816.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  817.         $result Database::fetch_array($sql_result);
  818.         $target_number $result["target_course_code"];
  819.  
  820.         if ($target_number == NULL)
  821.         {
  822.             return false//this is not a virtual course
  823.         }
  824.         else
  825.         {
  826.             return true//this is a virtual course
  827.         }
  828.     }
  829.  
  830.     /**
  831.     *    What's annoying is that you can't overload functions in PHP.
  832.     *    @return true if the course is a virtual course, false otherwise
  833.     */
  834.     function is_virtual_course_from_visual_code($visual_code)
  835.     {
  836.         $table Database :: get_main_table(TABLE_MAIN_COURSE);
  837.         $sql_query "SELECT * FROM $table WHERE `visual_code` = '$visual_code'";
  838.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  839.         $result Database::fetch_array($sql_result);
  840.         $target_number $result["target_course_code"];
  841.  
  842.         if ($target_number == NULL)
  843.         {
  844.             return false//this is not a virtual course
  845.         }
  846.         else
  847.         {
  848.             return true//this is a virtual course
  849.         }
  850.     }
  851.  
  852.     /**
  853.     * @return true if the real course has virtual courses that the user is subscribed to, false otherwise
  854.     */
  855.     function has_virtual_courses_from_code($real_course_code$user_id)
  856.     {
  857.         $user_subscribed_virtual_course_list CourseManager :: get_list_of_virtual_courses_for_specific_user_and_real_course($user_id$real_course_code);
  858.         $number_of_virtual_courses count($user_subscribed_virtual_course_list);
  859.  
  860.         if (count($user_subscribed_virtual_course_list0)
  861.         {
  862.             return true;
  863.         }
  864.         else
  865.         {
  866.             return false;
  867.         }
  868.     }
  869.  
  870.     /**
  871.     *    Return an array of arrays, listing course info of all virtual course
  872.     *    linked to the real course ID $real_course_code
  873.     *
  874.     *    @param $real_course_code, the id of the real course which the virtual courses are linked to
  875.     */
  876.     function get_virtual_courses_linked_to_real_course($real_course_code)
  877.     {
  878.         $table Database :: get_main_table(TABLE_MAIN_COURSE);
  879.         $sql_query "SELECT * FROM $table WHERE `target_course_code` = '$real_course_code'";
  880.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  881.         $result_array array ();
  882.         while ($result Database::fetch_array($sql_result))
  883.         {
  884.             $result_array[$result;
  885.         }
  886.  
  887.         return $result_array;
  888.     }
  889.  
  890.     /**
  891.     * This function returns the course code of the real course
  892.     * to which a virtual course is linked.
  893.     *
  894.     * @param the course code of the virtual course
  895.     * @return the course code of the real course
  896.     */
  897.     function get_target_of_linked_course($virtual_course_code)
  898.     {
  899.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  900.  
  901.         //get info about the virtual course
  902.         $sql_query "SELECT * FROM $course_table WHERE `code` = '$virtual_course_code'";
  903.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  904.         $result Database::fetch_array($sql_result);
  905.         $target_course_code $result["target_course_code"];
  906.  
  907.         return $target_course_code;
  908.     }
  909.  
  910.     /*
  911.     ==============================================================================
  912.         USER FUNCTIONS
  913.     ==============================================================================
  914.     */
  915.  
  916.     /**
  917.     * Return course info array of virtual course
  918.     *
  919.     * @param $user_id, the id (int) of the user
  920.     * @param $course_info, array with info about the course (comes from course table)
  921.     *
  922.     * @return true if the user is registered in the course, false otherwise
  923.     */
  924.     function is_user_subscribed_in_course($user_id$course_code$in_a_session=false)
  925.     {
  926.         $user_id intval($user_id);
  927.         $course_code addslashes($course_code);
  928.         
  929.         $table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  930.  
  931.         $sql_query "SELECT * FROM $table WHERE `user_id` = '$user_id' AND `course_code` = '$course_code'";
  932.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  933.         $result Database::fetch_array($sql_result);
  934.  
  935.         if (!isset ($result|| empty ($result))
  936.         {
  937.             if($in_a_session)
  938.             {
  939.                 $sql 'SELECT 1 FROM '.Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER).'
  940.                         WHERE id_user = '.$user_id.' AND course_code="'.$course_code.'"';
  941.                 
  942.                 $rs api_sql_query($sql__FILE____LINE__);
  943.                 if(Database::num_rows($rs)>0)
  944.                 {
  945.                     return true;
  946.                 }
  947.                 else{
  948.                     $sql 'SELECT 1 FROM '.Database :: get_main_table(TABLE_MAIN_SESSION_COURSE).'
  949.                         WHERE id_coach = '.$user_id.' AND course_code="'.$course_code.'"';
  950.                     $rs api_sql_query($sql__FILE____LINE__);
  951.                     if(Database::num_rows($rs)>0)
  952.                     {
  953.                         return true;
  954.                     }
  955.                 }
  956.             }
  957.             else
  958.                 return false//user is not registered in course
  959.         }
  960.         else
  961.         {
  962.             return true//user is registered in course
  963.         }
  964.     }
  965.     
  966.     /**
  967.     *    Is the user a teacher in the given course?
  968.     *
  969.     *    @param $user_id, the id (int) of the user
  970.     *    @param $course_code, the course code
  971.     *
  972.     *    @return true if the user is a teacher in the course, false otherwise
  973.     */
  974.     function is_course_teacher($user_id,$course_code)
  975.     {
  976.         $tbl_course_user    Database::get_main_table(TABLE_MAIN_COURSE_USER);
  977.         $sql_query='SELECT status FROM '.$tbl_course_user.' WHERE course_code="'.$course_code.'" and user_id="'.$user_id.'"';
  978.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  979.         if(Database::num_rows($sql_result)>0)
  980.         {
  981.             $status=Database::result($sql_result,0,'status');
  982.             if($status==1return trueelse return false;
  983.         }
  984.         return false;
  985.     }
  986.  
  987.     /**
  988.     *    Is the user subscribed in the real course or linked courses?
  989.     *
  990.     *    @param $user_id, the id (int) of the user
  991.     *    @param $course_info, array with info about the course (comes from course table, see database lib)
  992.     *
  993.     *    @return true if the user is registered in the real course or linked courses, false otherwise
  994.     */
  995.     function is_user_subscribed_in_real_or_linked_course($user_id$course_code$session_id='')
  996.     {
  997.         if($session_id==''){
  998.             $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  999.             $course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1000.  
  1001.             $sql_query "    SELECT *
  1002.                                 FROM $course_table course
  1003.                                 LEFT JOIN $course_user_table course_user
  1004.                                 ON course.`code` = course_user.`course_code`
  1005.                                 WHERE course_user.`user_id` = '$user_id' AND ( course.`code` = '$course_code' OR `target_course_code` = '$course_code') ";
  1006.  
  1007.             $sql_result api_sql_query($sql_query__FILE____LINE__);
  1008.             $result Database::fetch_array($sql_result);
  1009.  
  1010.             if (!isset ($result|| empty ($result))
  1011.             {
  1012.                 return false//user is not registered in course
  1013.             }
  1014.             else
  1015.             {
  1016.                 return true//user is registered in course
  1017.             }
  1018.         }
  1019.         else {
  1020.             // is he subscribed to the course of the session ?
  1021.             // Database Table Definitions
  1022.             $tbl_sessions            Database::get_main_table(TABLE_MAIN_SESSION);
  1023.             $tbl_sessions_course    Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1024.             $tbl_session_course_userDatabase::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1025.  
  1026.  
  1027.             //users
  1028.             $sql "SELECT id_user
  1029.                     FROM $tbl_session_course_user
  1030.                     WHERE id_session='".$_SESSION['id_session']."'
  1031.                     AND id_user='$user_id'";
  1032.  
  1033.             $result api_sql_query($sql,__FILE__,__LINE__);
  1034.             if(Database::num_rows($result))
  1035.                 return true;
  1036.  
  1037.             // is it a course coach ?
  1038.             $sql "SELECT id_coach
  1039.                     FROM $tbl_sessions_course AS session_course
  1040.                     WHERE id_session='".$_SESSION['id_session']."'
  1041.                     AND id_coach = '$user_id'
  1042.                     AND course_code='$course_code'";
  1043.  
  1044.             $result api_sql_query($sql,__FILE__,__LINE__);
  1045.             if(Database::num_rows($result))
  1046.                 return true;
  1047.  
  1048.             // is it a session coach ?
  1049.             $sql "SELECT id_coach
  1050.                     FROM $tbl_sessions AS session
  1051.                     WHERE session.id='".$_SESSION['id_session']."'
  1052.                     AND id_coach='$user_id'";
  1053.  
  1054.             $result api_sql_query($sql,__FILE__,__LINE__);
  1055.             if(Database::num_rows($result))
  1056.                 return true;
  1057.  
  1058.             return false;
  1059.  
  1060.         }
  1061.     }
  1062.     
  1063.     /**
  1064.     *    Return user info array of all users registered in the specified real or virtual course
  1065.     *    This only returns the users that are registered in this actual course, not linked courses.
  1066.     *
  1067.     *    @param string $course_code 
  1068.     *    @return array with user info
  1069.     */
  1070.     function get_user_list_from_course_code($course_code$with_session=true$session_id=0$limit=''$order_by='')
  1071.     {        
  1072.         $session_id intval($session_id);
  1073.         $a_users array();        
  1074.         $table_users Database :: get_main_table(TABLE_MAIN_USER);
  1075.         
  1076.         $where array();
  1077.         
  1078.         $sql 'SELECT DISTINCT user.user_id ';
  1079.         if $session_id == {
  1080.             $sql .= ', course_rel_user.role, course_rel_user.tutor_id ';
  1081.         }        
  1082.         $sql .= ' FROM '.$table_users.' as user ';
  1083.  
  1084.         if(api_get_setting('use_session_mode')=='true' && $with_session)
  1085.         {
  1086.             $table_session_course_user Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1087.             $sql .= ' LEFT JOIN '.$table_session_course_user.' as session_course_user
  1088.                          ON user.user_id = session_course_user.id_user
  1089.                          AND session_course_user.course_code="'.Database::escape_string($course_code).'"';
  1090.             if($session_id!=0
  1091.             {
  1092.                 $sql .= ' AND session_course_user.id_session = '.$session_id;
  1093.             }
  1094.             $where[' session_course_user.course_code IS NOT NULL ';
  1095.         }
  1096.         
  1097.         if($session_id == 0)
  1098.         {
  1099.             $table_course_user Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1100.             $sql .= ' LEFT JOIN '.$table_course_user.' as course_rel_user
  1101.                         ON user.user_id = course_rel_user.user_id
  1102.                         AND course_rel_user.course_code="'.Database::escape_string($course_code).'"';
  1103.             $where[' course_rel_user.course_code IS NOT NULL ';
  1104.         }        
  1105.         
  1106.         $sql .= ' WHERE '.implode(' OR ',$where);
  1107.                   
  1108.         $sql .= ' '.$order_by;
  1109.         $sql .= ' '.$limit;
  1110.  
  1111.         $rs api_sql_query($sql__FILE____LINE__);
  1112.         
  1113.         while($user Database::fetch_array($rs))
  1114.         {
  1115.             $user_infos Database :: get_user_info_from_id($user['user_id']);
  1116.             //$user_infos['status'] = $user['status'];
  1117.             if isset($user['role']) ) {
  1118.                 $user_infos['role'$user['role'];
  1119.             }
  1120.             if isset($user['tutor_id']) ) {
  1121.                 $user_infos['tutor_id'$user['tutor_id'];
  1122.             }                
  1123.             $a_users[$user['user_id']] $user_infos
  1124.         }    
  1125.         
  1126.         return $a_users;
  1127.         
  1128.     }
  1129.     
  1130.     
  1131.     function get_coach_list_from_course_code($course_code,$session_id){
  1132.         
  1133.         $table_session_course Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  1134.         $table_session Database :: get_main_table(TABLE_MAIN_SESSION);
  1135.         
  1136.         $a_users=array();
  1137.         
  1138.         //We get the coach for the given course in a given session
  1139.         $sql 'SELECT id_coach FROM '.$table_session_course.' WHERE id_session="'.$session_id.'" AND course_code="'.$course_code.'"';
  1140.         $rs api_sql_query($sql__FILE____LINE__);
  1141.         while($user Database::fetch_array($rs))
  1142.         {
  1143.             $user_infos Database :: get_user_info_from_id($user['id_coach']);
  1144.             $user_infos["status"$user["status"];
  1145.             $user_infos["role"$user["role"];
  1146.             $user_infos["tutor_id"$user["tutor_id"];
  1147.             $user_infos['email'$user['email'];
  1148.             $a_users[$user['id_coach']] $user_infos;
  1149.         }
  1150.         
  1151.         //We get the session coach
  1152.         $sql 'SELECT id_coach FROM '.$table_session.' WHERE id="'.$session_id.'"';
  1153.         $rs api_sql_query($sql__FILE____LINE__);
  1154.         $user_infos=array();
  1155.         $session_id_coach Database::result($rs,0,'id_coach');
  1156.         $user_infos Database :: get_user_info_from_id($session_id_coach);
  1157.         $user_infos["status"$user["status"];
  1158.         $user_infos["role"$user["role"];
  1159.         $user_infos["tutor_id"$user["tutor_id"];
  1160.         $user_infos['email'$user['email'];
  1161.         $a_users[$session_id_coach$user_infos;
  1162.         
  1163.         return $a_users;
  1164.         
  1165.     }
  1166.     
  1167.     
  1168.     /**
  1169.     *    Return user info array of all users registered in the specified real or virtual course
  1170.     *    This only returns the users that are registered in this actual course, not linked courses.
  1171.     *
  1172.     *    @param string $course_code 
  1173.     *    @param boolean $full list to true if we want sessions students too
  1174.     *    @return array with user id
  1175.     */
  1176.     function get_student_list_from_course_code($course_code$with_session=false$session_id=0)
  1177.     {
  1178.         $a_students array();
  1179.         
  1180.         $session_id intval($session_id);
  1181.         
  1182.         if($session_id == 0)
  1183.         {
  1184.             // students directly subscribed to the course
  1185.             $table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1186.             $sql_query "SELECT * FROM $table WHERE `course_code` = '$course_code' AND `status` = 5";
  1187.             $rs api_sql_query($sql_query__FILE____LINE__);
  1188.             while($student Database::fetch_array($rs))
  1189.             {
  1190.                 $a_students[$student['user_id']] $student
  1191.             }
  1192.         }
  1193.         
  1194.         // students subscribed to the course through a session
  1195.         
  1196.         if(api_get_setting('use_session_mode')=='true' && $with_session)
  1197.         {
  1198.             $table Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1199.             $sql_query "SELECT * FROM $table WHERE `course_code` = '$course_code'";
  1200.             if($session_id!=0)
  1201.                 $sql_query .= ' AND id_session = '.$session_id;
  1202.             $rs api_sql_query($sql_query__FILE____LINE__);
  1203.             while($student Database::fetch_array($rs))
  1204.             {
  1205.                 $a_students[$student['id_user']] $student
  1206.             }
  1207.         }
  1208.         
  1209.         return $a_students;
  1210.     }
  1211.     /**
  1212.     *    Return user info array of all teacher-users registered in the specified real or virtual course
  1213.     *    This only returns the users that are registered in this actual course, not linked courses.
  1214.     *
  1215.     *    @param string $course_code 
  1216.     *    @return array with user id
  1217.     */
  1218.     function get_teacher_list_from_course_code($course_code)
  1219.     {
  1220.         $a_teachers array();
  1221.         // teachers directly subscribed to the course
  1222.         $t_cu Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1223.         $t_u Database :: get_main_table(TABLE_MAIN_USER);
  1224.         $sql_query "SELECT u.user_id, u.lastname, u.firstname, u.email, u.username, u.status " .
  1225.                 "FROM $t_cu cu, $t_u u .
  1226.                 "WHERE cu.course_code = '$course_code.
  1227.                 "AND cu.status = 1 " .
  1228.                 "AND cu.user_id = u.user_id";
  1229.         $rs api_sql_query($sql_query__FILE____LINE__);
  1230.         while($teacher Database::fetch_array($rs))
  1231.         {
  1232.             $a_students[$teacher['user_id']] $teacher
  1233.         }
  1234.         return $a_students;
  1235.     }
  1236.  
  1237.     /**
  1238.     *    Return user info array of all users registered in the specified course
  1239.     *    this includes the users of the course itsel and the users of all linked courses.
  1240.     *
  1241.     *    @param array $course_info 
  1242.     *    @return array with user info
  1243.     */
  1244.     function get_real_and_linked_user_list($course_code$with_sessions true$session_id=0)
  1245.     {
  1246.         //get list of virtual courses
  1247.         $virtual_course_list CourseManager :: get_virtual_courses_linked_to_real_course($course_code);
  1248.  
  1249.         //get users from real course
  1250.         $user_list CourseManager :: get_user_list_from_course_code($course_code$with_sessions$session_id);
  1251.         foreach ($user_list as $this_user)
  1252.         {
  1253.             $complete_user_list[$this_user;
  1254.         }
  1255.  
  1256.         //get users from linked courses
  1257.         foreach ($virtual_course_list as $this_course)
  1258.         {
  1259.             $course_code $this_course["code"];
  1260.             $user_list CourseManager :: get_user_list_from_course_code($course_code$with_sessions$session_id);
  1261.             foreach ($user_list as $this_user)
  1262.             {
  1263.                 $complete_user_list[$this_user;
  1264.             }
  1265.         }
  1266.  
  1267.         return $complete_user_list;
  1268.     }
  1269.  
  1270.     /**
  1271.     *    Return an array of arrays, listing course info of all courses in the list
  1272.     *    linked to the real course $real_course_code, to which the user $user_id is subscribed.
  1273.     *
  1274.     *    @param $user_id, the id (int) of the user
  1275.     *    @param $real_course_code, the id (char) of the real course
  1276.     *
  1277.     *    @return array of course info arrays
  1278.     */
  1279.     function get_list_of_virtual_courses_for_specific_user_and_real_course($user_id$real_course_code)
  1280.     {
  1281.         $result_array array();
  1282.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  1283.         $course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1284.  
  1285.  
  1286.         $sql_query "    SELECT *
  1287.                             FROM $course_table course
  1288.                             LEFT JOIN $course_user_table course_user
  1289.                             ON course.`code` = course_user.`course_code`
  1290.                             WHERE course.`target_course_code` = '$real_course_code' AND course_user.`user_id` = '$user_id'";
  1291.         
  1292.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  1293.  
  1294.         while ($result Database::fetch_array($sql_result))
  1295.         {
  1296.             $result_array[$result;
  1297.         }
  1298.  
  1299.         return $result_array;
  1300.     }
  1301.  
  1302.     /*
  1303.     ==============================================================================
  1304.         GROUP FUNCTIONS
  1305.     ==============================================================================
  1306.     */
  1307.  
  1308.     function get_group_list_of_course($course_code)
  1309.     {
  1310.         $course_info Database :: get_course_info($course_code);
  1311.         $database_name $course_info['db_name'];
  1312.         $group_table Database :: get_course_table(TABLE_GROUP$database_name);
  1313.         $group_user_table Database :: get_course_table(TABLE_GROUP_USER$database_name);
  1314.  
  1315.         $sql "SELECT g.id, g.name, COUNT(gu.id) userNb
  1316.                                 FROM $group_table AS g LEFT JOIN $group_user_table gu
  1317.                                 ON g.id = gu.group_id
  1318.                                 GROUP BY g.id
  1319.                                 ORDER BY g.name";
  1320.  
  1321.         $result api_sql_query($sql__FILE____LINE__or die(mysql_error());
  1322.         while ($group_data Database::fetch_array($result))
  1323.         {
  1324.             $group_list[$group_data['id']] $group_data;
  1325.         }
  1326.         return $group_list;
  1327.     }
  1328.  
  1329.     /**
  1330.     *    Checks all parameters needed to create a virtual course.
  1331.     *    If they are all set, the virtual course creation procedure is called.
  1332.     *
  1333.     *    Call this function instead of create_virtual_course
  1334.     */
  1335.     function attempt_create_virtual_course($real_course_code$course_title$wanted_course_code$course_language$course_category)
  1336.     {
  1337.         //better: create parameter list, check the entire list, when false display errormessage
  1338.         CourseManager :: check_parameter_or_fail($real_course_code"Unspecified parameter: real course id.");
  1339.         CourseManager :: check_parameter_or_fail($course_title"Unspecified parameter: course title.");
  1340.         CourseManager :: check_parameter_or_fail($wanted_course_code"Unspecified parameter: wanted course code.");
  1341.         CourseManager :: check_parameter_or_fail($course_language"Unspecified parameter: course language.");
  1342.         CourseManager :: check_parameter_or_fail($course_category"Unspecified parameter: course category.");
  1343.  
  1344.         $creation_success CourseManager :: create_virtual_course($real_course_code$course_title$wanted_course_code$course_language$course_category);
  1345.  
  1346.         return $creation_success;
  1347.     }
  1348.  
  1349.     /**
  1350.     *    This function creates a virtual course.
  1351.     *    It assumes all parameters have been checked and are not empty.
  1352.     *    It checks wether a course with the $wanted_course_code already exists.
  1353.     *
  1354.     *    Users of this library should consider this function private,
  1355.     *    please call attempt_create_virtual_course instead of this one.
  1356.     *
  1357.     *    NOTE:
  1358.     *    The virtual course 'owner' id (the first course admin) is set to the CURRENT user id.
  1359.     *    @return true if the course creation succeeded, false otherwise
  1360.     *    @todo research: expiration date of a course
  1361.     */
  1362.     function create_virtual_course($real_course_code$course_title$wanted_course_code$course_language$course_category)
  1363.     {
  1364.         global $firstExpirationDelay;
  1365.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  1366.         $user_id api_get_user_id();
  1367.         $real_course_info Database :: get_course_info($real_course_code);
  1368.         $real_course_code $real_course_info["system_code"];
  1369.  
  1370.         //check: virtual course creation fails if another course has the same
  1371.         //code, real or fake.
  1372.         if (CourseManager :: is_existing_course_code($wanted_course_code))
  1373.         {
  1374.             Display :: display_error_message($wanted_course_code." - ".get_lang("CourseCodeAlreadyExists"));
  1375.             return false;
  1376.         }
  1377.  
  1378.         //add data to course table, course_rel_user
  1379.         $course_sys_code $wanted_course_code;
  1380.         $course_screen_code $wanted_course_code;
  1381.         $course_repository $real_course_info["directory"];
  1382.         $course_db_name $real_course_info["db_name"];
  1383.         $responsible_teacher $real_course_info["tutor_name"];
  1384.         $faculty_shortname $course_category;
  1385.         // $course_title = $course_title;
  1386.         // $course_language = $course_language;
  1387.         $teacher_id $user_id;
  1388.  
  1389.         //HACK ----------------------------------------------------------------
  1390.         $expiration_date time($firstExpirationDelay;
  1391.         //END HACK ------------------------------------------------------------
  1392.  
  1393.         register_course($course_sys_code$course_screen_code$course_repository$course_db_name$responsible_teacher$faculty_shortname$course_title$course_language$teacher_id$expiration_date);
  1394.  
  1395.         //above was the normal course creation table update call,
  1396.         //now one more thing: fill in the target_course_code field
  1397.  
  1398.         $sql_query "UPDATE $course_table SET `target_course_code` = '$real_course_code' WHERE `code` = '$course_sys_code' LIMIT 1 ";
  1399.         api_sql_query($sql_query__FILE____LINE__);
  1400.  
  1401.         return true;
  1402.     }
  1403.  
  1404.     /**
  1405.      * Delete a course
  1406.      * This function deletes a whole course-area from the platform. When the
  1407.      * given course is a virtual course, the database and directory will not be
  1408.      * deleted.
  1409.      * When the given course is a real course, also all virtual courses refering
  1410.      * to the given course will be deleted.
  1411.      * Considering the fact that we remove all traces of the course in the main
  1412.      * database, it makes sense to remove all tracking as well (if stats databases exist)
  1413.      * so that a new course created with this code would not use the remains of an older
  1414.      * course.
  1415.      * 
  1416.      * @param string $code The code of the course to delete
  1417.      * @todo When deleting a virtual course: unsubscribe users from that virtual
  1418.      *  course from the groups in the real course if they are not subscribed in
  1419.      *  that real course.
  1420.      * @todo Remove globals
  1421.      */
  1422.     function delete_course($code)
  1423.     {
  1424.         global $_configuration;
  1425.         
  1426.         $table_course Database :: get_main_table(TABLE_MAIN_COURSE);
  1427.         $table_course_user Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1428.         $table_course_class Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  1429.         $user_role_table Database :: get_main_table(MAIN_USER_ROLE_TABLE);
  1430.         $location_table Database::get_main_table(MAIN_LOCATION_TABLE);
  1431.         $role_right_location_table Database::get_main_table(MAIN_ROLE_RIGHT_LOCATION_TABLE);
  1432.         $table_session_course Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1433.         $table_session_course_user Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1434.         $table_course_survey Database::get_main_table(TABLE_MAIN_SHARED_SURVEY);
  1435.         $table_course_survey_question Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  1436.         $table_course_survey_question_option Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1437.         $stats false;
  1438.         if(Database::get_statistic_database(!= ''){
  1439.             $stats true;
  1440.             $table_stats_hotpots Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  1441.             $table_stats_attempt Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1442.             $table_stats_exercises Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1443.             $table_stats_access Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  1444.             $table_stats_lastaccess Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1445.             $table_stats_course_access Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1446.             $table_stats_online Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  1447.             $table_stats_default Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  1448.             $table_stats_downloads Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  1449.             $table_stats_links Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  1450.             $table_stats_uploads Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
  1451.         }
  1452.  
  1453.         $sql "SELECT * FROM $table_course WHERE code='".$code."'";
  1454.         $res api_sql_query($sql__FILE____LINE__);
  1455.         if (Database::num_rows($res== 0)
  1456.         {
  1457.             return;
  1458.         }
  1459.         $this_course Database::fetch_array($res);
  1460.         $db_name $this_course['db_name'];
  1461.         CourseManager :: create_database_dump($code);
  1462.         if (!CourseManager :: is_virtual_course_from_system_code($code))
  1463.         {
  1464.             // If this is not a virtual course, look for virtual courses that depend on this one, if any
  1465.             $virtual_courses CourseManager :: get_virtual_courses_linked_to_real_course($code);
  1466.             foreach ($virtual_courses as $index => $virtual_course)
  1467.             {
  1468.                 // Unsubscribe all classes from the virtual course
  1469.                 $sql "DELETE FROM $table_course_class WHERE course_code='".$virtual_course['code']."'";
  1470.                 api_sql_query($sql__FILE____LINE__);
  1471.                 // Unsubscribe all users from the virtual course
  1472.                 $sql "DELETE FROM $table_course_user WHERE course_code='".$virtual_course['code']."'";
  1473.                 api_sql_query($sql__FILE____LINE__);
  1474.                 // Delete the course from the sessions tables
  1475.                 $sql "DELETE FROM $table_session_course WHERE course_code='".$virtual_course['code']."'";
  1476.                 api_sql_query($sql,__FILE__,__LINE__);
  1477.                 $sql "DELETE FROM $table_session_course_user WHERE course_code='".$virtual_course['code']."'";
  1478.                 api_sql_query($sql,__FILE__,__LINE__);
  1479.                 // Delete the course from the survey tables
  1480.                 $sql "DELETE FROM $table_course_survey WHERE course_code='".$virtual_course['code']."'";
  1481.                 api_sql_query($sql,__FILE__,__LINE__);
  1482.                 $sql "DELETE FROM $table_course_survey_user WHERE db_name='".$virtual_course['db_name']."'";
  1483.                 api_sql_query($sql,__FILE__,__LINE__);
  1484.                 $sql "DELETE FROM $table_course_survey_reminder WHERE db_name='".$virtual_course['db_name']."'";
  1485.                 api_sql_query($sql,__FILE__,__LINE__);
  1486.                 
  1487.                 // Delete the course from the stats tables
  1488.                 if($stats)
  1489.                 {
  1490.                     $sql "DELETE FROM $table_stats_hotpots WHERE exe_cours_id = '".$virtual_course['code']."'";
  1491.                     api_sql_query($sql,__FILE__,__LINE__);
  1492.                     $sql "DELETE FROM $table_stats_attempt WHERE course_code = '".$virtual_course['code']."'";
  1493.                     api_sql_query($sql,__FILE__,__LINE__);
  1494.                     $sql "DELETE FROM $table_stats_exercises WHERE exe_cours_id = '".$virtual_course['code']."'";
  1495.                     api_sql_query($sql,__FILE__,__LINE__);
  1496.                     $sql "DELETE FROM $table_stats_access WHERE access_cours_code = '".$virtual_course['code']."'";
  1497.                     api_sql_query($sql,__FILE__,__LINE__);
  1498.                     $sql "DELETE FROM $table_stats_lastaccess WHERE access_cours_code = '".$virtual_course['code']."'";
  1499.                     api_sql_query($sql,__FILE__,__LINE__);
  1500.                     $sql "DELETE FROM $table_stats_course_access WHERE course_code = '".$virtual_course['code']."'";
  1501.                     api_sql_query($sql,__FILE__,__LINE__);
  1502.                     $sql "DELETE FROM $table_stats_online WHERE course = '".$virtual_course['code']."'";
  1503.                     api_sql_query($sql,__FILE__,__LINE__);
  1504.                     $sql "DELETE FROM $table_stats_default WHERE default_cours_code = '".$virtual_course['code']."'";
  1505.                     api_sql_query($sql,__FILE__,__LINE__);
  1506.                     $sql "DELETE FROM $table_stats_downloads WHERE down_cours_id = '".$virtual_course['code']."'";
  1507.                     api_sql_query($sql,__FILE__,__LINE__);
  1508.                     $sql "DELETE FROM $table_stats_links WHERE links_cours_id = '".$virtual_course['code']."'";
  1509.                     api_sql_query($sql,__FILE__,__LINE__);
  1510.                     $sql "DELETE FROM $table_stats_uploads WHERE upload_cours_id = '".$virtual_course['code']."'";
  1511.                     api_sql_query($sql,__FILE__,__LINE__);
  1512.                 }                
  1513.                 
  1514.                 // Delete the course from the course table
  1515.                 $sql "DELETE FROM $table_course WHERE code='".$virtual_course['code']."'";
  1516.                 api_sql_query($sql__FILE____LINE__);
  1517.             }
  1518.             $sql "SELECT * FROM $table_course WHERE code='".$code."'";
  1519.             $res api_sql_query($sql__FILE____LINE__);
  1520.             $course Database::fetch_array($res);
  1521.             if (!$_configuration['single_database'])
  1522.             {
  1523.                 $sql "DROP DATABASE IF EXISTS ".$course['db_name'];
  1524.                 api_sql_query($sql__FILE____LINE__);
  1525.             }
  1526.             else
  1527.             {
  1528.                 //TODO Clean the following code as currently it would probably delete another course
  1529.                 //similarly named, by mistake...
  1530.                 $db_pattern $_configuration['table_prefix'].$course['db_name'].$_configuration['db_glue'];
  1531.                 $sql "SHOW TABLES LIKE '$db_pattern%'";
  1532.                 $result api_sql_query($sql__FILE____LINE__);
  1533.                 while (list ($courseTableDatabase::fetch_array($result))
  1534.                 {
  1535.                     api_sql_query("DROP TABLE `$courseTable`"__FILE____LINE__);
  1536.                 }
  1537.             }
  1538.             $course_dir api_get_path(SYS_COURSE_PATH).$course['directory'];
  1539.             $garbage_dir api_get_path(GARBAGE_PATH).$course['directory'].'_'.time();
  1540.             rename($course_dir$garbage_dir);
  1541.         }
  1542.  
  1543.         // Unsubscribe all classes from the course
  1544.         $sql "DELETE FROM $table_course_class WHERE course_code='".$code."'";
  1545.         api_sql_query($sql__FILE____LINE__);
  1546.         // Unsubscribe all users from the course
  1547.         $sql "DELETE FROM $table_course_user WHERE course_code='".$code."'";
  1548.         api_sql_query($sql__FILE____LINE__);
  1549.         // Delete the course from the sessions tables
  1550.         $sql "DELETE FROM $table_session_course WHERE course_code='".$code."'";
  1551.         api_sql_query($sql,__FILE__,__LINE__);
  1552.         $sql "DELETE FROM $table_session_course_user WHERE course_code='".$code."'";
  1553.         api_sql_query($sql,__FILE__,__LINE__);
  1554.         
  1555.         $sql='SELECT survey_id FROM '.$table_course_survey.' WHERE course_code="'.$code.'"';
  1556.         $result_surveys=api_sql_query($sql);
  1557.         while($surveys=Database::fetch_array($result_surveys)){
  1558.             $survey_id=$surveys[0];
  1559.             $sql='DELETE FROM '.$table_course_survey_question.' WHERE survey_id="'.$survey_id.'"';
  1560.             api_sql_query($sql,__FILE__,__LINE__);
  1561.             $sql='DELETE FROM '.$table_course_survey_question_option.' WHERE survey_id="'.$survey_id.'"';
  1562.             api_sql_query($sql,__FILE__,__LINE__);
  1563.             $sql='DELETE FROM '.$table_course_survey.' WHERE survey_id="'.$survey_id.'"';
  1564.             api_sql_query($sql,__FILE__,__LINE__);
  1565.         }
  1566.  
  1567.         // Delete the course from the stats tables
  1568.         if($stats)
  1569.         {
  1570.             $sql "DELETE FROM $table_stats_hotpots WHERE exe_cours_id = '".$code."'";
  1571.             api_sql_query($sql,__FILE__,__LINE__);
  1572.             $sql "DELETE FROM $table_stats_attempt WHERE course_code = '".$code."'";
  1573.             api_sql_query($sql,__FILE__,__LINE__);
  1574.             $sql "DELETE FROM $table_stats_exercises WHERE exe_cours_id = '".$code."'";
  1575.             api_sql_query($sql,__FILE__,__LINE__);
  1576.             $sql "DELETE FROM $table_stats_access WHERE access_cours_code = '".$code."'";
  1577.             api_sql_query($sql,__FILE__,__LINE__);
  1578.             $sql "DELETE FROM $table_stats_lastaccess WHERE access_cours_code = '".$code."'";
  1579.             api_sql_query($sql,__FILE__,__LINE__);
  1580.             $sql "DELETE FROM $table_stats_course_access WHERE course_code = '".$code."'";
  1581.             api_sql_query($sql,__FILE__,__LINE__);
  1582.             $sql "DELETE FROM $table_stats_online WHERE course = '".$code."'";
  1583.             api_sql_query($sql,__FILE__,__LINE__);
  1584.             $sql "DELETE FROM $table_stats_default WHERE default_cours_code = '".$code."'";
  1585.             api_sql_query($sql,__FILE__,__LINE__);
  1586.             $sql "DELETE FROM $table_stats_downloads WHERE down_cours_id = '".$code."'";
  1587.             api_sql_query($sql,__FILE__,__LINE__);
  1588.             $sql "DELETE FROM $table_stats_links WHERE links_cours_id = '".$code."'";
  1589.             api_sql_query($sql,__FILE__,__LINE__);
  1590.             $sql "DELETE FROM $table_stats_uploads WHERE upload_cours_id = '".$code."'";
  1591.             api_sql_query($sql,__FILE__,__LINE__);
  1592.         }                
  1593.         // Delete the course from the database
  1594.         $sql "DELETE FROM $table_course WHERE code='".$code."'";
  1595.         api_sql_query($sql__FILE____LINE__);
  1596.     }
  1597.  
  1598.     /**
  1599.      * Creates a file called mysql_dump.sql in the course folder
  1600.      * @param $course_code The code of the course
  1601.      * @todo Implementation for single database
  1602.      */
  1603.     function create_database_dump($course_code)
  1604.     {
  1605.         global $_configuration;
  1606.         
  1607.         if ($_configuration['single_database'])
  1608.         {
  1609.             return;
  1610.         }
  1611.         $sql_dump '';
  1612.         $table_course Database :: get_main_table(TABLE_MAIN_COURSE);
  1613.         $sql "SELECT * FROM $table_course WHERE code = '$course_code'";
  1614.         $res api_sql_query($sql__FILE____LINE__);
  1615.         $course Database::fetch_array($res);
  1616.         $sql "SHOW TABLES FROM ".$course['db_name'];
  1617.         $res api_sql_query($sql__FILE____LINE__);
  1618.         while ($table Database::fetch_array($res))
  1619.         {
  1620.             $sql "SELECT * FROM `".$course['db_name']."`.`".$table[0]."`";
  1621.             $res3 api_sql_query($sql__FILE____LINE__);
  1622.             while ($row Database::fetch_array($res3))
  1623.             {
  1624.                 foreach ($row as $key => $value)
  1625.                 {
  1626.                     $row[$key$key."='".addslashes($row[$key])."'";
  1627.                 }
  1628.                 $sql_dump .= "\nINSERT INTO $table[0] SET ".implode(', '$row).';';
  1629.             }
  1630.         }
  1631.         $file_name api_get_path(SYS_COURSE_PATH).$course['directory'].'/mysql_dump.sql';
  1632.         $handle fopen($file_name'a+');
  1633.         if($handle!==false){
  1634.             fwrite($handle$sql_dump);
  1635.             fclose($handle);
  1636.         }else{
  1637.             //TODO trigger exception in a try-catch
  1638.         }
  1639.     }
  1640.     
  1641.     function userCourseSort($user_id,$course_code){
  1642.     
  1643.         $TABLECOURSE Database :: get_main_table(TABLE_MAIN_COURSE);
  1644.         $TABLECOURSUSER Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1645.         
  1646.         $sql 'SELECT title FROM '.$TABLECOURSE.' WHERE code="'.$course_code.'"';
  1647.         $result api_sql_query($sql__FILE____LINE__);
  1648.         $course_title Database::result($result,0,0);
  1649.         
  1650.         $sql 'SELECT course.code as code, course.title as title, cu.sort as sort FROM '.$TABLECOURSUSER.' as cu, '.$TABLECOURSE.' as course 
  1651.                 WHERE course.code = cu.course_code 
  1652.                 AND user_id = "'.$user_id.'" 
  1653.                 AND user_course_cat=0 ORDER BY cu.sort';
  1654.         $result api_sql_query($sql__FILE____LINE__);
  1655.         
  1656.         $s_course_title_precedent '';
  1657.         $counter 0;
  1658.         $b_find_course false;
  1659.         $i_course_sort 1;
  1660.         
  1661.         while($courses=Database::fetch_array($result)){
  1662.             
  1663.             if($s_course_title_precedent == ''){
  1664.                 $s_course_title_precedent $courses['title'];
  1665.             }
  1666.     
  1667.             if(strcasecmp($s_course_title_precedent,$course_title)<0){
  1668.                 
  1669.                 $b_find_course true;
  1670.                 $i_course_sort $courses['sort'];
  1671.     
  1672.                 $s_course_code $courses['code'];
  1673.                 if($counter == 0){
  1674.                     $sql 'UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'"  AND user_course_cat="0" AND sort > "'.$i_course_sort.'"';
  1675.                     $i_course_sort++;
  1676.                 }
  1677.                 else{
  1678.                     $sql 'UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'"  AND user_course_cat="0" AND sort >= "'.$i_course_sort.'"';
  1679.                 }
  1680.     
  1681.                 api_sql_query($sql__FILE____LINE__);
  1682.                 break;
  1683.             }
  1684.             
  1685.             else{
  1686.                 $s_course_title_precedent $courses['title'];
  1687.             }
  1688.             
  1689.             $counter++;
  1690.         }
  1691.         
  1692.         //We must register the course in the beginning of the list
  1693.         if(Database::num_rows($result)>&& !$b_find_course)
  1694.         {
  1695.             $sql_max 'SELECT min(sort) as min_sort FROM '.$TABLECOURSUSER.' WHERE user_id="'.$user_id.'" AND user_course_cat="0"';
  1696.             $result_min_sort=api_sql_query($sql_max__FILE____LINE__);
  1697.             $i_course_sort Database::result($result_min_sort,0,0);
  1698.             
  1699.             $sql 'UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'"  AND user_course_cat="0"';
  1700.             api_sql_query($sql__FILE____LINE__);
  1701.         }
  1702.         return $i_course_sort;
  1703.     }
  1704.     
  1705.     /**
  1706.      * create recursively all categories as option of the select passed in paramater.
  1707.      * 
  1708.      * @param object $select_element the quickform select where the options will be added
  1709.      * @param string $category_selected_code the option value to select by default (used mainly for edition of courses)
  1710.      * @param string $parent_code the parent category of the categories added (default=null for root category)
  1711.      * @param string $padding the indent param (you shouldn't indicate something here)
  1712.      */
  1713.     function select_and_sort_categories($select_element$category_selected_code=""$parent_code=null $padding="")
  1714.     {
  1715.         $table_course_category Database :: get_main_table(TABLE_MAIN_CATEGORY);
  1716.         $sql "SELECT code, name, auth_course_child, auth_cat_child
  1717.                 FROM ".$table_course_category.
  1718.                 WHERE parent_id ".(is_null($parent_code"IS NULL" "='".Database::escape_string($parent_code)."'")."
  1719.                 ORDER BY code";
  1720.         $res api_sql_query($sql__FILE____LINE__);
  1721.         
  1722.         $new_padding $padding.str_repeat('-',3);
  1723.         
  1724.         while ($cat Database::fetch_array($res))
  1725.         {
  1726.             $params $cat['auth_course_child'== 'TRUE' '' 'disabled';
  1727.             $params .= ($cat['code'== $category_selected_code' selected' '';
  1728.             $select_element->addOption($padding.'('.$cat['code'].') '.$cat['name']$cat['code']$params);
  1729.             if($cat['auth_cat_child'])
  1730.             {
  1731.                 CourseManager::select_and_sort_categories($select_element$cat['code']$category_selected_code$new_padding);
  1732.             }
  1733.         }
  1734.     }
  1735.  
  1736.     /**
  1737.      * check if course exists
  1738.      * @param string course_code
  1739.      * @return true if exists, false else
  1740.      */
  1741.     function course_exists($course_code)
  1742.     {
  1743.         $sql 'SELECT 1 FROM '.Database :: get_main_table(TABLE_MAIN_COURSE).' WHERE code="'.Database::escape_string($course_code).'"';
  1744.         $rs api_sql_query($sql,__FILE__,__LINE__);
  1745.         return Database::num_rows($rs);
  1746.     }    
  1747. //end class CourseManager
  1748. ?>

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