forked from sbourget/moodle-mod_pcast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
1339 lines (1100 loc) · 44 KB
/
lib.php
File metadata and controls
1339 lines (1100 loc) · 44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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();
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);
/** example constant */
//define('PCAST_ULTIMATE_ANSWER', 42);
/**
* 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.
*/
//global $PCAST_GLOBAL_VARIABLE;
//$PCAST_QUESTION_OF = array('Life', 'Universe', 'Everything');
/**
* Lists supported features
*
* @uses FEATURE_GROUPS
* @uses FEATURE_GROUPINGS
* @uses FEATURE_GROUPMEMBERSONLY
* @uses FEATURE_MOD_INTRO
* @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_GROUPMEMBERSONLY: return true;
case FEATURE_MOD_INTRO: return true;
case FEATURE_COMPLETION_TRACKS_VIEWS: 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;
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
* @global stdClass $DB
* @global stdClass $USER
* @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) or empty($pcast->assessed)) {
$pcast->assesstimestart = 0;
$pcast->assesstimefinish = 0;
}
// If no owner then set it to the instance creator.
if (isset($pcast->enablerssitunes) and ($pcast->enablerssitunes == 1)) {
if (!isset($pcast->userid)) {
$pcast->userid = $USER->id;
}
}
// Get the episode category information
$defaults->topcategory = 0;
$defaults->nestedcategory = 0;
$pcast = pcast_get_itunes_categories($pcast, $defaults);
# You may have to add extra stuff in here #
$result = $DB->insert_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 = get_context_instance(CONTEXT_MODULE, $cmid);
if ($draftitemid) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_pcast', 'logo', 0, array('subdirs'=>false));
}
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
* @global stdClass $DB
* @global stdClass $USER
* @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) or 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) and ($pcast->enablerssitunes == 1)) {
if (!isset($pcast->userid)) {
$pcast->userid = $USER->id;
}
}
// Get the episode category information
$defaults->topcategory = 0;
$defaults->nestedcategory = 0;
$pcast = pcast_get_itunes_categories($pcast, $defaults);
# You may have to add extra stuff in here #
$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 = get_context_instance(CONTEXT_MODULE, $cmid);
if ($draftitemid) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_pcast', 'logo', 0, array('subdirs'=>false));
}
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
* @global stdClass $DB
* @global stdClass $USER
* @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 = get_context_instance(CONTEXT_MODULE, $cm->id)) {
return false;
}
# Delete any dependent records here #
// Delete Comments
$episode_select = "SELECT id FROM {pcast_episodes} WHERE pcastid = ?";
$DB->delete_records_select('comments', "contextid=? AND commentarea=? AND itemid IN ($episode_select)", array($id, 'pcast_episode', $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
$episode_select = "SELECT id FROM {pcast_episodes} WHERE pcastid = ?";
$DB->delete_records_select('pcast_views', "episodeid IN ($episode_select)", array($pcast->id));
//Delete Episodes
$DB->delete_records('pcast_episodes', array('pcastid' => $pcast->id));
//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
*
* @global stdClass $DB
* @param stdClass $course
* @param stdClass $user
* @param stdClass $mod
* @param stdClass $pcast
* @return object $result
*/
function pcast_user_outline($course, $user, $mod, $pcast) {
global $DB;
if ($logs = $DB->get_records("log", array('userid'=>$user->id, 'module'=>'pcast',
'action'=>'view', 'info'=>$pcast->id), "time ASC")) {
$numviews = count($logs);
$lastlog = array_pop($logs);
$result = new object();
$result->info = get_string("numviews", "", $numviews);
$result->time = $lastlog->time;
return $result;
}
return null;
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @global stdClass $DB
* @global stdClass $CFG
* @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, $DB;
if ($logs = $DB->get_records("log", array('userid'=>$user->id, 'module'=>'pcast',
'action'=>'view', 'info'=>$pcast->id), "time ASC")) {
$numviews = count($logs);
$lastlog = array_pop($logs);
$strmostrecently = get_string("mostrecently");
$strnumviews = get_string("numviews", "", $numviews);
echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
} else {
print_string("noviews", "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.
*
* @global stdClass $CFG
* @global stdClass $USER
* @global stdClass $DB
* @global stdClass $OUTPUT
* @param stdClass $course
* @param bool $viewfullnames
* @param int $timestart
* @return bool
*/
function pcast_print_recent_activity($course, $viewfullnames, $timestart) {
// return false; // True if anything was printed, otherwise false
global $CFG, $USER, $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 glossaries in one course, right?
if (!$episodes = $DB->get_records_sql("SELECT e.id, e.name, e.approved, e.timemodified, e.pcastid,
e.userid, u.firstname, u.lastname, u.email, u.picture
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', get_context_instance(CONTEXT_MODULE, $modinfo->instances['pcast'][$episode->pcastid]->id));
}
if (!$editor[$episode->pcastid]) {
unset($episodes[$episodeid]);
}
}
if (!$episodes) {
return false;
}
echo $OUTPUT->heading(get_string('newepisodes', 'pcast').':');
$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;
}
/**
* Must return an array of user records (all data) who are participants
* for a given instance of pcast. Must include every user involved
* in the instance, independient of his role (student, teacher, admin...)
* See other modules as example.
*
* @param int $pcastid ID of an instance of this module
* @return mixed boolean/array of students
*/
function pcast_get_participants($pcastid) {
global $DB;
//Get participants
$participants = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
FROM {user} u, {pcast_episodes} p
WHERE p.pcastid = ? AND u.id = p.userid", array($pcastid));
//Return participants array (it contains an array of unique users)
return $participants;
}
/**
* 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
* @global $DB
* @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 $scaleid int
* @return boolean True if the scale is used by any pcast
*/
function pcast_scale_used_anywhere($scaleid) {
global $DB;
if ($scaleid and $DB->record_exists('pcast', 'grade', -$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('pcast_episode','pcast_logo');
$areas = array();
$areas['logo'] = get_string('arealogo', 'pcast');
$areas['episode'] = get_string('areaepisode', '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;
}
/**
* Adds module specific settings to the navigation block
* @global stdClass $CFG
* @param stdClass $navigation
* @param stdClass $course
* @param stdClass $module
* @param stdClass $cm
*/
function pcast_extend_navigation($navigation, $course, $module, $cm) {
global $CFG;
$navigation->add(get_string('standardview', 'pcast'), new moodle_url('/mod/pcast/view.php', array('id'=>$cm->id, 'mode'=>PCAST_STANDARD_VIEW)));
$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;
$mode = optional_param('mode', '', PARAM_ALPHA);
$hook = optional_param('hook', 'ALL', PARAM_CLEAN);
$group = optional_param('group', '', PARAM_ALPHANUM);
if (has_capability('mod/pcast:approve', $PAGE->cm->context) && ($hiddenentries = $DB->count_records('pcast_episodes', array('pcastid'=>$PAGE->cm->instance, 'approved'=>0)))) {
$pcastnode->add(get_string('waitingapproval', 'pcast'), new moodle_url('/mod/pcast/view.php', array('id'=>$PAGE->cm->id, 'mode'=>PCAST_APPROVAL_VIEW)));
}
if (has_capability('mod/pcast:write', $PAGE->cm->context)) {
$pcastnode->add(get_string('addnewepisode', 'pcast'), new moodle_url('/mod/pcast/edit.php', array('cmid'=>$PAGE->cm->id)));
}
$pcast = $DB->get_record('pcast', array("id" => $PAGE->cm->instance));
if (!empty($CFG->enablerssfeeds) && !empty($CFG->pcast_enablerssfeeds)
&& $pcast->rssepisodes) {
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));
$pcastnode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', ''));
if (!empty($CFG->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);
$pcastnode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', ''));
}
}
}
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;
}
/**
* Serves all files for the pcast module.
*
* @global stdClass $CFG
* @global stdClass $DB
* @global stdClass $USER (used only for logging if available)
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @return bool false if file not found, does not return if found - justsend the file
*/
function pcast_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
global $CFG, $DB, $USER;
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
if ($filearea === 'episode') {
$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 and !$episode->approved and !has_capability('mod/pcast:approve', $context)) {
return false;
}
$relativepath = implode('/', $args);
$filecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
$fullpath = "/$filecontext->id/mod_pcast/$filearea/$episodeid/$relativepath";
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $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, $USER->id);
}
// finally send the file
send_stored_file($file, 0, 0, $forcedownload); // download MUST be forced - security!
} else if ($filearea === 'logo') {
$relativepath = implode('/', $args);
$filecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
$fullpath = "/$filecontext->id/mod_pcast/$filearea/$relativepath";
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// finally send the file
send_stored_file($file, 0, 0, $forcedownload); // download MUST be forced - security!
}
return false;
}
/**
* logs the pcast files.
*
* @global stdClass $CFG
* @param stdClass $pcast
* @param string $userid
* @return bool false if error else true
*/
function pcast_add_view_instance($pcast, $userid) {
global $DB;
//lookup the user add add to the view count
if (!$view = $DB->get_record("pcast_views", array("episodeid" => $pcast->id, "userid" => $userid))) {
$view=null;
unset($view);
$view->userid=$userid;
$view->views=1;
$view->episodeid=$pcast->id;
$view->lastview = time();
if(!$result=$DB->insert_record("pcast_views", $view, $returnid=false, $bulk=false)) {
print_error('databaseerror', 'pcast');
}
} else { //Never viewed the file before
$temp_view = $view->views + 1;
$view->views = $temp_view;
$view->lastview=time();
if(!$result = $DB->update_record("pcast_views", $view, $bulk=false)) {
print_error('databaseerror', 'pcast');
}
}
add_to_log($pcast->course, "pcast", "view", $pcast->URL, $pcast->filename,0 ,$userid);
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'));
$mform->disabledIf('reset_pcast_notenrolled', 'reset_pcast_all', 'checked');
$mform->addElement('checkbox', 'reset_pcast_ratings', get_string('deleteallratings'));
$mform->disabledIf('reset_pcast_ratings', 'reset_pcast_all', 'checked');
$mform->addElement('checkbox', 'reset_pcast_comments', get_string('deleteallcomments'));
$mform->disabledIf('reset_pcast_comments', 'reset_pcast_all', 'checked');
$mform->addElement('checkbox', 'reset_pcast_views', get_string('deleteallviews', 'pcast'));
$mform->disabledIf('reset_pcast_views', 'reset_pcast_all', 'checked');
}
/**
* Course reset form defaults.
* @return array
*/
function pcast_reset_course_form_defaults($course) {
return array('reset_pcast_all'=>0, 'reset_pcast_ratings'=>1, 'reset_pcast_comments'=>1, 'reset_pcast_notenrolled'=>0, 'reset_pcast_views'=>1);
}
/**
* Removes all grades from gradebook
*
* @global stdClass
* @param int $courseid
* @param string optional type
*/
// TODO LOOK AT AFTER GRADES ARE IMPLEMENTED
function pcast_reset_gradebook($courseid, $type='') {
global $DB;
$sql = "SELECT g.*, cm.idnumber as cmidnumber, g.course as courseid
FROM {pcast} g, {course_modules} cm, {modules} m
WHERE m.name='pcast' AND m.id=cm.module AND cm.instance=g.id AND g.course=?";
if ($pcasts = $DB->get_records_sql($sql, array($courseid))) {
foreach ($pcasts as $pcast) {
pcast_grade_item_update($pcast, 'reset');
}
}
}
/**
* Actual implementation of the rest coures functionality, delete all the
* pcast responses for course $data->courseid.
*
* @global stdClass
* @param $data the data submitted from the reset course.
* @return array status array
*/
function pcast_reset_userdata($data) {
global $CFG, $DB;
require_once($CFG->dirroot.'/rating/lib.php');
$componentstr = get_string('modulenameplural', 'pcast');
$status = array();
$allepisodessql = "SELECT e.id
FROM {pcast_episodes} e
JOIN {pcast} p ON e.pcastid = p.id
WHERE p.course = ?";
$allpcastssql = "SELECT p.id
FROM {pcast} p
WHERE p.course = ?";
$params = array($data->courseid);
$fs = get_file_storage();
$rm = new rating_manager();
$ratingdeloptions = new stdClass();
$ratingdeloptions->component = 'mod_pcast';
$ratingdeloptions->ratingarea = 'episode';
// delete entries if requested
if (!empty($data->reset_pcast_all)) {
$params[] = 'pcast_episode';
$DB->delete_records_select('comments', "itemid IN ($allepisodessql) AND commentarea=?", $params);
$DB->delete_records_select('pcast_episodes', "pcastid IN ($allpcastssql)", $params);
// now get rid of all attachments
if ($pcasts = $DB->get_records_sql($allpcastssql, $params)) {
foreach ($pcasts as $pcastid => $unused) {
if (!$cm = get_coursemodule_from_instance('pcast', $pcastid)) {
continue;
}
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$fs->delete_area_files($context->id, 'mod_pcast', 'episode');
//delete ratings
$ratingdeloptions->contextid = $context->id;
$rm->delete_ratings($ratingdeloptions);
}
}
// remove all grades from gradebook
if (empty($data->reset_gradebook_grades)) {
pcast_reset_gradebook($data->courseid);
}
$status[] = array('component'=>$componentstr, 'item'=>get_string('resetpcastsall', 'pcast'), 'error'=>false);
}
// remove entries by users not enrolled into course
else if (!empty($data->reset_pcast_notenrolled)) {
$course_context = get_context_instance(CONTEXT_COURSE, $data->courseid);
// Get list of enrolled users
$people = get_enrolled_users($course_context);
$list ='';
$list2 ='';
foreach ($people as $person) {
$list .=' AND e.userid != ?';
$list2 .=' AND userid != ?';
$params[] = $person->id;
}
// Construct SQL to episodes from users whe are no longer enrolled
$unenrolledepisodessql = "SELECT e.id
FROM {pcast_episodes} e
WHERE e.course = ? " . $list;
$params[] = 'pcast_episode';
$DB->delete_records_select('comments', "itemid IN ($unenrolledepisodessql) AND commentarea=?", $params);
$DB->delete_records_select('pcast_episodes', "course =? ". $list2, $params);
// now get rid of all attachments
if ($pcasts = $DB->get_records_sql($unenrolledepisodessql, $params)) {
foreach ($pcasts as $pcastid => $unused) {
if (!$cm = get_coursemodule_from_instance('pcast', $pcastid)) {
continue;
}
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$fs->delete_area_files($context->id, 'mod_pcast', 'episode');
//delete ratings
$ratingdeloptions->contextid = $context->id;
$rm->delete_ratings($ratingdeloptions);
}
}
// remove all grades from gradebook
if (empty($data->reset_gradebook_grades)) {
pcast_reset_gradebook($data->courseid);
}
$status[] = array('component'=>$componentstr, 'item'=>get_string('deletenotenrolled', 'pcast'), 'error'=>false);
}
// remove all ratings
if (!empty($data->reset_pcast_ratings)) {
//remove ratings
if ($pcasts = $DB->get_records_sql($allpcastssql, $params)) {
foreach ($pcasts as $pcastid => $unused) {
if (!$cm = get_coursemodule_from_instance('pcast', $pcastid)) {
continue;
}
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
//delete ratings
$ratingdeloptions->contextid = $context->id;
$rm->delete_ratings($ratingdeloptions);
}
}
// remove all grades from gradebook
if (empty($data->reset_gradebook_grades)) {
pcast_reset_gradebook($data->courseid);
}
$status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallratings'), 'error'=>false);
}
// remove comments
if (!empty($data->reset_pcast_comments)) {
$params[] = 'pcast_episode';
$DB->delete_records_select('comments', "itemid IN ($allepisodessql) AND commentarea= ? ", $params);
$status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallcomments'), 'error'=>false);
}
// remove views
if (!empty($data->reset_pcast_views)) {
// $params[] = 'pcast_episode';
$DB->delete_records_select('pcast_views',"episodeid IN ($allepisodessql) ", $params);
// $DB->delete_records_select('pcast_views', "itemid IN ($allepisodessql) AND commentarea= ? ", $params);
$status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallviews','pcast'), 'error'=>false);
}
/// updating dates - shift may be negative too
if ($data->timeshift) {
shift_course_mod_dates('pcast', array('assesstimestart', 'assesstimefinish'), $data->timeshift, $data->courseid);
$status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
}
return $status;
}
//TODO: RATINGS CODE -UNTESTED
/**
* Return grade for given user or all users.
*
* @global stdClass
* @param int $pcastid id of pcast