private void EnrollUser(string usersetName) { const string logMethodName = ".EnrollUser(string usersetName) - "; _log.Debug(logMethodName + "Begin Method"); string functionName = "local_datahub_elis_userset_enrolment_create"; String postData = String.Format("data[user_username]={0}&data[user_idnumber]={1}&data[user_email]={2}", muModel.username, muModel.idnumber, muModel.email); postData += "&data[userset_name]=" + usersetName; string enrollmentResult = MoodleServiceCall(functionName, postData); //handle result if (enrollmentResult.Contains("exception")) { // Error MoodleExceptionModel moodleError = UtilityMethods.DeserializeResponse <MoodleExceptionModel>(enrollmentResult); _log.Error("Exception in: " + logMethodName + moodleError.errorcode + " - " + moodleError.message + " for member with emailaddress " + memberModel.PrimaryEmailKey); _log.Debug("Debug info in: " + logMethodName + " - " + moodleError.debuginfo); } else { // Successful enrollment ElisUsersetEnrollmentCreateResponseModel enrolledUser = UtilityMethods.DeserializeResponse <ElisUsersetEnrollmentCreateResponseModel>(enrollmentResult); if (enrolledUser.messagecode == "userset_enrolment_created") { //capture result into model if to be required for future features, otherwise successful and do nothing. } } _log.Debug(logMethodName + "End Method"); //return enrollmentResult; }
private bool UpdateUser() { const string logMethodName = ".UpdateUser() - "; _log.Debug(logMethodName + "Begin Method"); bool bUpdate = false; string functionName = "core_user_update_users"; string postData = String.Format("users[0][id]={0}", muModel.id); //id required for moodle update //update postData with more parameters postData = ExtendPostData(postData); string updateResult = MoodleServiceCall(functionName, postData); //null returned means successful update if (updateResult == "null") { bUpdate = true; } else if (updateResult.Contains("exception")) { // Error MoodleExceptionModel moodleError = UtilityMethods.DeserializeResponse <MoodleExceptionModel>(updateResult); _log.Error("Error in: " + logMethodName + " - " + moodleError.errorcode + " - " + moodleError.message + " for member with emailaddress " + memberModel.PrimaryEmailKey); _log.Debug("Debug info in: " + logMethodName + " - " + moodleError.debuginfo); return(bUpdate); } _log.Debug(logMethodName + "End Method"); return(bUpdate); }
public List <CourseModel> GetUserCourses(bool fromMoodle = false) { const string logMethodName = ".GetUserCourses(int saltMemberId) - "; _log.Debug(logMethodName + "Begin Method"); List <CourseModel> courses = BuildCoursesFromConfig(); //if fromMoodle is false, return the courses representation built form config, i.e. without user's actual completion data if (fromMoodle) { //check if user exists in moodle this.muModel = this.GetUser(); if (!string.IsNullOrEmpty(this.muModel.username)) { string classidnumber = ""; int username = int.Parse(this.muModel.username); string functionName = "local_datahub_elis_class_enrolment_update"; foreach (var course in courses) { //populate each course with grade and completestatusid classidnumber = course.idnumber; String postData = String.Format("data[class_idnumber]={0}&data[user_username]={1}", classidnumber, username); string callResult = MoodleServiceCall(functionName, postData); if (callResult.Contains("exception")) { // Error MoodleExceptionModel moodleError = UtilityMethods.DeserializeResponse <MoodleExceptionModel>(callResult); _log.Error("Exception in: " + logMethodName + moodleError.errorcode + " - " + moodleError.message + " for member with MemberID " + this.muModel.username); _log.Debug("Debug info in: " + logMethodName + " - " + moodleError.debuginfo); } else { var courseUpdateResponse = UtilityMethods.DeserializeResponse <ElisClassEnrolmentUpdateResponseModel>(callResult); course.grade = courseUpdateResponse.record.grade; course.completestatusid = courseUpdateResponse.record.completestatusid; } } } } _log.Debug(logMethodName + "End Method"); return(courses); }
private int CreateUser() { const string logMethodName = ".CreateUser() - "; _log.Debug(logMethodName + "Begin Method"); string functionName = "core_user_create_users"; string postData = String.Format("users[0][username]={0}&users[0][password]={1}&users[0][auth]={2}", muModel.username, muModel.password, muModel.auth); //update postData with more parameters postData = ExtendPostData(postData); string creationResult = MoodleServiceCall(functionName, postData); //handle result if (creationResult.Contains("exception")) { // Error MoodleExceptionModel moodleError = UtilityMethods.DeserializeResponse <MoodleExceptionModel>(creationResult); _log.Error("Error in: " + logMethodName + " - " + moodleError.errorcode + " - " + moodleError.message + " for member with emailaddress " + memberModel.PrimaryEmailKey); _log.Debug("Debug info in: " + logMethodName + " - " + moodleError.debuginfo); return(-1); } else { // Successful creation List <MoodleCreateUserResponseModel> createdUsers = UtilityMethods.DeserializeResponse <List <MoodleCreateUserResponseModel> >(creationResult); if (createdUsers.Count > 0) { //update Model with the returned id, required for any subsequent update calls muModel.id = createdUsers[0].id; } } _log.Debug(logMethodName + "End Method"); return(muModel.id); }
private UserModel GetUser() { const string logMethodName = ".GetUser() - "; _log.Debug(logMethodName + "Begin Method"); UserModel moodleUser = new UserModel(); string functionName = "core_user_get_users"; String postData = String.Format("{0}={1}", "criteria[0][key]=username&criteria[0][value]", this.muModel.username); //username in moodle is the SALT MemberID string callResult = MoodleServiceCall(functionName, postData); if (callResult.Contains("exception")) { // Error (e.g. invalidtoken) MoodleExceptionModel moodleError = UtilityMethods.DeserializeResponse <MoodleExceptionModel>(callResult); _log.Error("Error in: " + logMethodName + " - " + moodleError.errorcode + " - " + moodleError.message); _log.Debug("Debug info in: " + logMethodName + " - " + moodleError.debuginfo); throw new Exception(moodleError.message); } else { MoodleGetUsersResponseModel responseModel = UtilityMethods.DeserializeResponse <MoodleGetUsersResponseModel>(callResult); if (HasAccount(responseModel)) { if (responseModel.users.Count > 0) { //update Model with the returned id, as update moodle account requires an id returned by get muModel.id = responseModel.users[0].id; moodleUser = responseModel.users[0]; } } } _log.Debug(logMethodName + "End Method"); return(moodleUser); }