public function definition()
 {
     global $CFG;
     $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);
     }
     if (!empty($this->departmentid)) {
         $departmentslist = company::get_all_subdepartments($this->departmentid);
     } else {
         $departmentslist = company::get_all_departments($company->id);
     }
     if (!empty($this->departmentid)) {
         $department = company::get_departmentbyid($this->departmentid);
     } else {
         $department = company::get_company_parentnode($this->selectedcompany);
     }
     $subdepartmentslist = company::get_subdepartments_list($department);
     $subdepartments = company::get_subdepartments($department);
     // Create the sub department checkboxes html.
     $subdepartmenthtml = "";
     if (!empty($subdepartmentslist)) {
         $subdepartmenthtml = "<div class='locationlist_container checkboxcontainer'>";
         $subdepartmenthtml .= "<p>" . get_string('subdepartments', 'block_iomad_company_admin') . "</p>";
         foreach ($subdepartmentslist as $key => $value) {
             $subdepartmenthtml .= '<input type = "checkbox" name = "departmentids[]" value="' . $key . '" /> ' . $value . '</br>';
         }
         $subdepartmenthtml .= '</div>';
         $subdepartmenthtml .= '<p><br/><input type="checkbox" id="selectallcheckbox" />Select all / Deselect All</p>';
     }
     // Then show the fields about where this block appears.
     $mform->addElement('header', 'header', get_string('companydepartment', 'block_iomad_company_admin') . $company->get_name());
     $mform->addElement('select', 'deptid', get_string('department', 'block_iomad_company_admin'), $departmentslist);
     $mform->addElement('html', $subdepartmenthtml);
     $buttonarray = array();
     $buttonarray[] = $mform->createElement('submit', 'create', get_string('createdepartment', 'block_iomad_company_admin'));
     if (!empty($subdepartmentslist)) {
         $buttonarray[] = $mform->createElement('submit', 'edit', get_string('editdepartments', 'block_iomad_company_admin'), array('class' => 'editdepart'));
         $buttonarray[] = $mform->createElement('submit', 'delete', get_string('deletedepartment', 'block_iomad_company_admin'));
     }
     $mform->addGroup($buttonarray, '', array(' '), ' ', false);
 }
 public function definition()
 {
     global $CFG, $USER;
     $mform =& $this->_form;
     $company = new company($this->selectedcompany);
     //$departmentslist = company::get_all_departments($company->id);
     $departmentlist = array();
     if ($this->deptid) {
         $departmentlist = array();
         if ($this->locationid) {
             $department = company::get_departmentbyid($this->locationid);
             $departmenttree = company::get_subdepartments($department);
             $parentlist[$department->id] = array($department->id => '/' . $department->name);
             $departmentlist = company::array_flatten($parentlist + company::get_subdepartments_list($departmenttree));
         }
     }
     // Then show the fields about where this block appears.
     if ($this->action == 0) {
         $mform->addElement('header', 'header', get_string('createdepartment', 'block_iomad_company_admin'));
     } else {
         $mform->addElement('header', 'header', get_string('editdepartments', 'block_iomad_company_admin'));
     }
     $regionlist = company::get_all_regions($company->id);
     $regionlistarr = array('' => get_string('choose'));
     foreach ($regionlist as $region) {
         $regionlistarr[$region->id] = $region->name;
     }
     $parentnode = company::get_company_parentnode($this->selectedcompany);
     $context = context_system::instance();
     if (iomad::has_capability('block/iomad_company_admin:edit_all_departments', $context)) {
         $userhierarchylevel = $parentnode->id;
     } else {
         $userlevel = company::get_userlevel($USER);
         $userhierarchylevel = $userlevel->id;
     }
     $showregion = 1;
     $showlocation = 1;
     if ($parentnode->id != $userhierarchylevel) {
         $myregion = company::get_my_region($userhierarchylevel);
         if (!$myregion) {
             $showregion = 0;
         }
         $mylocation = company::get_my_location($userhierarchylevel);
         if (!$mylocation) {
             $showlocation = 0;
         }
     }
     $locationlist = array();
     if ($this->locationid) {
         $locationdetail = company::get_departmentbyid($this->locationid);
         $locationlist[$locationdetail->id] = $locationdetail->name;
     }
     $mform->addElement('hidden', 'departmentid', $this->deptid);
     $mform->setType('departmentid', PARAM_INT);
     $mform->addElement('hidden', 'deptlocation', $this->locationid);
     $mform->setType('deptlocation', PARAM_INT);
     $mform->addElement('hidden', 'action', $this->action);
     $mform->setType('action', PARAM_INT);
     if ($showregion) {
         $mform->addElement('select', 'regionid', get_string('region', 'block_iomad_company_admin'), $regionlistarr);
         $mform->disabledIf('regionid', 'action', 'eq', 1);
     }
     if ($showlocation) {
         $mform->addElement('select', 'locationid', get_string('location', 'block_iomad_company_admin'), $locationlist);
         $mform->disabledIf('locationid', 'action', 'eq', 1);
     }
     if ($departmentlist) {
         $attr = array('class' => 'dept_container');
         if ($this->disabledeptlist) {
             $attr['disabled'] = 'disabled';
         }
         $mform->addElement('select', 'deptid', get_string('department', 'block_iomad_company_admin'), $departmentlist, $attr);
     }
     $mform->addElement('text', 'fullname', get_string('fullnamedepartment', 'block_iomad_company_admin'), 'maxlength = "254" size = "50"');
     $mform->addHelpButton('fullname', 'fullnamedepartment', 'block_iomad_company_admin');
     $mform->addRule('fullname', get_string('missingfullnamedepartment', 'block_iomad_company_admin'), 'required', null, 'client');
     $mform->setType('fullname', PARAM_MULTILANG);
     $mform->addElement('text', 'shortname', get_string('shortnamedepartment', 'block_iomad_company_admin'), 'maxlength = "100" size = "20"');
     $mform->addHelpButton('shortname', 'shortnamedepartment', 'block_iomad_company_admin');
     $mform->addRule('shortname', get_string('missingshortnamedepartment', 'block_iomad_company_admin'), 'required', null, 'client');
     $mform->setType('shortname', PARAM_MULTILANG);
     $mform->addElement('hidden', 'chosenid', $this->chosenid);
     $mform->setType('chosenid', PARAM_INT);
     $this->add_action_buttons();
 }