Example #1
0
/**
 * Function to be run periodically according to the moodle cron
 * Mails new conversations out to participants, checks for any new
 * participants, and cleans up expired/closed conversations
 * @return   bool   true when complete
 */
function dialogue_process_bulk_openrules()
{
    global $CFG, $DB;
    require_once $CFG->dirroot . '/mod/dialogue/locallib.php';
    mtrace('1. Dealing with bulk open rules...');
    $sql = "SELECT dbor.*\r\n              FROM {dialogue_bulk_opener_rules} dbor\r\n              JOIN {dialogue_messages} dm ON dm.conversationid = dbor.conversationid\r\n             WHERE dm.state = :bulkautomated\r\n               AND dbor.lastrun = 0\r\n                OR (dbor.includefuturemembers = 1 AND dbor.cutoffdate > dbor.lastrun)";
    $params = array('bulkautomated' => \mod_dialogue\dialogue::STATE_BULK_AUTOMATED);
    $rs = $DB->get_recordset_sql($sql, $params);
    if ($rs->valid()) {
        foreach ($rs as $record) {
            // try and die elegantly
            try {
                // setup dialogue
                $dialogue = \mod_dialogue\dialogue::instance($record->dialogueid);
                if (!$dialogue->is_visible()) {
                    mtrace(' Skipping hidden dialogue: ' . $dialogue->activityrecord->name);
                    continue;
                }
                // setup conversation
                $conversation = new \mod_dialogue\conversation($dialogue, (int) $record->conversationid);
                $withcapability = 'mod/dialogue:receive';
                $groupid = 0;
                // it either a course or a group, default to course
                $requiredfields = user_picture::fields('u');
                if ($record->type == 'group') {
                    $groupid = $record->sourceid;
                }
                $conversationsopened = 0;
                // get users that can receive
                $enrolledusers = get_enrolled_users($dialogue->context, $withcapability, $groupid, $requiredfields);
                $sentusers = $DB->get_records('dialogue_flags', array('conversationid' => $conversation->conversationid, 'flag' => \mod_dialogue\dialogue::FLAG_SENT), '', 'userid');
                $users = array_diff_key($enrolledusers, $sentusers);
                foreach ($users as $user) {
                    // don't start with author
                    if ($user->id == $conversation->author->id) {
                        continue;
                    }
                    // get a copy of the conversation
                    $copy = $conversation->copy();
                    $copy->add_participant($user->id);
                    $copy->save();
                    $copy->send();
                    // mark the sent in automated conversation, so can track who sent to
                    $conversation->set_flag(\mod_dialogue\dialogue::FLAG_SENT, $user);
                    unset($copy);
                    mtrace('  opened ' . $conversation->subject . ' with ' . fullname($user));
                    // up open count
                    $conversationsopened++;
                }
                $DB->set_field('dialogue_bulk_opener_rules', 'lastrun', time(), array('conversationid' => $record->conversationid));
                mtrace(' Opened ' . $conversationsopened . ' for conversation ' . $conversation->subject);
            } catch (moodle_exception $e) {
                mtrace($e->module . ' : ' . $e->errorcode);
            }
        }
    } else {
        mtrace(' None to process');
    }
    $rs->close();
    return true;
}