Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
declare(strict_types=1);
18
 
19
namespace core_reportbuilder\customfield;
20
 
21
use core\context;
22
use core\context\system;
23
use core\exception\coding_exception;
24
use core\url;
25
use core_customfield\field_controller;
26
use core_reportbuilder\local\models\report;
27
use core_reportbuilder\manager;
28
use core_reportbuilder\permission;
29
 
30
/**
31
 * Report handler for custom fields
32
 *
33
 * @package    core_reportbuilder
34
 * @copyright  2025 David Carrillo <davidmc@moodle.com>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class report_handler extends \core_customfield\handler {
38
    /**
39
     * @var report_handler|null
40
     */
41
    protected static ?report_handler $singleton = null;
42
 
43
    /**
44
     * Returns a singleton
45
     *
46
     * @param int $itemid
47
     * @return self
48
     */
49
    public static function create(int $itemid = 0): self {
50
        if (static::$singleton === null) {
51
            self::$singleton = new static($itemid);
52
        }
53
        return self::$singleton;
54
    }
55
 
56
    /**
57
     * Run reset code after unit tests to reset the singleton usage.
58
     */
59
    public static function reset_caches(): void {
60
        if (!PHPUNIT_TEST) {
61
            throw new coding_exception('This feature is only intended for use in unit tests');
62
        }
63
 
64
        static::$singleton = null;
65
    }
66
 
67
    /**
68
     * The current user can configure custom fields on this component.
69
     *
70
     * @return bool true if the current can configure custom fields, false otherwise
71
     */
72
    public function can_configure(): bool {
73
        return has_capability('moodle/reportbuilder:configurecustomfields', $this->get_configuration_context());
74
    }
75
 
76
    /**
77
     * The current user can edit custom fields on the given report.
78
     *
79
     * @param field_controller $field
80
     * @param int $instanceid id of the report to test edit permission
81
     * @return bool true if the current can edit custom fields, false otherwise
82
     */
83
    public function can_edit(field_controller $field, int $instanceid = 0): bool {
84
        if ($instanceid > 0) {
85
            $report = report::get_record(['id' => $instanceid], MUST_EXIST);
86
            return permission::can_edit_report($report);
87
        } else {
88
            return permission::can_create_report();
89
        }
90
    }
91
 
92
    /**
93
     * The current user can view custom fields on the given report.
94
     *
95
     * @param field_controller $field
96
     * @param int $instanceid id of the report to test edit permission
97
     * @return bool true if the current can edit custom fields, false otherwise
98
     */
99
    public function can_view(field_controller $field, int $instanceid): bool {
100
        if ($instanceid > 0) {
101
            $report = report::get_record(['id' => $instanceid], MUST_EXIST);
102
            return permission::can_view_report($report);
103
        }
104
 
105
        return permission::can_view_reports_list();
106
    }
107
 
108
    /**
109
     * Context that should be used for new categories created by this handler
110
     *
111
     * @return context the context for configuration
112
     */
113
    public function get_configuration_context(): context {
114
        return system::instance();
115
    }
116
 
117
    /**
118
     * URL for configuration of the fields on this handler.
119
     *
120
     * @return url The URL to configure custom fields for this component
121
     */
122
    public function get_configuration_url(): url {
123
        return new url('/reportbuilder/customfield.php');
124
    }
125
 
126
    /**
127
     * Returns the context for the data associated with the given instanceid.
128
     *
129
     * @param int $instanceid id of the record to get the context for
130
     * @return context the context for the given record
131
     */
132
    public function get_instance_context(int $instanceid = 0): context {
133
        if ($instanceid > 0) {
134
            return (manager::get_report_from_id($instanceid))->get_context();
135
        } else {
136
            return system::instance();
137
        }
138
    }
139
}