protected void btnImport_Click(object sender, EventArgs e) { btnImport.Enabled = false; var rockContext = new RockContext(); var volunteerTrackingContext = new VolunteerTrackingContext(); var volunteerMembershipService = new VolunteerMembershipService(volunteerTrackingContext); var groupService = new GroupService(rockContext); var groupTypeGuids = GetAttributeValue("GroupTypes").SplitDelimitedValues().AsGuidList(); var groupTypeIds = new GroupTypeService(rockContext).GetByGuids(groupTypeGuids).Select(gt => gt.Id); var groups = groupService.Queryable().Where(g => groupTypeIds.Contains(g.GroupTypeId)).ToList(); foreach (var group in groups) { foreach (var groupMember in group.Members) { var volunteerMember = volunteerMembershipService.Queryable().FirstOrDefault(v => v.GroupId == groupMember.GroupId && v.PersonId == groupMember.PersonId && v.LeftGroupDateTime == null); if (volunteerMember == null) { volunteerMember = new VolunteerMembership { GroupId = groupMember.GroupId, PersonId = groupMember.PersonId, GroupRoleId = groupMember.GroupRoleId, JoinedGroupDateTime = DateTime.Now }; volunteerMembershipService.Add(volunteerMember); } } } volunteerTrackingContext.SaveChanges(); btnImport.Enabled = true; }
/// <summary> /// Executes the specified workflow. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="action">The action.</param> /// <param name="entity">The entity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages) { errorMessages = new List <string>(); // Determine the group Group group = null; var guidGroupAttribute = GetAttributeValue(action, "Group").AsGuidOrNull(); if (guidGroupAttribute.HasValue) { var attributeGroup = AttributeCache.Read(guidGroupAttribute.Value, rockContext); if (attributeGroup != null) { var groupGuid = action.GetWorklowAttributeValue(guidGroupAttribute.Value).AsGuidOrNull(); if (groupGuid.HasValue) { group = new GroupService(rockContext).Get(groupGuid.Value); } } } if (group == null) { errorMessages.Add("No group was provided"); } // determine the person Person person = null; // get the Attribute.Guid for this workflow's Person Attribute so that we can lookup the value var guidPersonAttribute = GetAttributeValue(action, "Person").AsGuidOrNull(); if (guidPersonAttribute.HasValue) { var attributePerson = AttributeCache.Read(guidPersonAttribute.Value, rockContext); if (attributePerson != null) { string attributePersonValue = action.GetWorklowAttributeValue(guidPersonAttribute.Value); if (!string.IsNullOrWhiteSpace(attributePersonValue)) { if (attributePerson.FieldType.Class == typeof(Rock.Field.Types.PersonFieldType).FullName) { Guid personAliasGuid = attributePersonValue.AsGuid(); if (!personAliasGuid.IsEmpty()) { person = new PersonAliasService(rockContext).Queryable() .Where(a => a.Guid.Equals(personAliasGuid)) .Select(a => a.Person) .FirstOrDefault(); } } else { errorMessages.Add("The attribute used to provide the person was not of type 'Person'."); } } } } if (person == null) { errorMessages.Add(string.Format("Person could not be found for selected value ('{0}')!", guidPersonAttribute.ToString())); } // Add Person to Volunteer Tracking Table if (!errorMessages.Any()) { var groupMemberService = new GroupMemberService(rockContext); var groupMember = groupMemberService.Queryable().FirstOrDefault(m => m.GroupId == group.Id && m.PersonId == person.Id); if (groupMember != null) { var volunteerTrackingContext = new VolunteerTrackingContext(); var volunteerMembershipService = new VolunteerMembershipService(volunteerTrackingContext); var oldVolunteerMembership = volunteerMembershipService.Queryable().FirstOrDefault(v => v.GroupId == groupMember.GroupId && v.PersonId == groupMember.PersonId && v.LeftGroupDateTime == null); if (oldVolunteerMembership != null) { oldVolunteerMembership.LeftGroupDateTime = DateTime.Now; } var newVolunteerMembership = new VolunteerMembership { GroupId = groupMember.GroupId, PersonId = groupMember.PersonId, GroupRoleId = groupMember.GroupRoleId, JoinedGroupDateTime = DateTime.Now }; volunteerMembershipService.Add(newVolunteerMembership); volunteerTrackingContext.SaveChanges(); } else { // the person is not in the group provided errorMessages.Add("The person is not in the group provided."); } } errorMessages.ForEach(m => action.AddLogEntry(m, true)); return(true); }