forked from chamilo/chamilo-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chamilo.module
545 lines (502 loc) · 19.9 KB
/
chamilo.module
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
/**
* @file
* Integration with Chamilo
*/
// Base URL
define('CHAMILO_BASE_URL', '/main/webservices');
// Base API KEY URL
define('CHAMILO_API_KEY_URL', '/main/auth/profile.php');
// Default amount of items(photos) per page.
define('CHAMILO_DEFAULT_IPP', 10);
// signature types
define('CHAMILO_GLOBAL_SIGNATURE', 1);
define('CHAMILO_PERUSER_SIGNATURE', 2);
/**
* Implementation of hook_init().
*/
function chamilo_init() {
global $user;
// Implementation of Master SSO server
if ($user->uid && isset($_GET['sso_referer'])) {
chamilo_sso_auth($user);
}
}
function chamilo_sso_protocol() {
$protocols = array('http://', 'https://');
return $protocols[variable_get('chamilo_sso_protocol', 0)];
}
/**
* Given a drupal account, redirect to Vanity server including login info.
*/
function chamilo_sso_auth($account) {
global $base_path;
$master_auth_uri = filter_xss($_SERVER['HTTP_HOST']) . $base_path . '?q=user';
// Master Cookie
$sso = array(
'username' => $account->name,
'secret' => sha1($account->pass),
'master_domain' => filter_xss($_SERVER['HTTP_HOST']),
'master_auth_uri' => $master_auth_uri,
'lifetime' => time() + 3600,
'target' => filter_xss($_GET['sso_target']),
);
$cookie = base64_encode(serialize($sso));
// Redirect to Vanity Server
$url = chamilo_sso_protocol() . $master_auth_uri;
$params = 'sso_referer='. urlencode($url) .'&sso_cookie='. urlencode($cookie);
header('Location: '. filter_xss($_GET['sso_referer']) .'?'. $params);
?><html>
<head><title><?php print t('Redirection');?></title>
</head><body><?php print t('Please wait...');?></body></html><?php
exit;
}
function chamilo_soap_call() {
// Prepare params
$params = func_get_args();
$service = array_shift($params);
$action = array_shift($params);
ini_set('soap.wsdl_cache_enabled', 0);
// Init SOAP client
$service_path = chamilo_get_service_path($service);
if ($service_path) {
$client = new SoapClient($service_path);
$chamilo_user = variable_get('chamilo_user', '');
$signature = chamilo_get_signature();
// Make call and its return result
return call_user_method_array($action, $client, array_merge(array($chamilo_user, $signature), $params));
} else {
return FALSE;
}
}
/**
* Implementation of hook_user().
*/
function chamilo_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'login':
if ($account->uid && isset($_GET['sso_referer'])) {
chamilo_sso_auth($account);
}
break;
case 'load':
static $chamilo_accounts;
if (!isset($chamilo_accounts)) {
$chamilo_accounts = variable_get('chamilo_accounts', array());
}
if (!isset($account->chamilo_user)) {
$account->chamilo_user = in_array($account->uid, $chamilo_accounts);
}
chamilo_load_user_data($account);
break;
case 'form':
$admin_access = user_access('administer chamilo');
$connect_access = user_access('connect with own Chamilo account');
$sync_access = user_access('sync users with Chamilo accounts');
if ($category == 'account' && ($connect_access || $sync_access || $admin_access)) {
$form['chamilo_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Chamilo settings'),
'#collapsible' => TRUE,
'#weight' => 1,
'#tree' => TRUE,
);
if ($sync_access || $admin_access) {
$form['chamilo_settings']['sync_account'] = array(
'#type' => 'checkbox',
'#title' => t('Sync with a Chamilo account.'),
'#default_value' => in_array($account->uid, variable_get('chamilo_accounts', array())),
'#description' => t('If enabled, system will create or override a Chamilo account with same username, email and password.')
);
}
if ($connect_access || $admin_access) {
$form['chamilo_settings']['chamilo_user'] = array(
'#type' => 'textfield',
'#title' => t('Chamilo username'),
'#default_value' => $account->chamilo_settings['user'],
'#description' => t('Chamilo username.'),
);
$api_key_url = chamilo_sso_protocol() . variable_get('chamilo_server', '') . CHAMILO_API_KEY_URL;
$form['chamilo_settings']['chamilo_apikey'] = array(
'#type' => 'textfield',
'#title' => t('Chamilo API key'),
'#default_value' => $account->chamilo_settings['apikey'],
'#description' => t('Chamilo API key. Find your API key in your Chamilo Profile by clicking <a href="@url">here</a>.',array('@url' => $api_key_url)),
);
$form['chamilo_settings']['chamilo_course_visibility'] = array(
'#type' => 'checkboxes',
'#title' => t('Trainings Visibilities'),
'#description' => t('If this site displays a block with a list of your courses, will be filtered by criterias above.'),
'#default_value' => !empty($account->chamilo_settings['course_visibility'])? $account->chamilo_settings['course_visibility']: array(),
'#options' => chamilo_course_visibility(),
);
$form['chamilo_settings']['agenda_time_frame'] = array(
'#type' => 'radios',
'#title' => t('Agenda time frame'),
'#default_value' => $account->chamilo_settings['agenda_time_frame'],
'#options' => chamilo_agenda_time_frame(TRUE),
);
}
return $form;
}
break;
case 'update':
$admin_access = user_access('administer chamilo');
$connect_access = user_access('connect with own Chamilo account');
$sync_access = user_access('sync users with Chamilo accounts');
if ($category == 'account') {
$users = variable_get('chamilo_accounts', array());
// Sync account
if ($edit['chamilo_settings']['sync_account'] && ($sync_access || $admin_access)) {
// Check if user has chamilo account
$chamilo_user_courses = chamilo_soap_call('user_info', 'DokeosWSCourseListOfUser');
// Deterimine wether to Create or Override account at chamilo
if (is_array($chamilo_user_courses)) {
$action = 'WSCreateUser';
}
else {
$action = 'WSEditUser';
}
// Make call
$user = chamilo_soap_call('registration', $action, array(
'loginname' => $edit['name'], 'password' => ($edit['pass'] ? $edit['pass'] : $account->pass),
'password_crypted' => 1, 'email' => $edit['mail'],
));
// Save
if (!isset($users[$account->uid])) {
$users[$account->uid] = $account->uid;
variable_set('chamilo_accounts', $users);
}
if ($connect_access || $admin_access) {
chamilo_save_user_data($edit, $account, $category);
}
drupal_set_message('User succesfully syncronized with Chamilo.');
}
// Unlink account
else {
// Save
if (isset($users[$account->uid])) {
unset($users[$account->uid]);
variable_set('chamilo_accounts', $users);
}
}
/* if (!isset($edit['chamilo_settings']['sync_account']) && ($sync_access || $admin_access)) {
$users = variable_get('chamilo_accounts', array());
// Sync account
if ($edit['chamilo_settings']['sync_account']) {
// Check as chamilo account
$chamilo_account = chamilo_soap_call('user_manager', 'DokeosWSUserInfoFromUsername', array('name' => $edit['name']));
// Deterimine wether to Create or Override account at chamilo
if (isset($chamilo_account->firstName) ? $chamilo_account->username == '' : FALSE) {
$action = 'WSCreateUser';
}
else {
$action = 'DokeosWSUpdateUser';
}
// Make call
$user = chamilo_soap_call('user_manager', $action, array(
'loginname' => $edit['name'], 'password' => ($edit['pass'] ? $edit['pass'] : $account->pass),
'password_crypted' => 1, 'email' => $edit['mail'],
));
// Save
if (!isset($users[$account->uid])) {
$users[$account->uid] = $account->uid;
variable_set('chamilo_accounts', $users);
}
drupal_set_message('User succesfully syncronized with Chamilo.');
}
// Unlink account
else {
// Save
if (isset($users[$account->uid])) {
unset($users[$account->uid]);
variable_set('chamilo_accounts', $users);
}
}
}
if ($connect_access || $admin_access) {
chamilo_save_user_data($edit, $account, $category);
} */
}
break;
}
}
function chamilo_load_user_data(&$account) {
$result = db_query('SELECT duser, apikey, course_visibility, agenda_time_frame FROM {chamilo_user} WHERE user_id = %d', $account->uid);
while ($user_data = db_fetch_object($result)) {
$account->chamilo_settings = array(
'user' => $user_data->duser,
'apikey' => $user_data->apikey,
'course_visibility' => unserialize($user_data->course_visibility),
'agenda_time_frame' => $user_data->agenda_time_frame,
);
}
}
function chamilo_save_user_data(&$edit, &$account, $category, $register = FALSE) {
$data = &$edit['chamilo_settings'];
// Pre-process courses visibility
$data['chamilo_course_visibility'] = preg_grep('/^0$/', $data['chamilo_course_visibility'], PREG_GREP_INVERT);
$data['chamilo_course_visibility'] = count($data['chamilo_course_visibility']) ? serialize($data['chamilo_course_visibility']) : NULL;
// Look for data
$save_data = FALSE;
$fields = array(NULL, 'chamilo_user', 'chamilo_apikey', 'chamilo_course_visibility', 'agenda_time_frame');
while($field = next($fields)) {
if (!empty($data[$field]) && $data[$field]) {
$save_data = TRUE;
break;
}
}
// Delete old user data
db_query("DELETE FROM {chamilo_user} WHERE user_id = %d", $account->uid);
if ($save_data) {
// Save data
db_query("INSERT INTO {chamilo_user} (user_id, duser, apikey, course_visibility, agenda_time_frame) VALUES (%d, '%s', '%s', '%s', %d)", $account->uid, $data['chamilo_user'], $data['chamilo_apikey'], $data['chamilo_course_visibility'], $data['agenda_time_frame']);
}
}
/**
* Implementation of hook_menu().
*/
function chamilo_menu() {
$items['admin/settings/chamilo'] = array(
'title' => 'Chamilo',
'description' => 'Configure integration settings with Chamilo.',
'page callback' => 'chamilo_admin_settings',
'access callback' => 'user_access',
'access arguments' => array('administer chamilo'),
'file' => 'chamilo.admin.inc',
);
return $items;
}
function chamilo_get_signature($type = CHAMILO_GLOBAL_SIGNATURE, $method = 1) {
global $user;
switch ($type) {
case CHAMILO_PERUSER_SIGNATURE:
chamilo_load_user_data($user);
return sha1($user->chamilo_settings['user'] . $user->chamilo_settings['apikey']);
case CHAMILO_GLOBAL_SIGNATURE:
$chamilo_user = variable_get('chamilo_user', '');
$chamilo_apikey = variable_get('chamilo_appkey', '');
if ($method == 2) {
$chamilo_server = variable_get('chamilo_server', '');
$path = chamilo_sso_protocol(). $chamilo_server . CHAMILO_BASE_URL .'/testip.php';
//this requires having the allow_url_fopen = on, but so does
// the webservice anyway
$list = file($path);
$ip = $list[0];
return sha1( $ip . $chamilo_apikey);
} else {
return sha1($chamilo_user . $chamilo_apikey);
}
default:
return '';
}
}
/**
* Implementation of hook_block().
*
* Generates the administrator-defined blocks for display.
*/
function chamilo_block($op = 'list', $delta = 0, $edit = array()) {
global $user;
switch ($op) {
case 'list':
$blocks = array();
$blocks[0]['info'] = t('Chamilo trainings'); // global configuration
$blocks[1]['info'] = t('My Chamilo agenda'); // per user
$blocks[2]['info'] = t('My Chamilo trainings'); // per user
return $blocks;
case 'view':
if (variable_get('chamilo_server', '') == '') {
drupal_set_message(t("Chamilo module yet not configured, can't display blocks."));
return;
}
switch ($delta) {
case 0:
$signature = chamilo_get_signature(CHAMILO_GLOBAL_SIGNATURE,2);
$service = chamilo_get_service_path('',2);
$client = new SoapClient($service);
$data['subject'] = t('Chamilo trainings');
$chamilo_courses_block_items = variable_get('chamilo_courses_block_items', array('public'));
foreach ($chamilo_courses_block_items as $key => $item) {
if ($item === 0) {
unset($chamilo_courses_block_items[$key]);
}
}
$visibilities = implode(',', $chamilo_courses_block_items);
// updated to Chamilo 1.8.8 service name - breaks 1.8.6 and 1.8.7 integration
$courses = $client->{'WSCourse.ListCourses'}($signature, 'chamilo_course_id',$visibilities);
$data['content'] = theme_chamilo_course_list($courses);
// -----------
/*
$signature = chamilo_get_signature();
$chamilo_user = variable_get('chamilo_user', '');
$service = chamilo_get_service_path('courses_list');
$client = new SoapClient($service);
//exit('<pre>'.print_r($service,TRUE));
$data['subject'] = t('Chamilo trainings');
$chamilo_courses_block_items = variable_get('chamilo_courses_block_items', array('public'));
foreach ($chamilo_courses_block_items as $key => $item) {
if ($item === 0) {
unset($chamilo_courses_block_items[$key]);
}
}
$visibilities = implode(',', $chamilo_courses_block_items);
// updated to Chamilo 1.8.8 service name - breaks 1.8.6 and 1.8.7 integration
$courses = $client->WSCourseList($chamilo_user, $signature, $visibilities);
$data['content'] = theme_chamilo_course_list($courses);
*/ break;
case 1:
chamilo_load_user_data($user);
$signature = chamilo_get_signature(CHAMILO_PERUSER_SIGNATURE);
$service = chamilo_get_service_path('user_info');
$data['subject'] = t('My Chamilo agenda');
$agenda_time_frame = !empty($user->chamilo_settings['agenda_time_frame'])? $user->chamilo_settings['agenda_time_frame']: 30;
$datestart = (int) date('Ymd');
$dateend = (int) date('Ymd', strtotime("+$agenda_time_frame days"));
$client = new SoapClient($service);
$agenda = $client->DokeosWSEventsList($user->chamilo_settings['user'], $signature, $datestart, $dateend);
$data['content'] = theme_chamilo_user_agenda_list($agenda);
break;
case 2:
chamilo_load_user_data($user);
$signature = chamilo_get_signature(CHAMILO_PERUSER_SIGNATURE);
$service = chamilo_get_service_path('user_info');
$data['subject'] = t('My Chamilo trainings');
$client = new SoapClient($service);
$agenda = $client->DokeosWSCourseListOfUser($user->chamilo_settings['user'], $signature);
$data['content'] = theme_chamilo_course_list($agenda);
break;
}
return $data;
break;
case 'configure':
switch ($delta) {
case 0;
$form['items'] = array(
'#type' => 'checkboxes',
'#title' => t('Trainings Visibilities'),
'#default_value' => variable_get('chamilo_courses_block_items', array()),
'#options' => chamilo_course_visibility(),
);
return $form;
}
break;
case 'save':
switch ($delta) {
case 0;
variable_set('chamilo_courses_block_items', $edit['items']);
break;
}
}
}
/**
* Implementation of hook_theme().
*/
function chamilo_theme($existing, $type, $theme, $path) {
return array(
'chamilo_course_list' => array(
'arguments' => array('courses' => NULL),
),
'chamilo_user_agenda_list' => array(
'arguments' => array('agenda_items' => NULL),
),
'chamilo_course_list' => array(
'arguments' => array('courses' => NULL),
),
);
}
/**
* Implementation of hook_theme (or should be)
* @param Array
* A list of course details as returned by chamilo_curl_exec()
*/
function theme_chamilo_course_list($courses) {
if (!is_array($courses)) return;
if (count($courses)) {
drupal_add_css(drupal_get_path('module', 'chamilo') .'/chamilo.css', 'module', 'all', FALSE);
$chamilo_server = variable_get('chamilo_server', '');
$path = chamilo_sso_protocol(). $chamilo_server . '/main/course_home/course_home.php?cidReq=';
ob_start();
?><ul><?php
foreach ($courses as $course) {
?><li class="chamilo-course">
<span class="chamilo-course-title"><a href="<?php print $path.$course->code ?>"><?php print $course->title ?></a></span>
<span class="chamilo-course-info"><?php print $course->language .' - '. $course->teacher?></span>
</li><?php
}
?></ul><?php
$output = ob_get_contents(); ob_end_clean();
return $output;
}
}
function theme_chamilo_user_agenda_list($agenda_items) {
if (!is_array($agenda_items)) return;
$path = drupal_get_path('module', 'chamilo');
drupal_add_css($path .'/chamilo.css', 'module', 'all', FALSE);
$output = '';
$agenda = array();
foreach ($agenda_items as $item) {
$html = '<span class="chamilo-event-title">'. l($item->title, $item->link) .'</span>';
$html .= '<span class="chamilo-event-info">'. $item->coursetitle .'</span>';
$html .= '<span class="chamilo-event-info">'. date('M j, Y, g:i a', strtotime($item->datestart)) .'</span>';
$html .= '<span class="chamilo-event-info">'. date('M j, Y, g:i a', strtotime($item->dateend)) .'</span>';
$agenda[] = $html;
}
$output .= theme('item_list', $agenda);
return $output;
}
/**
* Implementation of hook_perm().
*/
function chamilo_perm() {
return array('administer chamilo', 'connect with own Chamilo account'/*, 'sync users with Chamilo accounts'*/);
}
/**
* Return supported course visibility options
*/
function chamilo_course_visibility() {
return array(
'public' => t('public'),
'private' => t('private'),
'public-registered' => t('public registered'),
'closed' => t('closed')
);
}
function chamilo_agenda_time_frame($optional = FALSE) {
static $time_frames;
if (!is_array($time_frames)) {
$time_keys = array(30 => '', 15 => '', 7 => '');
// Describe time frames
$time_frames = $optional ? array('' => t('Disabled')) : array();
foreach ($time_keys as $f => $d) {
$time_frames[$f] = t('between now and within next !n days', array('!n' => $f));
}
}
return $time_frames;
}
/**
* Given a $service name, return corresponding location and uri into an array.
*/
function chamilo_get_service_path($service, $algo = 1) {
static $services;
$chamilo_server = variable_get('chamilo_server', '');
if (!isset($services)) {
$services = array('courses_list', 'user_info', 'registration');
}
if (empty($service) && $algo == 2) {
return chamilo_sso_protocol(). $chamilo_server . CHAMILO_BASE_URL .'/soap.php?wsdl';
}
if (in_array($service, $services)) {
return chamilo_sso_protocol(). $chamilo_server . CHAMILO_BASE_URL .'/'. $service .'.soap.php?wsdl';
}
}
/**
* Add some help on admin config page
*/
function chamilo_help($path, $arg) {
switch ($path) {
//Admin page help
case 'admin/settings/chamilo';
return '<p>' . t('Setting the information in this form will allow your Drupal site to connect to Chamilo and query some information about the courses available in the virtual campus. Please read the examples below the box, they will certainly help you. Once you have configured this form, you will need to add some of the predefined Chamilo blocks to some of your pages to see the results.') . '</p>';
}
}