Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable completion tracking for a user in multiple companies #1339

Open
wants to merge 1 commit into
base: IOMAD_38_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions blocks/mycourses/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ function mycourses_get_my_completion($datefrom = 0) {
JOIN {course} c ON (c.id = cc.courseid)
WHERE cc.userid = :userid
AND c.visible = 1
AND cc.timecompleted IS NOT NULL",
array('userid' => $USER->id));
AND cc.timecompleted IS NOT NULL
AND cc.companyid = :companyid",
array('userid' => $USER->id, "companyid" => $companyid));
$myinprogress = $DB->get_records_sql("SELECT cc.id, cc.userid, cc.courseid as courseid, c.fullname as coursefullname, c.summary as coursesummary
FROM {local_iomad_track} cc
JOIN {course} c ON (c.id = cc.courseid)
Expand All @@ -51,8 +52,9 @@ function mycourses_get_my_completion($datefrom = 0) {
WHERE cc.userid = :userid
AND c.visible = 1
AND cc.timecompleted IS NULL
AND ue.timestart != 0",
array('userid' => $USER->id));
AND ue.timestart != 0
AND cc.companyid = :companyid",
array('userid' => $USER->id, "companyid" => $companyid));

// We dont care about these. If you have enrolled then you are started.
$mynotstartedenrolled = array();
Expand Down
174 changes: 101 additions & 73 deletions local/iomad/lib/company.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ public static function by_userid($userid) {
}
}

/**
* Return a list of instances of companies using userid
*
* Paramters -
* $userid = int;
*
* Returns class object.
*
**/
public static function by_userid_list($userid) {
global $DB;

if ($companies = $DB->get_records('company_users', array('userid' => $userid), 'companyid DESC')) {
foreach ($companies as $company) {
$company_list[] = new company($company->companyid);
}
return $company_list;
} else {
return false;
}
}

