Nessuna descrizione

TemplatesController.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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_ids = Criterion::orderBy('name', 'ASC')->lists('id');
  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. /*
  233. $criteria = Criterion::whereHas(
  234. 'programs',
  235. function ($q) use ($program_ids) {
  236. $q->whereIn('program_id', $program_ids);
  237. }
  238. )
  239. ->whereHas(
  240. 'getObjectivesAttribute',
  241. function ($q) {
  242. $q->where('objective_id', '!=', 0);
  243. }
  244. )
  245. ->orderBy('name', 'ASC')->get(); */
  246. $criteria_ids = Criterion::join('criterion_objective_outcome as cobo', 'cobo.criterion_id', '=', 'criteria.id')
  247. ->join('program_criterion_objective_outcome as poco', 'poco.cri_obj_out_id', '=', 'cobo.id')
  248. ->whereIn('program_id', $program_ids)
  249. ->where('objective_id', '<>', 0)
  250. ->select('criteria.*')
  251. ->distinct()
  252. ->lists('criteria.id');
  253. }
  254. // Log::info("aqui".(count($criteria)));
  255. // Log::info(json_encode(count($criteria)));
  256. $templates = Template::where('school_id', '=', $school_id_user)->orWhere('school_id', '=', NULL)
  257. ->orderBy('name', 'ASC')->get();
  258. /*$criteria_ids = array();
  259. foreach ($criteria as $criterion) {
  260. $criteria_ids[] = $criterion->id;
  261. }*/
  262. $templates_fuera = Template::whereHas('criteria', function ($q) use ($criteria_ids) {
  263. $q->whereNotIn('criterion_id', $criteria_ids);
  264. })
  265. ->orderBy('name', 'ASC')->get();
  266. $templates_fuera_ids = array();
  267. foreach ($templates_fuera as $tf) {
  268. $templates_fuera_ids[] = $tf->id;
  269. }
  270. // Log::info(json_encode($templates_fuera_ids));
  271. // exit();
  272. $templates_dentro = Template::whereNotIn('id', $templates_fuera_ids)
  273. ->orderBy('name', 'ASC')->get();
  274. $annual_plans = DB::table('annual_cycle')
  275. ->join('annual_plans', 'annual_cycle_id', '=', 'annual_cycle.id')
  276. ->join('programs', 'programs.id', '=', 'annual_plans.program_id')
  277. ->join('typ_semester_outcome', function ($j) {
  278. $j->on('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_start')
  279. ->orOn('typ_semester_outcome.semester_id', '=', 'annual_cycle.semester_end');
  280. })
  281. ->join('typ_program', function ($j) {
  282. $j->on('typ_program.program_id', '=', 'annual_plans.program_id')
  283. ->on('typ_program.id', '=', 'typ_semester_outcome.typ_program_id');
  284. })
  285. ->whereIn('annual_plans.program_id', $program_ids)
  286. ->orderBy('annual_cycle_id', 'DESC')
  287. ->orderBy('programs.name', 'ASC')
  288. ->select("programs.*", 'programs.name as program_name', "annual_cycle.*", 'annual_plans.id as annual_plan_id')
  289. ->distinct()
  290. ->get();
  291. /*$annual_plans = DB::table('annual_plans')
  292. ->join('programs', 'programs.id', '=', 'program_id')
  293. ->join('annual_cycle', 'annual_cycle_id', '=', 'annual_cycle.id')
  294. ->select("programs.*", 'programs.name as program_name', "annual_cycle.*", 'annual_plans.id as annual_plan_id')
  295. ->whereIn("program_id", $program_ids)
  296. ->orderBy('annual_cycle_id', 'DESC')
  297. ->orderBy('programs.name', 'ASC')
  298. ->get();
  299. Log::info("NOus avons");
  300. Log::info($annual_plans);*/
  301. /*$templates_dentro = Template::join('template_criterion', 'template_criterion.template_id', '=', 'templates.id')
  302. ->join('criterion_objective_outcome as cobo', 'cobo.criterion_id', '=', 'template_criterion.criterion_id')
  303. ->where('objective_id', '<>', '0')
  304. ->where(function ($query) use ($school_id_user) {
  305. $query->where('school_id', $school_id_user)
  306. ->orWhere('school_id', '=', NULL);
  307. })
  308. // ->orWhere('school_id', '=', NULL)
  309. ->orderBy('name', 'ASC')
  310. ->groupBy('templates.id')
  311. ->select("templates.*")
  312. ->get();*/
  313. // if(!isset($templates_dentro))$templates_dentro=
  314. // var_dump(json_encode($templates_dentro));
  315. // }
  316. $templates_fuera = array();
  317. return View::make('local.managers.shared.rubrics_new', compact('annual_plans', 'title', 'templates_dentro', 'templates_fuera', 'outcomes', 'criteria', 'schools', 'programs', 'criteria_ids'));
  318. }
  319. public function fetchObjectivesForTemplate()
  320. {
  321. $criteria_ids = Input::get("allCriteria");
  322. //program_id only to solve for the rubric builder
  323. $template_id = Input::get('template_id');
  324. if (isset($template_id)) {
  325. $template = Template::find($template_id);
  326. if ($template->school_id == null) {
  327. $program_ids = DB::table('programs')
  328. ->lists('programs.id');
  329. } else if ($template->program_id == null) {
  330. $program_ids = DB::table('programs')
  331. ->where('school_id', $template->school_id)->lists('programs.id');
  332. } else {
  333. $program_ids = DB::table('programs')
  334. ->where('program_id', $template->program_id)->lists('programs.id');
  335. }
  336. } else {
  337. $role = Auth::user()->role;
  338. switch ($role) {
  339. case 1:
  340. $program_ids = DB::table('programs')
  341. ->lists('programs.id');
  342. break;
  343. case 2:
  344. $program_ids = DB::table('programs')
  345. ->where('school_id', Auth::user()->school_id)
  346. ->lists('programs.id');
  347. break;
  348. case 3:
  349. case 4:
  350. $program_ids = Auth::user()->programs->lists('id');
  351. break;
  352. default:
  353. # code...
  354. break;
  355. }
  356. }
  357. foreach ($criteria_ids as $crit_id) {
  358. $crit = Criterion::where('id', $crit_id)->first();
  359. if (!isset($crit)) {
  360. return "NO pa";
  361. }
  362. $crit->all_objectives = DB::table("objectives")
  363. ->join("criterion_objective_outcome", 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  364. ->join('program_criterion_objective_outcome as poco', 'poco.cri_obj_out_id', '=', 'criterion_objective_outcome.id')
  365. ->where('criterion_id', $crit->id)
  366. ->whereIn("program_id", $program_ids)
  367. ->select('objectives.*')
  368. ->groupBy('objectives.id')
  369. ->get();
  370. $criteria[] = $crit;
  371. }
  372. return $criteria;
  373. }
  374. /**
  375. * Save a new rubric
  376. *
  377. * @return Response
  378. */
  379. public function create()
  380. {
  381. DB::beginTransaction();
  382. $template = new Template;
  383. $template->name = Input::get('name');
  384. $template->is_visible = Input::get('is_visible');
  385. $template->expected_percentage = Input::get('expected_percentage');
  386. $template->expected_points = Input::get('expected_points');
  387. // If user can set the school (that is, if school_id is not undefined)
  388. // set the school id or set to null
  389. if (is_numeric(Input::get('school_id'))) {
  390. if (Input::get('school_id') != 0)
  391. $template->school_id = Input::get('school_id');
  392. elseif (Input::get('school_id') == 0)
  393. $template->school_id = NULL;
  394. }
  395. // If user can set the program (that is, if program_id is not undefined)
  396. // set the program id or set to null
  397. if (is_numeric(Input::get('program_id'))) {
  398. if (Input::get('program_id') != 0)
  399. $template->program_id = Input::get('program_id');
  400. elseif (Input::get('program_id') == 0)
  401. $template->program_id = NULL;
  402. }
  403. // If the user is any coordinator, set the school id
  404. // If the user is a program coordinator, also set program id
  405. switch (Auth::user()->role) {
  406. case 2:
  407. $template->school_id = Auth::user()->school->id;
  408. break;
  409. //TODO esto yo no lo hice
  410. case 3:
  411. $template->school_id = Auth::user()->programs[0]->school->id;
  412. //Comenté esto porque siempre va a estar el program_id
  413. //TODO
  414. if ($template->program_id == NULL)
  415. $template->program_id = Auth::user()->programs[0]->id;
  416. break;
  417. }
  418. $criteria = Input::get('criteria');
  419. $max_score = Input::get('max_score');
  420. $titles = Input::get('titles');
  421. $template->num_scales = count($titles);
  422. $template->max_score = $max_score;
  423. if ($template->save()) {
  424. $templateId = $template->id;
  425. foreach ($criteria as $index => $criterion_id) {
  426. if (!(DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, '{$index}')"))) {
  427. Session::flash('status', 'danger');
  428. Session::flash('message', 'Rubric could not be created.');
  429. DB::rollback();
  430. return;
  431. }
  432. }
  433. foreach ($titles as $index => $text) {
  434. $query = DB::table('titles')
  435. ->where('text', $text)->first();
  436. if ($query) {
  437. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  438. if (!$result) {
  439. Session::flash('status', 'danger');
  440. Session::flash('message', 'Rubric could not be created.');
  441. DB::rollback();
  442. return;
  443. }
  444. } else {
  445. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  446. if (!$result1) {
  447. Session::flash('status', 'danger');
  448. Session::flash('message', 'Rubric could not be created.');
  449. DB::rollback();
  450. return;
  451. }
  452. $query = DB::table('titles')
  453. ->where('text', $text)->first();
  454. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  455. if (!$result) {
  456. Session::flash('status', 'danger');
  457. Session::flash('message', 'Rubric could not be created.');
  458. DB::rollback();
  459. return;
  460. }
  461. }
  462. }
  463. Session::flash('status', 'success');
  464. Session::flash('message', 'Rubric created. You can now select it from the list.');
  465. DB::commit();
  466. return;
  467. } else {
  468. Session::flash('status', 'danger');
  469. Session::flash('message', 'Rubric could not be created.');
  470. DB::rollback();
  471. return;
  472. }
  473. }
  474. /**
  475. * Return a specific template
  476. *
  477. * @return Template
  478. */
  479. public function fetch()
  480. {
  481. $template_info = [];
  482. $template_info['template'] = Template::find(Input::get('id'));
  483. $template_info['criterion'] = DB::table('criteria')
  484. ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
  485. ->where("template_criterion.template_id", '=', Input::get('id'))
  486. ->get();
  487. Log::info(json_encode($template_info['criterion']));
  488. // Log::info(($temp_crit->program_ids));
  489. foreach ($template_info['criterion'] as $temp_crit) {
  490. $temp_crit->scales = DB::table('scales')
  491. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  492. ->where('criterion_scale.criterion_id', '=', $temp_crit->criterion_id)
  493. ->orderBy('position', 'ASC')
  494. ->get();
  495. $temp_crit->program_ids = json_encode(DB::table('program_criterion_objective_outcome')
  496. ->join('criterion_objective_outcome', 'criterion_objective_outcome.id', '=', 'program_criterion_objective_outcome.cri_obj_out_id')
  497. ->where('criterion_objective_outcome.criterion_id', $temp_crit->id)
  498. ->select('program_criterion_objective_outcome.program_id')
  499. ->distinct()
  500. ->lists('program_criterion_objective_outcome.program_id'));
  501. Log::info("ee" . json_encode($temp_crit->program_ids));
  502. $temp_crit->objectives = DB::table('criterion_objective_outcome')
  503. ->join('objectives', 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  504. ->where('criterion_id', $temp_crit->id)
  505. ->select('objectives.*')
  506. ->distinct()
  507. ->lists('text');
  508. $outcomeID = DB::table('criterion_objective_outcome')->where('criterion_id', '=', $temp_crit->criterion_id)
  509. ->lists('outcome_id');
  510. $outcomes = DB::table('outcomes')->whereIn('id', $outcomeID)->get();
  511. $outcomeStr = '';
  512. foreach ($outcomes as $key => $outcome) {
  513. $outcomeStr .= $outcome->name . ', ';
  514. }
  515. $outcomeStr = rtrim($outcomeStr, ',');
  516. $temp_crit->outcomes = $outcomeStr;
  517. }
  518. Log::info("ee2" . json_encode($template_info));
  519. $template_info['titles'] = DB::table('titles')
  520. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  521. ->where('template_id', Input::get('id'))
  522. ->orderBy('position')
  523. ->get();
  524. // Log::info(json_encode($template_info));
  525. return json_encode($template_info);
  526. }
  527. /**
  528. * Update the specified resource in storage.
  529. *
  530. * @return Response
  531. */
  532. public function update()
  533. {
  534. DB::beginTransaction();
  535. $template = Template::find(Input::get('id'));
  536. Log::info(Input::all());
  537. $template->name = Input::get('name');
  538. $template->is_visible = Input::get('is_visible');
  539. $template->expected_percentage = Input::get('expected_percentage');
  540. $template->expected_points = Input::get('expected_points');
  541. // If user can set the school (that is, if school_id is not undefined)
  542. // set the school id or set to null
  543. if (is_numeric(Input::get('school_id'))) {
  544. if (Input::get('school_id') != 0)
  545. $template->school_id = Input::get('school_id');
  546. elseif (Input::get('school_id') == 0)
  547. $template->school_id = NULL;
  548. }
  549. // If user can set the program (that is, if program_id is not undefined)
  550. // set the program id or set to null
  551. if (is_numeric(Input::get('program_id'))) {
  552. if (Input::get('program_id') != 0)
  553. $template->program_id = Input::get('program_id');
  554. elseif (Input::get('program_id') == 0)
  555. $template->program_id = NULL;
  556. }
  557. switch (Auth::user()->role) {
  558. case 2:
  559. $template->school_id = Auth::user()->school->id;
  560. break;
  561. case 3:
  562. $template->school_id = Auth::user()->programs[0]->school->id;
  563. if ($template->program_id == NULL)
  564. $template->program_id = Auth::user()->programs[0]->id;
  565. break;
  566. }
  567. $criteria = Input::get('criteria');
  568. $max_score = Input::get('max_score');
  569. $titles = Input::get('titles');
  570. $template->num_scales = count($titles);
  571. $template->max_score = $max_score;
  572. //$division = $max_score / count($scales[0]);
  573. if ($template->save()) {
  574. $templateId = $template->id;
  575. DB::delete("delete from template_criterion where template_id ={$template->id}");
  576. foreach ($criteria as $index => $criterion_id) {
  577. if (!DB::insert("insert into template_criterion (`template_id`,`criterion_id`, `position`) values ({$templateId},{$criterion_id}, {$index})")) {
  578. Session::flash('status', 'danger');
  579. Session::flash('message', 'Rubric could not be created.');
  580. DB::rollback();
  581. return;
  582. }
  583. }
  584. DB::delete("delete from template_title where template_id = {$template->id}");
  585. foreach ($titles as $index => $text) {
  586. $query = DB::table('titles')
  587. ->where('text', $text)->first();
  588. if ($query) {
  589. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  590. if (!$result) {
  591. Session::flash('status', 'danger');
  592. Session::flash('message', 'Rubric could not be created.');
  593. DB::rollback();
  594. return;
  595. }
  596. } else {
  597. $result1 = DB::insert("insert into `titles` (`text`) values ('{$text}')");
  598. if (!$result1) {
  599. Session::flash('status', 'danger');
  600. Session::flash('message', 'Rubric could not be created.');
  601. DB::rollback();
  602. return;
  603. }
  604. $query = DB::table('titles')
  605. ->where('text', $text)->first();
  606. $result = DB::insert("insert into `template_title` (`template_id`, `title_id`, `position`) values ({$templateId}, {$query->id}, {$index})");
  607. if (!$result) {
  608. Session::flash('status', 'danger');
  609. Session::flash('message', 'Rubric could not be created.');
  610. DB::rollback();
  611. return;
  612. }
  613. }
  614. }
  615. Session::flash('status', 'success');
  616. Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
  617. DB::commit();
  618. } else {
  619. Session::flash('status', 'danger');
  620. Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
  621. DB::commit();
  622. }
  623. }
  624. /**
  625. * Remove the specified resource from storage.
  626. *
  627. * @return Response
  628. */
  629. public function destroy()
  630. {
  631. $template = Template::find(Input::get('id'));
  632. if ($template->delete()) {
  633. Session::flash('status', 'success');
  634. Session::flash('message', 'Rubric deleted.');
  635. } else {
  636. Session::flash('status', 'danger');
  637. Session::flash('message', 'Rubric could not be deleted.');
  638. }
  639. return;
  640. }
  641. public function printview($id)
  642. {
  643. try {
  644. $template = Template::find($id);
  645. $template->titles = DB::table('titles')
  646. ->join('template_title', 'template_title.title_id', '=', 'titles.id')
  647. ->where("template_id", $template->id)
  648. ->orderBy('position', 'ASC')
  649. ->lists('text');
  650. $template_criterion = DB::table('template_criterion')
  651. ->join('criteria', 'criteria.id', '=', 'template_criterion.criterion_id')
  652. ->where('template_id', $template->id)
  653. ->orderBy('position', 'ASC')
  654. ->get();
  655. foreach ($template_criterion as $single_crit) {
  656. $single_crit->scales = DB::table('criterion_scale')
  657. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  658. ->where('criterion_id', $single_crit->id)
  659. ->orderBy('position')
  660. ->lists('description');
  661. $single_crit->outcomes = DB::table('outcomes')
  662. ->join('criterion_objective_outcome', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  663. ->where('criterion_id', $single_crit->id)
  664. ->select('name')
  665. ->distinct()
  666. ->lists('name');
  667. }
  668. $title = $template->name;
  669. if ($template->school_id != NULL)
  670. $school = $template->school->name;
  671. else
  672. $school = 'All Schools';
  673. if ($template->program_id != NULL)
  674. $program = $template->program->name;
  675. else
  676. $program = 'All Programs';
  677. return View::make('local.managers.shared.print_rubric', compact('title', 'template_criterion', 'template', 'school', 'program'));
  678. } catch (Exception $e) {
  679. Session::flash('status', 'danger');
  680. Session::flash('message', $e->getMessage());
  681. return Redirect::back();
  682. }
  683. }
  684. }