forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsslib.php
116 lines (91 loc) · 5.03 KB
/
rsslib.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
<?php
// This file adds support to rss feeds generation
// This function is the main entry point to database module
// rss feeds generation. Foreach database with rss enabled
// build one XML rss structure.
function data_rss_feeds() {
global $CFG;
$status = true;
// Check CFG->enablerssfeeds.
if (empty($CFG->enablerssfeeds)) {
debugging("DISABLED (admin variables)");
}
// Check CFG->data_enablerssfeeds.
else if (empty($CFG->data_enablerssfeeds)) {
debugging("DISABLED (module configuration)");
}
// It's working so we start...
else {
// Iterate over all data.
if ($datas = get_records('data')) {
foreach ($datas as $data) {
if ($data->rssarticles > 0) {
// Get the first field in the list (a hack for now until we have a selector)
if (!$firstfield = get_record_sql('SELECT id,name from '.$CFG->prefix.'data_fields WHERE dataid = '.$data->id.' ORDER by id', true)) {
continue;
}
// Get the data_records out.
$approved = ($data->approval) ? ' AND dr.approved = 1 ' : ' ';
$sql = 'SELECT dr.* ' .
"FROM {$CFG->prefix}data_records AS dr " .
"WHERE dr.dataid = {$data->id} " .$approved.
'ORDER BY dr.timecreated DESC ' .
"LIMIT {$data->rssarticles}";
if (!$records = get_records_sql($sql)) {
continue;
}
$firstrecord = array_shift($records); // Get the first and put it back
array_unshift($records, $firstrecord);
$filename = rss_file_name('data', $data);
if (file_exists($filename)) {
if (filemtime($filename) >= $firstrecord->timemodified) {
continue;
}
}
// Now create all the articles
mtrace('Creating feed for '.$data->name);
$items = array();
foreach ($records as $record) {
$recordarray = array();
array_push($recordarray, $record);
$item = null;
// guess title or not
if ($data->rsstitletemplate) {
$item->title = data_print_template('rsstitletemplate', $recordarray, $data, '', 0, true);
} else { // else we guess
$item->title = strip_tags(get_field('data_content', 'content',
'fieldid', $firstfield->id, 'recordid', $record->id));
}
$item->description = data_print_template('rsstemplate', $recordarray, $data, '', 0, true);
$item->pubdate = $record->timecreated;
$item->link = $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$record->id;
array_push($items, $item);
}
$course = get_record('course', 'id', $data->course);
// First all rss feeds common headers.
$header = rss_standard_header($course->shortname.': '.format_string($data->name,true),
$CFG->wwwroot."/mod/data/view.php?d=".$data->id,
format_string($data->intro,true));
if (!empty($header)) {
$articles = rss_add_items($items);
}
// Now all rss feeds common footers.
if (!empty($header) && !empty($articles)) {
$footer = rss_standard_footer();
}
// Now, if everything is ok, concatenate it.
if (!empty($header) && !empty($articles) && !empty($footer)) {
$rss = $header.$articles.$footer;
//Save the XML contents to file.
$status = rss_save_file('data', $data, $rss);
}
else {
$status = false;
}
}
}
}
}
return $status;
}
?>