-
Notifications
You must be signed in to change notification settings - Fork 0
/
cohortdetail_form.php
242 lines (207 loc) · 7.6 KB
/
cohortdetail_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file defines the form for the cohortdetail report.
*
* @package report_cohortdetail
* @copyright 2024 DNnum UHA
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_cohortdetail;
// Include the necessary libraries.
use moodleform;
use context_system;
use context;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->dirroot . '/cohort/lib.php');
require_once($CFG->libdir.'/accesslib.php');
/**
* A class that defines the form for the cohortdetail report.
*
* It is used to search for a cohort and display the members of the system cohort.
*
* @package report_cohortdetail
* @category form
*/
class cohortdetail_form_system extends moodleform {
/**
* Method definition
* Add elements to form
* The form is used to search for a cohort and display the members of the cohort.
* @return void
*/
public function definition() {
// Get the USER global variable.
global $DB;
// Create a new form object.
$mform = $this->_form;
// Get the system context.
$context = context_system::instance();
// Get all the cohorts.
$sql = "SELECT c.id as ci, c.name as cn, c.description as cd
FROM {cohort} c
WHERE c.visible = :visible AND c.contextid = :context
ORDER BY cn";
// Execute the SQL query.
$cohorts = $DB->get_records_sql($sql, ['visible' => 1, 'context' => $context->id]);
// Check if there are cohorts.
if (empty($cohorts)) {
// Display a bootstrap alert if there are no cohorts.
$mform->addElement(
'html',
'<div class="alert alert-danger" role="alert">'
. get_string('nocohorts', 'report_cohortdetail')
. '</div>'
);
return;
}
// Create an array of cohorts with the cohort name and description for the autocomplete element.
$cohorts = array_map(fn($cohort) => $cohort->cn . ' (' . $cohort->cd . ')', $cohorts);
// Set the options for the autocomplete element.
$options = [
'class' => 'autocompletecohort',
'id' => 'cohortsystem',
];
// Add an autocomplete element to the form.
$mform->addElement('autocomplete', 'cohort', get_string('searchtitle', 'report_cohortdetail'), $cohorts, $options);
// Add a button group to the form.
$buttonarray = [];
// Add a button to search all member of the cohort in the button group.
$buttonarray[] = $mform->createElement(
'submit',
'members',
get_string('searchmember', 'report_cohortdetail'),
['id' => 'memberssystem']
);
// Add a button to get all the courses linked to the cohort in the button group.
$buttonarray[] = $mform->createElement(
'submit',
'courseslinked',
get_string('searchcourse', 'report_cohortdetail'),
['id' => 'courseslinkedsystem']
);
// Add a cancel button to the button group.
$buttonarray[] = $mform->createElement(
'cancel',
);
// Set the options for the button group.
$optionsbuttonar = [
'class' => 'buttonarcohort',
'id' => 'buttonarcohortsystem',
];
// Add the button group to the form.
$mform->addGroup($buttonarray, 'buttonarcohort', '', null, false, $optionsbuttonar);
}
}
/**
* A class that defines the form for the cohortdetail report.
*
* It is used to search for a cohort and display the members of the cohort from categories.
*
* @package report_cohortdetail
* @category form
*/
class cohortdetail_form_category extends moodleform {
/**
* Method definition
* Add elements to form
* The form is used to search for a cohort and display the members of the cohort.
* @return void
*/
public function definition() {
// Get the USER global variable.
global $DB;
// Create a new form object.
$mform = $this->_form;
// Get the system context.
$context = context_system::instance();
// Get all the cohorts.
$sql = "SELECT
c.id as ci,
c.name as cn,
c.description as cd,
ccat.name as catname
FROM
{cohort} c
JOIN
{context} ctxt
ON
c.contextid = ctxt.id
JOIN
{course_categories} ccat
ON
ctxt.instanceid = ccat.id
WHERE
c.visible = :visible
AND
c.contextid != :context
ORDER BY
cn";
// Execute the SQL query.
$cohorts = $DB->get_records_sql($sql, ['visible' => 1, 'context' => $context->id]);
// Check if there are cohorts.
if (empty($cohorts)) {
// Display a bootstrap alert if there are no cohorts.
$mform->addElement(
'html',
'<div class="alert alert-danger" role="alert">'
. get_string('nocohorts', 'report_cohortdetail')
. '</div>'
);
return;
}
// Create an array of cohorts with the cohort name and description for the autocomplete element.
$cohorts = array_map(
fn($cohort) => '[' . $cohort->catname . '] ' . $cohort->cn . ' (' . $cohort->cd . ')',
$cohorts
);
// Set the options for the autocomplete element.
$options = [
'class' => 'autocompletecohort',
'id' => 'cohortcategory',
];
// Add an autocomplete element to the form.
$mform->addElement('autocomplete', 'cohort', get_string('searchtitle', 'report_cohortdetail'), $cohorts, $options);
// Add a button group to the form.
$buttonarray = [];
// Add a button to search all member of the cohort in the button group.
$buttonarray[] = $mform->createElement(
'submit',
'members',
get_string('searchmember', 'report_cohortdetail'),
['id' => 'memberscategory']
);
// Add a button to get all the courses linked to the cohort in the button group.
$buttonarray[] = $mform->createElement(
'submit',
'courseslinked',
get_string('searchcourse', 'report_cohortdetail'),
['id' => 'courseslinkedcategory']
);
// Add a cancel button to the button group.
$buttonarray[] = $mform->createElement(
'cancel',
);
// Set the options for the button group.
$optionsbuttonar = [
'class' => 'buttonarcohort',
'id' => 'buttonarcohortcategory',
];
// Add the button group to the form.
$mform->addGroup($buttonarray, 'buttonarcohortcategory', '', null, false, $optionsbuttonar);
}
}