<commit_before><?php

include_once( 'errors.inc.php' );
include_once( 'db.pg.class.php' );
include_once( 'session.class.php' );
include_once( 'tools.inc.php' );
include_once( 'json.inc.php' );
include_once( 'utils.php' );

$db =& getDB();
$ses =& getSession();

$pid = isset( $_REQUEST[ 'pid' ] ) ? intval( $_REQUEST[ 'pid' ] ) : 0;
$uid = $ses->isSessionValid() ? $ses->getId() : 0;

# Check preconditions:

# 1. There must be a project id
if ( ! $pid ) {
  echo json_encode( array( 'error' => 'Project closed. Cannot apply operation.' ) );
	return;
}

# 2. There must be a user id
if ( ! $uid ) {
    echo json_encode( array( 'error' => 'You are not logged in currently.  Please log in to be able to add treenodes.' ) );
	return;
}

# 3. The user must be allowed to view annotations:
checkPermissionsOrExit($db, $uid, $pid, $VIEW_ANY_ALLOWED);

// retrieve treenode id, when set retrieve skeleton id
$atnid = isset( $_REQUEST[ 'atnid' ] ) ? intval( $_REQUEST[ 'atnid' ] ) : 0;

// Utility data and associated function below
$columnToFieldArray = array( "tid",
                     "type",
                     "labels",
                     "confidence",
					 "x",
					 "y",
					 "z",
					 "radius",
					 "username",
					 "last_modified" );

function fnColumnToField( $i )
{
	global $columnToFieldArray;
	return $columnToFieldArray[$i];
}



// Start transaction: ensure all queries are consistent
if (! $db->begin() ) {
	echo json_encode( array( 'error' => 'Could not start transaction.' ) );
	return;
}

