<?php
  require_once ($CFG->libdir.'/ddllib.php');

  function xmldb_newmodule_upgrade($oldversion=0) {
      global $CFG, $report, $db;
      $result = true;
      
      // No database found
      if ($result && $oldversion <= 0) {
          /// Define table census to be created
              $table = new XMLDBTable('census');
          
          /// Adding fields to table census
              $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
              $table->addFieldInfo('version', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, $report->version);
              $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, null, 'census');
              $table->addFieldInfo('allow_remote_reports', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
              $table->addFieldInfo('remote_hash', XMLDB_TYPE_CHAR, '32', null, null, null, null, null. null);
                                
          /// Adding keys to table census
              $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
                                   
          /// Launch create table for census
              $result = $result && create_table($table);
          
          /// Create default row in database
              $record = new stdClass;
              $record->name = 'census';
              $record->version = $report->version;
              $result = insert_record('census', $record);
      }
      
      if ($result && $oldversion < '2009060300') {
      	  /// Add remote system field to database
      	      $table = new XMLDBTable('census');
      	      $field = new XMLDBField('remote_server_name');
      	      $field->setAttributes(XMLDB_TYPE_CHAR, '120', null, XMLDB_NULL, null, null, null, 'http://');
      	      $result = $result && add_field($table, $field);	
      	      
      	  /// Update database version
      	      $record = new stdClass;
      	      $record->id = 1;
      	      $record->name = 'census';
      	      $record->version = $report->version;
      	      $result = update_record('census', $record);      	  
      }

      if ($result && $oldversion < '2009060301') {
          /// Add role storage
              $table = new XMLDBTable('census');
              $field = new XMLDBField('teacherroles');
              $field->setAttributes(XMLDB_TYPE_CHAR, '32', null, XMLDB_NULL, null, null, null, '3');
              $result = $result && add_field($table, $field);
              
              $field = new XMLDBField('studentroles');
              $field->setAttributes(XMLDB_TYPE_CHAR, '32', null, XMLDB_NULL, null, null, null, '5');
              $result = $result && add_field($table, $field);
              
          /// Update database version
              $record = new stdClass;
              $record->id = 1;
              $record->name = 'census';
              $record->version = $report->version;
              $result = update_record('census', $record);  
      }
      
      return $result;
  }
?>
