-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlocallib.php
112 lines (93 loc) · 3.52 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Plugin local library methods
*
* @package local_metagroups
* @copyright 2014 Paul Holden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/grouplib.php');
require_once($CFG->dirroot . '/group/lib.php');
/**
* Get a list of parent courses for a given course ID
*
* @param int|null $courseid or null for all parents
* @return array of course IDs
*/
function local_metagroups_parent_courses($courseid = null) {
global $DB;
$conditions = ['enrol' => 'meta', 'status' => ENROL_INSTANCE_ENABLED];
if ($courseid !== null) {
$conditions['customint1'] = $courseid;
}
return $DB->get_records_menu('enrol', $conditions, 'sortorder', 'id, courseid');
}
/**
* Get a list of all child courses for a given course ID
*
* @param int $courseid
* @return array of course IDs
*/
function local_metagroups_child_courses($courseid) {
global $DB;
$conditions = ['enrol' => 'meta', 'courseid' => $courseid, 'status' => ENROL_INSTANCE_ENABLED];
return $DB->get_records_menu('enrol', $conditions, 'sortorder', 'id, customint1');
}
/**
* Run synchronization process
*
* @param progress_trace $trace
* @param int|null $courseid or null for all courses
* @return void
*/
function local_metagroups_sync(progress_trace $trace, $courseid = null) {
global $DB;
if ($courseid !== null) {
$courseids = [$courseid];
} else {
$courseids = local_metagroups_parent_courses();
}
foreach (array_unique($courseids) as $courseid) {
$parent = get_course($courseid);
// If parent course doesn't use groups, we can skip synchronization.
if (groups_get_course_groupmode($parent) == NOGROUPS) {
continue;
}
$trace->output($parent->fullname, 1);
$children = local_metagroups_child_courses($parent->id);
foreach ($children as $childid) {
$child = get_course($childid);
$trace->output($child->fullname, 2);
$groups = groups_get_all_groups($child->id);
foreach ($groups as $group) {
if (! $metagroup = $DB->get_record('groups', ['courseid' => $parent->id, 'idnumber' => $group->id])) {
$metagroup = new stdClass();
$metagroup->courseid = $parent->id;
$metagroup->idnumber = $group->id;
$metagroup->name = $group->name;
$metagroup->id = groups_create_group($metagroup, false, false);
}
$trace->output($metagroup->name, 3);
$users = groups_get_members($group->id);
foreach ($users as $user) {
groups_add_member($metagroup->id, $user->id, 'local_metagroups', $group->id);
}
}
}
}
}