/**
* Gets the company name for the current instance
*
Expand Down Expand Up @@ -348,7 +370,6 @@ public static function get_companyname_byid($companyid) {
$company = $DB->get_record('company', array('id' => $companyid));
return $company->name;
}

/**
* Gets the company record given a member
*
Expand Down Expand Up @@ -3581,83 +3602,90 @@ public static function course_completed(\core\event\course_completed $event) {
$courseid = $data['courseid'];
$timecompleted = $data['timecreated'];

// Get the enrolment record as the completion record isn't fully formed at this point.
$enrolrec = $DB->get_record_sql("SELECT ue.* FROM {user_enrolments} ue
JOIN {enrol} e ON (ue.enrolid = e.id)
WHERE ue.userid = :userid
AND e.courseid = :courseid
AND e.status = 0",
array('userid' => $userid,
'courseid' => $courseid));

// Do not send if this is already recorded.
if (!empty($enrolrec->timestart) &&
$DB->get_record_sql("SELECT id FROM {local_iomad_track}
WHERE userid=:userid
AND courseid = :courseid
AND timeenrolled = :timeenrolled
AND timecompleted IS NOT NULL",
array('userid' => $userid,
'courseid' => $courseid,
'timeenrolled' => $enrolrec->timestart))) {
return true;
}
$usercompanies = \company::by_userid_list($userid);
foreach ($usercompanies as $company){

// Get the enrolment record as the completion record isn't fully formed at this point.
$enrolrec = $DB->get_record_sql("SELECT ue.* FROM {user_enrolments} ue
JOIN {enrol} e ON (ue.enrolid = e.id)
WHERE ue.userid = :userid
AND e.courseid = :courseid
AND e.status = 0",
array('userid' => $userid,
'courseid' => $courseid));

// Do not send if this is already recorded.
if ($DB->get_record_sql("SELECT id FROM {local_iomad_track}
WHERE userid=:userid
AND courseid = :courseid
AND timeenrolled = :timeenrolled
AND companyid = :companyid
AND timecompleted IS NOT NULL",
array('userid' => $userid,
'courseid' => $courseid,
'timeenrolled' => $enrolrec->timestart,
'companyid' => $company->id ))) {
continue;
}

// Need to make sure any certificate is created.
sleep(9);
// Need to make sure any certificate is created.
sleep(9);

$course = $DB->get_record('course', array('id' => $courseid));
$user = $DB->get_record('user', array('id' => $userid));
$companyinfo = self::get_company_byuserid($userid);
$company = new company($companyinfo->id);
$course = $DB->get_record('course', array('id' => $courseid));
$user = $DB->get_record('user', array('id' => $userid));

// Deal with attachment.
$trackinfos = $DB->get_records_sql('SELECT * FROM {local_iomad_track}
WHERE userid = :userid
AND courseid = :courseid
ORDER BY id DESC',
array('userid' => $userid, 'courseid' => $courseid), 0, 1);
$attachment = new stdclass();
$trackinfo = array_pop($trackinfos);
if ($trackfileinfo = $DB->get_record('local_iomad_track_certs', array('trackid' => $trackinfo->id))) {
$fileinfo = $DB->get_record('files', array('itemid' => $trackinfo->id, 'component' => 'local_iomad_track', 'filename' => $trackfileinfo->filename));
$filedir1 = substr($fileinfo->contenthash,0,2);
$filedir2 = substr($fileinfo->contenthash,2,2);
$attachment->filepath = $CFG->dataroot . '/filedir/' . $filedir1 . '/' . $filedir2 . '/' . $fileinfo->contenthash;
$attachment->filename = $trackfileinfo->filename;
} else {
$attachment = null;
}

$complete = false;
if ($licenses = $DB->get_records_sql("SELECT * FROM {companylicense_users} clu
JOIN {companylicense} cl
ON (clu.licenseid = cl.id and cl.program = 1)
WHERE clu.userid = :userid
AND clu.licenseid = (
SELECT licenseid FROM {companylicense_users}
WHERE userid = :userid2
AND licensecourseid = :courseid)",
array('userid' => $user->id, 'userid2' => $user->id, 'courseid' => $courseid))) {
foreach ($licenses as $license) {
if ($license->isusing && $DB->get_record_sql("SELECT id FROM {course_completions}
WHERE userid = :userid
AND course = :courseid
AND timecompleted IS NOT NULL",
array('courseid' => $license->licensecourseid,
'userid' => $user->id))) {
$complete = true;
} else {
$complete = false;
// Deal with attachment.
$trackinfos = $DB->get_records_sql('SELECT * FROM {local_iomad_track}
WHERE userid = :userid
AND courseid = :courseid
and companyid = :companyid
ORDER BY id DESC',
array('userid' => $userid,
'courseid' => $courseid,
'companyid' => $company->id
), 0, 1);
$attachment = new stdclass();
$trackinfo = array_pop($trackinfos);
if ($trackfileinfo = $DB->get_record('local_iomad_track_certs', array('trackid' => $trackinfo->id))) {
$fileinfo = $DB->get_record('files', array('itemid' => $trackinfo->id, 'component' => 'local_iomad_track', 'filename' => $trackfileinfo->filename));
$filedir1 = substr($fileinfo->contenthash,0,2);
$filedir2 = substr($fileinfo->contenthash,2,2);
$attachment->filepath = $CFG->dataroot . '/filedir/' . $filedir1 . '/' . $filedir2 . '/' . $fileinfo->contenthash;
$attachment->filename = $trackfileinfo->filename;
} else {
$attachment = null;
}

$complete = false;
if ($licenses = $DB->get_records_sql("SELECT * FROM {companylicense_users} clu
JOIN {companylicense} cl
ON (clu.licenseid = cl.id and cl.program = 1)
WHERE clu.userid = :userid
AND clu.licenseid = (
SELECT licenseid FROM {companylicense_users}
WHERE userid = :userid2
AND licensecourseid = :courseid)",
array('userid' => $user->id, 'userid2' => $user->id, 'courseid' => $courseid))) {
foreach ($licenses as $license) {
if ($license->isusing && $DB->get_record_sql("SELECT id FROM {course_completions}
WHERE userid = :userid
AND course = :courseid
AND timecompleted IS NOT NULL",
array('courseid' => $license->licensecourseid,
'userid' => $user->id))) {
$complete = true;
} else {
$complete = false;
}
}
}
}
if (!$complete) {
EmailTemplate::send('completion_course_user', array('course' => $course, 'user' => $user, 'company' => $company, 'attachment' => $attachment));
$supervisortemplate = new EmailTemplate('completion_course_supervisor', array('course' => $course, 'user' => $user, 'company' => $company, 'attachment' => $attachment));
$supervisortemplate->email_supervisor();
} else {
EmailTemplate::send('user_programcompleted', array('course' => $course, 'user' => $user, 'company' => $company, 'attachment' => $attachment));
if (!$complete) {
EmailTemplate::send('completion_course_user', array('course' => $course, 'user' => $user, 'company' => $company, 'attachment' => $attachment));
$supervisortemplate = new EmailTemplate('completion_course_supervisor', array('course' => $course, 'user' => $user, 'company' => $company, 'attachment' => $attachment));
$supervisortemplate->email_supervisor();
} else {
EmailTemplate::send('user_programcompleted', array('course' => $course, 'user' => $user, 'company' => $company, 'attachment' => $attachment));
}
}

return true;
Expand Down