<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Keeps track of upgrades to the surveyproitem radiobutton
 *
 * @package   surveyprofield_radiobutton
 * @copyright 2013 onwards kordan <stringapiccola@gmail.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

/**
 * Performs upgrade of the database structure and data
 *
 * @param int $oldversion Version we are upgrading from
 * @return bool true
 */
function xmldb_surveyprofield_radiobutton_upgrade($oldversion) {
    global $DB;

    $dbman = $DB->get_manager();

    if ($oldversion < 2014051701) {
        // Define key surveyproid (foreign) to be dropped form surveyprofield_radiobutton.
        $table = new xmldb_table('surveyprofield_radiobutton');
        $key = new xmldb_key('surveyproid', XMLDB_KEY_FOREIGN, ['surveyproid'], 'surveypro', ['id']);

        // Launch drop key surveyproid.
        $dbman->drop_key($table, $key);

        // Define field surveyproid to be dropped from surveyprofield_radiobutton.
        $table = new xmldb_table('surveyprofield_radiobutton');
        $field = new xmldb_field('surveyproid');

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

        // Radiobutton savepoint reached.
        upgrade_plugin_savepoint(true, 2014051701, 'surveyprofield', 'radiobutton');
    }

    if ($oldversion < 2024022701) {
        // Define field content to be dropped from surveyprofield_radiobutton.
        $table = new xmldb_table('surveyprofield_radiobutton');
        $field1 = new xmldb_field('content');
        $field2 = new xmldb_field('contentformat');

        // Copy the content of the dropping fields to the new corresponding fields in surveypro_item.
        $condition = $dbman->field_exists($table, $field1);
        $condition = $condition && $dbman->field_exists($table, $field2);
        if ($condition) {
            // Strange query syntax because of https://github.com/kordan/moodle-mod_surveypro/issues/977.
            $whereclause = 'WHERE f.itemid = {surveypro_item}.id';
            $sql = 'UPDATE {surveypro_item}
                    SET content = (SELECT f.content FROM {surveyprofield_radiobutton} f ' . $whereclause . '),
                        contentformat = (SELECT f.contentformat FROM {surveyprofield_radiobutton} f ' . $whereclause . ')
                    WHERE EXISTS (SELECT 1 FROM {surveyprofield_radiobutton} f ' . $whereclause . ')';
            $DB->execute($sql);
        }

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

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

        // Radiobutton savepoint reached.
        upgrade_plugin_savepoint(true, 2024022701, 'surveyprofield', 'radiobutton');
    }

    if ($oldversion < 2024032800) {
        $table = new xmldb_table('surveyprofield_radiobutton');

        $fieldnames = ['required', 'indent', 'position', 'customnumber', 'variable', 'extranote'];
        foreach ($fieldnames as $fieldname) {
            // Define field content to be dropped from surveyprofield_radiobutton.
            $field = new xmldb_field($fieldname);

            // Copy the content of the dropping fields to the new corresponding fields in surveypro_item.
            $condition = $dbman->field_exists($table, $field);
            if ($dbman->field_exists($table, $field)) {
                // Copy the content of the dieing column to the new corresponding column in surveypro_item.
                // Strange query syntax because of https://github.com/kordan/moodle-mod_surveypro/issues/977.
                $whereclause = 'WHERE f.itemid = {surveypro_item}.id';
                $sql = 'UPDATE {surveypro_item}
                        SET ' . $fieldname . ' = (
                            SELECT f.' . $fieldname . '
                            FROM {surveyprofield_radiobutton} f ' . $whereclause . '
                        ) WHERE EXISTS (SELECT 1 FROM {surveyprofield_radiobutton} f ' . $whereclause . ')';
                $DB->execute($sql);
            }

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

        // Radiobutton savepoint reached.
        upgrade_plugin_savepoint(true, 2024032800, 'surveyprofield', 'radiobutton');
    }

    return true;
}
