Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / group / db / dbsetup.php
blob8c536720febda4b1576b2c37bc1ad83f98087a11
1 <?php
2 /**
3 * Functions required for setting up the database to use the new groups.
5 * TODO: replace with, postrges7.sql, mysql.php, install.xml
7 * @copyright &copy; 2006 The Open University
8 * @author J.White AT open.ac.uk
9 * @author N.D.Freear AT open.ac.uk
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 * @package groups
13 require_once($CFG->libdir.'/datalib.php');
16 // @@@ TO DO Needs lots of sorting out so proper install/upgrade and also
17 // so used new db stuff. In practice we probably don't actually want to rename
18 // the tables (the group_member table in particular as this is basically
19 // unchanged)that already exist on the whole if we can help it so this and the
20 // the other dblib files should really be sorted out to do this.
22 // Database changes
23 // New tables - the SQL for creating the tables is below (though should be
24 // foreign keys!) - however it might be more sensible to modify the existing
25 // tables instead as much as we can so that we don't need to copy data over and
26 // that any existing code that does assume the existence of those tables
27 // might still work.
28 // Another caveat - the code below doesn't contain the new fields in the
29 // groupings table - viewowngroup, viewallgroupsmemebers, viewallgroupsactivities,
30 // teachersgroupmark, teachersgroupview, teachersoverride, teacherdeletetable.
31 // Other changes:
32 // * course currently contains groupmode and groupmodeforce - we need to change
33 // this to groupingid which is either null or a forced groupingid - need to
34 // copy over existing data sensibly.
35 // * course_modules needs groupingid (think it previously had groupmode)
38 // Change database tables - course table need to remove two fields add groupingid field
39 // Move everything over
40 // Course module instance need to add groupingid field
41 // Module table - add group support field.
42 // Add deletable by teacher field.
45 /**
46 * Creates the database tables required
48 function groups_create_database_tables() {
49 global $CFG;
51 if ('mysql' == $CFG->dbfamily) {
53 $createcoursegrouptablesql = "CREATE TABLE IF NOT EXISTS `{$CFG->prefix}groups_courses_groups` (
54 `id` int(10) unsigned NOT NULL auto_increment,
55 `courseid` int(10) unsigned NOT NULL default '0',
56 `groupid` int(11) NOT NULL,
57 PRIMARY KEY (`id`),
58 UNIQUE KEY `id` (`id`),
59 KEY `courseid` (`courseid`)
60 ) ";
62 $creategroupstablesql = "CREATE TABLE IF NOT EXISTS `{$CFG->prefix}groups_groups` (
63 `id` int(10) unsigned NOT NULL auto_increment,
64 `name` varchar(254) collate latin1_general_ci NOT NULL default '',
65 `description` text collate latin1_general_ci NOT NULL,
66 `enrolmentkey` varchar(50) collate latin1_general_ci NOT NULL default '',
67 `lang` varchar(10) collate latin1_general_ci NOT NULL default 'en',
68 `theme` varchar(50) collate latin1_general_ci NOT NULL default '',
69 `picture` int(10) unsigned NOT NULL default '0',
70 `hidepicture` int(2) unsigned NOT NULL default '0',
71 `timecreated` int(10) unsigned NOT NULL default '0',
72 `timemodified` int(10) unsigned NOT NULL default '0',
73 PRIMARY KEY (`id`),
74 UNIQUE KEY `id` (`id`)
75 ) ";
77 $creategroupsuserstablesql = "CREATE TABLE IF NOT EXISTS `{$CFG->prefix}groups_groups_users` (
78 `id` int(10) unsigned NOT NULL auto_increment,
79 `groupid` int(10) unsigned NOT NULL default '0',
80 `userid` int(10) unsigned NOT NULL default '0',
81 `timeadded` int(10) unsigned NOT NULL default '0',
82 PRIMARY KEY (`id`),
83 UNIQUE KEY `id` (`id`),
84 KEY `groupid` (`groupid`),
85 KEY `userid` (`userid`)
86 ) ";
88 $createcoursesgroupingtablesql = "CREATE TABLE IF NOT EXISTS `{$CFG->prefix}groups_courses_groupings` (
89 `id` int(10) unsigned NOT NULL auto_increment,
90 `courseid` int(10) unsigned NOT NULL default '0',
91 `groupingid` mediumint(9) NOT NULL,
92 PRIMARY KEY (`id`),
93 UNIQUE KEY `id` (`id`),
94 KEY `courseid` (`courseid`)
95 ) ";
97 $creategroupingstablesql = "CREATE TABLE `{$CFG->prefix}groups_groupings` (
98 `id` int(10) unsigned NOT NULL auto_increment,
99 `name` varchar(254) collate latin1_general_ci NOT NULL,
100 `description` text collate latin1_general_ci NOT NULL default '',
101 `timecreated` int(10) unsigned NOT NULL default 0,
102 `viewowngroup` binary(1) NOT NULL default 1,
103 `viewallgroupsmembers` binary(1) NOT NULL default 0,
104 `viewallgroupsactivities` binary(1) NOT NULL default 0,
105 `teachersgroupmark` binary(1) NOT NULL default 0,
106 `teachersgroupview` binary(1) NOT NULL default 0,
107 `teachersoverride` binary(1) NOT NULL default 0,
108 PRIMARY KEY (`id`),
109 UNIQUE KEY `id` (`id`)
110 ) ";
112 $creategroupingsgroupstablesql = "CREATE TABLE IF NOT EXISTS `{$CFG->prefix}groups_groupings_groups` (
113 `id` int(10) unsigned NOT NULL auto_increment,
114 `groupingid` int(10) unsigned default '0',
115 `groupid` int(10) NOT NULL,
116 `timeadded` int(10) unsigned NOT NULL default '0',
117 PRIMARY KEY (`id`),
118 UNIQUE KEY `id` (`id`),
119 KEY `courseid` (`groupingid`)
120 ) ";
122 } else { //postgres7
124 $createcoursegrouptablesql = "CREATE TABLE {$CFG->prefix}groups_courses_groups (
125 id SERIAL PRIMARY KEY,
126 courseid integer NOT NULL default '0',
127 groupid integer NOT NULL default '0'
129 CREATE INDEX {$CFG->prefix}groups_courses_groups_courseid_idx ON {$CFG->prefix}groups_courses_groups (courseid);
131 //?? CONSTRAINT {$CFG->prefix}groups_courses_groups_id_courseid_uk UNIQUE (id, courseid)
133 $creategroupstablesql = "CREATE TABLE {$CFG->prefix}groups_groups (
134 id SERIAL PRIMARY KEY,
135 name varchar(255) NOT NULL,
136 description text NOT NULL default '',
137 enrolmentkey varchar(50) NOT NULL default '',
138 lang varchar(10) NOT NULL default 'en',
139 theme varchar(50) NOT NULL default '',
140 picture integer NOT NULL default '0',
141 hidepicture integer NOT NULL default '0',
142 timecreated integer NOT NULL default '0',
143 timemodified integer NOT NULL default '0'
144 ) ";
146 $creategroupsuserstablesql = "CREATE TABLE {$CFG->prefix}groups_groups_users (
147 id SERIAL PRIMARY KEY,
148 groupid integer NOT NULL default '0',
149 userid integer NOT NULL default '0',
150 timeadded integer NOT NULL default '0'
152 CREATE INDEX {$CFG->prefix}groups_groups_users_groupid_idx ON {$CFG->prefix}groups_groups_users (groupid);
153 CREATE INDEX {$CFG->prefix}groups_groups_users_userid_idx ON {$CFG->prefix}groups_groups_users (userid);
154 COMMENT ON TABLE {$CFG->prefix}groups_groups_users IS 'New groupings (OU).';
157 $createcoursesgroupingtablesql = "CREATE TABLE {$CFG->prefix}groups_courses_groupings (
158 id SERIAL PRIMARY KEY,
159 courseid integer NOT NULL default '0',
160 groupingid integer NOT NULL
162 CREATE INDEX {$CFG->prefix}groups_courses_groupings_courseid_idx ON {$CFG->prefix}groups_courses_groupings (courseid);
163 COMMENT ON TABLE {$CFG->prefix}groups_courses_groupings IS 'New groupings (OU).';
166 $creategroupingstablesql = "CREATE TABLE {$CFG->prefix}groups_groupings (
167 id SERIAL PRIMARY KEY,
168 name varchar(254) NOT NULL default,
169 description text NOT NULL default '',
170 timecreated integer NOT NULL default 0,
171 viewowngroup integer NOT NULL default 1,
172 viewallgroupsmembers integer NOT NULL default 0,
173 viewallgroupsactivities integer NOT NULL default 0,
174 teachersgroupmark integer NOT NULL default 0,
175 teachersgroupview integer NOT NULL default 0,
176 teachersoverride integer NOT NULL default 0
177 ) ";
179 $creategroupingsgroupstablesql = "CREATE TABLE {$CFG->prefix}groups_groupings_groups (
180 id SERIAL PRIMARY KEY,
181 groupingid integer default '0',
182 groupid integer NOT NULL,
183 timeadded integer NOT NULL default '0'
185 CREATE INDEX {$CFG->prefix}groups_groupings_groups_groupingid_idx ON {$CFG->prefix}groups_groupings_groups (groupingid);
189 modify_database('', $createcoursegrouptablesql);
190 modify_database('', $creategroupstablesql);
191 modify_database('', $creategroupsuserstablesql);
192 modify_database('', $createcoursesgroupingtablesql);
193 modify_database('', $creategroupingstablesql);
194 modify_database('', $creategroupingsgroupstablesql);
199 * Copies any old style moodle group to a new style moodle group - we'll need this for any upgrade code
200 * @param int $groupid The 'old moodle groups' id of the group to copy
201 * @param int $courseid The course id
202 * @param boolean True if the operation was successful, false otherwise.
204 function groups_db_copy_moodle_group_to_imsgroup($groupid, $courseid) {
206 $success = true;
208 $groupsettings = get_record('groups', 'id ', $groupid, '');
210 // Only copy the group if the group exists.
211 if ($groupsettings != false) {
212 $record = new Object();
213 $record->name = $groupsettings->name;
214 $record->description = $groupsettings->description;
215 $record->password = $groupsettings->password;
216 $record->lang = $groupsettings->lang;
217 $record->theme = $groupsettings->theme;
218 $record->picture = $groupsettings->picture;
219 $record->hidepicture = $groupsettings->hidepicture;
220 $record->timecreated = $groupsettings->timecreated;
221 $record->timemodified = $groupsettings->timemodified;
223 $newgroupid = insert_record('groups_groups', $record);
224 if (!$newgroupid) {
225 $success = false;
228 $courserecord = new Object();
229 $courserecord->courseid = $groupsettings->courseid;
230 $courserecord->groupid = $newgroupid;
232 $added = insert_record('groups_courses_groups', $courserecord);
234 if (!$added) {
235 $success = false;
238 // Copy over the group members
239 $groupmembers = get_records('groups_users', 'groupid', $groupid);
240 if ($groupmembers != false) {
241 foreach($groupmembers as $member) {
242 $record = new Object();
243 $record->groupid = $newgroupid;
244 $record->userid = $member->userid;
245 $useradded = insert_record('groups_groups_users', $record);
246 if (!$useradded) {
247 $success = false;
253 if (!$success) {
254 notify('Copy operations from Moodle groups to IMS Groups failed');
257 return $success;