<?php

use Illuminate\Support\Facades\Input;

class TransformativeActionsController extends \BaseController
{

    // load the Tranformative actions page
    public function editTA()
    {
        $title = "Transformative Action";
        $role = Auth::user()['role'];
        $outcomes = Outcome::orderBy('name', 'ASC')->lists('name', 'id');

        $schools = School::orderBy('name', 'ASC')->get();
        $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
        $programs = Program::orderBy('name', 'ASC')->get();
        $user_id = auth::user()->id;

        $program_id = DB::table('program_user')
            ->where('user_id',$user_id)
            ->select('program_id')
            ->get();
        $program_id = $program_id[0]->program_id; //program id 15 debido al user 8
        $outcomes = Outcome::orderBy('name', 'ASC')
                    ->where('deactivation_date','=','0000-00-00')
                    ->orWhereNull('deactivation_date')
                    ->get();
        $objectives = array();

        // if user is program coordinator
        if ($role == 3){
          //1 edit panel: load the TA that
          //    are custom ('transformative_actions.by_professor' == 0)
          //    were approved in past ('transformative_actions.is_custom' == 1)
          $ta_edit_panel = DB::table('transformative_actions')
                          ->where('transformative_actions.is_custom', 1)
                          ->where('transformative_actions.program_id', $program_id)
                          ->where('transformative_actions.by_professor', 0)
                          ->orderBy('at_text', 'ASC')
                          ->get();
          //2 approve panel: load TAs that
          //    can be approved ('transformative_actions.by_professor' == 1)
          $ta_approval_panel = DB::table('transformative_actions')
                            ->where('transformative_actions.is_custom', 1)
                            ->where('transformative_actions.program_id', $program_id)
                            ->where('transformative_actions.by_professor',1)
                            ->orderBy('at_text', 'ASC')
                            ->get();
          //2.1 approve panel: load the filter options.
          // the "->where()" should be the same from $ta_approval_panel,
          //  but with aditional joins and different select
          //
          // get the names of the professors
          $professor_filter_approvePanel = DB::table('transformative_actions')
                            ->join('users','users.id','=','transformative_actions.user_id')
                            ->where('transformative_actions.is_custom', 1)
                            ->where('transformative_actions.program_id', $program_id)
                            ->where('transformative_actions.by_professor', 1)
                            ->select('users.*')
                            ->groupby('transformative_actions.user_id')
                            ->orderBy('users.first_name', 'ASC')
                            ->get();
          // get the courses from asociated with a TA
          $course_filter_approvePanel = DB::table('ta_course')
                            ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
                            ->join('courses','courses.id','=','ta_course.course_id')
                            ->where('transformative_actions.is_custom', 1)
                            ->where('transformative_actions.program_id', $program_id)
                            ->where('transformative_actions.by_professor', 1)
                            ->select('courses.*')
                            ->groupBy('courses.name','courses.code')
                            ->orderBy('courses.name', 'ASC')
                            ->orderBy('courses.code', 'ASC')
                            ->get();
          // get the outcome asociated with a TA
          $outcome_filter_approvePanel = DB::table('transformative_actions')
                            ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
                            ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
                            ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
                            ->where('transformative_actions.is_custom', 1)
                            ->where('transformative_actions.program_id', $program_id)
                            ->where('transformative_actions.by_professor', 1)
                            ->select('outcomes.*')
                            ->groupBy('outcomes.id')
                            ->orderBy('outcomes.name', 'ASC')
                            ->get();
          //3 edit panel: load the filter options.
          // the "->where()" should be the same from $ta_edit_panel,
          //  but with aditional joins and different select
          //
          $professor_filter_editPanel = DB::table('transformative_actions')
                            ->join('users','users.id','=','transformative_actions.user_id')
                            ->where('transformative_actions.is_custom', 1)
                            ->where('transformative_actions.program_id', $program_id)
                            ->where('transformative_actions.by_professor', 0)
                            ->select('users.*')
                            ->groupby('transformative_actions.user_id')
                            ->orderBy('users.first_name', 'ASC')
                            ->get();
          $course_filter_editPanel =  DB::table('ta_course')
                            ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
                            ->join('courses','courses.id','=','ta_course.course_id')
                            ->where('transformative_actions.is_custom', 1)
                            ->where('transformative_actions.program_id', $program_id)
                            ->where('transformative_actions.by_professor', 0)
                            ->select('courses.*')
                            ->groupBy('courses.name','courses.code')
                            ->orderBy('courses.name', 'ASC')
                            ->orderBy('courses.code', 'ASC')
                            ->get();
          $outcome_filter_editPanel = DB::table('transformative_actions')
                            ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
                            ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
                            ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
                            ->where('transformative_actions.is_custom', 1)
                            ->where('transformative_actions.program_id', $program_id)
                            ->where('transformative_actions.by_professor', 0)
                            ->select('outcomes.*')
                            ->groupBy('outcomes.id')
                            ->orderBy('outcomes.name', 'ASC')
                            ->get();
          // 4 create panel: search all courses
          $courses_create = DB::table('courses')
                            ->where('courses.program_id',$program_id)
                            ->select('courses.*')
                            ->groupBy('courses.name','courses.code')
                            ->orderBy('courses.name', 'ASC')
                            ->orderBy('courses.code', 'ASC')
                            ->get();
        }
        // if user is profesor
        elseif ($role == 4){
          // 1 the user can only edit TA that need approval and has been submited by the same user
          $ta_edit_panel = DB::table('transformative_actions')
                          ->where('transformative_actions.is_custom', 1)
                          ->where('transformative_actions.program_id',$program_id)
                          ->where('transformative_actions.user_id', Auth::user()->id)
                          ->where('transformative_actions.by_professor',1)
                          ->select('transformative_actions.*')
                          ->orderBy('at_text', 'ASC')
                          ->get();
          // 2 approve panel: dont load TA since professors cant approve them
          $ta_approval_panel = array();
          // 3 edit panel: load professor filter for his courses
          $professor_filter_editPanel = array();
          $course_filter_editPanel =  DB::table('ta_course')
                            ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
                            ->join('courses','courses.id','=','ta_course.course_id')
                            ->where('transformative_actions.is_custom', 1)
                            ->where('transformative_actions.user_id', Auth::user()->id)
                            ->where('transformative_actions.program_id', $program_id)
                            ->where('transformative_actions.by_professor', 1)
                            ->select('courses.*')
                            ->groupBy('courses.name','courses.code')
                            ->orderBy('courses.name', 'ASC')
                            ->orderBy('courses.code', 'ASC')
                            ->get();
          $outcome_filter_editPanel = DB::table('transformative_actions')
                            ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
                            ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
                            ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
                            ->where('transformative_actions.is_custom', 1)
                            ->where('transformative_actions.user_id', Auth::user()->id)
                            ->where('transformative_actions.program_id',$program_id)
                            ->where('transformative_actions.by_professor', 1)
                            ->select('outcomes.*')
                            ->groupBy('outcomes.id')
                            ->orderBy('outcomes.name', 'ASC')
                            ->get();
          // these arent used by professors
          $professor_filter_approvePanel = array();
          $course_filter_approvePanel = array();
          $outcome_filter_approvePanel = array();

          // 4 create panel: search courses given by the professor
          $courses_create = DB::table('courses')
                            ->where('courses.program_id',$program_id)
                            ->where('courses.user_id', Auth::user()->id)
                            ->select('courses.*')
                            ->groupBy('courses.name','courses.code')
                            ->orderBy('courses.name', 'ASC')
                            ->orderBy('courses.code', 'ASC')
                            ->get();
        }

        return View::make('local.managers.admins.transformativeAction', compact('title','role', 'outcomes', 'schools', 'criteria', 'programs', 'outcomes', 'objectives',
        'ta_edit_panel','ta_approval_panel','courses_create',
        'professor_filter_approvePanel', 'course_filter_approvePanel', 'outcome_filter_approvePanel',
        'professor_filter_editPanel', 'outcome_filter_editPanel', 'course_filter_editPanel'
      ));
    }

