No Description

TemplatesController.php 19KB

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