forked from jcrodriguez-dis/moodle-mod_vpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpl.class.php
1678 lines (1586 loc) · 59 KB
/
vpl.class.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 VPL for Moodle - http://vpl.dis.ulpgc.es/
//
// VPL for 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.
//
// VPL for 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 VPL for Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* VPL class definition
*
* @package mod_vpl
* @copyright 2013 Juan Carlos Rodríguez-del-Pino
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Juan Carlos Rodríguez-del-Pino <[email protected]>
*/
/**
* Module instance files
* path= vpl_data/vpl_instance#
* General info
* path/required_files.lst
* path/required_files/
* path/execution_files.lst
* path/execution_files/
* path/execution_files/vpl_run.sh
* path/execution_files/vpl_debug.sh
* path/execution_files/vpl_evaluate.sh
* * Submission info
* path/usersdata/userid#/submissionid#/submissionfiles/
* path/usersdata/userid#/submissionid#/grade_comments.txt
* path/usersdata/userid#/submissionid#/teachertest.txt
* path/usersdata/userid#/submissionid#/studenttest.txt
*/
require_once dirname(__FILE__).'/filegroup.class.php';
require_once dirname(__FILE__).'/lib.php';
class file_group_execution extends file_group_process{
/**
* Name of fixed file names
*
* @var string[]
*/
static protected $base_files = array('vpl_run.sh','vpl_debug.sh', 'vpl_evaluate.sh', 'vpl_evaluate.cases');
/**
* Number of $base_files elements
*
* @var int
*/
static protected $num_base_files;
/**
* Constructor
*
* @param string $filelistname
* @param string $dir
*/
function __construct($filelistname,$dir){
self::$num_base_files=count(self::$base_files);
parent::__construct($filelistname,$dir,1000,self::$num_base_files);
}
/**
* Get list of files
*
* @return string[]
*/
function getFileList(){
return array_values(array_unique(array_merge(self::$base_files,parent::getFileList())));
}
/**
* Get the file comment by number
*
* @param int $num
* @return string
*/
function getFileComment($num){
if($num<self::$num_base_files){
return get_string(self::$base_files[$num],VPL);
}else{
return get_string('file').' '.($num+1-self::$num_base_files);
}
}
/**
* Get list of files to keep when running
*
* @return string[]
*/
function getFileKeepList(){
return vpl_read_list_from_file($this->filelistname.'.keep');
}
/**
* Set the file list to keep when running
*
* @param string[] $filelist
*/
function setFileKeepList($filelist){
vpl_write_list_to_file($this->filelistname.'.keep',$filelist);
}
}
class mod_vpl {
/**
* Internal var for course_module
* @var object $cm
*/
protected $cm;
/**
* Internal var for course
* @var object $course
*/
protected $course;
/**
* Internal var for vpl
* @var object $instance
*/
protected $instance;
/**
* Internal var object to requied file group manager
* @var object of file group manager
*/
protected $required_fgm;
/**
* Internal var object to execution file group manager
* @var object of file group manager
*/
protected $execution_fgm;
/**
* Constructor
* @param $id int optional course_module id
* @param $a int optional module instance id
*/
function __construct($id, $a=null) {
global $OUTPUT;
global $DB;
if($id){
if (! $this->cm = get_coursemodule_from_id(VPL,$id)) {
print_error('invalidcoursemodule');
}
if (! $this->course = $DB->get_record("course", array("id" => $this->cm->course))) {
print_error('unknowncourseidnumber','',$this->cm->course);
}
if (! $this->instance = $DB->get_record(VPL, array("id" => $this->cm->instance))) {
print_error('module instance id unknow');
}
}else{
if (! $this->instance = $DB->get_record(VPL, array("id" => $a))) {
print_error('module instance id unknow');
}
if (! $this->course = $DB->get_record("course", array("id" => $this->instance->course))) {
print_error('unknowncourseidnumber','',$this->instance->course);
}
if (! $this->cm = get_coursemodule_from_instance(VPL, $this->instance->id, $this->course->id)) {
echo $OUTPUT->box(get_string('invalidcoursemodule').' VPL id='.$a);
//Don't stop on error. This let delete a corrupted course.
}
}
$this->required_fgm = null;
$this->execution_fgm = null;
}
/**
* @return module db instance
**/
function get_instance(){
return $this->instance;
}
/**
* @return course
**/
function get_course(){
return $this->course;
}
/**
* @return course_module
**/
function get_course_module(){
return $this->cm;
}
/**
* Delete a vpl instance
*
* @return bool true if all OK
**/
function delete_all(){
global $DB;
//Delete all data files
vpl_delete_dir($this->get_data_directory());
//delete grade_item
vpl_delete_grade_item($this->instance);
//delete event
$DB->delete_records('event', array('modulename'=>VPL, 'instance'=>$this->instance->id));
//Delete all submissions records
$DB->delete_records('vpl_submissions',array('vpl' => $this->instance->id));
//delete vpl record
$DB->delete_records(VPL, array('id' => $this->instance->id));
// Allways true
return true;
}
/**
* @return instance data directory
**/
function get_data_directory(){
global $CFG;
return $CFG->dataroot.'/vpl_data/'.$this->instance->id;
}
/**
* @return instance config data directory
**/
function get_users_data_directory(){
return $this->get_data_directory().'/usersdata';
}
/**
* @return directory to stored initial required files
**/
function get_required_files_directory(){
return $this->get_data_directory().'/required_files/';
}
/**
* @return filename to store required files
**/
function get_required_files_filename(){
return $this->get_data_directory().'/required_files.lst';
}
/**
* @return array of files required name
**/
function get_required_files(){
return vpl_read_list_from_file($this->get_required_files_filename());
}
/**
* @param $files array of required files
**/
function set_required_files($files){
vpl_write_list_to_file($this->get_required_files_filename(),$files);
}
/**
* @return object file group manager for required files
**/
function get_required_fgm(){
if(!$this->required_fgm){
$this->required_fgm = new file_group_process($this->get_required_files_filename(),
$this->get_required_files_directory(), $this->instance->maxfiles);
}
return $this->required_fgm;
}
/**
* @return directory to stored execution files
**/
function get_execution_files_directory(){
return $this->get_data_directory().'/execution_files/';
}
/**
* @return filename to store execution files
**/
function get_execution_files_filename(){
return $this->get_data_directory().'/execution_files.lst';
}
/**
* @return array of files execution name
**/
function get_execution_files(){
return vpl_read_list_from_file($this->get_execution_files_filename());
}
/**
* @return object file group manager for execution files
**/
function get_execution_fgm(){
if(!$this->execution_fgm){
$this->execution_fgm = new file_group_execution($this->get_execution_files_filename(), $this->get_execution_files_directory());
}
return $this->execution_fgm;
}
//FIXME check and remove function
function set_initial_file($name, $files){
$filelist='';
$basepath = $this->get_submission_directory();
foreach($files as $file){
$name = basename($file['name']);
if($name>''){
if($filelist>''){
$filelist .= "\n";
}
$filelist .= $name;
$fp = vpl_fopen($basepath.$name);
fwrite($fp,$file['data']);
fclose($fp);
}
}
$fp = vpl_fopen($this->get_submissionfilelistname());
fwrite($fp,$filelist);
fclose($fp);
}
/**
* get instance name with groupping name if available
*
* @return string with name+(grouping name)
**/
function get_printable_name(){
global $CFG;
$ret = $this->instance->name;
if(!empty($CFG->enablegroupings) && ($this->cm->groupingid>0)) {
$grouping = groups_get_grouping($this->cm->groupingid);
if($grouping !== false){
$ret .= ' ('.$grouping->name.')';
}
}
return $ret;
}
/**
* get fulldescription
*
* @return fulldescription
**/
function get_fulldescription(){
$instance = $this->get_instance();
if($instance->intro){
return format_module_intro(VPL,$this->get_instance(),
$this->get_course_module()->id);
}else{
return '';
}
}
/**
* get fulldescription adding basedon
*
* @return fulldescription
**/
function get_fulldescription_with_basedon(){
$ret='';
if($this->instance->basedon){ //Show recursive varaitions
$basevpl = new mod_vpl(false,$this->instance->basedon);
$ret .= $basevpl->get_fulldescription_with_basedon();
}
return $ret.$this->get_fulldescription();
}
/**
* Return maximum file size allowed
* @return int
**/
function get_maxfilesize(){
$plugincfg = get_config('mod_vpl');
$max = vpl_get_max_post_size();
if($plugincfg->maxfilesize > 0 && $plugincfg->maxfilesize < $max){
$max = $plugincfg->maxfilesize;
}
if($this->instance->maxfilesize>0
&& $this->instance->maxfilesize<$max){
$max=$this->instance->maxfilesize;
}
return $max;
}
/**
* Get grading information help
* @return string grade comments summary in html format
**/
function get_grading_help(){
$list=array();
$submissions=$this->all_last_user_submission();
foreach($submissions as $submission){
$sub = new mod_vpl_submission($this,$submission);
$sub->filter_feedback($list);
}
//TODO show evaluation criteria with show hidde button
$all= array();
foreach($list as $text => $info){
$astext = s(addslashes_js($text));
$html='';
$html.=s($text);
foreach($info->grades as $grade=>$nothing){
if($grade >=0){ //No grade
$jscript='VPL.addComment(\''.$astext.'\')';
}else{
$jscript='VPL.addComment(\''.$astext.' ('.$grade.')\')';
}
$link='<a href="javascript:void(0)" onclick="'.$jscript.'">'.$grade.'</a>';
$html.=' ('.$link.')';
}
$html.='<br />'."\n";
if(isset($all[$info->count])){
$all[$info->count] .= '('.$info->count.') '.$html;
}else{
$all[$info->count] = '('.$info->count.') '.$html;
}
}
//Sort comments by number of occurrences
krsort($all);
$html='';
foreach($all as $count => $info){
$html.=$info;
}
//TODO show info about others review with show hidde button
return $html;
}
/**
* Get password
**/
function get_password(){
return trim($this->instance->password);
}
/**
* Get password md5
**/
function get_password_md5(){
return md5($this->instance->id.(sesskey()));
}
/**
* Check if pass password restriction
**/
function pass_password_check($pass_set=''){
$password = $this->get_password();
if($password >''){
global $SESSION;
$password_md5 = $this->get_password_md5();
$pasvar = 'vpl_password_'.$this->instance->id;
if(isset($SESSION->$pasvar) && $SESSION->$pasvar == $password_md5){
return true;
}
if($pass_set == ''){
$pass_set=optional_param('password','',PARAM_TEXT);
}
if($pass_set>''){
if($pass_set == $password){
$SESSION->$pasvar=$password_md5;
unset($SESSION->vpl_attempt_number);
return true;
}
if(isset($SESSION->vpl_attempt_number)){
$SESSION->vpl_attempt_number++;
}else{
$SESSION->vpl_attempt_number=1;
}
sleep($SESSION->vpl_attempt_number); //Wait vpl_attempt_number seg to limit force brute crack
}
return false;
}
return true;
}
/**
* Check password restriction
**/
function password_check(){
if(!$this->pass_password_check()){
require_once('forms/password_form.php');
$this->print_header();
$mform = new mod_vpl_password_form($_SERVER['SCRIPT_NAME']);
$mform->display();
$this->print_footer();
die;
}
}
/**
* Check netword restriction and return true o false
* Network field may be a combination of ip, netmask and domain
* comma separated to check.
**/
function pass_network_check($netreq=null, $ip=null){
if($netreq===null){
$netreq=$this->instance->requirednet;
}
$netreq = trim($netreq);
if($netreq ==''){ // No net required
return true;
}
if($ip===null){
$ip=getremoteaddr();
}
return address_in_subnet($ip,$netreq);
}
/**
* Check netword restriction and show error if not passed
**/
function network_check(){
global $OUTPUT;
if(!$this->pass_network_check()){
$this->print_header();
echo $OUTPUT->box(get_string('opnotallowfromclient',VPL).' '.getremoteaddr());
$this->print_footer();
die;
}
}
/**
* Check submission restriction
* @param $data Object with submitted data
* @param & $error string
* @return bool
**/
function pass_submission_restriction(& $alldata,& $error){
$max = $this->get_maxfilesize();
$rfn = $this->get_required_fgm();
$list = $rfn->getFilelist();
$error='';
if(count($alldata)>$this->instance->maxfiles){
$error.= get_string('maxfilesexceeded',VPL)."\n";
}
$lr=count($list);
$lad=count($alldata);
for($i=0; $i< $lad ; $i++){
$name = $alldata[$i]['name'];
$data = $alldata[$i]['data'];
if(strlen($data)>$max){
$error .= '"'.s($name).'" '.get_string('maxfilesizeexceeded',VPL)."<br />";
}
if(!vpl_is_valid_path_name($name)){
$error .= '"'.s($name).'" '.get_string('incorrect_file_name',VPL)."<br />";
}
if($i<$lr && $list[$i] != $name){
$a = new stdClass();
$a->expected = $list[$i];
$a->found = $name;
$error .= s(get_string('unexpected_file_name',VPL,$a))."<br />";
}
}
return strlen($error)==0;
}
/**
* Check and submission
* @param $userid
* @param $data Object with submitted data
* @param & $error string
* @return false or submission id
**/
function add_submission($userid, & $files, $comments, & $error){
global $USER,$DB;
if(!$this->pass_submission_restriction($files,$error)){
return false;
}
if($this->is_group_activity()){
$liderid=$this->get_group_leaderid($userid);
if($liderid==0){ //No group or inconsistence
if($this->has_capability(VPL_MANAGE_CAPABILITY) ||
($this->has_capability(VPL_GRADE_CAPABILITY) && ($userid == $USER->id)))
{ //Is manager or grader own submission
$liderid = $userid;
}else{
$error=get_string('notsaved',VPL)."\n".get_string('inconsistentgroup',VPL);
return false;
}
}
$userid=$liderid;
}
//Grader submmission or group activity
if($USER->id != $userid || $this->is_group_activity()){
$user = $DB->get_record('user',array('id' => $USER->id));
$submittedby = get_string('submittedby',VPL,fullname($user))."\n";
}else{
$submittedby='';
}
if(($last_sub_ins=$this->last_user_submission($userid)) !== false){
$last_sub =new mod_vpl_submission($this,$last_sub_ins);
if($last_sub->is_equal_to($files,$submittedby.$comments)){
//TODO check for concistence: NOT SAVE but say nothing
//$error=get_string('notsaved',VPL)."\n".get_string('fileNotChanged',VPL);
return $last_sub_ins->id;
}
}
ignore_user_abort (true);
//Create submission record
$submissiondata = new stdClass();
$submissiondata->vpl = $this->get_instance()->id;
$submissiondata->userid = $userid;
$submissiondata->datesubmitted = time();
$submissiondata->comments = $submittedby.$comments;
$submissionid = $DB->insert_record('vpl_submissions', $submissiondata , TRUE);
if(!$submissionid){
$error=get_string('notsaved',VPL)."\ninserting vpl_submissions record";
return false;
}
//Save files
$submission = new mod_vpl_submission($this,$submissionid);
$submission->set_submitted_file($files);
$submission->remove_grade();
//if no submitted by grader and not group activity
//remove near submmissions
if($submittedby == ''){
$this->delete_overflow_submissions($userid);
}
return $submissionid;
}
/**
* Get user submissions, order reverse submission id
* @param $id user id
* @return FALSE/array of objects
**/
function user_submissions($userid){
global $DB;
if($this->is_group_activity()){
$userid=$this->get_group_leaderid($userid);
if($userid==0){
return array();
}
}
$select = '(userid = ?) AND (vpl = ?)';
$parms = array($userid,$this->instance->id);
return $DB->get_records_select('vpl_submissions', $select,$parms,'id DESC');
}
/**
* Get all last usersubmission
* @param $fields fields to retrieve from submissions table,
* default s.*. userid is always retrieved
* @return object array
**/
function all_last_user_submission($fields='s.*'){
//Get last submissions records for this vpl module
global $DB;
$id=$this->get_instance()->id;
$query = "SELECT s.userid, $fields FROM {vpl_submissions} AS s";
$query .= ' inner join ';
$query .= ' (SELECT max(id) as maxid FROM {vpl_submissions} ';
$query .= ' WHERE {vpl_submissions}.vpl=? ';
$query .= ' GROUP BY {vpl_submissions}.userid) as ls';
$query .= ' on s.id = ls.maxid';
$parms = array($id);
return $DB->get_records_sql($query,$parms);
}
/**
* Get number of user submissions
* @return oject array
**/
function get_submissions_number(){
global $DB;
$query = 'SELECT userid, COUNT(*) as submissions FROM {vpl_submissions}';
$query .= ' WHERE {vpl_submissions}.vpl=?';
$query .= ' GROUP BY {vpl_submissions}.userid';
$parms = array($this->get_instance()->id);
return $DB->get_records_sql($query,$parms);
}
/**
* Get last usersubmission
* @param $id user id
* @return FALSE/object
**/
function last_user_submission($userid){
global $DB;
if($this->is_group_activity()){
$userid=$this->get_group_leaderid($userid);
if($userid==0){
return false;
}
}
$select = "(userid = ?) AND (vpl = ?)";
$params = array($userid,$this->instance->id);
$res = $DB->get_records_select('vpl_submissions', $select,$params,'id DESC','*',0,1);
foreach($res as $sub){
return $sub;
}
return false;
}
static protected $context = array();
/**
* Return context object for this module instance
* @return object
*/
public function get_context(){
if(!isset(self::$context[$this->cm->id])){
self::$context[$this->cm->id] = context_module::instance($this->cm->id);
}
return self::$context[$this->cm->id];
}
/**
* Requiere the current user has the capability of performing
* $capability in this module instance
* @param string $capability capability name
* @param bool $alert if true show a JavaScript alert message
* @return void
*/
function require_capability($capability, $alert=false){
if($alert && ! ($this->has_capability($capability))){
global $OUTPUT;
echo $OUTPUT->header();
vpl_js_alert(get_string('notavailable'));
}
require_capability($capability,$this->get_context());
}
/**
* Check if the user has the capability of performing
* $capability in this module instance
* @param string $capability capability name
* @param int $userid default NULL => current user
* @return bool
*/
function has_capability($capability,$userid=NULL){
return has_capability($capability,$this->get_context(),$userid);
}
/**
* Delete overflow submissions.
* If three submissions within the period central is delete
* @param $userid
* @return void
**/
function delete_overflow_submissions($userid){
global $CFG, $DB;
$plugincfg = get_config('mod_vpl');
if(!isset($plugincfg->discard_submission_period)){
return;
}
if($plugincfg->discard_submission_period == 0){
//Keep all submissions
return;
}
if($plugincfg->discard_submission_period > 0){
$select = "(userid = ?) AND (vpl = ?)";
$params = array($userid, $this->instance->id);
$res = $DB->get_records_select(VPL_SUBMISSIONS, $select,$params,'id DESC','*',0,3);
if(count($res) == 3){
$i=0;
foreach($res as $sub){
switch($i){
case 0:$last=$sub;
break;
case 1:$second=$sub;
break;
case 2:$first=$sub;
break;
}
$i++;
}
//Check time consistence
if(!($last->datesubmitted > $second->datesubmitted &&
$second->datesubmitted > $first->datesubmitted)){
return;
}
if(($last->datesubmitted - $first->datesubmitted)<$plugincfg->discard_submission_period){
//Remove second submission
$submission = new mod_vpl_submission($this,$second);
$submission->delete();
}
}
}
}
/**
* Check if it is submission period
* @return bool
**/
function is_submission_period(){
$now = time();
$ret = $this->instance->startdate <= $now;
return $ret && ($this->instance->duedate == 0 || $this->instance->duedate >= $now);
}
/**
* is visible this vpl instance
* @return bool
*/
function is_visible(){
$cm = $this->get_course_module();
$modinfo = get_fast_modinfo($cm->course);
$ret = true;
$ret = $ret && $modinfo->get_cm($cm->id)->uservisible;
$ret = $ret && $this->has_capability(VPL_VIEW_CAPABILITY);
//grader and manager always view
$ret = $ret || $this->has_capability(VPL_GRADE_CAPABILITY);
$ret = $ret || $this->has_capability(VPL_MANAGE_CAPABILITY);
return $ret;
}
/**
* this vpl instance admit submission
* @return bool
*/
function is_submit_able(){
$cm = $this->get_course_module();
$modinfo = get_fast_modinfo($cm->course);
$instance = $this->get_instance();
$ret = true;
$ret = $ret && $this->has_capability(VPL_SUBMIT_CAPABILITY);
$ret = $ret && $this->is_submission_period();
$ret = $ret && $modinfo->get_cm($cm->id)->uservisible;
//manager can always submit
$ret = $ret || $this->has_capability(VPL_MANAGE_CAPABILITY);
return $ret;
}
/**
* is group activity
* @return bool
*/
function is_group_activity(){
global $CFG;
if(!isset($this->group_activity)){
$cm = $this->get_course_module();
$this->group_activity =
$cm->groupingid>0 &&
$this->get_instance()->worktype==1;
// groups_get_activity_groupmode($cm)==SEPARATEGROUPS;
}
return $this->group_activity;
}
/**
* @param user object
* return HTML code to show user picture
* @return String
*/
function user_fullname_picture($user){
return $this->user_picture($user).
' '.$this->fullname($user);
}
/**
* @param user object
* return HTML code to show user picture
* @return String
*/
function user_picture($user){
global $OUTPUT;
if($this->is_group_activity()){
return print_group_picture($this->get_usergroup($user->id),$this->get_course()->id,false,true);
}else{
return $OUTPUT->user_picture($user);
}
}
/**
* return formated name of user or group
* @param user object
* @param withlink boolean. if true and is group add link to group
* @return String
*/
function fullname($user,$withlink=true){
if($this->is_group_activity()){
$group = $this->get_usergroup($user->id);
if($group !== false){
if($withlink){
$url = vpl_abs_href('/user/index.php','id', $this->get_course()->id, 'group', $group->id);
return '<a href="'.$url.'">'.$group->name.'</a>';
}else{
return $group->name;
}
}
return '';
}else{
return fullname($user);
}
}
/**
* Get array of graders for this activity and group (optional)
* @param $group optional parm with group to search for
* @return array
*/
function get_graders($group=''){
if(! isset($this->graders)){
$this->graders = get_users_by_capability($this->get_context(),
VPL_GRADE_CAPABILITY,
user_picture::fields('u'),'u.lastname ASC','','',
$group);
}
return $this->graders;
}
/**
* Get array of students for this activity and group (optional)
* if is group activity return only group liders
* @param $group optional parm with group to search for
* @return array
*/
function get_students($group=''){
if(! isset($this->students)){
//Generate array of graders indexed
$nostudens = array();
foreach($this->get_graders() as $user){
$nostudens[$user->id]=true;
}
$students = array();
$all = get_users_by_capability($this->get_context(),
VPL_SUBMIT_CAPABILITY,
user_picture::fields('u'),'u.lastname ASC','','',
$group);
//TODO the following code is too slow
/* if($this->is_group_activity()){
//Mark all group member but leaders as no students
foreach($all as $user){
if(isset($nostudens[$user->id])){
continue;
}
$group = $this->get_usergroup_members($user->id);
$leaderid = $this->get_group_leaderid($user->id);
foreach($group as $gm){
if($gm->id != $leaderid){
$nostudens[$gm->id]=true;
}
}
}
}*/
foreach($all as $user){
if(!isset($nostudens[$user->id])){
$students[]=$user;
}
}
if(memory_get_usage(true)> 50000000){ //Don't cache if low memory
return $students;
}
$this->students = $students;
}
return $this->students;
}
/**
* Return if is group activity
* @return bool
*/
function is_inconsistent_user($current,$real){
if($this->is_group_Activity()){
return $current != $this->get_group_leaderid($real);
}else{
return $current != $real;
}
}
/**
* If is a group activity search for a group leader
* for the group of the userid (0 is not found)
* @return Integer userid
*/
function get_group_leaderid($userid){
$leaderid=0;
foreach($this->get_usergroup_members($userid) as $user){
if($user->id < $leaderid || $leaderid==0){
$leaderid = $user->id;
}
}
return $leaderid;
}
/**
* If is a group activity
* return the group of the userid
* @return group object or false
*/
function get_usergroup($userid){
if($this->is_group_activity()){
$courseid =$this->get_course()->id;
$groupingid = $this->get_course_module()->groupingid;
$groups=groups_get_all_groups($courseid,$userid,$groupingid);
if($groups === false || count($groups)>1){
return false;
}
return reset($groups);
}
return false;
}
static $user_groups_cache=array();
/**
* If is a group activity