00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "GetCoursesViewController.h"
00010 #import "Moodle.h"
00011 #import "WIRemoting.h"
00012 #import "ShowCourseViewController.h"
00013
00014 @implementation GetCoursesViewController
00015
00016 @synthesize mineOnly;
00017
00018 - (id) initWithTitle:(NSString *)title andDescription:(NSString *)aDescription
00019 {
00020 self = [super initWithTitle:title andDescription:aDescription];
00021
00022 if (nil != self) {
00023 mineOnly = NO;
00024 }
00025
00026 return self;
00027 }
00028
00029 - (void) execute
00030 {
00031 if (mineOnly) {
00032 [moodle getMyCourses:self];
00033 }
00034 else {
00035 [moodle getCourses:self];
00036 }
00037 }
00038
00039 - (void) finished:(RMResponse*) response
00040 {
00041 NSDictionary *r = [[response responseString] JSONValue];
00042
00043 if ([[r valueForKey:@"error"] boolValue]) {
00044
00045 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
00046 message:[[r valueForKey:@"message"] valueForKey:@"string"]
00047 delegate:self
00048 cancelButtonTitle:@"OK"
00049 otherButtonTitles:nil];
00050 [alert show];
00051 [alert release];
00052 }
00053
00054
00055 [views removeAllObjects];
00056
00057 NSArray *courses = [r valueForKey:@"courses"];
00058 for (NSDictionary *c in courses) {
00059
00060 if ([[c valueForKey:@"id"] intValue] <= 1) {
00061 continue;
00062 }
00063
00064 if (![[c valueForKey:@"error"] isEqual:@""]) {
00065
00066 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
00067 message:[c valueForKey:@"error"]
00068 delegate:self
00069 cancelButtonTitle:@"OK"
00070 otherButtonTitles:nil];
00071 [alert show];
00072 [alert release];
00073 [self.navigationController popViewControllerAnimated:YES];
00074 break;
00075 }
00076
00077
00078 ShowCourseViewController *controller =
00079 [[ShowCourseViewController alloc] initWithTitle:[c valueForKey:@"shortname"]
00080 andDescription:[c valueForKey:@"fullname"]];
00081 controller.courseId = [[c valueForKey:@"id"] intValue];
00082 [views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
00083 [c valueForKey:@"shortname"], @"title",
00084 [c valueForKey:@"fullname"], @"description",
00085 controller, @"controller",
00086 nil]];
00087 [controller release];
00088 }
00089
00090
00091 [self.view reloadData];
00092 [self.view setNeedsDisplay];
00093
00094 }
00095
00096
00097
00098 @end
00099