8
8
using System . Linq ;
9
9
using System . Text . RegularExpressions ;
10
10
using System . Web ;
11
+ using System . Web . Routing ;
11
12
using CTCClassSchedule . Properties ;
13
+ using Common . Logging ;
12
14
using Ctc . Ods ;
13
15
using Ctc . Ods . Data ;
14
16
using Ctc . Ods . Types ;
18
20
using System . Configuration ;
19
21
using System . Text ;
20
22
using Ctc . Ods . Config ;
23
+ using Encoder = Microsoft . Security . Application . Encoder ;
21
24
22
25
namespace CTCClassSchedule . Common
23
26
{
24
27
public static class Helpers
25
28
{
26
- /// <summary>
29
+ private static readonly ILog _log = LogManager . GetCurrentClassLogger ( ) ;
30
+
31
+ /// <summary>
27
32
/// Useful if a helper method works with a timespan and should default to
28
33
/// either 12:00am or 23:59pm (start time/end time).
29
34
/// </summary>
@@ -266,7 +271,7 @@ public static CourseID getCourseIdFromString(string courseId)
266
271
ApiSettings _apiSettings = ConfigurationManager . GetSection ( ApiSettings . SectionName ) as ApiSettings ;
267
272
string commonCourseChar = _apiSettings . RegexPatterns . CommonCourseChar ;
268
273
269
- return new CourseID ( subject . Replace ( commonCourseChar , string . Empty ) , courseNumber , subject . Contains ( commonCourseChar ) ) ;
274
+ return new CourseID ( subject . Replace ( commonCourseChar , String . Empty ) , courseNumber , subject . Contains ( commonCourseChar ) ) ;
270
275
}
271
276
272
277
/// <summary>
@@ -430,7 +435,7 @@ static public IDictionary<string, object> getLinkParams(HttpRequestBase httpRequ
430
435
{
431
436
string value = httpRequest . QueryString . AllKeys . Contains ( key ) ? httpRequest . QueryString [ key ] : httpRequest . Form [ key ] ;
432
437
433
- if ( ! string . IsNullOrWhiteSpace ( value ) )
438
+ if ( ! String . IsNullOrWhiteSpace ( value ) )
434
439
{
435
440
if ( linkParams . ContainsKey ( key ) )
436
441
{
@@ -663,7 +668,7 @@ public static IList<SectionsBlock> GroupSectionsIntoBlocks(IList<SectionWithSeat
663
668
IList < SectionWithSeats > remainingSections = nonLinkedSections . Skip ( processedCount ) . ToList ( ) ;
664
669
SectionWithSeats firstSection = remainingSections . First ( ) ;
665
670
// TODO: Replace BuildCourseID() with this logic - and pull in CommonCourceChar from .config
666
- string blockCourseID = string . Format ( "{0}{1} {2}" , firstSection . CourseSubject , firstSection . IsCommonCourse ? "&" : string . Empty , firstSection . CourseNumber ) ;
671
+ string blockCourseID = String . Format ( "{0}{1} {2}" , firstSection . CourseSubject , firstSection . IsCommonCourse ? "&" : String . Empty , firstSection . CourseNumber ) ;
667
672
668
673
if ( allLinkedSections . Any ( l => l . LinkedTo == firstSection . ID . ItemNumber ) )
669
674
{
@@ -933,6 +938,11 @@ static private string getYRQValueForBookstoreURL(YearQuarter yrq)
933
938
return String . Concat ( quarter , year ) ;
934
939
}
935
940
941
+ /// <summary>
942
+ ///
943
+ /// </summary>
944
+ /// <param name="linkedSections"></param>
945
+ /// <returns></returns>
936
946
public static IList < SectionWithSeats > ParseCommonHeadingLinkedSections ( List < SectionWithSeats > linkedSections )
937
947
{
938
948
string prevCourseID = String . Empty ;
@@ -957,5 +967,103 @@ public static IList<SectionWithSeats> ParseCommonHeadingLinkedSections(List<Sect
957
967
958
968
return common ;
959
969
}
970
+
971
+ // TODO: Move StripHtml() into CtcApi
972
+ /// <summary>
973
+ ///
974
+ /// </summary>
975
+ /// <param name="withHtml"></param>
976
+ /// <param name="whitelistPattern"></param>
977
+ /// <returns></returns>
978
+ public static string StripHtml ( string withHtml , string whitelistPattern = null )
979
+ {
980
+ string stripped ;
981
+ if ( String . IsNullOrWhiteSpace ( whitelistPattern ) )
982
+ {
983
+ whitelistPattern = ConfigurationManager . AppSettings [ "CMSHtmlParsingAllowedElements" ] ;
984
+ }
985
+ try
986
+ {
987
+ string pattern = @"</?(?(?=" + whitelistPattern +
988
+ @")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:(["",']?).*?\1?)?)*\s*/?>" ;
989
+ stripped = Regex . Replace ( withHtml , pattern , String . Empty ) ;
990
+ }
991
+ catch ( Exception ex )
992
+ {
993
+ stripped = Encoder . HtmlEncode ( withHtml ) ;
994
+ _log . Warn ( m => m ( "Unable to remove HTML from string '{0}'\n Returning HTML-encoded string instead.\n {1}" , withHtml , ex ) ) ;
995
+ }
996
+ return stripped ;
997
+ }
998
+
999
+ /// <summary>
1000
+ /// Gets the course outcome information by scraping the Bellevue College
1001
+ /// course outcomes website
1002
+ /// </summary>
1003
+ public static dynamic GetCourseOutcome ( ICourseID courseId )
1004
+ {
1005
+ string fullCourseID = BuildCourseID ( courseId . Number , courseId . Subject . TrimEnd ( ) , courseId . IsCommonCourse ) ;
1006
+ string courseOutcomes ;
1007
+ try
1008
+ {
1009
+ Service1Client client = new Service1Client ( ) ;
1010
+ string rawCourseOutcomes = client . GetCourseOutcome ( fullCourseID ) ;
1011
+ if ( rawCourseOutcomes . IndexOf ( "<li>" , StringComparison . OrdinalIgnoreCase ) >= 0 )
1012
+ {
1013
+ courseOutcomes = StripHtml ( rawCourseOutcomes , "ul|UL|li|LI" ) ;
1014
+ }
1015
+ else
1016
+ {
1017
+ courseOutcomes = StripHtml ( rawCourseOutcomes , "" ) ;
1018
+ string [ ] outcomeArray = courseOutcomes . Split ( '\n ' ) ;
1019
+
1020
+ StringBuilder outcomes = new StringBuilder ( "<ul>" ) ;
1021
+ foreach ( string outcome in outcomeArray )
1022
+ {
1023
+ outcomes . AppendFormat ( "<li>{0}</li>" , outcome ) ;
1024
+ }
1025
+ outcomes . Append ( "</ul>" ) ;
1026
+
1027
+ courseOutcomes = outcomes . ToString ( ) ;
1028
+ }
1029
+ }
1030
+ catch ( Exception ex )
1031
+ {
1032
+ // TODO: log exception details
1033
+ courseOutcomes = "Error: Cannot find course outcome for this course or cannot connect to the course outcomes webservice." ;
1034
+ _log . Warn ( m => m ( "Unable to retrieve course outomes for '{0}'" , fullCourseID ) , ex ) ;
1035
+ }
1036
+
1037
+ return courseOutcomes ;
1038
+ }
1039
+
1040
+ /// <summary>
1041
+ /// Allows user to enter "current" in URL in place of quarter
1042
+ /// </summary>
1043
+ /// <param name="quarter"></param>
1044
+ /// <param name="currentQuarter"></param>
1045
+ /// <param name="routeData"></param>
1046
+ /// <returns></returns>
1047
+ public static YearQuarter DetermineRegistrationQuarter ( string quarter , YearQuarter currentQuarter , RouteData routeData )
1048
+ {
1049
+ YearQuarter yrq ;
1050
+ if ( String . IsNullOrWhiteSpace ( quarter ) )
1051
+ {
1052
+ yrq = null ;
1053
+ }
1054
+ else
1055
+ {
1056
+ if ( quarter . ToUpper ( ) == "CURRENT" )
1057
+ {
1058
+ yrq = currentQuarter ;
1059
+ routeData . Values [ "YearQuarter" ] = yrq . FriendlyName . Replace ( " " , string . Empty ) ;
1060
+ }
1061
+ else
1062
+ {
1063
+ yrq = YearQuarter . FromFriendlyName ( quarter ) ;
1064
+ }
1065
+ }
1066
+ return yrq ;
1067
+ }
960
1068
}
961
1069
}
0 commit comments