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
 * this file contains the user selection form to exclude users
19
 *
20
 * File         user.php
21
 * Encoding     UTF-8
22
 * @copyright   Sebsoft.nl
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace tool_usersuspension\forms\exclude;
27
 
28
defined('MOODLE_INTERNAL') || die;
29
 
30
require_once($CFG->libdir . '/formslib.php');
31
 
32
/**
33
 * tool_usersuspension\forms\user
34
 *
35
 * @package     tool_usersuspension
36
 *
37
 * @copyright   Sebsoft.nl
38
 * @author      R.J. van Dongen <rogier@sebsoft.nl>
39
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class user extends \moodleform {
42
 
43
    /**
44
     * User Selector for currently known users
45
     * @var \tool_usersuspension\exclude\user\selector\current
46
     */
47
    protected $currentuserselector;
48
    /**
49
     * User Selector for currently known users
50
     * @var \tool_usersuspension\exclude\user\selector\potential
51
     */
52
    protected $potentialuserselector;
53
 
54
    /**
55
     * form definition
56
     */
57
    public function definition() {
58
        global $OUTPUT;
59
        // Create the user selector objects.
60
        $options = array('accesscontext' => \context_system::instance());
61
        $this->currentuserselector = new \tool_usersuspension\exclude\user\selector\current('removeselect', $options);
62
        $this->potentialuserselector = new \tool_usersuspension\exclude\user\selector\potential('addselect', $options);
63
        $mform = $this->_form;
64
        // This element is only here so the form will actually get submitted.
65
        $mform->addElement('hidden', 'processor', 1);
66
        $mform->setType('processor', PARAM_INT);
67
 
68
        // Add user selection lists and submit controls.
69
        $html = '
70
          <table summary="" class="suspendexcludetable generaltable generalbox boxaligncenter" cellspacing="0">
71
            <tr><td id="existingcell"><p><label for="removeselect">' .
72
                get_string('label:users:excluded', 'tool_usersuspension') . '</label></p>';
73
        $html .= $this->currentuserselector->display(true);
74
        $html .= '</td><td id="buttonscell"><div id="controls">';
75
        $html .= '<input name="add" id="add" type="submit" value="' . $OUTPUT->larrow() .
76
                '&nbsp;' . get_string('add') . '" title="' . get_string('add') . '" /><br />
77
                <input name="remove" id="remove" type="submit" value="' . get_string('remove') .
78
                '&nbsp;' . $OUTPUT->rarrow() . '" title="' . get_string('remove') . '" />';
79
        $html .= '</div></td><td id="potentialcell"><p><label for="addselect">' .
80
                get_string('label:users:potential', 'tool_usersuspension') . '</label></p>';
81
        $html .= $this->potentialuserselector->display(true);
82
        $html .= '</td></tr></table>';
83
        $mform->addElement('html', $html);
84
    }
85
 
86
    /**
87
     * Process the posted form
88
     *
89
     * @throws \moodle_exception
90
     */
91
    public function process() {
92
        global $DB;
93
        $data = $this->get_data();
94
        if ($data === null) {
95
            return false;
96
        }
97
 
98
        $add = (bool)optional_param('add', false, PARAM_BOOL);
99
        $remove = (bool)optional_param('remove', false, PARAM_BOOL);
100
 
101
        if ($remove) {
102
            // Remove user(s).
103
            $userstoremove = $this->currentuserselector->get_selected_users();
104
            if (!empty($userstoremove)) {
105
                foreach ($userstoremove as $removeuser) {
106
                    $obj = array('type' => 'user', 'refid' => $removeuser->id);
107
                    if ($DB->record_exists('tool_usersuspension_excl', $obj)) {
108
                        $DB->delete_records('tool_usersuspension_excl', $obj);
109
                        $removeuser->fullname = fullname($removeuser);
110
                        \tool_usersuspension\util::print_notification(
111
                            get_string('msg:exclusion:record:user:deleted', 'tool_usersuspension', $removeuser), 'success');
112
                    }
113
                }
114
            }
115
        } else if ($add) {
116
            // Add user(s).
117
            $userstoexclude = $this->potentialuserselector->get_selected_users();
118
            if (!empty($userstoexclude)) {
119
                foreach ($userstoexclude as $adduser) {
120
                    $obj = array('type' => 'user', 'refid' => $adduser->id);
121
                    if (!$DB->record_exists('tool_usersuspension_excl', $obj)) {
122
                        $obj['timecreated'] = time();
123
                        $DB->insert_record('tool_usersuspension_excl', (object)$obj);
124
                        $adduser->fullname = fullname($adduser);
125
                        \tool_usersuspension\util::print_notification(get_string('msg:exclusion:record:user:inserted',
126
                            'tool_usersuspension', $adduser), 'success');
127
                    }
128
                }
129
            }
130
        }
131
        $this->potentialuserselector->invalidate_selected_users();
132
        $this->currentuserselector->invalidate_selected_users();
133
    }
134
 
135
}