<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains notification_list_processor class for displaying on message preferences page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\preferences;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/message/lib.php');
use renderable;
use templatable;
/**
* Class to create context for a notification component on the message preferences page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class notification_list_processor implements templatable, renderable {
/**
* @var \stdClass A notification processor.
*/
protected $processor;
/**
* @var \stdClass A notification provider.
*/
protected $provider;
/**
* @var \stdClass A list of message preferences.
*/
protected $preferences;
/**
* Constructor.
*
* @param \stdClass $processor
* @param \stdClass $provider
* @param \stdClass $preferences
*/
public function __construct($processor, $provider, $preferences) {
$this->processor = $processor;
$this->provider = $provider;
$this->preferences = $preferences;
}
/**
* Get the base key prefix for the given provider.
*
* @return string
*/
private function get_preference_base() {
return $this->provider->component . '_' . $this->provider->name;
}
/**
* Check if the given preference is enabled or not.
*
* @param string $name preference name
> * @param string $locked Wether the preference is locked by admin.
* @return bool
*/
< private function is_preference_enabled($name) {
> private function is_preference_enabled($name, $locked) {
$processor = $this->processor;
$preferences = $this->preferences;
$defaultpreferences = get_message_output_default_preferences();
$checked = false;
// See if user has touched this preference.
< if (isset($preferences->{$name})) {
> if (!$locked && isset($preferences->{$name})) {
// User has some preferences for this state in the database.
$checked = isset($preferences->{$name}[$processor->name]);
} else {
// User has not set this preference yet, using site default preferences set by admin.
$defaultpreference = 'message_provider_'.$name;
if (isset($defaultpreferences->{$defaultpreference})) {
$checked = (int)in_array($processor->name, explode(',', $defaultpreferences->{$defaultpreference}));
}
}
return $checked;
}
> /**
public function export_for_template(\renderer_base $output) {
> * Export this data so it can be used as the context for a mustache template.
$processor = $this->processor;
> * @todo Remove loggedin and loggedoff from context on MDL-73284.
$preferencebase = $this->get_preference_base();
> *
$permitted = MESSAGE_DEFAULT_PERMITTED;
> * @param renderer_base $output
$defaultpreferences = get_message_output_default_preferences();
> * @return stdClass
$defaultpreference = $processor->name.'_provider_'.$preferencebase.'_permitted';
> */
< $permitted = MESSAGE_DEFAULT_PERMITTED;
< $defaultpreference = $processor->name.'_provider_'.$preferencebase.'_permitted';
> $defaultpreference = $processor->name.'_provider_'.$preferencebase.'_locked';
> $providername = get_string('messageprovider:'.$this->provider->name, $this->provider->component);
> $processorname = get_string('pluginname', 'message_'.$processor->name);
> $labelparams = [
> 'provider' => $providername,
> 'processor' => $processorname,
> ];
>
< 'displayname' => get_string('pluginname', 'message_'.$processor->name),
> 'displayname' => $processorname,
'locked' => false,
'userconfigured' => $processor->object->is_user_configured(),
> // Backward compatibility, deprecated attribute.
'loggedin' => [
'name' => 'loggedin',
< 'displayname' => get_string('loggedindescription', 'message'),
< 'checked' => $this->is_preference_enabled($preferencebase.'_loggedin'),
> 'displayname' => 'loggedin',
> 'checked' => false,
],
> // Backward compatibility, deprecated attribute.
'loggedoff' => [
'name' => 'loggedoff',
< 'displayname' => get_string('loggedoffdescription', 'message'),
< 'checked' => $this->is_preference_enabled($preferencebase.'_loggedoff'),
> 'displayname' => 'loggedoff',
> 'checked' => false,
],
> 'enabled' => false,
];
> 'enabledlabel' => get_string('sendingviaenabled', 'message', $labelparams),
// Determine the default setting.
if (isset($defaultpreferences->{$defaultpreference})) {
< $permitted = $defaultpreferences->{$defaultpreference};
> $context['locked'] = $defaultpreferences->{$defaultpreference};
}
>
// If settings are disallowed or forced, just display the corresponding message, if not use user settings.
> $context['enabled'] = $this->is_preference_enabled($preferencebase.'_enabled', $context['locked']);
if ($permitted == 'disallowed') {
> $context['loggedoff']['checked'] = $context['enabled']; // Backward compatibility, deprecated attribute.
$context['locked'] = true;
> $context['loggedin']['checked'] = $context['enabled']; // Backward compatibility, deprecated attribute.
$context['lockedmessage'] = get_string('disallowed', 'message');
>
< if ($permitted == 'disallowed') {
< $context['locked'] = true;
> if ($context['locked']) {
> if ($context['enabled']) {
> $context['lockedmessage'] = get_string('forcedmessage', 'message');
> $context['lockedlabel'] = get_string('providerprocesorislocked', 'message', $labelparams);
> } else {
< } else if ($permitted == 'forced') {
< $context['locked'] = true;
< $context['lockedmessage'] = get_string('forced', 'message');
> $context['lockedlabel'] = get_string('providerprocesorisdisallowed', 'message', $labelparams);
> }
return $context;
}
}