Ei kuvausta

TemplatesController.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. $criteria = Input::get('criteria');
  114. $max_score = Input::get('max_score');
  115. $titles = Input::get('titles');
  116. $template->num_scales = count($titles);
  117. $template->max_score = $max_score;
  118. if ($template->save()) {
  119. $templateId = $template->id;
  120. foreach ($criteria as $index => $criterion_id) {
  121. DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, '{$index}')");
  122. $template_criterion_id = DB::table('template_criterion')->where('template_id', '=', $templateId)
  123. ->where('criterion_id', '=', $criterion_id)->first();
  124. for ($i = 0; $i < count($scales[$index]); $i++) {
  125. $scale = Scale::where('description', '=', $scales[$index][$i])->first();
  126. Log::info($scale);
  127. if ($scale) {
  128. DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
  129. DB::commit();
  130. } else {
  131. $scale = new Scale;
  132. $scale->description = $scales[$index][$i];
  133. if ($scale->save()) {
  134. DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
  135. DB::commit();
  136. } else {
  137. Session::flash('status', 'danger');
  138. Session::flash('message', 'Rubric could not be created.');
  139. }
  140. }
  141. }
  142. }
  143. Session::flash('status', 'success');
  144. Session::flash('message', 'Rubric created. You can now select it from the list.');
  145. } else {
  146. Session::flash('status', 'danger');
  147. Session::flash('message', 'Rubric could not be created.');
  148. }
  149. }
  150. /**
  151. * Return a specific template
  152. *
  153. * @return Template
  154. */
  155. public function fetch()
  156. {
  157. $template_info = [];
  158. $template_info['template'] = Template::find(Input::get('id'));
  159. $template_info['criterion'] = DB::table('criteria')
  160. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  161. ->where("template_criterion.template_id", '=', Input::get('id'))
  162. ->get();
  163. foreach ($template_info['criterion'] as $temp_crit) {
  164. $template_info['scales'][$temp_crit->id] = DB::table('scales')
  165. ->join('template_criterion_scale', 'template_criterion_scale.scale_id', '=', 'scales.id')
  166. ->where('template_criterion_scale.template_criterion_id', '=', $temp_crit->id)
  167. ->orderBy('position', 'ASC')
  168. ->get();
  169. $outcomeID = DB::table('criterion_objective_outcome')->where('criterion_id', '=', $temp_crit->criterion_id)
  170. ->lists('outcome_id');
  171. $outcomes = DB::table('outcomes')->whereIn('id', $outcomeID)->get();
  172. $outcomeStr = '';
  173. foreach ($outcomes as $key => $outcome) {
  174. $outcomeStr .= $outcome->name . ', ';
  175. }
  176. $outcomeStr = rtrim($outcomeStr, ',');
  177. $temp_crit->outcomes = $outcomeStr;
  178. }
  179. Log::info($template_info);
  180. return json_encode($template_info);
  181. }
  182. /**
  183. * Update the specified resource in storage.
  184. *
  185. * @return Response
  186. */
  187. public function update()
  188. {
  189. $template = Template::find(Input::get('id'));
  190. $template->name = Input::get('name');
  191. $template->is_visible = Input::get('is_visible');
  192. $template->expected_percentage = Input::get('expected_percentage');
  193. $template->expected_points = Input::get('expected_points');
  194. // If user can set the school (that is, if school_id is not undefined)
  195. // set the school id or set to null
  196. if (is_numeric(Input::get('school_id'))) {
  197. if (Input::get('school_id') != 0)
  198. $template->school_id = Input::get('school_id');
  199. elseif (Input::get('school_id') == 0)
  200. $template->school_id = NULL;
  201. }
  202. // If user can set the program (that is, if program_id is not undefined)
  203. // set the program id or set to null
  204. if (is_numeric(Input::get('program_id'))) {
  205. if (Input::get('program_id') != 0)
  206. $template->program_id = Input::get('program_id');
  207. elseif (Input::get('program_id') == 0)
  208. $template->program_id = NULL;
  209. }
  210. switch (Auth::user()->role) {
  211. case 2:
  212. $template->school_id = Auth::user()->school->id;
  213. break;
  214. case 3:
  215. $template->school_id = Auth::user()->programs[0]->school->id;
  216. $template->program_id = Auth::user()->programs[0]->id;
  217. break;
  218. }
  219. $scales = Input::get('scales');
  220. $criteria = Input::get('criteria');
  221. $max_score = Input::get('max_score');
  222. $copyright = Input::get('copyright');
  223. $notes = Input::get('notes');
  224. Log::info($scales);
  225. Log::info(Input::all());
  226. $template->num_scales = count($scales[0]);
  227. $template->max_score = $max_score;
  228. //$division = $max_score / count($scales[0]);
  229. if ($template->save()) {
  230. $templateId = $template->id;
  231. DB::delete("delete from template_criterion where template_id ={$template->id}");
  232. foreach ($criteria as $index => $criterion_id) {
  233. $copy = $copyright[$index];
  234. $note = $notes[$index];
  235. if ($copy == 'Empty') $copy = NULL;
  236. if ($note == 'Empty') $note = NULL;
  237. DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `copyright`, `notes`) values ({$templateId},{$criterion_id}, '{$copy}', '{$note}')");
  238. $template_criterion_id = DB::table('template_criterion')->where('template_id', '=', $templateId)
  239. ->where('criterion_id', '=', $criterion_id)->first();
  240. DB::delete("delete from template_criterion_scale where template_criterion_id ={$template_criterion_id->id}");
  241. for ($i = 0; $i < count($scales[$index]); $i++) {
  242. $scale = Scale::where('description', '=', $scales[$index][$i])
  243. //->where('max_score', '=', ($division * ($i + 1)))
  244. //->where("min_score", '=', (1 + ($division * $i)))
  245. ->first();
  246. Log::info($scale);
  247. if ($scale) {
  248. DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
  249. DB::commit();
  250. } else {
  251. $scale = new Scale;
  252. $scale->description = $scales[$index][$i];
  253. //$scale->min_score = 1 + ($division * $i);
  254. //$scale->max_score = ($division * ($i + 1));
  255. if ($scale->save()) {
  256. DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
  257. DB::commit();
  258. } else {
  259. Session::flash('status', 'danger');
  260. Session::flash('message', 'Rubric could not be created.');
  261. }
  262. }
  263. }
  264. }
  265. Session::flash('status', 'success');
  266. Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
  267. } else {
  268. Session::flash('status', 'danger');
  269. Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
  270. }
  271. }
  272. /**
  273. * Remove the specified resource from storage.
  274. *
  275. * @return Response
  276. */
  277. public function destroy()
  278. {
  279. $template = Template::find(Input::get('id'));
  280. if ($template->delete()) {
  281. Session::flash('status', 'success');
  282. Session::flash('message', 'Rubric deleted.');
  283. } else {
  284. Session::flash('status', 'danger');
  285. Session::flash('message', 'Rubric could not be deleted.');
  286. }
  287. return;
  288. }
  289. public function printview($id)
  290. {
  291. try {
  292. $template = Template::find($id);
  293. $title = $template->name;
  294. if ($template->school_id != NULL)
  295. $school = $template->school->name;
  296. else
  297. $school = 'All Schools';
  298. if ($template->program_id != NULL)
  299. $program = $template->program->name;
  300. else
  301. $program = 'All Programs';
  302. return View::make('local.managers.shared.print_rubric', compact('title', 'template', 'school', 'program'));
  303. } catch (Exception $e) {
  304. Session::flash('status', 'danger');
  305. Session::flash('message', $e->getMessage());
  306. return Redirect::back();
  307. }
  308. }
  309. }