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 customizable reports
|
|
|
20 |
* @package blocks
|
|
|
21 |
* @author: Juan leyva <http://www.twitter.com/jleyvadelgado>
|
|
|
22 |
* @date: 2009
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
class block_configurable_reports extends block_list {
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Sets the block name and version number
|
|
|
29 |
*
|
|
|
30 |
* @return void
|
|
|
31 |
**/
|
|
|
32 |
public function init() {
|
|
|
33 |
$this->title = get_string('pluginname', 'block_configurable_reports');
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Act on instance data.
|
|
|
38 |
*/
|
|
|
39 |
public function specialization() {
|
|
|
40 |
|
|
|
41 |
if ($this->config) {
|
|
|
42 |
$this->title = $this->config->title ? $this->config->title : get_string('pluginname', 'block_configurable_reports');
|
|
|
43 |
} else {
|
|
|
44 |
$this->title = get_string('pluginname', 'block_configurable_reports');
|
|
|
45 |
$this->config = new stdClass();
|
|
|
46 |
$this->config->displayglobalreports = true;
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public function instance_allow_config() {
|
|
|
51 |
return true;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Where to add the block
|
|
|
56 |
*
|
|
|
57 |
* @return boolean
|
|
|
58 |
**/
|
|
|
59 |
public function applicable_formats() {
|
|
|
60 |
return array('site' => true, 'course' => true, 'my' => true);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Global Config?
|
|
|
65 |
*
|
|
|
66 |
* @return boolean
|
|
|
67 |
**/
|
|
|
68 |
public function has_config() {
|
|
|
69 |
return true;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* More than one instance per page?
|
|
|
74 |
*
|
|
|
75 |
* @return boolean
|
|
|
76 |
**/
|
|
|
77 |
public function instance_allow_multiple() {
|
|
|
78 |
return false;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Gets the contents of the block (course view)
|
|
|
83 |
*
|
|
|
84 |
* @return object An object with the contents
|
|
|
85 |
**/
|
|
|
86 |
public function get_content() {
|
|
|
87 |
global $DB, $USER, $CFG, $COURSE;
|
|
|
88 |
|
|
|
89 |
if ($this->content !== null) {
|
|
|
90 |
return $this->content;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$this->content = new stdClass;
|
|
|
94 |
$this->content->footer = '';
|
|
|
95 |
$this->content->icons = array();
|
|
|
96 |
|
|
|
97 |
if (!isloggedin()) {
|
|
|
98 |
return $this->content;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
require_once($CFG->dirroot."/blocks/configurable_reports/locallib.php");
|
|
|
102 |
|
|
|
103 |
$course = $DB->get_record('course', array('id' => $COURSE->id));
|
|
|
104 |
|
|
|
105 |
if (!$course) {
|
|
|
106 |
print_error('coursedoesnotexists');
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
if ($course->id == SITEID) {
|
|
|
110 |
$context = context_system::instance();
|
|
|
111 |
} else {
|
|
|
112 |
$context = context_course::instance($course->id);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
// Site (Shared) reports.
|
|
|
116 |
if (!empty($this->config->displayglobalreports)) {
|
|
|
117 |
$reports = $DB->get_records('block_configurable_reports', array('global' => 1), 'name ASC');
|
|
|
118 |
|
|
|
119 |
if ($reports) {
|
|
|
120 |
foreach ($reports as $report) {
|
|
|
121 |
if ($report->visible && cr_check_report_permissions($report, $USER->id, $context)) {
|
|
|
122 |
$rname = format_string($report->name);
|
|
|
123 |
$params = ['id' => $report->id, 'courseid' => $course->id];
|
|
|
124 |
$url = new \moodle_url('/blocks/configurable_reports/viewreport.php', $params);
|
|
|
125 |
$attrs = ['alt' => $rname];
|
|
|
126 |
$this->content->items[] = \html_writer::link($url, $rname, $attrs);
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
if (!empty($this->content->items)) {
|
|
|
130 |
$this->content->items[] = '========';
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
// Course reports.
|
|
|
136 |
if (!property_exists($this, 'config')
|
|
|
137 |
or !isset($this->config->displayreportslist)
|
|
|
138 |
or $this->config->displayreportslist) {
|
|
|
139 |
$reports = $DB->get_records('block_configurable_reports', array('courseid' => $course->id), 'name ASC');
|
|
|
140 |
|
|
|
141 |
if ($reports) {
|
|
|
142 |
foreach ($reports as $report) {
|
|
|
143 |
if (!$report->global && $report->visible && cr_check_report_permissions($report, $USER->id, $context)) {
|
|
|
144 |
$rname = format_string($report->name);
|
|
|
145 |
$params = ['id' => $report->id, 'courseid' => $course->id];
|
|
|
146 |
$url = new \moodle_url('/blocks/configurable_reports/viewreport.php', $params);
|
|
|
147 |
$attrs = ['alt' => $rname];
|
|
|
148 |
$this->content->items[] = \html_writer::link($url, $rname, $attrs);
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
if (!empty($this->content->items)) {
|
|
|
152 |
$this->content->items[] = '========';
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
if (has_capability('block/configurable_reports:managereports', $context)
|
|
|
158 |
|| has_capability('block/configurable_reports:manageownreports', $context)) {
|
|
|
159 |
$url = new \moodle_url('/blocks/configurable_reports/managereport.php', ['courseid' => $course->id]);
|
|
|
160 |
$linktext = get_string('managereports', 'block_configurable_reports');
|
|
|
161 |
$this->content->items[] = \html_writer::link($url, $linktext);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
return $this->content;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
public function cron() {
|
|
|
168 |
global $CFG, $DB;
|
|
|
169 |
|
|
|
170 |
$hour = get_config('block_configurable_reports', 'cron_hour');
|
|
|
171 |
$min = get_config('block_configurable_reports', 'cron_minute');
|
|
|
172 |
|
|
|
173 |
$date = usergetdate(time());
|
|
|
174 |
$usertime = mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']);
|
|
|
175 |
|
|
|
176 |
$crontime = mktime($hour, $min, $date['seconds'], $date['mon'], $date['mday'], $date['year']);
|
|
|
177 |
|
|
|
178 |
if ( ($crontime - $usertime) < 0 ) {
|
|
|
179 |
return false;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
$lastcron = $DB->get_field('block', 'lastcron', array('name' => 'configurable_reports'));
|
|
|
183 |
if (!$lastcron and ($lastcron + $this->cron < time()) ) {
|
|
|
184 |
return false;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
// Starting to run...
|
|
|
188 |
require_once($CFG->dirroot."/blocks/configurable_reports/locallib.php");
|
|
|
189 |
require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
|
|
|
190 |
require_once($CFG->dirroot.'/blocks/configurable_reports/reports/sql/report.class.php');
|
|
|
191 |
|
|
|
192 |
mtrace("\nConfigurable report (block)");
|
|
|
193 |
|
|
|
194 |
$reports = $DB->get_records('block_configurable_reports');
|
|
|
195 |
if ($reports) {
|
|
|
196 |
foreach ($reports as $report) {
|
|
|
197 |
// Running only SQL reports. $report->type == 'sql'.
|
|
|
198 |
if ($report->type == 'sql' AND (!empty($report->cron) AND $report->cron == '1')) {
|
|
|
199 |
$reportclass = new report_sql($report);
|
|
|
200 |
|
|
|
201 |
// Execute it using $remotedb.
|
|
|
202 |
$starttime = microtime(true);
|
|
|
203 |
mtrace("\nExecuting query '$report->name'");
|
|
|
204 |
|
|
|
205 |
$components = cr_unserialize($reportclass->config->components);
|
|
|
206 |
$config = (isset($components['customsql']['config'])) ? $components['customsql']['config'] : new \stdclass;
|
|
|
207 |
$sql = $reportclass->prepare_sql($config->querysql);
|
|
|
208 |
|
|
|
209 |
$sqlqueries = explode(';', $sql);
|
|
|
210 |
|
|
|
211 |
foreach ($sqlqueries as $sql) {
|
|
|
212 |
mtrace(substr($sql, 0, 60)); // Show some SQL.
|
|
|
213 |
$results = $reportclass->execute_query($sql);
|
|
|
214 |
if ($results == 1) {
|
|
|
215 |
mtrace('...OK time='.round((microtime(true) - $starttime) * 1000).'mSec');
|
|
|
216 |
} else {
|
|
|
217 |
mtrace('Some SQL Error'.'\n');
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
unset($reportclass);
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
return true; // Finished OK.
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
}
|