Пример #1
0
        /// <summary>
        /// Creates the child controls.
        /// </summary>
        /// <returns></returns>
        public override Control[] CreateChildControls(Type entityType, FilterField filterControl)
        {
            _ddlCourseRequirement                = new RockDropDownList();
            _ddlCourseRequirement.ID             = filterControl.ID + "_courseRequirement";
            _ddlCourseRequirement.Label          = "Course";
            _ddlCourseRequirement.Required       = true;
            _ddlCourseRequirement.DataTextField  = "Value";
            _ddlCourseRequirement.DataValueField = "Key";
            filterControl.Controls.Add(_ddlCourseRequirement);

            RockContext rockContext = new RockContext();
            CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);
            var courseRequirements = courseRequirementService.Queryable("Group,DataView").ToList()
                                     .Where(cr => cr.IsAuthorized(Rock.Security.Authorization.VIEW, filterControl.RockBlock().CurrentPerson))
                                     .ToDictionary(k => k.Id.ToString(),
                                                   v => v.Course.Name + " - " + (v.GroupId.HasValue ? v.Group.Name : "") + (v.DataViewId.HasValue ? v.DataView.Name : ""));

            _ddlCourseRequirement.DataSource = courseRequirements;
            _ddlCourseRequirement.DataBind();

            _tbDays          = new RockTextBox();
            _tbDays.Label    = "Days Until Expiration";
            _tbDays.ID       = filterControl.ID + "_tbDays";
            _tbDays.Required = true;
            filterControl.Controls.Add(_tbDays);

            _cbIncludeExpired      = new RockCheckBox();
            _cbIncludeExpired.ID   = filterControl.ID + "_cbIncludeExpired";
            _cbIncludeExpired.Text = "Include Expired";
            filterControl.Controls.Add(_cbIncludeExpired);

            return(new Control[] { _ddlCourseRequirement, _tbDays, _cbIncludeExpired });
        }
Пример #2
0
        /// <summary>
        /// Creates the child controls.
        /// </summary>
        /// <returns></returns>
        public override Control[] CreateChildControls(Type entityType, FilterField filterControl)
        {
            _ddlFilterBy          = new RockDropDownList();
            _ddlFilterBy.ID       = filterControl.ID + "_filterBy";
            _ddlFilterBy.Label    = "Filter By";
            _ddlFilterBy.Required = true;
            _ddlFilterBy.Items.Add(new ListItem("Has Course Requirement", "0"));
            _ddlFilterBy.Items.Add(new ListItem("Has Complete Course Requirement", "1"));
            _ddlFilterBy.Items.Add(new ListItem("Has Incomplete or Expired Course Requirement", "2"));
            _ddlFilterBy.Items.Add(new ListItem("Has Incomplete Course Requirement", "3"));
            _ddlFilterBy.Items.Add(new ListItem("Has Expired Course Requirement", "4"));
            filterControl.Controls.Add(_ddlFilterBy);

            _ddlCourseRequirement                = new RockDropDownList();
            _ddlCourseRequirement.ID             = filterControl.ID + "_courseRequirement";
            _ddlCourseRequirement.Label          = "Course Requirement";
            _ddlCourseRequirement.Required       = true;
            _ddlCourseRequirement.DataTextField  = "Value";
            _ddlCourseRequirement.DataValueField = "Key";
            filterControl.Controls.Add(_ddlCourseRequirement);

            RockContext rockContext = new RockContext();
            CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);
            var courseRequirements = courseRequirementService.Queryable("Group,DataView").ToList()
                                     .Where(cr => cr.IsAuthorized(Rock.Security.Authorization.VIEW, filterControl.RockBlock().CurrentPerson))
                                     .ToDictionary(k => k.Id.ToString(), v => v.Course.Name + " - " + (v.GroupId.HasValue ? v.Group.Name : "") + (v.DataViewId.HasValue ? v.DataView.Name : ""));

            _ddlCourseRequirement.DataSource = courseRequirements;
            _ddlCourseRequirement.DataBind();

            return(new Control[2] {
                _ddlFilterBy, _ddlCourseRequirement
            });
        }
