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) { var 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)); } // Mark Volunteer as Left if (!errorMessages.Any()) { var volunteerTrackingContext = new VolunteerTrackingContext(); var volunteerMembershipService = new VolunteerMembershipService(volunteerTrackingContext); var volunteerMembership = volunteerMembershipService.Queryable().FirstOrDefault(v => v.GroupId == group.Id && v.PersonId == person.Id && v.LeftGroupDateTime == null); if (volunteerMembership != null) { volunteerMembership.LeftGroupDateTime = DateTime.Now; volunteerTrackingContext.SaveChanges(); } else { // the person is not in the group provided errorMessages.Add("The person was not in the group provided."); } } errorMessages.ForEach(m => action.AddLogEntry(m, true)); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="VolunteerMembershipService"/> class. /// </summary> /// <param name="context">The context.</param> public VolunteerMembershipService(VolunteerTrackingContext context) : base(context) { }