forked from moodlehq/moodle-local_hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
2258 lines (1955 loc) · 85.6 KB
/
lib.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
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/>.
/**
* Hub library
* @package localhub
* @copyright 2010 Moodle Pty Ltd (http://moodle.com)
* @author Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('HUB_COURSE_PER_PAGE', 10);
//NEVER change this value after installation, otherwise you will need to change all rating in the DB
define('HUB_COURSE_RATING_SCALE', 10);
/**
* Maximum number of course per web service request
*/
define('HUB_MAXWSCOURSESRESULT', 25);
/**
* Maximum number of course per day default
*/
define('HUB_MAXCOURSESPERSITEPERDAY', 20);
//// HUB IMAGE SIZE
/**
* maximum width that a logo image can have (used to display site logo
* in community hub)
*/
define('HUBLOGOIMAGEWIDTH', 150);
/**
* maximum height that a logo image can have (used to display site logo
* in community hub)
*/
define('HUBLOGOIMAGEHEIGHT', 150);
/**
* maximum number of screenshot for a course
*/
define('MAXSCREENSHOTSNUMBER', 10);
//// HUB PRIVACY
/**
* Hub privacy: private
*/
define('HUBPRIVATE', 'private');
/**
* Hub privacy: public
*/
define('HUBALLOWPUBLICSEARCH', 'public');
/**
* Hub privacy: public and global
*/
define('HUBALLOWGLOBALSEARCH', 'search');
//// Communication /////
/**
* Hub server
*/
define('HUB', 'hub');
/**
* Registered site
*/
define('REGISTEREDSITE', 'site');
/**
* Public site
*/
define('PUBLICSITE', 'publicsite');
/**
* Hub directory
*/
define('HUBDIRECTORY', 'hubdirectory');
/**
* WS server
*/
define('WSSERVER', 'server');
/**
* WS client
*/
define('WSCLIENT', 'client');
//// Course visibility ////
/**
* Course visibility: all
*/
define('COURSEVISIBILITY_ALL', '2');
/**
* Course visibility: visible
*/
define('COURSEVISIBILITY_VISIBLE', '1');
/**
* Course visibility: not visible
*/
define('COURSEVISIBILITY_NOTVISIBLE', '0');
class local_hub {
///////////////////////////
/// DB Facade functions //
///////////////////////////
/**
* Add a new course feedback
* @param object $feedback
* $feedback->courseid integer - mandatory
* $feedback->type string ('question', 'issue', 'improvement', 'appreciation')
* $feedback->text string
* $feedback->userid integer if not given, the current user is used
*/
public function add_feedback($feedback) {
global $DB, $USER;
if (!isset($feedback->courseid)) {
throw new moodle_exception('feedbackcourseidmissing');
}
if (!isset($feedback->userid)) {
$feedback->userid = $USER->id;
}
$feedback->time = time();
$DB->insert_record('hub_course_feedbacks', $feedback);
}
/**
* Return a site for a given id
* @param integer $id
* @param integer $strictness
* @return object site, false if null
*/
public function get_site($id, $strictness = IGNORE_MISSING) {
global $DB;
return $DB->get_record('hub_site_directory', array('id' => $id), '*', $strictness);
}
/**
* Unregister a site from the directory
* If the site doesn't exist do nothing - no error thrown
* If the site id doesn't match the site url, throw an error
* @param string $siteurl
*/
public function unregister_site($site) {
global $CFG;
//check that the site exist (otherwise unregister)
if (!empty($site)) {
//reset the following settings that could have been changed by admin
$site->deleted = 1;
$site->trusted = 0;
$site->visible = 0;
$site->prioritise = 0;
$this->update_site($site);
add_to_log(SITEID, 'local_hub', 'site unregistration', '', $site->id);
}
}
/**
* Update a site
* @param object $site
* @throws dml_exception if error
*/
public function update_site($site) {
global $DB;
$site->timemodified = time();
$DB->update_record('hub_site_directory', $site);
// update_sendy_list($site);
}
/**
* Add a site into the site directory
* @param object $site
* @return object site
* @throws dml_exception if error
*/
public function add_site($site, $usegivenregtime=false) {
global $DB;
if (!$usegivenregtime) {
$site->timeregistered = time();
}
$site->timemodified = time();
// Check if site already exists.
$oldsite = $this->get_site_by_url($site->url, null);
$site->deleted = 0;
if (!empty($oldsite)) {
$site->id = $oldsite->id;
$this->update_site($site);
} else {
$site->id = $DB->insert_record('hub_site_directory', $site);
// update_sendy_list($site);
}
return $site;
}
/**
* Delete all outcomes linked to a course id and add a new array of outcome
* @param integer $courseid
* @param array $outcomes
* this array contain the outcome. One outcome is an array.
* outcome['fullname'] => the outcome fullname
* TODO outcome['description']
*/
public function update_course_outcomes($courseid, $outcomes = array()) {
global $DB;
//delete previous outcomes
$DB->delete_records('hub_course_outcomes', array('courseid' => $courseid));
//add new outcomes
if (!empty($outcomes)) {
foreach ($outcomes as $outcome) {
$newoutcome = new stdClass();
$newoutcome->courseid = $courseid;
$newoutcome->outcome = $outcome['fullname'];
$DB->insert_record('hub_course_outcomes', $newoutcome);
}
}
}
/**
* Add a course into the course directory
* @param object $
* @return integer course id
* @throws dml_exception if error
*/
public function add_course($course) {
global $DB;
$course->timepublished = time();
$course->timemodified = time();
$id = $DB->insert_record('hub_course_directory', $course);
return $id;
}
/**
* Return a course for a given id
* @param integer $id
* @param integer $strictness
* @return object course, false if null
*/
public function get_course($id, $strictness = IGNORE_MISSING) {
global $DB;
return $DB->get_record('hub_course_directory', array('id' => $id), '*', $strictness);
}
/**
* Return a enrollable course for a given site id and course id
* @param integer $id
* @return object course, false if null
*/
public function get_enrollable_course_by_site($siteid, $sitecourseid) {
global $DB;
return $DB->get_record('hub_course_directory',
array('siteid' => $siteid, 'sitecourseid' => $sitecourseid, 'enrollable' => 1));
}
/**
* Mark a course as deleted (we never really delete a course)
* @param integer $id - id of the course to remove from the directory
* @return boolean true
* @throws dml_exception if error
*/
public function delete_course($id) {
global $DB;
$course = $DB->get_record('hub_course_directory',
array('id' => $id));
$course->deleted = 1;
$course->timemodified = time();
return $DB->update_record('hub_course_directory', $course);
}
/**
* Mark all courses of a site as deleted (we never really delete a course)
* @param integer $siteid - site id of the courses to remove from the directory
* @return boolean true
* @throws dml_exception if error
*/
public function delete_courses($siteid) {
global $DB;
$DB->set_field('hub_course_directory', 'deleted', 1, array('siteid' => $siteid));
$DB->set_field('hub_course_directory', 'timemodified', time(), array('siteid' => $siteid));
return true;
}
/**
* Update a course
* @param object $course
* @throws dml_exception if error
*/
public function update_course($course) {
global $DB;
$course->timemodified = time();
$DB->update_record('hub_course_directory', $course);
}
/**
* Return course found against some options, by default it returns all visible courses
* @param array $options all different search options
* string 'search' string that will be compared to course name and site description
* string 'options' language, license, audience, subject...
* boolean 'onlyvisible' - set to true to return only visible course (not set => all course returned)
* boolean 'downloadable' - set to true to return downloadable course
* boolean 'enrollable' - set to true to return enrollable course
* boolean 'onlydeleted' - set to true to return deleted course only,
* otherwise only undeleted
* string 'subject' - all course with this subject and subsubject
* (TODO: need a subjectonly options)
* string 'language' - all course with this language
* string 'audience' - all course with this audience
* string 'licenceshortname' - all course with this licence
* string 'educationallevel' - select
* string 'visibility' - if 'onlyvisible' is equal to true, then this option is not used.
* This option should only be used for admin pages.
* array 'ids' - return all the course for these given id on the hub
* array 'sitecourseids' - return all the course for these given id on the site
* (most of the time you will give a siteid)
* integer 'siteid' - return course for this site id only
* integer 'lastmodified' - return all the courses that have been modified/created
* after this lastmodified time.
* integer 'lastpublished' - return all the courses that have been first published
* after this lastpublished time.
* string 'orderby' - let you override the default order by
* @param int $limitfrom
* @param int $limitnum
* @param bool $countresult if set to true return only the course total
* (then limitnum and limitfrom are ignored),
* otherwise return the courses. It exist mainly for pagination, so we don't
* need a second cout_courses() function almost similar to this one.
* @return array of courses/int
*/
public function get_courses($options = array(), $limitfrom=0, $limitnum=0, $countresult = false) {
global $DB;
$sqlparams = array();
$wheresql = '';
if (!empty($options['onlyvisible'])) {
$wheresql .= " privacy = :visible";
$sqlparams['visible'] = 1;
$ordersql = 'fullname';
} else {
$ordersql = 'privacy DESC, fullname';
}
if (!empty($options['search'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " ( " . $DB->sql_like('fullname', ':namesearch', false)
. " OR " . $DB->sql_like('description', ':descsearch', false)
. " OR " . $DB->sql_like('coverage', ':coveragesearch', false) . " )";
$sqlparams['namesearch'] = '%' . $options['search'] . '%';
$sqlparams['descsearch'] = '%' . $options['search'] . '%';
$sqlparams['coveragesearch'] = '%' . $options['search'] . '%';
}
if (!empty($options['language'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " language = :language";
$sqlparams['language'] = $options['language'];
}
if (!empty($options['audience'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " audience = :audience";
$sqlparams['audience'] = $options['audience'];
}
if (!empty($options['licenceshortname'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " licenceshortname = :licenceshortname";
$sqlparams['licenceshortname'] = $options['licenceshortname'];
}
if (!empty($options['subject'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
//search subject and all sub-subjects
$edufields = get_string_manager()->load_component_strings('edufields', 'en');
$topsubject = true;
foreach ($edufields as $key => $value) {
if (strpos($key, $options['subject']) !== false) {
if ($topsubject) {
$wheresql .= " (";
$topsubject = false;
} else {
$wheresql .= " OR";
}
$wheresql .= " subject = :" . $key;
$sqlparams[$key] = $key;
}
}
$wheresql .= ")";
}
if (!empty($options['educationallevel'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " educationallevel = :educationallevel";
$sqlparams['educationallevel'] = $options['educationallevel'];
}
//this option should be only be called by admin script
//note that this option is overrided by the onlyvisible parameter
if (key_exists('visibility', $options) and $options['visibility'] != COURSEVISIBILITY_ALL) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
if ($options['visibility'] === COURSEVISIBILITY_VISIBLE) {
$privacy = 1;
} else {
$privacy = 0;
}
$wheresql .= " privacy = :visibility";
$sqlparams['visibility'] = $privacy;
}
if (!empty($options['ids'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$idlist = '(';
foreach ($options['ids'] as $id) {
if ($idlist == '(') {
$idlist .= $id;
} else {
$idlist .= ',' . $id;
}
}
$idlist .= ')';
$wheresql .= " id IN " . $idlist;
}
if (!empty($options['sitecourseids'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$idlist = '(';
foreach ($options['sitecourseids'] as $sitecourseid) {
if ($idlist == '(') {
$idlist .= $sitecourseid;
} else {
$idlist .= ',' . $sitecourseid;
}
}
$idlist .= ')';
$wheresql .= " sitecourseid IN " . $idlist;
}
//check that one of the downloadable/enrollable option is false (otherwise display both kind of course)
if (!((key_exists('downloadable', $options) and $options['downloadable'])
and
(key_exists('enrollable', $options) and $options['enrollable']))) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
if (key_exists('downloadable', $options) and $options['downloadable']) {
$wheresql .= " enrollable = 0";
} else if (key_exists('enrollable', $options) and $options['enrollable']) {
$wheresql .= " enrollable = 1";
} else { //this case means that we are searching course as downloadable == 0 and enrollable == 0
$wheresql .= " enrollable = 4"; //=> return nothing,
//it should never be ask to return a course not enrollable AND not downloadable
}
}
if (!empty($options['siteid'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " siteid = :siteid";
$sqlparams['siteid'] = $options['siteid'];
}
if (!empty($options['lastmodified'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " timemodified > :lastmodified";
$sqlparams['lastmodified'] = $options['lastmodified'];
}
if (!empty($options['lastpublished'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " timepublished > :lastpublished";
$sqlparams['lastpublished'] = $options['lastpublished'];
}
if (!empty($wheresql)) {
$wheresql .= " AND";
}
if (!key_exists('onlydeleted', $options) or !$options['onlydeleted']) {
$wheresql .= " deleted = 0";
} else {
$wheresql .= " deleted = 1";
}
if ($countresult) {
$courses = $DB->count_records_select('hub_course_directory', $wheresql, $sqlparams);
} else {
//retrieve rating
$extracolumns = ' , r.ratingaverage, r.ratingcount ';
$joinsql = ' LEFT JOIN (
SELECT itemid, AVG(rating) AS ratingaverage, COUNT(id) AS ratingcount
FROM {rating} GROUP BY itemid
) r ON r.itemid = c.id ';
//sort result
$ordersql = '';
if (!empty($options['orderby'])) {
$ordersql = ' ORDER BY ' . $options['orderby'];
}
$sql = 'SELECT c.* ' . $extracolumns . 'FROM {hub_course_directory} c ' . $joinsql . ' WHERE '
. $wheresql . $ordersql;
$courses = $DB->get_records_sql($sql, $sqlparams, $limitfrom, $limitnum);
}
//retrieve the outcomes
//TODO: improve this, incorporating it into the request above
if (!$countresult) {
if (!empty($courses)) {
foreach ($courses as &$course) {
$courseoutcomes = $this->get_course_outcomes($course->id);
foreach ($courseoutcomes as $courseoutcome) {
$course->outcomes[] = $courseoutcome->outcome;
}
}
}
}
return $courses;
}
/**
* Return sites found against some options.
* By default it returns all visible and not deleted sites
*
* @param array $options array of options
* onlyvisible - boolean - return only visible sites, otherwise all sites
* search - string - search terms (on name and description)
* language - string - return sites of this language
* contactemail - string - returns sites associated with this email address
* urls - array of strings - return sites for these urls
* visible - boolean - return visible or not visible sites
* trusted - boolean - return trusted or not trusted sites
* prioritise - boolean - return prioritised or not prioritised sites
* countrycode - string - return sites for this country code
* onlydeleted - boolean - return only deleted sites
* deleted - boolean - return deleted and not deleted sites
* @return array of sites
*/
public function get_sites($options = array(), $limitfrom=0, $limitnum=0, $countresult = false) {
global $DB;
$sqlparams = array();
$wheresql = '';
if (key_exists('onlyvisible', $options) and !empty($options['onlyvisible'])) {
$wheresql .= " visible = :visible";
$sqlparams['visible'] = 1;
$ordersql = 'prioritise DESC, trusted DESC, name';
} else {
$ordersql = 'prioritise DESC, trusted DESC, visible DESC, name';
}
if (key_exists('search', $options) and !empty($options['search'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " (" . $DB->sql_like('name', ':namesearch', false)
. " OR " . $DB->sql_like('description', ':descsearch', false) . " "
. " OR " . $DB->sql_like('url', ':urlsearch', false) . " )";
$sqlparams['namesearch'] = '%' . $options['search'] . '%';
$sqlparams['descsearch'] = '%' . $options['search'] . '%';
$sqlparams['urlsearch'] = '%' . $options['search'] . '%';
}
if (key_exists('language', $options) and !empty($options['language'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " language = :language";
$sqlparams['language'] = $options['language'];
}
if (key_exists('contactemail', $options) and !empty($options['contactemail'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " contactemail = :contactemail";
$sqlparams['contactemail'] = $options['contactemail'];
}
if (key_exists('urls', $options) and !empty($options['urls'])) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$urllist = '(\'';
foreach ($options['urls'] as $url) {
if ($urllist == '(\'') {
$urllist .= $url;
} else {
$urllist .= '\',\'' . $url;
}
}
$urllist .= '\')';
$wheresql .= " url IN " . $urllist;
}
//this option should be only be called by admin script
//note that this option is overrided by the onlyvisible parameter
if (key_exists('visible', $options)) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " visible = :visibility";
$sqlparams['visibility'] = $options['visible'];
}
if (key_exists('trusted', $options)) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " trusted = :trusted";
$sqlparams['trusted'] = $options['trusted'];
}
if (key_exists('prioritise', $options)) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " prioritise = :prioritise";
$sqlparams['prioritise'] = $options['prioritise'];
}
if (key_exists('countrycode', $options)) {
if (!empty($wheresql)) {
$wheresql .= " AND";
}
$wheresql .= " countrycode = :countrycode";
$sqlparams['countrycode'] = $options['countrycode'];
}
if (!empty($wheresql)) {
$wheresql .= " AND";
}
if (!key_exists('onlydeleted', $options) or !$options['onlydeleted']) {
if (empty($options['deleted'])) { //do no return any deleted sites
$wheresql .= " deleted = 0";
}
} else {
$wheresql .= " deleted = 1";
}
if ($countresult) {
$sites = $DB->count_records_select('hub_site_directory', $wheresql, $sqlparams);
} else {
$sites = $DB->get_records_select('hub_site_directory', $wheresql, $sqlparams, $ordersql,
'*', $limitfrom, $limitnum);
}
return $sites;
}
/**
* Return a site for a given secret
* @param string $secret
* @return object site , false if null
*/
public function get_site_by_secret($secret) {
global $DB;
return $DB->get_record('hub_site_directory', array('secret' => $secret, 'deleted' => 0));
}
/**
* Return a site for a given url
* @param string $url
* @param int $deleted 0 for not deleted, 1 for deleted, null for either
* @return object site , false if null
*/
public function get_site_by_url($url, $deleted = 0) {
global $DB;
$params = array('url' => $url);
if ($deleted !== null) {
$params['deleted'] = $deleted;
}
return $DB->get_record('hub_site_directory', $params);
}
/**
* Return number of visible registered sites
* @return integer
*/
public function get_sitesregister($fromid=0, $numrecs=50, $modifiedafter=0) {
global $DB;
return $DB->get_records_select('hub_site_directory', 'id > :id AND (timemodified > :timemod OR timelinkchecked > :timelinkcheck)', array('id' => $fromid, 'timemod' => $modifiedafter, 'timelinkcheck' => $modifiedafter), '', '*', 0, $numrecs);
}
/**
* Return number of visible registered sites
* @return integer
*/
public function get_registered_sites_total() {
global $DB;
return $DB->count_records('hub_site_directory', array('deleted' => 0));
}
/**
* Return number of visible registered courses
* @return integer
*/
public function get_registered_courses_total() {
global $DB;
return $DB->count_records('hub_course_directory', array('privacy' => 1, 'deleted' => 0));
}
/**
* Record a communication information between the hub and another entity
* (hub directory, site, public site)
* Mostly use to remember the token given to us by the remote entity,
* or the token that we gave to the remote entity
* A communication is composed by:
* type of the communication, hub point of view: SERVER / CLIENT
* remoteentity name
* remoteentity url
* token used during this communication
* If a communication was previously existing, just overrride token, remotename and confirmed fields
* @param object $communication
* @return int id of the create communication into the DB
*/
public function add_communication($communication) {
global $DB;
//look for previously deleted communication
$deletedcommunication = $this->get_communication($communication->type,
$communication->remoteentity, $communication->remoteurl, null, 1);
$communication->deleted = 0;
if (!empty($deletedcommunication)) {
$communication->id = $deletedcommunication->id;
$DB->update_record('hub_communications', $communication);
$id = $communication->id;
} else {
$id = $DB->insert_record('hub_communications', $communication);
}
return $id;
}
/**
* Get communication information between the hub and another entity (hub directory, site, public site)
* Mostly use to remember the token given to us by the remote entity,
* or the token that we gave to the remote entity
* @param string $type can be SERVER or CLIENT. SERVER mean that the hub
* is the server into the communication, so the token
* refered, is used by the remote entity to call the a hub function.
* CLIENT mean that the hub use the token to call
* the remote entity function (see the define values top of this file)
* @param string $remoteentity the kind of the remote entity (registered site, hubdirectory, any site - see the define values)
* @param string $remoteurl the token of the remote entity
* @param string $token the token used by this communication
* @param int $deleted set to 1, return only deleted communication
* @return object return the communication
*/
public function get_communication($type, $remoteentity, $remoteurl = null, $token = null, $deleted = 0) {
global $DB;
$params = array('type' => $type,
'remoteentity' => $remoteentity);
if (!empty($remoteurl)) {
$params['remoteurl'] = $remoteurl;
}
if (!empty($token)) {
$params['token'] = $token;
}
$params['deleted'] = $deleted;
$token = $DB->get_record('hub_communications', $params);
return $token;
}
/**
* Delete communication
* @param integer $communicationid
*/
public function delete_communication($communication) {
global $DB;
$communication->deleted = 1;
$DB->update_record('hub_communications', $communication);
}
/**
* Confirm a communication
* When the hub try to register on the hub directory, it first creates
* a token for the hub directory, send it,
* and wait for the hub directory to confirm the communication has been established.
* Then the hub call this function to confirm
* the communication.
* @param object $communication
*/
public function confirm_communication($communication) {
$communication->confirmed = 1;
$this->update_communication($communication);
}
/**
* update a communication object
* @param object $communication
*/
public function update_communication($communication) {
global $DB;
$DB->update_record('hub_communications', $communication);
}
/**
* Add a course content
* @param object $content
*/
public function add_course_content($content) {
global $DB;
$DB->insert_record('hub_course_contents', $content);
}
/**
* Delete all course contents of a course
* @param int $courseid
*/
public function delete_course_contents($courseid) {
global $DB;
$DB->delete_records('hub_course_contents', array('courseid' => $courseid));
}
/**
* Get all course contents of a course
* @param integer $courseid
* @return array of course contents
*/
public function get_course_contents($courseid) {
global $DB;
return $DB->get_records('hub_course_contents',
array('courseid' => $courseid), 'contentcount DESC');
}
/**
* Get all course outcome of a course
* @param integer $courseid
* @return array of course outcomes
*/
public function get_course_outcomes($courseid) {
global $DB;
return $DB->get_records('hub_course_outcomes',
array('courseid' => $courseid));
}
///////////////////////////
/// Library functions ///
///////////////////////////
/**
* Return hub server information
* @return array
*/
public function get_info() {
global $CFG;
$hubinfo = array();
$hubinfo['name'] = get_config('local_hub', 'name');
$hubinfo['description'] = get_config('local_hub', 'description');
$hubinfo['contactname'] = get_config('local_hub', 'contactname');
$hubinfo['contactemail'] = get_config('local_hub', 'contactemail');
$hubinfo['hublogo'] = get_config('local_hub', 'hublogo');
$hubinfo['privacy'] = get_config('local_hub', 'privacy');
$hubinfo['language'] = get_config('local_hub', 'language');
$hubinfo['url'] = $CFG->wwwroot;
$hubinfo['sites'] = $this->get_registered_sites_total();
$hubinfo['courses'] = $this->get_registered_courses_total();
//enrollable course total
$options = array();
$options['onlyvisible'] = true;
$options['downloadable'] = false;
$options['enrollable'] = true;
$enrollablecourses = $this->get_courses($options, 0, 0, true);
$hubinfo['enrollablecourses'] = $enrollablecourses;
//downloadable course total
$options['downloadable'] = true;
$options['enrollable'] = false;
$downloadablecourses = $this->get_courses($options, 0, 0, true);
$hubinfo['downloadablecourses'] = $downloadablecourses;
return $hubinfo;
}
/**
* Retrieve the privacy string matching the define value
* @param string $privacy must match the define into moodlelib.php
* @return string
*/
public function get_privacy_string($privacy) {
switch ($privacy) {
case HUBPRIVATE:
$privacystring = get_string('nosearch', 'local_hub');
break;
case HUBALLOWPUBLICSEARCH:
$privacystring = get_string('allowpublicsearch', 'local_hub');
break;
case HUBALLOWGLOBALSEARCH:
$privacystring = get_string('allowglobalsearch', 'local_hub');
break;
}
if (empty($privacy)) {
throw new moodle_exception('unknownprivacy');
}
return $privacystring;
}
/**
* Unregister a course from the directory
* If the course doesn't exist do nothing - no error thrown
* If the course site id doesn't match the site url, throw an error
* @param int $courseid
* @param string $siteurl
*/
public function unregister_course($courseid, $siteurl) {
global $CFG;
$site = $this->get_site_by_url($siteurl);
$course = $this->get_course($courseid);
//check that the course exist (otherwise unregister)
if (!empty($course)) {
//check that the course match the site
if (empty($site) or ($course->siteid != $site->id)) {
throw new moodle_exception('triedtounregisteracourseforwrongsite');
}
$course->deleted = 1;
$this->update_course($course);
//delete outcomes
$this->update_course_outcomes($courseid, null);
add_to_log(SITEID, 'local_hub', 'course unregistration', '', $course->id);
}
}
/**
* Register a course for a specific site
* @param object $course