protected string GetRating(int versionId) { var ratings = new PackageVersionRatingService().GetPackageVersionRatings(versionId); if (ratings.Count > 0) { var avgRating = ratings.Sum(r => r.Rating) / ratings.Count(); return((Math.Round((double)avgRating * 2) / 2).ToString()); } else { return("0"); } }
private void ShowPackage() { string errorResponse = string.Empty; lbRate.Visible = false; // get package id int packageId = -1; if (!string.IsNullOrWhiteSpace(PageParameter("PackageId"))) { packageId = Convert.ToInt32(PageParameter("PackageId")); } PackageService packageService = new PackageService(); var package = packageService.GetPackage(packageId, out errorResponse); string storeKey = StoreService.GetOrganizationKey(); // check for errors ErrorCheck(errorResponse); lPackageName.Text = package.Name; lPackageDescription.Text = package.Description; lVendorName.Text = package.Vendor.Name; imgPackageImage.ImageUrl = package.PackageIconBinaryFile.ImageUrl; lbPackageLink.PostBackUrl = package.SupportUrl; lRatingSummary.Text = string.Format("<div class='rating rating-{0} pull-left margin-r-sm'><small></small></div>", package.Rating.ToString().Replace(".", "")); lAuthorInfo.Text = string.Format("<a href='{0}'>{1}</a>", package.Vendor.Url, package.Vendor.Name); if (package.IsFree) { lCost.Text = "<div class='pricelabel free'><h4>Free</h4></div>"; } else { lCost.Text = string.Format("<div class='pricelabel cost'><h4>${0}</h4></div>", package.Price); } // get latest version PackageVersion latestVersion = null; if (package.Versions.Count > 0) { RockSemanticVersion rockVersion = RockSemanticVersion.Parse(VersionInfo.GetRockSemanticVersionNumber()); latestVersion = package.Versions.Where(v => v.RequiredRockSemanticVersion <= rockVersion).OrderByDescending(v => v.Id).FirstOrDefault(); } // determine the state of the install button (install, update, buy or installed) InstalledPackage installedPackage = InstalledPackageService.InstalledPackageVersion(package.Id); if (installedPackage == null) { // it's not installed // todo add logic that it's not installed but has been purchased if (package.IsFree) { // the package is free lbInstall.Text = "Install"; } else { if (package.IsPurchased) { lbInstall.Text = "Install"; lInstallNotes.Text = string.Format("<small>Purchased {0}</small>", package.PurchasedDate.ToShortDateString()); // set rating link button lbRate.Visible = true; lbRate.PostBackUrl = string.Format("http://www.rockrms.com/Store/Rate?OrganizationKey={0}&PackageId={1}", storeKey, packageId.ToString()); } else { lbInstall.Text = "Buy"; } } } else { if (installedPackage.VersionId == latestVersion.Id) { // have the latest version installed lbInstall.Text = "Installed"; lbInstall.Enabled = false; lbInstall.CssClass = "btn btn-default btn-install"; lbInstall.Attributes.Add("disabled", "disabled"); // set rating link button lbRate.Visible = true; lbRate.PostBackUrl = string.Format("http://www.rockrms.com/Store/Rate?OrganizationKey={0}&PackageId={1}&InstalledVersionId={2}", storeKey, packageId.ToString(), installedPackage.VersionId.ToString()); } else { // have a previous version installed lbInstall.Text = "Update"; lInstallNotes.Text = string.Format("<small>You have {0} installed</small>", installedPackage.VersionLabel); // set rating link button lbRate.Visible = true; lbRate.PostBackUrl = string.Format("http://www.rockrms.com/Store/Rate?OrganizationKey={0}&PackageId={1}&InstalledVersionId={2}", storeKey, packageId.ToString(), installedPackage.VersionId.ToString()); } } if (latestVersion != null) { rptScreenshots.DataSource = latestVersion.Screenshots; rptScreenshots.DataBind(); lLatestVersionLabel.Text = latestVersion.VersionLabel; lLatestVersionDescription.Text = latestVersion.Description; var versionReviews = new PackageVersionRatingService().GetPackageVersionRatings(latestVersion.Id); rptLatestVersionRatings.DataSource = versionReviews; rptLatestVersionRatings.DataBind(); lNoReviews.Visible = versionReviews.Count() == 0; // alert the user if a newer version exists but requires a rock update if (package.Versions.Where(v => v.Id > latestVersion.Id).Count() > 0) { var lastVersion = package.Versions.OrderByDescending(v => v.RequiredRockSemanticVersion).FirstOrDefault(); lVersionWarning.Text = string.Format("<div class='alert alert-info'>A newer version of this item is available but requires Rock v{0}.{1}.</div>", lastVersion.RequiredRockSemanticVersion.Minor.ToString(), lastVersion.RequiredRockSemanticVersion.Patch.ToString()); } lLastUpdate.Text = latestVersion.AddedDate.ToShortDateString(); lRequiredRockVersion.Text = string.Format("v{0}.{1}", latestVersion.RequiredRockSemanticVersion.Minor.ToString(), latestVersion.RequiredRockSemanticVersion.Patch.ToString()); lDocumenationLink.Text = string.Format("<a href='{0}'>Documentation Link</a>", latestVersion.DocumentationUrl); lSupportLink.Text = string.Format("<a href='{0}'>Support Link</a>", package.SupportUrl); // fill in previous version info rptAdditionalVersions.DataSource = package.Versions.Where(v => v.Id < latestVersion.Id).OrderByDescending(v => v.AddedDate); rptAdditionalVersions.DataBind(); // get the details for the latest version PackageVersion latestVersionDetails = new PackageVersionService().GetPackageVersion(latestVersion.Id, out errorResponse); // check for errors ErrorCheck(errorResponse); } else { // hide install button lbInstall.Visible = false; // display info on what Rock version you need to be on to run this package if (package.Versions.Count > 0) { var firstVersion = package.Versions.OrderBy(v => v.RequiredRockSemanticVersion).FirstOrDefault(); var lastVersion = package.Versions.OrderByDescending(v => v.RequiredRockSemanticVersion).FirstOrDefault(); if (firstVersion == lastVersion) { lVersionWarning.Text = string.Format("<div class='alert alert-warning'>This item requires Rock version v{0}.{1}.</div>", lastVersion.RequiredRockSemanticVersion.Minor.ToString(), lastVersion.RequiredRockSemanticVersion.Patch.ToString()); } else { lVersionWarning.Text = string.Format("<div class='alert alert-warning'>This item requires at least Rock version v{0}.{1} but the latest version requires v{2}.{3}.</div>", firstVersion.RequiredRockSemanticVersion.Minor.ToString(), firstVersion.RequiredRockSemanticVersion.Patch.ToString(), lastVersion.RequiredRockSemanticVersion.Minor.ToString(), lastVersion.RequiredRockSemanticVersion.Patch.ToString()); } } } }