-
Notifications
You must be signed in to change notification settings - Fork 21
/
locallib.php
2296 lines (2008 loc) · 95.8 KB
/
locallib.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 mod_publication for Moodle - http://moodle.org/
//
// It 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.
//
// It 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/>.
/**
* Contains much of the logic needed for mod_publication
*
* @package mod_publication
* @author Philipp Hager
* @author Andreas Windbichler
* @copyright 2014 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
define('PUBLICATION_MODE_UPLOAD', 0);
define('PUBLICATION_MODE_IMPORT', 1);
// Used in DB to mark online-text-files!
define('PUBLICATION_MODE_ONLINETEXT', 2);
define('PUBLICATION_APPROVAL_GROUPAUTOMATIC', -1);
define('PUBLICATION_APPROVAL_ALL', 0);
define('PUBLICATION_APPROVAL_SINGLE', 1);
define('PUBLICATION_EVENT_TYPE_DUE', 'due');
define('PUBLICATION_FILTER_NOFILTER', 'nofilter');
define('PUBLICATION_FILTER_ALLFILES', 'allfiles');
define('PUBLICATION_FILTER_APPROVED', 'approved');
define('PUBLICATION_FILTER_REJECTED', 'rejected');
define('PUBLICATION_FILTER_APPROVALREQUIRED', 'approvalrequired');
define('PUBLICATION_FILTER_NOFILES', 'nofiles');
define('PUBLICATION_MODE_FILEUPLOAD', 'fileupload');
define('PUBLICATION_MODE_ASSIGN_TEAMSUBMISSION', 'teamsubmission');
define('PUBLICATION_MODE_ASSIGN_IMPORT', 'import');
define('PUBLICATION_NOTIFY_NONE', 0);
define('PUBLICATION_NOTIFY_TEACHER', 1);
define('PUBLICATION_NOTIFY_STUDENT', 2);
define('PUBLICATION_NOTIFY_ALL', 3);
define('PUBLICATION_NOTIFY_STATUSCHANGE', 'status');
define('PUBLICATION_NOTIFY_FILECHANGE', 'file');
require_once($CFG->dirroot . '/mod/publication/mod_publication_allfiles_form.php');
/**
* publication class contains much logic used in mod_publication
*
* @package mod_publication
* @author Philipp Hager
* @author Andreas Windbichler
* @copyright 2014 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class publication {
// TODO replace $instance with proper properties + PHPDoc comments?!?
/** @var object instance */
protected $instance;
/** @var object context */
protected $context;
/** @var object course */
protected $course;
/** @var object coursemodule */
protected $coursemodule;
/** @var bool requiregroup if mode = import and group membership is required for submission in assign to import from */
protected $requiregroup = 0;
protected $mode;
protected $allfilespage = false;
protected $teamsubmission = false;
protected static $pendingnotifications = [];
/**
* Constructor
*
* @param object $cm course module object
* @param object $course (optional) course object
* @param context_module $context (optional) Course Module Context
*/
public function __construct($cm, $course = null, $context = null) {
global $DB;
$this->coursemodule = $cm;
if ($course != null) {
$this->course = $course;
} else {
$this->course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
}
if ($context != null) {
$this->context = $context;
} else {
$this->context = context_module::instance($cm->id);
}
$this->instance = $DB->get_record("publication", ["id" => $cm->instance]);
// $this->instance->obtainteacherapproval = !$this->instance->obtainteacherapproval;
if ($this->instance->mode == PUBLICATION_MODE_IMPORT) {
$cond = ['id' => $this->instance->importfrom];
$this->requiregroup = $DB->get_field('assign', 'preventsubmissionnotingroup', $cond);
$this->teamsubmission = $DB->get_field('assign', 'teamsubmission', $cond);
}
if ($this->get_instance()->mode == PUBLICATION_MODE_UPLOAD) {
$this->mode = PUBLICATION_MODE_FILEUPLOAD;
} else if ($this->teamsubmission) {
$this->mode = PUBLICATION_MODE_ASSIGN_TEAMSUBMISSION;
} else {
$this->mode = PUBLICATION_MODE_ASSIGN_IMPORT;
}
}
/**
* Whether or not to show intro text right now
*
* @return bool
*/
public function show_intro() {
if ($this->get_instance()->alwaysshowdescription ||
time() > $this->get_instance()->allowsubmissionsfromdate) {
return true;
}
return false;
}
/**
* Display the intro text if available
*/
public function display_intro() {
global $OUTPUT;
if ($this->show_intro()) {
if ($this->instance->intro) {
echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
echo format_module_intro('publication', $this->instance, $this->coursemodule->id);
echo $OUTPUT->box_end();
}
} else {
if ($this->alwaysshowdescription) {
$message = get_string('allowsubmissionsfromdatesummary',
'publication', userdate($this->instance->allowsubmissionsfromdate));
} else {
$message = get_string('allowsubmissionsanddescriptionfromdatesummary',
'publication', userdate($this->instance->allowsubmissionsfromdate));
}
echo html_writer::div($message, '', ['id' => 'intro']);
}
}
/**
* Display dates which limit submission timespan
*/
public function display_availability() {
global $USER, $OUTPUT;
// Display availability dates.
$textsuffix = ($this->instance->mode == PUBLICATION_MODE_IMPORT) ? "_import" : "_upload";
echo $OUTPUT->box_start('generalbox boxaligncenter', 'dates');
echo '<table>';
if ($this->instance->allowsubmissionsfromdate) {
echo '<tr><td class="c0">' . get_string('allowsubmissionsfromdate' . $textsuffix, 'publication') . ':</td>';
echo ' <td class="c1">' . userdate($this->instance->allowsubmissionsfromdate) . '</td></tr>';
}
if ($this->instance->duedate) {
echo '<tr><td class="c0">' . get_string('duedate' . $textsuffix, 'publication') . ':</td>';
echo ' <td class="c1">' . userdate($this->instance->duedate) . '</td></tr>';
}
$extensionduedate = $this->user_extensionduedate($USER->id);
if ($extensionduedate) {
echo '<tr><td class="c0">' . get_string('extensionto', 'publication') . ':</td>';
echo ' <td class="c1">' . userdate($extensionduedate) . '</td></tr>';
}
echo '</table>';
echo $OUTPUT->box_end();
}
/**
* If the mode is set to import then the link to the corresponding
* assignment will be displayed
*/
public function get_importlink() {
global $DB, $OUTPUT;
if ($this->instance->mode == PUBLICATION_MODE_IMPORT) {
$context = new stdClass;
if ($this->get_instance()->importfrom == -1) {
$context->notset = true;
} else {
$assign = $DB->get_record('assign', ['id' => $this->instance->importfrom]);
$assignmoduleid = $DB->get_field('modules', 'id', ['name' => 'assign']);
if ($assign) {
$assigncm = $DB->get_record('course_modules', [
'course' => $assign->course,
'module' => $assignmoduleid,
'instance' => $assign->id,
]);
} else {
$assigncm = false;
}
if ($assign && $assigncm) {
$assignurl = new moodle_url('/mod/assign/view.php', ['id' => $assigncm->id]);
$context->assign = true;
$context->name = $assign->name;
$context->url = $assignurl->out(false);
} else {
$context->notfound = true;
}
}
return $OUTPUT->render_from_template('mod_publication/partial_assignlink', $context);
}
return null;
}
/**
* Display Link to upload form if submission date is open
* and the user has the capability to upload files
*
* @return string HTML snippet with upload link (single button or plain text if not allowed)
*/
public function display_uploadlink() {
global $OUTPUT;
if ($this->instance->mode == PUBLICATION_MODE_UPLOAD) {
if (has_capability('mod/publication:upload', $this->context)) {
if ($this->is_open()) {
$url = new moodle_url('/mod/publication/upload.php',
['id' => $this->instance->id, 'cmid' => $this->coursemodule->id]);
$label = get_string('edit_uploads', 'publication');
$editbutton = $OUTPUT->single_button($url, $label);
return $editbutton;
} else {
return get_string('edit_timeover', 'publication');
}
} else {
return get_string('edit_notcapable', 'publication');
}
}
}
/**
* Get the extension due date (if set)
*
* @param int $uid User ID to fetch extension due date for
* @return int extension due date if set or 0
*/
public function user_extensionduedate($uid) {
global $DB;
$extensionduedate = $DB->get_field('publication_extduedates', 'extensionduedate', [
'publication' => $this->get_instance()->id,
'userid' => $uid,
]);
if (!$extensionduedate) {
return 0;
}
return $extensionduedate;
}
public function set_allfilespage($allfilespage) {
$this->allfilespage = $allfilespage;
}
public function get_allfilespage() {
return $this->allfilespage;
}
/**
* Check if submission is currently allowed due to allowsubmissionsfromdae and duedate
*
* @return bool
*/
public function is_open() {
global $USER;
if (!has_capability('mod/publication:upload', $this->get_context())) {
return false;
}
$now = time();
$from = $this->get_instance()->allowsubmissionsfromdate;
$due = $this->get_instance()->duedate;
$extensionduedate = $this->user_extensionduedate($USER->id);
if ($extensionduedate) {
$due = $extensionduedate;
}
if (($from == 0 || $from < $now) &&
($due == 0 || $due > $now)) {
return true;
}
return false;
}
public function is_approval_open() {
global $USER;
$now = time();
$from = $this->get_instance()->approvalfromdate;
$to = $this->get_instance()->approvaltodate;
$extensionduedate = $this->user_extensionduedate($USER->id);
if ($to != 0 && $extensionduedate) {
$to = $extensionduedate;
}
if (($from == 0 || $from < $now) && ($to == 0 || $to > $now)) {
return true;
}
return false;
}
public function is_approval_open_string() {
$fromstr = '';
if ($this->get_instance()->approvalfromdate > 0) {
$fromstr = get_string('from') . ' ' . userdate($this->get_instance()->approvalfromdate);
}
$tostr = '';
if ($this->get_instance()->approvaltodate > 0) {
$tostr = get_string('until') . ' ' . userdate($this->get_instance()->approvaltodate);
}
return $fromstr . ' ' . $tostr;
}
/**
* Instance getter
*
* @return object instance object
*/
public function get_instance() {
return $this->instance;
}
/**
* Context getter
*
* @return \context_module context object
*/
public function get_context() {
return $this->context;
}
/**
* Coursemodule getter
*
* @return object coursemodule object
*/
public function get_coursemodule() {
return $this->coursemodule;
}
/**
* Whether or not the assign to import from requires group membership for submissions!
*
* @return bool true if group membership is required, false if not or type = upload
*/
public function requiregroup() {
return $this->requiregroup;
}
/**
* Get's all groups (optionaly filtered by groupingid or group-IDs in selgroups-array)
*
* @param int $groupingid (optional) Grouping-ID to filter groups for or 0
* @param int[] $selgroups (optional) selected group's IDs to filter for or empty array()
* @return int[] array of group's IDs
*/
public function get_groups($groupingid = 0, $selgroups = []) {
$groups = groups_get_all_groups($this->get_instance()->course, 0, $groupingid);
$groups = array_keys($groups);
if (!$this->requiregroup()) {
$groups[] = 0;
}
if (is_array($selgroups) && count($selgroups) > 0) {
$groups = array_intersect($groups, $selgroups);
}
foreach ($groups as $id => $groupid) {
$members = $this->get_submissionmembers($groupid);
if (empty($members)) {
unset($groups[$id]);
}
}
return $groups;
}
/**
* Get userids to fetch files for, when displaying all submitted files or downloading them as ZIP
*
* @param int[] $users (optional) user ids for which the returned user ids have to filter
* @return int[] array of userids
*/
public function get_users($users = [], $ignoreallfilespage = false) {
global $DB;
$customusers = '';
if (is_array($users) && count($users) > 0) {
$customusers = " and u.id IN (" . implode(', ', $users) . ") ";
} else if ($users === false) {
return [];
}
// Find out current groups mode.
$currentgroup = groups_get_activity_group($this->get_coursemodule(), true);
// Get all ppl that are allowed to submit assignments.
list($esql, $params) = get_enrolled_sql($this->context, 'mod/publication:view', $currentgroup);
$allfilespage = $ignoreallfilespage || $this->allfilespage;
if ($allfilespage && (has_capability('mod/publication:approve', $this->context)
|| has_capability('mod/publication:grantextension', $this->context))) {
// We can skip the approval-checks for teachers!
$sql = 'SELECT u.* FROM {user} u ' .
'LEFT JOIN (' . $esql . ') eu ON eu.id=u.id ' .
'WHERE u.deleted = 0 AND eu.id=u.id ' . $customusers;
} else {
$sql = 'SELECT u.* FROM {user} u ' .
'LEFT JOIN (' . $esql . ') eu ON eu.id=u.id ' .
'LEFT JOIN {publication_file} files ON (u.id = files.userid) ' .
'WHERE u.deleted = 0 AND eu.id=u.id ' . $customusers .
'AND files.publication = ' . $this->get_instance()->id . ' ';
$where = '';
if ($this->get_instance()->obtainteacherapproval == 1) {
// Need teacher approval.
$where = 'files.teacherapproval = 1';
} else {
// No need for teacher approval.
// Teacher only hasnt rejected.
//$where = '(files.teacherapproval = 1 OR files.teacherapproval IS NULL)';
}
if ($this->get_instance()->obtainstudentapproval == 1) {
// No need to ask student and teacher has approved.
if (mb_strlen($where) > 0) {
$where .= ' AND ';
}
$where .= 'files.studentapproval = 1';
} else {
// Student and teacher have approved.
//$where = 'files.teacherapproval = 1 AND files.studentapproval = 1';
}
/*if ($this->get_instance()->mode == PUBLICATION_MODE_UPLOAD) {
// Mode upload.
} else {
// TODO group mode!
// Mode import.
}*/
if (mb_strlen($where) > 0) {
$sql .= 'AND ' . $where . ' ';
}
$sql .= 'GROUP BY u.id';
}
$users = $DB->get_records_sql($sql, $params);
if (empty($users)) {
return [-1];
}
$modinfo = get_fast_modinfo($this->course->id);
$info = new \core_availability\info_module($modinfo->get_cm($this->coursemodule->id));
$filtered = $info->filter_user_list($users);
if (empty($filtered)) {
return [-1];
}
return array_keys($filtered);
}
public function get_mode() {
return $this->mode;
}
public function get_allfilestable($filter, $ignoreallfilespage = false) {
global $DB;
$mode = $this->get_mode();
$oldallfilespage = $this->allfilespage;
if ($ignoreallfilespage) {
$this->allfilespage = true;
}
$uniqueid = \mod_publication\local\allfilestable\base::get_table_uniqueid($this->instance->id);
if ($mode == PUBLICATION_MODE_FILEUPLOAD) {
$table = new \mod_publication\local\allfilestable\upload($uniqueid . $this->coursemodule->id, $this, $filter);
} else if ($mode == PUBLICATION_MODE_ASSIGN_TEAMSUBMISSION) {
$table = new \mod_publication\local\allfilestable\group($uniqueid, $this, $filter);
} else {
$table = new \mod_publication\local\allfilestable\import($uniqueid, $this, $filter);
}
$this->allfilespage = $oldallfilespage;
return $table;
}
public function get_filestable() {
global $DB;
$mode = $this->get_mode();
if ($mode == PUBLICATION_MODE_FILEUPLOAD) {
$table = new \mod_publication\local\filestable\upload($this);
} else if ($mode == PUBLICATION_MODE_ASSIGN_TEAMSUBMISSION) {
$table = new \mod_publication\local\filestable\group($this);
} else {
$table = new \mod_publication\local\filestable\import($this);
}
return $table;
}
/**
* Display form with table containing all files
*
* TODO: for Moodle 3.6 we should replace old form classes with a nice bootstrap based form layout!
*/
public function display_allfilesform() {
global $CFG, $DB;
$output = '';
$cm = $this->coursemodule;
$context = $this->context;
$updatepref = optional_param('updatepref', 0, PARAM_BOOL);
if ($updatepref) {
$perpage = optional_param('perpage', 10, PARAM_INT);
$perpage = ($perpage < 0) ? 10 : $perpage;
set_user_preference('mod-publication-perpage-' . $this->instance->id, $perpage);
}
// Next we get perpage param from database!
$perpage = get_user_preferences('mod-publication-perpage-' . $this->instance->id, 10);
$filter = optional_param('filter', PUBLICATION_FILTER_NOFILTER, PARAM_ALPHANUMEXT);
$page = optional_param('page', 0, PARAM_INT);
$formattrs = [];
$formattrs['action'] = new moodle_url('/mod/publication/view.php', ['allfilespage' => $this->allfilespage]);
$formattrs['id'] = 'fastg';
$formattrs['method'] = 'post';
$formattrs['class'] = 'mform';
$output .= html_writer::start_tag('form', $formattrs) .
html_writer::empty_tag('input', [
'type' => 'hidden',
'name' => 'id',
'value' => $this->get_coursemodule()->id,
]) .
html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'page', 'value' => $page]) .
html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]) .
html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'filter', 'value' => $filter]);
$output .= html_writer::start_tag('fieldset', ['class' => 'clearfix collapsible', 'id' => 'id_allfiles']);
$allfiles = get_string('allfiles', 'publication');
$publicfiles = get_string('publicfiles', 'publication');
$title = (has_capability('mod/publication:approve', $context) && $this->allfilespage) ? $allfiles : $publicfiles;
$output .= html_writer::tag('legend', $title, ['class' => 'ftoggler h3']);
$output .= html_writer::start_div('fcontainer clearfix mb-3');
$f = groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/publication/view.php?id=' . $cm->id, true);
$mf = new mod_publication_allfiles_form(null, array('form' => $f));
$output .= $mf->render();
$table = $this->get_allfilestable($filter);
ob_start();
$table->out($perpage, true); // Print the whole table.
$tableoutput = ob_get_contents();
ob_end_clean();
$norowsfound = $table->get_count() == 0;
$nofilesfound = $table->get_totalfilescount() == 0;
$link = html_writer::link(new moodle_url('/mod/publication/view.php', [
'id' => $this->coursemodule->id,
'action' => 'zip',
'allfilespage' => $this->allfilespage,
]),
get_string('downloadall', 'publication'),
['class' => 'btn btn-secondary mb-2 btn-sm']
);
if (!$norowsfound && !$nofilesfound) {
$output .= html_writer::tag('div', $link, ['class' => 'mod-publication-download-link']);
}
if ($perpage == 0) {
$output .= '<style> nav.pagination ul.pagination li:only-child { display: none} </style>';
}
$output .= $tableoutput;
$options = [];
$options['zipusers'] = get_string('zipusers', 'publication');
if (has_capability('mod/publication:approve', $context) && $table->totalfiles() > 0 && $this->allfilespage) {
if ($this->get_instance()->obtainteacherapproval) {
$options['approveusers'] = get_string('approveusers', 'publication');
$options['rejectusers'] = get_string('rejectusers', 'publication');
}
if ($this->get_instance()->obtainstudentapproval) {
$options['resetstudentapproval'] = get_string('resetstudentapproval', 'publication');
}
}
if (has_capability('mod/publication:grantextension', $this->get_context()) && $this->allfilespage) {
$options['grantextension'] = get_string('grantextension', 'publication');
}
if (count($options) > 0 && !$norowsfound && !$nofilesfound) {
$output .= html_writer::start_div('form-row');
if (has_capability('mod/publication:approve', $context) && $this->allfilespage) {
$buttons = html_writer::empty_tag('input', [
'type' => 'reset',
'name' => 'resetvisibility',
'value' => get_string('reset', 'publication'),
'class' => 'visibilitysaver btn btn-secondary ml-1',
]);
if ($this->get_instance()->mode == PUBLICATION_MODE_IMPORT &&
$this->get_instance()->obtainstudentapproval) {
$buttons .= html_writer::empty_tag('input', [
'type' => 'submit',
'name' => 'savevisibility',
'value' => get_string('saveapproval', 'publication'),
'class' => 'visibilitysaver btn btn-primary',
]);
} else {
$buttons .= html_writer::empty_tag('input', [
'type' => 'submit',
'name' => 'savevisibility',
'value' => get_string('saveteacherapproval', 'publication'),
'class' => 'visibilitysaver btn btn-primary',
]);
}
} else {
$buttons = '';
}
$output .= html_writer::start_div('withselection col-7').
html_writer::span(get_string('withselected', 'publication')).
html_writer::select($options, 'action').
html_writer::empty_tag('input', [
'type' => 'submit',
'name' => 'submitgo',
'value' => get_string('go', 'publication'),
'class' => 'btn btn-primary',
]).html_writer::end_div().
html_writer::div($buttons, 'col');
}
// Select all/none.
$output .= html_writer::start_tag('div', ['class' => 'checkboxcontroller']) . "
<script type=\"text/javascript\">
function toggle_userselection() {
var checkboxes = document.getElementsByClassName('userselection');
var sel = document.getElementById('selectallnone');
if (checkboxes.length > 0) {
checkboxes[0].checked = sel.checked;
for(var i = 1; i < checkboxes.length;i++) {
checkboxes[i].checked = checkboxes[0].checked;
}
}
}
</script>" .
html_writer::end_div() .
html_writer::end_div() .
html_writer::end_tag('fieldset') .
html_writer::end_tag('form');
// Mini form for setting user preference.
$formaction = new moodle_url('/mod/publication/view.php', ['id' => $this->coursemodule->id, 'allfilespage' => $this->allfilespage]);
$mform = new MoodleQuickForm('optionspref', 'post', $formaction, '', ['class' => 'optionspref']);
$attributes = [];
$attributes['onChange'] = "$('form.optionspref').submit();";
$mform->addElement('hidden', 'updatepref');
$mform->setDefault('updatepref', 1);
$mform->addElement('header', 'qgprefs', get_string('optionalsettings', 'publication'));
$mform->addElement('select', 'perpage', get_string('entiresperpage', 'publication'), [
0 => get_string('all'),
3 => 3,
10 => 10,
20 => 20,
50 => 50,
100 => 100,
], $attributes);
$mform->setDefault('perpage', $perpage);
if (has_capability('mod/publication:approve', $context) && $this->allfilespage) {
$filteroptions = [
PUBLICATION_FILTER_NOFILTER => get_string('filter:' . PUBLICATION_FILTER_NOFILTER, 'publication'),
PUBLICATION_FILTER_ALLFILES => get_string('filter:' . PUBLICATION_FILTER_ALLFILES, 'publication'),
];
if ($this->get_instance()->obtainteacherapproval || $this->get_instance()->obtainstudentapproval) {
$filteroptions += [
PUBLICATION_FILTER_APPROVED => get_string('filter:' . PUBLICATION_FILTER_APPROVED, 'publication'),
PUBLICATION_FILTER_REJECTED => get_string('filter:' . PUBLICATION_FILTER_REJECTED, 'publication'),
PUBLICATION_FILTER_APPROVALREQUIRED => get_string('filter:' . PUBLICATION_FILTER_APPROVALREQUIRED, 'publication'),
];
}
$filteroptions += [
PUBLICATION_FILTER_NOFILES => get_string('filter:' . PUBLICATION_FILTER_NOFILES, 'publication'),
];
$mform->addElement('select', 'filter', get_string('filter', 'publication'), $filteroptions, $attributes);
$mform->setDefault('filter', $filter);
}
$mform->disable_form_change_checker();
$output .= $mform->toHtml();
return $output;
}
/**
* Returns if a user has the permission to view a file
*
* @param int $fileid
* @param number $userid use for custom user, if 0 then if public visible
* @return boolean
*/
public function has_filepermission($fileid, $userid = 0) {
global $DB;
$conditions = [];
$conditions['publication'] = $this->get_instance()->id;
$conditions['fileid'] = $fileid;
$filepermissions = $DB->get_record('publication_file', $conditions);
$haspermission = false;
if ($filepermissions) {
if ($userid != 0) {
if ($this->get_instance()->mode == PUBLICATION_MODE_UPLOAD && $filepermissions->userid == $userid) {
// Everyone is allowed to view their own files.
$haspermission = true;
} else if ($this->get_instance()->mode == PUBLICATION_MODE_IMPORT) {
// If it's a team-submission, we have to check for the group membership!
$teamsubmission = $this->teamsubmission;
if (!empty($teamsubmission)) {
$groupmembers = $this->get_submissionmembers($filepermissions->userid);
if (array_key_exists($userid, $groupmembers)) {
$haspermission = true;
}
} else if ($filepermissions->userid == $userid) {
// Everyone is allowed to view their own files.
$haspermission = true;
}
}
}
$obtainteacherapproval = $this->get_instance()->obtainteacherapproval;
$obtainstudentapproval = $this->get_instance()->obtainstudentapproval;
$teacherapproval = $filepermissions->teacherapproval;
$studentapproval = $filepermissions->studentapproval;
$haspermission = $haspermission || ((!$obtainteacherapproval || $teacherapproval == 1) && (!$obtainstudentapproval || $studentapproval == 1));
/*
if ($this->get_instance()->mode == PUBLICATION_MODE_UPLOAD) {
// Mode upload.
if ($this->get_instance()->obtainteacherapproval) {
// Need teacher approval.
if ($filepermissions->teacherapproval == 1) {
// Teacher has approved.
$haspermission = true;
}
} else {
// No need for teacher approval.
if (is_null($filepermissions->teacherapproval) || $filepermissions->teacherapproval == 1) {
// Teacher only hasnt rejected.
$haspermission = true;
}
}
} else {
// Mode import.
if (!$this->get_instance()->obtainstudentapproval && $filepermissions->teacherapproval == 1) {
// No need to ask student and teacher has approved.
$haspermission = true;
} else if ($this->get_instance()->obtainstudentapproval &&
$filepermissions->teacherapproval == 1 && $filepermissions->studentapproval == 1) {
// Student and teacher have approved.
$haspermission = true;
}
}*/
}
return $haspermission;
}
/**
* Sets group approval for the specified user and returns current cumulated group approval!
*
* @param null|int $approval 0 if rejected, 1 if approved and 'null' if not set!
* @param int $pubfileid ID of publication file entry in DB
* @param int $userid ID of user to set approval/rejection for
* @return array cumulated approval for specified file, approving and needed count
* @throws coding_exception
* @throws dml_exception
*/
public function set_group_approval($approval, $pubfileid, $userid) {
global $DB;
// Normalize approval value!
/*if ($approval !== null) {
$approval = empty($approval) ? 0 : 1;
}*/
$approvalforgroupapproval = $approval == 1 ? 1 : 0; // $approval == 2 => $approvalforgroupapproval = 0...
$record = $DB->get_record('publication_groupapproval', ['fileid' => $pubfileid, 'userid' => $userid]);
$filerec = $DB->get_record('publication_file', ['id' => $pubfileid]);
if (!empty($record)) {
if ($record->approval === $approvalforgroupapproval) {
// Nothing changed, return!
return $filerec->studentapproval;
}
$record->approval = $approvalforgroupapproval;
$record->timemodified = time();
$DB->update_record('publication_groupapproval', $record);
} else {
$record = new stdClass();
$record->fileid = $pubfileid;
$record->userid = $userid;
$record->approval = $approvalforgroupapproval;
$record->timecreated = time();
$record->timemodified = $record->timecreated;
$record->id = $DB->insert_record('publication_groupapproval', $record);
}
// Calculate new cumulated studentapproval for caching in file table!
// Get group members!
$groupmembers = $this->get_submissionmembers($filerec->userid);
$stats = array();
$stats['approving'] = 0;
$stats['needed'] = count($groupmembers);
if (!empty($groupmembers)) {
list($usersql, $userparams) = $DB->get_in_or_equal(array_keys($groupmembers), SQL_PARAMS_NAMED, 'user');
$select = "fileid = :fileid AND approval = :approval AND userid " . $usersql;
$params = ['fileid' => $pubfileid, 'approval' => 0] + $userparams;
if ($DB->record_exists_select('publication_groupapproval', $select, $params)) {
// If anyone rejected it's rejected, no matter what!
$approval = 2; // 2 is rejected...
} else {
if ($this->get_instance()->groupapproval == PUBLICATION_APPROVAL_SINGLE) {
// If only one has to approve, we check for that!
$params['approval'] = 1;
if ($DB->record_exists_select('publication_groupapproval', $select, $params)) {
$approval = 1;
} else {
$approval = 0;
}
} else {
// All group members have to approve!
$select = "fileid = :fileid AND approval IS NULL AND userid " . $usersql;
$params = ['fileid' => $pubfileid] + $userparams;
$approving = $DB->count_records_sql("SELECT count(DISTINCT userid)
FROM {publication_groupapproval}
WHERE fileid = :fileid AND approval = 1 AND userid " . $usersql, $params);
$stats['approving'] = $approving;
$stats['needed'] = count($userparams);
if ($approving < count($userparams)) {
// Rejected if not every group member has approved the file!
$approval = 0;
} else {
$approval = 1;
}
}
}
} else {
// Group without members, so no one could approve! (Should never happen, never ever!)
$approval = 2;
}
// Update approval value and return it!
$filerec->studentapproval = $approval;
$DB->update_record('publication_file', $filerec);
$stats['approval'] = $approval;
return $stats;
}
/**
* Determine and return the teacher's approval status for the given file!
*
* @param stored_file $file file to determine approval status for
* @return int|null teacher's approval status (null pending, 1 approved, all other rejected)
*/
public function teacher_approval(\stored_file $file) {
global $DB;
if (empty($conditions)) {
static $conditions = [];
$conditions['publication'] = $this->get_instance()->id;
}
$conditions['fileid'] = $file->get_id();
$teacherapproval = $DB->get_field('publication_file', 'teacherapproval', $conditions);
$obtainteacherapproval = $this->get_instance()->obtainteacherapproval;
if (!$obtainteacherapproval) {
return 1;
}
return $teacherapproval;
}
/**
* Determine and return the student's approval status for the given file!
*
* @param stored_file $file file to determine approval status for
* @return int|null student's approval status (null/0 = pending, 1 = rejected, 2 = approved)
*/
public function student_approval(\stored_file $file) {
global $DB;
if (empty($conditions)) {
static $conditions = [];
$conditions['publication'] = $this->get_instance()->id;
}
$conditions['fileid'] = $file->get_id();
$studentapproval = $DB->get_field('publication_file', 'studentapproval', $conditions);
//$studentapproval = (!is_null($studentapproval)) ? $studentapproval + 1 : null;
return $studentapproval;
}
/**
* Gets the group members for the specified group. Or users without membership if groupid is 0!
*
* @param int $groupid
* @return stdClass[] Group member's user records.
*/
public function get_submissionmembers($groupid) {
global $DB;
static $availabilityinfo = null;
if (is_null($availabilityinfo)) {
$modinfo = get_fast_modinfo($this->course->id);
$availabilityinfo = new \core_availability\info_module($modinfo->get_cm($this->coursemodule->id));
}
if ($this->mode != PUBLICATION_MODE_ASSIGN_TEAMSUBMISSION) {
throw new coding_exception('Cannot be called if files get uploaded or teamsubmission is deactivated!');
}
if (!empty($groupid)) {
$groupmembers = groups_get_members($groupid);
} else if (!$DB->get_field('assign', 'preventsubmissionnotingroup', ['id' => $this->get_instance()->importfrom])) {
// If groupid == 0, we get all users without group!
$groupmembers = [];
$assigncm = get_coursemodule_from_instance('assign', $this->instance->importfrom);
$context = context_module::instance($assigncm->id);
$users = get_enrolled_users($context, "mod/assign:submit", 0);
if (!empty($users)) {
foreach ($users as $user) {
$ugrps = groups_get_user_groups($this->instance->course, $user->id);
if (!count($ugrps[0])) {