暂无描述

TemplatesController.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. public function newTemplate_new()
  154. {
  155. $title = "Rubric Builder";
  156. $templates = Template::orderBy('name', 'ASC')->get();
  157. $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->get();
  158. $schools = School::orderBy('name', 'ASC')->get();
  159. $role = Auth::user()->role;
  160. $templates = NULL;
  161. $programs = NULL;
  162. // Returns templates depending on the type of user
  163. if ($role == 1) {
  164. $templates = Template::orderBy('name', 'ASC')->get();
  165. $programs = Program::orderBy('name', 'ASC')->get();
  166. $criteria = Criterion::orderBy('name', 'ASC')->get();
  167. }
  168. else
  169. {
  170. if ($role == 2) {
  171. $programs = Auth::user()->school->programs;
  172. }
  173. if ($role == 3) {
  174. $programs = Auth::user()->programs()->get();
  175. }
  176. $program_ids=array();
  177. foreach($programs as $program)
  178. {
  179. $program_ids[]=$program->id;
  180. }
  181. $criteria = Criterion::whereHas('programs', function ($q) use ($program_ids)
  182. {
  183. $q->whereIn('program_id', $program_ids );
  184. }
  185. )->orderBy('name', 'ASC')->get();
  186. $templates = Template::where('school_id', '=', Auth::user()->programs[0]->school->id)->orWhere('school_id', '=', NULL)
  187. ->orderBy('name', 'ASC')->get();
  188. $criteria_ids=array();
  189. foreach($criteria as $criterion)
  190. {
  191. $criteria_ids[]=$criterion->id;
  192. }
  193. $templates_fuera = Template::whereHas('criteria', function ($q) use ($criteria_ids)
  194. {
  195. $q->whereNotIn('criterion_id', $criteria_ids );
  196. })
  197. ->orderBy('name', 'ASC')->get();
  198. $templates_fuera_ids=array();
  199. foreach($templates_fuera as $tf)
  200. {
  201. $templates_fuera_ids[]=$tf->id;
  202. }
  203. $templates_dentro = Template::whereNotIn('id', $templates_fuera_ids)
  204. ->orderBy('name', 'ASC')->get();
  205. // var_dump(json_encode($templates_dentro));
  206. }
  207. return View::make('local.managers.shared.rubrics_new', compact('title', 'templates_dentro', 'templates_fuera', 'outcomes', 'criteria', 'schools', 'programs', 'criteria_ids'));
  208. }
  209. /**
  210. * Save a new rubric
  211. *
  212. * @return Response
  213. */
  214. public function create()
  215. {
  216. DB::beginTransaction();
  217. $template = new Template;
  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. // If the user is any coordinator, set the school id
  239. // If the user is a program coordinator, also set program id
  240. switch (Auth::user()->role) {
  241. case 2:
  242. $template->school_id = Auth::user()->school->id;
  243. break;
  244. case 3:
  245. $template->school_id = Auth::user()->programs[0]->school->id;
  246. $template->program_id = Auth::user()->programs[0]->id;
  247. break;
  248. }
  249. $criteria = Input::get('criteria');
  250. $max_score = Input::get('max_score');
  251. $titles = Input::get('titles');
  252. $template->num_scales = count($titles);
  253. $template->max_score = $max_score;
  254. if ($template->save()) {
  255. $templateId = $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. foreach ($titles as $index => $text) {
  265. $query = DB::table('titles')
  266. ->where('text', $text)->first();
  267. if ($query) {
  268. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  269. if (!$result) {
  270. Session::flash('status', 'danger');
  271. Session::flash('message', 'Rubric could not be created.');
  272. DB::rollback();
  273. return;
  274. }
  275. } else {
  276. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  277. if (!$result1) {
  278. Session::flash('status', 'danger');
  279. Session::flash('message', 'Rubric could not be created.');
  280. DB::rollback();
  281. return;
  282. }
  283. $query = DB::table('titles')
  284. ->where('text', $text)->first();
  285. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  286. if (!$result) {
  287. Session::flash('status', 'danger');
  288. Session::flash('message', 'Rubric could not be created.');
  289. DB::rollback();
  290. return;
  291. }
  292. }
  293. }
  294. Session::flash('status', 'success');
  295. Session::flash('message', 'Rubric created. You can now select it from the list.');
  296. DB::commit();
  297. return;
  298. } else {
  299. Session::flash('status', 'danger');
  300. Session::flash('message', 'Rubric could not be created.');
  301. DB::rollback();
  302. return;
  303. }
  304. }
  305. /**
  306. * Return a specific template
  307. *
  308. * @return Template
  309. */
  310. public function fetch()
  311. {
  312. $template_info = [];
  313. $template_info['template'] = Template::find(Input::get('id'));
  314. $template_info['criterion'] = DB::table('criteria')
  315. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  316. ->where("template_criterion.template_id", '=', Input::get('id'))
  317. ->get();
  318. foreach ($template_info['criterion'] as $temp_crit) {
  319. $temp_crit->scales = DB::table('scales')
  320. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  321. ->where('criterion_scale.criterion_id', '=', $temp_crit->criterion_id)
  322. ->orderBy('position', 'ASC')
  323. ->get();
  324. $temp_crit->program_ids = json_encode(DB::table('program_criterion')
  325. ->where('criterion_id', $temp_crit->id)
  326. ->lists('program_id'));
  327. $temp_crit->objectives = DB::table('criterion_objective_outcome')
  328. ->join('objectives', 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  329. ->where('criterion_id', $temp_crit->id)
  330. ->select('objectives.*')
  331. ->distinct()
  332. ->lists('text');
  333. $outcomeID = DB::table('criterion_objective_outcome')->where('criterion_id', '=', $temp_crit->criterion_id)
  334. ->lists('outcome_id');
  335. $outcomes = DB::table('outcomes')->whereIn('id', $outcomeID)->get();
  336. $outcomeStr = '';
  337. foreach ($outcomes as $key => $outcome) {
  338. $outcomeStr .= $outcome->name . ', ';
  339. }
  340. $outcomeStr = rtrim($outcomeStr, ',');
  341. $temp_crit->outcomes = $outcomeStr;
  342. }
  343. $template_info['titles'] = DB::table('titles')
  344. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  345. ->where('template_id', Input::get('id'))
  346. ->orderBy('position')
  347. ->get();
  348. Log::info($template_info);
  349. return json_encode($template_info);
  350. }
  351. /**
  352. * Update the specified resource in storage.
  353. *
  354. * @return Response
  355. */
  356. public function update()
  357. {
  358. DB::beginTransaction();
  359. $template = Template::find(Input::get('id'));
  360. Log::info(Input::all());
  361. $template->name = Input::get('name');
  362. $template->is_visible = Input::get('is_visible');
  363. $template->expected_percentage = Input::get('expected_percentage');
  364. $template->expected_points = Input::get('expected_points');
  365. // If user can set the school (that is, if school_id is not undefined)
  366. // set the school id or set to null
  367. if (is_numeric(Input::get('school_id'))) {
  368. if (Input::get('school_id') != 0)
  369. $template->school_id = Input::get('school_id');
  370. elseif (Input::get('school_id') == 0)
  371. $template->school_id = NULL;
  372. }
  373. // If user can set the program (that is, if program_id is not undefined)
  374. // set the program id or set to null
  375. if (is_numeric(Input::get('program_id'))) {
  376. if (Input::get('program_id') != 0)
  377. $template->program_id = Input::get('program_id');
  378. elseif (Input::get('program_id') == 0)
  379. $template->program_id = NULL;
  380. }
  381. switch (Auth::user()->role) {
  382. case 2:
  383. $template->school_id = Auth::user()->school->id;
  384. break;
  385. case 3:
  386. $template->school_id = Auth::user()->programs[0]->school->id;
  387. $template->program_id = Auth::user()->programs[0]->id;
  388. break;
  389. }
  390. $criteria = Input::get('criteria');
  391. $max_score = Input::get('max_score');
  392. $titles = Input::get('titles');
  393. $template->num_scales = count($titles);
  394. $template->max_score = $max_score;
  395. //$division = $max_score / count($scales[0]);
  396. if ($template->save()) {
  397. $templateId = $template->id;
  398. DB::delete("delete from template_criterion where template_id ={$template->id}");
  399. foreach ($criteria as $index => $criterion_id) {
  400. if (!DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, {$index})")) {
  401. Session::flash('status', 'danger');
  402. Session::flash('message', 'Rubric could not be created.');
  403. DB::rollback();
  404. return;
  405. }
  406. }
  407. DB::delete("delete from template_title where template_id = {$template->id}");
  408. foreach ($titles as $index => $text) {
  409. $query = DB::table('titles')
  410. ->where('text', $text)->first();
  411. if ($query) {
  412. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  413. if (!$result) {
  414. Session::flash('status', 'danger');
  415. Session::flash('message', 'Rubric could not be created.');
  416. DB::rollback();
  417. return;
  418. }
  419. } else {
  420. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  421. if (!$result1) {
  422. Session::flash('status', 'danger');
  423. Session::flash('message', 'Rubric could not be created.');
  424. DB::rollback();
  425. return;
  426. }
  427. $query = DB::table('titles')
  428. ->where('text', $text)->first();
  429. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  430. if (!$result) {
  431. Session::flash('status', 'danger');
  432. Session::flash('message', 'Rubric could not be created.');
  433. DB::rollback();
  434. return;
  435. }
  436. }
  437. }
  438. Session::flash('status', 'success');
  439. Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
  440. DB::commit();
  441. } else {
  442. Session::flash('status', 'danger');
  443. Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
  444. DB::commit();
  445. }
  446. }
  447. /**
  448. * Remove the specified resource from storage.
  449. *
  450. * @return Response
  451. */
  452. public function destroy()
  453. {
  454. $template = Template::find(Input::get('id'));
  455. if ($template->delete()) {
  456. Session::flash('status', 'success');
  457. Session::flash('message', 'Rubric deleted.');
  458. } else {
  459. Session::flash('status', 'danger');
  460. Session::flash('message', 'Rubric could not be deleted.');
  461. }
  462. return;
  463. }
  464. public function printview($id)
  465. {
  466. try {
  467. $template = Template::find($id);
  468. $template->titles = DB::table('titles')
  469. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  470. ->where("template_id", $template->id)
  471. ->orderBy('position', 'ASC')
  472. ->lists('text');
  473. $template_criterion = DB::table('template_criterion')
  474. ->join('criteria', 'criteria.id', '=', 'template_criterion.criterion_id')
  475. ->where('template_id', $template->id)
  476. ->orderBy('position', 'ASC')
  477. ->get();
  478. foreach ($template_criterion as $single_crit) {
  479. $single_crit->scales = DB::table('criterion_scale')
  480. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  481. ->where('criterion_id', $single_crit->id)
  482. ->orderBy('position')
  483. ->lists('description');
  484. $single_crit->outcomes = DB::table('outcomes')
  485. ->join('criterion_objective_outcome', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  486. ->where('criterion_id', $single_crit->id)
  487. ->select('name')
  488. ->distinct()
  489. ->lists('name');
  490. }
  491. $title = $template->name;
  492. if ($template->school_id != NULL)
  493. $school = $template->school->name;
  494. else
  495. $school = 'All Schools';
  496. if ($template->program_id != NULL)
  497. $program = $template->program->name;
  498. else
  499. $program = 'All Programs';
  500. return View::make('local.managers.shared.print_rubric', compact('title', 'template_criterion', 'template', 'school', 'program'));
  501. } catch (Exception $e) {
  502. Session::flash('status', 'danger');
  503. Session::flash('message', $e->getMessage());
  504. return Redirect::back();
  505. }
  506. }
  507. }