Community block MDL-19314 better error messages when an communication error occurs...
authorjerome mouneyrac <jerome@moodle.com>
Mon, 21 Jun 2010 09:00:41 +0000 (09:00 +0000)
committerjerome mouneyrac <jerome@moodle.com>
Mon, 21 Jun 2010 09:00:41 +0000 (09:00 +0000)
blocks/community/communitycourse.php
blocks/community/forms.php
blocks/community/lang/en/block_community.php

index d16b06c..6d6f0b2 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+
 // This file is part of Moodle - http://moodle.org/
 //
 // Moodle is free software: you can redistribute it and/or modify
  * This page display the community course search form.
  * It also handles adding a course to the community block.
  * It also handles downloading a course template.
-*/
+ */
 
 require('../../config.php');
-require_once($CFG->dirroot.'/blocks/community/locallib.php');
-require_once($CFG->dirroot.'/blocks/community/forms.php');
+require_once($CFG->dirroot . '/blocks/community/locallib.php');
+require_once($CFG->dirroot . '/blocks/community/forms.php');
 
 require_login();
 
@@ -40,7 +41,7 @@ $PAGE->set_title(get_string('searchcourse', 'block_community'));
 $PAGE->navbar->ignore_active(true);
 $PAGE->navbar->add(get_string('searchcourse', 'block_community'));
 
-$search  = optional_param('search', null, PARAM_TEXT);
+$search = optional_param('search', null, PARAM_TEXT);
 
 //if no capability to search course, display an error message
 $usercansearch = has_capability('moodle/community:add', get_context_instance(CONTEXT_USER, $USER->id));
@@ -56,42 +57,42 @@ if (empty($usercansearch)) {
 $communitymanager = new block_community_manager();
 
 /// Check if the page has been called with trust argument
-$add  = optional_param('add', -1, PARAM_INTEGER);
-$confirm  = optional_param('confirmed', false, PARAM_INTEGER);
+$add = optional_param('add', -1, PARAM_INTEGER);
+$confirm = optional_param('confirmed', false, PARAM_INTEGER);
 if ($add != -1 and $confirm and confirm_sesskey()) {
     $course = new stdClass();
-    $course->name  = optional_param('coursefullname', '', PARAM_TEXT);
-    $course->description  = optional_param('coursedescription', '', PARAM_TEXT);
-    $course->url  = optional_param('courseurl', '', PARAM_URL);
-    $course->imageurl  = optional_param('courseimageurl', '', PARAM_URL);
+    $course->name = optional_param('coursefullname', '', PARAM_TEXT);
+    $course->description = optional_param('coursedescription', '', PARAM_TEXT);
+    $course->url = optional_param('courseurl', '', PARAM_URL);
+    $course->imageurl = optional_param('courseimageurl', '', PARAM_URL);
     $communitymanager->block_community_add_course($course, $USER->id);
     $notificationmessage = $OUTPUT->notification(get_string('addedtoblock', 'hub'),
-            'notifysuccess');
+                    'notifysuccess');
 }
 
 /// Download
-$huburl  = optional_param('huburl', false, PARAM_URL);
-$download  = optional_param('download', -1, PARAM_INTEGER);
-$courseid  = optional_param('courseid', '', PARAM_INTEGER);
-$coursefullname  = optional_param('coursefullname', '', PARAM_ALPHANUMEXT);
+$huburl = optional_param('huburl', false, PARAM_URL);
+$download = optional_param('download', -1, PARAM_INTEGER);
+$courseid = optional_param('courseid', '', PARAM_INTEGER);
+$coursefullname = optional_param('coursefullname', '', PARAM_ALPHANUMEXT);
 if ($usercandownload and $download != -1 and !empty($courseid) and confirm_sesskey()) {
     $course = new stdClass();
     $course->fullname = $coursefullname;
     $course->id = $courseid;
     $course->huburl = $huburl;
     $communitymanager->block_community_download_course_backup($course);
-    $filename =  'backup_'.$course->fullname."_".$course->id.".zip";
+    $filename = 'backup_' . $course->fullname . "_" . $course->id . ".zip";
     $notificationmessage = $OUTPUT->notification(get_string('downloadconfirmed', 'hub', $filename),
-            'notifysuccess');
+                    'notifysuccess');
 }
 
 /// Remove community
