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
 * @package     report_overviewstats
19
 * @copyright   2013 David Mudrak <david@moodle.com>
20
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 */
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
/**
26
 * Reports various users related charts and figures
27
 */
28
class report_overviewstats_chart_courses extends report_overviewstats_chart {
29
 
30
    /**
31
     * @return array
32
     */
33
    public function get_content() {
34
        global $OUTPUT;
35
 
36
        $this->prepare_data();
37
 
38
        $title = get_string('chart-courses', 'report_overviewstats');
39
        $titlepercategory = get_string('chart-courses-percategory', 'report_overviewstats');
40
 
41
        $percategorydata = new html_table();
42
        $percategorydata->head = array(
43
            get_string('chart-courses-percategory-categoryname', 'report_overviewstats'),
44
            get_string('chart-courses-percategory-coursesrecursive', 'report_overviewstats'),
45
            get_string('chart-courses-percategory-coursesown', 'report_overviewstats'),
46
        );
47
        foreach ($this->data['percategory'] as $catdata) {
48
            $percategorydata->data[] = new html_table_row(array(
49
                $catdata['categoryname'],
50
                $catdata['coursesrecursive'],
51
                $catdata['coursesown'],
52
            ));
53
        }
54
 
55
        $titlesizes = sprintf('%s %s', get_string('chart-courses-sizes', 'report_overviewstats'),
56
            $OUTPUT->help_icon('chart-courses-sizes', 'report_overviewstats'));
57
 
58
        return array($title => array(
59
            $titlepercategory => html_writer::tag('div',
60
                html_writer::table($percategorydata),
61
                array(
62
                    'id' => 'chart_courses_percategory',
63
                    'class' => 'simple_data_table',
64
                )
65
            ),
66
            $titlesizes => html_writer::tag('div', '', array(
67
                'id' => 'chart_courses_sizes',
68
                'class' => 'chartplaceholder',
69
                'style' => 'min-height: 300px;',
70
            )),
71
        ));
72
    }
73
 
74
    /**
75
     * @see parent
76
     */
77
    public function inject_page_requirements(moodle_page $page) {
78
 
79
        $this->prepare_data();
80
 
81
        $page->requires->yui_module(
82
            'moodle-report_overviewstats-charts',
83
            'M.report_overviewstats.charts.courses.init',
84
            array($this->data)
85
        );
86
    }
87
 
88
    /**
89
     * Prepares data to report
90
     */
91
    protected function prepare_data() {
92
        global $DB;
93
 
94
        if (!is_null($this->data)) {
95
            return;
96
        }
97
 
98
        // Number of courses per category.
99
        $cats = core_course_category::make_categories_list();
100
        $this->data['percategory'] = array();
101
        $total = 0;
102
 
103
        foreach ($cats as $catid => $catname) {
104
            $cat = core_course_category::get($catid);
105
            $coursesown = $cat->get_courses_count();
106
            $total += $coursesown;
107
            $this->data['percategory'][] = array(
108
                'categoryname' => $catname,
109
                'coursesrecursive' => $cat->get_courses_count(array('recursive' => true)),
110
                'coursesown' => $coursesown,
111
            );
112
        }
113
 
114
        $this->data['percategory'][] = array(
115
            'categoryname' => html_writer::tag('strong', get_string('total')),
116
            'coursesrecursive' => '',
117
            'coursesown' => html_writer::tag('strong', $total),
118
        );
119
 
120
        // Distribution graph of number of activities per course.
121
        $sql = "SELECT course, COUNT(id) AS modules
122
                  FROM {course_modules}
123
              GROUP BY course";
124
 
125
        $rs = $DB->get_recordset_sql($sql);
126
 
127
        $max = 0;
128
        $data = array();
129
        $this->data['sizes'] = array();
130
 
131
        foreach ($rs as $record) {
132
            $distributiongroup = floor($record->modules / 5); // 0 for 0-4, 1 for 5-9, 2 for 10-14 etc.
133
            if (!isset($data[$distributiongroup])) {
134
                $data[$distributiongroup] = 1;
135
            } else {
136
                $data[$distributiongroup]++;
137
            }
138
            if ($distributiongroup > $max) {
139
                $max = $distributiongroup;
140
            }
141
        }
142
 
143
        $rs->close();
144
 
145
        for ($i = 0; $i <= $max; $i++) {
146
            if (!isset($data[$i])) {
147
                $data[$i] = 0;
148
            }
149
        }
150
        ksort($data);
151
 
152
        foreach ($data as $distributiongroup => $courses) {
153
            $distributiongroupname = sprintf("%d-%d", $distributiongroup * 5, $distributiongroup * 5 + 4);
154
            $this->data['sizes'][] = array(
155
                'course_size' => $distributiongroupname,
156
                'courses' => $courses,
157
            );
158
        }
159
    }
160
}