]> git.mjollnir.org Git - moodle.git/commitdiff
Updated for better reporting when loading from remote url fails. Now displays error...
authordhawes <dhawes>
Thu, 30 Dec 2004 18:08:38 +0000 (18:08 +0000)
committerdhawes <dhawes>
Thu, 30 Dec 2004 18:08:38 +0000 (18:08 +0000)
blocks/rss_client/block_rss_client.php
blocks/rss_client/block_rss_client_action.php
blocks/rss_client/config_global.html
blocks/rss_client/config_instance.html
rss/templib.php

index bc7b8fc719479ddafc2cb873de048b18d759c7cd..0b79e4865528df5efd30c6fc7738702c9f6f3899 100644 (file)
@@ -9,8 +9,13 @@ class block_rss_client extends block_base {
     }
 
     function specialization() {
+        // After the block has been loaded we customize the block's title display
         if (!empty($this->config) && !empty($this->config->title)) {
+            // There is a customized block title, display it
             $this->title = $this->config->title;
+        } else {
+            // No customized block title, use localized remote news feed string
+            $this->title = get_string('block_rss_remote_news_feed', 'block_rss_client');
         }
     }
     
@@ -22,7 +27,7 @@ class block_rss_client extends block_base {
         }
 
         $this->content = new stdClass;
