/**
  * Upgrades the modules
  * @param string $old_vers
  * @param string $new_vers
  * @returns boolean
  */
 public function upgrade($old_vers, $new_vers)
 {
     if (I2CE_Validate::checkVersion($old_vers, '<', '3.2.12')) {
         I2CE::raiseError("Changing contact child forms of training_institution to training_institution_contact");
         if (!iHRIS_Module_Contact::changeContactForm('training_institution', 'contact_type|facility', 'training_institution_contact', true)) {
             I2CE::raiseError("Could not upgrade training instituion contacts");
             return false;
         }
     }
     if (I2CE_Validate::checkVersion($old_vers, '=', '3.2.12')) {
         //the changeContactForm did not remvoe the contact form before
         if (!iHRIS_Module_Contact::removeContactForm('training_institution')) {
             return false;
         }
     }
     if (I2CE_Validate::checkVersion($old_vers, '<', '3.2.19')) {
         I2CE::raiseError("Changing contact child forms of training_funder to training_funder_contact");
         if (!iHRIS_Module_Contact::changeContactForm('training_funder', 'contact_type|facility', 'training_funder_contact', true)) {
             I2CE::raiseError("Could not upgrade training funder contacts");
             return false;
         }
     }
     if (I2CE_Validate::checkVersion($old_vers, '<', '3.2.20')) {
         $user = new I2CE_User(1, false, false, false);
         $class_config = I2CE::getConfig()->modules->forms->formClasses;
         $migrate_path = "/I2CE/formsData/migrate_data/3.2.3";
         if (!I2CE_FormStorage::migrateField("training_institution", array("location" => array("county" => "county", "district" => "district", "country" => "country")), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("training_funder", array("location" => array("county" => "county", "district" => "district", "country" => "country")), $migrate_path, $user)) {
             return false;
         }
     }
     return true;
 }
 protected function updateContacts()
 {
     foreach (self::$contacts as $contact) {
         I2CE::raiseError("Changing contact child form of type {$contact}  of person to person_contact_{$contact}");
         if (!iHRIS_Module_Contact::changeContactForm('person', "contact_type|{$contact}", "person_contact_{$contact}", true, false)) {
             I2CE::raiseError("Could not upgrade training funder contacts");
             return false;
         }
     }
     iHRIS_Module_Contact::removeContactForm('person');
     return true;
 }
 /**
  * Upgrade this module if necessary based on the previous and new
  * versions of the module.
  * @param string $old_vers
  * @param string $new_vers
  * @return boolean
  */
 public function upgrade($old_vers, $new_vers)
 {
     if (I2CE_Validate::checkVersion($old_vers, '<', '3.0.1000')) {
         if (!$this->updateContactTypes()) {
             return false;
         }
         if (!$this->ensureFormsAndPages()) {
             return false;
         }
     }
     if (I2CE_Validate::checkVersion($old_vers, "<", "3.3.3")) {
         $user = new I2CE_User(1, false, false, false);
         $class_config = I2CE::getConfig()->modules->forms->formClasses;
         $migrate_path = "/I2CE/formsData/migrate_data/3.2.3";
         if (!I2CE_FormStorage::migrateField("person", array("home" => array("home_county" => "county", "home_district" => "district", "home_country" => "country")), $migrate_path, $user)) {
             return false;
         }
         unset($class_config->iHRIS_PersonQualify->fields->home_country);
         unset($class_config->iHRIS_PersonQualify->fields->home_district);
         unset($class_config->iHRIS_PersonQualify->fields->home_county);
     }
     if (I2CE_Validate::checkVersion($old_vers, "<", "3.3.5")) {
         $user = new I2CE_User(1, false, false, false);
         $class_config = I2CE::getConfig()->modules->forms->formClasses;
         $migrate_path = "/I2CE/formsData/migrate_data/3.2.3";
         $facilityObj = I2CE_FormFactory::instance()->createContainer("health_facility");
         if (!$facilityObj instanceof iHRIS_HealthFacility) {
             I2CE::raiseError("Bad health facility form");
             return false;
         }
         $childForms = $facilityObj->getChildForms();
         if (in_array("contact", $childForms)) {
             I2CE::raiseError("Changing contact child forms of health facility to facility_contact");
             if (!iHRIS_Module_Contact::changeContactForm("health_facility", "contact_type|facility", "facility_contact", true)) {
                 return false;
             }
         }
         if (!I2CE_FormStorage::migrateField("health_facility", array("location" => array("county" => "county", "district" => "district", "country" => "country"), "facility_type" => "facility_type", "facility_agent" => "facility_agent", "facility_status" => "facility_status"), $migrate_path, $user)) {
             return false;
         }
         unset($class_config->iHRIS_HealthFacility->fields->country);
         unset($class_config->iHRIS_HealthFacility->fields->district);
         unset($class_config->iHRIS_HealthFacility->fields->county);
         unset($class_config->iHRIS_HealthFacility->fields->type);
     }
     if (I2CE_Validate::checkVersion($old_vers, "<", "3.3.6")) {
         $user = new I2CE_User(1, false, false, false);
         $class_config = I2CE::getConfig()->modules->forms->formClasses;
         $migrate_path = "/I2CE/formsData/migrate_data/3.2.3";
         $migrate_node = I2CE::getConfig()->traverse($migrate_path, true, false);
         $constant_try = array(1 => 'exam_try|first', 2 => 'exam_try|retry', 3 => 'exam_try|final');
         foreach ($constant_try as $old_id => $new_id) {
             $migrate_node->forms->exam_try->{$old_id} = $new_id;
         }
         $constant_result = array(1 => 'exam_result|pass', 2 => 'exam_result|fail', 3 => 'exam_result|notake');
         foreach ($constant_result as $old_id => $new_id) {
             $migrate_node->forms->exam_result->{$old_id} = $new_id;
         }
         $constant_reg_type = array(1 => 'registration_type|permanent', 2 => 'registration_type|temporary');
         foreach ($constant_reg_type as $old_id => $new_id) {
             $migrate_node->forms->registration_type->{$old_id} = $new_id;
         }
         if (!I2CE_FormStorage::migrateField("cadre", array("qualification" => "qualification"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("certificate", array("academic_level" => "academic_level"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("deployment", array("health_facility" => "health_facility"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("training_disruption_reason", array("training_disruption_category" => "training_disruption_category"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("training_disrupt", array("disruption_reason" => "training_disruption_reason"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("disciplinary_action_reason", array("disciplinary_action_category" => "disciplinary_action_category"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("disciplinary_action", array("disciplinary_action_reason" => "disciplinary_action_reason"), $migrate_path, $user)) {
             return false;
         }
         $traininstObj = I2CE_FormFactory::instance()->createContainer("training_institution");
         if (!$traininstObj instanceof iHRIS_QualifyTrainingInstitution) {
             I2CE::raiseError("Bad training institution form");
             return false;
         }
         $childForms = $traininstObj->getChildForms();
         if (in_array("contact", $childForms)) {
             I2CE::raiseError("Changing contact child forms of training institution to facility_contact");
             if (!iHRIS_Module_Contact::changeContactForm("training_institution", "contact_type|facility", "facility_contact", true)) {
                 return false;
             }
         }
         if (!I2CE_FormStorage::migrateField("training_institution", array("location" => array("county" => "county", "district" => "district", "country" => "country"), "facility_agent" => "facility_agent", "facility_status" => "facility_status"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("training_program", array("cadre" => "cadre"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("facility_institution", array("health_facility" => "health_facility", "training_institution" => "training_institution"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("demographic", array("birth_location" => array("birth_county" => "county", "birth_district" => "district", "birth_country" => "country")), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("education", array("certificate" => "certificate"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("training", array("cadre" => "cadre", "training_institution" => "training_institution", "out_country" => "country"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("exam", array("try" => "exam_try", "results" => "exam_result"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("registration", array("practice_type" => "registration_type"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("out_migration", array("country" => "country", "out_migration_reason" => "out_migration_reason"), $migrate_path, $user)) {
             return false;
         }
         if (!I2CE_FormStorage::migrateField("private_practice", array("health_facility" => "health_facility"), $migrate_path, $user)) {
             return false;
         }
         unset($class_config->iHRIS_DisciplinaryAction->fields->disciplinary_action_category);
         unset($class_config->iHRIS_Education->fields->academic_level);
         unset($class_config->iHRIS_QualifyDemographic->fields->birth_country);
         unset($class_config->iHRIS_QualifyDemographic->fields->birth_district);
         unset($class_config->iHRIS_QualifyDemographic->fields->birth_county);
         unset($class_config->iHRIS_Search);
         unset($class_config->iHRIS_TrainingDisrupt->fields->disruption_category);
         unset($class_config->iHRIS_Cadre->fields->type);
         unset($class_config->iHRIS_Certificate->fields->type);
         unset($class_config->iHRIS_DisciplinaryActionReason->fields->type);
         unset($class_config->iHRIS_HealthFacility->fields->type);
         unset($class_config->iHRIS_TrainingDisruptionReason->fields->type);
     }
     if (I2CE_Validate::checkVersion($old_vers, "<", "3.3.15")) {
         $user = new I2CE_User(1, false, false, false);
         $factory = I2CE_FormFactory::instance();
         $training_programs = $factory->getRecords("training_program");
         I2CE::raiseError("Training programs are: " . print_r($training_programs, true));
         foreach ($training_programs as $id) {
             $program = $factory->createContainer("training_program|" . $id);
             $program->populate();
             $program->getField("training_institution")->setFromDB($program->getParent());
             I2CE::raiseError("Saving training_program {$id} to have institution " . $program->getParent());
             $program->setParent("");
             $program->save($user);
             $program->cleanup();
             unset($program);
         }
     }
     return true;
 }
 protected function fixupContact()
 {
     $facilityObj = I2CE_FormFactory::instance()->createContainer('facility');
     if (!$facilityObj instanceof iHRIS_Facility) {
         I2CE::raiseError("Bad facility form");
         return false;
     }
     $childForms = $facilityObj->getChildForms();
     if (!in_array('contact', $childForms)) {
         return true;
     }
     I2CE::raiseError("Changing contact child forms of facility to facility_contact");
     if (!iHRIS_Module_Contact::changeContactForm('facility', 'contact_type|facility', 'facility_contact', true)) {
         I2CE::raiseError("Could not upgrade training funder contacts");
         return false;
     }
     return true;
 }