if (!empty($departmentid) && !company_department::check_valid_department($companyid, $departmentid)) {
         print_error('invaliddepartment', 'block_iomad_company_admin');
     }
     if (empty($deleteids)) {
         echo get_string('departmentnoselect', 'block_iomad_company_admin');
     }
     $mform->display();
     echo $OUTPUT->footer();
     die;
 } else {
     if (isset($data->edit)) {
         // Editing an existing department.
         if (!empty($deleteids)) {
             $department = array_shift($deleteids);
             $departmentrecord = $DB->get_record('local_company_department', array('id' => $department));
             $editform = new user_department_edit_form(null, $companyid, $departmentrecord->id, 1);
             $editform->set_data(array('departmentid' => $departmentrecord->id, 'fullname' => $departmentrecord->name, 'shortname' => $departmentrecord->shortname));
             $blockpage->display_header();
             // Check the department is valid.
             if (!empty($departmentid) && !company_department::check_valid_department($companyid, $departmentid)) {
                 print_error('invaliddepartment', 'block_iomad_company_admin');
             }
             $editform->display();
             echo $OUTPUT->footer();
         } else {
             $blockpage->display_header();
             // Check the department is valid.
             if (!empty($departmentid) && !company_department::check_valid_department($companyid, $departmentid)) {
                 print_error('invaliddepartment', 'block_iomad_company_admin');
             }
             echo get_string('departmentnoselect', 'block_iomad_company_admin');
     if (!empty($departmentid) && !company::check_valid_department($companyid, $departmentid)) {
         print_error('invaliddepartment', 'block_iomad_company_admin');
     }
     if (empty($deleteids)) {
         echo get_string('departmentnoselect', 'block_iomad_company_admin');
     }
     $mform->display();
     echo $OUTPUT->footer();
     die;
 } else {
     if (isset($data->edit)) {
         // Editing an existing department.
         if (!empty($deleteids)) {
             $department = array_shift($deleteids);
             $departmentrecord = $DB->get_record('department', array('id' => $department));
             $editform = new user_department_edit_form($PAGE->url, $companyid, $departmentid, 0, 1);
             $editform->set_data(array('departmentid' => $departmentrecord->id, 'fullname' => $departmentrecord->name, 'shortname' => $departmentrecord->shortname, 'chosenid' => $departmentid));
             $blockpage->display_header();
             // Check the department is valid.
             if (!empty($departmentid) && !company::check_valid_department($companyid, $departmentid)) {
                 print_error('invaliddepartment', 'block_iomad_company_admin');
             }
             $editform->display();
             echo $OUTPUT->footer();
         } else {
             $blockpage->display_header();
             // Check the department is valid.
             if (!empty($departmentid) && !company::check_valid_department($companyid, $departmentid)) {
                 print_error('invaliddepartment', 'block_iomad_company_admin');
             }
             echo get_string('departmentnoselect', 'block_iomad_company_admin');