Merge branch 'MDL-34707_22' of git://github.com/timhunt/moodle into MOODLE_22_STABLE
[moodle.git] / enrol / bulkchange_forms.php
blob65e6152b20455fbe543c8e1b0af7577333adb1b3
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This file contains form for bulk changing user enrolments.
21 * @package core
22 * @subpackage enrol
23 * @copyright 2011 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once("$CFG->libdir/formslib.php");
31 /**
32 * A base class that can be used to easily construct a form for use with bulk operations
34 * @copyright 2011 Sam Hemelryk
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 abstract class enrol_bulk_enrolment_change_form extends moodleform {
39 /**
40 * Defines the standard structure of the form
42 protected function definition() {
43 $form = $this->_form;
44 $users = $this->_customdata['users'];
46 $statusoptions = $this->get_status_options();
47 $form->addElement('html', $this->get_users_table($users, $statusoptions));
48 $form->addElement('select', 'status', get_string('alterstatus', 'enrol_manual'), $statusoptions, array('optional' => true));
49 $form->addElement('date_selector', 'timestart', get_string('altertimestart', 'enrol_manual'), array('optional' => true));
50 $form->addElement('date_selector', 'timeend', get_string('altertimeend', 'enrol_manual'), array('optional' => true));
52 $this->add_action_buttons();
55 /**
56 * Returns an array of status options
57 * @return array
59 protected function get_status_options() {
60 return array(-1 => get_string('nochange', 'enrol'),
61 ENROL_USER_ACTIVE => get_string('participationactive', 'enrol'),
62 ENROL_USER_SUSPENDED => get_string('participationsuspended', 'enrol'));
65 /**
66 * Generates an HTML table to display the users being affected by the bulk change.
68 * @param array $users
69 * @param array $statusoptions
70 * @return string
72 protected function get_users_table(array $users, array $statusoptions) {
73 $table = new html_table();
74 $table->head = array(
75 get_string('name'),
76 get_string('participationstatus', 'enrol'),
77 get_string('enroltimestart', 'enrol'),
78 get_string('enroltimeend', 'enrol'),
80 $table->data = array();
81 foreach ($users as $user) {
82 foreach ($user->enrolments as $enrolment) {
83 $input = html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'bulkuser[]', 'value' => $user->id));
84 $table->data[] = array(
85 fullname($user).$input,
86 $statusoptions[$enrolment->status],
87 (!empty($enrolment->timestart))?userdate($enrolment->timestart):'',
88 (!empty($enrolment->timeend))?userdate($enrolment->timeend):'',
92 return html_writer::table($table);
96 /**
97 * A convenience class to allow the quick generation of a confirmation form for a bulk operation.
98 * @copyright 2011 Sam Hemelryk
99 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
101 abstract class enrol_bulk_enrolment_confirm_form extends enrol_bulk_enrolment_change_form {
104 * Defines the standard structure of the form
106 protected function definition() {
107 $form = $this->_form;
108 $users = $this->_customdata['users'];
109 $title = $this->_customdata['title'];
110 $message = $this->_customdata['message'];
111 $button = $this->_customdata['button'];
113 $form->addElement('html', $this->get_users_table($users, $this->get_status_options()));
114 $form->addElement('header', 'ebecf_header', $title);
115 $form->addElement('html', html_writer::tag('p', $message));
116 $this->add_action_buttons(true, $button);