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