    private function cleanInput()
    {
        $clean_input = array();


        $trimmed = trim(preg_replace('/\t+/', '', Input::get('text')));

        Log::info('trimmed 1 -->' . $trimmed . '<--');

        // if ($trimmed == '') {
        //     $trimmed = NULL;
        // } else {
        //     $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
        // }

        Log::info('trimmed 2 -->' . $trimmed . '<--');


        $clean_input['text'] = $trimmed;

        //////
        $trimmed = trim(preg_replace('/\t+/', '', Input::get('description')));

        Log::info('trimmed 3 -->' . $trimmed . '<--');

        // if ($trimmed == '') {
        //     $trimmed = NULL;
        // } else {
        //     $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
        // }

        Log::info('trimmed 4 -->' . $trimmed . '<--');


        $clean_input['description'] = $trimmed;



        $clean_input['objectiveid'] = Input::get('objectiveid');
        $clean_input['courseid'] = Input::get('courseid');
        $clean_input['approval'] = Input::get('approval');
        $clean_input['ta_id'] = Input::get('ta_id');

        return $clean_input;
    }

    private function makeValidator($clean_input)
    {

      /** Validation rules */
      return Validator::make(
            array(
                'text' => $clean_input['text'],
                'description' => $clean_input['description'],
                'objectiveid' => $clean_input['objectiveid'],
                'courseid' => $clean_input['courseid'],
                'approval' => $clean_input['approval'],
                'ta_id' => $clean_input['ta_id'],
            ),
            array(
                'text' => 'required|string',
                'description' => 'required|string',
                'objectiveid' => 'required|array',
                'courseid' => 'required|array',
                'approval' => 'integer',
                'ta_id' => 'integer',
            )
        );
    }


