-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedit_upload_form.php
165 lines (131 loc) · 5.11 KB
/
edit_upload_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
// to display as moodle page
require_once dirname(__FILE__)."/../../config.php";
//to read the csv file
require_once($CFG->libdir.'/csvlib.class.php');
//here we store the form to upload the csv file
require_once('upload_form.php');
require_login();
$context = context_course::instance($COURSE->id);
require_capability('moodle/site:manageblocks', $context);
$PAGE->set_context(context_system::instance());
$PAGE->set_url('/blocks/simple_map/edit_upload_form.php');
$place = new stdClass();
// the name of the table in the database
$table = 'block_simple_map_places';
$PAGE->set_title(format_string(get_string('upload_form','block_simple_map')));
$PAGE->set_heading(get_string('upload_form','block_simple_map'));
$PAGE->navbar->add("View list of places");
echo $OUTPUT->header();
$mform = new select_file_form();
if ($formdata = $mform->get_data()) {
$iid = csv_import_reader::get_new_iid('simple_map');
$cir = new csv_import_reader($iid, 'simple_map');
$content = $mform->get_file_content('userfile');
$readcount = $cir->load_csv_content($content, 'UTF-8', 'semicolon');
$columns = $cir->get_columns();
//$result = $DB->delete_records($table);
$data = array();
$cir->init();
$linenum = 0; //column header is first line
$noerror = true; // Keep status of any error.
$lines_in_database = $DB->count_records($table);
while ($linenum <= $readcount and $fields = $cir->next()) {
$result = null;
//we create the place Object
$place->title = $fields[0];
$place->description = $fields[1];
$place->opening_hours = $fields[2];
$place->address = $fields[3];
$place->city = $fields[4];
$place->area_code = $fields[5];
$place->country = $fields[6];
$place->region = $fields[7];
$place->lat = $fields[8];
$place->lng = $fields[9];
$place->category = $fields[10];
$place->link_1 = $fields[11];
$place->link_2 = $fields[12];
$place->link_3 = $fields[13];
$place->link_4 = $fields[14];
$place->link_5 = $fields[15];
$place->contact = $fields[16];
// is this record already in the database?
if ($results = $DB->get_records($table,array('title'=>$place->title))) {
foreach ($results as $result) {
$place->id = $result->id;
// make sure the lat and long values are with ".", not with ","
if ($place->lng || $place->lat) {
$place->lat = str_replace(",", ".", $place->lat);
$place->lng = str_replace(",", ".", $place->lng);
}
$DB->update_record($table, $place, $bulk=false);
}
}
else {
// First we check, if the Geo-Coordinates are already submitted. If not, we fetch them now, if there is an address to do so
if ($place->address && !$place->lat && !$place->lng) {
$object = get_lat_lng_by_address($place->address, $place->city, $place->country);
$place->lat = $object->lat;
$place->lng = $object->lng;
}
$lines_in_database++;
//$place->id = $lines_in_database;
$DB->insert_record($table, $place);
}
$linenum++;
}
//unset($content);
}
echo get_string('upload_instructions', 'block_simple_map').'
<br /><br />
<a href="export_table.php?sesskey='.sesskey().'&example=true">Download sample csv file (first line only)</a>
<br />
<a href="export_table.php?sesskey='.sesskey().'">'.get_string('download_instructions', 'block_simple_map')."</a>";
//load all the records from the database and display them in a table
//$results = $DB->delete_records($table);
$results = $DB->get_records($table);
echo '<table id="block_simple_map_editentries" border="1"><tr>
<th>ID</th>
<th>Title and description, etc.</th>
<th>
<a href="delete_record.php?id=-2&sesskey='.sesskey().'">Delete all</a>
</th>
</tr>';
foreach ($results as $result) {
$class = "";
//before we display the list, we check once again if there are the geo-coordinates available:
if ($result->address && !$result->lat && !$result->lng) {
$object = get_lat_lng_by_address($result->address, $result->city, $result->country);
if (!$object) {
$class = "class ='red'";
}
else {
$result->lat = $object->lat;
$result->lng = $object->lng;
// and we update the database
$DB->update_record($table, $result, $bulk=false);
}
}
echo '<tr '.$class.'>';
echo '
<td><a href="edit_record.php?id='.$result->id.'&sesskey='.sesskey().'">'.$result->id.' edit</a></td>
<td><div style="font-weight: bold;">Title: '.$result->title.'</div><div>'.$result->description.'</div>
<div>'.$result->opening_hours.'</div>
<div>'.$result->address.'<br />'.$result->city.'<br />'.$result->area_code.'<br />'.$result->country.'<br />'.$result->region.'</div>
<div>'.$result->lat.', '.$result->lng.'</div>
<div>'.$result->category.'</div>
<div>'.$result->link_1.'</div>
<div>'.$result->link_2.'</div>
<div>'.$result->link_3.'</div>
<div>'.$result->link_4.'</div>
<div>'.$result->link_5.'</div>
<div>'.$result->contact.'</div></td>
<td><a href="delete_record.php?id='.$result->id.'&sesskey='.sesskey().'">delete</a></td>';
echo '</tr>';
}
echo '</table>';
echo '<a href="edit_record.php?id=-1&sesskey='.sesskey().'">'.get_string('insert_new_record', 'block_simple_map')."</a>";
$mform->display();
echo $OUTPUT->footer();
?>