Example #1
0
function scorm_find_common_ancestor($ancestors, $sco)
{
    $pos = scorm_array_search('identifier', $sco->parent, $ancestors);
    if ($sco->parent != '/') {
        if ($pos === false) {
            return scorm_find_common_ancestor($ancestors, scorm_get_parent($sco));
        }
    }
    return $pos;
}
Example #2
0
function scorm_parse_aicc($pkgdir, $scormid)
{
    $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 = scorm_get_aicc_columns($rows[0]);
            $regexp = scorm_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 = scorm_get_aicc_columns($rows[0]);
            $regexp = scorm_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 = scorm_get_aicc_columns($rows[0], 'block');
            $regexp = scorm_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);
            $columns = scorm_get_aicc_columns($rows[0], 'course_element');
            $regexp = scorm_forge_cols_regexp($columns->columns, '(.+)?,');
            for ($i = 1; $i < count($rows); $i++) {
                if (preg_match($regexp, $rows[$i], $matches)) {
                    for ($j = 0; $j < count($matches) - 1; $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->pre)) {
            $rows = file($pkgdir . '/' . $id->pre);
            $columns = scorm_get_aicc_columns($rows[0], 'structure_element');
            $regexp = scorm_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('scorm_scoes', 'scorm', $scormid);
    $launch = 0;
    if (isset($courses)) {
        foreach ($courses as $course) {
            $sco = new object();
            $sco->identifier = $course->id;
            $sco->scorm = $scormid;
            $sco->organization = '';
            $sco->title = $course->title;
            $sco->parent = '/';
            $sco->launch = '';
            $sco->scormtype = '';
            //print_r($sco);
            if (get_record('scorm_scoes', 'scorm', $scormid, 'identifier', $sco->identifier)) {
                $id = update_record('scorm_scoes', addslashes_recursive($sco));
                unset($oldscoes[$id]);
            } else {
                $id = insert_record('scorm_scoes', addslashes_recursive($sco));
            }
            if ($launch == 0) {
                $launch = $id;
            }
            if (isset($course->elements)) {
                foreach ($course->elements as $element) {
                    unset($sco);
                    $sco->identifier = $element->system_id;
                    $sco->scorm = $scormid;
                    $sco->organization = $course->id;
                    $sco->title = $element->title;
                    if (!isset($element->parent) || strtolower($element->parent) == 'root') {
                        $sco->parent = '/';
                    } else {
                        $sco->parent = $element->parent;
                    }
                    if (isset($element->file_name)) {
                        $sco->launch = $element->file_name;
                        $sco->scormtype = 'sco';
                        $sco->previous = 0;
                        $sco->next = 0;
                        $id = null;
                        if ($oldscoid = scorm_array_search('identifier', $sco->identifier, $oldscoes)) {
                            $sco->id = $oldscoid;
                            if (update_record('scorm_scoes', addslashes_recursive($sco))) {
                                $id = $oldscoid;
                            }
                            delete_records('scorm_scoes_data', 'scoid', $oldscoid);
                            unset($oldscoes[$oldscoid]);
                        } else {
                            $id = insert_record('scorm_scoes', addslashes_recursive($sco));
                        }
                        if (!empty($id)) {
                            unset($scodata);
                            $scodata->scoid = $id;
                            if (isset($element->web_launch)) {
                                $scodata->name = 'parameters';
                                $scodata->value = $element->web_launch;
                                $dataid = insert_record('scorm_scoes_data', addslashes_recursive($scodata));
                            }
                            if (isset($element->prerequisites)) {
                                $scodata->name = 'prerequisites';
                                $scodata->value = $element->prerequisites;
                                $dataid = insert_record('scorm_scoes_data', addslashes_recursive($scodata));
                            }
                            if (isset($element->max_time_allowed)) {
                                $scodata->name = 'max_time_allowed';
                                $scodata->value = $element->max_time_allowed;
                                $dataid = insert_record('scorm_scoes_data', addslashes_recursive($scodata));
                            }
                            if (isset($element->time_limit_action)) {
                                $scodata->name = 'time_limit_action';
                                $scodata->value = $element->time_limit_action;
                                $dataid = insert_record('scorm_scoes_data', addslashes_recursive($scodata));
                            }
                            if (isset($element->mastery_score)) {
                                $scodata->name = 'mastery_score';
                                $scodata->value = $element->mastery_score;
                                $dataid = insert_record('scorm_scoes_data', addslashes_recursive($scodata));
                            }
                            if (isset($element->core_vendor)) {
                                $scodata->name = 'datafromlms';
                                $scodata->value = eregi_replace('<cr>', "\r\n", $element->core_vendor);
                                $dataid = insert_record('scorm_scoes_data', addslashes_recursive($scodata));
                            }
                        }
                        if ($launch == 0) {
                            $launch = $id;
                        }
                    }
                }
            }
        }
    }
    if (!empty($oldscoes)) {
        foreach ($oldscoes as $oldsco) {
            delete_records('scorm_scoes', 'id', $oldsco->id);
            delete_records('scorm_scoes_track', 'scoid', $oldsco->id);
        }
    }
    set_field('scorm', 'version', 'AICC', 'id', $scormid);
    return $launch;
}
Example #3
0
function scorm_parse_aicc($scorm)
{
    global $DB;
    if (!isset($scorm->cmid)) {
        $cm = get_coursemodule_from_instance('scorm', $scorm->id);
        $scorm->cmid = $cm->id;
    }
    $context = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
    $fs = get_file_storage();
    $files = $fs->get_area_files($context->id, 'scorm_content', 0, '', false);
    $version = 'AICC';
    $ids = array();
    $courses = array();
    $extaiccfiles = array('crs', 'des', 'au', 'cst', 'ort', 'pre', 'cmp');
    foreach ($files as $file) {
        $filename = $file->get_filename();
        $ext = substr($filename, strrpos($filename, '.'));
        $extension = strtolower(substr($ext, 1));
        if (in_array($extension, $extaiccfiles)) {
            $id = strtolower(basename($filename, $ext));
            $ids[$id]->{$extension} = $file;
        }
    }
    foreach ($ids as $courseid => $id) {
        if (isset($id->crs)) {
            $contents = $id->crs->get_content();
            $rows = explode("\r\n", $contents);
            if (is_array($rows)) {
                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)) {
            $contents = $id->des->get_content();
            $rows = explode("\r\n", $contents);
            $columns = scorm_get_aicc_columns($rows[0]);
            $regexp = scorm_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)) {
            $contents = $id->au->get_content();
            $rows = explode("\r\n", $contents);
            $columns = scorm_get_aicc_columns($rows[0]);
            $regexp = scorm_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)) {
            $contents = $id->cst->get_content();
            $rows = explode("\r\n", $contents);
            $columns = scorm_get_aicc_columns($rows[0], 'block');
            $regexp = scorm_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)) {
            $contents = $id->ort->get_content();
            $rows = explode("\r\n", $contents);
            $columns = scorm_get_aicc_columns($rows[0], 'course_element');
            $regexp = scorm_forge_cols_regexp($columns->columns, '(.+)?,');
            for ($i = 1; $i < count($rows); $i++) {
                if (preg_match($regexp, $rows[$i], $matches)) {
                    for ($j = 0; $j < count($matches) - 1; $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->pre)) {
            $contents = $id->pre->get_content();
            $rows = explode("\r\n", $contents);
            $columns = scorm_get_aicc_columns($rows[0], 'structure_element');
            $regexp = scorm_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)) {
            $contents = $id->cmp->get_content();
            $rows = explode("\r\n", $contents);
        }
    }
    //print_r($courses);
    $oldscoes = $DB->get_records('scorm_scoes', array('scorm' => $scorm->id));
    $launch = 0;
    if (isset($courses)) {
        foreach ($courses as $course) {
            $sco = new object();
            $sco->identifier = $course->id;
            $sco->scorm = $scorm->id;
            $sco->organization = '';
            $sco->title = $course->title;
            $sco->parent = '/';
            $sco->launch = '';
            $sco->scormtype = '';
            //print_r($sco);
            if ($ss = $DB->get_record('scorm_scoes', array('scorm' => $scorm->id, 'identifier' => $sco->identifier))) {
                $id = $ss->id;
                $DB->update_record('scorm_scoes', $sco);
                unset($oldscoes[$id]);
            } else {
                $id = $DB->insert_record('scorm_scoes', $sco);
            }
            if ($launch == 0) {
                $launch = $id;
            }
            if (isset($course->elements)) {
                foreach ($course->elements as $element) {
                    unset($sco);
                    $sco->identifier = $element->system_id;
                    $sco->scorm = $scorm->id;
                    $sco->organization = $course->id;
                    $sco->title = $element->title;
                    if (!isset($element->parent) || strtolower($element->parent) == 'root') {
                        $sco->parent = '/';
                    } else {
                        $sco->parent = $element->parent;
                    }
                    if (isset($element->file_name)) {
                        $sco->launch = $element->file_name;
                        $sco->scormtype = 'sco';
                        $sco->previous = 0;
                        $sco->next = 0;
                        $id = null;
                        if ($oldscoid = scorm_array_search('identifier', $sco->identifier, $oldscoes)) {
                            $sco->id = $oldscoid;
                            $id = $DB->update_record('scorm_scoes', $sco);
                            $DB->delete_records('scorm_scoes_data', array('scoid' => $oldscoid));
                            unset($oldscoes[$oldscoid]);
                        } else {
                            $id = $DB->insert_record('scorm_scoes', $sco);
                        }
                        if (!empty($id)) {
                            unset($scodata);
                            $scodata->scoid = $id;
                            if (isset($element->web_launch)) {
                                $scodata->name = 'parameters';
                                $scodata->value = $element->web_launch;
                                $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                            }
                            if (isset($element->prerequisites)) {
                                $scodata->name = 'prerequisites';
                                $scodata->value = $element->prerequisites;
                                $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                            }
                            if (isset($element->max_time_allowed)) {
                                $scodata->name = 'max_time_allowed';
                                $scodata->value = $element->max_time_allowed;
                                $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                            }
                            if (isset($element->time_limit_action)) {
                                $scodata->name = 'time_limit_action';
                                $scodata->value = $element->time_limit_action;
                                $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                            }
                            if (isset($element->mastery_score)) {
                                $scodata->name = 'mastery_score';
                                $scodata->value = $element->mastery_score;
                                $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                            }
                            if (isset($element->core_vendor)) {
                                $scodata->name = 'datafromlms';
                                $scodata->value = eregi_replace('<cr>', "\r\n", $element->core_vendor);
                                $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                            }
                        }
                        if ($launch == 0) {
                            $launch = $id;
                        }
                    }
                }
            }
        }
    }
    if (!empty($oldscoes)) {
        foreach ($oldscoes as $oldsco) {
            $DB->delete_records('scorm_scoes', array('id' => $oldsco->id));
            $DB->delete_records('scorm_scoes_track', array('scoid' => $oldsco->id));
        }
    }
    $scorm->version = 'AICC';
    $scorm->launch = $launch;
    return true;
}
Example #4
0
/**
 * Sets up AICC packages
 * Called whenever package changes
 * @param object $scorm instance - fields are updated and changes saved into database
 * @return bool
 */
