forked from moodleou/moodle-mod_openstudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
1076 lines (930 loc) · 37.2 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/>.
/**
* Library of interface functions and constants for module openstudio
*
* All the core Moodle functions, neeeded to allow the module to work
* integrated in Moodle should be placed here.
*
* All the openstudio specific functions, needed to implement all the module
* logic, should go to locallib.php. This will help to save some memory when
* Moodle is performing actions across all modules.
*
* @package mod_openstudio
* @copyright 2016 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_openstudio\local\api\content;
use mod_openstudio\local\api\filesystem;
use mod_openstudio\local\util;
use mod_openstudio\local\util\feature;
use mod_openstudio\local\util\defaults;
use mod_openstudio\local\api\comments;
use mod_openstudio\local\api\levels;
use mod_openstudio\local\api\search;
defined('MOODLE_INTERNAL') || die();
/* Moodle API */
/**
* Returns the information on whether the module supports a feature
*
* See {@link plugin_supports()} for more info.
*
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed true if the feature is supported, null if unknown
*/
function openstudio_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_IDNUMBER:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return false;
case FEATURE_COMPLETION_HAS_RULES:
return false;
case FEATURE_GRADE_HAS_GRADE:
return false;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_RATE:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
default:
return null;
}
}
/**
* Saves a new instance of the openstudio into the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param stdClass $openstudio Submitted data from the form in mod_form.php
* @param mod_openstudio_mod_form $mform The form instance itself (if needed)
* @return int The id of the newly inserted openstudio record
*/
function openstudio_add_instance(stdClass $studio, mod_openstudio_mod_form $mform = null) {
global $DB;
$studio->timemodified = time();
if (isset($studio->enabledvisibility) && is_array($studio->enabledvisibility)) {
$studio->allowedvisibility = implode(",", $studio->enabledvisibility);
}
if (isset($studio->enabledflags) && is_array($studio->enabledflags)) {
$studio->flags = implode(",", $studio->enabledflags);
}
$studio->filetypes = '*';
if (isset($studio->tutorrolesgroup) && is_array($studio->tutorrolesgroup)) {
$tutorroles = array_keys(array_filter($studio->tutorrolesgroup));
$studio->tutorroles = implode(",", $tutorroles);
}
if (isset($studio->enablelocking)) {
$studio->locking = $studio->enablelocking;
}
$studio->themefeatures = openstudio_feature_settings($studio);
return $DB->insert_record('openstudio', $studio);
}
/**
* Updates an instance of the openstudio in the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param stdClass $openstudio An object from the form in mod_form.php
* @param mod_openstudio_mod_form $mform The form instance itself (if needed)
* @return boolean Success/Fail
*/
function openstudio_update_instance(stdClass $studio, mod_openstudio_mod_form $mform = null) {
global $DB;
$studio->timemodified = time();
$studio->id = $studio->instance;
if (isset($studio->enabledvisibility) && is_array($studio->enabledvisibility)) {
$studio->allowedvisibility = implode(",", $studio->enabledvisibility);
}
if (isset($studio->enabledflags) && is_array($studio->enabledflags)) {
$studio->flags = implode(",", $studio->enabledflags);
}
$studio->filetypes = '*';
if (isset($studio->tutorrolesgroup) && is_array($studio->tutorrolesgroup)) {
$tutorroles = array_keys(array_filter($studio->tutorrolesgroup));
$studio->tutorroles = implode(",", $tutorroles);
}
if (isset($studio->enablelocking)) {
$studio->locking = $studio->enablelocking;
}
$studio->themefeatures = openstudio_feature_settings($studio);
return $DB->update_record('openstudio', $studio);
}
/**
* Removes an instance of the openstudio from the database
*
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id Id of the module instance
* @return boolean Success/Failure
*/
function openstudio_delete_instance($id) {
global $CFG, $DB;
if (! $studio = $DB->get_record('openstudio', array('id' => $id))) {
return false;
}
$result = true;
$cm = get_coursemodule_from_id('openstudio', $id);
if ($cm) {
// Delete search indexes.
if (util::search_installed()) {
local_ousearch_document::delete_module_instance_data($cm);
}
// Delete content files from moodle file system.
$modulecontext = context_module::instance($cm->id);
if ($modulecontext) {
filesystem::remove_content_files($modulecontext->id, $studio->id);
}
}
$sql = <<<EOF
DELETE FROM {openstudio_level3} l3
WHERE l3.level2id IN (SELECT l2.id
FROM {openstudio_level2} l2
JOIN {openstudio_level1} l1 ON l1.id = l2.level1id AND l1.openstudioid = ?)
EOF;
if (! $DB->execute($sql, array($studio->id))) {
return false;
}
$sql = <<<EOF
DELETE FROM {openstudio_level2} l2
WHERE l2.level1id IN (SELECT l1.id
FROM {openstudio_level1} l1
WHERE l1.openstudioid = ?)
EOF;
if (! $DB->execute($sql, array($studio->id))) {
return false;
}
if (! $DB->delete_records('openstudio_level1', array('openstudioid' => $studio->id))) {
return false;
}
$sql = <<<EOF
DELETE FROM {openstudio_comments} sc
WHERE sc.contentid IN (SELECT s.id
FROM {openstudio_contents} s
WHERE s.openstudioid = ?)
EOF;
if (! $DB->execute($sql, array($studio->id))) {
return false;
}
$sql = <<<EOF
DELETE FROM {openstudio_flags} sf
WHERE sf.contentid IN (SELECT s.id
FROM {openstudio_contents} s
WHERE s.openstudioid = ?)
EOF;
if (! $DB->execute($sql, array($studio->id))) {
return false;
}
if (! $DB->delete_records('openstudio_honesty_checks', array('openstudioid' => $studio->id))) {
return false;
}
if (! $DB->delete_records('openstudio_subscriptions', array('openstudioid' => $studio->id))) {
return false;
}
$sql = <<<EOF
DELETE FROM {openstudio_tracking} st
WHERE st.contentid IN (SELECT s.id
FROM {openstudio_contents} s
WHERE s.openstudioid = ?)
EOF;
if (! $DB->execute($sql, array($studio->id))) {
return false;
}
$sql = <<<EOF
DELETE FROM {openstudio_content_files} sf
WHERE sf.id IN (SELECT s.fileid
FROM {openstudio_contents} s
WHERE s.openstudioid = ?)
OR sf.id IN (SELECT sv.fileid
FROM {openstudio_content_versions} sv
JOIN {openstudio_contents} s ON s.id = sv.contentid AND s.openstudioid = ?)
EOF;
if (! $DB->execute($sql, array($studio->id, $studio->id))) {
return false;
}
$sql = <<<EOF
DELETE FROM {openstudio_content_versions} sv
WHERE sv.contentid IN (SELECT s.id
FROM {openstudio_contents} s
WHERE s.openstudioid = ?)
EOF;
if (! $DB->execute($sql, array($studio->id))) {
return false;
}
if (! $DB->delete_records('openstudio_contents', array('openstudioid' => $studio->id))) {
return false;
}
if (! $DB->delete_records('openstudio', array('id' => $studio->id))) {
return false;
}
return true;
}
/**
* Returns a small object with summary information about what a
* user has done with a given particular instance of this module
* Used for user activity reports.
*
* $return->time = the time they did it
* $return->info = a short text description
*
* @param stdClass $course The course record
* @param stdClass $user The user record
* @param cm_info|stdClass $mod The course module info object or record
* @param stdClass $openstudio The openstudio instance record
* @return stdClass|null
*/
function openstudio_user_outline($course, $user, $mod, $openstudio) {
$return = new stdClass();
$return->time = 0;
$return->info = '';
return $return;
}
/* File API */
/**
* Serves the files from the openstudio file areas
*
* @package mod_openstudio
* @category files
*
* @param stdClass $course the course object
* @param stdClass $cm the course module object
* @param stdClass $context the openstudio's context
* @param string $filearea the name of the file area
* @param array $args extra arguments (itemid, path)
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return false on failure
*/
function openstudio_pluginfile($course, $cm, $context, $filearea, array $args, $forcedownload, array $options=array()) {
global $USER, $DB;
$extractfile = null;
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
if (! in_array($filearea,
array('content', 'contentthumbnail', 'contentversion', 'contentthumbnailversion',
'contentcomment', 'notebook', 'notebookversion'))) {
return false;
}
require_login($course, false, $cm);
$itemid = (int) array_shift($args);
if (($filearea === 'content') || ($filearea === 'contentthumbnail') || ($filearea == 'notebook')) {
$record = $contentdata = $DB->get_record('openstudio_contents', array('id' => $itemid), '*', MUST_EXIST);
} else if ($filearea === 'contentcomment') {
$sql = <<<EOF
SELECT s.*
FROM {openstudio_contents} s
JOIN {openstudio_comments} sc ON sc.contentid = s.id
WHERE sc.id = ?
EOF;
$record = $contentdata = $DB->get_record_sql($sql, array($itemid));
} else {
$record = $DB->get_record('openstudio_content_versions', array('id' => $itemid), '*', MUST_EXIST);
$contentdata = $DB->get_record('openstudio_contents', array('id' => $record->contentid), '*', MUST_EXIST);
if ($filearea == 'contentversion') {
$filearea = 'content';
}
if ($filearea == 'contentthumbnailversion') {
$filearea = 'contentthumbnail';
}
if ($filearea == 'notebookversion') {
$filearea = 'notebook';
}
}
$visibility = $contentdata->visibility;
if (count($args) > 1) {
// $Args contains 1 item is filename or 2 items are folder id and file name if content is inside foler.
// E.g: [15, small.mp3].
// or ["small.mp3"].
// The filename must end by file name because we must follow media url pattern.
$folderid = (int)(array_slice($args, -2, 1)[0]);
} else {
$folderid = 0;
}
if (is_numeric($folderid)) {
if ($DB->record_exists('openstudio_folder_contents', array('folderid' => $folderid, 'contentid' => $contentdata->id))) {
if ($folder = $DB->get_record('openstudio_contents', array('id' => $folderid))) {
$visibility = $folder->visibility;
}
}
}
// Permission check.
$modulecontext = context_module::instance($cm->id);
if (!has_capability('mod/openstudio:managecontent', $modulecontext)) {
if ($contentdata->userid == $USER->id) {
if (!has_capability('mod/openstudio:view', $modulecontext)) {
return false;
}
} else {
if (!has_capability('mod/openstudio:viewothers', $modulecontext)) {
return false;
}
// If the content is folder to private, then user cant see it.
if ($visibility == content::VISIBILITY_PRIVATE) {
return false;
}
// If the content is shared with the course users, then proceed.
if ($visibility == content::VISIBILITY_MODULE) {
$sql = <<<EOF
SELECT ue1.id
FROM {user_enrolments} ue1
JOIN {enrol} e1 ON e1.id = ue1.enrolid
WHERE ue1.userid = ?
AND e1.courseid IN (SELECT e2.courseid
FROM {enrol} e2
JOIN {user_enrolments} ue2 ON ue2.enrolid = e2.id AND ue2.userid = ?
WHERE e2.courseid = ?)
EOF;
if (!$DB->record_exists_sql($sql, array($USER->id, $contentdata->userid, $course->id))) {
return false;
}
}
if (($visibility == content::VISIBILITY_GROUP) || ($visibility < 0)) {
$coursecontext = context_course::instance($course->id);
$canaccessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext);
if (!$canaccessallgroups) {
if (($cm->groupmode == 2) || $visibility == content::VISIBILITY_GROUP) {
// If the content is shared with the user's course group,
// then check group membership to decide whether to proceed.
//
// If groupmode is all visible groups, then the check is as long as they
// are within the same group grouping id as the content owner.
$sql = <<<EOF
SELECT DISTINCT gm1.groupid
FROM {groups_members} gm1
JOIN {groupings_groups} gg ON gg.groupid = gm1.groupid
JOIN {course_modules} cm ON cm.groupingid = gg.groupingid
JOIN {openstudio_contents} s ON s.openstudioid = cm.instance
WHERE gm1.userid = ?
AND s.id = ?
EOF;
if (!$DB->record_exists_sql($sql, array($USER->id, $contentdata->id))) {
return false;
}
} else if ($visibility < 0) {
// If the content is shared with a specific group,
// then check group membership to decide whether to proceed.
$groupid = 0 - $visibility;
$sql = <<<EOF
SELECT DISTINCT gm1.groupid
FROM {groups_members} gm1
JOIN {groups_members} gm2 ON gm2.groupid = gm1.groupid AND gm2.userid = ?
WHERE gm1.groupid = ?
AND gm1.userid = ?
EOF;
if (!$DB->record_exists_sql($sql, array($USER->id, $groupid, $contentdata->userid))) {
return false;
}
}
}
}
}
}
if ($filearea === 'contentcomment') {
$relativepath = array_pop($args);
$fullpath = "/{$context->id}/mod_openstudio/$filearea/{$itemid}/$relativepath";
} else {
switch ($record->contenttype) {
case content::TYPE_IMAGE:
$sizecheck = optional_param('sizecheck', 0, PARAM_INT);
if ($sizecheck == 1) {
/*
* Request has been made to check the image file size
* and if it is greater than defaults::CONTENTVIEWIMAGESIZELIMIT
* then we force the use of thumbnail size image.
*
*/
$filerecord = $DB->get_record('files',
array('component' => 'mod_openstudio',
'filearea' => 'content',
'itemid' => $record->fileid,
'filename' => $record->content),
'filesize', MUST_EXIST);
if ($filerecord->filesize > defaults::CONTENTVIEWIMAGESIZELIMIT) {
$filearea = 'contentthumbnail';
}
}
break;
case content::TYPE_VIDEO:
case content::TYPE_AUDIO:
case content::TYPE_DOCUMENT:
if ($filearea == 'notebook') {
$filename = 'openstudio_' . $record->id . '_notebook.' . pathinfo(implode('/', $args), PATHINFO_EXTENSION);
$options['filename'] = $filename;
}
case content::TYPE_PRESENTATION:
case content::TYPE_SPREADSHEET:
case content::TYPE_ZIP:
break;
default:
return false;
}
$relativepath = array_pop($args);
$fullpath = "/{$context->id}/mod_openstudio/$filearea/{$record->fileid}/$relativepath";
}
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, (bool) $forcedownload, $options);
}
/**
* Obtains a search document given the ousearch parameters.
*
* @param object $document Object containing fields from the ousearch documents table
* @return mixed False if object can't be found, otherwise object containing the following
* fields: ->content, ->title, ->url, ->activityname, ->activityurl,
* and optionally ->extrastrings array and ->data
*/
function openstudio_ousearch_get_document($document) {
global $CFG, $USER;
$slotid = $document->intref1;
$slot = content::get_record($USER->id, $slotid);
if ($slot == false) {
return false;
}
// Note: for quoted search term to work, the content fed to the indexered
// must be the same when the search engine calls this function. So the below
// construction of title and name is the same as found in the search->update()
// function call.
//
// This is becase when quoted search term is used, the search index is not enough
// and so it calls upon the original text string below to match the quoted text term order.
//
// Various slot metadata is concatenated into the search document content field
// so it can be found in a search text query.
// Set search document title.
$title = stripslashes($slot->name);
$content = $slot->l1name . ' . ';
$content .= $slot->l2name . ' . ';
$content .= $slot->l3name . ' . ';
$content .= $slot->name . ' . ';
$content .= $slot->description . ' . ';
$content .= $slot->firstname . ' . ';
$content .= $slot->lastname . ' . ';
$content .= $slot->username . ' . ';
$content .= $slot->urltitle . ' . ';
$content .= $slot->imagealt . ' . ';
$content .= $slot->thumbnail . ' . ';
$content .= $slot->mimetype . ' . ';
$content .= $slot->content . ' . ';
// Add in tags data.
foreach ($slot->tagsraw as $slottag) {
// Store tag as is, so it can be found.
$content .= $slottag->name . ' . ';
// Store tag with prefix as is, so it can be found exactly if required.
$content .= 'tag:' . str_replace(' ', '', $slottag->name) . ' . ';
}
// Add in comments.
// To prevenr performance issue, we will only index the latest 25 comments.
$commentsdata = comments::get_for_content($slot->id, null, 25);
if ($commentsdata != false) {
$content .= ' . ';
foreach ($commentsdata as $comment) {
$content .= ' . ' . $comment->commenttext;
}
}
$content = stripslashes($content);
$result = new stdClass();
$result->title = $title;
$result->content = $content;
$result->url = (new moodle_url('/mod/openstudio/content.php',
array('id' => $document->coursemoduleid, 'sid' => $slot->id))) . '';
$result->activityname = $result->title;
$result->activityurl = $result->url;
return $result;
}
/**
* This is a helper function to call studio_ousearch_filter_permission() exlictly
* requesting slots in sets to be included in search result.
*
* @param object $result Search result to check whether it should be filtered out.
* @return bool Returns true (acccept) or false (reject).
*/
function openstudio_ousearch_filter_permission_include_setslots($result) {
return openstudio_ousearch_filter_permission($result, true);
}
/**
* Sets a filter function which can run on results (after they have been
* obtained) to exclude unwanted ones or make other changes.
*
* The filter below does a permission tocheck to make sure the logged in user
* does have permission to view a given slot.
*
* @param object $result Search result to check whether it should be filtered out.
* @param bool $includeinsetslots Set to true to include slots in set in the search result.
* @return bool Returns true (acccept) or false (reject).
*/
function openstudio_ousearch_filter_permission($result, $includeinsetslots = false) {
global $USER, $COURSE, $DB;
// Check view context to filter by.
$vid = content::VISIBILITY_MODULE;
if (util::cache_check('search_view_context')) {
$vid = util::cache_get('search_view_context');
}
// Check groupmode to filter by.
$groupmode = 0;
if (util::cache_check('search_view_groupmode')) {
$groupmode = util::cache_get('search_view_groupmode');
}
// Check capability to see I can view other people's slots.
if (util::cache_check('search_viewothers_capability')) {
if (!util::cache_get('search_viewothers_capability')) {
return false;
}
}
if ($vid == content::VISIBILITY_PRIVATE_PINBOARD) {
$sql = <<<EOF
SELECT DISTINCT s.id
FROM {openstudio_contents} s
WHERE s.id = ?
AND s.userid = ?
AND s.levelid = 0
AND s.levelcontainer = 0
EOF;
if ($includeinsetslots) {
return $DB->record_exists_sql($sql, array($result->intref1, $USER->id));
} else {
$sql .= ' AND s.visibility != ?';
return $DB->record_exists_sql($sql, array($result->intref1, $USER->id, content::VISIBILITY_INFOLDERONLY));
}
}
if ($vid == content::VISIBILITY_PRIVATE) {
$sql = <<<EOF
SELECT DISTINCT s.id
FROM {openstudio_contents} s
WHERE s.id = ?
AND s.userid = ?
AND s.levelid > 0
AND s.levelcontainer > 0
EOF;
if ($includeinsetslots) {
return $DB->record_exists_sql($sql, array($result->intref1, $USER->id));
} else {
$sql .= ' AND s.visibility != ?';
return $DB->record_exists_sql($sql, array($result->intref1, $USER->id, content::VISIBILITY_INFOLDERONLY));
}
}
// If the slot is shared with the user's course group,
// then check group membership to decide whether to show search result.
if (($result->intref2 == content::VISIBILITY_GROUP) || ($result->intref2 < 0)) {
$coursecontext = context_course::instance($COURSE->id);
$canaccessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext);
if ($canaccessallgroups) {
return true;
}
if (($groupmode == 2) || ($result->intref2 == content::VISIBILITY_GROUP)) {
$sql = <<<EOF
SELECT DISTINCT gm1.groupid
FROM {groups_members} gm1
JOIN {groups_members} gm2 ON gm2.groupid = gm1.groupid AND gm2.userid = ?
JOIN {groupings_groups} gg ON gg.groupid = gm2.groupid
JOIN {course_modules} cm ON cm.groupingid = gg.groupingid
JOIN {openstudio_contents} s ON s.studioid = cm.instance
WHERE gm1.userid = ?
AND s.id = ?
EOF;
if ($includeinsetslots) {
return $DB->record_exists_sql($sql, array($result->userid, $USER->id, $result->intref1));
} else {
$sql .= ' AND s.visibility != ?';
return $DB->record_exists_sql($sql, array(
$result->userid, $USER->id, $result->intref1, content::VISIBILITY_INFOLDERONLY));
}
} else {
if (!$includeinsetslots) {
$slotdata = content::get($result->intref1);
if ($slotdata->visibility == content::VISIBILITY_INFOLDERONLY) {
return false;
}
}
$groupid = 0 - $result->intref2;
$sql = <<<EOF
SELECT DISTINCT gm1.groupid
FROM {groups_members} gm1
JOIN {groups_members} gm2 ON gm2.groupid = gm1.groupid AND gm2.userid = ?
WHERE gm1.groupid = ?
AND gm1.userid = ?
EOF;
return $DB->record_exists_sql($sql, array($USER->id, $groupid, $result->userid));
}
}
if (!$includeinsetslots) {
$slotdata = content::get($result->intref1);
if ($slotdata->visibility == content::VISIBILITY_INFOLDERONLY) {
return false;
}
}
// If the slot belongs to me, then show search result.
if ($result->userid == $USER->id) {
return true;
}
// If the slot is shared with the course users, then show search result.
// Note: assumption is that the search results are already retricted by
// course module.
if ($result->intref2 == content::VISIBILITY_MODULE) {
$sql = <<<EOF
SELECT ue1.id
FROM {user_enrolments} ue1
JOIN {enrol} e1 ON e1.id = ue1.enrolid
WHERE ue1.userid = ?
AND e1.courseid IN (SELECT e2.courseid
FROM {enrol} e2
JOIN {user_enrolments} ue2 ON ue2.enrolid = e2.id AND ue2.userid = ?
WHERE e2.courseid = ?)
EOF;
if ($vid == content::VISIBILITY_GROUP) {
$sql .= <<<EOF
AND EXISTS (SELECT DISTINCT gm1.groupid
FROM {groups_members} gm1
JOIN {groups_members} gm2 ON gm2.groupid = gm1.groupid AND gm2.userid = ?
JOIN {groupings_groups} gg ON gg.groupid = gm2.groupid
JOIN {course_modules} cm ON cm.groupingid = gg.groupingid
JOIN {openstudio_contents} s ON s.studioid = cm.instance
WHERE gm1.userid = ?
AND s.id = ?)
EOF;
return $DB->record_exists_sql($sql,
array($USER->id, $result->userid, $COURSE->id, $result->userid, $USER->id, $result->intref1));
} else {
return $DB->record_exists_sql($sql, array($USER->id, $result->userid, $COURSE->id));
}
}
if ($result->intref2 == content::VISIBILITY_TUTOR) {
$cm = get_coursemodule_from_id('openstudio', $result->coursemoduleid);
$cminstance = $DB->get_record('openstudio', array('id' => $cm->instance), '*', MUST_EXIST);
$tutorroles = array_filter(explode(',', $cminstance->tutorroles));
return content::user_is_tutor($result->intref1, $USER->id, $tutorroles);
}
return false;
}
function openstudio_ousearch_filter_browseslots(&$result) {
if (openstudio_ousearch_filter_permission($result, true)) {
$result = content::get($result->intref1);
if ($result->contenttype == content::TYPE_FOLDER) {
return false;
}
return true;
} else {
return false;
}
}
function openstudio_ousearch_filter_browseslots_useronly(&$result) {
global $USER;
if ($result->userid == $USER->id) {
return openstudio_ousearch_filter_browseslots($result);
} else {
return false;
}
}
/**
* Update all documents for ousearch.
*
* @param bool $feedback If true, prints feedback as HTML list items
* @param int $courseid If specified, restricts to particular courseid
*/
function openstudio_ousearch_update_all($feedback = true, $courseid = 0) {
global $DB;
if (get_config('local_ousearch', 'ousearchindexingdisabled')) {
// Do nothing if the OU Search system is turned off.
return;
}
raise_memory_limit(MEMORY_EXTRA);
if ($courseid > 0) {
$sql = <<<EOF
SELECT cm.*
FROM {modules} m
JOIN {course_modules} cm ON cm.module = m.id
WHERE m.name = 'openstudio'
AND cm.course = ?
EOF;
$cms = $DB->get_records_sql($sql, array($courseid));
if (empty($cms)) {
return false;
}
foreach ($cms as $cm) {
$slots = content::get_all_records($cm->instance);
if ($slots != false) {
$counter = 0;
foreach ($slots as $slotdata) {
$counter++;
if ($slotdata->contenttype == content::TYPE_NONE) {
search::delete($cm, $slotdata, true);
} else {
search::update($cm, $slotdata);
}
}
if ($feedback) {
print '<li>Studio instance: ' . $cm->instance
. '. No. of slots: ' . $counter . '</li>';
}
}
}
} else {
$sql = <<<EOF
SELECT cm.*
FROM {modules} m
JOIN {course_modules} cm ON cm.module = m.id
WHERE m.name = 'openstudio'
EOF;
$cms = $DB->get_recordset_sql($sql, array($courseid));
if (!$cms->valid()) {
return false;
}
foreach ($cms as $cm) {
$slots = content::get_all_records($cm->instance);
if ($slots != false) {
$counter = 0;
foreach ($slots as $slotdata) {
$counter++;
if ($slotdata->contenttype == content::TYPE_NONE) {
search::delete($cm, $slotdata, true);
} else {
search::update($cm, $slotdata);
}
}
if ($feedback) {
print '<li>Studio instance: ' . $cm->instance
. '. No. of slots: ' . $counter . '</li>';
}
}
}
}
}
/**
* Returns the features enabled for the Studio instance. Given this value changes and may not
* be refelected in the database, the optional fflag $updatedb exists to save the calculated
* feature to database.
*
* @param mixed $studioorid The studio object or id that needs to be checked for features.
* @param bool $updatedb Set to true if the feature calculation should be stored in the database.
* @return int Return a bitmask value of what features are enabled.
*/
function openstudio_feature_settings($studioorid, $updatedb = false) {
global $CFG, $DB;
if (is_object($studioorid)) {
$studio = $studioorid;
} else {
$studio = $DB->get_record('openstudio', array('id' => $studioorid));
$module = $DB->get_record('modules', array('name' => 'openstudio'));
$studiomodule = $DB->get_record('course_modules',
array('course' => $studio->course, 'module' => $module->id, 'instance' => $studioorid));
$studio->enablemodule = $studio->themefeatures & feature::MODULE;
$studio->groupmode = $studiomodule->groupmode;
$studio->groupingid = $studiomodule->groupingid;
$studio->pinboard = $studio->themefeatures & feature::PINBOARD;
$studio->enablecontenthtml = 1;
$studio->enablecontentcommenthtml = 1;
$studio->enablecontentcommentaudio = $studio->themefeatures & feature::CONTENTCOMMENTUSESAUDIO;
$studio->enablecontentusesfileupload = $studio->themefeatures & feature::CONTENTUSESFILEUPLOAD;
if ($studio->themefeatures & feature::ENABLEFOLDERS) {
$studio->enablefolders = 1;
} else {
$studio->enablefolders = 0;
}
$studio->enablefoldersanycontent = $studio->themefeatures & feature::ENABLEFOLDERSANYCONTENT;
$studio->enablerss = $studio->themefeatures & feature::ENABLERSS;
$studio->enablesubscription = $studio->themefeatures & feature::ENABLESUBSCRIPTION;
$studio->enableexportimport = $studio->themefeatures & feature::ENABLEEXPORTIMPORT;
$studio->enablecontentusesweblink = $studio->themefeatures & feature::CONTENTUSESWEBLINK;
$studio->enablecontentusesembedcode = $studio->themefeatures & feature::CONTENTUSESEMBEDCODE;
$studio->enablecontentallownotebooks = $studio->themefeatures & feature::CONTENTALLOWNOTEBOOKS;
$studio->enablecontentreciprocalaccess = util::has_feature(
$studio, feature::CONTENTRECIPROCALACCESS
);
$studio->enableparticipationsmiley = $studio->themefeatures & feature::PARTICIPATIONSMILEY;
$studio->enablelocking = $studio->themefeatures & feature::ENABLELOCK;
$studio->allowlatesubmissions = $studio->themefeatures & feature::LATESUBMISSIONS;
}
$featuremodule = ($studio->enablemodule > 0) ? feature::MODULE : 0;
$featuregroup = 0;
if ($studio->groupmode && $studio->groupingid) {
$featuregroup = feature::GROUP;
}
$featurestudio = 0;
if (isset($studio->id) && levels::defined_for_studio($studio->id)) {
$featurestudio = feature::STUDIO;
}
$featurepinboard = 0;
if ($studio->pinboard) {
$featurepinboard = feature::PINBOARD;
}
$featurecontenttextuseshtml = feature::CONTENTTEXTUSESHTML;
$featurecontentcommentuseshtml = feature::CONTENCOMMENTUSESHTML;
$featurecontentcommentusesaudio = 0;
if ($studio->enablecontentcommentaudio) {
$featurecontentcommentusesaudio = feature::CONTENTCOMMENTUSESAUDIO;
}
$featurecontentusesfileupload = 0;
if ($studio->enablecontentusesfileupload) {
$featurecontentusesfileupload = feature::CONTENTUSESFILEUPLOAD;
}
$featureenablefolders = 0;
$featureenablefoldersanycontent = 0;
if ($studio->enablefolders == 1) {
$featureenablefolders = feature::ENABLEFOLDERS;
if ($studio->enablefoldersanycontent) {
$featureenablefoldersanycontent = feature::ENABLEFOLDERSANYCONTENT;
}
}
$featurecontentallownotebooks = 0;
if ($studio->enablecontentallownotebooks) {
$featurecontentallownotebooks = feature::CONTENTALLOWNOTEBOOKS;
}
$featurecontentreciprocalaccess = 0;
if ($studio->enablecontentreciprocalaccess) {
$featurecontentreciprocalaccess = feature::CONTENTRECIPROCALACCESS;
}