Proyectos de Subversion Moodle

Rev

| 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
 
177
foreach ($overrides as $override) {
178
 
179
    $fields = array();
180
    $values = array();
181
    $active = true;
182
 
183
    // Check for inactive overrides.
184
    if (!$groupmode) {
185
        if (!is_enrolled($context, $override->userid)) {
186
            // User not enrolled.
187
            $active = false;
188
        } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
189
            // User cannot access the module.
190
            $active = false;
191
        }
192
    }
193
 
194
    // Format allowsubmissionsfromdate.
195
    if (isset($override->allowsubmissionsfromdate)) {
196
        $fields[] = get_string('open', 'assign');
197
        $values[] = $override->allowsubmissionsfromdate > 0 ? userdate($override->allowsubmissionsfromdate) : get_string('noopen',
198
            'assign');
199
    }
200
 
201
    // Format duedate.
202
    if (isset($override->duedate)) {
203
        $fields[] = get_string('duedate', 'assign');
204
        $values[] = $override->duedate > 0 ? userdate($override->duedate) : get_string('noclose', 'assign');
205
    }
206
 
207
    // Format cutoffdate.
208
    if (isset($override->cutoffdate)) {
209
        $fields[] = get_string('cutoffdate', 'assign');
210
        $values[] = $override->cutoffdate > 0 ? userdate($override->cutoffdate) : get_string('noclose', 'assign');
211
    }
212
 
213
    // Format timelimit.
214
    $timelimitenabled = get_config('assign', 'enabletimelimit');
215
    if ($timelimitenabled && isset($override->timelimit)) {
216
        $fields[] = get_string('timelimit', 'assign');
217
        $values[] = $override->timelimit > 0 ? format_time($override->timelimit) : get_string('none', 'assign');
218
    }
219
 
220
    // Icons.
221
    $iconstr = '';
222
 
223
    // Edit.
224
    $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
225
    $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
226
            $OUTPUT->pix_icon('t/edit', get_string('edit')) . '</a> ';
227
    // Duplicate.
228
    $copyurlstr = $overrideediturl->out(true,
229
            array('id' => $override->id, 'action' => 'duplicate'));
230
    $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
231
            $OUTPUT->pix_icon('t/copy', get_string('copy')) . '</a> ';
232
    // Delete.
233
    $deleteurlstr = $overridedeleteurl->out(true,
234
            array('id' => $override->id, 'sesskey' => sesskey()));
235
    $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
236
                $OUTPUT->pix_icon('t/delete', get_string('delete')) . '</a> ';
237
 
238
    if ($groupmode) {
239
        $usergroupstr = '<a href="' . $groupurl->out(true, ['group' => $override->groupid]) . '" >' .
240
            format_string($override->name, true, ['context' => $context]) . '</a>';
241
 
242
        // Move up.
243
        if ($override->sortorder > 1) {
244
            $iconstr .= '<a title="'.get_string('moveup').'" href="overrides.php?cmid=' . $cmid .
245
                '&amp;id=' . $override->id .'&amp;action=movegroupoverride&amp;dir=up&amp;sesskey='.sesskey().'">' .
246
                $OUTPUT->pix_icon('t/up', get_string('moveup')) . '</a> ';
247
        } else {
248
            $iconstr .= $OUTPUT->spacer() . ' ';
249
        }
250
 
251
        // Move down.
252
        if ($override->sortorder < $overridecountgroup) {
253
            $iconstr .= '<a title="'.get_string('movedown').'" href="overrides.php?cmid='.$cmid.
254
                '&amp;id=' . $override->id . '&amp;action=movegroupoverride&amp;dir=down&amp;sesskey='.sesskey().'">' .
255
                $OUTPUT->pix_icon('t/down', get_string('movedown')) . '</a> ';
256
        } else {
257
            $iconstr .= $OUTPUT->spacer() . ' ';
258
        }
259
 
260
 
261
    } else {
262
        $usergroupstr = html_writer::link($userurl->out(false,
263
                array('id' => $override->userid, 'course' => $course->id)),
264
                fullname($override));
265
    }
266
 
267
    $class = '';
268
    if (!$active) {
269
        $class = "dimmed_text";
270
        $usergroupstr .= '*';
271
        $hasinactive = true;
272
    }
273
 
274
    $usergroupcell = new html_table_cell();
275
    $usergroupcell->rowspan = count($fields);
276
    $usergroupcell->text = $usergroupstr;
277
    $actioncell = new html_table_cell();
278
    $actioncell->rowspan = count($fields);
279
    $actioncell->text = $iconstr;
280
 
281
    for ($i = 0; $i < count($fields); ++$i) {
282
        $row = new html_table_row();
283
        $row->attributes['class'] = $class;
284
        if ($i == 0) {
285
            $row->cells[] = $usergroupcell;
286
        }
287
        $cell1 = new html_table_cell();
288
        $cell1->text = $fields[$i];
289
        $row->cells[] = $cell1;
290
        $cell2 = new html_table_cell();
291
        $cell2->text = $values[$i];
292
        $row->cells[] = $cell2;
293
        if ($i == 0) {
294
            $row->cells[] = $actioncell;
295
        }
296
        $table->data[] = $row;
297
    }
298
}
299
 
300
// Output the table and button.
301
echo html_writer::start_tag('div', array('id' => 'assignoverrides'));
302
if (count($table->data)) {
303
    echo html_writer::table($table);
304
} else {
305
    if ($groupmode) {
306
        echo $OUTPUT->notification(get_string('nogroupoverrides', 'mod_assign'), 'info');
307
    } else {
308
        echo $OUTPUT->notification(get_string('nouseroverrides', 'mod_assign'), 'info');
309
    }
310
}
311
 
312
if ($hasinactive) {
313
    echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'assign'), 'dimmed_text');
314
}
315
 
316
echo html_writer::start_tag('div', array('class' => 'buttons'));
317
$options = array();
318
if ($groupmode) {
319
    if (empty($groups)) {
320
        // There are no groups.
321
        echo $OUTPUT->notification(get_string('groupsnone', 'assign'), 'error');
322
        $options['disabled'] = true;
323
    }
324
} else {
325
    $users = array();
326
    // See if there are any users in the assign.
327
    if ($accessallgroups) {
328
        $users = get_enrolled_users($context, '', 0, 'u.id');
329
        $nousermessage = get_string('usersnone', 'assign');
330
    } else if ($groups) {
331
        $enrolledjoin = get_enrolled_join($context, 'u.id');
332
        list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
333
        $params = $enrolledjoin->params + $ingroupparams;
334
        $sql = "SELECT u.id
335
                  FROM {user} u
336
                  JOIN {groups_members} gm ON gm.userid = u.id
337
                       {$enrolledjoin->joins}
338
                 WHERE gm.groupid $ingroupsql
339
                       AND {$enrolledjoin->wheres}
340
              ORDER BY $sort";
341
        $users = $DB->get_records_sql($sql, $params);
342
        $nousermessage = get_string('usersnone', 'assign');
343
    } else {
344
        $nousermessage = get_string('groupsnone', 'assign');
345
    }
346
    $info = new \core_availability\info_module($cm);
347
    $users = $info->filter_user_list($users);
348
 
349
    if (empty($users)) {
350
        // There are no users.
351
        echo $OUTPUT->notification($nousermessage, 'error');
352
        $options['disabled'] = true;
353
    }
354
}
355
echo html_writer::end_tag('div');
356
echo html_writer::end_tag('div');
357
 
358
// Finish the page.
359
echo $OUTPUT->footer();