-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_sheet.php
80 lines (62 loc) · 2.2 KB
/
upload_sheet.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
<?php
require_once('settings.php');
require_once('moodle.inc');
if (isloggedin() && $USER->username != 'guest') {
$moodle_id = $USER->id;
} else {
die("Not logged in");
}
if (isloggedin() && $USER->username != 'guest') {
$moodle_id = $USER->id;
} else {
die("Not logged in");
}
$link = mysql_connect($db_server,$db_username,$db_password);
if (! mysql_select_db($db_database)) {
die(mysql_error());
}
if (empty( $type_id)) {
if (preg_match('/^\d+$/', $_POST['type_id'])) {
$query = sprintf('SELECT type_id, number_of_columns FROM module_sheet_type WHERE type_id=%s',$_POST['type_id']);
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
if ($row = mysql_fetch_assoc($result)) {
$type_id = $row['type_id'];
$number_of_columns = $row['number_of_columns'];
} else {
$message = 'Unknown type id ' . $_POST['type_id'];
die($message);
}
} else {
$message = 'Invalid type id';
die($message);
return;
}
}
$name = mysql_real_escape_string( $_POST['name']);
if ($name == '') die("Sheet name must be specified");
$query = sprintf('insert into module_sheets (type_id, title, number_of_rows, moodle_id) values (%d,"%s",0,%d);',$type_id, $name, $moodle_id);
if (! mysql_query($query)) die("Could not create sheet");
$sheet_id = mysql_insert_id();
$r = 0;
if (($handle = fopen($_FILES['sheet_data']['tmp_name'], "r")) !== FALSE) {
while (($data = fgetcsv($handle,0, ",")) !== FALSE) {
$num = count($data);
if ($num > $number_of_columns) $num = $number_of_columns;
for ($c=0; $c < $num; $c++) {
$query = sprintf('insert into module_data (sheet_id, row_id, column_id, value) values (%s,%s,%s,"%s");',
$sheet_id, $r, $c, mysql_real_escape_string($data[$c]));
if (! mysql_query($query)) die("Could not insert data");
}
$r++;
}
fclose($handle);
}
$query = sprintf('update module_sheets set number_of_rows=%s where sheet_id=%s',$r,$sheet_id);
if (! mysql_query($query)) die("Could not update number of rows");
header("Location: " . $sqa_www_root . "/admin_audits.php");
?>