Ejemplo n.º 1
0
        /// <summary>
        /// Removes the person RSVP.
        /// </summary>
        /// <param name="person">The person.</param>
        /// <param name="groupId">The group identifier.</param>
        /// <param name="scheduleId">The schedule identifier.</param>
        /// <param name="scheduleDate">The schedule date.</param>
        private void RemovePersonRsvp(Person person, int groupId, int scheduleId, DateTime?scheduleDate)
        {
            RockContext rockContext        = new RockContext();
            var         attendanceService  = new AttendanceService(rockContext);
            var         groupMemberService = new GroupMemberService(rockContext);

            // delete the RSVP
            var attendanceRecord = attendanceService.Queryable()
                                   .Where(a =>
                                          a.PersonAlias.PersonId == CurrentPersonId &&
                                          a.Occurrence.GroupId == groupId &&
                                          a.Occurrence.ScheduleId == scheduleId &&
                                          a.RSVP == RSVP.Yes &&
                                          DbFunctions.TruncateTime(a.StartDateTime) == DbFunctions.TruncateTime(scheduleDate.Value))
                                   .FirstOrDefault();

            if (attendanceRecord != null)
            {
                attendanceService.Delete(attendanceRecord);
            }

            // remove them from the group
            var groupMember = groupMemberService.Queryable().Where(m => m.PersonId == CurrentPersonId && m.GroupId == groupId).FirstOrDefault();

            if (groupMember != null)
            {
                groupMemberService.Delete(groupMember);
            }

            rockContext.SaveChanges();
        }
        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                var service = new AttendanceService(rockContext);

                var attendanceId = GetAttendanceId();

                if (attendanceId == null)
                {
                    return;
                }

                var attendance = service.Get(attendanceId.Value);
                if (attendance == null)
                {
                    return;
                }

                var personGuid = attendance.PersonAlias.Person?.Guid;

                service.Delete(attendance);
                rockContext.SaveChanges();

                var queryParams = new Dictionary <string, string>();

                queryParams.Add(PageParameterKey.PersonGuid, personGuid.ToString());

                NavigateToLinkedPage(AttributeKey.PersonProfilePage, queryParams);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles the Delete event of the gOccurrences control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        protected void gOccurrences_Delete(object sender, RowEventArgs e)
        {
            if (_group != null)
            {
                var      occurrenceDate = (DateTime)e.RowKeyValues["Date"];
                DateTime startDate      = occurrenceDate.Date;
                DateTime endDate        = occurrenceDate.Date.AddDays(1);

                using (var rockContext = new RockContext())
                {
                    var attendanceService = new AttendanceService(rockContext);
                    var qry = attendanceService.Queryable()
                              .Where(a =>
                                     a.GroupId == _group.Id &&
                                     a.StartDateTime >= startDate &&
                                     a.StartDateTime < endDate);

                    int?scheduleId = e.RowKeyValues["ScheduleId"] as int?;
                    if (scheduleId.HasValue)
                    {
                        qry = qry.Where(a =>
                                        a.ScheduleId.HasValue &&
                                        a.ScheduleId.Value == scheduleId.Value);
                    }
                    else
                    {
                        qry = qry.Where(a => !a.ScheduleId.HasValue);
                    }

                    int?locationId = e.RowKeyValues["LocationId"] as int?;
                    if (locationId.HasValue)
                    {
                        qry = qry.Where(a =>
                                        a.LocationId.HasValue &&
                                        a.LocationId.Value == locationId.Value);
                    }
                    else
                    {
                        qry = qry.Where(a => !a.LocationId.HasValue);
                    }

                    foreach (var attendance in qry)
                    {
                        attendanceService.Delete(attendance);
                    }

                    rockContext.SaveChanges();

                    if (locationId.HasValue)
                    {
                        Rock.CheckIn.KioskLocationAttendance.Flush(locationId.Value);
                    }
                }
            }

            BindGrid();
        }
