From 9125b5c4ab5f71826c2066eed047f19870f6dbef Mon Sep 17 00:00:00 2001
From: John Hoopes <john.hoopes@wisc.edu>
Date: Fri, 15 Aug 2014 18:27:23 +0000
Subject: [PATCH] [UWMOODLE-666] - SOAP error handling in UW Moodle My courses
 block

---
 block_course_overview_uwmoodle.php         | 22 ++++++--
 getcourses.php                             | 11 +++-
 js/course_overview_uwmoodle.js             | 35 +++++++++++++
 lang/en/block_course_overview_uwmoodle.php |  6 +++
 locallib.php                               |  2 +-
 renderer.php                               | 58 +++++++++++++++++++++-
 styles.css                                 | 45 ++++++++++++++++-
 7 files changed, 169 insertions(+), 10 deletions(-)

diff --git a/block_course_overview_uwmoodle.php b/block_course_overview_uwmoodle.php
index 8487e37..4d4b786 100644
--- a/block_course_overview_uwmoodle.php
+++ b/block_course_overview_uwmoodle.php
@@ -99,11 +99,16 @@ class block_course_overview_uwmoodle extends block_base {
             $this->content->text .= $renderer->render_news($config->news);
         }
 
-        // Finally render the legend
-        $this->content->text .= $renderer->render_create_course_and_legend();
-
         $usephp = optional_param('usephp', '', PARAM_ALPHA);
