暂无描述

TemplatesController.php 19KB

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