-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathlib.php
2528 lines (2227 loc) · 92.4 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 detail.
//
// 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 hotpot module functions needed by Moodle core and other subsystems
*
* All the functions neeeded by Moodle core, gradebook, file subsystem etc
* are placed here.
*
* @package mod
* @subpackage hotpot
* @copyright 2009 Gordon Bateson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
////////////////////////////////////////////////////////////////////////////////
// Moodle core API //
////////////////////////////////////////////////////////////////////////////////
/**
* Returns the information if the module supports a feature
*
* the very latest Moodle 2.x expects "mod_hotpot_supports"
* but since this module may also be run in early Moodle 2.x
* we leave this function with its legacy name "hotpot_supports"
*
* @see plugin_supports() in lib/moodlelib.php
* @see init_features() in course/moodleform_mod.php
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed true if the feature is supported, null if unknown
*/
function hotpot_supports($feature) {
// these constants are defined in "lib/moodlelib.php"
// they are not all defined in Moodle 2.0, so we
// check each one is defined before trying to use it
$constants = array(
'FEATURE_ADVANCED_GRADING' => false,
'FEATURE_BACKUP_MOODLE2' => true, // default=false
'FEATURE_COMMENT' => true,
'FEATURE_COMPLETION_HAS_RULES' => true,
'FEATURE_COMPLETION_TRACKS_VIEWS' => true,
'FEATURE_CONTROLS_GRADE_VISIBILITY' => false,
'FEATURE_GRADE_HAS_GRADE' => true, // default=false
'FEATURE_GRADE_OUTCOMES' => true,
'FEATURE_GROUPINGS' => true, // default=false
'FEATURE_GROUPMEMBERSONLY' => true, // default=false
'FEATURE_GROUPS' => true,
'FEATURE_IDNUMBER' => true, // Moodle >= 2.0
'FEATURE_MOD_INTRO' => false, // default=true
'FEATURE_MODEDIT_DEFAULT_COMPLETION' => true,
'FEATURE_NO_VIEW_LINK' => false,
'FEATURE_PLAGIARISM' => false,
'FEATURE_RATE' => false,
'FEATURE_SHOW_DESCRIPTION' => true, // default=false (Moodle 2.2)
'FEATURE_USES_QUESTIONS' => false
);
if (defined('MOD_ARCHETYPE_OTHER')) {
$constants['FEATURE_MOD_ARCHETYPE'] = MOD_ARCHETYPE_OTHER; // Moodle >= 2.x
}
if (defined('MOD_PURPOSE_ASSESSMENT')) {
$constants['FEATURE_MOD_PURPOSE'] = MOD_PURPOSE_ASSESSMENT; // Moodle >= 4.x
}
foreach ($constants as $constant => $value) {
if (defined($constant) && $feature==constant($constant)) {
return $value;
}
}
return false;
}
/**
* Saves a new instance of the hotpot into the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will save a new instance and return the id number
* of the new instance.
*
* @param stdclass $data An object from the form in mod_form.php
* @return int The id of the newly inserted hotpot record
*/
function hotpot_add_instance(stdclass $data, $mform) {
global $DB;
hotpot_process_formdata($data, $mform);
// insert the new record so we get the id
$data->id = $DB->insert_record('hotpot', $data);
// update calendar events
hotpot_update_events_wrapper($data);
// update gradebook item
hotpot_grade_item_update($data);
if (class_exists('\core_completion\api')) {
$completiontimeexpected = (empty($data->completionexpected) ? null : $data->completionexpected);
\core_completion\api::update_completion_date_event($data->coursemodule, 'hotpot', $data->id, $completiontimeexpected);
}
return $data->id;
}
/**
* 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 $data An object from the form in mod_form.php
* @return bool success
*/
function hotpot_update_instance(stdclass $data, $mform) {
global $DB;
hotpot_process_formdata($data, $mform);
$data->id = $data->instance;
$DB->update_record('hotpot', $data);
// update calendar events
hotpot_update_events_wrapper($data);
// update gradebook item
if ($data->grademethod==$mform->get_original_value('grademethod', 0)) {
hotpot_grade_item_update($data);
} else {
// recalculate grades for all users
hotpot_update_grades($data);
}
if (class_exists('\core_completion\api')) {
$completiontimeexpected = (empty($data->completionexpected) ? null : $data->completionexpected);
\core_completion\api::update_completion_date_event($data->coursemodule, 'hotpot', $data->id, $completiontimeexpected);
}
return true;
}
/**
* Set secondary fields (i.e. fields derived from the form fields)
* for this HotPot acitivity
*
* @param stdclass $data (passed by reference)
* @param moodle_form $mform
*/
function hotpot_process_formdata(stdclass &$data, $mform) {
global $CFG;
require_once($CFG->dirroot.'/mod/hotpot/locallib.php');
// The intro field was added in Moodle 4.0 because it is expected in
// "lib/classes/output/activity_header.php". If left unset, it may
// generate a database error, so we force it to a reasonable default.
if (! isset($data->intro)) {
$data->intro = '';
$data->introformat = 0;
}
if ($mform->is_add()) {
$data->timecreated = time();
} else {
$data->timemodified = time();
}
// get context for this HotPot instance
$context = hotpot_get_context(CONTEXT_MODULE, $data->coursemodule);
$sourcefile = null;
$data->sourcefile = '';
$data->sourcetype = '';
if ($data->sourceitemid) {
$options = hotpot::sourcefile_options();
file_save_draft_area_files($data->sourceitemid, $context->id, 'mod_hotpot', 'sourcefile', 0, $options);
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'mod_hotpot', 'sourcefile');
// do we need to remove the draft files ?
// otherwise the "files" table seems to get full of "draft" records
// $fs->delete_area_files($context->id, 'user', 'draft', $data->sourceitemid);
foreach ($files as $hash => $file) {
if ($file->get_sortorder()==1) {
$data->sourcefile = $file->get_filepath().$file->get_filename();
$data->sourcetype = hotpot::get_sourcetype($file);
$sourcefile = $file;
break;
}
}
unset($fs, $files, $file, $hash, $options);
}
if (is_null($sourcefile) || $data->sourcefile=='' || $data->sourcetype=='') {
// sourcefile was missing or not a recognized type - shouldn't happen !!
}
// process text fields that may come from source file
$source = false;
$textfields = array('name', 'entrytext', 'exittext');
foreach($textfields as $textfield) {
$textsource = $textfield.'source';
if (! isset($data->$textsource)) {
$data->$textsource = hotpot::TEXTSOURCE_SPECIFIC;
}
switch ($data->$textsource) {
case hotpot::TEXTSOURCE_FILE:
if ($data->sourcetype && $sourcefile && empty($source)) {
$class = 'hotpot_source_'.$data->sourcetype;
$source = new $class($sourcefile, $data);
}
$method = 'get_'.$textfield;
if ($source && method_exists($source, $method)) {
$data->$textfield = $source->$method();
} else {
$data->$textfield = '';
}
break;
case hotpot::TEXTSOURCE_FILENAME:
$data->$textfield = basename($data->sourcefile);
break;
case hotpot::TEXTSOURCE_FILEPATH:
$data->$textfield = str_replace(array('/', '\\'), ' ', $data->sourcefile);
break;
case hotpot::TEXTSOURCE_SPECIFIC:
default:
if (isset($data->$textfield)) {
$data->$textfield = trim($data->$textfield);
} else {
$data->$textfield = $mform->get_original_value($textfield, '');
}
}
// default activity name is simply "HotPot"
if ($textfield=='name' && $data->$textfield=='') {
$data->$textfield = get_string('modulename', 'mod_hotpot');
}
}
// process entry/exit page settings
foreach (hotpot::text_page_types() as $type) {
// show page (boolean switch)
$pagefield = $type.'page';
if (! isset($data->$pagefield)) {
$data->$pagefield = 0;
}
// set field names
$textfield = $type.'text';
$formatfield = $type.'format';
$editorfield = $type.'editor';
$sourcefield = $type.'textsource';
$optionsfield = $type.'options';
// ensure text, format and option fields are set
// (these fields can't be null in the database)
if (! isset($data->$textfield)) {
$data->$textfield = $mform->get_original_value($textfield, '');
}
if (! isset($data->$formatfield)) {
$data->$formatfield = $mform->get_original_value($formatfield, FORMAT_HTML);
}
if (! isset($data->$optionsfield)) {
$data->$optionsfield = $mform->get_original_value($optionsfield, 0);
}
if (! is_numeric($data->$optionsfield)) {
$data->$optionsfield = 0; // prevent "non-numeric" warnings in PHP 7.1
}
// set text and format fields
if ($data->$sourcefield==hotpot::TEXTSOURCE_SPECIFIC) {
// transfer wysiwyg editor text
if ($itemid = $data->{$editorfield}['itemid']) {
if (isset($data->{$editorfield}['text'])) {
// get the text that was sent from the browser
$editoroptions = hotpot::text_editors_options($context);
$text = file_save_draft_area_files($itemid, $context->id, 'mod_hotpot', $type, 0, $editoroptions, $data->{$editorfield}['text']);
// remove leading and trailing white space,
// - empty html paragraphs (from IE)
// - and blank lines (from Firefox)
$text = preg_replace('/^((<p>\s*<\/p>)|(<br[^>]*>)|\s)+/is', '', $text);
$text = preg_replace('/((<p>\s*<\/p>)|(<br[^>]*>)|\s)+$/is', '', $text);
$data->$textfield = $text;
$data->$formatfield = $data->{$editorfield}['format'];
}
}
}
// set entry/exit page options
foreach (hotpot::text_page_options($type) as $name => $mask) {
$optionfield = $type.'_'.$name;
if ($data->$pagefield) {
if (empty($data->$optionfield)) {
// disable this option
$data->$optionsfield = $data->$optionsfield & ~$mask;
} else {
// enable this option
$data->$optionsfield = $data->$optionsfield | $mask;
}
}
}
// don't show exit page if no content is specified
if ($type=='exit' && empty($data->$optionsfield) && empty($data->$textfield)) {
$data->$pagefield = 0;
}
}
// timelimit
if ($data->timelimit==hotpot::TIME_SPECIFIC) {
$data->timelimit = $data->timelimitspecific;
}
// delay3
if ($data->delay3==hotpot::TIME_SPECIFIC) {
$data->delay3 = $data->delay3specific;
}
// set stopbutton and stoptext
if (empty($data->stopbutton_yesno)) {
$data->stopbutton = hotpot::STOPBUTTON_NONE;
$data->stoptext = $mform->get_original_value('stoptext', '');
} else {
if (! isset($data->stopbutton_type)) {
$data->stopbutton_type = '';
}
if (! isset($data->stopbutton_text)) {
$data->stopbutton_text = '';
}
if ($data->stopbutton_type=='specific') {
$data->stopbutton = hotpot::STOPBUTTON_SPECIFIC;
$data->stoptext = $data->stopbutton_text;
} else {
$data->stopbutton = hotpot::STOPBUTTON_LANGPACK;
$data->stoptext = $data->stopbutton_type;
}
}
// set review options
$data->reviewoptions = 0;
list($times, $items) = hotpot::reviewoptions_times_items();
foreach ($times as $timename => $timevalue) {
foreach ($items as $itemname => $itemvalue) {
$name = $timename.$itemname; // e.g. duringattemptresponses
if (isset($data->$name)) {
if ($data->$name) {
$data->reviewoptions += ($timevalue & $itemvalue);
}
unset($data->$name);
}
}
}
// save these form settings as user preferences
$preferences = array();
foreach (hotpot::user_preferences_fieldnames() as $fieldname) {
if (isset($data->$fieldname)) {
$preferences['hotpot_'.$fieldname] = $data->$fieldname;
}
}
set_user_preferences($preferences);
}
/**
* 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 hotpot_delete_instance($id) {
global $CFG, $DB;
require_once($CFG->dirroot.'/lib/gradelib.php');
// check the hotpot $id is valid
if (! $hotpot = $DB->get_record('hotpot', array('id' => $id))) {
return false;
}
// delete all associated hotpot questions
$DB->delete_records('hotpot_questions', array('hotpotid' => $hotpot->id));
// delete all associated hotpot attempts, details and responses
if ($attempts = $DB->get_records('hotpot_attempts', array('hotpotid' => $hotpot->id), '', 'id')) {
$ids = array_keys($attempts);
$DB->delete_records_list('hotpot_details', 'attemptid', $ids);
$DB->delete_records_list('hotpot_responses', 'attemptid', $ids);
$DB->delete_records_list('hotpot_attempts', 'id', $ids);
}
// remove records from the hotpot cache
$DB->delete_records('hotpot_cache', array('hotpotid' => $hotpot->id));
// gradebook cleanup
grade_update('mod/hotpot', $hotpot->course, 'mod', 'hotpot', $hotpot->id, 0, null, array('deleted' => true));
// Finally, remove the hotpot record itself.
// Apparently, this should be done AFTER removing the grade item.
$DB->delete_records('hotpot', array('id' => $hotpot->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 object $DB
* @param object $course
* @param object $user
* @param object $mod
* @param object $hotpot
* @return stdclass|null
*/
function hotpot_user_outline($course, $user, $mod, $hotpot) {
global $CFG, $DB;
require_once($CFG->dirroot.'/mod/hotpot/locallib.php');
$conditions = array('hotpotid'=>$hotpot->id, 'userid'=>$user->id);
if (! $attempts = $DB->get_records('hotpot_attempts', $conditions, "timestart ASC", 'id,score,timestart')) {
return null;
}
$time = 0;
$info = null;
$scores = array();
foreach ($attempts as $attempt){
if ($time==0) {
$time = $attempt->timestart;
}
$scores[] = hotpot::format_score($attempt);
}
if (count($scores)) {
$info = get_string('score', 'mod_hotpot').': '.implode(', ', $scores);
} else {
$info = get_string('noactivity', 'mod_hotpot');
}
return (object)array('time'=>$time, 'info'=>$info);
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @return string HTML
*/
function hotpot_user_complete($course, $user, $mod, $hotpot) {
$report = hotpot_user_outline($course, $user, $mod, $hotpot);
if (empty($report)) {
echo get_string("noactivity", 'mod_hotpot');
} else {
$date = userdate($report->time, get_string('strftimerecentfull'));
echo $report->info.' '.get_string('mostrecently').': '.$date;
}
return true;
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in hotpot 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 boolean
*/
function hotpot_print_recent_activity($course, $viewfullnames, $timestart) {
global $CFG, $DB, $OUTPUT;
$result = false;
// the Moodle "logs" table contains the following fields:
// time, userid, course, ip, module, cmid, action, url, info
// this function utilitizes the following index on the log table
// log_timcoumodact_ix : time, course, module, action
// log records are added by the following function in "lib/datalib.php":
// hotpot_add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0)
// log records are added by the following HotPot scripts:
// (scriptname : log action)
// attempt.php : attempt
// index.php : index
// report.php : report
// review.php : review
// submit.php : submit
// view.php : view
// all these actions have a record in the "log_display" table
$select = "time > ? AND course = ? AND module = ? AND action IN (?, ?, ?, ?, ?)";
$params = array($timestart, $course->id, 'hotpot', 'add', 'update', 'view', 'attempt', 'submit');
if ($logs = $DB->get_records_select('log', $select, $params, 'time ASC')) {
$modinfo = get_fast_modinfo($course);
$cmids = array_keys($modinfo->get_cms());
$stats = array();
foreach ($logs as $log) {
$cmid = $log->cmid;
if (! in_array($cmid, $cmids)) {
continue; // invalid $cmid - shouldn't happen !!
}
$cm = $modinfo->get_cm($cmid);
if (! $cm->uservisible) {
continue; // coursemodule is hidden from user
}
$sortorder = array_search($cmid, $cmids);
if (! array_key_exists($sortorder, $stats)) {
if (has_capability('mod/hotpot:reviewmyattempts', $cm->context) || has_capability('mod/hotpot:reviewallattempts', $cm->context)) {
$viewreport = true;
} else {
$viewreport = false;
}
$options = array('context' => $cm->context);
if (method_exists($cm, 'get_formatted_name')) {
$name = $cm->get_formatted_name($options);
} else {
$name = format_string($cm->name, true, $options);
}
$stats[$sortorder] = (object)array(
'name' => $name,
'cmid' => $cmid,
'add' => 0,
'update' => 0,
'view' => 0,
'attempt' => 0,
'submit' => 0,
'users' => array(),
'viewreport' => $viewreport
);
}
$action = $log->action;
switch ($action) {
case 'add':
case 'update':
// store most recent time
$stats[$sortorder]->$action = $log->time;
break;
case 'view':
case 'attempt':
case 'submit':
// increment counter
$stats[$sortorder]->$action ++;
break;
}
$stats[$sortorder]->users[$log->userid] = true;
}
$strusers = get_string('users');
$stradded = get_string('added', 'mod_hotpot');
$strupdated = get_string('updated', 'mod_hotpot');
$strviews = get_string('views', 'mod_hotpot');
$strattempts = get_string('attempts', 'mod_hotpot');
$strsubmits = get_string('submits', 'mod_hotpot');
$print_headline = true;
ksort($stats);
foreach ($stats as $stat) {
$li = array();
if ($stat->add) {
$li[] = $stradded.': '.userdate($stat->add);
}
if ($stat->update) {
$li[] = $strupdated.': '.userdate($stat->update);
}
if ($stat->viewreport) {
// link to a detailed report of recent activity for this hotpot
$url = new moodle_url(
'/course/recent.php',
array('id'=>$course->id, 'modid'=>$stat->cmid, 'date'=>$timestart)
);
if ($count = count($stat->users)) {
$li[] = $strusers.': '.html_writer::link($url, $count);
}
if ($stat->view) {
$li[] = $strviews.': '.html_writer::link($url, $stat->view);
}
if ($stat->attempt) {
$li[] = $strattempts.': '.html_writer::link($url, $stat->attempt);
}
if ($stat->submit) {
$li[] = $strsubmits.': '.html_writer::link($url, $stat->submit);
}
}
if (count($li)) {
if ($print_headline) {
$print_headline = false;
echo $OUTPUT->heading(get_string('modulenameplural', 'mod_hotpot').':', 3);
}
$url = new moodle_url('/mod/hotpot/view.php', array('id'=>$stat->cmid));
$link = html_writer::link($url, format_string($stat->name));
$text = html_writer::tag('p', $link).html_writer::alist($li);
echo html_writer::tag('div', $text, array('class'=>'hotpotrecentactivity'));
$result = true;
}
}
}
return $result;
}
/**
* Returns all activity in course hotpots since a given time
* This function returns activity for all hotpots since a given time.
* It is initiated from the "Full report of recent activity" link in the "Recent Activity" block.
* Using the "Advanced Search" page (cousre/recent.php?id=99&advancedfilter=1),
* results may be restricted to a particular course module, user or group
*
* This function is called from: {@link course/recent.php}
*
* @param array(object) $activities sequentially indexed array of course module objects
* @param integer $index length of the $activities array
* @param integer $timestart start date, as a UNIX date
* @param integer $courseid id in the "course" table
* @param integer $coursemoduleid id in the "course_modules" table
* @param integer $userid id in the "users" table (default = 0)
* @param integer $groupid id in the "groups" table (default = 0)
* @return void adds items into $activities and increments $index
* for each hotpot attempt, an $activity object is appended
* to the $activities array and the $index is incremented
* $activity->type : module type (always "hotpot")
* $activity->defaultindex : index of this object in the $activities array
* $activity->instance : id in the "hotpot" table;
* $activity->name : name of this hotpot
* $activity->section : section number in which this hotpot appears in the course
* $activity->content : array(object) containing information about hotpot attempts to be printed by {@link print_recent_mod_activity()}
* $activity->content->attemptid : id in the "hotpot_quiz_attempts" table
* $activity->content->attempt : the number of this attempt at this quiz by this user
* $activity->content->score : the score for this attempt
* $activity->content->timestart : the server time at which this attempt started
* $activity->content->timefinish : the server time at which this attempt finished
* $activity->user : object containing user information
* $activity->user->userid : id in the "user" table
* $activity->user->fullname : the full name of the user (see {@link lib/moodlelib.php}::{@link fullname()})
* $activity->user->picture : $record->picture;
* $activity->timestamp : the time that the content was recorded in the database
*/
function hotpot_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $coursemoduleid=0, $userid=0, $groupid=0) {
global $CFG, $DB, $OUTPUT, $USER;
// CONTRIB-4025 don't allow students to see each other's scores
$coursecontext = hotpot_get_context(CONTEXT_COURSE, $courseid);
if (! has_capability('mod/hotpot:reviewmyattempts', $coursecontext)) {
return; // can't view recent activity
}
if (! has_capability('mod/hotpot:reviewallattempts', $coursecontext)) {
$userid = $USER->id; // force this user only
}
// we want to detect Moodle >= 2.4
// method_exists('course_modinfo', 'get_used_module_names')
// method_exists('cm_info', 'get_modue_type_name')
// method_exists('cm_info', 'is_user_access_restricted_by_capability')
$reflector = new ReflectionFunction('get_fast_modinfo');
if ($reflector->getNumberOfParameters() >= 3) {
// Moodle >= 2.4 has 3rd parameter ($resetonly)
$modinfo = get_fast_modinfo($courseid);
$course = $modinfo->get_course();
} else {
// Moodle <= 2.3
$course = $DB->get_record('course', array('id' => $courseid));
$modinfo = get_fast_modinfo($course);
}
$cms = $modinfo->get_cms();
$hotpots = array(); // hotpotid => cmid
$users = array(); // cmid => array(userids)
foreach ($cms as $cmid => $cm) {
if ($cm->modname=='hotpot' && ($coursemoduleid==0 || $coursemoduleid==$cmid)) {
// save mapping from hotpotid => coursemoduleid
$hotpots[$cm->instance] = $cmid;
// initialize array of users who have recently attempted this HotPot
$users[$cmid] = array();
} else {
// we are not interested in this mod
unset($cms[$cmid]);
}
}
if (empty($hotpots)) {
return; // no hotpots
}
$select = 'ha.*, (ha.timemodified - ha.timestart) AS duration';
// Append name fields required for picture
if (class_exists('\\core_user\\fields')) {
// Moodle >= 3.11 (leading comma added by default)
$fields = \core_user\fields::for_userpic();
$select .= $fields->get_sql('u')->selects;
} else if (class_exists('user_picture')) {
// Moodle >= 2.6
$select .= ','.user_picture::fields('u');
} else {
// Moodle <= 2.5
$fields = array('u.id', 'u.firstname', 'u.lastname', 'u.picture', 'u.imagealt', 'u.email');
$select .= ','.implode(',', $fields);
}
// Remove "u.id".
$select = preg_replace('/, *u.id *,/', ',', $select);
$from = '{hotpot_attempts} ha JOIN {user} u ON ha.userid = u.id';
list($where, $params) = $DB->get_in_or_equal(array_keys($hotpots));
$where = 'ha.hotpotid '.$where;
$order = 'ha.userid, ha.attempt';
if ($groupid) {
// restrict search to a users from a particular group
$from .= ', {groups_members} gm';
$where .= ' AND ha.userid = gm.userid AND gm.id = ?';
$params[] = $groupid;
}
if ($userid) {
// restrict search to a single user
$where .= ' AND ha.userid = ?';
$params[] = $userid;
}
$where .= ' AND ha.timemodified > ?';
$params[] = $timestart;
if (! $attempts = $DB->get_records_sql("SELECT $select FROM $from WHERE $where ORDER BY $order", $params)) {
return; // no recent attempts at these hotpots
}
foreach (array_keys($attempts) as $attemptid) {
$attempt = &$attempts[$attemptid];
if (! array_key_exists($attempt->hotpotid, $hotpots)) {
continue; // invalid hotpotid - shouldn't happen !!
}
$user = clone($attempt);
$userid = $user->id = $user->userid;
$cmid = $hotpots[$attempt->hotpotid];
if (! array_key_exists($userid, $users[$cmid])) {
$users[$cmid][$userid] = (object)array(
'userid' => $userid,
'fullname' => fullname($user),
'picture' => $OUTPUT->user_picture($user, array('courseid' => $courseid)),
'attempts' => array(),
);
}
// add this attempt by this user at this course module
$users[$cmid][$userid]->attempts[$attempt->attempt] = &$attempt;
}
foreach ($cms as $cmid => $cm) {
if (empty($users[$cmid])) {
continue;
}
// add an activity object for each user's attempts at this hotpot
foreach ($users[$cmid] as $userid => $user) {
// get index of last (=most recent) attempt
$max_unumber = max(array_keys($user->attempts));
$options = array('context' => $cm->context);
if (method_exists($cm, 'get_formatted_name')) {
$name = $cm->get_formatted_name($options);
} else {
$name = format_string($cm->name, true, $options);
}
$activities[$index++] = (object)array(
'type' => 'hotpot',
'cmid' => $cmid,
'name' => $name,
'user' => $user,
'attempts' => $user->attempts,
'timestamp' => $user->attempts[$max_unumber]->timemodified
);
}
}
}
/**
* Print single activity item prepared by {@see hotpot_get_recent_mod_activity()}
*
* This function is called from: {@link course/recent.php}
*
* @param object $activity an object created by {@link get_recent_mod_activity()}
* @param integer $courseid id in the "course" table
* @param boolean $detail
* true : print a link to the hotpot activity
* false : do no print a link to the hotpot activity
* @param xxx $modnames
* @param xxx $viewfullnames
* @return no return value is required
*/
function hotpot_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
global $CFG, $OUTPUT;
require_once($CFG->dirroot.'/mod/hotpot/locallib.php');
static $dateformat = null;
if (is_null($dateformat)) {
$dateformat = get_string('strftimerecentfull');
}
$table = new html_table();
$table->cellpadding = 3;
$table->cellspacing = 0;
if ($detail) {
$row = new html_table_row();
$cell = new html_table_cell(' ', array('width'=>15));
$row->cells[] = $cell;
// activity icon and link to activity
if (method_exists($OUTPUT, 'image_icon')) {
// Moodle >= 3.3
$img = $OUTPUT->image_icon('icon', $modnames[$activity->type], $activity->type);
} else {
// Moodle <= 3.2
$img = $OUTPUT->pix_icon('icon', $modnames[$activity->type], $activity->type);
}
// link to activity
$href = new moodle_url('/mod/hotpot/view.php', array('id' => $activity->cmid));
$link = html_writer::link($href, $activity->name);
$cell = new html_table_cell("$img $link");
$cell->colspan = 6;
$row->cells[] = $cell;
$table->data[] = new html_table_row(array(
new html_table_cell(' ', array('width'=>15)),
new html_table_cell("$img $link")
));
$table->data[] = $row;
}
$row = new html_table_row();
// set rowspan to (number of attempts) + 1
$rowspan = count($activity->attempts) + 1;
$cell = new html_table_cell(' ', array('width'=>15));
$cell->rowspan = $rowspan;
$row->cells[] = $cell;
$cell = new html_table_cell($activity->user->picture, array('width'=>35, 'valign'=>'top', 'class'=>'forumpostpicture'));
$cell->rowspan = $rowspan;
$row->cells[] = $cell;
$href = new moodle_url('/user/view.php', array('id'=>$activity->user->userid, 'course'=>$courseid));
$cell = new html_table_cell(html_writer::link($href, $activity->user->fullname));
$cell->colspan = 5;
$row->cells[] = $cell;
$table->data[] = $row;
foreach ($activity->attempts as $attempt) {
if ($attempt->duration) {
$duration = '('.hotpot::format_time($attempt->duration).')';
} else {
$duration = ' ';
}
$href = new moodle_url('/mod/hotpot/review.php', array('id'=>$attempt->id));
$link = html_writer::link($href, userdate($attempt->timemodified, $dateformat));
$table->data[] = new html_table_row(array(
new html_table_cell($attempt->attempt),
new html_table_cell($attempt->score.'%'),
new html_table_cell(hotpot::format_status($attempt->status, true)),
new html_table_cell($link),
new html_table_cell($duration)
));
}
echo html_writer::table($table);
}
/*
* This function defines what log actions will be selected from the Moodle logs
* and displayed for course -> report -> activity module -> HotPOt -> View OR All actions
*
* This function is called from: {@link course/report/participation/index.php}
* @return array(string) of text strings used to log HotPot view actions
*/
function hotpot_get_view_actions() {
return array('view', 'index', 'report', 'review');
}
/*
* This function defines what log actions will be selected from the Moodle logs
* and displayed for course -> report -> activity module -> Hot Potatoes Quiz -> Post OR All actions
*
* This function is called from: {@link course/report/participation/index.php}
* @return array(string) of text strings used to log HotPot post actions
*/
function hotpot_get_post_actions() {
return array('submit');
}
/*
* For the given list of courses, this function creates an HTML report
* of which HotPot activities have been completed and which have not
* This function is called from: {@link course/lib.php}
*
* @param array(object) $courses records from the "course" table
* @param array(array(string)) $htmlarray array, indexed by courseid, of arrays, indexed by module name (e,g, "hotpot), of HTML strings
* each HTML string shows a list of the following information about each open HotPot in the course
* HotPot name and link to the activity + open/close dates, if any
* for teachers:
* how many students have attempted/completed the HotPot
* for students:
* which HotPots have been completed
* which HotPots have not been completed yet
* the time remaining for incomplete HotPots
* @return no return value is required, but $htmlarray may be updated
*/
function hotpot_print_overview($courses, &$htmlarray) {
global $CFG, $DB, $USER;
require_once($CFG->dirroot.'/mod/hotpot/locallib.php');
if (empty($CFG->hotpot_enablemymoodle)) {
return; // HotPots are not shown on MyMoodle on this site
}
if (! isset($courses) || ! is_array($courses) || ! count($courses)) {
return; // no courses
}
if (! $hotpots = get_all_instances_in_courses('hotpot', $courses)) {
return; // no hotpots
}
$strhotpot = get_string('modulename', 'mod_hotpot');
$strtimeopen = get_string('timeopen', 'mod_hotpot');
$strtimeclose = get_string('timeclose', 'mod_hotpot');
$strdateformat = get_string('strftimerecentfull');
$strattempted = get_string('attempted', 'mod_hotpot');
$strcompleted = get_string('completed', 'mod_hotpot');
$strnotattemptedyet = get_string('notattemptedyet', 'mod_hotpot');
$now = time();
foreach ($hotpots as $hotpot) {
if ($hotpot->timeopen > $now || $hotpot->timeclose < $now) {
continue; // skip activities that are not open, or are closed
}
$str = ''
.'<div class="hotpot overview">'
.'<div class="name">'.$strhotpot. ': '
.'<a '.($hotpot->visible ? '':' class="dimmed"')
.'title="'.$strhotpot.'" href="'.$CFG->wwwroot
.'/mod/hotpot/view.php?id='.$hotpot->coursemodule.'">'
.format_string($hotpot->name).'</a></div>'
;
if ($hotpot->timeopen) {
$str .= '<div class="info">'.$strtimeopen.': '.userdate($hotpot->timeopen, $strdateformat).'</div>';
}
if ($hotpot->timeclose) {
$str .= '<div class="info">'.$strtimeclose.': '.userdate($hotpot->timeclose, $strdateformat).'</div>';
}
$modulecontext = hotpot_get_context(CONTEXT_MODULE, $hotpot->coursemodule);
if (has_capability('mod/hotpot:reviewallattempts', $modulecontext)) {
// manager: show class grades stats