function xmldb_lightboxgallery_upgrade($oldversion = 0)
{
    global $CFG, $THEME, $db;
    $result = true;
    if ($result && $oldversion < 2007111400) {
        // Insert perpage and comments fields into lightboxgallery
        $table = new XMLDBTable('lightboxgallery');
        $field = new XMLDBField('perpage');
        if (!field_exists($table, $field)) {
            $field->setAttributes(XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'description');
            $result = $result && add_field($table, $field);
        }
        $field = new XMLDBField('comments');
        if (!field_exists($table, $field)) {
            $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'perpage');
            $result = $result && add_field($table, $field);
        }
        // Create new lightboxgallery_comments table
        $table = new XMLDBTable('lightboxgallery_comments');
        if (!table_exists($table)) {
            $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
            $table->addFieldInfo('gallery', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
            $table->addFieldInfo('user', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
            $table->addFieldInfo('comment', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
            $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
            $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
            $table->addIndexInfo('gallery', XMLDB_INDEX_NOTUNIQUE, array('gallery'));
            $result = $result && create_table($table);
        }
        // Insert add_to_log entry to log_display table
        if (!record_exists('log_display', 'module', 'lightboxgallery', 'action', 'comment')) {
            $record = new object();
            $record->module = 'lightboxgallery';
            $record->action = 'comment';
            $record->mtable = 'lightboxgallery';
            $record->field = 'name';
            $result = $result && insert_record('log_display', $record);
        }
    }
    if ($result && $oldversion < 2007121700) {
        // Insert extinfo field into lightboxgallery
        $table = new XMLDBTable('lightboxgallery');
        $field = new XMLDBField('extinfo');
        if (!field_exists($table, $field)) {
            $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'comments');
            $result = $result && add_field($table, $field);
        }
        // Create lightboxgallery_captions table
        $table = new XMLDBTable('lightboxgallery_captions');
        if (!table_exists($table)) {
            $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
            $table->addFieldInfo('gallery', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
            $table->addFieldInfo('image', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
            $table->addFieldInfo('caption', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
            $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
            $table->addIndexInfo('gallery', XMLDB_INDEX_NOTUNIQUE, array('gallery'));
            $result = $result && create_table($table);
        }
    }
    if ($result && $oldversion < 2008110600) {
        // Insert public, rss, autoresize, resize fields into lightboxgallery
        $table = new XMLDBTable('lightboxgallery');
        $newfields = array('public', 'rss', 'autoresize', 'resize');
        $previousfield = 'comments';
        foreach ($newfields as $newfield) {
            $field = new XMLDBField($newfield);
            if (!field_exists($table, $field)) {
                $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $previousfield);
                $result = $result && add_field($table, $field);
                $previousfield = $newfield;
            }
        }
        // Rename user field to userid in lightboxgallery_comments (for postgres)
        $table = new XMLDBTable('lightboxgallery_comments');
        $field = new XMLDBField('user');
        if (field_exists($table, $field)) {
            $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
            $result = $result && rename_field($table, $field, 'userid');
        }
        // Rename caption field to description and insert metatype field in lightboxgallery_captions
        $table = new XMLDBTable('lightboxgallery_captions');
        $field = new XMLDBField('caption');
        if (field_exists($table, $field)) {
            $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null, 'image');
            $result = $result && rename_field($table, $field, 'description');
        }
        $field = new XMLDBField('metatype');
        if (table_exists($table) && !field_exists($table, $field)) {
            $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('caption', 'tag'), 'caption', 'image');
            $result = $result && add_field($table, $field);
        }
        // Rename table lightboxgallery_captions to lightboxgallery_image_meta
        $result = $result && rename_table($table, 'lightboxgallery_image_meta');
    }
    if ($result && $oldversion < 2009051200) {
        // Rename public field to ispublic in lightboxgallery (for mssql)
        $table = new XMLDBTable('lightboxgallery');
        $field = new XMLDBField('public');
        if (field_exists($table, $field)) {
            $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'comments');
            $result = $result && rename_field($table, $field, 'ispublic');
        }
    }
    return $result;
}
Esempio n. 2
0
 /**
  * Invoke method, every class will have its own
  * returns true/false on completion, setting both
  * errormsg and output as necessary
  */
 function invoke()
 {
     parent::invoke();
     $result = true;
     /// Set own core attributes
     //$this->does_generate = ACTION_NONE;
     $this->does_generate = ACTION_GENERATE_HTML;
     /// These are always here
     global $CFG, $XMLDB, $db;
     /// ADD YOUR CODE HERE
     require_once $CFG->libdir . '/ddllib.php';
     /// Where all the tests will be stored
     $tests = array();
     /// The back to edit table button
     $b = ' <p class="centerpara buttons">';
     $b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
     $b .= '</p>';
     $o = $b;
     /// Silenty drop any previous test tables
     $table = new XMLDBTable('testtable');
     if (table_exists($table)) {
         $status = drop_table($table, true, false);
     }
     $table = new XMLDBTable('anothertest');
     if (table_exists($table)) {
         $status = drop_table($table, true, false);
     }
     $table = new XMLDBTable('newnameforthetable');
     if (table_exists($table)) {
         $status = drop_table($table, true, false);
     }
     /// 1st test. Complete table creation.
     $table = new XMLDBTable('testtable');
     $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
     $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general');
     $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
     $table->addFieldInfo('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
     $table->addFieldInfo('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null, null, null);
     $table->addFieldInfo('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
     $table->addFieldInfo('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null, null, null);
     $table->addFieldInfo('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null, null, null);
     $table->addFieldInfo('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addFieldInfo('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
     $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
     $table->addKeyInfo('type-name', XMLDB_KEY_UNIQUE, array('type', 'name'));
     $table->addIndexInfo('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
     $table->addIndexInfo('rsstype', XMLDB_INDEX_UNIQUE, array('rsstype'));
     $table->setComment("This is a test'n drop table. You can drop it safely");
     /// Get SQL code and execute it
     $test = new stdClass();
     $test->sql = $table->getCreateTableSQL($CFG->dbtype, $CFG->prefix, true);
     $test->status = create_table($table, false, false);
     if (!$test->status) {
         $test->error = $db->ErrorMsg();
     }
     $tests['create table'] = $test;
     /// 2nd test. drop table
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $test->sql = $table->getDropTableSQL($CFG->dbtype, $CFG->prefix, true);
         $test->status = drop_table($table, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['drop table'] = $test;
     }
     /// 3rd test. creating another, smaller table
     if ($test->status) {
         $table = new XMLDBTable('anothertest');
         $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
         $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
         $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle');
         $table->addFieldInfo('secondname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null);
         $table->addFieldInfo('intro', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null);
         $table->addFieldInfo('avatar', XMLDB_TYPE_BINARY, 'medium', null, null, null, null, null, null);
         $table->addFieldInfo('grade', XMLDB_TYPE_NUMBER, '20,10', null, null, null, null, null);
         $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
         /// Get SQL code and execute it
         $test = new stdClass();
         $test->sql = $table->getCreateTableSQL($CFG->dbtype, $CFG->prefix, true);
         $test->status = create_table($table, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['create table - 2'] = $test;
     }
     /// Insert two records to do the work with real data
     $rec->course = 1;
     $rec->name = 'Martin';
     $rec->secondname = 'Dougiamas';
     $rec->intro = 'The creator of Moodle';
     $rec->grade = 10.0001;
     insert_record('anothertest', $rec);
     $rec->course = 2;
     $rec->name = 'Eloy';
     $rec->secondname = 'Lafuente';
     $rec->intro = 'One poor developer';
     $rec->grade = 9.99;
     insert_record('anothertest', $rec);
     /// 4th test. Adding one complex enum field
     if ($test->status) {
         /// Create a new field with complex specs (enums are good candidates)
         $field = new XMLDBField('type');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
         /// Get SQL code and execute it
         $test = new stdClass();
         $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = add_field($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add enum field'] = $test;
     }
     /// 5th test. Dropping one complex enum field
     if ($test->status) {
         /// Create a new field with complex specs (enums are good candidates)
         $test = new stdClass();
         $test->sql = $table->getDropFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = drop_field($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['drop enum field'] = $test;
     }
     /// 6th test. Adding one complex enum field
     if ($test->status) {
         /// Create a new field with complex specs (enums are good candidates)
         $field = new XMLDBField('type');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
         /// Get SQL code and execute it
         $test = new stdClass();
         $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = add_field($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add enum field again'] = $test;
     }
     /// 7th test. Adding one numeric field
     if ($test->status) {
         /// Create a new field (numeric)
         $field = new XMLDBField('onenumber');
         $field->setAttributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0, 'type');
         /// Get SQL code and execute it
         $test = new stdClass();
         $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = add_field($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add numeric field'] = $test;
     }
     /// 8th test. Dropping one complex enum field
     if ($test->status) {
         /// Create a new field with complex specs (enums are good candidates)
         $field = new XMLDBField('type');
         $test = new stdClass();
         $test->sql = $table->getDropFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = drop_field($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['drop enum field again'] = $test;
     }
     /// 9th test. Change the type of one column from integer to varchar
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('course');
         $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, '0');
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_type($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field type (int2char)'] = $test;
     }
     /// 10th test. Change the type of one column from varchar to integer
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('course');
         $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_type($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field type (char2int)'] = $test;
     }
     /// 11th test. Change the type of one column from number to varchar
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('grade');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, "test'n drop");
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_type($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field type (number2char)'] = $test;
     }
     /// 12th test. Change the type of one column from varchar to float
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('grade');
         $field->setAttributes(XMLDB_TYPE_FLOAT, '20,10', XMLDB_UNSIGNED, null, null, null, null, null);
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_type($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field type (char2float)'] = $test;
     }
     /// 13th test. Change the type of one column from float to char
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('grade');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'test');
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_type($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field type (float2char)'] = $test;
     }
     /// 14th test. Change the type of one column from char to number
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('grade');
         $field->setAttributes(XMLDB_TYPE_NUMBER, '20,10', XMLDB_UNSIGNED, null, null, null, null, null);
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_type($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field type (char2number)'] = $test;
     }
     /// 15th test. Change the precision of one text field
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('intro');
         $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null);
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_precision($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field precision (text)'] = $test;
     }
     /// 16th test. Change the precision of one char field
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('secondname');
         $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, null);
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_precision($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field precision (char)'] = $test;
     }
     /// 17th test. Change the precision of one numeric field
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('grade');
         $field->setAttributes(XMLDB_TYPE_NUMBER, '10,2', null, null, null, null, null, null);
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_precision($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field precision (number)'] = $test;
     }
     /// 18th test. Change the precision of one integer field to a smaller one
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('course');
         $field->setAttributes(XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_precision($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field precision (integer) to smaller one'] = $test;
     }
     /// 19th test. Change the sign of one numeric field to unsigned
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('grade');
         $field->setAttributes(XMLDB_TYPE_NUMBER, '10,2', XMLDB_UNSIGNED, null, null, null, null, null);
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_unsigned($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field sign (unsigned)'] = $test;
     }
     /// 20th test. Change the sign of one numeric field to signed
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('grade');
         $field->setAttributes(XMLDB_TYPE_NUMBER, '10,2', null, null, null, null, null, null);
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_unsigned($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field sign (signed)'] = $test;
     }
     /// 21th test. Change the nullability of one char field to not null
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('name');
         $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'Moodle');
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_notnull($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field nullability (not null)'] = $test;
     }
     /// 22th test. Change the nullability of one char field to null
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('name');
         $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle');
         $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_notnull($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['change field nullability (null)'] = $test;
     }
     /// 23th test. Dropping the default of one field
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('name');
         $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null);
         $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_default($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['drop field default of NULL field'] = $test;
     }
     /// 24th test. Creating the default for one field
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('name');
         $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle');
         $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_default($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add field default of NULL field'] = $test;
     }
     /// 25th test. Creating the default for one field
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('secondname');
         $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, 'Moodle2');
         $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_default($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add field default of NOT NULL field'] = $test;
     }
     /// 26th test. Dropping the default of one NOT NULL field
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('secondname');
         $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, null);
         $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_default($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['drop field default of NOT NULL field'] = $test;
     }
     /// 27th test. Adding one unique index to the table
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $index = new XMLDBIndex('secondname');
         $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'secondname', 'grade'));
         $test->sql = $table->getAddIndexSQL($CFG->dbtype, $CFG->prefix, $index, true);
         $test->status = add_index($table, $index, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add unique index'] = $test;
     }
     /// 28th test. Adding one not unique index to the table
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $index = new XMLDBIndex('secondname');
         $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name'));
         $test->sql = $table->getAddIndexSQL($CFG->dbtype, $CFG->prefix, $index, true);
         $test->status = add_index($table, $index, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add not unique index'] = $test;
     }
     /// 29th test. Re-add the same index than previous test. Check find_index_name() works.
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $index = new XMLDBIndex('secondname');
         $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('name', 'course'));
         if ($indexfound = find_index_name($table, $index)) {
             $test->status = true;
             $test->sql = array();
         } else {
             $test->status = true;
             $test->error = 'Index not found!';
             $test->sql = array();
         }
         $tests['check find_index_name()'] = $test;
     }
     /// 30th test. Dropping one index from the table
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $index = new XMLDBIndex('name');
         $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'grade', 'secondname'));
         $test->sql = $table->getDropIndexSQL($CFG->dbtype, $CFG->prefix, $index, true);
         $test->status = drop_index($table, $index, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['drop index'] = $test;
     }
     /// 31th test. Adding one unique key to the table
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $key = new XMLDBKey('id-course-grade');
         $key->setAttributes(XMLDB_KEY_UNIQUE, array('id', 'course', 'grade'));
         $test->sql = $table->getAddKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
         $test->status = add_key($table, $key, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add unique key'] = $test;
     }
     /// 32th test. Adding one foreign+unique key to the table
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $key = new XMLDBKey('course');
         $key->setAttributes(XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'anothertest', array('id'));
         $test->sql = $table->getAddKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
         $test->status = add_key($table, $key, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add foreign+unique key'] = $test;
     }
     /// 33th test. Drop one key
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $key = new XMLDBKey('course');
         $key->setAttributes(XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'anothertest', array('id'));
         $test->sql = $table->getDropKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
         $test->status = drop_key($table, $key, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['drop foreign+unique key'] = $test;
     }
     /// 34th test. Adding one foreign key to the table
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $key = new XMLDBKey('course');
         $key->setAttributes(XMLDB_KEY_FOREIGN, array('course'), 'anothertest', array('id'));
         $test->sql = $table->getAddKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
         $test->status = add_key($table, $key, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add foreign key'] = $test;
     }
     /// 35th test. Drop one foreign key
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $key = new XMLDBKey('course');
         $key->setAttributes(XMLDB_KEY_FOREIGN, array('course'), 'anothertest', array('id'));
         $test->sql = $table->getDropKeySQL($CFG->dbtype, $CFG->prefix, $key, true);
         $test->status = drop_key($table, $key, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['drop foreign key'] = $test;
     }
     /// 36th test. Adding one complex enum field
     if ($test->status) {
         /// Create a new field with complex specs (enums are good candidates)
         $field = new XMLDBField('type');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
         /// Get SQL code and execute it
         $test = new stdClass();
         $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = add_field($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add field with enum'] = $test;
     }
     /// 37th test. Dropping the enum of one field
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('type');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'general', 'course');
         $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_enum($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['delete enumlist from one field'] = $test;
     }
     /// 38th test. Creating the enum for one field
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('type');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
         $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_enum($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add enumlist to one field'] = $test;
     }
     /// 39th test. Renaming one index
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $index = new XMLDBIndex('anyname');
         $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'course'));
         $test->sql = $table->getRenameIndexSQL($CFG->dbtype, $CFG->prefix, $index, 'newnamefortheindex', true);
         $test->status = rename_index($table, $index, 'newnamefortheindex', false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['rename index (experimental. DO NOT USE IT)'] = $test;
     }
     /// 40th test. Renaming one key
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $key = new XMLDBKey('anyname');
         $key->setAttributes(XMLDB_KEY_UNIQUE, array('id', 'course', 'grade'));
         $test->sql = $table->getRenameKeySQL($CFG->dbtype, $CFG->prefix, $key, 'newnameforthekey', true);
         $test->status = rename_key($table, $key, 'newnameforthekey', false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['rename key (experimental. DO NOT USE IT)'] = $test;
     }
     /// 41th test. Renaming one field
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $field = new XMLDBField('type');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
         $test->sql = $table->getRenameFieldSQL($CFG->dbtype, $CFG->prefix, $field, 'newnameforthefield', true);
         $test->status = rename_field($table, $field, 'newnameforthefield', false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['rename field'] = $test;
     }
     /// 42th test. Renaming one table
     if ($test->status) {
         /// Get SQL code and execute it
         $test = new stdClass();
         $test->sql = $table->getRenameTableSQL($CFG->dbtype, $CFG->prefix, 'newnameforthetable', true);
         $test->status = rename_table($table, 'newnameforthetable', false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['rename table'] = $test;
     }
     /// 43th test. Add enum to field containing enum
     if ($test->status) {
         /// Add enum to field containing enum
         $table->setName('newnameforthetable');
         $field = new XMLDBField('newnameforthefield');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
         /// Get SQL code and execute it
         $test = new stdClass();
         $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_enum($table, $field, false, false);
         /// Let's see if the constraint exists to alter results
         if (check_constraint_exists($table, $field)) {
             $test->sql = array('Nothing executed. Enum already exists. Correct.');
         } else {
             $test->status = false;
         }
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['add enum to field containing enum'] = $test;
     }
     /// 44th test. Drop enum from field containing enum
     if ($test->status) {
         /// Drop enum from field containing enum
         $table->setName('newnameforthetable');
         $field = new XMLDBField('newnameforthefield');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'general', 'course');
         /// Get SQL code and execute it
         $test = new stdClass();
         $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_enum($table, $field, false, false);
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['drop enum from field containing enum'] = $test;
     }
     /// 45th test. Drop enum from field not containing enum
     if ($test->status) {
         /// Drop enum from field not containing enum
         $table->setName('newnameforthetable');
         $field = new XMLDBField('newnameforthefield');
         $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'general', 'course');
         /// Get SQL code and execute it
         $test = new stdClass();
         $test->sql = $table->getModifyEnumSQL($CFG->dbtype, $CFG->prefix, $field, true);
         $test->status = change_field_enum($table, $field, false, false);
         /// Let's see if the constraint exists to alter results
         if (!check_constraint_exists($table, $field)) {
             $test->sql = array('Nothing executed. Enum does not exists. Correct.');
         } else {
             $test->status = false;
         }
         if (!$test->status) {
             $test->error = $db->ErrorMsg();
         }
         $tests['drop enum from field not containing enum'] = $test;
     }
     /// 46th test. Getting the PK sequence name for one table
     if ($test->status) {
         $test = new stdClass();
         $test->sql = array(find_sequence_name($table));
         $test->status = find_sequence_name($table);
         if (!$test->status) {
             if (!($test->error = $db->ErrorMsg())) {
                 //If no db errors, result is ok. Just the driver doesn't support this
                 $test->sql = array('Not needed for this DB. Correct.');
                 $test->status = true;
             }
         }
         $tests['find sequence name'] = $test;
     }
     /// 47th test. Inserting TEXT contents
     $textlib = textlib_get_instance();
     if ($test->status) {
         $test = new stdClass();
         $test->status = false;
         $test->sql = array();
         $basetext = "\\ ''語 • Русский • Deutsch • English • Español • Français • Italiano • Nederlands • Polski • Português • Svenska • العربية • فارسی 한국어 • עברית • ไทย中文  Ελληνικά • Български • Српски • Українська • Bosanski • Català • Česky • Dansk • Eesti • Simple English • Esperanto • Euskara • Galego • Hrvatski • Ido • Bahasa Indonesia • Íslenska • Lëtzebuergesch • Lietuvių • Magyar • Bahasa Melayu اردو • ئۇيغۇرچه • हिन्दी • नेपाल भाषा मराठी • தமிழ் Հայերեն • Беларуская • Чăваш • Ирон æвзаг • Македонски • Сибирской говор • Afrikaans • Aragonés • Arpitan • Asturianu • Kreyòl Ayisyen • Azərbaycan • Bân-lâm-gú • Basa Banyumasan • Brezhoneg • Corsu • Cymraeg • Deitsch • Føroyskt • Frysk • Furlan • Gaeilge • Gàidhlig • Ilokano • Interlingua • Basa Jawa • Kapampangan • Kernewek • Kurdî  كوردی • Ladino  לאדינו • Latina • Latviešu • Limburgs • Lumbaart • Nedersaksisch • Nouormand • Occitan • O‘zbek • Piemontèis • Plattdüütsch • Ripoarisch • Sámegiella • Scots • Shqip • Sicilianu • Sinugboanon • Srpskohrvatski / Српскохрватски • Basa Sunda • Kiswahili • Tagalog • Tatarça • Walon • Winaray  Авар • Башҡорт • Кыргызча  Монгол • Қазақша • Тоҷикӣ • Удмурт • Armãneashce • Bamanankan • Eald Englisc • Gaelg • Interlingue • Kaszëbsczi • Kongo • Ligure • Lingála • lojban • Malagasy • Malti • Māori • Nāhuatl • Ekakairũ Naoero • Novial • Pangasinán • Tok Pisin • Romani / रोमानी • Rumantsch • Runa Simi • Sardu • Tetun • Türkmen / تركمن / Туркмен • Vèneto • Volapük • Võro • West-Vlaoms • Wollof • Zazaki • Žemaitėška";
         /// Create one big text (1.500.000 chars)
         $fulltext = '';
         for ($i = 0; $i < 1000; $i++) {
             //1500 * 1000 chars
             $fulltext .= $basetext;
         }
         /// Build the record to insert
         $rec->intro = addslashes($fulltext);
         $rec->name = 'texttest';
         /// Calculate its length
         $textlen = $textlib->strlen($fulltext);
         if ($rec->id = insert_record('newnameforthetable', $rec)) {
             if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
                 delete_records('newnameforthetable', 'id', $new->id);
                 $newtextlen = $textlib->strlen($new->intro);
                 if ($fulltext === $new->intro) {
                     $test->sql = array($newtextlen . ' cc. (text) sent and received ok');
                     $test->status = true;
                 } else {
                     $test->error = $db->ErrorMsg();
                     $test->sql = array($newtextlen . ' cc. (text) transfer failed. Data changed!');
                     print_object($new);
                     $test->status = false;
                 }
             } else {
                 $test->error = $db->ErrorMsg();
             }
         } else {
             $test->error = $db->ErrorMsg();
         }
         $tests['insert record ' . $textlen . ' cc. (text)'] = $test;
     }
     /// 48th test. Inserting BINARY contents
     if ($test->status) {
         $test = new stdClass();
         $test->status = false;
         /// Build the record to insert
         $rec->avatar = addslashes($fulltext);
         $rec->name = 'binarytest';
         /// Calculate its length
         $textlen = strlen($fulltext);
         if ($rec->id = insert_record('newnameforthetable', $rec)) {
             if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
                 $newtextlen = strlen($new->avatar);
                 if ($fulltext === $new->avatar) {
                     $test->sql = array($newtextlen . ' bytes (binary) sent and received ok');
                     $test->status = true;
                 } else {
                     $test->error = $db->ErrorMsg();
                     $test->sql = array($newtextlen . ' bytes (binary) transfer failed. Data changed!');
                     $test->status = false;
                 }
             } else {
                 $test->error = $db->ErrorMsg();
             }
         } else {
             $test->error = $db->ErrorMsg();
         }
         $tests['insert record ' . $textlen . ' bytes (binary)'] = $test;
     }
     /// 49th test. update_record with TEXT and BINARY contents
     if ($test->status) {
         $test = new stdClass();
         $test->status = false;
         $test->sql = array();
         /// Build the record to insert
         $rec->intro = addslashes($basetext);
         $rec->avatar = addslashes($basetext);
         $rec->name = 'updatelobs';
         /// Calculate its length
         $textlen = $textlib->strlen($basetext);
         $imglen = strlen($basetext);
         if (update_record('newnameforthetable', $rec)) {
             if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
                 $newtextlen = $textlib->strlen($new->intro);
                 $newimglen = strlen($new->avatar);
                 if ($basetext === $new->avatar && $basetext === $new->intro) {
                     $test->sql = array($newtextlen . ' cc. (text) sent and received ok', $newimglen . ' bytes (binary) sent and received ok');
                     $test->status = true;
                 } else {
                     if ($rec->avatar !== $new->avatar) {
                         $test->error = $db->ErrorMsg();
                         $test->sql = array($newimglen . ' bytes (binary) transfer failed. Data changed!');
                         $test->status = false;
                     } else {
                         $test->error = $db->ErrorMsg();
                         $test->sql = array($newtextlen . ' cc. (text) transfer failed. Data changed!');
                         $test->status = false;
                     }
                 }
             } else {
                 $test->error = $db->ErrorMsg();
             }
         } else {
             $test->error = $db->ErrorMsg();
         }
         $tests['update record ' . $textlen . ' cc. (text) and ' . $imglen . ' bytes (binary)'] = $test;
     }
     /// 50th test. set_field with TEXT contents
     if ($test->status) {
         $test = new stdClass();
         $test->status = false;
         $test->sql = array();
         /// Build the record to insert
         $rec->intro = addslashes($fulltext);
         $rec->name = 'updatelobs';
         /// Calculate its length
         $textlen = $textlib->strlen($fulltext);
         if (set_field('newnameforthetable', 'intro', $rec->intro, 'name', $rec->name)) {
             if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
                 $newtextlen = $textlib->strlen($new->intro);
                 if ($fulltext === $new->intro) {
                     $test->sql = array($newtextlen . ' cc. (text) sent and received ok');
                     $test->status = true;
                 } else {
                     $test->error = $db->ErrorMsg();
                     $test->sql = array($newtextlen . ' cc. (text) transfer failed. Data changed!');
                     $test->status = false;
                 }
             } else {
                 $test->error = $db->ErrorMsg();
             }
         } else {
             $test->error = $db->ErrorMsg();
         }
         $tests['set field ' . $textlen . ' cc. (text)'] = $test;
     }
     /// 51th test. set_field with BINARY contents
     if ($test->status) {
         $test = new stdClass();
         $test->status = false;
         $test->sql = array();
         /// Build the record to insert
         $rec->avatar = addslashes($fulltext);
         $rec->name = 'updatelobs';
         /// Calculate its length
         $textlen = strlen($fulltext);
         if (set_field('newnameforthetable', 'avatar', $rec->avatar, 'name', $rec->name)) {
             if ($new = get_record('newnameforthetable', 'id', $rec->id)) {
                 $newtextlen = strlen($new->avatar);
                 if ($fulltext === $new->avatar) {
                     $test->sql = array($newtextlen . ' bytes (binary) sent and received ok');
                     $test->status = true;
                 } else {
                     $test->error = $db->ErrorMsg();
                     $test->sql = array($newtextlen . ' bytes (binary) transfer failed. Data changed!');
                     $test->status = false;
                 }
             } else {
                 $test->error = $db->ErrorMsg();
             }
         } else {
             $test->error = $db->ErrorMsg();
         }
         $tests['set field ' . $textlen . ' bytes (binary)'] = $test;
     }
     /// TODO: Check here values of the inserted records to see that everything ha the correct value
     /// Iterate over tests, showing information as needed
     $o .= '<ol>';
     foreach ($tests as $key => $test) {
         $o .= '<li>' . $key . ($test->status ? '<font color="green"> Ok</font>' : ' <font color="red">Error</font>');
         if (!$test->status) {
             $o .= '<br/><font color="red">' . $test->error . '</font>';
         }
         $o .= '<pre>' . implode('<br/>', $test->sql) . '</pre>';
         $o .= '</li>';
     }
     $o .= '</ol>';
     $this->output = $o;
     /// Launch postaction if exists (leave this here!)
     if ($this->getPostAction() && $result) {
         return $this->launch($this->getPostAction());
     }
     /// Return ok if arrived here
     return $result;
 }
