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

/**
 * Functions used by some stages of rubric grading upgrade
 *
 * @package   workshopform_rubric
 * @copyright 2010 <NAME> <<EMAIL>>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

/**
 * Check if there are some legacy workshop 1.x data to be migrated and upgrade them
 *
 * This must be called after workshop core migration has finished so that
 * all assessments are already upgraded and tables are correctly renamed.
 */
function workshopform_rubric_upgrade_legacy() {

    if (!workshopform_rubric_upgrade_legacy_needed()) {
        return;
    }
    workshopform_rubric_upgrade_legacy_criterion();
    workshopform_rubric_upgrade_legacy_rubric();
}

/**
 * Upgrades legacy workshops using criterion grading strategy
 */
function workshopform_rubric_upgrade_legacy_criterion() {
    global $CFG, $DB, $OUTPUT;
    require_once($CFG->dirroot . '/mod/workshop/db/upgradelib.php');

    // get the list of all legacy workshops using this grading strategy
    if ($legacyworkshops = $DB->get_records('workshop_old', array('gradingstrategy' => 3), 'course,id', 'id')) {
        echo $OUTPUT->notification('Copying criterion assessment form elements', 'notifysuccess');
        $legacyworkshops = array_keys($legacyworkshops);
        // get the list of all form elements
        list($workshopids, $params) = $DB->get_in_or_equal($legacyworkshops, SQL_PARAMS_NAMED);
        $sql = "SELECT *
                  FROM {workshop_elements_old}
                 WHERE workshopid $workshopids
                       AND newid IS NULL";
        $rs = $DB->get_recordset_sql($sql, $params);
        $newworkshopids = workshop_upgrade_workshop_id_mappings();
        $newdimensionids = array(); // (int)oldworkshopid => (int)dimensionid
        foreach ($rs as $old) {
            // create rubric criterion and the configuration if necessary
            if (!isset($newdimensionids[$old->workshopid])) {
                if (!$DB->record_exists('workshopform_rubric', array('workshopid' => $newworkshopids[$old->workshopid], 'sort' => 1))) {
                    $newdimension = new stdclass();
                    $newdimenison->workshopid = $newworkshopids[$old->workshopid];
                    $newdimenison->sort = 1;
                    $newdimenison->description = trim(get_string('dimensionnumber', 'workshopform_rubric', ''));
                    $newdimenison->descriptionformat = FORMAT_HTML;
                    $newdimensionids[$old->workshopid] = $DB->insert_record('workshopform_rubric', $newdimenison);
                } else {
                    $newdimensionids[$old->workshopid] = $DB->get_field('workshopform_rubric', 'id',
                                                                array('workshopid' => $newworkshopids[$old->workshopid], 'sort' => 1));
                }
                if (!$DB->record_exists('workshopform_rubric_config', array('workshopid' => $newworkshopids[$old->workshopid]))) {
                    $newconfig = new stdclass();
                    $newconfig->workshopid = $newworkshopids[$old->workshopid];
                    $newconfig->layout = 'list';
                    $DB->insert_record('workshopform_rubric_config', $newconfig);
                }
            }
            // process the information about the criterion levels
            if (trim($old->description)) {
                $new = workshopform_rubric_upgrade_criterion_level($old, $newdimensionids[$old->workshopid]);
                $newid = $DB->insert_record('workshopform_rubric_levels', $new);
                $DB->set_field('workshop_elements_old', 'newplugin', 'rubric_levels', array('id' => $old->id));
                $DB->set_field('workshop_elements_old', 'newid', $newid, array('id' => $old->id));
            }
        }
        $rs->close();

        // reload the mappings - this must be reloaded to that we can run this during recovery
        $newelementids = workshop_upgrade_element_id_mappings('rubric_levels');

        // migrate all grades for these elements (it est the values that reviewers put into forms)
        echo $OUTPUT->notification('Copying criterion assessment form grades', 'notifysuccess');
        $sql = "SELECT *
                  FROM {workshop_grades_old}
                 WHERE workshopid $workshopids
                       AND elementno = 0
                       AND newid IS NULL";
        $rs = $DB->get_recordset_sql($sql, $params);
        $newassessmentids = workshop_upgrade_assessment_id_mappings();
        $newdimensionids = $DB->get_records('workshopform_rubric_levels', array(), '', 'id,dimensionid');
        foreach ($rs as $old) {
            if (!isset($newelementids[$old->workshopid]) or !isset($newelementids[$old->workshopid][$old->elementno])) {
                // orphaned grade - the assessment form element has been removed after the grade was recorded
                continue;
            }
            $newlevelid = $newelementids[$old->workshopid][$old->elementno]->newid;
            $new                = new stdclass();
            $new->assessmentid  = $newassessmentids[$old->assessmentid];
            $new->strategy      = 'rubric';
            $new->dimensionid   = $newdimensionids[$newlevelid]->dimensionid;
            $new->grade         = $newelementids[$old->workshopid][$old->grade]->maxscore;
            $newid = $DB->insert_record('workshop_grades', $new);
            $DB->set_field('workshop_grades_old', 'newplugin', 'rubric', array('id' => $old->id));
            $DB->set_field('workshop_grades_old', 'newid', $newid, array('id' => $old->id));
        }
        $rs->close();
    }
}

