}
        $licensecourses = $DB->get_records('companylicense_courses', array('licenseid' => $license->id));
        $coursestring = "";
        foreach ($licensecourses as $licensecourse) {
            $coursename = $DB->get_record('course', array('id' => $licensecourse->courseid));
            if (empty($coursestring)) {
                $coursestring = "<a href='" . new moodle_url('/course/view.php', array('id' => $licensecourse->courseid)) . "'>" . $coursename->fullname . "</a>";
            } else {
                $coursestring .= ",</br><a href='" . new moodle_url('/course/view.php', array('id' => $licensecourse->courseid)) . "'>" . $coursename->fullname . "</a>";
            }
        }
        // Create the table data.
        $table->data[] = array("{$license->name}", $coursestring, date('d M Y', $license->expirydate), "{$license->validlength}", "{$license->allocation}", "{$license->used}", $editbutton, $deletebutton);
    }
} else {
    if ($licenses = company::get_recursive_departments_licenses($companydepartment->id)) {
        foreach ($licenses as $licenseid) {
            // Get the license record.
            $license = $DB->get_record('companylicense', array('id' => $licenseid->licenseid));
            // Set up the edit buttons.
            if (iomad::has_capability('block/iomad_company_admin:edit_licenses', $context)) {
                $deletebutton = "<a href=\"company_license_list.php?delete={$license->id}&amp;sesskey=" . sesskey() . "\">{$strdelete}</a>";
                $editbutton = "<a href='" . new moodle_url('company_license_edit_form.php', array("licenseid" => $license->id, 'departmentid' => $departmentid)) . "'>{$stredit}</a>";
            } else {
                $deletebutton = "";
                $editbutton = "";
            }
            $table->data[] = array("{$license->name}", "{$license->expirydate}", "{$license->validlength}", "{$license->allocation}", "{$license->used}", $editbutton, $deletebutton);
        }
    }
}
$licenselist = array();
if (iomad::has_capability('block/iomad_company_admin:unallocate_licenses', context_system::instance())) {
    $userhierarchylevel = $parentlevel->id;
    // Get all the licenses.
    $licenses = $DB->get_records('companylicense', array('companyid' => $companyid), null, 'id,name,expirydate');
    foreach ($licenses as $license) {
        if ($license->expirydate > time()) {
            $licenselist[$license->id] = $license->name;
        } else {
            $licenselist[$license->id] = $license->name . " (expired)";
        }
    }
} else {
    $userlevel = company::get_userlevel($USER);
    $userhierarchylevel = $userlevel->id;
    $licenses = company::get_recursive_departments_licenses($userhierarchylevel);
    if (!empty($licenses)) {
        foreach ($licenses as $deptlicenseid) {
            // Get the license record.
            if ($license = $DB->get_records('companylicense', array('id' => $deptlicenseid->licenseid, 'companyid' => $companyid), null, 'id,name')) {
                $licenselist[$license[$deptlicenseid->licenseid]->id] = $license[$deptlicenseid->licenseid]->name;
            }
        }
    }
}
$usersform = new company_license_users_form($PAGE->url, $context, $companyid, $licenseid, $userhierarchylevel);
$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');
}