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
 * Configurable Reports
19
 * A Moodle block for creating Configurable Reports
20
 * @package blocks
21
 * @author: Juan leyva <http://www.twitter.com/jleyvadelgado>
22
 * @date: 2009
23
 */
24
 
25
require_once("../../config.php");
26
 
27
require_once($CFG->dirroot."/blocks/configurable_reports/locallib.php");
28
 
29
$id = optional_param('id', 0, PARAM_INT);
30
$courseid = optional_param('courseid', SITEID, PARAM_INT);
31
$delete = optional_param('delete', 0, PARAM_BOOL);
32
$confirm = optional_param('confirm', 0, PARAM_BOOL);
33
$show = optional_param('show', 0, PARAM_BOOL);
34
$hide = optional_param('hide', 0, PARAM_BOOL);
35
$duplicate = optional_param('duplicate', 0, PARAM_BOOL);
36
 
37
$report = null;
38
 
39
if ($id) {
40
    $courseid = $DB->get_field('block_configurable_reports', 'courseid', ['id' => $id]);
41
}
42
 
43
if (!$course = $DB->get_record('course', ['id' => $courseid]) ) {
44
    print_error('nosuchcourseid', 'block_configurable_reports');
45
}
46
 
47
// Force user login in course (SITE or Course).
48
if ($course->id == SITEID) {
49
    require_login();
50
    $context = context_system::instance();
51
} else {
52
    require_login($course->id);
53
    $context = context_course::instance($course->id);
54
}
55
 
56
$hasmanagereportcap = has_capability('block/configurable_reports:managereports', $context);
57
if (!$hasmanagereportcap && !has_capability('block/configurable_reports:manageownreports', $context)) {
58
    print_error('badpermissions', 'block_configurable_reports');
59
}
60
 
61
$PAGE->set_context($context);
62
$PAGE->set_pagelayout('incourse');
63
 
64
 
65
if ($id) {
66
    if (!$report = $DB->get_record('block_configurable_reports', ['id' => $id])) {
67
        print_error('reportdoesnotexists', 'block_configurable_reports');
68
    }
69
 
70
    if (!$hasmanagereportcap && $report->ownerid != $USER->id) {
71
        print_error('badpermissions', 'block_configurable_reports');
72
    }
73
 
74
    $title = format_string($report->name);
75
 
76
    $courseid = $report->courseid;
77
    if (!$course = $DB->get_record('course', ['id' => $courseid])) {
78
        print_error('nosuchcourseid', 'block_configurable_reports');
79
    }
80
 
81
    require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
82
    require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$report->type.'/report.class.php');
83
    $reportclassname = 'report_'.$report->type;
84
    $reportclass = new $reportclassname($report->id);
85
    $PAGE->set_url('/blocks/configurable_reports/editreport.php', ['id' => $id]);
86
} else {
87
    $title = get_string('newreport', 'block_configurable_reports');
88
    $PAGE->set_url('/blocks/configurable_reports/editreport.php', null);
89
}
90
 
91
if ($report) {
92
    $title = format_string($report->name);
93
} else {
94
    $title = get_string('report', 'block_configurable_reports');
95
}
96
 
97
$courseurl = new \moodle_url($CFG->wwwroot.'/course/view.php', ['id' => $courseid]);
98
$PAGE->navbar->add($course->shortname, $courseurl);
99
 
100
if (!empty($report->courseid)) {
101
    $params = ['courseid' => $report->courseid];
102
} else {
103
    $params = ['courseid' => $courseid];
104
}
105
 
106
$managereporturl = new \moodle_url($CFG->wwwroot.'/blocks/configurable_reports/managereport.php', $params);
107
$PAGE->navbar->add(get_string('managereports', 'block_configurable_reports'), $managereporturl);
108
 
109
$PAGE->navbar->add($title);
110
 
111
// Common actions.
112
if (($show || $hide) && confirm_sesskey()) {
113
    $visible = ($show) ? 1 : 0;
114
    if (!$DB->set_field('block_configurable_reports', 'visible', $visible, ['id' => $report->id])) {
115
        print_error('cannotupdatereport', 'block_configurable_reports');
116
    }
117
    $action = ($visible) ? 'showed' : 'hidden';
118
    cr_add_to_log($report->courseid, 'configurable_reports', 'report '.$action, '/block/configurable_reports/editreport.php?id='.$report->id, $report->id);
119
    header("Location: $CFG->wwwroot/blocks/configurable_reports/managereport.php?courseid=$courseid");
120
    die;
121
}
122
 
123
if ($duplicate && confirm_sesskey()) {
124
    $newreport = new stdclass();
125
    $newreport = $report;
126
    unset($newreport->id);
127
    $newreport->name = get_string('copyasnoun').' '.$newreport->name;
128
    $newreport->summary = $newreport->summary;
129
    if (!$newreportid = $DB->insert_record('block_configurable_reports', $newreport)) {
130
        print_error('cannotduplicate', 'block_configurable_reports');
131
    }
132
    cr_add_to_log($newreport->courseid, 'configurable_reports', 'report duplicated', '/block/configurable_reports/editreport.php?id='.$newreportid, $id);
133
    header("Location: $CFG->wwwroot/blocks/configurable_reports/managereport.php?courseid=$courseid");
134
    die;
135
}
136
 