Пример #3
0
        public virtual void Execute(IJobExecutionContext context)
        {
            using (RockContext rockContext = new RockContext())
            {
                CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);

                var courseRequirements = courseRequirementService.Queryable().AsNoTracking().ToList();
                foreach (var courseRequirement in courseRequirements)
                {
                    CourseRequirementHelper.UpdateCourseRequirementStatuses(courseRequirement);
                }
            }
        }
        private CourseRequirement GetCourseRequirement(CourseRequirementService courseRequirementService)
        {
            var chapterId         = PageParameter(PageParameterKey.CourseRequirementId).AsInteger();
            var courseRequirement = courseRequirementService.Get(chapterId);

            if (courseRequirement == null)
            {
                courseRequirement = new CourseRequirement
                {
                    CourseId = PageParameter(PageParameterKey.CourseId).AsInteger()
                };
            }
            return(courseRequirement);
        }
        protected void btnDelete_Click(object sender, RowEventArgs e)
        {
            var         id          = ( int )e.RowKeyValue;
            RockContext rockContext = new RockContext();
            CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);
            var courseRequirement = courseRequirementService.Get(id);

            if (courseRequirement != null)
            {
                courseRequirementService.Delete(courseRequirement);
                rockContext.SaveChanges();
            }

            BindGrid();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            RockContext rockContext = new RockContext();
            CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);

            var courseRequirement = GetCourseRequirement(courseRequirementService);

            var courseId = courseRequirement.CourseId;

            courseRequirementService.Delete(courseRequirement);
            rockContext.SaveChanges();

            NavigateToParentPage(new Dictionary <string, string> {
                { PageParameterKey.CourseId, courseId.ToString() }
            });
        }
Пример #7
0
        private static void UpdateStatuses(int courseRequirementId)
        {
            RockContext rockContext = new RockContext();
            CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);

            var courseRequirement = courseRequirementService.Get(courseRequirementId);

            if (courseRequirement == null)
            {
                return;
            }

            CourseRequirementStatusService statusService = new CourseRequirementStatusService(rockContext);

            var statuses = statusService.Queryable("PersonAlias")
                           .Where(s => s.CourseRequirementId == courseRequirement.Id)
                           .ToList();

            CourseRecordService courseRecordService = new CourseRecordService(rockContext);

            foreach (var status in statuses)
            {
                var newestRecord = courseRecordService.Queryable()
                                   .Where(r => r.Passed)
                                   .Where(r => r.PersonAlias.PersonId == status.PersonAlias.PersonId)
                                   .Where(r => r.CourseId == courseRequirement.CourseId)
                                   .OrderByDescending(r => r.CompletionDateTime.Value)
                                   .FirstOrDefault();
                if (newestRecord == null)
                {
                    status.IsComplete = false;
                }
                else
                {
                    status.IsComplete = true;
                    if (courseRequirement.DaysValid.HasValue)
                    {
                        status.ValidUntil = newestRecord.CompletionDateTime.Value.AddDays(courseRequirement.DaysValid.Value);
                    }
                    else
                    {
                        status.ValidUntil = null;
                    }
                }
            }
            rockContext.SaveChanges();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var         isNew       = false;
            RockContext rockContext = new RockContext();
            CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);

            var courseRequirement = GetCourseRequirement(courseRequirementService);

            if (courseRequirement.Id == 0)
            {
                isNew = true;
                courseRequirementService.Add(courseRequirement);
            }

            courseRequirement.CourseId = pCourse.SelectedValueAsId() ?? 0;

            if (ddlSelect.SelectedValue == "DATAVIEW")
            {
                courseRequirement.DataViewId = pDataview.SelectedValueAsId();
                courseRequirement.GroupId    = null;
            }
            else if (ddlSelect.SelectedValue == "GROUP")
            {
                courseRequirement.GroupId    = pGroup.SelectedValueAsId();
                courseRequirement.DataViewId = null;
            }

            courseRequirement.DaysValid = tbDaysValid.Text.AsIntegerOrNull();

            rockContext.SaveChanges();
            ;

            CourseRequirementHelper.UpdateCourseRequirementStatuses(courseRequirement);

            if (isNew)
            {
                NavigateToCurrentPage(new Dictionary <string, string> {
                    { PageParameterKey.CourseId, PageParameter(PageParameterKey.CourseId) },
                    { PageParameterKey.CourseRequirementId, courseRequirement.Id.ToString() },
                });
            }
            else
            {
                ShowDetails();
            }
        }