    // Create a Transformative Action
    public function createTA()
    {
        $clean_input = $this->cleanInput();

        /** Validation rules */
        $validator = $this->makeValidator($clean_input);

        /** If validation fails */
        if ($validator->fails()) {
            /** Prepare error message */
            $message = '<p>Error(s) creating a new Transformative Action:</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);
            $role = Auth::user()['role'];
            return Redirect::to('transformativeAction')->withInput();
        } else {

          $user_id = auth::user()->id;
          $program_id = DB::table('program_user')
              ->where('user_id',$user_id)
              ->select('program_id')
              ->get();
          $program_id = $program_id[0]->program_id;
          $role = Auth::user()['role'];
          $by_professor = 1;
          if($role == 3){
            $by_professor = 0;
          }

          // $by_professor = $clean_input['approval'];

          $current_timestamp = date('Y/m/d H:i:s', time());
          // insert the TA
          $ta_id = DB::table('transformative_actions')->insertGetId(
              array('at_text' => $clean_input['text'],
                    'description' => $clean_input['description'],
                    'is_custom' => 1,
                    'user_id' => $user_id,
                    'program_id' => $program_id,
                    'created_at' => $current_timestamp,
                    'by_professor' => $by_professor,
                  )
          );

          //
          // // insert the multiple TA_objective_program
          foreach ($clean_input['objectiveid'] as $objective_id) {
            DB::table('transformative_objective_program')->insert(
                array(
                  'ta_id' => $ta_id,
                  'objective_id' => $objective_id,
                  'program_id' => $program_id,
                  'created_at' => $current_timestamp,
                  )
            );
          }

          //
          // // insert the multiple TA_course
          foreach ($clean_input['courseid'] as $course_id) {
            DB::table('TA_course')->insert(
                array(
                  'ta_id' => $ta_id,
                  'course_id' => $course_id,
                  )
            );
          }

          Session::flash('status', 'success');
          Session::flash('message', 'Transformative Action created: "' . $clean_input['text'] . '".');
          $role = Auth::user()['role'];
          return Redirect::to('transformativeAction')->withInput();
        }
    }

    // apporve a Transformative Action
    public function approveTA()
    {
      $role = Auth::user()['role'];
      if($role != 3){
        $message = 'Only Program Coordinators can approve a TA';
        Session::flash('status', 'danger');
        Session::flash('message', $message);
        return Redirect::to('transformativeAction')->withInput();
      }

      $ta_id = Input::get('ta_id');
      $text = Input::get('at_text');

      if($ta_id == 0){
        $message = 'Please select a Transformative Action</p>';

        Session::flash('status', 'danger');
        Session::flash('message', $message);

        $role = Auth::user()['role'];
        return Redirect::to('transformativeAction')->withInput();
      }

      $current_timestamp = date('Y/m/d H:i:s', time());

      // edit the TA
      DB::table('transformative_actions')
                  ->where('id', $ta_id)
                  ->update([
                        'by_professor' => 0,
                        'updated_at' => $current_timestamp,
                    ]);

      Session::flash('status', 'success');
      Session::flash('message', 'Approved the Transformative Action: "' . $text . '".');
      $role = Auth::user()['role'];
      return Redirect::to('transformativeAction')->withInput();
    }


