Ignore:
Timestamp:
May 2, 2016, 12:09:23 PM (3 years ago)
Author:
jrpelegrina
Message:

Updated to moodle 3.0.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • moodle/trunk/fuentes/lib/db/upgradelib.php

    r136 r1331  
    531531    }
    532532}
     533
     534/**
     535 * Marks all courses with changes in extra credit weight calculation
     536 *
     537 * Used during upgrade and in course restore process
     538 *
     539 * This upgrade script is needed because we changed the algorithm for calculating the automatic weights of extra
     540 * credit items and want to prevent changes in the existing student grades.
     541 *
     542 * @param int $onlycourseid
     543 */
     544function upgrade_extra_credit_weightoverride($onlycourseid = 0) {
     545    global $DB;
     546
     547    // Find all courses that have categories in Natural aggregation method where there is at least one extra credit
     548    // item and at least one item with overridden weight.
     549    $courses = $DB->get_fieldset_sql(
     550        "SELECT DISTINCT gc.courseid
     551          FROM {grade_categories} gc
     552          INNER JOIN {grade_items} gi ON gc.id = gi.categoryid AND gi.weightoverride = :weightoverriden
     553          INNER JOIN {grade_items} gie ON gc.id = gie.categoryid AND gie.aggregationcoef = :extracredit
     554          WHERE gc.aggregation = :naturalaggmethod" . ($onlycourseid ? " AND gc.courseid = :onlycourseid" : ''),
     555        array('naturalaggmethod' => 13,
     556            'weightoverriden' => 1,
     557            'extracredit' => 1,
     558            'onlycourseid' => $onlycourseid,
     559        )
     560    );
     561    foreach ($courses as $courseid) {
     562        $gradebookfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
     563        if (!$gradebookfreeze) {
     564            set_config('gradebook_calculations_freeze_' . $courseid, 20150619);
     565        }
     566    }
     567}
     568
     569/**
     570 * Marks all courses that require calculated grade items be updated.
     571 *
     572 * Used during upgrade and in course restore process.
     573 *
     574 * This upgrade script is needed because the calculated grade items were stuck with a maximum of 100 and could be changed.
     575 * This flags the courses that are affected and the grade book is frozen to retain grade integrity.
     576 *
     577 * @param int $courseid Specify a course ID to run this script on just one course.
     578 */
     579function upgrade_calculated_grade_items($courseid = null) {
     580    global $DB, $CFG;
     581
     582    $affectedcourses = array();
     583    $possiblecourseids = array();
     584    $params = array();
     585    $singlecoursesql = '';
     586    if (isset($courseid)) {
     587        $singlecoursesql = "AND ns.id = :courseid";
     588        $params['courseid'] = $courseid;
     589    }
     590    $siteminmaxtouse = 1;
     591    if (isset($CFG->grade_minmaxtouse)) {
     592        $siteminmaxtouse = $CFG->grade_minmaxtouse;
     593    }
     594    $courseidsql = "SELECT ns.id
     595                      FROM (
     596                        SELECT c.id, coalesce(" . $DB->sql_compare_text('gs.value') . ", :siteminmax) AS gradevalue
     597                          FROM {course} c
     598                          LEFT JOIN {grade_settings} gs
     599                            ON c.id = gs.courseid
     600                           AND ((gs.name = 'minmaxtouse' AND " . $DB->sql_compare_text('gs.value') . " = '2'))
     601                        ) ns
     602                    WHERE " . $DB->sql_compare_text('ns.gradevalue') . " = '2' $singlecoursesql";
     603    $params['siteminmax'] = $siteminmaxtouse;
     604    $courses = $DB->get_records_sql($courseidsql, $params);
     605    foreach ($courses as $course) {
     606        $possiblecourseids[$course->id] = $course->id;
     607    }
     608
     609    if (!empty($possiblecourseids)) {
     610        list($sql, $params) = $DB->get_in_or_equal($possiblecourseids);
     611        // A calculated grade item grade min != 0 and grade max != 100 and the course setting is set to
     612        // "Initial min and max grades".
     613        $coursesql = "SELECT DISTINCT courseid
     614                        FROM {grade_items}
     615                       WHERE calculation IS NOT NULL
     616                         AND itemtype = 'manual'
     617                         AND (grademax <> 100 OR grademin <> 0)
     618                         AND courseid $sql";
     619        $affectedcourses = $DB->get_records_sql($coursesql, $params);
     620    }
     621
     622    // Check for second type of affected courses.
     623    // If we already have the courseid parameter set in the affectedcourses then there is no need to run through this section.
     624    if (!isset($courseid) || !in_array($courseid, $affectedcourses)) {
     625        $singlecoursesql = '';
     626        $params = array();
     627        if (isset($courseid)) {
     628            $singlecoursesql = "AND courseid = :courseid";
     629            $params['courseid'] = $courseid;
     630        }
     631        $nestedsql = "SELECT id
     632                        FROM {grade_items}
     633                       WHERE itemtype = 'category'
     634                         AND calculation IS NOT NULL $singlecoursesql";
     635        $calculatedgradecategories = $DB->get_records_sql($nestedsql, $params);
     636        $categoryids = array();
     637        foreach ($calculatedgradecategories as $key => $gradecategory) {
     638            $categoryids[$key] = $gradecategory->id;
     639        }
     640
     641        if (!empty($categoryids)) {
     642            list($sql, $params) = $DB->get_in_or_equal($categoryids);
     643            // A category with a calculation where the raw grade min and the raw grade max don't match the grade min and grade max
     644            // for the category.
     645            $coursesql = "SELECT DISTINCT gi.courseid
     646                            FROM {grade_grades} gg, {grade_items} gi
     647                           WHERE gi.id = gg.itemid
     648                             AND (gg.rawgrademax <> gi.grademax OR gg.rawgrademin <> gi.grademin)
     649                             AND gi.id $sql";
     650            $additionalcourses = $DB->get_records_sql($coursesql, $params);
     651            foreach ($additionalcourses as $key => $additionalcourse) {
     652                if (!array_key_exists($key, $affectedcourses)) {
     653                    $affectedcourses[$key] = $additionalcourse;
     654                }
     655            }
     656        }
     657    }
     658
     659    foreach ($affectedcourses as $affectedcourseid) {
     660        if (isset($CFG->upgrade_calculatedgradeitemsonlyregrade) && !($courseid)) {
     661            $DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $affectedcourseid->courseid));
     662        } else {
     663            // Check to see if the gradebook freeze is already in affect.
     664            $gradebookfreeze = get_config('core', 'gradebook_calculations_freeze_' . $affectedcourseid->courseid);
     665            if (!$gradebookfreeze) {
     666                set_config('gradebook_calculations_freeze_' . $affectedcourseid->courseid, 20150627);
     667            }
     668        }
     669    }
     670}
     671
     672/**
     673 * This upgrade script merges all tag instances pointing to the same course tag
     674 *
     675 * User id is no longer used for those tag instances
     676 */
     677function upgrade_course_tags() {
     678    global $DB;
     679    $sql = "SELECT min(ti.id)
     680        FROM {tag_instance} ti
     681        LEFT JOIN {tag_instance} tii on tii.itemtype = ? and tii.itemid = ti.itemid and tii.tiuserid = 0 and tii.tagid = ti.tagid
     682        where ti.itemtype = ? and ti.tiuserid <> 0 AND tii.id is null
     683        group by ti.tagid, ti.itemid";
     684    $ids = $DB->get_fieldset_sql($sql, array('course', 'course'));
     685    if ($ids) {
     686        list($idsql, $idparams) = $DB->get_in_or_equal($ids);
     687        $DB->execute('UPDATE {tag_instance} SET tiuserid = 0 WHERE id ' . $idsql, $idparams);
     688    }
     689    $DB->execute("DELETE FROM {tag_instance} WHERE itemtype = ? AND tiuserid <> 0", array('course'));
     690}
Note: See TracChangeset for help on using the changeset viewer.