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 page handles listing of assign overrides
19
 *
20
 * @package    mod_assign
21
 * @copyright  2016 Ilya Tregubov
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
 
26
require_once(dirname(__FILE__) . '/../../config.php');
27
require_once($CFG->dirroot.'/mod/assign/lib.php');
28
require_once($CFG->dirroot.'/mod/assign/locallib.php');
29
require_once($CFG->dirroot.'/mod/assign/override_form.php');
30
 
31
 
32
$cmid = required_param('cmid', PARAM_INT);
33
$mode = optional_param('mode', '', PARAM_ALPHA); // One of 'user' or 'group', default is 'group'.
34
 
35
$action   = optional_param('action', '', PARAM_ALPHA);
36
$redirect = $CFG->wwwroot.'/mod/assign/overrides.php?cmid=' . $cmid . '&amp;mode=group';
37
 
38
list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'assign');
39
$assign = $DB->get_record('assign', array('id' => $cm->instance), '*', MUST_EXIST);
40
 
41
require_login($course, false, $cm);
42
 
43
$context = context_module::instance($cm->id);
44
 
45
// Check the user has the required capabilities to list overrides.
46
require_capability('mod/assign:manageoverrides', $context);
47
 
48
$assigngroupmode = groups_get_activity_groupmode($cm);
49
$accessallgroups = ($assigngroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $context);
50
 
51
$overridecountgroup = $DB->count_records('assign_overrides', array('userid' => null, 'assignid' => $assign->id));
52
 
53
// Get the course groups that the current user can access.
54
$groups = $accessallgroups ? groups_get_all_groups($cm->course) : groups_get_activity_allowed_groups($cm);
55
 
56
// Default mode is "group", unless there are no groups.
57
if ($mode != "user" and $mode != "group") {
58
    if (!empty($groups)) {
59
        $mode = "group";
60
    } else {
61
        $mode = "user";
62
    }
63
}
64
$groupmode = ($mode == "group");
65
 
66
$url = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id, 'mode' => $mode));
67
 
68
$PAGE->set_url($url);
69
navigation_node::override_active_url(new moodle_url('/mod/assign/overrides.php', ['cmid' => $cmid]));
70
 
71
if ($action == 'movegroupoverride') {
72
    $id = required_param('id', PARAM_INT);
73
    $dir = required_param('dir', PARAM_ALPHA);
74
 
75
    if (confirm_sesskey()) {
76
        move_group_override($id, $dir, $assign->id);
77
    }
78
    redirect($redirect);
79
}
80
 
81
// Display a list of overrides.
82
$PAGE->set_pagelayout('admin');
83
$PAGE->add_body_class('limitedwidth');
84
$PAGE->set_title(get_string('overrides', 'assign'));
85
$PAGE->set_heading($course->fullname);
86
$activityheader = $PAGE->activityheader;
87
$activityheader->set_attrs([
88
    'description' => '',
89
    'hidecompletion' => true,
90
    'title' => $activityheader->is_title_allowed() ? format_string($assign->name, true, ['context' => $context]) : ""
91
]);
92
echo $OUTPUT->header();
93
echo $OUTPUT->heading(get_string('overrides', 'mod_assign'), 2);
94
$overridemenu = new \mod_assign\output\override_actionmenu($url, $cm);
95
$renderer = $PAGE->get_renderer('mod_assign');
96
echo $renderer->render($overridemenu);
97
 
98
// Delete orphaned group overrides.
99
$sql = 'SELECT o.id
100
          FROM {assign_overrides} o
101
     LEFT JOIN {groups} g ON o.groupid = g.id
102
         WHERE o.groupid IS NOT NULL
103
               AND g.id IS NULL
104
               AND o.assignid = ?';
105
$params = array($assign->id);
106
$orphaned = $DB->get_records_sql($sql, $params);
107
if (!empty($orphaned)) {
108
    $DB->delete_records_list('assign_overrides', 'id', array_keys($orphaned));
109
}
110
 
111
$overrides = [];
112
 
