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 |
/**
|
|
|
26 |
* COHORT FILTER
|
|
|
27 |
* A filter for configurable reports
|
|
|
28 |
* @author: François Parlant <https://www.linkedin.com/in/francois-parlant/>
|
|
|
29 |
* @date: 2020
|
|
|
30 |
*/
|
|
|
31 |
|
|
|
32 |
/* example of report query
|
|
|
33 |
***********
|
|
|
34 |
* Display the students from a cohort and all the courses they are enrolled in
|
|
|
35 |
***********
|
|
|
36 |
SELECT
|
|
|
37 |
u.firstname AS Firstname,
|
|
|
38 |
u.lastname AS Lastname,
|
|
|
39 |
u.email AS Email,
|
|
|
40 |
c.fullname AS Course
|
|
|
41 |
|
|
|
42 |
FROM prefix_course AS c
|
|
|
43 |
JOIN prefix_enrol AS en ON en.courseid = c.id
|
|
|
44 |
JOIN prefix_user_enrolments AS ue ON ue.enrolid = en.id
|
|
|
45 |
JOIN prefix_user AS u ON ue.userid = u.id
|
|
|
46 |
WHERE u.id in (SELECT u.id
|
|
|
47 |
FROM prefix_cohort AS h
|
|
|
48 |
JOIN prefix_cohort_members AS hm ON h.id = hm.cohortid
|
|
|
49 |
JOIN prefix_user AS u ON hm.userid = u.id
|
|
|
50 |
WHERE 1=1
|
|
|
51 |
%%FILTER_COHORTS:h.id%%
|
|
|
52 |
ORDER BY u.firstname)
|
|
|
53 |
|
|
|
54 |
*/
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
require_once($CFG->dirroot.'/blocks/configurable_reports/plugin.class.php');
|
|
|
58 |
|
|
|
59 |
class plugin_cohorts extends plugin_base{
|
|
|
60 |
|
|
|
61 |
public function init() {
|
|
|
62 |
$this->form = false;
|
|
|
63 |
$this->unique = true;
|
|
|
64 |
$this->fullname = get_string('filtercohorts', 'block_configurable_reports');
|
|
|
65 |
$this->reporttypes = array('courses', 'sql');
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public function summary($data) {
|
|
|
69 |
return get_string('filtercohorts_summary', 'block_configurable_reports');
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public function execute($finalelements, $data) {
|
|
|
73 |
|
|
|
74 |
$filtercohorts = optional_param('filter_cohorts', 0, PARAM_INT);
|
|
|
75 |
if (!$filtercohorts) {
|
|
|
76 |
return $finalelements;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
if ($this->report->type != 'sql') {
|
|
|
80 |
return array($filtercohorts);
|
|
|
81 |
} else {
|
|
|
82 |
if (preg_match("/%%FILTER_COHORTS:([^%]+)%%/i", $finalelements, $output)) {
|
|
|
83 |
$replace = ' AND '.$output[1].' = '.$filtercohorts;
|
|
|
84 |
return str_replace('%%FILTER_COHORTS:'.$output[1].'%%', $replace, $finalelements);
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
return $finalelements;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
public function print_filter(&$mform) {
|
|
|
91 |
global $remotedb, $COURSE, $PAGE, $CFG;
|
|
|
92 |
|
|
|
93 |
$reportclassname = 'report_'.$this->report->type;
|
|
|
94 |
$reportclass = new $reportclassname($this->report);
|
|
|
95 |
|
|
|
96 |
if ($this->report->type != 'sql') {
|
|
|
97 |
$components = cr_unserialize($this->report->components);
|
|
|
98 |
$conditions = $components['conditions'];
|
|
|
99 |
|
|
|
100 |
$cohortslist = $reportclass->elements_by_conditions($conditions);
|
|
|
101 |
} else {
|
|
|
102 |
$sql = 'SELECT h.id, h.name
|
|
|
103 |
FROM {cohort} h
|
|
|
104 |
';
|
|
|
105 |
$studentlist = $remotedb->get_records_sql($sql);
|
|
|
106 |
foreach ($studentlist as $student) {
|
|
|
107 |
$cohortslist[] = $student->userid;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
$cohortsoptions = array();
|
|
|
113 |
$cohortsoptions[0] = get_string('filter_all', 'block_configurable_reports');
|
|
|
114 |
|
|
|
115 |
if (!empty($cohortslist)) {
|
|
|
116 |
|
|
|
117 |
$cohorts = $remotedb->get_records_sql($sql);
|
|
|
118 |
|
|
|
119 |
foreach ($cohorts as $c) {
|
|
|
120 |
$cohortsoptions[$c->id] = $c->name;
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
$elestr = get_string('cohorts', 'block_configurable_reports');
|
|
|
125 |
$mform->addElement('select', 'filter_cohorts', $elestr, $cohortsoptions);
|
|
|
126 |
$mform->setType('filter_cohorts', PARAM_INT);
|
|
|
127 |
}
|
|
|
128 |
}
|