-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourseList.php
75 lines (62 loc) · 1.79 KB
/
courseList.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
<?php
/**
* Generates JSON file from MOOCs database
* to be used later for processing by D3 library functions
*
* @author Chris Rehfeld
* @author Tatiana Bragints
*/
header('content-type: application/json;charset=utf-8');
//header('content-type: text/plain;charset=utf-8');
require_once 'connection.php';
$sql = "
select site, title, course_link from course_data
";
$result = $dbc->query($sql);
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
$groups = array();
foreach ($rows as $row) {
extract($row);
$letter = mb_strtolower(mb_substr($title, 0, 1, 'utf-8'), 'utf-8');
if ($site === "MIT") {
$groups[$letter]["MIT"]["..."][] = $row;
} else {
$groups[$letter]["Coursera, Canvas, Edx, Stanford, Udacity"][$site][] = $row;
}
}
/**
* Converts array to JSON structure
* @param array $arr
* @return string
*/
function toD3Structure($arr) {
//check if it has numeric keys
//non-numeric means titles and names etc...
//assume first key represents all the keys types
if (is_numeric(key($arr)) && array_key_exists("title", $arr[0])) {
$ret = array();
foreach ($arr as $subArr) {
//$ret[] = array('name' => $subArr['title']);
$ret[] = $subArr + array('name' => $subArr['title'], 'size' => 2);
}
return $ret;
}
$ret = array();
foreach ($arr as $key => $subArr) {
if (is_numeric($key)) {
$key = "...";
}
if (count($subArr) > 15) {
$ret[] = array('name' => $key, 'children' => toD3Structure(array_chunk($subArr, 15)));
} else {
$ret[] = array('name' => $key, 'children' => toD3Structure($subArr));
}
}
return $ret;
}
$dataGroupedByLetters = array_map('toD3Structure', $groups);
//print_r($dataGroupedByLetters);
echo json_encode($dataGroupedByLetters);