123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
-
- class TemplatesController extends \BaseController
- {
-
- /**
- * List all templates, grouped by program
- */
- public function index()
- {
- $title = 'Rubric List';
-
- $global_templates = Template::whereNull('school_id')->whereNull('program_id')->get();
-
- $schools = School::with('programs.templates')
- ->orderBy('name')
- ->get();
-
- $templates = Template::orderBy('name')->get();
-
- return View::make('local.managers.admins.rubric_list', compact('title', 'global_templates', 'schools', 'templates'));
- }
-
- public function schoolCoordinatorIndex()
- {
- $title = 'Rubric List';
-
- $global_templates = Template::whereNull('school_id')->whereNull('program_id')->get();
-
- $schools = School::with('programs.templates')
- ->orderBy('name')
- ->get();
-
- $templates = Template::orderBy('name')->get();
-
- return View::make('local.managers.admins.rubric_list', compact('title', 'global_templates', 'schools', 'templates'));
- }
-
- public function show(Template $template)
- {
- $title = $template->name;
-
- return View::make('local.managers.admins.view_template', compact('template', 'title'));
- }
-
- public function onLoadFetch()
- {
- $json_to_send = [];
- $template_id = Input::get('id');
- $json_to_send["criteria"] = DB::table("criteria")->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
- ->where("template_criterion.template_id", '=', $template_id)
- ->get();
- Log::info($json_to_send["criteria"]);
- foreach ($json_to_send['criteria'] as $criteria) {
- $json_to_send['scales'][$criteria->criterion_id] = DB::table("scales")->join('template_criterion_scale', 'template_criterion_scale.scale_id', '=', 'scales.id')
- ->where("template_criterion_scale.template_criterion_id", '=', $criteria->id)->orderBy('position', 'ASC')->get();
- }
- return json_encode($json_to_send);
- }
- /**
- * Show the form for creating a new rubric
- *
- * @return Response
- */
- public function newTemplate()
- {
- $title = "Rubric Builder";
-
- $templates = Template::orderBy('name', 'ASC')->get();
- $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->get();
- $criteria = Criterion::orderBy('name', 'ASC')->get();
- $schools = School::orderBy('name', 'ASC')->get();
- $role = Auth::user()->role;
-
- $templates = NULL;
- $programs = NULL;
- // Returns templates depending on the type of user
- if ($role == 1) {
- $templates = Template::orderBy('name', 'ASC')->get();
- $programs = Program::orderBy('name', 'ASC')->get();
- } elseif ($role == 2) {
- $templates = Template::where('school_id', '=', Auth::user()->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
- $programs = Auth::user()->school->programs;
- } elseif ($role == 3) {
- $templates = Template::where('school_id', '=', Auth::user()->programs[0]->school->id)->orWhere('school_id', '=', NULL)->orderBy('name', 'ASC')->get();
- $programs = Auth::user()->programs()->get();
- }
-
- return View::make('local.managers.shared.rubrics', compact('title', 'templates', 'outcomes', 'criteria', 'schools', 'programs'));
- }
-
- /**
- * Save a new rubric
- *
- * @return Response
- */
- public function create()
- {
-
- $template = new Template;
-
- $template->name = Input::get('name');
-
- $template->is_visible = Input::get('is_visible');
- $template->expected_percentage = Input::get('expected_percentage');
- $template->expected_points = Input::get('expected_points');
-
- // If user can set the school (that is, if school_id is not undefined)
- // set the school id or set to null
- if (is_numeric(Input::get('school_id'))) {
- if (Input::get('school_id') != 0)
- $template->school_id = Input::get('school_id');
- elseif (Input::get('school_id') == 0)
- $template->school_id = NULL;
- }
-
- // If user can set the program (that is, if program_id is not undefined)
- // set the program id or set to null
- if (is_numeric(Input::get('program_id'))) {
- if (Input::get('program_id') != 0)
- $template->program_id = Input::get('program_id');
- elseif (Input::get('program_id') == 0)
- $template->program_id = NULL;
- }
-
- // If the user is any coordinator, set the school id
- // If the user is a program coordinator, also set program id
- switch (Auth::user()->role) {
- case 2:
- $template->school_id = Auth::user()->school->id;
- break;
-
- case 3:
- $template->school_id = Auth::user()->programs[0]->school->id;
- $template->program_id = Auth::user()->programs[0]->id;
- break;
- }
- $scales = Input::get('scales');
- $criteria = Input::get('criteria');
-
- $max_score = Input::get('max_score');
- Log::info($scales);
- Log::info($criteria);
-
-
- $template->num_scales = count($scales[0]);
- $template->max_score = $max_score;
-
- if ($template->save()) {
- $templateId = $template->id;
- foreach ($criteria as $index => $criterion_id) {
- DB::insert("insert into template_criterion (`template_id`,`criterion_id`) values ({$templateId},{$criterion_id})");
- $template_criterion_id = DB::table('template_criterion')->where('template_id', '=', $templateId)
- ->where('criterion_id', '=', $criterion_id)->first();
-
- for ($i = 0; $i < count($scales[$index]); $i++) {
- $scale = Scale::where('description', '=', $scales[$index][$i])->first();
- Log::info($scale);
- if ($scale) {
- DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
- DB::commit();
- } else {
- $scale = new Scale;
- $scale->description = $scales[$index][$i];
-
- if ($scale->save()) {
- DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
- DB::commit();
- } else {
- Session::flash('status', 'danger');
- Session::flash('message', 'Rubric could not be created.');
- }
- }
- }
- }
- Session::flash('status', 'success');
- Session::flash('message', 'Rubric created. You can now select it from the list.');
- } else {
- Session::flash('status', 'danger');
- Session::flash('message', 'Rubric could not be created.');
- }
- }
-
- /**
- * Return a specific template
- *
- * @return Template
- */
- public function fetch()
- {
- $template_info = [];
- $template_info['template'] = Template::find(Input::get('id'));
- $template_info['criterion'] = DB::table('criteria')
- ->join('template_criterion', 'template_criterion.criterion_id', '=', 'criteria.id')
- ->where("template_criterion.template_id", '=', Input::get('id'))
- ->get();
- foreach ($template_info['criterion'] as $temp_crit) {
- $template_info['scales'][$temp_crit->id] = DB::table('scales')
- ->join('template_criterion_scale', 'template_criterion_scale.scale_id', '=', 'scales.id')
- ->where('template_criterion_scale.template_criterion_id', '=', $temp_crit->id)
- ->orderBy('position', 'ASC')
- ->get();
- }
- Log::info($template_info);
-
- return json_encode($template_info);
- }
-
- /**
- * Update the specified resource in storage.
- *
- * @return Response
- */
- public function update()
- {
- $template = Template::find(Input::get('id'));
- $template->name = Input::get('name');
-
- $template->is_visible = Input::get('is_visible');
- $template->expected_percentage = Input::get('expected_percentage');
- $template->expected_points = Input::get('expected_points');
-
-
- // If user can set the school (that is, if school_id is not undefined)
- // set the school id or set to null
- if (is_numeric(Input::get('school_id'))) {
- if (Input::get('school_id') != 0)
- $template->school_id = Input::get('school_id');
- elseif (Input::get('school_id') == 0)
- $template->school_id = NULL;
- }
-
- // If user can set the program (that is, if program_id is not undefined)
- // set the program id or set to null
- if (is_numeric(Input::get('program_id'))) {
- if (Input::get('program_id') != 0)
- $template->program_id = Input::get('program_id');
- elseif (Input::get('program_id') == 0)
- $template->program_id = NULL;
- }
-
- switch (Auth::user()->role) {
- case 2:
- $template->school_id = Auth::user()->school->id;
- break;
-
- case 3:
- $template->school_id = Auth::user()->programs[0]->school->id;
- $template->program_id = Auth::user()->programs[0]->id;
- break;
- }
-
- $scales = Input::get('scales');
- $criteria = Input::get('criteria');
-
- $max_score = Input::get('max_score');
- Log::info($scales);
- Log::info(Input::all());
- $template->num_scales = count($scales[0]);
- $template->max_score = $max_score;
- //$division = $max_score / count($scales[0]);
-
- if ($template->save()) {
- $templateId = $template->id;
- DB::delete("delete from template_criterion where template_id ={$template->id}");
- foreach ($criteria as $index => $criterion_id) {
-
- DB::insert("insert into template_criterion (`template_id`,`criterion_id`) values ({$templateId},{$criterion_id})");
- $template_criterion_id = DB::table('template_criterion')->where('template_id', '=', $templateId)
- ->where('criterion_id', '=', $criterion_id)->first();
- DB::delete("delete from template_criterion_scale where template_criterion_id ={$template_criterion_id->id}");
- for ($i = 0; $i < count($scales[$index]); $i++) {
- $scale = Scale::where('description', '=', $scales[$index][$i])
- //->where('max_score', '=', ($division * ($i + 1)))
- //->where("min_score", '=', (1 + ($division * $i)))
- ->first();
- Log::info($scale);
- if ($scale) {
- DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
- DB::commit();
- } else {
- $scale = new Scale;
- $scale->description = $scales[$index][$i];
- //$scale->min_score = 1 + ($division * $i);
- //$scale->max_score = ($division * ($i + 1));
- if ($scale->save()) {
- DB::insert("insert into `template_criterion_scale` (`template_criterion_id`, `scale_id`, `position`) values ({$template_criterion_id->id},{$scale->id}, {$i})");
- DB::commit();
- } else {
- Session::flash('status', 'danger');
- Session::flash('message', 'Rubric could not be created.');
- }
- }
- }
- }
- Session::flash('status', 'success');
- Session::flash('message', 'Rubric updated (' . date('m/d/y, h:i:s a') . ').');
- } else {
- Session::flash('status', 'danger');
- Session::flash('message', 'Rubric could not be updated (' . date('m/d/y, h:i:s a') . ').');
- }
- }
-
- /**
- * Remove the specified resource from storage.
- *
- * @return Response
- */
- public function destroy()
- {
- $template = Template::find(Input::get('id'));
-
- if ($template->delete()) {
- Session::flash('status', 'success');
- Session::flash('message', 'Rubric deleted.');
- } else {
- Session::flash('status', 'danger');
- Session::flash('message', 'Rubric could not be deleted.');
- }
-
- return;
- }
-
- public function printview($id)
- {
- try {
- $template = Template::find($id);
-
- $title = $template->name;
-
- if ($template->school_id != NULL)
- $school = $template->school->name;
- else
- $school = 'All Schools';
-
- if ($template->program_id != NULL)
- $program = $template->program->name;
- else
- $program = 'All Programs';
-
- return View::make('local.managers.shared.print_rubric', compact('title', 'template', 'school', 'program'));
- } catch (Exception $e) {
- Session::flash('status', 'danger');
- Session::flash('message', $e->getMessage());
- return Redirect::back();
- }
- }
- }
|