Нет описания

TemplatesController.php 8.2KB

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