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
require_once("../../config.php");
18
 
19
global $DB, $PAGE, $OUTPUT;
20
 
21
// Input params.
22
$courseid = required_param('courseid', PARAM_INT);
23
$instanceid = required_param('instanceid', PARAM_INT);
24
 
25
// Require course login.
26
$course = $DB->get_record("course", array("id" => $courseid), '*', MUST_EXIST);
27
require_course_login($course);
28
 
29
// Require capability to use this plugin in block context.
30
$context = context_block::instance($instanceid);
31
require_capability('block/dedication:use', $context);
32
 
33
require_once('dedication_lib.php');
34
 
35
// Optional params from request or default values.
36
$action = optional_param('action', 'all', PARAM_ALPHANUM);
37
$id = optional_param('id', 0, PARAM_INT);
38
$download = optional_param('download', false, PARAM_BOOL);
39
 
40
// Current url.
41
$pageurl = new moodle_url('/blocks/dedication/dedication.php');
42
$pageurl->params(array(
43
    'courseid' => $courseid,
44
    'instanceid' => $instanceid,
45
    'action' => $action,
46
    'id' => $id,
47
));
48
 
49
// Page format.
50
$PAGE->set_context($context);
51
$PAGE->set_pagelayout('report');
52
$PAGE->set_pagetype('course-view-' . $course->format);
53
$PAGE->navbar->add(get_string('pluginname', 'block_dedication'), new moodle_url('/blocks/dedication/dedication.php', array('courseid' => $courseid, 'instanceid' => $instanceid)));
54
$PAGE->set_url($pageurl);
55
$PAGE->set_title(get_string('pagetitle', 'block_dedication', $course->shortname));
56
$PAGE->set_heading($course->fullname);
57
 
58
// Load libraries.
59
require_once('dedication_form.php');
60
 
61
// Load calculate params from form, request or set default values.
62
$mform = new dedication_block_selection_form($pageurl, null, 'get');
63
if ($mform->is_submitted()) {
64
    // Params from form post.
65
    $formdata = $mform->get_data();
66
    $mintime = $formdata->mintime;
67
    $maxtime = $formdata->maxtime;
68
    $limit = $formdata->limit;
69
} else {
70
    // Params from request or default values.
71
    $mintime = optional_param('mintime', $course->startdate, PARAM_INT);
72
    $maxtime = optional_param('maxtime', time(), PARAM_INT);
73
    $limit = optional_param('limit', BLOCK_DEDICATION_DEFAULT_SESSION_LIMIT, PARAM_INT);
74
    $mform->set_data(array('mintime' => $mintime, 'maxtime' => $maxtime, 'limit' => $limit));
75
}
76
 
77
// Url with params for links inside tables.
78
$pageurl->params(array(
79
    'mintime' => $mintime,
80
    'maxtime' => $maxtime,
81
    'limit' => $limit,
82
));
83
 
84
// Object to store view data.
85
$view = new stdClass();
86
$view->header = array();
87
 
88
$tablestyles = block_dedication_utils::get_table_styles();
89
$view->table = new html_table();
90
$view->table->attributes = array('class' => $tablestyles['table_class'] . " table-$action");
91
 
