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 |
if (!defined('MOODLE_INTERNAL')) {
|
|
|
18 |
// It must be included from a Moodle page.
|
|
|
19 |
die('Direct access to this script is forbidden.');
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
require_once($CFG->libdir.'/formslib.php');
|
|
|
23 |
|
|
|
24 |
class pie_form extends moodleform {
|
|
|
25 |
public function definition() {
|
|
|
26 |
global $DB, $USER, $CFG;
|
|
|
27 |
|
|
|
28 |
$mform =& $this->_form;
|
|
|
29 |
$options = array();
|
|
|
30 |
|
|
|
31 |
$report = $this->_customdata['report'];
|
|
|
32 |
|
|
|
33 |
if ($report->type != 'sql') {
|
|
|
34 |
$components = cr_unserialize($this->_customdata['report']->components);
|
|
|
35 |
|
|
|
36 |
if (!is_array($components) || empty($components['columns']['elements'])) {
|
|
|
37 |
print_error('nocolumns');
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
$columns = $components['columns']['elements'];
|
|
|
41 |
foreach ($columns as $c) {
|
|
|
42 |
if (!empty($c['summary'])) {
|
|
|
43 |
$options[] = $c['summary'];
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
} else {
|
|
|
47 |
|
|
|
48 |
require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
|
|
|
49 |
require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$report->type.'/report.class.php');
|
|
|
50 |
|
|
|
51 |
$reportclassname = 'report_'.$report->type;
|
|
|
52 |
$reportclass = new $reportclassname($report);
|
|
|
53 |
|
|
|
54 |
$components = cr_unserialize($report->components);
|
|
|
55 |
$config = (isset($components['customsql']['config'])) ? $components['customsql']['config'] : new \stdclass;
|
|
|
56 |
|
|
|
57 |
if (isset($config->querysql)) {
|
|
|
58 |
|
|
|
59 |
$sql = $config->querysql;
|
|
|
60 |
$sql = $reportclass->prepare_sql($sql);
|
|
|
61 |
if ($rs = $reportclass->execute_query($sql)) {
|
|
|
62 |
foreach ($rs as $row) {
|
|
|
63 |
$i = 0;
|
|
|
64 |
foreach ($row as $colname => $value) {
|
|
|
65 |
$options[$i] = str_replace('_', ' ', $colname);
|
|
|
66 |
$i++;
|
|
|
67 |
}
|
|
|
68 |
break;
|
|
|
69 |
}
|
|
|
70 |
$rs->close();
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$mform->addElement('header', 'crformheader', get_string('coursefield', 'block_configurable_reports'), '');
|
|
|
76 |
|
|
|
77 |
$mform->addElement('select', 'areaname', get_string('pieareaname', 'block_configurable_reports'), $options);
|
|
|
78 |
$mform->addElement('select', 'areavalue', get_string('pieareavalue', 'block_configurable_reports'), $options);
|
|
|
79 |
$mform->addElement('checkbox', 'group', get_string('groupvalues', 'block_configurable_reports'));
|
|
|
80 |
|
|
|
81 |
$mform->addElement('header', 'legendheader', get_string('legendheader', 'block_configurable_reports'), '');
|
|
|
82 |
$mform->addElement('static', 'legendheaderdesc', get_string('description', 'block_configurable_reports'),
|
|
|
83 |
get_string('legendheaderdesc', 'block_configurable_reports'));
|
|
|
84 |
|
|
|
85 |
$repeatarray = [];
|
|
|
86 |
$repeatarray[] = $mform->createElement('text', 'piechart_label', get_string('piechart_label', 'block_configurable_reports', '{no}'));
|
|
|
87 |
$repeatarray[] = $mform->createElement('text', 'piechart_label_color', get_string('piechart_label_color', 'block_configurable_reports', '{no}'));
|
|
|
88 |
$mform->setType('piechart_label', PARAM_TEXT);
|
|
|
89 |
$mform->setType('piechart_label_color', PARAM_TEXT);
|
|
|
90 |
|
|
|
91 |
$repeatno = 3;
|
|
|
92 |
$repeateloptions = [];
|
|
|
93 |
|
|
|
94 |
$this->repeat_elements($repeatarray, $repeatno,
|
|
|
95 |
$repeateloptions, 'piechart_label_repeats', 'piechart_add_colors', 1,
|
|
|
96 |
get_string('piechart_add_colors', 'block_configurable_reports'), true);
|
|
|
97 |
|
|
|
98 |
$mform->addElement('header', 'generalcolorpaletteheader', get_string('generalcolorpalette', 'block_configurable_reports'), '');
|
|
|
99 |
$mform->addElement('textarea', 'generalcolorpalette', get_string('generalcolorpalette', 'block_configurable_reports'), 'rows="10" cols="7"');
|
|
|
100 |
$mform->addHelpButton('generalcolorpalette', 'generalcolorpalette', 'block_configurable_reports');
|
|
|
101 |
|
|
|
102 |
// Buttons.
|
|
|
103 |
$this->add_action_buttons(true, get_string('add'));
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
function validation($data, $files) {
|
|
|
107 |
$errors = array();
|
|
|
108 |
|
|
|
109 |
$length = count($data['piechart_label']);
|
|
|
110 |
for ($i = 0; $i < $length; $i++) {
|
|
|
111 |
if (!empty($data['piechart_label'][$i])) {
|
|
|
112 |
if (empty($data['piechart_label_color'][$i]) ||
|
|
|
113 |
!preg_match('/^#+([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/', $data['piechart_label_color'][$i])) {
|
|
|
114 |
$errors["piechart_label_color[$i]"] = get_string('invalidcolorcode', 'block_configurable_reports');
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
if (!empty($data['generalcolorpalette'])) {
|
|
|
120 |
$colors = explode(PHP_EOL, $data['generalcolorpalette']);
|
|
|
121 |
foreach ($colors as $color) {
|
|
|
122 |
if (!empty($color) && !preg_match('/^#+([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/', trim($color))) {
|
|
|
123 |
$errors['generalcolorpalette'] = get_string('invalidcolorcode', 'block_configurable_reports');
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
return $errors;
|
|
|
128 |
}
|
|
|
129 |
}
|