Няма описание

TemplatesController.php 19KB

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