Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / enrol / mnet / addinstance_form.php
blobe6d13d12a82e221e3e11e4f551fba5cb90ff1faf
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 * Form to add an instance of enrol_mnet plugin
21 * @package enrol
22 * @subpackage mnet
23 * @copyright 2010 David Mudrak <david@moodle.com>
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 class enrol_mnet_addinstance_form extends moodleform {
32 function definition() {
33 global $CFG, $DB;
35 $mform = $this->_form;
36 $course = $this->_customdata['course'];
37 $enrol = $this->_customdata['enrol'];
38 $service = $this->_customdata['service'];
39 $coursecontext = context_course::instance($course->id);
41 $subscribers = $service->get_remote_subscribers();
42 $hosts = array(0 => get_string('remotesubscribersall', 'enrol_mnet'));
43 foreach ($subscribers as $hostid => $subscriber) {
44 $hosts[$hostid] = $subscriber->appname.': '.$subscriber->hostname.' ('.$subscriber->hosturl.')';
46 $roles = get_assignable_roles($coursecontext);
48 $mform->addElement('header','general', get_string('pluginname', 'enrol_mnet'));
50 $mform->addElement('select', 'hostid', get_string('remotesubscriber', 'enrol_mnet'), $hosts);
51 $mform->addHelpButton('hostid', 'remotesubscriber', 'enrol_mnet');
52 $mform->addRule('hostid', get_string('required'), 'required', null, 'client');
54 $mform->addElement('select', 'roleid', get_string('roleforremoteusers', 'enrol_mnet'), $roles);
55 $mform->addHelpButton('roleid', 'roleforremoteusers', 'enrol_mnet');
56 $mform->addRule('roleid', get_string('required'), 'required', null, 'client');
57 $mform->setDefault('roleid', $enrol->get_config('roleid'));
59 $mform->addElement('text', 'name', get_string('instancename', 'enrol_mnet'));
60 $mform->addHelpButton('name', 'instancename', 'enrol_mnet');
61 $mform->setType('name', PARAM_TEXT);
63 $mform->addElement('hidden', 'id', null);
64 $mform->setType('id', PARAM_INT);
66 $this->add_action_buttons();
68 $this->set_data(array('id'=>$course->id));
71 /**
72 * Do not allow multiple instances for single remote host
74 * @param array $data raw form data
75 * @param array $files
76 * @return array of errors
78 function validation($data, $files) {
79 global $DB;
81 $errors = array();
83 if ($DB->record_exists('enrol', array('enrol' => 'mnet', 'courseid' => $data['id'], 'customint1' => $data['hostid']))) {
84 $errors['hostid'] = get_string('error_multiplehost', 'enrol_mnet');
87 return $errors;