Nenhuma descrição

TemplatesController.php 25KB

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