<commit_before><?
/**
 *  Functions relating to the display and creation of codes
 *
 *
 *      This file is part of queXC
 *      
 *      queXC is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU Affero General Public License as published by
 *      the Free Software Foundation; either version 3 of the License, or
 *      (at your option) any later version.
 *      
 *      queXC is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU Affero General Public License for more details.
 *      
 *      You should have received a copy of the GNU Affero General Public License
 *      along with queXC; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Deakin University 2007,2008,2009
 * @package queXC
 * @subpackage functions
 * @link http://www.deakin.edu.au/dcarf/ queXC was writen for DCARF - Deakin Computer Assisted Research Facility
 * @license http://opensource.org/licenses/agpl-v3.html The GNU Affero General Public License (AGPL) Version 3
 * 
 */

/**
 * Configuration file
 */
include_once(dirname(__FILE__).'/../config.inc.php');

/**
 * Database file
 */
include_once(dirname(__FILE__).'/../db.inc.php');

/**
 * Display a group of codes given the column_group_id
 *
 * @param int $column_group_id The column_group_id
 */
function display_code_group($column_group_id)
{
	global $db;

	$codes = array();
	$last = 0; //Make sure we don't automatically select this code
	$allow = false; //Not allowed to automatically add codes

	//First see if we are allowed to add codes to this code
	$sql = "SELECT cg.allow_additions, cg.code_group_id
		FROM code_group as cg, `column_group` as cog
		WHERE cg.code_group_id = cog.code_group_id
		AND cog.column_group_id = $column_group_id";
	
	$allowa = $db->GetRow($sql);
	
	if (!empty($allowa) && $allowa['allow_additions'] == 1)
		$allow = true;

	$code_group_id = $allowa['code_group_id'];

	$sql = "SELECT c.code_level_id, c.column_id
		FROM `column` as c, code_level as cl
		WHERE c.column_group_id = '$column_group_id'
		AND cl.code_level_id = c.code_level_id	
		ORDER BY cl.level ASC";

	$levels = $db->GetAll($sql);

	//display levels only up to where selected

	foreach($levels as $level)
	{
		$code_level_id = $level['code_level_id'];
		$column_id = $level['column_id'];

		$sql = "SELECT c.code_id, c.label, c.value
			FROM `code` as c
			WHERE c.code_level_id = '$code_level_id'";

		$codes = $db->GetAll($sql);

		print "<div class='level'>";
		foreach($codes as $code)
		{
			print "<div class='row'><input class='rab' type='radio' name='cc" . $code_level_id . "X" . $column_group_id . "' value='{$code['value']}' id='ccc{$code['code_id']}X" . $code_level_id . "X" . $column_group_id . "'";
	//if ($base && $r['code_id'] == $c && !($last == 1 && ($count == $total)))
	//{
	//	print " checked='checked'";
	//			$lastcode = 1;
	//			$parentcode = $r['code_id'];
	//		}
			print "/><label for='ccc{$code['code_id']}X" . $code_level_id . "X" . $column_group_id . "'>{$code['value']}:{$code['label']}</label></div>";
		}
		print "</div>";
	}
}


/**
 * Display codes given the work_id
 * 
 * @param int $work_unit_id The given work_unit_id
 * @param bool|int $operator_id The operator_id
 * @param string $cdata The code data to search on
 * @see display_all_codes
 */
