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 customizable 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
require_login();
29
 
30
$id = required_param('id', PARAM_ALPHANUM);
31
$reportid = required_param('reportid', PARAM_INT);
32
 
33
if (!$report = $DB->get_record('block_configurable_reports', array('id' => $reportid))) {
34
    print_error('reportdoesnotexists');
35
}
36
 
37
$courseid = $report->courseid;
38
 
39
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
40
    print_error('No such course id');
41
}
42
 
43
// Force user login in course (SITE or Course).
44
if ($course->id == SITEID) {
45
    require_login();
46
    $context = context_system::instance();
47
} else {
48
    require_login($course->id);
49
    $context = context_course::instance($course->id);
50
}
51
require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
52
require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$report->type.'/report.class.php');
53
 
54
$reportclassname = 'report_'.$report->type;
55
$reportclass = new $reportclassname($report);
56
 
57
if (!$reportclass->check_permissions($USER->id, $context)) {
58
    print_error("No permissions");
59
} else {
60
    $components = cr_unserialize($report->components);
61
    $graphs = $components['plot']['elements'];
62
 
63
    if (!empty($graphs)) {
64
        $series = array();
65
        foreach ($graphs as $g) {
66
            require_once($CFG->dirroot.'/blocks/configurable_reports/components/plot/'.$g['pluginname'].'/plugin.class.php');
67
            if ($g['id'] == $id) {
68
                $classname = 'plugin_'.$g['pluginname'];
69
                $class = new $classname($report);
70
                $series = $class->get_series($g['formdata']);
71
                break;
72
            }
73
        }
74
 
75
        if ($g['id'] == $id) {
76
            include($CFG->dirroot."/blocks/configurable_reports/lib/pChart2/class/pDraw.class.php");
77
            include($CFG->dirroot."/blocks/configurable_reports/lib/pChart2/class/pData.class.php");
78
            include($CFG->dirroot."/blocks/configurable_reports/lib/pChart2/class/pImage.class.php");
79
 
80
            // Dataset definition.
81
            $dataset = new pData();
82
            $labels = array_shift($series);
83
 
84
            // Invert/Reverse Hebrew labels so it can be rendered using PHP imagettftext()
85
            // Also find the longest value, to aid with sizing the image.
86
            $longestlabel = 0;
87
            foreach ($labels as $key => $value) {
88
                $labellen = strlen($value);
89
                $labellen > $longestlabel && $longestlabel = $labellen;
90
                $invertedlabels[$key] = strip_tags((preg_match("/[\xE0-\xFA]/", iconv("UTF-8", "ISO-8859-8", $value))) ? $reportclass->utf8_strrev($value) : $value);
91
            }
92
            $dataset->addPoints($invertedlabels, "Labels");
93
            $dataset->setAbscissa("Labels");
94
 
95
            $longestlegend = 0;
96
            foreach ($series as $name => $valueset) {
97
                $legendlen = strlen($name);
98
                $legendlen > $longestlegend && $longestlegend = $legendlen;
99
                $dataset->addPoints($valueset, $name);
100
            }
101
 
102
            $width = property_exists($g['formdata'], "width") ? $g['formdata']->width : 900;
103
            $height = property_exists($g['formdata'], "height") ? $g['formdata']->height : 500;
104
            $colorr = property_exists($g['formdata'], "color_r") ? $g['formdata']->color_r : 170;
105
            $colorg = property_exists($g['formdata'], "color_g") ? $g['formdata']->color_g : 183;
106
            $colorb = property_exists($g['formdata'], "color_b") ? $g['formdata']->color_b : 87;
107
            $padding = 30;
108
            $fontsize = 8;
109
            $fontpath = $CFG->dirroot."/blocks/configurable_reports/lib/pChart2/fonts";
110
            $labeloffset = $longestlabel * ($fontsize / 2);
111
            $minlabeloffset = $padding + 100;
112
            $maxlabeloffset = $height / 2 + $padding;
113
            if ($labeloffset < $minlabeloffset) {
114
                $labeloffset = $minlabeloffset;
115
            } else if ($labeloffset > $maxlabeloffset) {
116
                $labeloffset = $maxlabeloffset;
117
            }
118
            $legendoffset = ($longestlegend * ($fontsize / 2));
119
            $maxlegendoffset = $width / 3 + $padding;
120
            if ($legendoffset > $maxlegendoffset) {
121
                $legendoffset = $maxlegendoffset;
122
            }
123
 
124
            $mypicture = new pImage($width, $height, $dataset);
125
            $mypicture->setFontProperties(array("FontName" => "$fontpath/calibri.ttf", "FontSize" => $fontsize));
126
            list($legendwidth, $legendheight) = array_values($mypicture->getLegendSize());
127
            $legendx = $width - $legendwidth - $padding;
128
            $legendy = $padding;
129
            $colnames = array_keys($series);
130
            $firstcol = $colnames[0];
131
            $graphx = $padding + (strlen($firstcol) * ($fontsize / 2));
132
            $graphy = $padding;
133
            $graphwidth = $legendx - $padding;
134
            $graphheight = $height - $labeloffset;
135
 
136
            $bgsettings = array('R' => 225, 'G' => 225, 'B' => 225);
137
            $mypicture->drawFilledRectangle(0, 0, $width + 2, $height + 2, $bgsettings);
138
            $mypicture->setGraphArea($graphx, $graphy, $graphwidth, $graphheight);
139
 
140
            $scalesettings = array(
141
                "TickR" => 0,
142
                "TickG" => 0,
143
                "TickB" => 0,
144
                "LabelRotation" => 45,
145
                "DrawSubTicks" => true
146
            );
147
            $mypicture->drawScale($scalesettings);
148
            $mypicture->setShadow(true, array("X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10));
149
 
150
            $chartsettings = array(
151
                "DisplayValues" => true,
152
                "Rounded" => true,
153
                "Surrounding" => 60,
154
                "DisplayR" => 0,
155
                "DisplayG" => 0,
156
                "DisplayB" => 0,
157
                "DisplayOffset" => 5
158
            );
159
            $mypicture->drawBarChart($chartsettings);
160
            $mypicture->setShadow(false);
161
            $mypicture->drawLegend($legendx, $legendy);
162
            $mypicture->stroke();
163
 
164
            // Hack to clear output and send only IMAGE data to browser.
165
            ob_clean();
166
        }
167
    }
168
}