/// <summary> /// The recent modifications for a course /// </summary> /// <param name="account">With the account data to use on the query</param> private void GetRecentModificationsForAccount(Account account) { LinkedList <Frame> frames = new LinkedList <Frame>(); BackgroundWorker bgWorker = new BackgroundWorker(); this.workers.AddLast(bgWorker); bgWorker.WorkerReportsProgress = true; bgWorker.WorkerSupportsCancellation = true; bgWorker.DoWork += new DoWorkEventHandler(delegate(object wsender, DoWorkEventArgs args) { // Load the remote data try { UedWs.UEDWSPortTypeClient client = new UedWs.UEDWSPortTypeClient(); client.Endpoint.Address = new EndpointAddress(account.getOption(Properties.Settings.Default.MoodleServiceUrlSettingName)); UedWs.UedCredentials credentials = new UedWs.UedCredentials(); credentials.username = account.getOption(Properties.Settings.Default.MoodleServiceUsernameSettingName); credentials.autorizationKey = account.getOption(Properties.Settings.Default.MoodleServiceAutorizationKeySettingName); UedWs.UedDate beginDate = MoodleTimeline.DateTimeToUedDate(account.LastUpdate); if (client.validateCredentials(credentials)) { UedWs.UedCourse[] courses = client.getMyCourses(credentials); int courseCount = 0; bgWorker.ReportProgress(courseCount++); foreach (UedWs.UedCourse course in courses) { UedWs.UedRecentModification[] recentModifications = client.getCourseRecentModifications(credentials, course.id, beginDate); foreach (UedWs.UedRecentModification modification in recentModifications) { frames.AddLast(new Frame(account, "[" + account.Name + "] @" + course.fullName, modification.text + " " + modification.modulename, MoodleTimeline.UedDateToDateTime(modification.date), (modification.url.Equals("") ? null : new Uri(modification.url)), course.id)); } bgWorker.ReportProgress(((courseCount++) * 100) / courses.Length); } } } catch (Exception) { //MessageBox.Show(ex.Message+ ex.StackTrace); } }); bgWorker.ProgressChanged += new ProgressChangedEventHandler(delegate(object s, ProgressChangedEventArgs args) { TimelineManager.ReportProgress(new Status(account.ID, "Checking for course events. " + args.ProgressPercentage + "% completed.")); }); bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object wsender, RunWorkerCompletedEventArgs args) { this.CacheModificationsForAccount(account, frames); foreach (Frame frame in frames) { this.AddFrame(frame); } this.workers.Remove(bgWorker); TimelineManager.RegisterEvent(account); ConfigurationManager.UpdateUserAccountLastUpdate(account.ID); TimelineManager.ReportProgress(new Status(account.ID, frames.Count + " account events loaded.")); }); bgWorker.RunWorkerAsync(account); }
/// <summary> /// Event for saving a moodle account /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveMoodleAccount(object sender, System.Windows.RoutedEventArgs e) { lblValidating.Visibility = System.Windows.Visibility.Visible; MoodleAccountForm.IsEnabled = false; BackgroundWorker bgWorker = new BackgroundWorker(); String serverUrl = txtMoodleUrl.Text + Properties.Settings.Default.MoodleModuleUrl; String username = txtMoodleUsername.Text; String password = txtMoodlePassword.Password; String accountName = txtMoodleAccountName.Text; Boolean enabled = cbMoodleAccountEnabled.IsChecked.Value; bgWorker.DoWork += (wsender, we) => { DoWorkEventArgs args = we as DoWorkEventArgs; BackgroundWorker worker = wsender as BackgroundWorker; try { Account account = null; UedWs.UEDWSPortTypeClient client = new UedWs.UEDWSPortTypeClient(); client.Endpoint.Address = new EndpointAddress(serverUrl); UedWs.UedCredentials credentials = new UedWs.UedCredentials(); credentials.username = username; credentials.autorizationKey = ""; // if it's an existing account and the password is empty, use the previous authorization key if(this._accountId > -1 && password.Equals("") && ConfigurationManager.UserAccountExists(this._accountId)) { account = ConfigurationManager.GetUserAccount(this._accountId); if(account != null) { credentials.autorizationKey = account.getOption(Properties.Settings.Default.MoodleServiceAutorizationKeySettingName); } } else { // else, retrieve a new authorization key credentials.autorizationKey = client.getAuthorizationKey(credentials.username, password, "iBoard"); } // if the credentials are valid if(!credentials.autorizationKey.Equals("") && client.validateCredentials(credentials)) { if(account != null) { account.Name = accountName; account.Type = Account.MOODLETYPE; account.Enabled = enabled; account.setOption(Properties.Settings.Default.MoodleServiceUrlSettingName, serverUrl); account.setOption(Properties.Settings.Default.MoodleServiceUsernameSettingName, credentials.username); account.setOption(Properties.Settings.Default.MoodleServiceAutorizationKeySettingName, credentials.autorizationKey); ConfigurationManager.UpdateUserAccount(account); } else { account = new Account(accountName, Account.MOODLETYPE, enabled); account.addOption(Properties.Settings.Default.MoodleServiceUrlSettingName, serverUrl); account.addOption(Properties.Settings.Default.MoodleServiceUsernameSettingName, credentials.username); account.addOption(Properties.Settings.Default.MoodleServiceAutorizationKeySettingName, credentials.autorizationKey); ConfigurationManager.AddUserAccount(account); } UedWs.UedUser user = client.getMyUserPublicProfile(credentials); args.Result = user; } else { throw new Exception(); } } catch(Exception ex) { args.Result = ex.Message; args.Cancel = true; } }; bgWorker.RunWorkerCompleted += (wsender, we) => { RunWorkerCompletedEventArgs args = we as RunWorkerCompletedEventArgs; if(args.Cancelled || args.Error != null) { ExtendedVisualStateManager.GoToElementState(AddMoodleAccountGrid as FrameworkElement, MoodleAccountValidationFailedState.Name, true); }else{ lblMoodleAccountSaved.Content = "The moodle account " + accountName + " was saved:"; UedWs.UedUser user = args.Result as UedWs.UedUser; if(user != null) { if(user.imageUrl != null && !user.imageUrl.Equals("")) { imgNewMoodleAccountAvatar.Source = new BitmapImage(new Uri(user.imageUrl)); } if(user.fullName != null) { lblNewMoodleAccountName.Content = user.fullName; } } ConfigurationManager.ConfigurationUpdated(); ExtendedVisualStateManager.GoToElementState(AddMoodleAccountGrid as FrameworkElement, MoodleAccountValidationOkState.Name, true); } lblValidating.Visibility = System.Windows.Visibility.Hidden; MoodleAccountForm.IsEnabled = true; }; bgWorker.RunWorkerAsync(); }