public function test_get_user_digest_options_sorting() { global $USER, $DB; $this->resetAfterTest(true); // Set up a basic user enrolled in a course. $helper = $this->helper_setup_user_in_course(); $user = $helper->user; $course1 = $helper->courses->course1; $twf1 = $helper->twfs->twf1; // Set to the user. self::setUser($helper->user); // Retrieve the list of applicable options. $options = twf_get_user_digest_options(); // The default option must always be at the top of the list. $lastoption = -2; foreach ($options as $value => $description) { $this->assertGreaterThan($lastoption, $value); $lastoption = $value; } }
require_once dirname(dirname(__DIR__)) . '/config.php'; require_once $CFG->dirroot . '/mod/twf/lib.php'; $id = required_param('id', PARAM_INT); $maildigest = required_param('maildigest', PARAM_INT); $backtoindex = optional_param('backtoindex', 0, PARAM_INT); // We must have a valid session key. require_sesskey(); $twf = $DB->get_record('twf', array('id' => $id)); $course = $DB->get_record('course', array('id' => $twf->course), '*', MUST_EXIST); $cm = get_coursemodule_from_instance('twf', $twf->id, $course->id, false, MUST_EXIST); $context = context_module::instance($cm->id); require_login($course, false, $cm); $url = new moodle_url('/mod/twf/maildigest.php', array('id' => $id, 'maildigest' => $maildigest)); $PAGE->set_url($url); $PAGE->set_context($context); $digestoptions = twf_get_user_digest_options(); $info = new stdClass(); $info->name = fullname($USER); $info->twf = format_string($twf->name); twf_set_user_maildigest($twf, $maildigest); $info->maildigest = $maildigest; if ($maildigest === -1) { // Get the default maildigest options. $info->maildigest = $USER->maildigest; $info->maildigesttitle = $digestoptions[$info->maildigest]; $info->maildigestdescription = get_string('emaildigest_' . $info->maildigest, 'mod_twf', $info); $updatemessage = get_string('emaildigestupdated_default', 'twf', $info); } else { $info->maildigesttitle = $digestoptions[$info->maildigest]; $info->maildigestdescription = get_string('emaildigest_' . $info->maildigest, 'mod_twf', $info); $updatemessage = get_string('emaildigestupdated', 'twf', $info);
/** * Set the per-twf maildigest option for the specified user. * * @param stdClass $twf The twf to set the option for. * @param int $maildigest The maildigest option. * @param stdClass $user The user object. This defaults to the global $USER object. * @throws invalid_digest_setting thrown if an invalid maildigest option is provided. */ function twf_set_user_maildigest($twf, $maildigest, $user = null) { global $DB, $USER; if (is_number($twf)) { $twf = $DB->get_record('twf', array('id' => $twf)); } if ($user === null) { $user = $USER; } $course = $DB->get_record('course', array('id' => $twf->course), '*', MUST_EXIST); $cm = get_coursemodule_from_instance('twf', $twf->id, $course->id, false, MUST_EXIST); $context = context_module::instance($cm->id); // User must be allowed to see this twf. require_capability('mod/twf:viewdiscussion', $context, $user->id); // Validate the maildigest setting. $digestoptions = twf_get_user_digest_options($user); if (!isset($digestoptions[$maildigest])) { throw new moodle_exception('invaliddigestsetting', 'mod_twf'); } // Attempt to retrieve any existing twf digest record. $subscription = $DB->get_record('twf_digests', array('userid' => $user->id, 'twf' => $twf->id)); // Create or Update the existing maildigest setting. if ($subscription) { if ($maildigest == -1) { $DB->delete_records('twf_digests', array('twf' => $twf->id, 'userid' => $user->id)); } else { if ($maildigest !== $subscription->maildigest) { // Only update the maildigest setting if it's changed. $subscription->maildigest = $maildigest; $DB->update_record('twf_digests', $subscription); } } } else { if ($maildigest != -1) { // Only insert the maildigest setting if it's non-default. $subscription = new stdClass(); $subscription->twf = $twf->id; $subscription->userid = $user->id; $subscription->maildigest = $maildigest; $subscription->id = $DB->insert_record('twf_digests', $subscription); } } }