Пример #9
0
        /// <summary>
        /// Formats the selection.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <param name="selection">The selection.</param>
        /// <returns></returns>
        public override string FormatSelection(Type entityType, string selection)
        {
            string result = "Course Requirement Status";

            string[] selectionValues = selection.Split('|');
            if (selectionValues.Length >= 2)
            {
                RockContext rockContext         = new RockContext();
                var         courseRequirementId = selectionValues[1].AsInteger();
                CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);
                var courseName = courseRequirementService.Queryable("Group,DataView")
                                 .Where(r => r.Id == courseRequirementId)
                                 .Select(r => r.Course.Name + " - " + (r.GroupId.HasValue ? r.Group.Name : "") + (r.DataViewId.HasValue ? r.DataView.Name : ""))
                                 .FirstOrDefault();


                switch (selectionValues[0])
                {
                case "0":     //Has Course Requirement
                    result = "Has Course Requirement: " + courseName;
                    break;

                case "1":     //Has Complete Course Requrirement
                    result = "Has Complete Course Requirement: " + courseName;
                    break;

                case "2":     //Has Incomplete or Expired Course Requrirement
                    result = "Has Incomplete or Expired Course Requirement: " + courseName;
                    break;

                case "3":     //Has Incomplete Course Requirement
                    result = "Has Incomplete Course Requirement: " + courseName;
                    break;

                case "4":     //Has Expired Course Requirement
                    result = "Has Expired Course Requirement: " + courseName;
                    break;

                default:
                    break;
                }
            }

            return(result);
        }
Пример #10
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            RockContext rockContext = new RockContext();
            CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);

            var allItems = courseRequirementService.Queryable()
                           .OrderBy(cr => cr.Id).ToList();

            var allowedItems = new List <CourseRequirement>();

            foreach (var item in allItems)
            {
                if (item.IsAuthorized(Rock.Security.Authorization.VIEW, CurrentPerson))
                {
                    allowedItems.Add(item);
                }
            }

            gList.DataSource = allowedItems;
            gList.DataBind();
        }
Пример #11
0
        public static void UpdateCourseStatuses(int courseId, int personAliasId, bool passed)
        {
            RockContext rockContext = new RockContext();
            CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);

            var courseRequirements = courseRequirementService.Queryable()
                                     .Where(r => r.CourseId == courseId)
                                     .ToList();

            if (!courseRequirements.Any())
            {
                return;
            }

            PersonAliasService personAliasService = new PersonAliasService(rockContext);
            var personAliasIds = personAliasService.Get(personAliasId)
                                 .Person.Aliases.Select(pa => pa.Id).ToList();
            CourseRequirementStatusService courseStatusService = new CourseRequirementStatusService(rockContext);

            foreach (var courseRequirement in courseRequirements)
            {
                var status = courseStatusService.Queryable()
                             .Where(s => s.CourseRequirementId == courseRequirement.Id)
                             .Where(s => personAliasIds.Contains(s.PersonAliasId))
                             .FirstOrDefault();
                if (status != null)
                {
                    status.IsComplete = passed;
                    if (status.IsComplete)
                    {
                        if (courseRequirement.DaysValid.HasValue)
                        {
                            status.ValidUntil = Rock.RockDateTime.Today.AddDays(courseRequirement.DaysValid.Value);
                        }
                    }
                }
            }
            rockContext.SaveChanges();
        }
Пример #12
0
        /// <summary>
        /// Formats the selection.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <param name="selection">The selection.</param>
        /// <returns></returns>
        public override string FormatSelection(Type entityType, string selection)
        {
            string result = "Expiring Course Requirement";

            string[] selectionValues = selection.Split('|');
            if (selectionValues.Length >= 3)
            {
                RockContext rockContext         = new RockContext();
                var         courseRequirementId = selectionValues[0].AsInteger();
                CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);
                var courseName = courseRequirementService.Queryable("Group,DataView")
                                 .Where(r => r.Id == courseRequirementId)
                                 .Select(r => r.Course.Name + " - " + (r.GroupId.HasValue ? r.Group.Name : "") + (r.DataViewId.HasValue ? r.DataView.Name : ""))
                                 .FirstOrDefault();

                result = string.Format("{0} expiring in the next {1} days {2}",
                                       courseName,
                                       selectionValues[1],
                                       selectionValues[2].AsBoolean() ? "including expired" : "not including expired");
            }

            return(result);
        }
