vpl.class.php 69.3 KB
Newer Older
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
<?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 onwards 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 <jcrodriguez@dis.ulpgc.es>
 */

/**
 * 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
 */
defined('MOODLE_INTERNAL') || die();

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[]
     */
    protected static $basefiles = array (
            'vpl_run.sh',
            'vpl_debug.sh',
            'vpl_evaluate.sh',
            'vpl_evaluate.cases'
    );

    /**
     * Number of $basefiles elements
     *
     * @var int
     */
    protected static $numbasefiles;

    /**
     * Constructor
     *
     * @param string $filelistname
     * @param string $dir
     */
    public function __construct($filelistname, $dir) {
        self::$numbasefiles = count( self::$basefiles );
        parent::__construct( $filelistname, $dir, 1000, self::$numbasefiles );
    }

    /**
     * Get list of files
     *
     * @return string[]
     */
    public function getfilelist() {
        return array_values( array_unique( array_merge( self::$basefiles, parent::getfilelist() ) ) );
    }

    /**
     * Get the file comment by number
     *
     * @param int $num
     * @return string
     */
    public function getfilecomment($num) {
        if ($num < self::$numbasefiles) {
            return get_string( self::$basefiles [$num], VPL );
        } else {
            return get_string( 'file' ) . ' ' . ($num + 1 - self::$numbasefiles);
        }
    }

    /**
     * Get list of files to keep when running
     *
     * @return string[]
     */
    public function getfilekeeplist() {
        return file_group_process::read_list( $this->filelistname . '.keep' );
    }

    /**
     * Set the file list to keep when running
     *
     * @param string[] $filelist
     */
    public function setfilekeeplist($filelist) {
        file_group_process::write_list( $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;

    /**
143
     * Internal var array of object to  file group manager
144
     *
145
     * @var araay of object of file group manager
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
     */
    protected $requiredfgm;

    /**
     * Internal var object to execution file group manager
     *
     * @var object of file group manager
     */
    protected $fgm;

    /**
     * Constructor
     *
     * @param $id int
     *            optional course_module id
     * @param $a int
     *            optional module instance id
     */
    public 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' );
            }
            $this->instance->cmidnumber = $this->cm->id;
        } 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 )) {
                vpl_notice( get_string( 'invalidcoursemodule', 'error' ) . ' VPL id=' . $a );
                // Don't stop on error. This let delete a corrupted course.
            } else {
                $this->instance->cmidnumber = $this->cm->id;
            }
        }
201
        $this->fgm = array();
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
    }

    /**
     *
     * @return Object of module DB instance
     */
    public function get_instance() {
        return $this->instance;
    }

    /**
     *
     * @return Object of course DB instance
     *
     */
    public function get_course() {
        return $this->course;
    }

    /**
     *
     * @return Object of course_module DB instance
     *
     */
    public function get_course_module() {
        return $this->cm;
    }

    /**
     * Delete a vpl instance
     *
     * @return bool true if all OK
     */
    public 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;
    }

    /**
     * Update a VPL instance including timemodified field
     *
     * @return bool true if all OK
     */
    public function update() {
        global $DB;
        $this->instance->timemodified = time();
        return $DB->update_record( VPL, $this->instance );
    }

    /**
     * Get data directory path
     * @return string data directory path
     */
    public function get_data_directory() {
        global $CFG;
        return $CFG->dataroot . '/vpl_data/' . $this->instance->id;
    }

    /**
     * Get config data directory path
     * @return string config data directory path
     */
    public function get_users_data_directory() {
        return $this->get_data_directory() . '/usersdata';
    }

    /**
     *
289
290
     * @param $type string
     * @return directory to stored initial files
291
     */
292
293
294
    public function get_files_directory($type) {
        return $this->get_data_directory() . '/'.$type.'_files/';
    }
295
296
297
298
299

    /**
     * Get path to filename to store required files
     * @return string path to filename to store required files
     */
300
301
302
    public function get_files_filename($type) {
        return $this->get_data_directory() . '/'.$type.'_files.lst';
    }
303
304
305
306
307

    /**
     * Get array of files required file names
     * @return array of strings
     */
308
309
310
    public function get_files($type) {
        return file_group_process::read_list( $this->get_files_filename($type) );
    }
311
312
313
314
315
316

    /**
     *
     * @param $files array
     *            of required files
     */
317
    public function set_files($type, $files) {
318
319
         file_group_process::write_list( $this->get_files_filename($type), $files );
    }
320
321
322

    /**
     *
323
     * @return file_group_process file group manager for specified files
324
     */
325
326
    public function get_fgm($type) {
        if (! isset($this->fgm[$type])) {
327
328
            if ($type == 'execution') {
                $this->fgm[$type] = new file_group_execution( $this->get_files_filename($type)
329
330
                                                       , $this->get_files_directory($type) );
            } else {
331
                $this->fgm[$type] = new file_group_process( $this->get_files_filename($type)
332
333
334
335
336
337
                                                       , $this->get_files_directory($type)
                                                       , $this->instance->maxfiles );
            }
        }
        return $this->fgm[$type];
    }
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

    /**
     * get instance name with groupping name if available
     *
     * @return string with name+(grouping name)
     */
    public 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 string fulldescription
     *
     */
    public 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 descriptions
     *
     * @return string fulldescription
     *
     */
    public 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
     *
     */
    public 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
     */
    public 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
     */
    protected function get_password() {
        return trim( $this->instance->password );
    }

    /**
     * Get password md5
     */
    protected function get_password_md5() {
        return md5( $this->instance->id . (sesskey()) );
    }
459

460
461
462
    /**
     * Get is use as base
     **/
