<?php
    if(isset($sql_data) && is_array($sql_data) && count($sql_data))
    {
        if(!function_exists('_get_field_name'))
        {
            function _get_field_name($field)
            {
                $static_fields = array("FIRST_NAME"=>_("First Name"),"LAST_NAME"=>_("Last Name"), "MIDDLE_NAME"=>_("Middle Name"), "NAME_SUFFIX"=>_("Name Suffix"), "STUDENT_ID"=>_("ID Number"));
                if(strpos($field, "CFIELD_")!==false)
                {
                    $field_1 = str_replace("CFIELD_", "", $field) + 0;
                    if($field_1 + 0)
                    {
                        $result = db_request(db_execute("SELECT title FROM STUDENT_FIELDS WHERE id='$field_1'"));

                        return $result[1]['TITLE'];
                    }
                    else
                    {
                        return _describeFields($field);
                    }
                }
                else
                {
                    return strtr(_describeFields($field), $static_fields);
                }
            }
        }
        /*
	if(!function_exists("_describeFields"))
        {
              function _describeFields ($field_name)
              {
            	    switch($field_name)
                    {
                	    case 'SCHOOL_YEAR':
                        	    return _('School Year');
                            case 'SCHOOL_DATE':
    	                            return _('School Date');
                            case 'MINUTES_PRESENT':
    	                            return _('Minutes Present');
                            case 'STATE_VALUE':
    	                            return _('Attendance State');
                            case 'SCHOOL_TERM_ID':
    	                            return _('School Term');
                            case 'COMMENT':
    	                            return _('Comments');
                            case 'DAILY_CODE':
    	                            return _('Daily Code');

		            default:
                	            return $field_name;
		    }
            }
        }
        */                                                                                            
                                                                                                                                                                                                          
        $table = "STUDENT_ATTENDANCE_DAYS";
        $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();
        $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
        if($where_clause)
        {
            //if where clause is set - then it is UPDATE
            if(strpos($where_clause,"STUDENT_ID")!==false)
            {
                $students_array_1 = db_request(db_execute("SELECT STUDENT_ID FROM $table ".$where_clause));
                if(count($students_array_1))
                {
                    $student_id = array();
                    
                    foreach($students_array_1 as $student)
                        $student_id[] = $student['STUDENT_ID'];
                }
            }
            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']);
        }

        if($table && $where_clause)
        {
            $temp_array = db_request(db_execute("SELECT * FROM $table ".$where_clause),array(),array("STUDENT_ID"));
        }

        //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);
                }

                $field_name = _get_field_name($field);

                if($list_this_item)
                    $changes_string .= "$field_name: ".($has_prev_values ? "".$temp_array[$id][1][$field]." > " : null)."$value, ";
            }

            $changes_string = addslashes(substr($changes_string, 0, -2));
            $changes_string = str_replace("\'","''",$changes_string);

            db_execute("INSERT INTO STUDENTS_AUDITLOG(DATA_ELEMENT,STUDENT_ID,USER_ID,CHANGE_DATE,CHANGE_DESCRIPTION) VALUES('$table','$id','$user_id','$date','$changes_string')");
        }
    }
?>
