<?php
/*
 * Discovery Tiki by 
 *	porting http://fabien.benetou.fr/pub/pim_functions.php.txt
 *	by following http://dev.tiki.org/Hello+World#To_create_a_new_plugin
 * test in http://localhost/tiki/tiki-index.php?page=Testing
 */


//the info function is needed for tw >= 3.0
function wikiplugin_pjsviz_help() {
	return "do the viz";
}

function wikiplugin_pjsviz_info() {
	return array(
		'name' => tra('Pjsviz'),
		'documentation' => 'PJS Viz', 
		'description' => tra('Display a visualization of edits.'), 
		'format' => 'html', // html or wiki
		'prefs' => array( 'wikiplugin_pjsviz'),
		// else fails on the feature_myfeature
		// each plugin can have an admin setting(pref) to enable them
		'body' => tra('explain what the body of the plugin means'),
		//'validate' =>'all',
		//add this line if each insertion of this plugin needs to be validated by an admin,
		// (because new or modified plugin body or plugin arguments is found). Possible values are: 'all', body' or 'arguments'.
		'params' => array( 
			/*
				'title' => array( 
				'required' => false, 
				'name' => tra('Title'), 
				'description' => tra('Describe what title is'), 
			), 
			 */
		),
	); 
}   

function wikiplugin_pjsviz($data, $params) { 
	require_once ('tiki-setup.php');
	include_once ('lib/wiki/histlib.php');
	$pagename = "Testing";
	$history = $histlib->get_page_history($pagename);
	//var_dump($history); returned from the newest to the oldest
	$pjs_path = "/tiki/lib/processingjs/processing.min.js";
	$processinglocalfile = 'e:\webserver\htdocs\tiki\lib\processingjs\edits_per_page\\'.$pagename.".pjs";
        $processingfile = "/tiki/lib/processingjs/edits_per_page/$pagename.pjs";	
        $canvasheight = 600-2*10;
        $canvaswidth = 100;
        $block_width = 90;
        $block_height = 4;	
	$content = "";
	foreach ($history as $edit){
		$diffs[] = intval($edit["lastModif"]);
	}
	//var_dump($diffs);
	$min = min($diffs);
	$max = max($diffs);
	$number_of_diffs = count($history);
        $now = time();
	$content .= "<script src=\"$pjs_path\" type=\"text/javascript\"></script>";
        $canvas = "
                void setup()
                {
                  size($canvaswidth, $canvasheight);
PFont font;
font = loadFont(\"FFScala-Bold-12.vlw\");
textFont(font);
                }
                void mouseMoved() {
                  checkButtons();
                }

                void mouseDragged() {
                  checkButtons();
                }
                void draw()
                {
                  background(251);
                  fill(0,0,255,20);
                noStroke();
";
	foreach ($diffs as $diff){
                $x = 5;
                $y = round ( ($canvasheight - $block_height) * ( (($now - $min)-($now-$diff)) / ($now - $min)));
                $canvas .= "\t\trect($x,$y,$block_width,$block_height);\n";		
	}

        $canvas .= "\t\tstroke(0,155);\n";
        $canvas .= "\t\tfill(0,0,255,80);\n";
        $canvas .= "\t\ttext(\"Edits:\",2,10 );\n";
	$canvas .= "}";

        $write_result = file_put_contents($processinglocalfile,$canvas);	
	//here instead save to MongoDB

	$content .= '<canvas data-src="'.$processingfile.'" width="'.$canvaswidth.'" height="'.$canvasheight.'"></canvas><br/>';
	return $content;

	//*************************************************************
	//kept just to use if parameters are later given
	if( isset( $params["title"] ) ) {
		$title = $params["title"]; 
		return "Hello World __".$title."__ $data!"; 
	} else { 
		// Indicate that a parameter is missing 
		return WikiParser_PluginOutput::argumentError( array('title') ); 
	} 
	//*************************************************************
} 

?>