Няма описание

TemplatesController.php 11KB

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