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
 *
19
 * @package   report
20
 * @subpackage coursestats
21
 * @copyright 2017 Paulo Jr.
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
require(dirname(__FILE__).'/../../config.php');
26
require_once($CFG->libdir.'/adminlib.php');
27
require(__DIR__. '/constants.php');
28
 
29
function get_amount_created_courses($category) {
30
	global $DB;
31
 
32
	if ($category == ALL_CATEGORIES) {
33
		return ($DB->count_records(COURSE_TABLE_NAME,
34
			array('visible'=>'1')) - 1);
35
	} else {
36
		return $DB->count_records(COURSE_TABLE_NAME,
37
			array('visible'=>'1', 'category'=>$category));
38
	}
39
}
40
function get_amount_used_courses($category) {
41
	global $DB;
42
 
43
	if ($category == ALL_CATEGORIES) {
44
		return $DB->count_records_sql('SELECT COUNT(*) FROM {report_coursestats} cs JOIN {course} co ON co.id = cs.courseid WHERE co.visible = :visible',
45
			array('visible'=>'1'));
46
	} else {
47
		return $DB->count_records_sql('SELECT COUNT(*) FROM {report_coursestats} cs JOIN {course} co ON co.id = cs.courseid WHERE co.category = :cat AND co.visible = :visible',
48
			array('cat'=>$category, 'visible'=>'1'));
49
	}
50
}
51
 
52
header('Content-Type: application/excel');
53
header('Content-Disposition: attachment; filename="sample.csv"');
54
 
55
$fp = fopen('php://output', 'w');
56
 
57
$head = array(get_string('lb_category', 'report_coursestats'), get_string('lb_courses_created_amount', 'report_coursestats'),
58
	get_string('lb_used_courses', 'report_coursestats'), get_string('lb_percent_of_used_courses', 'report_coursestats'));
59
 
60
fputcsv($fp, $head);
61
 
62
$result = $DB->get_records(COURSE_CATEGORIES_TABLE_NAME, null, 'name');
63
 
64
// All categories
65
$co_created = get_amount_created_courses(ALL_CATEGORIES);
66
$co_used = get_amount_used_courses(ALL_CATEGORIES);
67
if ($co_created > 0) {
68
	$co_percent = number_format(($co_used / $co_created) * 100, 2);
69
} else {
70
	$co_percent = '-';
71
}
72
 
73
$catdata = array(get_string('lb_all_categories', 'report_coursestats'), $co_created, $co_used, $co_percent);
74
 
75
fputcsv($fp, $catdata);
76
 
77
// For each category
78
$cat_array = array();
79
$created_courses_array = array();
80
$used_courses_array = array();
81
$percentage_used_courses_array = array();
82
 
83
foreach ($result as $cs) {
84
	$co_created = get_amount_created_courses($cs->id);
85
	$co_used = get_amount_used_courses($cs->id);
86
 
87
    $cat_array[] = $cs->name;
88
    $created_courses_array[] = $co_created;
89
	$used_courses_array[] = $co_used;
90
 
91
    if ($co_created > 0) {
92
		$co_percent = number_format(($co_used / $co_created) * 100, 2);
93
		$percentage_used_courses_array[] = number_format(($co_used / $co_created) * 100, 2);
94
	} else {
95
		$co_percent = '0';
96
		$percentage_used_courses_array[] = 0;
97
	}
98
 
99
    $catdata = array($cs->name, $co_created, $co_used, $co_percent);
100
	fputcsv($fp, $catdata);
101
}
102
 
103
fclose($fp);