<?php // $Id: mark_free_answer.php,v 1.1.2.1 2005/08/30 01:47:37 yannoo Exp $
/*
============================================================================== 
	Dokeos - elearning and course management software
	
	Copyright (c) 2004 Dokeos S.A.
	Copyright (c) 2005 Yannick Warnier <yannick.warnier@dokeos.com>
	
	For a full list of contributors, see "credits.txt".
	The full license can be read in "license.txt".
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	See the GNU General Public License for more details.
	
	Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
============================================================================== 
*/
/**
============================================================================== 
*	FREE ANSWER MARKING SCRIPT  
*
*	This script allows a course tutor to mark a student's free answer.
*@author Yannick Warnier <yannick.warnier@dokeos.com>
*	@package dokeos.exercise
============================================================================== 
*/

/*
==============================================================================
		INIT SECTION
==============================================================================
*/

include('exercise.class.php');
include('question.class.php');
include('answer.class.php');

// answer types
define('UNIQUE_ANSWER',	1);
define('MULTIPLE_ANSWER',	2);
define('FILL_IN_BLANKS',	3);
define('MATCHING',		4);
define('FREE_ANSWER', 5);

$langFile='exercice';

include('../inc/claro_init_global.inc.php');

include(api_get_library_path().'/text.lib.php');

$TBL_EXERCICE_QUESTION = $_course['dbNameGlu'].'quiz_rel_question';
$TBL_EXERCICES         = $_course['dbNameGlu'].'quiz';
$TBL_QUESTIONS         = $_course['dbNameGlu'].'quiz_question';
$TBL_REPONSES          = $_course['dbNameGlu'].'quiz_answer';

//debug param. 0: no display - 1: debug display
$debug=0;
if($debug>0){echo str_repeat('&nbsp;',0).'Entered exercise_result.php'."<br />\n";var_dump($_POST);}

// general parameters passed via POST/GET
$my_course_code = mysql_real_escape_string($_GET['cid']);
if(!empty($_REQUEST['exe'])){
	$my_exe = $_REQUEST['exe'];
}else{
	$my_exe = null;
}
if(!empty($_REQUEST['qst'])){
	$my_qst = $_REQUEST['qst'];
}else{
	$my_qst = null;
}
if(!empty($_REQUEST['usr'])){
	$my_usr = $_REQUEST['usr'];
}else{
	$my_usr = null;
}
if(!empty($_REQUEST['cidReq'])){
	$my_cid = $_REQUEST['cidReq'];
}else{
	$my_cid = null;
}
if(!empty($_POST['action'])){
	$action = $_POST['action'];
}else{
	$action = '';
}

if (empty($my_qst) or empty($my_usr) or empty($my_cid) or empty($my_exe)){
	header('Location: exercice.php');
	exit();
}

if(!$is_courseTutor)
{
	api_not_allowed();
}

$obj_question=new Question();
$obj_question->read($my_qst);

$nameTools=get_lang('Exercice');

$interbredcrump[]=array("url" => "exercice.php","name" => get_lang('Exercices'));

$my_msg = 'No change.';

if($action == 'mark'){
	if (!empty($_POST['score']) AND $_POST['score'] < $obj_question->selectWeighting() AND $_POST['score'] >= 0){
		
		//mark the user mark into the database using something similar to the following function:
		global $is_trackingEnabled, $statsDbName;
		if($is_trackingEnabled){
			$exercise_table = Database::get_statistic_table('track_e_exercices');
			#$tbl_learnpath_user = Database::get_course_table('learnpath_user');
			#global $origin, $tbl_learnpath_user, $learnpath_id, $learnpath_item_id;
			$sql = "SELECT * FROM $exercise_table 
				WHERE exe_user_id = '$my_usr' AND exe_cours_id = '$my_cid' AND exe_exo_id = '$my_exe' 
				ORDER BY exe_date DESC";
			#echo $sql;
			$res = api_sql_query($sql,__FILE__,__LINE__);
			if(mysql_num_rows($res)>0){
				$row = mysql_fetch_array($res);
				//@todo Check that just summing past score and the new free answer mark doesn't come up
				// with a score higher than the possible score for that exercise
				$my_score = $row['exe_result'] + $_POST['score'];
				$sql = "UPDATE $exercise_table SET exe_result = '$my_score' 
					WHERE exe_id = '".$row['exe_id']."'";
				#echo $sql;
				$res = api_sql_query($sql,__FILE__,__LINE__);
				$my_msg = "Updated";
			}else{
				$my_score = $_POST['score'];
				$reallyNow = time();
				$sql = "INSERT INTO $exercise_table
						  (
						   `exe_user_id`,
						   `exe_cours_id`,
						   `exe_exo_id`,
						   `exe_result`,
						   `exe_weighting`,
						   `exe_date`
						  )
				
						  VALUES
						  (
						  ".$my_usr.",
						   '".$my_cid."',
						   '".$my_exe."',
						   '".$my_score."',
						   '".$obj_question->selectWeighting()."',
						   FROM_UNIXTIME(".$reallyNow.")
						  )";
				#if ($origin == 'learnpath')
				#{
				#	if ($user_id == "NULL")
				#	{
				#		$user_id = '0';
				#	}
				#	$sql2 = "update `$tbl_learnpath_user` set score='$score' where (user_id=$user_id and learnpath_id='$learnpath_id' and learnpath_item_id='$learnpath_item_id')";
				#	$res2 = api_sql_query($sql2,__FILE__,__LINE__);
				#}
				$res = api_sql_query($sql,__FILE__,__LINE__);
				$my_msg = "Inserted";
			}
			//$mysql_query($sql);
			//return 0;
		}
	}else{
		$my_msg .= " There might have been a problem with the total score being too big...<br />\n"
	}
}

Display::display_header($nameTools,"Exercise");

// Display simple marking interface

// 1a - result of previous marking then exit suggestion
// 1b - user answer and marking box + submit button
if($action == 'mark'){
	echo "Status: ".$my_msg.". Use menu to do other tasks.<br />\n";
}else{
	echo "You received the user's answer to question ".$my_qst." (".$obj_question->selectTitle().") by email.<br />\n";
	echo "How would you rate his/her answer on a scale from 0 to ".$obj_question->selectWeighting()." ?<br />\n";
	echo "<form action='' method='POST'>\n"
		."<input type='hidden' name='exe' value='$my_exe'>\n"
		."<input type='hidden' name='usr' value='$my_usr'>\n"
		."<input type='hidden' name='cidReq' value='$my_cid'>\n"
		."<input type='hidden' name='action' value='mark'>\n"
		."<input type='text' name='score' value=''>\n"
		."<input type='submit' name='submit' value='".get_lang('Ok')."'>\n"
		."</form>";
}

Display::display_footer();

?>
