No Description

TemplatesController.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. Log::info($templates->toSql());
  67. $templates = $templates->get();
  68. break;
  69. }
  70. Log::info($templates);
  71. //$templates = Template::orderBy('name')->get();
  72. return View::make('local.managers.shared.rubric_list', compact('title', 'global_templates', 'schools', 'templates', 'role'));
  73. }
  74. /*public function schoolCoordinatorIndex()
  75. {
  76. $title = 'Rubric List';
  77. $global_templates = Template::whereNull('school_id')->whereNull('program_id')->get();
  78. $schools = School::with('programs.templates')
  79. ->orderBy('name')
  80. ->get();
  81. $templates = Template::orderBy('name')->get();
  82. return View::make('local.managers.admins.rubric_list', compact('title', 'global_templates', 'schools', 'templates'));
  83. }*/
  84. public function show(Template $template)
  85. {
  86. $title = $template->name;
  87. $template->titles = DB::table('titles')
  88. ->join('template_title', 'template_title.title_id', '=', "titles.id")
  89. ->where('template_id', $template->id)
  90. ->orderBy('position', 'ASC')
  91. ->lists('text');
  92. $template->criteria = DB::table('criteria')
  93. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  94. ->where('template_id', $template->id)
  95. ->orderBy('position')
  96. ->get();
  97. foreach ($template->criteria as $criterion) {
  98. $criterion->scales = DB::table('criterion_scale')
  99. ->join('scales', 'scales.id', '=', 'criterion_scale.scale_id')
  100. ->where('criterion_id', $criterion->criterion_id)
  101. ->orderBy('position')
  102. ->lists('description');
  103. $criterion->outcomes = DB::table('criterion_objective_outcome as cobo')
  104. ->join('outcomes', 'outcomes.id', '=', 'cobo.outcome_id')
  105. ->where('cobo.criterion_id', $criterion->criterion_id)
  106. ->select('outcomes.*')
  107. ->distinct()
  108. ->get();
  109. }
  110. return View::make('local.managers.admins.view_template', compact('template', 'title'));
  111. }
  112. public function onLoadFetch()
  113. {
  114. $json_to_send = [];
  115. $template_id = Input::get('id');
  116. $json_to_send["criteria"] = DB::table("criteria")->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  117. ->where("template_criterion.template_id", '=', $template_id)
  118. ->get();
  119. Log::info($json_to_send["criteria"]);
  120. foreach ($json_to_send['criteria'] as $criteria) {
  121. $json_to_send['scales'][$criteria->criterion_id] = DB::table("scales")
  122. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  123. ->where("criterion_scale.criterion_id", '=', $criteria->criterion_id)->orderBy('position', 'ASC')->get();
  124. }
  125. return json_encode($json_to_send);
  126. }
  127. /**
  128. * Show the form for creating a new rubric
  129. *
  130. * @return Response
  131. */
  132. public function newTemplate()
  133. {
  134. $title = "Rubric Builder";
  135. $templates = Template::orderBy('name', 'ASC')->get();
  136. $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->get();
  137. $criteria = Criterion::orderBy('name', 'ASC')->get();
  138. $schools = School::orderBy('name', 'ASC')->get();
  139. $role = Auth::user()->role;
  140. $templates = NULL;
  141. $programs = NULL;
  142. // Returns templates depending on the type of user
  143. if ($role == 1) {
  144. $templates = Template::orderBy('name', 'ASC')->get();
  145. $programs = Program::orderBy('name', 'ASC')->get();
  146. } elseif ($role == 2) {
  147. $templates = Template::where('school_id', '=', Auth::user()->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  148. $programs = Auth::user()->school->programs;
  149. } elseif ($role == 3) {
  150. $templates = Template::where('school_id', '=', Auth::user()->programs[0]->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  151. $programs = Auth::user()->programs()->get();
  152. }
  153. return View::make('local.managers.shared.rubrics', compact('title', 'templates', 'outcomes', 'criteria', 'schools', 'programs'));
  154. }
  155. public function newTemplate_new()
  156. {
  157. $title = "Rubric Builder";
  158. $templates = Template::orderBy('name', 'ASC')->get();
  159. $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->get();
  160. $schools = School::orderBy('name', 'ASC')->get();
  161. $role = Auth::user()->role;
  162. $templates = NULL;
  163. $programs = NULL;
  164. // Returns templates depending on the type of user
  165. if ($role == 1) {
  166. $templates = Template::orderBy('name', 'ASC')->get();
  167. $programs = Program::orderBy('name', 'ASC')->get();
  168. $criteria = Criterion::orderBy('name', 'ASC')->get();
  169. } else {
  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. $program_ids[] = $program->id;
  179. }
  180. $criteria = Criterion::whereHas(
  181. 'programs',
  182. function ($q) use ($program_ids) {
  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. $criteria_ids[] = $criterion->id;
  191. }
  192. $templates_fuera = Template::whereHas('criteria', function ($q) use ($criteria_ids) {
  193. $q->whereNotIn('criterion_id', $criteria_ids);
  194. })
  195. ->orderBy('name', 'ASC')->get();
  196. $templates_fuera_ids = array();
  197. foreach ($templates_fuera as $tf) {
  198. $templates_fuera_ids[] = $tf->id;
  199. }
  200. $templates_dentro = Template::whereNotIn('id', $templates_fuera_ids)
  201. ->orderBy('name', 'ASC')->get();
  202. // var_dump(json_encode($templates_dentro));
  203. }
  204. return View::make('local.managers.shared.rubrics_new', compact('title', 'templates_dentro', 'templates_fuera', 'outcomes', 'criteria', 'schools', 'programs', 'criteria_ids'));
  205. }
  206. /**
  207. * Save a new rubric
  208. *
  209. * @return Response
  210. */
  211. public function create()
  212. {
  213. DB::beginTransaction();
  214. $template = new Template;
  215. $template->name = Input::get('name');
  216. $template->is_visible = Input::get('is_visible');
  217. $template->expected_percentage = Input::get('expected_percentage');
  218. $template->expected_points = Input::get('expected_points');
  219. // If user can set the school (that is, if school_id is not undefined)
  220. // set the school id or set to null
  221. if (is_numeric(Input::get('school_id'))) {
  222. if (Input::get('school_id') != 0)
  223. $template->school_id = Input::get('school_id');
  224. elseif (Input::get('school_id') == 0)
  225. $template->school_id = NULL;
  226. }
  227. // If user can set the program (that is, if program_id is not undefined)
  228. // set the program id or set to null
  229. if (is_numeric(Input::get('program_id'))) {
  230. if (Input::get('program_id') != 0)
  231. $template->program_id = Input::get('program_id');
  232. elseif (Input::get('program_id') == 0)
  233. $template->program_id = NULL;
  234. }
  235. // If the user is any coordinator, set the school id
  236. // If the user is a program coordinator, also set program id
  237. switch (Auth::user()->role) {
  238. case 2:
  239. $template->school_id = Auth::user()->school->id;
  240. break;
  241. //TODO esto yo no lo hice
  242. case 3:
  243. $template->school_id = Auth::user()->programs[0]->school->id;
  244. //Comenté esto porque siempre va a estar el program_id
  245. //TODO
  246. if ($template->program_id == NULL)
  247. $template->program_id = Auth::user()->programs[0]->id;
  248. break;
  249. }
  250. $criteria = Input::get('criteria');
  251. $max_score = Input::get('max_score');
  252. $titles = Input::get('titles');
  253. $template->num_scales = count($titles);
  254. $template->max_score = $max_score;
  255. if ($template->save()) {
  256. $templateId = $template->id;
  257. foreach ($criteria as $index => $criterion_id) {
  258. if (!(DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, '{$index}')"))) {
  259. Session::flash('status', 'danger');
  260. Session::flash('message', 'Rubric could not be created.');
  261. DB::rollback();
  262. return;
  263. }
  264. }
  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 created. You can now select it from the list.');
  297. DB::commit();
  298. return;
  299. } else {
  300. Session::flash('status', 'danger');
  301. Session::flash('message', 'Rubric could not be created.');
  302. DB::rollback();
  303. return;
  304. }
  305. }
  306. /**
  307. * Return a specific template
  308. *
  309. * @return Template
  310. */
  311. public function fetch()
  312. {
  313. $template_info = [];
  314. $template_info['template'] = Template::find(Input::get('id'));
  315. $template_info['criterion'] = DB::table('criteria')
  316. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  317. ->where("template_criterion.template_id", '=', Input::get('id'))
  318. ->get();
  319. Log::info(json_encode($template_info['criterion']));
  320. // Log::info(($temp_crit->program_ids));
  321. foreach ($template_info['criterion'] as $temp_crit) {
  322. $temp_crit->scales = DB::table('scales')
  323. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  324. ->where('criterion_scale.criterion_id', '=', $temp_crit->criterion_id)
  325. ->orderBy('position', 'ASC')
  326. ->get();
  327. $temp_crit->program_ids = json_encode(DB::table('program_criterion')
  328. ->where('criterion_id', $temp_crit->id)
  329. ->lists('program_id'));
  330. Log::info("ee" . json_encode($temp_crit->program_ids));
  331. $temp_crit->objectives = DB::table('criterion_objective_outcome')
  332. ->join('objectives', 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  333. ->where('criterion_id', $temp_crit->id)
  334. ->select('objectives.*')
  335. ->distinct()
  336. ->lists('text');
  337. $outcomeID = DB::table('criterion_objective_outcome')->where('criterion_id', '=', $temp_crit->criterion_id)
  338. ->lists('outcome_id');
  339. $outcomes = DB::table('outcomes')->whereIn('id', $outcomeID)->get();
  340. $outcomeStr = '';
  341. foreach ($outcomes as $key => $outcome) {
  342. $outcomeStr .= $outcome->name . ', ';
  343. }
  344. $outcomeStr = rtrim($outcomeStr, ',');
  345. $temp_crit->outcomes = $outcomeStr;
  346. }
  347. Log::info("ee2" . json_encode($template_info));
  348. $template_info['titles'] = DB::table('titles')
  349. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  350. ->where('template_id', Input::get('id'))
  351. ->orderBy('position')
  352. ->get();
  353. // Log::info(json_encode($template_info));
  354. return json_encode($template_info);
  355. }
  356. /**
  357. * Update the specified resource in storage.
  358. *
  359. * @return Response
  360. */
  361. public function update()
  362. {
  363. DB::beginTransaction();
  364. $template = Template::find(Input::get('id'));
  365. Log::info(Input::all());
  366. $template->name = Input::get('name');
  367. $template->is_visible = Input::get('is_visible');
  368. $template->expected_percentage = Input::get('expected_percentage');
  369. $template->expected_points = Input::get('expected_points');
  370. // If user can set the school (that is, if school_id is not undefined)
  371. // set the school id or set to null
  372. if (is_numeric(Input::get('school_id'))) {
  373. if (Input::get('school_id') != 0)
  374. $template->school_id = Input::get('school_id');
  375. elseif (Input::get('school_id') == 0)
  376. $template->school_id = NULL;
  377. }
  378. // If user can set the program (that is, if program_id is not undefined)
  379. // set the program id or set to null
  380. if (is_numeric(Input::get('program_id'))) {
  381. if (Input::get('program_id') != 0)
  382. $template->program_id = Input::get('program_id');
  383. elseif (Input::get('program_id') == 0)
  384. $template->program_id = NULL;
  385. }
  386. switch (Auth::user()->role) {
  387. case 2:
  388. $template->school_id = Auth::user()->school->id;
  389. break;
  390. case 3:
  391. $template->school_id = Auth::user()->programs[0]->school->id;
  392. if ($template->program_id == NULL)
  393. $template->program_id = Auth::user()->programs[0]->id;
  394. break;
  395. }
  396. $criteria = Input::get('criteria');
  397. $max_score = Input::get('max_score');
  398. $titles = Input::get('titles');
  399. $template->num_scales = count($titles);
  400. $template->max_score = $max_score;
  401. //$division = $max_score / count($scales[0]);
  402. if ($template->save()) {
  403. $templateId = $template->id;
  404. DB::delete("delete from template_criterion where template_id ={$template->id}");
  405. foreach ($criteria as $index => $criterion_id) {
  406. if (!DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, {$index})")) {
  407. Session::flash('status', 'danger');
  408. Session::flash('message', 'Rubric could not be created.');
  409. DB::rollback();
  410. return;
  411. }
  412. }
  413. DB::delete("delete from template_title where template_id = {$template->id}");
  414. foreach ($titles as $index => $text) {
  415. $query = DB::table('titles')
  416. ->where('text', $text)->first();
  417. if ($query) {
  418. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  419. if (!$result) {
  420. Session::flash('status', 'danger');
  421. Session::flash('message', 'Rubric could not be created.');
  422. DB::rollback();
  423. return;
  424. }
  425. } else {
  426. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  427. if (!$result1) {
  428. Session::flash('status', 'danger');
  429. Session::flash('message', 'Rubric could not be created.');
  430. DB::rollback();
  431. return;
  432. }
  433. $query = DB::table('titles')
  434. ->where('text', $text)->first();
  435. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  436. if (!$result) {
  437. Session::flash('status', 'danger');
  438. Session::flash('message', 'Rubric could not be created.');
  439. DB::rollback();
  440. return;
  441. }
  442. }
  443. }
  444. Session::flash('status', 'success');
  445. Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
  446. DB::commit();
  447. } else {
  448. Session::flash('status', 'danger');
  449. Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
  450. DB::commit();
  451. }
  452. }
  453. /**
  454. * Remove the specified resource from storage.
  455. *
  456. * @return Response
  457. */
  458. public function destroy()
  459. {
  460. $template = Template::find(Input::get('id'));
  461. if ($template->delete()) {
  462. Session::flash('status', 'success');
  463. Session::flash('message', 'Rubric deleted.');
  464. } else {
  465. Session::flash('status', 'danger');
  466. Session::flash('message', 'Rubric could not be deleted.');
  467. }
  468. return;
  469. }
  470. public function printview($id)
  471. {
  472. try {
  473. $template = Template::find($id);
  474. $template->titles = DB::table('titles')
  475. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  476. ->where("template_id", $template->id)
  477. ->orderBy('position', 'ASC')
  478. ->lists('text');
  479. $template_criterion = DB::table('template_criterion')
  480. ->join('criteria', 'criteria.id', '=', 'template_criterion.criterion_id')
  481. ->where('template_id', $template->id)
  482. ->orderBy('position', 'ASC')
  483. ->get();
  484. foreach ($template_criterion as $single_crit) {
  485. $single_crit->scales = DB::table('criterion_scale')
  486. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  487. ->where('criterion_id', $single_crit->id)
  488. ->orderBy('position')
  489. ->lists('description');
  490. $single_crit->outcomes = DB::table('outcomes')
  491. ->join('criterion_objective_outcome', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  492. ->where('criterion_id', $single_crit->id)
  493. ->select('name')
  494. ->distinct()
  495. ->lists('name');
  496. }
  497. $title = $template->name;
  498. if ($template->school_id != NULL)
  499. $school = $template->school->name;
  500. else
  501. $school = 'All Schools';
  502. if ($template->program_id != NULL)
  503. $program = $template->program->name;
  504. else
  505. $program = 'All Programs';
  506. return View::make('local.managers.shared.print_rubric', compact('title', 'template_criterion', 'template', 'school', 'program'));
  507. } catch (Exception $e) {
  508. Session::flash('status', 'danger');
  509. Session::flash('message', $e->getMessage());
  510. return Redirect::back();
  511. }
  512. }
  513. }