function scorm_parse_aicc(&$scorm)
{
    global $DB;
    if ($scorm->scormtype == SCORM_TYPE_AICCURL) {
        return scorm_aicc_generate_simple_sco($scorm);
    }
    if (!isset($scorm->cmid)) {
        $cm = get_coursemodule_from_instance('scorm', $scorm->id);
        $scorm->cmid = $cm->id;
    }
    $context = context_module::instance($scorm->cmid);
    $fs = get_file_storage();
    $files = $fs->get_area_files($context->id, 'mod_scorm', 'content', 0, 'sortorder, itemid, filepath, filename', false);
    $version = 'AICC';
    $ids = array();
    $courses = array();
    $extaiccfiles = array('crs', 'des', 'au', 'cst', 'ort', 'pre', 'cmp');
    foreach ($files as $file) {
        $filename = $file->get_filename();
        $ext = substr($filename, strrpos($filename, '.'));
        $extension = strtolower(substr($ext, 1));
        if (in_array($extension, $extaiccfiles)) {
            $id = strtolower(basename($filename, $ext));
            if (!isset($ids[$id])) {
                $ids[$id] = new stdClass();
            }
            $ids[$id]->{$extension} = $file;
        }
    }
    foreach ($ids as $courseid => $id) {
        if (!isset($courses[$courseid])) {
            $courses[$courseid] = new stdClass();
        }
        if (isset($id->crs)) {
            $contents = $id->crs->get_content();
            $rows = explode("\r\n", $contents);
            if (is_array($rows)) {
                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)) {
            $contents = $id->des->get_content();
            $rows = explode("\r\n", $contents);
            $columns = scorm_get_aicc_columns($rows[0]);
            $regexp = scorm_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];
                        if (!isset($courses[$courseid]->elements[substr(trim($matches[$columns->mastercol + 1]), 1, -1)])) {
                            $courses[$courseid]->elements[substr(trim($matches[$columns->mastercol + 1]), 1, -1)] = new stdClass();
                        }
                        $courses[$courseid]->elements[substr(trim($matches[$columns->mastercol + 1]), 1, -1)]->{$column} = substr(trim($matches[$j + 1]), 1, -1);
                    }
                }
            }
        }
        if (isset($id->au)) {
            $contents = $id->au->get_content();
            $rows = explode("\r\n", $contents);
            $columns = scorm_get_aicc_columns($rows[0]);
            $regexp = scorm_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)) {
            $contents = $id->cst->get_content();
            $rows = explode("\r\n", $contents);
            $columns = scorm_get_aicc_columns($rows[0], 'block');
            $regexp = scorm_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) {
                            $element = substr(trim($matches[$j + 1]), 1, -1);
                            if (!empty($element)) {
                                $courses[$courseid]->elements[$element]->parent = substr(trim($matches[$columns->mastercol + 1]), 1, -1);
                            }
                        }
                    }
                }
            }
        }
        if (isset($id->ort)) {
            $contents = $id->ort->get_content();
            $rows = explode("\r\n", $contents);
            $columns = scorm_get_aicc_columns($rows[0], 'course_element');
            $regexp = scorm_forge_cols_regexp($columns->columns, '(.+)?,');
            for ($i = 1; $i < count($rows); $i++) {
                if (preg_match($regexp, $rows[$i], $matches)) {
                    for ($j = 0; $j < count($matches) - 1; $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->pre)) {
            $contents = $id->pre->get_content();
            $rows = explode("\r\n", $contents);
            $columns = scorm_get_aicc_columns($rows[0], 'structure_element');
            $regexp = scorm_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)) {
            $contents = $id->cmp->get_content();
            $rows = explode("\r\n", $contents);
        }
    }
    $oldscoes = $DB->get_records('scorm_scoes', array('scorm' => $scorm->id));
    $sortorder = 0;
    $launch = 0;
    if (isset($courses)) {
        foreach ($courses as $course) {
            $sortorder++;
            $sco = new stdClass();
            $sco->identifier = $course->id;
            $sco->scorm = $scorm->id;
            $sco->organization = '';
            $sco->title = $course->title;
            $sco->parent = '/';
            $sco->launch = '';
            $sco->scormtype = '';
            $sco->sortorder = $sortorder;
            if ($ss = $DB->get_record('scorm_scoes', array('scorm' => $scorm->id, 'identifier' => $sco->identifier))) {
                $id = $ss->id;
                $sco->id = $id;
                $DB->update_record('scorm_scoes', $sco);
                unset($oldscoes[$id]);
            } else {
                $id = $DB->insert_record('scorm_scoes', $sco);
            }
            if ($launch == 0) {
                $launch = $id;
            }
            if (isset($course->elements)) {
                foreach ($course->elements as $element) {
                    unset($sco);
                    $sco = new stdClass();
                    $sco->identifier = $element->system_id;
                    $sco->scorm = $scorm->id;
                    $sco->organization = $course->id;
                    $sco->title = $element->title;
                    if (!isset($element->parent)) {
                        $sco->parent = '/';
                    } else {
                        if (strtolower($element->parent) == 'root') {
                            $sco->parent = $course->id;
                        } else {
                            $sco->parent = $element->parent;
                        }
                    }
                    $sco->launch = '';
                    $sco->scormtype = '';
                    $sco->previous = 0;
                    $sco->next = 0;
                    $id = null;
                    // Is it an Assignable Unit (AU)?
                    if (isset($element->file_name)) {
                        $sco->launch = $element->file_name;
                        $sco->scormtype = 'sco';
                    }
                    if ($oldscoid = scorm_array_search('identifier', $sco->identifier, $oldscoes)) {
                        $sco->id = $oldscoid;
                        $DB->update_record('scorm_scoes', $sco);
                        $id = $oldscoid;
                        $DB->delete_records('scorm_scoes_data', array('scoid' => $oldscoid));
                        unset($oldscoes[$oldscoid]);
                    } else {
                        $id = $DB->insert_record('scorm_scoes', $sco);
                    }
                    if (!empty($id)) {
                        $scodata = new stdClass();
                        $scodata->scoid = $id;
                        if (isset($element->web_launch)) {
                            $scodata->name = 'parameters';
                            $scodata->value = $element->web_launch;
                            $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                        }
                        if (isset($element->prerequisites)) {
                            $scodata->name = 'prerequisites';
                            $scodata->value = $element->prerequisites;
                            $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                        }
                        if (isset($element->max_time_allowed)) {
                            $scodata->name = 'max_time_allowed';
                            $scodata->value = $element->max_time_allowed;
                            $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                        }
                        if (isset($element->time_limit_action)) {
                            $scodata->name = 'time_limit_action';
                            $scodata->value = $element->time_limit_action;
                            $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                        }
                        if (isset($element->mastery_score)) {
                            $scodata->name = 'mastery_score';
                            $scodata->value = $element->mastery_score;
                            $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                        }
                        if (isset($element->core_vendor)) {
                            $scodata->name = 'datafromlms';
                            $scodata->value = preg_replace('/<cr>/i', "\r\n", $element->core_vendor);
                            $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
                        }
                    }
                    if ($launch == 0) {
                        $launch = $id;
                    }
                }
            }
        }
    }
    if (!empty($oldscoes)) {
        foreach ($oldscoes as $oldsco) {
            $DB->delete_records('scorm_scoes', array('id' => $oldsco->id));
            $DB->delete_records('scorm_scoes_track', array('scoid' => $oldsco->id));
        }
    }
    // Find first launchable object.
    $sqlselect = 'scorm = ? AND ' . $DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
    // We use get_records here as we need to pass a limit in the query that works cross db.
    $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id), 'sortorder', 'id', 0, 1);
    if (!empty($scoes)) {
        $sco = reset($scoes);
        // We only care about the first record - the above query only returns one.
        $scorm->launch = $sco->id;
    } else {
        $scorm->launch = $launch;
    }
    $scorm->version = 'AICC';
    return true;
}
/**
 * 
 * Parse the TCAPI resource.
 * 
 * @param object $scorm
 * @param object $manifest
 */
