-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradebook.install
66 lines (56 loc) · 1.76 KB
/
gradebook.install
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
<?php
/**
* @file
* Install, update and uninstall functions for the gradebook module.
*/
use Drupal\Core\Language\Language;
/**
* Implements hook_install().
*/
function gradebook_install() {
gradebook_create_terms();
}
/**
* Create default taxonomy terms.
*/
function gradebook_create_terms() {
$files = ['grade_aggregation_type', 'grade_display_type', 'grade_method', 'grade_source'];
foreach ($files as $file) {
$path = \Drupal::service('extension.list.module')->getPath('gradebook');
$filename = $path . '/data/taxonomy_term/' . $file . '.csv';
$fp = fopen($filename, 'r');
if ( $fp === FALSE ) {
\Drupal::logger('gradebook')->error('Failed to open @filename.', array('@filename' => $filename));
return;
}
$header = [];
while (($line = fgetcsv($fp, 1000, ",")) !== FALSE) {
if ( $line === FALSE ) {
// No more records to process or something else happened.
break;
}
else {
// First row is the header. We don't need to process it.
if (!count($header)) {
$header = $line;
continue;
}
// prepare array of values.
$row = array_combine($header, $line);
$values = [
'vid' => $row['vid'],
'name' => $row['name'],
'description' => $row['description'],
'format' => (isset($row['format'])) ? $row['format'] : filter_fallback_format(),
'weight' => isset($row['weight']) ? $row['weight'] : 0,
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
'term_machine_name' => $row['term_machine_name'],
];
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->create($values);
$term->save();
}
}
// close handle.
fclose($fp);
}
}