mirrored from git://git.moodle.org/moodle.git
/
grade_import_form.php
98 lines (78 loc) · 4.41 KB
/
grade_import_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once $CFG->libdir.'/formslib.php';
class grade_import_form extends moodleform {
function definition () {
global $COURSE, $USER, $CFG, $DB;
$mform =& $this->_form;
// course id needs to be passed for auth purposes
$mform->addElement('hidden', 'id', optional_param('id', 0, PARAM_INT));
$mform->setType('id', PARAM_INT);
$mform->addElement('header', 'general', get_string('importfile', 'grades'));
$mform->addElement('advcheckbox', 'feedback', get_string('importfeedback', 'grades'));
$mform->setDefault('feedback', 0);
// file upload
$mform->addElement('filepicker', 'userfile', get_string('file'));
$mform->disabledIf('userfile', 'url', 'noteq', '');
$mform->addElement('text', 'url', get_string('fileurl', 'gradeimport_xml'), 'size="80"');
$mform->disabledIf('url', 'userfile', 'noteq', '');
if (!empty($CFG->gradepublishing)) {
$mform->addElement('header', 'publishing', get_string('publishing', 'grades'));
$options = array(get_string('nopublish', 'grades'), get_string('createnewkey', 'userkey'));
$keys = $DB->get_records_select('user_private_key',
"script='grade/import' AND instance=? AND userid=?",
array($COURSE->id, $USER->id));
if ($keys) {
foreach ($keys as $key) {
$options[$key->value] = $key->value; // TODO: add more details - ip restriction, valid until ??
}
}
$mform->addElement('select', 'key', get_string('userkey', 'userkey'), $options);
$mform->addHelpButton('key', 'userkey', 'userkey');
$mform->addElement('static', 'keymanagerlink', get_string('keymanager', 'userkey'),
'<a href="'.$CFG->wwwroot.'/grade/import/keymanager.php?id='.$COURSE->id.'">'.get_string('keymanager', 'userkey').'</a>');
$mform->addElement('text', 'iprestriction', get_string('keyiprestriction', 'userkey'), array('size'=>80));
$mform->addHelpButton('iprestriction', 'keyiprestriction', 'userkey');
$mform->setDefault('iprestriction', getremoteaddr()); // own IP - just in case somebody does not know what user key is
$mform->addElement('date_time_selector', 'validuntil', get_string('keyvaliduntil', 'userkey'), array('optional'=>true));
$mform->addHelpButton('validuntil', 'keyvaliduntil', 'userkey');
$mform->setDefault('validuntil', time()+3600*24*7); // only 1 week default duration - just in case somebody does not know what user key is
$mform->disabledIf('iprestriction', 'key', 'noteq', 1);
$mform->disabledIf('validuntil', 'key', 'noteq', 1);
$mform->disabledIf('iprestriction', 'url', 'eq', '');
$mform->disabledIf('validuntil', 'url', 'eq', '');
$mform->disabledIf('key', 'url', 'eq', '');
}
$this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
}
function validation($data, $files) {
$err = parent::validation($data, $files);
if (empty($data['url']) and empty($data['userfile'])) {
if (array_key_exists('url', $data)) {
$err['url'] = get_string('required');
}
if (array_key_exists('userfile', $data)) {
$err['userfile'] = get_string('required');
}
} else if (array_key_exists('url', $data) and $data['url'] != clean_param($data['url'], PARAM_URL)) {
$err['url'] = get_string('error');
}
return $err;
}
}