463
464
    public function is_use_as_base() {
        return $this->instance->usableasbase == 1;
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
    /**
     * Check if pass password restriction
     */
    public function pass_password_check($passset = '') {
        $password = $this->get_password();
        if ($password > '') {
            global $SESSION;
            $passwordmd5 = $this->get_password_md5();
            $passvar = 'vpl_password_' . $this->instance->id;
            $passattempt = 'vpl_password_attempt' . $this->instance->id;
            if (isset( $SESSION->$passvar ) && $SESSION->$passvar == $passwordmd5) {
                return true;
            }
            if ($passset == '') {
                $passset = optional_param( 'password', '', PARAM_TEXT );
            }
            if ($passset > '') {
                if ($passset == $password) {
                    $SESSION->$passvar = $passwordmd5;
                    unset( $SESSION->$passattempt );
                    return true;
                }
                if (isset( $SESSION->$passattempt )) {
                    $SESSION->$passattempt ++;
                } else {
                    $SESSION->$passattempt = 1;
                }
                // Wait vpl_attempt_number seg to limit force brute crack.
                sleep( $SESSION->$passattempt );
            }
            return false;
        }
        return true;
    }

    /**
     * Check password restriction
     */
    public function password_check() {
        global $SESSION;
        if (! $this->pass_password_check()) {
            if ( constant( 'AJAX_SCRIPT' ) ) {
                throw new Exception( get_string( 'requiredpassword', VPL ) );
            }
            require_once('forms/password_form.php');
            $this->print_header();
            $mform = new mod_vpl_password_form( $_SERVER ['SCRIPT_NAME'], $this);
            $passattempt = 'vpl_password_attempt' . $this->get_instance()->id;
            if (isset( $SESSION->$passattempt)) {
                vpl_notice( get_string( 'attemptnumber', VPL, $SESSION->$passattempt),
                            'warning');
            }
            $mform->display();
            $this->print_footer();
            die();
        }
    }

    /**
     * Check network restriction and return true o false
     * @return boolean
     */
    public function pass_network_check() {
        return vpl_check_network( $this->instance->requirednet );
    }

    /**
     * Check netword restriction and show error if not passed
     * @return void
     */
    public function network_check() {
        global $OUTPUT;
        if (! $this->pass_network_check()) {
            $str = get_string( 'opnotallowfromclient', VPL ) . ' ' . getremoteaddr();
            if ( constant( 'AJAX_SCRIPT') ) {
                throw new Exception( $str );
            }
            $this->print_header();
            vpl_notice( $str , 'warning');
            $this->print_footer();
            die();
        }
    }

    /**
     * Checks if SEB key is valid
     * @return void
     */
    protected function is_sebkey_valid() {
        global $FULLME;
        $keys = trim($this->get_instance()->sebkeys);
        if ( $keys == '') {
            return true;
        }
        if ( ! isset($_SERVER['HTTP_X_SAFEEXAMBROWSER_REQUESTHASH']) ) {
            return false;
        }
        $key = $_SERVER['HTTP_X_SAFEEXAMBROWSER_REQUESTHASH'];
        foreach (preg_split('/\s+/', $keys) as $testkey) {
            if (hash('sha256', $FULLME . $testkey) === $key) {
                return true;
            }
        }
        return false;
    }

    /**
     * Checks SEB restrictions and shows error if not passed
     * @return void
     */
    protected function seb_check() {
        $inst = $this->get_instance();
        $fail = $inst->sebrequired > 0;
        $fail = $fail && strpos($_SERVER['HTTP_USER_AGENT'], 'SEB') === false;
        $fail = $fail || ! $this->is_sebkey_valid();
        if ( $fail ) {
            $str = get_string( 'sebrequired_help', VPL );
            if ( constant( 'AJAX_SCRIPT') ) {
                throw new Exception( $str );
            }
            $this->print_header();
            vpl_notice( $str , 'warning');
            $this->print_footer();
            die();
        }
    }

    /**
     * Return true if is set to use SEB
     * @return void
     */
    protected function use_seb() {
        $inst = $this->get_instance();
        return $inst->sebrequired || $inst->sebkeys > '';
    }

    /**
     * Checks all restrictions and shows error if not passed
     * @return void
     */
    public function restrictions_check() {
        $this->network_check();
        $this->password_check();
        $this->seb_check();
    }

    /**
     * Check submission restriction
     *
     * @param $data Object
     *            with submitted data
     * @param & $error
     *            string
     * @return bool
     *
     */
    public function pass_submission_restriction(& $alldata, & $error) {
        $max = $this->get_maxfilesize();
626
        $rfn = $this->get_fgm('required');
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
        $list = $rfn->getFilelist();
        $error = '';
        if (count( $alldata ) > $this->instance->maxfiles) {
            $error .= get_string( 'maxfilesexceeded', VPL ) . "\n";
        }
        $lr = count( $list );
        $lad = count( $alldata );
        $i = 0;
        foreach ($alldata as $name => $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 />";
            }
            $i++;
        }
        return strlen( $error ) == 0;
    }

    /**
     * Check and submission
     *
     * @param
     *            $userid
     * @param $data Object
     *            with submitted data
     * @param & $error
     *            string
     * @return false or submission id
     */
    public function add_submission($userid, & $files, $comments, & $error) {
        global $USER, $DB;
        if (! $this->pass_submission_restriction( $files, $error )) {
            return false;
        }
        $group = $this->get_usergroup($userid);
        if ($this->is_group_activity() && $group === false) {
            $error = get_string( 'notsaved', VPL ) . "\n" . get_string( 'inconsistentgroup', VPL );
            return false;
        }
        $submittedby = '';
        if ($USER->id != $userid ) {
            if ($this->has_capability( VPL_MANAGE_CAPABILITY ) || ($this->has_capability(
                    VPL_GRADE_CAPABILITY ) )) {
                if (! $this->is_group_activity() ) {
                    $user = $DB->get_record( 'user', array (
                            'id' => $USER->id
                    ) );
                    $submittedby = get_string( 'submittedby', VPL, fullname( $user ) ) . "\n";
                    if (strpos($comments, $submittedby) === 0 ) {
                        $submittedby = '';
                    }
                }
            } else {
                $error = get_string( 'notsaved', VPL ) . "\n" . get_string( 'inconsistentgroup', VPL );
                return false;
            }
        }
        $saveduserid = $this->is_group_activity() ? $USER->id : $userid;
        $lastsub = false;
        $lock = new \mod_vpl\util\lock($this->get_users_data_directory() . '/' . $saveduserid);
        if (($lastsubins = $this->last_user_submission( $userid )) !== false) {
            $lastsub = new mod_vpl_submission( $this, $lastsubins );
            if ($lastsub->is_equal_to( $files, $submittedby . $comments )) {
                return $lastsubins->id;
            }
        }
        ignore_user_abort( true );
        // Create submission record.
        $submissiondata = new stdClass();
        $submissiondata->vpl = $this->get_instance()->id;
        $submissiondata->userid = $saveduserid;
        $submissiondata->datesubmitted = time();
        $submissiondata->comments = $submittedby . $comments;
        if ( $lastsubins !== false ) {
            $submissiondata->nevaluations = $lastsubins->nevaluations;
        }
        if ( $group !== false ) {
            $submissiondata->groupid = $group->id;
        }
        $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, $lastsub );
        $submission->remove_grade();
        // If no submitted by grader and not group activity, remove near submmissions.
        if ($USER->id == $userid) {
            $this->delete_overflow_submissions( $userid );
        }
        return $submissionid;
    }

    /**
     * Get user submissions, order reverse submission id
     *
     * @param $userid int the user id to retrieve submissions
     * @param $groupifga boolean if group activity get group submissions. default true
     * @return FALSE/array of objects
     */
    public function user_submissions($userid, $groupifga = true) {
        global $DB;

        if ($groupifga && $this->is_group_activity()) {
            $group = $this->get_usergroup($userid);
            if ($group) {
                $select = '(groupid = ?) AND (vpl = ?)';
                $parms = array (
                        $group->id,
                        $this->instance->id
                );
            } else {
                return array ();
            }
        } else {
            $select = '(userid = ?) AND (vpl = ?)';
            $parms = array (
                    $userid,
                    $this->instance->id
            );
        }
        return $DB->get_records_select( 'vpl_submissions', $select, $parms, 'id DESC' );
    }

    /**
     * Get the last submission of all users
     *
     * @param string $fields conma separeted
     *            to retrieve from submissions table, default s.*. userid is always retrieved
     * @return object array
     */
    public function all_last_user_submission($fields = 's.*') {
        // Get last submissions records for this vpl module.
        global $DB;
        $id = $this->get_instance()->id;
        if ($this->is_group_activity()) {
            $idfield = 'groupid';
        } else {
            $idfield = 'userid';
        }
        $query = "SELECT s.$idfield, $fields FROM {vpl_submissions} s";
        $query .= ' inner join ';
        $query .= ' (SELECT max(id) as maxid FROM {vpl_submissions} ';
        $query .= '  WHERE {vpl_submissions}.vpl=? ';
        $query .= "  GROUP BY {vpl_submissions}.$idfield) as ls";
        $query .= ' on s.id = ls.maxid';
        return $DB->get_records_sql( $query, array( $id ) );
    }

    /**
     * Get all saved submission of all users
     *
     * @param string $fields fields conma separeted
     *            to retrieve from submissions table, default s.*
     * @return object array
     */
    public function all_user_submission($fields = 's.*') {
        global $DB;
        $id = $this->get_instance()->id;
        $query = "SELECT s.id, $fields FROM {vpl_submissions} s WHERE vpl=?";
        return $DB->get_records_sql( $query, array ( $id ) );
    }
    /**
     * Get number of user submissions
     *
     * @return Array of objects with 'submissions' atribute as number of submissions saved
     */
    public function get_submissions_number() {
        global $DB;
        if ( $this->is_group_activity() ) {
            $field = 'groupid';
        } else {
            $field = 'userid';
        }
811
        $id = $this->get_instance()->id;
812
        $query = "SELECT $field, COUNT(*) as submissions FROM {vpl_submissions}";
813
        $query .= " WHERE {vpl_submissions}.vpl=$id";
814
        $query .= " GROUP BY {vpl_submissions}.$field";
815
        return $DB->get_records_sql( $query );
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
    }

    /**
     * This is for compatibility to old group scheme.
     * Update the submission groupid for VPL version <= 3.2
     *
     * Set the correct groupid when groupid = 0
     * @param int $groupid.
     *     If no $groupid => update $groupid of all groups of the activity
     * @return void
     */
    public function update_group_v32($groupid='') {
        global $DB;
        if ( ! $this->is_group_activity() ) {
            return;
        }
        // All groups.
        if ($groupid == '') {
            $cm = $this->get_course_module();
            $groups = groups_get_all_groups($this->get_course()->id, 0, $cm->groupingid);
            foreach ($groups as $cgroup) {
                $this->update_group_v32($cgroup->id);
            }
        }
        $students = $this->get_students($groupid);
        if (count($students) > 0) {
            $studentsids = array_keys($students);
            $insql = $DB->get_in_or_equal($studentsids);
            $vplid = $this->get_instance()->id;
            $select = 'userid ' . ($insql[0]) . ' and vpl = ? and groupid = 0';
            $params = $insql[1];
            $params[] = $vplid;
            $DB->set_field_select(VPL_SUBMISSIONS, 'groupid', $groupid, $select, $params);
        }
    }

    /**
     * Get last user submission
     *
     * @param int $userid
     * @return FALSE/object
     *
     */
    public function last_user_submission($userid) {
        global $DB;
        if ($this->is_group_activity()) {
            $group = $this->get_usergroup($userid);
            if ($group !== false) {
                $select = "(groupid = ?) AND (vpl = ?)";
                $params = array (
                        $group->id,
                        $this->instance->id
                );
                $res = $DB->get_records_select( 'vpl_submissions', $select, $params, 'id DESC', '*', 0, 1 );
                foreach ($res as $sub) {
                    return $sub;
                }
                $this->update_group_v32($group->id);
                $res = $DB->get_records_select( 'vpl_submissions', $select, $params, 'id DESC', '*', 0, 1 );
                foreach ($res as $sub) {
                    return $sub;
                }
            }
            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;
    }
    protected static $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
     */
    public 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
     */
    public 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
     *
     */
    public function delete_overflow_submissions($userid) {
        global $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
     *
     */
    public 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
     */
    public 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
     */
    public 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
     */
    public 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;
            // TODO check groups_get_activity_groupmode($cm)==SEPARATEGROUPS.
        }
        return $this->group_activity;
    }

    /**
     *
     * @param
     *            user object return HTML code to show user picture
     * @return String
     */
