No Description

TemplatesController.php 19KB

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