MDL-61864 tool_policy: user agreement reports
[moodle.git] / admin / tool / policy / classes / output / acceptances.php
blobace885d3cfe7922df4ffd7485806d543f6cf533c
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.
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/>.
17 /**
18 * Provides {@link tool_policy\output\acceptances} class.
20 * @package tool_policy
21 * @category output
22 * @copyright 2018 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace tool_policy\output;
28 use tool_policy\api;
30 defined('MOODLE_INTERNAL') || die();
32 use moodle_url;
33 use renderable;
34 use renderer_base;
35 use single_button;
36 use templatable;
37 use tool_policy\policy_version;
39 /**
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 {
47 /** @var id */
48 protected $userid;
50 /** @var moodle_url */
51 protected $returnurl;
53 /**
54 * Contructor.
56 * @param int $userid
58 public function __construct($userid, $returnurl = null) {
59 $this->userid = $userid;
60 $this->returnurl = $returnurl ? (new moodle_url($returnurl))->out(false) : null;
63 /**
64 * Export the page data for the mustache template.
66 * @param renderer_base $output renderer to be used to render the page elements.
67 * @return stdClass
69 public function export_for_template(renderer_base $output) {
70 $data = (object)[];
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),
93 ]))->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);
101 if ($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);
130 return $data;