Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * This file contains the forms to create and edit an instance of this module
19
 *
20
 * @package   mod_assign
21
 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
26
 
27
 
28
require_once($CFG->libdir.'/formslib.php');
29
require_once($CFG->dirroot . '/mod/assign/locallib.php');
30
 
31
/**
32
 * Assignment extension dates form
33
 *
34
 * @package   mod_assign
35
 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
36
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class mod_assign_extension_form extends moodleform {
39
    /** @var array $instance - The data passed to this form */
40
    private $instance;
41
 
42
    /**
43
     * Define the form - called by parent constructor
44
     */
45
    public function definition() {
46
        global $DB;
47
 
48
        $mform = $this->_form;
49
        $params = $this->_customdata;
50
 
51
        // Instance variable is used by the form validation function.
52
        $instance = $params['instance'];
53
        $this->instance = $instance;
54
 
55
        // Get the assignment class.
1441 ariadna 56
        /** @var assign $assign */
1 efrain 57
        $assign = $params['assign'];
58
        $userlist = $params['userlist'];
59
 
1441 ariadna 60
        // Load current extensions for all selected users.
61
        [$usercondition, $params] = $DB->get_in_or_equal($userlist);
62
        array_unshift($params, $assign->get_instance()->id);
63
        $userinfo = $DB->get_records_sql("
64
                SELECT u.*, auf.extensionduedate
65
                  FROM {user} u
66
             LEFT JOIN {assign_user_flags} auf ON auf.userid = u.id AND auf.assignment = ?
67
                 WHERE u.id $usercondition
68
            ", $params);
69
 
70
        // Prepare display of up to 5 users.
1 efrain 71
        // TODO Does not support custom user profile fields (MDL-70456).
72
        $extrauserfields = \core_user\fields::get_identity_fields($assign->get_context(), false);
1441 ariadna 73
        $displayedusercount = 0;
74
        $usershtml = '';
1 efrain 75
        foreach ($userlist as $userid) {
1441 ariadna 76
            $displayedusercount += 1;
77
            if ($displayedusercount > 5) {
1 efrain 78
                $usershtml .= get_string('moreusers', 'assign', count($userlist) - 5);
79
                break;
80
            }
81
 
1441 ariadna 82
            $user = $userinfo[$userid];
83
            $usershtml .= $assign->get_renderer()->render(
84
                new assign_user_summary($user,
85
                    $assign->get_course()->id,
86
                    has_capability('moodle/site:viewfullnames',
87
                        $assign->get_course_context()),
88
                    $assign->is_blind_marking(),
89
                    $assign->get_uniqueid_for_user($user->id),
90
                    $extrauserfields,
91
                    !$assign->is_active_user($userid)
92
                ),
93
            );
1 efrain 94
        }
95
 
1441 ariadna 96
        // Find the range of extensions that currently exist for these users.
97
        $earliestextension = null;
98
        $lateststextension = 0;
99
        $userswithoutanextension = 0;
100
        foreach ($userlist as $userid) {
101
            $user = $userinfo[$userid];
1 efrain 102
 
1441 ariadna 103
            if ($user->extensionduedate) {
104
                $lateststextension = max($lateststextension, $user->extensionduedate);
105
                if ($earliestextension !== null) {
106
                    $earliestextension = min($earliestextension, $user->extensionduedate);
107
                } else {
108
                    $earliestextension = $user->extensionduedate;
109
                }
110
            } else {
111
                $userswithoutanextension += 1;
112
            }
113
        }
114
 
115
        $listusersmessage = get_string('grantextensionforusers', 'assign', count($userlist));
1 efrain 116
        $mform->addElement('header', 'general', $listusersmessage);
117
        $mform->addElement('static', 'userslist', get_string('selectedusers', 'assign'), $usershtml);
118
 
119
        if ($instance->allowsubmissionsfromdate) {
120
            $mform->addElement('static', 'allowsubmissionsfromdate', get_string('allowsubmissionsfromdate', 'assign'),
121
                               userdate($instance->allowsubmissionsfromdate));
122
        }
123
 
124
        $finaldate = 0;
125
        if ($instance->duedate) {
126
            $mform->addElement('static', 'duedate', get_string('duedate', 'assign'), userdate($instance->duedate));
127
            $finaldate = $instance->duedate;
128
        }
129
        if ($instance->cutoffdate) {
130
            $mform->addElement('static', 'cutoffdate', get_string('cutoffdate', 'assign'), userdate($instance->cutoffdate));
131
            $finaldate = $instance->cutoffdate;
132
        }
1441 ariadna 133
 
134
        if ($userswithoutanextension == count($userlist)) {
135
            // All users don't have an extension yet.
136
            $currentdatesdisplay = get_string('extensionduedatenone', 'assign');
137
 
138
        } else {
139
            if ($earliestextension == $lateststextension) {
140
                $currentdatesdisplay = userdate($lateststextension);
141
            } else {
142
                $currentdatesdisplay = get_string('extensionduedaterange', 'assign', (object) [
143
                    'earliest' => userdate($earliestextension),
144
                    'latest' => userdate($lateststextension),
145
                ]);
146
            }
147
            if ($userswithoutanextension) {
148
                $currentdatesdisplay .= '<br>' . get_string('extensionduedatewithout', 'assign', $userswithoutanextension);
149
            }
150
        }
151
 
152
        $mform->addElement('static', 'currentextension', get_string('extensionduedatecurrent', 'assign'),
153
            $currentdatesdisplay);
154
 
155
        if ($lateststextension) {
156
            // If there are existing extensions, edit based on the current (latest) value.
157
            $defaultdate = $lateststextension;
158
        } else {
159
            // Otherwise take the later of the deadline and one minute before midnight tonight (server time).
160
            $endoftoday = new DateTimeImmutable('today 23:59', core_date::get_server_timezone_object());
161
            $defaultdate = max($finaldate, $endoftoday->getTimestamp());
162
        }
1 efrain 163
        $mform->addElement('date_time_selector', 'extensionduedate',
164
                           get_string('extensionduedate', 'assign'), array('optional'=>true));
1441 ariadna 165
        $mform->setDefault('extensionduedate', $defaultdate);
1 efrain 166
 
167
        $mform->addElement('hidden', 'id');
168
        $mform->setType('id', PARAM_INT);
169
        $mform->addElement('hidden', 'userid');
170
        $mform->setType('userid', PARAM_INT);
171
        $mform->addElement('hidden', 'selectedusers');
172
        $mform->setType('selectedusers', PARAM_SEQUENCE);
173
        $mform->addElement('hidden', 'action', 'saveextension');
174
        $mform->setType('action', PARAM_ALPHA);
175
 
176
        $this->add_action_buttons(true, get_string('savechanges', 'assign'));
177
    }
178
 
179
    /**
180
     * Perform validation on the extension form
181
     * @param array $data
182
     * @param array $files
183
     */
184
    public function validation($data, $files) {
185
        $errors = parent::validation($data, $files);
186
        if ($this->instance->duedate && $data['extensionduedate']) {
187
            if ($this->instance->duedate > $data['extensionduedate']) {
188
                $errors['extensionduedate'] = get_string('extensionnotafterduedate', 'assign');
189
            }
190
        }
191
        if ($this->instance->allowsubmissionsfromdate && $data['extensionduedate']) {
192
            if ($this->instance->allowsubmissionsfromdate > $data['extensionduedate']) {
193
                $errors['extensionduedate'] = get_string('extensionnotafterfromdate', 'assign');
194
            }
195
        }
196
 
197
        return $errors;
198
    }
199
}