137
if ($delete && confirm_sesskey()) {
138
    if (!$confirm) {
139
        $PAGE->set_title($title);
140
        $PAGE->set_heading( $title);
141
        $PAGE->set_cacheable( true);
142
        echo $OUTPUT->header();
143
        $message = get_string('confirmdeletereport', 'block_configurable_reports');
144
        $optionsyes = ['id' => $report->id, 'delete' => $delete, 'sesskey' => sesskey(), 'confirm' => 1];
145
        $optionsno = [];
146
        $buttoncontinue = new single_button(new moodle_url('editreport.php', $optionsyes), get_string('yes'), 'get');
147
        $buttoncancel   = new single_button(new moodle_url('managereport.php', $optionsno), get_string('no'), 'get');
148
        echo $OUTPUT->confirm($message, $buttoncontinue, $buttoncancel);
149
        echo $OUTPUT->footer();
150
        exit;
151
    } else {
152
        if ($DB->delete_records('block_configurable_reports', ['id' => $report->id])) {
153
            cr_add_to_log($report->courseid, 'configurable_reports', 'report deleted', '/block/configurable_reports/editreport.php?id='.$report->id, $report->id);
154
        }
155
        header("Location: $CFG->wwwroot/blocks/configurable_reports/managereport.php?courseid=$courseid");
156
        die;
157
    }
158
}
159
 
160
require_once('editreport_form.php');
161
 
162
if (!empty($report)) {
163
    $editform = new report_edit_form('editreport.php', compact('report', 'courseid', 'context'));
164
} else {
165
    $editform = new report_edit_form('editreport.php', compact('courseid', 'context'));
166
}
167
 
168
if (!empty($report)) {
169
    $export = explode(',', $report->export);
170
    if (!empty($export)) {
171
        foreach ($export as $e) {
172
            $report->{'export_'.$e} = 1;
173
        }
174
    }
175
    $editform->set_data($report);
176
}
177
 
178
if ($editform->is_cancelled()) {
179
    if (!empty($report)) {
180
        redirect($CFG->wwwroot.'/blocks/configurable_reports/editreport.php?id='.$report->id);
181
    } else {
182
        redirect($CFG->wwwroot.'/blocks/configurable_reports/editreport.php');
183
    }
184
} else if ($data = $editform->get_data()) {
185
    require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
186
    require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$data->type.'/report.class.php');
187
    if (empty($report)) {
188
        $reportclassname = 'report_'.$data->type;
189
    } else {
190
        $reportclassname = 'report_'.$report->type;
191
    }
192
 
193
    $arraydata = (array) $data;
194
    $data->export = '';
195
    foreach ($arraydata as $key => $d) {
196
        if (strpos($key, 'export_') !== false) {
197
            $data->export .= str_replace('export_', '', $key).',';
198
        }
199
    }
200
 
201
    if (!isset($data->global)) {
202
        $data->global = 0;
203
    }
204
 
205
    if (!isset($data->jsordering)) {
206
        $data->jsordering = 0;
207
    }
208
 
209
    if (!isset($data->remote)) {
210
        $data->remote = 0;
211
    }
212
 
213
    if (!isset($data->cron)) {
214
        $data->cron = 0;
215
    }
216
 
217
    if (empty($report)) {
218
        $data->ownerid = $USER->id;
219
        $data->courseid = $courseid;
220
        $data->visible = 1;
221
        $data->components = '';
222
 
223
        // Extra check.
224
        if ($data->type == 'sql' && !has_capability('block/configurable_reports:managesqlreports', $context)) {
225
            print_error('nosqlpermissions');
226
        }
227
 
228
        if (!$lastid = $DB->insert_record('block_configurable_reports', $data)) {
229
            print_error('errorsavingreport', 'block_configurable_reports');
230
        } else {
231
            cr_add_to_log($courseid, 'configurable_reports', 'report created', '/block/configurable_reports/editreport.php?id='.$lastid, $data->name);
232
            $reportclass = new $reportclassname($lastid);
233
            redirect($CFG->wwwroot.'/blocks/configurable_reports/editcomp.php?id='.$lastid.'&comp='.$reportclass->components[0]);
234
        }
235
    } else {
236
        cr_add_to_log($report->courseid, 'configurable_reports', 'edit', '/block/configurable_reports/editreport.php?id='.$id, $report->name);
237
        $reportclass = new $reportclassname($data->id);
238
        $data->type = $report->type;
239
 
240
        if (!$DB->update_record('block_configurable_reports', $data)) {
241
            print_error('errorsavingreport', 'block_configurable_reports');
242
        } else {
243
            redirect($CFG->wwwroot.'/blocks/configurable_reports/editcomp.php?id='.$data->id.'&comp='.$reportclass->components[0]);
244
        }
245
    }
246
}
247
 
248
 
249
$PAGE->set_context($context);
250
 
251
 
252
$PAGE->set_pagelayout('incourse');
253
 
254
 
255
$PAGE->set_title($title);
256
 
257
 
258
$PAGE->set_heading( $title);
259
 
260
 
261
$PAGE->set_cacheable( true);
262
 
263
 
264
echo $OUTPUT->header();
265
 
266
if ($id) {
267
    $currenttab = 'report';
268
    include('tabs.php');
269
}
270
 
271
$editform->display();
272
 
273
echo $OUTPUT->footer();