説明なし

TemplatesController.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php
  2. class TemplatesController extends \BaseController
  3. {
  4. /**
  5. * List all templates, grouped by program
  6. */
  7. public function ProfIndex()
  8. {
  9. $title = 'Rubric List';
  10. $role = Auth::user()->role;
  11. $program_id = DB::table("program_user")
  12. ->where('user_id', Auth::user()->id)
  13. ->lists('program_id')[0];
  14. $school_id = DB::table('programs')
  15. ->where('id', $program_id)
  16. ->lists('school_id')[0];
  17. $templates = Template::orderBy('name')
  18. ->whereNull("school_id")
  19. ->orWhere(function ($query) use (&$school_id) {
  20. $query->where('school_id', $school_id)
  21. ->whereNull('program_id');
  22. })
  23. ->orWhere(function ($query) use (&$program_id, &$school_id) {
  24. $query->where('school_id', $school_id)
  25. ->where('program_id', $program_id);
  26. })
  27. ->get();
  28. return View::make('local.managers.shared.rubric_list', compact('title', 'global_templates', 'schools', 'templates', 'role'));
  29. }
  30. public function index()
  31. {
  32. $title = 'Rubric List';
  33. $global_templates = Template::whereNull('school_id')->whereNull('program_id')->get();
  34. $schools = School::with('programs.templates')
  35. ->orderBy('name')
  36. ->get();
  37. $role = Auth::user()->role;
  38. switch ($role) {
  39. case 1:
  40. $templates = Template::orderBy('name')->get();
  41. break;
  42. case 2:
  43. $templates = Template::orderBy('name')
  44. ->whereNull("school_id")
  45. ->orWhere('school_id', Auth::user()->school_id)
  46. ->get();
  47. break;
  48. case 3:
  49. case 4:
  50. $program_id = DB::table("program_user")
  51. ->where('user_id', Auth::user()->id)
  52. ->lists('program_id')[0];
  53. $school_id = DB::table('programs')
  54. ->where('id', $program_id)
  55. ->lists('school_id')[0];
  56. $templates = Template::orderBy('name')
  57. ->whereNull("school_id")
  58. ->orWhere(function ($query) use (&$school_id) {
  59. $query->where('school_id', $school_id)
  60. ->whereNull('program_id');
  61. })
  62. ->orWhere(function ($query) use (&$program_id, &$school_id) {
  63. $query->where('school_id', $school_id)
  64. ->where('program_id', $program_id);
  65. })
  66. ->get();
  67. break;
  68. }
  69. //$templates = Template::orderBy('name')->get();
  70. return View::make('local.managers.shared.rubric_list', compact('title', 'global_templates', 'schools', 'templates', 'role'));
  71. }
  72. /*public function schoolCoordinatorIndex()
  73. {
  74. $title = 'Rubric List';
  75. $global_templates = Template::whereNull('school_id')->whereNull('program_id')->get();
  76. $schools = School::with('programs.templates')
  77. ->orderBy('name')
  78. ->get();
  79. $templates = Template::orderBy('name')->get();
  80. return View::make('local.managers.admins.rubric_list', compact('title', 'global_templates', 'schools', 'templates'));
  81. }*/
  82. public function show(Template $template)
  83. {
  84. $title = $template->name;
  85. $template->titles = DB::table('titles')
  86. ->join('template_title', 'template_title.title_id', '=', "titles.id")
  87. ->where('template_id', $template->id)
  88. ->orderBy('position', 'ASC')
  89. ->lists('text');
  90. $template->criteria = DB::table('criteria')
  91. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  92. ->where('template_id', $template->id)
  93. ->orderBy('position')
  94. ->get();
  95. foreach ($template->criteria as $criterion) {
  96. $criterion->scales = DB::table('criterion_scale')
  97. ->join('scales', 'scales.id', '=', 'criterion_scale.scale_id')
  98. ->where('criterion_id', $criterion->criterion_id)
  99. ->orderBy('position')
  100. ->lists('description');
  101. $criterion->outcomes = DB::table('criterion_objective_outcome as cobo')
  102. ->join('outcomes', 'outcomes.id', '=', 'cobo.outcome_id')
  103. ->where('cobo.criterion_id', $criterion->criterion_id)
  104. ->select('outcomes.*')
  105. ->distinct()
  106. ->get();
  107. }
  108. return View::make('local.managers.admins.view_template', compact('template', 'title'));
  109. }
  110. public function onLoadFetch()
  111. {
  112. $json_to_send = [];
  113. $template_id = Input::get('id');
  114. $json_to_send["criteria"] = DB::table("criteria")->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  115. ->where("template_criterion.template_id", '=', $template_id)
  116. ->get();
  117. Log::info($json_to_send["criteria"]);
  118. foreach ($json_to_send['criteria'] as $criteria) {
  119. $json_to_send['scales'][$criteria->criterion_id] = DB::table("scales")
  120. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  121. ->where("criterion_scale.criterion_id", '=', $criteria->criterion_id)->orderBy('position', 'ASC')->get();
  122. }
  123. return json_encode($json_to_send);
  124. }
  125. /**
  126. * Show the form for creating a new rubric
  127. *
  128. * @return Response
  129. */
  130. public function newTemplate()
  131. {
  132. $title = "Rubric Builder";
  133. $templates = Template::orderBy('name', 'ASC')->get();
  134. $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->get();
  135. $criteria = Criterion::orderBy('name', 'ASC')->get();
  136. $schools = School::orderBy('name', 'ASC')->get();
  137. $role = Auth::user()->role;
  138. $templates = NULL;
  139. $programs = NULL;
  140. // Returns templates depending on the type of user
  141. if ($role == 1) {
  142. $templates = Template::orderBy('name', 'ASC')->get();
  143. $programs = Program::orderBy('name', 'ASC')->get();
  144. } elseif ($role == 2) {
  145. $templates = Template::where('school_id', '=', Auth::user()->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  146. $programs = Auth::user()->school->programs;
  147. } elseif ($role == 3) {
  148. $templates = Template::where('school_id', '=', Auth::user()->programs[0]->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  149. $programs = Auth::user()->programs()->get();
  150. }
  151. return View::make('local.managers.shared.rubrics', compact('title', 'templates', 'outcomes', 'criteria', 'schools', 'programs'));
  152. }
  153. /**
  154. * Save a new rubric
  155. *
  156. * @return Response
  157. */
  158. public function create()
  159. {
  160. DB::beginTransaction();
  161. $template = new Template;
  162. $template->name = Input::get('name');
  163. $template->is_visible = Input::get('is_visible');
  164. $template->expected_percentage = Input::get('expected_percentage');
  165. $template->expected_points = Input::get('expected_points');
  166. // If user can set the school (that is, if school_id is not undefined)
  167. // set the school id or set to null
  168. if (is_numeric(Input::get('school_id'))) {
  169. if (Input::get('school_id') != 0)
  170. $template->school_id = Input::get('school_id');
  171. elseif (Input::get('school_id') == 0)
  172. $template->school_id = NULL;
  173. }
  174. // If user can set the program (that is, if program_id is not undefined)
  175. // set the program id or set to null
  176. if (is_numeric(Input::get('program_id'))) {
  177. if (Input::get('program_id') != 0)
  178. $template->program_id = Input::get('program_id');
  179. elseif (Input::get('program_id') == 0)
  180. $template->program_id = NULL;
  181. }
  182. // If the user is any coordinator, set the school id
  183. // If the user is a program coordinator, also set program id
  184. switch (Auth::user()->role) {
  185. case 2:
  186. $template->school_id = Auth::user()->school->id;
  187. break;
  188. case 3:
  189. $template->school_id = Auth::user()->programs[0]->school->id;
  190. $template->program_id = Auth::user()->programs[0]->id;
  191. break;
  192. }
  193. $criteria = Input::get('criteria');
  194. $max_score = Input::get('max_score');
  195. $titles = Input::get('titles');
  196. $template->num_scales = count($titles);
  197. $template->max_score = $max_score;
  198. if ($template->save()) {
  199. $templateId = $template->id;
  200. foreach ($criteria as $index => $criterion_id) {
  201. if (!(DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, '{$index}')"))) {
  202. Session::flash('status', 'danger');
  203. Session::flash('message', 'Rubric could not be created.');
  204. DB::rollback();
  205. return;
  206. }
  207. }
  208. foreach ($titles as $index => $text) {
  209. $query = DB::table('titles')
  210. ->where('text', $text)->first();
  211. if ($query) {
  212. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  213. if (!$result) {
  214. Session::flash('status', 'danger');
  215. Session::flash('message', 'Rubric could not be created.');
  216. DB::rollback();
  217. return;
  218. }
  219. } else {
  220. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  221. if (!$result1) {
  222. Session::flash('status', 'danger');
  223. Session::flash('message', 'Rubric could not be created.');
  224. DB::rollback();
  225. return;
  226. }
  227. $query = DB::table('titles')
  228. ->where('text', $text)->first();
  229. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  230. if (!$result) {
  231. Session::flash('status', 'danger');
  232. Session::flash('message', 'Rubric could not be created.');
  233. DB::rollback();
  234. return;
  235. }
  236. }
  237. }
  238. Session::flash('status', 'success');
  239. Session::flash('message', 'Rubric created. You can now select it from the list.');
  240. DB::commit();
  241. return;
  242. } else {
  243. Session::flash('status', 'danger');
  244. Session::flash('message', 'Rubric could not be created.');
  245. DB::rollback();
  246. return;
  247. }
  248. }
  249. /**
  250. * Return a specific template
  251. *
  252. * @return Template
  253. */
  254. public function fetch()
  255. {
  256. $template_info = [];
  257. $template_info['template'] = Template::find(Input::get('id'));
  258. $template_info['criterion'] = DB::table('criteria')
  259. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  260. ->where("template_criterion.template_id", '=', Input::get('id'))
  261. ->get();
  262. foreach ($template_info['criterion'] as $temp_crit) {
  263. $temp_crit->scales = DB::table('scales')
  264. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  265. ->where('criterion_scale.criterion_id', '=', $temp_crit->criterion_id)
  266. ->orderBy('position', 'ASC')
  267. ->get();
  268. $temp_crit->program_ids = json_encode(DB::table('program_criterion')
  269. ->where('criterion_id', $temp_crit->id)
  270. ->lists('program_id'));
  271. $temp_crit->objectives = DB::table('criterion_objective_outcome')
  272. ->join('objectives', 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  273. ->where('criterion_id', $temp_crit->id)
  274. ->select('objectives.*')
  275. ->distinct()
  276. ->lists('text');
  277. $outcomeID = DB::table('criterion_objective_outcome')->where('criterion_id', '=', $temp_crit->criterion_id)
  278. ->lists('outcome_id');
  279. $outcomes = DB::table('outcomes')->whereIn('id', $outcomeID)->get();
  280. $outcomeStr = '';
  281. foreach ($outcomes as $key => $outcome) {
  282. $outcomeStr .= $outcome->name . ', ';
  283. }
  284. $outcomeStr = rtrim($outcomeStr, ',');
  285. $temp_crit->outcomes = $outcomeStr;
  286. }
  287. $template_info['titles'] = DB::table('titles')
  288. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  289. ->where('template_id', Input::get('id'))
  290. ->orderBy('position')
  291. ->get();
  292. Log::info($template_info);
  293. return json_encode($template_info);
  294. }
  295. /**
  296. * Update the specified resource in storage.
  297. *
  298. * @return Response
  299. */
  300. public function update()
  301. {
  302. DB::beginTransaction();
  303. $template = Template::find(Input::get('id'));
  304. Log::info(Input::all());
  305. $template->name = Input::get('name');
  306. $template->is_visible = Input::get('is_visible');
  307. $template->expected_percentage = Input::get('expected_percentage');
  308. $template->expected_points = Input::get('expected_points');
  309. // If user can set the school (that is, if school_id is not undefined)
  310. // set the school id or set to null
  311. if (is_numeric(Input::get('school_id'))) {
  312. if (Input::get('school_id') != 0)
  313. $template->school_id = Input::get('school_id');
  314. elseif (Input::get('school_id') == 0)
  315. $template->school_id = NULL;
  316. }
  317. // If user can set the program (that is, if program_id is not undefined)
  318. // set the program id or set to null
  319. if (is_numeric(Input::get('program_id'))) {
  320. if (Input::get('program_id') != 0)
  321. $template->program_id = Input::get('program_id');
  322. elseif (Input::get('program_id') == 0)
  323. $template->program_id = NULL;
  324. }
  325. switch (Auth::user()->role) {
  326. case 2:
  327. $template->school_id = Auth::user()->school->id;
  328. break;
  329. case 3:
  330. $template->school_id = Auth::user()->programs[0]->school->id;
  331. $template->program_id = Auth::user()->programs[0]->id;
  332. break;
  333. }
  334. $criteria = Input::get('criteria');
  335. $max_score = Input::get('max_score');
  336. $titles = Input::get('titles');
  337. $template->num_scales = count($titles);
  338. $template->max_score = $max_score;
  339. //$division = $max_score / count($scales[0]);
  340. if ($template->save()) {
  341. $templateId = $template->id;
  342. DB::delete("delete from template_criterion where template_id ={$template->id}");
  343. foreach ($criteria as $index => $criterion_id) {
  344. if (!DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, {$index})")) {
  345. Session::flash('status', 'danger');
  346. Session::flash('message', 'Rubric could not be created.');
  347. DB::rollback();
  348. return;
  349. }
  350. }
  351. DB::delete("delete from template_title where template_id = {$template->id}");
  352. foreach ($titles as $index => $text) {
  353. $query = DB::table('titles')
  354. ->where('text', $text)->first();
  355. if ($query) {
  356. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  357. if (!$result) {
  358. Session::flash('status', 'danger');
  359. Session::flash('message', 'Rubric could not be created.');
  360. DB::rollback();
  361. return;
  362. }
  363. } else {
  364. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  365. if (!$result1) {
  366. Session::flash('status', 'danger');
  367. Session::flash('message', 'Rubric could not be created.');
  368. DB::rollback();
  369. return;
  370. }
  371. $query = DB::table('titles')
  372. ->where('text', $text)->first();
  373. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  374. if (!$result) {
  375. Session::flash('status', 'danger');
  376. Session::flash('message', 'Rubric could not be created.');
  377. DB::rollback();
  378. return;
  379. }
  380. }
  381. }
  382. Session::flash('status', 'success');
  383. Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
  384. DB::commit();
  385. } else {
  386. Session::flash('status', 'danger');
  387. Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
  388. DB::commit();
  389. }
  390. }
  391. /**
  392. * Remove the specified resource from storage.
  393. *
  394. * @return Response
  395. */
  396. public function destroy()
  397. {
  398. $template = Template::find(Input::get('id'));
  399. if ($template->delete()) {
  400. Session::flash('status', 'success');
  401. Session::flash('message', 'Rubric deleted.');
  402. } else {
  403. Session::flash('status', 'danger');
  404. Session::flash('message', 'Rubric could not be deleted.');
  405. }
  406. return;
  407. }
  408. public function printview($id)
  409. {
  410. try {
  411. $template = Template::find($id);
  412. $template->titles = DB::table('titles')
  413. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  414. ->where("template_id", $template->id)
  415. ->orderBy('position', 'ASC')
  416. ->lists('text');
  417. $template_criterion = DB::table('template_criterion')
  418. ->join('criteria', 'criteria.id', '=', 'template_criterion.criterion_id')
  419. ->where('template_id', $template->id)
  420. ->orderBy('position', 'ASC')
  421. ->get();
  422. foreach ($template_criterion as $single_crit) {
  423. $single_crit->scales = DB::table('criterion_scale')
  424. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  425. ->where('criterion_id', $single_crit->id)
  426. ->orderBy('position')
  427. ->lists('description');
  428. $single_crit->outcomes = DB::table('outcomes')
  429. ->join('criterion_objective_outcome', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  430. ->where('criterion_id', $single_crit->id)
  431. ->select('name')
  432. ->distinct()
  433. ->lists('name');
  434. }
  435. $title = $template->name;
  436. if ($template->school_id != NULL)
  437. $school = $template->school->name;
  438. else
  439. $school = 'All Schools';
  440. if ($template->program_id != NULL)
  441. $program = $template->program->name;
  442. else
  443. $program = 'All Programs';
  444. return View::make('local.managers.shared.print_rubric', compact('title', 'template_criterion', 'template', 'school', 'program'));
  445. } catch (Exception $e) {
  446. Session::flash('status', 'danger');
  447. Session::flash('message', $e->getMessage());
  448. return Redirect::back();
  449. }
  450. }
  451. }