function udutu_find_common_ancestor($ancestors, $sco)
{
    $pos = udutu_array_search('identifier', $sco->parent, $ancestors);
    if ($sco->parent != '/') {
        if ($pos === false) {
            return udutu_find_common_ancestor($ancestors, udutu_get_parent($sco));
        }
    }
    return $pos;
}
function udutu_parse_aicc($pkgdir, $udutuid)
{
    echo 'here';
    $version = 'AICC';
    $ids = array();
    $courses = array();
    $extaiccfiles = array('crs', 'des', 'au', 'cst', 'ort', 'pre', 'cmp');
    if ($handle = opendir($pkgdir)) {
        while (($file = readdir($handle)) !== false) {
            if ($file[0] != '.') {
                $ext = substr($file, strrpos($file, '.'));
                $extension = strtolower(substr($ext, 1));
                if (in_array($extension, $extaiccfiles)) {
                    $id = strtolower(basename($file, $ext));
                    $ids[$id]->{$extension} = $file;
                }
            }
        }
        closedir($handle);
    }
    foreach ($ids as $courseid => $id) {
        if (isset($id->crs)) {
            if (is_file($pkgdir . '/' . $id->crs)) {
                $rows = file($pkgdir . '/' . $id->crs);
                foreach ($rows as $row) {
                    if (preg_match("/^(.+)=(.+)\$/", $row, $matches)) {
                        switch (strtolower(trim($matches[1]))) {
                            case 'course_id':
                                $courses[$courseid]->id = trim($matches[2]);
                                break;
                            case 'course_title':
                                $courses[$courseid]->title = trim($matches[2]);
                                break;
                            case 'version':
                                $courses[$courseid]->version = 'AICC_' . trim($matches[2]);
                                break;
                        }
                    }
                }
            }
        }
        if (isset($id->des)) {
            $rows = file($pkgdir . '/' . $id->des);
            $columns = udutu_get_aicc_columns($rows[0]);
            $regexp = udutu_forge_cols_regexp($columns->columns);
            for ($i = 1; $i < count($rows); $i++) {
                if (preg_match($regexp, $rows[$i], $matches)) {
                    for ($j = 0; $j < count($columns->columns); $j++) {
                        $column = $columns->columns[$j];
                        $courses[$courseid]->elements[substr(trim($matches[$columns->mastercol + 1]), 1, -1)]->{$column} = substr(trim($matches[$j + 1]), 1, -1);
                    }
                }
            }
        }
        if (isset($id->au)) {
            $rows = file($pkgdir . '/' . $id->au);
            $columns = udutu_get_aicc_columns($rows[0]);
            $regexp = udutu_forge_cols_regexp($columns->columns);
            for ($i = 1; $i < count($rows); $i++) {
                if (preg_match($regexp, $rows[$i], $matches)) {
                    for ($j = 0; $j < count($columns->columns); $j++) {
                        $column = $columns->columns[$j];
                        $courses[$courseid]->elements[substr(trim($matches[$columns->mastercol + 1]), 1, -1)]->{$column} = substr(trim($matches[$j + 1]), 1, -1);
                    }
                }
            }
        }
        if (isset($id->cst)) {
            $rows = file($pkgdir . '/' . $id->cst);
            $columns = udutu_get_aicc_columns($rows[0], 'block');
            $regexp = udutu_forge_cols_regexp($columns->columns, '(.+)?,');
            for ($i = 1; $i < count($rows); $i++) {
                if (preg_match($regexp, $rows[$i], $matches)) {
                    for ($j = 0; $j < count($columns->columns); $j++) {
                        if ($j != $columns->mastercol) {
                            $courses[$courseid]->elements[substr(trim($matches[$j + 1]), 1, -1)]->parent = substr(trim($matches[$columns->mastercol + 1]), 1, -1);
                        }
                    }
                }
            }
        }
        if (isset($id->ort)) {
            $rows = file($pkgdir . '/' . $id->ort);
        }
        if (isset($id->pre)) {
            $rows = file($pkgdir . '/' . $id->pre);
            $columns = udutu_get_aicc_columns($rows[0], 'structure_element');
            $regexp = udutu_forge_cols_regexp($columns->columns, '(.+),');
            for ($i = 1; $i < count($rows); $i++) {
                if (preg_match($regexp, $rows[$i], $matches)) {
                    $courses[$courseid]->elements[$columns->mastercol + 1]->prerequisites = substr(trim($matches[1 - $columns->mastercol + 1]), 1, -1);
                }
            }
        }
        if (isset($id->cmp)) {
            $rows = file($pkgdir . '/' . $id->cmp);
        }
    }
    //print_r($courses);
    $oldscoes = get_records('udutu_scoes', 'udutu', $udutuid);
    $launch = 0;
    if (isset($courses)) {
        foreach ($courses as $course) {
            unset($sco);
            $sco->identifier = $course->id;
            $sco->udutu = $udutuid;
            $sco->organization = '';
            $sco->title = $course->title;
            $sco->parent = '/';
            $sco->launch = '';
            $sco->udututype = '';
            //print_r($sco);
            if (get_record('udutu_scoes', 'udutu', $udutuid, 'identifier', $sco->identifier)) {
                $id = update_record('udutu_scoes', $sco);
                unset($oldscoes[$id]);
            } else {
                $id = insert_record('udutu_scoes', $sco);
            }
            if ($launch == 0) {
                $launch = $id;
            }
            if (isset($course->elements)) {
                foreach ($course->elements as $element) {
                    unset($sco);
                    $sco->identifier = $element->system_id;
                    $sco->udutu = $udutuid;
                    $sco->organization = $course->id;
                    $sco->title = $element->title;
                    if (strtolower($element->parent) == 'root') {
                        $sco->parent = '/';
                    } else {
                        $sco->parent = $element->parent;
                    }
                    if (isset($element->file_name)) {
                        $sco->launch = $element->file_name;
                        $sco->udututype = 'sco';
                    } else {
                        $element->file_name = '';
                        $sco->udututype = '';
                    }
                    if (!isset($element->prerequisites)) {
                        $element->prerequisites = '';
                    }
                    $sco->prerequisites = $element->prerequisites;
                    if (!isset($element->max_time_allowed)) {
                        $element->max_time_allowed = '';
                    }
                    $sco->maxtimeallowed = $element->max_time_allowed;
                    if (!isset($element->time_limit_action)) {
                        $element->time_limit_action = '';
                    }
                    $sco->timelimitaction = $element->time_limit_action;
                    if (!isset($element->mastery_score)) {
                        $element->mastery_score = '';
                    }
                    $sco->masteryscore = $element->mastery_score;
                    $sco->previous = 0;
                    $sco->next = 0;
                    if ($oldscoid = udutu_array_search('identifier', $sco->identifier, $oldscoes)) {
                        $sco->id = $oldscoid;
                        $id = update_record('udutu_scoes', $sco);
                        unset($oldscoes[$oldscoid]);
                    } else {
                        $id = insert_record('udutu_scoes', $sco);
                    }
                    if ($launch == 0) {
                        $launch = $id;
                    }
                }
            }
        }
    }
    if (!empty($oldscoes)) {
        foreach ($oldscoes as $oldsco) {
            delete_records('udutu_scoes', 'id', $oldsco->id);
            delete_records('udutu_scoes_track', 'scoid', $oldsco->id);
        }
    }
    set_field('udutu', 'version', 'AICC', 'id', $udutuid);
    return $launch;
}