General developer forum

A line of code needed ...

Picture of Rex Lorenzo
Re: A line of code needed ...
Core developersParticularly helpful MoodlersPlugin developersPlugins guardiansTesters

I would recommend against just using straight using PHP's mail function. We wrote a wrapper for the mail function that takes into account Moodle's divertallemailsto setting. Very useful when testing something out in a development environment. 

 *  Wrapper with debugging and diverting controls for PHP's mail.
function send_mail_wrapper($to, $subj, $body='', $header='') {
    global $CFG;

    if (!empty($CFG->divertallemailsto)) {
        // change subject to have divert message
        $subj = "[DIVERTED $to] $subj";      
        // clear out old to
        $to = $CFG->divertallemailsto;
        // clear header variable, because it might contain an email address
        $header = '';        

    if (debugging() && empty($CFG->divertallemailsto)) {
        // if divertallemailsto is set, then send out email even if debugging is 
        // enabled
        debugging("TO: $to\nSUBJ: $subj\nBODY: $body\nHEADER: $header");
    } else {
        debugging("Sending real email to " . $to);
        return @mail($to, $subj, $body, $header);

    return true;
Average of ratings: -
Re: A line of code needed ...

Actually - I would still recommend using moodles email_to_user function. 

You can call it with the minimum of arguments to make it simpler:

email_to_user($user, $from, $subject, $messagetext, $messagehtml)

$from can just be a string, e.g. "Grade report"

This will honor a bunch of moodle settings like "noemailever" (which I use for development). 

That report has a class to generate the table - so to get the report as a string you can do something like this:

$gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'user', 'courseid'=>$courseid, 'userid'=>$userid)); 

$report = new grade_report_user($courseid, $gpr, $context, $userid);

if ($report->fill_table()) {

    $reporthtml = $report->print_table(true);


And finally - grades are usually very private information and I can picture lots of people being very scared about sending these via email. I would suggest you only send a link back to Moodle instead.

Regards, Damyon


Average of ratings: -
Rosario playing soccer
Re: A line of code needed ...
Plugin developers

Rex, Damyon, thank you very much. I learned a lot from your codes even if I never had a look at the grade-report-code itself.

I like the idea of sending only a link to the grade-report. But finally it is redundant, isn't it?

I have a whole university that keeps ALL the grades of their students inside a Moodle course so that every student and all teachers can look at them at any moment. Students of course can only see their own grades for privacy reasons. They do a clever trick by importing the grades of exams and other graded activities not being part of any Moodle course adding even all the grades of every single Moodle course to build a big Excel or CSV table and then import the data to the grades of one course so as to offer access by web to the students. And the teachers can correct the grades at any time online.

Again, sending such a report to every student on a weekly basis could be an option, even if the built in online solution should be enough.

I know teachers and admins can import/export the grade report, but at a first glance I miss such an option for the students. Did I miss it or would it be a simpler extension to program?

Average of ratings: -