PageRenderTime 25ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/app/Http/Controllers/API/AssessmentController.php

https://gitlab.com/besingamkb/rtwc
PHP | 300 lines | 183 code | 42 blank | 75 comment | 11 complexity | 9fd451b48561db6a21aacbd57d81652e MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\APIController;
  4. use App\Http\Requests\Assessment\Employer\SubmitRequest;
  5. use App\Http\Requests\Assessment\SearchRequest;
  6. use App\Models\Assessment;
  7. use App\Models\Export\AssessmentExport;
  8. class AssessmentController extends APIController
  9. {
  10. /**
  11. * Return all assessments.
  12. *
  13. * @return \Illuminate\Http\JsonResponse
  14. */
  15. public function index()
  16. {
  17. // Get this authorised accounts assessments.
  18. $assessments = $this->authAccount
  19. ->assessments()
  20. ->with([
  21. 'account'
  22. ])
  23. ->get()
  24. ->toArray();
  25. return response()->json([
  26. 'data' => $assessments
  27. ]);
  28. }
  29. /**
  30. * Return an assessment by Key.
  31. *
  32. * @param $key
  33. * @return \Illuminate\Http\JsonResponse
  34. */
  35. public function getByKey($key)
  36. {
  37. $assessment = new Assessment();
  38. $assessment = $assessment->where('key', $key)->first();
  39. if (!$assessment) {
  40. return response()->json([
  41. 'errors' => [
  42. 'Assessment not found'
  43. ]
  44. ], 404);
  45. }
  46. return $this->show($assessment->id);
  47. }
  48. /**
  49. * Return an assessment by ID.
  50. *
  51. * @param $id
  52. * @return \Illuminate\Http\JsonResponse
  53. */
  54. public function show($id)
  55. {
  56. \DB::enableQueryLog();
  57. $assessments = $this->authAccount
  58. ->assessments()
  59. ->with('account')
  60. ->get();
  61. $assessments = $assessments->whereLoose('id', $id)->first();
  62. return response()->json([
  63. 'data' => $assessments
  64. ]);
  65. }
  66. /**
  67. * Search, filter and sort assessments.
  68. *
  69. * @param SearchRequest $request
  70. * @return \Illuminate\Http\JsonResponse
  71. */
  72. public function search(SearchRequest $request, $reporting = false)
  73. {
  74. $connectedAssessmentIDs = $this->authAccount->assessments->pluck('id');
  75. $assessments = new Assessment();
  76. $query = $assessments->whereIn('id', $connectedAssessmentIDs);
  77. // include account as we show account name
  78. $query = $query->with('account');
  79. // if reporting only include completed assessments
  80. if ($reporting) {
  81. $query = $query->where('assessments.status', 10);
  82. }
  83. if ($request->filters) {
  84. $query = $assessments->createFilterQuery($request->filters, $query);
  85. }
  86. if ($request->sort) {
  87. $query = $assessments->createSortQuery($request->sort, $query);
  88. }
  89. if ($request->search != "") {
  90. $query = $assessments->createSearchQuery($request->search, $query);
  91. }
  92. $query = $query->paginate();
  93. return response()->json($query);
  94. }
  95. /**
  96. * Search, filter and sort assessments.
  97. *
  98. * @param SearchRequest $request
  99. * @return \Illuminate\Http\JsonResponse
  100. */
  101. public function searchReporting(SearchRequest $request)
  102. {
  103. return $this->search($request, true);
  104. }
  105. /**
  106. * Return assessment parts that belong to the assessment.
  107. * Should return a worker part and employer part.
  108. *
  109. * @param $id
  110. * @return \Illuminate\Http\JsonResponse
  111. */
  112. public function getAssessmentParts($id)
  113. {
  114. $assessment = new Assessment();
  115. $assessment = $assessment->findOrFail($id);
  116. $workerPart = $assessment->getWorkerPart();
  117. $employerPart = $assessment->getEmployerPart();
  118. return response()->json([
  119. 'data' => [
  120. 'worker' => $workerPart,
  121. 'employer' => $employerPart
  122. ]
  123. ]);
  124. }
  125. /**
  126. * Return an assessment with all questions and their categories correctly ordered and ready to be taken.
  127. *
  128. * Normally called when an employer needs to take the assessment.
  129. *
  130. * @param $id
  131. * @param $part
  132. * @return \Illuminate\Http\JsonResponse
  133. */
  134. public function showInSubmissionFormat($id, $part)
  135. {
  136. $assessment = new Assessment();
  137. $assessment = $assessment->findOrFail($id);
  138. switch ($part) {
  139. case 'employer':
  140. $assessment = $assessment->formatEmployerPartForSubmission();
  141. break;
  142. case 'worker':
  143. return response()->json([
  144. 'errors' => [
  145. 'This assessment cannot be taken in this way'
  146. ]
  147. ], 500);
  148. default:
  149. return response()->json([
  150. 'errors' => [
  151. 'No assessment could be found with this ID'
  152. ]
  153. ], 404);
  154. }
  155. return response()->json([
  156. 'data' => $assessment
  157. ]);
  158. }
  159. /**
  160. * Submits a test against a given assessment ID.
  161. *
  162. * @param $id
  163. * @param SubmitRequest $submitRequest
  164. * @return \Illuminate\Http\JsonResponse
  165. */
  166. public function submitAssessment($id, SubmitRequest $submitRequest)
  167. {
  168. $assessment = new Assessment();
  169. $assessment = $assessment->findOrFail($id);
  170. $submissionResult = $assessment->submitAssessment($submitRequest->all());
  171. if (!$submissionResult) {
  172. return response()->json([
  173. 'errors' => [
  174. 'Assessment could not be submitted'
  175. ]
  176. ], 500);
  177. }
  178. // Load in the results of the submission.
  179. $assessment->load([
  180. 'results',
  181. 'results.resultsEmployer'
  182. ]);
  183. return response()->json([
  184. 'data' => $assessment
  185. ]);
  186. }
  187. /**
  188. * Resubmits a test against a given assessment ID.
  189. *
  190. * @param $id
  191. * @param SubmitRequest $submitRequest
  192. * @return \Illuminate\Http\JsonResponse
  193. */
  194. public function resubmitAssessment($id, SubmitRequest $submitRequest)
  195. {
  196. $assessment = new Assessment();
  197. $assessment = $assessment->findOrFail($id);
  198. $submissionResult = $assessment->resubmitAssessment($submitRequest->all());
  199. if (!$submissionResult) {
  200. return response()->json([
  201. 'errors' => [
  202. 'Assessment could not be submitted'
  203. ]
  204. ], 500);
  205. }
  206. // Load in the results of the submission.
  207. $assessment->load([
  208. 'results',
  209. 'results.resultsEmployer'
  210. ]);
  211. return response()->json([
  212. 'data' => $assessment
  213. ]);
  214. }
  215. /**
  216. * Attempts to export an assessment from the provided ID and export type.
  217. *
  218. * @param $id
  219. * @param $type
  220. * @return \Illuminate\Http\JsonResponse
  221. */
  222. public function export($id, $type)
  223. {
  224. $assessment = new Assessment();
  225. $assessment = $assessment->findOrFail($id);
  226. $export = null;
  227. $type = str_replace('-', '_', $type);
  228. switch ($type) {
  229. case 'additional_text':
  230. $export = new AssessmentExport();
  231. $export = $export->setAssessments($assessment->id)
  232. ->setType($type)
  233. ->build();
  234. break;
  235. default:
  236. // If no type is listed above then return 404.
  237. return response()->json([
  238. 'errors' => [
  239. 'No export of ' . str_replace('-', ' ', $type) . ' type found'
  240. ]
  241. ], 404);
  242. }
  243. // If the export could not be built.
  244. if (!$export) {
  245. return response()->json([
  246. 'errors' => [
  247. 'Export failed to build correctly'
  248. ]
  249. ], 500);
  250. }
  251. // Get the export as an output steam.
  252. $export = $export->get();
  253. // Return the file.
  254. return response()->json([
  255. 'data' => $export
  256. ]);
  257. }
  258. }