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 the number of users from each country
27
 */
28
class report_overviewstats_chart_countries extends report_overviewstats_chart {
29
 
30
    /**
31
     * @return array
32
     */
33
    public function get_content() {
34
 
35
        $this->prepare_data();
36
 
37
        $title = get_string('chart-countries', 'report_overviewstats');
38
        $info = html_writer::div(get_string('chart-countries-info', 'report_overviewstats', count($this->data)), 'chartinfo');
39
        $chart = html_writer::tag('div', '', array(
40
            'id' => 'chart_countries',
41
            'class' => 'chartplaceholder',
42
            'style' => 'min-height: '.max(66, (count($this->data) * 20)).'px;'
43
        ));
44
 
45
        return array($title => $info . $chart);
46
    }
47
 
48
    /**
49
     * @see parent
50
     */
51
    public function inject_page_requirements(moodle_page $page) {
52
 
53
        $this->prepare_data();
54
 
55
        $page->requires->yui_module(
56
            'moodle-report_overviewstats-charts',
57
            'M.report_overviewstats.charts.countries.init',
58
            array($this->data)
59
        );
60
    }
61
 
62
    /**
63
     * Prepares data to report.
64
     */
65
    protected function prepare_data() {
66
        global $DB;
67
 
68
        if (!is_null($this->data)) {
69
            return;
70
        }
71
 
72
        $sql = "SELECT country, COUNT(*)
73
                  FROM {user}
74
                 WHERE country IS NOT NULL AND country <> '' AND deleted = 0 AND confirmed = 1
75
              GROUP BY country
76
              ORDER BY COUNT(*) DESC, country ASC";
77
 
78
        $data = array();
79
        foreach ($DB->get_records_sql_menu($sql) as $country => $count) {
80
            if (get_string_manager()->string_exists($country, 'core_countries')) {
81
                $countryname = get_string($country, 'core_countries');
82
            } else {
83
                $countryname = $country;
84
            }
85
            $data[] = array(
86
                'country' => $countryname,
87
                'count' => $count
88
            );
89
        }
90
 
91
        $this->data = $data;
92
    }
93
}