92
switch ($action) {
93
    case 'user':
94
        $userid = required_param('id', PARAM_INT);
95
 
96
        $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
97
        if (!is_enrolled(context_course::instance($course->id), $user)) {
98
            print_error('usernotincourse');
99
        }
100
 
101
        $dm = new block_dedication_manager($course, $mintime, $maxtime, $limit);
102
        if ($download) {
103
            $dm->download_user_dedication($user);
104
            exit;
105
        }
106
 
107
        // Table formatting & total count.
108
        $totaldedication = 0;
109
        $rows = $dm->get_user_dedication($user);
110
        foreach ($rows as $index => $row) {
111
            $totaldedication += $row->dedicationtime;
112
            $rows[$index] = array(
113
                userdate($row->start_date),
114
                block_dedication_utils::format_dedication($row->dedicationtime),
115
                block_dedication_utils::format_ips($row->ips),
116
            );
117
        }
118
 
119
        $view->header[] = get_string('userdedication', 'block_dedication', $OUTPUT->user_picture($user, array('courseid' => $course->id)) . fullname($user));
120
        $view->header[] = get_string('period', 'block_dedication', (object) array('mintime' => userdate($mintime), 'maxtime' => userdate($maxtime)));
121
        $view->header[] = get_string('perioddiff', 'block_dedication', format_time($maxtime - $mintime));
122
        $view->header[] = get_string('totaldedication', 'block_dedication', block_dedication_utils::format_dedication($totaldedication));
123
        $view->header[] = get_string('meandedication', 'block_dedication', block_dedication_utils::format_dedication(count($rows) ? $totaldedication / count($rows) : 0));
124
 
125
        $view->table->head = array(get_string('sessionstart', 'block_dedication'), get_string('sessionduration', 'block_dedication'), 'IP');
126
        $view->table->data = $rows;
127
        break;
128
 
129
    case 'group':
130
    case 'all':
131
    default:
132
        $groups = groups_get_all_groups($course->id);
133
 
134
        if ($action == 'group') {
135
            $groupid = required_param('id', PARAM_INT);
136
            if (groups_group_exists($groupid)) {
137
                $students = groups_get_members($groupid);
138
            } else {
139
                // TODO: PUT ERROR STRING NO GROUP.
140
            }
141
        } else {
142
            // Get all students in this course or ordered by group.
143
            if ($course->groupmode == NOGROUPS) {
144
                $students = get_enrolled_users(context_course::instance($course->id));
145
            } else {
146
                $students = array();
147
                foreach ($groups as $group) {
148
                    $members = groups_get_members($group->id);
149
                    $students = array_replace($students, $members);
150
                }
151
                // Empty groups or missconfigured, get all students anyway.
152
                if (!$students) {
153
                    $students = get_enrolled_users(context_course::instance($course->id));
154
                }
155
            }
156
        }
157
 
158
        if (!$students) {
159
            print_error('noparticipants');
160
        }
161
        $dm = new block_dedication_manager($course, $mintime, $maxtime, $limit);
162
        $rows = $dm->get_students_dedication($students);
163
        if ($download) {
164
            $dm->download_students_dedication($rows);
165
            exit;
166
        }
167
 
168
        // Table formatting & total count.
169
        $totaldedication = 0;
170
        foreach ($rows as $index => $row) {
171
            $totaldedication += $row->dedicationtime;
172
            $userurl = new moodle_url($pageurl, array('action' => 'user', 'id' => $row->user->id));
173
            $groupurl = new moodle_url($pageurl, array('action' => 'group', 'id' => $row->groupid));
174
            $rows[$index] = array(
175
                $OUTPUT->user_picture($row->user, array('courseid' => $course->id)),
176
                html_writer::link($userurl, $row->user->firstname),
177
                html_writer::link($userurl, $row->user->lastname),
178
                html_writer::link($groupurl, isset($groups[$row->groupid]) ? $groups[$row->groupid]->name : ''),
179
                block_dedication_utils::format_dedication($row->dedicationtime),
180
                $row->connectionratio
181
            );
182
        }
183
 
184
        if ($action == 'group') {
185
            $view->header[] = get_string('dedicationgroup', 'block_dedication', $groups[$groupid]->name);
186
        } else {
187
            $view->header[] = get_string('dedicationall', 'block_dedication');
188
        }
189
        $view->header[] = get_string('period', 'block_dedication', (object) array('mintime' => userdate($mintime), 'maxtime' => userdate($maxtime)));
190
        $view->header[] = get_string('perioddiff', 'block_dedication', format_time($maxtime - $mintime));
191
        $view->header[] = get_string('totaldedication', 'block_dedication', block_dedication_utils::format_dedication($totaldedication));
192
        $view->header[] = get_string('meandedication', 'block_dedication', block_dedication_utils::format_dedication(count($rows) ? $totaldedication / count($rows) : 0));
193
 
194
        $view->table->head = array('', get_string('firstname'), get_string('lastname'), get_string('group'),
195
            get_string('dedicationrow', 'block_dedication'), get_string('connectionratiorow', 'block_dedication'));
196
        $view->table->data = $rows;
197
        break;
198
}
199
 
200
// START PAGE: layout, headers, title, boxes...
201
echo $OUTPUT->header();
202
 
203
// Form.
204
$mform->display();
205
 
206
echo $OUTPUT->box_start();
207
 
208
foreach ($view->header as $header) {
209
    echo $OUTPUT->heading($header, 4);
210
}
211
 
212
// Download button.
213
echo html_writer::start_tag('div', array('class' => 'download-dedication'));
214
echo html_writer::start_tag('p');
215
echo $OUTPUT->single_button(new moodle_url($pageurl, array('download' => true)), get_string('downloadexcel'), 'get');
216
echo html_writer::end_tag('p');
217
echo html_writer::end_tag('div');
218
 
219
// Format table headers if they exists.
220
if (!empty($view->table->head)) {
221
    $headers = array();
222
    foreach ($view->table->head as $header) {
223
        $cell = new html_table_cell($header);
224
        $cell->style = $tablestyles['header_style'];
225
        $headers[] = $cell;
226
    }
227
    $view->table->head = $headers;
228
}
229
echo html_writer::table($view->table);
230
 
231
// END PAGE.
232
echo $OUTPUT->box_end();
233
echo $OUTPUT->footer();