Proyectos de Subversion Moodle

Rev

| 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
 * Provides {@link tool_policy\output\renderer} class.
19
 *
20
 * @package     tool_policy
21
 * @category    output
22
 * @copyright   2018 Sara Arjona <sara@moodle.com>
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace tool_policy\output;
27
 
28
use moodle_exception;
29
 
30
defined('MOODLE_INTERNAL') || die();
31
 
32
require_once("$CFG->libdir/filelib.php");
33
 
34
use context_system;
35
use moodle_url;
36
use renderable;
37
use renderer_base;
38
use single_button;
39
use templatable;
40
use tool_policy\api;
41
use tool_policy\policy_version;
42
 
43
/**
44
 * Represents a page for showing all the policy documents with a current version.
45
 *
46
 * @copyright 2018 Sara Arjona <sara@moodle.com>
47
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48
 */
49
class page_viewalldoc implements renderable, templatable {
50
 
51
    /** @var ?moodle_url Return url */
52
    private $returnurl = null;
53
 
54
    /** @var array List current (active) policy versions. */
55
    private array $policies = [];
56
 
57
    /**
58
     * Prepare the page for rendering.
59
     *
60
     */
61
    public function __construct($returnurl) {
62
        if (!empty($returnurl)) {
63
            $this->returnurl = new moodle_url($returnurl);
64
        }
65
 
66
        $this->prepare_global_page_access();
67
        $this->prepare_policies();
68
    }
69
 
70
    /**
71
     * Loads the policy versions to display on the page.
72
     *
73
     */
74
    protected function prepare_policies() {
75
        $this->policies = api::list_current_versions();
76
    }
77
 
78
    /**
79
     * Sets up the global $PAGE and performs the access checks.
80
     */
81
    protected function prepare_global_page_access() {
82
        global $PAGE, $SITE, $USER;
83
 
84
        $myurl = new moodle_url('/admin/tool/policy/viewall.php', []);
85
 
86
        // Disable notifications for new users, guests or users who haven't agreed to the policies.
87
        if (isguestuser() || empty($USER->id) || !$USER->policyagreed) {
88
            $PAGE->set_popup_notification_allowed(false);
89
        }
90
 
91
        $PAGE->set_context(context_system::instance());
92
        $PAGE->set_pagelayout('popup');
93
        $PAGE->set_url($myurl);
94
        $PAGE->set_heading($SITE->fullname);
95
        $PAGE->set_title(get_string('policiesagreements', 'tool_policy'));
96
    }
97
 
98
    /**
99
     * Export the page data for the mustache template.
100
     *
101
     * @param renderer_base $output renderer to be used to render the page elements.
102
     * @return stdClass
103
     */
104
    public function export_for_template(renderer_base $output) {
105
 
106
        $data = (object) [
107
            'pluginbaseurl' => (new moodle_url('/admin/tool/policy'))->out(false),
108
        ];
109
 
110
        $data->policies = array_values($this->policies);
111
        if (!empty($this->returnurl)) {
112
            $data->returnurl = $this->returnurl;
113
        }
114
 
115
        array_walk($data->policies, function($item, $key) {
116
            $item->policytypestr = get_string('policydoctype'.$item->type, 'tool_policy');
117
            $item->policyaudiencestr = get_string('policydocaudience'.$item->audience, 'tool_policy');
118
        });
119
 
120
        return $data;
121
    }
122
}