Proyectos de Subversion Moodle

Rev

Rev 5 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4 ariadna 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
 * File call to overview the votes in the table
19
 *
20
 * @package    block_point_view
21
 * @copyright  2020 Quentin Fombaron, 2021 Astor Bizard
22
 * @author     Quentin Fombaron <q.fombaron@outlook.fr>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
require_once(__DIR__ . '/../../config.php');
27
global $CFG, $DB, $PAGE, $OUTPUT;
28
require_once(__DIR__ . '/locallib.php');
29
 
30
require_login();
31
 
32
$id = required_param('instanceid', PARAM_INT);
33
$courseid = required_param('courseid', PARAM_INT);
34
$contextid = required_param('contextid', PARAM_INT);
35
$downloadformat = optional_param('downloadformat', '', PARAM_RAW);
36
 
37
$context = context_course::instance($courseid);
38
 
39
require_capability('block/point_view:access_overview', $context);
40
 
41
$course = get_course($courseid);
42
 
43
$PAGE->set_course($course);
44
$PAGE->set_context($context);
45
 
46
$blockrecord = $DB->get_record('block_instances', [ 'id' => $id ]);
47
 
48
block_point_view_check_instance($blockrecord, $context, format_string($course->fullname));
49
 
50
$parameters = [
51
        'instanceid' => $id,
52
        'contextid'  => $contextid,
53
        'courseid'   => $courseid,
54
];
55
 
56
$PAGE->set_url(new moodle_url("{$CFG->wwwroot}/blocks/point_view/overview.php", $parameters));
57
 
58
$title = get_string('reactionsdetails', 'block_point_view');
59
$heading = format_string($course->fullname) . ' - ' . get_string('pluginname', 'block_point_view');
60
$PAGE->set_title($heading . ' - ' . $title);
61
$PAGE->set_heading($heading);
62
$PAGE->navbar->add($title);
63
$PAGE->set_pagelayout('report');
64
 
65
$block = block_instance('point_view', $blockrecord);
66
 
67
$PAGE->requires->js_call_amd('block_point_view/script_menu_point_view', 'init');
68
 
69
$sql = 'SELECT cmid,
70
    COALESCE(COUNT(cmid), 0) AS total,
71
    COALESCE(TableTypeOne.TotalTypeOne, 0) AS typeone,
72
    COALESCE(TableTypeTwo.TotalTypeTwo, 0) AS typetwo,
73
    COALESCE(TableTypeThree.TotalTypethree, 0) AS typethree
74
  FROM {block_point_view}
75
    NATURAL LEFT JOIN (SELECT cmid, COUNT(vote) AS TotalTypeOne FROM {block_point_view}
76
      WHERE vote = 1 GROUP BY cmid) AS TableTypeOne
77
    NATURAL LEFT JOIN (SELECT cmid, COUNT(vote) AS TotalTypeTwo FROM {block_point_view}
78
      WHERE vote = 2 GROUP BY cmid) AS TableTypeTwo
79
    NATURAL LEFT JOIN (SELECT cmid, COUNT(vote) AS TotalTypethree FROM {block_point_view}
80
      WHERE vote = 3 GROUP BY cmid) AS TableTypeThree
81
    WHERE courseid = :courseid
82
  GROUP BY cmid, TableTypeOne.TotalTypeOne, TableTypeTwo.TotalTypeTwo, TableTypeThree.TotalTypethree;';
83
 
84
$result = $DB->get_records_sql($sql, [ 'courseid' => $courseid ]);
85
 
86
$users = $DB->get_records('user', null, '', implode(',', \core_user\fields::get_picture_fields()));
87
 
88
$sqldata = $DB->get_records('block_point_view', ['courseid' => $courseid], '', 'id,cmid,userid,vote');
89
 
90
$usersdisplay = [];
91
 
92
$tabledata = [];
93
$tablerowclasses = [];
94
 
95
$isdownloading = ($downloadformat > '');
96
$downloaddata = [];
97
 
98
$votestypes = [ 'typeone' => 'easy', 'typetwo' => 'better', 'typethree' => 'hard' ];
99
$pixparam = block_point_view_get_current_pix($block, array_values($votestypes));
100
 
