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
/**
19
 * Completion Progress block overview page
20
 *
21
 * @package    block_completion_progress
22
 * @copyright  2018 Michael de Raadt
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
// Include required files.
27
require_once(dirname(__FILE__) . '/../../config.php');
28
require_once($CFG->dirroot.'/notes/lib.php');
29
require_once($CFG->libdir.'/tablelib.php');
30
 
31
use block_completion_progress\completion_progress;
32
 
33
/**
34
 * Default number of participants per page.
35
 */
36
const DEFAULT_PAGE_SIZE = 20;
37
 
38
/**
39
 * An impractically high number of participants indicating 'all' are to be shown.
40
 */
41
const SHOW_ALL_PAGE_SIZE = 5000;
42
 
43
// Gather form data.
44
$id       = required_param('instanceid', PARAM_INT);
45
$courseid = required_param('courseid', PARAM_INT);
46
$page     = optional_param('page', 0, PARAM_INT); // Which page to show.
47
$perpage  = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
48
$group    = optional_param('group', 0, PARAM_ALPHANUMEXT); // Group selected.
49
$role     = optional_param('role', null, PARAM_INT);
50
$download = optional_param('download', '', PARAM_ALPHA);
51
 
52
// Determine course and context.
53
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
54
$context = context_course::instance($courseid);
55
 
56
// Get specific block config and context.
57
$block = $DB->get_record('block_instances', array('id' => $id), '*', MUST_EXIST);
58
$blockcontext = context_block::instance($id);
59
 
60
$notesallowed = !empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context);
61
$messagingallowed = !empty($CFG->messaging) && has_capability('moodle/site:sendmessage', $context);
62
$bulkoperations = has_capability('moodle/course:bulkmessaging', $context) && ($notesallowed || $messagingallowed);
63
 
64
// Set up page parameters.
65
$PAGE->set_course($course);
66
$PAGE->set_url(
67
    '/blocks/completion_progress/overview.php',
68
    array(
69
        'instanceid' => $id,
70
        'courseid'   => $courseid,
71
        'page'       => $page,
72
        'perpage'    => $perpage,
73
        'group'      => $group,
74
        'role'       => $role,
75
    )
76
);
77
$PAGE->set_context($context);
78
$title = get_string('overview', 'block_completion_progress');
79
$PAGE->set_title($title);
80
$PAGE->set_heading($title);
81
$PAGE->navbar->add($title);
82
$PAGE->set_pagelayout('report');
83
 
84
$cachevalue = debugging() ? -1 : (int)get_config('block_completion_progress', 'cachevalue');
85
$PAGE->requires->css('/blocks/completion_progress/css.php?v=' . $cachevalue);
86
 
87
// Check user is logged in and capable of accessing the Overview.
88
require_login($course, false);
89
require_capability('block/completion_progress:overview', $blockcontext);
90
 
91
$progress = (new completion_progress($course))
92
    ->for_overview()
93
    ->for_block_instance($block);
94
 
95
// Prepare a group selector if there are groups in the course.
96
$groupids = [];
97
$groupoptions = [];
98
if (has_capability('moodle/site:accessallgroups', $context)) {
99
    $allgroups = groups_get_all_groups($course->id, 0);
100
    $allgroupings = groups_get_all_groupings($course->id);
101
    if ($allgroups) {
102
        $groupoptions[0] = get_string('allparticipants');
103
    }
104
} else {
105
    $allgroups = groups_get_all_groups($course->id, $USER->id);
106
    $allgroupings = [];
107
    $groupids = array_keys($allgroups);
108
}
109
foreach ($allgroups as $rec) {
110
    if ($group == $rec->id) {
111
        $groupids = [ $rec->id ]; // Selected filter.
112
    }
113
    $groupoptions[$rec->id] = format_string($rec->name);
114
}
115
foreach ($allgroupings as $rec) {
116
    if ($group === "g{$rec->id}") { // Selected grouping.
117
        $groupids = array_keys(groups_get_all_groups($course->id, 0, $rec->id));
118
    }
119
    $groupoptions["g{$rec->id}"] = format_string($rec->name);
120
}
121
if (!$groupids) {
122
    $group = 0;
123
    $PAGE->set_url($PAGE->url, ['group' => $group]);
124
}
125
 
126
// Prepare the roles menu.
127
$sql = "SELECT DISTINCT r.id, r.name, r.shortname, r.archetype, r.sortorder
128
          FROM {role} r, {role_assignments} ra
129
         WHERE ra.contextid = :contextid
130
           AND r.id = ra.roleid
131
        ORDER BY r.sortorder";
132
$params = ['contextid' => $context->id];
133
$roles = role_fix_names($DB->get_records_sql($sql, $params), $context);
134
$roleoptions = array(0 => get_string('allparticipants'));
135
foreach ($roles as $rec) {
136
    if ($role === null && $rec->archetype === 'student') {
137
        $role = $rec->id;  // First student role is the default.
138
        $PAGE->set_url($PAGE->url, ['role' => $role]);
139
    }
140
    $roleoptions[$rec->id] = $rec->localname;
141
}
142
 