function display_codes($work_unit_id, $operator_id = false,$cdata = "")
{
	global $db;

	//If the operator_id is set, find out if auto_code is set for this operator and process
	$sql = "SELECT op.auto_code, w.column_multi_group_id as cmgi, w.column_group_id as cgi, ce.row_id as row_id
		FROM operator_process as op, work as w, work_unit as wu, `cell` as ce
		WHERE op.operator_id = $operator_id
		AND op.process_id = w.process_id
		AND w.work_id = wu.work_id
		AND wu.work_unit_id = '$work_unit_id'
		AND ce.cell_id = wu.cell_id";
	
	$ac = $db->GetRow($sql);

	//If this is a multi-group - loop over each unique column group and display entire code set
	if (!empty($ac['cmgi'])) //multi group
	{
		$sql = "SELECT c.column_group_id as cgi, c.column_id, c.description
			FROM `column` as c
			WHERE c.column_multi_group_id = '{$ac['cmgi']}'
			GROUP BY c.column_group_id";

		$rs = $db->GetAll($sql);
		
		print "<script type='text/javascript'>";
		print "document.addEvent('domready', function(){ function jsfcheck(item, state){ alert('test');  } var testMenu = new Mif.Menu().attach('menu-target').load(";
	
		$json = array();
		

		foreach($rs as $r)
		{
			//display root element (checkbox)

			$column_group_id = $r['cgi'];
			$column_description = $r['description'];
		
			$tmp = (object) array('name' => $r['description'], 'id' => 'ci' . $r['column_id'], 'submenu' => 0);
			$tmp->submenu = array();


			//can use action to call a function

			$codes = array();
		
			$sql = "SELECT c.code_level_id, c.column_id
				FROM `column` as c, code_level as cl
				WHERE c.column_group_id = '{$r['cgi']}'
				AND cl.code_level_id = c.code_level_id	
				ORDER BY cl.level ASC";

			$levels = $db->GetAll($sql);


			foreach($levels as $level)
			{
				$code_level_id = $level['code_level_id'];
				$column_id = $level['column_id'];

				$sql = "SELECT c.code_id, c.label, c.value
					FROM `code` as c
					WHERE c.code_level_id = '$code_level_id'";

				$codes = $db->GetAll($sql);

/*				$tmp->submenu[] = (object) array('options' => "{
						onCheck: function(item, state){
							alert('test');
						}
					}");*/

				foreach($codes as $code)
				{
					//print "<div class='row'><input class='rab' type='radio' name='cc" . $code_level_id . "X" . $column_group_id . "' value='{$code['value']}' id='ccc{$code['code_id']}X" . $code_level_id . "X" . $column_group_id . "'";


					//This javascript needs to:
					//	1. search for an existing element of the same name, and if so, append text to it/update value
					//	2. add a new radio input element with the selected value and the text selected - clicking itself should destroy itself
					$jse = "cc" . $code_level_id . "X" . $column_group_id;
					$jset = "tt" . $column_group_id;
					$jsed = "dd" . $code_level_id . "X" . $column_group_id;
					$value = $code['value'];
					$label = $code['label'];

					$js = "	function(item, state) {
							var el = $('$jse');

							if (el != null)
							{
								$('$jsed').destroy();
							}

							txt = $('selectedText').get('value');
							tel = new Element('input');
							tel.set('name', '$jset');
							tel.set('value', txt);
							tel.set('size', txt.length);
							tel.set('id', '$jset');
							tel.set('type', 'text');
							tel.set('readonly','readonly');
							nel = new Element('input');
							nel.set('name', '$jse');
							nel.set('value', '$value');
							nel.set('id', '$jse');
							nel.set('type', 'radio');
							nel.set('checked','true');
							lel = new Element('label');
							lel.set('for', '$jse');
							lel.set('html', '$column_description - $value:$label');
							lel.set('onclick','$(\'$jsed\').destroy();');							

							del = new Element('div');
							del.set('id','$jsed');
							del.adopt(tel);
							del.adopt(lel);
							del.adopt(nel);
							document.id('cleancode').adopt(del);
						}";
					

					$tmp->submenu[] = (object) array('name' => $code['label'], 'checked' => 'false', 'group' => 'ci' . $r['column_id'], 'check' => 'jsfcheck', 'action' => $js);
				}
				break; //temporary: only use first level
			}
			
			$json[] = $tmp;
		}

		print json_encode($json);
		print ")})</script>";
	}
	else if (empty($ac) || $ac['auto_code'] == 0)
	{
		//get a code from this code group

		$sql = "SELECT c.code_id,w.column_group_id
			FROM code as c
			JOIN work_unit AS wu ON (wu.work_unit_id = '$work_unit_id')
			JOIN work as w ON (w.work_id = wu.work_id)
			JOIN column_group as cg ON (cg.column_group_id = w.column_group_id)
			JOIN code_level as cl ON (cl.code_group_id = cg.code_group_id AND c.code_level_id = cl.code_level_id)
			LIMIT 1";

		$r = $db->GetRow($sql);
	
		if (!empty($r))
			display_all_codes($r['code_id'],false,$work_unit_id);
	}
	else
	{
		$sql = "SELECT c.code_id, (c.label LIKE '%$cdata%' OR c.keywords LIKE '%$cdata%') as test,w.column_group_id
			FROM code as c
			JOIN work_unit as wu ON (wu.work_unit_id = '$work_unit_id'
			JOIN work as w ON (w.work_id = wu.work_id)
			JOIN column_group as cg ON (cg.column_group_id = w.column_group_id)
			JOIN code_level as cl ON (cl.code_group_id = cg.code_group_id AND c.code_level_id = cl.code_level_id)
			ORDER BY (c.label LIKE '%$cdata%' OR c.keywords LIKE '%$cdata%') DESC, cl.level ASC
			LIMIT 1";

		$r = $db->GetRow($sql);

		if (!empty($r))
		{
			$tmp = true;
			if ($r['test'] == 0) $tmp = false;
			display_all_codes($r['code_id'],$tmp,$work_unit_id);
		}


	}
}

