Adding department to Certificate E-mail

Adding department to Certificate E-mail

por Bryan Dearlove -
Número de respostas: 7
Good Day All,
We are currently rolling our Moodle throughout our 2300 user hospital with so far great success!

I have run into our first roadblock, they would like to add the department name of the user to the certificate E-mail, unfortunately I am not skilled enough at PHP in order to accomplish this. Would anyone have knowledge of what code I would add?

I already have the department added to the users profiles and I was able to edit the certificate report to add he department there, but cannot figure out the E-mail.

Any help would be greatly appreciated!
Em resposta a 'Bryan Dearlove'

Re: Adding department to Certificate E-mail

por Raymond Fürst -
Do you want to have the department appear in the email going to the student or in the notification going to the teacher?

Take a look at %moodledir%/mod/certificate/lib.php
This file contains the functions certificate_email_students and certificate_email_teachers that prepare the mail generation.

Take a look at the certificate language file. Here the strings "emailstudenttext" and "emailteachermail" define the actual contents of the mails.

Consider adding a line like $info->department = $student->department to the desired function in lib.php.
Then you can insert "$a->department" into the mail text where the department name should appear.

I did not test it, so I do not know if it works.
Em resposta a 'Raymond Fürst'

Re: Adding department to Certificate E-mail

por Bryan Dearlove -
I am so close, just need a bit more. Would you know how I get the department into the lib file?

I have added to lib: $info->department = $student;

And have added certificate.php: $a->department and the student name appears perfectly in the E-mails. I have tried changing lib to:
$info->department = $department;

but get nothing. I am assuming I need a method to get department into lib. so, so close!

Em resposta a 'Bryan Dearlove'

Re: Adding department to Certificate E-mail

por Raymond Fürst -
You should add
$info->department = $user->department;

$user is the user record of the student, department is a field in the user table. $user->department should point to the department-field of the desired student.

$user is initialized by the get_record() function (in certificate_email_teachers) or passed as a parameter (in certificate_email_students)

(sorry I mistook $student for $user in my previous answer)
Em resposta a 'Raymond Fürst'

Re: Adding department to Certificate E-mail

por Bryan Dearlove -
It looks as though it is something I am doing incorrect with lib.php. I added the function as I believe it should be but no luck. If I change it to:

$info->department = $student
I receive the student name correctly in the e-mail.

/************************************************************************
* Alerts others by email of received certificates. First checks *
* whether the option to email others is set for this certificate. *
* Uses the email_teachers info. *
* Code suggested by Eloy Lafuente *
************************************************************************/
function certificate_email_others ($course, $certificate, $certrecord, $cm) {
global $USER, $CFG;

if ($certificate->emailothers) {
$student = $certrecord->studentname;

$others = explode(',', $certificate->emailothers);
if ($others) {
$strawarded = get_string('awarded', 'certificate');
foreach ($others as $other) {
$other = trim($other);
if (validate_email($other)) {
$destination->email = $other;
unset($info);
$info->student = $student;
$info->department = $user->department;
$info->course = format_string($course->fullname,true);
$info->certificate = format_string($certificate->name,true);
$info->url = $CFG->wwwroot.'/mod/certificate/report.php?id='.$cm->id;
$from = $student;
$postsubject = $strawarded.': '.$info->student.' -> '.$certificate->name;
$posttext = certificate_email_teachers_text($info);
$posthtml = certificate_email_teachers_html($info);

@email_to_user($destination, $from, $postsubject, $posttext, $posthtml); // If it fails, oh well, too bad.
}
}
}
}
}
Em resposta a 'Bryan Dearlove'

Re: Adding department to Certificate E-mail

por Raymond Fürst -
Compare the functions certificate_email_others() and certificate_email_teachers()

Note that $student is defined as $certrecord->studentname, that means it picks the student's name from the certificate data. The name of the student is stored in the issued certificate itself, together with the user id of the student (and other certificate related data).

The department is a field in the user table.
In certificate_email_teachers() the variable $student is filled with $certrecord->studentname as well, but in the next line all student data is stored in the variable $user ( by get_record('user', 'id', $certrecord->userid); )

After that all fields of the student can be accessed from the variable $user, hence $user->department

Maybe adding $user = get_record('user', 'id' $certrecord->userid); to the function certificate_email_others() does the trick.
Em resposta a 'Raymond Fürst'

Re: Adding department to Certificate E-mail

por Bryan Dearlove -
You are amazing! I could hug you! It worked perfectly. Incase anyone else requires it:

Within lib.php I added to function certificate_email_others:

$user = get_record('user', 'id', $certrecord->userid);
$info->department = $user->department;

Then within certificate.php I modified the E-mail text:

$a->student has received their certificate: \'$a->certificate\'
for $a->course. Department Location: $a->department




Em resposta a 'Bryan Dearlove'

Re: Adding department to Certificate E-mail

por Raymond Fürst -
Please note that the function certificate_email_teachers should also contain the line $info->department = $user->department;

Both functions use the same mail text from the language file, so both mails expect a department string now.