143
// Setup the overview table.
144
$table = new block_completion_progress\table\overview($progress, $groupids, $role, $bulkoperations);
145
$table->define_baseurl($PAGE->url);
146
$table->show_download_buttons_at([]);   // We'll output them ourselves.
147
$table->is_downloading($download, 'completion_progress-' . $COURSE->shortname);
148
$table->setup();
149
 
150
if ($download) {
151
    $table->query_db($perpage);
152
    $table->start_output();
153
    $table->build_table();
154
    $table->finish_output();
155
    exit;
156
}
157
 
158
$output = $PAGE->get_renderer('block_completion_progress');
159
 
160
// Start page output.
161
echo $output->header();
162
echo $output->heading($title, 2);
163
echo $output->container_start('block_completion_progress');
164
 
165
// Check if activities/resources have been selected in config.
166
if (!$progress->has_activities()) {
167
    echo get_string('no_activities_message', 'block_completion_progress');
168
    echo $output->container_end();
169
    echo $output->footer();
170
    die();
171
}
172
 
173
echo $output->container_start('progressoverviewmenus');
174
if ($groupoptions) {
175
    echo $output->single_select($PAGE->url, 'group', $groupoptions, $group,
176
        ['' => 'choosedots'], null, ['label' => get_string('groupsvisible')]);
177
}
178
if ($roleoptions) {
179
    echo $output->single_select($PAGE->url, 'role', $roleoptions, $role,
180
        ['' => 'choosedots'], null, ['label' => get_string('role')]);
181
}
182
echo $output->container_end();
183
 
184
// Form for messaging selected participants.
185
$formattributes = array('action' => $CFG->wwwroot.'/user/action_redir.php', 'method' => 'post', 'id' => 'participantsform');
186
$formattributes['data-course-id'] = $course->id;
187
$formattributes['data-table-unique-id'] = 'block-completion_progress-overview-' . $course->id;
188
echo html_writer::start_tag('form', $formattributes);
189
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
190
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'returnto', 'value' => s($PAGE->url->out(false))));
191
 
192
// Imitate a 3.9 dynamic table enough to fool the core_user/participants JS code, until
193
// next time it changes again.
194
$tabledivattributes = [
195
    'data-region' => 'core_table/dynamic',
196
    'data-table-uniqueid' => $formattributes['data-table-unique-id'],
197
];
198
echo html_writer::start_div('', $tabledivattributes);
199
 
200
// Render the overview table.
201
$table->query_db($perpage);
202
$table->start_output();
203
$table->build_table();
204
$table->finish_output();
205
 
206
echo html_writer::end_div();    // Closes the 3.9 imitation table wrapper.
207
 
208
// Output paging controls.
209
if ($table->totalrows > $perpage || $perpage == SHOW_ALL_PAGE_SIZE) {
210
    $perpageurl = new moodle_url($PAGE->url, ['page' => 0]);
211
    if ($perpage < SHOW_ALL_PAGE_SIZE) {
212
        $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
213
        echo $output->container(html_writer::link($perpageurl,
214
            get_string('showall', '', $table->totalrows)), array(), 'showall');
215
    } else {
216
        $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
217
        echo $output->container(html_writer::link($perpageurl,
218
            get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
219
    }
220
}
221
 
222
if ($bulkoperations) {
223
    echo '<br /><div class="form-inline m-1">';
224
 
225
    $displaylist = array();
226
    if ($messagingallowed) {
227
        $displaylist['#messageselect'] = get_string('messageselectadd');
228
    }
229
    if ($notesallowed) {
230
        $displaylist['#addgroupnote'] = get_string('addnewnote', 'notes');
231
    }
232
 
233
    echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
234
    echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
235
 
236
    echo '<input type="hidden" name="id" value="'.$course->id.'" />';
237
    echo '<noscript style="display:inline">';
238
    echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
239
    echo '</noscript>';
240
    echo '</div>';
241
 
242
    $options = new stdClass();
243
    $options->noteStateNames = note_get_state_names();
244
    $options->uniqueid = $formattributes['data-table-unique-id'];
245
    echo '<div class="d-none" data-region="state-help-icon">' . $output->help_icon('publishstate', 'notes') . '</div>';
246
    $PAGE->requires->js_call_amd('block_completion_progress/overview', 'init', [$options]);
247
}
248
echo html_writer::end_tag('form');
249
 
250
echo $table->download_buttons();
251
 
252
// Organise access to JS for progress bars.
253
$PAGE->requires->js_call_amd('block_completion_progress/progressbar', 'init', [
254
    'instances' => array($block->id),
255
]);
256
 
257
echo $output->container_end();
258
echo $output->footer();