/**
 * Display multiple choice codes (root is multiple choice, each code (if any) is a child
 * Each column is a root multiple choice code
 *<commit_msg>Bug: missing parentheses<commit_after><?
/**
 *  Functions relating to the display and creation of codes
 *
 *
 *      This file is part of queXC
 *      
 *      queXC is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU Affero General Public License as published by
 *      the Free Software Foundation; either version 3 of the License, or
 *      (at your option) any later version.
 *      
 *      queXC is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU Affero General Public License for more details.
 *      
 *      You should have received a copy of the GNU Affero General Public License
 *      along with queXC; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Deakin University 2007,2008,2009
 * @package queXC
 * @subpackage functions
 * @link http://www.deakin.edu.au/dcarf/ queXC was writen for DCARF - Deakin Computer Assisted Research Facility
 * @license http://opensource.org/licenses/agpl-v3.html The GNU Affero General Public License (AGPL) Version 3
 * 
 */

/**
 * Configuration file
 */
include_once(dirname(__FILE__).'/../config.inc.php');

/**
 * Database file
 */
include_once(dirname(__FILE__).'/../db.inc.php');

/**
 * Display a group of codes given the column_group_id
 *
 * @param int $column_group_id The column_group_id
 */
function display_code_group($column_group_id)
{
	global $db;

	$codes = array();
	$last = 0; //Make sure we don't automatically select this code
	$allow = false; //Not allowed to automatically add codes

	//First see if we are allowed to add codes to this code
	$sql = "SELECT cg.allow_additions, cg.code_group_id
		FROM code_group as cg, `column_group` as cog
		WHERE cg.code_group_id = cog.code_group_id
		AND cog.column_group_id = $column_group_id";
	
	$allowa = $db->GetRow($sql);
	
	if (!empty($allowa) && $allowa['allow_additions'] == 1)
		$allow = true;

	$code_group_id = $allowa['code_group_id'];

	$sql = "SELECT c.code_level_id, c.column_id
		FROM `column` as c, code_level as cl
		WHERE c.column_group_id = '$column_group_id'
		AND cl.code_level_id = c.code_level_id	
		ORDER BY cl.level ASC";

	$levels = $db->GetAll($sql);

	//display levels only up to where selected

	foreach($levels as $level)
	{
		$code_level_id = $level['code_level_id'];
		$column_id = $level['column_id'];

		$sql = "SELECT c.code_id, c.label, c.value
			FROM `code` as c
			WHERE c.code_level_id = '$code_level_id'";

		$codes = $db->GetAll($sql);

		print "<div class='level'>";
		foreach($codes as $code)
		{
			print "<div class='row'><input class='rab' type='radio' name='cc" . $code_level_id . "X" . $column_group_id . "' value='{$code['value']}' id='ccc{$code['code_id']}X" . $code_level_id . "X" . $column_group_id . "'";
	//if ($base && $r['code_id'] == $c && !($last == 1 && ($count == $total)))
	//{
	//	print " checked='checked'";
	//			$lastcode = 1;
	//			$parentcode = $r['code_id'];
	//		}
			print "/><label for='ccc{$code['code_id']}X" . $code_level_id . "X" . $column_group_id . "'>{$code['value']}:{$code['label']}</label></div>";
		}
		print "</div>";
	}
}


