1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Exports all notes from activity. Code based on CSV export from plugin Board.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_stickynotes
|
|
|
21 |
* @copyright 2021 Olivier VALENTIN
|
|
|
22 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require(__DIR__.'/../../config.php');
|
|
|
26 |
|
|
|
27 |
use mod_stickynotes\stickynotes;
|
|
|
28 |
|
|
|
29 |
// Course module id.
|
|
|
30 |
$id = optional_param('id', 0, PARAM_INT);
|
|
|
31 |
|
|
|
32 |
// Retrieve all informations.
|
|
|
33 |
if ($id) {
|
|
|
34 |
$cm = get_coursemodule_from_id('stickynotes', $id, 0, false, MUST_EXIST);
|
|
|
35 |
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
|
|
|
36 |
$moduleinstance = $DB->get_record('stickynotes', array('id' => $cm->instance), '*', MUST_EXIST);
|
|
|
37 |
} else {
|
|
|
38 |
throw new moodle_exception(get_string('missingidandcmid', 'mod_stickynotes'));
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
require_login($course, true, $cm);
|
|
|
42 |
$modulecontext = context_module::instance($cm->id);
|
|
|
43 |
|
|
|
44 |
require_capability('mod/stickynotes:export', $modulecontext);
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
// Start to retrieve all columns for this instance.
|
|
|
49 |
$cols = $DB->get_records('stickynotes_column', array('stickyid' => $moduleinstance->id), '', '*');
|
|
|
50 |
$allcols = array();
|
|
|
51 |
|
|
|
52 |
// For each columns, retrieve all notes.
|
|
|
53 |
foreach ($cols as $col) {
|
|
|
54 |
$notes = $DB->get_records('stickynotes_note', array('stickyid' => $moduleinstance->id, 'stickycolid' => $col->id),
|
|
|
55 |
'ordernote', '*');
|
|
|
56 |
|
|
|
57 |
$allnotes = new StdClass;
|
|
|
58 |
$allnotes = array();
|
|
|
59 |
// For each note, retrieve and define all necessary information.
|
|
|
60 |
foreach ($notes as $note) {
|
|
|
61 |
// Count votes for note.
|
|
|
62 |
$votes = $DB->count_records('stickynotes_vote', array('stickyid' => $moduleinstance->id, 'stickynoteid' => $note->id),'*');
|
|
|
63 |
// Retrieve author.
|
|
|
64 |
$getname = $DB->get_record('user', array('id' => $note->userid));
|
|
|
65 |
$author = $getname->lastname." ".$getname->firstname;
|
|
|
66 |
// Contact message, author and votes.
|
|
|
67 |
$note->final = $note->message.' ('.$author.' - '.$votes.' votes)';
|
|
|
68 |
|
|
|
69 |
$allnotes[] = (object)$note;
|
|
|
70 |
}
|
|
|
71 |
$notescol = new StdClass;
|
|
|
72 |
$notescol->columnid = $col->id;
|
|
|
73 |
$notescol->title = $col->title;
|
|
|
74 |
$notescol->allnotes = $allnotes;
|
|
|
75 |
|
|
|
76 |
$allcols[] = (object)$notescol;
|
|
|
77 |
}
|
|
|
78 |
// All informations are set.
|
|
|
79 |
|
|
|
80 |
// Header for a new CSV document.
|
|
|
81 |
header('Content-Type: text/csv;charset=utf-8');
|
|
|
82 |
header("Content-disposition: attachment; filename=\"" . strip_tags($moduleinstance->name).'_stickynotes_'.
|
|
|
83 |
date('YmdHis').'.csv' . "\"");
|
|
|
84 |
header("Pragma: no-cache");
|
|
|
85 |
header("Expires: 0");
|
|
|
86 |
|
|
|
87 |
$fp = fopen('php://output', 'w');
|
|
|
88 |
$maxnotes = 0;
|
|
|
89 |
$line = [];
|
|
|
90 |
|
|
|
91 |
foreach ($allcols as $col) {
|
|
|
92 |
$countnotes = count($col->allnotes);
|
|
|
93 |
$maxnotes = $countnotes > $maxnotes ? $countnotes : $maxnotes;
|
|
|
94 |
|
|
|
95 |
array_push($line, $col->title);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
fputcsv($fp, $line);
|
|
|
99 |
|
|
|
100 |
$noterow = 0;
|
|
|
101 |
while ($noterow < $maxnotes) {
|
|
|
102 |
$line = [];
|
|
|
103 |
foreach ($allcols as $col) {
|
|
|
104 |
$notes = array_values($col->allnotes);
|
|
|
105 |
array_push($line, isset($notes[$noterow]) ? $notes[$noterow]->final : '');
|
|
|
106 |
}
|
|
|
107 |
$noterow++;
|
|
|
108 |
fputcsv($fp, $line);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
fclose($fp);
|
|
|
112 |
exit();
|