Пример #13
0
        private static void AddAndRemoveRows(int courseRequirementId)
        {
            RockContext rockContext = new RockContext();
            CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);

            var courseRequirement = courseRequirementService.Get(courseRequirementId);

            if (courseRequirement == null)
            {
                return;
            }

            IQueryable <Person> peopleQry = null;

            if (courseRequirement.GroupId.HasValue)
            {
                GroupMemberService groupMemberService = new GroupMemberService(rockContext);
                peopleQry = groupMemberService.Queryable()
                            .Where(gm => gm.GroupId == courseRequirement.GroupId)
                            .Select(gm => gm.Person);
            }
            else if (courseRequirement.DataViewId.HasValue)
            {
                var             errorMessages   = new List <string>();
                DataViewService dataViewService = new DataViewService(rockContext);
                var             dataview        = dataViewService.Get(courseRequirement.DataViewId.Value);
                var             qry             = dataview.GetQuery(null, rockContext, 300, out errorMessages);
                peopleQry = qry.Select(e => ( Person )e);
            }


            //If you can't take this course, you shouldn't be required to take it
            if (courseRequirement.Course.AllowedGroupId.HasValue)
            {
                GroupMemberService groupMemberService = new GroupMemberService(rockContext);
                var groupMemberIds = groupMemberService.Queryable()
                                     .Where(gm => gm.GroupId == courseRequirement.Course.AllowedGroupId.Value)
                                     .Select(gm => gm.PersonId);
                peopleQry = peopleQry.Where(p => groupMemberIds.Contains(p.Id));
            }
            else if (courseRequirement.Course.AllowedDataViewId.HasValue)
            {
                var             errorMessages   = new List <string>();
                DataViewService dataViewService = new DataViewService(rockContext);
                var             dataview        = dataViewService.Get(courseRequirement.Course.AllowedDataViewId.Value);
                var             entityIds       = dataview.GetQuery(null, rockContext, 300, out errorMessages).Select(e => e.Id);
                peopleQry = peopleQry.Where(p => entityIds.Contains(p.Id));
            }

            var people = peopleQry.ToList();

            CourseRequirementStatusService statusService = new CourseRequirementStatusService(rockContext);

            var peopleInStatuses = statusService.Queryable()
                                   .Where(s => courseRequirement.Id == s.CourseRequirementId)
                                   .Select(s => s.PersonAlias.PersonId)
                                   .ToList();

            var peopleWithoutStatus = people.Where(p => !peopleInStatuses.Contains(p.Id)).ToList();

            foreach (var person in peopleWithoutStatus)
            {
                if (!person.PrimaryAliasId.HasValue)
                {
                    continue;
                }

                var status = new CourseRequirementStatus
                {
                    PersonAliasId       = person.PrimaryAliasId.Value,
                    CourseRequirementId = courseRequirement.Id
                };
                statusService.Add(status);
            }

            var personAliasIds = people.SelectMany(p => p.Aliases.Select(pa => pa.Id)).ToList();

            var statusesToRemove = statusService.Queryable()
                                   .Where(s => courseRequirement.Id == s.CourseRequirementId)
                                   .Where(s => !personAliasIds.Contains(s.PersonAliasId))
                                   .ToList();

            foreach (var toRemove in statusesToRemove)
            {
                statusService.Delete(toRemove);
            }

            rockContext.SaveChanges();
        }
