-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·91 lines (66 loc) · 2.39 KB
/
index.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
<?php
require_once('../../../config.php');
require_once('forms.php');
require_once('lib.php');
$_s = function($key, $a=NULL) { return get_string($key, 'gradeimport_smart', $a); };
$id = required_param('id', PARAM_INT);
$url = new moodle_url('/grade/import/smart/index.php', array('id' => $id));
$PAGE->set_url($url);
if (!$course = $DB->get_record('course', array('id' => $id))) {
print_error('nocourseid');
}
require_login($course);
$context = context_course::instance($id);
$PAGE->set_context($context);
require_capability('moodle/grade:import', $context);
require_capability('gradeimport/smart:view', $context);
$file_text = optional_param('file_text', null, PARAM_TEXT);
print_grade_page_head($course->id, 'import', 'smart');
$file_form = new smart_file_form();
$results_form = new smart_results_form(null, array('messages' => null));
if ($form_data = $file_form->get_data()) {
$file_text = $file_form->get_file_content('userfile');
$grade_item_id = $form_data->grade_item_id;
$messages = array();
$import_success = true;
if (!$smart_file = smart_autodiscover_filetype($file_text)) {
$messages[] = $_s('file_not_identified');
$import_success = false;
}
if ($import_success) {
$smart_file->validate();
$smart_file->extract_data();
$smart_file->set_courseid($id);
$smart_file->set_gi_id($grade_item_id);
$smart_file->convert_ids();
if (!$smart_file->insert_grades()) {
$messages[] = $_s('import_error');
$import_success = false;
}
if ($smart_file->bad_lines) {
foreach ($smart_file->bad_lines as $n => $line) {
$messages[] = $_s('bad_line', $n);
}
}
if ($smart_file->bad_ids) {
foreach ($smart_file->bad_ids as $userid) {
$messages[] = $_s('bad_userid', $userid);
}
}
}
if (!$import_success) {
echo $OUTPUT->notification($_s('failure'));
} else {
echo $OUTPUT->notification($_s('success'), 'notifysuccess');
}
$data = array('messages' => $messages);
if ($messages) {
$results_form = new smart_results_form(null, $data);
$results_form->display();
}
$url = new moodle_url('/grade/index.php', array('id' => $id));
echo $OUTPUT->continue_button($url);
} else {
$file_form->display();
}
echo $OUTPUT->footer();