-$remove  = optional_param('remove', '', PARAM_INTEGER);
-$communityid  = optional_param('communityid', '', PARAM_INTEGER);
+$remove = optional_param('remove', '', PARAM_INTEGER);
+$communityid = optional_param('communityid', '', PARAM_INTEGER);
 if ($remove != -1 and !empty($communityid) and confirm_sesskey()) {
     $communitymanager->block_community_remove_course($communityid, $USER->id);
     $notificationmessage = $OUTPUT->notification(get_string('communityremoved', 'hub'),
-            'notifysuccess');
+                    'notifysuccess');
 }
 
 
@@ -100,10 +101,10 @@ $renderer = $PAGE->get_renderer('block_community');
 //forms
 $hubselectorform = new community_hub_search_form('', array('search' => $search));
 $fromform = $hubselectorform->get_data();
-$courses= null;
+$courses = null;
 //Retrieve courses by web service
 if (!empty($fromform)) {
-    $downloadable  = optional_param('downloadable', false, PARAM_INTEGER);
+    $downloadable = optional_param('downloadable', false, PARAM_INTEGER);
 
     $options = new stdClass();
     if (!empty($fromform->coverage)) {
@@ -125,13 +126,17 @@ if (!empty($fromform)) {
         $options->language = $fromform->language;
     }
 
-
     $function = 'hub_get_courses';
     $params = array($search, $downloadable, !$downloadable, $options);
-    $serverurl = $huburl."/local/hub/webservice/webservices.php";
-    require_once($CFG->dirroot."/webservice/xmlrpc/lib.php");
+    $serverurl = $huburl . "/local/hub/webservice/webservices.php";
+    require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
     $xmlrpcclient = new webservice_xmlrpc_client();
-    $courses = $xmlrpcclient->call($serverurl, 'publichub', $function, $params);
+    try {
+        $courses = $xmlrpcclient->call($serverurl, 'publichub', $function, $params);
+    } catch (Exception $e) {
+        $hubs = array();
+        $errormessage = $OUTPUT->notification(get_string('errorcourselisting', 'block_community', $e->getMessage()));
+    }
 }
 
 // OUTPUT
@@ -141,5 +146,8 @@ if (!empty($notificationmessage)) {
     echo $notificationmessage;
 }
 $hubselectorform->display();
+if (!empty($errormessage)) {
+    echo $errormessage;
+}
 echo $renderer->course_list($courses, $huburl);
 echo $OUTPUT->footer();
\ No newline at end of file
index 1566a68..597630c 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+
 ///////////////////////////////////////////////////////////////////////////
 //                                                                       //
 // This file is part of Moodle - http://moodle.org/                      //
  * @copyright  (C) 1999 onwards Martin Dougiamas  http://dougiamas.com
  *
  * Form for community search
-*/
+ */
 
-require_once($CFG->dirroot.'/lib/formslib.php');
-require_once($CFG->dirroot.'/course/publish/lib.php');
+require_once($CFG->dirroot . '/lib/formslib.php');
+require_once($CFG->dirroot . '/course/publish/lib.php');
 
 class community_hub_search_form extends moodleform {
 
     public function definition() {
-        global $CFG, $USER;
+        global $CFG, $USER, $OUTPUT;
         $strrequired = get_string('required');
-        $mform =& $this->_form;
+        $mform = & $this->_form;
         $search = $this->_customdata['search'];
         $mform->addElement('header', 'site', get_string('search', 'block_community'));
 
         //retrieve the hub list on the hub directory by web service
         $function = 'hubdirectory_get_hubs';
         $params = array();
-        $serverurl = HUB_HUBDIRECTORYURL."/local/hubdirectory/webservice/webservices.php";
-        require_once($CFG->dirroot."/webservice/xmlrpc/lib.php");
+        $serverurl = HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/webservices.php";
+        require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
         $xmlrpcclient = new webservice_xmlrpc_client();
-        $hubs = $xmlrpcclient->call($serverurl, 'publichubdirectory', $function, $params);
-
-        //sort hubs by trusted/prioritize
-
-        //Public hub list
-        $options = array();
-       // $mform->addElement('static','huburlstring',get_string('selecthub', 'block_community'));
-        
-        foreach ($hubs as $hub) {
-            $params = array('hubid' => $hub['id'],
-                'filetype' => HUB_HUBSCREENSHOT_FILE_TYPE);
-            $imgurl = new moodle_url(HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/download.php", $params);
-            $ascreenshothtml = html_writer::empty_tag('img', array('src' => $imgurl, 'alt' => $hub['name']));
-            $brtag = html_writer::empty_tag('br');
-            $hubdescription =   '&nbsp;&nbsp;'.$hub['name'];
-            $hubdescription .= $brtag;
-            $hubdescription .=  html_writer::tag('span', $ascreenshothtml, array('class' => 'hubscreenshot'));
-            $hubdescription .= html_writer::tag('span', $hub['description'], array('class' => 'hubdescription'));
-            $hubdescription .= $brtag;
-            $additionaldesc = get_string('sites', 'block_community'). ': ' . $hub['sites'] . ' - ' .
-                                get_string('courses', 'block_community'). ': ' . $hub['courses'];
-            $hubdescription .= html_writer::tag('span', $additionaldesc, array('class' => 'hubadditionaldesc'));
-            $hubdescription .= $brtag;
-            $hubdescription .= html_writer::tag('span',
-                    $hub['trusted']?get_string('hubtrusted', 'block_community'):get_string('hubnottrusted', 'block_community'),
-                    array('class' => $hub['trusted']?'trusted':'nottrusted'));
-             $hubdescription = html_writer::tag('span',
-                    $hubdescription,
-                    array('class' => $hub['trusted']?'hubtrusted':'hubnottrusted'));
-
-            if (empty($firsthub)) {
-                $mform->addElement('radio','huburl',get_string('selecthub', 'block_community'),
-                        $hubdescription, $hub['url']);
-                $mform->setDefault('huburl', HUB_MOODLEORGHUBURL);
-                $firsthub = true;
-            } else {
-                $mform->addElement('radio','huburl','', $hubdescription, $hub['url']);
-            }
+        try {
+            $hubs = $xmlrpcclient->call($serverurl, 'publichubdirectory', $function, $params);
+        } catch (Exception $e) {
+            $hubs = array();
+            $error = $OUTPUT->notification(get_string('errorhublisting', 'block_community', $e->getMessage()));
+            $mform->addElement('static', 'errorhub', '', $error);
         }
+        if (empty($error)) {
+            //sort hubs by trusted/prioritize
+            //Public hub list
+            $options = array();
+            // $mform->addElement('static','huburlstring',get_string('selecthub', 'block_community'));
+
+            foreach ($hubs as $hub) {
+                $params = array('hubid' => $hub['id'],
+                    'filetype' => HUB_HUBSCREENSHOT_FILE_TYPE);
+                $imgurl = new moodle_url(HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/download.php", $params);
+                $ascreenshothtml = html_writer::empty_tag('img', array('src' => $imgurl, 'alt' => $hub['name']));
+                $brtag = html_writer::empty_tag('br');
+                $hubdescription = '&nbsp;&nbsp;' . $hub['name'];
+                $hubdescription .= $brtag;
+                $hubdescription .= html_writer::tag('span', $ascreenshothtml, array('class' => 'hubscreenshot'));
+                $hubdescription .= html_writer::tag('span', $hub['description'], array('class' => 'hubdescription'));
+                $hubdescription .= $brtag;
+                $additionaldesc = get_string('sites', 'block_community') . ': ' . $hub['sites'] . ' - ' .
+                        get_string('courses', 'block_community') . ': ' . $hub['courses'];
+                $hubdescription .= html_writer::tag('span', $additionaldesc, array('class' => 'hubadditionaldesc'));
+                $hubdescription .= $brtag;
+                $hubdescription .= html_writer::tag('span',
+                                $hub['trusted'] ? get_string('hubtrusted', 'block_community') :
+                                        get_string('hubnottrusted', 'block_community'),
+                                array('class' => $hub['trusted'] ? 'trusted' : 'nottrusted'));
+                $hubdescription = html_writer::tag('span',
+                                $hubdescription,
+                                array('class' => $hub['trusted'] ? 'hubtrusted' : 'hubnottrusted'));
+
+                if (empty($firsthub)) {
+                    $mform->addElement('radio', 'huburl', get_string('selecthub', 'block_community'),
+                            $hubdescription, $hub['url']);
+                    $mform->setDefault('huburl', HUB_MOODLEORGHUBURL);
+                    $firsthub = true;
+                } else {
+                    $mform->addElement('radio', 'huburl', '', $hubdescription, $hub['url']);
+                }
+            }
 
-        //display enrol/download select box if the USER has the download capability
-        if (has_capability('moodle/community:download', get_context_instance(CONTEXT_USER, $USER->id))) {
-            $options = array(0 => get_string('enrollable', 'block_community'),
+            //display enrol/download select box if the USER has the download capability
+            if (has_capability('moodle/community:download', get_context_instance(CONTEXT_USER, $USER->id))) {
+                $options = array(0 => get_string('enrollable', 'block_community'),
                     1 => get_string('downloadable', 'block_community'));
-            $mform->addElement('select', 'downloadable', get_string('enroldownload', 'block_community'),
-                    $options);
-            $mform->addHelpButton('downloadable', 'enroldownload', 'block_community');
-        } else {
-            $mform->addElement('hidden', 'downloadable', 0);
-        }
+                $mform->addElement('select', 'downloadable', get_string('enroldownload', 'block_community'),
+                        $options);
+                $mform->addHelpButton('downloadable', 'enroldownload', 'block_community');
+            } else {
+                $mform->addElement('hidden', 'downloadable', 0);
+            }
 
-        $options = array();
-        $options['all'] = get_string('any');
-        $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
-        $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
-        $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
-        $mform->addElement('select', 'audience', get_string('audience', 'block_community'), $options);
-        $mform->setDefault('audience', 'all');
-        unset($options);
-        $mform->addHelpButton('audience', 'audience', 'block_community');
-
-        $options = array();
-        $options['all'] = get_string('any');
-        $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
-        $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
-        $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
-        $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
-        $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
-        $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
-        $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
-        $mform->addElement('select', 'educationallevel', get_string('educationallevel', 'block_community'), $options);
-        $mform->setDefault('educationallevel', 'all');
-        unset($options);
-        $mform->addHelpButton('educationallevel', 'educationallevel', 'block_community');
-
-        $options = get_string_manager()->load_component_strings('edufields', current_language());
-        foreach ($options as $key => &$option) {
-            $keylength = strlen ( $key );
-            if ( $keylength == 10) {
-                $option = "&nbsp;&nbsp;" . $option;
-            } else  if ( $keylength == 12) {
-                $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
+            $options = array();
+            $options['all'] = get_string('any');
+            $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
+            $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
+            $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
+            $mform->addElement('select', 'audience', get_string('audience', 'block_community'), $options);
+            $mform->setDefault('audience', 'all');
+            unset($options);
+            $mform->addHelpButton('audience', 'audience', 'block_community');
+
+            $options = array();
+            $options['all'] = get_string('any');
+            $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
+            $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
+            $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
+            $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
+            $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
+            $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
+            $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
+            $mform->addElement('select', 'educationallevel',
+                    get_string('educationallevel', 'block_community'), $options);
+            $mform->setDefault('educationallevel', 'all');
+            unset($options);
+            $mform->addHelpButton('educationallevel', 'educationallevel', 'block_community');
+
+            $options = get_string_manager()->load_component_strings('edufields', current_language());
+            foreach ($options as $key => &$option) {
+                $keylength = strlen($key);
+                if ($keylength == 10) {
+                    $option = "&nbsp;&nbsp;" . $option;
+                } else if ($keylength == 12) {
+                    $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
+                }
             }
-        }
-        $options = array_merge (array('all' => get_string('any')),$options);
-        $mform->addElement('select', 'subject', get_string('subject', 'block_community'), $options, array('id'=>'communitysubject'));
-        $mform->setDefault('subject', 'all');
-        unset($options);
-        $mform->addHelpButton('subject', 'subject', 'block_community');
-        $this->init_javascript_enhancement('subject', 'smartselect', array('selectablecategories' => true, 'mode'=>'compact'));
-
-        require_once($CFG->dirroot."/lib/licenselib.php");
-        $licensemanager = new license_manager();
-        $licences = $licensemanager->get_licenses();
-        $options = array();
-        $options['all'] = get_string('any');
-        foreach ($licences as $license) {
-            $options[$license->shortname] = get_string($license->shortname, 'license');
-        }
-        $mform->addElement('select', 'licence', get_string('licence', 'block_community'), $options);
-        $mform->setDefault('licence', 'cc');
-        unset($options);
-        $mform->addHelpButton('licence', 'licence', 'block_community');
-        $mform->setDefault('licence', 'all');
+            $options = array_merge(array('all' => get_string('any')), $options);
+            $mform->addElement('select', 'subject', get_string('subject', 'block_community'),
+                    $options, array('id' => 'communitysubject'));
+            $mform->setDefault('subject', 'all');
+            unset($options);
+            $mform->addHelpButton('subject', 'subject', 'block_community');
+            $this->init_javascript_enhancement('subject', 'smartselect',
+                    array('selectablecategories' => true, 'mode' => 'compact'));
+
+            require_once($CFG->dirroot . "/lib/licenselib.php");
+            $licensemanager = new license_manager();
+            $licences = $licensemanager->get_licenses();
+            $options = array();
+            $options['all'] = get_string('any');
+            foreach ($licences as $license) {
+                $options[$license->shortname] = get_string($license->shortname, 'license');
+            }
+            $mform->addElement('select', 'licence', get_string('licence', 'block_community'), $options);
+            $mform->setDefault('licence', 'cc');
+            unset($options);
+            $mform->addHelpButton('licence', 'licence', 'block_community');
+            $mform->setDefault('licence', 'all');
 
-        $languages = get_string_manager()->get_list_of_languages();
-        asort($languages, SORT_LOCALE_STRING);
-        $languages = array_merge (array('all' => get_string('any')),$languages);
-        $mform->addElement('select', 'language',get_string('language'), $languages);
-        $mform->setDefault('language', 'all');
-        $mform->addHelpButton('language', 'language', 'block_community');
+            $languages = get_string_manager()->get_list_of_languages();
+            asort($languages, SORT_LOCALE_STRING);
+            $languages = array_merge(array('all' => get_string('any')), $languages);
+            $mform->addElement('select', 'language', get_string('language'), $languages);
+            $mform->setDefault('language', 'all');
+            $mform->addHelpButton('language', 'language', 'block_community');
 
+            $mform->addElement('text', 'search', get_string('keywords', 'block_community'));
+            $mform->addHelpButton('search', 'keywords', 'block_community');
 
-        $mform->addElement('text','search' , get_string('keywords', 'block_community'));
-        $mform->addHelpButton('search', 'keywords', 'block_community');
 
-        $mform->addElement('submit', 'submitbutton', get_string('search', 'block_community'));
+            $mform->addElement('submit', 'submitbutton', get_string('search', 'block_community'));
+        }
     }
 
     function validation($data, $files) {
index 249266a..5dbbb98 100644 (file)
@@ -51,6 +51,8 @@ $string['enroldownload_help'] = 'Some courses listed in the selected hub are bei
 
 Others are course templates provided for you to download and use on your own Moodle site.';
 $string['enrollable'] = 'courses I can enrol in';
+$string['errorcourselisting'] = 'An error occured when retrieving the course listing from the selected hub, please try again later. ({$a})';
+$string['errorhublisting'] = 'An error occured when retrieving the hub listing from Moodle.org, please try again later. ({$a})';
 $string['hub'] = 'hub';
 $string['hubnottrusted'] = 'Not trusted';
 $string['hubtrusted'] = 'This hub is trusted by Moodle.org';