-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
6576 lines (5262 loc) · 181 KB
/
Copy pathauth.php
File metadata and controls
6576 lines (5262 loc) · 181 KB
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Joomdle main class file
*
* Contains all Joomdle web service functions
*
* @package auth_joomdle
* @copyright 2009 Qontori Pte Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->libdir.'/authlib.php');
require_once($CFG->dirroot.'/auth/manual/auth.php');
require_once($CFG->dirroot.'/calendar/lib.php');
require_once($CFG->dirroot.'/mod/forum/lib.php');
require_once($CFG->dirroot.'/lib/datalib.php');
require_once($CFG->dirroot.'/lib/gdlib.php');
require_once($CFG->dirroot.'/lib/grade/grade_grade.php');
require_once($CFG->dirroot.'/lib/grade/grade_item.php');
require_once($CFG->dirroot.'/lib/gradelib.php');
require_once $CFG->dirroot.'/grade/lib.php';
require_once("$CFG->dirroot/enrol/locallib.php");
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/group/lib.php');
require_once($CFG->dirroot.'/lib/grouplib.php');
require_once($CFG->dirroot.'/cohort/lib.php');
require_once $CFG->dirroot.'/grade/report/user/lib.php';
require_once $CFG->dirroot.'/lib/coursecatlib.php';
/**
* Joomdle authentication plugin.
*/
class auth_plugin_joomdle extends auth_plugin_manual {
/**
* Constructor.
*/
function auth_plugin_joomdle() {
$this->authtype = 'joomdle';
$this->config = get_config('auth/joomdle');
}
function can_signup() {
return true;
}
function user_signup($user, $notify=true)
{
global $CFG, $DB;
require_once($CFG->dirroot.'/user/profile/lib.php');
$password_clear = $user->password;
$user->password = hash_internal_user_password($user->password);
if (! ($user->id = $DB->insert_record('user', $user)) ) {
print_error('auth_emailnoinsert','auth');
}
/// Save any custom profile field information
profile_save_data($user);
$conditions = array ('id' => $user->id);
$user = $DB->get_record('user', $conditions);
/* Create user in Joomla */
$userinfo['username'] = $user->username;
$userinfo['password'] = $password_clear;
$userinfo['password2'] = $password_clear;
$userinfo['name'] = $user->firstname. " " . $user->lastname;
$userinfo['firstname'] = $user->firstname;
$userinfo['lastname'] = $user->lastname;
$userinfo['email'] = $user->email;
$userinfo['block'] = 1;
\core\event\user_created::create_from_userid($user->id)->trigger();
if (! send_confirmation_email($user)) {
print_error('auth_emailnoemail','auth');
}
if ($notify) {
$emailconfirm = get_string('emailconfirm');
$PAGE->set_url('/auth/joomdle/auth.php');
$PAGE->navbar->add($emailconfirm);
$PAGE->set_title($emailconfirm);
$PAGE->set_heading($emailconfirm);
echo $OUTPUT->header();
notice(get_string('emailconfirmsent', '', $user->email), "{$CFG->wwwroot}/index.php");
} else {
return true;
}
}
function can_confirm() {
return true;
}
function user_confirm($username, $confirmsecret = null)
{
global $DB;
$user = get_complete_user_data('username', $username);
if (!empty($user)) {
if ($user->confirmed) {
return AUTH_CONFIRM_ALREADY;
} else if ($user->auth != 'joomdle') {
return AUTH_CONFIRM_ERROR;
} else if ($user->secret == stripslashes($confirmsecret)) { // They have provided the secret key to get in
$conditions = array ('id' => $user->id);
if (!$DB->set_field("user", "confirmed", 1, $conditions)) {
return AUTH_CONFIRM_FAIL;
}
if (!$DB->set_field("user", "firstaccess", time(), $conditions)) {
return AUTH_CONFIRM_FAIL;
}
/* Enable de user in Joomla */
$this->call_method ("activateUser", $username);
return AUTH_CONFIRM_OK;
}
} else {
return AUTH_CONFIRM_ERROR;
}
}
/**
* Returns true if the username and password work and false if they are
* wrong or don't exist.
*
* @param string $username The username (with system magic quotes)
* @param string $password The password (with system magic quotes)
*
* @return bool Authentication success or failure.
*/
function user_login($username, $password) {
if (!$password)
return false;
$user = get_complete_user_data ('username', $username);
if (!$user)
return false;
$logged = $this->call_method ("login", $username, $password);
return $logged;
}
/**
* Returns true if this authentication plugin is 'internal'.
*
* @return bool
*/
function is_internal() {
return true;
}
function can_change_password() {
return true;
}
function user_update_password ($user, $password)
{
$return = $this->call_method ("changePassword", $user->username, $password);
$user = get_complete_user_data('id', $user->id);
return update_internal_user_password($user, $password);
// return true;
// return $return;
}
/**
* Prints a form for configuring this authentication plugin.
*
* This function is called from admin/auth.php, and outputs a full page with
* a form for configuring this plugin.
*
* @param array $page An object containing all the data for this page.
*/
function config_form($config, $err, $user_fields) {
include "config.html";
}
function process_config($config)
{
global $DB;
if (!isset($config->joomla_url)) {
$config->joomla_url = '';
}
set_config('joomla_url', $config->joomla_url, 'auth/joomdle');
if (!isset($config->connection_method)) {
$config->connection_method = 'fgc';
}
set_config('connection_method', $config->connection_method, 'auth/joomdle');
if (!isset($config->joomla_auth_token)) {
$config->joomla_auth_token = '';
}
set_config('joomla_auth_token', $config->joomla_auth_token, 'auth/joomdle');
if (!isset($config->sync_to_joomla)) {
$config->sync_to_joomla = 0;
}
set_config('sync_to_joomla', $config->sync_to_joomla, 'auth/joomdle');
if (!isset($config->joomla_lang)) {
$config->joomla_lang = '';
}
set_config('joomla_lang', $config->joomla_lang, 'auth/joomdle');
if (!isset($config->joomla_sef)) {
$config->joomla_sef = '';
}
set_config('joomla_sef', $config->joomla_sef, 'auth/joomdle');
if (!isset($config->redirectless_sso)) {
$config->redirectless_sso = '';
}
set_config('redirectless_sso', $config->redirectless_sso, 'auth/joomdle');
if (!isset($config->logout_redirect_to_joomla)) {
$config->logout_redirect_to_joomla = '';
}
set_config('logout_redirect_to_joomla', $config->logout_redirect_to_joomla, 'auth/joomdle');
if (!isset($config->jomsocial_activities)) {
$config->jomsocial_activities = 0;
}
set_config('jomsocial_activities', $config->jomsocial_activities, 'auth/joomdle');
if (!isset($config->jomsocial_groups)) {
$config->jomsocial_groups = 0;
}
set_config('jomsocial_groups', $config->jomsocial_groups, 'auth/joomdle');
if (!isset($config->jomsocial_groups_delete)) {
$config->jomsocial_groups_delete = 0;
}
set_config('jomsocial_groups_delete', $config->jomsocial_groups_delete, 'auth/joomdle');
if (!isset($config->auto_sell)) {
$config->auto_sell = 0;
}
set_config('auto_sell', $config->auto_sell, 'auth/joomdle');
if (!isset($config->enrol_parents)) {
$config->enrol_parents = 0;
}
set_config('enrol_parents', $config->enrol_parents, 'auth/joomdle');
if (!isset($config->parent_role_id)) {
$config->parent_role_id = '';
}
set_config('parent_role_id', $config->parent_role_id, 'auth/joomdle');
if (!isset($config->give_points)) {
$config->give_points = 0;
}
set_config('give_points', $config->give_points, 'auth/joomdle');
if (!isset($config->auto_mailing_lists)) {
$config->auto_mailing_lists = 0;
}
set_config('auto_mailing_lists', $config->auto_mailing_lists, 'auth/joomdle');
if (!isset($config->joomla_user_groups)) {
$config->joomla_user_groups = 0;
}
set_config('joomla_user_groups', $config->joomla_user_groups, 'auth/joomdle');
if (!isset($config->use_kunena_forums)) {
$config->use_kunena_forums = 0;
}
set_config('use_kunena_forums', $config->use_kunena_forums, 'auth/joomdle');
return true;
}
function _get_xmlrpc_url () {
$joomla_lang = get_config('auth/joomdle', 'joomla_lang');
$joomla_sef = get_config('auth/joomdle', 'joomla_sef');
$joomla_auth_token = get_config('auth/joomdle', 'joomla_auth_token');
if ($joomla_lang == '')
$joomla_xmlrpc_server_url = get_config ('auth/joomdle', 'joomla_url').'/index.php?option=com_joomdle&task=ws.server&format=xmlrpc';
else
if ($joomla_sef)
$joomla_xmlrpc_server_url = get_config ('auth/joomdle', 'joomla_url').'/index.php/'.$joomla_lang.'/?option=com_joomdle&task=ws.server&format=xmlrpc';
else
$joomla_xmlrpc_server_url = get_config ('auth/joomdle', 'joomla_url').'/index.php?lang='.$joomla_lang.'&option=com_joomdle&task=ws.server&format=xmlrpc';
// Add auth token
$joomla_xmlrpc_server_url .= "&token=" . $joomla_auth_token;
return $joomla_xmlrpc_server_url;
}
function call_method ($method, $params = '', $params2 = '', $params3 = '' , $params4 = '', $params5 = '')
{
$connection_method = get_config('auth/joomdle', 'connection_method');
if ($connection_method == 'fgc')
$response = $this->call_method_fgc ($method, $params, $params2, $params3, $params4, $params5);
else
$response = $this->call_method_curl ($method, $params, $params2, $params3, $params4, $params5);
return $response;
}
function call_method_fgc ($method, $params = '', $params2 = '', $params3 = '' , $params4 = '', $params5 = '')
{
$joomla_xmlrpc_url = $this->_get_xmlrpc_url ();
$options = array ();
if ($params == '')
$request = xmlrpc_encode_request("joomdle.".$method, array (), $options);
else if ($params2 == '')
$request = xmlrpc_encode_request("joomdle.".$method, array ($params), $options);
else if ($params3 == '')
$request = xmlrpc_encode_request("joomdle.".$method, array ($params, $params2), $options);
else if ($params4 == '')
$request = xmlrpc_encode_request("joomdle.".$method, array ($params, $params2, $params3), $options);
else if ($params5 == '')
$request = xmlrpc_encode_request("joomdle.".$method, array ($params, $params2, $params3, $params4), $options);
else
$request = xmlrpc_encode_request("joomdle.".$method, array ($params, $params2, $params3, $params4, $params5), $options);
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml ",
'content' => $request
)));
$response = file_get_contents($joomla_xmlrpc_url, false, $context);
$data = xmlrpc_decode($response);
if (is_array ($data))
if (xmlrpc_is_fault ($data))
{
return "XML-RPC Error (".$data['faultCode']."): ".$data['faultString'];
}
return $data;
}
function call_method_curl ($method, $params = '', $params2 = '', $params3 = '' , $params4 = '', $params5 = '')
{
global $CFG;
$joomla_xmlrpc_url = $this->_get_xmlrpc_url ();
$options = array ();
if ($params == '')
$request = xmlrpc_encode_request("joomdle.".$method, array (), $options);
else if ($params2 == '')
$request = xmlrpc_encode_request("joomdle.".$method, array ($params), $options);
else if ($params3 == '')
$request = xmlrpc_encode_request("joomdle.".$method, array ($params, $params2), $options);
else if ($params4 == '')
$request = xmlrpc_encode_request("joomdle.".$method, array ($params, $params2, $params3), $options);
else if ($params5 == '')
$request = xmlrpc_encode_request("joomdle.".$method, array ($params, $params2, $params3, $params4), $options);
else
$request = xmlrpc_encode_request("joomdle.".$method, array ($params, $params2, $params3, $params4, $params5), $options);
$headers = array();
array_push($headers,"Content-Type: text/xml");
array_push($headers,"Content-Length: ".strlen($request));
array_push($headers,"\r\n");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $joomla_xmlrpc_url); # URL to post to
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); # custom headers, see above
curl_setopt( $ch, CURLOPT_POSTFIELDS, $request );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); # This POST is special, and uses its specified Content-type
// use proxy if one is configured
if (!empty($CFG->proxyhost)) {
if (empty($CFG->proxyport)) {
curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost);
} else {
curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost.':'.$CFG->proxyport);
}
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
}
$response = curl_exec( $ch ); # run!
curl_close($ch);
$data = xmlrpc_decode($response);
if (is_array ($response))
if (xmlrpc_is_fault ($response))
{
return "XML-RPC Error (".$response['faultCode']."): ".$response['faultString'];
}
return $data;
}
function get_file ($file)
{
$connection_method = get_config('auth/joomdle', 'connection_method');
if ($connection_method == 'fgc')
$response = file_get_contents ($file, FALSE, NULL);
else
$response = $this->get_file_curl ($file);
return $response;
}
function get_file_curl ($file)
{
global $CFG;
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $file);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// use proxy if one is configured
if (!empty($CFG->proxyhost)) {
if (empty($CFG->proxyport)) {
curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost);
} else {
curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost.':'.$CFG->proxyport);
}
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
}
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
return $output;
}
function test () {
return "Moodle web services are working!";
}
function system_check ()
{
$system['joomdle_auth'] = is_enabled_auth('joomdle');
// $system['mnet_auth'] = is_enabled_auth('mnet');
$system['mnet_auth'] = 1; // Left this way so we can have the same system check code for 19 and 20
/* Web services and XMLRPC enabled */
// $system['enablewebservices'] = get_config (NULL, 'enablewebservices');
$joomla_url = get_config ('auth/joomdle', 'joomla_url');
if ($joomla_url == '')
{
$system['joomdle_configured'] = 0;
}
else
{
$system['joomdle_configured'] = 1;
$data = $this->call_method ("test");
if (is_array ($data))
{
//j1.5 devuelve un error xmlrpc con esta info
$system['test_data'] = $data['faultString'];
}
else
$system['test_data'] = $data;
}
return $system;
}
function get_paypal_config ()
{
global $CFG;
$paypal_config = array ();
$paypal_config['paypalurl'] = empty($CFG->usepaypalsandbox) ? 'https://www.paypal.com/cgi-bin/webscr' : 'https://www.sandbox.paypal.com/cgi-bin/webscr';
$plugin = enrol_get_plugin('paypal');
$paypal_config['paypalbusiness'] = $plugin->get_config('paypalbusiness');
return $paypal_config;
}
function my_courses ($username, $order_by_cat = 0)
{
global $CFG;
$username = utf8_decode ($username);
$username = strtolower ($username);
$user = get_complete_user_data ('username', $username);
if (!$user)
return array ();
if ($order_by_cat)
$c = enrol_get_users_courses ($user->id, true, array ('summary'), 'category, sortorder ASC');
else
$c = enrol_get_users_courses ($user->id, true, array ('summary'));
$courses = array ();
$i = 0;
foreach ($c as $course)
{
$record = array ();
$record['id'] = $course->id;
$record['fullname'] = $course->fullname;
$record['category'] = $course->category;
$record['cat_name'] = $this->get_cat_name ($course->category);
$record['summary'] = $course->summary;
// Check if user can self-unenrol
$context = context_course::instance($course->id);
if ((has_capability('enrol/manual:unenrolself', $context, $user->id)) || (has_capability('enrol/self:unenrolself', $context, $user->id)))
$record['can_unenrol'] = 1;
else
$record['can_unenrol'] = 0;
$record['summary_files'] = array ();
$course_obj = new course_in_list(get_course($course->id));
foreach ($course_obj->get_course_overviewfiles() as $file)
{
$isimage = $file->is_valid_image();
$url = file_encode_url("$CFG->wwwroot/auth/joomdle/pluginfile_joomdle.php",
'/'. $file->get_contextid(). '/'. $file->get_component(). '/'.
$file->get_filearea(). $file->get_filepath(). $file->get_filename(), !$isimage);
$url_item = array ();
$url_item['url'] = $url;
$record['summary_files'][] = $url_item;
}
$courses[$i] = $record;
$i++;
}
return $courses;
}
// Returns all courses in which the user has a role assigned
function my_all_courses ($username)
{
global $CFG, $DB;
$username = utf8_decode ($username);
$username = strtolower ($username);
$user = get_complete_user_data ('username', $username);
$query = " SELECT distinct c.id as remoteid, c.fullname, ca.name as cat_name, ca.id as cat_id, ra.roleid
FROM {$CFG->prefix}course as c, {$CFG->prefix}role_assignments AS ra, {$CFG->prefix}user AS u, {$CFG->prefix}context AS ct, {$CFG->prefix}course_categories ca
WHERE c.id = ct.instanceid AND ra.userid = u.id AND ct.id = ra.contextid AND ca.id = c.category and u.username= ?";
// Get user student courses
$user = get_complete_user_data ('username', $username);
$c = enrol_get_users_courses ($user->id, true); // get only non supended enrolments
$my_courses = array ();
foreach ($c as $course)
{
$my_courses[] = $course->id;
}
$params = array ($username);
$records = $DB->get_records_sql($query, $params);
$data = array ();
$i = 0;
foreach ($records as $p)
{
if ($p->roleid == 5) // If student, check that enrolment is not suspended
{
if (!in_array ($p->remoteid, $my_courses))
continue;
}
$e['id'] = $p->remoteid;
$e['fullname'] = $p->fullname;
$e['fullname'] = format_string($e['fullname']);
$e['category'] = $p->cat_id;
$data[$i] = $e;
$i++;
}
return $data;
}
function my_teachers ($username)
{
global $CFG;
$username = utf8_decode ($username);
$username = strtolower ($username);
$user = get_complete_user_data ('username', $username);
$c = enrol_get_users_courses ($user->id);
$courses = array ();
$i = 0;
foreach ($c as $course)
{
$record = array ();
$record['id'] = $course->id;
$record['fullname'] = $course->fullname;
$context = context_course::instance($course->id);
/* 3 indica profesores editores (table mdl_role) */
$profs = get_role_users(3 , $context);
$data = array ();
foreach ($profs as $p)
{
$e['firstname'] = $p->firstname;
$e['lastname'] = $p->lastname;
$e['username'] = $p->username;
$data[] = $e;
}
$record['teachers'] = $data;
$courses[$i] = $record;
$i++;
}
return $courses;
}
function my_classmates ($username)
{
global $CFG;
$username = utf8_decode ($username);
$username = strtolower ($username);
$user = get_complete_user_data ('username', $username);
$c = enrol_get_users_courses ($user->id);
$courses = array ();
$i = 0;
$data = array ();
foreach ($c as $course)
{
$mates = $this->get_course_students ($course->id);
foreach ($mates as $p)
{
$e['firstname'] = $p['firstname'];
$e['lastname'] = $p['lastname'];
$e['username'] = $p['username'];
$data[$e['username']] = $e;
}
}
return $data;
}
/**
* Returns course list
*
* @param int $available If true, return only self enrollable courses
*/
function list_courses ($available = 0, $sortby = 'created', $guest = 0, $username = '')
{
global $CFG, $DB;
$where = '';
$query =
"SELECT
co.id AS remoteid,
ca.id AS cat_id,
co.sortorder,
co.fullname,
co.shortname,
co.idnumber,
co.summary,
co.startdate,
co.timecreated as created,
co.timemodified as modified,
ca.name AS cat_name,
ca.description AS cat_description
FROM
{$CFG->prefix}course_categories ca
JOIN
{$CFG->prefix}course co ON
ca.id = co.category
WHERE
co.visible = '1'
$where
ORDER BY
$sortby
";
$records = $DB->get_records_sql($query);
if ($username)
{
$user = get_complete_user_data ('username', $username);
$c = enrol_get_users_courses ($user->id, true);
$my_courses = array ();
foreach ($c as $course)
{
$my_courses[] = $course->id;
}
}
$i = 0;
$now = time();
$options['noclean'] = true;
$cursos = array ();
foreach ($records as $curso)
{
$enrol_methods = enrol_get_instances($curso->remoteid, true);
$c = get_object_vars ($curso);
$c['self_enrolment'] = 0;
$c['guest'] = 0;
$in = true;
foreach ($enrol_methods as $instance)
{
if (($instance->enrol == 'paypal') || ($instance->enrol == 'joomdle'))
{
$enrol = $instance->enrol;
$query = "SELECT cost, currency
FROM {$CFG->prefix}enrol
where courseid = ? and enrol = ?";
$params = array ($curso->remoteid, $enrol);
$record = $DB->get_record_sql($query, $params);
$c['cost'] = (float) $record->cost;
$c['currency'] = $record->currency;
}
// Self-enrolment
if ($instance->enrol == 'self')
$c['self_enrolment'] = 1;
else
$c['self_enrolment'] = 0;
// Guest access
if ($instance->enrol == 'guest')
$c['guest'] = 1;
if (($instance->enrolstartdate) && ($instance->enrolenddate))
{
$in = false;
if (($instance->enrolstartdate <= $now) && ($instance->enrolenddate >= $now))
$in = true;
}
else if ($instance->enrolstartdate)
{
$in = false;
if (($instance->enrolstartdate <= $now))
$in = true;
}
else if ($instance->enrolenddate)
{
$in = false;
if ($instance->enrolenddate >= $now)
$in = true;
}
}
// Check if only guest courses are wanted
if (($guest) && (!$course_info['guest']))
continue;
// Skip not self-enrolable courses if param says so
if (($available) && (!$c['self_enrolment']))
continue;
$c['in_enrol_date'] = $in;
$c['enroled'] = 0;
if ($username)
{
if (in_array ($curso->remoteid, $my_courses))
$c['enroled'] = 1;
}
$c['fullname'] = format_string($c['fullname']);
$c['cat_name'] = format_string($c['cat_name']);
$context = context_course::instance($curso->remoteid);
$c['summary'] = file_rewrite_pluginfile_urls ($c['summary'], 'pluginfile.php', $context->id, 'course', 'summary', NULL);
$c['summary'] = str_replace ('pluginfile.php', '/auth/joomdle/pluginfile_joomdle.php', $c['summary']);
$c['summary'] = format_text($c['summary'], FORMAT_MOODLE, $options);
$context = context_coursecat::instance($curso->cat_id);
$c['cat_description'] = file_rewrite_pluginfile_urls ($c['cat_description'], 'pluginfile.php', $context->id, 'coursecat', 'description', NULL);
$c['cat_description'] = str_replace ('pluginfile.php', '/auth/joomdle/pluginfile_joomdle.php', $c['cat_description']);
$c['cat_description'] = format_text($c['cat_description'], FORMAT_MOODLE, $options);
$c['summary_files'] = array ();
$course = new course_in_list(get_course($curso->remoteid));
foreach ($course->get_course_overviewfiles() as $file)
{
$isimage = $file->is_valid_image();
$url = file_encode_url("$CFG->wwwroot/auth/joomdle/pluginfile_joomdle.php",
'/'. $file->get_contextid(). '/'. $file->get_component(). '/'.
$file->get_filearea(). $file->get_filepath(). $file->get_filename(), !$isimage);
$url_item = array ();
$url_item['url'] = $url;
$c['summary_files'][] = $url_item;
}
$cursos[$i] = $c;
$i++;
}
return ($cursos);
}
/**
* Returns course list, including non visible courses
*/
function get_all_courses ($sortby = 'created')
{
global $CFG, $DB;
$query =
"SELECT
co.id AS remoteid,
ca.id AS cat_id,
co.sortorder,
co.fullname,
co.shortname,
co.idnumber,
co.summary,
co.startdate,
co.timecreated as created,
co.timemodified as modified,
ca.name AS cat_name,
ca.description AS cat_description
FROM
{$CFG->prefix}course_categories ca
JOIN
{$CFG->prefix}course co ON
ca.id = co.category
ORDER BY
$sortby
";
$records = $DB->get_records_sql($query);
$i = 0;
$now = time();
$options['noclean'] = true;
$cursos = array ();
foreach ($records as $curso)
{
$enrol_methods = enrol_get_instances($curso->remoteid, true);
$c = get_object_vars ($curso);
$c['self_enrolment'] = 0;
$c['guest'] = 0;
$in = true;
foreach ($enrol_methods as $instance)
{
if (($instance->enrol == 'paypal') || ($instance->enrol == 'joomdle'))
{
$enrol = $instance->enrol;
$query = "SELECT cost, currency
FROM {$CFG->prefix}enrol
where courseid = ? and enrol = ?";
$params = array ($curso->remoteid, $enrol);
$record = $DB->get_record_sql($query, $params);
$c['cost'] = (float) $record->cost;
$c['currency'] = $record->currency;
}
// Self-enrolment
if ($instance->enrol == 'self')
$c['self_enrolment'] = 1;
else
$c['self_enrolment'] = 0;
// Guest access
if ($instance->enrol == 'guest')
$c['guest'] = 1;
if (($instance->enrolstartdate) && ($instance->enrolenddate))
{
$in = false;
if (($instance->enrolstartdate <= $now) && ($instance->enrolenddate >= $now))
$in = true;
}
else if ($instance->enrolstartdate)
{
$in = false;
if (($instance->enrolstartdate <= $now))
$in = true;
}
else if ($instance->enrolenddate)
{
$in = false;
if ($instance->enrolenddate >= $now)
$in = true;
}
}
$c['in_enrol_date'] = $in;
$c['fullname'] = format_string($c['fullname']);
$c['cat_name'] = format_string($c['cat_name']);
$context = context_course::instance($curso->remoteid);
$c['summary'] = file_rewrite_pluginfile_urls ($c['summary'], 'pluginfile.php', $context->id, 'course', 'summary', NULL);
$c['summary'] = str_replace ('pluginfile.php', '/auth/joomdle/pluginfile_joomdle.php', $c['summary']);
$c['summary'] = format_text($c['summary'], FORMAT_MOODLE, $options);
$context = context_coursecat::instance($curso->cat_id);
$c['cat_description'] = file_rewrite_pluginfile_urls ($c['cat_description'], 'pluginfile.php', $context->id, 'coursecat', 'description', NULL);
$c['cat_description'] = str_replace ('pluginfile.php', '/auth/joomdle/pluginfile_joomdle.php', $c['cat_description']);
$c['cat_description'] = format_text($c['cat_description'], FORMAT_MOODLE, $options);
$c['summary_files'] = array ();
$course = new course_in_list(get_course($curso->remoteid));
foreach ($course->get_course_overviewfiles() as $file)
{
$isimage = $file->is_valid_image();
$url = file_encode_url("$CFG->wwwroot/auth/joomdle/pluginfile_joomdle.php",
'/'. $file->get_contextid(). '/'. $file->get_component(). '/'.
$file->get_filearea(). $file->get_filepath(). $file->get_filename(), !$isimage);
$url_item = array ();
$url_item['url'] = $url;
$c['summary_files'][] = $url_item;
}
$cursos[$i] = $c;
$i++;
}
return ($cursos);
}
/**
* Returns course list based on start chars
*
* @param start_chars: return courses that starts with these chars
*/
function courses_abc ($start_chars, $username)
{
global $CFG, $DB;
$where = '(';
$array = str_split ($start_chars);
foreach ($array as $c)