## General developer forum

### A line of code needed ...

A line of code needed ...

Hi. I'm trying to finish a plugin that will provide a feature that a colleague has been crying out for: a grade mailout. I've copied the entire "User Report" plugin and successfully installed it as a new Grade plugin, and I am looking for a line of code that, instead of printing the User Report, will mail it out instead. An experienced programmer could probably do this in 10 minutes, rather than the week I expect it will take me.

Can anybody help with this?

Thanks

Glen

Average of ratings: -
Re: A line of code needed ...

You might want to have a look into the CRON.php code where Forum posts are sent out to the users. I think this will lead you to the Moodle-PHP-functions used to send emails.

Rosario

Average of ratings: -
Re: A line of code needed ...

^^ I agree with Rosario, use /admin/cron.php

Average of ratings: -
Re: A line of code needed ...

Just a thought, email is THE most unsecure method of transmitting information short of placing it in the newspapers.

Average of ratings: -
Re: A line of code needed ...

I want to mention first that so many people have asked for this feature, which an experienced programmer should be able to do in minutes. Several posters in the forums have referred to it as a "holy grail" feature. Sad that a person with my limited programming skills has to finally do it.

Re: the cron job; I know the basic PHP syntax for sending emails, but it seems to me that the User Report (Grades) does absolutely everything I want, except for mailing it. Does the User Report generate a single variable containing the report or does it generate it line by line from the dbase? If it's a single variable, then a single line of code should be able to mail it out?

Emailing grades is a standard practice here. If a hacker wants to know the grades of a student identified only by a nickname or initials, then I'm okay with that.

Thanks

Glen

Average of ratings: -
Re: A line of code needed ...

Moodle has an "email_to_user" function that you can use to send emails.  You can see an example of how it's called in message/output/email/message_output_email.php.  It should be fairly self-explanatory.

As for the report generation, I don't know much about it, but my guess would be that it generates the report line-by-line.  If it's too hard to change it so that it outputs to a variable, you can always use PHP's output buffering.  Note, however, that the email_to_user function expects both plain text and HTML versions of the message.  I don't know what it would do if you just gave it HTML.

Average of ratings: -
Re: A line of code needed ...

Thanks, I looked at the email_to_user function but it seems to do far, far more than I need. I am hoping that I need something much simpler, a single line of code inside a loop that looks something like: mail($recip, 'Grades',$report);   where $report is the output from the User Report. That would do it, right? I know I can always invest half my winter vacation looking for the few lines of code to do this, but given the noise I've heard surrounding it, I find it astonishing that the feature doesn't exist. Average of ratings: - Re: A line of code needed ... Speaking out of my experience, when you do not find the "simple" solution you want, then it is because nobody else is interested in such a solution. If it is a problem, then nobody else has that problem. Or it must be because you are looking at things in a completely other way, eg. you want to send the report by email whilst everybody else looks at it online in Moodle so as to get the most recent version. So finally you need code that: • decides which user report to use • collects this report into something you can send as email • automates this function somehow like a cron job unless you want to send the reports by clicking somewhere in Moodle's GUI I know you can write plugins in a manner that Moodle's built in cron.php will execute/call it for you. I know you can write own plugins like my uploadusersandcourses_silently.php you can integrate into Moodle and call/trigger from the OS' cron table. And for certain we all will be able to find the different mail-functions either Moodle uses or PHP offers natively. But obviously the great work to do is not sending out the email but implementing the whole workflow I mentioned. Rosario Average of ratings: - Re: A line of code needed ... Hey Glen, you could always just use a plain old mail() function http://php.net/manual/en/function.mail.php Average of ratings: - Re: A line of code needed ... 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;
$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: -
Re: A line of code needed ...

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: -