Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Contains notification_list_processor class for displaying on message preferences page.
19
 *
20
 * @package   core_message
21
 * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_message\output\preferences;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
require_once($CFG->dirroot . '/message/lib.php');
30
 
31
use renderable;
32
use templatable;
33
 
34
/**
35
 * Class to create context for a notification component on the message preferences page.
36
 *
37
 * @package   core_message
38
 * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class notification_list_processor implements templatable, renderable {
42
 
43
    /**
44
     * @var \stdClass A notification processor.
45
     */
46
    protected $processor;
47
 
48
    /**
49
     * @var \stdClass A notification provider.
50
     */
51
    protected $provider;
52
 
53
    /**
54
     * @var \stdClass A list of message preferences.
55
     */
56
    protected $preferences;
57
 
58
    /**
59
     * Constructor.
60
     *
61
     * @param \stdClass $processor
62
     * @param \stdClass $provider
63
     * @param \stdClass $preferences
64
     */
65
    public function __construct($processor, $provider, $preferences) {
66
        $this->processor = $processor;
67
        $this->provider = $provider;
68
        $this->preferences = $preferences;
69
    }
70
 
71
    /**
72
     * Get the base key prefix for the given provider.
73
     *
74
     * @return string
75
     */
76
    private function get_preference_base() {
77
        return $this->provider->component . '_' . $this->provider->name;
78
    }
79
 
80
    /**
81
     * Check if the given preference is enabled or not.
82
     *
83
     * @param string $name preference name
84
     * @param string $locked Wether the preference is locked by admin.
85
     * @return bool
86
     */
87
    private function is_preference_enabled($name, $locked) {
88
        $processor = $this->processor;
89
        $preferences = $this->preferences;
90
        $defaultpreferences = get_message_output_default_preferences();
91
 
92
        $checked = false;
93
        // See if user has touched this preference.
94
        if (!$locked && isset($preferences->{$name})) {
95
            // User has some preferences for this state in the database.
96
            $checked = isset($preferences->{$name}[$processor->name]);
97
        } else {
98
            // User has not set this preference yet, using site default preferences set by admin.
99
            $defaultpreference = 'message_provider_'.$name;
100
            if (isset($defaultpreferences->{$defaultpreference})) {
101
                $checked = (int)in_array($processor->name, explode(',', $defaultpreferences->{$defaultpreference}));
102
            }
103
        }
104
 
105
        return $checked;
106
    }
107
 
108
    /**
109
     * Export this data so it can be used as the context for a mustache template.
110
     *
111
     * @param renderer_base $output
112
     * @return stdClass
113
     */
114
    public function export_for_template(\renderer_base $output) {
115
        $processor = $this->processor;
116
        $preferencebase = $this->get_preference_base();
117
        $defaultpreferences = get_message_output_default_preferences();
118
        $defaultpreference = $processor->name.'_provider_'.$preferencebase.'_locked';
119
        $providername = get_string('messageprovider:'.$this->provider->name, $this->provider->component);
120
        $processorname = get_string('pluginname', 'message_'.$processor->name);
121
        $labelparams = [
122
            'provider'  => $providername,
123
            'processor' => $processorname,
124
        ];
125
 
126
        $context = [
127
            'displayname' => $processorname,
128
            'name' => $processor->name,
129
            'locked' => false,
130
            'userconfigured' => $processor->object->is_user_configured(),
131
            'enabled' => false,
132
            'enabledlabel' => get_string('sendingviaenabled', 'message', $labelparams),
133
        ];
134
 
135
        // Determine the default setting.
136
        if (isset($defaultpreferences->{$defaultpreference})) {
137
            $context['locked'] = $defaultpreferences->{$defaultpreference};
138
        }
139
 
140
        $context['enabled'] = $this->is_preference_enabled($preferencebase.'_enabled', $context['locked']);
141
 
142
        // If settings are disallowed or forced, just display the corresponding message, if not use user settings.
143
        if ($context['locked']) {
144
            if ($context['enabled']) {
145
                $context['lockedmessage'] = get_string('forcedmessage', 'message');
146
                $context['lockedlabel'] = get_string('providerprocesorislocked', 'message', $labelparams);
147
            } else {
148
                $context['lockedmessage'] = get_string('disallowed', 'message');
149
                $context['lockedlabel'] = get_string('providerprocesorisdisallowed', 'message', $labelparams);
150
            }
151
        }
152
 
1441 ariadna 153
        $supportsprocessor = true;
154
        if ($processor->name === 'sms') {
155
            $supportsprocessor = \core_message\helper::supports_sms_notifications($this->provider);
156
        }
157
        $context['supportsprocessor'] = $supportsprocessor;
158
 
1 efrain 159
        return $context;
160
    }
161
}