function scorm_parse_tincan($scorm, $manifest) {
    global $CFG, $DB;

    // load manifest into string
    if ($manifest instanceof stored_file) {
        $xmltext = $manifest->get_content();
    } else {
        require_once("$CFG->libdir/filelib.php");
        $xmltext = download_file_content($manifest);
    }

    $launch = 0;
    $pattern = '/&(?!\w{2,6};)/';
    $replacement = '&amp;';
    $xmltext = preg_replace($pattern, $replacement, $xmltext);

    $objXML = new xml2Array();
    $manifests = $objXML->parse($xmltext);
    $scoes = new stdClass();
    $scoes->version = 'TCAPI';
    $scoes = scorm_get_tincan_manifest($manifests, $scoes);
    if (count($scoes->elements) > 0) {
        $olditems = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id));
        foreach ($scoes->elements as $manifest => $organizations) {
            foreach ($organizations as $organization => $items) {
                foreach ($items as $identifier => $item) {
                    $newitem = new stdClass();
                    $newitem->scorm = $scorm->id;
                    $newitem->manifest = $manifest;
                    $newitem->organization = $organization;
                    $standarddatas = array('parent', 'identifier', 'launch', 'scormtype', 'title');
                    foreach ($standarddatas as $standarddata) {
                        if (isset($item->$standarddata)) {
                            $newitem->$standarddata = $item->$standarddata;
                        }
                    }

                    // Insert the new SCO, and retain the link between the old and new for later adjustment
                    $id = $DB->insert_record('scorm_scoes', $newitem);
                    if (!empty($olditems) && ($olditemid = scorm_array_search('identifier', $newitem->identifier, $olditems))) {
                        $olditems[$olditemid]->newid = $id;
                    }

                    if ($optionaldatas = scorm_tincan_optionals_data($item, $standarddatas)) {
                        $data = new stdClass();
                        $data->scoid = $id;
                        foreach ($optionaldatas as $optionaldata) {
                            if (isset($item->$optionaldata)) {
                                $data->name =  $optionaldata;
                                $data->value = $item->$optionaldata;
                                $dataid = $DB->insert_record('scorm_scoes_data', $data);
                            }
                        }
                    }

                    if (($launch == 0) && ((empty($scoes->defaultorg)) || ($scoes->defaultorg == $identifier))) {
                        $launch = $id;
                    }
                }
            }
        }
        if (!empty($olditems)) {
            foreach ($olditems as $olditem) {
                $DB->delete_records('scorm_scoes', array('id'=>$olditem->id));
                $DB->delete_records('scorm_scoes_data', array('scoid'=>$olditem->id));
                if (isset($olditem->newid)) {
                    $DB->set_field('scorm_scoes_track', 'scoid', $olditem->newid, array('scoid' => $olditem->id));
                }
                $DB->delete_records('scorm_scoes_track', array('scoid'=>$olditem->id));
            }
        }
        $DB->set_field('scorm', 'version', $scoes->version, array('id'=>$scorm->id));
        $scorm->version = $scoes->version;
    }

    $scorm->launch = $launch;

    return true;
}