Пример #14
0
        private void ShowDetails()
        {
            if (!course.IsAuthorized(Rock.Security.Authorization.EDIT, CurrentPerson))
            {
                btnEdit.Visible = false;
            }

            HideSecondaryBlocks(false);
            pnlView.Visible    = true;
            pnlEdit.Visible    = false;
            ltDescription.Text = course.Description;
            if (course.Image != null)
            {
                ltImage.Text = string.Format("<img src='/GetImage.ashx?Guid={0}' class='img-responsive'>", course.Image.Guid);
            }

            if (course.IsActive)
            {
                ltLabels.Text = " <span class='label label-success' data-toggle='tooltip'>Active</span>";
            }
            else
            {
                ltLabels.Text = " <span class='label label-danger' data-toggle='tooltip'>Inactive</span>";
            }

            if (course.ExternalCourseUrl.IsNotNullOrWhiteSpace())
            {
                ltLabels.Text += " <span class='label label-info' data-toggle='tooltip'data-original-title='Course is hosted outside of Rock.'>External Course</span>";
            }

            CourseRecordService courseRecordService = new CourseRecordService(rockContext);
            var recordsQry = courseRecordService.Queryable()
                             .Where(r => r.CourseId == course.Id);

            var attempts        = recordsQry.Count();
            var completes       = recordsQry.Where(r => r.Passed).Count();
            var attemptPeople   = recordsQry.DistinctBy(r => r.PersonAlias.PersonId).Count();
            var completesPeople = recordsQry.Where(r => r.Passed).DistinctBy(r => r.PersonAlias.PersonId).Count();

            ltStatus.Text = string.Format("Taken {0} times by {1} {2}.<br>Completed {3} times by {4} {5}.",
                                          attempts,
                                          attemptPeople,
                                          attemptPeople == 1 ? "person" : "people",
                                          completes,
                                          completesPeople,
                                          completesPeople == 1 ? "person" : "people"
                                          );

            var records = recordsQry
                          .GroupBy(r => r.PersonAlias.Person)
                          .Select(r => new
            {
                Person            = r.Key,
                TimesCompleted    = r.Where(cr => cr.Passed).Count(),
                CompletedDateTime = r.Select(cr => cr.CompletionDateTime).OrderByDescending(dt => dt).FirstOrDefault()
            })
                          .ToList();

            gReport.DataSource = records;
            gReport.DataBind();

            CourseRequirementService courseRequirementService = new CourseRequirementService(rockContext);
            var courseRequirements = courseRequirementService.Queryable("Group,DataView").AsNoTracking().Where(cr => cr.CourseId == course.Id).ToList();


            var sb = new StringBuilder();

            foreach (var requirement in courseRequirements)
            {
                if (requirement.Group != null)
                {
                    sb.Append(string.Format("<a href='{0}'>Group: {1}</a><br>",
                                            LinkedPageUrl(AttributeKey.RequirementDetailPage, new Dictionary <string, string> {
                        { "CourseRequirementId", requirement.Id.ToString() }
                    }),
                                            requirement.Group.Name));
                }
                else if (requirement.DataView != null)
                {
                    sb.Append(string.Format("<a href='{0}'>DataView: {1}</a><br>",
                                            LinkedPageUrl(AttributeKey.RequirementDetailPage, new Dictionary <string, string> {
                        { "CourseRequirementId", requirement.Id.ToString() }
                    }),
                                            requirement.DataView.Name));
                }
            }

            var requirementsPage = LinkedPageUrl(AttributeKey.RequirementDetailPage, new Dictionary <string, string> {
                { PageParameterKey.CourseId, course.Id.ToString() }
            });

            sb.Append(string.Format("<small><a href='{0}'><i>Add New Course Requirement</i></a></small>", requirementsPage));

            ltRequirements.Text = sb.ToString();

            if (course.AllowedGroupId.HasValue && course.AllowedGroup != null)
            {
                ltAllowed.Visible = true;
                ltAllowed.Label   = "Restricted To Group";
                ltAllowed.Text    = course.AllowedGroup.Name;
            }
            else if (course.AllowedDataViewId.HasValue && course.AllowedDataView != null)
            {
                ltAllowed.Visible = true;
                ltAllowed.Label   = "Restricted To Data View";
                ltAllowed.Text    = course.AllowedDataView.Name;
            }

            if (course.ExternalCourseUrl.IsNotNullOrWhiteSpace())
            {
                ltExternalUrl.Visible = true;
                ltExternalUrl.Text    = course.ExternalCourseUrl;
            }
        }