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
class component_conditions extends component_base{
26
 
27
    public function init() {
28
        $this->plugins = true;
29
        $this->ordering = false;
30
        $this->form = true;
31
        $this->help = true;
32
    }
33
 
34
    public function form_process_data(&$cform) {
35
        global $DB;
36
 
37
        if ($this->form) {
38
            $data = $cform->get_data();
39
 
40
            // Function cr_serialize() will add slashes.
41
 
42
            $components = cr_unserialize($this->config->components);
43
            $components['conditions']['config'] = $data;
44
            if (isset($components['conditions']['config']->conditionexpr)) {
45
                $components['conditions']['config']->conditionexpr = $this->add_missing_conditions($components['conditions']['config']->conditionexpr);
46
            }
47
            $this->config->components = cr_serialize($components);
48
            $DB->update_record('block_configurable_reports', $this->config);
49
        }
50
    }
51
 
52
    public function add_missing_conditions($cond) {
53
        global $DB;
54
 
55
        $components = cr_unserialize($this->config->components);
56
 
57
        if (isset($components['conditions']['elements'])) {
58
 
59
            $elements = $components['conditions']['elements'];
60
            $count = count($elements);
61
            if ($count == 0 || $count == 1) {
62
                return '';
63
            }
64
            for ($i = $count; $i > 0; $i--) {
65
                if (strpos($cond, 'c'.$i) === false) {
66
                    if ($count > 1 && $cond) {
67
                        $cond .= " and c$i";
68
                    } else {
69
                        $cond .= "c$i";
70
                    }
71
                }
72
            }
73
 
74
            // Deleting extra conditions.
75
            for ($i = $count + 1; $i <= $count + 5; $i++) {
76
                $cond = preg_replace('/(\bc'.$i.'\b\s+\b(and|or|not)\b\s*)/i', '', $cond);
77
                $cond = preg_replace('/(\s+\b(and|or|not)\b\s+\bc'.$i.'\b)/i', '', $cond);
78
            }
79
        }
80
 
81
        return $cond;
82
    }
83
 
84
    public function form_set_data(&$cform) {
85
        global $DB;
86
        if ($this->form) {
87
            $fdata = new stdclass;
88
            $components = cr_unserialize($this->config->components);
89
            $conditionsconfig = (isset($components['conditions']['config'])) ? $components['conditions']['config'] : new stdclass;
90
 
91
            if (!isset($conditionsconfig->conditionexpr)) {
92
                $conditionsconfig->conditionexpr = '';
93
                $conditionsconfig->conditionexpr = '';
94
            }
95
            $conditionsconfig->conditionexpr = $this->add_missing_conditions($conditionsconfig->conditionexpr);
96
            $fdata->conditionexpr = $conditionsconfig->conditionexpr;
97
 
98
            if (empty($components['conditions'])) {
99
                $components['conditions'] = array();
100
            }
101
 
102
            if (!empty($components['conditions']['config']->conditionexpr)) {
103
                $components['conditions']['config']->conditionexpr = $fdata->conditionexpr;
104
            }
105
            $this->config->components = cr_serialize($components);
106
            $DB->update_record('block_configurable_reports', $this->config);
107
 
108
            $cform->set_data($fdata);
109
        }
110
    }
111
}