-
Notifications
You must be signed in to change notification settings - Fork 0
/
prestashopchamilo.php
executable file
·618 lines (541 loc) · 23.6 KB
/
prestashopchamilo.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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
<?php
/* For licensing terms, see /license.txt */
/**
* Prestashop - Chamilo 1.10.x module integration
* Sell Chamilo courses in your Prestashop
*
* @author Julio Montoya <[email protected]> Beeznest 2016
*
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once _PS_ROOT_DIR_.'/modules/prestashopchamilo/nusoap/class.nusoap_base.php';
require_once _PS_ROOT_DIR_.'/modules/prestashopchamilo/nusoap/class.soap_parser.php';
require_once _PS_ROOT_DIR_.'/modules/prestashopchamilo/nusoap/class.xmlschema.php';
require_once _PS_ROOT_DIR_.'/modules/prestashopchamilo/nusoap/class.soap_transport_http.php';
require_once _PS_ROOT_DIR_.'/modules/prestashopchamilo/nusoap/class.wsdl.php';
require_once _PS_ROOT_DIR_.'/modules/prestashopchamilo/nusoap/class.soapclient.php';
/**
* Class chamilo
*/
class PrestashopChamilo extends Module
{
public $chamilo_url;
public $chamilo_security_key;
public $encrypt_method;
public $ip;
public $sha1;
public function __construct()
{
$this->refreshChamiloValues();
// Module configuration
$this->name = 'prestashopchamilo';
$this->tab = 'others';
$this->version = "1.4.1";
$this->author = 'Chamilo';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
$this->wsdl = $this->chamilo_url.'/main/webservices/registration.soap.php?wsdl';
$this->debug = true;
parent::__construct();
$this->displayName = $this->l('Chamilo 1.10.x Module');
$this->description = $this->l('Let users buy Chamilo courses in your PS platform!');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
}
/**
* Refresh values
*/
public function refreshChamiloValues()
{
$parameters = Configuration::getMultiple(
array(
'PS_CHAMILO_URL',
'PS_CHAMILO_SECRET_KEY',
'PS_CHAMILO_ENCRYPT_METHOD',
'PS_CHAMILO_HOST_IP',
)
);
// Chamilo configuration
// Your chamilo URL
$this->chamilo_url = $parameters['PS_CHAMILO_URL'];
// Check the configuration.php file
$this->chamilo_security_key = $parameters['PS_CHAMILO_SECRET_KEY'];
$this->encrypt_method = $parameters['PS_CHAMILO_ENCRYPT_METHOD'];
$this->ip = $parameters['PS_CHAMILO_HOST_IP'];
$this->sha1 = sha1($this->ip.$this->chamilo_security_key);
}
/**
* @return bool
*/
public function install()
{
if (!parent::install() || !$this->installDB() ||
!$this->registerHook('newOrder') ||
!$this->registerHook('paymentConfirm') ||
!$this->registerHook('OrderDetail')
) {
return false;
}
return true;
}
/**
* @return bool
*/
public function installDB()
{
//Creating chamilo - prestashop tables
/*$create_table = '
CREATE TABLE '._DB_PREFIX_.'customer_chamilo (
id INT NOT NULL AUTO_INCREMENT
id_customer INT UNSIGNED NOT NULL,
id_chamilo INT UNSIGNED NOT NULL,
created_at DATETIME,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;';
Db::getInstance()->Execute($create_table);*/
// Creating a new feature
$id = Feature::addFeatureImport('CHAMILO_CODE');
// Creating configuration value
Configuration::updateValue('CHAMILO_FEATURE_ID', $id);
return true;
}
/**
* @return bool
*/
public function uninstall()
{
if (!parent::uninstall() || !$this->uninstallDB()) {
return false;
}
return true;
}
/**
* @return bool
*/
public function uninstallDB()
{
//Dropping table
//Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'customer_chamilo `;');
// Removing feature
$chamilo_feature_id = Configuration::get('CHAMILO_FEATURE_ID');
$feature = new Feature($chamilo_feature_id);
$feature->delete();
//Removing configuration
Configuration::deleteByName('CHAMILO_FEATURE_ID');
Configuration::deleteByName('PS_CHAMILO_URL');
Configuration::deleteByName('PS_CHAMILO_SECRET_KEY');
Configuration::deleteByName('PS_CHAMILO_ENCRYPT_METHOD');
Configuration::deleteByName('PS_CHAMILO_HOST_IP');
return true;
}
/**
* @return string
*/
public function displayForm()
{
$client = new nusoap_client($this->wsdl, true);
$params = array(
'secret_key' => $this->sha1,
'from' => 0,
'to' => 10
);
$courseList = $client->call(
'WSListCourses',
array('listCourseInput' => $params)
);
//$output .= 'To edit this values go to the modules/chamilo/chamilo.php file<br /><br />';
$output = '<fieldset><legend>'.$this->l('Chamilo Settings').'</legend>';
if (!empty($this->chamilo_url)) {
$output .= '<label>'.$this->l('Chamilo URL').'</label>
<div class="margin-form"><a href="'.$this->chamilo_url.'" target="_blank">'.$this->chamilo_url.'</a></div>';
$output .= '<label>'.$this->l('Chamilo WSDL').'</label><div class="margin-form"><a href="'.$this->wsdl.'" target="_blank">'.$this->wsdl.'</a></div>';
}
$chamilo_host_ip = Configuration::get('PS_CHAMILO_HOST_IP');
if (empty($chamilo_host_ip)) {
$chamilo_host_ip = $_SERVER['SERVER_ADDR'];
}
$output .='
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
<div class="margin-form">
<br class="clear"/>
<label for="chamilo_url">'.$this->l('Chamilo URL').' </label>
<input size="50px" type="text" name="chamilo_url" value="'.stripslashes(html_entity_decode(Configuration::get('PS_CHAMILO_URL'))).'" />
<br class="clear"/><br />
<label for="chamilo_secret_key">'.$this->l('Chamilo Security key').' </label>
<input size="50px" type="text" name="chamilo_secret_key" value="'.stripslashes(html_entity_decode(Configuration::get('PS_CHAMILO_SECRET_KEY'))).'" />
<br /><br class="clear"/><br />
<label for="chamilo_encrypt_method">'.$this->l('Chamilo encrypted method').' </label>
<input type="text" name="chamilo_encrypt_method" value="'.stripslashes(html_entity_decode(Configuration::get('PS_CHAMILO_ENCRYPT_METHOD'))).'" />
<i>(sha1 or md5)</i><br class="clear"/><br />
<label for="chamilo_host_ip">'.$this->l('Your public IP').' </label>
<input type="text" name="chamilo_host_ip" value="'.stripslashes(html_entity_decode($chamilo_host_ip)).'" />
</div>
<center><input type="submit" name="submitChamilo" value="'.$this->l('Save').'" class="btn btn-default" /></center>
</form>';
$output .= '</fieldset><br />';
if (!empty($courseList)) {
$output .= '<fieldset><legend>'.$this->l('Chamilo first 10 courses').'</legend>';
$output .= '<table class="table"><tr>';
$output .= '<th>'.$this->l('Title').'</th>';
$output .= '<th>'.$this->l('Code').'</th>';
$output .= '<th>'.$this->l('Language').'</th>';
$output .= '</tr>';
foreach ($courseList as $course) {
$output .= '<tr><td>'.$course['title'].'</td>';
$output .= '<td>'.$course['code'].'</td>';
$output .= '<td>'.$course['language'].'</td></tr>';
}
$output .= '</table>';
} else {
$output .= $this->l('No Chamilo courses found, you need to create a Chamilo course. If you already have a Chamilo course you should check the Chamilo settings');
}
$output .= '</fieldset>';
$soapError = $client->getError();
if (!empty($soapError)) {
$output .= $soapError;
}
$client = new nusoap_client($this->wsdl, true);
$params = array(
'secret_key' => $this->sha1,
'from' => 0,
'to' => 10
);
$sessionList = $client->call('WSListSessions', array('input' => $params) );
if (!empty($sessionList)) {
$output .= '<fieldset><legend>'.$this->l('Chamilo first 10 Sessions').'</legend>';
$output .= '<table class="table"><tr>';
$output .= '<th>'.$this->l('Title').'</th>';
$output .= '<th>'.$this->l('Code').'</th>';
$output .= '</tr>';
foreach ($sessionList as $session) {
$output .= '<tr><td>'.$session['title'].'</td>';
$output .= '<td>SESSION_'.$session['id'].'</td>';
}
$output .= '</table>';
} else {
$output .= $this->l('No Chamilo session found.');
}
return $output;
}
/**
* @return string
*/
public function getContent()
{
$output = '<h2>'.$this->displayName.'</h2>';
if (Tools::isSubmit('submitChamilo')) {
Configuration::updateValue('PS_CHAMILO_URL', htmlentities(str_replace(array("\r\n", "\n"), '', Tools::getValue('chamilo_url'))));
Configuration::updateValue('PS_CHAMILO_SECRET_KEY', htmlentities(str_replace(array("\r\n", "\n"), '', Tools::getValue('chamilo_secret_key'))));
Configuration::updateValue('PS_CHAMILO_ENCRYPT_METHOD', htmlentities(str_replace(array("\r\n", "\n"), '', Tools::getValue('chamilo_encrypt_method'))));
Configuration::updateValue('PS_CHAMILO_HOST_IP', htmlentities(str_replace(array("\r\n", "\n"), '', Tools::getValue('chamilo_host_ip'))));
$output .= '<div class="conf confirm"><img src="../img/admin/ok.gif" alt="'.$this->l('Confirmation').'" />'.$this->l('Settings updated').'</div>';
/*
$nbr = intval(Tools::getValue('nbr'));
if (!$nbr OR $nbr <= 0 OR !Validate::isInt($nbr))
$errors[] = $this->l('Invalid number of product');
else
Configuration::updateValue('HOME_FEATURED_NBR', $nbr);
if (isset($errors) AND sizeof($errors))
$output .= $this->displayError(implode(' ', $errors));
else
$output .= $this->displayConfirmation($this->l('Settings updated'));*/
}
$this->refreshChamiloValues();
return $output.$this->displayForm();
}
/**
* @param array $params
* @return mixed
*/
public function hookHome($params)
{
return $this->display(__FILE__, 'content.tpl');
}
/**
* @param array $params
* @return mixed
*/
public function hookRightColumn($params)
{
return $this->hookHome($params);
}
/**
* @param array $params
*/
public function hookOrderDetail($params)
{
}
/**
* @param $params
* @return bool
*/
public function hookPaymentConfirm($params)
{
$order = new Order($params['id_order']);
if ($this->debug) error_log('hookPaymentConfirm');
if ($this->debug) error_log(print_r($params,1));
$client = new nusoap_client($this->wsdl, true);
// Getting PS customer
$customer = new Customer($order->id_customer);
// Getting default PS parameters
$parameters = Configuration::getMultiple(array('PS_LANG_DEFAULT', 'CHAMILO_FEATURE_ID'));
$lang_id = $parameters['PS_LANG_DEFAULT'];
$chamilo_feature_id = $parameters['CHAMILO_FEATURE_ID'];
// Getting Chamilo course code in the product list
$product_list = $order->getProducts();
$courseCodeList = array();
$sessionList = array();
foreach ($product_list as $product) {
$my_product = new Product($product['product_id']);
$features = $my_product->getFeatures();
if (!empty($features)) {
foreach ($features as $feature) {
if ($feature['id_feature'] == $chamilo_feature_id) {
$feature_value = new FeatureValue($feature['id_feature_value']);
$value = $feature_value->value[$lang_id];
if (substr($value, 0, 8) == 'SESSION_') {
$sessionList[] = str_replace('SESSION_', '', $value);
} else {
$courseCodeList[] = $value;
}
break;
}
}
}
}
if (empty($courseCodeList) && empty($sessionList)) {
if ($this->debug) error_log('Course code and sessionlist are empty, nothing to create');
return true;
}
if ($this->debug) error_log(print_r($courseCodeList, 1));
if ($this->debug) error_log(print_r($sessionList, 1));
// Check if the PS customer have already an account in Chamilo
$chamilo_params = array(
'original_user_id_name' => 'prestashop_customer_id',
//required, field name about original user id
'original_user_id_value' => $customer->id,
//required, field value about original user id
'secret_key' => $this->sha1
//required, secret key ("your IP address and security key from chamilo") encrypted with sha1
);
$chamilo_user_data = $client->call('WSGetUser',array('GetUser'=>$chamilo_params));
$chamilo_user_id = null;
if (!empty($chamilo_user_data)) {
$chamilo_user_id = $chamilo_user_data['user_id'];
}
// Login generation - firstname (30 len char) + PS customer id
$parts = explode("@", $customer->email);
$username = $parts[0];
$login = substr(strtolower($username),0,30).$customer->id;
// User does not have a Chamilo account we proceed to create it
if (empty($chamilo_user_id)) {
if ($this->debug) error_log('PS Customer does not exist in Chamilo proceed the creation of the Chamilo user');
// Password generation
$password = $clean_password = self::generate_password();
switch($this->encrypt_method) {
case 'md5':
$password = md5($password);
break;
case 'sha1':
$password = sha1($password);
break;
}
// Default account validity in chamilo.
$expirationDate = date('Y-m-d H:i:s', strtotime("+3660 days"));
// Setting params
$chamilo_params =
array(
'firstname' => $customer->firstname, // required
'lastname' => $customer->lastname, // required
'status' => '5', // required, (1 => teacher, 5 => learner)
'email' => $customer->email, // optional, follow the same format ([email protected])
'loginname' => $login, // required
'password' => $password, // required, it's important to define the salt into an extra field param
'encrypt_method' => $this->encrypt_method, // required, check if the encrypt is the same than dokeos configuration
'language' => 'english', // optional
'phone' => '', // optional
'expiration_date' => $expirationDate, // optional, follow the same format
'original_user_id_name' => 'prestashop_customer_id', //required, field name about original user id
'original_user_id_value' => $customer->id, //required, field value about original user id
'secret_key' => $this->sha1 //required, secret key ("your IP address and security key from chamilo") encrypted with sha1
);
// Creating a Chamilo user, calling the webservice
$chamilo_user_id = $client->call(
'WSCreateUserPasswordCrypted',
array('createUserPasswordCrypted' => $chamilo_params)
);
if (!empty($chamilo_user_id)) {
if ($this->debug) error_log('User is subscribed');
global $cookie;
/* Email generation */
$subject = Configuration::get('PS_SHOP_NAME').' [Campus - Chamilo]';
$templateVars = array(
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
'{email}' => $customer->email,
'{login}' => $login,
'{password}' => $clean_password,
'{chamilo_url}' => $this->chamilo_url,
'{site}' => Configuration::get('PS_SHOP_NAME'),
);
/* Email sending */
if ($this->debug) error_log('Sending message');
$mailResult = Mail::Send(
(int)($cookie->id_lang),
'chamilo',
$subject,
$templateVars,
$customer->email,
null,
Configuration::get('PS_SHOP_EMAIL'),
Configuration::get('PS_SHOP_NAME'),
null,
null,
dirname(__FILE__).'/mails/'
);
if ($this->debug) {
error_log(print_r($mailResult, 1));
}
} else {
if ($this->debug) {
error_log($client->getError());
}
}
if ($this->debug) error_log('WSCreateUserPasswordCrypted was called this is the result: '.print_r($chamilo_user_id, 1));
} else {
if ($this->debug) error_log('User have already a chamilo account associated with the current PS customer. Chamilo user_id = '.$chamilo_user_id);
if (!empty($chamilo_user_id)) {
if ($this->debug) error_log('User is subscribed');
global $cookie;
/* Email generation */
$subject = Configuration::get('PS_SHOP_NAME').' [Campus - Chamilo]';
$templateVars = array(
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
'{chamilo_url}' => $this->chamilo_url,
'{site}' => Configuration::get('PS_SHOP_NAME'),
);
/* Email sending */
if ($this->debug) error_log('Sending message');
$mailResult = Mail::Send(
(int)($cookie->id_lang),
'chamilo_already_registered',
$subject,
$templateVars,
$customer->email,
null,
Configuration::get('PS_SHOP_EMAIL'),
Configuration::get('PS_SHOP_NAME'),
null,
null,
dirname(__FILE__).'/mails/'
);
if ($this->debug) {
error_log(print_r($mailResult, 1));
}
}
}
if (!empty($chamilo_user_id)) {
foreach ($courseCodeList as $course_code) {
if ($this->debug) error_log('Subscribing user to the course: '.$course_code);
//if ($this->debug) error_log('Chamilo user was registered with user_id = '.$chamilo_user_id);
$chamilo_params = array(
'course' => $course_code,
'user_id' => $chamilo_user_id,
'secret_key' => $this->sha1
//required, secret key ("your IP address and security key from chamilo") encrypted with sha1
);
$result = $client->call(
'WSSubscribeUserToCourseSimple',
array('subscribeUserToCourseSimple' => $chamilo_params)
);
if ($client->fault) {
if ($this->debug) error_log('Fault');
if ($this->debug) error_log(print_r($result,1));
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
if ($this->debug) error_log('Error');
if ($this->debug) error_log(print_r($err,1));
} else {
if ($this->debug) error_log('Ok');
// Display the result
if ($this->debug) error_log(print_r($result,1));
}
}
}
foreach ($sessionList as $sessionId) {
if ($this->debug) error_log('Subscribing user to the session: '.$sessionId);
$params = array(
'session' => $sessionId,
'user_id' => $chamilo_user_id,
'secret_key' => $this->sha1
);
$result = $client->call(
'WSSubscribeUserToSessionSimple',
array('subscribeUserToSessionSimple' => $params)
);
if ($client->fault) {
if ($this->debug) error_log('Fault');
if ($this->debug) error_log(print_r($result, 1));
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
if ($this->debug) error_log('Error');
if ($this->debug) error_log(print_r($err,1));
} else {
if ($this->debug) error_log('Ok');
// Display the result
if ($this->debug) error_log(print_r($result,1));
}
}
}
} else {
if ($this->debug) error_log('Error while trying to create a Chamilo user : '.print_r($chamilo_params, 1));
}
/* global $cookie;
// Email generation
$subject = '[Prestashop - Chamilo] '.Configuration::get('PS_SHOP_NAME');
$templateVars = array (
'{firstname}' => $params['customer']->firstname,
'{lastname}' => $params['customer']->lastname,
'{email}' => $params['customer']->email,
'{id_order}' => $params['order']->id
);
// Email sending
if (!Mail::Send((int)($cookie->id_lang), 'ekomi', $subject, $templateVars, Configuration::get('PS_SHOP_EMAIL'), NULL, $params['customer']->email, Configuration::get('PS_SHOP_NAME'), NULL, NULL, dirname(__FILE__).'/mails/'))
return false;*/
return true;
}
/**
* Executes hook when a order is created
* @param $params
*/
public function hookNewOrder($params)
{
}
/**
* Returns a difficult to guess password.
* @param int $length, the length of the password
* @todo implement this in Chamilo
* @return string the generated password
*/
public function generate_password($length = 8)
{
$characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
if ($length < 2) {
$length = 2;
}
$password = '';
for ($i = 0; $i < $length; $i ++) {
$password .= $characters[rand() % strlen($characters)];
}
return $password;
}
}