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
 * A bulk operation for the coursecompleted enrolment plugin to delete selected users enrolments.
19
 *
20
 * @package   enrol_coursecompleted
21
 * @copyright 2020 eWallah (www.eWallah.net)
22
 * @author    Renaat Debleu <info@eWallah.net>
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
// @codeCoverageIgnoreStart
29
require_once($CFG->dirroot . '/enrol/locallib.php');
30
// @codeCoverageIgnoreEnd
31
/**
32
 * A bulk operation for the coursecompleted enrolment plugin to delete selected users enrolments.
33
 *
34
 * @package   enrol_coursecompleted
35
 * @copyright 2020 eWallah (www.eWallah.net)
36
 * @author    Renaat Debleu <info@eWallah.net>
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class enrol_coursecompleted_bulkdelete extends enrol_bulk_enrolment_operation {
40
 
41
    /**
42
     * Returns the identifier for this bulk operation. This is the key used when the plugin
43
     * returns an array containing all of the bulk operations it supports.
44
     *
45
     * @return string
46
     */
47
    public function get_identifier() {
48
        return 'deleteselectedusers';
49
    }
50
 
51
    /**
52
     * Returns the title to display for this bulk operation.
53
     *
54
     * @return string
55
     */
56
    public function get_title() {
57
        return get_string('deleteselectedusers', 'enrol_coursecompleted');
58
    }
59
 
60
    /**
61
     * Returns a enrol_bulk_enrolment_operation extension form to be used
62
     * in collecting required information for this operation to be processed.
63
     *
64
     * @param string|moodle_url|null $defaultaction
65
     * @param mixed $defaultcustomdata
66
     * @return enrol_coursecompleted_deleteselectedusers_form
67
     */
68
    public function get_form($defaultaction = null, $defaultcustomdata = null) {
69
        $data = is_array($defaultcustomdata) ? $defaultcustomdata : [];
70
        $data['title'] = $this->get_title();
71
        $data['message'] = get_string('confirmbulkdeleteenrolment', 'enrol_coursecompleted');
72
        $data['button'] = get_string('unenrolusers', 'enrol_coursecompleted');
73
        return new \enrol_coursecompleted\form\bulkdelete($defaultaction, $data);
74
    }
75
 
76
    /**
77
     * Processes the bulk operation request for the given userids with the provided properties.
78
     *
79
     * @param course_enrolment_manager $manager
80
     * @param array $users
81
     * @param stdClass $properties The data returned by the form.
82
     * @return bool
83
     */
84
    public function process(course_enrolment_manager $manager, array $users, stdClass $properties) {
85
        if (!has_capability("enrol/coursecompleted:unenrol", $manager->get_context())) {
86
            return false;
87
        }
88
        foreach ($users as $user) {
89
            foreach ($user->enrolments as $enrolment) {
90
                $plugin = $enrolment->enrolmentplugin;
91
                $instance = $enrolment->enrolmentinstance;
92
                if ($plugin->allow_unenrol_user($instance, $enrolment)) {
93
                    $plugin->unenrol_user($instance, $user->id);
94
                }
95
            }
96
        }
97
        return true;
98
    }
99
}