📄 groupmanager.lib.php
字号:
<?php/*============================================================================== Dokeos - elearning and course management software Copyright (c) 2004 Dokeos S.A. Copyright (c) 2003 Ghent University (UGent) Copyright (c) 2001 Universite catholique de Louvain (UCL) For a full list of contributors, see "credits.txt". The full license can be read in "license.txt". This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the GNU General Public License for more details. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium Mail: info@dokeos.com==============================================================================*//**==============================================================================* This is the group library for Dokeos.* Include/require it in your code to use its functionality.** @author various authors* @author Roan Embrechts (Vrije Universiteit Brussel), virtual courses support + some cleaning* @author Bart Mollet (HoGent), all functions in class GroupManager* @package dokeos.library==============================================================================*/require_once ('database.lib.php');require_once ('course.lib.php');require_once ('tablesort.lib.php');require_once ('fileManage.lib.php');require_once ('fileUpload.lib.php');/** * infinite */define("INFINITE", "99999");/** * No limit on the number of users in a group */define("MEMBER_PER_GROUP_NO_LIMIT", "0");/** * No limit on the number of groups per user */define("GROUP_PER_MEMBER_NO_LIMIT", "0");/** * The tools of a group can have 3 states * - not available * - public * - private */define("TOOL_NOT_AVAILABLE", "0");define("TOOL_PUBLIC", "1");define("TOOL_PRIVATE", "2");/** * Constants for the available group tools *///define("GROUP_TOOL_FORUM", "0");define("GROUP_TOOL_DOCUMENTS", "1");define("GROUP_TOOL_CALENDAR","2");define("GROUP_TOOL_ANNOUNCEMENT","3");define("GROUP_TOOL_WORK","4");/** * Fixed id's for group categories * - VIRTUAL_COURSE_CATEGORY: in this category groups are created based on the * virtual course of a course * - DEFAULT_GROUP_CATEGORY: When group categories aren't available (platform- * setting), all groups are created in this 'dummy'-category */define("VIRTUAL_COURSE_CATEGORY", 1);define("DEFAULT_GROUP_CATEGORY", 2);/** * This library contains some functions for group-management. * @author Bart Mollet * @package dokeos.library * @todo Add $course_code parameter to all functions. So this GroupManager can * be used outside a session. */class GroupManager{ /*============================================================================== * GROUP FUNCTIONS ==============================================================================*/ /** * Get list of groups for current course. * @param int $category The id of the category from which the groups are * requested * @param string $course_code Default is current course * @return array An array with all information about the groups. */ function get_group_list($category = null, $course_code = null) { global $_user; $course_db = ''; if ($course_code != null) { $course_info = Database :: get_course_info($course_code); $course_db = $course_info['database']; } $table_group = Database :: get_course_table(TABLE_GROUP, $course_db); $table_user = Database :: get_main_table(TABLE_MAIN_USER); $table_course = Database :: get_main_table(TABLE_MAIN_COURSE); $table_group_user = Database :: get_course_table(TABLE_GROUP_USER, $course_db); $sql = "SELECT g.id , g.name , g.description , g.category_id, g.max_student maximum_number_of_members, g.secret_directory, g.self_registration_allowed, g.self_unregistration_allowed, ug.user_id is_member, COUNT(ug2.id) number_of_members FROM ".$table_group." `g` LEFT JOIN ".$table_group_user." `ug` ON `ug`.`group_id` = `g`.`id` AND `ug`.`user_id` = '".$_user['user_id']."' LEFT JOIN ".$table_group_user." `ug2` ON `ug2`.`group_id` = `g`.`id`"; if ($category != null) $sql .= " WHERE `g`.`category_id` = '".$category."' "; $sql .= " GROUP BY `g`.`id` ORDER BY UPPER(g.name)"; $groupList = api_sql_query($sql,__FILE__,__LINE__); $groups = array (); while ($thisGroup = mysql_fetch_array($groupList)) { if ($thisGroup['category_id'] == VIRTUAL_COURSE_CATEGORY) { $sql = "SELECT title FROM $table_course WHERE code = '".$thisGroup['name']."'"; $obj = mysql_fetch_object(api_sql_query($sql,__FILE__,__LINE__)); $thisGroup['name'] = $obj->title; } $groups[] = $thisGroup; } return $groups; } /** * Create a group * @param string $name The name for this group * @param int $tutor The user-id of the group's tutor * @param int $places How many people can subscribe to the new group */ function create_group($name, $category_id, $tutor, $places) { global $_course,$_user; $currentCourseRepository = $_course['path']; $table_group = Database :: get_course_table(TABLE_GROUP); $table_forum = Database :: get_course_table(TABLE_FORUM); $category = GroupManager :: get_category($category_id); if (intval($places) == 0) //if the amount of users per group is not filled in, use the setting from the category { $places = $category['max_student']; } $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']."'"; api_sql_query($sql,__FILE__,__LINE__); $lastId = mysql_insert_id(); /*$secret_directory = uniqid("")."_team_".$lastId; while (is_dir(api_get_path(SYS_COURSE_PATH).$currentCourseRepository."/group/$secret_directory")) { $secret_directory = uniqid("")."_team_".$lastId; } FileManager :: mkdirs(api_get_path(SYS_COURSE_PATH).$currentCourseRepository."/group/".$secret_directory, 0770); */ $desired_dir_name= '/'.replace_dangerous_char($name,'strict').'_groupdocs'; $dir_name = create_unexisting_directory($_course,$_user['user_id'],$lastId,NULL,api_get_path(SYS_COURSE_PATH).$currentCourseRepository.'/document',$desired_dir_name); /* Stores the directory path into the group table */ $sql = "UPDATE ".$table_group." SET name = '".mysql_real_escape_string($name)."', secret_directory = '".$dir_name."' WHERE id ='".$lastId."'"; api_sql_query($sql,__FILE__,__LINE__); // create a forum if needed if ($category['forum_state'] > 0) { include_once(api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php'); include_once(api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php'); $forum_categories = get_forum_categories(); $values['forum_title'] = get_lang('ForumOfGroup').' '.$name; $counter = 0; foreach ($forum_categories as $key=>$value) { if ($counter==0) { $forum_category_id = $key; } $counter++; } $values['forum_category'] = $forum_category_id; $values['allow_anonymous_group']['allow_anonymous'] = 0; $values['students_can_edit_group']['students_can_edit'] = 0; $values['approval_direct_group']['approval_direct'] = 0; $values['allow_attachments_group']['allow_attachments'] = 1; $values['allow_new_threads_group']['allow_new_threads'] = 1; $values['default_view_type_group']['default_view_type']=api_get_setting('default_forum_view'); $values['group_forum'] = $lastId; if ($category['forum_state'] == '1') { $values['public_private_group_forum_group']['public_private_group_forum']='public'; } else { $values['public_private_group_forum_group']['public_private_group_forum']='private'; } store_forum($values); } return $lastId; } /** * Create subgroups. * This function creates new groups based on an existing group. It will * create the specified number of groups and fill those groups with users * from the base group * @param int $group_id The group from which subgroups have to be created. * @param int $number_of_groups The number of groups that have to be created */ function create_subgroups($group_id, $number_of_groups) { $table_group = Database :: get_course_table(TABLE_GROUP); $category_id = GroupManager :: create_category('Subgroups', '', TOOL_PRIVATE, TOOL_PRIVATE, 0, 0, 1, 1); $users = GroupManager :: get_users($group_id); $group_ids = array (); for ($group_nr = 1; $group_nr <= $number_of_groups; $group_nr ++) { $group_ids[] = GroupManager :: create_group('SUBGROUP '.$group_nr, $category_id, 0, 0); } $members = array (); foreach ($users as $index => $user_id) { GroupManager :: subscribe_users($user_id, $group_ids[$index % $number_of_groups]); $members[$group_ids[$index % $number_of_groups]]++; } foreach ($members as $group_id => $places) { $sql = "UPDATE $table_group SET max_student = $places WHERE id = $group_id"; api_sql_query($sql,__FILE__,__LINE__); } } /** * Create groups from all virtual courses in the given course. */ function create_groups_from_virtual_courses() { GroupManager :: delete_category(VIRTUAL_COURSE_CATEGORY); $id = GroupManager :: create_category(get_lang('GroupsFromVirtualCourses'), '', TOOL_NOT_AVAILABLE, TOOL_NOT_AVAILABLE, 0, 0, 1, 1); $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY); $sql = "UPDATE ".$table_group_cat." SET id=".VIRTUAL_COURSE_CATEGORY." WHERE id=$id"; api_sql_query($sql,__FILE__,__LINE__); $course = api_get_course_info(); $course['code'] = $course['sysCode']; $course['title'] = $course['name']; $virtual_courses = CourseManager :: get_virtual_courses_linked_to_real_course($course['sysCode']); $group_courses = $virtual_courses; $group_courses[] = $course; $ids = array (); foreach ($group_courses as $index => $group_course) { $users = CourseManager :: get_user_list_from_course_code($group_course['code']); $members = array (); foreach ($users as $index => $user) { if ($user['status'] == 5 && $user['tutor_id'] == 0) { $members[] = $user['user_id']; } } $id = GroupManager :: create_group($group_course['code'], VIRTUAL_COURSE_CATEGORY, 0, count($members)); GroupManager :: subscribe_users($members, $id); $ids[] = $id; } return $ids; } /** * Create a group for every class subscribed to the current course * @param int $category_id The category in which the groups should be * created */ function create_class_groups($category_id) { global $_course; $classes = ClassManager::get_classes_in_course($_course['sysCode']); $group_ids = array(); foreach($classes as $index => $class) { $users = ClassManager::get_users($class['id']); $group_id = GroupManager::create_group($class['name'],$category_id,0,count($users)); $user_ids = array(); foreach($users as $index_user => $user) { $user_ids[] = $user['user_id']; } GroupManager::subscribe_users($user_ids,$group_id); $group_ids[] = $group_id; } return $group_ids; } /** * deletes groups and their data. * @author Christophe Gesche <christophe.gesche@claroline.net> * @author Hugues Peeters <hugues.peeters@claroline.net> * @author Bart Mollet * @param mixed $groupIdList - group(s) to delete. It can be a single id * (int) or a list of id (array). * @param string $course_code Default is current course * @return integer - number of groups deleted. */ function delete_groups($group_ids, $course_code = null) { $course_db = ''; if ($course_code != null) { $course = Database :: get_course_info($course_code); $course['path'] = $course['directory']; $course_db = $course['database']; } else { $course = api_get_course_info(); } // Database table definitions $group_table = Database :: get_course_table(TABLE_GROUP, $course_db); $group_user_table = Database :: get_course_table(TABLE_GROUP_USER, $course_db); $forum_table = Database :: get_course_table(TABLE_FORUM, $course_db); $forum_post_table = Database :: get_course_table(TABLE_FORUM_POST, $course_db); $forum_post_text_table = Database :: get_course_table(TOOL_FORUM_POST_TEXT_TABLE, $course_db); $forum_topic_table = Database :: get_course_table(TABLE_FORUM_POST, $course_db); $group_ids = is_array($group_ids) ? $group_ids : array ($group_ids); // define repository for deleted element $group_garbage = api_get_path(GARBAGE_PATH).$course['path']."/group/"; $perm = api_get_setting('permissions_for_new_directories'); $perm = (!empty($perm)?$perm:'0770'); if (!file_exists($group_garbage)) FileManager :: mkdirs($group_garbage, $perm); // Unsubscribe all users GroupManager :: unsubscribe_all_users($group_ids); $sql = 'SELECT id, secret_directory FROM '.$group_table.' WHERE id IN ('.implode(' , ', $group_ids).')'; $db_result = api_sql_query($sql,__FILE__,__LINE__); $forum_ids = array ();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -