/**
  * Form tweaks that depend on current data.
  */
 public function definition_after_data()
 {
     global $USER, $SESSION;
     $mform =& $this->_form;
     $columns =& $this->_customdata;
     foreach ($columns as $column) {
         if ($mform->elementExists($column)) {
             $mform->removeElement($column);
         }
     }
     // Set the companyid to bypass the company select form if possible.
     if (!empty($SESSION->currenteditingcompany)) {
         $companyid = $SESSION->currenteditingcompany;
     } else {
         $companyid = company_user::companyid();
     }
     // Get the department list.
     $parentlevel = company::get_company_parentnode($companyid);
     if (iomad::has_capability('block/iomad_company_admin:edit_all_departments', context_system::instance())) {
         $userhierarchylevel = $parentlevel->id;
     } else {
         $userlevel = company::get_userlevel($USER);
         $userhierarchylevel = $userlevel->id;
     }
     $this->departmentid = $userhierarchylevel;
     $subhierarchieslist = company::get_all_subdepartments($userhierarchylevel);
     //  Department drop down.
     $mform->insertElementBefore($mform->createElement('select', 'userdepartment', get_string('department', 'block_iomad_company_admin'), $subhierarchieslist, $userhierarchylevel), 'uutypelabel');
     $this->courseselector = $this->add_course_selector();
     $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser'));
 }
 public function __construct($actionurl, $companyid, $departmentid)
 {
     global $CFG, $USER;
     $this->selectedcompany = $companyid;
     $this->departmentid = $departmentid;
     $company = new company($this->selectedcompany);
     $parentlevel = company::get_company_parentnode($company->id);
     $this->companydepartment = $parentlevel->id;
     if (iomad::has_capability('block/iomad_company_admin:edit_all_departments', context_system::instance())) {
         $userhierarchylevel = $parentlevel->id;
     } else {
         $userlevel = company::get_userlevel($USER);
         $userhierarchylevel = $userlevel->id;
     }
     $this->subhierarchieslist = company::get_all_subdepartments($userhierarchylevel);
     if ($this->departmentid == 0) {
         $departmentid = $userhierarchylevel;
     } else {
         $departmentid = $this->departmentid;
     }
     $this->userdepartment = $userhierarchylevel;
     $options = array('context' => $this->context, 'multiselect' => true, 'companyid' => $this->selectedcompany, 'departmentid' => $departmentid, 'subdepartments' => $this->subhierarchieslist, 'parentdepartmentid' => $parentlevel, 'showopenshared' => true, 'license' => false);
     $this->currentcourses = new potential_subdepartment_course_selector('currentcourses', $options);
     $this->currentcourses->set_rows(20);
     $this->context = context_coursecat::instance($CFG->defaultrequestcategory);
     parent::moodleform($actionurl);
 }
 public function __construct($actionurl, $context, $companyid, $deptid, $roleid, $showothermanagers)
 {
     global $USER;
     $this->selectedcompany = $companyid;
     $this->context = $context;
     $this->departmentid = $deptid;
     $this->roletype = $roleid;
     /* GWL :  */
     if (!iomad::has_capability('block/iomad_company_admin:company_add', context_system::instance())) {
         $this->showothermanagers = false;
     } else {
         $this->showothermanagers = $showothermanagers;
     }
     $company = new company($this->selectedcompany);
     $parentlevel = company::get_company_parentnode($company->id);
     $this->companydepartment = $parentlevel->id;
     if (iomad::has_capability('block/iomad_company_admin:edit_all_departments', context_system::instance())) {
         $userhierarchylevel = $parentlevel->id;
     } else {
         $userlevel = company::get_userlevel($USER);
         $userhierarchylevel = $userlevel->id;
     }
     $this->subhierarchieslist = company::get_all_subdepartments($userhierarchylevel);
     if ($this->departmentid == 0) {
         $departmentid = $userhierarchylevel;
     } else {
         $departmentid = $this->departmentid;
     }
     $options = array('context' => $this->context, 'companyid' => $this->selectedcompany, 'departmentid' => $departmentid, 'roletype' => $this->roletype, 'subdepartments' => $this->subhierarchieslist, 'parentdepartmentid' => $parentlevel, 'showothermanagers' => $this->showothermanagers);
     $this->potentialusers = new potential_department_user_selector('potentialmanagers', $options);
     $this->currentusers = new current_department_user_selector('currentmanagers', $options);
     parent::__construct($actionurl);
 }
 public function __construct($actionurl, $context, $companyid, $departmentid)
 {
     global $USER;
     $this->selectedcompany = $companyid;
     $this->context = $context;
     $this->departmentid = $departmentid;
     $company = new company($this->selectedcompany);
     $parentlevel = company::get_company_parentnode($company->id);
     $this->companydepartment = $parentlevel->id;
     $syscontext = context_system::instance();
     if (iomad::has_capability('block/iomad_company_admin:edit_all_departments', $syscontext)) {
         $userhierarchylevel = $parentlevel->id;
     } else {
         $userlevel = company::get_userlevel($USER);
         $userhierarchylevel = $userlevel->id;
     }
     $this->subhierarchieslist = company::get_all_subdepartments($userhierarchylevel);
     if ($this->departmentid == 0) {
         $departmentid = $userhierarchylevel;
     } else {
         $departmentid = $this->departmentid;
     }
     $options = array('context' => $this->context, 'companyid' => $this->selectedcompany, 'departmentid' => $departmentid, 'subdepartments' => $this->subhierarchieslist, 'parentdepartmentid' => $parentlevel, 'shared' => false, 'partialshared' => true);
     $this->potentialcourses = new potential_company_course_selector('potentialcourses', $options);
     $this->currentcourses = new current_company_course_selector('currentcourses', $options);
     parent::__construct($actionurl);
 }
 public function __construct($actionurl, $context, $companyid, $departmentid, $userid, $licenseid)
 {
     global $USER, $DB;
     $this->selectedcompany = $companyid;
     $this->context = $context;
     $company = new company($this->selectedcompany);
     $this->parentlevel = company::get_company_parentnode($company->id);
     $this->companydepartment = $this->parentlevel->id;
     $this->licenseid = $licenseid;
     if (iomad::has_capability('block/iomad_company_admin:edit_all_departments', context_system::instance())) {
         $userhierarchylevel = $this->parentlevel->id;
     } else {
         $userlevel = company::get_userlevel($USER);
         $userhierarchylevel = $userlevel->id;
     }
     $this->subhierarchieslist = company::get_all_subdepartments($userhierarchylevel);
     if ($departmentid == 0) {
         $this->departmentid = $userhierarchylevel;
     } else {
         $this->departmentid = $departmentid;
     }
     $this->userid = $userid;
     $this->user = $DB->get_record('user', array('id' => $this->userid));
     parent::__construct($actionurl);
 }
 public function __construct($actionurl, $context, $companyid, $templateid, $templaterecord)
 {
     $this->templateid = $templateid;
     $this->context = $context;
     $this->templaterecord = $templaterecord;
     $this->companyid = $companyid;
     $department = company::get_company_parentnode($this->companyid);
     $subhierarchieslist = company::get_all_subdepartments($department->id);
     $options = array('context' => $this->context, 'multiselect' => false, 'companyid' => $this->companyid, 'departmentid' => $department->id, 'subdepartments' => $subhierarchieslist, 'parentdepartmentid' => $department);
     $this->currentcourses = new current_company_course_selector('currentcourses', $options);
     $this->currentcourses->set_rows(1);
     parent::__construct($actionurl);
 }
 public function process()
 {
     global $DB;
     if ($this->selectedcompany) {
         $company = new company($this->selectedcompany);
         $companyshortname = $company->get_shortname();
         $companydefaultdepartment = company::get_company_parentnode($company->id);
         // Process incoming assignments.
         if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
             $userstoassign = $this->potentialusers->get_selected_users();
             if (!empty($userstoassign)) {
                 foreach ($userstoassign as $adduser) {
                     $allow = true;
                     if ($allow) {
                         $user = $DB->get_record('user', array('id' => $adduser->id));
                         // Add user to default company department.
                         $company->assign_user_to_company($adduser->id);
                     }
                 }
                 $this->potentialusers->invalidate_selected_users();
                 $this->currentusers->invalidate_selected_users();
             }
         }
         // Process incoming unassignments.
         if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
             $userstounassign = $this->currentusers->get_selected_users();
             if (!empty($userstounassign)) {
                 foreach ($userstounassign as $removeuser) {
                     // Check if the user was a company manager.
                     if ($DB->get_records('company_users', array('userid' => $removeuser->id, 'managertype' => 1))) {
                         $companymanagerrole = $DB->get_record('role', array('shortname' => 'companymanager'));
                         role_unassign($companymanagerrole->id, $removeuser->id, $this->context->id);
                     }
                     if ($DB->get_records('company_users', array('userid' => $removeuser->id, 'managertype' => 2))) {
                         $departmentmanagerrole = $DB->get_record('role', array('shortname' => 'departmentmanager'));
                         role_unassign($departmentmanagerrole->id, $removeuser->id, $this->context->id);
                     }
                     $DB->delete_records('company_users', array('userid' => $removeuser->id));
                     // Deal with the company theme.
                     $DB->set_field('user', 'theme', '', array('id' => $removeuser->id));
                 }
                 $this->potentialusers->invalidate_selected_users();
                 $this->currentusers->invalidate_selected_users();
             }
         }
     }
 }
 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, $DB;
     $mform =& $this->_form;
     $company = new company($this->selectedcompany);
     $parentlevel = company::get_company_parentnode($company->id);
     $options = array('context' => context_system::instance(), 'multiselect' => true, 'companyid' => $company->id, 'deptid' => $this->department, 'titleid' => $this->title, 'departmentid' => $parentlevel->id, 'department' => $this->department, 'title' => $this->title, 'exclude' => array(4), 'parentdepartmentid' => $parentlevel, 'licenses' => false, 'shared' => true);
     $this->currentcourses = new current_company_all_course_selector('currentcourses', $options);
     $this->selectedcourses = new department_title_courses('selectedcourses', $options);
     $mform->addElement('hidden', 'department', $this->department);
     $mform->setType('department', PARAM_INT);
     $mform->addElement('hidden', 'title', $this->title);
     $mform->setType('title', PARAM_INT);
     //$courselist = array('Course1', 'Course2', 'Course3', 'Course4');
     // $this->currentcourses->display(true);
     //$select = $mform->addElement('select', 'departmentcourses', get_string('departments', 'local_manage_company_dept_title'), $courselist);
     //$select->setMultiple(true);
     //$select = $mform->addElement('select', 'companycourses', get_string('departments', 'local_manage_company_dept_title'), $courselist);
     //$select->setMultiple(true);
     //$mform->addElement('submit');
 }
