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\page_managedocs_list} class.
19
 *
20
 * @package     tool_policy
21
 * @category    output
22
 * @copyright   2018 David Mudrák <david@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 html_writer;
29
use tool_policy\api;
30
 
31
defined('MOODLE_INTERNAL') || die();
32
 
33
use action_menu;
34
use action_menu_link;
35
use moodle_url;
36
use pix_icon;
37
use renderable;
38
use renderer_base;
39
use single_button;
40
use templatable;
41
use tool_policy\policy_version;
42
 
43
/**
44
 * Represents a management page with the list of policy documents.
45
 *
46
 * The page displays all policy documents in their sort order, together with draft future versions.
47
 *
48
 * @copyright 2018 David Mudrak <david@moodle.com>
49
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
50
 */
51
class page_managedocs_list implements renderable, templatable {
52
 
53
    /** @var int  */
54
    protected $policyid = null;
55
    /** @var moodle_url */
56
    protected $returnurl = null;
57
 
58
    /**
59
     * page_managedocs_list constructor.
60
     * @param int $policyid when specified only archived versions of this policy will be displayed.
61
     */
62
    public function __construct($policyid = null) {
63
        $this->policyid = $policyid;
64
        $this->returnurl = new moodle_url('/admin/tool/policy/managedocs.php');
65
        if ($this->policyid) {
66
            $this->returnurl->param('archived', $this->policyid);
67
        }
68
    }
69
 
70
    /**
71
     * Export the page data for the mustache template.
72
     *
73
     * @param renderer_base $output renderer to be used to render the page elements.
74
     * @return stdClass
75
     */
76
    public function export_for_template(renderer_base $output) {
77
 
78
        $data = (object) [];
79
        $data->pluginbaseurl = (new moodle_url('/admin/tool/policy'))->out(false);
80
        $data->canmanage = has_capability('tool/policy:managedocs', \context_system::instance());
81
        $data->canaddnew = $data->canmanage && !$this->policyid;
82
        $data->canviewacceptances = has_capability('tool/policy:viewacceptances', \context_system::instance());
83
        $data->title = get_string('policiesagreements', 'tool_policy');
84
        $data->policies = [];
85
 
86
        if ($this->policyid) {
87
            // We are only interested in the archived versions of the given policy.
88
            $data->backurl = (new moodle_url('/admin/tool/policy/managedocs.php'))->out(false);
89
            $policy = api::list_policies([$this->policyid], true)[0];
90
            if ($firstversion = $policy->currentversion ?: (reset($policy->draftversions) ?: reset($policy->archivedversions))) {
91
                $data->title = get_string('previousversions', 'tool_policy', format_string($firstversion->name));
92
            }
93
 
94
            foreach ($policy->archivedversions as $i => $version) {
95
                $data->versions[] = $this->export_version_for_template($output, $policy, $version,
96
                    false, false, false);
97
            }
98
            return $data;
99
        }
100
 
101
        // List all policies. Display current and all draft versions of each policy in this list.
102
        // If none found, then show only one archived version.
103
        $policies = api::list_policies(null, true);
104
        foreach ($policies as $i => $policy) {
105
 
106
            if (empty($policy->currentversion) && empty($policy->draftversions)) {
107
                // There is no current and no draft versions, display the first archived version.
108
                $firstpolicy = array_shift($policy->archivedversions);
109
                $data->versions[] = $this->export_version_for_template($output, $policy, $firstpolicy,
110
                    false, $i > 0, $i < count($policies) - 1);
111
            }
112
 
113
            if (!empty($policy->currentversion)) {
114
 
115
                // Current version of the policy.
116
                $data->versions[] = $this->export_version_for_template($output, $policy, $policy->currentversion,
117
                    false, $i > 0, $i < count($policies) - 1);
118
 
119
            } else if ($policy->draftversions) {
120
 
121
                // There is no current version, display the first draft version as the current.
122
                $firstpolicy = array_shift($policy->draftversions);
123
                $data->versions[] = $this->export_version_for_template($output, $policy, $firstpolicy,
124
                    false, $i > 0, $i < count($policies) - 1);
125
            }
126
 
127
            foreach ($policy->draftversions as $draft) {
128
                // Show all [other] draft policies indented.
129
                $data->versions[] = $this->export_version_for_template($output, $policy, $draft,
130
                    true, false, false);
131
            }
132
 
133
        }
134
 
135
        return $data;
136
    }
137
 
138
    /**
139
     * Exports one version for the list of policies
140
     *
141
     * @param \renderer_base $output
142
     * @param \stdClass $policy
143
     * @param \stdClass $version
144
     * @param bool $isindented display indented (normally drafts of the current version)
145
     * @param bool $moveup can move up
146
     * @param bool $movedown can move down
147
     * @return \stdClass
148
     */
149
    protected function export_version_for_template($output, $policy, $version, $isindented, $moveup, $movedown) {
150
 
151
        $status = $version->status;
152
        $version->statustext = get_string('status' . $status, 'tool_policy');
153
 
154
        if ($status == policy_version::STATUS_ACTIVE) {
155
            $version->statustext = html_writer::span($version->statustext, 'badge bg-success text-white');
156
        } else if ($status == policy_version::STATUS_DRAFT) {
157
            $version->statustext = html_writer::span($version->statustext, 'badge bg-warning text-dark');
158
        } else {
159
            $version->statustext = html_writer::span($version->statustext, 'label');
160
        }
161
 
162
        if ($version->optional == policy_version::AGREEMENT_OPTIONAL) {
163
            $version->optionaltext = get_string('policydocoptionalyes', 'tool_policy');
164
        } else {
165
            $version->optionaltext = get_string('policydocoptionalno', 'tool_policy');
166
        }
167
 
168
        $version->indented = $isindented;
169
 
170
        $editbaseurl = new moodle_url('/admin/tool/policy/editpolicydoc.php', [
171
            'sesskey' => sesskey(),
172
            'policyid' => $policy->id,
173
            'returnurl' => $this->returnurl->out_as_local_url(false),
174
        ]);
175
 
176
        $viewurl = new moodle_url('/admin/tool/policy/view.php', [
177
            'policyid' => $policy->id,
178
            'versionid' => $version->id,
179
            'manage' => 1,
180
            'returnurl' => $this->returnurl->out_as_local_url(false),
181
        ]);
182
 
183
        $actionmenu = new action_menu();
184
        $actionmenu->set_menu_trigger(get_string('actions', 'tool_policy'));
185
        $actionmenu->prioritise = true;
186
        if ($moveup) {
187
            $actionmenu->add(new action_menu_link(
188
                new moodle_url($editbaseurl, ['moveup' => $policy->id]),
189
                new pix_icon('t/up', get_string('moveup', 'tool_policy')),
190
                get_string('moveup', 'tool_policy'),
191
                true
192
            ));
193
        }
194
        if ($movedown) {
195
            $actionmenu->add(new action_menu_link(
196
                new moodle_url($editbaseurl, ['movedown' => $policy->id]),
197
                new pix_icon('t/down', get_string('movedown', 'tool_policy')),
198
                get_string('movedown', 'tool_policy'),
199
                true
200
            ));
201
        }
202
        $actionmenu->add(new action_menu_link(
203
            $viewurl,
204
            null,
205
            get_string('view'),
206
            false
207
        ));
208
        if ($status != policy_version::STATUS_ARCHIVED) {
209
            $actionmenu->add(new action_menu_link(
210
                new moodle_url($editbaseurl, ['versionid' => $version->id]),
211
                null,
212
                get_string('edit'),
213
                false
214
            ));
215
        }
216
        if ($status == policy_version::STATUS_ACTIVE) {
217
            $actionmenu->add(new action_menu_link(
218
                new moodle_url($editbaseurl, ['inactivate' => $policy->id]),
219
                null,
220
                get_string('inactivate', 'tool_policy'),
221
                false,
222
                ['data-action' => 'inactivate']
223
            ));
224
        }
225
        if ($status == policy_version::STATUS_DRAFT) {
226
            $actionmenu->add(new action_menu_link(
227
                new moodle_url($editbaseurl, ['makecurrent' => $version->id]),
228
                null,
229
                get_string('activate', 'tool_policy'),
230
                false,
231
                ['data-action' => 'makecurrent']
232
            ));
233
        }
234
        if (api::can_delete_version($version)) {
235
            $actionmenu->add(new action_menu_link(
236
                new moodle_url($editbaseurl, ['delete' => $version->id]),
237
                null,
238
                get_string('delete'),
239
                false,
240
                ['data-action' => 'delete']
241
            ));
242
        }
243
        if ($status == policy_version::STATUS_ARCHIVED) {
244
            $actionmenu->add(new action_menu_link(
245
                new moodle_url($editbaseurl, ['versionid' => $version->id]),
246
                null,
247
                get_string('settodraft', 'tool_policy'),
248
                false
249
            ));
250
        }
251
        if (!$this->policyid && !$isindented && $policy->archivedversions &&
252
                ($status != policy_version::STATUS_ARCHIVED || count($policy->archivedversions) > 1)) {
253
            $actionmenu->add(new action_menu_link(
254
                new moodle_url('/admin/tool/policy/managedocs.php', ['archived' => $policy->id]),
255
                null,
256
                get_string('viewarchived', 'tool_policy'),
257
                false
258
            ));
259
        }
260
 
261
        $version->actionmenu = $actionmenu->export_for_template($output);
262
        return $version;
263
    }
264
}