/// <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(); }
public MoodleAccount(Account account) : base() { if (account.Type.Equals(Account.MOODLETYPE)) { // the local part this.CloneFrom(account); // the remote part BackgroundWorker bgWorker = new BackgroundWorker(); bgWorker.DoWork += new DoWorkEventHandler(delegate(object wsender, DoWorkEventArgs args) { 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); // let's get the remote moodle user account UedWs.UedUser user = client.getMyUserPublicProfile(credentials); args.Result = user; } catch (Exception ex) { args.Result = "Unable to get the Moodle account: " + ex.Message; args.Cancel = true; } }); bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object wsender, RunWorkerCompletedEventArgs args) { if (!args.Cancelled && args.Error == null) { UedWs.UedUser user = args.Result as UedWs.UedUser; if (user != null) { this.MoodleId = user.id; if (!user.city.Equals("") && user.city != null) { this.City = user.city; } if (!user.country.Equals("") && user.country != null) { this.Country = user.country; } if (!user.description.Equals("") && user.description != null) { this.Description = user.description; } if (!user.fullName.Equals("") && user.fullName != null) { this.Fullname = user.fullName; } if (!user.imageUrl.Equals("") && user.imageUrl != null) { this.ImageUrl = new Uri(user.imageUrl); } if (!user.profileLink.Equals("") && user.profileLink != null) { this.ProfileLink = new Uri(user.profileLink); } if (!user.webPageUrl.Equals("") && user.webPageUrl != null) { this.WebPageUrl = new Uri(user.webPageUrl); } } } }); bgWorker.RunWorkerAsync(); } else { throw new InvalidCastException("Unable to create a MoodleAccount from something other than a Moodle account"); } }