-
Notifications
You must be signed in to change notification settings - Fork 5
/
externallib.php
307 lines (272 loc) · 8.86 KB
/
externallib.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?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/>.
/**
* Library of interface functions and constants for module groupformation
*
* All the core Moodle functions, neeeded to allow the module to work
* integrated in Moodle should be placed here.
* All the newmodule specific functions, needed to implement all the module
* logic, should go to locallib.php. This will help to save some memory when
* Moodle is performing actions across all modules.
*
* @package mod_groupformation
* @author Eduard Gallwas, Johannes Konert, Rene Roepke, Nora Wester, Ahmed Zukic
* @copyright 2015 MoodlePeers
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die ();
require_once($CFG->dirroot . '/mod/groupformation/classes/moodle_interface/storage_manager.php');
require_once($CFG->dirroot . '/mod/groupformation/classes/moodle_interface/groups_manager.php');
/**
* Returns activity state
*
* @param int $groupformationid ID of the activity
* @return mixed
* @throws dml_exception
*/
function groupformation_get_activity_state($groupformationid) {
$store = new mod_groupformation_storage_manager($groupformationid);
return $store->statemachine->get_state();
}
/**
* Returns user state
*
* @param int $groupformationid ID of the activity
* @param int $userid ID of the user
* @return mixed
* @throws dml_exception
*/
function groupformation_get_user_state($groupformationid, $userid) {
$store = new mod_groupformation_storage_manager($groupformationid);
return $store->userstatemachine->get_state($userid);
}
/**
* Returns all instances of groupformation activities in a given course
*
* @param int $courseid The id of the current course
* @return mixed
* @throws dml_exception
*/
function groupformation_get_instances($courseid) {
global $DB;
$instances = $DB->get_records('groupformation', array('course' => $courseid));
return $instances;
}
/**
* Returns all instances of groupformation activities in a given course
*
* @param int $groupformationid ID of the activity
* @return mixed
* @throws dml_exception
*/
function groupformation_get_instance_by_id($groupformationid) {
global $DB;
$instance = $DB->get_record('groupformation', array('id' => $groupformationid));
return $instance;
}
/**
* Returns an array including all group members names.
*
* @param int $groupformationid ID of the activity
* @param int $userid ID of user
* @return array
* @throws dml_exception
*/
function groupformation_get_group_members($groupformationid, $userid) {
$groupsmanager = new mod_groupformation_groups_manager ($groupformationid);
$groupmembers = $groupsmanager->get_group_members($userid);
$members = array();
foreach ($groupmembers as $groupmember) {
$user = get_complete_user_data('id', $groupmember);
$members[$groupmember] = fullname($user);
}
return $members;
}
/**
* Returns group name
*
* @param int $groupformationid ID of the activity
* @param int $userid ID of user
* @return mixed
* @throws dml_exception
*/
function groupformation_get_group_name($groupformationid, $userid) {
$groupsmanager = new mod_groupformation_groups_manager ($groupformationid);
return $groupsmanager->get_group_name($userid);
}
/**
* Returns course module for activity
*
* @param int $groupformationid ID of the activity
* @return mixed
* @throws dml_exception
*/
function groupformation_get_cm($groupformationid) {
global $DB;
$gfinstance = groupformation_get_instance_by_id($groupformationid);
if ($gfinstance) {
$moduleid = $DB->get_field('modules', 'id', array('name' => 'groupformation'));
return $DB->get_field('course_modules', 'id',
array('course' => $gfinstance->course, 'module' => $moduleid, 'instance' => $groupformationid));
}
return null;
}
/**
* Returns whether user has a group
*
* @param int $groupformationid ID of the activity
* @param int $userid ID of user
* @return bool
* @throws dml_exception
*/
function groupformation_has_group($groupformationid, $userid) {
$groupsmanager = new mod_groupformation_groups_manager ($groupformationid);
return $groupsmanager->has_group($userid);
}
/**
* Returns total number of answers
*
* @param int $groupformationid ID of the activity
* @return int
* @throws dml_exception
*/
function groupformation_get_number_of_questions($groupformationid) {
$store = new mod_groupformation_storage_manager($groupformationid);
return $store->get_total_number_of_answers();
}
/**
* Returns number of answers questions
*
* @param int $groupformationid ID of the activity
* @param int $userid ID of user
* @return number
* @throws dml_exception
*/
function groupformation_get_number_of_answered_questions($groupformationid, $userid) {
$usermanager = new mod_groupformation_user_manager($groupformationid);
return $usermanager->get_number_of_answers($userid);
}
/**
* Returns statistics
*
* The returned array contains four values:
* enrolled (number of enrolled students),
* processing (number of students processing the questionnaire),
* submitted (number of submitted questionnaires),
* submitted completely (number of submitted questionnaires with complete answers to all questions)
*
* @param int $groupformationid ID of the activity
* @return array
* @throws dml_exception
*/
function groupformation_get_progress_statistics($groupformationid) {
$usermanager = new mod_groupformation_user_manager($groupformationid);
return $usermanager->get_statistics();
}
/**
* Returns dates for started and terminated if set.
*
* @param int $groupformationid ID of the activity
* @return array
* @throws dml_exception
*/
function groupformation_get_dates($groupformationid) {
$store = new mod_groupformation_storage_manager($groupformationid);
return $store->get_time();
}
/**
* Return users for this activity.
*
* @param int $groupformationid ID of the activity
* @return array
* @throws dml_exception
*/
function groupformation_get_users($groupformationid) {
$store = new mod_groupformation_storage_manager($groupformationid);
return $store->get_users_for_grouping();
}
/**
* Checks whethter a groupformation exists.
*
* @param int $instance instance of groupformation
* @return bool
* @throws dml_exception
*/
function groupformation_check_instance($instance) {
global $DB;
return $DB->record_exists('groupformation', array('id' => $instance));
}
/**
* Returns the ids of all groupformations a user had visited.
*
* @param int $userid ID of user
* @return array
* @throws dml_exception
*/
function get_groupformationids_for_user($userid) {
global $DB;
return $DB->get_fieldset_select('groupformation_users', 'groupformation', 'userid =' . $userid, array('userid' => $userid));
}
/**
* Returns whether a groupformation should be tracked for a user.
*
* @param int $userid ID of user
* @param int $gfid ID of the activity
* @return mixed
* @throws dml_exception
*/
function get_gf_tracked_for_user($userid, $gfid) {
global $DB;
return $DB->get_field('groupformation_users', 'tracked', array('userid' => $userid, 'groupformation' => $gfid));
}
/**
* Sets wheter a groupformation should tracked for user.
*
* @param int $userid ID of user
* @param int $gfid ID of the activity
* @param int $tracked 0 or 1 if user is tracked
* @throws dml_exception
*/
function set_gf_tracked_for_user($userid, $gfid, $tracked) {
global $DB;
if ($tracked == 1 || $tracked == 0) {
$DB->set_field('groupformation_users', 'tracked', $tracked, array('userid' => $userid, 'groupformation' => $gfid));
}
}
/**
* Returns whether a groupformation should be tracked for the teacher.
*
* @param int $gfid ID of the activity
* @return mixed
* @throws dml_exception
*/
function get_gf_tracked_for_teacher($gfid) {
global $DB;
return $DB->get_field('groupformation', 'tracked', array('id' => $gfid));
}
/**
* Sets wheter a groupformation should tracked for the teacher.
*
* @param int $gfid ID of the activity
* @param int $tracked 0 or 1 if it is tracked
* @throws dml_exception
*/
function set_gf_tracked_for_teacher($gfid, $tracked) {
global $DB;
if ($tracked == 1 || $tracked == 0) {
$DB->set_field('groupformation', 'tracked', $tracked, array('id' => $gfid));
}
}