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

Source for file groupmanager.lib.php

Documentation is available at groupmanager.lib.php

  1. <?php
  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.  
  10.     For a full list of contributors, see "credits.txt".
  11.     The full license can be read in "license.txt".
  12.  
  13.     This program is free software; you can redistribute it and/or
  14.     modify it under the terms of the GNU General Public License
  15.     as published by the Free Software Foundation; either version 2
  16.     of the License, or (at your option) any later version.
  17.  
  18.     See the GNU General Public License for more details.
  19.  
  20.     Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  21.     Mail: info@dokeos.com
  22. ==============================================================================
  23. */
  24. /**
  25. ==============================================================================
  26. *    This is the group library for Dokeos.
  27. *    Include/require it in your code to use its functionality.
  28. *
  29. *    @author various authors
  30. *    @author Roan Embrechts (Vrije Universiteit Brussel), virtual courses support + some cleaning
  31. *   @author Bart Mollet (HoGent), all functions in class GroupManager
  32. *    @package dokeos.library
  33. ==============================================================================
  34. */
  35. require_once ('database.lib.php');
  36. require_once ('course.lib.php');
  37. require_once ('tablesort.lib.php');
  38. require_once ('fileManage.lib.php');
  39. require_once ('fileUpload.lib.php');
  40. /**
  41.  * infinite
  42.  */
  43. define("INFINITE""99999");
  44. /**
  45.  * No limit on the number of users in a group
  46.  */
  47. define("MEMBER_PER_GROUP_NO_LIMIT""0");
  48. /**
  49.  * No limit on the number of groups per user
  50.  */
  51. define("GROUP_PER_MEMBER_NO_LIMIT""0");
  52. /**
  53.  * The tools of a group can have 3 states
  54.  * - not available
  55.  * - public
  56.  * - private
  57.  */
  58. define("TOOL_NOT_AVAILABLE""0");
  59. define("TOOL_PUBLIC""1");
  60. define("TOOL_PRIVATE""2");
  61. /**
  62.  * Constants for the available group tools
  63.  */
  64. //define("GROUP_TOOL_FORUM", "0");
  65. define("GROUP_TOOL_DOCUMENTS""1");
  66. define("GROUP_TOOL_CALENDAR","2");
  67. define("GROUP_TOOL_ANNOUNCEMENT","3");
  68. define("GROUP_TOOL_WORK","4");
  69.  
  70. /**
  71.  * Fixed id's for group categories
  72.  * - VIRTUAL_COURSE_CATEGORY: in this category groups are created based on the
  73.  *   virtual  course of a course
  74.  * - DEFAULT_GROUP_CATEGORY: When group categories aren't available (platform-
  75.  *   setting),  all groups are created in this 'dummy'-category
  76.  */
  77. define("VIRTUAL_COURSE_CATEGORY"1);
  78. define("DEFAULT_GROUP_CATEGORY"2);
  79. /**
  80.  * This library contains some functions for group-management.
  81.  * @author Bart Mollet
  82.  * @package dokeos.library
  83.  * @todo Add $course_code parameter to all functions. So this GroupManager can
  84.  *  be used outside a session.
  85.  */
  86. {
  87.     /*==============================================================================
  88.     *    GROUP FUNCTIONS
  89.       ==============================================================================*/
  90.     /**
  91.      * Get list of groups for current course.
  92.      * @param int $category The id of the category from which the groups are
  93.      *  requested
  94.      * @param string $course_code Default is current course
  95.      * @return array An array with all information about the groups.
  96.      */
  97.     function get_group_list($category null$course_code null)
  98.     {
  99.         global $_user;
  100.         $course_db '';
  101.         if ($course_code != null)
  102.         {
  103.             $course_info Database :: get_course_info($course_code);
  104.             $course_db $course_info['database'];
  105.         }
  106.         $table_group Database :: get_course_table(TABLE_GROUP$course_db);
  107.         $table_user Database :: get_main_table(TABLE_MAIN_USER);
  108.         $table_course Database :: get_main_table(TABLE_MAIN_COURSE);
  109.         $table_group_user Database :: get_course_table(TABLE_GROUP_USER$course_db);
  110.         $sql "SELECT  g.id ,
  111.                         g.name ,
  112.                         g.description ,
  113.                         g.category_id,
  114.                         g.max_student maximum_number_of_members,
  115.                         g.secret_directory,
  116.                         g.self_registration_allowed,
  117.                         g.self_unregistration_allowed,
  118.                         ug.user_id is_member,
  119.                         COUNT(ug2.id) number_of_members
  120.                     FROM ".$table_group." `g`
  121.                     LEFT JOIN ".$table_group_user." `ug`
  122.                     ON `ug`.`group_id` = `g`.`id` AND `ug`.`user_id` = '".$_user['user_id']."'
  123.                     LEFT JOIN ".$table_group_user." `ug2`
  124.                     ON `ug2`.`group_id` = `g`.`id`";
  125.         if ($category != null)
  126.             $sql .= " WHERE `g`.`category_id` = '".$category."' ";
  127.         $sql .= " GROUP BY `g`.`id` ORDER BY UPPER(g.name)";
  128.         $groupList api_sql_query($sql,__FILE__,__LINE__);
  129.         $groups array ();
  130.         while ($thisGroup mysql_fetch_array($groupList))
  131.         {
  132.             if ($thisGroup['category_id'== VIRTUAL_COURSE_CATEGORY)
  133.             {
  134.                 $sql "SELECT title FROM $table_course WHERE code = '".$thisGroup['name']."'";
  135.                 $obj mysql_fetch_object(api_sql_query($sql,__FILE__,__LINE__));
  136.                 $thisGroup['name'$obj->title;
  137.             }
  138.             $groups[$thisGroup;
  139.         }
  140.         return $groups;
  141.     }
  142.     /**
  143.      * Create a group
  144.      * @param string $name The name for this group
  145.      * @param int $tutor The user-id of the group's tutor
  146.      * @param int $places How many people can subscribe to the new group
  147.      */
  148.     function create_group($name$category_id$tutor$places)
  149.     {
  150.         global $_course,$_user;
  151.         
  152.         $currentCourseRepository $_course['path'];
  153.         $table_group Database :: get_course_table(TABLE_GROUP);
  154.         $table_forum Database :: get_course_table(TABLE_FORUM);
  155.         $category GroupManager :: get_category($category_id);
  156.         
  157.         if (intval($places== 0//if the amount of users per group is not filled in, use the setting from the category
  158.         {
  159.             $places $category['max_student'];
  160.         }
  161.         $sql "INSERT INTO ".$table_group." SET category_id='".$category_id."', max_student = '".$places."', doc_state = '".$category['doc_state']."', calendar_state = '".$category['calendar_state']."', work_state = '".$category['work_state']."', announcements_state = '".$category['announcements_state']."', self_registration_allowed = '".$category['self_reg_allowed']."',  self_unregistration_allowed = '".$category['self_unreg_allowed']."'";
  162.         api_sql_query($sql,__FILE__,__LINE__);
  163.         $lastId mysql_insert_id();
  164.         /*$secret_directory = uniqid("")."_team_".$lastId;
  165.         while (is_dir(api_get_path(SYS_COURSE_PATH).$currentCourseRepository."/group/$secret_directory"))
  166.         {
  167.             $secret_directory = uniqid("")."_team_".$lastId;
  168.         }
  169.         FileManager :: mkdirs(api_get_path(SYS_COURSE_PATH).$currentCourseRepository."/group/".$secret_directory, 0770);
  170.         */
  171.         $desired_dir_name'/'.replace_dangerous_char($name,'strict').'_groupdocs';
  172.         $dir_name create_unexisting_directory($_course,$_user['user_id'],$lastId,NULL,api_get_path(SYS_COURSE_PATH).$currentCourseRepository.'/document',$desired_dir_name);
  173.         /* Stores the directory path into the group table */
  174.         $sql "UPDATE ".$table_group." SET   name = '".mysql_real_escape_string($name)."', secret_directory = '".$dir_name."' WHERE id ='".$lastId."'";
  175.         api_sql_query($sql,__FILE__,__LINE__);
  176.         
  177.         // create a forum if needed
  178.         if ($category['forum_state'0)
  179.         {
  180.             include_once(api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php');
  181.             include_once(api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php');
  182.             
  183.             $forum_categories get_forum_categories();
  184.             $values['forum_title'get_lang('ForumOfGroup').' '.$name;
  185.             $counter 0;
  186.             foreach ($forum_categories as $key=>$value)
  187.             {
  188.                 if ($counter==0)    
  189.                 {
  190.                     $forum_category_id $key;
  191.                 }
  192.                 $counter++;
  193.             }
  194.             $values['forum_category'$forum_category_id;
  195.             $values['allow_anonymous_group']['allow_anonymous'0;
  196.             $values['students_can_edit_group']['students_can_edit'0;
  197.             $values['approval_direct_group']['approval_direct'0;
  198.             $values['allow_attachments_group']['allow_attachments'1;
  199.             $values['allow_new_threads_group']['allow_new_threads'1;
  200.             $values['default_view_type_group']['default_view_type']=api_get_setting('default_forum_view');
  201.             $values['group_forum'$lastId
  202.             if ($category['forum_state'== '1')
  203.             {
  204.                 $values['public_private_group_forum_group']['public_private_group_forum']='public';
  205.             }
  206.             else 
  207.             {
  208.                 $values['public_private_group_forum_group']['public_private_group_forum']='private';
  209.             }
  210.             store_forum($values);
  211.         }
  212.         return $lastId;
  213.     }
  214.     /**
  215.      * Create subgroups.
  216.      * This function creates new groups based on an existing group. It will
  217.      * create the specified number of groups and fill those groups with users
  218.      * from the base group
  219.      * @param int $group_id The group from which subgroups have to be created.
  220.      * @param int $number_of_groups The number of groups that have to be created
  221.      */
  222.     function create_subgroups($group_id$number_of_groups)
  223.     {
  224.         $table_group Database :: get_course_table(TABLE_GROUP);
  225.         $category_id GroupManager :: create_category('Subgroups'''TOOL_PRIVATETOOL_PRIVATE0011);
  226.         $users GroupManager :: get_users($group_id);
  227.         $group_ids array ();
  228.         for ($group_nr 1$group_nr <= $number_of_groups$group_nr ++)
  229.         {
  230.             $group_ids[GroupManager :: create_group('SUBGROUP '.$group_nr$category_id00);
  231.         }
  232.         $members array ();
  233.         foreach ($users as $index => $user_id)
  234.         {
  235.             GroupManager :: subscribe_users($user_id$group_ids[$index $number_of_groups]);
  236.             $members[$group_ids[$index $number_of_groups]]++;
  237.         }
  238.         foreach ($members as $group_id => $places)
  239.         {
  240.             $sql "UPDATE $table_group SET max_student = $places WHERE id = $group_id";
  241.             api_sql_query($sql,__FILE__,__LINE__);
  242.         }
  243.     }
  244.     /**
  245.      * Create groups from all virtual courses in the given course.
  246.      */
  247.     {
  248.         $id GroupManager :: create_category(get_lang('GroupsFromVirtualCourses')''TOOL_NOT_AVAILABLETOOL_NOT_AVAILABLE0011);
  249.         $table_group_cat Database :: get_course_table(TABLE_GROUP_CATEGORY);
  250.         $sql "UPDATE ".$table_group_cat." SET id=".VIRTUAL_COURSE_CATEGORY." WHERE id=$id";
  251.         api_sql_query($sql,__FILE__,__LINE__);
  252.         $course api_get_course_info();
  253.         $course['code'$course['sysCode'];
  254.         $course['title'$course['name'];
  255.         $virtual_courses CourseManager :: get_virtual_courses_linked_to_real_course($course['sysCode']);
  256.         $group_courses $virtual_courses;
  257.         $group_courses[$course;
  258.         $ids array ();
  259.         foreach ($group_courses as $index => $group_course)
  260.         {
  261.             $users CourseManager :: get_user_list_from_course_code($group_course['code']);
  262.             $members array ();
  263.             foreach ($users as $index => $user)
  264.             {
  265.                 if ($user['status'== && $user['tutor_id'== 0)
  266.                 {
  267.                     $members[$user['user_id'];
  268.                 }
  269.             }
  270.             $id GroupManager :: create_group($group_course['code']VIRTUAL_COURSE_CATEGORY0count($members));
  271.             GroupManager :: subscribe_users($members$id);
  272.             $ids[$id;
  273.         }
  274.         return $ids;
  275.     }
  276.     /**
  277.      * Create a group for every class subscribed to the current course
  278.      * @param int $category_id The category in which the groups should be
  279.      *  created
  280.      */
  281.     function create_class_groups($category_id)
  282.     {
  283.         global $_course;
  284.         $classes ClassManager::get_classes_in_course($_course['sysCode']);
  285.         $group_ids array();
  286.         foreach($classes as $index => $class)
  287.         {
  288.             $users ClassManager::get_users($class['id']);
  289.             $group_id GroupManager::create_group($class['name'],$category_id,0,count($users));
  290.             $user_ids array();
  291.             foreach($users as $index_user => $user)
  292.             {
  293.                 $user_ids[$user['user_id'];
  294.             }
  295.             GroupManager::subscribe_users($user_ids,$group_id);
  296.             $group_ids[$group_id;
  297.         }
  298.         return $group_ids;
  299.     }
  300.  
  301.     /**
  302.      * deletes groups and their data.
  303.      * @author Christophe Gesche <christophe.gesche@claroline.net>
  304.      * @author Hugues Peeters <hugues.peeters@claroline.net>
  305.      * @author Bart Mollet
  306.      * @param  mixed   $groupIdList - group(s) to delete. It can be a single id
  307.      *                                 (int) or a list of id (array).
  308.      * @param string $course_code Default is current course
  309.      * @return integer              - number of groups deleted.
  310.      */
  311.     function delete_groups($group_ids$course_code null)
  312.     {
  313.         $course_db '';
  314.         if ($course_code != null)
  315.         {
  316.             $course Database :: get_course_info($course_code);
  317.             $course['path'$course['directory'];
  318.             $course_db $course['database'];
  319.         }
  320.         else
  321.         {
  322.             $course api_get_course_info();
  323.         }
  324.         
  325.         // Database table definitions
  326.         $group_table             Database :: get_course_table(TABLE_GROUP$course_db);
  327.         $group_user_table         Database :: get_course_table(TABLE_GROUP_USER$course_db);
  328.         $forum_table             Database :: get_course_table(TABLE_FORUM$course_db);
  329.         $forum_post_table         Database :: get_course_table(TABLE_FORUM_POST$course_db);
  330.         $forum_post_text_table     Database :: get_course_table(TOOL_FORUM_POST_TEXT_TABLE$course_db);
  331.         $forum_topic_table         Database :: get_course_table(TABLE_FORUM_POST$course_db);
  332.         
  333.         $group_ids is_array($group_ids$group_ids array ($group_ids);
  334.         // define repository for deleted element
  335.         $group_garbage api_get_path(GARBAGE_PATH).$course['path']."/group/";
  336.         $perm api_get_setting('permissions_for_new_directories');
  337.         $perm (!empty($perm)?$perm:'0770');
  338.         if (!file_exists($group_garbage))
  339.             FileManager :: mkdirs($group_garbage$perm);
  340.         // Unsubscribe all users
  341.         GroupManager :: unsubscribe_all_users($group_ids);
  342.         $sql 'SELECT id, secret_directory FROM '.$group_table.' WHERE id IN ('.implode(' , '$group_ids).')';
  343.         $db_result api_sql_query($sql,__FILE__,__LINE__);
  344.         $forum_ids array ();
  345.         while ($group mysql_fetch_object($db_result))
  346.         {
  347.             // move group-documents to garbage
  348.             $source_directory api_get_path(SYS_COURSE_PATH).$course['path']."/group/".$group->secret_directory;
  349.             $destination_directory $group_garbage.$group->secret_directory;
  350.             if (file_exists($source_directory))
  351.             {
  352.                 rename($source_directory$destination_directory);
  353.             }
  354.             //$forum_ids[] = $group->forum_id;
  355.         }
  356.         // delete the groups
  357.         $sql "DELETE FROM ".$group_table." WHERE id IN ('".implode("' , '"$group_ids)."')";
  358.         api_sql_query($sql,__FILE__,__LINE__);
  359.         return mysql_affected_rows();
  360.     }
  361.  
  362.     /**
  363.      * Get group properties
  364.      * @param int $group_id The group from which properties are requested.
  365.      * @return array All properties. Array-keys are name, tutor_id, description, maximum_number_of_students, directory
  366.      */
  367.     function get_group_properties($group_id)
  368.     {
  369.         if (empty($group_idor !is_integer(intval($group_id)) ) {
  370.             return null;
  371.         }
  372.         $table_group Database :: get_course_table(TABLE_GROUP);
  373.         $sql 'SELECT   *  FROM '.$table_group.' WHERE id = '.$group_id;
  374.         $db_result api_sql_query($sql,__FILE__,__LINE__);
  375.         $db_object mysql_fetch_object($db_result);
  376.         $result['id'$db_object->id;
  377.         $result['name'$db_object->name;
  378.         $result['tutor_id'$db_object->tutor_id;
  379.         $result['description'$db_object->description;
  380.         $result['maximum_number_of_students'$db_object->max_student;
  381.         $result['doc_state'$db_object->doc_state;
  382.         $result['work_state'$db_object->work_state;
  383.         $result['calendar_state'$db_object->calendar_state;
  384.         $result['announcements_state'$db_object->announcements_state;
  385.         $result['directory'$db_object->secret_directory;
  386.         $result['self_registration_allowed'$db_object->self_registration_allowed;
  387.         $result['self_unregistration_allowed'$db_object->self_unregistration_allowed;
  388.         return $result;
  389.     }
  390.     /**
  391.      * Set group properties
  392.      * Changes the group's properties.
  393.      * @param int $group_id 
  394.      * @param string $name 
  395.      * @param string $description 
  396.      * @param int $tutor_id 
  397.      * @param int $maximum_number_of_students 
  398.      * @param bool $self_registration_allowed 
  399.      * @param bool $self_unregistration_allowed 
  400.      * @return bool TRUE if properties are successfully changed.
  401.      */
  402.     function set_group_properties($group_id$name$description$maximum_number_of_students$doc_state$work_state$calendar_state$announcements_state$self_registration_allowed$self_unregistration_allowed)
  403.     {
  404.         $table_group Database :: get_course_table(TABLE_GROUP);
  405.         //$table_forum = Database :: get_course_table(TABLE_FORUM);
  406.         $sql "UPDATE ".$table_group."
  407.                     SET name='".trim($name)."',
  408.                     doc_state = '".$doc_state."',
  409.                     work_state = '".$work_state."',
  410.                     calendar_state = '".$calendar_state."',
  411.                     announcements_state = '".$announcements_state."',
  412.                     description='".trim($description)."',
  413.                     max_student=".$maximum_number_of_students.",
  414.                     self_registration_allowed='".$self_registration_allowed."',
  415.                     self_unregistration_allowed='".$self_unregistration_allowed."'
  416.                     WHERE id=".$group_id;
  417.         $result api_sql_query($sql,__FILE__,__LINE__);
  418.         //$sql = "UPDATE ".$table_forum." SET forum_name='".trim($name)."' WHERE forum_id=".$forum_id;
  419.         //$result &= api_sql_query($sql,__FILE__,__LINE__);
  420.         return $result;
  421.     }
  422.     /**
  423.      * Get the total number of groups for the current course.
  424.      * @return int The number of groups for the current course.
  425.      */
  426.     function get_number_of_groups()
  427.     {
  428.         $table_group Database :: get_course_table(TABLE_GROUP);
  429.         $res api_sql_query('SELECT COUNT(id) AS number_of_groups FROM '.$table_group);
  430.         $obj mysql_fetch_object($res);
  431.         return $obj->number_of_groups;
  432.     }
  433.  
  434.  
  435.     /*==============================================================================
  436.     *    GROUPCATEGORY FUNCTIONS
  437.       ==============================================================================*/
  438.     /**
  439.      * Get all categories
  440.      * @param string $course_code The cours (default = current course)
  441.      */
  442.     function get_categories($course_code null)
  443.     {
  444.         $course_db '';
  445.         if ($course_code != null)
  446.         {
  447.             $course_info Database :: get_course_info($course_code);
  448.             $course_db $course_info['database'];
  449.         }
  450.         $table_group_cat Database :: get_course_table(TABLE_GROUP_CATEGORY$course_db);
  451.         $sql "SELECT * FROM $table_group_cat ORDER BY display_order";
  452.         $res api_sql_query($sql,__FILE__,__LINE__);
  453.         $cats array ();
  454.         while ($cat mysql_fetch_array($res))
  455.         {
  456.             $cats[$cat;
  457.         }
  458.         return $cats;
  459.     }
  460.     /**
  461.      * Get a group category
  462.      * @param int $id The category id
  463.      * @param string $course_code The course (default = current course)
  464.      */
  465.     function get_category($id$course_code null)
  466.     {
  467.         $course_db '';
  468.         if ($course_code != null)
  469.         {
  470.             $course_info Database :: get_course_info($course_code);
  471.             $course_db $course_info['database'];
  472.         }
  473.         $table_group_cat Database :: get_course_table(TABLE_GROUP_CATEGORY$course_db);
  474.         $sql "SELECT * FROM $table_group_cat WHERE id = $id";
  475.         $res api_sql_query($sql,__FILE__,__LINE__);
  476.         return mysql_fetch_array($res);
  477.     }
  478.     /**
  479.      * Get the category of a given group
  480.      * @param int $group_id The id of the group
  481.      * @param string $course_code The course in which the group is (default =
  482.      *  current course)
  483.      * @return array The category
  484.      */
  485.     function get_category_from_group($group_id$course_code null)
  486.     {
  487.         $course_db '';
  488.         if ($course_code != null)
  489.         {
  490.             $course_info Database :: get_course_info($course_code);
  491.             $course_db $course_info['database'];
  492.         }
  493.         $table_group Database :: get_course_table(TABLE_GROUP$course_db);
  494.         $table_group_cat Database :: get_course_table(TABLE_GROUP_CATEGORY$course_db);
  495.         $sql "SELECT gc.* FROM $table_group_cat gc, $table_group g WHERE gc.id = g.category_id AND g.id=$group_id";
  496.         $res api_sql_query($sql,__FILE__,__LINE__);
  497.         $cat mysql_fetch_array($res);
  498.         return $cat;
  499.     }
  500.     /**
  501.      * Delete a group category
  502.      * @param int $cat_id The id of the category to delete
  503.      * @param string $course_code The code in which the category should be
  504.      *  deleted (default = current course)
  505.      */
  506.     function delete_category($cat_id$course_code null)
  507.     {
  508.         $course_db '';
  509.         if ($course_code != null)
  510.         {
  511.             $course_info Database :: get_course_info($course_code);
  512.             $course_db $course_info['database'];
  513.         }
  514.         $table_group Database :: get_course_table(TABLE_GROUP$course_db);
  515.         $table_group_cat Database :: get_course_table(TABLE_GROUP_CATEGORY$course_db);
  516.         $sql "SELECT id FROM $table_group WHERE category_id='".$cat_id."'";
  517.         $res api_sql_query($sql,__FILE__,__LINE__);
  518.         if (mysql_num_rows($res0)
  519.         {
  520.             $groups_to_delete array ();
  521.             while ($group mysql_fetch_object($res))
  522.             {
  523.                 $groups_to_delete[$group->id;
  524.             }
  525.             GroupManager :: delete_groups($groups_to_delete);
  526.         }
  527.         $sql "DELETE FROM $table_group_cat WHERE id='".$cat_id."'";
  528.         api_sql_query($sql,__FILE__,__LINE__);
  529.     }
  530.     /**
  531.      * Create group category
  532.      * @param string $title The title of the new category
  533.      * @param string $description The description of the new category
  534.      * @param bool $self_registration_allowed 
  535.      * @param bool $self_unregistration_allowed 
  536.      * @param int $max_number_of_students 
  537.      * @param int $groups_per_user 
  538.      */
  539.     function create_category($title$description$doc_state$work_state$calendar_state$announcements_state$forum_state$self_registration_allowed$self_unregistration_allowed$maximum_number_of_students$groups_per_user)
  540.     {
  541.         $table_group_category Database :: get_course_table(TABLE_GROUP_CATEGORY);
  542.         $sql "SELECT MAX(display_order)+1 as new_order FROM $table_group_category ";
  543.         $res api_sql_query($sql,__FILE__,__LINE__);
  544.         $obj mysql_fetch_object($res);
  545.         if (!isset ($obj->new_order))
  546.         {
  547.             $obj->new_order 1;
  548.         }
  549.         $sql "INSERT INTO ".$table_group_category."
  550.                     SET title='".mysql_real_escape_string($title)."',
  551.                     display_order = $obj->new_order,
  552.                     description='".mysql_real_escape_string($description)."',
  553.                     doc_state = '".$doc_state."',
  554.                     work_state = '".$work_state."',
  555.                     calendar_state = '".$calendar_state."',
  556.                       announcements_state = '".$announcements_state."',  
  557.                       forum_state = '".Database::escape_string($forum_state)."',
  558.                     groups_per_user   = ".$groups_per_user.",
  559.                     self_reg_allowed = '".$self_registration_allowed."',
  560.                     self_unreg_allowed = '".$self_unregistration_allowed."',
  561.                     max_student = ".$maximum_number_of_students." ";
  562.         api_sql_query($sql,__FILE__,__LINE__);
  563.         $id mysql_insert_id();
  564.         if ($id == VIRTUAL_COURSE_CATEGORY)
  565.         {
  566.             $sql "UPDATE  ".$table_group_category." SET id = "($id +1)." WHERE id = $id";
  567.             api_sql_query($sql,__FILE__,__LINE__);
  568.             return $id +1;
  569.         }
  570.         return $id;
  571.     }
  572.  
  573.     /**
  574.      * Update group category
  575.      * @param int $id The id of the category
  576.      * @param string $title The title of the new category
  577.      * @param string $description The description of the new category
  578.      * @param bool $self_registration_allowed 
  579.      * @param bool $self_unregistration_allowed 
  580.      * @param int $max_number_of_students 
  581.      * @param int $groups_per_user 
  582.      */
  583.     function update_category($id$title$description$doc_state$work_state$calendar_state$announcements_state$forum_state$self_registration_allowed$self_unregistration_allowed$maximum_number_of_students$groups_per_user)
  584.     {
  585.         $table_group_category Database :: get_course_table(TABLE_GROUP_CATEGORY);
  586.         $sql "UPDATE ".$table_group_category."
  587.                 SET title='".mysql_real_escape_string($title)."',
  588.                 description='".mysql_real_escape_string($description)."',
  589.                 doc_state = '".$doc_state."',
  590.                 work_state = '".$work_state."',
  591.                 calendar_state = '".$calendar_state."',
  592.                 announcements_state = '".$announcements_state."',
  593.                 forum_state = '".Database::escape_string($forum_state)."', 
  594.                 groups_per_user   = ".$groups_per_user.",
  595.                 self_reg_allowed = '".$self_registration_allowed."',
  596.                 self_unreg_allowed = '".$self_unregistration_allowed."',
  597.                 max_student = ".$maximum_number_of_students."
  598.                 WHERE id=$id";
  599.         api_sql_query($sql,__FILE__,__LINE__);
  600.     }
  601.     
  602.     
  603.     /**
  604.      * Returns the number of groups of the user with the greatest number of
  605.      * subscribtions in the given category
  606.      */
  607.     function get_current_max_groups_per_user($category_id null$course_code null)
  608.     {
  609.         $course_db '';
  610.         if ($course_code != null)
  611.         {
  612.             $course_info Database :: get_course_info($course_code);
  613.             $course_db $course_info['database'];
  614.         }
  615.         $group_table Database :: get_course_table(TABLE_GROUP$course_db);
  616.         $group_user_table Database :: get_course_table(TABLE_GROUP_USER$course_db);
  617.         $sql 'SELECT COUNT(gu.group_id) AS current_max FROM '.$group_user_table.' gu, '.$group_table.' g WHERE gu.group_id = g.id ';
  618.         if ($category_id != null)
  619.             $sql .= ' AND g.category_id = '.$category_id;
  620.         $sql .= ' GROUP BY gu.user_id ORDER BY current_max DESC LIMIT 1';
  621.         $res api_sql_query($sql,__FILE__,__LINE__);
  622.         $obj mysql_fetch_object($res);
  623.         return $obj->current_max;
  624.     }
  625.     /**
  626.      * Swaps the display-order of two categories
  627.      * @param int $id1 The id of the first category
  628.      * @param int $id2 The id of the second category
  629.      */
  630.     function swap_category_order($id1$id2)
  631.     {
  632.         $table_group_cat Database :: get_course_table(TABLE_GROUP_CATEGORY);
  633.         $sql "SELECT id,display_order FROM $table_group_cat WHERE id IN ($id1,$id2)";
  634.         $res api_sql_query($sql,__FILE__,__LINE__);
  635.         $cat1 mysql_fetch_object($res);
  636.         $cat2 mysql_fetch_object($res);
  637.         $sql "UPDATE $table_group_cat SET display_order=$cat2->display_order WHERE id=$cat1->id";
  638.         api_sql_query($sql,__FILE__,__LINE__);
  639.         $sql "UPDATE $table_group_cat SET display_order=$cat1->display_order WHERE id=$cat2->id";
  640.         api_sql_query($sql,__FILE__,__LINE__);
  641.     }
  642.  
  643.  
  644.  
  645.  
  646.     /*==============================================================================
  647.     *    GROUP USERS FUNCTIONS
  648.       ==============================================================================*/
  649.  
  650.     /**
  651.      * Get all users from a given group
  652.      * @param int $group_id The group
  653.      */
  654.     function get_users($group_id)
  655.     {
  656.         $group_user_table Database :: get_course_table(TABLE_GROUP_USER$course_db);
  657.         $sql "SELECT user_id FROM $group_user_table WHERE group_id = $group_id";
  658.         $res api_sql_query($sql,__FILE__,__LINE__);
  659.         $users array ();
  660.         while ($obj mysql_fetch_object($res))
  661.         {
  662.             $users[$obj->user_id;
  663.         }
  664.         return $users;
  665.     }
  666.  
  667.     /**
  668.      * Fill the groups with students.
  669.      * The algorithm takes care to first fill the groups with the least # of users.
  670.      *    Analysis
  671.      *    There was a problem with the "ALL" setting.
  672.      *    When max # of groups is set to all, the value is sometimes NULL and sometimes ALL
  673.      *    and in both cased the query does not work as expected.
  674.      *    Stupid solution (currently implemented: set ALL to a big number (INFINITE) and things are solved :)
  675.      *    Better solution: that's up to you.
  676.      *
  677.      *    Note
  678.      *    Throughout Dokeos there is some confusion about "course id" and "course code"
  679.      *    The code is e.g. TEST101, but sometimes a variable that is called courseID also contains a course code string.
  680.      *    However, there is also a integer course_id that uniquely identifies the course.
  681.      *    ywarnier:> Now the course_id has been removed (25/1/2005)
  682.      *    The databases are als very inconsistent in this.
  683.      *
  684.      * @author Chrisptophe Gesche <christophe.geshe@claroline.net>,
  685.      *          Hugues Peeters     <hugues.peeters@claroline.net> - original version
  686.      * @author Roan Embrechts - virtual course support, code cleaning
  687.      * @author Bart Mollet - code cleaning, use other GroupManager-functions
  688.      * @return void 
  689.      */
  690.     function fill_groups($group_ids)
  691.     {
  692.         $group_ids is_array($group_ids$group_ids array ($group_ids);
  693.         global $_course;
  694.         $category GroupManager :: get_category_from_group($group_ids[0]);
  695.         $groups_per_user $category['groups_per_user'];
  696.         $course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  697.         $group_table Database :: get_course_table(TABLE_GROUP);
  698.         $group_user_table Database :: get_course_table(TABLE_GROUP_USER);
  699.         $complete_user_list CourseManager :: get_real_and_linked_user_list($_course['sysCode']);
  700.         $number_groups_per_user ($groups_per_user == GROUP_PER_MEMBER_NO_LIMIT INFINITE $groups_per_user);
  701.         /*
  702.          * Retrieve all the groups where enrollment is still allowed
  703.          * (reverse) ordered by the number of place available
  704.          */
  705.         $sql "SELECT g.id gid, g.max_student-count(ug.user_id) nbPlaces, g.max_student
  706.                 FROM ".$group_table." g
  707.                 LEFT JOIN  ".$group_user_table." ug
  708.                 ON    `g`.`id` = `ug`.`group_id`
  709.                 WHERE g.id IN (".implode(','$group_ids).")
  710.                 GROUP BY (`g`.`id`)
  711.                 HAVING (nbPlaces > 0 OR g.max_student = ".MEMBER_PER_GROUP_NO_LIMIT.")
  712.                 ORDER BY nbPlaces DESC";
  713.         $sql_result api_sql_query($sql,__FILE__,__LINE__);
  714.         $group_available_place array ();
  715.         while ($group Database::fetch_array($sql_result'ASSOC'))
  716.         {
  717.             $group_available_place[$group['gid']] $group['nbPlaces'];
  718.         }
  719.         /*
  720.          * Retrieve course users (reverse) ordered by the number
  721.          * of group they are already enrolled
  722.          */
  723.         for ($i 0$i count($complete_user_list)$i ++)
  724.         {
  725.             
  726.             //find # of groups the user is enrolled in
  727.             $number_of_groups GroupManager :: user_in_number_of_groups($complete_user_list[$i]["user_id"],$category['id']);
  728.             //add # of groups to user list
  729.             $complete_user_list[$i]['number_groups_left'$number_groups_per_user $number_of_groups;
  730.         }
  731.         //first sort by user_id to filter out duplicates
  732.         $complete_user_list TableSort :: sort_table($complete_user_list'user_id');
  733.         $complete_user_list GroupManager :: filter_duplicates($complete_user_list"user_id");
  734.         $complete_user_list GroupManager :: filter_only_students($complete_user_list);
  735.         //now sort by # of group left
  736.         $complete_user_list TableSort :: sort_table($complete_user_list'number_groups_left'SORT_DESC);
  737.         $userToken array ();
  738.         foreach ($complete_user_list as $this_user)
  739.         {
  740.             
  741.             if ($this_user['number_groups_left'0)
  742.             {
  743.                 $userToken[$this_user['user_id']] $this_user['number_groups_left'];
  744.             }
  745.         }
  746.         /*
  747.          * Retrieve the present state of the users repartion in groups
  748.          */
  749.         $sql "SELECT user_id uid, group_id gid FROM ".$group_user_table;
  750.         $result api_sql_query($sql,__FILE__,__LINE__);
  751.         while ($member Database::fetch_array($result'ASSOC'))
  752.         {
  753.             $groupUser[$member['gid']][$member['uid'];
  754.         }
  755.         $changed true;
  756.         while ($changed)
  757.         {
  758.             
  759.             $changed false;
  760.             reset($group_available_place);
  761.             arsort($group_available_place);
  762.             reset($userToken);
  763.             arsort($userToken);
  764.             foreach ($group_available_place as $group_id => $place)
  765.             {
  766.                 foreach ($userToken as $user_id => $places)
  767.                 {
  768.                     if (GroupManager :: can_user_subscribe($user_id$group_id))
  769.                     {
  770.                         
  771.                         GroupManager :: subscribe_users($user_id$group_id);
  772.                         $group_available_place[$group_id]--;
  773.                         $userToken[$user_id]--;
  774.                         $changed true;
  775.                         break;
  776.                     }
  777.                 }
  778.                 if ($changed)
  779.                 {
  780.                     break;
  781.                 }
  782.             }
  783.         }
  784.     }
  785.  
  786.  
  787.     /**
  788.      * Get the number of students in a group.
  789.      * @param int $group_id 
  790.      * @return int Number of students in the given group.
  791.      */
  792.     function number_of_students($group_id)
  793.     {
  794.         $table_group_user Database :: get_course_table(TABLE_GROUP_USER);
  795.         $db_result api_sql_query('SELECT  COUNT(*) AS number_of_students FROM '.$table_group_user.' WHERE group_id = '.$group_id);
  796.         $db_object mysql_fetch_object($db_result);
  797.         return $db_object->number_of_students;
  798.     }
  799.     /**
  800.      * Maximum number of students in a group
  801.      * @param int $group_id 
  802.      * @return int Maximum number of students in the given group.
  803.      */
  804.     function maximum_number_of_students($group_id)
  805.     {
  806.         $table_group Database :: get_course_table(TABLE_GROUP);
  807.         $db_result api_sql_query('SELECT   max_student  FROM '.$table_group.' WHERE id = '.$group_id);
  808.         $db_object mysql_fetch_object($db_result);
  809.         if ($db_object->max_student == 0)
  810.         {
  811.             return INFINITE;
  812.         }
  813.         return $db_object->max_student;
  814.     }
  815.     /**
  816.      * Number of groups of a user
  817.      * @param int $user_id 
  818.      * @return int The number of groups the user is subscribed in.
  819.      */
  820.     function user_in_number_of_groups($user_id$cat_id)
  821.     {
  822.         $table_group_user Database :: get_course_table(TABLE_GROUP_USER);
  823.         $table_group Database :: get_course_table(TABLE_GROUP);
  824.         $sql 'SELECT  COUNT(*) AS number_of_groups FROM '.$table_group_user.' gu, '.$table_group.' g WHERE gu.user_id = \''.$user_id.'\' AND g.id = gu.group_id AND g.category_id=  \''.$cat_id.'\'';
  825.         $db_result api_sql_query($sql,__FILE__,__LINE__);
  826.         $db_object mysql_fetch_object($db_result);
  827.         return $db_object->number_of_groups;
  828.     }
  829.     /**
  830.      * Is sef-registration allowed?
  831.      * @param int $user_id 
  832.      * @param int $group_id 
  833.      * @return bool TRUE if self-registration is allowed in the given group.
  834.      */
  835.     function is_self_registration_allowed($user_id$group_id)
  836.     {
  837.         if (!$user_id 0)
  838.             return false;
  839.         $table_group Database :: get_course_table(TABLE_GROUP);
  840.         $sql 'SELECT  self_registration_allowed FROM '.$table_group.' WHERE id = '.$group_id;
  841.         $db_result api_sql_query($sql,__FILE__,__LINE__);
  842.         $db_object mysql_fetch_object($db_result);
  843.         return $db_object->self_registration_allowed == && GroupManager :: can_user_subscribe($user_id$group_id);
  844.     }
  845.     /**
  846.      * Is sef-unregistration allowed?
  847.      * @param int $user_id 
  848.      * @param int $group_id 
  849.      * @return bool TRUE if self-unregistration is allowed in the given group.
  850.      */
  851.     function is_self_unregistration_allowed($user_id$group_id)
  852.     {
  853.         if (!$user_id 0)
  854.             return false;
  855.         $table_group Database :: get_course_table(TABLE_GROUP);
  856.         $db_result api_sql_query('SELECT  self_unregistration_allowed FROM '.$table_group.' WHERE id = '.$group_id);
  857.         $db_object mysql_fetch_object($db_result);
  858.         return $db_object->self_unregistration_allowed == && GroupManager :: can_user_unsubscribe($user_id$group_id);
  859.     }
  860.     /**
  861.      * Is user subscribed in group?
  862.      * @param int $user_id 
  863.      * @param int $group_id 
  864.      * @return bool TRUE if given user is subscribed in given group
  865.      */
  866.     function is_subscribed($user_id$group_id)
  867.     {
  868.         if(empty($user_idor empty($group_id)){return false;}
  869.         $table_group_user Database :: get_course_table(TABLE_GROUP_USER);
  870.         $sql 'SELECT 1 FROM '.$table_group_user.' WHERE group_id = '.$group_id.' AND user_id = '.$user_id;
  871.         $db_result api_sql_query($sql);
  872.         return Database::num_rows($db_result0;
  873.     }
  874.     /**
  875.      * Can a user subscribe to a specified group in a course
  876.      * @param int $user_id 
  877.      * @param int $group_id 
  878.      * @return bool TRUE if given user  can be subscribed in given group
  879.      */
  880.     function can_user_subscribe($user_id$group_id)
  881.     {
  882.         global $_course;
  883.         $course_code $_course['sysCode'];
  884.         $category GroupManager :: get_category_from_group($group_id);
  885.         $result CourseManager :: is_user_subscribed_in_real_or_linked_course($user_id$course_code);
  886.         $result !GroupManager :: is_subscribed($user_id$group_id);
  887.         $result &= (GroupManager :: number_of_students($group_idGroupManager :: maximum_number_of_students($group_id));
  888.         if ($category['groups_per_user'== GROUP_PER_MEMBER_NO_LIMIT)
  889.         {
  890.             $category['groups_per_user'INFINITE;
  891.         }
  892.         $result &= (GroupManager :: user_in_number_of_groups($user_id$category['id']$category['groups_per_user']);
  893.         $result &= !GroupManager :: is_tutor($user_id);
  894.         return $result;
  895.     }
  896.     /**
  897.      * Can a user unsubscribe to a specified group in a course
  898.      * @param int $user_id 
  899.      * @param int $group_id 
  900.      * @return bool TRUE if given user  can be unsubscribed from given group
  901.      * @internal for now, same as GroupManager::is_subscribed($user_id,$group_id)
  902.      */
  903.     function can_user_unsubscribe($user_id$group_id)
  904.     {
  905.         $result GroupManager :: is_subscribed($user_id$group_id);
  906.         return $result;
  907.     }
  908.     /**
  909.      * Get all subscribed users from a group
  910.      * @param int $group_id 
  911.      * @return array An array with information of all users from the given group.
  912.      *                (user_id, firstname, lastname, email)
  913.      */
  914.     function get_subscribed_users($group_id)
  915.     {
  916.         $table_user Database :: get_main_table(TABLE_MAIN_USER);
  917.         $table_group_user Database :: get_course_table(TABLE_GROUP_USER);
  918.         $sql "SELECT `ug`.`id`, `u`.`user_id`, `u`.`lastname`, `u`.`firstname`, `u`.`email`
  919.                     FROM ".$table_user." u, ".$table_group_user." ug
  920.                     WHERE `ug`.`group_id`='".$group_id."'
  921.                     AND `ug`.`user_id`=`u`.`user_id`
  922.                     ORDER BY UPPER(`u`.`lastname`), UPPER(`u`.`firstname`)";
  923.         $db_result api_sql_query($sql,__FILE__,__LINE__);
  924.         $users array ();
  925.         while ($user mysql_fetch_object($db_result))
  926.         {
  927.             $member['user_id'$user->user_id;
  928.             $member['firstname'$user->firstname;
  929.             $member['lastname'$user->lastname;
  930.             $member['email'$user->email;
  931.             $users[$member;
  932.         }
  933.         return $users;
  934.     }
  935.  
  936.     /**
  937.      * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  938.      *  Get all subscribed tutors of a group
  939.      * @param int $group_id 
  940.      * @return array An array with information of all users from the given group.
  941.      *                (user_id, firstname, lastname, email)
  942.      */
  943.     function get_subscribed_tutors($group_id,$id_only=false)
  944.     {
  945.         $table_user Database :: get_main_table(TABLE_MAIN_USER);
  946.         $table_group_tutor Database :: get_course_table(TABLE_GROUP_TUTOR);
  947.         $sql "SELECT `tg`.`id`, `u`.`user_id`, `u`.`lastname`, `u`.`firstname`, `u`.`email`
  948.                     FROM ".$table_user." u, ".$table_group_tutor." tg
  949.                     WHERE `tg`.`group_id`='".$group_id."'
  950.                     AND `tg`.`user_id`=`u`.`user_id`
  951.                     ORDER BY UPPER(`u`.`lastname`), UPPER(`u`.`firstname`)";
  952.         $db_result api_sql_query($sql,__FILE__,__LINE__);
  953.         $users array ();
  954.         while ($user mysql_fetch_object($db_result))
  955.         {
  956.             if ($id_only==false)
  957.             {
  958.                 $member['user_id'$user->user_id;
  959.                 $member['firstname'$user->firstname;
  960.                 $member['lastname'$user->lastname;
  961.                 $member['email'$user->email;
  962.                 $users[$member;
  963.             }
  964.             else
  965.             {
  966.                 $users[]=$user->user_id;
  967.             }
  968.         }
  969.         return $users;
  970.     }
  971.     /**
  972.      * Subscribe user(s) to a specified group in current course
  973.      * @param mixed $user_ids Can be an array with user-id's or a single user-id
  974.      * @param int $group_id 
  975.      * @return bool TRUE if successfull
  976.      */
  977.     function subscribe_users($user_ids$group_id)
  978.     {
  979.         $user_ids is_array($user_ids$user_ids array ($user_ids);
  980.         $result true;
  981.         foreach ($user_ids as $index => $user_id)
  982.         {
  983.             $table_group_user Database :: get_course_table(TABLE_GROUP_USER);
  984.             $sql "INSERT INTO ".$table_group_user." (user_id, group_id) VALUES ('".$user_id."', '".$group_id."')";
  985.             $result &= api_sql_query($sql,__FILE__,__LINE__);
  986.         }
  987.         return $result;
  988.     }
  989.  
  990.     /**
  991.      * Subscribe tutor(s) to a specified group in current course
  992.      * @param mixed $user_ids Can be an array with user-id's or a single user-id
  993.      * @param int $group_id 
  994.      * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  995.      * @see subscribe_users. This function is almost an exact copy of that function.
  996.      * @return bool TRUE if successfull
  997.      */
  998.     function subscribe_tutors($user_ids$group_id)
  999.     {
  1000.         $user_ids is_array($user_ids$user_ids array ($user_ids);
  1001.         $result true;
  1002.         foreach ($user_ids as $index => $user_id)
  1003.         {
  1004.             $table_group_tutor Database :: get_course_table(TABLE_GROUP_TUTOR);
  1005.             $sql "INSERT INTO ".$table_group_tutor." (user_id, group_id) VALUES ('".$user_id."', '".$group_id."')";
  1006.             $result &= api_sql_query($sql,__FILE__,__LINE__);
  1007.         }
  1008.         return $result;
  1009.     }
  1010.  
  1011.     /**
  1012.      * Unsubscribe user(s) from a specified group in current course
  1013.      * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1014.      * @param int $group_id 
  1015.      * @return bool TRUE if successfull
  1016.      */
  1017.     function unsubscribe_users($user_ids$group_id)
  1018.     {
  1019.         $user_ids is_array($user_ids$user_ids array ($user_ids);
  1020.         $table_group_user Database :: get_course_table(TABLE_GROUP_USER);
  1021.         $result &= api_sql_query('DELETE FROM '.$table_group_user.' WHERE group_id = '.$group_id.' AND user_id IN ('.implode(','$user_ids).')');
  1022.     }
  1023.     /**
  1024.      * Unsubscribe all users from one or more groups
  1025.      * @param mixed $group_id Can be an array with group-id's or a single group-id
  1026.      * @return bool TRUE if successfull
  1027.      */
  1028.     function unsubscribe_all_users($group_ids)
  1029.     {
  1030.         $group_ids is_array($group_ids$group_ids array ($group_ids);
  1031.         ifcount($group_ids0)
  1032.         {
  1033.             $table_group_user Database :: get_course_table(TABLE_GROUP_USER);
  1034.             $sql 'DELETE FROM '.$table_group_user.' WHERE group_id IN ('.implode(','$group_ids).')';
  1035.             $result api_sql_query($sql,__FILE__,__LINE__);
  1036.             return $result;
  1037.         }
  1038.         return true;
  1039.     }
  1040.     /**
  1041.      * Unsubscribe all tutors from one or more groups
  1042.      * @param mixed $group_id Can be an array with group-id's or a single group-id
  1043.      * @see unsubscribe_all_users. This function is almost an exact copy of that function.
  1044.      * @return bool TRUE if successfull
  1045.      * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1046.      */
  1047.     function unsubscribe_all_tutors($group_ids)
  1048.     {
  1049.         $group_ids is_array($group_ids$group_ids array ($group_ids);
  1050.         ifcount($group_ids0)
  1051.         {
  1052.             $table_group_tutor Database :: get_course_table(TABLE_GROUP_TUTOR);
  1053.             $sql 'DELETE FROM '.$table_group_tutor.' WHERE group_id IN ('.implode(','$group_ids).')';
  1054.             $result api_sql_query($sql,__FILE__,__LINE__);
  1055.             return $result;
  1056.         }
  1057.         return true;
  1058.     }
  1059.  
  1060.     /**
  1061.      * Is the user a tutor of this group?
  1062.      * @param $user_id the id of the user
  1063.      * @param $group_id the id of the group
  1064.      * @return boolean true/false
  1065.      * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1066.      */
  1067.     function is_tutor_of_group($user_id,$group_id)
  1068.     {
  1069.         global $_course;
  1070.  
  1071.         $table_group_tutor Database :: get_course_table(TABLE_GROUP_TUTOR);
  1072.         $sql "SELECT * FROM ".$table_group_tutor." WHERE user_id='".$user_id."' AND group_id='".$group_id."'";
  1073.         $result api_sql_query($sql,__FILE__,__LINE__);
  1074.         if (mysql_num_rows($result)>0)
  1075.         {
  1076.             return true;
  1077.         }
  1078.         else
  1079.         {
  1080.             return false;
  1081.         }
  1082.     }
  1083.  
  1084.     /**
  1085.      * Is the user part of this group? This can be a tutor or a normal member
  1086.      * you should use this function if the access to a tool or functionality is restricted to the people who are actually in the group
  1087.      * before you had to check if the user was 1. a member of the group OR 2. a tutor of the group. This function combines both
  1088.      * @param $user_id the id of the user
  1089.      * @param $group_id the id of the group
  1090.      * @return boolean true/false
  1091.      * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1092.     */
  1093.     function is_user_in_group($user_id$group_id)
  1094.     {
  1095.         $member GroupManager :: is_subscribed($user_id,$group_id);
  1096.         $tutor  GroupManager :: is_tutor_of_group($user_id,$group_id);
  1097.  
  1098.         if ($member OR $tutor)
  1099.         {
  1100.             return true;
  1101.         }
  1102.         else
  1103.         {
  1104.             return false;
  1105.         }
  1106.     }
  1107.  
  1108.  
  1109.  
  1110.     /**
  1111.      * Get all tutors for the current course.
  1112.      * @return array An array with firstname, lastname and user_id for all
  1113.      *                tutors in the current course.
  1114.      * @deprecated this function uses the old tutor implementation
  1115.      */
  1116.     function get_all_tutors()
  1117.     {
  1118.         global $_course;
  1119.         $course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1120.         $user_table Database :: get_main_table(TABLE_MAIN_USER);
  1121.         $sql "SELECT user.user_id AS user_id, user.lastname AS lastname, user.firstname AS firstname
  1122.                 FROM ".$user_table." user, ".$course_user_table." cu
  1123.                 WHERE cu.user_id=user.user_id
  1124.                 AND cu.tutor_id='1'
  1125.                 AND cu.course_code='".$_course['sysCode']."'";
  1126.         $resultTutor api_sql_query($sql,__FILE__,__LINE__);
  1127.         $tutors array ();
  1128.         while ($tutor mysql_fetch_array($resultTutor))
  1129.         {
  1130.             $tutors[$tutor;
  1131.         }
  1132.         return $tutors;
  1133.     }
  1134.  
  1135.  
  1136.     /**
  1137.      * Is user a tutor in current course
  1138.      * @param int $user_id 
  1139.      * @return bool TRUE if given user is a tutor in the current course.
  1140.      * @deprecated this function uses the old tutor implementation
  1141.      */
  1142.     function is_tutor($user_id)
  1143.     {
  1144.         global $_course;
  1145.         $course_user_table Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1146.         $sql "SELECT tutor_id FROM ".$course_user_table."
  1147.                      WHERE `user_id`='".$user_id."'
  1148.                      AND `course_code`='".$_course['sysCode']."'"."AND tutor_id=1";
  1149.         $db_result api_sql_query($sql,__FILE__,__LINE__);
  1150.         $result (mysql_num_rows($db_result0);
  1151.         return $result;
  1152.     }
  1153.  
  1154.     /**
  1155.      * Get all group's from a given course in which a given user is ubscribed
  1156.      * @author  Patrick Cool
  1157.      * @param     string $course_db: the database of the course you want to
  1158.      *  retrieve the groups for
  1159.      * @param integer $user_id: the ID of the user you want to know all its
  1160.      *  group memberships
  1161.      */
  1162.     function get_group_ids($course_db,$user_id)
  1163.     {
  1164.     $groups array();
  1165.     $tbl_group Database::get_course_table(TABLE_GROUP_USER,$course_db);
  1166.  
  1167.     $sql "SELECT group_id FROM $tbl_group WHERE user_id = '$user_id'";
  1168.     $groupres api_sql_query($sql);
  1169.  
  1170.     // uncommenting causes a bug in Agenda AND announcements because there we check if the return value of this function is an array or not
  1171.     //$groups=array();
  1172.  
  1173.     if($groupres)
  1174.     {
  1175.         while ($myrowmysql_fetch_array($groupres))
  1176.             $groups[]=$myrow['group_id'];
  1177.     }
  1178.  
  1179.     return $groups;
  1180.     }
  1181.     /*
  1182.     -----------------------------------------------------------
  1183.     Group functions
  1184.     these take virtual/linked courses into account when necessary
  1185.     -----------------------------------------------------------
  1186.     */
  1187.     /**
  1188.     *    Get a combined list of all users of the real course $course_code
  1189.     *        and all users in virtual courses linked to this course $course_code
  1190.     *    Filter user list: remove duplicate users; plus
  1191.     *        remove users that
  1192.     *        - are already in the current group $group_id;
  1193.     *        - do not have student status in these courses;
  1194.     *        - are not appointed as tutor (group assistent) for this group;
  1195.     *        - have already reached their maximum # of groups in this course.
  1196.     *
  1197.     *    Originally to get the correct list of users a big SQL statement was used,
  1198.     *    but this has become more complicated now there is not just one real course but many virtual courses.
  1199.     *    Still, that could have worked as well.
  1200.     *
  1201.     *    @version 1.1.3
  1202.     *    @author Roan Embrechts
  1203.     */
  1204.     function get_complete_list_of_users_that_can_be_added_to_group($course_code$group_id)
  1205.     {
  1206.         global $_course$_user;
  1207.         $category GroupManager :: get_category_from_group($group_id$course_code);
  1208.         $number_of_groups_limit $category['groups_per_user'== GROUP_PER_MEMBER_NO_LIMIT INFINITE $category['groups_per_user'];
  1209.         $real_course_code $_course['sysCode'];
  1210.         $real_course_info Database :: get_course_info($real_course_code);
  1211.         $real_course_user_list CourseManager :: get_user_list_from_course_code($virtual_course_code);
  1212.         //get list of all virtual courses
  1213.         $user_subscribed_course_list CourseManager :: get_list_of_virtual_courses_for_specific_user_and_real_course($_user['user_id']$real_course_code);
  1214.         //add real course to the list
  1215.         $user_subscribed_course_list[$real_course_info;
  1216.         if (!is_array($user_subscribed_course_list))
  1217.             return;
  1218.         //for all courses...
  1219.         foreach ($user_subscribed_course_list as $this_course)
  1220.         {
  1221.             $this_course_code $this_course["code"];
  1222.             $course_user_list CourseManager :: get_user_list_from_course_code($this_course_code);
  1223.             //for all users in the course
  1224.             foreach ($course_user_list as $this_user)
  1225.             {
  1226.                 $user_id $this_user["user_id"];
  1227.                 $loginname $this_user["username"];
  1228.                 $lastname $this_user["lastname"];
  1229.                 $firstname $this_user["firstname"];
  1230.                 $status $this_user["status"];
  1231.                 //$role =  $this_user["role"];
  1232.                 $tutor_id $this_user["tutor_id"];
  1233.                 $full_name $lastname.", ".$firstname;
  1234.                 if ($lastname == "" || $firstname == '')
  1235.                     $full_name $loginname;
  1236.                 $complete_user["user_id"$user_id;
  1237.                 $complete_user["full_name"$full_name;
  1238.                 $complete_user['firstname'$firstname;
  1239.                 $complete_user['lastname'$lastname;
  1240.                 $complete_user["status"$status;
  1241.                 $complete_user["tutor_id"$tutor_id;
  1242.                 $student_number_of_groups GroupManager :: user_in_number_of_groups($user_id$category['id']);
  1243.                 //filter: only add users that have not exceeded their maximum amount of groups
  1244.                 if ($student_number_of_groups $number_of_groups_limit)
  1245.                 {
  1246.                     $complete_user_list[$complete_user;
  1247.                 }
  1248.             }
  1249.         }
  1250.         if (is_array($complete_user_list))
  1251.         {
  1252.             //sort once, on array field "full_name"
  1253.             $complete_user_list TableSort :: sort_table($complete_user_list"full_name");
  1254.             //filter out duplicates, based on field "user_id"
  1255.             $complete_user_list GroupManager :: filter_duplicates($complete_user_list"user_id");
  1256.             $complete_user_list GroupManager :: filter_users_already_in_group($complete_user_list$group_id);
  1257.             //$complete_user_list = GroupManager :: filter_only_students($complete_user_list);
  1258.         }
  1259.         return $complete_user_list;
  1260.     }
  1261.     /**
  1262.     *    Filter out duplicates in a multidimensional array
  1263.     *    by comparing field $compare_field.
  1264.     *
  1265.     *    @param $user_array_in list of users (must be sorted).
  1266.     *    @param string $compare_field, the field to be compared
  1267.     */
  1268.     function filter_duplicates($user_array_in$compare_field)
  1269.     {
  1270.         $total_number count($user_array_in);
  1271.         $user_array_out[0$user_array_in[0];
  1272.         $count_out 0;
  1273.         for ($count_in 1$count_in $total_number$count_in ++)
  1274.         {
  1275.             if ($user_array_in[$count_in][$compare_field!= $user_array_out[$count_out][$compare_field])
  1276.             {
  1277.                 $count_out ++;
  1278.                 $user_array_out[$count_out$user_array_in[$count_in];
  1279.             }
  1280.         }
  1281.         return $user_array_out;
  1282.     }
  1283.     /**
  1284.     *    Filters from the array $user_array_in the users already in the group $group_id.
  1285.     */
  1286.     function filter_users_already_in_group($user_array_in$group_id)
  1287.     {
  1288.         foreach ($user_array_in as $this_user)
  1289.         {
  1290.             if (!GroupManager :: is_subscribed($this_user['user_id']$group_id))
  1291.             {
  1292.                 $user_array_out[$this_user;
  1293.             }
  1294.         }
  1295.         return $user_array_out;
  1296.     }
  1297.     /**
  1298.     * Remove all users that are not students and all users who have tutor status
  1299.     * from  the list.
  1300.     */
  1301.     function filter_only_students($user_array_in)
  1302.     {
  1303.         $user_array_out array ();
  1304.         foreach ($user_array_in as $this_user)
  1305.         {
  1306.             if ($this_user['status'== STUDENT && $this_user['tutor_id'== 0)
  1307.             {
  1308.                 $user_array_out[$this_user;
  1309.             }
  1310.         }
  1311.         return $user_array_out;
  1312.     }
  1313.     /**
  1314.      * Check if a user has access to a certain group tool
  1315.      * @param int $user_id The user id
  1316.      * @param int $group_id The group id
  1317.      * @param constant $tool The tool to check the access rights. This should be
  1318.      *  one of constants: GROUP_TOOL_DOCUMENTS
  1319.      * @return bool True if the given user has access to the given tool in the
  1320.      *  given course.
  1321.      */
  1322.     function user_has_access($user_id$group_id$tool)
  1323.     {
  1324.         switch ($tool)
  1325.         {
  1326.             case GROUP_TOOL_DOCUMENTS :
  1327.                 $state_key 'doc_state';
  1328.                 break;
  1329.             case GROUP_TOOL_CALENDAR :
  1330.                 $state_key 'calendar_state';
  1331.                 break;
  1332.             case GROUP_TOOL_ANNOUNCEMENT :
  1333.                 $state_key 'announcements_state';
  1334.                 break;
  1335.             case GROUP_TOOL_WORK :
  1336.                 $state_key 'work_state';
  1337.                 break;    
  1338.             default:
  1339.                 return false;
  1340.         }
  1341.         $group GroupManager :: get_group_properties($group_id);
  1342.         if ($group[$state_key== TOOL_NOT_AVAILABLE)
  1343.         {
  1344.             return false;
  1345.         }
  1346.         elseif ($group[$state_key== TOOL_PUBLIC)
  1347.         {
  1348.             return true;
  1349.         }
  1350.         elseif (api_is_allowed_to_edit())
  1351.         {
  1352.             return true;
  1353.         }
  1354.         elseif($group['tutor_id'== $user_id)
  1355.         {
  1356.             return true;
  1357.         }
  1358.         else
  1359.         {
  1360.             return GroupManager :: is_subscribed($user_id$group_id);
  1361.         }
  1362.     }
  1363.     /**
  1364.      * Get all groups where a specific user is subscribed
  1365.      */
  1366.     function get_user_group_name($user_id){
  1367.         
  1368.         $table_group_user=Database::get_course_table(TABLE_GROUP_USER);
  1369.         $table_group=Database::get_course_table(TABLE_GROUP);
  1370.         
  1371.         $sql_groups 'SELECT name FROM '.$table_group.' g,'.$table_group_user.' gu WHERE gu.user_id="'.$user_id.'" AND gu.group_id=g.id';
  1372.         $res api_sql_query($sql_groups,__FILE__,__LINE__);
  1373.         
  1374.         $groups=array();
  1375.         while($group mysql_fetch_array($res))
  1376.         {
  1377.             $groups[.= $group['name'];
  1378.         }
  1379.         return $groups;
  1380.     }
  1381. }
  1382. ?>

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