function referentiel_editingteachers($id_course) { //liste des profs d'un cours // version MOODLE 2.0 global $DB; global $CFG; $liste = ""; //if ($CFG->version < 2011120100) { $context = context_course::instance($id_course); //} else { // $context = context_course::instance($id_course); //} if ($context) { // print_object ($context); $roles = $DB->get_records_sql("SELECT DISTINCT r.*, ra.userid as userid\n FROM {role_assignments} ra, {role} r\n WHERE r.archetype = 'editingteacher'\n AND ra.roleid = r.id\n AND ra.contextid = ?", array($context->id)); if ($roles) { foreach ($roles as $p) { // echo "<br />\n"; // print_object ($p); if ($user = referentiel_get_user($p->userid)) { $liste .= $user->firstname . ' ' . $user->lastname . '<' . $user->email . '>'; } } } } return $liste; }
/** * Given an activite record this function returns a list of teachers * that may be sent a notification. * * @param activite object * @return string * @todo Finish documenting this function **/ function referentiel_get_referents_notification($activite) { // retourne la liste des enseignants et tuteurs qui seront notifies // Créé par JF pour la Version 6.xx // Decalque du traitement des referents destinataire dans referentiel_cron_activite() $destinataires = new stdClass(); $destinataires->liste_destinataires = ''; $destinataires->nbdestinataires = 0; $teachers = array(); // liste des enseignants associes à cette activite if (!empty($activite->ref_instance) && !empty($activite->ref_course)) { if (!empty($activite->teacherid)) { // referent de l'activite $teachers[] = referentiel_get_user($activite->teacherid); } else { // on recherche les accompagnateurs $teachers_repartition = array(); $teachers_repartition = referentiel_get_repartition_competences($activite->ref_instance, $activite->ref_course, $activite->competences_activite, $teachers_repartition); $teachers = referentiel_get_accompagnements_user($activite->ref_instance, $activite->ref_course, $activite->userid); // verifier si intersection $teachers = referentiel_intersection_teachers($teachers_repartition, $teachers); } if (empty($teachers)) { // notifier tous les enseignants sauf les administrateurs et createurs de cours $teachers = referentiel_get_teachers_course($activite->ref_course); } if ($teachers) { // DEBUG //mtrace("\nlib.php :: referentiel_get_destinataires_notification :: 147 :: TEACHERS\n"); //print_r($teachers); //mtrace("\n"); foreach ($teachers as $teacher) { // DEBUG //mtrace("\nlib.php :: 154 :: TEACHER\n"); //print_object($teacher); //mtrace("\n"); if (!empty($teacher->userid)) { if ($user = referentiel_get_user($teacher->userid)) { /* u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest, u.emailstop, u.imagealt, u.email, u.city, u.country, u.lastaccess, u.lastlogin, u.picture, u.timezone, u.theme, u.lang, u.trackforums, u.mnethostid */ if (!$user->emailstop) { $email = ''; if ($user->maildisplay) { $email = ' ' . $user->email; } $destinataires->nbdestinataires++; $destinataires->liste_destinataires .= ' ' . mb_strtoupper($user->lastname, 'UTF-8') . ' ' . mb_convert_case($user->firstname, MB_CASE_TITLE, 'UTF-8') . $email . ' ::'; } } } } } } if ($destinataires->nbdestinataires) { $destinataires->liste_destinataires = '<b>' . $activite->type_activite . '</b><br /> ' . $destinataires->liste_destinataires; } return $destinataires; }
function referentiel_cron_taches() { global $CFG, $USER; global $DB; $cronuser = clone $USER; $site = get_site(); // all users that are subscribed to any post that needs sending $users = array(); // status arrays $mailcount = array(); $errorcount = array(); // caches $tasks = array(); $courses = array(); $coursemodules = array(); $subscribedusers = array(); // Posts older than 2 days 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(); if (NOTIFICATION_DELAI) { $endtime = $timenow - $CFG->maxeditingtime; } else { $endtime = $timenow; } $starttime = $endtime - NOTIFICATION_INTERVALLE_JOUR * 24 * 3600; // Two days earlier // JF // DEBUG : cron_lib.php : mtrace("DEBUT CRON REFERENTIEL TACHES"); $taches = referentiel_get_unmailed_tasks($starttime, $endtime); if ($taches) { if (REFERENTIEL_DEBUG) { mtrace("\nlib_cron.php :: 1675 :: TACHES\n"); print_r($taches); mtrace("\n"); } // Mark them all now as being mailed. It's unlikely but possible there // might be an error later so that a post is NOT actually mailed out, // but since mail isn't crucial, we can accept this risk. Doing it now // prevents the risk of duplicated mails, which is a worse problem. if (!referentiel_mark_old_tasks_as_mailed($endtime)) { mtrace('Errors occurred while trying to mark some referentiel tasks as being mailed.'); return false; // Don't continue trying to mail them, in case we are in a cron loop } // checking task validity, and adding users to loop through later foreach ($taches as $tid => $task) { // DEBUG : cron_lib.php : // mtrace('task '.$task->id.' '.$tid); if (REFERENTIEL_DEBUG) { mtrace("\nlib_cron.php :: 1694 :: TASK\n"); print_r($task); mtrace("\n"); } if (!isset($tasks[$tid])) { $tasks[$tid] = $task; } // cours $courseid = $tasks[$tid]->ref_course; if (!isset($courses[$courseid])) { if ($course = $DB->get_record("course", array("id" => "{$courseid}"))) { $courses[$courseid] = $course; } else { mtrace('Could not find course ' . $courseid); unset($tasks[$tid]); continue; } } // modules $instanceid = $task->ref_instance; if (!isset($coursemodules[$instanceid])) { if ($cm = get_coursemodule_from_instance('referentiel', $instanceid, $courseid)) { $coursemodules[$instanceid] = $cm; } else { mtrace('./mod/referentiel/lib.php : 1355 : Could not load course module for referentiel instance ' . $instanceid); unset($tasks[$tid]); continue; } } // caching subscribed students of each task $students = array(); $teachers = referentiel_get_teachers_course($courseid); // DEBUG : cron_lib.php : if (!isset($subscribedusers[$tid])) { // notifier tous les etudiants // ICI MODIFIER LA COLLECTE // NE PRENDRE QUE LES ETUDIANTS ACCOMPAGNES // $students=referentiel_get_students_accompagne($courseid,$task->auteurid); $students = referentiel_get_accompagnements_teacher($task->ref_instance, $task->ref_course, $task->auteurid); if (empty($students)) { $students = referentiel_get_students_course($courseid, 0, 0, false); } if ($students) { foreach ($students as $student) { $user = referentiel_get_user($student->userid); if ($user->emailstop) { if (!empty($CFG->forum_logblocked)) { // add_to_log(SITEID, 'referentiel', 'mail blocked', '', '', 0, $user->id); } } else { // this user is subscribed to this notification $subscribedusers[$tid][$student->userid] = $student->userid; // this user is a user we have to process later $users[$student->userid] = $user; if (REFERENTIEL_DEBUG) { mtrace("\nDEBUG :: lib_cron:: 1758 :: \nDESTINATAIRE ETUDIANT ID: " . $user->id . "\n"); //print_r($user); } } } } unset($students); // release memory if (NOTIFICATION_TACHES_AUX_REFERENTS) { // if ($teachers) { foreach ($teachers as $teacher) { $user = referentiel_get_user($teacher->userid); if ($user->emailstop) { if (!empty($CFG->forum_logblocked)) { // add_to_log(SITEID, 'referentiel', 'mail blocked', '', '', 0, $teacher->userid); } } else { // this user is subscribed to this notification $subscribedusers[$tid][$user->id] = $user->id; // this user is a user we have to process later $users[$user->id] = $user; // DEBUG : cron_lib.php : if (REFERENTIEL_DEBUG) { mtrace("\nDEBUG :: lib_cron:: 1782 :: \nDESTINATAIRE TEACHER: " . $user->id . " \n"); //print_r($user); } } } } } unset($teachers); // release memory $mailcount[$tid] = 0; $errorcount[$tid] = 0; } } } if ($users && $tasks) { // DEBUG : cron_lib.php : if (REFERENTIEL_DEBUG) { mtrace("TRAITEMENT DES MESSAGES TACHES\n"); } $urlinfo = parse_url($CFG->wwwroot); $hostname = $urlinfo['host']; foreach ($users as $userto) { @set_time_limit(TIME_LIMIT); // terminate if processing of any account takes longer than 2 minutes // set this so that the capabilities are cached, and environment matches receiving user $USER = $userto; // mtrace('./mod/referentiel/lib.php :: Line 253 : Processing user '.$userto->id); // init caches $userto->viewfullnames = array(); // $userto->canpost = array(); // $userto->markposts = array(); $userto->enrolledin = array(); // reset the caches foreach ($coursemodules as $coursemoduleid => $unused) { $coursemodules[$coursemoduleid]->cache = new object(); $coursemodules[$coursemoduleid]->cache->caps = array(); unset($coursemodules[$coursemoduleid]->uservisible); } foreach ($tasks as $tid => $task) { // Set up the environment for activity, course $course = $courses[$task->ref_course]; $cm =& $coursemodules[$task->ref_instance]; // Do some checks to see if we can mail out now if (!isset($subscribedusers[$tid][$userto->id])) { continue; // user does not subscribe to this activity } // Get info about the author user if (array_key_exists($task->auteurid, $users)) { // we might know him/her already but we need a really complete user object, so.. get it again! if ($userfrom = $DB->get_record("user", array("id" => "{$task->auteurid}"))) { $users[$task->auteurid] = $userfrom; } else { // error mtrace('Could not find user ' . $task->auteurid); continue; } } else { if ($userfrom = $DB->get_record("user", array("id" => "{$task->auteurid}"))) { $users[$userfrom->id] = $userfrom; // fetch only once, we can add it to user list, it will be skipped anyway } else { mtrace('Could not find user ' . $task->auteurid); continue; } } // setup global $COURSE properly - needed for roles and languages // course_setup($course); // More environment // setup global $COURSE properly - needed for roles and languages cron_setup_user($userto, $course); // Fill caches if (!isset($userto->viewfullnames[$tid])) { $modcontext = context_module::instance($cm->id); $userto->viewfullnames[$tid] = has_capability('moodle/site:viewfullnames', $modcontext); } if (!isset($userfrom->groups[$tid])) { if (!isset($userfrom->groups)) { $userfrom->groups = array(); $users[$userfrom->id]->groups = array(); } $userfrom->groups[$tid] = groups_get_all_groups($course->id, $userfrom->id, $cm->groupingid); $users[$userfrom->id]->groups[$tid] = $userfrom->groups[$tid]; } // OK so we need to send the email. // Does the user want this post in a digest? If so postpone it for now. if ($userto->maildigest > 0) { $queue = new object(); $queue->userid = $userto->id; $queue->activiteid = $task->id; $queue->timemodified = $task->date_modif; $queue->type = TYPE_TACHE; // 1 if (!$DB->insert_record('referentiel_notification', $queue)) { mtrace("Error: mod/referentiel/lib.php : Line 1510 : Could not queue for digest mail for id {$task->id} to user {$userto->id} ({$userto->email}) .. not trying again."); } continue; } // Prepare to actually send the post now, and build up the content $strreferentielname = get_string('referentiel', 'referentiel') . ': ' . referentiel_get_instance_name($task->ref_instance); $cleanactivityname = str_replace('"', "'", strip_tags(format_string($strreferentielname . ' -> ' . $task->type_task))); $userfrom->customheaders = array('Precedence: Bulk', 'List-Id: "' . $cleanactivityname . '" <moodle_referentiel_activity_' . $task->id . '@' . $hostname . '>', 'List-Help: ' . $CFG->wwwroot . '/mod/referentiel/task.php?d=' . $task->ref_instance . '&task_id=' . $task->id . '&mode=listtaskall', 'Message-ID: <moodle_referentiel_task_' . $task->id . '@' . $hostname . '>', 'X-Course-Id: ' . $course->id, 'X-Course-Name: ' . format_string($course->fullname, true)); if (!($cm = get_coursemodule_from_instance('referentiel', $task->ref_instance, $course->id))) { print_error('Course Module ID was incorrect'); } $postsubject = "{$course->shortname}: " . format_string($strreferentielname . ' ' . $task->type_task, true); $context = context_module::instance($cm->id); $posttext = referentiel_make_mail_text(TYPE_TACHE, $context, $course, $task, $userfrom, $userto); $posthtml = referentiel_make_mail_html(TYPE_TACHE, $context, $course, $task, $userfrom, $userto); // Send the post now! // mtrace('Sending ', ''); if (!($mailresult = email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml, '', '', $CFG->forum_replytouser))) { mtrace("Error: Could not send out mail for id {$task->id} to user {$userto->id}" . " ({$userto->email}) .. not trying again."); // add_to_log($course->id, 'referentiel', 'mail error', "task $task->id to $userto->id ($userto->email)","", $cm->id, $userto->id); $errorcount[$tid]++; } else { if ($mailresult === 'emailstop') { // should not be reached anymore - see check above } else { $mailcount[$tid]++; } } } } } if ($tasks) { foreach ($tasks as $task) { mtrace($mailcount[$task->id] . " users were sent task {$task->id}, {$task->type_task}"); if ($errorcount[$task->id]) { $DB->set_field("referentiel_task", "mailed", "2", array("id" => "{$task->id}")); } } } // release some memory unset($subscribedusers); unset($mailcount); unset($errorcount); $USER = clone $cronuser; // course_setup(SITEID); cron_setup_user(); $sitetimezone = $CFG->timezone; // Now see if there are any digest mails waiting to be sent, and if we should send them mtrace('Starting digest processing...'); @set_time_limit(TIME_LIMIT * 2); // terminate if not able to fetch all digests in 5 minutes if (!isset($CFG->digesttasktimelast)) { // To catch the first time set_config('digesttasktimelast', 0); } $timenow = time(); $digesttime = usergetmidnight($timenow, $sitetimezone) + $CFG->digestmailtime * 3600; // Delete any really old ones (normally there shouldn't be any) $weekago = $timenow - 7 * 24 * 3600; $DB->delete_records_select('referentiel_notification', "timemodified < {$weekago} AND type='" . TYPE_TACHE . "'", NULL); mtrace('Cleaned old digest records'); if ($CFG->digesttasktimelast < $digesttime and $timenow > $digesttime) { mtrace("Sending task digests: " . userdate($timenow, '', $sitetimezone)); $params = array("digesttime" => "{$digesttime}", "type" => TYPE_TACHE); $sql = " (timemodified < :digesttime) AND (type=:type) "; $digestposts_rs = $DB->get_recordset_select('referentiel_notification', $sql, $params); // if (!rs_EOF($digestposts_rs)) { // deprecated if ($digestposts_rs->valid()) { // We have work to do $usermailcount = 0; //caches - reuse the those filled before too $userposts = array(); // while ($digestpost = rs_fetch_next_record($digestposts_rs)) { foreach ($digestposts_rs as $digestpost) { if (!isset($users[$digestpost->userid])) { if ($user = $DB->get_record("user", array("id" => "{$digestpost->userid}"))) { $users[$digestpost->userid] = $user; } else { continue; } } $postuser = $users[$digestpost->userid]; if ($postuser->emailstop) { if (!empty($CFG->forum_logblocked)) { // add_to_log(SITEID, 'referentiel', 'mail blocked', '', '', 0, $postuser->id); } continue; } // contenu activite if (!isset($taches[$digestpost->activiteid])) { if ($tache = $DB->get_record("referentiel_task", array("id" => "{$digestpost->activiteid}"))) { $taches[$digestpost->activiteid] = $tache; } else { continue; } } $courseid = $taches[$digestpost->activiteid]->ref_course; if (!isset($courses[$courseid])) { if ($course = $DB->get_record("course", array("id" => "{$courseid}"))) { $courses[$courseid] = $course; } else { continue; } } if (!isset($coursemodules[$taches[$digestpost->activiteid]->ref_instance]) && $taches[$digestpost->activiteid]) { if ($cm = get_coursemodule_from_instance('referentiel', $taches[$digestpost->activiteid]->ref_instance, $courseid)) { $coursemodules[$taches[$digestpost->activiteid]->ref_instance] = $cm; } else { continue; } } $userposts[$digestpost->userid][$digestpost->activiteid] = $digestpost->activiteid; } $digestposts_rs->close(); /// Finished iteration, let's close the resultset // Data collected, start sending out emails to each user // foreach ($userdiscussions as $userid => $thesediscussions) { foreach ($userposts as $userid => $theseactivities) { @set_time_limit(TIME_LIMIT); // terminate if processing of any account takes longer than 2 minutes $USER = $cronuser; // course_setup(SITEID); // reset cron user language, theme and timezone settings cron_setup_user(); mtrace(get_string('processingdigest', 'referentiel', $userid), '... '); // First of all delete all the queue entries for this user $DB->delete_records_select('referentiel_notification', "userid = {$userid} AND timemodified < {$digesttime} AND type='" . TYPE_TACHE . "'", NULL); $userto = $users[$userid]; // Override the language and timezone of the "current" user, so that // mail is customised for the receiver. $USER = $userto; // course_setup(SITEID); cron_setup_user(); // init caches $userto->viewfullnames = array(); // $userto->canpost = array(); // $userto->markposts = array(); $postsubject = get_string('digestmailsubject', 'referentiel', format_string($site->shortname, true)); $headerdata = new object(); $headerdata->sitename = format_string($site->fullname, true); $headerdata->userprefs = $CFG->wwwroot . '/user/edit.php?id=' . $userid . '&course=' . $site->id; $posttext = get_string('digestmailheader', 'referentiel', $headerdata) . "\n\n"; $headerdata->userprefs = '<a target="_blank" href="' . $headerdata->userprefs . '">' . get_string('digestmailprefs', 'referentiel') . '</a>'; $posthtml = "<head>"; /* foreach ($CFG->stylesheets as $stylesheet) { $posthtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n"; } */ $posthtml .= "</head>\n<body id=\"email\">\n"; $posthtml .= '<p>' . get_string('digestmailheader', 'referentiel', $headerdata) . '</p><br /><hr size="1" noshade="noshade" />'; foreach ($theseactivities as $tid) { @set_time_limit(TIME_LIMIT); // to be reset for each post $type_notification = TYPE_TACHE; $tache = $taches[$tid]; $course = $courses[$taches[$tid]->ref_course]; $cm = $coursemodules[$taches[$tid]->ref_instance]; //override language // course_setup($course); // setup global $COURSE properly - needed for roles and languages cron_setup_user($userto, $course); // Fill caches if (!isset($userto->viewfullnames[$tache->id])) { $modcontext = context_module::instance($cm->id); $userto->viewfullnames[$tache->id] = has_capability('moodle/site:viewfullnames', $modcontext); } $strreferentiels = get_string('referentiels', 'referentiel'); $posttext .= "\n \n"; $posttext .= '====================================================================='; $posttext .= "\n \n"; $posttext .= "{$course->shortname} -> {$strreferentiels} -> " . format_string($tache->type_task, true); $posttext .= "\n"; $posthtml .= "<p><font face=\"sans-serif\">" . "<a target=\"_blank\" href=\"{$CFG->wwwroot}/course/view.php?id={$course->id}\">{$course->shortname}</a> -> " . "<a target=\"_blank\" href=\"{$CFG->wwwroot}/mod/referentiel/index.php?id={$course->id}\">{$strreferentiels}</a> -> " . "<a target=\"_blank\" href=\"{$CFG->wwwroot}/mod/referentiel/tache.php?id={$tache->ref_instance}&activite_id={$tache->id}\">" . format_string($tache->type_task, true) . "</a>"; $posthtml .= "</font></p>"; $posthtml .= '<p>'; //$postsarray = $userposts[$tache->id]; $postsarray = $userposts[$userid]; sort($postsarray); foreach ($postsarray as $activiteid) { $post = $taches[$activiteid]; if (array_key_exists($post->auteurid, $users)) { // we might know him/her already $userfrom = $users[$post->auteurid]; } else { if ($userfrom = $DB->get_record("user", array("id" => "{$post->auteurid}"))) { $users[$userfrom->id] = $userfrom; // fetch only once, we can add it to user list, it will be skipped anyway } else { mtrace('Could not find user ' . $post->auteurid); continue; } } if (!isset($userfrom->groups[$post->id])) { if (!isset($userfrom->groups)) { $userfrom->groups = array(); $users[$userfrom->id]->groups = array(); } $userfrom->groups[$post->id] = groups_get_all_groups($course->id, $userfrom->id, $cm->groupingid); $users[$userfrom->id]->groups[$post->id] = $userfrom->groups[$post->id]; } $userfrom->customheaders = array("Precedence: Bulk"); if ($userto->maildigest == 2) { // Subjects only $by = new object(); $by->name = fullname($userfrom); $by->date = userdate($post->date_modif); $posttext .= "\n" . format_string($post->type_task, true) . ' ' . get_string("bynameondate", "referentiel", $by); $posttext .= "\n---------------------------------------------------------------------"; $by->name = "<a target=\"_blank\" href=\"{$CFG->wwwroot}/user/view.php?id={$userfrom->id}&course={$course->id}\">{$by->name}</a>"; $posthtml .= '<div><a target="_blank" href="' . $CFG->wwwroot . '/mod/referentiel/task.php?d=' . $post->ref_instance . '&activite_id=' . $post->id . '">' . format_string($post->type_activite, true) . '</a> ' . get_string("bynameondate", "referentiel", $by) . '</div>'; } else { $context = context_module::instance($cm->id); $posttext = referentiel_make_mail_text(TYPE_TACHE, $context, $course, $post, $userfrom, $userto, true); $posthtml = referentiel_make_mail_post(TYPE_TACHE, $context, $course, $post, $userfrom, $userto, false, true, false); } } $posthtml .= '<hr size="1" noshade="noshade" /></p>'; } $posthtml .= '</body>'; if ($userto->mailformat != 1) { // This user DOESN'T want to receive HTML $posthtml = ''; } if (!($mailresult = email_to_user($userto, $site->shortname, $postsubject, $posttext, $posthtml, '', '', $CFG->forum_replytouser))) { mtrace("ERROR!"); mtrace("Error: Could not send out digest mail to user {$userto->id} ({$userto->email})... not trying again.\n"); // add_to_log($course->id, 'referentiel', 'mail digest error', '', '', $cm->id, $userto->id); } else { if ($mailresult === 'emailstop') { // should not happen anymore - see check above } else { mtrace("success."); $usermailcount++; } } } } /// We have finishied all digest emails activities, update $CFG->digestactivitytimelast set_config('digesttasktimelast', $timenow); } $USER = $cronuser; // course_setup(SITEID); // reset cron user language, theme and timezone settings cron_setup_user(); if (!empty($usermailcount)) { mtrace(get_string('digestsentusers', 'referentiel', $usermailcount)); } mtrace("FIN CRON REFERENTIEL TACHE.\n"); return true; }