No Description

TemplatesController.php 19KB

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