123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- <?php
-
- use Illuminate\Support\Facades\Auth;
-
- class Objective2Controller extends \BaseController
- {
-
- /**
- * Display a listing of the resource.
- *
- * @return Response
- */
-
-
-
-
- /**
- * Show the form for creating a new resource.
- *
- * @return Response
- */
- public function isObjectiveUnique($input, $existing_Objective = NULL)
- {
-
- Log::info('isObjectiveUnique');
-
- if (Input::get('program_id') != 0)
- $program_id = $input['program_id'];
- else
- $program_id = NULL;
-
- $saved_Objective = DB::table('objectives')->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
- ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
- ->where('objectives.text', '=', $input['text'])
- ->whereIn('objective_outcome.outcome_id', $input['outcome_id'])
- ->whereIn('objective_program.program_id', $input['program_id'])
-
- ->get();
-
-
-
- if (count($saved_Objective))
- return false;
- else
- return true;
- }
-
- //edit == true or edit == false
- private function makeValidator($clean_input, $edit)
- {
- /** Validation rules */
-
- if (!$edit) {
- return Validator::make(
- array(
- 'text' => $clean_input['text'],
-
- 'outcome_id' => $clean_input['outcome_id'],
- 'program_id' => $clean_input['program_id']
-
- ),
- array(
-
- 'outcome_id' => 'required|array',
- 'program_id' => 'required|array'
-
- )
-
- );
- } else {
- return Validator::make(
- array(
- 'text' => $clean_input['text'],
-
- 'outcome_id' => $clean_input['outcome_id'],
- 'program_id' => $clean_input['program_id']
-
- ),
- array(
- 'text' => 'required|string|unique',
-
- 'outcome_id' => 'required|array',
- 'program_id' => 'required|array'
-
- )
-
- );
- }
- }
-
-
-
- private function cleanInput()
- {
- $clean_input = array();
-
- $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
-
-
- $clean_input['outcome_id'] = Input::get('outcome');
- $counter = Input::get('counter') + 0;
- // Log::info($clean_input);
-
- foreach ($clean_input['outcome_id'] as $index => $outcome_id) {
- $clean_input['outcome_id'][$index] = trim(preg_replace('/\t+/', '', $clean_input['outcome_id'][$index]));
- }
-
- $clean_input['program_id'] = Input::get('program_id');
- // Log::info(Input::get('program_id'));
-
- return $clean_input;
- }
-
- private function cleanAssocInput()
- {
- $clean_input = array();
-
- $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
-
-
- $clean_input['outcome_id'] = Input::get('assoc_outcome');
- Log::info(Input::all());
-
-
- $clean_input['program_id'] = Input::get('program_id');
- $clean_input['pair_criteria'] = Input::get('pair_every_criteria_with_objective');
-
-
- return $clean_input;
- }
- public function fetchObjective()
- {
- return Objective::find(Input::get('id'));
- }
-
- public function fetchObjectiveWithTrashed()
- {
- $json = array();
-
- $json['program'] = DB::select("select program_id from objective_program where objective_id = ?", array(Input::get('id')));
-
- //$json['outcome'] = DB::select("select outcome_id from objective_outcome outc where outc.objective_id = ?", array(Input::get('id')));
- $json['objective'] = DB::select("select text, id from objectives where id =?", array(Input::get('id')));
- $json['assessment'] = DB::select("select * from assessments where activity_criterion_id in (select id from activity_criterion where criterion_id in (select criterion_id from criterion_objective_outcome where objective_id = ?))", array(Input::get('id')));
- /*
- select objective_outcome.*, typ_semester_outcome.id as typ_semester_outcome_id,count(criterion_id) from objectives join objective_outcome on objective_outcome.objective_id = objectives.id left join criterion_objective_outcome on criterion_objective_outcome.objective_id = objectives.id and objective_outcome.outcome_id = criterion_objective_outcome.outcome_id
- left join typ_semester_objectives on typ_semester_objectives.objective_id = objectives.id left join typ_semester_outcome on typ_semester_outcome.outcome_id = objective_outcome.outcome_id and typ_semester_outcome.id = typ_semester_objectives.typ_semester_outcome_id group by objectives.id, objective_outcome.outcome_id
-
- */
- $json['outcome'] = DB::table('objective_outcome')
-
- ->leftJoin('criterion_objective_outcome', function ($j) {
- $j->on('criterion_objective_outcome.objective_id', '=', 'objective_outcome.objective_id')
- ->on('criterion_objective_outcome.outcome_id', '=', 'objective_outcome.outcome_id');
- })
- ->leftJoin('typ_semester_objectives', 'typ_semester_objectives.objective_id', '=', 'objective_outcome.objective_id')
- ->leftJoin('typ_semester_outcome', function ($j) {
- $j->on('typ_semester_outcome.outcome_id', '=', 'objective_outcome.outcome_id')
- ->on('typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id');
- })
- ->groupBy("objective_outcome.objective_id", 'objective_outcome.outcome_id')
- ->where('objective_outcome.objective_id', Input::get('id'))
- ->select(
- 'objective_outcome.*',
- DB::raw("count(criterion_id) as count_criterion_id"),
- 'typ_semester_outcome.id as typ_semester_outcome_id'
- )
- ->get();
-
-
- $json['typ_semester_objectives'] = DB::table('typ_semester_objectives')
- ->where('objective_id', Input::get('id'))
- ->get();
-
- $json['assoc_criteria'] = DB::select("select name from criteria where id in(select criterion_id from criterion_objective_outcome where objective_id =?)", array(Input::get('id')));
- Log::info('is here');
- // Log::info(json_encode($json));
- return json_encode($json);
- }
- public function fetchAllobjectives()
- {
- $program_id = Input::get('program_fetch');
- $outcome_id = Input::get('outcome_fetch');
- $json = array();
-
-
- $json['objective'] = DB::select("SELECT * FROM `objectives` where id in (select objective_id from objective_outcome where outcome_id ={$outcome_id}) and id in (select objective_id from objective_program where program_id = {$program_id} and objective_id <> 0)");
- Log::info('En mi sueño');
- Log::info($json);
- return json_encode($json);
- }
-
- public function delete()
- {
- DB::delete("delete from objectives where id = ?", array(Input::get('deleteObj')));
- return Redirect::to('objectives')->withInput();
- /*$role = Auth::user()['role'];
- switch ($role) {
- case 1:
- return Redirect::to('objectives')->withInput();
-
- case 2:
- return Redirect::to('school-objective')->withInput();
-
- case 3:
- return Redirect::to('program-objective')->withInput();
- }*/
- }
- /**
- * Create a new Objective.
- *
- * @return Redirect Redirect back to form page
- */
- public function create()
- {
-
- $clean_input = $this->cleanInput();
-
- /** Validation rules */
- $validator = $this->makeValidator($clean_input, false);
-
- /** If validation fails */
- if ($validator->fails()) {
- /** Prepare error message */
- $message = '<p>Error(s) creating a new Objective:</p><ul>';
-
- foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
- $message .= $validationError;
- }
-
- $message .= '</ul>';
-
- /** Send error message and old data */
- Session::flash('status', 'danger');
- Session::flash('message', $message);
- return Redirect::to('objectives')->withInput();
- /*$role = Auth::user()['role'];
- switch ($role) {
- case 1:
- return Redirect::to('objective')->withInput();
-
- case 2:
- return Redirect::to('school-objective')->withInput();
-
- case 3:
- return Redirect::to('program-objective')->withInput();
- }*/
- } else {
- // Check Objective uniqueness
- if (!$this->isObjectiveUnique($clean_input)) {
- /** Send error message and old data */
- Session::flash('status', 'danger');
- Session::flash('message', "This objective is a duplicate of an already saved Objective because it's and associated program are the same.");
- return Redirect::to('objectives')->withInput();
- /*$role = Auth::user()['role'];
- switch ($role) {
- case 1:
- return Redirect::to('objective')->withInput();
-
- case 2:
- return Redirect::to('school-objective')->withInput();
-
- case 3:
- return Redirect::to('program-objective')->withInput();
- }*/
- }
-
- /** Instantiate new Objective */
- $objective = new Objective;
- $objective->text = $clean_input['text'];
-
-
-
-
-
-
-
- /** If Objective is saved, send success message */
- if ($objective->save()) {
- $objectiveId = $objective->id;
-
- Log::info($clean_input['outcome_id']);
-
-
- foreach ($clean_input['program_id'] as $program_id) {
- DB::insert("insert into objective_program (objective_id, program_id) values({$objectiveId},{$program_id})");
- }
- foreach ($clean_input['outcome_id'] as $outcome_id) {
- DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
-
- /*if (!($objectiveOutcome->save())) {
- Session::flash('status', 'danger');
- Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
- return Redirect::to('objective')->withInput();
- }*/
- }
- DB::table('criterion_objective_outcome')
- ->join('program_criterion', 'criterion_objective_outcome.criterion_id', "=", 'program_criterion.criterion_id')
- ->whereIn('program_id', $clean_input['program_id'])
- ->whereIn('outcome_id', $clean_input['outcome_id'])
- ->where('objective_id', '=', 0)
- ->update(array('objective_id' => $objectiveId));
- // update("update criterion_objective_outcome coo join program_criterion pc on coo.criterion_id=pc.criterion_id set
- // objective_id= {$objectiveId} where program_id in (".$clean_input['program_id'].") and objective_id=0 and outcome_id in (".$clean_input['outcome_id'].")");
-
- Session::flash('status', 'success');
- Session::flash('message', 'Objective created: "' . $objective->text . '".');
- return Redirect::to('objectives')->withInput(Input::only('outcome_id'));
- /*$role = Auth::user()['role'];
- switch ($role) {
- case 1:
- return Redirect::to('objective')->withInput(Input::only('outcome_id'));
-
- case 2:
- return Redirect::to('school-objective')->withInput(Input::only('outcome_id'));
-
- case 3:
- return Redirect::to('program-objective')->withInput(Input::only('outcome_id'));
- }*/
- }
-
- /** If saving fails, send error message and old data */
- else {
- Session::flash('status', 'danger');
- Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
- return Redirect::to('objectives')->withInput();
- /*$role = Auth::user()['role'];
- switch ($role) {
- case 1:
- return Redirect::to('objective')->withInput();
-
- case 2:
- return Redirect::to('school-objective')->withInput();
-
- case 3:
- return Redirect::to('program-objective')->withInput();
- }*/
- }
- }
- }
- /**
- * Store a newly created resource in storage.
- *
- * @return Response
- */
- public function store()
- {
- //
- }
-
-
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return Response
- */
- public function show($id)
- {
- //
- }
-
-
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return Response
- */
- public function edit()
- {
- $title = "Objective";
- $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
-
- $role = Auth::user()->role;
-
- switch ($role) {
- case 1:
- $program_ids = DB::table("programs")
- ->lists('id');
- break;
-
- case 2:
- $program_ids = DB::table('programs')
- ->where('school_id', Auth::user()->school_id)
- ->lists('id');
-
- break;
- case 3:
- $program_ids = DB::table('program_user')
- ->where('user_id', Auth::user()->id)
- ->lists('program_id');
-
-
- break;
- }
- $objectives_from_program = DB::table('objective_program')
- ->where('program_id', $program_ids)
- ->where('objective_program.objective_id', "<>", 0)
- ->lists('objective_id');
- $objectives = Objective::withTrashed()->whereIn('id', $objectives_from_program)->orderBy('text', 'ASC')->get();
- $programs = Program::whereIn('id', $program_ids)->orderBy('name', 'ASC')->get();
-
- return View::make('local.managers.shared.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
- }
-
- public function editProgram()
- {
- $userProgram = Auth::user()['id'];
- Log::info(Auth::user());
-
- $userProgram = DB::select("select program_user.program_id from program_user where user_id = {$userProgram}");
-
- $title = "Objective";
- $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
- $objectives_from_program = DB::table('objective_program')
- ->where('program_id', $userProgram[0]->program_id)
- ->where('objective_program.objective_id', "<>", 0)
- ->lists('objective_id');
- $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->whereIn('id', $objectives_from_program)->get();
-
- $programs = Program::where("id", '=', $userProgram[0]->program_id)->get();
-
-
-
-
- return View::make('local.managers.pCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
- }
-
- public function editSchool()
- {
- $userSchool = Auth::user()['school_id'];
- Log::info($userSchool);
- $title = "Objective";
- $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
-
- $objectives_from_school = DB::table('programs')
- ->join('objective_program', 'objective_program.program_id', '=', 'programs.id')
- ->where('programs.school_id', $userSchool)
- ->where('objective_program.objective_id', "<>", 0)
- ->lists('objective_id');
- $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->whereIn('id', $objectives_from_school)->get();
- $programs = Program::where("school_id", "=", $userSchool)->orderBy('name', 'ASC')->get();
-
-
-
- return View::make('local.managers.sCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
- }
-
- /**
- * Update the specified resource in storage.
- *
- * @param int $id
- * @return Response
- */
-
- public function update()
-
- {
-
- $Objective = Objective::withTrashed()->find(Input::get('id'));
-
- $clean_input = $this->cleanAssocInput();
-
-
- //Log::info(print_r($clean_input, true));
-
- /** Validation rules */
- if ($clean_input['text'] == $Objective->text) {
- $validator = $this->makeValidator($clean_input, true);
- } else
- $validator = $this->makeValidator($clean_input, false);
-
- /** If validation fails */
- if ($validator->fails()) {
- /** Prepare error message */
- $message = 'Error(s) updating the Objective: <ul>';
-
- foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
- $message .= $validationError;
- }
-
- $message .= '</ul>';
-
- /** Send error message and old data */
- //Session::flash('status', 'danger');
- //Session::flash('message', $message);
- $MessageArray = array('status' => 'danger', 'message' => $message);
- return $MessageArray;
- /*$role = Auth::user()['role'];
- switch ($role) {
- case 1:
- return $MessageArray;
- return Redirect::to('objective')->withInput();
-
- case 2:
- return $MessageArray;
- return Redirect::to('school-objective')->withInput();
-
- case 3:
- return $MessageArray;
- return Redirect::to('program-objective')->withInput();
- }*/
- } else {
-
-
-
- /** Set info */
- Log::info($clean_input);
- $Objective->text = $clean_input['text'];
-
-
-
-
- // Set status
-
-
- /** If Objective is updated, send success message */
- if ($Objective->save()) {
-
- //TODO
-
- $objectiveId = $Objective->id;
- //DB::delete("delete from `objective_outcome` where objective_id ={$objectiveId}");
- DB::delete("delete from objective_program where objective_id = {$objectiveId}");
-
- foreach ($clean_input['program_id'] as $program_id) {
- DB::insert("insert into `objective_program`(objective_id, program_id) values ({$objectiveId},{$program_id})");
- }
- $criteria_assoc = DB::table('criterion_objective_outcome')
- ->join('program_criterion', 'program_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
- ->whereIn('program_id', $clean_input['program_id'])
- ->where('objective_id', $objectiveId)
- ->groupBy('program_criterion.criterion_id')
- ->select('program_criterion.criterion_id')
- ->lists('program_criterion.criterion_id');
-
- $criterion_array = [];
- foreach ($clean_input['outcome_id'] as $outcome_id) {
- $check_if_already_inserted = DB::table('objective_outcome')
- ->where('objective_id', $objectiveId)
- ->where('outcome_id', $outcome_id)
- ->first();
-
- if (!isset($check_if_already_inserted)) {
- DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
- if ($clean_input['pair_criteria'] == '1') {
- foreach ($criteria_assoc as $criterion_id) {
-
- DB::table('criterion_objective_outcome')
- ->insert(array(
- "criterion_id" => $criterion_id,
- "objective_id" => $objectiveId,
- "outcome_id" => $outcome_id
- ));
- }
- }
- }
- }
-
- DB::table('objective_outcome')
- ->whereNotIn('outcome_id', $clean_input['outcome_id'])
- ->where('objective_id', $objectiveId)
- ->delete();
-
- //Session::flash('status', 'success');
- //Session::flash('message', 'Updated Objective: "' . $Objective->text . '"');
- $MessageArray = array('status' => 'success', 'message' => 'Updated Objective: "' . $Objective->text . '"');
- return $MessageArray;
- /*$role = Auth::user()['role'];
-
- switch ($role) {
- case 1:
- return $MessageArray;
- return Redirect::to('objective')->withInput();
-
- case 2:
- return $MessageArray;
- return Redirect::to('school-objective')->withInput();
-
- case 3:
- return $MessageArray;
- return Redirect::to('program-objective')->withInput();
- }*/
- }
-
- /** If saving fails, send error message and old data */
- else {
- //Session::flash('status', 'danger');
- ////Session::flash('message', 'Error updating the Objective. Please try again later.');
- $MessageArray = array('status' => 'danger', 'message' => 'Error updating the Objective. Please try again later.');
- return $MessageArray;
- $role = Auth::user()['role'];
- switch ($role) {
- case 1:
- return $MessageArray;
- return Redirect::to('objectives')->withInput();
-
- case 2:
- return $MessageArray;
- return Redirect::to('school-objective')->withInput();
-
- case 3:
- return $MessageArray;
- return Redirect::to('program-objective')->withInput();
- }
- }
- }
- }
-
-
-
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return Response
- */
- public function destroy($id)
- {
- //
- }
- }
|