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 |
require_once($CFG->dirroot.'/blocks/configurable_reports/plugin.class.php');
|
|
|
26 |
|
|
|
27 |
class plugin_pie extends plugin_base{
|
|
|
28 |
|
|
|
29 |
public function init() {
|
|
|
30 |
$this->fullname = get_string('pie', 'block_configurable_reports');
|
|
|
31 |
$this->form = true;
|
|
|
32 |
$this->ordering = true;
|
|
|
33 |
$this->reporttypes = array('courses', 'sql', 'users', 'timeline', 'categories');
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function summary($data) {
|
|
|
37 |
return get_string('piesummary', 'block_configurable_reports');
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
// Data -> Plugin configuration data.
|
|
|
41 |
public function execute($id, $data, $finalreport) {
|
|
|
42 |
global $DB, $CFG;
|
|
|
43 |
|
|
|
44 |
$series = array();
|
|
|
45 |
if ($finalreport) {
|
|
|
46 |
foreach ($finalreport as $r) {
|
|
|
47 |
if ($data->areaname == $data->areavalue) {
|
|
|
48 |
$hash = md5(strtolower($r[$data->areaname]));
|
|
|
49 |
if (isset($series[0][$hash])) {
|
|
|
50 |
$series[1][$hash] += 1;
|
|
|
51 |
} else {
|
|
|
52 |
$series[0][$hash] = str_replace(',', '', $r[$data->areaname]);
|
|
|
53 |
$series[1][$hash] = 1;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
} else if (!isset($data->group) || ! $data->group) {
|
|
|
57 |
$series[0][] = str_replace(',', '', $r[$data->areaname]);
|
|
|
58 |
$series[1][] = (isset($r[$data->areavalue]) && is_numeric($r[$data->areavalue])) ? $r[$data->areavalue] : 0;
|
|
|
59 |
} else {
|
|
|
60 |
$hash = md5(strtolower($r[$data->areaname]));
|
|
|
61 |
if (isset($series[0][$hash])) {
|
|
|
62 |
$series[1][$hash] += (isset($r[$data->areavalue]) && is_numeric($r[$data->areavalue])) ? $r[$data->areavalue] : 0;
|
|
|
63 |
} else {
|
|
|
64 |
$series[0][$hash] = str_replace(',', '', $r[$data->areaname]);
|
|
|
65 |
$series[1][$hash] = (isset($r[$data->areavalue]) && is_numeric($r[$data->areavalue])) ? $r[$data->areavalue] : 0;
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
// Custom sort.
|
|
|
72 |
$colors = [];
|
|
|
73 |
$mappedcolors = [];
|
|
|
74 |
$unmappedcolors = [];
|
|
|
75 |
|
|
|
76 |
if (!empty($data->{'piechart_label'})) {
|
|
|
77 |
$length = count($data->{'piechart_label'});
|
|
|
78 |
for ($i = 0; $i < $length; $i++) {
|
|
|
79 |
if (!empty($data->{'piechart_label'}[$i])) {
|
|
|
80 |
$key = $data->{'piechart_label'}[$i];
|
|
|
81 |
$colorcode = ltrim($data->{'piechart_label_color'}[$i], '#');
|
|
|
82 |
$mappedcolors[$key] = $colorcode;
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
$mappedcolorkeys = array_keys($mappedcolors);
|
|
|
87 |
|
|
|
88 |
if (!empty($data->{'generalcolorpalette'})) {
|
|
|
89 |
$rawunmappedcolors = explode(PHP_EOL, $data->{'generalcolorpalette'});
|
|
|
90 |
foreach ($rawunmappedcolors as $rawcolor) {
|
|
|
91 |
if (!empty($rawcolor)) {
|
|
|
92 |
$unmappedcolors[] = ltrim(trim($rawcolor), '#');
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
$serie0sorted = [];
|
|
|
98 |
$serie1sorted = [];
|
|
|
99 |
$i = 0;
|
|
|
100 |
$unmappedindex = 0;
|
|
|
101 |
$unmappedcolorcount = count($unmappedcolors);
|
|
|
102 |
foreach ($series[0] as $index => $serie) {
|
|
|
103 |
$serie = strip_tags($serie);
|
|
|
104 |
$serie0sorted[] = $serie;
|
|
|
105 |
$serie1sorted[] = $series[1][$index];
|
|
|
106 |
if (in_array($serie, $mappedcolorkeys)) {
|
|
|
107 |
$colors[$i] = $this->parse_color($mappedcolors[$serie]);
|
|
|
108 |
} else if ($unmappedindex < $unmappedcolorcount) {
|
|
|
109 |
$colors[$i] = $this->parse_color($unmappedcolors[$unmappedindex]);
|
|
|
110 |
$unmappedindex++;
|
|
|
111 |
} else {
|
|
|
112 |
$colors[$i] = '';
|
|
|
113 |
}
|
|
|
114 |
$i++;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
$serie0 = base64_encode(strip_tags(implode(',', $serie0sorted)));
|
|
|
118 |
$serie1 = base64_encode(implode(',', $serie1sorted));
|
|
|
119 |
$colorpalette = base64_encode(implode(',', $colors));
|
|
|
120 |
|
|
|
121 |
return $CFG->wwwroot.'/blocks/configurable_reports/components/plot/pie/graph.php?reportid='.$this->report->id.'&id='.$id.'&serie0='.$serie0.'&serie1='.$serie1.'&colorpalette='.$colorpalette;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
public function get_series($data) {
|
|
|
125 |
$serie0 = required_param('serie0', PARAM_RAW);
|
|
|
126 |
$serie1 = required_param('serie1', PARAM_RAW);
|
|
|
127 |
|
|
|
128 |
return array(explode(',', base64_decode($serie0)), explode(',', base64_decode($serie1)));
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public function get_color_palette($data) {
|
|
|
132 |
if ($colorpalette = optional_param('colorpalette', '', PARAM_RAW)) {
|
|
|
133 |
$colorpalette = explode(',', base64_decode($colorpalette));
|
|
|
134 |
foreach ($colorpalette as $index => $item) {
|
|
|
135 |
if (!empty($item)) {
|
|
|
136 |
$colorpalette[$index] = explode('|', $item);
|
|
|
137 |
} else {
|
|
|
138 |
unset($colorpalette[$index]);
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
return $colorpalette;
|
|
|
142 |
}
|
|
|
143 |
return null;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
public function parse_color($colorcode) {
|
|
|
147 |
return implode('|', array_map(
|
|
|
148 |
function ($c) {
|
|
|
149 |
return hexdec(str_pad($c, 2, $c));
|
|
|
150 |
}, str_split($colorcode, strlen($colorcode) > 4 ? 2 : 1)));
|
|
|
151 |
}
|
|
|
152 |
}
|