function questionnaire_upgrade_2007120101()
{
    $status = true;
    /// Shorten table names to bring them in accordance with the XML DB schema.
    $q_table = new XMLDBTable('questionnaire_question_choice');
    $status &= rename_table($q_table, 'questionnaire_quest_choice', false);
    unset($q_table);
    $q_table = new XMLDBTable('questionnaire_response_multiple');
    $status &= rename_table($q_table, 'questionnaire_resp_multiple', false);
    unset($q_table);
    $q_table = new XMLDBTable('questionnaire_response_single');
    $status &= rename_table($q_table, 'questionnaire_resp_single', false);
    unset($q_table);
    /// Upgrade the questionnaire_question_type table to use typeid.
    unset($table);
    unset($field);
    $table = new XMLDBTable('questionnaire_question_type');
    $field = new XMLDBField('typeid');
    $field->setAttributes(XMLDB_TYPE_CHAR, '20', true, true, false, false, null, '0', 'id');
    $status &= add_field($table, $field);
    if (($numrecs = count_records('questionnaire_question_type')) > 0) {
        $recstart = 0;
        $recstoget = 100;
        while ($recstart < $numrecs) {
            if ($records = get_records('questionnaire_question_type', '', '', '', '*', $recstart, $recstoget)) {
                foreach ($records as $record) {
                    $status &= set_field('questionnaire_question_type', 'typeid', $record->id, 'id', $record->id);
                }
            }
            $recstart += $recstoget;
        }
    }
    return $status;
}
function xmldb_attforblock_upgrade($oldversion = 0)
{
    global $CFG, $THEME, $db;
    $result = true;
    /// And upgrade begins here. For each one, you'll need one
    /// block of code similar to the next one. Please, delete
    /// this comment lines once this file start handling proper
    /// upgrade code.
    if ($result && $oldversion < 2008021904) {
        //New version in version.php
        global $USER;
        if ($sessions = get_records('attendance_sessions', 'takenby', 0)) {
            foreach ($sessions as $sess) {
                if (count_records('attendance_log', 'attsid', $sess->id) > 0) {
                    $sess->takenby = $USER->id;
                    $sess->timetaken = $sess->timemodified ? $sess->timemodified : time();
                    $sess->description = addslashes($sess->description);
                    $result = update_record('attendance_sessions', $sess) and $result;
                }
            }
        }
    }
    if ($oldversion < 2008102401 and $result) {
        $table = new XMLDBTable('attforblock');
        $field = new XMLDBField('grade');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '100', 'name');
        $result = $result && add_field($table, $field);
        $table = new XMLDBTable('attendance_sessions');
        $field = new XMLDBField('courseid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'id');
        $result = $result && change_field_unsigned($table, $field);
        //        $field = new XMLDBField('creator');
        //        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'courseid');
        //        $result = $result && change_field_unsigned($table, $field);
        $field = new XMLDBField('sessdate');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'creator');
        $result = $result && change_field_unsigned($table, $field);
        $field = new XMLDBField('duration');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'sessdate');
        $result = $result && add_field($table, $field);
        $field = new XMLDBField('timetaken');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'takenby');
        $result = $result && change_field_unsigned($table, $field);
        $result = $result && rename_field($table, $field, 'lasttaken');
        $field = new XMLDBField('takenby');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'lasttaken');
        $result = $result && change_field_unsigned($table, $field);
        $result = $result && rename_field($table, $field, 'lasttakenby');
        $field = new XMLDBField('timemodified');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'lasttaken');
        $result = $result && change_field_unsigned($table, $field);
        $table = new XMLDBTable('attendance_log');
        $field = new XMLDBField('attsid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'id');
        $result = $result && change_field_unsigned($table, $field);
        $field = new XMLDBField('studentid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'attsid');
        $result = $result && change_field_unsigned($table, $field);
        $field = new XMLDBField('statusid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'status');
        $result = $result && add_field($table, $field);
        $field = new XMLDBField('statusset');
        $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'statusid');
        $result = $result && add_field($table, $field);
        $field = new XMLDBField('timetaken');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'statusid');
        $result = $result && add_field($table, $field);
        $field = new XMLDBField('takenby');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'timetaken');
        $result = $result && add_field($table, $field);
        //Indexes
        $index = new XMLDBIndex('statusid');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('statusid'));
        $result = $result && add_index($table, $index);
        $index = new XMLDBIndex('attsid');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('attsid'));
        $result = $result && drop_index($table, $index);
        $field = new XMLDBField('attsid');
        //Rename field
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'id');
        $result = $result && rename_field($table, $field, 'sessionid');
        $index = new XMLDBIndex('sessionid');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('sessionid'));
        $result = $result && add_index($table, $index);
        $table = new XMLDBTable('attendance_settings');
        $field = new XMLDBField('courseid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'id');
        $result = $result && change_field_unsigned($table, $field);
        $field = new XMLDBField('visible');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'grade');
        $result = $result && add_field($table, $field);
        $field = new XMLDBField('deleted');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'visible');
        $result = $result && add_field($table, $field);
        //Indexes
        $index = new XMLDBIndex('visible');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('visible'));
        $result = $result && add_index($table, $index);
        $index = new XMLDBIndex('deleted');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('deleted'));
        $result = $result && add_index($table, $index);
        $result = $result && rename_table($table, 'attendance_statuses');
    }
    if ($oldversion < 2008102406 and $result) {
        if ($courses = get_records_sql("SELECT courseid FROM {$CFG->prefix}attendance_sessions GROUP BY courseid")) {
            foreach ($courses as $c) {
                //Adding own status for course (now it must have own)
                if (!count_records('attendance_statuses', 'courseid', $c->courseid)) {
                    $statuses = get_records('attendance_statuses', 'courseid', 0);
                    foreach ($statuses as $stat) {
                        $rec = $stat;
                        $rec->courseid = $c->courseid;
                        insert_record('attendance_statuses', $rec);
                    }
                }
                $statuses = get_records('attendance_statuses', 'courseid', $c->courseid);
                $statlist = implode(',', array_keys($statuses));
                $sess = get_records_select_menu('attendance_sessions', "courseid = {$c->courseid} AND lasttakenby > 0");
                $sesslist = implode(',', array_keys($sess));
                foreach ($statuses as $stat) {
                    execute_sql("UPDATE {$CFG->prefix}attendance_log\n\t\t\t\t\t\t\t\t\t\tSET statusid = {$stat->id}, statusset = '{$statlist}'\n\t\t\t\t\t\t\t\t\t  WHERE sessionid IN ({$sesslist}) AND status = '{$stat->status}'");
                }
                $sessions = get_records_list('attendance_sessions', 'id', $sesslist);
                foreach ($sessions as $sess) {
                    execute_sql("UPDATE {$CFG->prefix}attendance_log\n\t\t\t\t\t\t\t\t\t\tSET timetaken = {$sess->lasttaken}, \n\t\t\t\t\t\t\t\t\t\t\ttakenby = {$sess->lasttakenby}\n\t\t\t\t\t\t\t\t\t  WHERE sessionid = {$sess->id}");
                }
            }
        }
    }
    if ($oldversion < 2008102409 and $result) {
        $table = new XMLDBTable('attendance_statuses');
        $field = new XMLDBField('status');
        $result = $result && drop_field($table, $field);
        $index = new XMLDBIndex('status');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('status'));
        $result = $result && drop_index($table, $index);
        $table = new XMLDBTable('attendance_log');
        $field = new XMLDBField('status');
        $result = $result && drop_field($table, $field);
        $index = new XMLDBIndex('status');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('status'));
        $result = $result && drop_index($table, $index);
        $table = new XMLDBTable('attendance_sessions');
        $field = new XMLDBField('creator');
        $result = $result && drop_field($table, $field);
    }
    return $result;
}
Esempio n. 5
0
         //fwrite ($ct,$chain2);
     }
     rename_table("cadastre.b_depdgi", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.b_desdgi", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.b_exodgi", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.b_habdgi", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.b_prodgi", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.b_subdgi", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.b_taxdgi", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.batidgi", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.p_exoner", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.p_subdif", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.p_taxat", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.parcel", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.propriet", $_POST['ann_ref'], $pgx);
     rename_table("cadastre.voies", $_POST['ann_ref'], $pgx);
     //rename_table("cadastre.commune",$_POST['ann_ref'],$pgx);
 }
 //fclose($ctab);
 if (strrpos($_POST['rep_f'], "/") == strlen($_POST['rep_f']) - 1) {
     $_POST['rep_f'] = substr($_POST['rep_f'], 0, -1);
 } else {
     $_POST['rep_f'] = $_POST['rep_f'];
 }
 exec("perl /var/www/application/apps/cadastre/lit_cad.pl " . $_POST['rep_f'] . " " . $_POST['ext_f']);
 pg_exec($pgx, "copy cadastre.parcel from '" . $_POST['rep_f'] . "/parcelle.csv' with csv;");
 pg_exec($pgx, "copy cadastre.batidgi from '" . $_POST['rep_f'] . "/batidgi.csv' with csv;");
 pg_exec($pgx, "copy cadastre.b_depdgi from '" . $_POST['rep_f'] . "/bdepdgi.csv' with csv;");
 pg_exec($pgx, "copy cadastre.b_desdgi from '" . $_POST['rep_f'] . "/bdesdgi.csv' with csv;");
 pg_exec($pgx, "copy cadastre.b_exodgi from '" . $_POST['rep_f'] . "/bexodgi.csv' with csv;");
 pg_exec($pgx, "copy cadastre.b_taxdgi from '" . $_POST['rep_f'] . "/btaxdgi.csv' with csv;");
