MDL-4908 Forum: per-forum digests
[moodle.git] / mod / forum / maildigest.php
CommitLineData
8e08c731
AN
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Set the mail digest option in a specific forum for a user.
20 *
21 * @copyright 2013 Andrew Nicols
22 * @package mod_forum
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require_once(dirname(dirname(__DIR__)) . '/config.php');
27require_once($CFG->dirroot.'/mod/forum/lib.php');
28
29$id = required_param('id', PARAM_INT);
30$maildigest = required_param('maildigest', PARAM_INT);
31$backtoindex = optional_param('backtoindex', 0, PARAM_INT);
32
33// We must have a valid session key.
34require_sesskey();
35
36$forum = $DB->get_record('forum', array('id' => $id));
37$course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
38$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
39$context = context_module::instance($cm->id);
40
41require_course_login($course);
42
43$url = new moodle_url('/mod/forum/maildigest.php', array(
44 'id' => $id,
45 'maildigest' => $maildigest,
46));
47$PAGE->set_url($url);
48$PAGE->set_context($context);
49
50$digestoptions = forum_get_user_digest_options();
51
52$info = new stdClass();
53$info->name = fullname($USER);
54$info->forum = format_string($forum->name);
55$info->maildigest = forum_set_user_maildigest($forum, $maildigest);
56
57if ($maildigest === -1) {
58 // Get the default maildigest options.
59 $info->maildigest = $USER->maildigest;
60 $info->maildigesttitle = $digestoptions[$info->maildigest];
61 $info->maildigestdescription = get_string('emaildigest_' . $info->maildigest,
62 'mod_forum', $info);
63 $updatemessage = get_string('emaildigestupdated_default', 'forum', $info);
64} else {
65 $info->maildigesttitle = $digestoptions[$info->maildigest];
66 $info->maildigestdescription = get_string('emaildigest_' . $info->maildigest,
67 'mod_forum', $info);
68 $updatemessage = get_string('emaildigestupdated', 'forum', $info);
69}
70
71if ($backtoindex) {
72 $returnto = "index.php?id={$course->id}";
73} else {
74 $returnto = "view.php?f={$id}";
75}
76
77redirect($returnto, $updatemessage, 1);