private static int CreateUserInMoodleCheckIfExist(ClsMoodleAPI.MoodleCreateUser CreateUser) { int iResult = InitializeModule.FAIL_RET; //Start Check if user exists or not string get_users = core_user_get_users("username", CreateUser.username.Trim()); // Deserialize JavaScriptSerializer serializer = new JavaScriptSerializer(); if (get_users.Contains("exception")) { // Error ClsMoodleAPI.MoodleException moodleError = serializer.Deserialize <ClsMoodleAPI.MoodleException>(get_users); // lbl_results.Text += " Error in get user:"******"/" + get_users; } else { // Good ClsMoodleAPI.ListOfUsers myDeserializedClass = JsonConvert.DeserializeObject <ClsMoodleAPI.ListOfUsers>(get_users); if (myDeserializedClass.users.Count > 0) { //string sReturnedUserName = myDeserializedClass.users[0].username; ClsMoodleAPI.MoodleUpdateUser UpdateUser = new ClsMoodleAPI.MoodleUpdateUser(); UpdateUser.id = myDeserializedClass.users[0].id; iResult = UpdateUser.id; UpdateUser.suspended = 0; UpdateUser.username = CreateUser.username; UpdateUser.email = CreateUser.email; UpdateUser.password = CreateUser.password; //UpdateUser.=CreateUser.createpassword ; UpdateUser.firstname = CreateUser.firstname; UpdateUser.lastname = CreateUser.lastname; UpdateUser.auth = CreateUser.auth; // oidc or manual UpdateUser.suspended = CreateUser.suspended; //UpdateUser.idnumber =CreateUser.idnumber ; //UpdateUser.lang=CreateUser.lang; //UpdateUser.calendartype =CreateUser.calendartype ; //UpdateUser.theme =CreateUser.theme; //UpdateUser.timezone=CreateUser.timezone; UpdateUser.mailformat = CreateUser.mailformat; UpdateUser.description = myDeserializedClass.users[0].description; //CreateUser.description; UpdateUser.city = CreateUser.city; UpdateUser.country = CreateUser.country; UpdateUser.firstnamephonetic = CreateUser.firstnamephonetic; UpdateUser.lastnamephonetic = CreateUser.lastnamephonetic; UpdateUser.middlename = CreateUser.middlename; UpdateUser.alternatename = CreateUser.alternatename; //if (CreateUser.username.Trim() == sReturnedUserName) //{ // lbl_results.Text = "User Exists Already"; // //should update the User //} //else //{ //else start //If user not exists then create a new user with below parameters; string created_user = core_user_update_users(UpdateUser);//txt_Username.Text.Trim(), txt_Password.Text.Trim(), txt_FirstName.Text.Trim(), txt_LastName.Text.Trim(), txt_Email.Text.Trim(), txt_Auth.Text.Trim(), txt_Mailformat.Text.Trim(), txt_Description.Text.Trim(), txt_City.Text.Trim(), txt_Country.Text.Trim(), txt_firstnamephonetic.Text.Trim(), txt_lastnamephonetic.Text.Trim(), txt_middlename.Text.Trim(), txt_alternatename.Text.Trim()); //core_user_update_users // Deserialize JavaScriptSerializer serializercreated_user = new JavaScriptSerializer(); if (created_user.Contains("exception")) { // Error ClsMoodleAPI.MoodleException moodleError = serializercreated_user.Deserialize <ClsMoodleAPI.MoodleException>(created_user); //lbl_results.Text += "Error in updating user:"******" / " + created_user; } else { List <ClsMoodleAPI.MoodleCreatedUser> newImport = serializercreated_user.Deserialize <List <ClsMoodleAPI.MoodleCreatedUser> >(created_user); // lbl_results.Text += "User profile Updated Successfully" ; } //} //else end } else { //If user not exists then create a new user with below parameters; string created_user = core_user_create_users(CreateUser);//txt_Username.Text.Trim(), txt_Password.Text.Trim(), txt_FirstName.Text.Trim(), txt_LastName.Text.Trim(), txt_Email.Text.Trim(), txt_Auth.Text.Trim(), txt_Mailformat.Text.Trim(), txt_Description.Text.Trim(), txt_City.Text.Trim(), txt_Country.Text.Trim(), txt_firstnamephonetic.Text.Trim(), txt_lastnamephonetic.Text.Trim(), txt_middlename.Text.Trim(), txt_alternatename.Text.Trim()); // Deserialize JavaScriptSerializer serializercreated_user = new JavaScriptSerializer(); if (created_user.Contains("exception")) { // Error ClsMoodleAPI.MoodleException moodleError = serializercreated_user.Deserialize <ClsMoodleAPI.MoodleException>(created_user); //lbl_results.Text += "Error in updating:" + CreateUser.username + " / " + created_user; } else { List <ClsMoodleAPI.MoodleCreatedUser> myserializercreated_user = serializercreated_user.Deserialize <List <ClsMoodleAPI.MoodleCreatedUser> >(created_user); foreach (var value in myserializercreated_user) { iResult = value.id; } // lbl_results.Text += "User:"******" / " + CreateUser.username + " Created Successfully"; } } } // iResult = InitializeModule.SUCCESS_RET; return(iResult); }
public static int EnrollStudentinMoodleCourses(string sEmail, string sStudentID) { int iResult = InitializeModule.FAIL_RET; string sSQL = ""; DataTable dtUser = new DataTable("User"); DataSet dsUser = new DataSet(); sSQL = "SELECT username, password, firstname, lastname"; sSQL += ", course1, role1, group1, email, auth"; sSQL += ", country, city, department,MoodleUserNo,MoodleCourseNo"; sSQL += " FROM Moodle_Import_Students_M_F_Step4"; sSQL += " WHERE email='" + sEmail + "'"; //sSQL += " ORDER By course1,username ASC"; //sSQL = "SELECT S.username, S.password, S.firstname"; //sSQL += ", S.lastname"; //sSQL += ", S.course1"; //sSQL += ", S.role1"; //sSQL += ", S.group1, S.email"; //sSQL += ", S.auth, S.country"; //sSQL += ", S.city, S.department"; //sSQL += ", S.MoodleUserNo, S.MoodleCourseNo"; //sSQL += " FROM Moodle_Import_Students_M_F_Step4 AS S INNER JOIN"; //sSQL += " AddTransactionsFromDate_MF ON S.sStudentNumber = AddTransactionsFromDate_MF.lngStudentNumber"; //sSQL += " ORDER BY S.course1, S.username"; Connection_StringCLS MyConnection_string = new Connection_StringCLS(InitializeModule.EnumCampus.Males); SqlConnection Conn = new SqlConnection(MyConnection_string.Conn_string.ToString()); Conn.Open(); SqlDataAdapter adapter; adapter = new SqlDataAdapter(sSQL, Conn); adapter.SelectCommand.CommandTimeout = 2000; dsUser.Tables.Add(dtUser); adapter.Fill(dtUser); adapter.Dispose(); ClsMoodleAPI.MoodleManualEnrol enroluser = new ClsMoodleAPI.MoodleManualEnrol(); int iSuspend = 0; int iUserID = 0; string sUsername = ""; string sCourse = ""; int iCourseID = 0; string sGroupName = ""; int iGroupID = 0; iSuspend = 0; DataSet dsGroups = new DataSet(); string Token = "1b4cb9114197a84985695b19b1164d0a"; for (int i = 0; i < dsUser.Tables["User"].Rows.Count; i++) //i < dsUser.Tables["User"].Rows.Count { enroluser.roleid = Convert.ToInt32(dsUser.Tables["User"].Rows[i]["role1"].ToString()); //get userid from username sUsername = dsUser.Tables["User"].Rows[i]["username"].ToString(); iUserID = Convert.ToInt32("0" + dsUser.Tables["User"].Rows[i]["MoodleUserNo"].ToString()); if (iUserID == 0) { iUserID = LibraryMOD.GetStudentMoodleUserNo(sStudentID); } iCourseID = Convert.ToInt32("0" + dsUser.Tables["User"].Rows[i]["MoodleCourseNo"].ToString()); JavaScriptSerializer serializer = new JavaScriptSerializer(); enroluser.userid = iUserID; sCourse = dsUser.Tables["User"].Rows[i]["course1"].ToString(); enroluser.courseid = iCourseID; enroluser.suspend = iSuspend; if (iCourseID == 2455) //ESL_Gen_20201 { //enroll student in course 2541 iCourseID = 2541; // ESL_Gen_155_20201 enroluser.courseid = iCourseID; } iGroupID = 0; string contents = enrol_manual_enrol_users(enroluser); // Deserialize serializer = new JavaScriptSerializer(); if (contents.Contains("exception")) { // Error ClsMoodleAPI.MoodleException moodleError = serializer.Deserialize <ClsMoodleAPI.MoodleException>(contents); //lbl_results.Text += "Error in enrolling user in course: " + enroluser.courseid + " / " + contents; } else { // Good List <string> newGroups = serializer.Deserialize <List <string> >(contents); // lbl_results.Text += "User Enrolled Successfuly - "; sGroupName = dsUser.Tables["User"].Rows[i]["group1"].ToString().Trim(); if (iGroupID == 0) { //get group no from moodle iGroupID = GetGroupIDFromGroupName(Token, iCourseID, sGroupName); } //add to group contents = core_group_add_group_members(iGroupID, iUserID); //Deserialize serializer = new JavaScriptSerializer(); if (contents.Contains("exception")) { // Error ClsMoodleAPI.MoodleException moodleError = serializer.Deserialize <ClsMoodleAPI.MoodleException>(contents); // lbl_results.Text += "Error in adding member to group:" + sGroupName + "in course:" + sCourse + " / " + contents; } else { // Good //List<string> newGroups = serializer.Deserialize<List<string>>(contents); // lbl_results.Text += "Member Added Successfuly"; iResult = InitializeModule.SUCCESS_RET; } } } //for return(iResult); }
public static int EnrollStudentinMoodleCourses_CourseCode(string sEmail, string sStudentID, string cousreshortcode, int Moodle_CourseNo, int roleid, string groupname) { int iResult = InitializeModule.FAIL_RET; ClsMoodleAPI.MoodleManualEnrol enroluser = new ClsMoodleAPI.MoodleManualEnrol(); int iSuspend = 0; int iUserID = 0; string sUsername = ""; string sCourse = ""; int iCourseID = 0; string sGroupName = ""; int iGroupID = 0; iSuspend = 0; DataSet dsGroups = new DataSet(); string Token = "1b4cb9114197a84985695b19b1164d0a"; enroluser.roleid = roleid; //get userid from username sUsername = sEmail; iUserID = 0; if (iUserID == 0) { iUserID = LibraryMOD.GetStudentMoodleUserNo(sStudentID); } iCourseID = Moodle_CourseNo; JavaScriptSerializer serializer = new JavaScriptSerializer(); enroluser.userid = iUserID; sCourse = cousreshortcode; enroluser.courseid = iCourseID; enroluser.suspend = iSuspend; if (iCourseID == 2455) //ESL_Gen_20201 { //enroll student in course 2541 iCourseID = 2541; // ESL_Gen_155_20201 enroluser.courseid = iCourseID; } iGroupID = 0; string contents = enrol_manual_enrol_users(enroluser); // Deserialize serializer = new JavaScriptSerializer(); if (contents.Contains("exception")) { // Error ClsMoodleAPI.MoodleException moodleError = serializer.Deserialize <ClsMoodleAPI.MoodleException>(contents); //lbl_results.Text += "Error in enrolling user in course: " + enroluser.courseid + " / " + contents; } else { // Good List <string> newGroups = serializer.Deserialize <List <string> >(contents); // lbl_results.Text += "User Enrolled Successfuly - "; sGroupName = groupname; if (iGroupID == 0) { //get group no from moodle iGroupID = GetGroupIDFromGroupName(Token, iCourseID, sGroupName); } //add to group contents = core_group_add_group_members(iGroupID, iUserID); //Deserialize serializer = new JavaScriptSerializer(); if (contents.Contains("exception")) { // Error ClsMoodleAPI.MoodleException moodleError = serializer.Deserialize <ClsMoodleAPI.MoodleException>(contents); // lbl_results.Text += "Error in adding member to group:" + sGroupName + "in course:" + sCourse + " / " + contents; } else { // Good //List<string> newGroups = serializer.Deserialize<List<string>>(contents); // lbl_results.Text += "Member Added Successfuly"; iResult = InitializeModule.SUCCESS_RET; } } //for return(iResult); }
public static int GetGroupIDFromGroupName(string token, int iCourseID, string sGroupName) { int iGroupID = 0; //Start Check if any groups were present in the imported course or not; if yes delete all existing groups ClsMoodleAPI.MoodleGetCourseGroup GetCourse = new ClsMoodleAPI.MoodleGetCourseGroup(); GetCourse.courseid = iCourseID; List <ClsMoodleAPI.MoodleGetCourseGroup> CourseGroupList = new List <ClsMoodleAPI.MoodleGetCourseGroup>(); CourseGroupList.Add(GetCourse); Array arrCourseGroup = CourseGroupList.ToArray(); String postCourseGroup = String.Format("courseid={0}", GetCourse.courseid); string createRequestCourseGroup = string.Format("https://lms.ectmoodle.ae/webservice/rest/server.php?wstoken={0}&wsfunction={1}&moodlewsrestformat=json", token, "core_group_get_course_groups"); // Call Moodle REST Service HttpWebRequest reqCourseGroup = (HttpWebRequest)WebRequest.Create(createRequestCourseGroup); reqCourseGroup.Timeout = 200000; //added by bahaa to solve reqImport.GetResponse() timeout error reqCourseGroup.Method = "POST"; reqCourseGroup.ContentType = "application/x-www-form-urlencoded"; // Encode the parameters as form data: byte[] formCourseGroup = UTF8Encoding.UTF8.GetBytes(postCourseGroup); reqCourseGroup.ContentLength = formCourseGroup.Length; // Write out the form Data to the request: using (Stream postImportCourseGroup = reqCourseGroup.GetRequestStream()) { postImportCourseGroup.Write(formCourseGroup, 0, formCourseGroup.Length); } // Get the Response HttpWebResponse respImportCourseGroup = (HttpWebResponse)reqCourseGroup.GetResponse(); Stream resStreamImportCourseGroup = respImportCourseGroup.GetResponseStream(); StreamReader readerImportCourseGroup = new StreamReader(resStreamImportCourseGroup); string contentsImportCourseGroup = readerImportCourseGroup.ReadToEnd(); JavaScriptSerializer serializerImportCourseGroup = new JavaScriptSerializer(); if (contentsImportCourseGroup.Contains("exception")) { // Error ClsMoodleAPI.MoodleException moodleError = serializerImportCourseGroup.Deserialize <ClsMoodleAPI.MoodleException>(contentsImportCourseGroup); // lblFailResults.Text += "Error in getting course groups -- " + contentsImportCourseGroup; } else { // Good List <ClsMoodleAPI.MoodleGetCourseGroupResponse> newCourseGroup = serializerImportCourseGroup.Deserialize <List <ClsMoodleAPI.MoodleGetCourseGroupResponse> >(contentsImportCourseGroup); int groupid = 0; string groupname = ""; foreach (var value in newCourseGroup) { groupid = value.id; groupname = value.name; if (groupname == sGroupName) { iGroupID = groupid; } } } return(iGroupID); }