    // update a Tranformative Action
    public function updateTA()
    {
      $clean_input = $this->cleanInput();

      /** Validation rules */
      $validator = $this->makeValidator($clean_input);

      /** If validation fails */
      if ($validator->fails()) {
          /** Prepare error message */
          $message = 'Error(s) updating the Transformative Action: <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);
          $role = Auth::user()['role'];
          return Redirect::to('transformativeAction')->withInput();
      } else {

          $user_id = auth::user()->id;
          $program_id = DB::table('program_user')
              ->where('user_id',$user_id)
              ->select('program_id')
              ->get();
          $program_id = $program_id[0]->program_id;
          $role = Auth::user()['role'];
          $by_professor = $clean_input['approval'];
          if($role == 4){
            $by_professor = 1;
          }

          // $by_professor = $clean_input['approval'];

          $current_timestamp = date('Y/m/d H:i:s', time());
          // edit the TA
          DB::table('transformative_actions')
                      ->where('id', $clean_input['ta_id'])
                      ->update([
                            'by_professor' => $by_professor,
                            'at_text' => $clean_input['text'],
                            'description' => $clean_input['description'],
                            'updated_at' => $current_timestamp,
                        ]);


          $ta_id = $clean_input['ta_id'];
          $new_objective_id = $clean_input['objectiveid'];
          $old_objective_id = DB::table('transformative_objective_program')
                      ->where('ta_id',$ta_id)
                      ->select('objective_id')
                      ->lists('objective_id');

          //delete existing objective_id if it isnt in new_ids array
          foreach ($old_objective_id as $old_id) {
            if(in_array($old_id,$new_objective_id)){
              //do nothing if a new id is already in atble
            }
            else {
              //if old id not in new id, delete
              DB::table('transformative_objective_program')
                ->where('ta_id',$ta_id)
                ->where('objective_id', $old_id)
                ->delete();
            }
          }

          //
          foreach ($new_objective_id as $new_id) {
            $result = DB::table('transformative_objective_program')
                        ->where('objective_id',$new_id)
                        ->select('objective_id')
                        ->lists('objective_id');

            if(count($result) == 0){
              //if the new_id does not exists, do nothing
              DB::table('transformative_objective_program')->insert(
                array(
                  'ta_id' => $ta_id,
                  'objective_id' => $new_id,
                  'program_id' => $program_id,
                  'created_at' => $current_timestamp,
                )
              );
            }
            else {
              //if the new_id already exists, do nothing
            }
          }


          $new_course_id = $clean_input['courseid'];
          $old_course_id = DB::table('ta_course')
                      ->where('ta_id',$ta_id)
                      ->select('course_id')
                      ->lists('course_id');

          //delete existing course_id if it isnt in new_ids array
          foreach ($old_course_id as $old_id) {
            if(in_array($old_id,$new_course_id)){
              //do nothing if a new id is already in atble
            }
            else {
              //if old id not in new id, delete
              DB::table('ta_course')
                ->where('ta_id',$ta_id)
                ->where('course_id', $old_id)
                ->delete();
            }
          }
          //add course_id if it isnt already inserted
          foreach ($new_course_id as $new_id) {
            $result = DB::table('ta_course')
                        ->where('ta_id',$ta_id)
                        ->where('course_id',$new_id)
                        ->select('course_id')
                        ->lists('course_id');

            if(count($result) == 0){
              //if the new_id does not exists, do nothing
              DB::table('ta_course')->insert(
                array(
                  'ta_id' => $ta_id,
                  'course_id' => $new_id,
                )
              );
            }
            else {
              //if the new_id already exists, do nothing
            }
          }

          Session::flash('status', 'success');
          Session::flash('message', 'Updated Transformative Action: "' . $clean_input['text'] . '".');
          $role = Auth::user()['role'];
          return Redirect::to('transformativeAction')->withInput();
      }
    }

