<?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'));
	}

	/**
	 * Show the form for creating a new rubric
	 *
	 * @return Response
	 */
	public function newTemplate()
	{
		$title= "Rubric Builder";

		$templates = Template::orderBy('name', 'ASC')->get();
		$outcomes = Outcome::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->contents = Input::get('contents');
		$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;
		}

		if($template->save())
		{
			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()
	{
		return Template::find(Input::get('id'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @return Response
	 */
	public function update()
	{
		$template = Template::find(Input::get('id'));
		$template->name = Input::get('name');
		$template->contents = Input::get('contents');
		$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($template->save())
		{
			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();
		}
	}

}