This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.php
192 lines (161 loc) · 7.91 KB
/
training.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
<?php
/*
Retrieves training history for a user, given user ID
Andrew Park - 20/07/2016
*/
header('Access-Control-Allow-Origin: *');
require "config.php";
$list_courses = array();
$conn = mssql_connect($host, $username, $password)
or die("Unable to connect to mssql");
$selected = mssql_select_db($db_name, $conn)
or die("Could not select CCRS db");
if (isset($_GET["user"])) {
$history = array();
$options = 0;
$user = $_GET['user'];
if(isset($_GET['options']))
$options = $_GET['options'];
//query for temp table t
$temp_t="CREATE TABLE #T (
ID INT IDENTITY(1,1) NOT NULL,
CourseId INT,
Title NVARCHAR(100),
CompletedDate DATETIME,
Grade NVARCHAR(20),
Result NVARCHAR(50),
CourseLength float,
SortingID VARCHAR(20),
PrintCertificate BIT,
Parameters VARCHAR(300)
);";
mssql_query($temp_t);
//query for temp table t1
$temp_t1="CREATE TABLE #T1 (
ID INT IDENTITY(1,1) NOT NULL,
CourseId INT,
SortingID VARCHAR(20)
);";
mssql_query($temp_t1);
//grab neccessary table information from ccrs tables
$select_t=" SELECT c.CourseId,
Title = ltrim(c.Title),
CompletedDate = b.EndDate,
Grade = RTRIM(ISNULL(a.Grade, '')),
Result = d.sValue,
c.CourseLength,
PrintCertificate = CASE
WHEN c.RecordOfCompletion = 0 THEN 0
WHEN b.EndDate < '2011-01-01' THEN 0
WHEN Result IN ( 3, 6 ) THEN 1 ELSE 0 END,
Parameters = CASE
WHEN c.RecordOfCompletion = 0 THEN ''
ELSE CONVERT(VARCHAR(12), $user) + '|' +
CONVERT(VARCHAR(12), c.CourseId) + '|' +
CONVERT(VARCHAR(10), b.EndDate, 11 ) + '|' +
CASE WHEN ISNULL(a.Grade, '') = '' THEN d.sValue ELSE a.Grade END
END
FROM tblCourseGrading a
JOIN tblSessions b ON a.SessionID= b.SessionID
JOIN tblCourses c ON b.CourseID = c.CourseID
JOIN tblOrganizations e ON c.OrgId = e.OrgId,
tblConstants d
WHERE a.UserID = $user
AND d.nType = 1
AND d.nKey = a.Result
UNION
SELECT b.CourseId,
ltrim(b.Title),
CompletionDate,
Result,
'',
0.0,
PrintCertificate = CASE
WHEN b.RecordOfCompletion = 0 THEN 0
WHEN CompletionDate < '2011-01-01' THEN 0
ELSE [dbo].[udfPrintCertificate] ( b.CourseId, CompletionDate, Result ) END,
Parameters = CASE
WHEN b.RecordOfCompletion = 0 THEN ''
ELSE CONVERT(VARCHAR(12), $user) + '|' +
CONVERT(VARCHAR(12), b.CourseId) + '|' +
CONVERT(VARCHAR(10), a.CompletionDate, 11 ) + '|' +
Result
END
FROM tblOnlineCourseResults a
JOIN tblCourses b ON a.CourseId = b.CourseId
JOIN tblOrganizations c ON b.OrgId = c.OrgId
WHERE a.UserId = $user
ORDER BY CompletedDate DESC";
$rs = mssql_query($select_t);
while($row = mssql_fetch_array($rs)){
//insert into t temp table for later use
$insert_t="INSERT INTO #T (CourseId, Title, CompletedDate, Grade, Result, CourseLength, SortingID, PrintCertificate, Parameters) VALUES (".$row['CourseId'].", \"".$row['Title']."\", '".$row['CompletedDate']."', '".$row['Grade']."', '".$row['Result']."', ".$row['CourseLength'].", '',".$row['PrintCertificate'].", '".$row['Parameters']."')";
mssql_query($insert_t);
}
mssql_free_result($rs);
//get information for t1 table
$select_t1 = "SELECT CourseId, CAST(MIN(ID) AS VARCHAR(20)) FROM #T GROUP BY CourseId";
$rs_t1 = mssql_query($select_t1);
while($row = mssql_fetch_array($rs_t1)){
//insert into t1 table. SortingID is min of id group by course id
$insert_t1 = "INSERT #T1 (CourseId, SortingID) VALUES (".$row['CourseId'].", ".$row['computed'].")";
mssql_query($insert_t1);
}
mssql_free_result($rs_t1);
//update SortingID for table t
$update = "UPDATE a
SET SortingID = b.SortingID
FROM #T a
JOIN #T1 b ON a.CourseId = b.CourseId";
mssql_query($update);
//mark any grades from the same course
$update = "UPDATE a
SET SortingID = a.SortingID + '.1'
FROM #T a
LEFT JOIN #T1 b ON a.ID = CAST( b.SortingID AS INT ) WHERE b.ID IS NULL";
mssql_query($update);
$f_query = '';
//get completed information ordered by date or title
if($options==0){
$f_query = "SELECT CourseId,
Title,
CONVERT(VARCHAR(10), CompletedDate, 11) as CompletedDate,
Grade,
Result,
CourseLength = round(CourseLength, 2),
SortingId = 'R'+ SortingId,
SortOrder = CASE WHEN RIGHT(SortingId, 2) <> '.1' then SortingId + '.2' ELSE SortingId END,
PrintCertificate = case PrintCertificate when 1 then '' else 'DISPLAY: none' end,
Parameters
FROM #T
ORDER BY CompletedDate DESC";
}else{
$f_query = "SELECT CourseId,
Title,
CONVERT(VARCHAR(10), CompletedDate, 11) as CompletedDate,
Grade,
Result,
CourseLength = round(CourseLength, 2),
SortingId = 'R'+ SortingId,
PrintCertificate = case PrintCertificate when 1 then '' else 'DISPLAY: none' end,
Parameters
FROM #T
ORDER BY Title ASC";
}
$final_rs = mssql_query($f_query);
while($row = mssql_fetch_assoc($final_rs)){
array_push($history,$row);
}
mssql_free_result($final_rs);
$cert_pass = mssql_query("SELECT CourseID, PassingGrade FROM tblOnlineCourses");
$passing_grade = array();
while($row = mssql_fetch_array($cert_pass)){
$passing_grade[$row["CourseID"]] = $row["PassingGrade"];
}
mssql_free_result($cert_pass);
mssql_close($db_handle);
echo json_encode($history);
} else {
echo "No user supplied";
}
?>