diff --git a/local/email/version.php b/local/email/version.php index 4ac6a95eeebf..eaf0a17765a5 100644 --- a/local/email/version.php +++ b/local/email/version.php @@ -22,6 +22,6 @@ */ $plugin->release = '4.2.6 (Build: 20240212)'; // Human-friendly version name -$plugin->version = 2024032600; // The (date) version of this plugin. +$plugin->version = 2024032900; // The (date) version of this plugin. $plugin->requires = 2019052000; // Requires this Moodle version. $plugin->component = 'local_email'; diff --git a/local/email_reports/classes/task/company_license_expiring_task.php b/local/email_reports/classes/task/company_license_expiring_task.php new file mode 100644 index 000000000000..71329787f181 --- /dev/null +++ b/local/email_reports/classes/task/company_license_expiring_task.php @@ -0,0 +1,98 @@ +. + +/** + * @package local_email + * @copyright 2022 Derick Turner + * @author Derick Turner + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_email_reports\task; + +use \EmailTemplate; +use \company; +use \context_course; + +//require_once($CFG->dirroot . '/local/iomad/lib/company.php'); + +class company_license_expiring_task extends \core\task\scheduled_task { + + /** + * Get a descriptive name for this task (shown to admins). + * + * @return string + */ + public function get_name() { + return get_string('company_license_expiring_task', 'local_email_reports'); + } + + /** + * Run email company_license_expiring_task. + */ + public function execute() { + global $DB, $CFG; + + // Set some defaults. + $runtime = time(); + $courses = array(); + $dayofweek = date('w', $runtime) + 1; + + mtrace("Running email report company license expiring task at ".date('d M Y h:i:s', $runtime)); + + // Get all of the licenses which are going to expire in the next 30 days and have un-unsed slots. + $licenses = $DB->get_records_sql("SELECT * FROM {companylicense} + WHERE used < allocated + AND expirydate > :now + AND expirydate < :warn", + ['now' => $runtime, + 'warn' => $runtime + 30 * 24 * 60 * 60]); + // Process any we found. + foreach ($licenses as $license) { + $company = new company($license->companyid); + $companyusql = ""; + $companysql = ""; + + // Only want company managers not parent company managers. + if ($parentslist = $company->get_parent_companies_recursive()) { + $companyusql = " AND u.id NOT IN ( + SELECT userid FROM {company_users} + WHERE companyid IN (" . implode(',', array_keys($parentslist)) ."))"; + $companysql = " AND userid NOT IN ( + SELECT userid FROM {company_users} + WHERE companyid IN (" . implode(',', array_keys($parentslist)) ."))"; + } + + $managers = $DB->get_records_sql("SELECT * FROM {company_users} + WHERE companyid = :companyid + AND managertype = 1 + $companysql", + ['companyid' => $company->id]); + foreach ($managers as $manager) { + if ($user = $DB->get_record('user', ['id' => $manager->userid, 'deleted' => 0, 'suspended' => 0])) { + + $license->expirydate = date($CFG->iomad_date_format, $license->expirydate); + // Passed all checks, send the email. + mtrace("Sending license pool expiring email to $user->email"); + EmailTemplate::send('licensepoolexpiringq', array('user' => $user, 'license' => $license, 'company' => $company)); + } + } + } + + mtrace("email reporting training event not selected completed at " . date('d M Y h:i:s', time())); + } + +} diff --git a/local/email_reports/db/tasks.php b/local/email_reports/db/tasks.php index 92c6c29e449d..ccf15fe7939b 100644 --- a/local/email_reports/db/tasks.php +++ b/local/email_reports/db/tasks.php @@ -71,5 +71,14 @@ 'day' => '*', 'month' => '*', 'dayofweek' => '*' + ], + [ + 'classname' => 'local_email_reports\task\company_license_expiring_task', + 'blocking' => 0, + 'minute' => '0', + 'hour' => '0', + 'day' => '*', + 'month' => '*', + 'dayofweek' => '*' ] ]; diff --git a/local/iomad/lib/company.php b/local/iomad/lib/company.php index 245a132fea29..16e497596bb9 100644 --- a/local/iomad/lib/company.php +++ b/local/iomad/lib/company.php @@ -4536,6 +4536,27 @@ public static function user_license_assigned(\block_iomad_company_admin\event\us // Update the license usage. self::update_license_usage($licenseid); + // Check if we need to warn about usage. + $licenserec = $DB->get_record('companylicense', ['id' => $licenseid]); + if ($licenserec->used/$licenserec->allocation * 100 > 90) { + // Get the company managers. + if ($companymanagers = $DB->get_records_sql("SELECT u.* + FROM {user} u + JOIN {company_users} cu ON (u.id = cu.userid) + WHERE u.deleted = 0 + AND u.suspended = 0 + AND cu.companyid = :companyid + AND cu.managertype =1", + ['companyid' => $company->id])) { + foreach ($companymanagers as $companymanager) { + EmailTemplate::send('licensepoolwarning', array('course' => $course, + 'company' => $company, + 'user' => $companymanager, + 'license' => $license)); + } + } + } + // Is this an immediate license? if (!empty($licenserecord->instant)) { if (self::license_ok_to_use($licenseid, $courseid, $userid)) {