113
// Fetch all overrides.
114
if ($groupmode) {
115
    $colname = get_string('group');
116
    // To filter the result by the list of groups that the current user has access to.
117
    if ($groups) {
118
        $params = ['assignid' => $assign->id];
119
        list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
120
        $params += $inparams;
121
 
122
        $sql = "SELECT o.*, g.name
123
                  FROM {assign_overrides} o
124
                  JOIN {groups} g ON o.groupid = g.id
125
                 WHERE o.assignid = :assignid AND g.id $insql
126
              ORDER BY o.sortorder";
127
 
128
        $overrides = $DB->get_records_sql($sql, $params);
129
    }
130
} else {
131
    $colname = get_string('user');
132
    list($sort, $params) = users_order_by_sql('u');
133
    $params['assignid'] = $assign->id;
134
 
135
    $userfieldsapi = \core_user\fields::for_name();
136
    if ($accessallgroups) {
137
        $sql = 'SELECT o.*, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects . '
138
                  FROM {assign_overrides} o
139
                  JOIN {user} u ON o.userid = u.id
140
                 WHERE o.assignid = :assignid
141
              ORDER BY ' . $sort;
142
 
143
        $overrides = $DB->get_records_sql($sql, $params);
144
    } else if ($groups) {
145
        list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
146
        $params += $inparams;
147
 
148
        $sql = 'SELECT o.*, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects . '
149
                  FROM {assign_overrides} o
150
                  JOIN {user} u ON o.userid = u.id
151
                  JOIN {groups_members} gm ON u.id = gm.userid
152
                 WHERE o.assignid = :assignid AND gm.groupid ' . $insql . '
153
              ORDER BY ' . $sort;
154
 
155
        $overrides = $DB->get_records_sql($sql, $params);
156
    }
157
}
158
 
159
// Initialise table.
160
$table = new html_table();
161
$table->headspan = array(1, 2, 1);
162
$table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
163
$table->head = array(
164
        $colname,
165
        get_string('overrides', 'assign'),
166
        get_string('action'),
167
);
168
 
169
$userurl = new moodle_url('/user/view.php', array());
170
$groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
171
 
172
$overridedeleteurl = new moodle_url('/mod/assign/overridedelete.php');
173
$overrideediturl = new moodle_url('/mod/assign/overrideedit.php');
174
 
175
$hasinactive = false; // Whether there are any inactive overrides.
176
 
1441 ariadna 177
$PAGE->requires->js_call_amd('mod_assign/override_modal_manager', 'init', [$mode, !empty($assign->gradepenalty)]);
178
 
1 efrain 179
foreach ($overrides as $override) {
180
 
181
    $fields = array();
182
    $values = array();
183
    $active = true;
184
 
185
    // Check for inactive overrides.
186
    if (!$groupmode) {
187
        if (!is_enrolled($context, $override->userid)) {
188
            // User not enrolled.
189
            $active = false;
190
        } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
191
            // User cannot access the module.
192
            $active = false;
193
        }
194
    }
195
 
196
    // Format allowsubmissionsfromdate.
197
    if (isset($override->allowsubmissionsfromdate)) {
198
        $fields[] = get_string('open', 'assign');
199
        $values[] = $override->allowsubmissionsfromdate > 0 ? userdate($override->allowsubmissionsfromdate) : get_string('noopen',
200
            'assign');
201
    }
202
 
203
    // Format duedate.
204
    if (isset($override->duedate)) {
205
        $fields[] = get_string('duedate', 'assign');
206
        $values[] = $override->duedate > 0 ? userdate($override->duedate) : get_string('noclose', 'assign');
207
    }
208
 
209
    // Format cutoffdate.
210
    if (isset($override->cutoffdate)) {
211
        $fields[] = get_string('cutoffdate', 'assign');
212
        $values[] = $override->cutoffdate > 0 ? userdate($override->cutoffdate) : get_string('noclose', 'assign');
213
    }
214
 
215
    // Format timelimit.
216
    $timelimitenabled = get_config('assign', 'enabletimelimit');
217
    if ($timelimitenabled && isset($override->timelimit)) {
218
        $fields[] = get_string('timelimit', 'assign');
219
        $values[] = $override->timelimit > 0 ? format_time($override->timelimit) : get_string('none', 'assign');
220
    }
221
 
222
    // Icons.
223
    $iconstr = '';
224
 
225
    // Edit.
226
    $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
227
    $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
228
            $OUTPUT->pix_icon('t/edit', get_string('edit')) . '</a> ';
229
    // Duplicate.
230
    $copyurlstr = $overrideediturl->out(true,
231
            array('id' => $override->id, 'action' => 'duplicate'));
232
    $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
233
            $OUTPUT->pix_icon('t/copy', get_string('copy')) . '</a> ';
1441 ariadna 234
 
1 efrain 235
    // Delete.
1441 ariadna 236
    $deletelink = html_writer::link(
237
        "#",
238
        $OUTPUT->pix_icon('t/delete', get_string('delete')),
239
        [
240
            'class' => 'delete-override',
241
            'data-overrideid' => $override->id,
242
            'data-sesskey' => sesskey(),
243
        ],
244
    );
245
    $iconstr .= $deletelink;
1 efrain 246
 
