/** * Function to be run periodically according to the moodle cron * * Finds all assignment notifications that have yet to be mailed out, and mails them */ function assignment_cron () { global $CFG, $USER, $DB; /// first execute all crons in plugins if ($plugins = get_plugin_list('assignment')) { foreach ($plugins as $plugin=>$dir) { require_once("$dir/assignment.class.php"); $assignmentclass = "assignment_$plugin"; $ass = new $assignmentclass(); $ass->cron(); } } /// Notices older than 1 day will not be mailed. This is to avoid the problem where /// cron has not been running for a long time, and then suddenly people are flooded /// with mail from the past few weeks or months $timenow = time(); $endtime = $timenow - $CFG->maxeditingtime; $starttime = $endtime - 24 * 3600; /// One day earlier if ($submissions = assignment_get_unmailed_submissions($starttime, $endtime)) { $realuser = clone($USER); foreach ($submissions as $key => $submission) { $DB->set_field("assignment_submissions", "mailed", "1", array("id"=>$submission->id)); } $timenow = time(); foreach ($submissions as $submission) { echo "Processing assignment submission $submission->id\n"; if (! $user = $DB->get_record("user", array("id"=>$submission->userid))) { echo "Could not find user $user->id\n"; continue; } if (! $course = $DB->get_record("course", array("id"=>$submission->course))) { echo "Could not find course $submission->course\n"; continue; } /// Override the language and timezone of the "current" user, so that /// mail is customised for the receiver. cron_setup_user($user, $course); if (!is_enrolled(get_context_instance(CONTEXT_COURSE, $submission->course), $user->id)) { echo fullname($user)." not an active participant in " . format_string($course->shortname) . "\n"; continue; } if (! $teacher = $DB->get_record("user", array("id"=>$submission->teacher))) { echo "Could not find teacher $submission->teacher\n"; continue; } if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) { echo "Could not find course module for assignment id $submission->assignment\n"; continue; } if (! $mod->visible) { /// Hold mail notification for hidden assignments until later continue; } $strassignments = get_string("modulenameplural", "assignment"); $strassignment = get_string("modulename", "assignment"); $assignmentinfo = new stdClass(); $assignmentinfo->teacher = fullname($teacher); $assignmentinfo->assignment = format_string($submission->name,true); $assignmentinfo->url = "$CFG->wwwroot/mod/assignment/view.php?id=$mod->id"; $postsubject = "$course->shortname: $strassignments: ".format_string($submission->name,true); $posttext = "$course->shortname -> $strassignments -> ".format_string($submission->name,true)."\n"; $posttext .= "---------------------------------------------------------------------\n"; $posttext .= get_string("assignmentmail", "assignment", $assignmentinfo)."\n"; $posttext .= "---------------------------------------------------------------------\n"; if ($user->mailformat == 1) { // HTML $posthtml = "<p><font face=\"sans-serif\">". "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->". "<a href=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</a> ->". "<a href=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">".format_string($submission->name,true)."</a></font></p>"; $posthtml .= "<hr /><font face=\"sans-serif\">"; $posthtml .= "<p>".get_string("assignmentmailhtml", "assignment", $assignmentinfo)."</p>"; $posthtml .= "</font><hr />"; } else { $posthtml = ""; } $eventdata = new stdClass(); $eventdata->modulename = 'assignment'; $eventdata->userfrom = $teacher; $eventdata->userto = $user; $eventdata->subject = $postsubject; $eventdata->fullmessage = $posttext; $eventdata->fullmessageformat = FORMAT_PLAIN; $eventdata->fullmessagehtml = $posthtml; $eventdata->smallmessage = ''; $eventdata->name = 'assignment_updates'; $eventdata->component = 'mod_assignment'; $eventdata->notification = 1; message_send($eventdata); } cron_setup_user(); } return true; }
/** * Function to be run periodically according to the moodle cron * * Finds all assignment notifications that have yet to be mailed out, and mails them */ function assignment_cron() { global $CFG, $USER; /// first execute all crons in plugins if ($plugins = get_list_of_plugins('mod/assignment/type')) { foreach ($plugins as $plugin) { require_once "{$CFG->dirroot}/mod/assignment/type/{$plugin}/assignment.class.php"; $assignmentclass = "assignment_{$plugin}"; $ass = new $assignmentclass(); $ass->cron(); } } /// Notices older than 1 day will not be mailed. This is to avoid the problem where /// cron has not been running for a long time, and then suddenly people are flooded /// with mail from the past few weeks or months $timenow = time(); $endtime = $timenow - $CFG->maxeditingtime; $starttime = $endtime - 24 * 3600; /// One day earlier if ($submissions = assignment_get_unmailed_submissions($starttime, $endtime)) { $CFG->enablerecordcache = true; // We want all the caching we can get $realuser = clone $USER; foreach ($submissions as $key => $submission) { if (!set_field("assignment_submissions", "mailed", "1", "id", "{$submission->id}")) { echo "Could not update the mailed field for id {$submission->id}. Not mailed.\n"; unset($submissions[$key]); } } $timenow = time(); foreach ($submissions as $submission) { echo "Processing assignment submission {$submission->id}\n"; if (!($user = get_record("user", "id", "{$submission->userid}"))) { echo "Could not find user {$post->userid}\n"; continue; } if (!($course = get_record("course", "id", "{$submission->course}"))) { echo "Could not find course {$submission->course}\n"; continue; } /// Override the language and timezone of the "current" user, so that /// mail is customised for the receiver. $USER = $user; course_setup($course); if (!has_capability('moodle/course:view', get_context_instance(CONTEXT_COURSE, $submission->course), $user->id)) { echo fullname($user) . " not an active participant in " . format_string($course->shortname) . "\n"; continue; } if (!($teacher = get_record("user", "id", "{$submission->teacher}"))) { echo "Could not find teacher {$submission->teacher}\n"; continue; } if (!($mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id))) { echo "Could not find course module for assignment id {$submission->assignment}\n"; continue; } if (!$mod->visible) { /// Hold mail notification for hidden assignments until later continue; } $strassignments = get_string("modulenameplural", "assignment"); $strassignment = get_string("modulename", "assignment"); $assignmentinfo = new object(); $assignmentinfo->teacher = fullname($teacher); $assignmentinfo->assignment = format_string($submission->name, true); $assignmentinfo->url = "{$CFG->wwwroot}/mod/assignment/view.php?id={$mod->id}"; $postsubject = "{$course->shortname}: {$strassignments}: " . format_string($submission->name, true); $posttext = "{$course->shortname} -> {$strassignments} -> " . format_string($submission->name, true) . "\n"; $posttext .= "---------------------------------------------------------------------\n"; $posttext .= get_string("assignmentmail", "assignment", $assignmentinfo) . "\n"; $posttext .= "---------------------------------------------------------------------\n"; if ($user->mailformat == 1) { // HTML $posthtml = "<p><font face=\"sans-serif\">" . "<a href=\"{$CFG->wwwroot}/course/view.php?id={$course->id}\">{$course->shortname}</a> ->" . "<a href=\"{$CFG->wwwroot}/mod/assignment/index.php?id={$course->id}\">{$strassignments}</a> ->" . "<a href=\"{$CFG->wwwroot}/mod/assignment/view.php?id={$mod->id}\">" . format_string($submission->name, true) . "</a></font></p>"; $posthtml .= "<hr /><font face=\"sans-serif\">"; $posthtml .= "<p>" . get_string("assignmentmailhtml", "assignment", $assignmentinfo) . "</p>"; $posthtml .= "</font><hr />"; } else { $posthtml = ""; } if (!email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) { echo "Error: assignment cron: Could not send out mail for id {$submission->id} to user {$user->id} ({$user->email})\n"; } } $USER = $realuser; course_setup(SITEID); // reset cron user language, theme and timezone settings } return true; }