-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage_collab_db.php
360 lines (295 loc) · 11.1 KB
/
manage_collab_db.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
// This file is part of the Moodle block "EJSApp Collab Session"
//
// EJSApp Collab Session 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.
//
// EJSApp Collab Session 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.
//
// The GNU General Public License is available on <http://www.gnu.org/licenses/>
//
// EJSApp Collab Session has been developed by:
// - Luis de la Torre (1): ldelatorre@dia.uned.es
// - Ruben Heradio (1): rheradio@issi.uned.es
// - Carlos Jara (2): carlos.jara@ua.es
//
// (1): Computer Science and Automatic Control, Spanish Open University (UNED),
// Madrid, Spain
// (2): Physics, Systems Engineering and Signal Theory Department, University
// of Alicante, Spain
/**
* File that centralizes all DB management
*
* @package block
* @subpackage ejsapp_collab_session
* @copyright 2012 Luis de la Torre, Ruben Heradio and Carlos Jara
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* returns true if the user is participating in at least one collaborative session
*/
function is_the_user_participating_in_any_session(){
global $USER, $DB;
$invited = $DB->record_exists('ejsapp_collab_acceptances', array('accepted_user'=>$USER->id));
$master = $DB->record_exists('ejsapp_collab_sessions', array('master_user'=>$USER->id));
return ($invited || $master);
} //is_the_user_participating_in_any_session
/**
* returns true if the user has been invited to in at least one collaborative session
*/
function has_the_user_been_invited_to_any_session(){
global $USER, $DB;
return $DB->record_exists('ejsapp_collab_invitations', array('invited_user'=>$USER->id));
} //has_the_user_been_invited_to_any_session
/**
* creates a new collaborative session
*
* @param int $localport connection port of the master user
* @param int $ejsapp id of the EJS simulation to be shared
* @param int $master_user id of the master user
* @param int $ip connection ip of the session director
* @param int $enlargeport connection port of the ENLARGE server
* @param int $using_enlarge whether ENLARGE is used or not
* @param int $course id of the course that includes the collaborative EJS simulation
*/
function insert_collaborative_session($localport, $ejsapp, $master_user, $ip, $enlargeport, $using_enlarge, $course){
global $DB;
// if the session exists do nothing
if (is_the_user_participating_in_any_session()) {
return;
}
$collab_session = new stdClass();
$collab_session->ip = $ip;
$collab_session->localport = $localport;
$collab_session->using_enlarge = $using_enlarge;
$collab_session->enlargeport = $enlargeport;
$collab_session->ejsapp = $ejsapp;
$collab_session->master_user = $master_user;
$collab_session->course = $course;
$DB->insert_record('ejsapp_collab_sessions', $collab_session);
} //insert_collaborative_session
/**
* includes a user into a new collaborative session
*
* @param int $collaborative_session id of the collaborative session
*/
function insert_collaborative_user($collaborative_session){
global $USER, $DB;
// if the user has already been inserted do nothing
if (is_the_user_participating_in_any_session()) {
return;
}
$collab_acceptances = new stdClass();
$collab_acceptances->accepted_user = $USER->id;
$collab_acceptances->collaborative_session = $collaborative_session;
$DB->insert_record('ejsapp_collab_acceptances', $collab_acceptances);
} //insert_collaborative_user
/**
* returns the id of the collaborative session directed by a given master user
*
* @param int $master_user id of the master user
* @return int record id
*/
function get_collaborative_session_id($master_user){
global $DB;
$record = $DB->get_record('ejsapp_collab_sessions', array('master_user'=>$master_user));
if (isset($record->id)) return $record->id;
else return null;
} //get_collaborative_session_id
/**
* creates a collaborative invitation
*
* @param int $invited_user id of the invited user
* @param int $collaborative_session id of the collaborative session
*/
function insert_collaborative_invitation($invited_user, $collaborative_session){
global $DB;
// <if the invitation exists do noting>
$record = $DB->get_record('ejsapp_collab_invitations', array('invited_user'=>$invited_user,'collaborative_session'=>$collaborative_session));
if (isset($record->invited_user)) {
return;
}
// <\if the invitation exists do noting>
$collab_invitations = new stdClass();
$collab_invitations->invited_user = $invited_user;
$collab_invitations->collaborative_session = $collaborative_session;
$DB->insert_record('ejsapp_collab_invitations',$collab_invitations);
} //insert_collaborative_invitation
/**
* drops out a user from the collaborative session
*/
function delete_me_as_collaborative_user(){
global $DB, $USER;
$DB->delete_records('ejsapp_collab_acceptances', array('accepted_user'=>$USER->id));
} //delete_me_as_collaborative_user
/**
* finishes a collaborative session
*
* @param int $master_user id of the master user
*/
function delete_collaborative_session($master_user){
global $DB, $USER;
$session = get_collaborative_session_id($master_user);
if ($session) {
$DB->delete_records('ejsapp_collab_sessions', array('id'=>$session));
$DB->delete_records('ejsapp_collab_invitations', array('collaborative_session'=>$session));
$DB->delete_records('ejsapp_collab_acceptances', array('collaborative_session'=>$session));
$DB->delete_records('block_remlab_manager_sb_keys', array('user'=>$USER->username));
}
} //delete_collaborative_session
/**
* returns the id of the EJS simulation shared in a collaborative session
*
* @param int $master_user id of the master user
* @return int ejsappid
*/
function get_ejsapp($master_user){
global $DB;
$ejsapp = null;
$record = $DB->get_record('ejsapp_collab_sessions', array('master_user'=>$master_user));
if (isset($record)) $ejsapp = $record->ejsapp;
return $ejsapp;
} //get_ejsapp
/**
* returns the name of the EJS simulation shared in a collaborative session
*
* @param int $master_user id of the master user
* @return string ejsapp name
*/
function get_ejsapp_name($master_user){
global $DB;
$ejsapp_name = null;
$record = $DB->get_record('ejsapp_collab_sessions', array('master_user'=>$master_user));
if (isset($record->master_user)) {
$record2 = $DB->get_record('ejsapp', array('id'=>$record->ejsapp));
if (isset($record2->id)) $ejsapp_name = $record2->name;
}
return $ejsapp_name;
} //get_ejsapp_name
/**
* returns an object that fully describes the simulation shared in a collaborative session
*
* @param int $session id of the collaborative session
* @return stdClass ejsapp
*/
function get_ejsapp_object($session){
global $DB;
$ejsapp = null;
$record = $DB->get_record('ejsapp_collab_sessions', array('id'=>$session));
if (isset($record->ejsapp)) {
$ejsapp = $DB->get_record('ejsapp', array('id'=>$record->ejsapp));
}
return $ejsapp;
} //get_ejsapp_object
/**
* returns an object that fully describes the master user that leads a collaborative session
*
* @param int $session id of the collaborative session
* @return stdClass ejsapp_collab_acceptances
*/
function get_master_user_object($session){
global $DB;
$record = $DB->get_record('ejsapp_collab_sessions', array('id'=>$session));
$record2 = new stdClass();
if(isset($record->master_user)) {
$record2 = $DB->get_record('ejsapp_collab_acceptances', array('id'=>$record->master_user));
}
return $record2;
} //get_master_user_object
/**
* returns a list of all collaborative sessions where a user has been invited
*/
function get_sessions_where_i_am_invited(){
global $DB, $USER;
$records = $DB->get_records('ejsapp_collab_invitations', array('invited_user'=>$USER->id));
$sessions = array();
foreach ($records as $record) {
$session = $record->collaborative_session;
$new_record = $DB->get_record('ejsapp_collab_sessions', array('id'=>$session));
$sessions[$new_record->master_user] = $new_record->ejsapp;
}
return $sessions;
} //get_sessions_where_i_am_invited
/**
* returns the name of a user
*
* @param int $user id of user
* @return string user name
* @throws
*/
function get_user_name($user){
global $DB;
$record = $DB->get_record('user', array('id'=>$user));
return "{$record->firstname} {$record->lastname}";
} //get_user_name
/**
* drops out a non master user from the collaborative session
*/
function delete_non_master_user_from_collaborative_users(){
global $DB, $USER;
$DB->delete_records('ejsapp_collab_acceptances', array('id'=>$USER->id));
} //delete_non_master_user_from_collaborative_users
/**
* selects records of collaborative labs which are usable (either virtual labs or available remote labs)
*
* @param array $collaborative_lab_records database records of collaborative labs
* @return array collaborative_lab_records
* @throws
*/
function get_available_collab_lab_records($collaborative_lab_records) {
global $DB;
require_once('../../mod/ejsapp/locallib.php');
$available_collaborative_lab_records = array();
foreach ($collaborative_lab_records as $collaborative_lab_record) {
if ($collaborative_lab_record->is_rem_lab == 0) $available_collaborative_lab_records[] = $collaborative_lab_record;
else {
$course = $DB->get_record('course', array('id' => $collaborative_lab_record->course), '*', MUST_EXIST);
$remote_lab_access = remote_lab_access_info($collaborative_lab_record, $course);
$remlab_conf = $remote_lab_access->remlab_conf;
if ($remote_lab_access->allow_free_access && $remote_lab_access->operative) {
if ($remlab_conf->usestate == 'available') {
$available_collaborative_lab_records[] = $collaborative_lab_record;
}
} else {
if ($remote_lab_access->operative) { // TODO: more checks needed
$available_collaborative_lab_records[] = $collaborative_lab_record;
}
}
}
}
return $available_collaborative_lab_records;
} //get_available_collab_lab_records
/**
* returns the connection port that the session director is using in the collaborative session
*
* @param int $session id of the collaborative session
* @return int port
*/
function get_port($session){
global $DB;
$record = $DB->get_record('ejsapp_collab_sessions', array('id'=>$session));
return $record->port;
} //get_port
/**
* returns the name of a course
*
* @param int $course_id id of the course
*/
function get_course_name($course_id){
global $DB;
$record = $DB->get_record('course', array('id'=>$course_id));
return $record->fullname;
} //get_course_name
/**
* returns true if the caller is the master user of a collaborative session
*/
function am_i_master_user(){
global $DB, $USER;
$record = $DB->get_record('ejsapp_collab_sessions', array('master_user'=>$USER->id));
return (isset($record->master_user));
} //am_i_master_user