-
Notifications
You must be signed in to change notification settings - Fork 1
/
assign.controller.php
130 lines (118 loc) · 5.47 KB
/
assign.controller.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
<?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/>.
/**
* @package block_course_ascendants
* @category blocks
* @author Moodle 2.x Valery Fremaux <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* controller for course assignation
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/enrol/meta/locallib.php');
require_once($CFG->dirroot.'/enrol/meta/lib.php');
require_once($CFG->dirroot.'/blocks/course_ascendants/listlib.php');
if ($data) {
$dataarr = (array)$data;
$inputdata = preg_grep('/^c\d+/', array_keys($dataarr));
$allmetas = array();
// Prepare next order for insertion.
$lastorder = list_last_order($id);
if (is_null($lastorder)) {
$neworder = 0;
} else {
$neworder = $lastorder + 1;
}
// We know now we got them all (radio buttons).
if (!empty($inputdata)) {
foreach ($inputdata as $cid) {
$metaid = str_replace('c', '', $cid);
$params = array('enrol' => 'meta', 'customint1' => $data->course, 'courseid' => $metaid);
if ($enrol = $DB->get_record('enrol', $params)) {
$enrol->status = !$dataarr[$cid];
$DB->update_record('enrol', $enrol);
if (!$dataarr[$cid]) {
// Not any more an attached module.
if ($DB->record_exists('block_course_ascendants', array('blockid' => $id, 'courseid' => $metaid))) {
list_remove($id, $metaid);
}
} else {
$localrec = new StdClass();
$localrec->blockid = $id;
$localrec->courseid = $metaid;
$localrec->sortorder = $neworder;
if (!$DB->record_exists('block_course_ascendants', array('blockid' => $id, 'courseid' => $metaid))) {
$DB->insert_record('block_course_ascendants', $localrec);
$neworder++;
}
}
} else {
// If must be attached, make a new meta enrol record and add it to the remote metacourse.
if ($dataarr[$cid] == 1) {
$enrol = new enrol_meta_plugin();
$metacourse = $DB->get_record('course', array('id' => $metaid));
$enrol->add_instance($metacourse, array('customint1' => $data->course));
$localrec = new StdClass();
$localrec->blockid = $id;
$localrec->courseid = $metaid;
$localrec->sortorder = $neworder;
if (!$DB->record_exists('block_course_ascendants', array('blockid' => $id, 'courseid' => $metaid))) {
$DB->insert_record('block_course_ascendants', $localrec);
$neworder++;
}
}
}
if ($dataarr[$cid] == 1) {
// For further group pushing.
$allmetas[] = $metaid;
}
// Sync all users in open metacourses.
enrol_meta_sync($metaid);
}
}
// Now we sync our course group into opened metacourses.
// If told to, push the course group whereever it is missing, based on groupname.
if (@$data->pushnewgroups) {
foreach ($allmetas as $m) {
if (!$DB->record_exists('groups', array('courseid' => $m, 'name' => $coursegroup->name))) {
$metagroup->courseid = $m;
$metagroup->name = $groupname;
$metagroup->timecreated = time();
$metagroup->modified = 0;
$metagroup->id = $DB->insert_record('groups', $metagroup);
}
// Now resync group anyway.
if ($members = groups_get_members($coursegroup->id)) {
$context = context_course::instance($m);
foreach ($members as $u) {
// We need check if candidate usr to transfer has real role (was synced bymetacourse).
$select = " contextid = ? AND userid = ? AND hidden = 0 ";
if ($DB->get_records_select('role_assignments', $select, array($context->id, $u->id))) {
// Just create if not registered there.
$params = array('groupid' => $metagroup->id, 'userid' => $u->id);
if (!$DB->record_exists('groups_members', $params)) {
$groupmember = new StdClass;
$groupmember->groupid = $metagroup->id;
$groupmember->userid = $u->id;
$groupmember->timeadded = time();
$DB->insert_record('groups_members', $groupmember);
}
}
}
}
}
}
}