-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlib.php
1663 lines (1409 loc) · 57 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 pcast
*
* All the core Moodle functions, neeeded to allow the module to work
* integrated in Moodle should be placed here.
* All the pcast 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_pcast
* @copyright 2010 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/completionlib.php');
define("PCAST_SHOW_ALL_CATEGORIES", 0);
define("PCAST_SHOW_NOT_CATEGORISED", -1);
define("PCAST_NO_VIEW", -1);
define("PCAST_STANDARD_VIEW", 0);
define("PCAST_CATEGORY_VIEW", 1);
define("PCAST_DATE_VIEW", 2);
define("PCAST_AUTHOR_VIEW", 3);
define("PCAST_ADDENTRY_VIEW", 4);
define("PCAST_APPROVAL_VIEW", 5);
define("PCAST_ENTRIES_PER_PAGE", 20);
define("PCAST_DATE_UPDATED", 100);
define("PCAST_DATE_CREATED", 101);
define("PCAST_AUTHOR_LNAME", 200);
define("PCAST_AUTHOR_FNAME", 201);
define("PCAST_EPISODE_VIEW", 300);
define("PCAST_EPISODE_COMMENT_AND_RATE", 301);
define("PCAST_EPISODE_VIEWS", 302);
define("PCAST_EPISODE_APPROVE", 1);
define("PCAST_EPISODE_DISAPPROVE", 0);
/**
* If you for some reason need to use global variables instead of constants, do not forget to make them
* global as this file can be included inside a function scope. However, using the global variables
* at the module level is not a recommended.
*/
/**
* Lists supported features
*
* @uses FEATURE_GROUPS
* @uses FEATURE_GROUPINGS
* @uses FEATURE_GROUPMEMBERSONLY
* @uses FEATURE_MOD_INTRO
* @uses FEATURE_SHOW_DESCRIPTION
* @uses FEATURE_COMPLETION_TRACKS_VIEWS
* @uses FEATURE_GRADE_HAS_GRADE
* @uses FEATURE_GRADE_OUTCOMES
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, null if doesn't know
**/
function pcast_supports($feature) {
switch($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_RATE:
return true;
case FEATURE_MOD_PURPOSE:
return MOD_PURPOSE_COLLABORATION;
default:
return null;
}
}
/**
* 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 $pcast An object from the form in mod_form.php
* @return int The id of the newly inserted pcast record
*/
function pcast_add_instance($pcast) {
global $DB, $USER;
$pcast->timecreated = time();
// If it is a new instance time created is the same as modified.
$pcast->timemodified = $pcast->timecreated;
// Handle ratings.
if (empty($pcast->assessed)) {
$pcast->assessed = 0;
}
if (empty($pcast->ratingtime) || empty($pcast->assessed)) {
$pcast->assesstimestart = 0;
$pcast->assesstimefinish = 0;
}
// If no owner then set it to the instance creator.
if (isset($pcast->enablerssitunes) && ($pcast->enablerssitunes == 1)) {
if (!isset($pcast->userid)) {
$pcast->userid = $USER->id;
}
}
// Get the episode category information.
$defaults = new stdClass();
$defaults->topcategory = 0;
$defaults->nestedcategory = 0;
$pcast = pcast_get_itunes_categories($pcast, $defaults);
$result = $DB->insert_record('pcast', $pcast);
$pcast->id = $result;
$cmid = $pcast->coursemodule;
$draftitemid = $pcast->image;
// We need to use context now, so we need to make sure all needed info is already in db.
$context = context_module::instance($cmid);
if ($draftitemid) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_pcast', 'logo', 0, array('subdirs' => false));
}
pcast_grade_item_update($pcast);
// Add action event for dashboard.
$completiontimeexpected = !empty($pcast->completionexpected) ? $pcast->completionexpected : null;
\core_completion\api::update_completion_date_event($pcast->coursemodule,
'pcast', $pcast->id, $completiontimeexpected);
return $result;
}
/**
* 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 $pcast An object from the form in mod_form.php
* @return boolean Success/Fail
*/
function pcast_update_instance($pcast) {
global $DB, $USER;
$pcast->timemodified = time();
// Handle ratings.
if (empty($pcast->assessed)) {
$pcast->assessed = 0;
}
if (empty($pcast->ratingtime) || empty($pcast->assessed)) {
$pcast->assesstimestart = 0;
$pcast->assesstimefinish = 0;
}
$pcast->id = $pcast->instance;
// If no owner then set it to the instance creator.
if (isset($pcast->enablerssitunes) && ($pcast->enablerssitunes == 1)) {
if (!isset($pcast->userid)) {
$pcast->userid = $USER->id;
}
}
// Get the episode category information.
$defaults = new stdClass();
$defaults->topcategory = 0;
$defaults->nestedcategory = 0;
$pcast = pcast_get_itunes_categories($pcast, $defaults);
$result = $DB->update_record('pcast', $pcast);
$cmid = $pcast->coursemodule;
$draftitemid = $pcast->image;
// We need to use context now, so we need to make sure all needed info is already in db.
$context = context_module::instance($cmid);
if ($draftitemid) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_pcast', 'logo', 0, array('subdirs' => false));
}
pcast_grade_item_update($pcast);
// Update action event for dashboard.
$completiontimeexpected = !empty($pcast->completionexpected) ? $pcast->completionexpected : null;
\core_completion\api::update_completion_date_event($pcast->coursemodule,
'pcast', $pcast->id, $completiontimeexpected);
return $result;
}
/**
* 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 pcast_delete_instance($id) {
global $DB, $CFG;
require_once($CFG->dirroot . '/rating/lib.php');
if (! $pcast = $DB->get_record('pcast', array('id' => $id))) {
return false;
}
if (!$cm = get_coursemodule_from_instance('pcast', $id)) {
return false;
}
if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) {
return false;
}
// Delete any dependent records here.
// Delete Comments.
$episodeselect = "SELECT id FROM {pcast_episodes} WHERE pcastid = ?";
$DB->delete_records_select('comments', "contextid=? AND commentarea=? AND itemid IN ($episodeselect)",
array($id, 'pcast_episode', $context->id));
// Delete Tags.
core_tag_tag::delete_instances('mod_pcast', 'pcast_episodes', $context->id);
// Delete all files.
$fs = get_file_storage();
$fs->delete_area_files($context->id);
// Delete ratings.
$rm = new rating_manager();
$ratingdeloptions = new stdClass();
$ratingdeloptions->contextid = $context->id;
$rm->delete_ratings($ratingdeloptions);
// Delete Views.
$episodeselect = "SELECT id FROM {pcast_episodes} WHERE pcastid = ?";
$DB->delete_records_select('pcast_views', "episodeid IN ($episodeselect)", array($pcast->id));
// Delete Episodes.
$DB->delete_records('pcast_episodes', array('pcastid' => $pcast->id));
// Delete Grades.
pcast_grade_item_delete($pcast);
// Delete action events.
\core_completion\api::update_completion_date_event($cm->id, 'pcast', $pcast->id, null);
// Delete Podcast.
$DB->delete_records('pcast', array('id' => $pcast->id));
return true;
}
/**
* Return 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
* @param stdClass $user
* @param stdClass $mod
* @param stdClass $pcast
* @return object $result
*/
function pcast_user_outline($course, $user, $mod, $pcast) {
global $CFG;
require_once("$CFG->libdir/gradelib.php");
$grades = grade_get_grades($course->id, 'mod', 'pcast', $pcast->id, $user->id);
if (empty($grades->items[0]->grades)) {
$grade = false;
} else {
$grade = reset($grades->items[0]->grades);
}
if ($entries = pcast_get_user_episodes($pcast->id, $user->id)) {
$result = new stdClass();
$result->info = get_string("episodes", "pcast", count($entries));
$lastentry = array_pop($entries);
$result->time = $lastentry->timemodified;
if ($grade) {
$result->info .= ', ' . get_string('grade') . ': ' . $grade->str_long_grade;
}
return $result;
} else if ($grade) {
$result = new stdClass();
$result->info = get_string('grade') . ': ' . $grade->str_long_grade;
// Datesubmitted == time created. dategraded == time modified or time overridden.
// If grade was last modified by the user themselves use date graded. Otherwise use date submitted.
// TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704.
if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
$result->time = $grade->dategraded;
} else {
$result->time = $grade->datesubmitted;
}
return $result;
}
return null;
}
/**
* Get all the episodes for a user in a podcast.
* @param int $pcastid
* @param int $userid
* @return array
*/
function pcast_get_user_episodes($pcastid, $userid) {
global $DB;
$userfieldsapi = \core_user\fields::for_name();
$allnamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT p.id AS id,
p.pcastid AS pcastid,
p.course AS course,
p.userid AS userid,
p.name AS name,
p.summary AS summary,
p.summaryformat AS summaryformat,
p.summarytrust AS summarytrust,
p.mediafile AS mediafile,
p.duration AS duration,
p.explicit AS explicit,
p.subtitle AS subtitle,
p.keywords AS keywords,
p.topcategory as topcatid,
p.nestedcategory as nestedcatid,
p.timecreated as timecreated,
p.timemodified as timemodified,
p.approved as approved,
p.sequencenumber as sequencenumber,
pcast.userscancomment as userscancomment,
pcast.userscancategorize as userscancategorize,
pcast.userscanpost as userscanpost,
pcast.requireapproval as requireapproval,
pcast.displayauthor as displayauthor,
pcast.displayviews as displayviews,
pcast.assessed as assessed,
pcast.assesstimestart as assesstimestart,
pcast.assesstimefinish as assesstimefinish,
pcast.scale as scale,
cat.name as topcategory,
ncat.name as nestedcategory,
$allnamefields
FROM {pcast_episodes} p
LEFT JOIN {pcast} AS pcast ON
p.pcastid = pcast.id
LEFT JOIN {user} AS u ON
p.userid = u.id
LEFT JOIN {pcast_itunes_categories} AS cat ON
p.topcategory = cat.id
LEFT JOIN {pcast_itunes_nested_cat} AS ncat ON
p.nestedcategory = ncat.id
WHERE pcast.id = ?
AND p.pcastid = pcast.id
AND p.userid = ?
AND p.userid = u.id
ORDER BY p.timemodified ASC";
return $DB->get_records_sql($sql, array($pcastid, $userid));
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @param stdClass $course
* @param stdClass $user
* @param stdClass $mod
* @param stdClass $pcast
* @return object $result
*/
function pcast_user_complete($course, $user, $mod, $pcast) {
global $CFG, $OUTPUT;
require_once("$CFG->libdir/gradelib.php");
require_once($CFG->dirroot.'/mod/pcast/locallib.php');
$cm = get_coursemodule_from_instance("pcast", $pcast->id, $course->id);
$grades = grade_get_grades($course->id, 'mod', 'pcast', $pcast->id, $user->id);
if (!empty($grades->items[0]->grades)) {
$grade = reset($grades->items[0]->grades);
echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
if ($grade->str_feedback) {
echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
}
}
if ($episodes = pcast_get_user_episodes($pcast->id, $user->id)) {
foreach ($episodes as $episode) {
pcast_display_episode_brief($episode, $cm, false, false);
}
} else {
// Not contributed to.
echo get_string('noepisodesposted', 'pcast');
}
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in pcast activities and print it out.
* Return true if there was output, or false is there was none.
*
* @param stdClass $course
* @param bool $viewfullnames
* @param int $timestart
* @return bool
*/
function pcast_print_recent_activity($course, $viewfullnames, $timestart) {
global $DB, $OUTPUT;
$modinfo = get_fast_modinfo($course);
$ids = array();
foreach ($modinfo->cms as $cm) {
if ($cm->modname != 'pcast') {
continue;
}
if (!$cm->uservisible) {
continue;
}
$ids[$cm->instance] = $cm->instance;
}
if (!$ids) {
return false;
}
$plist = implode(',', $ids); // There should not be hundreds of podcasts in one course, right?
$userfieldsapi = \core_user\fields::for_name();
$allnamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
if (!$episodes = $DB->get_records_sql("SELECT e.id, e.name, e.approved, e.timemodified, e.pcastid,
e.userid, $allnamefields
FROM {pcast_episodes} e
JOIN {user} u ON u.id = e.userid
WHERE e.pcastid IN ($plist) AND e.timemodified > ?
ORDER BY e.timemodified ASC", array($timestart))) {
return false;
}
$editor = array();
foreach ($episodes as $episodeid => $episode) {
if ($episode->approved) {
continue;
}
if (!isset($editor[$episode->pcastid])) {
$editor[$episode->pcastid] = has_capability('mod/pcast:approve',
context_module::instance($modinfo->instances['pcast'][$episode->pcastid]->id));
}
if (!$editor[$episode->pcastid]) {
unset($episodes[$episodeid]);
}
}
if (!$episodes) {
return false;
}
echo $OUTPUT->heading(get_string('newepisodes', 'pcast').':', 3);
$strftimerecent = get_string('strftimerecent');
foreach ($episodes as $episode) {
$link = new moodle_url('/mod/pcast/showepisode.php', array('eid' => $episode->id));
if ($episode->approved) {
$out = html_writer::start_tag('div', array('class' => 'head')). "\n";
} else {
$out = html_writer::start_tag('div', array('class' => 'head dimmed_text')). "\n";
}
$out .= html_writer::start_tag('div', array('class' => 'date')). "\n";
$out .= userdate($episode->timemodified, $strftimerecent);
$out .= html_writer::end_tag('div') . "\n";
$out .= html_writer::start_tag('div', array('class' => 'name')). "\n";
$out .= fullname($episode, $viewfullnames);
$out .= html_writer::end_tag('div') . "\n";
$out .= html_writer::end_tag('div') . "\n";
$out .= html_writer::start_tag('div', array('class' => 'info')). "\n";
$out .= html_writer::tag('a', format_text($episode->name, true), array('href' => $link));
$out .= html_writer::end_tag('div') . "\n";
echo $out;
}
return true;
}
/**
* Function to be run periodically according to the moodle cron
* This function searches for things that need to be done, such
* as sending out mail, toggling flags etc ...
*
* @return boolean
**/
function pcast_cron () {
return true;
}
/**
* This function returns if a scale is being used by one pcast
* if it has support for grading and scales. Commented code should be
* modified if necessary. See forum, glossary or journal modules
* as reference.
*
* @param int $pcastid ID of an instance of this module
* @param int $scaleid
* @return mixed
*/
function pcast_scale_used($pcastid, $scaleid) {
global $DB;
$return = false;
$rec = $DB->get_record("pcast", array("id" => "$pcastid", "scale" => "-$scaleid"));
if (!empty($rec) && !empty($scaleid)) {
$return = true;
}
return $return;
}
/**
* Checks if scale is being used by any instance of pcast.
* This function was added in 1.9
*
* This is used to find out if scale used anywhere
* @param int $scaleid
* @return boolean True if the scale is used by any pcast
*/
function pcast_scale_used_anywhere($scaleid) {
global $DB;
if ($scaleid && $DB->record_exists('pcast', array('scale' => -$scaleid))) {
return true;
} else {
return false;
}
}
/**
* Lists all browsable file areas
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @return array
*/
function pcast_get_file_areas($course, $cm, $context) {
$areas = array();
$areas['logo'] = get_string('arealogo', 'pcast');
$areas['episode'] = get_string('areaepisode', 'pcast');
$areas['summary'] = get_string('areasummary', 'pcast');
return $areas;
}
/**
* Support for the Reports (Participants)
* @return array()
*/
function pcast_get_view_actions() {
return array('view', 'view all', 'get attachment');
}
/**
* Support for the Reports (Participants)
* @return array()
*/
function pcast_get_post_actions() {
return array('add', 'update');
}
/**
* Tells if files in moddata are trusted and can be served without XSS protection.
*
* @return bool (true if file can be submitted by teacher only, otherwise false)
*/
function pcast_is_moddata_trusted() {
return false;
}
/**
* Obtains the automatic completion state for this pcast based on any conditions
* in pcast settings.
*
* @param object $course Course
* @param object $cm Course-module
* @param int $userid User ID
* @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
* @return bool True if completed, false if not. (If no conditions, then return
* value depends on comparison type)
*/
function pcast_get_completion_state($course, $cm, $userid, $type) {
global $DB;
// Get pcast details.
if (!($pcast = $DB->get_record('pcast', array('id' => $cm->instance)))) {
throw new Exception("Can't find podcast {$cm->instance}");
}
// Default return value.
$result = $type;
if ($pcast->completionepisodes) {
$value = $pcast->completionepisodes <= $DB->count_records('pcast_episodes',
array('pcastid' => $pcast->id, 'userid' => $userid, 'approved' => PCAST_EPISODE_APPROVE));
if ($type == COMPLETION_AND) {
$result = $result && $value;
} else {
$result = $result || $value;
}
}
return $result;
}
/**
* Adds module specific settings to the navigation block
* @param stdClass $navigation
* @param stdClass $course
* @param stdClass $module
* @param stdClass $cm
*/
function pcast_extend_navigation($navigation, $course, $module, $cm) {
$navigation->add(get_string('standardview', 'pcast'),
new moodle_url('/mod/pcast/view.php', array('id' => $cm->id, 'mode' => PCAST_STANDARD_VIEW)));
if ($module->userscancategorize) {
$navigation->add(get_string('categoryview', 'pcast'),
new moodle_url('/mod/pcast/view.php', array('id' => $cm->id, 'mode' => PCAST_CATEGORY_VIEW)));
}
$navigation->add(get_string('dateview', 'pcast'),
new moodle_url('/mod/pcast/view.php', array('id' => $cm->id, 'mode' => PCAST_DATE_VIEW)));
$navigation->add(get_string('authorview', 'pcast'),
new moodle_url('/mod/pcast/view.php', array('id' => $cm->id, 'mode' => PCAST_AUTHOR_VIEW)));
}
/**
* Adds module specific settings to the settings block
*
* @param settings_navigation $settings The settings navigation object
* @param navigation_node $pcastnode The node to add module settings to
*/
function pcast_extend_settings_navigation(settings_navigation $settings, navigation_node $pcastnode) {
global $PAGE, $DB, $CFG, $USER;
$group = optional_param('group', '', PARAM_ALPHANUM);
$pcast = $DB->get_record('pcast', array("id" => $PAGE->cm->instance));
$pcastconfig = get_config('mod_pcast');
// Display approval link only when required.
if ($pcast->requireapproval) {
if (has_capability('mod/pcast:approve', $PAGE->cm->context)) {
$pcastnode->add(get_string('waitingapproval', 'pcast'), new moodle_url('/mod/pcast/view.php',
array('id' => $PAGE->cm->id, 'mode' => PCAST_APPROVAL_VIEW)));
}
}
// Display add new episode link. (Must have write + manage / approve as teacher or write + allow user episodes).
if (has_capability('mod/pcast:write', $PAGE->cm->context) && (has_capability('mod/pcast:manage', $PAGE->cm->context)
|| has_capability('mod/pcast:approve', $PAGE->cm->context))) {
// This is a teacher.
$node = $pcastnode->add(get_string('addnewepisode', 'pcast'),
new moodle_url('/mod/pcast/edit.php',
array('cmid' => $PAGE->cm->id)));
$node->set_show_in_secondary_navigation(false);
} else if (has_capability('mod/pcast:write', $PAGE->cm->context)) {
// See if the activity allows student posting.
if ($pcast->userscanpost == true) {
// Add a link to ad an episode.
$node = $pcastnode->add(get_string('addnewepisode', 'pcast'),
new moodle_url('/mod/pcast/edit.php',
array('cmid' => $PAGE->cm->id)));
$node->set_show_in_secondary_navigation(false);
}
}
if (!empty($CFG->enablerssfeeds) && !empty($pcastconfig->enablerssfeeds) && $pcast->enablerssfeed) {
require_once("$CFG->libdir/rsslib.php");
$string = get_string('rsslink', 'pcast');
// Sort out groups.
if (is_numeric($group)) {
$currentgroup = $group;
} else {
$groupmode = groups_get_activity_groupmode($PAGE->cm);
if ($groupmode > 0) {
$currentgroup = groups_get_activity_group($PAGE->cm);
} else {
$currentgroup = 0;
}
}
$args = $pcast->id . '/'.$currentgroup;
$url = new moodle_url(rss_get_url($PAGE->cm->context->id, $USER->id, 'pcast', $args));
$node = $pcastnode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', ''));
$node->set_show_in_secondary_navigation(false);
if (!empty($pcastconfig->enablerssitunes) && $pcast->enablerssitunes) {
$string = get_string('pcastlink', 'pcast');
require_once("$CFG->dirroot/mod/pcast/rsslib.php");
$url = pcast_rss_get_url($PAGE->cm->context->id, $USER->id, 'pcast', $args);
$node = $pcastnode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', ''));
$node->set_show_in_secondary_navigation(false);
}
}
}
/**
* Helper function to get the RSS category.
* @param class $item
* @param class $pcast
* @return stdclass
*/
function pcast_get_itunes_categories($item, $pcast) {
// Split the category info into the top category and nested category.
if (isset($item->category)) {
$length = strlen($item->category);
switch ($length) {
case 4:
$item->topcategory = substr($item->category, 0, 1);
$item->nestedcategory = (int)substr($item->category, 1, 3);
break;
case 5:
$item->topcategory = substr($item->category, 0, 2);
$item->nestedcategory = (int)substr($item->category, 2, 3);
break;
case 6:
$item->topcategory = substr($item->category, 0, 3);
$item->nestedcategory = (int)substr($item->category, 3, 3);
break;
default:
// SHOULD NEVER HAPPEN.
$item->topcategory = $pcast->topcategory;
$item->nestedcategory = $pcast->nestedcategory;
break;
}
} else {
// Will only happen if categories are disabled.
$item->topcategory = $pcast->topcategory;
$item->nestedcategory = $pcast->nestedcategory;
}
return $item;
}
/**
* File browsing support for pcast module.
*
* @param file_browser $browser
* @param array $areas
* @param stdClass $course
* @param cm_info $cm
* @param context $context
* @param string $filearea
* @param int $itemid
* @param string $filepath
* @param string $filename
* @return file_info_stored file_info_stored instance or null if not found
*/
function mod_pcast_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
global $CFG, $DB;
if ($context->contextlevel != CONTEXT_MODULE) {
return null;
}
if ($filearea === 'summary' || $filearea === 'episode' || $filearea === 'logo') {
if (!$episode = $DB->get_record('pcast_episodes', array('id' => $itemid))) {
return null;
}
// Make sure the podcast exists.
if (!$pcast = $DB->get_record('pcast', array('id' => $cm->instance))) {
return null;
}
if (is_null($itemid)) {
require_once($CFG->dirroot.'/mod/pcast/locallib.php');
return new pcast_file_info_container($browser, $course, $cm, $context, $areas, $filearea);
}
// Is it an episode, and has it been approved?
if ($filearea === 'episode' &&
$pcast->requireapproval &&
!$episode->approved &&
!has_capability('mod/pcast:approve', $context)) {
return null;
}
// Everything is OK, so serve the file.
$filecontext = context_module::instance($cm->id);
$fs = get_file_storage();
$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;
if (!($storedfile = $fs->get_file($filecontext->id, 'mod_pcast', $filearea, $itemid, $filepath, $filename))) {
return null;
}
$urlbase = $CFG->wwwroot.'/pluginfile.php';
return new file_info_stored($browser, $filecontext, $storedfile, $urlbase, $filearea, $itemid, true, true, false, false);
}
return null;
}
/**
* Serves all files for the pcast module.
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @param array $options additional options affecting the file serving
* @return bool false if file not found, does not return if found - justsend the file
*/
function pcast_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
global $CFG, $DB, $USER;
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
if ($filearea === 'episode' || $filearea === 'summary') {
$episodeid = (int)array_shift($args);
if (!$episode = $DB->get_record('pcast_episodes', array('id' => $episodeid))) {
return false;
}
if (!$pcast = $DB->get_record('pcast', array('id' => $cm->instance))) {
return false;
}
if ($pcast->requireapproval && !$episode->approved && !has_capability('mod/pcast:approve', $context)) {
return false;
}
$relativepath = implode('/', $args);
$filecontext = context_module::instance($cm->id);
$fullpath = "/$filecontext->id/mod_pcast/$filearea/$episodeid/$relativepath";
$fs = get_file_storage();
if ((!$file = $fs->get_file_by_hash(sha1($fullpath))) || $file->is_directory()) {
return false;
}
// Log the file as viewed.
$pcast->URL = $CFG->wwwroot . '/pluginfile.php' . $fullpath;
$pcast->filename = implode('/', $args);
if (!empty($USER->id)) {
pcast_add_view_instance($pcast, $episode, $USER->id, $context);
}
// Finally send the file.
send_stored_file($file, 0, 0, $forcedownload, $options); // Download MUST be forced - security!
} else if ($filearea === 'logo') {
$relativepath = implode('/', $args);
$filecontext = context_module::instance($cm->id);
$fullpath = "/$filecontext->id/mod_pcast/$filearea/$relativepath";
$fs = get_file_storage();
if ((!$file = $fs->get_file_by_hash(sha1($fullpath))) || $file->is_directory()) {
return false;
}
// Finally send the file.
send_stored_file($file, 0, 0, $forcedownload, $options); // Download MUST be forced - security!
}
return false;
}
/**
* logs the pcast files.
*
* @param stdClass $pcast
* @param stdClass $episode
* @param string $userid
* @param stdClass $context Moodle Context.
* @return bool false if error else true
*/
function pcast_add_view_instance($pcast, $episode, $userid, $context) {
global $DB;
// Lookup the user add add to the view count.
if (!$view = $DB->get_record("pcast_views", array("episodeid" => $episode->id, "userid" => $userid))) {
// User has never seen the podcast episode.
$view = new stdClass();
$view->userid = $userid;
$view->views = 1;
$view->episodeid = $episode->id;
$view->lastview = time();
if (!$result = $DB->insert_record("pcast_views", $view)) {
throw new moodle_exception('databaseerror', 'pcast');
}
} else {
// The user has viewed the episode before.
$view->views = $view->views + 1;
$view->lastview = time();
if (!$result = $DB->update_record("pcast_views", $view)) {
throw new moodle_exception('databaseerror', 'pcast');
}
}
$event = \mod_pcast\event\episode_viewed::create(array(
'objectid' => $view->episodeid,
'context' => $context,
));
$event->add_record_snapshot('pcast_episodes', $episode);
$event->add_record_snapshot('pcast', $pcast);
$event->trigger();
return $result;
}
/**
* Returns all other caps used in module
* @return array
*/
function pcast_get_extra_capabilities() {
return array('moodle/comment:post',
'moodle/comment:view',
'moodle/site:viewfullnames',
'moodle/site:trustcontent',
'moodle/rating:view',
'moodle/rating:viewany',
'moodle/rating:viewall',
'moodle/rating:rate',
'moodle/site:accessallgroups',
);
}
// Course reset code.
/**
* Implementation of the function for printing the form elements that control
* whether the course reset functionality affects the pcast.
* @param stdClass $mform form passed by reference
*/
function pcast_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'pcastheader', get_string('modulenameplural', 'pcast'));
$mform->addElement('checkbox', 'reset_pcast_all', get_string('resetpcastsall', 'pcast'));
$mform->addElement('checkbox', 'reset_pcast_notenrolled', get_string('deletenotenrolled', 'pcast'));