Ingen beskrivning

TemplatesController.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. class TemplatesController extends \BaseController
  3. {
  4. /**
  5. * List all templates, grouped by program
  6. */
  7. public function index()
  8. {
  9. $title = 'Rubric List';
  10. $global_templates = Template::whereNull('school_id')->whereNull('program_id')->get();
  11. $schools = School::with('programs.templates')
  12. ->orderBy('name')
  13. ->get();
  14. $role = Auth::user()->role;
  15. switch ($role) {
  16. case 1:
  17. $templates = Template::orderBy('name')->get();
  18. break;
  19. case 2:
  20. $templates = Template::orderBy('name')
  21. ->whereNull("school_id")
  22. ->orWhere('school_id', Auth::user()->school_id)
  23. ->get();
  24. break;
  25. case 3:
  26. $program_id = DB::table("program_user")
  27. ->where('user_id', Auth::user()->id)
  28. ->lists('program_id')[0];
  29. $school_id = DB::table('programs')
  30. ->where('id', $program_id)
  31. ->lists('school_id')[0];
  32. $templates = Template::orderBy('name')
  33. ->whereNull("school_id")
  34. ->orWhere('school_id', $school_id)
  35. ->orWhere('program_id', $program_id)
  36. ->get();
  37. break;
  38. }
  39. $templates = Template::orderBy('name')->get();
  40. return View::make('local.managers.shared.rubric_list', compact('title', 'global_templates', 'schools', 'templates', 'role'));
  41. }
  42. /*public function schoolCoordinatorIndex()
  43. {
  44. $title = 'Rubric List';
  45. $global_templates = Template::whereNull('school_id')->whereNull('program_id')->get();
  46. $schools = School::with('programs.templates')
  47. ->orderBy('name')
  48. ->get();
  49. $templates = Template::orderBy('name')->get();
  50. return View::make('local.managers.admins.rubric_list', compact('title', 'global_templates', 'schools', 'templates'));
  51. }*/
  52. public function show(Template $template)
  53. {
  54. $title = $template->name;
  55. $template->titles = DB::table('titles')
  56. ->join('template_title', 'template_title.title_id', '=', "titles.id")
  57. ->where('template_id', $template->id)
  58. ->orderBy('position', 'ASC')
  59. ->lists('text');
  60. $template->criteria = DB::table('criteria')
  61. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  62. ->where('template_id', $template->id)
  63. ->orderBy('position')
  64. ->get();
  65. foreach ($template->criteria as $criterion) {
  66. $criterion->scales = DB::table('criterion_scale')
  67. ->join('scales', 'scales.id', '=', 'criterion_scale.scale_id')
  68. ->where('criterion_id', $criterion->criterion_id)
  69. ->orderBy('position')
  70. ->lists('description');
  71. $criterion->outcomes = DB::table('criterion_objective_outcome as cobo')
  72. ->join('outcomes', 'outcomes.id', '=', 'cobo.outcome_id')
  73. ->where('cobo.criterion_id', $criterion->criterion_id)
  74. ->select('outcomes.*')
  75. ->distinct()
  76. ->get();
  77. }
  78. return View::make('local.managers.admins.view_template', compact('template', 'title'));
  79. }
  80. public function onLoadFetch()
  81. {
  82. $json_to_send = [];
  83. $template_id = Input::get('id');
  84. $json_to_send["criteria"] = DB::table("criteria")->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  85. ->where("template_criterion.template_id", '=', $template_id)
  86. ->get();
  87. Log::info($json_to_send["criteria"]);
  88. foreach ($json_to_send['criteria'] as $criteria) {
  89. $json_to_send['scales'][$criteria->criterion_id] = DB::table("scales")
  90. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  91. ->where("criterion_scale.criterion_id", '=', $criteria->criterion_id)->orderBy('position', 'ASC')->get();
  92. }
  93. return json_encode($json_to_send);
  94. }
  95. /**
  96. * Show the form for creating a new rubric
  97. *
  98. * @return Response
  99. */
  100. public function newTemplate()
  101. {
  102. $title = "Rubric Builder";
  103. $templates = Template::orderBy('name', 'ASC')->get();
  104. $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->get();
  105. $criteria = Criterion::orderBy('name', 'ASC')->get();
  106. $schools = School::orderBy('name', 'ASC')->get();
  107. $role = Auth::user()->role;
  108. $templates = NULL;
  109. $programs = NULL;
  110. // Returns templates depending on the type of user
  111. if ($role == 1) {
  112. $templates = Template::orderBy('name', 'ASC')->get();
  113. $programs = Program::orderBy('name', 'ASC')->get();
  114. } elseif ($role == 2) {
  115. $templates = Template::where('school_id', '=', Auth::user()->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  116. $programs = Auth::user()->school->programs;
  117. } elseif ($role == 3) {
  118. $templates = Template::where('school_id', '=', Auth::user()->programs[0]->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  119. $programs = Auth::user()->programs()->get();
  120. }
  121. return View::make('local.managers.shared.rubrics', compact('title', 'templates', 'outcomes', 'criteria', 'schools', 'programs'));
  122. }
  123. /**
  124. * Save a new rubric
  125. *
  126. * @return Response
  127. */
  128. public function create()
  129. {
  130. DB::beginTransaction();
  131. $template = new Template;
  132. $template->name = Input::get('name');
  133. $template->is_visible = Input::get('is_visible');
  134. $template->expected_percentage = Input::get('expected_percentage');
  135. $template->expected_points = Input::get('expected_points');
  136. // If user can set the school (that is, if school_id is not undefined)
  137. // set the school id or set to null
  138. if (is_numeric(Input::get('school_id'))) {
  139. if (Input::get('school_id') != 0)
  140. $template->school_id = Input::get('school_id');
  141. elseif (Input::get('school_id') == 0)
  142. $template->school_id = NULL;
  143. }
  144. // If user can set the program (that is, if program_id is not undefined)
  145. // set the program id or set to null
  146. if (is_numeric(Input::get('program_id'))) {
  147. if (Input::get('program_id') != 0)
  148. $template->program_id = Input::get('program_id');
  149. elseif (Input::get('program_id') == 0)
  150. $template->program_id = NULL;
  151. }
  152. // If the user is any coordinator, set the school id
  153. // If the user is a program coordinator, also set program id
  154. switch (Auth::user()->role) {
  155. case 2:
  156. $template->school_id = Auth::user()->school->id;
  157. break;
  158. case 3:
  159. $template->school_id = Auth::user()->programs[0]->school->id;
  160. $template->program_id = Auth::user()->programs[0]->id;
  161. break;
  162. }
  163. $criteria = Input::get('criteria');
  164. $max_score = Input::get('max_score');
  165. $titles = Input::get('titles');
  166. $template->num_scales = count($titles);
  167. $template->max_score = $max_score;
  168. if ($template->save()) {
  169. $templateId = $template->id;
  170. foreach ($criteria as $index => $criterion_id) {
  171. if (!(DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, '{$index}')"))) {
  172. Session::flash('status', 'danger');
  173. Session::flash('message', 'Rubric could not be created.');
  174. DB::rollback();
  175. return;
  176. }
  177. }
  178. foreach ($titles as $index => $text) {
  179. $query = DB::table('titles')
  180. ->where('text', $text)->first();
  181. if ($query) {
  182. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  183. if (!$result) {
  184. Session::flash('status', 'danger');
  185. Session::flash('message', 'Rubric could not be created.');
  186. DB::rollback();
  187. return;
  188. }
  189. } else {
  190. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  191. if (!$result1) {
  192. Session::flash('status', 'danger');
  193. Session::flash('message', 'Rubric could not be created.');
  194. DB::rollback();
  195. return;
  196. }
  197. $query = DB::table('titles')
  198. ->where('text', $text)->first();
  199. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  200. if (!$result) {
  201. Session::flash('status', 'danger');
  202. Session::flash('message', 'Rubric could not be created.');
  203. DB::rollback();
  204. return;
  205. }
  206. }
  207. }
  208. Session::flash('status', 'success');
  209. Session::flash('message', 'Rubric created. You can now select it from the list.');
  210. DB::commit();
  211. return;
  212. } else {
  213. Session::flash('status', 'danger');
  214. Session::flash('message', 'Rubric could not be created.');
  215. DB::rollback();
  216. return;
  217. }
  218. }
  219. /**
  220. * Return a specific template
  221. *
  222. * @return Template
  223. */
  224. public function fetch()
  225. {
  226. $template_info = [];
  227. $template_info['template'] = Template::find(Input::get('id'));
  228. $template_info['criterion'] = DB::table('criteria')
  229. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  230. ->where("template_criterion.template_id", '=', Input::get('id'))
  231. ->get();
  232. foreach ($template_info['criterion'] as $temp_crit) {
  233. $temp_crit->scales = DB::table('scales')
  234. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  235. ->where('criterion_scale.criterion_id', '=', $temp_crit->criterion_id)
  236. ->orderBy('position', 'ASC')
  237. ->get();
  238. $temp_crit->program_ids = json_encode(DB::table('program_criterion')
  239. ->where('criterion_id', $temp_crit->id)
  240. ->lists('program_id'));
  241. $temp_crit->objectives = DB::table('criterion_objective_outcome')
  242. ->join('objectives', 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  243. ->where('criterion_id', $temp_crit->id)
  244. ->select('objectives.*')
  245. ->distinct()
  246. ->lists('text');
  247. $outcomeID = DB::table('criterion_objective_outcome')->where('criterion_id', '=', $temp_crit->criterion_id)
  248. ->lists('outcome_id');
  249. $outcomes = DB::table('outcomes')->whereIn('id', $outcomeID)->get();
  250. $outcomeStr = '';
  251. foreach ($outcomes as $key => $outcome) {
  252. $outcomeStr .= $outcome->name . ', ';
  253. }
  254. $outcomeStr = rtrim($outcomeStr, ',');
  255. $temp_crit->outcomes = $outcomeStr;
  256. }
  257. $template_info['titles'] = DB::table('titles')
  258. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  259. ->where('template_id', Input::get('id'))
  260. ->orderBy('position')
  261. ->get();
  262. Log::info($template_info);
  263. return json_encode($template_info);
  264. }
  265. /**
  266. * Update the specified resource in storage.
  267. *
  268. * @return Response
  269. */
  270. public function update()
  271. {
  272. DB::beginTransaction();
  273. $template = Template::find(Input::get('id'));
  274. Log::info(Input::all());
  275. $template->name = Input::get('name');
  276. $template->is_visible = Input::get('is_visible');
  277. $template->expected_percentage = Input::get('expected_percentage');
  278. $template->expected_points = Input::get('expected_points');
  279. // If user can set the school (that is, if school_id is not undefined)
  280. // set the school id or set to null
  281. if (is_numeric(Input::get('school_id'))) {
  282. if (Input::get('school_id') != 0)
  283. $template->school_id = Input::get('school_id');
  284. elseif (Input::get('school_id') == 0)
  285. $template->school_id = NULL;
  286. }
  287. // If user can set the program (that is, if program_id is not undefined)
  288. // set the program id or set to null
  289. if (is_numeric(Input::get('program_id'))) {
  290. if (Input::get('program_id') != 0)
  291. $template->program_id = Input::get('program_id');
  292. elseif (Input::get('program_id') == 0)
  293. $template->program_id = NULL;
  294. }
  295. switch (Auth::user()->role) {
  296. case 2:
  297. $template->school_id = Auth::user()->school->id;
  298. break;
  299. case 3:
  300. $template->school_id = Auth::user()->programs[0]->school->id;
  301. $template->program_id = Auth::user()->programs[0]->id;
  302. break;
  303. }
  304. $criteria = Input::get('criteria');
  305. $max_score = Input::get('max_score');
  306. $titles = Input::get('titles');
  307. $template->num_scales = count($titles);
  308. $template->max_score = $max_score;
  309. //$division = $max_score / count($scales[0]);
  310. if ($template->save()) {
  311. $templateId = $template->id;
  312. DB::delete("delete from template_criterion where template_id ={$template->id}");
  313. foreach ($criteria as $index => $criterion_id) {
  314. if (!DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, {$index})")) {
  315. Session::flash('status', 'danger');
  316. Session::flash('message', 'Rubric could not be created.');
  317. DB::rollback();
  318. return;
  319. }
  320. }
  321. DB::delete("delete from template_title where template_id = {$template->id}");
  322. foreach ($titles as $index => $text) {
  323. $query = DB::table('titles')
  324. ->where('text', $text)->first();
  325. if ($query) {
  326. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  327. if (!$result) {
  328. Session::flash('status', 'danger');
  329. Session::flash('message', 'Rubric could not be created.');
  330. DB::rollback();
  331. return;
  332. }
  333. } else {
  334. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  335. if (!$result1) {
  336. Session::flash('status', 'danger');
  337. Session::flash('message', 'Rubric could not be created.');
  338. DB::rollback();
  339. return;
  340. }
  341. $query = DB::table('titles')
  342. ->where('text', $text)->first();
  343. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  344. if (!$result) {
  345. Session::flash('status', 'danger');
  346. Session::flash('message', 'Rubric could not be created.');
  347. DB::rollback();
  348. return;
  349. }
  350. }
  351. }
  352. Session::flash('status', 'success');
  353. Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
  354. DB::commit();
  355. } else {
  356. Session::flash('status', 'danger');
  357. Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
  358. DB::commit();
  359. }
  360. }
  361. /**
  362. * Remove the specified resource from storage.
  363. *
  364. * @return Response
  365. */
  366. public function destroy()
  367. {
  368. $template = Template::find(Input::get('id'));
  369. if ($template->delete()) {
  370. Session::flash('status', 'success');
  371. Session::flash('message', 'Rubric deleted.');
  372. } else {
  373. Session::flash('status', 'danger');
  374. Session::flash('message', 'Rubric could not be deleted.');
  375. }
  376. return;
  377. }
  378. public function printview($id)
  379. {
  380. try {
  381. $template = Template::find($id);
  382. $template->titles = DB::table('titles')
  383. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  384. ->where("template_id", $template->id)
  385. ->orderBy('position', 'ASC')
  386. ->lists('text');
  387. $template_criterion = DB::table('template_criterion')
  388. ->join('criteria', 'criteria.id', '=', 'template_criterion.criterion_id')
  389. ->where('template_id', $template->id)
  390. ->orderBy('position', 'ASC')
  391. ->get();
  392. foreach ($template_criterion as $single_crit) {
  393. $single_crit->scales = DB::table('criterion_scale')
  394. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  395. ->where('criterion_id', $single_crit->id)
  396. ->orderBy('position')
  397. ->lists('description');
  398. $single_crit->outcomes = DB::table('outcomes')
  399. ->join('criterion_objective_outcome', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  400. ->where('criterion_id', $single_crit->id)
  401. ->select('name')
  402. ->distinct()
  403. ->lists('name');
  404. }
  405. $title = $template->name;
  406. if ($template->school_id != NULL)
  407. $school = $template->school->name;
  408. else
  409. $school = 'All Schools';
  410. if ($template->program_id != NULL)
  411. $program = $template->program->name;
  412. else
  413. $program = 'All Programs';
  414. return View::make('local.managers.shared.print_rubric', compact('title', 'template_criterion', 'template', 'school', 'program'));
  415. } catch (Exception $e) {
  416. Session::flash('status', 'danger');
  417. Session::flash('message', $e->getMessage());
  418. return Redirect::back();
  419. }
  420. }
  421. }