    // delete a Transformative Action
    public function deleteTA()
    {
      $ta_id = array(Input::get('ta_id'));

      // si envia id 0, el backend se queja en la linea: $ta = $ta[0];
      // nunca deberia ocurrir, pero es un safity measure.
      if($ta_id == 0){
        $message = 'Please select a Transformative Action</p>';

        Session::flash('status', 'danger');
        Session::flash('message', $message);

        $role = Auth::user()['role'];
        return Redirect::to('transformativeAction')->withInput();
      }

      $ta = DB::table('transformative_actions')
          ->where('transformative_actions.is_custom', 1)
          ->where('id',$ta_id)
          ->get();
      $ta = $ta[0];

      $used = DB::table('annual_plan_transformative')
              ->where('id',$ta_id)
              ->get();

      $recommended = $ta->by_professor;
      // the TA can only be deleted if error if the TA is not currently as "Recommended"
      // and isnt used in an anual plan
      if ($recommended == 1 && count($used) == 0) {
        // delete the TA if it qualifies
        DB::delete("delete from transformative_actions where id = ?", $ta_id);

        $name = $ta->at_text;

        $message = 'The Transformative Action has been deleted:</p><ul>';
        $message .= '<li> '.$name.' </li>';
        $message .= '</ul>';

        Session::flash('status', 'success');
        Session::flash('message', $message);
        $role = Auth::user()['role'];
        return Redirect::to('transformativeAction')->withInput();
      }


      $message = 'Transformative Actions can only be deleted if:</p><ul>';
      $message .= '<li> It has a status of "Recommended"</li>';
      $message .= '<li> It is not currently used in a plan</li>';
      $message .= '</ul>';

      Session::flash('status', 'danger');
      Session::flash('message', $message);

      $role = Auth::user()['role'];
      return Redirect::to('transformativeAction')->withInput();
    }



    // load the information of a Tranformative Action when its
    //  selected on the approve or edit panel
    public function selectTA()
    {
      $ta_id = Input::get("ta_id");
      $user_id = Auth::user()->id;
      $program_id = DB::table('program_user')
          ->where('user_id',$user_id)
          ->select('program_id')
          ->get();
      $program_id = $program_id[0]->program_id;

      $objectives = DB::table('transformative_actions')
                        ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
                        ->join('objectives','objectives.id','=','transformative_objective_program.objective_id')
                        ->where('transformative_actions.id',$ta_id)
                        ->select('objectives.text as text', 'objectives.id as id')
                        ->orderBy('objectives.text', 'ASC')
                        ->get();

      $an_objective = $objectives[0]->id;
      $outcome_id = DB::table('criterion_objective_outcome')
                    ->where('criterion_objective_outcome.objective_id',$an_objective)
                    ->select('criterion_objective_outcome.outcome_id')
                    ->get();
      $outcome_id = $outcome_id[0]->outcome_id;


      $objectives_from_outcome = DB::table('objectives')
                    ->join('objective_program','objective_program.objective_id','=','objectives.id')
                    ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','objectives.id')
                    ->where('criterion_objective_outcome.outcome_id',$outcome_id)
                    ->where('objective_program.program_id',$program_id)
                    ->orderBy('objectives.text', 'ASC')
                    ->select('objectives.text as text', 'objectives.id as id')
                    ->get();
      $selected_courses = DB::table('ta_course')
                    ->join('courses','courses.id','=','ta_course.course_id')
                    ->where('ta_id',$ta_id)
                    ->orderBy('courses.name', 'ASC')
                    ->orderBy('courses.code', 'ASC')
                    ->select('courses.*')
                    ->get();
      $name = DB::table('transformative_actions')
                  ->where('id',$ta_id)
                  ->select('at_text as name')
                  ->lists('name');
      $description = DB::table('transformative_actions')
                  ->where('id',$ta_id)
                  ->select('description')
                  ->lists('description');
      $status = DB::table('transformative_actions')
                  ->where('id',$ta_id)
                  ->select('by_professor as status')
                  ->lists('status');
      $status = $status[0];
      $plans_count = DB::table('annual_plan_transformative')
                  ->where('id',$ta_id)
                  ->get();
      $plans_count = count($plans_count);
      $can_be_deleted = false;
      if($plans_count == 0 && $status == 1){
        $can_be_deleted = true;
      }
      return array(
        'objectives' => $objectives,
        'objectives_from_outcome' => $objectives_from_outcome,
        'selected_courses' => $selected_courses,
        'name' => $name,
        'description' => $description,
        'status' => $status,
        'can_be_deleted' => $can_be_deleted,
        'plans_count' => $plans_count,
      );
    }

