-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.php
137 lines (115 loc) · 3.88 KB
/
json.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
<?php
/**
* Dynamically output SIMILE Timeline JSON, given a Moodle Database ID, and
* the correct schema (effectively a json-feed for 'data'.)
*
* @author Nick Freear.
* @copyright Copyright (c) 2010 Nicholas Freear.
* @license http://gnu.org/copyleft/gpl.html
*/
/**
Usage 2:
[Timeline]
; Alternatively, get data from Moodle.
dataSrc= mod/data
dataId = 4
;;wikiUrl= mod/forum/discuss.php?d=2&title=
;;wikiSection=?
; The following are the same as above.
date = 1870
intervalUnit = CENTURY
intervalPixels= 75
[/Timeline]
*/
ini_set('display_errors', 1);
require_once('../../config.php');
$module_id = required_param('mid', PARAM_INT); #'d=' or 'mid='?
#$module = optional_param('m', 'mod/data', PARAM_RAW); #Or '=data'?
$json_pad = optional_param('jsonpad', 'var timeline_data', PARAM_RAW);
$json_pad_var = FALSE;
if (preg_match('/^var [a-zA-Z_]+$/', $json_pad)) {
#Variable.
$json_pad_var = TRUE;
} elseif (preg_match('/^[a-zA-Z_]+$/', $json_pad)) {
#Function name.
} else {
#ERROR.
}
_timeline_get_data($module_id, $module=NULL);
function _timeline_get_data($data_id, $data_mod) {
#$courseid= 2;
if (is_numeric($data_id)) { // backwards compatibility
$data = get_record('data', 'id', $data_id);
}
#ELSE: error?
$name = $data->name;
$intro= $data->intro;
$data_fields = get_records("data_fields", "dataid", $data_id);
$ids = array();
foreach ($data_fields as $field) {
$ids[$field->name] = $field->id;
$fields[$field->id] = $field->name;
}
//Required.
if (!isset($ids['title'])) {
die("Error, 'title' is a required field.");
}
if (!isset($ids['start'])) {
die("Error, 'start' (start-date) is a required field.");
}
$data_records= get_records("data_records", "dataid", $data_id);
$events = array();
foreach ($data_records as $record) {
$record_id = $record->id;
$data_content= get_records("data_content", "recordid", $record_id);
$event = array();
foreach ($data_content as $content) {
// Optionally, append meta-data inc. comment-count to end of content, truncate? (+Prepend 'subTitle'? No!)
$value = $content->content;
$field_id= $content->fieldid;
$name = str_replace(' ','', trim($fields[$field_id])); #lcfirst?
switch ($name) {
case 'start': #Drop-thru.
case 'end':
$event[$name] = parse_date_to_iso($value);
break;
default:
$event[$name] = $value;
break;
}
}
#$event['isDuration'] = true;
$events[] = $event;
}
global $CFG;
$url = $CFG->wwwroot."/mod/data/view.php?d=$data_id&title=";
$title= str_replace(' ', '_', $data->name);
$timeline = array('dateTimeFormat'=>'iso8601', 'wikiURL'=>$url,
'wikiSection'=>$title, 'events'=>$events);
@header("Content-Type: text/plain; charset=utf-8"); #application/json.
$json = json_encode($timeline);
$json = str_replace(array('{"', ',"'), array("{\n\"", ",\n\""), $json);
// JSON-P I think!
//echo "var timeline_data = $json;";
echo $json;
}
function parse_date_to_iso($value) {
$iso = false;
if (is_numeric($value) && $value < 1000) { #Just a year<1000, eg. 868?!
$iso = sprintf('%04d', $value);
} elseif (is_numeric($value)) {
$iso = $value;
} else {
$ts = strtotime($value);
if ($ts) {
$iso = date('c', $ts);
} elseif (function_exists('date_parse')) { #PHP 5.2+
$p = (object) date_parse($value); #("December 5, 1822");
$iso = sprintf("%04d-%02d-%02d", $p->year, $p->month, $p->day); #Hours..? #T.
} else {
$iso = $value; #Pray!
}
#$event[$name] = $ts ? date('c', $ts) : $value; #echo " C $ts;$value. ";
}
return $iso;
}