Ejemplo n.º 4
0
 public ActionResult <int> DeleteAttendance(int id)
 {
     try
     {
         _attendanceService.Delete(id);
         return(Ok());
     }
     catch (KeyNotFoundException e)
     {
         return(NotFound($"{e.Message} ID: {id}"));
     }
 }
Ejemplo n.º 5
0
        protected void gHistory_Delete(object sender, Rock.Web.UI.Controls.RowEventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                var service    = new AttendanceService(rockContext);
                var attendance = service.Get(e.RowKeyId);
                if (attendance != null)
                {
                    service.Delete(attendance);
                    rockContext.SaveChanges();
                }
            }

            ShowDetail(PageParameter(PERSON_GUID_PAGE_QUERY_KEY).AsGuid());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Handles the Delete event of the gHistory control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs"/> instance containing the event data.</param>
        protected void gHistory_Delete(object sender, Rock.Web.UI.Controls.RowEventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                var service    = new AttendanceService(rockContext);
                var attendance = service.Get(e.RowKeyId);
                if (attendance != null)
                {
                    service.Delete(attendance);
                    rockContext.SaveChanges();
                }
            }

            ShowDetail(GetPersonGuid());
        }
Ejemplo n.º 7
0
        protected void Save(object sender, EventArgs e)
        {
            if (hfIsDirty.Value.AsBoolean())
            {
                gList.Enabled = false;
                var attendanceService = new AttendanceService(_rockContext);
                foreach (var attendance in _attendanceToAdd)
                {
                    attendanceService.AddOrUpdate(attendance.PersonAliasId.Value, attendance.StartDateTime, _groupId, _locationId, _scheduleId, _campusId);
                }

                foreach (int id in _attendanceToChange)
                {
                    var attendance = attendanceService.Get(id);
                    if (attendance == null)
                    {
                        continue;
                    }
                    attendance.DidAttend = true;
                }

                foreach (int id in _attendanceToRemove)
                {
                    var attendance = attendanceService.Get(id);
                    attendanceService.Delete(attendance);
                }

                _rockContext.SaveChanges();
                _attendance.Clear();
                _attendanceToAdd.Clear();
                _attendanceToRemove.Clear();
                _attendanceToChange.Clear();
                gList.Enabled = true;
                DisplayBox("Attendance Saved", NotificationBoxType.Info);
                hfIsDirty.Value = "false";
                GetAttended();
                BindGrid();
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Handles the Click event of the lbSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbSave_Click(object sender, EventArgs e)
        {
            if (_group != null && _occurrence != null)
            {
                var rockContext        = new RockContext();
                var attendanceService  = new AttendanceService(rockContext);
                var personAliasService = new PersonAliasService(rockContext);
                var locationService    = new LocationService(rockContext);

                bool dateAdjusted = false;

                DateTime startDate = _occurrence.Date;

                // If this is a manuall entered occurrence, check to see if date was changed
                if (!_occurrence.ScheduleId.HasValue)
                {
                    DateTime?originalDate = PageParameter("Date").AsDateTime();
                    if (originalDate.HasValue && originalDate.Value.Date != startDate.Date)
                    {
                        startDate    = originalDate.Value.Date;
                        dateAdjusted = true;
                    }
                }
                DateTime endDate = startDate.AddDays(1);

                var existingAttendees = attendanceService
                                        .Queryable("PersonAlias")
                                        .Where(a =>
                                               a.GroupId == _group.Id &&
                                               a.LocationId == _occurrence.LocationId &&
                                               a.ScheduleId == _occurrence.ScheduleId &&
                                               a.StartDateTime >= startDate &&
                                               a.StartDateTime < endDate);

                if (dateAdjusted)
                {
                    foreach (var attendee in existingAttendees)
                    {
                        attendee.StartDateTime = _occurrence.Date;
                    }
                }

                // If did not meet was selected and this was a manually entered occurrence (not based on a schedule/location)
                // then just delete all the attendance records instead of tracking a 'did not meet' value
                if (cbDidNotMeet.Checked && !_occurrence.ScheduleId.HasValue)
                {
                    foreach (var attendance in existingAttendees)
                    {
                        attendanceService.Delete(attendance);
                    }
                }
                else
                {
                    int?campusId = locationService.GetCampusIdForLocation(_occurrence.LocationId);
                    if (!campusId.HasValue)
                    {
                        campusId = _group.CampusId;
                    }
                    if (!campusId.HasValue && _allowCampusFilter)
                    {
                        var campus = CampusCache.Read(ExtensionMethods.SelectedValueAsInt(bddlCampus) ?? 0);
                        if (campus != null)
                        {
                            campusId = campus.Id;
                        }
                    }

                    if (cbDidNotMeet.Checked)
                    {
                        // If the occurrence is based on a schedule, set the did not meet flags
                        foreach (var attendance in existingAttendees)
                        {
                            attendance.DidAttend   = null;
                            attendance.DidNotOccur = true;
                        }
                    }

                    foreach (var attendee in _attendees)
                    {
                        var attendance = existingAttendees
                                         .Where(a => a.PersonAlias.PersonId == attendee.PersonId)
                                         .FirstOrDefault();

                        if (attendance == null)
                        {
                            int?personAliasId = personAliasService.GetPrimaryAliasId(attendee.PersonId);
                            if (personAliasId.HasValue)
                            {
                                attendance               = new Attendance();
                                attendance.GroupId       = _group.Id;
                                attendance.ScheduleId    = _group.ScheduleId;
                                attendance.PersonAliasId = personAliasId;
                                attendance.StartDateTime = _occurrence.Date.Date.Add(_occurrence.StartTime);
                                attendance.LocationId    = _occurrence.LocationId;
                                attendance.CampusId      = campusId;
                                attendance.ScheduleId    = _occurrence.ScheduleId;

                                // check that the attendance record is valid
                                cvAttendance.IsValid = attendance.IsValid;
                                if (!cvAttendance.IsValid)
                                {
                                    cvAttendance.ErrorMessage = attendance.ValidationResults.Select(a => a.ErrorMessage).ToList().AsDelimited("<br />");
                                    return;
                                }

                                attendanceService.Add(attendance);
                            }
                        }

                        if (attendance != null)
                        {
                            if (cbDidNotMeet.Checked)
                            {
                                attendance.DidAttend   = null;
                                attendance.DidNotOccur = true;
                            }
                            else
                            {
                                attendance.DidAttend   = attendee.Attended;
                                attendance.DidNotOccur = null;
                            }
                        }
                    }
                }

                if (_occurrence.LocationId.HasValue)
                {
                    Rock.CheckIn.KioskLocationAttendance.Flush(_occurrence.LocationId.Value);
                }

                rockContext.SaveChanges();

                WorkflowType workflowType     = null;
                Guid?        workflowTypeGuid = GetAttributeValue("Workflow").AsGuidOrNull();
                if (workflowTypeGuid.HasValue)
                {
                    var workflowTypeService = new WorkflowTypeService(rockContext);
                    workflowType = workflowTypeService.Get(workflowTypeGuid.Value);
                    if (workflowType != null)
                    {
                        try
                        {
                            var workflow = Workflow.Activate(workflowType, _group.Name);

                            workflow.SetAttributeValue("StartDateTime", _occurrence.Date.ToString("o"));
                            workflow.SetAttributeValue("Schedule", _group.Schedule.Guid.ToString());

                            List <string> workflowErrors;
                            new WorkflowService(rockContext).Process(workflow, _group, out workflowErrors);
                        }
                        catch (Exception ex)
                        {
                            ExceptionLogService.LogException(ex, this.Context);
                        }
                    }
                }

                var qryParams = new Dictionary <string, string> {
                    { "GroupId", _group.Id.ToString() }
                };

                var groupTypeIds = PageParameter("GroupTypeIds");
                if (!string.IsNullOrWhiteSpace(groupTypeIds))
                {
                    qryParams.Add("GroupTypeIds", groupTypeIds);
                }

                NavigateToParentPage(qryParams);
            }
        }
        /// <summary>
        /// Handles the Click event of the lbSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbSave_Click(object sender, EventArgs e)
        {
            if (_group != null && _occurrence != null)
            {
                var rockContext        = new RockContext();
                var attendanceService  = new AttendanceService(rockContext);
                var personAliasService = new PersonAliasService(rockContext);

                var existingAttendees = attendanceService.Queryable()
                                        .Where(a =>
                                               a.GroupId == _group.Id &&
                                               a.ScheduleId == _group.ScheduleId &&
                                               a.StartDateTime >= _occurrence.StartDateTime &&
                                               a.StartDateTime < _occurrence.EndDateTime)
                                        .ToList();

                // If did not meet was selected and this was a manually entered occurrence (not based on a schedule)
                // then just delete all the attendance records instead of tracking a 'did not meet' value
                if (cbDidNotMeet.Checked && !_occurrence.ScheduleId.HasValue)
                {
                    foreach (var attendance in existingAttendees)
                    {
                        attendanceService.Delete(attendance);
                    }
                }
                else
                {
                    if (cbDidNotMeet.Checked)
                    {
                        // If the occurrence is based on a schedule, set the did not meet flags
                        foreach (var attendance in existingAttendees)
                        {
                            attendance.DidAttend   = null;
                            attendance.DidNotOccur = true;
                        }
                    }

                    foreach (var item in lvMembers.Items)
                    {
                        var hfMember = item.FindControl("hfMember") as HiddenField;
                        var cbMember = item.FindControl("cbMember") as CheckBox;

                        if (hfMember != null && cbMember != null)
                        {
                            int personId = hfMember.ValueAsInt();

                            var attendance = existingAttendees
                                             .Where(a => a.PersonAlias.PersonId == personId)
                                             .FirstOrDefault();

                            if (attendance == null)
                            {
                                int?personAliasId = personAliasService.GetPrimaryAliasId(personId);
                                if (personAliasId.HasValue)
                                {
                                    attendance               = new Attendance();
                                    attendance.GroupId       = _group.Id;
                                    attendance.ScheduleId    = _group.ScheduleId;
                                    attendance.PersonAliasId = personAliasId;
                                    attendance.StartDateTime = _occurrence.StartDateTime;
                                    attendanceService.Add(attendance);
                                }
                            }

                            if (attendance != null)
                            {
                                if (cbDidNotMeet.Checked)
                                {
                                    attendance.DidAttend   = null;
                                    attendance.DidNotOccur = true;
                                }
                                else
                                {
                                    attendance.DidAttend   = cbMember.Checked;
                                    attendance.DidNotOccur = null;
                                }
                            }
                        }
                    }
                }

                rockContext.SaveChanges();

                WorkflowType workflowType     = null;
                Guid?        workflowTypeGuid = GetAttributeValue("Workflow").AsGuidOrNull();
                if (workflowTypeGuid.HasValue)
                {
                    var workflowTypeService = new WorkflowTypeService(rockContext);
                    workflowType = workflowTypeService.Get(workflowTypeGuid.Value);
                    if (workflowType != null)
                    {
                        try
                        {
                            var workflowService = new WorkflowService(rockContext);
                            var workflow        = Workflow.Activate(workflowType, _group.Name);

                            workflow.SetAttributeValue("StartDateTime", _occurrence.StartDateTime.ToString("o"));
                            workflow.SetAttributeValue("Schedule", _group.Schedule.Guid.ToString());

                            List <string> workflowErrors;
                            if (workflow.Process(rockContext, _group, out workflowErrors))
                            {
                                if (workflow.IsPersisted || workflow.IsPersisted)
                                {
                                    if (workflow.Id == 0)
                                    {
                                        workflowService.Add(workflow);
                                    }

                                    rockContext.WrapTransaction(() =>
                                    {
                                        rockContext.SaveChanges();
                                        workflow.SaveAttributeValues(_rockContext);
                                        foreach (var activity in workflow.Activities)
                                        {
                                            activity.SaveAttributeValues(rockContext);
                                        }
                                    });
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ExceptionLogService.LogException(ex, this.Context);
                        }
                    }
                }

                NavigateToParentPage(new Dictionary <string, string> {
                    { "GroupId", _group.Id.ToString() }
                });
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Saves the attendance data.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="group">The group.</param>
        /// <param name="date">The date.</param>
        /// <param name="attendees">The attendees.</param>
        /// <param name="didNotMeet">if set to <c>true</c> then the group is marked as having not met.</param>
        /// <param name="notes">Contains the note text to save with the occurrence.</param>
        private void SaveAttendanceData(RockContext rockContext, Group group, DateTime date, List <Attendee> attendees, bool didNotMeet, string notes)
        {
            var attendanceService  = new AttendanceService(rockContext);
            var personAliasService = new PersonAliasService(rockContext);

            var occurrence        = GetOccurrence(rockContext, group, date, true);
            var existingAttendees = occurrence.Attendees.ToList();

            if (didNotMeet)
            {
                if (!occurrence.ScheduleId.HasValue)
                {
                    //
                    // If the attendance is not part of a schedule, just delete all the records.
                    //
                    foreach (var attendance in existingAttendees)
                    {
                        attendanceService.Delete(attendance);
                    }
                }
                else
                {
                    //
                    // If the occurrence is based on a schedule, clear the DidAttend property.
                    //
                    foreach (var attendance in existingAttendees)
                    {
                        attendance.DidAttend = null;
                    }
                }
            }
            else
            {
                foreach (var attendee in attendees)
                {
                    var attendance = existingAttendees
                                     .Where(a => a.PersonAlias.Person.Guid == attendee.PersonGuid)
                                     .FirstOrDefault();

                    if (attendance == null)
                    {
                        int?personAliasId = personAliasService.GetPrimaryAliasId(attendee.PersonGuid);
                        if (personAliasId.HasValue)
                        {
                            attendance = new Attendance
                            {
                                PersonAliasId = personAliasId,
                                CampusId      = null,
                                StartDateTime = occurrence.Schedule != null && occurrence.Schedule.HasSchedule() ? occurrence.OccurrenceDate.Date.Add(occurrence.Schedule.StartTimeOfDay) : occurrence.OccurrenceDate,
                                DidAttend     = attendee.Attended
                            };

                            occurrence.Attendees.Add(attendance);
                        }
                    }
                    else
                    {
                        // Otherwise, only record that they attended -- don't change their attendance startDateTime
                        attendance.DidAttend = attendee.Attended;
                    }
                }
            }

            occurrence.DidNotOccur = didNotMeet;

            // Only update the occurrence Notes if 'notes' is not null.
            occurrence.Notes = notes ?? occurrence.Notes;

            rockContext.SaveChanges();
        }
        /// <summary>
        /// Method to save attendance for use in two separate areas.
        /// </summary>
        protected bool SaveAttendance()
        {
            using (var rockContext = new RockContext())
            {
                var occurrenceService  = new AttendanceOccurrenceService(rockContext);
                var attendanceService  = new AttendanceService(rockContext);
                var personAliasService = new PersonAliasService(rockContext);
                var locationService    = new LocationService(rockContext);

                AttendanceOccurrence occurrence = null;

                if (_occurrence.Id != 0)
                {
                    occurrence = occurrenceService.Get(_occurrence.Id);
                }

                if (occurrence == null)
                {
                    var existingOccurrence = occurrenceService.Get(_occurrence.OccurrenceDate, _group.Id, _occurrence.LocationId, _occurrence.ScheduleId);
                    if (existingOccurrence != null)
                    {
                        nbNotice.Heading             = "Occurrence Already Exists";
                        nbNotice.Text                = "<p>An occurrence already exists for this group for the selected date, location, and schedule that you've selected. Please return to the list and select that occurrence to update it's attendance.</p>";
                        nbNotice.NotificationBoxType = NotificationBoxType.Danger;
                        nbNotice.Visible             = true;

                        return(false);
                    }
                    else
                    {
                        occurrence                = new AttendanceOccurrence();
                        occurrence.GroupId        = _occurrence.GroupId;
                        occurrence.LocationId     = _occurrence.LocationId;
                        occurrence.ScheduleId     = _occurrence.ScheduleId;
                        occurrence.OccurrenceDate = _occurrence.OccurrenceDate;
                        occurrenceService.Add(occurrence);
                    }
                }

                occurrence.Notes       = GetAttributeValue("ShowNotes").AsBoolean() ? dtNotes.Text : string.Empty;
                occurrence.DidNotOccur = cbDidNotMeet.Checked;

                var existingAttendees = occurrence.Attendees.ToList();

                // If did not meet was selected and this was a manually entered occurrence (not based on a schedule/location)
                // then just delete all the attendance records instead of tracking a 'did not meet' value
                if (cbDidNotMeet.Checked && !_occurrence.ScheduleId.HasValue)
                {
                    foreach (var attendance in existingAttendees)
                    {
                        attendanceService.Delete(attendance);
                    }
                }
                else
                {
                    int?campusId = locationService.GetCampusIdForLocation(_occurrence.LocationId) ?? _group.CampusId;
                    if (!campusId.HasValue && _allowCampusFilter)
                    {
                        var campus = CampusCache.Get(bddlCampus.SelectedValueAsInt() ?? 0);
                        if (campus != null)
                        {
                            campusId = campus.Id;
                        }
                    }

                    if (cbDidNotMeet.Checked)
                    {
                        // If the occurrence is based on a schedule, set the did not meet flags
                        foreach (var attendance in existingAttendees)
                        {
                            attendance.DidAttend = null;
                        }
                    }
                    else
                    {
                        foreach (var attendee in _attendees)
                        {
                            var attendance = existingAttendees
                                             .Where(a => a.PersonAlias.PersonId == attendee.PersonId)
                                             .FirstOrDefault();

                            if (attendance == null)
                            {
                                int?personAliasId = personAliasService.GetPrimaryAliasId(attendee.PersonId);
                                if (personAliasId.HasValue)
                                {
                                    attendance = new Attendance();
                                    attendance.PersonAliasId = personAliasId;
                                    attendance.CampusId      = campusId;
                                    attendance.StartDateTime = _occurrence.OccurrenceDate;

                                    // check that the attendance record is valid
                                    cvAttendance.IsValid = attendance.IsValid;
                                    if (!cvAttendance.IsValid)
                                    {
                                        cvAttendance.ErrorMessage = attendance.ValidationResults.Select(a => a.ErrorMessage).ToList().AsDelimited("<br />");
                                        return(false);
                                    }

                                    occurrence.Attendees.Add(attendance);
                                }
                            }

                            if (attendance != null)
                            {
                                attendance.DidAttend = attendee.Attended;
                            }
                        }
                    }
                }

                rockContext.SaveChanges();

                if (occurrence.LocationId.HasValue)
                {
                    Rock.CheckIn.KioskLocationAttendance.Remove(occurrence.LocationId.Value);
                }


                Guid?workflowTypeGuid = GetAttributeValue("Workflow").AsGuidOrNull();
                if (workflowTypeGuid.HasValue)
                {
                    var workflowType = WorkflowTypeCache.Get(workflowTypeGuid.Value);
                    if (workflowType != null && (workflowType.IsActive ?? true))
                    {
                        try
                        {
                            var workflow = Workflow.Activate(workflowType, _group.Name);

                            workflow.SetAttributeValue("StartDateTime", _occurrence.OccurrenceDate.ToString("o"));
                            workflow.SetAttributeValue("Schedule", _group.Schedule.Guid.ToString());

                            List <string> workflowErrors;
                            new WorkflowService(rockContext).Process(workflow, _group, out workflowErrors);
                        }
                        catch (Exception ex)
                        {
                            ExceptionLogService.LogException(ex, this.Context);
                        }
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Handles the Click event of the lbSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbSave_Click(object sender, EventArgs e)
        {
            if (_group != null && _occurrence != null)
            {
                var rockContext        = new RockContext();
                var attendanceService  = new AttendanceService(rockContext);
                var personAliasService = new PersonAliasService(rockContext);
                var locationService    = new LocationService(rockContext);

                DateTime startDate = _occurrence.Date;
                DateTime endDate   = _occurrence.Date.AddDays(1);

                var existingAttendees = attendanceService
                                        .Queryable("PersonAlias")
                                        .Where(a =>
                                               a.GroupId == _group.Id &&
                                               a.LocationId == _occurrence.LocationId &&
                                               a.ScheduleId == _occurrence.ScheduleId &&
                                               a.StartDateTime >= startDate &&
                                               a.StartDateTime < endDate);

                // If did not meet was selected and this was a manually entered occurrence (not based on a schedule/location)
                // then just delete all the attendance records instead of tracking a 'did not meet' value
                if (cbDidNotMeet.Checked && !_occurrence.ScheduleId.HasValue)
                {
                    foreach (var attendance in existingAttendees)
                    {
                        attendanceService.Delete(attendance);
                    }
                }
                else
                {
                    if (cbDidNotMeet.Checked)
                    {
                        // If the occurrence is based on a schedule, set the did not meet flags
                        foreach (var attendance in existingAttendees)
                        {
                            attendance.DidAttend   = null;
                            attendance.DidNotOccur = true;
                        }
                    }

                    foreach (var attendee in _attendees)
                    {
                        var attendance = existingAttendees
                                         .Where(a => a.PersonAlias.PersonId == attendee.PersonId)
                                         .FirstOrDefault();

                        if (attendance == null)
                        {
                            int?personAliasId = personAliasService.GetPrimaryAliasId(attendee.PersonId);
                            if (personAliasId.HasValue)
                            {
                                attendance               = new Attendance();
                                attendance.GroupId       = _group.Id;
                                attendance.ScheduleId    = _group.ScheduleId;
                                attendance.PersonAliasId = personAliasId;
                                attendance.StartDateTime = _occurrence.Date;
                                attendance.LocationId    = _occurrence.LocationId;
                                attendance.CampusId      = locationService.GetCampusIdForLocation(_occurrence.LocationId);
                                attendance.ScheduleId    = _occurrence.ScheduleId;
                                attendanceService.Add(attendance);
                            }
                        }

                        if (attendance != null)
                        {
                            if (cbDidNotMeet.Checked)
                            {
                                attendance.DidAttend   = null;
                                attendance.DidNotOccur = true;
                            }
                            else
                            {
                                attendance.DidAttend   = attendee.Attended;
                                attendance.DidNotOccur = null;
                            }
                        }
                    }
                }

                rockContext.SaveChanges();

                WorkflowType workflowType     = null;
                Guid?        workflowTypeGuid = GetAttributeValue("Workflow").AsGuidOrNull();
                if (workflowTypeGuid.HasValue)
                {
                    var workflowTypeService = new WorkflowTypeService(rockContext);
                    workflowType = workflowTypeService.Get(workflowTypeGuid.Value);
                    if (workflowType != null)
                    {
                        try
                        {
                            var workflow = Workflow.Activate(workflowType, _group.Name);

                            workflow.SetAttributeValue("StartDateTime", _occurrence.Date.ToString("o"));
                            workflow.SetAttributeValue("Schedule", _group.Schedule.Guid.ToString());

                            List <string> workflowErrors;
                            new WorkflowService(rockContext).Process(workflow, _group, out workflowErrors);
                        }
                        catch (Exception ex)
                        {
                            ExceptionLogService.LogException(ex, this.Context);
                        }
                    }
                }

                NavigateToParentPage(new Dictionary <string, string> {
                    { "GroupId", _group.Id.ToString() }
                });
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 删除员工当日考勤信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Result DelAtten(string ID)
        {
            attendance.Delete(new Guid(ID));

            return(new Result(true, "删除成功", null));
        }