2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Provides {@link tool_policy\output\acceptances} class.
20 * @package tool_policy
22 * @copyright 2018 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace tool_policy\output
;
30 defined('MOODLE_INTERNAL') ||
die();
37 use tool_policy\policy_version
;
40 * List of users and their acceptances
42 * @copyright 2018 Marina Glancy
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class acceptances
implements renderable
, templatable
{
50 /** @var moodle_url */
58 public function __construct($userid, $returnurl = null) {
59 $this->userid
= $userid;
60 $this->returnurl
= $returnurl ?
(new moodle_url($returnurl))->out(false) : null;
64 * Export the page data for the mustache template.
66 * @param renderer_base $output renderer to be used to render the page elements.
69 public function export_for_template(renderer_base
$output) {
71 $data->hasonbehalfagreements
= false;
72 $data->pluginbaseurl
= (new moodle_url('/admin/tool/policy'))->out(false);
73 $data->returnurl
= $this->returnurl
;
75 // Get the list of policies and versions that current user is able to see
76 // and the respective acceptance records for the selected user.
77 $policies = api
::get_policies_with_acceptances($this->userid
);
79 $canviewfullnames = has_capability('moodle/site:viewfullnames', \context_system
::instance());
80 foreach ($policies as $policy) {
82 foreach ($policy->versions
as $version) {
83 unset($version->summary
);
84 unset($version->content
);
85 $version->iscurrent
= ($version->status
== policy_version
::STATUS_ACTIVE
);
86 $version->name
= $version->name
;
87 $version->revision
= $version->revision
;
88 $returnurl = new moodle_url('/admin/tool/policy/user.php', ['userid' => $this->userid
]);
89 $version->viewurl
= (new moodle_url('/admin/tool/policy/view.php', [
90 'policyid' => $policy->id
,
91 'versionid' => $version->id
,
92 'returnurl' => $returnurl->out(false),
95 if (!empty($version->acceptance
->status
)) {
96 $acceptance = $version->acceptance
;
97 $version->timeaccepted
= userdate($acceptance->timemodified
, get_string('strftimedatetime'));
98 $onbehalf = $acceptance->usermodified
&& $acceptance->usermodified
!= $this->userid
;
99 $version->agreement
= new user_agreement($this->userid
, [$version->id
], $returnurl,
100 [$version->id
=> $version->name
], $onbehalf);
102 $usermodified = (object)['id' => $acceptance->usermodified
];
103 username_load_fields_from_object($usermodified, $acceptance, 'mod');
104 $profileurl = new \
moodle_url('/user/profile.php', array('id' => $usermodified->id
));
105 $version->acceptedby
= \html_writer
::link($profileurl, fullname($usermodified, $canviewfullnames ||
106 has_capability('moodle/site:viewfullnames', \context_user
::instance($acceptance->usermodified
))));
107 $data->hasonbehalfagreements
= true;
109 $version->note
= format_text($acceptance->note
);
110 } else if ($version->iscurrent
) {
111 $version->agreement
= new user_agreement($this->userid
, [], $returnurl, [$version->id
=> $version->name
]);
113 if (isset($version->agreement
)) {
114 $version->agreement
= $version->agreement
->export_for_template($output);
118 if ($policy->versions
[0]->status
!= policy_version
::STATUS_ACTIVE
) {
119 // Add an empty "currentversion" on top.
120 $policy->versions
= [0 => (object)[]] +
$policy->versions
;
123 $policy->versioncount
= count($policy->versions
);
124 $policy->versions
= array_values($policy->versions
);
125 $policy->versions
[0]->isfirst
= 1;
126 $policy->versions
[0]->hasarchived
= (count($policy->versions
) > 1);
129 $data->policies
= array_values($policies);