submission_form.php 6.17 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
<?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/>.

/**
 * Submission form definition
 *
 * @package mod_vpl
 * @copyright 2012 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>
 */

defined( 'MOODLE_INTERNAL' ) || die();

28
global $CFG;
29
30
require_once($CFG->libdir.'/formslib.php');
require_once(dirname(__FILE__).'/../locallib.php');
31
require_once(dirname(__FILE__).'/edit.class.php');
32
33
34

class mod_vpl_submission_form extends moodleform {
    protected $vpl;
35
    protected $userid;
36
37
38
    protected function getinternalform() {
        return $this->_form;
    }
39
    public function __construct($page, $vpl, $userid) {
40
        $this->vpl = $vpl;
41
        $this->userid = $userid;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        parent::__construct( $page );
    }
    protected function definition() {
        global $CFG;
        $mform = & $this->_form;
        // Identification info.
        $mform->addElement( 'hidden', 'id' );
        $mform->setType( 'id', PARAM_INT );
        $mform->addElement( 'hidden', 'userid', 0 );
        $mform->setType( 'userid', PARAM_INT );
        // Comments.
        $mform->addElement( 'textarea', 'comments', get_string( 'comments', VPL ), array (
                'cols' => '40',
                'rows' => 2
        ) );
        $mform->setType( 'comments', PARAM_TEXT );

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        $firstsub = ($this->vpl->last_user_submission( $this->userid ) === false);

        $mform->addElement( 'select', 'submitmethod', get_string( 'submitmethod', VPL ),
                array('archive' => get_string( 'archive', VPL ), 'files' => get_string( 'files' )) );

        $mform->addElement( 'header', 'headersubmitarchive', get_string( 'submitarchive', VPL ) );

        if (!$firstsub) {
            $mform->addElement( 'radio', 'archiveaction', get_string( 'submitarchive', VPL ),
                    get_string( 'archivereplacedelete', VPL ), 'replacedelete');
            $mform->addElement( 'radio', 'archiveaction', '',
                    get_string( 'archivereplace', VPL ), 'replace' );
            $mform->disabledIf( 'archiveaction', 'submitmethod', 'neq', 'archive' );
        }
        $mform->addElement( 'filepicker', 'archive', null, null, array('accepted_types' => '.zip') );
        $mform->disabledIf( 'archive', 'submitmethod', 'neq', 'archive' );

        $mform->addElement( 'header', 'headersubmitfiles', get_string( 'submitfiles', VPL ) );

78
79
        // Files upload.
        $instance = $this->vpl->get_instance();
80
81
82
        $reqfiles = $this->vpl->get_files('required');
        $i = 0;
        foreach ($reqfiles as $reqfile) {
83
            $field = 'file' . $i;
84
85
86
87
88
89
            if (!$firstsub) {
                $mform->addElement( 'radio', $field . 'action', $reqfile,
                        get_string( 'keepcurrentfile', VPL ), 'keep');
                $mform->addElement( 'radio', $field . 'action', '',
                        get_string( 'replacefile', VPL ), 'replace' );
                $mform->disabledIf( $field . 'action', 'submitmethod', 'neq', 'files' );
90
91
                $mform->addElement( 'hidden', $field . 'name', $reqfile );
                $mform->setType( $field . 'name', PARAM_RAW );
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
            $mform->addElement( 'filepicker', $field );
            $mform->disabledIf( $field, 'submitmethod', 'neq', 'files' );
            if (!$firstsub) {
                $mform->disabledIf( $field, $field . 'action', 'neq', 'replace' );
            }
            $i++;
        }
        $submission = $this->vpl->last_user_submission( $this->userid );
        if ($submission !== false) {
            $subfiles = (new mod_vpl_submission( $this->vpl, $submission ))->get_submitted_fgm()->getFileList();
            foreach ($subfiles as $subfile) {
                if (!in_array($subfile, $reqfiles)) {
                    $field = 'file' . $i;
                    if (!$firstsub) {
                        $mform->addElement( 'radio', $field . 'action', $subfile, get_string( 'keepcurrentfile', VPL ), 'keep');
                        $mform->addElement( 'radio', $field . 'action', '', get_string( 'deletefile', VPL ), 'delete' );
                        $mform->addElement( 'radio', $field . 'action', '', get_string( 'replacefile', VPL ), 'replace' );
                        $mform->disabledIf( $field . 'action', 'submitmethod', 'neq', 'files' );
                        $mform->addElement( 'hidden', $field . 'name', $subfile );
                        $mform->setType( $field . 'name', PARAM_RAW );
                    }
                    $mform->addElement( 'filepicker', $field );
                    $mform->disabledIf( $field, 'submitmethod', 'neq', 'files' );
                    if (!$firstsub) {
                        $mform->disabledIf( $field, $field . 'action', 'neq', 'replace' );
                    }
                    $i++;
                }
            }
        }

        while ($i < $instance->maxfiles) {
            $field = 'file' . $i;
            $mform->addElement( 'filepicker', $field, get_string( 'anyfile', VPL ) );
            $mform->disabledIf( $field, 'submitmethod', 'neq', 'files' );
            $i++;
129
130
        }
        $this->add_action_buttons( true, get_string( 'submit' ) );
131
132
133
134
135
136
137
138
139
140

        global $PAGE;
        $PAGE->requires->js_call_amd('mod_vpl/submissionform', 'setup');
    }
    public function set_data($data) {
        for($i=0; $i < $this->vpl->get_instance()->maxfiles; $i++) {
            $data->{'file'.$i.'action'} = 'keep';
            $data->{'archiveaction'} = 'replacedelete';
        }
        parent::set_data($data);
141
142
    }
}