/**
 * Upgrades legacy workshops using rubric grading strategy
 */
function workshopform_rubric_upgrade_legacy_rubric() {
    global $CFG, $DB, $OUTPUT;
    require_once($CFG->dirroot . '/mod/workshop/db/upgradelib.php');

    // get the list of all legacy workshops using this grading strategy
    if ($legacyworkshops = $DB->get_records('workshop_old', array('gradingstrategy' => 4), 'course,id', 'id')) {
        echo $OUTPUT->notification('Copying rubric assessment form elements', 'notifysuccess');
        $legacyworkshops = array_keys($legacyworkshops);
        // get the list of all form elements and rubrics
        list($workshopids, $params) = $DB->get_in_or_equal($legacyworkshops, SQL_PARAMS_NAMED);
        $sql = "SELECT e.id AS eid, e.workshopid AS workshopid, e.elementno AS esort, e.description AS edesc, e.weight AS eweight,
                       r.id AS rid, r.rubricno AS rgrade, r.description AS rdesc
                  FROM {workshop_elements_old} e
             LEFT JOIN {workshop_rubrics_old} r ON (r.elementno = e.elementno AND r.workshopid = e.workshopid)
                 WHERE e.workshopid $workshopids
                       AND e.newid IS NULL
                       AND r.newid IS NULL
              ORDER BY e.workshopid, e.elementno, r.rubricno";
        $rs = $DB->get_recordset_sql($sql, $params);
        $newworkshopids     = workshop_upgrade_workshop_id_mappings();  // array of (int)oldworkshopid => (int)newworkshopid
        $newdimensionids    = array();  // (int)oldworkshopid => (int)elementno => (int)dimensionid
        $newlevelids        = array();  // (int)oldrubricid => (int)newlevelid
        $prevelement        = null;
        foreach ($rs as $old) {
            // create rubric criterion and the configuration if necessary
            if (!isset($newdimensionids[$old->workshopid]) or !isset($newdimensionids[$old->workshopid][$old->esort])) {
                if (!$DB->record_exists('workshopform_rubric', array('workshopid' => $newworkshopids[$old->workshopid], 'sort' => $old->esort))) {
                    $newdimension = new stdclass();
                    $newdimenison->workshopid = $newworkshopids[$old->workshopid];
                    $newdimenison->sort = $old->esort;
                    $newdimenison->description = $old->edesc;
                    $newdimenison->descriptionformat = FORMAT_HTML;
                    $newdimensionids[$old->workshopid][$old->esort] = $DB->insert_record('workshopform_rubric', $newdimenison);
                } else {
                    $newdimensionids[$old->workshopid][$old->esort] = $DB->get_field('workshopform_rubric', 'id',
                                                                    array('workshopid' => $newworkshopids[$old->workshopid], 'sort' => $old->esort));
                }
                if (!$DB->record_exists('workshopform_rubric_config', array('workshopid' => $newworkshopids[$old->workshopid]))) {
                    $newconfig = new stdclass();
                    $newconfig->workshopid = $newworkshopids[$old->workshopid];
                    $newconfig->layout = 'grid';
                    $DB->insert_record('workshopform_rubric_config', $newconfig);
                }
            }
            // process the information about the criterion levels
            if (trim($old->rdesc)) {
                $new = workshopform_rubric_upgrade_rubric_level($old, $newdimensionids[$old->workshopid][$old->esort]);
                $newid = $DB->insert_record('workshopform_rubric_levels', $new);
                $DB->set_field('workshop_rubrics_old', 'newplugin', 'rubric_levels', array('id' => $old->rid));
                $DB->set_field('workshop_rubrics_old', 'newid', $newid, array('id' => $old->rid));
            }
            // mark the whole element as processed if the last level was processed<commit_msg>fixed $newdimension typos<commit_after><?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/>.

/**
 * Functions used by some stages of rubric grading upgrade
 *
 * @package   workshopform_rubric
 * @copyright 2010 <NAME> <<EMAIL>>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

/**
 * Check if there are some legacy workshop 1.x data to be migrated and upgrade them
 *
 * This must be called after workshop core migration has finished so that
 * all assessments are already upgraded and tables are correctly renamed.
 */
function workshopform_rubric_upgrade_legacy() {

    if (!workshopform_rubric_upgrade_legacy_needed()) {
        return;
    }
    workshopform_rubric_upgrade_legacy_criterion();
    workshopform_rubric_upgrade_legacy_rubric();
}

/**
 * Upgrades legacy workshops using criterion grading strategy
 */
function workshopform_rubric_upgrade_legacy_criterion() {
    global $CFG, $DB, $OUTPUT;
    require_once($CFG->dirroot . '/mod/workshop/db/upgradelib.php');

    // get the list of all legacy workshops using this grading strategy
    if ($legacyworkshops = $DB->get_records('workshop_old', array('gradingstrategy' => 3), 'course,id', 'id')) {
        echo $OUTPUT->notification('Copying criterion assessment form elements', 'notifysuccess');
        $legacyworkshops = array_keys($legacyworkshops);
        // get the list of all form elements
        list($workshopids, $params) = $DB->get_in_or_equal($legacyworkshops, SQL_PARAMS_NAMED);
        $sql = "SELECT *
                  FROM {workshop_elements_old}
                 WHERE workshopid $workshopids
                       AND newid IS NULL";
        $rs = $DB->get_recordset_sql($sql, $params);
        $newworkshopids = workshop_upgrade_workshop_id_mappings();
        $newdimensionids = array(); // (int)oldworkshopid => (int)dimensionid
        foreach ($rs as $old) {
            // create rubric criterion and the configuration if necessary
            if (!isset($newdimensionids[$old->workshopid])) {
                if (!$DB->record_exists('workshopform_rubric', array('workshopid' => $newworkshopids[$old->workshopid], 'sort' => 1))) {
                    $newdimension = new stdclass();
                    $newdimension->workshopid = $newworkshopids[$old->workshopid];
                    $newdimension->sort = 1;
                    $newdimension->description = trim(get_string('dimensionnumber', 'workshopform_rubric', ''));
                    $newdimension->descriptionformat = FORMAT_HTML;
                    $newdimensionids[$old->workshopid] = $DB->insert_record('workshopform_rubric', $newdimension);
                } else {
                    $newdimensionids[$old->workshopid] = $DB->get_field('workshopform_rubric', 'id',
                                                                array('workshopid' => $newworkshopids[$old->workshopid], 'sort' => 1));
                }
                if (!$DB->record_exists('workshopform_rubric_config', array('workshopid' => $newworkshopids[$old->workshopid]))) {
                    $newconfig = new stdclass();
                    $newconfig->workshopid = $newworkshopids[$old->workshopid];
                    $newconfig->layout = 'list';
                    $DB->insert_record('workshopform_rubric_config', $newconfig);
                }
            }
            // process the information about the criterion levels
            if (trim($old->description)) {
                $new = workshopform_rubric_upgrade_criterion_level($old, $newdimensionids[$old->workshopid]);
                $newid = $DB->insert_record('workshopform_rubric_levels', $new);
                $DB->set_field('workshop_elements_old', 'newplugin', 'rubric_levels', array('id' => $old->id));
                $DB->set_field('workshop_elements_old', 'newid', $newid, array('id' => $old->id));
            }
        }
        $rs->close();

        // reload the mappings - this must be reloaded to that we can run this during recovery
        $newelementids = workshop_upgrade_element_id_mappings('rubric_levels');

        // migrate all grades for these elements (it est the values that reviewers put into forms)
        echo $OUTPUT->notification('Copying criterion assessment form grades', 'notifysuccess');
        $sql = "SELECT *
                  FROM {workshop_grades_old}
                 WHERE workshopid $workshopids
                       AND elementno = 0
                       AND newid IS NULL";
        $rs = $DB->get_recordset_sql($sql, $params);
        $newassessmentids = workshop_upgrade_assessment_id_mappings();
        $newdimensionids = $DB->get_records('workshopform_rubric_levels', array(), '', 'id,dimensionid');
        foreach ($rs as $old) {
            if (!isset($newelementids[$old->workshopid]) or !isset($newelementids[$old->workshopid][$old->elementno])) {
                // orphaned grade - the assessment form element has been removed after the grade was recorded
                continue;
            }
            $newlevelid = $newelementids[$old->workshopid][$old->elementno]->newid;
            $new                = new stdclass();
            $new->assessmentid  = $newassessmentids[$old->assessmentid];
            $new->strategy      = 'rubric';
            $new->dimensionid   = $newdimensionids[$newlevelid]->dimensionid;
            $new->grade         = $newelementids[$old->workshopid][$old->grade]->maxscore;
            $newid = $DB->insert_record('workshop_grades', $new);
            $DB->set_field('workshop_grades_old', 'newplugin', 'rubric', array('id' => $old->id));
            $DB->set_field('workshop_grades_old', 'newid', $newid, array('id' => $old->id));
        }
        $rs->close();
    }
}

/**
 * Upgrades legacy workshops using rubric grading strategy
 */
function workshopform_rubric_upgrade_legacy_rubric() {
    global $CFG, $DB, $OUTPUT;
    require_once($CFG->dirroot . '/mod/workshop/db/upgradelib.php');

    // get the list of all legacy workshops using this grading strategy
    if ($legacyworkshops = $DB->get_records('workshop_old', array('gradingstrategy' => 4), 'course,id', 'id')) {
        echo $OUTPUT->notification('Copying rubric assessment form elements', 'notifysuccess');
        $legacyworkshops = array_keys($legacyworkshops);
        // get the list of all form elements and rubrics
        list($workshopids, $params) = $DB->get_in_or_equal($legacyworkshops, SQL_PARAMS_NAMED);
        $sql = "SELECT e.id AS eid, e.workshopid AS workshopid, e.elementno AS esort, e.description AS edesc, e.weight AS eweight,
                       r.id AS rid, r.rubricno AS rgrade, r.description AS rdesc
                  FROM {workshop_elements_old} e
             LEFT JOIN {workshop_rubrics_old} r ON (r.elementno = e.elementno AND r.workshopid = e.workshopid)
                 WHERE e.workshopid $workshopids
                       AND e.newid IS NULL
                       AND r.newid IS NULL
              ORDER BY e.workshopid, e.elementno, r.rubricno";
        $rs = $DB->get_recordset_sql($sql, $params);
        $newworkshopids     = workshop_upgrade_workshop_id_mappings();  // array of (int)oldworkshopid => (int)newworkshopid
        $newdimensionids    = array();  // (int)oldworkshopid => (int)elementno => (int)dimensionid
        $newlevelids        = array();  // (int)oldrubricid => (int)newlevelid
        $prevelement        = null;
        foreach ($rs as $old) {
            // create rubric criterion and the configuration if necessary
            if (!isset($newdimensionids[$old->workshopid]) or !isset($newdimensionids[$old->workshopid][$old->esort])) {
                if (!$DB->record_exists('workshopform_rubric', array('workshopid' => $newworkshopids[$old->workshopid], 'sort' => $old->esort))) {
                    $newdimension = new stdclass();
                    $newdimension->workshopid = $newworkshopids[$old->workshopid];
                    $newdimension->sort = $old->esort;
                    $newdimension->description = $old->edesc;
                    $newdimension->descriptionformat = FORMAT_HTML;
                    $newdimensionids[$old->workshopid][$old->esort] = $DB->insert_record('workshopform_rubric', $newdimension);
                } else {
                    $newdimensionids[$old->workshopid][$old->esort] = $DB->get_field('workshopform_rubric', 'id',
                                                                    array('workshopid' => $newworkshopids[$old->workshopid], 'sort' => $old->esort));
                }
                if (!$DB->record_exists('workshopform_rubric_config', array('workshopid' => $newworkshopids[$old->workshopid]))) {
                    $newconfig = new stdclass();
                    $newconfig->workshopid = $newworkshopids[$old->workshopid];
                    $newconfig->layout = 'grid';
                    $DB->insert_record('workshopform_rubric_config', $newconfig);
                }
            }
            // process the information about the criterion levels
            if (trim($old->rdesc)) {
                $new = workshopform_rubric_upgrade_rubric_level($old, $newdimensionids[$old->workshopid][$old->esort]);
                $newid = $DB->insert_record('workshopform_rubric_levels', $new);
                $DB->set_field('workshop_rubrics_old', 'newplugin', 'rubric_levels', array('id' => $old->rid));
                $DB->set_field('workshop_rubrics_old', 'newid', $newid, array('id' => $old->rid));
            }
            // mark the whole element as processed if the last level was processed
{'fraction_non_alphanumeric': 0.12696309383588536, 'fraction_numerical': 0.001276010993325481, 'mean_word_length': 2.493399622835591, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 249, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2878257', 'n_tokens_mistral': 5939, 'n_tokens_neox': 5665, 'n_words': 1323}