/** * 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; }
/** * Get a users total unread message count for a dialogue course module. * * @global stdClass $USER * @global stdClass $DB * @param \mod_dialogue\dialogue $dialogue * @return int */ function dialogue_cm_unread_total(\mod_dialogue\dialogue $dialogue) { global $USER, $DB; $sql = ''; $params = array(); $dialogueid = $dialogue->activityrecord->id; $userid = $USER->id; $params['todialogueid'] = $dialogueid; $params['touserid'] = $userid; list($insql, $inparams) = $DB->get_in_or_equal(\mod_dialogue\dialogue::get_unread_states(), SQL_PARAMS_NAMED, 'un'); $params = array_merge($params, $inparams); $params['undialogueid'] = $dialogueid; $params['unuserid'] = $userid; $params['unflag'] = \mod_dialogue\dialogue::FLAG_READ; // Most restrictive: view own $sql = "SELECT\r\n (SELECT COUNT(1)\r\n FROM {dialogue_messages} dm\r\n JOIN {dialogue_participants} dp ON dp.conversationid = dm.conversationid\r\n WHERE dm.dialogueid = :todialogueid\r\n AND dp.userid = :touserid\r\n AND dm.state {$insql}) -\r\n (SELECT COUNT(1)\r\n FROM {dialogue_flags} df\r\n JOIN {dialogue_participants} dp ON dp.conversationid = df.conversationid\r\n AND dp.userid = df.userid\r\n WHERE df.dialogueid = :undialogueid\r\n AND df.userid = :unuserid\r\n AND df.flag = :unflag) AS unread"; // Least restrictive: view any if (has_capability('mod/dialogue:viewany', $dialogue->context)) { $sql = "SELECT\r\n (SELECT COUNT(1)\r\n FROM {dialogue_messages} dm\r\n WHERE dm.dialogueid = :todialogueid\r\n AND dm.state {$insql}) -\r\n (SELECT COUNT(1)\r\n FROM {dialogue_flags} df\r\n WHERE df.dialogueid = :undialogueid\r\n AND df.userid = :unuserid\r\n AND df.flag = :unflag) AS unread"; } // get user's total unread count for a dialogue $record = (array) $DB->get_record_sql($sql, $params); if (isset($record['unread']) and $record['unread'] > 0) { return (int) $record['unread']; } return 0; }