try {

	$tabinject = '';
	if ($atnid != 0) {
				 $res = $db->getClassInstanceForTreenode( $pid, $atnid, "element_of" );
				 if(!empty($res)) {
						$skelid = $res[0]['class_instance_id'];
						$skelcon = "AND (";
						$skelcon .= '"tci"."class_instance_id" = '.$skelid;
						$skelcon .= ")";
						//$skelcon .= 'AND "treenode"."id" = "tci"."treenode_id"';
						$tabinject = ', "treenode_class_instance" AS "tci"';
						
					} else {
						emitErrorAndExit( $db, 'Can not find skeleton for the selected treenode.' );
					}

	} else {
			// try to retrieve the sent skeleton ids

			// retrieve skeleton ids if set
			if ( isset( $_REQUEST['skeleton_nr'] ))
			{
				$skelnr = intval( $_REQUEST['skeleton_nr'] );
				
				if ( $skelnr )
				{
					$skelcon = "AND (";
					
					for ( $i = 0; $i < $skelnr; $i++ )
					{
						// $skelid[] = $_REQUEST['skeleton_'.$i];
				if( $i != 0 )
					$skelcon .= 'OR ';
				$skelcon .= '"tci"."class_instance_id" = '. intval($_REQUEST['skeleton_'.$i]);
					}
					$skelcon .= ")";
					
				}
				else
				{
				// just not retrieve anything
					$skelcon = "AND false";
				}
			} else {
			// just not retrieve anything
				$skelcon = "AND false";
			}
	}

	/* Paging */
	$sLimit = "";
	$iDisplayStart = isset( $_REQUEST['iDisplayStart'] ) ? intval( $_REQUEST['iDisplayStart'] ) : 0;
	$iDisplayLength = isset( $_REQUEST['iDisplayLength'] ) ? intval( $_REQUEST['iDisplayLength'] ) : -1;

	if ( $iDisplayLength > 0 )
		$sLimit .= ' LIMIT '.$iDisplayLength;
	else
		$sLimit .= ''.$maxrows;

	if ( $iDisplayStart > 0 )
		$sLimit .= ' OFFSET '.$iDisplayStart;

	/* Ordering */
	if ( isset( $_REQUEST['iSortCol_0'] ) )
	{
		$sOrder = "ORDER BY  ";
		$sColumns = intval( $_REQUEST['iSortingCols'] );
		for ( $i=0 ; $i< $sColumns; $i++ )
		{
			$direction = (strtoupper($_REQUEST['sSortDir_'.$i]) === "DESC") ? "DESC" : "ASC";
			$columnIndex = intval( $_REQUEST['iSortCol_'.$i] );
			$sOrder .= fnColumnToField($columnIndex)." ".$direction.", ";
		}
		$sOrder = substr_replace( $sOrder, "", -2 );
	}

	// label logic
	// get id for relation 'labeled_as'      
			$tlabelrel_res = $db->getResult('SELECT "relation"."id" FROM "relation"
			WHERE "relation"."project_id" = '.$pid.' AND
			"relation"."relation_name" = \'labeled_as\'');

	if ( !empty($tlabelrel_res) )
	{
		$tlabelrel = $tlabelrel_res[0]['id'];

		// get treenode_class_instance rows
		$tlabel = $db->getResult(<commit_msg>Remove the variable name of course<commit_after><?php

include_once( 'errors.inc.php' );
include_once( 'db.pg.class.php' );
include_once( 'session.class.php' );
include_once( 'tools.inc.php' );
include_once( 'json.inc.php' );
include_once( 'utils.php' );

$db =& getDB();
$ses =& getSession();

$pid = isset( $_REQUEST[ 'pid' ] ) ? intval( $_REQUEST[ 'pid' ] ) : 0;
$uid = $ses->isSessionValid() ? $ses->getId() : 0;

# Check preconditions:

# 1. There must be a project id
if ( ! $pid ) {
  echo json_encode( array( 'error' => 'Project closed. Cannot apply operation.' ) );
	return;
}

# 2. There must be a user id
if ( ! $uid ) {
    echo json_encode( array( 'error' => 'You are not logged in currently.  Please log in to be able to add treenodes.' ) );
	return;
}

# 3. The user must be allowed to view annotations:
checkPermissionsOrExit($db, $uid, $pid, $VIEW_ANY_ALLOWED);

// retrieve treenode id, when set retrieve skeleton id
$atnid = isset( $_REQUEST[ 'atnid' ] ) ? intval( $_REQUEST[ 'atnid' ] ) : 0;

// Utility data and associated function below
$columnToFieldArray = array( "tid",
                     "type",
                     "labels",
                     "confidence",
					 "x",
					 "y",
					 "z",
					 "radius",
					 "username",
					 "last_modified" );

function fnColumnToField( $i )
{
	global $columnToFieldArray;
	return $columnToFieldArray[$i];
}



// Start transaction: ensure all queries are consistent
if (! $db->begin() ) {
	echo json_encode( array( 'error' => 'Could not start transaction.' ) );
	return;
}

try {

	$tabinject = '';
	if ($atnid != 0) {
				 $res = $db->getClassInstanceForTreenode( $pid, $atnid, "element_of" );
				 if(!empty($res)) {
						$skelid = $res[0]['class_instance_id'];
						$skelcon = "AND (";
						$skelcon .= '"tci"."class_instance_id" = '.$skelid;
						$skelcon .= ")";
						//$skelcon .= 'AND "treenode"."id" = "tci"."treenode_id"';
						$tabinject = ', "treenode_class_instance" AS "tci"';
						
					} else {
						emitErrorAndExit( $db, 'Can not find skeleton for the selected treenode.' );
					}

	} else {
			// try to retrieve the sent skeleton ids

			// retrieve skeleton ids if set
			if ( isset( $_REQUEST['skeleton_nr'] ))
			{
				$skelnr = intval( $_REQUEST['skeleton_nr'] );
				
				if ( $skelnr )
				{
					$skelcon = "AND (";
					
					for ( $i = 0; $i < $skelnr; $i++ )
					{
						// $skelid[] = $_REQUEST['skeleton_'.$i];
				if( $i != 0 )
					$skelcon .= 'OR ';
				$skelcon .= '"tci"."class_instance_id" = '. intval($_REQUEST['skeleton_'.$i]);
					}
					$skelcon .= ")";
					
				}
				else
				{
				// just not retrieve anything
					$skelcon = "AND false";
				}
			} else {
			// just not retrieve anything
				$skelcon = "AND false";
			}
	}

	/* Paging */
	$sLimit = "";
	$iDisplayStart = isset( $_REQUEST['iDisplayStart'] ) ? intval( $_REQUEST['iDisplayStart'] ) : 0;
	$iDisplayLength = isset( $_REQUEST['iDisplayLength'] ) ? intval( $_REQUEST['iDisplayLength'] ) : -1;

	if ( $iDisplayLength > 0 )
		$sLimit .= ' LIMIT '.$iDisplayLength;
	else
		$sLimit .= '';

	if ( $iDisplayStart > 0 )
		$sLimit .= ' OFFSET '.$iDisplayStart;

	/* Ordering */
	if ( isset( $_REQUEST['iSortCol_0'] ) )
	{
		$sOrder = "ORDER BY  ";
		$sColumns = intval( $_REQUEST['iSortingCols'] );
		for ( $i=0 ; $i< $sColumns; $i++ )
		{
			$direction = (strtoupper($_REQUEST['sSortDir_'.$i]) === "DESC") ? "DESC" : "ASC";
			$columnIndex = intval( $_REQUEST['iSortCol_'.$i] );
			$sOrder .= fnColumnToField($columnIndex)." ".$direction.", ";
		}
		$sOrder = substr_replace( $sOrder, "", -2 );
	}

	// label logic
	// get id for relation 'labeled_as'      
			$tlabelrel_res = $db->getResult('SELECT "relation"."id" FROM "relation"
			WHERE "relation"."project_id" = '.$pid.' AND
			"relation"."relation_name" = \'labeled_as\'');

	if ( !empty($tlabelrel_res) )
	{
		$tlabelrel = $tlabelrel_res[0]['id'];

		// get treenode_class_instance rows
		$tlabel = $db->getResult(
{'fraction_non_alphanumeric': 0.18699392712550608, 'fraction_numerical': 0.004554655870445344, 'mean_word_length': 2.926974664679583, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5309935', 'n_tokens_mistral': 3318, 'n_tokens_neox': 2839, 'n_words': 667}