public function create()
 {
     if (!($institution = $this->getInstitution())) {
         global $CFG;
         $CFG->current_app->gcError('Creator institution for new eSchool ' . $this->short_name . ' not found', 'gcdatabaseerror');
     } else {
         if (!($mhr_owner = $this->getInstitution()->getOwnerUser())) {
             global $CFG;
             $CFG->current_app->gcError('Institution owner of ' . $institution->getShortName() . ' not found', 'gcdatabaseerror');
         }
     }
     GcrDatabaseAccessPostgres::createSchema($this);
     $eschool = $this->getAppType()->getTemplateObject();
     // add the template's salt to the list of historical salts for the new eschool
     $salt_history = new GcrEschoolSaltHistory();
     $salt_history->setEschoolid($this->id);
     $salt_history->setSalt($eschool->password_salt);
     $salt_history->save();
     // transfer all old salts from template to new eschool
     if ($old_salts = Doctrine::getTable('GcrEschoolSaltHistory')->findByEschoolid($eschool->id)) {
         foreach ($old_salts as $salt_record) {
             $salt_history = new GcrEschoolSaltHistory();
             $salt_history->setEschoolid($this->id);
             $salt_history->setSalt($salt_record->salt);
             $salt_history->save();
         }
     }
     // change the title name of the eschool from Eschool Template to whatever its called
     $this->updateMdlTable('course', array('fullname' => $this->full_name, 'shortname' => $this->short_name), array('id' => 1));
     // change the self-reference entry in mdl_mnet_host to the new eschool's info
     $this->updateMdlTable('mnet_host', array('name' => $this->full_name), array('wwwroot' => $this->getAppUrl()));
     // change the gcadmin username and password to the generated one.
     $password = md5($this->admin_password);
     $this->updateMdlTable('user', array('password' => $password), array('username' => "gc4{$this->short_name}admin"));
     // Change the web services token for gc4<schema>admin to a new uniue value.
     $this->updateMdlTable('external_tokens', array('token' => md5(uniqid(rand(), 1))), array('userid' => $this->getGCAdminUser()->getObject()->id));
     $this->setMnetConnection();
     // Remove mnet connection to template's institution (if it exists)
     $template_institution = $eschool->getInstitution();
     if ($template_institution) {
         $this->removeMnetConnection($template_institution);
     }
     $this->setUser($mhr_owner, array('eschooladmin'));
     // set unique mdl_config vars
     $this->setConfigVar('calendar_exportsalt', GcrEschoolTable::generateRandomString(60));
     $this->setConfigVar('alternateloginurl', $this->getUrl() . '/eschool/login');
     $this->setConfigVar('resource_secretphrase', GcrEschoolTable::generateRandomString(60));
     $this->setConfigVar('calendar_exportsalt', GcrEschoolTable::generateRandomString(20));
     $this->setConfigVar('siteidentifier', GcrEschoolTable::generateRandomString(32) . $this->getDomain());
     $this->setConfigVar('cronremotepassword', GcrEschoolTable::generateRandomString(20));
     $this->setConfigVar('noreplyaddress', $this->getDomain());
     $this->setConfigVar('registerauth', 'email');
     $this->setMdlCacheSettings();
     $institution->createMnetConnection($this);
 }
 public function create($owner_credentials_array)
 {
     // Create random salt and admin password
     $this->admin_password = GcrEschoolTable::generateAdminPassword();
     $this->password_salt = GcrEschoolTable::generateRandomString();
     // Create the new database schema for this institution
     GcrDatabaseAccessPostgres::createSchema($this);
     // Setup gc4admin
     $gc4_credentials = $this->buildUserCredentials('gc4' . $this->short_name . 'admin', $this->admin_password);
     $this->updateMhrTable('usr', array('password' => $gc4_credentials->password, 'salt' => $gc4_credentials->salt), array('username' => 'gc4' . $this->short_name . 'admin'));
     $institution = $this->getAppType()->getTemplateObject();
     // add the template's salt to the list of historical salts for the new institution
     $salt_history = new GcrInstitutionSaltHistory();
     $salt_history->setInstitutionid($this->id);
     $salt_history->setSalt($institution->password_salt);
     $salt_history->save();
     // transfer all old salts from template to new institution
     $old_salts = Doctrine::getTable('GcrInstitutionSaltHistory')->findByInstitutionid($institution->id);
     if ($old_salts) {
         foreach ($old_salts as $salt_record) {
             $salt_history = new GcrInstitutionSaltHistory();
             $salt_history->setInstitutionid($this->id);
             $salt_history->setSalt($salt_record->salt);
             $salt_history->save();
         }
     }
     // Setup Owner
     $master_admin = $this->getPersonObject();
     $template_owner = $institution->getOwnerUser();
     $owner_credentials = $this->buildUserCredentials($owner_credentials_array['username'], $owner_credentials_array['password']);
     $params = array('username' => $owner_credentials->username, 'password' => $owner_credentials->password, 'salt' => $owner_credentials->salt, 'firstname' => $master_admin->getFirstName(), 'lastname' => $master_admin->getLastName(), 'email' => $master_admin->getEmail());
     $owner_obj = $template_owner->getObject();
     $this->updateMhrTable('usr', $params, array('id' => $owner_obj->id));
     $this->updateMhrTable('artefact', array('title' => $master_admin->getEmail()), array('owner' => $owner_obj->id, 'artefacttype' => 'email'));
     $this->updateMhrTable('artefact', array('title' => $master_admin->getFirstName()), array('owner' => $owner_obj->id, 'artefacttype' => 'firstname'));
     $this->updateMhrTable('artefact', array('title' => $master_admin->getLastName()), array('owner' => $owner_obj->id, 'artefacttype' => 'lastname'));
     $this->updateMhrTable('artefact', array('title' => $master_admin->getFirstName() . ' ' . $master_admin->getLastName()), array('owner' => $owner_obj->id, 'artefacttype' => 'blog'));
     $this->updateMhrTable('artefact_internal_profile_email', array('email' => $master_admin->getEmail()), array('owner' => $owner_obj->id, 'principal' => 1));
     $this->creator_id = $template_owner->getObject()->id;
     $this->save();
     // Set the sitename and mhr_institution display name
     $this->setConfigVar('sitename', $this->full_name);
     $this->updateMhrTable('institution', array('displayname' => $this->full_name), array('name' => gcr::maharaInstitutionName));
     // Inherit all commission arrangements with catalogs attached to the template
     $this->copyCommissions($institution);
     // Create the default eSchool if it doesn't exist. If an existing eSchool was chosen,
     // create the mnet connections on both sides.
     $eschool = $this->getDefaultEschool(true);
     if ($eschool) {
         $this->setMnetConnection($eschool);
         $eschool->setMnetConnection($this);
     } else {
         $this->createDefaultEschool();
     }
 }