설명 없음

TemplatesController.php 12KB

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