/**
 * Display codes given the work_id
 * 
 * @param int $work_unit_id The given work_unit_id
 * @param bool|int $operator_id The operator_id
 * @param string $cdata The code data to search on
 * @see display_all_codes
 */
function display_codes($work_unit_id, $operator_id = false,$cdata = "")
{
	global $db;

	//If the operator_id is set, find out if auto_code is set for this operator and process
	$sql = "SELECT op.auto_code, w.column_multi_group_id as cmgi, w.column_group_id as cgi, ce.row_id as row_id
		FROM operator_process as op, work as w, work_unit as wu, `cell` as ce
		WHERE op.operator_id = $operator_id
		AND op.process_id = w.process_id
		AND w.work_id = wu.work_id
		AND wu.work_unit_id = '$work_unit_id'
		AND ce.cell_id = wu.cell_id";
	
	$ac = $db->GetRow($sql);

	//If this is a multi-group - loop over each unique column group and display entire code set
	if (!empty($ac['cmgi'])) //multi group
	{
		$sql = "SELECT c.column_group_id as cgi, c.column_id, c.description
			FROM `column` as c
			WHERE c.column_multi_group_id = '{$ac['cmgi']}'
			GROUP BY c.column_group_id";

		$rs = $db->GetAll($sql);
		
		print "<script type='text/javascript'>";
		print "document.addEvent('domready', function(){ function jsfcheck(item, state){ alert('test');  } var testMenu = new Mif.Menu().attach('menu-target').load(";
	
		$json = array();
		

		foreach($rs as $r)
		{
			//display root element (checkbox)

			$column_group_id = $r['cgi'];
			$column_description = $r['description'];
		
			$tmp = (object) array('name' => $r['description'], 'id' => 'ci' . $r['column_id'], 'submenu' => 0);
			$tmp->submenu = array();


			//can use action to call a function

			$codes = array();
		
			$sql = "SELECT c.code_level_id, c.column_id
				FROM `column` as c, code_level as cl
				WHERE c.column_group_id = '{$r['cgi']}'
				AND cl.code_level_id = c.code_level_id	
				ORDER BY cl.level ASC";

			$levels = $db->GetAll($sql);


			foreach($levels as $level)
			{
				$code_level_id = $level['code_level_id'];
				$column_id = $level['column_id'];

				$sql = "SELECT c.code_id, c.label, c.value
					FROM `code` as c
					WHERE c.code_level_id = '$code_level_id'";

				$codes = $db->GetAll($sql);

/*				$tmp->submenu[] = (object) array('options' => "{
						onCheck: function(item, state){
							alert('test');
						}
					}");*/

				foreach($codes as $code)
				{
					//print "<div class='row'><input class='rab' type='radio' name='cc" . $code_level_id . "X" . $column_group_id . "' value='{$code['value']}' id='ccc{$code['code_id']}X" . $code_level_id . "X" . $column_group_id . "'";


					//This javascript needs to:
					//	1. search for an existing element of the same name, and if so, append text to it/update value
					//	2. add a new radio input element with the selected value and the text selected - clicking itself should destroy itself
					$jse = "cc" . $code_level_id . "X" . $column_group_id;
					$jset = "tt" . $column_group_id;
					$jsed = "dd" . $code_level_id . "X" . $column_group_id;
					$value = $code['value'];
					$label = $code['label'];

					$js = "	function(item, state) {
							var el = $('$jse');

							if (el != null)
							{
								$('$jsed').destroy();
							}

							txt = $('selectedText').get('value');
							tel = new Element('input');
							tel.set('name', '$jset');
							tel.set('value', txt);
							tel.set('size', txt.length);
							tel.set('id', '$jset');
							tel.set('type', 'text');
							tel.set('readonly','readonly');
							nel = new Element('input');
							nel.set('name', '$jse');
							nel.set('value', '$value');
							nel.set('id', '$jse');
							nel.set('type', 'radio');
							nel.set('checked','true');
							lel = new Element('label');
							lel.set('for', '$jse');
							lel.set('html', '$column_description - $value:$label');
							lel.set('onclick','$(\'$jsed\').destroy();');							

							del = new Element('div');
							del.set('id','$jsed');
							del.adopt(tel);
							del.adopt(lel);
							del.adopt(nel);
							document.id('cleancode').adopt(del);
						}";
					

					$tmp->submenu[] = (object) array('name' => $code['label'], 'checked' => 'false', 'group' => 'ci' . $r['column_id'], 'check' => 'jsfcheck', 'action' => $js);
				}
				break; //temporary: only use first level
			}
			
			$json[] = $tmp;
		}

		print json_encode($json);
		print ")})</script>";
	}
	else if (empty($ac) || $ac['auto_code'] == 0)
	{
		//get a code from this code group

		$sql = "SELECT c.code_id,w.column_group_id
			FROM code as c
			JOIN work_unit AS wu ON (wu.work_unit_id = '$work_unit_id')
			JOIN work as w ON (w.work_id = wu.work_id)
			JOIN column_group as cg ON (cg.column_group_id = w.column_group_id)
			JOIN code_level as cl ON (cl.code_group_id = cg.code_group_id AND c.code_level_id = cl.code_level_id)
			LIMIT 1";

		$r = $db->GetRow($sql);
	
		if (!empty($r))
			display_all_codes($r['code_id'],false,$work_unit_id);
	}
	else
	{
		$sql = "SELECT c.code_id, (c.label LIKE '%$cdata%' OR c.keywords LIKE '%$cdata%') as test,w.column_group_id
			FROM code as c
			JOIN work_unit as wu ON (wu.work_unit_id = '$work_unit_id')
			JOIN work as w ON (w.work_id = wu.work_id)
			JOIN column_group as cg ON (cg.column_group_id = w.column_group_id)
			JOIN code_level as cl ON (cl.code_group_id = cg.code_group_id AND c.code_level_id = cl.code_level_id)
			ORDER BY (c.label LIKE '%$cdata%' OR c.keywords LIKE '%$cdata%') DESC, cl.level ASC
			LIMIT 1";

		$r = $db->GetRow($sql);

		if (!empty($r))
		{
			$tmp = true;
			if ($r['test'] == 0) $tmp = false;
			display_all_codes($r['code_id'],$tmp,$work_unit_id);
		}


	}
}

/**
 * Display multiple choice codes (root is multiple choice, each code (if any) is a child
 * Each column is a root multiple choice code
 *
{'fraction_non_alphanumeric': 0.1644151565074135, 'fraction_numerical': 0.004063701263042284, 'mean_word_length': 3.226270596426085, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4484380', 'n_tokens_mistral': 7537, 'n_tokens_neox': 6435, 'n_words': 1756}