-
Notifications
You must be signed in to change notification settings - Fork 2
/
autogroup.php
150 lines (129 loc) · 5.7 KB
/
autogroup.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?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/>.
/**
* Relationship's Autogroup page
*
* @package local_relationship
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__.'/../../config.php');
require($CFG->dirroot.'/local/relationship/lib.php');
require_once($CFG->dirroot.'/local/relationship/locallib.php');
require_once($CFG->dirroot.'/group/lib.php');
require_login();
$relationshipid = required_param('relationshipid', PARAM_INT);
$relationship = $DB->get_record('relationship', array('id' => $relationshipid), '*', MUST_EXIST);
$context = context::instance_by_id($relationship->contextid, MUST_EXIST);
require_capability('local/relationship:manage', $context);
if (!empty($relationship->component)) {
print_error('cantedit', 'local_relationship');
}
$baseurl = new moodle_url('/local/relationship/autogroup.php', array('relationshipid' => $relationship->id));
$returnurl = new moodle_url('/local/relationship/groups.php', array('relationshipid' => $relationship->id));
relationship_set_header($context, $baseurl, $relationship, 'groups');
// Print the page and form
$preview = '';
$error = '';
/// Create the form
$editform = new \local_relationship\form\autogroup(null, array('relationshipid' => $relationshipid));
$editform->set_data(array('relationshipid' => $relationshipid));
/// Handle form submission
if ($editform->is_cancelled()) {
redirect($returnurl);
} elseif ($data = $editform->get_data()) {
$numgrps = $data->number;
$userlimit = $data->userlimit;
$relationshipcohortid = $data->relationshipcohortid;
$existing_groups = $DB->get_records_menu('relationship_groups', array('relationshipid' => $relationshipid), null, 'name, id');
$course_groups = relationship_get_other_courses_group_names($relationshipid);
// Plan the allocation
$new_groups = array();
if ($relationshipcohortid) {
$sql = "SELECT cm.userid, u.firstname, u.lastname
FROM {relationship_cohorts} rc
JOIN {cohort_members} cm ON (cm.cohortid = rc.cohortid)
JOIN {user} u ON (u.id = cm.userid)
WHERE rc.id = :relationshipcohortid
ORDER BY u.firstname";
$members = $DB->get_records_sql($sql, array('relationshipcohortid' => $relationshipcohortid));
$i = 0;
foreach ($members as $m) {
$new_groups[$i]['name'] = relationship_groups_parse_name(trim($data->namingscheme), "{$m->firstname} {$m->lastname}", true);
$new_groups[$i]['userid'] = $m->userid;
$new_groups[$i]['exists'] = isset($existing_groups[$new_groups[$i]['name']]);
$new_groups[$i]['exists_external'] = isset($course_groups[$new_groups[$i]['name']]);
$i++;
}
} else {
// allocate the users - all groups equal count first
$count = $numgrps;
$i = 0;
while ($count > 0) {
$new_groups[$i]['name'] = relationship_groups_parse_name(trim($data->namingscheme), $i);
$new_groups[$i]['userid'] = 0;
$new_groups[$i]['exists'] = isset($existing_groups[$new_groups[$i]['name']]);
$new_groups[$i]['exists_external'] = isset($course_groups[$new_groups[$i]['name']]);
if (!$new_groups[$i]['exists'] && !$new_groups[$i]['exists_external']) {
$count--;
}
$i++;
}
}
if (isset($data->preview)) {
$table = new html_table();
$table->size = array('70%');
$table->align = array('left');
$table->width = '40%';
$table->data = array();
foreach ($new_groups as $group) {
if ($group['exists']) {
$text = html_writer::tag('span', $group['name'], array('style' => 'color:red; font-weight: bold;')).get_string('alreadyexists', 'local_relationship');
} else if ($group['exists_external']) {
$text = html_writer::tag('span', $group['name'], array('style' => 'color:red; font-weight: bold;')).get_string('alreadyexistsexternal', 'local_relationship');
} else {
$text = $group['name'];
}
$table->data[] = array($text);
}
$preview .= html_writer::table($table);
} else {
foreach ($new_groups as $key => $group) {
if (!$group['exists']) {
$newgroup = new stdClass();
$newgroup->relationshipid = $relationshipid;
$newgroup->name = $group['name'];
$newgroup->uniformdistribution = 0;
$newgroup->userlimit = $userlimit;
$id = relationship_add_group($newgroup);
if ($group['userid']) {
relationship_add_member($id, $relationshipcohortid, $group['userid']);
}
}
}
redirect($returnurl);
}
}
relationship_set_title($relationship, 'autogroup');
if ($error != '') {
echo $OUTPUT->notification($error);
}
/// Display the form
$editform->display();
if ($preview !== '') {
echo $OUTPUT->heading(get_string('groupspreview', 'group'));
echo $preview;
}
echo $OUTPUT->footer();