$blockpage = new blockpage($PAGE, $OUTPUT, 'iomad_company_admin', 'block', 'company_edit_users_title');
$blockpage->setup();
// Set the companyid
$companyid = iomad::get_my_companyid($systemcontext);
require_login(null, false);
// Adds to $PAGE, creates $OUTPUT.
$baseurl = new moodle_url(basename(__FILE__), $params);
$returnurl = $baseurl;
$blockpage->display_header();
// GWL : Check the department is valid.
if (!empty($departmentid) && !company::check_valid_department($companyid, $departmentid)) {
    print_error('invaliddepartment', 'block_iomad_company_admin');
}
// Get the associated department id.
$company = new company($companyid);
$parentlevel = company::get_company_parentnode($company->id);
$companydepartment = $parentlevel->id;
if (iomad::has_capability('block/iomad_company_admin:edit_all_departments', context_system::instance())) {
    $userhierarchylevel = $parentlevel->id;
} else {
    $userlevel = company::get_userlevel($USER);
    $userhierarchylevel = $userlevel->id;
}
if ($departmentid == 0) {
    $departmentid = $userhierarchylevel;
}
// Get the appropriate list of departments.
$subhierarchieslist = company::get_all_subdepartments($userhierarchylevel);
$select = new single_select($baseurl, 'departmentid', $subhierarchieslist, $departmentid);
//GWL $select->label = get_string('department', 'block_iomad_company_admin');
$select->label = get_string('regionlocation', 'block_iomad_company_admin');
 public function find_courses($search)
 {
     global $DB, $SITE;
     // By default wherecondition retrieves all courses except the deleted, not confirmed and guest.
     list($wherecondition, $params) = $this->search_sql($search, 'c');
     $params['companyid'] = $this->companyid;
     $params['siteid'] = $SITE->id;
     $fields = 'SELECT ' . $this->required_fields_sql('c');
     $countfields = 'SELECT COUNT(1)';
     $distinctfields = 'SELECT DISTINCT ' . $this->required_fields_sql('c');
     $distinctcountfields = 'SELECT COUNT(DISTINCT c.id) ';
     // Get appropriate department ids.
     $departmentids = array_keys(company::get_all_subdepartments($this->departmentid));
     // Check the top department.
     $parentnode = company::get_company_parentnode($this->companyid);
     if (!empty($departmentids)) {
         if ($parentnode->id == $this->departmentid) {
             $departmentselect = "AND cc.departmentid in (" . implode(',', $departmentids) . ") ";
         } else {
             $departmentselect = "AND cc.departmentid in (" . $parentnode->id . ',' . implode(',', $departmentids) . ") ";
         }
     } else {
         $departmentselect = "AND cc.departmentid = " . $parentnode->id;
     }
     if (!$this->license) {
         if (!($licensecourses = $DB->get_records('iomad_courses', array('licensed' => 1), null, 'courseid'))) {
             $licensesql = "";
         } else {
             $licensesql = " AND c.id not in (" . implode(',', array_keys($licensecourses)) . ")";
         }
     } else {
         $licensesql = "";
     }
     $sqldistinct = " FROM {course} c,\n                        {company_course} cc\n                        WHERE {$wherecondition}\n                        AND cc.courseid = c.id\n                        AND c.id != :siteid\n                        {$licensesql}\n                        {$departmentselect}";
     $sql = " FROM {course} c\n                WHERE {$wherecondition}\n                      AND c.id != :siteid\n                      AND NOT EXISTS (SELECT NULL FROM {company_course} WHERE courseid = c.id)";
     if (!empty($this->showopenshared)) {
         $sqlopenshared = " FROM {course} c,\n                            {iomad_courses} ic\n                            WHERE {$wherecondition}\n                            AND ic.courseid = c.id\n                            AND c.id != :siteid\n                            AND ic.shared = 1\n                            {$licensesql}";
     }
     $order = ' ORDER BY c.fullname ASC';
     if (!$this->is_validating()) {
         $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params) + $DB->count_records_sql($distinctcountfields . $sqldistinct, $params);
         if ($potentialmemberscount > company_course_selector_base::MAX_COURSES_PER_PAGE) {
             return $this->too_many_results($search, $potentialmemberscount);
         }
     }
     $availablecourses = $DB->get_records_sql($fields . $sql . $order, $params) + $DB->get_records_sql($distinctfields . $sqldistinct . $order, $params);
     if (!empty($this->showopenshared)) {
         $availablecourses = $availablecourses + $DB->get_records_sql($distinctfields . $sqlopenshared . $order, $params);
     }
     if (empty($availablecourses)) {
         return array();
     }
     $sanitisedcourses = array();
     foreach ($availablecourses as $key => $availablecourse) {
         $sanitisedcourses[$key] = $availablecourse;
     }
     // Have any of the courses got enrollments?
     $this->process_enrollments($sanitisedcourses);
     if ($search) {
         $groupname = get_string('potcoursesmatching', 'block_iomad_company_admin', $search);
     } else {
         $groupname = get_string('potcourses', 'block_iomad_company_admin');
     }
     return array($groupname => $availablecourses);
 }
 public static function get_all_regions($companyid)
 {
     global $DB;
     if (!($parentnode = company::get_company_parentnode($companyid))) {
         return false;
     }
     return $DB->get_records('department', array('company' => $companyid, 'level' => 0, 'parent' => $parentnode->id));
 }
 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);
 }
 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();
 }
 protected function get_license_department_ids()
 {
     global $DB, $USER;
     if (!isset($this->licenseid)) {
         return array();
     } else {
         if (!$DB->get_record_sql("SELECT pc.id\n                                      FROM {iomad_courses} pc\n                                      INNER JOIN {companylicense_courses} clc\n                                      ON clc.courseid = pc.courseid\n                                      WHERE clc.licenseid={$this->licenseid}\n                                      AND pc.shared=1")) {
             // Check if we are a shared course or not.
             $courses = $DB->get_records('companylicense_courses', array('licenseid' => $this->licenseid));
             $shared = false;
             foreach ($courses as $course) {
                 if ($DB->get_record_select('iomad_courses', "courseid='" . $course->courseid . "' AND shared!= 0")) {
                     $shared = true;
                 }
             }
             $sql = "SELECT DISTINCT d.id from {department} d, {company_course} cc, {companylicense_courses} clc\n                        WHERE\n                        d.id = cc.departmentid\n                        AND\n                        cc.courseid = clc.courseid\n                        AND\n                        clc.licenseid = " . $this->licenseid . "\n                        AND d.company = " . $this->companyid;
             $departments = $DB->get_records_sql($sql);
             $shareddepartment = array();
             if ($shared) {
                 if (iomad::has_capability('block/iomad_company_admin:edit_licenses', context_system::instance())) {
                     // Need to add the top level department.
                     $shareddepartment = company::get_company_parentnode($this->companyid);
                     $departments = $departments + array($shareddepartment->id => $shareddepartment->id);
                 } else {
                     $shareddepartment = company::get_userlevel($USER);
                     $departments = $departments + array($shareddepartment->id => $shareddepartment->id);
                 }
             }
             if (!empty($departments)) {
                 // Only return the keys (user ids).
                 return array_keys($departments);
             } else {
                 return array();
             }
         } else {
             return array($this->departmentid);
         }
     }
 }
 public function definition()
 {
     global $CFG;
     $mform =& $this->_form;
     $company = new company($this->selectedcompany);
     //$regionlist = company::get_all_regions($company->id);
     $regionlistarr = array();
     $parentnode = company::get_company_parentnode($company->id);
     if ($this->regionid != $parentnode->id) {
         $regionlist = company::get_my_region($this->regionid);
         if ($regionlist) {
             $regionlistarr[$regionlist->id] = '/' . $regionlist->name;
         }
     } else {
         $regionlist = company::get_all_regions($company->id);
         foreach ($regionlist as $region) {
             $regionlistarr[$region->id] = '/' . $region->name;
         }
     }
     $department = company::get_departmentbyid($this->departmentid);
     // Then show the fields about where this block appears.
     if ($this->action == 0) {
         $mform->addElement('header', 'header', get_string('createlocation', 'block_iomad_company_admin'));
     } else {
         $mform->addElement('header', 'header', get_string('editlocations', 'block_iomad_company_admin'));
     }
     $mform->addElement('hidden', 'departmentid', $this->departmentid);
     $mform->setType('departmentid', PARAM_INT);
     $mform->addElement('hidden', 'action', $this->action);
     $mform->setType('action', PARAM_INT);
     if ($regionlist) {
         $select = $mform->addElement('select', 'deptid', get_string('region', 'block_iomad_company_admin'), $regionlistarr);
         //$mform->disabledIf('deptid', 'action', 'eq', 1);
     }
     $mform->addElement('text', 'fullname', get_string('fullnamelocation', 'block_iomad_company_admin'), 'maxlength = "254" size = "50"');
     $mform->addHelpButton('fullname', 'fullnamelocation', 'block_iomad_company_admin');
     $mform->addRule('fullname', get_string('missingfullnamelocation', 'block_iomad_company_admin'), 'required', null, 'client');
     $mform->setType('fullname', PARAM_MULTILANG);
     $mform->addElement('text', 'shortname', get_string('shortnamelocation', 'block_iomad_company_admin'), 'maxlength = "100" size = "20"');
     $mform->addHelpButton('shortname', 'shortnamelocation', 'block_iomad_company_admin');
     $mform->addRule('shortname', get_string('missingshortnamelocation', 'block_iomad_company_admin'), 'required', null, 'client');
     $mform->setType('shortname', PARAM_MULTILANG);
     if (!$this->departmentid) {
         $mform->addElement('hidden', 'chosenid', $this->chosenid);
     } else {
         $mform->addElement('hidden', 'chosenid', $this->departmentid);
     }
     $mform->setType('chosenid', PARAM_INT);
     $this->add_action_buttons();
 }
        company::initialise_departments($companyid);
        $company->id = $companyid;
        //  Set up a profiles field category for this company.
        $catdata = new object();
        $catdata->sortorder = $DB->count_records('user_info_category') + 1;
        $catdata->name = $company->shortname;
        $DB->insert_record('user_info_category', $catdata, false);
        // Set up course category for company.
        $coursecat = new object();
        $coursecat->name = $company->name;
        $coursecat->sortorder = 999;
        $coursecat->id = $DB->insert_record('course_categories', $coursecat);
        $coursecat->context = context_coursecat::instance($coursecat->id);
        $categorycontext = $coursecat->context;
        mark_context_dirty($coursecat->context->path);
        $DB->update_record('course_categories', $coursecat);
        fix_course_sortorder();
        $companydetails = $DB->get_record('company', array('id' => $company->id));
        $companydetails->category = $coursecat->id;
        $DB->update_record('company', $companydetails);
    }
    // Add user to default company department.
    $USER->profile_field_company = $company->shortname;
    profile_save_data($USER);
    $companydepartment = company::get_company_parentnode($company->id);
    company::assign_user_to_department($companydepartment->id, $USER->id);
}
if ($invoice->status == INVOICESTATUS_PAID) {
    processor::trigger_onordercomplete($invoice);
}
echo $OUTPUT->footer();