<?php

include_once (dirname(__FILE__).'/../../../inc/global.inc.php');
include_once (dirname(__FILE__).'/../be.inc.php');

/**
 * Table to display results for an evaluation
 * @author Stijn Konings
 * @author Bert Stepp�
 */
class ResultTable extends SortableTable
{

	private $datagen;
	private $evaluation;
	private $allresults;
	private $iscourse;


	/**
	 * Constructor
	 */
    function ResultTable ($evaluation, $results = array(), $iscourse, $addparams = null,$forprint = false)
    {
    	parent :: SortableTable ('resultlist', null, null, 1);

		$this->datagen = new ResultsDataGenerator($evaluation, $results, true);

		$this->evaluation = $evaluation;
		$this->iscourse = $iscourse;
		$this->forprint = $forprint;
		
		if (isset ($addparams))
			$this->set_additional_parameters($addparams);

		$scoredisplay = ScoreDisplay :: instance();

		$column= 0;
		if ($this->iscourse == '1')
		{
			$this->set_header($column++, '', false);
			$this->set_form_actions(array (
					'delete' => get_lang('Delete')
			));
		}
		$this->set_header($column++, get_lang('LastName'));
		$this->set_header($column++, get_lang('FirstName'));
		$this->set_header($column++, get_lang('Score'));
		if ($scoredisplay->is_custom())
			$this->set_header($column++, get_lang('Display'));
		if (!$this->forprint)
			$this->set_header($column++, get_lang('Modify'),false);
		
    }


	/**
	 * Function used by SortableTable to get total number of items in the table
	 */
	function get_total_number_of_items()
	{
		return $this->datagen->get_total_results_count();
	}


	/** 
	 * Function used by SortableTable to generate the data to display
	 */
	function get_table_data($from = 1)
	{

		$scoredisplay = ScoreDisplay :: instance();

		// determine sorting type
		$col_adjust = ($this->iscourse == '1' ? 1 : 0);
		switch ($this->column)
		{
			// Type
			case (0 + $col_adjust):
				$sorting = ResultsDataGenerator :: RDG_SORT_LASTNAME;
				break;
			case (1 + $col_adjust):
				$sorting = ResultsDataGenerator :: RDG_SORT_FIRSTNAME;
				break;
			case (2 + $col_adjust):
				$sorting = ResultsDataGenerator :: RDG_SORT_SCORE;
				break;
			case (3 + $col_adjust):
				$sorting = ResultsDataGenerator :: RDG_SORT_MASK;
				break;
		}
		if ($this->direction == 'DESC')
			$sorting |= ResultsDataGenerator :: RDG_SORT_DESC;
		else
			$sorting |= ResultsDataGenerator :: RDG_SORT_ASC;


		$data_array = $this->datagen->get_data($sorting, $from, $this->per_page);


		// generate the data to display
		$sortable_data = array();
		foreach ($data_array as $item)
		{
			$row = array ();
			if ($this->iscourse == '1')
				 $row[] = $item['result_id'];	
			$row[] = $item['lastname'];
			$row[] = $item['firstname'];
			$row[] = $item['score'];
			if ($scoredisplay->is_custom())
				$row[] = $item['display'];
			if (!$this->forprint)
				$row[] = $this->build_edit_column ($item);
			$sortable_data[] = $row;
		}
		
		return $sortable_data;
	}




	
// Other functions

	private function build_edit_column ($item)
	{
		$edit_column = '<a href="' . api_get_self() . '?editres=' . $item['result_id'] . '&selecteval=' . $this->evaluation->get_id() . '"><img src="../img/edit.gif" border="0" title="' . get_lang('Modify') . '" alt="" /></a>';
		if ($this->evaluation->get_course_code() == null)
			$edit_column.= '&nbsp;<a href="' . api_get_self() . '?resultdelete=' . $item['result_id'] . '&selecteval=' . $this->evaluation->get_id() . '" onclick="return confirmationuser();"><img src="../img/delete.gif" border="0" title="' . get_lang('Delete') . '" alt="" /></a>';
		$edit_column.= '&nbsp;<a href="user_stats.php?userid=' . $item['id'] . '&selecteval=' . $this->evaluation->get_id() . '"><img src="../img/statistics.gif" width="17px" border="0" title="' . get_lang('Statistics') . '" alt="" /></a>';

		// evaluation's origin is a link
		if ($this->evaluation->get_category_id() < 0)
		{
			$link = LinkFactory :: get_evaluation_link ($this->evaluation->get_id());

			$doc_url = $link->get_view_url($item['id']);
			if ($doc_url != null)
				$edit_column .= '&nbsp;<a href="'. $doc_url . '" target="_blank">'
								.'<img src="'. api_get_path(WEB_CODE_PATH) . 'img/link.gif" border="0" title="' . get_lang('OpenDocument') . '" alt="" />'
								.'</a>';
		}

		return $edit_column;
		
	}

}
?>