Sin descripción

TemplatesController.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. <?php
  2. class TemplatesController extends \BaseController
  3. {
  4. /**
  5. * List all templates, grouped by program
  6. */
  7. public function profShow(Template $template)
  8. {
  9. return $this->show($template);
  10. }
  11. public function ProfIndex()
  12. {
  13. $title = 'Rubric List';
  14. $role = Auth::user()->role;
  15. $program_id = DB::table("program_user")
  16. ->where('user_id', Auth::user()->id)
  17. ->lists('program_id')[0];
  18. $school_id = DB::table('programs')
  19. ->where('id', $program_id)
  20. ->lists('school_id')[0];
  21. $templates = Template::orderBy('name')
  22. ->whereNull("school_id")
  23. ->orWhere(function ($query) use (&$school_id) {
  24. $query->where('school_id', $school_id)
  25. ->whereNull('program_id');
  26. })
  27. ->orWhere(function ($query) use (&$program_id, &$school_id) {
  28. $query->where('school_id', $school_id)
  29. ->where('program_id', $program_id);
  30. })
  31. ->get();
  32. return View::make('local.managers.shared.rubric_list', compact('title', 'global_templates', 'schools', 'templates', 'role'));
  33. }
  34. public function index()
  35. {
  36. $title = 'Rubric List';
  37. $global_templates = Template::whereNull('school_id')->whereNull('program_id')->get();
  38. $schools = School::with('programs.templates')
  39. ->orderBy('name')
  40. ->get();
  41. $role = Auth::user()->role;
  42. switch ($role) {
  43. case 1:
  44. $templates = Template::orderBy('name')->get();
  45. break;
  46. case 2:
  47. $templates = Template::orderBy('name')
  48. ->whereNull("school_id")
  49. ->orWhere('school_id', Auth::user()->school_id)
  50. ->get();
  51. break;
  52. case 3:
  53. case 4:
  54. $program_ids = DB::table("program_user")
  55. ->where('user_id', Auth::user()->id)
  56. ->lists('program_id');
  57. $school_ids = DB::table('programs')
  58. ->whereIn('id', $program_ids)
  59. ->lists('school_id');
  60. $templates = Template::orderBy('name')
  61. ->whereNull("school_id")
  62. ->orWhere(function ($query) use (&$school_ids) {
  63. $query->whereIn('school_id', $school_ids)
  64. ->whereNull('program_id');
  65. })
  66. ->orWhere(function ($query) use (&$program_ids, &$school_ids) {
  67. $query->where('school_id', $school_ids)
  68. ->where('program_id', $program_ids);
  69. });
  70. Log::info($templates->toSql());
  71. $templates = $templates->get();
  72. break;
  73. }
  74. Log::info($templates);
  75. //$templates = Template::orderBy('name')->get();
  76. return View::make('local.managers.shared.rubric_list', compact('title', 'global_templates', 'schools', 'templates', 'role'));
  77. }
  78. /*public function schoolCoordinatorIndex()
  79. {
  80. $title = 'Rubric List';
  81. $global_templates = Template::whereNull('school_id')->whereNull('program_id')->get();
  82. $schools = School::with('programs.templates')
  83. ->orderBy('name')
  84. ->get();
  85. $templates = Template::orderBy('name')->get();
  86. return View::make('local.managers.admins.rubric_list', compact('title', 'global_templates', 'schools', 'templates'));
  87. }*/
  88. public function show(Template $template)
  89. {
  90. $title = $template->name;
  91. $template->titles = DB::table('titles')
  92. ->join('template_title', 'template_title.title_id', '=', "titles.id")
  93. ->where('template_id', $template->id)
  94. ->orderBy('position', 'ASC')
  95. ->lists('text');
  96. $template->criteria = DB::table('criteria')
  97. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  98. ->where('template_id', $template->id)
  99. ->select('criteria.*', 'criteria.id as criterion_id', 'template_criterion.id as template_criterion_id')
  100. ->orderBy('position')
  101. ->get();
  102. foreach ($template->criteria as $criterion) {
  103. $criterion->scales = DB::table('criterion_scale')
  104. ->join('scales', 'scales.id', '=', 'criterion_scale.scale_id')
  105. ->where('criterion_id', $criterion->criterion_id)
  106. ->orderBy('position')
  107. ->lists('description');
  108. $criterion->outcomes = DB::table('criterion_objective_outcome as cobo')
  109. ->join('outcomes', 'outcomes.id', '=', 'cobo.outcome_id')
  110. ->where('cobo.criterion_id', $criterion->criterion_id)
  111. ->select('outcomes.*')
  112. ->distinct()
  113. ->get();
  114. }
  115. $can_edit = false;
  116. $user = Auth::user();
  117. switch ($user->role) {
  118. case 1:
  119. $can_edit = true;
  120. break;
  121. case 2:
  122. if ($template->school_id != NULL && $user->school_id == $template->school_id)
  123. $can_edit = true;
  124. break;
  125. case 3:
  126. if ($template->program_id != NULL && in_array($template->program_id, $user->programs->lists('id')))
  127. $can_edit = true;
  128. break;
  129. default:
  130. if ($template->user_id == $user->id)
  131. $can_edit = true;
  132. break;
  133. }
  134. return View::make('local.managers.admins.view_template', compact('template', 'title', 'can_edit'));
  135. }
  136. public function onLoadFetch()
  137. {
  138. $json_to_send = [];
  139. $template_id = Input::get('id');
  140. $json_to_send["criteria"] = DB::table("criteria")->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  141. ->where("template_criterion.template_id", '=', $template_id)
  142. ->get();
  143. Log::info($json_to_send["criteria"]);
  144. foreach ($json_to_send['criteria'] as $criteria) {
  145. $json_to_send['scales'][$criteria->criterion_id] = DB::table("scales")
  146. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  147. ->where("criterion_scale.criterion_id", '=', $criteria->criterion_id)->orderBy('position', 'ASC')->get();
  148. }
  149. return json_encode($json_to_send);
  150. }
  151. /**
  152. * Show the form for creating a new rubric
  153. *
  154. * @return Response
  155. */
  156. public function newTemplate()
  157. {
  158. $title = "Rubric Builder";
  159. $templates = Template::orderBy('name', 'ASC')->get();
  160. $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->get();
  161. $criteria = Criterion::orderBy('name', 'ASC')->get();
  162. $schools = School::orderBy('name', 'ASC')->get();
  163. $role = Auth::user()->role;
  164. $templates = NULL;
  165. $programs = NULL;
  166. // Returns templates depending on the type of user
  167. if ($role == 1) {
  168. $templates = Template::orderBy('name', 'ASC')->get();
  169. $programs = Program::orderBy('name', 'ASC')->get();
  170. $program_ids = Program::orderBy('name', 'ASC')->lists('id');
  171. } elseif ($role == 2) {
  172. $templates = Template::where('school_id', '=', Auth::user()->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  173. $programs = Auth::user()->school->programs;
  174. $program_ids = Auth::user()->school->programs->lists('id');
  175. } elseif ($role == 3) {
  176. $templates = Template::where('school_id', '=', Auth::user()->programs[0]->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
  177. $programs = Auth::user()->programs()->get();
  178. $program_ids = Auth::user()->programs()->lists('id');
  179. } else {
  180. $templates = Template::where('user_id', Auth::user()->id);
  181. $programs = Auth::user()->programs()->get();
  182. $program_ids = Auth::user()->programs()->lists('id');
  183. }
  184. $annual_plans = DB::table('annual_plans')
  185. ->join('programs', 'programs.id', '=', 'program_id')
  186. ->join('annual_cycles', 'annual_cycle_id', '=', 'annual_cycles.id')
  187. ->select("programs.*", "annual_cycles.*", 'annual_plans.id as annual_plan_id')
  188. ->whereIn("program_id", $program_ids)
  189. ->orderBy('annual_cycle_id', 'DESC')
  190. ->orderBy('programs.name', 'ASC')
  191. ->get();
  192. Log::info("NOus avons");
  193. Log::info($annual_plans);
  194. return View::make('local.managers.shared.rubrics', compact('title', 'templates', 'outcomes', 'criteria', 'schools', 'programs'));
  195. }
  196. public function newTemplateProf()
  197. {
  198. return $this->newTemplate_new();
  199. }
  200. public function newTemplate_new()
  201. {
  202. $title = "Rubric Builder";
  203. $templates = Template::orderBy('name', 'ASC')->get();
  204. $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->get();
  205. $schools = School::orderBy('name', 'ASC')->get();
  206. $role = Auth::user()->role;
  207. $templates = NULL;
  208. $programs = NULL;
  209. // Returns templates depending on the type of user
  210. if ($role == 1) {
  211. $templates = Template::orderBy('name', 'ASC')->get();
  212. $programs = Program::orderBy('name', 'ASC')->get();
  213. $criteria = Criterion::orderBy('name', 'ASC')->get();
  214. $school_id_user = NULL;
  215. $program_ids = Program::orderBy('name', 'ASC')->lists('id');
  216. } else {
  217. if ($role == 2) {
  218. $programs = Auth::user()->school->programs;
  219. $school_id_user = Auth::user()->school_id;
  220. } else if ($role == 3) {
  221. $programs = Auth::user()->programs()->get();
  222. $school_id_user = Auth::user()->programs[0]->school->id;
  223. } else if ($role == 4) {
  224. $programs = Auth::user()->programs()->get();
  225. $school_id_user = Auth::user()->programs[0]->school->id;
  226. }
  227. Log::info(json_encode($school_id_user));
  228. $program_ids = array();
  229. foreach ($programs as $program) {
  230. $program_ids[] = $program->id;
  231. }
  232. $criteria = Criterion::whereHas(
  233. 'programs',
  234. function ($q) use ($program_ids) {
  235. $q->whereIn('program_id', $program_ids);
  236. }
  237. )
  238. ->whereHas(
  239. 'getObjectivesAttribute',
  240. function ($q) {
  241. $q->where('objective_id', '!=', 0);
  242. }
  243. )
  244. ->orderBy('name', 'ASC')->get();
  245. }
  246. // Log::info("aqui".(count($criteria)));
  247. // Log::info(json_encode(count($criteria)));
  248. $templates = Template::where('school_id', '=', $school_id_user)->orWhere('school_id', '=', NULL)
  249. ->orderBy('name', 'ASC')->get();
  250. $criteria_ids = array();
  251. foreach ($criteria as $criterion) {
  252. $criteria_ids[] = $criterion->id;
  253. }
  254. $templates_fuera = Template::whereHas('criteria', function ($q) use ($criteria_ids) {
  255. $q->whereNotIn('criterion_id', $criteria_ids);
  256. })
  257. ->orderBy('name', 'ASC')->get();
  258. $templates_fuera_ids = array();
  259. foreach ($templates_fuera as $tf) {
  260. $templates_fuera_ids[] = $tf->id;
  261. }
  262. // Log::info(json_encode($templates_fuera_ids));
  263. // exit();
  264. $templates_dentro = Template::whereNotIn('id', $templates_fuera_ids)
  265. ->orderBy('name', 'ASC')->get();
  266. $annual_plans = DB::table('annual_cycle')
  267. ->join('annual_plans', 'annual_cycle_id', '=', 'annual_cycle.id')
  268. ->join('programs', 'programs.id', '=', 'annual_plans.program_id')
  269. ->join('typ_semester_outcome', function ($j) {
  270. $j->on('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_start')
  271. ->orOn('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_end');
  272. })
  273. ->join('typ_program', function ($j) {
  274. $j->on('typ_program.program_id', '=', 'annual_plans.program_id')
  275. ->on('typ_program.id', '=', 'typ_semester_outcome.typ_program_id');
  276. })
  277. ->whereIn('annual_plans.program_id', $program_ids)
  278. ->orderBy('annual_cycle_id', 'DESC')
  279. ->orderBy('programs.name', 'ASC')
  280. ->select("programs.*", 'programs.name as program_name', "annual_cycle.*", 'annual_plans.id as annual_plan_id')
  281. ->distinct()
  282. ->get();
  283. /*$annual_plans = DB::table('annual_plans')
  284. ->join('programs', 'programs.id', '=', 'program_id')
  285. ->join('annual_cycle', 'annual_cycle_id', '=', 'annual_cycle.id')
  286. ->select("programs.*", 'programs.name as program_name', "annual_cycle.*", 'annual_plans.id as annual_plan_id')
  287. ->whereIn("program_id", $program_ids)
  288. ->orderBy('annual_cycle_id', 'DESC')
  289. ->orderBy('programs.name', 'ASC')
  290. ->get();
  291. Log::info("NOus avons");
  292. Log::info($annual_plans);*/
  293. /*$templates_dentro = Template::join('template_criterion', 'template_criterion.template_id', '=', 'templates.id')
  294. ->join('criterion_objective_outcome as cobo', 'cobo.criterion_id', '=', 'template_criterion.criterion_id')
  295. ->where('objective_id', '<>', '0')
  296. ->where(function ($query) use ($school_id_user) {
  297. $query->where('school_id', $school_id_user)
  298. ->orWhere('school_id', '=', NULL);
  299. })
  300. // ->orWhere('school_id', '=', NULL)
  301. ->orderBy('name', 'ASC')
  302. ->groupBy('templates.id')
  303. ->select("templates.*")
  304. ->get();*/
  305. // if(!isset($templates_dentro))$templates_dentro=
  306. // var_dump(json_encode($templates_dentro));
  307. // }
  308. $templates_fuera = array();
  309. return View::make('local.managers.shared.rubrics_new', compact('annual_plans', 'title', 'templates_dentro', 'templates_fuera', 'outcomes', 'criteria', 'schools', 'programs', 'criteria_ids'));
  310. }
  311. public function fetchObjectivesForTemplate()
  312. {
  313. $criteria_ids = Input::get("allCriteria");
  314. //program_id only to solve for the rubric builder
  315. $template_id = Input::get('template_id');
  316. if (isset($template_id)) {
  317. $template = Template::find($template_id);
  318. if ($template->school_id == null) {
  319. $program_ids = DB::table('programs')
  320. ->lists('programs.id');
  321. } else if ($template->program_id == null) {
  322. $program_ids = DB::table('programs')
  323. ->where('school_id', $template->school_id)->lists('programs.id');
  324. } else {
  325. $program_ids = DB::table('programs')
  326. ->where('program_id', $template->program_id)->lists('programs.id');
  327. }
  328. } else {
  329. $role = Auth::user()->role;
  330. switch ($role) {
  331. case 1:
  332. $program_ids = DB::table('programs')
  333. ->lists('programs.id');
  334. break;
  335. case 2:
  336. $program_ids = DB::table('programs')
  337. ->where('school_id', Auth::user()->school_id)
  338. ->lists('programs.id');
  339. break;
  340. case 3:
  341. case 4:
  342. $program_ids = Auth::user()->programs->lists('id');
  343. break;
  344. default:
  345. # code...
  346. break;
  347. }
  348. }
  349. foreach ($criteria_ids as $crit_id) {
  350. $crit = Criterion::where('id', $crit_id)->first();
  351. if (!isset($crit)) {
  352. return "NO pa";
  353. }
  354. $crit->all_objectives = DB::table("objectives")
  355. ->join("criterion_objective_outcome", 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  356. ->join('program_criterion_objective_outcome as poco', 'poco.cri_obj_out_id', '=', 'criterion_objective_outcome.id')
  357. ->where('criterion_id', $crit->id)
  358. ->whereIn("program_id", $program_ids)
  359. ->select('objectives.*')
  360. ->groupBy('objectives.id')
  361. ->get();
  362. $criteria[] = $crit;
  363. }
  364. return $criteria;
  365. }
  366. /**
  367. * Save a new rubric
  368. *
  369. * @return Response
  370. */
  371. public function create()
  372. {
  373. DB::beginTransaction();
  374. $template = new Template;
  375. $template->name = Input::get('name');
  376. $template->is_visible = Input::get('is_visible');
  377. $template->expected_percentage = Input::get('expected_percentage');
  378. $template->expected_points = Input::get('expected_points');
  379. // If user can set the school (that is, if school_id is not undefined)
  380. // set the school id or set to null
  381. if (is_numeric(Input::get('school_id'))) {
  382. if (Input::get('school_id') != 0)
  383. $template->school_id = Input::get('school_id');
  384. elseif (Input::get('school_id') == 0)
  385. $template->school_id = NULL;
  386. }
  387. // If user can set the program (that is, if program_id is not undefined)
  388. // set the program id or set to null
  389. if (is_numeric(Input::get('program_id'))) {
  390. if (Input::get('program_id') != 0)
  391. $template->program_id = Input::get('program_id');
  392. elseif (Input::get('program_id') == 0)
  393. $template->program_id = NULL;
  394. }
  395. // If the user is any coordinator, set the school id
  396. // If the user is a program coordinator, also set program id
  397. switch (Auth::user()->role) {
  398. case 2:
  399. $template->school_id = Auth::user()->school->id;
  400. break;
  401. //TODO esto yo no lo hice
  402. case 3:
  403. $template->school_id = Auth::user()->programs[0]->school->id;
  404. //Comenté esto porque siempre va a estar el program_id
  405. //TODO
  406. if ($template->program_id == NULL)
  407. $template->program_id = Auth::user()->programs[0]->id;
  408. break;
  409. }
  410. $criteria = Input::get('criteria');
  411. $max_score = Input::get('max_score');
  412. $titles = Input::get('titles');
  413. $template->num_scales = count($titles);
  414. $template->max_score = $max_score;
  415. if ($template->save()) {
  416. $templateId = $template->id;
  417. foreach ($criteria as $index => $criterion_id) {
  418. if (!(DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, '{$index}')"))) {
  419. Session::flash('status', 'danger');
  420. Session::flash('message', 'Rubric could not be created.');
  421. DB::rollback();
  422. return;
  423. }
  424. }
  425. foreach ($titles as $index => $text) {
  426. $query = DB::table('titles')
  427. ->where('text', $text)->first();
  428. if ($query) {
  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. } else {
  437. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  438. if (!$result1) {
  439. Session::flash('status', 'danger');
  440. Session::flash('message', 'Rubric could not be created.');
  441. DB::rollback();
  442. return;
  443. }
  444. $query = DB::table('titles')
  445. ->where('text', $text)->first();
  446. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  447. if (!$result) {
  448. Session::flash('status', 'danger');
  449. Session::flash('message', 'Rubric could not be created.');
  450. DB::rollback();
  451. return;
  452. }
  453. }
  454. }
  455. Session::flash('status', 'success');
  456. Session::flash('message', 'Rubric created. You can now select it from the list.');
  457. DB::commit();
  458. return;
  459. } else {
  460. Session::flash('status', 'danger');
  461. Session::flash('message', 'Rubric could not be created.');
  462. DB::rollback();
  463. return;
  464. }
  465. }
  466. /**
  467. * Return a specific template
  468. *
  469. * @return Template
  470. */
  471. public function fetch()
  472. {
  473. $template_info = [];
  474. $template_info['template'] = Template::find(Input::get('id'));
  475. $template_info['criterion'] = DB::table('criteria')
  476. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  477. ->where("template_criterion.template_id", '=', Input::get('id'))
  478. ->get();
  479. Log::info(json_encode($template_info['criterion']));
  480. // Log::info(($temp_crit->program_ids));
  481. foreach ($template_info['criterion'] as $temp_crit) {
  482. $temp_crit->scales = DB::table('scales')
  483. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  484. ->where('criterion_scale.criterion_id', '=', $temp_crit->criterion_id)
  485. ->orderBy('position', 'ASC')
  486. ->get();
  487. $temp_crit->program_ids = json_encode(DB::table('program_criterion_objective_outcome')
  488. ->join('criterion_objective_outcome', 'criterion_objective_outcome.id', '=', 'program_criterion_objective_outcome.cri_obj_out_id')
  489. ->where('criterion_objective_outcome.criterion_id', $temp_crit->id)
  490. ->select('program_criterion_objective_outcome.program_id')
  491. ->distinct()
  492. ->lists('program_criterion_objective_outcome.program_id'));
  493. Log::info("ee" . json_encode($temp_crit->program_ids));
  494. $temp_crit->objectives = DB::table('criterion_objective_outcome')
  495. ->join('objectives', 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  496. ->where('criterion_id', $temp_crit->id)
  497. ->select('objectives.*')
  498. ->distinct()
  499. ->lists('text');
  500. $outcomeID = DB::table('criterion_objective_outcome')->where('criterion_id', '=', $temp_crit->criterion_id)
  501. ->lists('outcome_id');
  502. $outcomes = DB::table('outcomes')->whereIn('id', $outcomeID)->get();
  503. $outcomeStr = '';
  504. foreach ($outcomes as $key => $outcome) {
  505. $outcomeStr .= $outcome->name . ', ';
  506. }
  507. $outcomeStr = rtrim($outcomeStr, ',');
  508. $temp_crit->outcomes = $outcomeStr;
  509. }
  510. Log::info("ee2" . json_encode($template_info));
  511. $template_info['titles'] = DB::table('titles')
  512. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  513. ->where('template_id', Input::get('id'))
  514. ->orderBy('position')
  515. ->get();
  516. // Log::info(json_encode($template_info));
  517. return json_encode($template_info);
  518. }
  519. /**
  520. * Update the specified resource in storage.
  521. *
  522. * @return Response
  523. */
  524. public function update()
  525. {
  526. DB::beginTransaction();
  527. $template = Template::find(Input::get('id'));
  528. Log::info(Input::all());
  529. $template->name = Input::get('name');
  530. $template->is_visible = Input::get('is_visible');
  531. $template->expected_percentage = Input::get('expected_percentage');
  532. $template->expected_points = Input::get('expected_points');
  533. // If user can set the school (that is, if school_id is not undefined)
  534. // set the school id or set to null
  535. if (is_numeric(Input::get('school_id'))) {
  536. if (Input::get('school_id') != 0)
  537. $template->school_id = Input::get('school_id');
  538. elseif (Input::get('school_id') == 0)
  539. $template->school_id = NULL;
  540. }
  541. // If user can set the program (that is, if program_id is not undefined)
  542. // set the program id or set to null
  543. if (is_numeric(Input::get('program_id'))) {
  544. if (Input::get('program_id') != 0)
  545. $template->program_id = Input::get('program_id');
  546. elseif (Input::get('program_id') == 0)
  547. $template->program_id = NULL;
  548. }
  549. switch (Auth::user()->role) {
  550. case 2:
  551. $template->school_id = Auth::user()->school->id;
  552. break;
  553. case 3:
  554. $template->school_id = Auth::user()->programs[0]->school->id;
  555. if ($template->program_id == NULL)
  556. $template->program_id = Auth::user()->programs[0]->id;
  557. break;
  558. }
  559. $criteria = Input::get('criteria');
  560. $max_score = Input::get('max_score');
  561. $titles = Input::get('titles');
  562. $template->num_scales = count($titles);
  563. $template->max_score = $max_score;
  564. //$division = $max_score / count($scales[0]);
  565. if ($template->save()) {
  566. $templateId = $template->id;
  567. DB::delete("delete from template_criterion where template_id ={$template->id}");
  568. foreach ($criteria as $index => $criterion_id) {
  569. if (!DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, {$index})")) {
  570. Session::flash('status', 'danger');
  571. Session::flash('message', 'Rubric could not be created.');
  572. DB::rollback();
  573. return;
  574. }
  575. }
  576. DB::delete("delete from template_title where template_id = {$template->id}");
  577. foreach ($titles as $index => $text) {
  578. $query = DB::table('titles')
  579. ->where('text', $text)->first();
  580. if ($query) {
  581. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  582. if (!$result) {
  583. Session::flash('status', 'danger');
  584. Session::flash('message', 'Rubric could not be created.');
  585. DB::rollback();
  586. return;
  587. }
  588. } else {
  589. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  590. if (!$result1) {
  591. Session::flash('status', 'danger');
  592. Session::flash('message', 'Rubric could not be created.');
  593. DB::rollback();
  594. return;
  595. }
  596. $query = DB::table('titles')
  597. ->where('text', $text)->first();
  598. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  599. if (!$result) {
  600. Session::flash('status', 'danger');
  601. Session::flash('message', 'Rubric could not be created.');
  602. DB::rollback();
  603. return;
  604. }
  605. }
  606. }
  607. Session::flash('status', 'success');
  608. Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
  609. DB::commit();
  610. } else {
  611. Session::flash('status', 'danger');
  612. Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
  613. DB::commit();
  614. }
  615. }
  616. /**
  617. * Remove the specified resource from storage.
  618. *
  619. * @return Response
  620. */
  621. public function destroy()
  622. {
  623. $template = Template::find(Input::get('id'));
  624. if ($template->delete()) {
  625. Session::flash('status', 'success');
  626. Session::flash('message', 'Rubric deleted.');
  627. } else {
  628. Session::flash('status', 'danger');
  629. Session::flash('message', 'Rubric could not be deleted.');
  630. }
  631. return;
  632. }
  633. public function printview($id)
  634. {
  635. try {
  636. $template = Template::find($id);
  637. $template->titles = DB::table('titles')
  638. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  639. ->where("template_id", $template->id)
  640. ->orderBy('position', 'ASC')
  641. ->lists('text');
  642. $template_criterion = DB::table('template_criterion')
  643. ->join('criteria', 'criteria.id', '=', 'template_criterion.criterion_id')
  644. ->where('template_id', $template->id)
  645. ->orderBy('position', 'ASC')
  646. ->get();
  647. foreach ($template_criterion as $single_crit) {
  648. $single_crit->scales = DB::table('criterion_scale')
  649. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  650. ->where('criterion_id', $single_crit->id)
  651. ->orderBy('position')
  652. ->lists('description');
  653. $single_crit->outcomes = DB::table('outcomes')
  654. ->join('criterion_objective_outcome', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  655. ->where('criterion_id', $single_crit->id)
  656. ->select('name')
  657. ->distinct()
  658. ->lists('name');
  659. }
  660. $title = $template->name;
  661. if ($template->school_id != NULL)
  662. $school = $template->school->name;
  663. else
  664. $school = 'All Schools';
  665. if ($template->program_id != NULL)
  666. $program = $template->program->name;
  667. else
  668. $program = 'All Programs';
  669. return View::make('local.managers.shared.print_rubric', compact('title', 'template_criterion', 'template', 'school', 'program'));
  670. } catch (Exception $e) {
  671. Session::flash('status', 'danger');
  672. Session::flash('message', $e->getMessage());
  673. return Redirect::back();
  674. }
  675. }
  676. }