Без опису

TemplatesController.php 14KB

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