This repository has been archived by the owner on May 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryBuilderBridge.php
158 lines (144 loc) · 6.88 KB
/
queryBuilderBridge.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
<?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/>.
// Created By Josh Willcock Copyright 2014.
// Created For CLC Hierarchies System Intergration.
// This is the class you create to run the build query function.
class queryBuilder
{
public function getManagerTags($userid) {
global $DB;
$managertagarray = $DB->get_records_sql('SELECT tm.tagid as tagid, tt.category as category FROM {hierarchy_managertags} tm JOIN {hierarchy_tags} tt ON (tm.tagid = tt.id) WHERE userid = "'.$userid.'"');
$managertags = array();
foreach ($managertagarray as $managertagfromarray) {
$managertag = new stdClass();
$managertag->tagid = $managertagfromarray->tagid;
$managertag->category = $managertagfromarray->category;
$managertags[] = $managertag;
}
return $managertags;
}
// This function requires the manager's userid to create a restrictive where clause.
public function build($userids, $useridcolumn, $pre = null, $post = null, $forcedtags = null, $deleted = null) {
// Builtforcount = userid = base.userid / false auser.id.
global $DB;
// Get Manager Tags.
$managertagresult = $DB->get_records_sql('SELECT mt.tagid as tagid, tt.category as categoryid FROM {hierarchy_managertags} mt JOIN {hierarchy_tags} tt ON (tt.id = mt.tagid) WHERE mt.userid = '.$userids);
$managertags = array();
$managerCatagories = array();
foreach($managertagresult as $managertag) {
$managertags[] = $managertag->tagid;
if (!in_array($managertag->categoryid, $managerCatagories)) {
$managerCatagories[] = $managertag->categoryid;
}
}
$numberoftags = count($managertags);
$categorycount = count($managerCatagories);
if ($forcedtags != null) {
$categorycount = $categorycount+count(explode(',',$forcedtags));
}
$listoftags = implode(",", $managertags);
if (!empty($forcedtags)) {
$listoftags = $listoftags.','.$forcedtags;
}
if ($listoftags == "") {
$sql = 'SELECT * FROM {hierarchy_usertags} WHERE tagid = 0';
} else {
if ($deleted == true) {
$sql = 'SELECT tu.userid as userid FROM {hierarchy_usertags} tu JOIN {user} u ON (tu.userid = u.id) WHERE tu.tagid IN ('.$listoftags.') AND u.deleted<>1 AND u.suspended<>1 GROUP BY tu.userid HAVING COUNT(tu.userid) >='.$categorycount;
} else {
$sql = 'SELECT * FROM {hierarchy_usertags} WHERE tagid IN ('.$listoftags.') GROUP BY userid HAVING COUNT(userid) >='.$categorycount;
}
}
// If admin .
$admins = get_admins();
$isadmin = false;
foreach ($admins as $admin) {
if ($userids == $admin->id) {
$isadmin = true; break;
}
}
if ($isadmin) {
$sql = 'SELECT id as userid FROM {user} ORDER BY firstname';
}
$usersOfManagers = $DB->get_records_sql($sql);
//Loop Through Available Requested Tags
if (!empty($usersOfManagers)) {
//Create SQL Query
//$query = 'AND ';
$query = $pre;
$query = $query.'(';
$and = 0;
foreach($usersOfManagers as $userOfManager) {
if ( $and == 0 ) { $and = 1; } else { $query = $query.' OR '; }
if (!empty($userOfManager)) {
$query = $query.$useridcolumn.' = '.$userOfManager->userid;
}
}
$query = $query.')'.$post;
//echo '<pre> Total users: '.count($usersOfManagers).'</pre>';
} else {
$query = $pre.'(0 = 1)'.$post;
}
return $query;
}
public function getUserArray($userids, $forcedtags = null, $deleted = null)
{
//builtforcount = userid = base.userid / false auser.id
global $DB;
//Get Manager Tags
$managertagresult = $DB->get_records_sql('SELECT mt.tagid as tagid, tt.category as categoryid FROM {hierarchy_managertags} mt JOIN {hierarchy_tags} tt ON (tt.id = mt.tagid) WHERE mt.userid = '.$userids);
$managertags = array();
$managerCatagories = array();
foreach($managertagresult as $managertag) {
$managertags[] = $managertag->tagid;
if (!in_array($managertag->categoryid, $managerCatagories)) {
$managerCatagories[] = $managertag->categoryid;
}
}
$numberoftags = count($managertags);
$categorycount = count($managerCatagories);
if ($forcedtags != null) {
}
$listoftags = implode(",",$managertags);
if (!empty($forcedtags)) {
$listoftags = $listoftags.','.$forcedtags;
$countForcedTags = count(explode(',',$forcedtags));
$categorycount = $categorycount+count(explode(',',$forcedtags));
}
if (empty($listoftags)) {
$listoftags = '0';
}
// $sql = 'SELECT * FROM {hierarchy_usertags} WHERE tagid IN ('.$listoftags.') GROUP BY userid HAVING COUNT(userid) > = '.$numberoftags;
// if admin
$admins = get_admins(); $isadmin = false; foreach ($admins as $admin) { if ($userids == $admin->id) { $isadmin = true; break; } }
if ($isadmin) {
if ($deleted) {
$sql = 'SELECT id as userid FROM {user} WHERE deleted<>1 and suspended<>1 ORDER BY firstname ';
} else {
$sql = 'SELECT id as userid FROM {user} ORDER BY firstname';
}
} else {
if ($deleted) {
$sql = 'SELECT * FROM {hierarchy_usertags} tu JOIN {user} mu ON (mu.id = tu.userid) WHERE tu.tagid IN ('.$listoftags.') AND mu.deleted<>1 and mu.suspended<>1 GROUP BY tu.userid HAVING COUNT(tu.userid) >='.$categorycount.' ORDER BY mu.firstname';
} else {
$sql = 'SELECT * FROM {hierarchy_usertags} tu JOIN {user} mu ON (mu.id = tu.userid) WHERE tu.tagid IN ('.$listoftags.') GROUP BY tu.userid HAVING COUNT(tu.userid) >='.$categorycount.' ORDER BY mu.firstname';
}
}
$usersOfManagers = $DB->get_records_sql($sql);
//Loop Through Available Requested Tags
return $usersOfManagers;
}
}