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

TemplatesController.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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")
  42. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  43. ->where("criterion_scale.criterion_id", '=', $criteria->criterion_id)->orderBy('position', 'ASC')->get();
  44. }
  45. return json_encode($json_to_send);
  46. }
  47. /**
  48. * Show the form for creating a new rubric
  49. *
  50. * @return Response
  51. */
  52. public function newTemplate()
  53. {
  54. $title = "Rubric Builder";
  55. $templates = Template::orderBy('name', 'ASC')->get();
  56. $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->get();
  57. $criteria = Criterion::orderBy('name', 'ASC')->get();
  58. $schools = School::orderBy('name', 'ASC')->get();
  59. $role = Auth::user()->role;
  60. $templates = NULL;
  61. $programs = NULL;
  62. // Returns templates depending on the type of user
  63. if ($role == 1) {
  64. $templates = Template::orderBy('name', 'ASC')->get();
  65. $programs = Program::orderBy('name', 'ASC')->get();
  66. } elseif ($role == 2) {
  67. $templates = Template::where('school_id', '=', Auth::user()->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  68. $programs = Auth::user()->school->programs;
  69. } elseif ($role == 3) {
  70. $templates = Template::where('school_id', '=', Auth::user()->programs[0]->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  71. $programs = Auth::user()->programs()->get();
  72. }
  73. return View::make('local.managers.shared.rubrics', compact('title', 'templates', 'outcomes', 'criteria', 'schools', 'programs'));
  74. }
  75. /**
  76. * Save a new rubric
  77. *
  78. * @return Response
  79. */
  80. public function create()
  81. {
  82. DB::beginTransaction();
  83. $template = new Template;
  84. $template->name = Input::get('name');
  85. $template->is_visible = Input::get('is_visible');
  86. $template->expected_percentage = Input::get('expected_percentage');
  87. $template->expected_points = Input::get('expected_points');
  88. // If user can set the school (that is, if school_id is not undefined)
  89. // set the school id or set to null
  90. if (is_numeric(Input::get('school_id'))) {
  91. if (Input::get('school_id') != 0)
  92. $template->school_id = Input::get('school_id');
  93. elseif (Input::get('school_id') == 0)
  94. $template->school_id = NULL;
  95. }
  96. // If user can set the program (that is, if program_id is not undefined)
  97. // set the program id or set to null
  98. if (is_numeric(Input::get('program_id'))) {
  99. if (Input::get('program_id') != 0)
  100. $template->program_id = Input::get('program_id');
  101. elseif (Input::get('program_id') == 0)
  102. $template->program_id = NULL;
  103. }
  104. // If the user is any coordinator, set the school id
  105. // If the user is a program coordinator, also set program id
  106. switch (Auth::user()->role) {
  107. case 2:
  108. $template->school_id = Auth::user()->school->id;
  109. break;
  110. case 3:
  111. $template->school_id = Auth::user()->programs[0]->school->id;
  112. $template->program_id = Auth::user()->programs[0]->id;
  113. break;
  114. }
  115. $criteria = Input::get('criteria');
  116. $max_score = Input::get('max_score');
  117. $titles = Input::get('titles');
  118. $template->num_scales = count($titles);
  119. $template->max_score = $max_score;
  120. if ($template->save()) {
  121. $templateId = $template->id;
  122. foreach ($criteria as $index => $criterion_id) {
  123. if (!(DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, '{$index}')"))) {
  124. Session::flash('status', 'danger');
  125. Session::flash('message', 'Rubric could not be created.');
  126. DB::rollback();
  127. return;
  128. }
  129. }
  130. foreach ($titles as $index => $text) {
  131. $query = DB::table('titles')
  132. ->where('text', $text)->first();
  133. if ($query) {
  134. $result = DB::insert("insert into `template_title` (`template_id`, `title_id` `position`) values ({$templateId}, {$query->id}, {$index})");
  135. if (!$result) {
  136. Session::flash('status', 'danger');
  137. Session::flash('message', 'Rubric could not be created.');
  138. DB::rollback();
  139. return;
  140. }
  141. } else {
  142. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  143. if (!$result1) {
  144. Session::flash('status', 'danger');
  145. Session::flash('message', 'Rubric could not be created.');
  146. DB::rollback();
  147. return;
  148. }
  149. $query = DB::table('titles')
  150. ->where('text', $text)->first();
  151. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  152. if (!$result) {
  153. Session::flash('status', 'danger');
  154. Session::flash('message', 'Rubric could not be created.');
  155. DB::rollback();
  156. return;
  157. }
  158. }
  159. }
  160. Session::flash('status', 'success');
  161. Session::flash('message', 'Rubric created. You can now select it from the list.');
  162. DB::commit();
  163. return;
  164. } else {
  165. Session::flash('status', 'danger');
  166. Session::flash('message', 'Rubric could not be created.');
  167. DB::rollback();
  168. return;
  169. }
  170. }
  171. /**
  172. * Return a specific template
  173. *
  174. * @return Template
  175. */
  176. public function fetch()
  177. {
  178. $template_info = [];
  179. $template_info['template'] = Template::find(Input::get('id'));
  180. $template_info['criterion'] = DB::table('criteria')
  181. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  182. ->where("template_criterion.template_id", '=', Input::get('id'))
  183. ->get();
  184. foreach ($template_info['criterion'] as $temp_crit) {
  185. $temp_crit->scales = DB::table('scales')
  186. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  187. ->where('criterion_scale.criterion_id', '=', $temp_crit->criterion_id)
  188. ->orderBy('position', 'ASC')
  189. ->get();
  190. $outcomeID = DB::table('criterion_objective_outcome')->where('criterion_id', '=', $temp_crit->criterion_id)
  191. ->lists('outcome_id');
  192. $outcomes = DB::table('outcomes')->whereIn('id', $outcomeID)->get();
  193. $outcomeStr = '';
  194. foreach ($outcomes as $key => $outcome) {
  195. $outcomeStr .= $outcome->name . ', ';
  196. }
  197. $outcomeStr = rtrim($outcomeStr, ',');
  198. $temp_crit->outcomes = $outcomeStr;
  199. }
  200. $template_info['titles'] = DB::table('titles')
  201. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  202. ->where('template_id', Input::get('id'))
  203. ->orderBy('position')
  204. ->get();
  205. Log::info($template_info);
  206. return json_encode($template_info);
  207. }
  208. /**
  209. * Update the specified resource in storage.
  210. *
  211. * @return Response
  212. */
  213. public function update()
  214. {
  215. DB::beginTransaction();
  216. $template = Template::find(Input::get('id'));
  217. Log::info(Input::all());
  218. $template->name = Input::get('name');
  219. $template->is_visible = Input::get('is_visible');
  220. $template->expected_percentage = Input::get('expected_percentage');
  221. $template->expected_points = Input::get('expected_points');
  222. // If user can set the school (that is, if school_id is not undefined)
  223. // set the school id or set to null
  224. if (is_numeric(Input::get('school_id'))) {
  225. if (Input::get('school_id') != 0)
  226. $template->school_id = Input::get('school_id');
  227. elseif (Input::get('school_id') == 0)
  228. $template->school_id = NULL;
  229. }
  230. // If user can set the program (that is, if program_id is not undefined)
  231. // set the program id or set to null
  232. if (is_numeric(Input::get('program_id'))) {
  233. if (Input::get('program_id') != 0)
  234. $template->program_id = Input::get('program_id');
  235. elseif (Input::get('program_id') == 0)
  236. $template->program_id = NULL;
  237. }
  238. switch (Auth::user()->role) {
  239. case 2:
  240. $template->school_id = Auth::user()->school->id;
  241. break;
  242. case 3:
  243. $template->school_id = Auth::user()->programs[0]->school->id;
  244. $template->program_id = Auth::user()->programs[0]->id;
  245. break;
  246. }
  247. $criteria = Input::get('criteria');
  248. $max_score = Input::get('max_score');
  249. $titles = Input::get('titles');
  250. $template->num_scales = count($titles);
  251. $template->max_score = $max_score;
  252. //$division = $max_score / count($scales[0]);
  253. if ($template->save()) {
  254. $templateId = $template->id;
  255. DB::delete("delete from template_criterion where template_id ={$template->id}");
  256. foreach ($criteria as $index => $criterion_id) {
  257. if (!DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, {$index})")) {
  258. Session::flash('status', 'danger');
  259. Session::flash('message', 'Rubric could not be created.');
  260. DB::rollback();
  261. return;
  262. }
  263. }
  264. DB::delete("delete from template_title where template_id = {$template->id}");
  265. foreach ($titles as $index => $text) {
  266. $query = DB::table('titles')
  267. ->where('text', $text)->first();
  268. if ($query) {
  269. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  270. if (!$result) {
  271. Session::flash('status', 'danger');
  272. Session::flash('message', 'Rubric could not be created.');
  273. DB::rollback();
  274. return;
  275. }
  276. } else {
  277. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  278. if (!$result1) {
  279. Session::flash('status', 'danger');
  280. Session::flash('message', 'Rubric could not be created.');
  281. DB::rollback();
  282. return;
  283. }
  284. $query = DB::table('titles')
  285. ->where('text', $text)->first();
  286. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  287. if (!$result) {
  288. Session::flash('status', 'danger');
  289. Session::flash('message', 'Rubric could not be created.');
  290. DB::rollback();
  291. return;
  292. }
  293. }
  294. }
  295. Session::flash('status', 'success');
  296. Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
  297. DB::commit();
  298. } else {
  299. Session::flash('status', 'danger');
  300. Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
  301. DB::commit();
  302. }
  303. }
  304. /**
  305. * Remove the specified resource from storage.
  306. *
  307. * @return Response
  308. */
  309. public function destroy()
  310. {
  311. $template = Template::find(Input::get('id'));
  312. if ($template->delete()) {
  313. Session::flash('status', 'success');
  314. Session::flash('message', 'Rubric deleted.');
  315. } else {
  316. Session::flash('status', 'danger');
  317. Session::flash('message', 'Rubric could not be deleted.');
  318. }
  319. return;
  320. }
  321. public function printview($id)
  322. {
  323. try {
  324. $template = Template::find($id);
  325. $template->titles = DB::table('titles')
  326. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  327. ->where("template_id", $template->id)
  328. ->orderBy('position', 'ASC')
  329. ->lists('text');
  330. $template_criterion = DB::table('template_criterion')
  331. ->join('criteria', 'criteria.id', '=', 'template_criterion.criterion_id')
  332. ->where('template_id', $template->id)
  333. ->orderBy('position', 'ASC')
  334. ->get();
  335. foreach ($template_criterion as $single_crit) {
  336. $single_crit->scales = DB::table('criterion_scale')
  337. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  338. ->where('criterion_id', $single_crit->id)
  339. ->orderBy('position')
  340. ->lists('description');
  341. $single_crit->outcomes = DB::table('outcomes')
  342. ->join('criterion_objective_outcome', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  343. ->where('criterion_id', $single_crit->id)
  344. ->select('name')
  345. ->distinct()
  346. ->lists('name');
  347. }
  348. $title = $template->name;
  349. if ($template->school_id != NULL)
  350. $school = $template->school->name;
  351. else
  352. $school = 'All Schools';
  353. if ($template->program_id != NULL)
  354. $program = $template->program->name;
  355. else
  356. $program = 'All Programs';
  357. return View::make('local.managers.shared.print_rubric', compact('title', 'template_criterion', 'template', 'school', 'program'));
  358. } catch (Exception $e) {
  359. Session::flash('status', 'danger');
  360. Session::flash('message', $e->getMessage());
  361. return Redirect::back();
  362. }
  363. }
  364. }