<?php
function xmldb_local_treestudyplan_upgrade($oldversion) {
    global $DB;
    $dbman = $DB->get_manager();

    if ($oldversion < 2020112900) {

        // Define table local_treestudyplan_gradeinc to be created.
        $table = new xmldb_table('local_treestudyplan_gradeinc');

        // Adding fields to table local_treestudyplan_gradeinc.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '18', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('grade_item_id', XMLDB_TYPE_INTEGER, '20', null, null, null, null);
        $table->add_field('include', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');

        // Adding keys to table local_treestudyplan_gradeinc.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
        $table->add_key('grade_item_id-id', XMLDB_KEY_FOREIGN, ['grade_item_id'], 'grade_item', ['id']);

        // Conditionally launch create table for local_treestudyplan_gradeinc.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2020112900, 'local', 'treestudyplan');
    }

    if ($oldversion < 2020120301) {


        // Define field studyitem_id to be added to local_treestudyplan_gradeinc.
        $table = new xmldb_table('local_treestudyplan_gradeinc');
        $field = new xmldb_field('studyitem_id', XMLDB_TYPE_INTEGER, '20', null, null, null, null, 'include');

        // Conditionally launch add field studyitem_id.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $key = new xmldb_key('studyitem_id-id', XMLDB_KEY_FOREIGN, ['studyitem_id'], 'local_treestudyplan_item', ['id']);

        // Launch add key studyitem_id-id.
        $dbman->add_key($table, $key);


        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2020120301, 'local', 'treestudyplan');
    }
    if ($oldversion < 2020120500) {

        // Define table local_treestudyplan_gradecfg to be created.
        $table = new xmldb_table('local_treestudyplan_gradecfg');

        // Adding fields to table local_treestudyplan_gradecfg.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('scale_id', XMLDB_TYPE_INTEGER, '20', null, null, null, null);
        $table->add_field('grade_points', XMLDB_TYPE_INTEGER, '20', null, null, null, null);
        $table->add_field('min_completed', XMLDB_TYPE_INTEGER, '20', null, null, null, null);
        $table->add_field('min_excellent', XMLDB_TYPE_INTEGER, '20', null, null, null, null);

        // Adding keys to table local_treestudyplan_gradecfg.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
        $table->add_key('scale_id-id', XMLDB_KEY_FOREIGN, ['scale_id'], 'scale', ['id']);

        // Conditionally launch create table for local_treestudyplan_gradecfg.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2020120500, 'local', 'treestudyplan');
    }

    if ($oldversion < 2021082300) {

        // Define field aggregation to be added to local_treestudyplan.
        $table = new xmldb_table('local_treestudyplan');
        $field = new xmldb_field('aggregation', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'tristate', 'enddate');

        // Conditionally launch add field aggregation.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        // Define field aggregation_config to be added to local_treestudyplan.
        $field = new xmldb_field('aggregation_config', XMLDB_TYPE_TEXT, null, null, null, null, null, 'aggregation');

        // Conditionally launch add field aggregation_config.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2021082300, 'local', 'treestudyplan');
    }

    if ($oldversion < 2021082600) {

        // Define field min_progress to be dropped from local_treestudyplan_gradecfg.
        $table = new xmldb_table('local_treestudyplan_gradecfg');
        $field = new xmldb_field('min_excellent');

        // Conditionally launch drop field min_progress.
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }

        $field = new xmldb_field('min_progress', XMLDB_TYPE_INTEGER, '20', null, null, null, null, 'min_completed');

        // Conditionally launch add field min_progress.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2021082600, 'local', 'treestudyplan');
    }

    if ($oldversion < 2021082601) {

        $table = new xmldb_table('local_treestudyplan_gradecfg');

        // Changing type of field min_completed on table local_treestudyplan_gradecfg to number.
        $field = new xmldb_field('min_completed', XMLDB_TYPE_NUMBER, '20', null, null, null, null, 'grade_points');
        // Launch change of type for field min_completed.
        $dbman->change_field_type($table, $field);

        // Changing type of field min_progress on table local_treestudyplan_gradecfg to number.
        $field = new xmldb_field('min_progress', XMLDB_TYPE_NUMBER, '20', null, null, null, null, 'min_completed');
        // Launch change of type for field min_progress.
        $dbman->change_field_type($table, $field);

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2021082601, 'local', 'treestudyplan');
    }


    if ($oldversion < 2021082700) {

        $table = new xmldb_table('local_treestudyplan_gradecfg');
        // Changing precision of field min_completed on table local_treestudyplan_gradecfg to (20, 2).
        $field = new xmldb_field('min_completed', XMLDB_TYPE_NUMBER, '20, 1', null, null, null, null, 'grade_points');
        // Launch change of precision for field min_completed.
        $dbman->change_field_precision($table, $field);

        // Changing precision of field min_progress on table local_treestudyplan_gradecfg to (20, 2).
        $field = new xmldb_field('min_progress', XMLDB_TYPE_NUMBER, '20, 1', null, null, null, null, 'min_completed');

        // Launch change of precision for field min_progress.
        $dbman->change_field_precision($table, $field);

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2021082700, 'local', 'treestudyplan');
    }

    if ($oldversion < 2021082701) {

        // Define field required to be added to local_treestudyplan_gradeinc.
        $table = new xmldb_table('local_treestudyplan_gradeinc');
        $field = new xmldb_field('required', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'studyitem_id');

        // Conditionally launch add field required.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2021082701, 'local', 'treestudyplan');
    }
    if ($oldversion < 2023051700) {

        // Define field context_id to be added to local_treestudyplan.
        $table = new xmldb_table('local_treestudyplan');
        $field = new xmldb_field('context_id', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'aggregation_config');

        // Conditionally launch add field context_id.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2023051700, 'local', 'treestudyplan');
    }

    if ($oldversion < 2023062603) {

        // Define field csync_flag to be added to local_treestudyplan.
        $table = new xmldb_table('local_treestudyplan');
        $field = new xmldb_field('csync_flag', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'context_id');

        // Conditionally launch add field csync_flag.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2023062603, 'local', 'treestudyplan');
    }
    if ($oldversion < 2023063002) {

        // Define table local_treestudyplan_teachers to be created.
        $table = new xmldb_table('local_treestudyplan_teachers');

        // Adding fields to table local_treestudyplan_teachers.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('teacher_id', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('studyplan_id', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('update_time', XMLDB_TYPE_INTEGER, '10', null, null, null, null);

        // Adding keys to table local_treestudyplan_teachers.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

        // Conditionally launch create table for local_treestudyplan_teachers.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2023063002, 'local', 'treestudyplan');
    }
    if ($oldversion < 2023071801) {

        // Define table local_treestudyplan_period to be created.
        $table = new xmldb_table('local_treestudyplan_period');

        // Adding fields to table local_treestudyplan_period.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('studyplan_id', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('period', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('starttime', XMLDB_TYPE_INTEGER, '20', null, null, null, null);
        $table->add_field('endtime', XMLDB_TYPE_INTEGER, '20', null, null, null, null);
        $table->add_field('shortname', XMLDB_TYPE_TEXT, null, null, null, null, null);
        $table->add_field('fullname', XMLDB_TYPE_TEXT, null, null, null, null, null);

        // Adding keys to table local_treestudyplan_period.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

        // Conditionally launch create table for local_treestudyplan_period.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }

        // Treestudyplan savepoint reached.
        upgrade_plugin_savepoint(true, 2023071801, 'local', 'treestudyplan');
    }

    return true;
}