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
declare(strict_types=1);
18
 
19
namespace core_reportbuilder\reportbuilder\audience;
20
 
21
use context_system;
22
use core_reportbuilder\local\audiences\base;
23
use core_reportbuilder\local\helpers\database;
24
use core_user;
25
use MoodleQuickForm;
26
 
27
/**
28
 * The backend class for Manually added users audience type
29
 *
30
 * @package     core_reportbuilder
31
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class manual extends base {
35
 
36
    /**
37
     * Adds audience's elements to the given mform
38
     *
39
     * @param MoodleQuickForm $mform The form to add elements to
40
     */
41
    public function get_config_form(MoodleQuickForm $mform): void {
42
        // Users selector.
43
        $options = [
44
            'ajax' => 'core_user/form_user_selector',
45
            'multiple' => true,
46
            'valuehtmlcallback' => function($userid) {
47
                $user = core_user::get_user($userid);
48
                return fullname($user, has_capability('moodle/site:viewfullnames', context_system::instance()));
49
            }
50
        ];
51
 
52
        $mform->addElement('autocomplete', 'users', get_string('addusers', 'core_reportbuilder'), [], $options);
53
        $mform->addRule('users', null, 'required', null, 'client');
54
    }
55
 
56
    /**
57
     * Helps to build SQL to retrieve users that matches the current report audience
58
     *
59
     * @param string $usertablealias
60
     * @return array array of three elements [$join, $where, $params]
61
     */
62
    public function get_sql(string $usertablealias): array {
63
        global $DB;
64
 
65
        $users = $this->get_configdata()['users'];
66
        [$insql, $inparams] = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, database::generate_param_name('_'));
67
 
68
        return ['', "{$usertablealias}.id $insql", $inparams];
69
    }
70
 
71
    /**
72
     * Return user friendly name of this audience type
73
     *
74
     * @return string
75
     */
76
    public function get_name(): string {
77
        return get_string('manuallyaddedusers', 'core_reportbuilder');
78
    }
79
 
80
    /**
81
     * Return the description for the audience.
82
     *
83
     * @return string
84
     */
85
    public function get_description(): string {
86
        global $DB;
87
 
88
        $canviewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
89
 
90
        $userslist = [];
91
 
92
        $userids = $this->get_configdata()['users'];
93
        [$sort] = users_order_by_sql();
94
        $users = $DB->get_records_list('user', 'id', $userids, $sort);
95
        foreach ($users as $user) {
96
            $userslist[] = fullname($user, $canviewfullnames);
97
        }
98
 
99
        return $this->format_description_for_multiselect($userslist);
100
    }
101
 
102
    /**
103
     * If the current user is able to add this audience.
104
     *
105
     * @return bool
106
     */
107
    public function user_can_add(): bool {
108
        return has_capability('moodle/user:viewalldetails', context_system::instance());
109
    }
110
 
111
    /**
112
     * If the current user is able to edit this audience.
113
     *
114
     * @return bool
115
     */
116
    public function user_can_edit(): bool {
117
        return has_capability('moodle/user:viewalldetails', context_system::instance());
118
    }
119
}