Esempio n. 6
0
function xmldb_block_search_upgrade($oldversion = 0)
{
    global $CFG, $THEME, $db;
    $result = true;
    /// And upgrade begins here. For each one, you'll need one
    /// block of code similar to the next one. Please, delete
    /// this comment lines once this file start handling proper
    /// upgrade code.
    /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
    ///     $result = result of "/lib/ddllib.php" function calls
    /// }
    if ($result && $oldversion < 2007071302) {
        /// Define table search_documents to be created
        $table = new XMLDBTable('search_documents');
        /// Drop it if it existed before
        drop_table($table, true, false);
        /// Adding fields to table search_documents
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('docid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('doctype', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, null, 'none');
        $table->addFieldInfo('itemtype', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, null, 'standard');
        $table->addFieldInfo('title', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
        $table->addFieldInfo('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
        $table->addFieldInfo('docdate', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null, null);
        $table->addFieldInfo('updated', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null, null);
        $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('groupid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        /// Adding keys to table search_documents
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table search_documents
        $table->addIndexInfo('mdl_search_docid', XMLDB_INDEX_NOTUNIQUE, array('docid'));
        $table->addIndexInfo('mdl_search_doctype', XMLDB_INDEX_NOTUNIQUE, array('doctype'));
        $table->addIndexInfo('mdl_search_itemtype', XMLDB_INDEX_NOTUNIQUE, array('itemtype'));
        /// Launch create table for search_documents
        $result = $result && create_table($table);
    }
    /// Rename table search_documents to block_search_documents and
    /// fix some defaults (MDL-10572)
    if ($result && $oldversion < 2007081100) {
        /// Define table search_documents to be renamed to block_search_documents
        $table = new XMLDBTable('search_documents');
        /// Launch rename table for block_search_documents
        $result = $result && rename_table($table, 'block_search_documents');
        /// Changing the default of field doctype on table block_search_documents to none
        $table = new XMLDBTable('block_search_documents');
        $field = new XMLDBField('doctype');
        $field->setAttributes(XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, null, 'none', 'docid');
        /// Launch change of default for field doctype
        $result = $result && change_field_default($table, $field);
        /// Changing the default of field itemtype on table block_search_documents to standard
        $table = new XMLDBTable('block_search_documents');
        $field = new XMLDBField('itemtype');
        $field->setAttributes(XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, null, 'standard', 'doctype');
        /// Launch change of default for field itemtype
        $result = $result && change_field_default($table, $field);
        /// Changing the default of field title on table block_search_documents to drop it
        $table = new XMLDBTable('block_search_documents');
        $field = new XMLDBField('title');
        $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'itemtype');
        /// Launch change of default for field title
        $result = $result && change_field_default($table, $field);
        /// Changing the default of field url on table block_search_documents to drop it
        $table = new XMLDBTable('block_search_documents');
        $field = new XMLDBField('url');
        $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'title');
        /// Launch change of default for field url
        $result = $result && change_field_default($table, $field);
    }
    return $result;
}
function xmldb_assignment_type_uploadpdf_upgrade($oldversion = 0)
{
    global $CFG, $THEME, $db;
    $result = true;
    if ($result && $oldversion < 2009041700) {
        $table = new XMLDBTable('assignment_uploadpdf');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('assignment', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('coversheet', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, '');
        $table->addFieldInfo('template', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('onlypdf', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, null, '1');
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('assignment', XMLDB_INDEX_UNIQUE, array('assignment'));
        $table->addIndexInfo('template', XMLDB_INDEX_NOTUNIQUE, array('template'));
        $result = $result && create_table($table);
        $table = new XMLDBTable('assignment_uploadpdf_template');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, '');
        $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
        $result = $result && create_table($table);
        $table = new XMLDBTable('assignment_uploadpdf_template_item');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('template', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('type', XMLDB_TYPE_CHAR, '15', null, XMLDB_NOTNULL, null, null, null, 'shorttext');
        $table->addFieldInfo('xpos', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('ypos', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('width', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, '0');
        $table->addFieldInfo('setting', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, '');
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('template', XMLDB_INDEX_NOTUNIQUE, array('template'));
        $result = $result && create_table($table);
        $table = new XMLDBTable('assignment_uploadpdf_comment');
        $field = new XMLDBField('colour');
        $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, null, null, null, null, 'yellow', null);
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009111800) {
        $table = new XMLDBTable('assignment_uploadpdf_quicklist');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('text', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, '');
        $table->addFieldInfo('width', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('colour', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, 'yellow');
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2009112800) {
        $table = new XMLDBTable('assignment_uploadpdf_annotation');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('assignment_submission', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('startx', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('starty', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('endx', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('endy', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('pageno', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('colour', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, 'red');
        $table->addFieldInfo('type', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, 'line');
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('assignment_submission', XMLDB_INDEX_NOTUNIQUE, array('assignment_submission'));
        $table->addIndexInfo('assignment_submission_pageno', XMLDB_INDEX_NOTUNIQUE, array('assignment_submission', 'pageno'));
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2009120100) {
        // Rename the tables to fit within Oracle's 30 char limits (including 2 char prefix)
        $table = new XMLDBTable('assignment_uploadpdf_template');
        $result = $result && rename_table($table, 'assignment_uploadpdf_tmpl');
        $table = new XMLDBTable('assignment_uploadpdf_template_item');
        $result = $result && rename_table($table, 'assignment_uploadpdf_tmplitm');
        $table = new XMLDBTable('assignment_uploadpdf_quicklist');
        $result = $result && rename_table($table, 'assignment_uploadpdf_qcklist');
        $table = new XMLDBTable('assignment_uploadpdf_annotation');
        $result = $result && rename_table($table, 'assignment_uploadpdf_annot');
        // Change the data type of the text field from 'char' to 'text' (removing 255 char limit)
        $table = new XMLDBTable('assignment_uploadpdf_qcklist');
        $field = new XMLDBField('text');
        $field->setAttributes(XMLDB_TYPE_TEXT . 'medium', null, null, null, null, null, '');
        $result = $result && change_field_type($table, $field);
        // Remove 255 char limit from comments
        $table = new XMLDBTable('assignment_uploadpdf_comment');
        $field = new XMLDBField('rawtext');
        $field->setAttributes(XMLDB_TYPE_TEXT . 'medium', null, null, null, null, null, '');
        $result = $result && change_field_type($table, $field);
        // Remove 255 char limit from coversheet path
        $table = new XMLDBTable('assignment_uploadpdf');
        $field = new XMLDBField('coversheet');
        $field->setAttributes(XMLDB_TYPE_TEXT . 'medium', null, null, null, null, null, '');
        $result = $result && change_field_type($table, $field);
    }
    if ($result && $oldversion < 2010031300) {
        // Add new fields to allow linking with the checklist module
        $table = new XMLDBTable('assignment_uploadpdf');
        $field = new XMLDBField('checklist');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'onlypdf');
        $result = $result && add_field($table, $field);
        $field = new XMLDBField('checklist_percent');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'checklist');
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2011040400) {
        $table = new XMLDBTable('assignment_uploadpdf_annot');
        $field = new XMLDBField('path');
        $field->setAttributes(XMLDB_TYPE_TEXT . 'medium', null, null, null, null, null, 'endy');
        $result = $result && add_field($table, $field);
    }
    return $result;
}
Esempio n. 8
0
/**
 * ELIS(TM): Enterprise Learning Intelligence Suite
 * Copyright (C) 2008-2012 Remote Learner.net Inc http://www.remote-learner.net
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    elis
 * @subpackage component
 * @author     Remote-Learner.net Inc
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2008-2012 Remote Learner.net Inc http://www.remote-learner.net
 *
 */
function xmldb_crlm_user_activity_upgrade($oldversion = 0)
{
    global $CFG, $THEME, $db;
    $result = true;
    if ($result && $oldversion < 2010071502) {
        /// Define table etl_user_module_activity to be created
        $table = new XMLDBTable('etl_user_module_activity');
        /// Adding fields to table etl_user_module_activity
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('cmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('hour', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('duration', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table etl_user_module_activity
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table etl_user_module_activity
        $table->addIndexInfo('user_cmid_hour_idx', XMLDB_INDEX_UNIQUE, array('userid', 'cmid', 'hour'));
        $table->addIndexInfo('cm_idx', XMLDB_INDEX_NOTUNIQUE, array('cmid'));
        $table->addIndexInfo('hour_idx', XMLDB_INDEX_NOTUNIQUE, array('hour'));
        /// Launch create table for etl_user_module_activity
        if (!table_exists($table)) {
            $result = $result && create_table($table);
        }
    }
    if ($result && $oldversion < 2010071503) {
        // clear all records -- they may be incorrect, and we'll recalculate
        // them all
        delete_records_select('etl_user_activity', 'TRUE');
        delete_records('crlm_config', 'name', 'user_activity_last_run');
        /// Define index user_idx (not unique) to be dropped form etl_user_activity
        $table = new XMLDBTable('etl_user_activity');
        $index = new XMLDBIndex('user_idx');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('userid'));
        /// Launch drop index user_idx
        $result = $result && drop_index($table, $index);
        /// Define index user_idx (not unique) to be added to etl_user_activity
        $table = new XMLDBTable('etl_user_activity');
        $index = new XMLDBIndex('user_idx');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('userid', 'courseid', 'hour'));
        /// Launch add index user_idx
        $result = $result && add_index($table, $index);
    }
    if ($result && $oldversion < 2010071505) {
        $sql = "SELECT userid, courseid, hour, COUNT('x') count\n                  FROM {$CFG->prefix}etl_user_activity\n                 GROUP BY userid, courseid, hour\n                HAVING COUNT('x') > 1";
        if (record_exists_sql($sql)) {
            $table = new XMLDBTable('etl_user_activity_temp');
            // fields
            $field = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE);
            $field = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'id');
            $field = $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'userid');
            $field = $table->addFieldInfo('hour', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'courseid');
            $field = $table->addFieldInfo('duration', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'hour');
            // Keys & indexes
            $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
            $table->addIndexInfo('user_idx', XMLDB_INDEX_UNIQUE, array('userid', 'courseid', 'hour'));
            $table->addIndexInfo('course_idx', XMLDB_INDEX_NOTUNIQUE, array('courseid'));
            $table->addIndexInfo('hour_idx', XMLDB_INDEX_NOTUNIQUE, array('hour'));
            $result = $result && create_table($table);
            if ($result) {
                $sql = "INSERT INTO {$CFG->prefix}etl_user_activity_temp (userid, courseid, hour, duration)\n                        SELECT userid, courseid, hour, duration\n                          FROM {$CFG->prefix}etl_user_activity\n                      GROUP BY userid, courseid, hour\n                      ORDER BY id ASC";
                $result = $result && execute_sql($sql);
                if ($result) {
                    $oldtable = new XMLDBTable('etl_user_activity');
                    $result = $result && drop_table($oldtable);
                    if ($result) {
                        $result = $result && rename_table($table, 'etl_user_activity');
                    }
                }
            }
        }
        if ($result) {
            $sql = "UPDATE {$CFG->prefix}etl_user_activity SET duration = 3600 WHERE duration > 3600";
            execute_sql($sql);
        }
    }
    return $result;
}
function xmldb_studynotes_upgrade($oldversion = 0)
{
    global $CFG, $THEME, $db;
    $result = true;
    if ($result && $oldversion < 2009032400) {
        error("Version too old to be upgraded. Please delete the module and re-install it.");
    }
    if ($result && $oldversion < 2009041603) {
        /// Define table studynotes_uploads to be created
        $table = new XMLDBTable('studynotes_uploads');
        /// Adding fields to table studynotes_uploads
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('type', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('user_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('topic_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('created', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('modified', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null, null, '0');
        /// Adding keys to table studynotes_uploads
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for studynotes_uploads
        $result = $result && create_table($table);
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_cards');
        $field = new XMLDBField('user');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        /// Launch rename field
        $result = $result && rename_field($table, $field, 'user_id');
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_cards');
        $field = new XMLDBField('index');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        if ($CFG->dbfamily == "mysql" || $CFG->dbfamily == "mysqli") {
            if (field_exists($table, $field)) {
                $query = "ALTER TABLE {$CFG->prefix}studynotes_cards CHANGE `index` index_num BIGINT( 11 ) UNSIGNED NOT NULL";
                $db->Execute($query);
            }
        } else {
            /// Launch rename field
            $result = $result && rename_field($table, $field, 'index_num');
        }
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_cards');
        $field = new XMLDBField('level');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        /// Launch rename field
        $result = $result && rename_field($table, $field, 'level_num');
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_flashcards');
        $field = new XMLDBField('user');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        /// Launch rename field
        $result = $result && rename_field($table, $field, 'user_id');
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_flashcards');
        $field = new XMLDBField('number');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        /// Launch rename field
        $result = $result && rename_field($table, $field, 'num');
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_flashcards');
        $field = new XMLDBField('level');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        /// Launch rename field
        $result = $result && rename_field($table, $field, 'level_num');
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_groups');
        $field = new XMLDBField('access');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        /// Launch rename field
        $result = $result && rename_field($table, $field, 'access_num');
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_markers');
        $field = new XMLDBField('user');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        /// Launch rename field
        $result = $result && rename_field($table, $field, 'user_id');
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_markers');
        $field = new XMLDBField('range');
        $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
        if ($CFG->dbfamily == "mysql" || $CFG->dbfamily == "mysqli") {
            if (field_exists($table, $field)) {
                $query = "ALTER TABLE {$CFG->prefix}studynotes_markers CHANGE `range` range_store TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL";
                $db->Execute($query);
            }
        } else {
            /// Launch rename field
            $result = $result && rename_field($table, $field, 'range_store');
        }
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_memberships');
        $field = new XMLDBField('user');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        /// Launch rename field
        $result = $result && rename_field($table, $field, 'user_id');
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_memberships');
        $field = new XMLDBField('group');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        if ($CFG->dbfamily == "mysql" || $CFG->dbfamily == "mysqli") {
            if (field_exists($table, $field)) {
                $query = "ALTER TABLE {$CFG->prefix}studynotes_memberships CHANGE `group` group_id BIGINT( 11 ) UNSIGNED NOT NULL";
                $db->Execute($query);
            }
        } else {
            /// Launch rename field
            $result = $result && rename_field($table, $field, 'group_id');
        }
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_memberships');
        $field = new XMLDBField('level');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        /// Launch rename field
        $result = $result && rename_field($table, $field, 'level_num');
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_rights');
        $field = new XMLDBField('group');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        if ($CFG->dbfamily == "mysql" || $CFG->dbfamily == "mysqli") {
            if (field_exists($table, $field)) {
                $query = "ALTER TABLE {$CFG->prefix}studynotes_rights CHANGE `group` group_id BIGINT( 11 ) UNSIGNED NOT NULL";
                $db->Execute($query);
            }
        } else {
            /// Launch rename field
            $result = $result && rename_field($table, $field, 'group_id');
        }
    }
    /// Rename field
    if ($result && $oldversion < 2009042400) {
        $table = new XMLDBTable('studynotes_topics');
        $field = new XMLDBField('user');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, null, null, null, null, '0', null);
        /// Launch rename field
        $result = $result && rename_field($table, $field, 'user_id');
    }
    // Rename tables longer than 24 chars
    if ($result && $oldversion < 2009043001) {
        /// Define table studynotes_feed_msg_stat to be renamed
        $table = new XMLDBTable('studynotes_feed_messages_status');
        /// Launch rename table for studynotes_feed_msg_stat
        $result = $result && rename_table($table, 'studynotes_feed_msg_stat');
    }
    // Rename tables longer than 24 chars
    if ($result && $oldversion < 2009043001) {
        /// Define table studynotes_feed_subscrib to be renamed
        $table = new XMLDBTable('studynotes_feed_subscriptions');
        /// Launch rename table for studynotes_feed_subscrib
        $result = $result && rename_table($table, 'studynotes_feed_subscrib');
    }
    // Rename tables longer than 24 chars
    if ($result && $oldversion < 2009043001) {
        /// Define table studynotes_rel_questions to be renamed
        $table = new XMLDBTable('studynotes_relation_questions');
        /// Launch rename table for studynotes_rel_questions
        $result = $result && rename_table($table, 'studynotes_rel_questions');
    }
    // Rename tables longer than 24 chars
    if ($result && $oldversion < 2009043001) {
        /// Define table studynotes_rel_questions to be renamed
        $table = new XMLDBTable('studynotes_relation_translations');
        /// Launch rename table for studynotes_rel_questions
        $result = $result && rename_table($table, 'studynotes_rel_translations');
    }
    if ($result && $oldversion < 2009050301) {
        $fields = array(array('studynotes_cards', 'created'), array('studynotes_cards', 'modified'), array('studynotes_cards', 'locked_time'), array('studynotes_feed_messages', 'created'), array('studynotes_feed_messages', 'modified'), array('studynotes_feed_msg_stat', 'created'), array('studynotes_feed_msg_stat', 'modified'), array('studynotes_feeds', 'created'), array('studynotes_feeds', 'modified'), array('studynotes_groups', 'created'), array('studynotes_groups', 'modified'), array('studynotes_markers', 'created'), array('studynotes_markers', 'modified'), array('studynotes_memberships', 'created'), array('studynotes_memberships', 'modified'), array('studynotes_relations', 'created'), array('studynotes_relations', 'modified'), array('studynotes_relation_answers', 'created'), array('studynotes_relation_answers', 'modified'), array('studynotes_relation_links', 'created'), array('studynotes_relation_links', 'modified'), array('studynotes_rel_questions', 'created'), array('studynotes_rel_questions', 'modified'), array('studynotes_topics', 'created'), array('studynotes_topics', 'modified'), array('studynotes_uploads', 'created'), array('studynotes_uploads', 'modified'), array('studynotes_users', 'created'), array('studynotes_users', 'last_login'));
        foreach ($fields as $info) {
            $table = new XMLDBTable($info[0]);
            $tmpField = new XMLDBField($info[1] . "_cpy");
            $tmpField->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', $info[1]);
            //add new integer field
            $result = $result && add_field($table, $tmpField);
            //get value
            if ($records = get_records($info[0], '', '', '', 'id,' . $info[1])) {
                //convert value
                foreach ($records as $record) {
                    $record->{$info[1] . "_cpy"} = strtotime($record->{$info[1]});
                    unset($record->{$info[1]});
                    print_r($record);
                    $result = $result && update_record($info[0], $record);
                }
            }
            //drop old field
            $field = new XMLDBField($info[1]);
            $result = $result && drop_field($table, $field);
            //rename copy
            $tmpField->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', null);
            $result = $result && change_field_default($table, $tmpField);
            $result = $result && rename_field($table, $tmpField, $info[1]);
        }
    }
    if ($result && $oldversion < 2009050301) {
        /// Define table studynotes_rel_translations to be dropped
        $table = new XMLDBTable('studynotes_rel_translations');
        /// Launch drop table for studynotes_rel_translations
        $result = $result && drop_table($table);
    }
    if ($result && $oldversion < 2009070300) {
        /// Define index link (not unique) to be dropped form studynotes_relation_links
        $table = new XMLDBTable('studynotes_relation_links');
        $index = new XMLDBIndex('link');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('link'));
        /// Launch drop index link
        $result = $result && drop_index($table, $index);
    }
    if ($result && $oldversion < 2009070300) {
        /// Changing type of field link on table studynotes_relation_links to text
        $table = new XMLDBTable('studynotes_relation_links');
        $field = new XMLDBField('link');
        $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null, 'id');
        /// Launch change of type for field link
        $result = $result && change_field_type($table, $field);
    }
    if ($result && $oldversion < 2009070300) {
        /// Changing type of field answer on table studynotes_relation_answers to text
        $table = new XMLDBTable('studynotes_relation_answers');
        $field = new XMLDBField('answer');
        $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, 'id');
        /// Launch change of type for field answer
        $result = $result && change_field_type($table, $field);
    }
    if ($result && $oldversion < 2009070300) {
        /// Changing type of field question on table studynotes_rel_questions to text
        $table = new XMLDBTable('studynotes_rel_questions');
        $field = new XMLDBField('question');
        $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null, 'id');
        /// Launch change of type for field question
        $result = $result && change_field_type($table, $field);
    }
    return $result;
}
function xmldb_game_upgrade($oldversion = 0)
{
    global $CFG, $THEME, $db;
    $result = true;
    //game.questioncategoryid
    if ($result && $oldversion < 2007082802) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('questioncategoryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'glossarycategoryid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_hangman.quizid
    if ($result && $oldversion < 2007082802) {
        /// Define field format to be added to data_comments
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('quizid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'questionid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_hangman.glossaryid
    if ($result && $oldversion < 2007082803) {
        /// Define field format to be added to data_comments
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('glossaryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'quizid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_hangman.glossarycategoryid
    if ($result && $oldversion < 2007082803) {
        /// Define field format to be added to data_comments
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('glossarycategoryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'glossaryid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_hangman.questioncategoryid
    if ($result && $oldversion < 2007082803) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('questioncategoryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'glossarycategoryid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_millionaire.questioncategoryid
    if ($result && $oldversion < 2007082804) {
        $table = new XMLDBTable('game_millionaire');
        $field = new XMLDBField('questioncategoryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'quizid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_hangman.try
    if ($result && $oldversion < 2007082805) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('try');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'answer');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_hangman.maxtries
    if ($result && $oldversion < 2007082805) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('maxtries');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'try');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_hangman.finishedword
    if ($result && $oldversion < 2007082807) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('finishedword');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'maxtries');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_hangman.corrects
    if ($result && $oldversion < 2007082807) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('corrects');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'finishedword');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.param7
    if ($result && $oldversion < 2007082808) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('param7');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'param6');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_hangman.letters : change to char( 30)
    if ($result && $oldversion < 2007082809) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('letters');
        $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null);
        /// Launch change of precision for field lang
        $result = $result && change_field_precision($table, $field);
    }
    //gamg_hangman.glossaryid
    if ($result && $oldversion < 2007082901) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('glossaryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'quizid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_instances.lastip : change to char( 30)
    if ($result && $oldversion < 2007083002) {
        $table = new XMLDBTable('game_instances');
        $field = new XMLDBField('lastip');
        $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null, '', 'grade');
        /// Launch change of precision for field lang
        $result = $result && add_field($table, $field);
    }
    //game_bookquiz_questions.glossarycategoryid
    if ($result && $oldversion < 2007091001) {
        $table = new XMLDBTable('game_bookquiz_questions');
        $field = new XMLDBField('questioncategoryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //new table game_bookquiz_chapters
    if ($result && $oldversion < 2007091701) {
        /// Define table scorm_scoes_data to be created
        $table = new XMLDBTable('game_bookquiz_chapters');
        /// Adding fields to table scorm_scoes_data
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('gameinstanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('chapterid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        /// Adding keys to table scorm_scoes_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table scorm_scoes_data
        $table->addIndexInfo('gameinstanceidchapterid', XMLDB_INDEX_NOTUNIQUE, array('gameinstanceid', 'chapterid'));
        /// Launch create table for scorm_scoes_data
        $result = $result && create_table($table);
    }
    //new table game_snakes_database
    if ($result && $oldversion < 2007092207) {
        /// Define table scorm_scoes_data to be created
        $table = new XMLDBTable('game_snakes_database');
        /// Adding fields to table scorm_scoes_data
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, '');
        $table->addFieldInfo('cols', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('rows', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('data', XMLDB_TYPE_TEXT, '0', null, XMLDB_NOTNULL, null, null, null, '');
        $table->addFieldInfo('file', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, '');
        $table->addFieldInfo('direction', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('headerx', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('headery', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('footerx', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('footery', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        /// Adding keys to table scorm_scoes_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for scorm_scoes_data
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2007092208) {
        /// Define table scorm_scoes_data to be created
        $table = new XMLDBTable('game_snakes');
        /// Adding fields to table scorm_scoes_data
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('snakesdatabaseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('position', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        /// Adding keys to table scorm_scoes_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for scorm_scoes_data
        $result = $result && create_table($table);
    }
    //game_snakes_database.width
    if ($result && $oldversion < 2007092301) {
        $table = new XMLDBTable('game_snakes_database');
        $field = new XMLDBField('width');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_snakes_database.height
    if ($result && $oldversion < 2007092302) {
        $table = new XMLDBTable('game_snakes_database');
        $field = new XMLDBField('height');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_snakes.sourcemodule
    if ($result && $oldversion < 2007092306) {
        $table = new XMLDBTable('game_snakes');
        $field = new XMLDBField('sourcemodule');
        $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, null, null, null, null, '');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_snakes.questionid
    if ($result && $oldversion < 2007092307) {
        $table = new XMLDBTable('game_snakes');
        $field = new XMLDBField('questionid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_snakes.glossaryentryid
    if ($result && $oldversion < 2007092308) {
        $table = new XMLDBTable('game_snakes');
        $field = new XMLDBField('glossaryentryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_snakes.dice
    if ($result && $oldversion < 2007092309) {
        $table = new XMLDBTable('game_snakes');
        $field = new XMLDBField('dice');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', null, null, null, null, null, null);
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_instances.lastremotehost
    if ($result && $oldversion < 2007100601) {
        $table = new XMLDBTable('game_instances');
        $field = new XMLDBField('lastremotehost');
        $field->setAttributes(XMLDB_TYPE_CHAR, '50', null, null, null, null, null, '');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_questions.timelastattempt
    if ($result && $oldversion < 2007100605) {
        $table = new XMLDBTable('game_questions');
        $field = new XMLDBField('timelastattempt');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_instances.tries
    if ($result && $oldversion < 2007101301) {
        $table = new XMLDBTable('game_instances');
        $field = new XMLDBField('tries');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //1.4
    //drop game_bookquiz_questions.bookid
    if ($result && $oldversion < 2007110801) {
        $table = new XMLDBTable('game_bookquiz_questions');
        $field = new XMLDBField('bookid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //new table game_grades
    if ($result && $oldversion < 2007110802) {
        /// Define table scorm_scoes_data to be created
        $table = new XMLDBTable('game_grades');
        /// Adding fields to table scorm_scoes_data
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('gameid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('score', XMLDB_TYPE_FLOAT, null, null, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        /// Adding keys to table scorm_scoes_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes
        $table->addIndexInfo('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
        $table->addIndexInfo('gameid', XMLDB_INDEX_NOTUNIQUE, array('gameid'));
        /// Launch create table for scorm_scoes_data
        $result = $result && create_table($table);
    }
    //drop game_hangman.sourcemodule
    if ($result && $oldversion < 2007110811) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('sourcemodule');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_hangman.questionid
    if ($result && $oldversion < 2007110812) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('questionsid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_hangman.sourcemodule
    if ($result && $oldversion < 2007110813) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('quizid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_hangman.glossaryid
    if ($result && $oldversion < 2007110814) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('glossaryid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_hangman.glossarycategoryid
    if ($result && $oldversion < 2007110815) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('glossarycategoryid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_hangman.glossaryentryid
    if ($result && $oldversion < 2007110816) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('glossaryentryid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_hangman.question
    if ($result && $oldversion < 2007110818) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('question');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_hangman.answer
    if ($result && $oldversion < 2007110819) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('answer');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_millionaire.sourcemodule
    if ($result && $oldversion < 2007110820) {
        $table = new XMLDBTable('game_millionaire');
        $field = new XMLDBField('sourcemodule');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_millionaire.quizid
    if ($result && $oldversion < 2007110821) {
        $table = new XMLDBTable('game_millionaire');
        $field = new XMLDBField('quizid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_millionaire.questionid
    if ($result && $oldversion < 2007110822) {
        $table = new XMLDBTable('game_millionaire');
        $field = new XMLDBField('questionid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //game_millionaire.queryid
    if ($result && $oldversion < 2007110823) {
        $table = new XMLDBTable('game_millionaire');
        $field = new XMLDBField('queryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'id');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //drop game_bookquiz.bookid
    if ($result && $oldversion < 2007110824) {
        $table = new XMLDBTable('game_bookquiz');
        $field = new XMLDBField('bookid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_sudoku.sourcemodule
    if ($result && $oldversion < 2007110825) {
        $table = new XMLDBTable('game_sudoku');
        $field = new XMLDBField('sourcemodule');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //game_sudoku.level
    if ($result && $oldversion < 2007110826) {
        $table = new XMLDBTable('game_millionaire');
        $field = new XMLDBField('queryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0', 'id');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //drop game_sudoku.quizid
    if ($result && $oldversion < 2007110827) {
        $table = new XMLDBTable('game_sudoku');
        $field = new XMLDBField('quizid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_sudoku.glossaryid
    if ($result && $oldversion < 2007110828) {
        $table = new XMLDBTable('game_sudoku');
        $field = new XMLDBField('glossaryid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_sudoku.glossarycategoryid
    if ($result && $oldversion < 2007110829) {
        $table = new XMLDBTable('game_sudoku');
        $field = new XMLDBField('glossarycategoryid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_sudoku.glossarycategoryid
    if ($result && $oldversion < 2007110830) {
        $result = $result && drop_table(new XMLDBTable('game_sudoku_questions'));
    }
    //drop game_cross.sourcemodule
    if ($result && $oldversion < 2007110832) {
        $table = new XMLDBTable('game_cross');
        $field = new XMLDBField('sourcemodule');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //game_cross.createscore
    if ($result && $oldversion < 2007110833) {
        $table = new XMLDBTable('game_cross');
        $field = new XMLDBField('createscore');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'wordsall');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //rename field game_cross.
    if ($result && $oldversion < 2007110834) {
        $table = new XMLDBTable('game_bookquiz');
        $field = new XMLDBField('attemptid');
        $field->setAttributes(XMLDB_TYPE_FLOAT, null, null, null, null, null, null, '0');
        $result = $result && rename_field($table, $field, 'score');
    }
    //drop game_cross.tries
    if ($result && $oldversion < 2007110835) {
        $table = new XMLDBTable('game_cross');
        $field = new XMLDBField('tries');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //rename field game_cross.createtimelimit
    if ($result && $oldversion < 2007110836) {
        $table = new XMLDBTable('game_cross');
        $field = new XMLDBField('timelimit');
        $field->setAttributes(XMLDB_TYPE_FLOAT, null, null, null, null, null, null, '0');
        $result = $result && rename_field($table, $field, 'createtimelimit');
    }
    //game_cross.createconnectors
    if ($result && $oldversion < 2007110837) {
        $table = new XMLDBTable('game_cross');
        $field = new XMLDBField('createconnectors');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_cross.createfilleds
    if ($result && $oldversion < 2007110838) {
        $table = new XMLDBTable('game_cross');
        $field = new XMLDBField('createfilleds');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_cross.createspaces
    if ($result && $oldversion < 2007110839) {
        $table = new XMLDBTable('game_cross');
        $field = new XMLDBField('createspaces');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //drop game_cross_questions
    if ($result && $oldversion < 2007110840) {
        $result = $result && drop_table(new XMLDBTable('game_cross_questions'));
    }
    //rename table game_instances to game_attempts
    if ($result && $oldversion < 2007110841) {
        $table = new XMLDBTable('game_questions');
        $result = $result && rename_table($table, 'game_queries');
    }
    //drop game_snakes.sourcemodule
    if ($result && $oldversion < 2007110853) {
        $table = new XMLDBTable('game_snakes');
        $field = new XMLDBField('sourcemodule');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_snakes.questionid
    if ($result && $oldversion < 2007110854) {
        $table = new XMLDBTable('game_snakes');
        $field = new XMLDBField('questionid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_snakes.glossaryentryid
    if ($result && $oldversion < 2007110855) {
        $table = new XMLDBTable('game_snakes');
        $field = new XMLDBField('glossaryentryid');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //rename table game_instances to game_attempts
    if ($result && $oldversion < 2007110856) {
        $table = new XMLDBTable('game_instances');
        $result = $result && rename_table($table, 'game_attempts');
    }
    //drop game_attempts.gamekind
    if ($result && $oldversion < 2007110857) {
        $table = new XMLDBTable('game_attempts');
        $field = new XMLDBField('gamekind');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_attempts.finished
    if ($result && $oldversion < 2007110858) {
        $table = new XMLDBTable('game_attempts');
        $field = new XMLDBField('finished');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //game_attempts.timestart
    if ($result && $oldversion < 2007110859) {
        $table = new XMLDBTable('game_attempts');
        $field = new XMLDBField('timestarted');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        $result = $result && rename_field($table, $field, 'timestart');
    }
    //game_attempts.timefinished
    if ($result && $oldversion < 2007110860) {
        $table = new XMLDBTable('game_attempts');
        $field = new XMLDBField('timefinished');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        $result = $result && rename_field($table, $field, 'timefinish');
    }
    //drop game_attempts.grade
    if ($result && $oldversion < 2007110861) {
        $table = new XMLDBTable('game_attempts');
        $field = new XMLDBField('grade');
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //drop game_attempts.attempts
    if ($result && $oldversion < 2007110862) {
        $table = new XMLDBTable('game_attempts');
        $field = new XMLDBField('tries');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        $result = $result && rename_field($table, $field, 'attempts');
    }
    //game_attempts.preview
    if ($result && $oldversion < 2007110863) {
        $table = new XMLDBTable('game_attempts');
        $field = new XMLDBField('preview');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, null, '0', 'lastremotehost');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_attempts.attempt
    if ($result && $oldversion < 2007110864) {
        $table = new XMLDBTable('game_attempts');
        $field = new XMLDBField('attempt');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'preview');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_attempts.score
    if ($result && $oldversion < 2007110865) {
        $table = new XMLDBTable('game_attempts');
        $field = new XMLDBField('score');
        $field->setAttributes(XMLDB_TYPE_FLOAT, null, XMLDB_UNSIGNED, null, null, null, null, '0', 'attempt');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //new table game_grades
    if ($result && $oldversion < 2007110866) {
        /// Define table scorm_scoes_data to be created
        $table = new XMLDBTable('game_course_input');
        /// Adding fields to table scorm_scoes_data
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, '');
        $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('sourcemodule', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, '');
        $table->addFieldInfo('ids', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, null, '');
        /// Adding keys to table scorm_scoes_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for scorm_scoes_data
        $result = $result && create_table($table);
    }
    //1.4-repair
    //game.gameinputid
    if ($result && $oldversion < 2007111302) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('gameinputid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'bookid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.bottomtext
    if ($result && $oldversion < 2007111303) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('bottomtext');
        $field->setAttributes(XMLDB_TYPE_TEXT, null, null, null, null, null, null);
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.grademethod
    if ($result && $oldversion < 2007111304) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('grademethod');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.grade
    if ($result && $oldversion < 2007111305) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('grade');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'bottomtext');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.decimalpoints
    if ($result && $oldversion < 2007111306) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('decimalpoints');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.popup
    if ($result && $oldversion < 2007111307) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('popup');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.review
    if ($result && $oldversion < 2007111308) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('review');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.attempts
    if ($result && $oldversion < 2007111309) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('attempts');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2007111310) {
        execute_sql("UPDATE {$CFG->prefix}game SET grade=0 WHERE grade IS NULL", true);
    }
    //ver 1.4 repair2
    //game_queries.attemptid
    if ($result && $oldversion < 2007111842) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('gameinstanceid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        $result = $result && rename_field($table, $field, 'attemptid');
    }
    //drop game_cross.tries
    if ($result && $oldversion < 2007111843) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('grade');
        //game.bottomtext
        if ($result && $oldversion < 2007111303) {
            $table = new XMLDBTable('game');
            $field = new XMLDBField('bottomtext');
            $field->setAttributes(XMLDB_TYPE_TEXT, null, null, null, null, null, null);
            /// Launch add field format
            $result = $result && add_field($table, $field);
        }
        /// Launch add field format
        $result = $result && drop_field($table, $field);
    }
    //game_queries.questiontext
    if ($result && $oldversion < 2007111844) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('questiontext');
        $field->setAttributes(XMLDB_TYPE_TEXT, null, null, null, null, null, null, '', 'glossaryentryid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_queries.score
    if ($result && $oldversion < 2007111845) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('score');
        $field->setAttributes(XMLDB_TYPE_FLOAT, null, null, null, null, null, null, '0', 'questiontext');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_queries.studentanswer
    if ($result && $oldversion < 2007111846) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('studentanswer');
        $field->setAttributes(XMLDB_TYPE_TEXT, null, null, null, null, null, null, '', 'glossaryentryid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_queries.col
    if ($result && $oldversion < 2007111847) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('col');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        $result = $result && add_field($table, $field);
    }
    //game_queries.row
    if ($result && $oldversion < 2007111848) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('row');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        $result = $result && add_field($table, $field);
    }
    //game_queries.horizontal
    if ($result && $oldversion < 2007111849) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('horizontal');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, null, '0');
        $result = $result && add_field($table, $field);
    }
    //game_queries.answertext
    if ($result && $oldversion < 2007111850) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('answertext');
        $field->setAttributes(XMLDB_TYPE_TEXT, null, null, null, null, null, null);
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_queries.correct
    if ($result && $oldversion < 2007111851) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('correct');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2007111853) {
        execute_sql("UPDATE {$CFG->prefix}game SET grademethod=1 WHERE grademethod=0 OR grademethod IS NULL", true);
    }
    //game_hangman.queryid
    if ($result && $oldversion < 2007111854) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('queryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'id');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_snakes.queryid
    if ($result && $oldversion < 2007111855) {
        $table = new XMLDBTable('game_snakes');
        $field = new XMLDBField('queryid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'snakesdatabaseid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_bookquiz_chapters.attemptid
    if ($result && $oldversion < 2007111856) {
        $table = new XMLDBTable('game_bookquiz_chapters');
        $field = new XMLDBField('attemptid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'id');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_hangman.letters : change to char( 100)
    if ($result && $oldversion < 2007120103) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('letters');
        $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null);
        /// Launch change of precision for field lang
        $result = $result && change_field_precision($table, $field);
    }
    //game_hangman.allletters : change to char( 100)
    if ($result && $oldversion < 2007120104) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('allletters');
        $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null);
        /// Launch change of precision for field lang
        $result = $result && change_field_precision($table, $field);
    }
    //1.4.c
    //game_queries.attachment
    if ($result && $oldversion < 2007120106) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('attachment');
        $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null);
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //1.6
    //game.glossaryid2
    if ($result && $oldversion < 2008011301) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('glossaryid2');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.glossarycategoryid2
    if ($result && $oldversion < 2008011302) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('glossarycategoryid2');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_queries.attachment
    if ($result && $oldversion < 2008011308) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('attachment');
        $field->setAttributes(XMLDB_TYPE_CHAR, '200', null, null, null, null, null, '');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //new table game_hiddenpicture
    if ($result && $oldversion < 2008011504) {
        /// Define table game_hiddenpicture to be created
        $table = new XMLDBTable('game_hiddenpicture');
        /// Adding fields to table scorm_scoes_data
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('correct', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('wrong', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('found', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        /// Adding keys to table scorm_scoes_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table
        $result = $result && create_table($table);
    }
    //game.param8
    if ($result && $oldversion < 2008012701) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('param8');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0', 'param7');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_queries.language
    if ($result && $oldversion < 2008071101) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('language');
        $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, null, null, null, null, '');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //new table game_export_javame
    if ($result && $oldversion < 2008072204) {
        /// Define table game_export_javame to be created
        $table = new XMLDBTable('game_export_javame');
        /// Adding fields to table scorm_scoes_data
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('gameid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('filename', XMLDB_TYPE_CHAR, '20');
        $table->addFieldInfo('icon', XMLDB_TYPE_CHAR, '100');
        $table->addFieldInfo('createdby', XMLDB_TYPE_CHAR, '50');
        $table->addFieldInfo('vendor', XMLDB_TYPE_CHAR, '50');
        $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '20');
        $table->addFieldInfo('description', XMLDB_TYPE_CHAR, '100');
        $table->addFieldInfo('version', XMLDB_TYPE_CHAR, '10');
        /// Adding keys to table scorm_scoes_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('gameid', XMLDB_INDEX_UNIQUE, array('gameid'));
        /// Launch create table
        $result = $result && create_table($table);
    }
    //Delete field game_hangman.quizid
    if ($result && $oldversion < 2008072501) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('quizid');
        /// Launch drop field grade_high
        $result = $result && drop_field($table, $field);
    }
    //Delete field game_hangman.glossaryid
    if ($result && $oldversion < 2008072502) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('glossaryid');
        /// Launch drop field grade_high
        $result = $result && drop_field($table, $field);
    }
    //Delete field game_hangman.questioncategoryid
    if ($result && $oldversion < 2008072503) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('questioncategoryid');
        /// Launch drop field grade_high
        $result = $result && drop_field($table, $field);
    }
    //Delete field game_hangman.gameinputid
    if ($result && $oldversion < 2008072504) {
        $table = new XMLDBTable('game_hangman');
        $field = new XMLDBField('gameinputid');
        /// Launch drop field grade_high
        $result = $result && drop_field($table, $field);
    }
    //game.subcategories
    if ($result && $oldversion < 2008090101) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('subcategories');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '1');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.state
    if ($result && $oldversion < 2008101103) {
        $table = new XMLDBTable('game_millionaire');
        $field = new XMLDBField('state');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch change_field_precision
        $result = $result && change_field_precision($table, $field);
    }
    //game_millionaire.level
    if ($result && $oldversion < 2008101104) {
        $table = new XMLDBTable('game_millionaire');
        $field = new XMLDBField('level');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch change_field_precision
        $result = $result && change_field_precision($table, $field);
    }
    //game_sudoku.level
    if ($result && $oldversion < 2008101106) {
        $table = new XMLDBTable('game_sudoku');
        $field = new XMLDBField('level');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch change_field_precision
        $result = $result && change_field_precision($table, $field);
    }
    //game_hiddenpicture.correct
    if ($result && $oldversion < 2008101107) {
        $table = new XMLDBTable('game_hiddenpicture');
        $field = new XMLDBField('correct');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch change_field_precision
        $result = $result && change_field_precision($table, $field);
    }
    //game_hiddenpicture.wrong
    if ($result && $oldversion < 2008101108) {
        $table = new XMLDBTable('game_hiddenpicture');
        $field = new XMLDBField('wrong');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch change_field_precision
        $result = $result && change_field_precision($table, $field);
    }
    //game_hiddenpicture.found
    if ($result && $oldversion < 2008101109) {
        $table = new XMLDBTable('game_hiddenpicture');
        $field = new XMLDBField('found');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch change_field_precision
        $result = $result && change_field_precision($table, $field);
    }
    //game_queries.answerid
    if ($result && $oldversion < 2008102701) {
        $table = new XMLDBTable('game_queries');
        $field = new XMLDBField('answerid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //new table game_export_html
    if ($result && $oldversion < 2008110701) {
        /// Define table game_export_html to be created
        $table = new XMLDBTable('game_export_html');
        /// Adding fields to table scorm_scoes_data
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('gameid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('filename', XMLDB_TYPE_CHAR, '30');
        $table->addFieldInfo('title', XMLDB_TYPE_CHAR, '200');
        $table->addFieldInfo('checkbutton', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL);
        $table->addFieldInfo('printbutton', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL);
        /// Adding keys to table scorm_scoes_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('gameid', XMLDB_INDEX_UNIQUE, array('gameid'));
        /// Launch create table
        $result = $result && create_table($table);
    }
    //rename field game_snakes_database.file to fileboard
    if ($result && $oldversion < 2008111701) {
        $table = new XMLDBTable('game_snakes_database');
        $field = new XMLDBField('file');
        $field->setAttributes(XMLDB_TYPE_CHAR, 100, null, null, null, null, null, '');
        $result = $result && rename_field($table, $field, 'fileboard');
    }
    //game_exp	//game.bottomtext
    if ($result && $oldversion < 2007111303) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('bottomtext');
        $field->setAttributes(XMLDB_TYPE_TEXT, null, null, null, null, null, null);
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009010502) {
        $table = new XMLDBTable('game_export_javame');
        $field = new XMLDBField('maxpicturewidth');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '7');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //new table game_repetitions
    if ($result && $oldversion < 2009031801) {
        /// Define table game_repetitions to be created
        $table = new XMLDBTable('game_repetitions');
        /// Adding fields to table game_repetitions
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('gameid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('questionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('glossaryentryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('repetitions', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        /// Adding keys to table scorm_scoes_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('main', XMLDB_INDEX_UNIQUE, array('gameid,userid,questionid,glossaryentryid'));
        /// Launch create table
        $result = $result && create_table($table);
    }
    //game.shuffle
    if ($result && $oldversion < 2009071403) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('shuffle');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, null, '1', 'param8');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009072801) {
        $table = new XMLDBTable('game_export_html');
        $field = new XMLDBField('inputsize');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED);
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_export_html.maxpicturewidth
    if ($result && $oldversion < 2009072901) {
        $table = new XMLDBTable('game_export_html');
        $field = new XMLDBField('maxpicturewidth');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '7');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_export_html.maxpictureheight
    if ($result && $oldversion < 2009073101) {
        $table = new XMLDBTable('game_export_html');
        $field = new XMLDBField('maxpictureheight');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '7');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game_export_javame.maxpictureheight
    if ($result && $oldversion < 2009073102) {
        $table = new XMLDBTable('game_export_javame');
        $field = new XMLDBField('maxpictureheight');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '7');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    //game.toptext
    if ($result && $oldversion < 2009083102) {
        $table = new XMLDBTable('game');
        $field = new XMLDBField('toptext');
        $field->setAttributes(XMLDB_TYPE_TEXT, null, null, null, null, null, null, null, 'gameinputid');
        /// Launch add field format
        $result = $result && add_field($table, $field);
    }
    return $result;
}
Esempio n. 11
0
/**
 * Drop 'new' 1.8 groups tables for 200701240 upgrade below.
 * (Also, for testing it's useful to be able to revert to 'old' groups.)
 */
function groups_revert_db($renametemp = true)
{
    $status = true;
    ///$status = (bool)$rs = delete_records('config', 'name', 'group_version');
    if (table_exists(new XMLDBTable('groups_groupings'))) {
        $tables = array('', '_members', '_groupings', '_courses_groups', '_courses_groupings', '_groupings_groups');
        foreach ($tables as $t_name) {
            $status = $status && drop_table(new XMLDBTable('groups' . $t_name));
        }
        $status = $status && (bool) delete_records('log_display', 'module', 'group');
        if ($renametemp) {
            $status = $status && rename_table(new XMLDBTable('groups_temp'), 'groups');
            $status = $status && rename_table(new XMLDBTable('groups_members_temp'), 'groups_members');
        }
    }
    return $status;
}
Esempio n. 12
0
        {
            $dx = strval($ann - 1);
            $dx = substr($dx, 2, 2);
            echo $dx;
            $chain1 = "create table " . substr($name_tb, 0, -2) . $dx . " as select * from " . $name_tb . ";\n";
            fwrite($ct, $chain1);
            $chain2 = "delete from " . $name_tb . ";\n";
            fwrite($ct, $chain2);
        }
        rename_table("b_depdgi", $ann_ref, $ctab);
        rename_table("b_desdgi", $ann_ref, $ctab);
        rename_table("b_exodgi", $ann_ref, $ctab);
        rename_table("b_habdgi", $ann_ref, $ctab);
        rename_table("b_prodgi", $ann_ref, $ctab);
        rename_table("b_subdgi", $ann_ref, $ctab);
        rename_table("b_taxdgi", $ann_ref, $ctab);
        rename_table("batidgi", $ann_ref, $ctab);
        rename_table("p_exoner", $ann_ref, $ctab);
        rename_table("p_subdif", $ann_ref, $ctab);
        rename_table("p_taxat", $ann_ref, $ctab);
        rename_table("parcel", $ann_ref, $ctab);
        rename_table("propriet", $ann_ref, $ctab);
        rename_table("voies", $ann_ref, $ctab);
        rename_table("commune", $ann_ref, $ctab);
    }
    fclose($ctab);
}
?>
</body>
</html>
Esempio n. 13
0
        echo "Erreur lors de la cr�ation de la base: " . mysql_error();
        echo "Changement de nom des tables en cours\n";
        mysql_select_db("cadastre", $lk);
        rename_table("b_depdgi", $_POST['ann_ref']);
        rename_table("b_desdgi", $_POST['ann_ref']);
        rename_table("b_exodgi", $_POST['ann_ref']);
        rename_table("b_habdgi", $_POST['ann_ref']);
        rename_table("b_prodgi", $_POST['ann_ref']);
        rename_table("b_subdgi", $_POST['ann_ref']);
        rename_table("b_taxdgi", $_POST['ann_ref']);
        rename_table("batidgi", $_POST['ann_ref']);
        rename_table("p_exoner", $_POST['ann_ref']);
        rename_table("p_subdif", $_POST['ann_ref']);
        rename_table("p_taxat", $_POST['ann_ref']);
        rename_table("parcel", $_POST['ann_ref']);
        rename_table("propriet", $_POST['ann_ref']);
    }
}
include "test_ora.php";
include "test_pg.php";
if ($voracle == 'oracle' or $_POST['vmysql'] == 'mysql') {
    $feror = fopen($_POST['ferr'], "a");
    if ($_POST['nbat'] != "") {
        $fp = fopen($_POST['nbat'], "r");
        /* Ouverture du fichier du non b�ti et chargement des tables*/
        if ($_POST['voracle'] == 'oracle') {
            $ctab = fopen($_POST['rep_f'] . "cree_tab.sql", a);
            /* Int�gration dans le script sql de l'insertion des donn�es */
            fwrite($ctab, "start " . $_POST['rep_f'] . "parcel.ora;\n");
            /* Donn�es de la table parcelle */
            fwrite($ctab, "commit;\n");
Esempio n. 14
0
/**
 * Drop, add fields and rename tables for groups upgrade from 1.8.*
 * @param XMLDBTable $table 'groups_groupings' table object.
 */
function upgrade_18_groups()
{
    global $CFG, $db;
    $result = upgrade_18_groups_drop_keys_indexes();
    /// Delete not used columns
    $fields_r = array('viewowngroup', 'viewallgroupsmembers', 'viewallgroupsactivities', 'teachersgroupmark', 'teachersgroupview', 'teachersoverride', 'teacherdeletable');
    foreach ($fields_r as $fname) {
        $table = new XMLDBTable('groups_groupings');
        $field = new XMLDBField($fname);
        if (field_exists($table, $field)) {
            $result = $result && drop_field($table, $field);
        }
    }
    /// Rename 'groups_groupings' to 'groupings'
    $table = new XMLDBTable('groups_groupings');
    $result = $result && rename_table($table, 'groupings');
    /// Add columns/key 'courseid', exclusivegroups, maxgroupsize, timemodified.
    $table = new XMLDBTable('groupings');
    $field = new XMLDBField('courseid');
    $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'id');
    $result = $result && add_field($table, $field);
    $table = new XMLDBTable('groupings');
    $key = new XMLDBKey('courseid');
    $key->setAttributes(XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
    $result = $result && add_key($table, $key);
    $table = new XMLDBTable('groupings');
    $field = new XMLDBField('configdata');
    $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'description');
    $result = $result && add_field($table, $field);
    $table = new XMLDBTable('groupings');
    $field = new XMLDBField('timemodified');
    $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'timecreated');
    $result = $result && add_field($table, $field);
    //==================
    /// Add columns/key 'courseid' into groups table
    $table = new XMLDBTable('groups');
    $field = new XMLDBField('courseid');
    $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'id');
    $result = $result && add_field($table, $field);
    $table = new XMLDBTable('groups');
    $key = new XMLDBKey('courseid');
    $key->setAttributes(XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
    $result = $result && add_key($table, $key);
    /// Changing nullability of field enrolmentkey on table groups to null
    $table = new XMLDBTable('groups');
    $field = new XMLDBField('enrolmentkey');
    $field->setAttributes(XMLDB_TYPE_CHAR, '50', null, null, null, null, null, null, 'description');
    $result = $result && change_field_notnull($table, $field);
    //==================
    /// Now, rename 'groups_groupings_groups' to 'groupings_groups' and add keys
    $table = new XMLDBTable('groups_groupings_groups');
    $result = $result && rename_table($table, 'groupings_groups');
    $table = new XMLDBTable('groupings_groups');
    $key = new XMLDBKey('groupingid');
    $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupingid'), 'groupings', array('id'));
    $result = $result && add_key($table, $key);
    $table = new XMLDBTable('groupings_groups');
    $key = new XMLDBKey('groupid');
    $key->setAttributes(XMLDB_KEY_FOREIGN, array('groupid'), 'groups', array('id'));
    $result = $result && add_key($table, $key);
    ///=================
    /// Transfer courseid from 'mdl_groups_courses_groups' to 'mdl_groups'.
    if ($result) {
        $sql = "UPDATE {$CFG->prefix}groups\n                   SET courseid = (\n                        SELECT MAX(courseid)\n                          FROM {$CFG->prefix}groups_courses_groups gcg\n                         WHERE gcg.groupid = {$CFG->prefix}groups.id)";
        execute_sql($sql);
    }
    /// Transfer courseid from 'groups_courses_groupings' to 'mdl_groupings'.
    if ($result) {
        $sql = "UPDATE {$CFG->prefix}groupings\n                   SET courseid = (\n                        SELECT MAX(courseid)\n                          FROM {$CFG->prefix}groups_courses_groupings gcg\n                         WHERE gcg.groupingid = {$CFG->prefix}groupings.id)";
        execute_sql($sql);
    }
    /// Drop the old tables
    if ($result) {
        drop_table(new XMLDBTable('groups_courses_groups'));
        drop_table(new XMLDBTable('groups_courses_groupings'));
        drop_table(new XMLDBTable('groups_temp'));
        drop_table(new XMLDBTable('groups_members_temp'));
        unset_config('group_version');
    }
    return $result;
}
function xmldb_block_exabis_eportfolio_upgrade($oldversion = 0, $tmp)
{
    global $CFG, $db;
    $result = true;
    //     if (empty($db)) {
    //         return false;
    //     }
    if ($oldversion < 2008090100 && !empty($db)) {
        // old tables
        $tables = array('block_exabeporpers', 'block_exabeporexte', 'block_exabeporcate', 'block_exabeporbooklink', 'block_exabeporcommlink', 'block_exabeporsharlink', 'block_exabeporbookfile', 'block_exabeporcommfile', 'block_exabeporsharfile', 'block_exabepornote', 'block_exabeporcommnote', 'block_exabeporsharnote');
        $tableNames = array();
        // rename tables to old_*
        foreach ($tables as $table) {
            $tableNames[$table] = 'old_' . $oldversion . '_' . $table;
            $xmltable = new XMLDBTable($table);
            rename_table($xmltable, $tableNames[$table]);
        }
        // add new tables
        install_from_xmldb_file(dirname(__FILE__) . '/install.xml');
        // import data from old tables
        $insert_type = 'REPLACE';
        $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporuser (id, user_id, persinfo_timemodified, description, user_hash)' . ' SELECT u.id, u.userid, u.timemodified, u.description, e.user_hash FROM ' . $CFG->prefix . $tableNames['block_exabeporpers'] . ' AS u LEFT JOIN ' . $CFG->prefix . $tableNames['block_exabeporexte'] . ' AS e ON u.userid = e.user_id');
        $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporcate (id, pid, userid, name, timemodified, courseid)' . ' SELECT id, pid, userid, name, timemodified, course FROM ' . $CFG->prefix . $tableNames['block_exabeporcate']);
        $file_id_start = 0;
        $note_id_start = get_field_select($tableNames['block_exabepornote'], 'MAX(id)', null) + 100;
        $link_id_start = get_field_select($tableNames['block_exabeporbooklink'], 'MAX(id)', null) + $note_id_start + 100;
        // combine item table
        $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporitem' . ' (id, userid, type, categoryid, name, url, intro, attachment, timemodified, courseid, shareall, externaccess, externcomment)' . ' SELECT id+' . $file_id_start . ', userid, "file", category, name, url, intro, attachment, timemodified, course, shareall, externaccess, externcomment' . ' FROM ' . $CFG->prefix . $tableNames['block_exabeporbookfile']);
        $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporitem' . ' (id, userid, type, categoryid, name, url, intro, attachment, timemodified, courseid, shareall, externaccess, externcomment)' . ' SELECT id+' . $note_id_start . ', userid, "note", category, name, url, intro, attachment, timemodified, course, shareall, externaccess, externcomment' . ' FROM ' . $CFG->prefix . $tableNames['block_exabepornote']);
        $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporitem' . ' (id, userid, type, categoryid, name, url, intro, attachment, timemodified, courseid, shareall, externaccess, externcomment)' . ' SELECT id+' . $link_id_start . ', userid, "link", category, name, url, intro, attachment, timemodified, course, shareall, externaccess, externcomment' . ' FROM ' . $CFG->prefix . $tableNames['block_exabeporbooklink']);
        // combine comment table
        $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporitemcomm' . ' (id, itemid, userid, entry, timemodified)' . ' SELECT id, bookmarkid+' . $file_id_start . ', userid, entry, timemodified' . ' FROM ' . $CFG->prefix . $tableNames['block_exabeporcommfile']);
        $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporitemcomm' . ' (id, itemid, userid, entry, timemodified)' . ' SELECT id, bookmarkid+' . $note_id_start . ', userid, entry, timemodified' . ' FROM ' . $CFG->prefix . $tableNames['block_exabeporcommnote']);
        $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporitemcomm' . ' (id, itemid, userid, entry, timemodified)' . ' SELECT id, bookmarkid+' . $link_id_start . ', userid, entry, timemodified' . ' FROM ' . $CFG->prefix . $tableNames['block_exabeporcommlink']);
        // combine share table
        $ret = $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporitemshar' . ' (id, itemid, userid, original, courseid)' . ' SELECT id, bookid+' . $file_id_start . ', userid, original, course' . ' FROM ' . $CFG->prefix . $tableNames['block_exabeporsharfile']);
        $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporitemshar' . ' (id, itemid, userid, original, courseid)' . ' SELECT id, bookid+' . $note_id_start . ', userid, original, course' . ' FROM ' . $CFG->prefix . $tableNames['block_exabeporsharnote']);
        $db->Execute($insert_type . ' INTO ' . $CFG->prefix . 'block_exabeporitemshar' . ' (id, itemid, userid, original, courseid)' . ' SELECT id, bookid+' . $link_id_start . ', userid, original, course' . ' FROM ' . $CFG->prefix . $tableNames['block_exabeporsharlink']);
        $result = true;
    }
    if ($result && $oldversion < 2009010104) {
        // Add THEME support (nadavkav)
        //$result = execute_sql("ALTER TABLE `{$CFG->prefix}_block_exabeporview` ADD `theme` TEXT NULL DEFAULT NULL AFTER `description`");
        $table = new XMLDBTable('block_exabeporview');
        /// Adding fields to table block_exabeporview
        //$table->addFieldInfo('theme', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null);
        $field = new XMLDBField('theme');
        $field->setAttributes(XMLDB_TYPE_CHAR, '25', null, null, null, null, null, null, 'description');
        /// Launch update table for block_exabeporview
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009010105) {
        // Add THEME support (nadavkav)
        //$result = execute_sql("ALTER TABLE `{$CFG->prefix}_block_exabeporuser` ADD `emailnotification` TEXT NULL DEFAULT NULL AFTER `description`");
        $table = new XMLDBTable('block_exabeporuser');
        /// Adding fields to table block_exabeporview
        //$table->addFieldInfo('theme', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null);
        $field = new XMLDBField('emailnotification');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'user_hash');
        /// Launch update table for block_exabeporview
        $result = $result && add_field($table, $field);
    }
    return $result;
}
Esempio n. 16
0
		rename_table("cadastre.b_exodgi",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.b_habdgi",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.b_prodgi",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.b_subdgi",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.b_taxdgi",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.batidgi",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.p_exoner",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.p_subdif",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.p_taxat",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.parcel",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.propriet",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.voies",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.pdldgi",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.pdlardgi",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.pdlasdgi",$_POST['ann_ref'],$pgx);
		rename_table("cadastre.pdldesdgi",$_POST['ann_ref'],$pgx);
		//rename_table("cadastre.commune",$_POST['ann_ref'],$pgx);
	}
//fclose($ctab);
	if (strrpos($_POST['rep_f'],"/")==(strlen($_POST['rep_f'])-1)){
		$_POST['rep_f']=substr($_POST['rep_f'],0,-1);
	}else{
		$_POST['rep_f']=$_POST['rep_f'];
	}
	exec("perl /var/www/application/apps/cadastre/lit_cad.pl ".$_POST['rep_f']. " ".$_POST['ext_f'] );
	pg_exec($pgx,"copy cadastre.parcel from '".$_POST['rep_f']."/parcelle.csv' with csv;");
	pg_exec($pgx,"copy cadastre.batidgi from '".$_POST['rep_f']."/batidgi.csv' with csv;");
	pg_exec($pgx,"copy cadastre.b_depdgi from '".$_POST['rep_f']."/bdepdgi.csv' with csv;");
	pg_exec($pgx,"copy cadastre.b_desdgi from '".$_POST['rep_f']."/bdesdgi.csv' with csv;");
	pg_exec($pgx,"copy cadastre.b_exodgi from '".$_POST['rep_f']."/bexodgi.csv' with csv;");
	pg_exec($pgx,"copy cadastre.b_taxdgi from '".$_POST['rep_f']."/btaxdgi.csv' with csv;");
Esempio n. 17
0
function xmldb_block_curr_admin_upgrade($oldversion = 0)
{
    global $CFG, $THEME, $db;
    $result = true;
    if ($oldversion < 2009010102) {
        $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
        if ($role = get_record('role', 'shortname', 'curriculumadmin')) {
            if ($role->name == 'Bundle Administrator') {
                $role->name = 'Curriculum Administrator';
                addslashes_object($role);
                update_record('role', $role);
            }
        }
        if (!empty($role->id)) {
            require_once dirname(__FILE__) . '/access.php';
            if (!empty($block_curr_admin_capabilities)) {
                foreach ($block_curr_admin_capabilities as $capname => $caprules) {
                    $result = $result && assign_capability($capname, CAP_ALLOW, $role->id, $context->id);
                }
            }
        }
    }
    if ($oldversion < 2009010103) {
        $table = new XMLDBTable('crlm_curriculum');
        $field = new XMLDBField('timetocomplete');
        $field->setAttributes(XMLDB_TYPE_CHAR, '64', NULL, XMLDB_NOTNULL, NULL, NULL, NULL, '0h, 0d, 0w, 0m, 0y', 'timemodified');
        $result = $result && add_field($table, $field);
        $field = new XMLDBField('frequency');
        $field->setAttributes(XMLDB_TYPE_CHAR, '64', NULL, XMLDB_NOTNULL, NULL, NULL, NULL, '0h, 0d, 0w, 0m, 0y', 'timetocomplete');
        $result = $result && add_field($table, $field);
    }
    if ($oldversion < 2009010104) {
        $table = new XMLDBTable('crlm_config');
        $table->comment = 'Curriculum management configuration values.';
        // fields
        $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $f = $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, false, null, null, null, null);
        $f = $table->addFieldInfo('value', XMLDB_TYPE_TEXT, 'medium', null, false, null, null, null, null);
        // PK and indexes
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('name_ix', XMLDB_INDEX_UNIQUE, array('name'));
        $result = $result && create_table($table);
    }
    if ($oldversion < 2009010105) {
        $table = new XMLDBTable('crlm_coursetemplate');
        $table->comment = 'Course templates';
        // fields
        $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $f = $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', false, XMLDB_NOTNULL, null, null, null, null);
        $f = $table->addFieldInfo('location', XMLDB_TYPE_CHAR, '255', null, false, null, null, null, null);
        $f = $table->addFieldInfo('templateclass', XMLDB_TYPE_CHAR, '255', null, false, null, null, null, null);
        // PK and indexes
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('courseid_ix', XMLDB_INDEX_UNIQUE, array('courseid'));
        $result = $result && create_table($table);
    }
    if ($oldversion < 2009010106) {
        $table = new XMLDBTable('crlm_cluster_curriculum');
        $table->comment = 'Association between clusters and curricula';
        // fields
        $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $f = $table->addFieldInfo('clusterid', XMLDB_TYPE_INTEGER, '10', null, true, null, null, null, null, 'id');
        $f->comment = 'Foreign key to cluster id';
        $f = $table->addFieldInfo('curriculumid', XMLDB_TYPE_INTEGER, '10', null, true, null, null, null, null, 'clusterid');
        $f->comment = 'Foreign key to curriculum id';
        // PK and indexes
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('cluster_idx', XMLDB_INDEX_NOTUNIQUE, array('clusterid'));
        $result = $result && create_table($table);
    }
    if ($oldversion < 2009010108) {
        $table = new XMLDBTable('crlm_cluster_track');
        $table->comment = 'Association between clusters and tracks';
        // fields
        $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $f = $table->addFieldInfo('clusterid', XMLDB_TYPE_INTEGER, '10', null, true, null, null, null, null, 'id');
        $f->comment = 'Foreign key to cluster id';
        $f = $table->addFieldInfo('trackid', XMLDB_TYPE_INTEGER, '10', null, true, null, null, null, null, 'clusterid');
        $f->comment = 'Foreign key to track id';
        $f = $table->addFieldInfo('autounenrol', XMLDB_TYPE_INTEGER, '1', null, true, null, null, null, null, 'trackid');
        $f->comment = 'Whether or not to remove a user from classes when removed from cluster';
        // PK and indexes
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('cluster_idx', XMLDB_INDEX_NOTUNIQUE, array('clusterid'));
        $result = $result && create_table($table);
        $table = new XMLDBTable('crlm_usercluster');
        $f = new XMLDBField('autoenrol');
        $f->setAttributes(XMLDB_TYPE_INTEGER, '1', null, true, null, null, null, 1, 'clusterid');
        $f->comment = 'Whether users should be autoenrolled in tracks associated with this cluster.';
        $result = $result && add_field($table, $f);
    }
    if ($oldversion < 2009010109) {
        /// Define table crlm_class_moodle to be created
        $table = new XMLDBTable('crlm_class_moodle');
        /// Adding fields to table crlm_class_moodle
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('classid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('moodlecourseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('enroltype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, null, '0');
        $table->addFieldInfo('enrolplugin', XMLDB_TYPE_CHAR, '20', null, null, null, null, null, 'crlm');
        $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        /// Adding keys to table crlm_class_moodle
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addKeyInfo('mdl_currclasmood_clamoo_uix', XMLDB_KEY_UNIQUE, array('classid', 'moodlecourseid'));
        /// Launch create table for crlm_class_moodle
        $result = $result && create_table($table);
    }
    if ($oldversion < 2009010110) {
        $table = new XMLDBTable('crlm_user_track');
        $table->comment = 'User enrolment in tracks';
        // fields
        $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $f = $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', null, true, null, null, null, null, 'id');
        $f->comment = 'Foreign key to user id';
        $f = $table->addFieldInfo('trackid', XMLDB_TYPE_INTEGER, '10', null, true, null, null, null, null, 'userid');
        $f->comment = 'Foreign key to track id';
        // PK and indexes
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2009010112) {
        /// Define table crlm_notification_log to be created
        $table = new XMLDBTable('crlm_notification_log');
        /// Adding fields to table crlm_notification_log
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('event', XMLDB_TYPE_CHAR, '166', null, null, null, null, null, null);
        $table->addFieldInfo('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('data', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
        $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        /// Adding keys to table crlm_notification_log
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table crlm_notification_log
        $table->addIndexInfo('event_inst_user_ix', XMLDB_INDEX_NOTUNIQUE, array('event', 'instance', 'userid'));
        /// Launch create table for crlm_notification_log
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2009010113) {
        /// Define index event_inst_user_ix (not unique) to be dropped from crlm_notification_log
        $table = new XMLDBTable('crlm_notification_log');
        $index = new XMLDBIndex('event_inst_user_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('event', 'instance', 'userid'));
        /// Launch drop index event_inst_user_ix
        $result = $result && drop_index($table, $index);
        /// Define index event_inst_user_ix (not unique) to be added to crlm_notification_log
        $table = new XMLDBTable('crlm_notification_log');
        $index = new XMLDBIndex('event_inst_user_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('userid', 'instance', 'event'));
        /// Launch add index event_inst_user_ix
        $result = $result && add_index($table, $index);
    }
    if ($result && $oldversion < 2009010114) {
        // Creating track table
        $table = new XMLDBTable('crlm_track');
        $table->comment = 'Track table';
        // fields
        $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $f = $table->addFieldInfo('curid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null);
        $f = $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
        $f = $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
        $f = $table->addFieldInfo('startdate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('enddate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        // PK and indexes
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('track_curr_idx', XMLDB_INDEX_NOTUNIQUE, array('curid'));
        $result = $result && create_table($table);
        $table = new XMLDBTable('crlm_track_class');
        $table->comment = 'Track class table';
        $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $f = $table->addFieldInfo('trackid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('classid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('requried', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('autoenrol', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('default', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $f = $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('trackid_idx', XMLDB_INDEX_NOTUNIQUE, array('trackid'));
        $table->addIndexInfo('track_classid_idx', XMLDB_INDEX_NOTUNIQUE, array('classid'));
        $table->addIndexInfo('track_courseid_idx', XMLDB_INDEX_NOTUNIQUE, array('courseid'));
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2009010115) {
        /// Define table crlm_cluster_profile to be created
        $table = new XMLDBTable('crlm_cluster_profile');
        /// Adding fields to table crlm_cluster_profile
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('clusterid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('fieldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('value', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table crlm_cluster_profile
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for crlm_cluster_profile
        $result = $result && create_table($table);
        /// Define table crlm_cluster_assignments to be created
        $table = new XMLDBTable('crlm_cluster_assignments');
        /// Adding fields to table crlm_cluster_assignments
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('clusterid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('plugin', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table crlm_cluster_assignments
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for crlm_cluster_assignments
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2009010116) {
        $table = new XMLDBTable('crlm_track_class');
        $field = new XMLDBField('default');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', 'autoenrol');
        $result = $result && drop_field($table, $field);
        $field = new XMLDBField('defaulttrack');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', 'autoenrol');
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009010117) {
        /// Remove obsolete job code tables if they exist.
        $table = new XMLDBTable('crlm_jobcode_list');
        if (table_exists($table)) {
            drop_table($table);
        }
        $table = new XMLDBTable('crlm_curriculum_jobcode');
        if (table_exists($table)) {
            drop_table($table);
        }
    }
    if ($result && $oldversion < 2009010118) {
        /// Removing defaulttrack column from table
        $table = new XMLDBTable('crlm_track_class');
        $field = new XMLDBField('defaulttrack');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', 'autoenrol');
        $result = $result && drop_field($table, $field);
        /// Adding defaulttrack column to table
        $table = new XMLDBTable('crlm_track');
        $field = new XMLDBField('defaulttrack');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', 'enddate');
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009010119) {
        /// Define field completed to be added to crlm_curriculum_assignment
        $table = new XMLDBTable('crlm_curriculum_assignment');
        $field = new XMLDBField('completed');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'curriculumid');
        /// Launch add field completed
        $result = $result && add_field($table, $field);
        /// Define field completiontime to be added to crlm_curriculum_assignment
        $field = new XMLDBField('timecompleted');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'completed');
        /// Launch add field completiontime
        $result = $result && add_field($table, $field);
        /// Define field credits to be added to crlm_curriculum_assignment
        $field = new XMLDBField('credits');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'timecompleted');
        /// Launch add field credits
        $result = $result && add_field($table, $field);
        /// Define field locked to be added to crlm_curriculum_assignment
        $table = new XMLDBTable('crlm_curriculum_assignment');
        $field = new XMLDBField('locked');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'credits');
        /// Launch add field locked
        $result = $result && add_field($table, $field);
        /// Define key mdl_currcurrassi_usecur_uix (unique) to be dropped from crlm_curriculum_assignment
        $key = new XMLDBKey('mdl_currcurrassi_usecur_uix');
        $key->setAttributes(XMLDB_KEY_UNIQUE, array('userid', 'curriculumid'));
        /// Launch drop key mdl_currcurrassi_usecur_uix
        $result = $result && drop_key($table, $key);
        /// Define index mdl_currcurrassi_usecurcom_ix (not unique) to be added to crlm_curriculum_assignment
        $index = new XMLDBIndex('mdl_currcurrassi_usecurcom_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('userid', 'curriculumid', 'completed'));
        /// Launch add index mdl_currcurrassi_usecurcom_ix
        $result = $result && add_index($table, $index);
        /// Define index completed_ix (not unique) to be added to crlm_curriculum_assignment
        $index = new XMLDBIndex('completed_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('completed'));
        /// Launch add index completed_ix
        $result = $result && add_index($table, $index);
    }
    if ($result && $oldversion < 2009010120) {
        /// Define field autoenrol to be added to crlm_cluster_assignments
        $table = new XMLDBTable('crlm_cluster_assignments');
        $field = new XMLDBField('autoenrol');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'plugin');
        /// Launch add field autoenrol
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009010121) {
        if (!record_exists('mnet_application', 'name', 'java')) {
            $application = new stdClass();
            $application->name = 'java';
            $application->display_name = 'Java servlet';
            $application->xmlrpc_server_url = '/mnet/server';
            $application->sso_land_url = '/mnet/land.jsp';
            $result = $result && insert_record('mnet_application', $application, false);
        }
    }
    if ($result && $oldversion < 2009010122) {
        $table = new XMLDBTable('crlm_track_class');
        $field = new XMLDBField('requried');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'courseid');
        $result = $result && drop_field($table, $field);
    }
    if ($result && $oldversion < 2009010125) {
        $result = $result && execute_sql('CREATE OR REPLACE VIEW `courseNforums` AS select `f`.`id` AS `forumid`,concat(`c`.`shortname`,_utf8\' | \',`f`.`name`) AS `courseNforumname` from (`mdl_forum` `f` join `mdl_course` `c` on((`c`.`id` = `f`.`course`))) order by `c`.`shortname`,`f`.`name`');
    }
    if ($result && $oldversion < 2009010126) {
        $table = new XMLDBTable('crlm_cluster_curriculum');
        $table->comment = 'Association between clusters and curricula';
        // fields
        $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $f = $table->addFieldInfo('clusterid', XMLDB_TYPE_INTEGER, '10', null, true, null, null, null, null, 'id');
        $f->comment = 'Foreign key to cluster id';
        $f = $table->addFieldInfo('curriculumid', XMLDB_TYPE_INTEGER, '10', null, true, null, null, null, null, 'clusterid');
        $f->comment = 'Foreign key to curriculum id';
        // PK and indexes
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('cluster_idx', XMLDB_INDEX_NOTUNIQUE, array('clusterid'));
        $result = $result && (table_exists($table) || create_table($table));
        $table = new XMLDBTable('crlm_cluster_track');
        $table->comment = 'Association between clusters and tracks';
        // fields
        $f = $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', false, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $f = $table->addFieldInfo('clusterid', XMLDB_TYPE_INTEGER, '10', null, true, null, null, null, null, 'id');
        $f->comment = 'Foreign key to cluster id';
        $f = $table->addFieldInfo('trackid', XMLDB_TYPE_INTEGER, '10', null, true, null, null, null, null, 'clusterid');
        $f->comment = 'Foreign key to track id';
        $f = $table->addFieldInfo('autounenrol', XMLDB_TYPE_INTEGER, '1', null, true, null, null, null, null, 'trackid');
        $f->comment = 'Whether or not to remove a user from classes when removed from cluster';
        // PK and indexes
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addIndexInfo('cluster_idx', XMLDB_INDEX_NOTUNIQUE, array('clusterid'));
        $result = $result && (table_exists($table) || create_table($table));
    }
    if ($result && $oldversion < 2009010127) {
        // fix silly typos
        $newtable = new XMLDBTable('crlm_user_track');
        $oldtable = new XMLDBTable('clrm_user_track');
        $result = $result && (table_exists($newtable) || rename_table($oldtable, 'crlm_user_track'));
        $oldtable = new XMLDBTable('clrm_cluster_track');
        $result = $result && (!table_exists($oldtable) || drop_table($oldtable));
        $oldtable = new XMLDBTable('clrm_cluster_curriculum');
        $result = $result && (!table_exists($oldtable) || drop_table($oldtable));
    }
    if ($result && $oldversion < 2009010128) {
        require_once $CFG->dirroot . '/curriculum/lib/lib.php';
        cm_migrate_moodle_users();
    }
    if ($result && $oldversion < 2009010131) {
        /// Get rid of any outdated cluster data we might have lying around.
        if ($CFG->dbfamily == 'postgres') {
            $sql = "DELETE FROM {$CFG->prefix}crlm_cluster_assignments\n                    WHERE id IN (\n                        SELECT ca.clusterid\n                        FROM {$CFG->prefix}crlm_cluster_assignments ca\n                        LEFT JOIN {$CFG->prefix}crlm_cluster c ON c.id = ca.clusterid\n                        WHERE c.id IS NULL\n                    )";
            $result = $result && execute_sql($sql);
            $sql = "DELETE FROM {$CFG->prefix}crlm_cluster_curriculum\n                    WHERE id IN (\n                        SELECT cc.clusterid\n                        FROM {$CFG->prefix}crlm_cluster_curriculum cc\n                        LEFT JOIN {$CFG->prefix}crlm_cluster c ON c.id = cc.clusterid\n                        WHERE c.id IS NULL\n                    )";
            $result = $result && execute_sql($sql);
            $sql = "DELETE FROM {$CFG->prefix}crlm_cluster_profile\n                    WHERE id IN (\n                        SELECT cp.clusterid\n                        FROM {$CFG->prefix}crlm_cluster_profile cp\n                        LEFT JOIN {$CFG->prefix}crlm_cluster c ON c.id = cp.clusterid\n                        WHERE c.id IS NULL\n                    )";
            $result = $result && execute_sql($sql);
            $sql = "DELETE FROM {$CFG->prefix}crlm_cluster_track\n                    WHERE id IN (\n                        SELECT ct.clusterid\n                        FROM {$CFG->prefix}crlm_cluster_track ct\n                        LEFT JOIN {$CFG->prefix}crlm_cluster c ON c.id = ct.clusterid\n                        WHERE c.id IS NULL\n                    )";
            $result = $result && execute_sql($sql);
            $sql = "DELETE FROM {$CFG->prefix}crlm_usercluster\n                    WHERE id IN (\n                        SELECT uc.clusterid\n                        FROM {$CFG->prefix}crlm_usercluster uc\n                        LEFT JOIN {$CFG->prefix}crlm_cluster c ON c.id = uc.clusterid\n                        WHERE c.id IS NULL\n                    )";
            $result = $result && execute_sql($sql);
        } else {
            $sql = "DELETE ca FROM {$CFG->prefix}crlm_cluster_assignments ca\n                    LEFT JOIN {$CFG->prefix}crlm_cluster c ON c.id = ca.clusterid\n                    WHERE c.id IS NULL";
            $result = $result && execute_sql($sql);
            $sql = "DELETE cc FROM {$CFG->prefix}crlm_cluster_curriculum cc\n                    LEFT JOIN {$CFG->prefix}crlm_cluster c ON c.id = cc.clusterid\n                    WHERE c.id IS NULL";
            $result = $result && execute_sql($sql);
            $sql = "DELETE cp FROM {$CFG->prefix}crlm_cluster_profile cp\n                    LEFT JOIN {$CFG->prefix}crlm_cluster c ON c.id = cp.clusterid\n                    WHERE c.id IS NULL";
            $result = $result && execute_sql($sql);
            $sql = "DELETE ct FROM {$CFG->prefix}crlm_cluster_track ct\n                    LEFT JOIN {$CFG->prefix}crlm_cluster c ON c.id = ct.clusterid\n                    WHERE c.id IS NULL";
            $result = $result && execute_sql($sql);
            $sql = "DELETE uc FROM {$CFG->prefix}crlm_usercluster uc\n                    LEFT JOIN {$CFG->prefix}crlm_cluster c ON c.id = uc.clusterid\n                    WHERE c.id IS NULL";
            $result = $result && execute_sql($sql);
        }
    }
    if ($result && $oldversion < 2009010133) {
        /// Define field leader to be added to crlm_cluster_assignments
        $table = new XMLDBTable('crlm_cluster_assignments');
        $field = new XMLDBField('leader');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'autoenrol');
        /// Launch add field leader
        $result = $result && add_field($table, $field);
        /// Define field leader to be added to crlm_usercluster
        $table = new XMLDBTable('crlm_usercluster');
        $field = new XMLDBField('leader');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'autoenrol');
        /// Launch add field leader
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009010134) {
        /// Define field inactive to be added to crlm_user
        $table = new XMLDBTable('crlm_user');
        $field = new XMLDBField('inactive');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, '0', 'timemodified');
        /// Launch add field inactive
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009010137) {
        $roleid = get_field('role', 'id', 'shortname', 'curriculumadmin');
        if (!empty($roleid)) {
            $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
            require_once dirname(dirname(__FILE__)) . '/db/access.php';
            if (!empty($block_curr_admin_capabilities)) {
                foreach ($block_curr_admin_capabilities as $capname => $caprules) {
                    $result = $result && assign_capability($capname, CAP_ALLOW, $roleid, $context->id);
                }
            }
        }
    }
    if ($result && $oldversion < 2009010139) {
        global $CURMAN;
        require_once $CFG->dirroot . '/curriculum/lib/classmoodlecourse.class.php';
        $moodleclasses = moodle_get_classes();
        if (!empty($moodleclasses)) {
            foreach ($moodleclasses as $class) {
                $context = get_context_instance(CONTEXT_COURSE, $class->moodlecourseid);
                $sql = "DELETE cmce\n                    FROM {$CURMAN->db->prefix_table('user')} u\n                    JOIN {$CURMAN->db->prefix_table('role_assignments')} ra ON u.id = ra.userid\n                    JOIN {$CURMAN->db->prefix_table(STUTABLE)} cmce ON u.idnumber = cmce.user_idnumber\n                    WHERE ra.roleid NOT IN ({$CFG->gradebookroles})\n                    AND ra.contextid " . get_related_contexts_string($context) . "AND cmce.classid = {$class->classid}";
                $result = $result && execute_sql($sql);
            }
        }
    }
    if ($result && $oldversion < 2009010140) {
        delete_records('crlm_cluster_profile', 'fieldid', 0);
    }
    if ($result && $oldversion < 2009010141) {
        set_config('field_lock_idnumber', 'locked', 'auth/manual');
    }
    if ($result && $oldversion < 2009010143) {
        /// Define table crlm_wait_list to be created
        $table = new XMLDBTable('crlm_wait_list');
        /// Adding fields to table crlm_wait_list
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('classid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $table->addFieldInfo('position', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
        /// Adding keys to table crlm_wait_list
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for crlm_wait_list
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2009010145) {
        $table = new XMLDBTable('crlm_wait_list');
        $field = new XMLDBField('enrolmenttime');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009010146) {
        // make sure trackclass's courseids are set
        // Let's just assume that all non-Postgres DB's use the same syntax as MySQL and call it a day.
        if ($CFG->dbfamily == 'postgres') {
            $sql = "UPDATE {$CFG->prefix}crlm_track_class\n                       SET courseid = c.courseid\n                      FROM {$CFG->prefix}crlm_track_class tc, {$CFG->prefix}crlm_class c\n                     WHERE tc.classid = c.id AND tc.courseid = 0";
        } else {
            $sql = "UPDATE {$CFG->prefix}crlm_track_class tc, {$CFG->prefix}crlm_class c\n                       SET tc.courseid = c.courseid\n                     WHERE tc.classid = c.id AND tc.courseid = 0";
        }
        $result = $result && execute_sql($sql);
    }
    if ($result && $oldversion < 2009010147) {
        // make sure all users have an idnumber
        $users = get_records('crlm_user', 'idnumber', '');
        foreach ($users as $user) {
            $user = addslashes_recursive($user);
            $mu = addslashes_recursive(get_record('user', 'username', $user->username));
            if (empty($mu->idnumber)) {
                $user->idnumber = $mu->idnumber = $mu->username;
                update_record('user', $mu);
                update_record('crlm_user', $user);
            } else {
                if (!get_record('crlm_user', 'idnumber', $mu->idnumber)) {
                    $user->idnumber = $mu->idnumber;
                    update_record('crlm_user', $user);
                } else {
                    if (!get_record('crlm_user', 'idnumber', $user->username)) {
                        $user->idnumber = $mu->idnumber;
                        update_record('crlm_user', $user);
                    }
                }
            }
        }
    }
    if ($result && $oldversion < 2009010149) {
        /// Define index clusterid_idx (not unique) to be added to crlm_cluster_assignments
        $table = new XMLDBTable('crlm_cluster_assignments');
        $index = new XMLDBIndex('clusterid_idx');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('clusterid'));
        /// Launch add index clusterid_idx
        $result = $result && add_index($table, $index);
        /// Define index userid_idx (not unique) to be added to crlm_cluster_assignments
        $table = new XMLDBTable('crlm_cluster_assignments');
        $index = new XMLDBIndex('userid_idx');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('userid'));
        /// Launch add index userid_idx
        $result = $result && add_index($table, $index);
        /// Define index clusterid_idx (not unique) to be added to crlm_cluster_profile
        $table = new XMLDBTable('crlm_cluster_profile');
        $index = new XMLDBIndex('clusterid_idx');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('clusterid'));
        /// Launch add index clusterid_idx
        $result = $result && add_index($table, $index);
    }
    if ($result && $oldversion < 2009010151) {
        $table = new XMLDBTable('crlm_curriculum');
        $field = new XMLDBField('priority');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2009010150) {
        require_once CURMAN_DIRLOCATION . '/lib/curriculumcourse.class.php';
        $sql = "SELECT cp.id, cp.courseid, cc.curriculumid\n                FROM {$CFG->prefix}crlm_course_prerequisite cp\n                JOIN {$CFG->prefix}crlm_curriculum_course cc ON cc.id = cp.curriculumcourseid\n                WHERE cp.courseid NOT IN (\n                    SELECT _cc.courseid\n                    FROM {$CFG->prefix}crlm_curriculum_course _cc\n                    WHERE _cc.curriculumid = cc.curriculumid\n                )";
        $students = get_records_sql($sql);
        $retval = 0;
        foreach ($students as $student) {
            $data = new object();
            $data->curriculumid = $student->curriculumid;
            $data->courseid = $student->courseid;
            $data->timeperiod = 'year';
            $currprereq = new curriculumcourse($data);
            $retval = $result && $currprereq->add();
        }
        $results = $retval;
    }
    if ($result && $oldversion < 2009103001) {
        $table = new XMLDBTable('crlm_curriculum');
        $field = new XMLDBField('priority');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
        $result = $result && add_field($table, $field);
        /// Define table context_levels to be created
        $table = new XMLDBTable('context_levels');
        /// Adding fields to table context_levels
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('component', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table context_levels
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table context_levels
        $table->addIndexInfo('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
        $table->addIndexInfo('component', XMLDB_INDEX_NOTUNIQUE, array('component'));
        /// Launch create table for context_levels
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2009103003) {
        /// Define table crlm_field to be created
        $table = new XMLDBTable('crlm_field');
        /// Adding fields to table crlm_field
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('name', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('datatype', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
        $table->addFieldInfo('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('required', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('visible', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('forceunique', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('defaultdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
        $table->addFieldInfo('params', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
        $table->addFieldInfo('syncwithmoodle', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table crlm_field
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table crlm_field
        $table->addIndexInfo('shortname_idx', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
        /// Launch create table for crlm_field
        $result = $result && create_table($table);
        /// Define table crlm_field_category to be created
        $table = new XMLDBTable('crlm_field_category');
        /// Adding fields to table crlm_field_category
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table crlm_field_category
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for crlm_field_category
        $result = $result && create_table($table);
        /// Define table crlm_field_contextlevel to be created
        $table = new XMLDBTable('crlm_field_contextlevel');
        /// Adding fields to table crlm_field_contextlevel
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('fieldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table crlm_field_contextlevel
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for crlm_field_contextlevel
        $result = $result && create_table($table);
        /// Define table crlm_field_data to be created
        $table = new XMLDBTable('crlm_field_data');
        /// Adding fields to table crlm_field_data
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('fieldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('data', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table crlm_field_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table crlm_field_data
        $table->addIndexInfo('context_idx', XMLDB_INDEX_NOTUNIQUE, array('contextid'));
        /// Launch create table for crlm_field_data
        $result = $result && create_table($table);
    }
    //if ($result && $oldversion < 2010040501) {
    //    require_once($CFG->dirroot . '/blocks/curr_admin/lib.php');
    //    $result = $result && create_views(); // create with default prefix
    //    $result = $result && create_views(''); // create with no prefix
    //}
    if ($result && $oldversion < 2010040501) {
        $table = new XMLDBTable('crlm_field_map');
        /// Adding fields to table crlm_field_data
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('context', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('elis_field', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('data_field', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table crlm_field_data
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for crlm_field_data
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2010040501) {
        require_once "{$CFG->dirroot}/curriculum/lib/customfield.class.php";
        // make sure all ELIS users have a context
        update_capabilities('block/curr_admin');
        $ctxlvl = context_level_base::get_custom_context_level('user', 'block_curr_admin');
        $rs = get_recordset('crlm_user');
        while ($rec = rs_fetch_next_record($rs)) {
            get_context_instance($ctxlvl, $rec->id);
        }
        // sync profile fields
        $fields = field::get_for_context_level($ctxlvl);
        $fields = $fields ? $fields : array();
        require_once $CFG->dirroot . '/curriculum/plugins/moodle_profile/custom_fields.php';
        foreach ($fields as $field) {
            $fieldobj = new field($field);
            $sync_profile_field_with_moodle();
        }
    }
    if ($result && $oldversion < 2010040501) {
        require_once $CFG->dirroot . '/curriculum/lib/notifications.php';
        if (!empty($CFG->coursemanager)) {
            $context_course = CONTEXT_COURSE;
            $sql = "SELECT role_assignments.* FROM {$CFG->prefix}role_assignments role_assignments\n                    JOIN {$CFG->prefix}context context\n                    ON role_assignments.contextid = context.id\n                    WHERE role_assignments.roleid IN ({$CFG->coursemanager})\n                    AND context.contextlevel = {$context_course}";
            if ($records = get_records_sql($sql)) {
                foreach ($records as $record) {
                    cm_assign_instructor_from_mdl($record);
                }
            }
        }
    }
    if ($result && $oldversion < 2010063001) {
        $table = new XMLDBTable('crlm_curriculum_assignment');
        $field = new XMLDBField('user_idnumber');
        $result = $result && drop_field($table, $field);
        $table = new XMLDBTable('crlm_class_enrolment');
        $field = new XMLDBField('user_idnumber');
        $result = $result && drop_field($table, $field);
        $table = new XMLDBTable('crlm_class_instructor');
        $field = new XMLDBField('user_idnumber');
        $result = $result && drop_field($table, $field);
        $table = new XMLDBTable('crlm_class_attendance');
        $field = new XMLDBField('user_idnumber');
        $result = $result && drop_field($table, $field);
    }
    //if ($result && $oldversion < 2010063002) {
    //    require_once($CFG->dirroot . '/blocks/curr_admin/lib.php');
    //    $result = $result && create_views(); // create with default prefix
    //    $result = $result && create_views(''); // create with no prefix
    //}
    if ($result && $oldversion < 2010040505) {
        require_once $CFG->dirroot . '/curriculum/lib/lib.php';
        $result = $result && cm_notify_duplicate_user_info(true);
    }
    if ($result && $oldversion < 2010040506 && $oldversion >= 2010040501) {
        global $CFG, $CURMAN;
        // fix instructor assignments that were migrated incorrectly in the
        // 2010040501 upgrade code (ELIS-1171)
        // remove the obvious errors (instructors assigned to a non-existent class)
        $context_course = CONTEXT_COURSE;
        $sql = "DELETE\n                  FROM {$CFG->prefix}crlm_class_instructor\n                 WHERE NOT EXISTS (SELECT 'x' FROM {$CFG->prefix}crlm_class cmclass\n                                    WHERE cmclass.id = {$CFG->prefix}crlm_class_instructor.classid)";
        $result = $result && execute_sql($sql);
        // warn about other potentially incorrect instructor assignments
        require_once $CFG->dirroot . '/curriculum/lib/lib.php';
        cm_notify_incorrect_instructor_assignment(true);
        // make sure the correct assignments are added
        if (!empty($CFG->coursemanager)) {
            require_once $CFG->dirroot . '/curriculum/lib/notifications.php';
            $context_course = CONTEXT_COURSE;
            $sql = "SELECT role_assignments.* FROM {$CFG->prefix}role_assignments role_assignments\n                    JOIN {$CFG->prefix}context context\n                    ON role_assignments.contextid = context.id\n                    WHERE role_assignments.roleid IN ({$CFG->coursemanager})\n                    AND context.contextlevel = {$context_course}";
            if ($records = get_records_sql($sql)) {
                foreach ($records as $record) {
                    cm_assign_instructor_from_mdl($record);
                }
            }
        }
    }
    if ($result && $oldversion < 2010063002) {
        //get the class table
        $table = new XMLDBTable('crlm_class');
        //add the auto enrol enabled flag
        $field = new XMLDBField('enrol_from_waitlist');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, '0');
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2010063005) {
        /// Define table crlm_field_data to be dropped
        $table = new XMLDBTable('crlm_field_map');
        /// Launch drop table for crlm_field_data
        $result = $result && drop_table($table);
    }
    if ($result && $oldversion < 2010063006) {
        /// Define table crlm_field_data to be renamed to crlm_field_data_text
        $table = new XMLDBTable('crlm_field_data');
        /// Define index context_idx (not unique) to be dropped form crlm_field_data_text
        $index = new XMLDBIndex('context_idx');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('contextid'));
        /// Launch drop index context_idx
        $result = $result && drop_index($table, $index);
        /// Changing nullability of field contextid on table crlm_field_data_text to null
        $field = new XMLDBField('contextid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'id');
        /// Define index context_idx (not unique) to be added to crlm_field_data_text
        $index = new XMLDBIndex('context_idx');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('contextid'));
        /// Launch add index context_idx
        $result = $result && add_index($table, $index);
        /// Define index field_idx (not unique) to be added to crlm_field_data_text
        $index = new XMLDBIndex('field_idx');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('fieldid'));
        /// Launch add index field_idx
        $result = $result && add_index($table, $index);
        /// Launch change of nullability for field contextid
        $result = $result && change_field_notnull($table, $field);
        /// Launch rename table for crlm_field_data
        $result = $result && rename_table($table, 'crlm_field_data_text');
        /// Define table crlm_field_owner to be created
        $table = new XMLDBTable('crlm_field_owner');
        /// Adding fields to table crlm_field_owner
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('fieldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
        $table->addFieldInfo('plugin', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
        $table->addFieldInfo('exclude', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, '0');
        $table->addFieldInfo('params', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
        /// Adding keys to table crlm_field_owner
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table crlm_field_owner
        $table->addIndexInfo('field_idx', XMLDB_INDEX_NOTUNIQUE, array('fieldid'));
        /// Launch create table for crlm_field_owner
        $result = $result && create_table($table);
        /// Define table crlm_field_category_context to be created
        $table = new XMLDBTable('crlm_field_category_context');
        /// Adding fields to table crlm_field_category_context
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
        $table->addFieldInfo('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
        /// Adding keys to table crlm_field_category_context
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table crlm_field_category_context
        $table->addIndexInfo('contextlevel_idx', XMLDB_INDEX_NOTUNIQUE, array('contextlevel'));
        $table->addIndexInfo('category_idx', XMLDB_INDEX_NOTUNIQUE, array('categoryid'));
        /// Launch create table for crlm_field_category_context
        $result = $result && create_table($table);
        $usercontextid = context_level_base::get_custom_context_level('user', 'block_curr_admin');
        if ($usercontextid) {
            $sql = "INSERT INTO {$CFG->prefix}crlm_field_category_context\n                           (categoryid, contextlevel)\n                    SELECT id, {$usercontextid}\n                      FROM {$CFG->prefix}crlm_field_category";
            $result = $result && execute_sql($sql);
        }
        /// Define table crlm_field_data_int to be created
        $table = new XMLDBTable('crlm_field_data_int');
        /// Adding fields to table crlm_field_data_int
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
        $table->addFieldInfo('fieldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('data', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table crlm_field_data_int
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table crlm_field_data_int
        $table->addIndexInfo('context_idx', XMLDB_INDEX_NOTUNIQUE, array('contextid'));
        $table->addIndexInfo('field_idx', XMLDB_INDEX_NOTUNIQUE, array('fieldid'));
        /// Launch create table for crlm_field_data_int
        $result = $result && create_table($table);
        /// Define table crlm_field_data_num to be created
        $table = new XMLDBTable('crlm_field_data_num');
        /// Adding fields to table crlm_field_data_num
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
        $table->addFieldInfo('fieldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('data', XMLDB_TYPE_NUMBER, '15, 5', null, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table crlm_field_data_num
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table crlm_field_data_num
        $table->addIndexInfo('context_idx', XMLDB_INDEX_NOTUNIQUE, array('contextid'));
        $table->addIndexInfo('field_idx', XMLDB_INDEX_NOTUNIQUE, array('fieldid'));
        /// Launch create table for crlm_field_data_num
        $result = $result && create_table($table);
        /// Define table crlm_field_data_char to be created
        $table = new XMLDBTable('crlm_field_data_char');
        /// Adding fields to table crlm_field_data_char
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
        $table->addFieldInfo('fieldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('data', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table crlm_field_data_char
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Adding indexes to table crlm_field_data_char
        $table->addIndexInfo('context_idx', XMLDB_INDEX_NOTUNIQUE, array('contextid'));
        $table->addIndexInfo('field_idx', XMLDB_INDEX_NOTUNIQUE, array('fieldid'));
        /// Launch create table for crlm_field_data_char
        $result = $result && create_table($table);
        $records = get_records('crlm_field');
        if ($records) {
            // FIXME: set data type based on old data type
            foreach ($records as $record) {
                unset($record->name);
                unset($record->shortname);
                unset($record->description);
                $record->defaultdata = addslashes($record->defaultdata);
                if (isset($record->syncwithmoodle)) {
                    // make sure the crlm_field table hasn't been upgraded yet
                    switch ($record->syncwithmoodle) {
                        case 2:
                            // sync from Moodle
                            // create "moodle_profile" owner
                            if (!record_exists('crlm_field_owner', 'fieldid', $record->id, 'plugin', 'moodle_profile')) {
                                $owner = new stdClass();
                                $owner->fieldid = $record->id;
                                $owner->plugin = 'moodle_profile';
                                $owner->exclude = true;
                                $result = $result && insert_record('crlm_field_owner', $owner);
                            }
                            // create "manual" owner
                            if (!record_exists('crlm_field_owner', 'fieldid', $record->id, 'plugin', 'manual')) {
                                $owner = new stdClass();
                                $owner->fieldid = $record->id;
                                $owner->plugin = 'manual';
                                $owner->exclude = false;
                                $owner->params = array('edit_capability' => 'disabled');
                                if (!$record->visible) {
                                    $owner->params['view_capability'] = 'moodle/user:viewhiddendetails';
                                }
                                $owner->params = serialize($owner->params);
                                $result = $result && insert_record('crlm_field_owner', $owner);
                            }
                            $record->datatype = 'text';
                            break;
                        case 1:
                            // sync to Moodle
                            // create "moodle_profile" owner
                            if (!record_exists('crlm_field_owner', 'fieldid', $record->id, 'plugin', 'moodle_profile')) {
                                $owner = new stdClass();
                                $owner->fieldid = $record->id;
                                $owner->plugin = 'moodle_profile';
                                $owner->exclude = false;
                                $result = $result && insert_record('crlm_field_owner', $owner);
                            }
                            // NOTE: fall through
                        // NOTE: fall through
                        default:
                            // no sync or invalid user
                            // create "manual" owner
                            $controltype = $record->datatype;
                            $record->datatype = 'text';
                            if (!record_exists('crlm_field_owner', 'fieldid', $record->id, 'plugin', 'manual')) {
                                $owner = new stdClass();
                                $owner->fieldid = $record->id;
                                $owner->plugin = 'manual';
                                $owner->exclude = false;
                                $owner->params = array('control' => $controltype, 'required' => $record->required);
                                if ($record->locked) {
                                    $owner->params['edit_capability'] = 'moodle/user:update';
                                }
                                if (!$record->visible) {
                                    $owner->params['view_capability'] = 'moodle/user:viewhiddendetails';
                                }
                                if (!empty($record->params)) {
                                    $owner->params += unserialize($record->params);
                                }
                                switch ($controltype) {
                                    case 'checkbox':
                                        // legacy checkboxes are all boolean
                                        $record->datatype = 'bool';
                                        $data_recs = get_records('crlm_field_data_text', 'fieldid', $record->id);
                                        foreach ($data_recs as $data_rec) {
                                            delete_records('crlm_field_data_text', 'id', $data_rec->id);
                                            unset($data_rec->id);
                                            insert_record('crlm_field_data_int', $data_rec);
                                        }
                                        break;
                                    case 'menu':
                                        // menu items should be short text
                                        $record->datatype = 'char';
                                        $data_recs = get_records('crlm_field_data_text', 'fieldid', $record->id);
                                        foreach ($data_recs as $data_rec) {
                                            delete_records('crlm_field_data_text', 'id', $data_rec->id);
                                            unset($data_rec->id);
                                            insert_record('crlm_field_data_char', $data_rec);
                                        }
                                    case 'text':
                                        $owner->params['columns'] = $owner->params['size'];
                                        unset($owner->params['size']);
                                        break;
                                }
                                $owner->params = addslashes(serialize($owner->params));
                                $result = $result && insert_record('crlm_field_owner', $owner);
                            }
                            break;
                    }
                    $record->params = '';
                    $result = $result && update_record('crlm_field', $record);
                    if (!empty($record->defaultdata)) {
                        if (!record_exists_select('crlm_field_data_text', "fieldid = {$record->id} AND contextid IS NULL")) {
                            $defaultdata = new stdClass();
                            $defaultdata->fieldid = $record->id;
                            $defaultdata->data = $record->defaultdata;
                            $result = $result && insert_record('crlm_field_data_text', $defaultdata);
                        }
                    }
                }
            }
        }
        $table = new XMLDBTable('crlm_field');
        /// Define field required to be dropped from crlm_field
        $field = new XMLDBField('required');
        /// Launch drop field required
        $result = $result && drop_field($table, $field);
        /// Define field locked to be dropped from crlm_field
        $field = new XMLDBField('locked');
        /// Launch drop field locked
        $result = $result && drop_field($table, $field);
        /// Define field visible to be dropped from crlm_field
        $field = new XMLDBField('visible');
        /// Launch drop field visible
        $result = $result && drop_field($table, $field);
        /// Define field defaultdata to be dropped from crlm_field
        $field = new XMLDBField('defaultdata');
        /// Launch drop field defaultdata
        $result = $result && drop_field($table, $field);
        /// Define field syncwithmoodle to be dropped from crlm_field
        $field = new XMLDBField('syncwithmoodle');
        /// Launch drop field syncwithmoodle
        $result = $result && drop_field($table, $field);
        /// Define field multivalued to be added to crlm_field
        $field = new XMLDBField('multivalued');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, '0', 'sortorder');
        /// Launch add field multivalued
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2010063007) {
        // install.xml accidentally had the char table use an integer data field
        /// Changing type of field data on table crlm_field_data_char to char
        $table = new XMLDBTable('crlm_field_data_char');
        $field = new XMLDBField('data');
        $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'fieldid');
        /// Launch change of type for field data
        $result = $result && change_field_type($table, $field);
    }
    if ($result && $oldversion < 2010063008) {
        $table = new XMLDBTable('crlm_cluster_curriculum');
        $field = new XMLDBField('autoenrol');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'curriculumid');
        $result = $result && add_field($table, $field);
        $table = new XMLDBTable('crlm_cluster_track');
        $field = new XMLDBField('autoenrol');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'trackid');
        $result = $result && add_field($table, $field);
        /// Define field parent to be added to crlm_cluster
        $table = new XMLDBTable('crlm_cluster');
        $field = new XMLDBField('parent');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'display');
        /// Launch add field parent
        $result = $result && add_field($table, $field);
        /// Define field depth to be added to crlm_cluster
        $field = new XMLDBField('depth');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'parent');
        /// Launch add field depth
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2010063013) {
        /*
         * Curriculum
         */
        $table = new XMLDBTable('crlm_curriculum');
        //name field
        $index = new XMLDBIndex('name_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('name'));
        if (!index_exists($table, $index)) {
            $result = $result && add_index($table, $index);
        }
        /*
         * Course
         */
        $table = new XMLDBTable('crlm_course');
        //name field
        $index = new XMLDBIndex('name_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('name'));
        if (!index_exists($table, $index)) {
            $result = $result && add_index($table, $index);
        }
        //credits field
        $index = new XMLDBIndex('credits_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('credits'));
        if (!index_exists($table, $index)) {
            $result = $result && add_index($table, $index);
        }
        /*
         * Class
         */
        $table = new XMLDBTable('crlm_class');
        //idnumber field
        $index = new XMLDBIndex('idnumber_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
        if (!index_exists($table, $index)) {
            $result = $result && add_index($table, $index);
        }
        //enddate field
        $index = new XMLDBIndex('enddate_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('enddate'));
        if (!index_exists($table, $index)) {
            $result = $result && add_index($table, $index);
        }
        /*
         * Class enrolment
         */
        $table = new XMLDBTable('crlm_class_enrolment');
        //completetime field
        $index = new XMLDBIndex('completetime_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('completetime'));
        if (!index_exists($table, $index)) {
            $result = $result && add_index($table, $index);
        }
        //completestatusid field
        $index = new XMLDBIndex('completestatusid_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('completestatusid'));
        if (!index_exists($table, $index)) {
            $result = $result && add_index($table, $index);
        }
        /*
         * CM user
         */
        $table = new XMLDBTable('crlm_user');
        //lastname field
        $index = new XMLDBIndex('lastname_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('lastname'));
        if (!index_exists($table, $index)) {
            $result = $result && add_index($table, $index);
        }
        //firstname field
        $index = new XMLDBIndex('firstname_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('firstname'));
        if (!index_exists($table, $index)) {
            $result = $result && add_index($table, $index);
        }
    }
    if ($result && $oldversion < 2010063015) {
        /// Define field autocreated to be added to crlm_class_moodle
        $table = new XMLDBTable('crlm_class_moodle');
        $field = new XMLDBField('autocreated');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null, null, '-1', 'timemodified');
        /// Launch add field autocreated
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2010111300) {
        $table = new XMLDBTable('crlm_curriculum_assignment');
        $field = new XMLDBField('timeexpired');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'timecompleted');
        // Launch add field multivalued
        $result = $result && add_field($table, $field);
    }
    if ($result && $oldversion < 2011050200) {
        /// Define index startdate_ix (not unique) to be added to crlm_class
        $table = new XMLDBTable('crlm_class');
        $index = new XMLDBIndex('startdate_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('startdate'));
        $result = $result && add_index($table, $index);
        /// Define index enrolmenttime_ix (not unique) to be added to crlm_class_enrolment
        $table = new XMLDBTable('crlm_class_enrolment');
        $index = new XMLDBIndex('enrolmenttime_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('enrolmenttime'));
        $result = $result && add_index($table, $index);
        /// Define index locked_ix (not unique) to be added to crlm_class_graded
        $table = new XMLDBTable('crlm_class_graded');
        $index = new XMLDBIndex('locked_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('locked'));
        $result = $result && add_index($table, $index);
        /// Define index timegraded_ix (not unique) to be added to crlm_class_graded
        $table = new XMLDBTable('crlm_class_graded');
        $index = new XMLDBIndex('timegraded_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('timegraded'));
        $result = $result && add_index($table, $index);
        /// Define index classid_ix (not unique) to be added to crlm_class_moodle
        $table = new XMLDBTable('crlm_class_moodle');
        $index = new XMLDBIndex('classid_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('classid'));
        $result = $result && add_index($table, $index);
        /// Define index curriculumid_ix (not unique) to be added to crlm_cluster_curriculum
        $table = new XMLDBTable('crlm_cluster_curriculum');
        $index = new XMLDBIndex('curriculumid_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('curriculumid'));
        $result = $result && add_index($table, $index);
        /// Define index fieldid_ix (not unique) to be added to crlm_cluster_profile
        $table = new XMLDBTable('crlm_cluster_profile');
        $index = new XMLDBIndex('fieldid_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('fieldid'));
        $result = $result && add_index($table, $index);
        /// Define index trackid_ix (not unique) to be added to crlm_cluster_track
        $table = new XMLDBTable('crlm_cluster_track');
        $index = new XMLDBIndex('trackid_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('trackid'));
        $result = $result && add_index($table, $index);
        /// Define index idnumber_ix (not unique) to be added to crlm_course_completion
        $table = new XMLDBTable('crlm_course_completion');
        $index = new XMLDBIndex('idnumber_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
        $result = $result && add_index($table, $index);
        /// Define index sortorder_ix (not unique) to be added to crlm_field
        $table = new XMLDBTable('crlm_field');
        $index = new XMLDBIndex('sortorder_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('sortorder'));
        $result = $result && add_index($table, $index);
        /// Define index username_ix (not unique) to be added to crlm_user
        $table = new XMLDBTable('crlm_user');
        $index = new XMLDBIndex('username_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('username'));
        $result = $result && add_index($table, $index);
        /// Define index inactive_ix (not unique) to be added to crlm_user
        $table = new XMLDBTable('crlm_user');
        $index = new XMLDBIndex('inactive_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('inactive'));
        $result = $result && add_index($table, $index);
        /// Define index userid_ix (not unique) to be added to crlm_user_track
        $table = new XMLDBTable('crlm_user_track');
        $index = new XMLDBIndex('userid_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('userid'));
        $result = $result && add_index($table, $index);
        /// Define index trackid_ix (not unique) to be added to crlm_user_track
        $table = new XMLDBTable('crlm_user_track');
        $index = new XMLDBIndex('trackid_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('trackid'));
        $result = $result && add_index($table, $index);
        /// Define index classid_ix (not unique) to be added to crlm_wait_list
        $table = new XMLDBTable('crlm_wait_list');
        $index = new XMLDBIndex('classid_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('classid'));
        $result = $result && add_index($table, $index);
        /// Define index userid_ix (not unique) to be added to crlm_wait_list
        $table = new XMLDBTable('crlm_wait_list');
        $index = new XMLDBIndex('userid_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('userid'));
        $result = $result && add_index($table, $index);
    }
    if ($result && $oldversion < 2011050201) {
        // make sure that hours are within 24 hours
        $sql = "UPDATE {$CFG->prefix}crlm_class\n                   SET starttimehour = MOD(starttimehour, 24),\n                       endtimehour = MOD(endtimehour, 24)";
        $result = $result && execute_sql($sql);
    }
    if ($result && $oldversion < 2011050202) {
        /// Changing type of field credits on table crlm_class_enrolment to number
        $table = new XMLDBTable('crlm_class_enrolment');
        $field = new XMLDBField('credits');
        $field->setAttributes(XMLDB_TYPE_NUMBER, '10, 2', XMLDB_UNSIGNED, null, null, null, null, '0', 'grade');
        /// Launch change of type for field credits
        $result = $result && change_field_type($table, $field);
        /// Changing type of field credits on table crlm_curriculum_assignment to number
        $table = new XMLDBTable('crlm_curriculum_assignment');
        $field = new XMLDBField('credits');
        $field->setAttributes(XMLDB_TYPE_NUMBER, '10, 2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'timeexpired');
        /// Launch change of type for field credits
        $result = $result && change_field_type($table, $field);
        /// Changing type of field reqcredits on table crlm_curriculum to number
        $table = new XMLDBTable('crlm_curriculum');
        $field = new XMLDBField('reqcredits');
        $field->setAttributes(XMLDB_TYPE_NUMBER, '10, 2', XMLDB_UNSIGNED, null, null, null, null, null, 'description');
        /// Launch change of type for field reqcredits
        $result = $result && change_field_type($table, $field);
        // update student class credits with decimal credits
        if ($CFG->dbfamily == 'postgres') {
            $sql = "UPDATE {$CFG->prefix}crlm_class_enrolment\n                       SET credits = CAST(c.credits AS numeric)\n                      FROM {$CFG->prefix}crlm_class_enrolment e, {$CFG->prefix}crlm_class cls, {$CFG->prefix}crlm_course c\n                     WHERE e.classid = cls.id\n                       AND cls.courseid = c.id\n                       AND e.credits = CAST(c.credits AS integer)";
        } else {
            $sql = "UPDATE {$CFG->prefix}crlm_class_enrolment e, {$CFG->prefix}crlm_class cls, {$CFG->prefix}crlm_course c\n                       SET e.credits = c.credits\n                     WHERE e.classid = cls.id\n                       AND cls.courseid = c.id\n                       AND e.credits = CAST(c.credits AS unsigned)";
        }
        $result = $result && execute_sql($sql);
    }
    if ($result && $oldversion < 2011050203) {
        //create a new column in the notification log table
        //to store the user who triggered the notification
        $table = new XMLDBTable('crlm_notification_log');
        $field = new XMLDBField('fromuserid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0, 'userid');
        $field->comment = 'CM user id that triggered the notification.';
        $result = $result && add_field($table, $field);
        //populate data, assuming that the user who received the notification is the one whose
        //criteria spawned it
        //NOTE: this fudges data and the side-effect implies that if someone had received a notification
        //for another user and satisfy the same criteria for the same instance for themself, they will not
        //receive a similar notification
        $sql = "UPDATE {$CFG->prefix}crlm_notification_log\n                SET fromuserid = userid";
        $result = $result && execute_sql($sql);
        if ($result) {
            /// Define field certificatecode to be added to crlm_curriculum_assignment
            $table = new XMLDBTable('crlm_curriculum_assignment');
            $field = new XMLDBField('certificatecode');
            $field->setAttributes(XMLDB_TYPE_CHAR, '40', null, null, null, null, null, null, 'locked');
            /// Launch add field autocreated
            $result = $result && add_field($table, $field);
            /// Define index userid_ix (not unique) to be added to crlm_wait_list
            $index = new XMLDBIndex('certificatecode_ix');
            $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('certificatecode'));
            $result = $result && add_index($table, $index);
        }
    }
    if ($result && $oldversion < 2011050204) {
        $table = new XMLDBTable('crlm_notification_log');
        $index = new XMLDBIndex('event_inst_fuser_ix');
        $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('fromuserid', 'instance', 'event'));
        $result = add_index($table, $index);
    }
    if ($result && $oldversion < 2011050205) {
        // Update the ELIS class enrolment grade value and completion score grade values so they store float
        // (decimal) values
        $table = new XMLDBTable('crlm_class_enrolment');
        $field = new XMLDBField('grade');
        $field->setAttributes(XMLDB_TYPE_NUMBER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
        $field->setDecimals(5);
        $result = change_field_type($table, $field);
        $table = new XMLDBTable('crlm_class_graded');
        $result = $result && change_field_type($table, $field);
        /*
         * Find all of the completion grades that are synchronised from Moodle grade items that are not locked and
         * where the ELIS completion score does not match the value in the Moodle gradebook and delete those
         * completion scores so they can be re-synchronised from Moodle with the correct float values stored.
         */
        // Attempt to handle different DBs in the most efficient way possible
        if ($CFG->dbfamily == 'postgres') {
            $sql = "DELETE FROM {$CFG->prefix}crlm_class_graded\n                    WHERE id IN (\n                        SELECT ccg.id\n                        FROM mdl_crlm_user cu\n                        INNER JOIN {$CFG->prefix}crlm_class_enrolment cce ON cce.userid = cu.id\n                        INNER JOIN {$CFG->prefix}crlm_class_graded ccg ON (ccg.userid = cce.userid AND ccg.classid = cce.classid)\n                        INNER JOIN {$CFG->prefix}crlm_course_completion ccc ON ccc.id = ccg.completionid\n                        INNER JOIN {$CFG->prefix}crlm_class_moodle ccm ON ccm.classid = ccg.classid\n                        INNER JOIN {$CFG->prefix}user u ON u.idnumber = cu.idnumber\n                        INNER JOIN {$CFG->prefix}course c ON c.id = ccm.moodlecourseid\n                        INNER JOIN {$CFG->prefix}grade_items gi ON (gi.courseid = c.id AND gi.idnumber = ccc.idnumber)\n                        INNER JOIN {$CFG->prefix}grade_grades gg ON (gg.itemid = gi.id AND gg.userid = u.id)\n                        WHERE ccg.locked = 0\n                        AND ccc.idnumber != ''\n                        AND gi.itemtype != 'course'\n                        AND ccg.grade != gg.finalgrade\n                        AND gg.finalgrade IS NOT NULL\n                    )";
            $result = $result && execute_sql($sql);
        } else {
            if ($CFG->dbfamily == 'mysql') {
                $sql = "DELETE ccg\n                    FROM mdl_crlm_user cu\n                    INNER JOIN {$CFG->prefix}crlm_class_enrolment cce ON cce.userid = cu.id\n                    INNER JOIN {$CFG->prefix}crlm_class_graded ccg ON (ccg.userid = cce.userid AND ccg.classid = cce.classid)\n                    INNER JOIN {$CFG->prefix}crlm_course_completion ccc ON ccc.id = ccg.completionid\n                    INNER JOIN {$CFG->prefix}crlm_class_moodle ccm ON ccm.classid = ccg.classid\n                    INNER JOIN {$CFG->prefix}user u ON u.idnumber = cu.idnumber\n                    INNER JOIN {$CFG->prefix}course c ON c.id = ccm.moodlecourseid\n                    INNER JOIN {$CFG->prefix}grade_items gi ON (gi.courseid = c.id AND gi.idnumber = ccc.idnumber)\n                    INNER JOIN {$CFG->prefix}grade_grades gg ON (gg.itemid = gi.id AND gg.userid = u.id)\n                    WHERE ccg.locked = 0\n                    AND ccc.idnumber != ''\n                    AND gi.itemtype != 'course'\n                    AND ccg.grade != gg.finalgrade\n                    AND gg.finalgrade IS NOT NULL";
                $result = $result && execute_sql($sql);
            } else {
                $sql = "SELECT ccg.id, ccg.grade\n                    FROM mdl_crlm_user cu\n                    INNER JOIN {$CFG->prefix}crlm_class_enrolment cce ON cce.userid = cu.id\n                    INNER JOIN {$CFG->prefix}crlm_class_graded ccg ON (ccg.userid = cce.userid AND ccg.classid = cce.classid)\n                    INNER JOIN {$CFG->prefix}crlm_course_completion ccc ON ccc.id = ccg.completionid\n                    INNER JOIN {$CFG->prefix}crlm_class_moodle ccm ON ccm.classid = ccg.classid\n                    INNER JOIN {$CFG->prefix}user u ON u.idnumber = cu.idnumber\n                    INNER JOIN {$CFG->prefix}course c ON c.id = ccm.moodlecourseid\n                    INNER JOIN {$CFG->prefix}grade_items gi ON (gi.courseid = c.id AND gi.idnumber = ccc.idnumber)\n                    INNER JOIN {$CFG->prefix}grade_grades gg ON (gg.itemid = gi.id AND gg.userid = u.id)\n                    WHERE ccg.locked = 0\n                    AND ccc.idnumber != ''\n                    AND gi.itemtype != 'course'\n                    AND ccg.grade != gg.finalgrade\n                    AND gg.finalgrade IS NOT NULL";
                if ($rs = get_recordset_sql($sql)) {
                    while ($cg = rs_fetch_next_record($rs)) {
                        $result = $result && delete_records('crlm_class_graded', 'id', $cg->id);
                    }
                    rs_close($rs);
                }
            }
        }
        // Force a re-synchronization of ELIS class grade data
        require_once $CFG->dirroot . '/curriculum/lib/lib.php';
        cm_synchronize_moodle_class_grades();
    }
    if ($result && $oldversion < 2011050206) {
        /// Define table crlm_field_data to be dropped
        $table = new XMLDBTable('crlm_field_map');
        if (table_exists($table)) {
            /// Launch drop table for crlm_field_data
            $result = drop_table($table);
        }
    }
    if ($result && $oldversion < 2011050207) {
        // Delete duplicate class completion element grades
        $xmldbtable = new XMLDBTable('crlm_class_graded_temp');
        if (table_exists($xmldbtable)) {
            drop_table($xmldbtable);
        }
        // Create a temporary table
        $result = $result && execute_sql("CREATE TABLE {$CFG->prefix}crlm_class_graded_temp LIKE {$CFG->prefix}crlm_class_graded");
        // Store the unique values in the temporary table
        $sql = "INSERT INTO {$CFG->prefix}crlm_class_graded_temp\n                SELECT MAX(id) AS id, classid, userid, completionid, grade, locked, timegraded, timemodified\n                FROM {$CFG->prefix}crlm_class_graded\n                GROUP BY classid, userid, completionid, locked";
        $result = $result && execute_sql($sql);
        // Detect if there are still duplicates in the temporary table
        $sql = "SELECT COUNT(*) AS count, classid, userid, completionid, grade, locked, timegraded, timemodified\n                FROM {$CFG->prefix}crlm_class_graded_temp\n                GROUP BY classid, userid, completionid\n                ORDER BY count DESC, classid ASC, userid ASC, completionid ASC";
        if ($result && ($dupcount = get_record_sql($sql, true))) {
            if ($dupcount->count > 1) {
                if ($rs = get_recordset_sql($sql)) {
                    while ($result && ($dupe = rs_fetch_next_record($rs))) {
                        if ($dupe->count <= 1) {
                            continue;
                        }
                        $classid = $dupe->classid;
                        $userid = $dupe->userid;
                        $goodid = 0;
                        // The ID of the record we will keep
                        // Look for the earliest locked grade record for this user and keep that (if any are locked)
                        $sql2 = "SELECT id, grade, locked, timegraded\n                                 FROM mdl_crlm_class_graded\n                                 WHERE classid = {$classid}\n                                 AND userid = {$userid}\n                                 ORDER BY timegraded ASC";
                        if ($rs2 = get_recordset_sql($sql2)) {
                            while ($rec = rs_fetch_next_record($rs2)) {
                                // Store the last record ID just in case we need it for cleanup
                                $lastid = $rec->id;
                                // Don't bother looking at remaining records if we have found a record to keep
                                if (!empty($goodid)) {
                                    continue;
                                }
                                if ($rec->locked = 1) {
                                    $goodid = $rec->id;
                                }
                            }
                            rs_close($rs2);
                            // We need to make sure we have a record ID to keep, if we found no "complete" and locked
                            // records, let's just keep the last record we saw
                            if (empty($goodid)) {
                                $goodid = $lastid;
                            }
                            $select = 'classid = ' . $classid . ' AND userid = ' . $userid . ' AND id != ' . $goodid;
                        }
                        if (!empty($select)) {
                            $result = $result && delete_records_select('crlm_class_graded_temp', $select);
                        }
                    }
                }
            }
        }
        // Drop the real table
        $result = $result && execute_sql("DROP TABLE {$CFG->prefix}crlm_class_graded");
        // Replace the real table with the temporary table that now only contains unique values.
        $result = $result && execute_sql("ALTER TABLE {$CFG->prefix}crlm_class_graded_temp RENAME TO {$CFG->prefix}crlm_class_graded");
    }
    return $result;
}