PEEP

Programming Economic Experiments with Php/mysql

with contributions by:
Jeroen van de Ven
Lucas Molleman
Joep Sonnemans
Roel van Veldhuizen
Boris van Leeuwen
Jona Linde

Chatbox


Click to enlarge

X
X

In group decision making experiments group members can discuss the problem. You can do that face-to-face and videotape everything, but the coding will be a lot of work. For many applications interaction by computer is a very good alternative. Nowadays for young people such way of interacting comes quite naturally. The whole conversation can be saved, including timestamps.
The two screenshots above are from an experiment about juridicial decision making where we compare individual and group decisions. The judges have to acquit or convict a suspect when there is (mostly) conflicting evidence that can be weak or strong. In each period each individual makes a decision, than the members of the group enter a groups discussion and they make a group decision, and than again the individuals make a decision (so we can see wether the discussion also has influence the individual opinion). Left you see the chatbox screen. The button "decision" at the bottom of the screen appears only after every group member has contributed something to the discussion. New messages appear at the top of the conversation. The right image shows the next screen; the individuals vote on the decision and can still see the finished conversation.
The larger part of the chatbox page is adapted from the most simple implementation I found on internet (at that time). For each conversation a new text-file is used; for example group 3, period 4 has the text file msg34.txt. These empty text files have to be made in advance and the webserver should be allowed to make changes to that file (the permissions are set to 777). The conversation is shown in an iframe.

<?php
Header("Cache-Control: no-cache, must-revalidate");
include("common.inc");
$groep=1+floor(($ppnummer-.5)/3);
$code=chr(($ppnummer-1)%3+65);
$voorstel=$koek[3];
$laatstgezien=$koek[4];
$period=lookUp("ppnummers","ppnummer='$ppnummer'","period");
if ($period==0) {
	$period=1;
	updateTable("ppnummers","ppnummer='$ppnummer'","deel","1");
	updateTable("ppnummers","ppnummer='$ppnummer'","period","1");
}
$bewijstekst=bewijsteksteng($period);
// Config : [ You can change these options ]
$refresh = 5 ; // Page refresh time in seconds
$max = 40 ; // Max. saved messages
$remain = 400 ; // Messages remaining time in seconds
$file = "msg".$groep.$period.".txt" ; // Message storing file
/*
	You may need to make an empty file (use notepad) and name it "msg.txt"
	($file), and send it to your server. Then CHMOD this file to 777 - e.g :
	ftp> SITE CHMOD 777 msg.txt
*/
// End of config //
$user = $code ;
$f = file($file) ;
$users = array($user) ;
header ("Content-Type: text/html; charset=utf-8") ; 

//new message
if ( isset($HTTP_POST_VARS['msg']) && $HTTP_POST_VARS['msg']!="") {
	$fp = fopen($file, "w") ;
	if (!$fp) { die ('Can not write to file .') ; }
	for ($i=0; isset($f[$i])||$i==0; $i++) 
	{
		$e=explode('||', $f[$i]) ;
		if ($e[2]=="\r\n" && !in_array($e[0], $users) && ( time() - $e[1] < $refresh*2 ) ) $users[]=$e[0] ;
		if ($i==0) 
		{ 
			if ( isset($HTTP_POST_VARS['msg']) && ($HTTP_POST_VARS['msg']!="")) fputs ($fp, $user."||".date("H:i:s")."||".str_replace("||", "", str_replace("\n", "", htmlspecialchars(stripslashes($HTTP_POST_VARS['msg']))))."||".$laatstgezien."\r\n") ;	
		}
		if ( (strlen($f[$i])>5) && ($e[0]!=$user && $e[2]=="\r\n") || ($e[2]!="\r\n") ) fputs($fp, $f[$i]) ;
	}
	fclose($fp) ;
	//save in database
	$connection = @mysql_connect(HOST,ADMIN, WWOORD) or die(mysql_error());
	$db = @mysql_select_db(DBNAME,$connection)or die(mysql_error());
	$sql="Update chat Set chat = CONCAT(chat, '$code') Where (groep=$groep and period=$period)";
	$result=@mysql_query($sql,$connection) or die("Couldn't execute query ".$sql);
}

