email scorm attempt notification to teachers

email scorm attempt notification to teachers

by Mike Glazebrook -
Number of replies: 6

I was hoping someone could help me. I am trying to send an email to each teacher of a course that they are member of when a user completes a scorm course. I have added this code int the locallib.php and it sends an email but I don't know how to get it to send to the courses teachers instead of just a hard coded email. Can anyone give a bit of assistance?

// EMAIL TEACHER ON COMPLETE
if ($track->value == 'completed') {
// the message
$msg = format_string($USER->firstname) . " " . format_string($USER->lastname) . " has Completed " . format_string($scorm->name);

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
mail("email@gmail.com","AccessCare Module Completion",$msg,"From: AccessCareResources");
}
//EMAIL TEACHER END

 

Thanks in advance!

Average of ratings: -
In reply to Mike Glazebrook

Re: email scorm attempt notification to teachers

by Mike Glazebrook -

Do what I really need to do is generate a list of email addresses that belong to the course teachers.

(example1@moodle.com, example2@moodle.com, example3@moodle.com)

 

also bump

 

In reply to Mike Glazebrook

Re: email scorm attempt notification to teachers

by Itamar Tzadok -

You can use get_role_users in the context to get the users with role teacher in the course.

See https://github.com/moodle/moodle/blob/c0e88129d16f4ab0f7687990b892ec3f0949623b/lib/accesslib.php#L4052.

You may want to consider to send_message via the Moodle messaging system rather than sending email directly. By default it would send an email too.

smile

Average of ratings: Useful (1)
In reply to Itamar Tzadok

Re: email scorm attempt notification to teachers

by Mike Glazebrook -

Thanks that is the info I was looking for to get me past my hurdle here. I will post back if I can get something working with either method.

 

Thanks again!

In reply to Mike Glazebrook

Re: email scorm attempt notification to teachers

by Mike Glazebrook -

If anyone could help me that has more expertise on this sort of thing I would REALLY appreciate it. I know the query is good in phpmyadmin. It does get all manager rolls which isn't perfect but I can't even get those to email. I figured I can get the context piece figured out after I get this working. Here is the bit of code that I am trying to pass:

 

// EMAIL TEACHER ON COMPLETE
if ($track->value == 'completed') {
// the message
$msg = format_string($USER->firstname) . " " . format_string($USER->lastname) . " has Completed " . format_string($scorm->name);

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

//get email address for every teacher in the course.
$courseid = $COURSE->id;
$query ="SELECT u.email FROM mdl1_user u, mdl1_role_assignments r, mdl1_context cx, mdl1_course c WHERE u.id = r.userid
AND cx.instanceid = c.id AND r.roleid = 1 AND r.contextid = 1 GROUP BY u.email";

$result = $DB->get_records_sql($query, array('email' => $mgmail));
foreach ($result as $r){
$mail = $r . ",";
}
$to = $mail;
// send email
mail($to,"AccessCare Module Completion",$msg,"From: AccessCareResources");
}
//EMAIL TEACHER END.

In reply to Mike Glazebrook

Re: email scorm attempt notification to teachers

by Itamar Tzadok -
$context = context_course::instance($courseid);
$roleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
$roleusers = get_role_users($roleid, $context);
foreach($roleusers as $user) {
$email = $user->email;
   // Etc.
}

Better use the API than construct all kinds of direct queries that may work today and break tomorrow.

hth smile

 

Average of ratings: Useful (2)
In reply to Itamar Tzadok

Re: email scorm attempt notification to teachers

by Mike Glazebrook -

Thanks for that. I just didn't know where to start writing the code.