forked from vfremaux/moodle-local_vmoodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.controller.php
370 lines (348 loc) · 16.3 KB
/
view.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
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
361
362
363
364
365
366
367
368
369
370
<?php
/**
* This file catches an action and do the corresponding usecase.
* Called by 'view.php'.
*
* @package local_vmoodle
* @category local
* @author Moheissen Fabien ([email protected])
* @copyright valeisti (http://www.valeisti.fr)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
*
* @usecase doaddupdate
* @usecase add
* @usecase edit
* @usecase delete
*/
require_once($CFG->dirroot.'/local/vmoodle/filesystemlib.php');
require_once($CFG->dirroot.'/mnet/lib.php');
/**************************** add the new vmoodle host and installs it ************/
if ($action == 'doaddupdate') {
$form->id = optional_param('id', '', PARAM_INT);
$form->name = required_param('name', PARAM_TEXT);
$form->shortname = required_param('shortname', PARAM_TEXT);
$form->description = required_param('description', PARAM_CLEANHTML);
$form->vhostname = required_param('vhostname', PARAM_TEXT);
$form->vdbtype = required_param('vdbtype', PARAM_TEXT);
$form->vdbhost = required_param('vdbhost', PARAM_TEXT);
$form->vdblogin = required_param('vdblogin', PARAM_TEXT);
$form->vdbpass = required_param('vdbpass', PARAM_RAW);
$form->vdbprefix = required_param('vdbprefix', PARAM_CLEANHTML);
$form->vdbpersist = required_param('vdbpersist', PARAM_INT);
$form->vdbname = required_param('vdbname', PARAM_RAW);
$form->vdatapath = required_param('vdatapath', PARAM_RAW);
$form->mnet = required_param('mnet', PARAM_INT);
$v = 'v'.required_param('v', PARAM_INT);
$vmoodle->name = $form->name;
$vmoodle->shortname = $form->shortname;
$vmoodle->description = $form->description;
$vmoodle->vhostname = $form->vhostname;
$vmoodle->vdbtype = $form->vdbtype;
$vmoodle->vdbhost = $form->vdbhost;
$vmoodle->vdblogin = $form->vdblogin;
$vmoodle->vdbpass = $form->vdbpass;
$vmoodle->vdbname = $form->vdbname;
$vmoodle->vdbprefix = $form->vdbprefix;
$vmoodle->vdbpersist = $form->vdbpersist;
$vmoodle->vdatapath = $CFG->local_vmoodle_vdatapathbase.'/'.$form->vdatapath;
$vmoodle->mnet = $form->mnet;
if ($CFG->ostype != 'WINDOWS'){
$form->crontab = required_param('crontab', PARAM_RAW);
}
unset($errors);
echo $OUTPUT->box_start();
if (empty($form->shortname) || empty($form->vhostname)) {
$erroritem->message = get_string('musthaveshortname', 'local_vmoodle');
$erroritem->on = 'shortname,vhostname';
$errors[] = $erroritem;
} elseif ($DB->get_record('local_vmoodle', array('name' => $form->name))) {
$erroritem->message = get_string('hostexists', 'local_vmoodle');
$erroritem->on = 'name';
$errors[] = $erroritem;
} elseif ($DB->get_record('local_vmoodle', array('shortname' => $form->shortname))) {
$erroritem->message = get_string('shortnameexists', 'local_vmoodle');
$erroritem->on = 'shortname';
$errors[] = $erroritem;
} elseif ($DB->get_record('local_vmoodle', array('vhostname' => $form->vhostname))) {
$erroritem->message = get_string('hostnameexists', 'local_vmoodle');
$erroritem->on = 'shortname';
$errors[] = $erroritem;
} else {
// cannot install anything when editing data.
// installing a vmoodle needs delete/add procedure
// the update function is only provided for fixing
// wring parameter values that have not influence upon
// physical bindings.
if ($form->id == ''){
if (!file_exists($form->vdatapath)) {
if (!filesystem_create_dir($form->vdatapath, FS_RECURSIVE, $CFG->local_vmoodle_vdatapathbase)){
$erroritem->message = get_string('couldnotcreatedataroot', 'local_vmoodle'). " ".$form->vdatapath;
$erroritem->on = 'vdatapath';
$errors[] = $erroritem;
} else {
filesystem_copy_tree($CFG->dirroot.'/local/vmoodle/'.$v.'_vmoodledata', $vmoodle->vdatapath, '');
}
} else {
$done[] = 'datapath';
print_string('datapathcreated', 'local_vmoodle');
echo '<br/>';
}
// Try to create database.
$side_cnx = vmoodle_make_connection($form, false);
if (!$side_cnx){
$erroritem->message = get_string('couldnotconnecttodb', 'local_vmoodle');
$erroritem->on = 'db';
$errors[] = $erroritem;
} else {
/// drop any previous database that could be in the way
@vmoodle_drop_database($form, $side_cnx);
if ($form->vdbtype == 'mysql') {
$sql = "
CREATE DATABASE `{$form->vdbname}` CHARACTER SET 'utf8'
";
} elseif($form->vdbtype == 'posstgres') {
$sql = "
CREATE DATABASE {$form->vdbname} WITH OWNER={$form->vdblogin} ENCODING=UTF8
";
}
$res = vmoodle_execute_query($form, $sql, $side_cnx);
if (!$res) {
$erroritem->message = get_string('couldnotcreatedb', 'local_vmoodle');
$erroritem->on = 'db';
$errors[] = $erroritem;
} else {
$done[] = 'database';
print_string('databasecreated', 'local_vmoodle');
echo "<br/>";
}
// make a new connection so we can bind to database
vmoodle_close_connection($form, $side_cnx);
$side_cnx = vmoodle_make_connection($form, true);
/// prepare a filter for absolute www roots
$manifest = vmoodle_get_vmanifest($v);
$filter[$manifest->templatehost] = $form->vhostname;
/// Try to setup full datamodel loading database template
if ($res = vmoodle_load_db_template($form, $CFG->dirroot.'/local/vmoodle/'.$v.'_sql/vmoodle_master.'.$form->vdbtype.'.sql', $side_cnx, $filter)) {
$errors[] = $res;
} else {
$done[] = 'databaseloaded';
print_string('databaseloaded', 'local_vmoodle');
echo "<br/>";
}
/// TODO run customisation SQL script
$vars = get_object_vars($vmoodle);
$vars['sessioncookie'] = strtoupper($vars->shortname);
$vars['sessioncookie'] = preg_replace("/[-_]/", '', $vars['sessioncookie']); // strips out token damaging chars
if ($res = vmoodle_load_db_template($form, $CFG->dirroot.'/local/vmoodle/'.$v.'_sql/vmoodle_setup_template.'.$form->vdbtype.'.sql', $side_cnx, $vars)){
$erroritem->message = get_string('errorsetupdb', 'local_vmoodle');
$erroritem->on = 'db';
$errors[] = $erroritem;
} else {
$done[] = 'databasesetup';
print_string('databasesetup', 'local_vmoodle');
echo '<br/>';
}
/// MNET cross-registration : if mnet enabled cross register the new instance
if ($form->mnet) {
// check master host is mnet enabled
if ($CFG->mnet_dispatcher_mode == 'strict') {
$services = vmoodle_get_service_desc();
// make a moodle mnet env for the new vmoodle
$mnet_env = vmoodle_setup_mnet_environment($form, $side_cnx);
unset($mnet_env->keypair); // do not fit in mnet_host records.
// record "this" environment in new peer
echo "registering VMaster in peer<br/> ";
$remote_MNET = clone($MNET);
unset($remote_MNET->id); // will force insertion in peer's database
unset($remote_MNET->keypair);
$peer_master_env = vmoodle_register_mnet_peer($form, $remote_MNET, $side_cnx);
// register services for this in peer
vmoodle_add_services($form, $peer_master_env, $side_cnx, $services);
// register the new vmoodle in "this" known hosts
echo "registering in VMaster<br/>";
$thismoodle = vmoodle_make_this();
$this_cnx = vmoodle_make_connection($thismoodle, true);
if ($this_cnx){
$master_mnet_env = vmoodle_register_mnet_peer($thismoodle, $mnet_env, $this_cnx);
// register services for peer in master (this)
vmoodle_add_services($thismoodle, $master_mnet_env, $this_cnx, $services);
vmoodle_close_connection($thismoodle, $this_cnx);
} else {
echo "Error with local connection";
}
// register in vmoodle peers
$mnet_peers = array();
echo "Examining other peers<br/>";
$mnet_moodles = $DB->get_records('local_vmoodle', array('mnet' => 1));
if (!empty($mnet_moodles)) {
foreach ($mnet_moodles as $peervmoodle) {
// register new vmoodle in older vmoodles and get older vmoodle
// definitions to make local's.
if ($peer_cnx = vmoodle_make_connection($peervmoodle, true)) {
echo "Registering peer in {$peervmoodle->name}<br/> ";
$mnet_peer_envs[] = vmoodle_get_mnet_env($peervmoodle);
$peer_mnet_env = vmoodle_register_mnet_peer($peervmoodle, $mnet_env, $peer_cnx);
//register services for peer in other peers
vmoodle_add_services($peervmoodle, $peer_mnet_env, $peer_cnx, $services);
vmoodle_close_connection($peervmoodle, $peer_cnx);
} else {
$erroritem->message = get_string('errorbindingmnet', 'local_vmoodle', $peervmoodle->name);
$erroritem->on = 'mnet';
$errors[] = $erroritem;
}
}
}
// register all peers in new vmoodle
if (!empty($mnet_peer_envs)) {
foreach ($mnet_peer_envs as $peer_env) {
echo "Registering {$peer_env->wwwroot} in peer<br/> ";
$peer_env = vmoodle_register_mnet_peer($form, $peer_env, $side_cnx);
//register services for other peers in new peer
vmoodle_add_services($form, $peer_env, $side_cnx, $services);
}
}
$done[] = 'mnet';
print_string('mnetbound', 'local_vmoodle');
echo "<br/>";
} else {
// mnet required and master is not mnet
echo get_string('mastermnetnotice', 'local_vmoodle');
}
}
vmoodle_close_connection($form, $side_cnx);
}
/// Try to setup cron
if ($CFG->ostype != 'WINDOWS') {
$crontab = escapeshellarg($form->crontab);
$crontabsetup = "echo $crontab | crontab -";
exec($crontabsetup);
}
}
}
if (empty($errors)) {
if ($form->id) {
$vmoodle->id = $form->id;
$DB->update_record('local_vmoodle', $vmoodle);
} else {
$DB->insert_record('local_vmoodle', $vmoodle);
}
} else {
/// Errors when virtualizing.
// Rollback
if (@array_key_exists('datapath', $done)) {
filesystem_clear_dir($form->vdatapath, true, $CFG->local_vmoodle_vdatapathbase);
print_string('datatpathunbound', 'local_vmoodle');
}
if (@array_key_exists('database', $done)) {
vmoodle_drop_database($form);
print_string('datatbasedroped', 'local_vmoodle');
}
if (@array_key_exists('mnet', $done)) {
}
// bounce to the form again.
echo "bouncing";
if ($form->id) {
$action = 'edit';
} else {
$action = 'add';
}
}
echo $OUTPUT->box_end();
}
/**************************** make the add form ************/
if ($action == 'add') {
/// print errors
if (!empty($errors)) {
$errorstr = '';
foreach ($errors as $anError) {
$errorstr .= $anError->message;
}
echo '<center>';
print_simple_box($errorstr, 'center', '90%', '', 5, 'errorbox');
echo '</center>';
}
echo $OUTPUT->heading(get_string('newvmoodle', 'local_vmoodle'));
if ($CFG->local_vmoodle_automatedschema) {
$form->vhostname = $CFG->local_vmoodle_vmoodlehost;
$form->vdbtype = $CFG->local_vmoodle_vdbtype;
$form->vdbhost = $CFG->local_vmoodle_vdbhost;
$form->vdblogin = $CFG->local_vmoodle_vdblogin;
$form->vdbpass = $CFG->local_vmoodle_vdbpass;
$form->vdbprefix = $CFG->local_vmoodle_vdbprefix;
$form->vdbpersist = $CFG->local_vmoodle_vdbpersist;
$form->vdbname = $CFG->local_vmoodle_vdbbasename;
$form->vdatapath = $CFG->local_vmoodle_vdatapathbase;
}
// Try to get crontab.
if ($CFG->ostype != 'WINDOWS') {
$crontabcmd = escapeshellcmd('crontab -l');
$form->crontab = passthru($crontabcmd);
}
$usehtmleditor = can_use_html_editor();
include "add.html";
return -1;
}
/**************************** make the edit form ************/
if ($action == 'edit') {
$id = required_param('id', PARAM_INT);
if (!($form = $DB->get_record('local_vmoodle', array('id' => $id)))) {
print_error('badvmoodleid');
return (-1);
}
/// print errors
if (!empty($errors)) {
$errorstr = '';
foreach ($errors as $anError) {
$errorstr .= $anError->message;
}
echo '<center>';
echo $OUTPUT->box($errorstr);
echo '</center>';
}
echo $OUTPUT->heading(get_string('editvmoodle', 'local_vmoodle'));
/// Try to get crontab
if ($CFG->ostype != 'WINDOWS'){
$crontabcmd = escapeshellcmd('crontab -l');
$form->crontab = passthru($crontabcmd);
}
$form->editing = 1;
include "add.html";
return -1;
}
/**************************** delete a vmoodle and uninstall it ************/
if ($action == 'delete') {
$id = required_param('id', PARAM_INT);
$vmoodle = $DB->get_record('local_vmoodle', array('id' => $id));
if ($vmoodle) {
/// drop record in vmoodle table
$DB->delete_records('local_vmoodle', array('id' => $id));
/// destroy database. work silently
@vmoodle_drop_database($vmoodle);
/// unlink datapath
filesystem_clear_dir($vmoodle->vdatapath, FS_FULL_DELETE, '');
/// unbind mnet hosts
if ($vmoodle->mnet){
// unregister from me (this)
$thismoodle = vmoodle_make_this();
$this_cnx = vmoodle_make_connection($thismoodle, true);
if ($this_cnx){
vmoodle_unregister_mnet($vmoodle, $thismoodle);
vmoodle_close_connection($vmoodle, $this_cnx);
}
// unregister from all remaining peers (this)
$mnetpeers = $DB->get_records('local_vmoodle', array('mnet' => 1));
if (!empty($mnetpeers)){
foreach($mnetpeers as $peervmoodle){
$this_cnx = vmoodle_make_connection($peervmoodle, true);
if ($peer_cnx){
vmoodle_unregister_mnet($vmoodle, $peervmoodle);
vmoodle_close_connection($vmoodle, $peer_cnx);
} else {
}
}
}
}
} else {
error ("Bad VMoodle Id");
}
}