if (isset($HTTP_GET_VARS['f1'])){ 
	$f2 = file($file) ;
	$e=explode("||", $f2[0]) ;
	$laatstgezien=$e[1];
	$cookie_val = $ppnummer."-".$groep."-".$code."-".$voorstel."-".$laatstgezien;
	setcookie("beheerder", $cookie_val, time()+7200); 
	echo("<html><head><title>Chat text frame</title><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head>") ;
	echo("<body text=#0000cc><meta http-equiv='refresh' content='{$refresh}; url={$HTTP_SERVER_VARS['REQUEST_URI']}'>") ;
	for($i=0; isset($f2[$i]); $i++) { 
		$e=explode("||", $f2[$i]) ;
		if ($e[2]!="\r\n") echo "<font color=green>{$e[0]}</font> <font color=black>{$e[2]}</font><br>\r\n" ;
	}
	die("</body></html>") ;
}
else { 
	echo
	"<html><head><title>Chat</title><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
	<script language=\"JavaScript\">
	function blockEnter(evt) {
	  evt = (evt) ? evt : event;
	  var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	  if (charCode == 13 || charCode == 3) {
	    return false;
	  } else {
	    return true;
	  }
	}
	</script>
	<script language=\"javascript\">
	     window.history.forward(1);
	</script>
	<style type=\"text/css\">
		strong.button { font-size:20}	 
		strong.roodbutton { color: red; font-size:20}
		strong.blauwbutton { color: blue; font-size:20}
		strong.Rood { color: red;}
		strong.Blauw { color: blue;}
	</style>
		</head>
		<body align=\'center\' OnLoad=\"document.m.msg.focus(); opdracht()\";> 
		<script type=\"text/javascript\">
		function confirm_box() {
			var valid=1;
			var prop=document.m.proposal.value;
			if (prop!=\"\"){
				var GoodChars = \"0123456789\";
				var i = 0;
				for (i =0; i <= prop.length -1; i++) {
					if (GoodChars.indexOf(prop.charAt(i)) == -1) {
					valid=0;
					}
				}
			}
			else if (prop>1200 || prop <0) {
				valid=0;
			}
			if (valid==1){
				return true;
			}
			else {
				alert(\"Vul als voorstel alleen gehele getallen in tussen 0 en 1200!\");
				return false;
			}
		}
		</script><H1 align=center>Period ".$period." of ".$numberperiods1.": judiciary panel</H1><br>".$bewijstekst."
		<p>
		<table width=95% align=center>
			<tr>
				<td>
					<form  name=\"m\" action=\"\" method=\"post\" onsubmit=\"return confirm_box()\">
					Message: <input name=\"msg\" size=80 AUTOCOMPLETE=\"OFF\"> <input type=\"submit\" name=\"send\" value=\"Send\">
					</form>
				</td>

				<td align=right>
				<i>(The last message is displayed at the top, you are ".$code.")</i>
				</td>
			</tr>
		</table>
		<center>
		<iframe src=\"?f1=1\" width=\"95%\" height=\"60%\"></iframe><br>		
		</body></html>";
	die("") ;
}
?>
<p align=center>
<H1 align=center>Period <?php echo $period." of ".$numberperiods1.": the chamber of judges"; ?></H1><br>
<?php echo $bewijstekst; ?>
<p  align=center>
<i>At the end of the experiment you will learn for each period whether the accused was the perpetrator or innocent, and what decision will be paid out.</i>
</body>
</html>

The msg file looks like this (last message at top):
A||12:22:01||A has left the chatbox and makes a decision.||12:20:58
C||12:20:58||all right||12:20:19
A||12:20:58||I agree with B: 65% and acquit||12:20:19
B||12:20:19||Yes, but not enough to convict||12:20:12
C||12:17:57||More likely he is guilty||