forked from GibbonEdu/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_fastFinder_ajax.php
217 lines (189 loc) · 12 KB
/
index_fastFinder_ajax.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
<?php
/*
Gibbon, Flexible & Open School System
Copyright (C) 2010, Ross Parker
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
//Gibbon system-wide includes
include './gibbon.php';
$themeName = $gibbon->session->get('gibbonThemeName') ?? 'Default';
if (!isset($_SESSION[$guid]) or !$gibbon->session->exists('gibbonPersonID')) {
die( __('Your request failed because you do not have access to this action.') );
} else {
$searchTerm = $_REQUEST['q'] ?? '';
// Allow for * as wildcard (as well as %)
$searchTerm = str_replace('*', '%', $searchTerm);
// Cancel out early for empty searches
if (empty($searchTerm)) die('[]');
// Check access levels
$studentIsAccessible = isActionAccessible($guid, $connection2, '/modules/students/student_view.php');
$highestActionStudent = getHighestGroupedAction($guid, '/modules/students/student_view.php', $connection2);
$staffIsAccessible = isActionAccessible($guid, $connection2, '/modules/Staff/staff_view.php');
$classIsAccessible = false;
$alarmIsAccessible = isActionAccessible($guid, $connection2, '/modules/System Admin/alarm.php');
$highestActionClass = getHighestGroupedAction($guid, '/modules/Planner/planner.php', $connection2);
if (isActionAccessible($guid, $connection2, '/modules/Planner/planner.php') and $highestActionClass != 'Lesson Planner_viewMyChildrensClasses') {
$classIsAccessible = true;
}
$resultSet = array();
$resultError = '[{"id":"","name":"Database Error"}]';
// ACTIONS
// Grab the cached set of translated actions from the session
$actions = $gibbon->session->get('fastFinderActions');
if (empty($actions)) {
$actions = $gibbon->session->cacheFastFinderActions($gibbon->session->get('gibbonRoleIDCurrent'));
$actions[] = array('');
}
if (!empty($actions) && is_array($actions)) {
foreach ($actions as $action) {
// Add actions that match the search query to the result set
if (stristr($action['name'], $searchTerm) !== false) {
$resultSet['Action'][] = $action;
}
// Handle the special Lockdown case
if ($alarmIsAccessible) {
if (stristr('Lockdown', $searchTerm) !== false && $action['name'] == 'Sound Alarm') {
$action['name'] = 'Lockdown';
$resultSet['Action'][] = $action;
}
}
}
}
// CLASSES
if ($classIsAccessible) {
try {
if ($highestActionClass == 'Lesson Planner_viewEditAllClasses' or $highestActionClass == 'Lesson Planner_viewAllEditMyClasses') {
$data = array( 'search' => '%'.$searchTerm.'%', 'gibbonSchoolYearID2' => $gibbon->session->get('gibbonSchoolYearID') );
$sql = "SELECT gibbonCourseClass.gibbonCourseClassID AS id, CONCAT(gibbonCourse.nameShort, '.', gibbonCourseClass.nameShort) AS name, NULL as type
FROM gibbonCourseClass
JOIN gibbonCourse ON (gibbonCourseClass.gibbonCourseID=gibbonCourse.gibbonCourseID)
WHERE gibbonCourse.gibbonSchoolYearID=:gibbonSchoolYearID2
AND (gibbonCourse.name LIKE :search OR CONCAT(gibbonCourse.nameShort, '.', gibbonCourseClass.nameShort) LIKE :search)
ORDER BY name";
} else {
$data = array('search' => '%'.$searchTerm.'%', 'gibbonSchoolYearID3' => $gibbon->session->get('gibbonSchoolYearID'), 'gibbonPersonID' => $gibbon->session->get('gibbonPersonID') );
$sql = "SELECT gibbonCourseClass.gibbonCourseClassID AS id, CONCAT(gibbonCourse.nameShort, '.', gibbonCourseClass.nameShort) AS name, NULL as type
FROM gibbonCourseClassPerson
JOIN gibbonCourseClass ON (gibbonCourseClassPerson.gibbonCourseClassID=gibbonCourseClass.gibbonCourseClassID)
JOIN gibbonCourse ON (gibbonCourseClass.gibbonCourseID=gibbonCourse.gibbonCourseID)
WHERE gibbonCourse.gibbonSchoolYearID=:gibbonSchoolYearID3
AND gibbonPersonID=:gibbonPersonID
AND (gibbonCourse.name LIKE :search OR CONCAT(gibbonCourse.nameShort, '.', gibbonCourseClass.nameShort) LIKE :search)
ORDER BY name";
}
$resultList = $connection2->prepare($sql);
$resultList->execute($data);
} catch (PDOException $e) { die($resultError); }
if ($resultList->rowCount() > 0) $resultSet['Class'] = $resultList->fetchAll();
}
// STAFF
if ($staffIsAccessible == true) {
try {
$data = array('search' => '%'.$searchTerm.'%', 'today' => date('Y-m-d') );
$sql = "SELECT gibbonPerson.gibbonPersonID AS id,
(CASE WHEN gibbonPerson.username LIKE :search
THEN concat(surname, ', ', preferredName, ' (', gibbonPerson.username, ')')
ELSE concat(surname, ', ', preferredName) END) AS name,
NULL as type
FROM gibbonPerson
JOIN gibbonStaff ON (gibbonStaff.gibbonPersonID=gibbonPerson.gibbonPersonID)
WHERE status='Full'
AND (dateStart IS NULL OR dateStart<=:today)
AND (dateEnd IS NULL OR dateEnd>=:today)
AND (gibbonPerson.surname LIKE :search
OR gibbonPerson.preferredName LIKE :search
OR gibbonPerson.username LIKE :search)
ORDER BY name";
$resultList = $connection2->prepare($sql);
$resultList->execute($data);
} catch (PDOException $e) { die($resultError); }
if ($resultList->rowCount() > 0) $resultSet['Staff'] = $resultList->fetchAll();
}
// STUDENTS
if ($studentIsAccessible == true) {
$data = array('search' => '%'.$searchTerm.'%', 'gibbonSchoolYearID' => $gibbon->session->get('gibbonSchoolYearID'), 'today' => date('Y-m-d') );
// Allow parents to search students in any family they belong to
if ($highestActionStudent == 'View Student Profile_myChildren') {
$data['gibbonPersonID'] = $gibbon->session->get('gibbonPersonID');
$sql = "SELECT gibbonPerson.gibbonPersonID AS id,
(CASE WHEN gibbonPerson.username LIKE :search THEN concat(surname, ', ', preferredName, ' (', gibbonRollGroup.name, ', ', gibbonPerson.username, ')')
WHEN gibbonPerson.studentID LIKE :search THEN concat(surname, ', ', preferredName, ' (', gibbonRollGroup.name, ', ', gibbonPerson.studentID, ')')
WHEN gibbonPerson.firstName LIKE :search AND firstName<>preferredName THEN concat(surname, ', ', firstName, ' \"', preferredName, '\" (', gibbonRollGroup.name, ')' )
ELSE concat(surname, ', ', preferredName, ' (', gibbonRollGroup.name, ')') END) AS name,
NULL as type
FROM gibbonPerson, gibbonStudentEnrolment, gibbonRollGroup, gibbonFamilyChild, gibbonFamilyAdult
WHERE gibbonPerson.gibbonPersonID=gibbonStudentEnrolment.gibbonPersonID
AND gibbonStudentEnrolment.gibbonRollGroupID=gibbonRollGroup.gibbonRollGroupID
AND gibbonFamilyAdult.gibbonPersonID=:gibbonPersonID
AND gibbonFamilyChild.gibbonPersonID=gibbonPerson.gibbonPersonID
AND gibbonFamilyChild.gibbonFamilyID=gibbonFamilyAdult.gibbonFamilyID";
}
// Allow individuals to only search themselves
else if ($highestActionStudent == 'View Student Profile_my') {
$data['gibbonPersonID'] = $gibbon->session->get('gibbonPersonID');
$sql = "SELECT gibbonPerson.gibbonPersonID AS id,
(CASE WHEN gibbonPerson.username LIKE :search THEN concat(surname, ', ', preferredName, ' (', gibbonRollGroup.name, ', ', gibbonPerson.username, ')')
WHEN gibbonPerson.studentID LIKE :search THEN concat(surname, ', ', preferredName, ' (', gibbonRollGroup.name, ', ', gibbonPerson.studentID, ')')
WHEN gibbonPerson.firstName LIKE :search AND firstName<>preferredName THEN concat(surname, ', ', firstName, ' \"', preferredName, '\" (', gibbonRollGroup.name, ')' )
ELSE concat(surname, ', ', preferredName, ' (', gibbonRollGroup.name, ')') END) AS name,
NULL as type
FROM gibbonPerson, gibbonStudentEnrolment, gibbonRollGroup
WHERE gibbonPerson.gibbonPersonID=gibbonStudentEnrolment.gibbonPersonID
AND gibbonStudentEnrolment.gibbonRollGroupID=gibbonRollGroup.gibbonRollGroupID
AND gibbonPerson.gibbonPersonID=:gibbonPersonID";
}
// Allow searching of all students
else {
$sql = "SELECT gibbonPerson.gibbonPersonID AS id,
(CASE WHEN gibbonPerson.username LIKE :search THEN concat(surname, ', ', preferredName, ' (', gibbonRollGroup.name, ', ', gibbonPerson.username, ')')
WHEN gibbonPerson.studentID LIKE :search THEN concat(surname, ', ', preferredName, ' (', gibbonRollGroup.name, ', ', gibbonPerson.studentID, ')')
WHEN gibbonPerson.firstName LIKE :search AND firstName<>preferredName THEN concat(surname, ', ', firstName, ' \"', preferredName, '\" (', gibbonRollGroup.name, ')' )
ELSE concat(surname, ', ', preferredName, ' (', gibbonRollGroup.name, ')') END) AS name,
NULL as type
FROM gibbonPerson, gibbonStudentEnrolment, gibbonRollGroup
WHERE gibbonPerson.gibbonPersonID=gibbonStudentEnrolment.gibbonPersonID
AND gibbonStudentEnrolment.gibbonRollGroupID=gibbonRollGroup.gibbonRollGroupID
AND status='Full'";
}
$sql.=" AND (dateStart IS NULL OR dateStart<=:today)
AND (dateEnd IS NULL OR dateEnd>=:today)
AND gibbonRollGroup.gibbonSchoolYearID=:gibbonSchoolYearID
AND (gibbonPerson.surname LIKE :search
OR gibbonPerson.firstName LIKE :search
OR gibbonPerson.preferredName LIKE :search
OR gibbonPerson.username LIKE :search
OR gibbonPerson.studentID LIKE :search
OR gibbonRollGroup.name LIKE :search)
ORDER BY name";
try {
$resultList = $connection2->prepare($sql);
$resultList->execute($data);
} catch (PDOException $e) { die($resultError); }
if ($resultList->rowCount() > 0) $resultSet['Student'] = $resultList->fetchAll();
}
$list = '';
foreach ($resultSet as $type => $results) {
foreach ($results as $token) {
if ($token['type'] == 'Core') {
$list .= '{"id": "'.substr($type, 0, 3).'-'.$token['id'].'", "name": "'.htmlPrep(__($type)).' - '.htmlPrep(__($token['name'])).'"},';
}
else if ($token['type'] == 'Additional') {
$list .= '{"id": "'.substr($type, 0, 3).'-'.$token['id'].'", "name": "'.htmlPrep(__($type)).' - '.htmlPrep(__($token['name'], $token['module'])).'"},';
}
else {
$list .= '{"id": "'.substr($type, 0, 3).'-'.$token['id'].'", "name": "'.htmlPrep(__($type)).' - '.htmlPrep($token['name']).'"},';
}
}
}
// Output the json
echo '['.substr($list, 0, -1).']';
}