<?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/>.

/**
 * Upgrade helper functions
 *
 * @package   mod_surveypro
 * @copyright 2013 onwards kordan <stringapiccola@gmail.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

/**
 * Delete from surveypro_answer
 * all the records referring to answers
 * to elements not presented to students because of branching.
 */
function surveypro_delete_supposed_blank_answers() {
    global $DB;

    // Step 1 of 2.
    // Start generating the list of each child element in this site (alias: conditioned by a parent child relation).
    // For each one of them verify if the parent item allows it.
    $where = 'type = :type AND parentid <> :parentid';
    $whereparams = ['type' => SURVEYPRO_TYPEFIELD, 'parentid' => 0];
    $orderby = 'surveyproid, sortindex';
    $fields = 'id as childid, parentid, parentvalue';
    $brancheditems = $DB->get_recordset_select('surveypro_item', $where, $whereparams, $orderby, $fields);

    // Delete all the answers to items that were not allowed by arent item.
    foreach ($brancheditems as $brancheditem) {
        // Select all the answers to the item $brancheditem['parentid'] different from $brancheditem['parentvalue'].
        $sql = 'SELECT child.id as answerid
                FROM {surveypro_answer} child
                    INNER JOIN {surveypro_answer} parent ON parent.submissionid = child.submissionid
                WHERE child.itemid = :childid
                    AND parent.itemid = :parentid
                    AND parent.content <> :parentcontent';
        $whereparams = [];
        $whereparams['childid'] = $brancheditem->childid;
        $whereparams['parentid'] = $brancheditem->parentid;
        $whereparams['parentcontent'] = $brancheditem->parentvalue;

        if ($deleturum = $DB->get_records_sql($sql, $whereparams)) {
            foreach ($deleturum as $todelete) {
                $DB->delete_records('surveypro_answer', ['id' => $todelete->answerid]);
            }
        }
    }
    $brancheditems->close();

    // Step 2 of 2.
    // Make the list of each parent item.
    // For each one of them verify they actually have a saved answer.
    // Parent items without a saved answer must be considered as parent NOT allowing children.
    // Delete all the answers to children items that have parent without saved answers.
    // Alias: if the parent was not allowed in the userform, its children will not be allowed even more.
    $sql = 'SELECT parentid, MAX(surveyproid) as surveyproid
            FROM {surveypro_item}
            WHERE type = :type
                AND parentid <> :parentid
            GROUP BY parentid
            ORDER BY MAX(surveyproid)';
    $whereparams = ['type' => SURVEYPRO_TYPEFIELD, 'parentid' => 0];
    $parentitems = $DB->get_records_sql($sql, $whereparams);

    $oldsurveyproid = 0;
    foreach ($parentitems as $parentitem) {
        // Get all submissions for this surveyproid.
        if ($parentitem->surveyproid != $oldsurveyproid) {
            $whereparams = [];
            $whereparams['surveyproid'] = $parentitem->surveyproid;
            $submissions = $DB->get_recordset('surveypro_submission', $whereparams, 'id', 'id');

            $oldsurveyproid = $parentitem->surveyproid;
        }
        foreach ($submissions as $submission) {
            // Get all the answers given to the parent item.
            $whereparams = [];
            $whereparams['submissionid'] = $submission->id;
            $whereparams['itemid'] = $parentitem->parentid;
            if (!$DB->count_records('surveypro_answer', $whereparams)) {
                // OK, parent item was not answered so its children were not allowed!
                // Get the list of children of $parentitem->parentid.
                $childrenitems = $DB->get_records('surveypro_item', ['parentid' => $parentitem->parentid], 'id', 'id');
                // Delete its childldren.
                foreach ($childrenitems as $childitem) {
                    $whereparams['itemid'] = $childitem->id;
                    $DB->delete_records('surveypro_answer', $whereparams);
                }
            }
        }
        $submissions->close();
    }
}

/**
 * Images of thankshtml area were saved to db with wrong itemid before version 2018020200.
 * The purpose of this routine is to correct them.
 * Even backups of courses created with surveypro older than the same version were buggy
 * and they restored (and still restore) wrong itemid to databases.
 * Because of this, this routine is also called by function after_restore (in restore_surveypro_stepslib.php)
 * to correct itemid just after they are wrongly restored.
 *
 * @param object $surveypro
 * @return void
 */
function surveypro_old_restore_fix($surveypro) {
    global $DB;

    $course = $DB->get_record('course', ['id' => $surveypro->course], '*', MUST_EXIST);
    $cm = get_coursemodule_from_instance('surveypro', $surveypro->id, $course->id, false, MUST_EXIST);
    $context = \context_module::instance($cm->id);

    $areas = [SURVEYPRO_THANKSPAGEFILEAREA, SURVEYPRO_STYLEFILEAREA, SURVEYPRO_TEMPLATEFILEAREA];

    $fs = get_file_storage();

    foreach ($areas as $area) {
        $files = $fs->get_area_files($context->id, 'mod_surveypro', $area);
        foreach ($files as $file) {
            // Surveypro with version lower than 2018020900 saved module level files with a wrong itemid <> 0.
            // Backup, performed over those old surveypros, were created to restore module level files with that wrong itemid.
            // On the other hand, backup performed over surveypro with version greater or equal to 2018020900
            // find module level files saved with correct itemid = 0 and they restore them with correct itemid.
            // The problem is that...
            // old backups always restore (in both old or new versions of surveypro) module level files with wrong itemid
            // while new backups work fine.
            // My purpose, here, is to delete wrong files generated by the restore process of old backups.
            // The rationale is:
            // (1) get each file in the proper filearea
            // (2) Create a copy of it with itemid = 0
            // (3) delete the original one.
            // Be carefull: restoring a new backup, the file you get in (1) has already the correct itemid = 0
            // AND ITS DUPLICATION WITH itemid = 0 (2) RISES AN ERROR because the file already exists.

            $itemid = $file->get_itemid();
            if (empty($itemid)) {
                continue;
            }

            $filerecord = [];
            $filerecord['contextid'] = $file->get_contextid();
            $filerecord['component'] = 'mod_surveypro';
            $filerecord['filearea'] = $area;
            $filerecord['itemid'] = 0;

            $fs->create_file_from_storedfile($filerecord, $file);

            $file->delete();
        }
    }
}
