File: rsslib.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (122 lines) | stat: -rw-r--r-- 5,302 bytes parent folder | download | duplicates (2)
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
<?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)) {
            //Some debug...
            if ($CFG->debug > 7) {
                echo "DISABLED (admin variables)";
            }
        }
        // Check CFG->data_enablerssfeeds.
        else if (empty($CFG->data_enablerssfeeds)) {
            //Some debug...
            if ($CFG->debug > 7) {
                echo "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;
    }

?>