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 |
require_once($CFG->dirroot."/blocks/configurable_reports/locallib.php");
|
|
|
27 |
|
|
|
28 |
$id = required_param('id', PARAM_INT);
|
|
|
29 |
$download = optional_param('download', false, PARAM_BOOL);
|
|
|
30 |
$format = optional_param('format', '', PARAM_ALPHA);
|
|
|
31 |
$courseid = optional_param('courseid', null, PARAM_INT);
|
|
|
32 |
|
|
|
33 |
if (!$report = $DB->get_record('block_configurable_reports', ['id' => $id])) {
|
|
|
34 |
print_error('reportdoesnotexists', 'block_configurable_reports');
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
if ($courseid && $report->global) {
|
|
|
38 |
$report->courseid = $courseid;
|
|
|
39 |
} else {
|
|
|
40 |
$courseid = $report->courseid;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
if (!$course = $DB->get_record('course', ['id' => $courseid])) {
|
|
|
44 |
print_error('No such course id');
|
|
|
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);
|
|
|
53 |
$context = context_course::instance($course->id);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
|
|
|
57 |
require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$report->type.'/report.class.php');
|
|
|
58 |
|
|
|
59 |
$reportclassname = 'report_'.$report->type;
|
|
|
60 |
$reportclass = new $reportclassname($report);
|
|
|
61 |
|
|
|
62 |
if (!$reportclass->check_permissions($USER->id, $context)) {
|
|
|
63 |
print_error('badpermissions', 'block_configurable_reports');
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
$PAGE->set_context($context);
|
|
|
67 |
$PAGE->set_pagelayout('incourse');
|
|
|
68 |
$PAGE->set_url('/blocks/configurable_reports/viewreport.php', ['id' => $id]);
|
|
|
69 |
$PAGE->requires->jquery();
|
|
|
70 |
|
|
|
71 |
$download = ($download && $format && strpos($report->export, $format.',') !== false) ? true : false;
|
|
|
72 |
|
|
|
73 |
if ($download && $report->type == "sql") $reportclass->setForExport(true);
|
|
|
74 |
$reportclass->create_report();
|
|
|
75 |
|
|
|
76 |
$action = (!empty($download)) ? 'download' : 'view';
|
|
|
77 |
cr_add_to_log($report->courseid, 'configurable_reports', $action, '/block/configurable_reports/viewreport.php?id='.$id, $report->name);
|
|
|
78 |
|
|
|
79 |
// No download, build navigation header etc..
|
|
|
80 |
if (!$download) {
|
|
|
81 |
$reportclass->check_filters_request();
|
|
|
82 |
$reportname = format_string($report->name);
|
|
|
83 |
$navlinks = array();
|
|
|
84 |
|
|
|
85 |
$hasmanageallcap = has_capability('block/configurable_reports:managereports', $context);
|
|
|
86 |
$hasmanageowncap = has_capability('block/configurable_reports:manageownreports', $context);
|
|
|
87 |
|
|
|
88 |
if ($hasmanageallcap || ($hasmanageowncap && $report->ownerid == $USER->id)) {
|
|
|
89 |
$managereporturl = new \moodle_url('/blocks/configurable_reports/managereport.php', ['courseid' => $report->courseid]);
|
|
|
90 |
$PAGE->navbar->add(get_string('managereports', 'block_configurable_reports'), $managereporturl);
|
|
|
91 |
$PAGE->navbar->add($report->name);
|
|
|
92 |
} else {
|
|
|
93 |
// These users don't have the capability to manage reports but we still want them to see some breadcrumbs.
|
|
|
94 |
$PAGE->navbar->add(get_string('viewreport', 'block_configurable_reports'));
|
|
|
95 |
$PAGE->navbar->add($report->name);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
$PAGE->set_title($reportname);
|
|
|
99 |
$PAGE->set_heading( $reportname);
|
|
|
100 |
$PAGE->set_cacheable( true);
|
|
|
101 |
echo $OUTPUT->header();
|
|
|
102 |
|
|
|
103 |
if ($hasmanageallcap || ($hasmanageowncap && $report->ownerid == $USER->id)) {
|
|
|
104 |
$currenttab = 'viewreport';
|
|
|
105 |
include('tabs.php');
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
// Print the report HTML.
|
|
|
109 |
$reportclass->print_report_page($PAGE);
|
|
|
110 |
|
|
|
111 |
} else {
|
|
|
112 |
// Large exports are likely to take their time and memory.
|
|
|
113 |
core_php_time_limit::raise();
|
|
|
114 |
raise_memory_limit(MEMORY_EXTRA);
|
|
|
115 |
$exportplugin = $CFG->dirroot.'/blocks/configurable_reports/export/'.$format.'/export.php';
|
|
|
116 |
if (file_exists($exportplugin)) {
|
|
|
117 |
require_once($exportplugin);
|
|
|
118 |
export_report($reportclass->finalreport);
|
|
|
119 |
}
|
|
|
120 |
die;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
// Never reached if download = true.
|
|
|
124 |
echo $OUTPUT->footer();
|