Sin descripción

TemplatesController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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")->join('template_criterion_scale', 'template_criterion_scale.scale_id', '=', 'scales.id')
  42. ->where("template_criterion_scale.template_criterion_id", '=', $criteria->id)->orderBy('position', 'ASC')->get();
  43. }
  44. return json_encode($json_to_send);
  45. }
  46. /**
  47. * Show the form for creating a new rubric
  48. *
  49. * @return Response
  50. */
  51. public function newTemplate()
  52. {
  53. $title = "Rubric Builder";
  54. $templates = Template::orderBy('name', 'ASC')->get();
  55. $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->get();
  56. $criteria = Criterion::orderBy('name', 'ASC')->get();
  57. $schools = School::orderBy('name', 'ASC')->get();
  58. $role = Auth::user()->role;
  59. $templates = NULL;
  60. $programs = NULL;
  61. // Returns templates depending on the type of user
  62. if ($role == 1) {
  63. $templates = Template::orderBy('name', 'ASC')->get();
  64. $programs = Program::orderBy('name', 'ASC')->get();
  65. } elseif ($role == 2) {
  66. $templates = Template::where('school_id', '=', Auth::user()->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  67. $programs = Auth::user()->school->programs;
  68. } elseif ($role == 3) {
  69. $templates = Template::where('school_id', '=', Auth::user()->programs[0]->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  70. $programs = Auth::user()->programs()->get();
  71. }
  72. return View::make('local.managers.shared.rubrics', compact('title', 'templates', 'outcomes', 'criteria', 'schools', 'programs'));
  73. }
  74. /**
  75. * Save a new rubric
  76. *
  77. * @return Response
  78. */
  79. public function create()
  80. {
  81. $template = new Template;
  82. $template->name = Input::get('name');
  83. $template->is_visible = Input::get('is_visible');
  84. $template->expected_percentage = Input::get('expected_percentage');
  85. $template->expected_points = Input::get('expected_points');
  86. // If user can set the school (that is, if school_id is not undefined)
  87. // set the school id or set to null
  88. if (is_numeric(Input::get('school_id'))) {
  89. if (Input::get('school_id') != 0)
  90. $template->school_id = Input::get('school_id');
  91. elseif (Input::get('school_id') == 0)
  92. $template->school_id = NULL;
  93. }
  94. // If user can set the program (that is, if program_id is not undefined)
  95. // set the program id or set to null
  96. if (is_numeric(Input::get('program_id'))) {
  97. if (Input::get('program_id') != 0)
  98. $template->program_id = Input::get('program_id');
  99. elseif (Input::get('program_id') == 0)
  100. $template->program_id = NULL;
  101. }
  102. // If the user is any coordinator, set the school id
  103. // If the user is a program coordinator, also set program id
  104. switch (Auth::user()->role) {
  105. case 2:
  106. $template->school_id = Auth::user()->school->id;
  107. break;
  108. case 3:
  109. $template->school_id = Auth::user()->programs[0]->school->id;
  110. $template->program_id = Auth::user()->programs[0]->id;
  111. break;
  112. }
  113. $scales = Input::get('scales');
  114. $criteria = Input::get('criteria');
  115. $max_score = Input::get('max_score');
  116. Log::info($scales);
  117. Log::info($criteria);
  118. $template->num_scales = count($scales[0]);
  119. $template->max_score = $max_score;
  120. $division = $max_score / count($scales[0]);
  121. if ($template->save()) {
  122. $templateId = $template->id;
  123. foreach ($criteria as $index => $criterion_id) {
  124. DB::insert("insert into template_criterion (`template_id`,`criterion_id`) values ({$templateId},{$criterion_id})");
  125. $template_criterion_id = DB::table('template_criterion')->where('template_id', '=', $templateId)
  126. ->where('criterion_id', '=', $criterion_id)->first();
  127. for ($i = 0; $i < count($scales[$index]); $i++) {
  128. $scale = Scale::where('description', '=', $scales[$index][$i])
  129. ->where('max_score', '=', ($division * ($i + 1)))
  130. ->where("min_score", '=', (1 + ($division * $i)))->first();
  131. Log::info($scale);
  132. if ($scale) {
  133. DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
  134. DB::commit();
  135. } else {
  136. $scale = new Scale;
  137. $scale->description = $scales[$index][$i];
  138. $scale->min_score = 1 + ($division * $i);
  139. $scale->max_score = ($division * ($i + 1));
  140. if ($scale->save()) {
  141. DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
  142. DB::commit();
  143. } else {
  144. Session::flash('status', 'danger');
  145. Session::flash('message', 'Rubric could not be created.');
  146. }
  147. }
  148. }
  149. }
  150. Session::flash('status', 'success');
  151. Session::flash('message', 'Rubric created. You can now select it from the list.');
  152. } else {
  153. Session::flash('status', 'danger');
  154. Session::flash('message', 'Rubric could not be created.');
  155. }
  156. }
  157. /**
  158. * Return a specific template
  159. *
  160. * @return Template
  161. */
  162. public function fetch()
  163. {
  164. $template_info = [];
  165. $template_info['template'] = Template::find(Input::get('id'));
  166. $template_info['criterion'] = DB::table('criteria')
  167. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  168. ->where("template_criterion.template_id", '=', Input::get('id'))
  169. ->get();
  170. foreach ($template_info['criterion'] as $temp_crit) {
  171. $template_info['scales'][$temp_crit->id] = DB::table('scales')
  172. ->join('template_criterion_scale', 'template_criterion_scale.scale_id', '=', 'scales.id')
  173. ->where('template_criterion_scale.template_criterion_id', '=', $temp_crit->id)
  174. ->orderBy('position', 'ASC')
  175. ->get();
  176. }
  177. Log::info($template_info);
  178. return json_encode($template_info);
  179. }
  180. /**
  181. * Update the specified resource in storage.
  182. *
  183. * @return Response
  184. */
  185. public function update()
  186. {
  187. $template = Template::find(Input::get('id'));
  188. $template->name = Input::get('name');
  189. $template->is_visible = Input::get('is_visible');
  190. $template->expected_percentage = Input::get('expected_percentage');
  191. $template->expected_points = Input::get('expected_points');
  192. // If user can set the school (that is, if school_id is not undefined)
  193. // set the school id or set to null
  194. if (is_numeric(Input::get('school_id'))) {
  195. if (Input::get('school_id') != 0)
  196. $template->school_id = Input::get('school_id');
  197. elseif (Input::get('school_id') == 0)
  198. $template->school_id = NULL;
  199. }
  200. // If user can set the program (that is, if program_id is not undefined)
  201. // set the program id or set to null
  202. if (is_numeric(Input::get('program_id'))) {
  203. if (Input::get('program_id') != 0)
  204. $template->program_id = Input::get('program_id');
  205. elseif (Input::get('program_id') == 0)
  206. $template->program_id = NULL;
  207. }
  208. switch (Auth::user()->role) {
  209. case 2:
  210. $template->school_id = Auth::user()->school->id;
  211. break;
  212. case 3:
  213. $template->school_id = Auth::user()->programs[0]->school->id;
  214. $template->program_id = Auth::user()->programs[0]->id;
  215. break;
  216. }
  217. $scales = Input::get('scales');
  218. $criteria = Input::get('criteria');
  219. $max_score = Input::get('max_score');
  220. Log::info($scales);
  221. Log::info(Input::all());
  222. $template->num_scales = count($scales[0]);
  223. $template->max_score = $max_score;
  224. $division = $max_score / count($scales[0]);
  225. if ($template->save()) {
  226. $templateId = $template->id;
  227. DB::delete("delete from template_criterion where template_id ={$template->id}");
  228. foreach ($criteria as $index => $criterion_id) {
  229. DB::insert("insert into template_criterion (`template_id`,`criterion_id`) values ({$templateId},{$criterion_id})");
  230. $template_criterion_id = DB::table('template_criterion')->where('template_id', '=', $templateId)
  231. ->where('criterion_id', '=', $criterion_id)->first();
  232. DB::delete("delete from template_criterion_scale where template_criterion_id ={$template_criterion_id->id}");
  233. for ($i = 0; $i < count($scales[$index]); $i++) {
  234. $scale = Scale::where('description', '=', $scales[$index][$i])
  235. ->where('max_score', '=', ($division * ($i + 1)))
  236. ->where("min_score", '=', (1 + ($division * $i)))->first();
  237. Log::info($scale);
  238. if ($scale) {
  239. DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
  240. DB::commit();
  241. } else {
  242. $scale = new Scale;
  243. $scale->description = $scales[$index][$i];
  244. $scale->min_score = 1 + ($division * $i);
  245. $scale->max_score = ($division * ($i + 1));
  246. if ($scale->save()) {
  247. DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
  248. DB::commit();
  249. } else {
  250. Session::flash('status', 'danger');
  251. Session::flash('message', 'Rubric could not be created.');
  252. }
  253. }
  254. }
  255. }
  256. Session::flash('status', 'success');
  257. Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
  258. } else {
  259. Session::flash('status', 'danger');
  260. Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
  261. }
  262. }
  263. /**
  264. * Remove the specified resource from storage.
  265. *
  266. * @return Response
  267. */
  268. public function destroy()
  269. {
  270. $template = Template::find(Input::get('id'));
  271. if ($template->delete()) {
  272. Session::flash('status', 'success');
  273. Session::flash('message', 'Rubric deleted.');
  274. } else {
  275. Session::flash('status', 'danger');
  276. Session::flash('message', 'Rubric could not be deleted.');
  277. }
  278. return;
  279. }
  280. public function printview($id)
  281. {
  282. try {
  283. $template = Template::find($id);
  284. $title = $template->name;
  285. if ($template->school_id != NULL)
  286. $school = $template->school->name;
  287. else
  288. $school = 'All Schools';
  289. if ($template->program_id != NULL)
  290. $program = $template->program->name;
  291. else
  292. $program = 'All Programs';
  293. return View::make('local.managers.shared.print_rubric', compact('title', 'template', 'school', 'program'));
  294. } catch (Exception $e) {
  295. Session::flash('status', 'danger');
  296. Session::flash('message', $e->getMessage());
  297. return Redirect::back();
  298. }
  299. }
  300. }