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 Configurable Reports
20
 * @package blocks
21
 * @author: Juan leyva <http://www.twitter.com/jleyvadelgado>
22
 * @date: 2009
23
 */
24
 
25
if (!defined('MOODLE_INTERNAL')) {
26
    //  It must be included from a Moodle page.
27
    die('Direct access to this script is forbidden.');
28
}
29
 
30
require_once($CFG->libdir.'/formslib.php');
31
 
32
class template_form extends moodleform {
33
    public function definition() {
34
        global $DB, $CFG;
35
 
36
        $mform =& $this->_form;
37
 
38
        $report = $this->_customdata['report'];
39
 
40
        $options = array();
41
 
42
        if ($report->type != 'sql') {
43
            $components = cr_unserialize($this->_customdata['report']->components);
44
 
45
            if (is_array($components) && !empty($components['columns']['elements'])) {
46
                $columns = $components['columns']['elements'];
47
                foreach ($columns as $c) {
48
                    $options[] = $c['summary'];
49
                }
50
            }
51
        } else {
52
 
53
            require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
54
            require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$report->type.'/report.class.php');
55
 
56
            $reportclassname = 'report_'.$report->type;
57
            $reportclass = new $reportclassname($report);
58
 
59
            $components = cr_unserialize($report->components);
60
            $config = (isset($components['customsql']['config'])) ? $components['customsql']['config'] : new \stdclass;
61
 
62
            if (isset($config->querysql)) {
63
 
64
                $sql = $config->querysql;
65
                $sql = $reportclass->prepare_sql($sql);
66
                if ($rs = $reportclass->execute_query($sql)) {
67
                    foreach ($rs as $row) {
68
                        $i = 0;
69
                        foreach ($row as $colname => $value) {
70
                            $options[$i] = str_replace('_', ' ', $colname);
71
                            $i++;
72
                        }
73
                        break;
74
                    }
75
                }
76
            }
77
        }
78
 
79
        $optionsenabled = array(
80
 
81
            1 => get_string('enabled', 'block_configurable_reports')
82
        );
83
 
84
        $mform->addElement('select', 'enabled', get_string('template', 'block_configurable_reports'), $optionsenabled);
85
        $mform->setDefault('enabled', 0);
86
 
87
        $mform->addElement('editor', 'header', get_string('header', 'block_configurable_reports'));
88
        $mform->disabledIf('header', 'enabled', 'eq', 0);
89
        $mform->addHelpButton('header', 'template_marks', 'block_configurable_reports');
90
 
91
        $availablemarksrec = '';
92
        if ($options) {
93
            foreach ($options as $o) {
94
                $availablemarksrec .= "[[$o]] => $o <br />";
95
            }
96
        }
97
 
98
        $mform->addElement('static', 'statictext', get_string('availablemarks', 'block_configurable_reports'), $availablemarksrec);
99
        $mform->addElement('editor', 'record', get_string('templaterecord', 'block_configurable_reports'));
100
        $mform->disabledIf('record', 'enabled', 'eq', 0);
101
 
102
        $mform->addElement('editor', 'footer', get_string('footer', 'block_configurable_reports'));
103
        $mform->disabledIf('footer', 'enabled', 'eq', 0);
104
        $mform->addHelpButton('footer', 'template_marks', 'block_configurable_reports');
105
 
106
        $mform->setType('header', PARAM_RAW);
107
        $mform->setType('record', PARAM_RAW);
108
        $mform->setType('footer', PARAM_RAW);
109
 
110
        $this->add_action_buttons();
111
    }
112
 
113
    public function validation($data, $files) {
114
        global $DB, $CFG, $db, $USER;
115
        $errors = parent::validation($data, $files);
116
        if ($data['enabled']) {
117
            if (!$data['record']) {
118
                $errors['record'] = get_string('required');
119
            }
120
        }
121
        return $errors;
122
    }
123
}