}
                     echo get_string('departmentnoselect', 'block_iomad_company_admin');
                     $mform->display();
                     echo $OUTPUT->footer();
                     die;
                 }
             }
         }
     }
 } else {
     if ($createdata = $editform->get_data()) {
         // Create or update the department.
         if ($deptid && !$createdata->chosenid) {
             $departmentid = $deptid;
         }
         if (!company::can_manage_department($departmentid)) {
             print_error("notallowedtoedit", 'block_iomad_company_admin');
         }
         if (!$createdata->chosenid) {
             // We are creating a new department.
             company::create_department($createdata->chosenid, $companyid, $createdata->fullname, $createdata->shortname, $departmentid, 2);
         } else {
             // We are editing a current department.
             company::create_department($createdata->departmentid, $companyid, $createdata->fullname, $createdata->shortname, 0);
         }
         $mform = new department_display_form($PAGE->url, $companyid, $departmentid);
         // Redisplay the form.
         $blockpage->display_header();
         // Check the department is valid.
         if (!empty($departmentid) && !company::check_valid_department($companyid, $departmentid)) {
             print_error('invaliddepartment', 'block_iomad_company_admin');
                     if (!empty($departmentid) && !company::check_valid_department($companyid, $departmentid)) {
                         print_error('invalidlocation', 'block_iomad_company_admin');
                     }
                     echo get_string('locationnoselect', 'block_iomad_company_admin');
                     $mform->display();
                     echo $OUTPUT->footer();
                     die;
                 }
             }
         }
     }
 } else {
     if ($createdata = $editform->get_data()) {
         // Create or update the department.
         $locationid = $createdata->chosenid ? $createdata->chosenid : $createdata->departmentid;
         if (!company::can_manage_department($locationid)) {
             print_error("notallowedtoedit", 'block_iomad_company_admin');
         }
         if (!$createdata->chosenid) {
             // We are creating a new department.
             company::create_department($createdata->chosenid, $companyid, $createdata->fullname, $createdata->shortname, $departmentid, 1);
         } else {
             // We are editing a current department.
             company::create_department($createdata->departmentid, $companyid, $createdata->fullname, $createdata->shortname, $departmentid, 1);
         }
         $mform = new location_display_form($PAGE->url, $companyid, $departmentid);
         // Redisplay the form.
         $blockpage->display_header();
         // Check the department is valid.
         if (!empty($departmentid) && !company::check_valid_department($companyid, $departmentid)) {
             print_error('invalidlocation', 'block_iomad_company_admin');
 public function definition()
 {
     global $CFG, $OUTPUT;
     $mform =& $this->_form;
     $company = new company($this->selectedcompany);
     if (!($parentnode = company::get_company_parentnode($company->id))) {
         // Company has not been set up, possibly from before an upgrade.
         company::initialise_departments($company->id);
     }
     $notcompanymanager = 0;
     if ($this->departmentid != $parentnode->id) {
         $regionlist = company::get_my_region($this->departmentid);
         $notcompanymanager = 1;
     } else {
         $regionlist = company::get_all_regions($company->id);
     }
     //print_object($regionlist);
     //die;
     $companieslist = company::get_companies_select();
     // Then show the fields about where this block appears.
     $mform->addElement('header', 'header', get_string('companyregion', 'block_iomad_company_admin'));
     //GWL : Remove Company Name
     //$mform->addElement('select', 'deptid', get_string('company', 'block_iomad_company_admin'), $companieslist);
     // $mform->addElement('html', $subdepartmenthtml);
     $regionlisthtml = '';
     if (!empty($regionlist)) {
         $regionlisthtml = "<p>" . get_string('regions', 'block_iomad_company_admin') . "</p>";
         $regioncount = 0;
         if (!$notcompanymanager) {
             $regionlisthtml = "<div class='locationlist_container checkboxcontainer'>";
             foreach ($regionlist as $region) {
                 $regioncount++;
                 $regionlisthtml .= '<input type = "checkbox" name = "regionids[]" value="' . $region->id . '" /> ' . $region->name . '</br>';
             }
         } else {
             $regionlisthtml .= '<input type = "checkbox" name = "regionids[]" value="' . $regionlist->id . '" /> ' . $regionlist->name . '</br>';
         }
         $regionlisthtml .= '</div>';
         if ($this->departmentid == $parentnode->id && $regioncount > 1) {
             $regionlisthtml .= '<p><br/><input type="checkbox" id="selectallcheckbox" />Select all / Deselect All</p>';
         }
     } else {
         if (!company::can_manage_department($parentnode->id)) {
             echo $OUTPUT->header();
             echo $OUTPUT->notification(get_string('notaccessregion', 'block_iomad_company_admin'));
             echo $OUTPUT->footer();
             die;
         }
     }
     $mform->addElement('html', $regionlisthtml);
     $buttonarray = array();
     if ($this->departmentid == $parentnode->id) {
         $buttonarray[] = $mform->createElement('submit', 'create', get_string('createregion', 'block_iomad_company_admin'));
     }
     if (!empty($regionlist)) {
         $buttonarray[] = $mform->createElement('submit', 'edit', get_string('editregions', 'block_iomad_company_admin'), array('class' => 'editdepart'));
         if ($this->departmentid == $parentnode->id) {
             $buttonarray[] = $mform->createElement('submit', 'delete', get_string('deleteregion', 'block_iomad_company_admin'));
         }
     }
     $mform->addGroup($buttonarray, '', array(' '), ' ', false);
 }