Brak opisu

TemplatesController.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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("new_criteria")->join('template_criterion', 'template_criterion.criterion_id', '=', 'new_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::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->contents = Input::get('contents');
  84. $template->is_visible = Input::get('is_visible');
  85. $template->expected_percentage = Input::get('expected_percentage');
  86. $template->expected_points = Input::get('expected_points');
  87. // If user can set the school (that is, if school_id is not undefined)
  88. // set the school id or set to null
  89. if (is_numeric(Input::get('school_id'))) {
  90. if (Input::get('school_id') != 0)
  91. $template->school_id = Input::get('school_id');
  92. elseif (Input::get('school_id') == 0)
  93. $template->school_id = NULL;
  94. }
  95. // If user can set the program (that is, if program_id is not undefined)
  96. // set the program id or set to null
  97. if (is_numeric(Input::get('program_id'))) {
  98. if (Input::get('program_id') != 0)
  99. $template->program_id = Input::get('program_id');
  100. elseif (Input::get('program_id') == 0)
  101. $template->program_id = NULL;
  102. }
  103. // If the user is any coordinator, set the school id
  104. // If the user is a program coordinator, also set program id
  105. switch (Auth::user()->role) {
  106. case 2:
  107. $template->school_id = Auth::user()->school->id;
  108. break;
  109. case 3:
  110. $template->school_id = Auth::user()->programs[0]->school->id;
  111. $template->program_id = Auth::user()->programs[0]->id;
  112. break;
  113. }
  114. $scales = Input::get('scales');
  115. $criteria = Input::get('criteria');
  116. $max_score = Input::get('max_score');
  117. Log::info($scales);
  118. Log::info($criteria);
  119. $template->num_scales = count($scales[0]);
  120. $template->max_score = $max_score;
  121. $division = $max_score / count($scales[0]);
  122. if ($template->save()) {
  123. $templateId = $template->id;
  124. foreach ($criteria as $index => $criterion_id) {
  125. DB::insert("insert into template_criterion (`template_id`,`criterion_id`) values ({$templateId},{$criterion_id})");
  126. $template_criterion_id = DB::table('template_criterion')->where('template_id', '=', $templateId)
  127. ->where('criterion_id', '=', $criterion_id)->first();
  128. for ($i = 0; $i < count($scales[$index]); $i++) {
  129. $scale = new Scale;
  130. $scale->description = $scales[$index][$i];
  131. $scale->min_score = 1 + ($division * $i);
  132. $scale->max_score = ($division * ($i + 1));
  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. } else {
  136. Session::flash('status', 'danger');
  137. Session::flash('message', 'Rubric could not be created.');
  138. }
  139. }
  140. }
  141. Session::flash('status', 'success');
  142. Session::flash('message', 'Rubric created. You can now select it from the list.');
  143. } else {
  144. Session::flash('status', 'danger');
  145. Session::flash('message', 'Rubric could not be created.');
  146. }
  147. }
  148. /**
  149. * Return a specific template
  150. *
  151. * @return Template
  152. */
  153. public function fetch()
  154. {
  155. $template_info = [];
  156. $template_info['template'] = Template::find(Input::get('id'));
  157. $template_info['criterion'] = DB::table('new_criteria')
  158. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'new_criteria.id')
  159. ->where("template_criterion.template_id", '=', Input::get('id'))
  160. ->get();
  161. foreach ($template_info['criterion'] as $temp_crit) {
  162. $template_info['scales'][$temp_crit->id] = DB::table('scales')
  163. ->join('template_criterion_scale', 'template_criterion_scale.scale_id', '=', 'scales.id')
  164. ->where('template_criterion_scale.template_criterion_id', '=', $temp_crit->id)
  165. ->orderBy('position', 'ASC')
  166. ->get();
  167. }
  168. Log::info($template_info);
  169. return json_encode($template_info);
  170. }
  171. /**
  172. * Update the specified resource in storage.
  173. *
  174. * @return Response
  175. */
  176. public function update()
  177. {
  178. $template = Template::find(Input::get('id'));
  179. $template->name = Input::get('name');
  180. $template->contents = Input::get('contents');
  181. $template->is_visible = Input::get('is_visible');
  182. $template->expected_percentage = Input::get('expected_percentage');
  183. $template->expected_points = Input::get('expected_points');
  184. // If user can set the school (that is, if school_id is not undefined)
  185. // set the school id or set to null
  186. if (is_numeric(Input::get('school_id'))) {
  187. if (Input::get('school_id') != 0)
  188. $template->school_id = Input::get('school_id');
  189. elseif (Input::get('school_id') == 0)
  190. $template->school_id = NULL;
  191. }
  192. // If user can set the program (that is, if program_id is not undefined)
  193. // set the program id or set to null
  194. if (is_numeric(Input::get('program_id'))) {
  195. if (Input::get('program_id') != 0)
  196. $template->program_id = Input::get('program_id');
  197. elseif (Input::get('program_id') == 0)
  198. $template->program_id = NULL;
  199. }
  200. if ($template->save()) {
  201. Session::flash('status', 'success');
  202. Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
  203. } else {
  204. Session::flash('status', 'danger');
  205. Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
  206. }
  207. }
  208. /**
  209. * Remove the specified resource from storage.
  210. *
  211. * @return Response
  212. */
  213. public function destroy()
  214. {
  215. $template = Template::find(Input::get('id'));
  216. if ($template->delete()) {
  217. Session::flash('status', 'success');
  218. Session::flash('message', 'Rubric deleted.');
  219. } else {
  220. Session::flash('status', 'danger');
  221. Session::flash('message', 'Rubric could not be deleted.');
  222. }
  223. return;
  224. }
  225. public function printview($id)
  226. {
  227. try {
  228. $template = Template::find($id);
  229. $title = $template->name;
  230. if ($template->school_id != NULL)
  231. $school = $template->school->name;
  232. else
  233. $school = 'All Schools';
  234. if ($template->program_id != NULL)
  235. $program = $template->program->name;
  236. else
  237. $program = 'All Programs';
  238. return View::make('local.managers.shared.print_rubric', compact('title', 'template', 'school', 'program'));
  239. } catch (Exception $e) {
  240. Session::flash('status', 'danger');
  241. Session::flash('message', $e->getMessage());
  242. return Redirect::back();
  243. }
  244. }
  245. }