MDL-61864 tool_policy: display policies to guests and users
[moodle.git] / admin / tool / policy / classes / output / page_viewalldoc.php
CommitLineData
6109f234
SA
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
26namespace tool_policy\output;
27
28use moodle_exception;
29
30defined('MOODLE_INTERNAL') || die();
31
32require_once("$CFG->libdir/filelib.php");
33
34use context_system;
35use moodle_url;
36use renderable;
37use renderer_base;
38use single_button;
39use templatable;
40use tool_policy\api;
41use 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 */
49class page_viewalldoc implements renderable, templatable {
50
51 /**
52 * Prepare the page for rendering.
53 *
54 */
55 public function __construct() {
56
57 $this->prepare_policies();
58 $this->prepare_global_page_access();
59 }
60
61 /**
62 * Loads the policy versions to display on the page.
63 *
64 */
65 protected function prepare_policies() {
66 global $USER;
67
68 if (isguestuser() || empty($USER->id)) {
69 $audience = policy_version::AUDIENCE_GUESTS;
70 } else {
71 $audience = policy_version::AUDIENCE_LOGGEDIN;
72 }
73 $this->policies = api::list_current_versions($audience);
74 }
75
76 /**
77 * Sets up the global $PAGE and performs the access checks.
78 */
79 protected function prepare_global_page_access() {
80 global $PAGE, $SITE, $USER;
81
82 $myurl = new moodle_url('/admin/tool/policy/viewall.php', []);
83
84 // Disable notifications for new users, guests or users who haven't agreed to the policies.
85 if (isguestuser() || empty($USER->id) || !$USER->policyagreed) {
86 $PAGE->set_popup_notification_allowed(false);
87 }
88
89 $PAGE->set_context(context_system::instance());
90 $PAGE->set_pagelayout('popup');
91 $PAGE->set_url($myurl);
92 $PAGE->set_heading($SITE->fullname);
93 $PAGE->set_title(get_string('policiesagreements', 'tool_policy'));
94 }
95
96 /**
97 * Export the page data for the mustache template.
98 *
99 * @param renderer_base $output renderer to be used to render the page elements.
100 * @return stdClass
101 */
102 public function export_for_template(renderer_base $output) {
103
104 $data = (object) [
105 'pluginbaseurl' => (new moodle_url('/admin/tool/policy'))->out(false),
106 ];
107
108 $data->policies = array_values($this->policies);
109
110 return $data;
111 }
112}