-        IF(empty($usephp)){ // set up course blocks, but then let ajax load courses
+        if(empty($usephp)){ // set up course blocks, but then let ajax load courses
+            // the next 2 renders happen for both, but to get the order to display correctly they have to be put in twice
+
+            // render the error container so that js/php can add errors
+            $this->content->text .= $renderer->render_error_container($usephp);
+
+            // render the legend
+            $this->content->text .= $renderer->render_create_course_and_legend();
+
             $this->content->text .= $renderer->use_ajax(true, $this->config); // adds in a javascript variable for using ajax
             $this->content->text .= $renderer->ajax_course_block();
 
@@ -112,7 +117,7 @@ class block_course_overview_uwmoodle extends block_base {
             $this->content->text .= $renderer->use_ajax(false); // adds in javascript variable to not use ajax
 
             // Get user's courses and sort by term
-            $courses = block_course_overview_uwmoodle_get_sorted_courses();
+            list($courses, $errors) = block_course_overview_uwmoodle_get_sorted_courses();
             $terms = block_course_overview_uwmoodle_group_courses_by_term($courses);
 
             $currentterm = block_course_overview_uwmoodle_get_current_term();
@@ -142,6 +147,13 @@ class block_course_overview_uwmoodle extends block_base {
                 }
             }
 
+            /// the next 2 renders happen for both, but to get the order to display correctly they have to be put in twice
+            // render the error container so that js/php can add errors
+            $this->content->text .= $renderer->render_error_container($usephp, $errors);
+
+            // render the legend
+            $this->content->text .= $renderer->render_create_course_and_legend();
+
             // Render the block
             $this->content->text .= $renderer->course_block($displayedterms, $overviews, $selectedterm);
 
diff --git a/getcourses.php b/getcourses.php
index af6a97c..fa02922 100644
--- a/getcourses.php
+++ b/getcourses.php
@@ -52,7 +52,16 @@ if(confirm_sesskey()) {
     profile_load_custom_fields($USER);
 
     // Get user's courses and sort by term
-    $courses = block_course_overview_uwmoodle_get_sorted_courses();
+    list($courses, $errors) = block_course_overview_uwmoodle_get_sorted_courses();
+
+    if(!empty($errors)){
+        if(debugging()){
+            $response->errors = $errors;
+        }else{
+            $response->errors = get_string('general_warning_message', 'block_course_overview_uwmoodle');
+        }
+    }
+
     $terms = block_course_overview_uwmoodle_group_courses_by_term($courses);
 
     $currentterm = block_course_overview_uwmoodle_get_current_term();
diff --git a/js/course_overview_uwmoodle.js b/js/course_overview_uwmoodle.js
index 01dc21f..84955f3 100644
--- a/js/course_overview_uwmoodle.js
+++ b/js/course_overview_uwmoodle.js
@@ -1,4 +1,20 @@
 
+function couwmm_create_warning_list(warnings){
+
+    if(warnings.length == 0){
+        return ''; // return nothing for no errors array
+    }
+
+    var returnHTML = '<ul>';
+    $.each(warnings, function(index, value){
+        returnHTML += '<li>' + value + '</li>';
+    });
+
+    returnHTML += '</ul>';
+    return returnHTML;
+}
+
+
 function create_course_box(course){
     'use strict';
 
@@ -87,6 +103,25 @@ $(document).ready(function(){
                     $('#ongoing_courses').html(ongoingHTML);
                 }
             }
+
+            if(response.errors !== undefined){
+
+                var warningMsg = '';
+                if( Object.prototype.toString.call( response.errors ) === '[object Array]' ) {
+                    // we have a list of errors
+                    warningMsg = couwmm_create_warning_list(response.errors);
+
+                }else{
+                    // we have a general warning string
+                    warningMsg = response.errors;
+                }
+
+                if(warningMsg.length > 0){ // if there is a warning message show it
+                    document.getElementById('warning_container').innerHTML = warningMsg;
+                    $('#warning_area').show();
+                }
+            }
+
         }, 'json');
 
     }else{
diff --git a/lang/en/block_course_overview_uwmoodle.php b/lang/en/block_course_overview_uwmoodle.php
index c42835a..9aa4099 100644
--- a/lang/en/block_course_overview_uwmoodle.php
+++ b/lang/en/block_course_overview_uwmoodle.php
@@ -63,3 +63,9 @@ $string['nojavascript'] = 'Javascript is not enabled on this page.  Click this l
 $string['news'] = 'News';
 $string['news_desc'] = 'Configurable text that will show above the courses section but below the welcome area';
 $string['create_course'] = 'Click here to create a new course';
+
+$string['warning_header'] = 'Warning: Partial list of courses';
+$string['general_warning_message'] = "We're sorry, something went wrong when we tried to load all of your classes.  " .
+                                        "If you continue to experience issues, contact the DoIT Help Desk at 264-HELP (4357).";
+
+$string['news_header'] = 'News';
diff --git a/locallib.php b/locallib.php
index 921042d..1eb5ea2 100644
--- a/locallib.php
+++ b/locallib.php
@@ -143,7 +143,7 @@ function block_course_overview_uwmoodle_get_sorted_courses() {
 
     $courses = array_merge($courses, $ecourses);
 
-    return $courses;
+    return array($courses, $externalcourses->get_errors());
 }
 
 /**
diff --git a/renderer.php b/renderer.php
index 6becb6a..b695ede 100644
--- a/renderer.php
+++ b/renderer.php
@@ -300,10 +300,66 @@ class block_course_overview_uwmoodle_renderer extends plugin_renderer_base {
      */
     public function render_news($news){
 
-        $output = $this->output->box_start('news_area');
+        $output = $this->output->box_start('notification_area news_bg', 'news_area' );
+        $output .= html_writer::tag('h3', get_string('news_header', 'block_course_overview_uwmoodle'), array('class'=>'notification_header news_h'));
+
+        $output .= html_writer::start_tag('div', array('class' => 'notifications_container'));
         $output .= $news;
+        $output .= html_writer::end_tag('div');
         $output .= $this->output->box_end();
         return $output;
+
+    }
+
+    /**
+     * There are 2 instances in which this function is used
+     *  -  When the block is functioning in AJAX mode and using the callback, all this function will do
+     *      will return the html of the error container to let JS put the error message into the container
+     *  -  When the block is functioning in PHP mode this function will recieve errors if there are any.  And if there are
+     *      call error_log() on them to add them to the error log as well add them to the error container
+     *
+     * @param string $usephp Whether or not to show the section, as in php mode there is no js to show the block
+     * @param array $errors An array of errors given by external courses
+     * @return string HTML string of the error container with possible errors
+     */
+    public function render_error_container($usephp, $errors = array()){
+
+        if(empty($usephp)){
+            $showcontainer = 'hidecontainer';
+        }else{
+
+            if(empty($errors)){
+                $showcontainer = 'hidecontainer';
+            }else{
+                $showcontainer = '';
+            }
+        }
+
+        $output = $this->output->box_start('notification_area warning_bg ' . $showcontainer, 'warning_area' );
+            $output .= html_writer::tag('h3', get_string('warning_header', 'block_course_overview_uwmoodle'), array('class'=>'notification_header warning_h'));
+
+            $output .= html_writer::start_tag('div', array('class' => 'notifications_container', 'id'=>'warning_container'));
+
+            if(!empty($errors)){
+
+                foreach($errors as $error){
+                    error_log($error);
+                }
+
+                if(debugging()){ // if debugging show actual error messages
+
+                    $output .= html_writer::alist($errors);
+
+                }else{ // show general message
+                    $output .= get_string('general_warning_message', 'block_course_overview_uwmoodle');
+                }
+
+            }
+            $output .= html_writer::end_tag('div');
+        $output .= $this->output->box_end();
+        return $output;
+
+
     }
 
     /**
diff --git a/styles.css b/styles.css
index ffada11..8f27476 100644
--- a/styles.css
+++ b/styles.css
@@ -7,7 +7,8 @@
 
 .block_course_overview_uwmoodle .courseblock {
     margin-bottom: 10px;
-    border: 1px solid #666666;
+    border: 1px solid;
+    border-radius: 4px;
     width: 100%;
     clear: both;
     position: relative;
@@ -20,6 +21,8 @@
     /*background-image: url("/theme/image.php?theme=big_red&component=theme&image=gradient_h");*/
     /*background-repeat: repeat-x;*/
     border-bottom: 1px solid #666666;
+    border-top-left-radius: 4px;
+    border-top-right-radius: 4px;
     clear: both;
 }
 
@@ -330,9 +333,47 @@
     margin-bottom: 10px;
 }
 
-.block_course_overview_uwmoodle .news_area{
+
+/* Notification Areas (News and Warning) */
+
+.block_course_overview_uwmoodle .hidecontainer{
+    display: none;
+}
+
+.block_course_overview_uwmoodle .notification_area{
     border: 1px solid;
+    border-radius: 10px;
     padding: 5px;
     margin-bottom: 10px;
+}
+
+.block_course_overview_uwmoodle .news_bg{
     background-color: white;
 }
+
+.block_course_overview_uwmoodle .warning_bg{
+    background-color: #faebcc;
+}
+
+.block_course_overview_uwmoodle .notifications_container{
+    margin: 15px;
+}
+
+.block_course_overview_uwmoodle .notification_header::before{
+    width: 20px;
+    height: 20px;
+    content: "";
+    background-repeat: no-repeat;
+    background-size: 20px 20px;
+    margin-right: 5px;
+    float: left;
+}
+
+.block_course_overview_uwmoodle .warning_h::before {
+    background-image: url('[[pix:i/warning]]');
+}
+
+.block_course_overview_uwmoodle  .news_h::before {
+    background-image: url('[[pix:block_course_overview_uwmoodle|news]]');
+}
+
-- 
GitLab