Skip to content

Commit

Permalink
IOMAD: added processing for license pool expiring email template
Browse files Browse the repository at this point in the history
  • Loading branch information
turf212 committed Mar 29, 2024
1 parent 06792f9 commit dca57e2
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
2 changes: 1 addition & 1 deletion local/email/version.php
Expand Up @@ -22,6 +22,6 @@
*/

$plugin->release = '4.1.9 (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';
98 changes: 98 additions & 0 deletions local/email_reports/classes/task/company_license_expiring_task.php
@@ -0,0 +1,98 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @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()));
}

}
9 changes: 9 additions & 0 deletions local/email_reports/db/tasks.php
Expand Up @@ -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' => '*'
]
];
21 changes: 21 additions & 0 deletions local/iomad/lib/company.php
Expand Up @@ -4508,6 +4508,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)) {
Expand Down

0 comments on commit dca57e2

Please sign in to comment.