-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadcourselib.php
53 lines (46 loc) · 1.58 KB
/
uploadcourselib.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
<?php
defined('MOODLE_INTERNAL') || die();
/**
* Validation callback function - verified the column line of csv file.
* Converts standard column names to lowercase.
* @param csv_import_reader $cir
* @param array $stdfields standard user fields
* @param moodle_url $returnurl return url in case of any error
* @return array list of fields
*/
function cu_validate_course_upload_columns(csv_import_reader $cir, $stdfields, moodle_url $returnurl) {
$columns = $cir->get_columns();
if (empty($columns)) {
$cir->close();
$cir->cleanup();
print_error('cannotreadtmpfile', 'error', $returnurl);
}
if (count($columns) < 2) {
$cir->close();
$cir->cleanup();
print_error('csvfewcolumns', 'error', $returnurl);
}
$textlib = textlib_get_instance(); // profile fields may contain unicode chars
// test columns
$processed = array();
foreach ($columns as $key=>$unused) {
$field = $columns[$key];
$lcfield = $textlib->strtolower($field);
if (in_array($field, $stdfields) or in_array($lcfield, $stdfields)) {
// standard fields are only lowercase
$newfield = $lcfield;
} else {
$cir->close();
$cir->cleanup();
print_error('invalidfieldname', 'error', $returnurl, $field);
}
if (in_array($newfield, $processed)) {
$cir->close();
$cir->cleanup();
print_error('duplicatefieldname', 'error', $returnurl, $newfield);
}
$processed[$key] = $newfield;
}
return $processed;
}
?>