-        $this->content->footer = '';        
+        $this->content->footer = '';
         
         if (empty($this->instance) || empty($CFG->blog_version)) {
             // Either we're being asked for content without
@@ -43,7 +48,13 @@ class block_rss_client extends block_base {
 
         if (!empty($this->config)) {
             if (!empty($this->config->rssid)) {
-                $rssid = intval($this->config->rssid);
+                if (blog_array_count($this->config->rssid)) {
+                    // rssid is an array of rssids
+                    $rssidarray = $this->config->rssid;
+                } else {
+                    // rssid is a single rssid
+                    $rssidarray = array($this->config->rssid);
+                }
             }
             if (!empty($this->config->display_description)) {
                 $display_description = intval($this->config->display_description);
@@ -70,10 +81,39 @@ class block_rss_client extends block_base {
             }
         }
 
+        // Daryl Hawes note: if count of rssidarray is greater than 1 
+        // we should possibly display a drop down menu of selected feed titles
+        // so user can select a single feed to view (similar to RSSFeed)
+        foreach ($rssidarray as $rssid) {
+            $output .=  $this->get_rss_by_id($rssid, $display_description, $shownumentries);
+        }
+        
+        $this->content->text = $output;
+        return $this->content;
+    }
+    
+    function instance_allow_multiple() {
+        return true;
+    }
+
+    function has_config() {
+        return true;
+    }
+
+    function instance_allow_config() {
+        return true;
+    }
+
+    function get_rss_by_id($rssid, $display_description, $shownumentries) {
+        $returnstring = '';
         $rss_record = get_record('block_rss_client', 'id', $rssid);
         if (isset($rss_record) && isset($rss_record->id)) {
             $rss = rss_get_feed($rss_record->id, $rss_record->url, $rss_record->type);
     //      print_object($rss); //debug        
+            if (empty($rss)) {
+                // There was a failure in loading the rss feed
+                return;
+            }
 
             if ($shownumentries > 0 && $shownumentries < count($rss->items) ) {
                 $count_to = $shownumentries;
@@ -84,7 +124,7 @@ class block_rss_client extends block_base {
             for ($y = 0; $y < $count_to; $y++) {
                 if ($rss->items[$y]['title'] == '') {
 //                    $rss->items[$y]['description'] = blog_unhtmlentities($rss->items[$y]['description']);
-                    //can define an additional instance/admin config item for this (20) - max_description_length
+                    //Daryl Hawes note: might want to define an additional instance/admin config item for this (20) - max_description_length
                     $rss->items[$y]['title'] = substr(strip_tags($rss->items[$y]['description']), 0, 20) . '...';
                 }
         
@@ -92,41 +132,25 @@ class block_rss_client extends block_base {
                     $rss->items[$y]['link'] = $rss->items[$y]['guid'];
                 }
 
-                $output .= '<a href="'. $rss->items[$y]['link'] .'" target=_new>'. $rss->items[$y]['title'] . '</a><br />' ."\n";
+                $returnstring .= '<a href="'. $rss->items[$y]['link'] .'" target=_new>'. $rss->items[$y]['title'] . '</a><br />' ."\n";
                 
-                if ($display_description){
-                    $output .= $rss->items[$y]['description'] . '<br />' ."\n";
+                if ($display_description && !empty($rss->items[$y]['description'])){
+                    $returnstring .= $rss->items[$y]['description'] . '<br />' ."\n";
                 }
             }
 
-            $output .= '<br />';
-    //      print_object($rss); //debug
-            $feedtitle = get_string('block_rss_remote_news_feed', 'block_rss_client');
-            
+    //      print_object($rss); //debug            
             if ( isset($rss->channel['link']) && isset($rss->channel['title']) ) {
                 $feedtitle = '<a href="'. $rss->channel['link'] .'">'. $rss->channel['title'] .'</a>';
             }
         }
 
-        //can we reset the title here?
-        if (isset($feedtitle) && $feedtitle != '') {
+        if (isset($feedtitle) && $feedtitle != '' && $feedtitle != '<a href="'. $rss->channel['link'] .'"></a>') {
             $this->title = $feedtitle;
         }
-
-        $this->content->text = $output;
-        return $this->content;
-    }
-    
-    function instance_allow_multiple() {
-        return true;
-    }
-
-    function has_config() {
-        return true;
+        $returnstring .= '<br />';
+        return $returnstring;
     }
 
-    function instance_allow_config() {
-        return true;
-    }
 }
 ?>
\ No newline at end of file
index 2c178528b5a3949de88e7028173bec8772db1d74..aa1942f53058a6ae4fbb03aa2c885ce951da3e14 100644 (file)
         }
             
         $rss = rss_get_feed($rssid, $url, $rsstype);
+
+        if (empty($rss)) {
+            print 'There was an error loading this rss feed. You may want to verify the url you have specified before using it.';
+        }
         
         $dataobject->id = $rssid;
         if (!empty($rss->channel['description'])) {
index c0102f7a74f8f730f53535329d35f802581b6113..80ae8b02482e99071d67eb9bb6327e2e308e0f10 100644 (file)
@@ -11,7 +11,7 @@
         } ?>" />
     </td>
     <td>
-        <?php print_string('block_rss_num_entries', 'block_rss_client') ?>
+        <?php print_string('block_rss_client_num_entries', 'block_rss_client') ?>
     </td>
 </tr>
 <tr valign="top">
@@ -39,6 +39,7 @@
     <input type="submit" value="<?php print_string('savechanges') ?>"></td>
 </tr>
 <tr>
-    <td align=center><a href=" <?php echo $CFG->wwwroot; ?>/blocks/rss_client/block_rss_client_action.php?courseid=<?php echo $courseid; ?>"><?php print_string('block_rss_feeds_add_edit', 'block_rss_client')?></a></center><br /><br />
+    
+    <td colspan="3" align="center"><a href=" <?php echo $CFG->wwwroot; ?>/blocks/rss_client/block_rss_client_action.php?courseid=<?php echo $courseid; ?>"><?php print_string('block_rss_feeds_add_edit', 'block_rss_client')?></a></center><br /><br />
     </td>
 </table>
\ No newline at end of file
index a378d6d0b9671e3b9a2af0f4c36546141c54beff..9f2e3eba68072a3cb834d38e514e70d4b36b0629 100644 (file)
     </td>
     <td>
         <?php 
-            $selected = '';
+            $selectedarray = '';
             if (isset($this->config) && isset($this->config->rssid)) {
-                $selected = $this->config->rssid;
+                if (blog_array_count($this->config->rssid)) {
+                    // rssid is an array of rssids
+                    $selectedarray = $this->config->rssid;
+                } else {
+                    // rssid is a single rssid
+                    $selectedarray = array($this->config->rssid);
+                }
             }
             if ($rssfeeds = get_records('block_rss_client')) {
                 foreach($rssfeeds as $rssfeed){
                     $feedoptions[$rssfeed->id] = $rssfeed->title;
                 }
-                choose_from_menu ($feedoptions, 'rssid', $selected);
+                $dropdownmenustring = choose_from_menu($feedoptions, 'rssid[]', '', '', '', '0', true);
+
+                //Daryl Hawes note:
+                // moodle's choose_from_menu() function does not support
+                // the "multiple" or "size" options, so before printing out the
+                // calculated drop down menu we insert the keyword "multiple"
+                $dropdownmenustring = preg_replace("|\"rssid\[\]\" >*|","\"rssid[]\" multiple>", $dropdownmenustring);
+
+                // since there may be multiple rssids to select
+                // we need to check for each
+                foreach ($selectedarray as $selected) {
+                    $selected = intval($selected);
+                    $dropdownmenustring = preg_replace("|\"$selected\">*|","\"$selected\" selected>", $dropdownmenustring);
+                }
+
+                print $dropdownmenustring;
+
             } else {
                 print_string('block_rss_no_feeds', 'block_rss_client');
                 if ( isadmin() ){
     </td>
 </tr>
 <tr valign="top">
-    <td align="right"><p><?php print_string('block_rss_word_title', 'block_rss_client'); ?>:</td>
-    <td><input type="text" name="title" size="30" value="<?php echo $this->config->title; ?>" />
+    <td align="right"><p><?php print_string('uploadlabel'); ?></td>
+    <?php 
+        $title = '';
+        if (!empty($this->config) && !empty($this->config->title)) {
+            $title = $this->config->title;
+        }
+    ?>
+    <td><input type="text" name="title" size="30" value="<?php echo $title; ?>" />
     </td>
 </tr>
 <tr>
index c84cf08a44adca29881280218f9ab13b9d055abd..ce6aa98862330cfa3980670b4a31cf62edfbab82 100644 (file)
@@ -9,29 +9,36 @@ if (empty($CFG->block_rss_client_submitters) ) {
 if (empty($CFG->block_rss_client_num_entries) ) {
     $CFG->block_rss_client_num_entries = 5; //default to 5 entries per block
 }
+if (empty($CFG->block_rss_timeout) ) {
+    $CFG->block_rss_timeout = 30;
+}
 
-/*
-* rss_get_feed
-*   Determines whether or not to get a news feed remotely or from cache and reads it into a string
-* rssid - id of feed in blog_rss table
-* url - remote url of feed
-* type - either 'A' or 'R' where A is an atom feed and R is either rss or rdf
-* NOTE that this requires allow_url_fopen be On in your php.ini file (it may
- be off for security by your web host)
-*/
+/**
+ *   Determines whether or not to get a news feed remotely or from cache and reads it into a string
+ * @param int rssid - id of feed in blog_rss table
+ * @param string url - url of remote feed
+ * @param string type - either 'A' or 'R' where A is an atom feed and R is either rss or rdf
+ * @return Atom|MagpieRSS|null This function returns an Atom object in the case of an Atom feed, a MagpieRSS object in the case of an RDF/RSS feed or null if there was an error loading the remote feed.
+ * NOTE that this function requires allow_url_fopen be On in your php.ini file 
+ * (it may be off for security by your web host)
+ */
 function rss_get_feed($rssid, $url, $type) {
     
-    global $CFG;;
-    $write = 0;
-
+    global $CFG;
+    $writetofile = false;
+    $urlfailurestring = 'Failed to open remote feed at: ' . $url .'<br /> allow_url_fopen needs to be On in the php.ini file for this file wrapper call to work. Please refer to <a href="http://us2.php.net/filesystem">http://us2.php.net/filesystem</a>';
+    $filefailurestring = 'Could not open the file located at: ';
     $secs = $CFG->block_rss_timeout * 60;
 
+    // If moodle dataroot cache folder is missing create it
     if (!file_exists($CFG->dataroot .'/cache/')) {
         mkdir($CFG->dataroot .'/cache');
     }
+    // If moodle dataroot cache/rsscache folder is missing create it
     if (!file_exists($CFG->dataroot .'/cache/rsscache/')) {
         mkdir($CFG->dataroot .'/cache/rsscache');
     }
+
     $file = $CFG->dataroot .'/cache/rsscache/'. $rssid .'.xml';
 //    echo "file = ". $file; //debug
     
@@ -45,33 +52,60 @@ function rss_get_feed($rssid, $url, $type) {
             $data = @stat($file);
         }
         $now = time();
-        if (($now - $data[10]) > $secs) { //if timedout
-    //          echo "read from original"; //debug
-            //read from source
-            if ($CFG->debug){
-                $xml = file($url) or die ('Could not open the feed located at the url: ' . $url . 'allow_url_fopen needs to be On in the php.ini file for this file wrapper call to work. Please refer to <a href="http://us2.php.net/filesystem">http://us2.php.net/filesystem</a>');
+        if (($now - $data[10]) > $secs) {
+            // The cached file has expired. Attempt to read fresh from source
+            $xml = load_feed_from_url($url);
+            if ($xml) {
+                //success
+                $writetofile = true;
             } else {
-                $xml = @file($url) or die ('Could not open the feed located at the url: ' . $url .'allow_url_fopen needs to be On in the php.ini file for this file wrapper call to work. Please refer to <a href="http://us2.php.net/filesystem">http://us2.php.net/filesystem</a>');
+                // Failed to load remote feed. Since the file exists attempt to read from cache
+                if ($CFG->debug) {
+                    print $urlfailurestring;
+                }
+                $xml = load_feed_from_file($file);
+                if (!$xml) {
+                    // Failed to load from cache as well!
+                    if ($CFG->debug) {
+                        print $filefailurestring . $file;
+                        return;
+                    }
+                }
             }
-            $write = 1;
         } else {
-    //          echo "read from cache"; //debug
-            //read in from cache
-            if ($CFG->debug){
-                $xml = file($file) or die ('Could not open the file located at: ' . $file);
-            } else {
-                $xml = @file($file) or die ('Could not open the file located at: ' . $file);
+            // Cached file has not expired. Attempt to read from cached file.
+            $xml = load_feed_from_file($file);
+            if (!$xml) {
+                // Failed to load from cache, attempt to read from source
+                if ($CFG->debug) {
+                    print $filefailurestring . $file;
+                }
+                $xml = load_feed_from_url($url);
+                if ($xml) {
+                    // success
+                    $writetofile = true;
+                } else {
+                    // Failed to read from source as well!
+                    if ($CFG->debug) {
+                        print $urlfailurestring;
+                    }
+                    return;
+                }
             }
         }
-    } else { //DNE, read from source
-    //      echo "url: ".$url; //debug
-
-        if ($CFG->debug){
-            $xml = file($url) or die ('Could not open the feed located at the url: ' . $url . 'allow_url_fopen needs to be Onin the php.ini file for this file wrapper call to work. Please refer to <a href="http://us2.php.net/filesystem">http://us2.php.net/filesystem</a>');
+    } else { 
+        // No cached fil at all, read from source
+        $xml = load_feed_from_url($url);
+        if ($xml) {
+            //success
+            $writetofile = true;
         } else {
-            $xml = @file($url) or die ('Could not open the feed located at the url: ' . $url . 'allow_url_fopen needs to be On in the php.ini file for this file wrapper call to work. Please refer to <a href="http://us2.php.net/filesystem">http://us2.php.net/filesystem</a>');
+            // Failed to read from source url!
+            if ($CFG->debug) {
+                print $urlfailurestring;
+            }
+            return;
         }
-        $write = 1;
     }
     
     //print_object($xml); //debug
@@ -81,7 +115,7 @@ function rss_get_feed($rssid, $url, $type) {
         $xmlstr = @implode(' ', $xml);
     }
     
-    if ( $write && !empty($xmlstr) ) { //write file to cache
+    if ( $writetofile && !empty($xmlstr) ) { //write file to cache
         // jlb: adding file:/ to the start of the file name fixed
         // some caching problems that I was experiencing.
         //$file="file:/" + $file;
@@ -107,7 +141,39 @@ function rss_get_feed($rssid, $url, $type) {
     }
 }
 
+/**
+ * @param string $file The path to the cached feed to load
+ */
+function load_feed_from_file($file) {
+    global $CFG;
+//          echo "read from cache"; //debug
+    //read in from cache
+    if ($CFG->debug){
+        $xml = file($file);
+    } else {
+        $xml = @file($file);
+    }
+    return $xml;
+}
 
+/**
+ * @param string $url The url of the remote news feed to load
+ */
+function load_feed_from_url($url) {
+    global $CFG;
+//          echo "read from original"; //debug
+    //read from source
+    if ($CFG->debug){
+        $xml = file($url);
+    } else {
+        $xml = @file($url);
+    }
+    return $xml;
+}
+
+/**
+ * @param int $rssid .
+ */
 function rss_display_feeds($rssid='none') {
     global $db, $USER, $CFG, $THEME;
     global $blogid; //hackish, but if there is a blogid it would be good to preserve it
@@ -165,6 +231,13 @@ $deleteString .= '" title="'. get_string('delete') .'" align="absmiddle" border=
     }
 }
 
+/**
+ * @param string $act .
+ * @param string $url .
+ * @param int $rssid .
+ * @param string $rsstype .
+ * @param bool $printnow .
+ */
 function rss_get_form($act, $url, $rssid, $rsstype, $printnow=true) {
     global $USER, $CFG, $_SERVER, $blockid, $blockaction;
     global $blogid; //hackish, but if there is a blogid it would be good to preserve it
@@ -229,6 +302,8 @@ function rss_get_form($act, $url, $rssid, $rsstype, $printnow=true) {
  * added by Daryl Hawes for rss/atom feeds
  * found at http://us4.php.net/manual/en/function.fwrite.php
  * added check for moodle debug option. if off then use '@' to suppress error/warning messages
+ * @param string $filename .
+ * @param string $content .
  */
 if (! function_exists('file_put_contents')){
     function file_put_contents($filename, $content) {