247
    if ($groupmode) {
1441 ariadna 248
        $usergroupstr = html_writer::link(
249
            $groupurl->out(true, ['group' => $override->groupid]),
250
            format_string($override->name, true, ['context' => $context]),
251
            ['class' => 'usergroupname'],
252
        );
1 efrain 253
 
254
        // Move up.
255
        if ($override->sortorder > 1) {
256
            $iconstr .= '<a title="'.get_string('moveup').'" href="overrides.php?cmid=' . $cmid .
257
                '&amp;id=' . $override->id .'&amp;action=movegroupoverride&amp;dir=up&amp;sesskey='.sesskey().'">' .
258
                $OUTPUT->pix_icon('t/up', get_string('moveup')) . '</a> ';
259
        } else {
260
            $iconstr .= $OUTPUT->spacer() . ' ';
261
        }
262
 
263
        // Move down.
264
        if ($override->sortorder < $overridecountgroup) {
265
            $iconstr .= '<a title="'.get_string('movedown').'" href="overrides.php?cmid='.$cmid.
266
                '&amp;id=' . $override->id . '&amp;action=movegroupoverride&amp;dir=down&amp;sesskey='.sesskey().'">' .
267
                $OUTPUT->pix_icon('t/down', get_string('movedown')) . '</a> ';
268
        } else {
269
            $iconstr .= $OUTPUT->spacer() . ' ';
270
        }
271
 
272
 
273
    } else {
1441 ariadna 274
        $usergroupstr = html_writer::link(
275
            $userurl->out(false, ['id' => $override->userid, 'course' => $course->id]),
276
            fullname($override),
277
            ['class' => 'usergroupname'],
278
        );
1 efrain 279
    }
280
 
281
    $class = '';
282
    if (!$active) {
283
        $class = "dimmed_text";
284
        $usergroupstr .= '*';
285
        $hasinactive = true;
286
    }
287
 
288
    $usergroupcell = new html_table_cell();
289
    $usergroupcell->rowspan = count($fields);
290
    $usergroupcell->text = $usergroupstr;
291
    $actioncell = new html_table_cell();
292
    $actioncell->rowspan = count($fields);
293
    $actioncell->text = $iconstr;
294
 
295
    for ($i = 0; $i < count($fields); ++$i) {
296
        $row = new html_table_row();
297
        $row->attributes['class'] = $class;
298
        if ($i == 0) {
299
            $row->cells[] = $usergroupcell;
300
        }
301
        $cell1 = new html_table_cell();
302
        $cell1->text = $fields[$i];
303
        $row->cells[] = $cell1;
304
        $cell2 = new html_table_cell();
305
        $cell2->text = $values[$i];
306
        $row->cells[] = $cell2;
307
        if ($i == 0) {
308
            $row->cells[] = $actioncell;
309
        }
310
        $table->data[] = $row;
311
    }
312
}
313
 
314
// Output the table and button.
315
echo html_writer::start_tag('div', array('id' => 'assignoverrides'));
316
if (count($table->data)) {
317
    echo html_writer::table($table);
318
} else {
319
    if ($groupmode) {
320
        echo $OUTPUT->notification(get_string('nogroupoverrides', 'mod_assign'), 'info');
321
    } else {
322
        echo $OUTPUT->notification(get_string('nouseroverrides', 'mod_assign'), 'info');
323
    }
324
}
325
 
326
if ($hasinactive) {
327
    echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'assign'), 'dimmed_text');
328
}
329
 
330
echo html_writer::start_tag('div', array('class' => 'buttons'));
331
$options = array();
332
if ($groupmode) {
333
    if (empty($groups)) {
334
        // There are no groups.
335
        echo $OUTPUT->notification(get_string('groupsnone', 'assign'), 'error');
336
        $options['disabled'] = true;
337
    }
338
} else {
339
    $users = array();
340
    // See if there are any users in the assign.
341
    if ($accessallgroups) {
342
        $users = get_enrolled_users($context, '', 0, 'u.id');
343
        $nousermessage = get_string('usersnone', 'assign');
344
    } else if ($groups) {
345
        $enrolledjoin = get_enrolled_join($context, 'u.id');
346
        list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
347
        $params = $enrolledjoin->params + $ingroupparams;
348
        $sql = "SELECT u.id
349
                  FROM {user} u
350
                  JOIN {groups_members} gm ON gm.userid = u.id
351
                       {$enrolledjoin->joins}
352
                 WHERE gm.groupid $ingroupsql
353
                       AND {$enrolledjoin->wheres}
354
              ORDER BY $sort";
355
        $users = $DB->get_records_sql($sql, $params);
356
        $nousermessage = get_string('usersnone', 'assign');
357
    } else {
358
        $nousermessage = get_string('groupsnone', 'assign');
359
    }
360
    $info = new \core_availability\info_module($cm);
361
    $users = $info->filter_user_list($users);
362
 
363
    if (empty($users)) {
364
        // There are no users.
365
        echo $OUTPUT->notification($nousermessage, 'error');
366
        $options['disabled'] = true;
367
    }
368
}
369
echo html_writer::end_tag('div');
370
echo html_writer::end_tag('div');
371
 
372
// Finish the page.
373
echo $OUTPUT->footer();