<?php
	global $_SYSTEM;

    if(isset($sql_data) && is_array($sql_data) && count($sql_data))
    {
        if(!function_exists('_describeLogFields'))
        {
            function _describeLogFields($field,$cfield)
            {
        	global $log_result;
                $static_fields = array("STUDENT_FIELD_ID"=>_("Field ID"), "SCHOOL_ID"=>_("School ID"), "STUDENT_ID"=>_("ID Number"));
//                echo "--->".$field;
                if(strpos($field, "LOG_FIELD")!==false)
                {
//                    $field_1 = str_replace("LOG_FIELD", "", $field) + 0;
//echo "==>".$cfield;
                    if($cfield)
                    {
                	if(!$log_result[$cfield])
                	{
	                        $log_result[$cfield] = db_request(db_execute("SELECT * FROM STUDENT_FIELDS WHERE ID='$cfield'"),array('ID'));
	                }
                        return $log_result[$cfield][1][$field.'_TITLE'];
                    }
                    else
                    {
                        return $field;
                    }
                }
                else
                {
                    return strtr($field, $static_fields);
                }
            }
        }

        $table = "STUDENT_LF_DATA";
        $changes_string = null;
        $has_prev_values = false;

        if(!$where_clause)
	        $where_clause = isset($sql_data['where_clause']) ? $sql_data['where_clause'] : null;
	else
		$sql_data['where_clause'] = $where_clause;
        $student_id = array('0'); //even if zero, there will be row in audit display table
        $date = get_db_date('timestamp');
        $user_id = get_user('USER_ID');
        $ignore_columns = array("LAST_UPDATED_DATE", "LAST_UPDATED_USER"); //dont need this - we log same information
        //find out student id array
//        var_dump($where_clause);
        if($where_clause)
        {
            //if where clause is set - then it is UPDATE
            if(strpos($where_clause," ID")!==false)
            {
        	if(strpos($where_clause," AND")!==false)            
            		$students_array_1 = db_request(db_execute("SELECT STUDENT_ID,STUDENT_FIELD_ID FROM $table ".substr($where_clause,0,strpos($where_clause,'AND'))));
            	else
            		$students_array_1 = db_request(db_execute("SELECT STUDENT_ID,STUDENT_FIELD_ID FROM $table ".$where_clause));
            	//	var_dump($students_array_1);
                if(count($students_array_1))
                {
                    $student_id = array();
                    
                    foreach($students_array_1 as $student)
                    {
                        $student_id[] = $student['STUDENT_ID'];
//                        $log_field[$student[1]['STUDENT_ID']][$student[1]['STUDENT_FIELD_ID']] = $student[1]['STUDENT_FIELD_ID'];
                        $lcfield = $student['STUDENT_FIELD_ID'];
//			echo "ggggggggg".$lcfield ;                        
                    }
                }
            }
            unset($sql_data['where_clause']);
        }
        else
        {
            //if no where_caluse set - the INSERT
            if(array_key_exists('STUDENT_ID', $sql_data))
            {
                $student_id = array($sql_data['STUDENT_ID']);
                $lcfield = $sql_data['STUDENT_FIELD_ID'];
            }
        }
//        var_dump($sql_data);
        if($table && $where_clause)
        {
            if(strpos($where_clause," AND")!==false)
	            $temp_array = db_request(db_execute("SELECT * FROM $table ".substr($where_clause,0,strpos($where_clause,'AND'))),array(),array("STUDENT_ID"));
            else
        	    $temp_array = db_request(db_execute("SELECT * FROM $table ".$where_clause),array(),array("STUDENT_ID"));
            if(!$student_id || $student_id[0] == 0 || $student_id[0] == '0')
            {
        	    $student_id = array();
	            $student_id[] = key($temp_array);
	    }
	    $del_string = '';
//	    var_dump($temp_array);
	    if($varDeleted === true)
	    {
        if(is_array($temp_array[key($temp_array)][1])){
		    foreach($temp_array[key($temp_array)][1] as $fname=>$info)
		    {
			    $del_string .= "<B>"._describeLogFields($fname,$lcfield).':</B> '._getFieldsInfo($fname,$info).', <BR/>';
	    	    }
        }
	    }
        }
//        echo "===========>".$lcfield ;
        //loop through all students changed
        foreach($student_id as $id)
        {
            $changes_string = null;
            //check if student data exists
            if($table && $where_clause)
            {
                $has_prev_values = (isset($temp_array[$id]));
            }
            //foreach fields changed
            foreach($sql_data as $field=>$value)
            {
                if(in_array($field,$ignore_columns)) continue;

                $list_this_item = true;
                if($has_prev_values)
                {
                    $list_this_item = ($temp_array[$id][1][$field] != $value);
                }
//echo "|$lcfield|";
                $field_name = _describeLogFields($field,$lcfield);
                if($list_this_item)
		    $changes_string .= " <B>".$field_name.":</B> ".($has_prev_values ? " "._getFieldsInfo($field,$temp_array[$id][1][$field])." > " : null)._getFieldsInfo($field,$value).", <BR/>";
            }
            $changes_string = addslashes(mb_substr($changes_string, 0, -2));
	    if($varDeleted === true)
	    {
		    $changes_string .= ' <B>'._('Record has been deleted').':</B> '.$del_string;
		    $flag = 'D';
	    }
            $changes_string = str_replace("\'","''",$changes_string);

	    $OldSequenceInsert = $_SYSTEM['use_sequence_insert'];
	    $_SYSTEM['use_sequence_insert'] = false;
	    //echo "INSERT INTO STUDENTS_AUDITLOG(DATA_ELEMENT,STUDENT_ID,USER_ID,CHANGE_DATE,CHANGE_DESCRIPTION) VALUES('$table','$id','$user_id','$date','$changes_string')";
            //var_dump($_REQUEST);
	    if(!$id) $id = $_REQUEST['student_id'];
	    if(is_array($id))
		    $id = $id[0];
	    if(!$id) $id = 0;
            if(!$user_id) $user_id = '0';
	    if($changes_string <> '')
	            db_execute("INSERT INTO STUDENTS_AUDITLOG(DATA_ELEMENT,STUDENT_ID,USER_ID,CHANGE_DATE,CHANGE_DESCRIPTION,FLAGS) VALUES('$table','$id','$user_id','$date','$changes_string','$flag')");
//	            echo "INSERT INTO STUDENTS_AUDITLOG(DATA_ELEMENT,STUDENT_ID,USER_ID,CHANGE_DATE,CHANGE_DESCRIPTION,FLAGS) VALUES('$table','$id','$user_id','$date','$changes_string','$flag')";
            $_SYSTEM['use_sequence_insert'] = $OldSequenceInsert;
        }
    }
?>
