-
Notifications
You must be signed in to change notification settings - Fork 1
/
AssessmentController.cs
297 lines (248 loc) · 12.7 KB
/
AssessmentController.cs
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using KaskKiosk.AESApplicationServiceRef;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
namespace KaskKiosk.Controllers
{
public class AssessmentsController : Controller
{
//
// GET: /Assessments/
[Authorize(Roles = "Administrator, HiringManager, StoreManager")]
public async Task<ActionResult> Index()
{
var assessments = await ServerResponse<List<AssessmentDAO>>.GetResponseAsync(ServiceURIs.ServiceAssessmentUri);
ViewBag.assessments = assessments;
return View();
}
//
// GET: /Assessments/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Assessments/Create
[AllowAnonymous]
public async Task<ActionResult> Create(int id)
{
// TODO: in the future, use more specific API calls for these queries... or LINQ or something for this section
// get skill information for questions
// via job opening id in JobRequirement table
List<JobRequirementDAO> jobRequirements = await ServerResponse<List<JobRequirementDAO>>.GetResponseAsync(ServiceURIs.ServiceJobRequirementUri);
// find related skill ids
List<int> relatedSkillIds = new List<int>();
foreach (JobRequirementDAO requirementItem in jobRequirements)
{
if (requirementItem.JobOpeningID.Equals(id))
relatedSkillIds.Add(requirementItem.SkillID);
}
// then find related skill-questionbank items
List<SkillQuestionBankDAO> skillQuestionBanks = await ServerResponse<List<SkillQuestionBankDAO>>.GetResponseAsync(ServiceURIs.ServiceSkillQuestionBankUri);
List<int> relatedQuestionBankIds = new List<int>();
foreach (SkillQuestionBankDAO skillQuestionBankItem in skillQuestionBanks)
{
if (relatedSkillIds.Contains(skillQuestionBankItem.SkillID))
relatedQuestionBankIds.Add(skillQuestionBankItem.QuestionBankID);
}
// then find related mc question banks
List<QuestionBankDAO> questionBanks = await ServerResponse<List<QuestionBankDAO>>.GetResponseAsync(ServiceURIs.ServiceQuestionBankUri);
HashSet<int> relatedMcQuestionIds = new HashSet<int>();
// idea for this:
// QuestionBankID -> QuestionID -> OptionID
Dictionary<int, Dictionary<int, HashSet<int>>> relatedMcOptionsForMcQuestions = new Dictionary<int, Dictionary<int, HashSet<int>>>();
foreach (QuestionBankDAO questionBankItem in questionBanks)
{
if (relatedQuestionBankIds.Contains(questionBankItem.QuestionBankID))
{
// get related question ids
relatedMcQuestionIds.Add(questionBankItem.MCQuestionID);
// get related question options for each question id
if (relatedMcOptionsForMcQuestions.ContainsKey(questionBankItem.MCQuestionID))
{
// key already exists, so just append
relatedMcOptionsForMcQuestions[questionBankItem.QuestionBankID][questionBankItem.MCQuestionID].Add(questionBankItem.MCOptionID);
}
else
{
// create new key, then append
relatedMcOptionsForMcQuestions.Add(questionBankItem.QuestionBankID, new Dictionary<int, HashSet<int>>());
relatedMcOptionsForMcQuestions[questionBankItem.QuestionBankID].Add(questionBankItem.MCQuestionID, new HashSet<int> { questionBankItem.MCOptionID });
}
}
}
// for now, get a list of all mc questions (to use in the view)
List<MCQuestionDAO> allMcQuestions = await ServerResponse<List<MCQuestionDAO>>.GetResponseAsync(ServiceURIs.ServiceMCQuestionUri);
// for now, get a list of all mc options (to use in the view)
List<MCOptionDAO> allMcOptions = await ServerResponse<List<MCOptionDAO>>.GetResponseAsync(ServiceURIs.ServiceMCOptionUri);
// find applicant id from the previously submitted Application form
var applieds = await ServerResponse<List<AppliedDAO>>.GetResponseAsync(ServiceURIs.ServiceAppliedUri);
int applicantID = applieds.Last().ApplicantID;
ViewBag.baseURL = Url.Content("~/");
ViewBag.allMcQuestions = allMcQuestions;
ViewBag.allMcOptions = allMcOptions;
ViewBag.relatedMcQuestionIds = relatedMcQuestionIds;
ViewBag.relatedMcOptionsForMcQuestions = relatedMcOptionsForMcQuestions;
ViewBag.jobOpeningIDReferenceNumber = id;
ViewBag.applicantID = applicantID;
return View();
}
//
// POST: /Assessments/Create
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> Create(FormCollection collection)
{
try
{
if (Request.Form["acknowledgeAccurateDataCheckbox"] != null)
{
// save application form data back to database through service
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("http://localhost:51309");
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage result = new HttpResponseMessage();
string resultContent = "";
// find applicant id from the previously submitted Application form, if available
int applicantID = 0;
if (Convert.ToInt32(Request.Form["applicantID"]) > 0)
{
applicantID = Convert.ToInt32(Request.Form["applicantID"]);
}
// otherwise, get last applicant id from database
else
{
var applieds = await ServerResponse<List<AppliedDAO>>.GetResponseAsync(ServiceURIs.ServiceAppliedUri);
applicantID = applieds.Last().ApplicantID;
}
// gather AssessmentOpening form data
AssessmentDAO assessment = new AssessmentDAO();
assessment.ApplicantID = applicantID;
// and save the mc answer chosen by applicant
// first, parse the questionBankId and optionId of checkbox chosen by user
int parsedQuestionBankId = 0;
int parsedQuestionId = 0;
int parsedOptionId = 0;
var questionBankAnswers = await ServerResponse<List<QuestionBankDAO>>.GetResponseAsync(ServiceURIs.ServiceQuestionBankUri);
// TODO: change the hardcoded "50" into some sort of variable
// (i.e. the length of all possible questionBank ids, questions, and options)
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
for (int k = 0; k < 50; k++)
{
try
{
if (Request.Form["MCOption_" + i + "_" + j + "_" + k] != null)
{
parsedQuestionBankId = i;
parsedQuestionId = j;
parsedOptionId = k;
// check to see if answer is valid or "correct"
// if not valid, redirect to "sorry" screen
foreach (QuestionBankDAO qbItem in questionBankAnswers)
{
if (qbItem.QuestionBankID.Equals(parsedQuestionBankId))
{
if (!parsedOptionId.Equals(Convert.ToInt32(qbItem.MCCorrectOption)))
{
// delete failed application
var applications = await ServerResponse<List<ApplicationDAO>>.GetResponseAsync(ServiceURIs.ServiceApplicationUri);
int applicationID = applications.Last().ApplicationID;
var resultTwo = await httpClient.DeleteAsync(ServiceURIs.ServiceApplicationUri.ToString() + "/" + applicationID.ToString());
// delete failed applicant
resultTwo = await httpClient.DeleteAsync(ServiceURIs.ServiceApplicantUri.ToString() + "/" + applicantID.ToString());
// delete failed applied
resultTwo = await httpClient.DeleteAsync(ServiceURIs.ServiceAppliedUri.ToString() + "/" + applicantID.ToString());
return RedirectToAction("Sorry");
}
else
{
// this is the answer that the user chose for this particular QuestionBank question
assessment.QuestionBankID = parsedQuestionBankId;
// post (save) AssessmentOpening data
result = httpClient.PostAsJsonAsync(ServiceURIs.ServiceAssessmentUri, assessment).Result;
resultContent = result.Content.ReadAsStringAsync().Result;
}
}
}
}
}
catch { }
}
}
}
}
// redirect to phone interview questions, if possible
return RedirectToAction("Create", "Interviews", new { ID = Convert.ToInt32(Request.Form["JobOpeningIDReferenceNumberOnAssessment"]) });
}
else
{
// TODO: validation later on...
return RedirectToAction("Create");
}
}
catch
{
// TODO: validation later on...
return RedirectToAction("Create");
}
}
//
// GET: /Assessments/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /Assessments/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Assessments/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /Assessments/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Assessments/Sorry
[AllowAnonymous]
public ActionResult Sorry()
{
return View();
}
}
}