diff --git a/course/edit.php b/course/edit.php index f257f910018..b88ab774f76 100644 --- a/course/edit.php +++ b/course/edit.php @@ -45,8 +45,11 @@ if ($id) { print_error('cannoteditsiteform'); } - $course = course_get_format($id)->get_course(); + // Login to the course and retrieve also all fields defined by course format. + $course = get_course($id); require_login($course); + $course = course_get_format($course)->get_course(); + $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST); $coursecontext = context_course::instance($course->id); require_capability('moodle/course:update', $coursecontext); diff --git a/course/format/lib.php b/course/format/lib.php index 38c4a69b989..a68ce5ae500 100644 --- a/course/format/lib.php +++ b/course/format/lib.php @@ -237,14 +237,21 @@ abstract class format_base { if ($this->course === false) { $this->course = get_course($this->courseid); $options = $this->get_format_options(); + $dbcoursecolumns = null; foreach ($options as $optionname => $optionvalue) { - if (!isset($this->course->$optionname)) { - $this->course->$optionname = $optionvalue; - } else { - debugging('The option name '.$optionname.' in course format '.$this->format. - ' is invalid because the field with the same name exists in {course} table', - DEBUG_DEVELOPER); + if (isset($this->course->$optionname)) { + // Course format options must not have the same names as existing columns in db table "course". + if (!isset($dbcoursecolumns)) { + $dbcoursecolumns = $DB->get_columns('course'); + } + if (isset($dbcoursecolumns[$optionname])) { + debugging('The option name '.$optionname.' in course format '.$this->format. + ' is invalid because the field with the same name exists in {course} table', + DEBUG_DEVELOPER); + continue; + } } + $this->course->$optionname = $optionvalue; } } return $this->course; diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 0fe0e436348..6aff33e5cb6 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -85,7 +85,7 @@ defined('MOODLE_INTERNAL') || die(); * @return bool always true */ function xmldb_main_upgrade($oldversion) { - global $CFG, $USER, $DB, $OUTPUT, $SITE; + global $CFG, $USER, $DB, $OUTPUT, $SITE, $COURSE; require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions @@ -316,6 +316,10 @@ function xmldb_main_upgrade($oldversion) { $dbman->drop_field($table, $field); } + // Since structure of 'course' table has changed we need to re-read $SITE from DB. + $SITE = $DB->get_record('course', array('id' => $SITE->id)); + $COURSE = clone($SITE); + upgrade_main_savepoint(true, 2012031500.02); } @@ -447,6 +451,10 @@ function xmldb_main_upgrade($oldversion) { $dbman->add_field($table, $field); } + // Since structure of 'course' table has changed we need to re-read $SITE from DB. + $SITE = $DB->get_record('course', array('id' => $SITE->id)); + $COURSE = clone($SITE); + // Main savepoint reached upgrade_main_savepoint(true, 2012050300.03); } @@ -563,6 +571,10 @@ function xmldb_main_upgrade($oldversion) { $dbman->add_field($table, $field); } + // Since structure of 'course' table has changed we need to re-read $SITE from DB. + $SITE = $DB->get_record('course', array('id' => $SITE->id)); + $COURSE = clone($SITE); + // Add course_sections_availability to add completion & grade availability conditions $table = new xmldb_table('course_sections_availability'); @@ -1334,6 +1346,10 @@ function xmldb_main_upgrade($oldversion) { // Launch change of type for field format $dbman->change_field_type($table, $field); + // Since structure of 'course' table has changed we need to re-read $SITE from DB. + $SITE = $DB->get_record('course', array('id' => $SITE->id)); + $COURSE = clone($SITE); + // Main savepoint reached upgrade_main_savepoint(true, 2012110200.00); } @@ -1386,6 +1402,10 @@ function xmldb_main_upgrade($oldversion) { } } + // Since structure of 'course' table has changed we need to re-read $SITE from DB. + $SITE = $DB->get_record('course', array('id' => $SITE->id)); + $COURSE = clone($SITE); + // Main savepoint reached upgrade_main_savepoint(true, 2012110201.00); } @@ -1501,6 +1521,7 @@ function xmldb_main_upgrade($oldversion) { if ($SITE->format !== 'site') { $DB->set_field('course', 'format', 'site', array('id' => $SITE->id)); $SITE->format = 'site'; + $COURSE->format = 'site'; } // Main savepoint reached @@ -1989,6 +2010,10 @@ function xmldb_main_upgrade($oldversion) { $dbman->drop_field($table, $field); } + // Since structure of 'course' table has changed we need to re-read $SITE from DB. + $SITE = $DB->get_record('course', array('id' => $SITE->id)); + $COURSE = clone($SITE); + // Main savepoint reached. upgrade_main_savepoint(true, 2013040300.01); } @@ -2376,6 +2401,10 @@ function xmldb_main_upgrade($oldversion) { $dbman->add_field($table, $field); } + // Since structure of 'course' table has changed we need to re-read $SITE from DB. + $SITE = $DB->get_record('course', array('id' => $SITE->id)); + $COURSE = clone($SITE); + // Define field calendartype to be added to user. $table = new xmldb_table('user'); $field = new xmldb_field('calendartype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'gregorian'); @@ -2400,6 +2429,10 @@ function xmldb_main_upgrade($oldversion) { $dbman->add_field($table, $field); } + // Since structure of 'course' table has changed we need to re-read $SITE from DB. + $SITE = $DB->get_record('course', array('id' => $SITE->id)); + $COURSE = clone($SITE); + // Main savepoint reached. upgrade_main_savepoint(true, 2013091000.02); } @@ -2423,6 +2456,10 @@ function xmldb_main_upgrade($oldversion) { $dbman->drop_field($table, $field); } + // Since structure of 'course' table has changed we need to re-read $SITE from DB. + $SITE = $DB->get_record('course', array('id' => $SITE->id)); + $COURSE = clone($SITE); + // Main savepoint reached. upgrade_main_savepoint(true, 2013091000.03); } diff --git a/lib/upgradelib.php b/lib/upgradelib.php index 338a5ccf804..472b32a5ab1 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -1503,7 +1503,7 @@ function install_core($version, $verbose) { * @return void, may throw exception */ function upgrade_core($version, $verbose) { - global $CFG; + global $CFG, $SITE, $DB, $COURSE; raise_memory_limit(MEMORY_EXTRA); @@ -1534,6 +1534,10 @@ function upgrade_core($version, $verbose) { upgrade_main_savepoint($result, $version, false); } + // In case structure of 'course' table has been changed and we forgot to update $SITE, re-read it from db. + $SITE = $DB->get_record('course', array('id' => $SITE->id)); + $COURSE = clone($SITE); + // perform all other component upgrade routines update_capabilities('moodle'); log_update_descriptions('moodle');