<?php


$servername = "127.0.0.1";
$username = "root";
$password = "";
$database = "trabajoolas5";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully\n";

$allrubrics = mysqli_query($conn, 'select id, contents from `templates`');

while ($row = mysqli_fetch_array($allrubrics)) {

    foreach (json_decode($row['contents']) as $criterion) {

        $criterion_id = $criterion->id;
        $querySelect = "select * from template_criterion where template_id ={$row['id']} and criterion_id = {$criterion_id}";
        $makeQuery = mysqli_query($conn, $querySelect);
        if (mysqli_num_rows($makeQuery)) {

            $row2 = mysqli_fetch_array($makeQuery);
            $template_criterion_id = $row2['id'];
            $description12 = mysqli_escape_string($conn, $criterion->description12);
            $queryForScale = "insert into scales (`description`, `min_score`, `max_score`) values ('{$description12}', 1, 2)";
            $makeQueryForScale = mysqli_query($conn, $queryForScale);
            if ($makeQueryForScale) {
                $toFindScale = "select * from scales order by id DESC";
                $findScale = mysqli_query($conn, $toFindScale);
                if (mysqli_num_rows($findScale)) {
                    $scale = mysqli_fetch_array($findScale);
                    $queryToInsert = "insert into `template_criterion_scale` (`template_criterion_id`,`scale_id`, `position`) values ({$template_criterion_id}, {$scale['id']}, 0)";
                    $insertIntoTemplate = mysqli_query($conn, $queryToInsert);
                    if (!$insertIntoTemplate) {
                        echo "Scales problem: " . mysqli_error($conn) . $queryToInsert;
                        die;
                    }
                }
            } else {
                echo "Couldnt insert scale: " . mysqli_error($conn) . $queryForScale;
                die;
            }
            $description34 = mysqli_escape_string($conn, $criterion->description34);
            $queryForScale = "insert into scales (`description`, `min_score`, `max_score`) values ('{$description34}', 3, 4)";
            $makeQueryForScale = mysqli_query($conn, $queryForScale);
            if ($makeQueryForScale) {
                $toFindScale = "select * from scales order by id DESC";
                $findScale = mysqli_query($conn, $toFindScale);
                if (mysqli_num_rows($findScale)) {
                    $scale = mysqli_fetch_array($findScale);
                    $queryToInsert = "insert into `template_criterion_scale` (`template_criterion_id`,`scale_id`, `position`) values ({$template_criterion_id}, {$scale['id']}, 1)";
                    $insertIntoTemplate = mysqli_query($conn, $queryToInsert);
                    if (!$insertIntoTemplate) {
                        echo "Scales problem: " . mysqli_error($conn) . $queryToInsert;
                        die;
                    }
                }
            } else {
                echo "Couldnt insert scale: " . mysqli_error($conn) . $queryForScale;
                die;
            }
            $description56 = mysqli_escape_string($conn, $criterion->description56);
            $queryForScale = "insert into scales (`description`, `min_score`, `max_score`) values ('{$description56}', 5, 6)";
            $makeQueryForScale = mysqli_query($conn, $queryForScale);
            if ($makeQueryForScale) {
                $toFindScale = "select * from scales order by id DESC";
                $findScale = mysqli_query($conn, $toFindScale);
                if (mysqli_num_rows($findScale)) {
                    $scale = mysqli_fetch_array($findScale);
                    $queryToInsert = "insert into `template_criterion_scale` (`template_criterion_id`,`scale_id`, `position`) values ({$template_criterion_id}, {$scale['id']}, 2)";
                    $insertIntoTemplate = mysqli_query($conn, $queryToInsert);
                    if (!$insertIntoTemplate) {
                        echo "Scales problem: " . mysqli_error($conn) . $queryToInsert;
                        die;
                    }
                }
            } else {
                echo "Couldnt insert scale: " . mysqli_error($conn) . $queryForScale;
                die;
            }
            $description78 = mysqli_escape_string($conn, $criterion->description78);
            $queryForScale = "insert into scales (`description`, `min_score`, `max_score`) values ('{$description78}', 7, 8)";
            $makeQueryForScale = mysqli_query($conn, $queryForScale);
            if ($makeQueryForScale) {
                $toFindScale = "select * from scales order by id DESC";
                $findScale = mysqli_query($conn, $toFindScale);
                if (mysqli_num_rows($findScale)) {
                    $scale = mysqli_fetch_array($findScale);
                    $queryToInsert = "insert into `template_criterion_scale` (`template_criterion_id`,`scale_id`, `position`) values ({$template_criterion_id}, {$scale['id']}, 3)";
                    $insertIntoTemplate = mysqli_query($conn, $queryToInsert);
                    if (!$insertIntoTemplate) {
                        echo "Scales problem: " . mysqli_error($conn) . $queryToInsert;
                        die;
                    }
                }
            } else {
                echo "Couldnt insert scale: " . mysqli_error($conn) . $queryForScale;
                die;
            }
        } else {
            echo "no creo que hay problema no?";
        }
    }
}