    // load the available objectieves from an outcome when creating a Tranformative Action
    public function objectivesFromOutcome()
    {
      $user_id = Auth::user()->id;
      $program_id = DB::table('program_user')
          ->where('user_id',$user_id)
          ->select('program_id')
          ->get();
      $program_id = $program_id[0]->program_id;
      $ta_id = Input::get("ta_id");
      $outcome_id = Input::get("outcome_id");

      $objectives = DB::table('objectives')
                    ->join('objective_program','objective_program.objective_id','=','objectives.id')
                    ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','objectives.id')
                    ->where('criterion_objective_outcome.outcome_id',$outcome_id)
                    ->where('objective_program.program_id',$program_id)
                    ->orderBy('objectives.text', 'ASC')
                    ->select('objectives.text as text', 'objectives.id as id')
                    ->get();

      return array(
        'objectives' => $objectives,
      );
    }

    // return Transformative Actions that meet the filter criterias
    public function filterTA()
    {
      $user_id = Auth::user()->id;
      $role = Auth::user()['role'];
      $professor_id = Input::get('professor_id');
      $course_id = Input::get('course_id');
      $outcome_id = Input::get('outcome_id');
      $panel_id = Input::get('panel_id');
      $program_id = DB::table('program_user')
          ->where('user_id',$user_id)
          ->select('program_id')
          ->get();
      $program_id = $program_id[0]->program_id;

      // if the user is a coordinator filtering the approvePanel
      if ($role == '3' && $panel_id == 'approvePanel'){

        // if professor isnt a desired filter, search all professors
        if ($professor_id == 0){
          $all_ta_users = DB::table('transformative_actions')
                        ->where('transformative_actions.is_custom', 1)
                        ->where('transformative_actions.program_id', $program_id)
                        ->where('transformative_actions.by_professor',1)
                        ->select('transformative_actions.user_id')
                        ->get();
          $professor_id = array();
          foreach ($all_ta_users as $key => $user) {
            array_push($professor_id, $user->user_id);
          }
        }else{
          $professor_id = array($professor_id);
        }
        // if course isnt a desired filter, search all courses
        if ($course_id == 0){
          $courses =  DB::table('ta_course')
                    ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
                    ->join('courses','courses.id','=','ta_course.course_id')
                    ->where('transformative_actions.is_custom', 1)
                    ->where('transformative_actions.program_id', $program_id)
                    ->where('transformative_actions.by_professor', 1)
                    ->select('courses.id')
                    ->get();

          $course_id = array();
          foreach ($courses as $key => $course) {
            array_push($course_id, $course->id);
          }
        }else{
          $course_id = array($course_id);
        }
        // if outcome isnt a desired filter, search all outcomes
        if ($outcome_id == 0){
          $outcomes = DB::table('transformative_actions')
                      ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
                      ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
                      ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
                      ->where('transformative_actions.is_custom', 1)
                      ->where('transformative_actions.program_id', $program_id)
                      ->where('transformative_actions.by_professor', 1)
                      ->select('outcomes.id')
                      ->groupBy('outcomes.id')
                      ->get();
          $outcome_id = array();
          foreach ($outcomes as $key => $outcome) {
            array_push($outcome_id, $outcome->id);
          }
        }else{
          $outcome_id = array($outcome_id);
        }

        // search TA with filters
        $filtered_at = DB::table('transformative_actions')
                      ->join('ta_course','ta_course.ta_id','=','transformative_actions.id')
                      ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
                      ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
                      ->where('transformative_actions.is_custom', 1)
                      ->where('transformative_actions.program_id',$program_id)
                      ->where('transformative_actions.by_professor',1)
                      ->whereIn('transformative_actions.user_id',$professor_id)
                      ->whereIn('criterion_objective_outcome.outcome_id',$outcome_id)
                      ->whereIn('ta_course.course_id',$course_id)
                      ->select('transformative_actions.*')
                      ->groupBy('transformative_actions.id')
                      ->orderBy('transformative_actions.at_text', 'ASC')
                      ->get();
        return $filtered_at;
      }
      // if the user is a coordinator filtering the editPanel
      elseif($role == '3' && $panel_id == 'editPanel'){

        // if professor isnt a desired filter, search all professors
        if ($professor_id == 0){
          $all_ta_users = DB::table('transformative_actions')
                        ->where('transformative_actions.is_custom', 1)
                        ->where('transformative_actions.program_id', $program_id)
                        ->where('transformative_actions.by_professor',0)
                        ->select('transformative_actions.user_id')
                        ->get();
          $professor_id = array();
          foreach ($all_ta_users as $key => $user) {
            array_push($professor_id, $user->user_id);
          }
        }else{
          $professor_id = array($professor_id);
        }
        // if course isnt a desired filter, search all courses
        if ($course_id == 0){
          $courses =  DB::table('ta_course')
                    ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
                    ->join('courses','courses.id','=','ta_course.course_id')
                    ->where('transformative_actions.is_custom', 1)
                    ->where('transformative_actions.program_id', $program_id)
                    ->where('transformative_actions.by_professor', 0)
                    ->select('courses.id')
                    ->get();

          $course_id = array();
          foreach ($courses as $key => $course) {
            array_push($course_id, $course->id);
          }
        }else{
          $course_id = array($course_id);
        }
        // if outcome isnt a desired filter, search all outcomes
        if ($outcome_id == 0){
          $outcomes = DB::table('transformative_actions')
                      ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
                      ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
                      ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
                      ->where('transformative_actions.is_custom', 1)
                      ->where('transformative_actions.program_id', $program_id)
                      ->where('transformative_actions.by_professor', 0)
                      ->select('outcomes.id')
                      ->groupBy('outcomes.id')
                      ->get();
          $outcome_id = array();
          foreach ($outcomes as $key => $outcome) {
            array_push($outcome_id, $outcome->id);
          }
        }else{
          $outcome_id = array($outcome_id);
        }

        // search TA with filters
        $filtered_at = DB::table('transformative_actions')
                      ->join('ta_course','ta_course.ta_id','=','transformative_actions.id')
                      ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
                      ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
                      ->where('transformative_actions.is_custom', 1)
                      ->where('transformative_actions.program_id',$program_id)
                      ->where('transformative_actions.by_professor',0)
                      ->whereIn('transformative_actions.user_id',$professor_id)
                      ->whereIn('criterion_objective_outcome.outcome_id',$outcome_id)
                      ->whereIn('ta_course.course_id',$course_id)
                      ->select('transformative_actions.*')
                      ->groupBy('transformative_actions.id')
                      ->orderBy('transformative_actions.at_text', 'ASC')
                      ->get();
        return $filtered_at;
      }
      // if the user is a professor filtering the editPanel
      elseif($role == '4' && $panel_id == 'editPanel'){

        // if course isnt a desired filter, search all courses
        if ($course_id == 0){
          $courses = DB::table('ta_course')
                    ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
                    ->join('courses','courses.id','=','ta_course.course_id')
                    ->where('transformative_actions.user_id', Auth::user()->id)
                    ->where('transformative_actions.is_custom', 1)
                    ->where('transformative_actions.program_id', $program_id)
                    ->where('transformative_actions.by_professor', 1)
                    ->select('courses.id')
                    ->get();

          $course_id = array();
          foreach ($courses as $key => $course) {
            array_push($course_id, $course->id);
          }
        }else{
          $course_id = array($course_id);
        }
        // if outcome isnt a desired filter, search all outcomes
        if ($outcome_id == 0){
          $outcomes = DB::table('transformative_actions')
                      ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
                      ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
                      ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
                      ->where('transformative_actions.user_id', Auth::user()->id)
                      ->where('transformative_actions.is_custom', 1)
                      ->where('transformative_actions.program_id', $program_id)
                      ->where('transformative_actions.by_professor', 1)
                      ->select('outcomes.id')
                      ->groupBy('outcomes.id')
                      ->get();
          $outcome_id = array();
          foreach ($outcomes as $key => $outcome) {
            array_push($outcome_id, $outcome->id);
          }
        }else{
          $outcome_id = array($outcome_id);
        }

        // search TA with filters
        $filtered_at = DB::table('transformative_actions')
                      ->join('ta_course','ta_course.ta_id','=','transformative_actions.id')
                      ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
                      ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
                      ->where('transformative_actions.user_id', Auth::user()->id)
                      ->where('transformative_actions.is_custom', 1)
                      ->where('transformative_actions.program_id',$program_id)
                      ->where('transformative_actions.by_professor',1)
                      ->whereIn('criterion_objective_outcome.outcome_id',$outcome_id)
                      ->whereIn('ta_course.course_id',$course_id)
                      ->select('transformative_actions.*')
                      ->groupBy('transformative_actions.id')
                      ->orderBy('transformative_actions.at_text', 'ASC')
                      ->get();
        return $filtered_at;
      }
      return 'ilegal';
    }

}