101
$cms = get_fast_modinfo($courseid, -1)->cms;
102
foreach ($cms as $cm) {
103
 
104
    if (isset($result[$cm->id])) {
105
 
106
        $sectionname = get_section_name($course, $cm->sectionnum);
107
        $modulename = $cm->get_formatted_name();
108
 
109
        if (!$isdownloading) {
110
            $icon = $OUTPUT->pix_icon('icon', $cm->get_module_type_name(), $cm->modname, [ 'class' => 'iconlarge activityicon' ]);
111
            $modulename = $icon . $modulename;
112
        }
113
 
114
        $votecells = [];
115
        foreach ($votestypes as $type => $difficulty) {
116
            $nvotes = intval($result[$cm->id]->$type);
117
            if ($isdownloading) {
118
                $votecells[] = $nvotes;
119
            } else {
120
                $text = block_point_view_get_reaction_text($block, $difficulty);
121
                $votecell = new html_table_cell(
122
                        html_writer::empty_tag('img', [
123
                                'src' => $pixparam[$difficulty],
124
                                'class' => 'overview_img align-bottom mr-1',
125
                                'alt' => $text,
126
                                'title' => $text,
127
                        ]) .
128
                        '<span class="votePercent">' .
129
                        round(100 * $nvotes / intval($result[$cm->id]->total)) . '%' .
130
                        '</span>' .
131
                        '<span class="voteInt" style="display: none;">' . $nvotes . '</span>');
132
                if ($nvotes === 0) {
133
                    $votecell->attributes['class'] .= ' novote';
134
                }
135
                $votecells[] = $votecell;
136
            }
137
        }
138
 
139
        $details = array_fill(0, 7, []);
140
 
141
        foreach ($sqldata as $row) {
142
            if ($row->cmid == $cm->id) {
143
                if (!isset($usersdisplay[$row->userid])) {
144
                    $user = $users[$row->userid];
145
                    $usersdisplay[$row->userid] = fullname($user);
146
                    if (!$isdownloading) {
147
                        $usersdisplay[$row->userid] = $OUTPUT->user_picture($user) . $usersdisplay[$row->userid];
148
                    }
149
                }
150
                $details[$row->vote + 1][] = $usersdisplay[$row->userid];
151
            }
152
        }
153
 
154
        $data = [
155
                $sectionname,
156
                $modulename,
157
                $votecells[0],
158
                $votecells[1],
159
                $votecells[2],
160
                $result[$cm->id]->total,
161
        ];
162
 
163
        if ($isdownloading) {
164
            // Set a slighlty different layout for table download.
165
            $vote = [ 'easy', 'better', 'hard' ];
166
            foreach (array_slice($details, 2, 3) as $uservote => $usernames) {
167
                foreach ($usernames as $username) {
168
                    $downloaddata[] = array_merge($data, [ $vote[$uservote], $username ]);
169
                }
170
            }
171
        } else {
172
            $detailsrow = new html_table_row(array_map(function($usernames) {
173
                return implode('<br>', $usernames);
174
            }, $details));
175
            $detailsrow->style = 'display: none;';
176
 
177
            array_push($tabledata,
178
                    array_merge($data, [ '<i class="fa fa-fw fa-lg fa-caret-right" style="display: none;"></i>' ]),
179
                    $detailsrow
180
                    );
181
 
182
            array_push($tablerowclasses, 'row_module', 'row_module_details');
183
        }
184
 
185
    }
186
}
187
 
188
if ($isdownloading) {
189
    // This is a request to download the table.
190
    confirm_sesskey();
191
 
192
    $headers = [
193
            get_string('section'),
194
            get_string('module', 'block_point_view'),
195
            'easy_vote_number',
196
            'better_vote_number',
197
            'hard_vote_number',
198
            get_string('total'),
199
            'user_vote',
200
            get_string('user'),
201
    ];
202
 
203
    $file = $CFG->dirroot . '/dataformat/' . $downloadformat . '/classes/writer.php';
204
    if (is_readable($file)) {
205
        include_once($file);
206
    }
207
    $writerclass = 'dataformat_' . $downloadformat. '\writer';
208
    if (!class_exists($writerclass)) {
209
        throw new moodle_exception('invalidparameter', 'debug');
210
    }
211
 
212
    $writer = new $writerclass();
213
 
214
    $writer->set_filename(clean_filename('block_point_view_export_' . $course->shortname));
215
    $writer->send_http_headers();
216
    $writer->set_sheettitle($course->shortname);
217
    $writer->start_output();
218
 
219
    $writer->start_sheet($headers);
220
 
221
    foreach ($downloaddata as $rownum => $row) {
222
        $writer->write_record($row, $rownum + 1);
223
    }
224
 
225
    $writer->close_sheet($headers);
226
 
227
    $writer->close_output();
228
    exit();
229
}
230
 
231
echo $OUTPUT->header();
232
echo $OUTPUT->heading($title);
233
echo $OUTPUT->container_start('block_point_view');
234
 
235
if (!empty($result)) {
236
 
237
    $table = new html_table();
238
 
239
    $table->head = [
240
        get_string('section'),
241
        get_string('module', 'block_point_view'),
242
        '',
243
        get_string('reactions', 'block_point_view'),
244
        '',
245
        get_string('total'),
246
        '',
247
    ];
248
 
249
    $table->size = [
250
            '5%',
251
            '25%',
252
            '20%',
253
            '20%',
254
            '20%',
255
            '5%',
256
            '5%',
257
    ];
258
 
259
    $table->attributes['class'] = 'generaltable';
260
 
261
    $table->rowclasses = $tablerowclasses;
262
 
263
    $table->colclasses = [
264
            '',
265
            '',
266
            'reactions-col clickable',
267
            'reactions-col clickable',
268
            'reactions-col clickable',
269
            'text-center',
270
            'text-center clickable font-weight-bold',
271
    ];
272
 
273
    $table->data = $tabledata;
274
 
275
    echo html_writer::table($table);
276
 
277
    echo $OUTPUT->download_dataformat_selector(
278
            get_string('downloadas', 'table'),
279
            $PAGE->url->out_omit_querystring(),
280
            'downloadformat',
281
            $PAGE->url->params()
282
    );
283
} else {
284
    echo html_writer::tag('h4', get_string('noreactionsyet', 'block_point_view'));
285
}
286
 
287
echo $OUTPUT->container_end();
288
echo $OUTPUT->footer();