-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.php
65 lines (51 loc) · 3.08 KB
/
form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
require_once("{$CFG->libdir}/formslib.php");
class migration_form extends moodleform {
//Regular Expression for old course URL
private $regexurl = '/^(http|https):\/\/moodle.uni-potsdam.de\/course\/view.php\?id=[0-9]+/';
function definition() {
global $OUTPUT;
$mform =& $this->_form;
$mform->addElement('header','displayinfo', get_string('form_caption', 'block_migrationup'));
$paragraph = '<p>'.get_string('form_request1', 'block_migrationup'). ' <a href="https://moodle.uni-potsdam.de" target="_blank">';
$paragraph .= get_string('oldmoodle', 'block_migrationup').'</a> '.get_string('form_request2', 'block_migrationup').'<br />';
$paragraph .= get_string('form_request_urlhelp1', 'block_migrationup').' <a href="https://moodle.uni-potsdam.de" target="_blank">';
$paragraph .= get_string('oldmoodle', 'block_migrationup').'</a> '.get_string('form_request_urlhelp2', 'block_migrationup').'</p>';
$urlhelppic = new moodle_url('/blocks/migrationup/pix/urlhelp.png');
$urlhelp = html_writer::tag('img', '', array('src' => $urlhelppic, 'alt' => get_string('url_help_pic','block_migrationup'), 'class' => 'url'));
$paragraph .= '<p>'.$urlhelp.'</p>';
$listitems = array(get_string('form_request_listitem1', 'block_migrationup'), get_string('form_request_listitem2', 'block_migrationup'), get_string('form_request_listitem3', 'block_migrationup'));
$paragraph .=html_writer::alist($listitems, null, 'ol');
$mform->addElement('html',$paragraph);
// Input for URL
$mform->addElement('text', 'oldcourseurl', get_string('url', 'block_migrationup'), array('size' => '50'));
$mform->addHelpButton('oldcourseurl', 'url', 'block_migrationup');
$mform->setDefault('url', 'https://moodle.uni-potsdam.de/course/view.php?id=6527');
$mform->setType('config_text', PARAM_MULTILANG);
$mform->addRule('oldcourseurl', get_string('form_required', 'block_migrationup'), 'required', null, 'client');
$mform->addRule('oldcourseurl', get_string('form_validurl', 'block_migrationup'), 'regex', $this->regexurl, 'client');
// Checkbox for Userdata
$mform->addElement('checkbox','userdata', get_string('userdata', 'block_migrationup'));
$mform->addHelpButton('userdata', 'userdata', 'block_migrationup');
$mform->addElement('hidden', 'blockid');
$mform->addElement('hidden', 'courseid');
// Send Buttons
$this->add_action_buttons(true, get_string('submit', 'block_migrationup'));
}
function validation($data, $files) {
global $DB, $USER;
$errors = array();
// check if valid url
if(preg_match($this->regexurl, $data['oldcourseurl'])) {
preg_match('/(?<=id=)(.(?!&))*./', $data['oldcourseurl'], $match);
$oldcourseid = $match[0];
//check if teacher in old course
if(!$DB->get_records('block_migrationup_courses', array('courseid' => $oldcourseid, 'username' => $USER->username))) {
$errors['oldcourseurl'] = get_string('notteacher', 'block_migrationup');
}
} else {
$errors['oldcourseurl'] = get_string('invalidurl', 'block_migrationup');
}
return $errors;
}
}