1058
1059
    public function user_fullname_picture($user, $withlink = true) {
        return $this->user_picture( $user ) . ' ' . $this->fullname( $user, $withlink );
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
    }

    /**
     *
     * @param
     *            user object return HTML code to show user picture
     * @return String
     */
    public function user_picture($user) {
        global $OUTPUT;
        if ($this->is_group_activity()) {
1071
1072
1073
1074
1075
1076
1077
1078
1079
            $group = $this->get_usergroup( $user->id );
            $picture = print_group_picture( $group, $this->get_course()->id, false, true );
            if (!$picture) {
                $url = vpl_abs_href( '/group/overview.php', 'id', $this->get_course()->id, 'group', $group->id);
                $picture = '<a href="' . $url . '" style="color: initial;">' .
                               $OUTPUT->render(new pix_icon('group', '', 'mod_vpl')) .
                           '</a>';
            }
            return $picture;
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
        } else {
            $options = array('courseid' => $this->get_instance()->course, 'link' => ! $this->use_seb());
            return $OUTPUT->user_picture( $user, $options);
        }
    }

    /**
     * return formated name of user or group
     *
     * @param
     *            user object
     * @param
     *            withlink boolean. if true and is group add link to group. Default true
     * @return String
     */
    public function fullname($user, $withlink = true) {
        if ($this->is_group_activity()) {
            $group = $this->get_usergroup( $user->id );
            if ($group !== false) {
                if ($withlink) {
1100
                    $url = vpl_abs_href( '/group/overview.php', 'id', $this->get_course()->id, 'group', $group->id);
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
                    return '<a href="' . $url . '">' . $group->name . '</a>';
                } else {
                    return $group->name;
                }
            }
            return '';
        } else {
            $fullname = fullname( $user );
            if ($withlink) {
                $url = vpl_abs_href( '/user/view.php', 'id', $user->id, 'course', $this->get_course()->id);
                $html = "<a href=\"$url\" title=\"$fullname\">$fullname</a>";
            } else {
                $html = $fullname;
            }
            return $html;
        }
    }

    /**
     * Get array of graders for this activity and group (optional)
     *
     * @param string $group optional parm with group to search for
     * @return array
     */
    public 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. If group is set return only group members
     *
     * @param string $group optional parm with group to search for
     * @return array of objects
     */
    public function get_students($group = '') {
        if ( isset( $this->students ) && $group == '') {
            return $this->students;
        }
        // Generate array of graders indexed.
        $nostudents = array ();
        foreach ($this->get_graders($group) as $user) {
            $nostudents [$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.
        foreach ($all as $user) {
            if (! isset( $nostudents [$user->id] )) {
                $students [$user->id] = $user;
            }
        }
        if ($group != '') { // Don't cache if group request.
            $this->students = $students;
        }
        return $students;
    }

    /**
1164
1165
     * Check for user consistency.
     * This checks that both users are the same, or if this is a group activity, that they belong to the same group.
1166
1167
1168
1169
     *
     * @return bool
     */
    public function is_inconsistent_user($current, $real) {
1170
1171
1172
1173
1174
1175
1176
        if ($this->is_group_activity()) {
            $g1 = $this->get_usergroup($current);
            $g2 = $this->get_usergroup($real);
            if ($g1 === false || $g2 === false) {
                return $current != $real;
            }
            return $g1->id != $g2->id;
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
        } 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
     */
    public function get_group_leaderid($userid) {
        $leaderid = $userid;
        $group = $this->get_usergroup($userid);
        if ($group) {
            foreach ($this->get_usergroup_members( $group->id ) as $user) {
                if ($user->id < $leaderid) {
                    $leaderid = $user->id;
                }
            }
        }
        return $leaderid;
    }

    /**
     * If is a group activity return the group of the userid
     *
     * @return Object/false
     */
    public 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;
    }
    protected static $usergroupscache = array ();
    /**
     * If is a group activity return group members for the groupid
     *
     * @return Array of user objects
     */
    public function get_group_members($groupid) {
        if (! isset( self::$usergroupscache [$groupid] )) {
            $gm = groups_get_members( $groupid );
            if ($gm) {
                self::$usergroupscache [$groupid] = $gm;
            } else {
                self::$usergroupscache [$groupid] = array();
            }
        }
        return self::$usergroupscache [$groupid];
    }
    /**
     * If is a group activity return group members for the group of the userid
     *
     * @return Array of user objects
     */
    public function get_usergroup_members($userid) {
        $group = $this->get_usergroup( $userid );
        if ($group !== false) {
            return $this->get_group_members($group->id);
        }
        return array ();
    }

    /**
     * Return scale record if grade < 0
     *
     * @return Object or false
     */
    public function get_scale() {
        global $DB;
        if (! isset( $this->scale )) {
            if ($this->get_grade() < 0) {
                $gradeid = - $this->get_grade();
                $this->scale = $DB->get_record( 'scale', array (
                        'id' => $gradeid
                ) );
            } else {
                $this->scale = false;
            }
        }
        return $this->scale;
    }

    /**
     * Return grade info take from gradebook
     *
     * @return Object or false
     */
    public function get_grade_info() {
        global $DB, $CFG, $USER;
        if (! isset( $this->grade_info )) {
            $this->grade_info = false;
            if ($this->get_instance()->grade != 0) { // If 0 then NO GRADE.
                $userid = ($this->has_capability( VPL_GRADE_CAPABILITY ) || $this->has_capability(
                        VPL_MANAGE_CAPABILITY )) ? null : $USER->id;
                require_once($CFG->libdir . '/gradelib.php');
                $gradinginfo = grade_get_grades( $this->get_course()->id, 'mod', 'vpl', $this->get_instance()->id, $userid );
                foreach ($gradinginfo->items as $gi) {
                    $this->grade_info = $gi;
                }
            }
        }
        return $this->grade_info;
    }

    /**
     * Return visiblegrade from gradebook and for every user
     *
     * @return boolean
     */
    public function get_visiblegrade() {
        global $USER;
        if ($gi = $this->get_grade_info()) {
            if (is_array( $gi->grades )) {
                $usergi = reset( $gi->grades );
                return ! ($gi->hidden || (is_object( $usergi ) && $usergi->hidden));
            } else {
                return ! ($gi->hidden);
            }
        } else {
            return false;
        }
    }

    /**
     * Return grade (=0 => no grade, >0 max grade, <0 scaleid)
     *
     * @return int
     */
    public function get_grade() {
        return $this->instance->grade;
    }

1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
    public function get_max_grades() {
        global $DB;
        if ( $this->is_group_activity() ) {
            $field = 'groupid';
        } else {
            $field = 'userid';
        }
        $id = $this->get_instance()->id;
        $query = "SELECT $field, max({vpl_submissions}.grade) as maxgrade FROM {vpl_submissions}";
        $query .= " WHERE {vpl_submissions}.vpl=$id and ({vpl_submissions}.grade is not null)";
        $query .= " GROUP BY {vpl_submissions}.$field";
        return $DB->get_records_sql( $query );
    }

1331
1332
1333
1334
1335
1336
1337
1338
1339
    /**
     * Format given grade as grade/maxgrade
     * @param float $grade
     * @return string Printable grade
     */
    public function format_grade($grade) {
        return format_float($grade, 2, true, false) . ' / ' . format_float($this->get_grade(), 2, true, false);
    }

1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
    /**
     * print end of page
     */
    public function print_footer() {
        global $OUTPUT;
        if (! $this->use_seb() ) {
            $style = "float:right; right:10px; padding:8px; background-color: white;text-align:center;";
            echo '<div style="' . $style . '">';
            echo '<a href="http://vpl.dis.ulpgc.es/">';
            echo 'VPL '. vpl_get_version();
            echo '</a>';
            echo '</div>';
        }
        echo $OUTPUT->footer();
    }

    /**
     * print end of page
     */
    public function print_footer_simple() {
        global $OUTPUT;
        echo $OUTPUT->footer();
    }

    /**
     * prepare_page initialy
     */
    public function prepare_page($url = false, $parms = array()) {
        global $PAGE;

        $PAGE->set_cm( $this->get_course_module(), $this->get_course(), $this->get_instance() );
        $PAGE->set_context( $this->get_context() );
        if ($url) {
            $PAGE->set_url( '/mod/vpl/' . $url, $parms );
        }
    }

    protected static $headerisout = false;
    public static function header_is_out() {
        return self::$headerisout;
    }
    /**
     * print header
     *
     * @param $info string title and last nav option
     */
    public function print_header($info = '') {
        global $COURSE, $PAGE, $OUTPUT;
        if (self::$headerisout) {
            return;
        }
        $tittle = $this->get_printable_name();
        if ($info) {
            $tittle .= ' ' . $info;
        }
        $PAGE->set_title( $this->get_course()->fullname . ' ' . $tittle );
        $PAGE->set_pagelayout( 'incourse' );
        $PAGE->set_heading( $this->get_course()->fullname );
        if ( $this->use_seb() && ! $this->has_capability(VPL_GRADE_CAPABILITY)) {
            $PAGE->set_popup_notification_allowed(false);
            $PAGE->set_pagelayout('secure');
        }
        echo $OUTPUT->header();
        self::$headerisout = true;
    }
    public function print_header_simple($info = '') {
        global $OUTPUT, $PAGE;
        if (self::$headerisout) {
            return;
        }
        $tittle = $this->get_printable_name();
        if ($info) {
            $tittle .= ' ' . $info;
        }
        $PAGE->set_title( $this->get_course()->fullname . ' ' . $tittle );
        $PAGE->set_pagelayout( 'popup' );
        if ( $this->use_seb() && ! $this->has_capability(VPL_GRADE_CAPABILITY)) {
            $PAGE->set_popup_notification_allowed(false);
            $PAGE->set_pagelayout('secure');
        }
        echo $OUTPUT->header();
        self::$headerisout = true;
    }
    /**
     * Print heading action with help
     *
     * @param $action string
     *            base text and help
     */
    public function print_heading_with_help($action) {
        global $OUTPUT;
        $title = get_string( $action, VPL ) . ': ' . $this->get_printable_name();
        echo $OUTPUT->heading_with_help( vpl_get_awesome_icon($action) . $title, $action, 'vpl');
        self::$headerisout = true;
    }

    /**
     * Create tabs to view_description/submit/view_submission/edit
     *
     * @param string $path to get the active tab
     *
     */
    public function print_view_tabs($path) {
        // TODO refactor using functions.
        global $CFG, $USER, $DB, $OUTPUT;
        $active = basename( $path );
        $cmid = $this->cm->id;
        $userid = optional_param( 'userid', null, PARAM_INT );
        $copy = optional_param( 'privatecopy', false, PARAM_INT );
        $viewer = $this->has_capability( VPL_VIEW_CAPABILITY );
        $submiter = $this->has_capability( VPL_SUBMIT_CAPABILITY );
        $similarity = $this->has_capability( VPL_SIMILARITY_CAPABILITY );
        $grader = $this->has_capability( VPL_GRADE_CAPABILITY );
        $manager = $this->has_capability( VPL_MANAGE_CAPABILITY );
        $example = $this->instance->example;
        if (! $userid || ! $grader || $copy) {
            $userid = $USER->id;
        }
        $level2 = $grader || $manager || $similarity;

        $strdescription = get_string( 'description', VPL );
        $strsubmission = get_string( 'submission', VPL );
        $stredit = get_string( 'edit', VPL );
        $strsubmissionview = get_string( 'submissionview', VPL );
        $maintabs = array ();
        $tabs = array ();
        $baseurl = $CFG->wwwroot . '/mod/' . VPL . '/';
        $href = vpl_mod_href( 'view.php', 'id', $cmid, 'userid', $userid );
        $viewtab = vpl_create_tabobject('view.php', $href, 'description' );
        if ($level2) {
            if ($viewer) {
                $maintabs [] = $viewtab;
            }
            $strsubmissionslist = get_string( 'submissionslist', VPL );
            $href = vpl_mod_href( 'views/submissionslist.php', 'id', $cmid );
            $maintabs [] = vpl_create_tabobject( 'submissionslist.php', $href, 'submissionslist' );
            // Similarity.
            if ($similarity) {
                if ($active == 'listwatermark.php' || $active == 'similarity_form.php' || $active == 'listsimilarity.php') {
                    $tabname = $active;
                } else {
                    $tabname = 'similarity';
                }
                $strsubmissionslist = get_string( 'similarity', VPL );
                $href = vpl_mod_href( 'similarity/similarity_form.php', 'id', $cmid );
                $maintabs [] = vpl_create_tabobject( $tabname, $href, 'similarity' );
            }
            // Test.
            if ($grader || $manager) {
                if ($active == 'submission.php' || $active == 'edit.php'
                        || $active == 'submissionview.php' || $active == 'gradesubmission.php'
                        || $active == 'previoussubmissionslist.php') {
                            $tabname = $active;
                } else {
                    $tabname = 'test';
                }
                $href = vpl_mod_href( 'forms/submissionview.php', 'id', $cmid, 'userid', $userid );
                if ($userid == $USER->id) {
                    $maintabs [] = vpl_create_tabobject( $tabname, $href, 'test' );
                } else {
                    $user = $DB->get_record( 'user', array (
                            'id' => $userid
                    ) );
                    if ($this->is_group_activity()) {
                        $text = get_string( 'group' ) . ' ';
                    } else {
                        $text = get_string( 'user' ) . ' ';
                    }
                    $text .= $this->fullname( $user, false );
                    $maintabs [] = new tabobject( $tabname, $href, $text, $text );
                }
            }
        }
        switch ($active) {
            case 'view.php' :
                if ($level2) {
                    // TODO replace by $OUTPUT->tabtree.
                    print_tabs(
                            array (
                                    $maintabs,
                                    $tabs
                            ), $active );
                    return;
                }
            case 'submission.php' :
            case 'edit.php' :
            case 'submissionview.php' :
            case 'gradesubmission.php' :
            case 'previoussubmissionslist.php' :
                require_once('vpl_submission.class.php');
                $subinstance = $this->last_user_submission( $userid );
                if ($subinstance !== false) {
                    $submission = new mod_vpl_submission( $this, $subinstance );
                }
                if ($viewer && ! $level2) {
                    $tabs [] = $viewtab;
                }
                if ($manager || ($grader && $USER->id == $userid)
                    || (! $grader && $submiter && $this->is_submit_able()
                    && ! $this->instance->restrictededitor && ! $example)) {
                    $href = vpl_mod_href( 'forms/submission.php', 'id', $cmid, 'userid', $userid );
                    $tabs [] = vpl_create_tabobject( 'submission.php', $href, 'submission' );
                }
                if ($manager || ($grader && $USER->id == $userid)
                    || (! $grader && $submiter && $this->is_submit_able())) {
                    $href = vpl_mod_href( 'forms/edit.php', 'id', $cmid, 'userid', $userid );
                    if ($example && $this->instance->run) {
                        $stredit = get_string( 'run', VPL );
                    }
                    $tabs [] = vpl_create_tabobject( 'edit.php', $href, 'edit');
                }
1551
                if (! $example && $submiter) {
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
                    $href = vpl_mod_href( 'forms/submissionview.php', 'id', $cmid, 'userid', $userid );
                    $tabs [] = vpl_create_tabobject( 'submissionview.php', $href, 'submissionview');
                    if ($grader && $this->get_grade() != 0 && $subinstance
                        && ($subinstance->dategraded == 0
                            || $subinstance->grader == $USER->id
                            || $subinstance->grader == 0)) {
                        $href = vpl_mod_href( 'forms/gradesubmission.php', 'id', $cmid, 'userid', $userid );
                        $text = get_string( 'grade' );
                        $tabs [] = vpl_create_tabobject( 'gradesubmission.php', $href, 'grade', 'moodle' );
                    }
1562
                    if ($subinstance /*&& ($grader || $similarity|| $this->instance->allowshowprevious)*/) {
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
                        $strlistprevoiussubmissions = get_string( 'previoussubmissionslist', VPL );
                        $href = vpl_mod_href( 'views/previoussubmissionslist.php', 'id', $cmid, 'userid', $userid );
                        $tabs [] = vpl_create_tabobject( 'previoussubmissionslist.php', $href, 'previoussubmissionslist' );
                    }
                }
                // Show user picture if this activity require password.
                if (! isset( $user ) && $this->instance->password > '') {
                    $user = $DB->get_record( 'user', array (
                            'id' => $userid
                    ) );
                }
                if (isset( $user )) {
                    echo '<div style="position:absolute; right:50px; z-index:50;">';
                    echo $this->user_picture( $user );
                    echo '</div>';
                }
                if ($level2) {
                    print_tabs(
                            array (
                                    $maintabs,
                                    $tabs
                            ), $active );
                    return;
                } else {
                    print_tabs( array (
                            $tabs
                    ), $active );
                    return;
                }

                break;
            case 'submissionslist.php' :
                print_tabs( array (
                        $maintabs
                ), $active );
                return;
            case 'listwatermark.php' :
            case 'similarity_form.php' :
            case 'listsimilarity.php' :
                if ($similarity) {
                    $href = vpl_mod_href( 'similarity/similarity_form.php', 'id', $cmid );
                    $string = get_string( 'similarity', VPL );
                    $tabs [] = vpl_create_tabobject( 'similarity_form.php', $href, 'similarity' );
                    if ($active == 'listsimilarity.php') {
                        $string = get_string( 'listsimilarity', VPL );
                        $tabs [] = vpl_create_tabobject( 'listsimilarity.php', '', 'listsimilarity' );
                    }
                    $plugincfg = get_config('mod_vpl');
                    $watermark = isset( $plugincfg->use_watermarks ) && $plugincfg->use_watermarks;
                    if ($watermark) {
                        $href = vpl_mod_href( 'similarity/listwatermark.php', 'id', $cmid );
                        $string = get_string( 'listwatermarks', VPL );
                        $tabs [] = vpl_create_tabobject( 'listwatermark.php', $href, 'listwatermarks' );
                    }
                }
                print_tabs( array (
                        $maintabs,
                        $tabs
                ), $active );
                break;
        }
    }

    /**
     * Show vpl name
     */
    public function print_name() {
        echo '<h2>';
        p( $this->get_printable_name() );
        echo '</h2>';
    }

    public function str_restriction($str, $value = null, $raw = false) {
        $html = '<b>';
        if ($raw) {
            $html .= s( $str );
        } else {
            $html .= s( get_string( $str, VPL ) );
        }
        $html .= '</b>: ';
        if ($value === null) {
            $value = $this->instance->$str;
        }
        $html .= $value;
        return $html;
    }

    /**
     * Print one VPL setting
     * @param string $str setting string i18n to get descriptoin
     * @param string $value setting value, default null
     * @param boolean $raw if true $str if raw string, default false
     * @param boolean $newline if true print new line after setting, default false
     */
    public function print_restriction($str, $value = null, $raw = false, $newline = true) {
        echo $this->str_restriction($str, $value, $raw);
        if ( $newline ) {
            echo '<br>';
        } else {
            echo '. ';
        }
    }

    /**
     * Show vpl submission period
     */
    public function print_submission_period() {
        if ($this->instance->startdate == 0 && $this->instance->duedate == 0) {
            return;
        }
        if ($this->instance->startdate) {
            $this->print_restriction( 'startdate', userdate( $this->instance->startdate ) );
        }
        if ($this->instance->duedate) {
            $this->print_restriction( 'duedate', userdate( $this->instance->duedate ) );
        }
    }

    /**
     * Show vpl submission restriction
     */
    public function print_submission_restriction() {
        global $CFG, $USER;
1686
        $filegroup = $this->get_fgm('required');
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
        $files = $filegroup->getfilelist();
        if (count( $files )) {
            $text = '';
            $needcomma = false;
            foreach ($files as $file) {
                if ($needcomma) {
                    $text .= ', ';
                }
                $text .= s( $file );
                $needcomma = true;
            }
            $link = ' (<a href="';
1699
            $link .= vpl_mod_href( 'views/downloadfiles.php', 'id', $this->get_course_module()->id, 'type', 'required');
1700
1701
1702
            $link .= '">';
            $link .= get_string( 'download', VPL );
            $link .= '</a>)';
1703
            $this->print_restriction( 'requiredfiles', $text . $link );
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
        }
        $instance = $this->get_instance();
        if (count( $files ) != $instance->maxfiles) {
            $this->print_restriction( 'maxfiles' );
        }
        if ($instance->maxfilesize) {
            $mfs = $this->get_maxfilesize();
            $this->print_restriction( 'maxfilesize', vpl_conv_size_to_string( $mfs ) );
        }
        $worktype = $instance->worktype;
        $values = array (
                0 => get_string( 'individualwork', VPL ),
                1 => get_string( 'groupwork', VPL )
        );
        if ($worktype) {
            $this->print_restriction( 'worktype', $values [$worktype] . ' ' . $this->fullname( $USER ) );
        } else {
            $this->print_restriction( 'worktype', $values [$worktype] );
        }
        if ($instance->example) {
            $this->print_restriction( 'isexample', $stryes );
        }
        $grader = $this->has_capability( VPL_GRADE_CAPABILITY );
        if ($grader) {
            $stryes = get_string( 'yes' );
            $strno = get_string( 'no' );
            require_once($CFG->libdir . '/gradelib.php');
            if ($gie = $this->get_grade_info()) {
                if ($gie->scaleid == 0) {
                    $info = get_string('grademax', 'core_grades')
                            . ': ' . format_float($gie->grademax, 5, true, true);
                    $info .= $gie->hidden ? (' <b>' . get_string( 'hidden', 'core_grades' ))
                            . '</b>' : '';
                    $info .= $gie->locked ? (' <b>' . get_string( 'locked', 'core_grades' ))
                            . '</b>' : '';
                } else {
                    $info = get_string( 'typescale', 'core_grades' );
                }
                $this->print_restriction( get_string( 'gradessettings', 'core_grades' ), $info, true );
            } else {
                $this->print_restriction( get_string( 'gradessettings', 'core_grades' ), get_string( 'nograde' ), true );
            }
        }
        $this->print_gradereduction();
        if ($grader) {
            if (trim( $instance->password ) > '') {
                $this->print_restriction( get_string( 'password' ), $stryes, true );
            }
            if (trim( $instance->requirednet ) > '') {
                $this->print_restriction( 'requirednet', s( $instance->requirednet ));
            }
            if ( $instance->sebrequired > 0) {
                $this->print_restriction('sebrequired', $stryes );
            }
            if (trim( $instance->sebkeys ) > '') {
                $this->print_restriction('sebkeys', $stryes );
            }
            if ($instance->restrictededitor) {
                $this->print_restriction( 'restrictededitor', $stryes );
            }
            if (! $this->get_course_module()->visible) {
                $this->print_restriction( get_string( 'visible' ), $strno, true );
            }
            if ($instance->basedon) {
                try {
                    $basedon = new mod_vpl( null, $instance->basedon );
                    $link = '<a href="';
                    $link .= vpl_mod_href( 'view.php', 'id', $basedon->cm->id );
                    $link .= '">';
                    $link .= $basedon->get_printable_name();
                    $link .= '</a>';
                    $this->print_restriction( 'basedon', $link );
                } catch (Exception $e) {
                    $this->print_restriction( 'basedon', $e->getMessage() );
                }
            }
            $noyes = array (
                    $strno,
                    $stryes
            );
            $this->print_restriction( 'run', $noyes [$instance->run], false, false );
            if ($instance->runscript) {
                $this->print_restriction( 'runscript', strtoupper($instance->runscript), false, false );
            }
            if ($instance->debug) {
                $this->print_restriction( 'debug', $noyes [1], false, false );
            }
            if ($instance->debugscript) {
                $this->print_restriction( 'debugscript', strtoupper($instance->debugscript), false, false );
            }
            $this->print_restriction( 'evaluate', $noyes [$instance->evaluate], false,
                    ! ($instance->evaluate && $instance->evaluateonsubmission) );
            if ($instance->evaluate && $instance->evaluateonsubmission) {
                $this->print_restriction( 'evaluateonsubmission', $noyes [1] );
            }
            if ($instance->automaticgrading) {
                $this->print_restriction( 'automaticgrading', $noyes [1], false, false );
            }
            if ($instance->maxexetime) {
1803
                $this->print_restriction( 'maxexetime', format_time($instance->maxexetime), false, false );
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
            }
            if ($instance->maxexememory) {
                $this->print_restriction( 'maxexememory', vpl_conv_size_to_string( $instance->maxexememory ), false, false );
            }
            if ($instance->maxexefilesize) {
                $this->print_restriction( 'maxexefilesize', vpl_conv_size_to_string( $instance->maxexefilesize ), false,
                        false );
            }
            if ($instance->maxexeprocesses) {
                $this->print_restriction( 'maxexeprocesses', null, false, false );
            }
        }
    }

    /**
     * Show short description
     */
    public function print_shordescription() {
        global $OUTPUT;
        if ($this->instance->shortdescription) {
            echo $OUTPUT->box_start();
            echo format_text( $this->instance->shortdescription, FORMAT_PLAIN );
            echo $OUTPUT->box_end();
        }
    }

    /**
     * Show short description
     */
    public function print_gradereduction($return = false) {
        if ($this->instance->reductionbyevaluation > 0) {
            $html = $this->str_restriction( 'reductionbyevaluation', $this->instance->reductionbyevaluation);
            if ( $this->instance->freeevaluations > 0) {
                $html .= ' ' . $this->str_restriction( 'freeevaluations', $this->instance->freeevaluations);
            }
            if ( $return ) {
                return $html;
            }
            echo $html . '<br>';
        }
    }

    /**
     * Show full description
     */
    public function print_fulldescription() {
        global $OUTPUT;
        $full = $this->get_fulldescription_with_basedon();
        if ($full > '') {
            echo $OUTPUT->box( $full );
        } else {
            $this->print_shordescription();
        }
    }

    /**
     * Print variations in vpl instance
     */
    public function print_variations() {
        global $OUTPUT;
        global $DB;
        require_once(dirname( __FILE__ ) . '/views/show_hide_div.class.php');
        $variations = $DB->get_records( VPL_VARIATIONS, array (
                'vpl' => $this->instance->id
        ) );
        if (count( $variations ) > 0) {
            $div = new vpl_hide_show_div();
            echo '<br /><b>' . get_string( 'variations', VPL ) . $div->generate( true ) . '</b><br />';
            $div->begin_div();
            if (! $this->instance->usevariations) {
                echo '<b>' . get_string( 'variations_unused', VPL ) . '</b><br />';
            }
            if ($this->instance->variationtitle) {
                echo '<b>' . get_string( 'variationtitle', VPL ) . ': ' . s( $this->instance->variationtitle ) . '</b><br />';
            }
            $number = 1;
            foreach ($variations as $variation) {
                echo '<b>' . get_string( 'variation', VPL, $number ) . '</b><br />';
                echo $OUTPUT->box( $variation->description );
                $number ++;
            }
            $div->end_div();
        }
    }

    /**
     * Get user variation. Assign one if needed
     */
    public function get_variation($userid) {
        global $DB;
        if ($this->is_group_activity()) {
            $userid = $this->get_group_leaderid( $userid );
            if ($userid == 0) {
                return false;
            }
        }
        $varassigned = $DB->get_record( VPL_ASSIGNED_VARIATIONS,
                array (
                        'vpl' => $this->instance->id,
                        'userid' => $userid
                ) );
        if ($varassigned === false) { // Variation not assigned.
            $variations = $DB->get_records( VPL_VARIATIONS,
                    array (
                            'vpl' => $this->instance->id
                    ) );
            if (count( $variations ) == 0) { // No variation set.
                return false;
            }
            // Select a random variation.
            shuffle( $variations );
            $variation = $variations [0];
            $assign = new stdClass();
            $assign->vpl = $this->instance->id;
            $assign->variation = $variation->id;
            $assign->userid = $userid;
            if (! $DB->insert_record( VPL_ASSIGNED_VARIATIONS, $assign )) {
                print_error( 'vpl variation not assigned' );
            }
        } else {
            if ($varassigned === false || $varassigned->vpl != $this->instance->id) { // Test consistency.
                // TODO repair inconsistence?
                print_error( 'vpl assigned variation inconsistency' );
            }
            $variation = $DB->get_record( VPL_VARIATIONS,
                    array (
                            'id' => $varassigned->variation
                    ) );
        }
        return $variation;
    }

    /**
     * Show variations if actived and defined
     */
    public function print_variation($userid = 0, $already = array()) {
        global $OUTPUT;
        if (isset( $already [$this->instance->id] )) { // Avoid infinite recursion.
            return;
        }
        $already [$this->instance->id] = true; // Mark as visited.
        if ($this->instance->basedon) { // Show recursive varaitions.
            $basevpl = new mod_vpl( false, $this->instance->basedon );
            $basevpl->print_variation( $userid, $already );
        }
        // If user with grade or manage capability print all variations.
        if ($this->has_capability( VPL_GRADE_CAPABILITY, $userid ) || $this->has_capability( VPL_MANAGE_CAPABILITY,
                $userid )) {
            $this->print_variations();
        }
        // Show user variation if active.
        if ($this->instance->usevariations) { // Variations actived.
            $variation = $this->get_variation( $userid );
            if ($variation !== false) { // Variations defined.
                if ($this->instance->variationtitle > '') {
                    echo '<b>' . format_text( $this->instance->variationtitle, FORMAT_HTML ) . '</b><br />';
                }
                echo $OUTPUT->box( $variation->description );
            }
        }
    }

    /**
     * return an array with variations for this user
     */
    public function get_variation_identification($userid = 0, &$already = array()) {
        if (! ($this->instance->usevariations) || isset( $already [$this->instance->id] )) { // Avoid infinite recursion.
            return array ();
        }
        $already [$this->instance->id] = true;
        if ($this->instance->basedon) {
            $basevpl = new mod_vpl( false, $this->instance->basedon );
            $ret = $basevpl->get_variation_identification( $userid, $already );
        } else {
            $ret = array ();
        }
        $variation = $this->get_variation( $userid );
        if ($variation !== false) {
            $ret [] = $variation->identification;
        }
        return $ret;
    }
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014

    public function count_graded() {
        $numsubs = 0;
        $numgraded = 0;
        $subs = $this->all_last_user_submission( 's.dategraded, s.userid' );
        if ($this->is_group_activity()) { // Fixes group activity userid.
            foreach ($subs as $sub) {
                $group = $this->get_group_members($sub->groupid);
                if ( count($group) ) {
                    $user = reset($group);
                    $sub->userid = $user->id;
                }
            }
        }
        $students = $this->get_students();
        foreach ($subs as $sub) {
            if (isset( $students [$sub->userid] )) {
                $numsubs ++;
                if ($sub->dategraded > 0) { // Is graded.
                    $numgraded ++;
                }
            }
        }
        return array (
                'submissions' => $numsubs,
                'graded' => $numgraded
        );
    }

2015
}