public static ModuleRatingInfo initModuleRating(int moduleID)
        {
            // Construct a new thread post for ratings for the given module.

            Modules.ModuleInfo module = Modules.getModuleInfo(moduleID);
            Post newPost = new Post();

            newPost.Username = "******";
            newPost.Subject  = module.Title;
            newPost.Body     = "Ratings for <a href=\"" + HttpContext.Current.Request.ApplicationPath +
                               "/viewModule.aspx?moduleID=" + module.Id + "\">" + module.Title + "</a>.";
            newPost.IsLocked = true;
            newPost.ForumID  = FORUMS_ID;

            // Add the post to the forums database.

            Post returnedPost = Posts.AddPost(newPost);

            // Construct a ModuleRatingInfo object with the thread ID of the
            // actual post created.

            ModuleRatingInfo modRating = new ModuleRatingInfo();

            modRating.ModuleID = module.Id;
            modRating.ThreadID = returnedPost.ThreadID;

            ModuleRatings.createRating(modRating);

            return(modRating);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="currentInfo"></param>
        /// <param name="post"></param>
        /// <param name="rating"></param>
        /// <returns></returns>
        public static ModuleRatingInfo addRating(ModuleRatingInfo currentInfo, Post post, Rating rating)
        {
            // If post is not null, add the post and rating to forums database.
            // Even if the post is empty, still add it.

            if (post != null)
            {
                post.PostType = Posts.PostType.Rating;
                post.ParentID = currentInfo.ThreadID;
                post.IsLocked = true;
                post          = Posts.AddPost(post);
                rating.PostID = post.PostID;
                Ratings.AddRating(rating);
            }

            // Calculate the new rating and construct a new ModuleRatingInfo
            // object to be returned.

            float newRating = 0;

            if (currentInfo.NumRatings == 0)
            {
                newRating = rating.Value;
            }
            else
            {
                newRating = (currentInfo.Rating * currentInfo.NumRatings + rating.Value) /
                            (currentInfo.NumRatings + 1);
            }

            ModuleRatingInfo newInfo = new ModuleRatingInfo();

            newInfo.ModuleID   = currentInfo.ModuleID;
            newInfo.ThreadID   = currentInfo.ThreadID;
            newInfo.Rating     = newRating;
            newInfo.NumRatings = currentInfo.NumRatings + 1;

            // Update the module rating in the module database.

            ModuleRatings.updateRating(newInfo);

            return(newInfo);
        }
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                try {
                    string strModuleID = Request.Params["moduleID"];

                    if (strModuleID != null)
                    {
                        int moduleID = int.Parse(Request.Params["moduleID"]);
                        Modules.ModuleInfo modInfo = Modules.getModuleInfo(moduleID);
                        ModInfo     = modInfo;
                        isSubmitter = ModulesControl.isModuleSubmitter(User.Identity.Name, modInfo.Id);
                        isAuthor    = ModulesControl.isModuleAuthor(User.Identity.Name, modInfo.Id);
                        bool isEditor = User.IsInRole(UserRole.Editor.ToString());

                        // If the user is not an editor...
                        if (!isEditor)
                        {
                            if (modInfo.Status == ModuleStatus.PreviousVersion)
                            {
                                // If the module being viewed is a previous version, display the newest
                                // version of the module, and tell the user.
                                Modules.ModuleInfo curVer = Modules.getModuleCurrentVersion(modInfo.BaseId);
                                string             newUrl = "viewModule.aspx?moduleID=" + curVer.Id;
                                lblMessage.Text = "There is a <a href=\"" + newUrl + "\">newer version</a> of the module you are trying to access.  "
                                                  + "You will be directed to it momentarily.";
                                Response.AppendHeader("Refresh", REFRESH_DELAY + "; URL=" + newUrl);
                                disableModule();
                                return;
                            }
                            else if (modInfo.Status != ModuleStatus.Approved && !isSubmitter)
                            {
                                // If it is the newest version, but has not been approved
                                // and this user is not the submitter, do not let them view
                                // the module.
                                disableModule();
                                lblMessage.Text = "Module not available.";
                                return;
                            }
                        }

                        // Modules.Size

                        string[] labels = { "Lecture",
                                            "Lab",
                                            "Exercise",
                                            "Homework",
                                            "Other" };
                        string[] sizeText = { modInfo.LectureSize,
                                              modInfo.LabSize,
                                              modInfo.ExerciseSize,
                                              modInfo.HomeworkSize,
                                              modInfo.OtherSize };

                        ArrayList sizes = new ArrayList(NUM_SIZES);

                        for (int i = 0; i < NUM_SIZES; i++)
                        {
                            if (!sizeText[i].Equals(""))
                            {
                                sizes.Add("<strong>" + labels[i] + "</strong>: " + sizeText[i]);
                            }
                        }

                        SizeRepeater.DataSource = sizes;
                        SizeRepeater.DataBind();

                        // Authors

                        AuthorsRepeater.DataSource = Authors.getAll(modInfo.Id);
                        AuthorsRepeater.DataBind();

                        // Materials
                        // Since Materials aren't displayed as simply as other
                        // areas, some modification needs to be done
                        // Retrieve the materials that the user has the access level right to acquire
                        tempMats1 = getAccessLevelFilesList(moduleID);                         //Materials.getAll( moduleID );
                        ArrayList tempMats2   = new ArrayList();
                        ArrayList knownTitles = new ArrayList();
                        int       pos         = 0;

                        foreach (Materials.MaterialInfo mi in tempMats1)
                        {
                            int    position = mi.Link.LastIndexOf('.'); // temp value for extracting the title and extension
                            string title;                               // the title part of the filename
                            string extension;                           // the extension of the filename

                            if (position == -1)
                            {
                                title     = "(No Title)";
                                extension = mi.Link;
                            }
                            else
                            {
                                title     = mi.Link.Substring(0, position);
                                extension = getFormat(mi.Link.Substring(position + 1));
                            }

                            // this is the reference to the current material that will
                            // be added to the materials repeater in the html
                            string reference = "(<a href='Materials/" + mi.ModuleID + "/" + mi.Link +
                                               "' target='_blank'>" + extension + "</a>)";

                            // see if we've already come across this title
                            int index = knownTitles.IndexOf(title.ToLower());

                            if (index == -1)
                            {
                                // Means that the title hasn't already been found,
                                // so we need to add it as a new Material group
                                tempMats2.Add(new Materials.MaterialInfo(mi.Id, mi.IdentInfo, reference,
                                                                         mi.ModuleID, mi.RatingImage, mi.Rating,
                                                                         mi.AccessFlag));
                                knownTitles.Add(title.ToLower());
                            }
                            else
                            {
                                // Means that the title has already been found, so we
                                // need to add a link for this extra Material to the
                                // appropriate existing Material group
                                ((Materials.MaterialInfo)(tempMats2[index])).Link += " " + reference;
                            }
                        }

                        MaterialsRepeater.DataSource = tempMats2;
                        MaterialsRepeater.DataBind();

                        // Categories

                        CategoriesRepeater.DataSource = Categories.getAll(modInfo.Id);
                        CategoriesRepeater.DataBind();

                        // Prerequisites

                        PrereqRepeater.DataSource = Prerequisites.getAll(modInfo.Id);
                        PrereqRepeater.DataBind();

                        // Objectives

                        ObjectivesRepeater.DataSource = Objectives.getAll(modInfo.Id);
                        ObjectivesRepeater.DataBind();

                        // Topics

                        TopicsRepeater.DataSource = Topics.getAll(modInfo.Id);
                        TopicsRepeater.DataBind();

                        // Resources

                        IList resourcesList = Resources.getAll(modInfo.Id);

                        // If there are no resources, display text that says so.
                        if (resourcesList.Count == 0)
                        {
                            ResourcesRepeater.Controls.Add(new LiteralControl("<p>No resources.</p>"));
                        }
                        else
                        {
                            ResourcesRepeater.DataSource = resourcesList;
                            ResourcesRepeater.DataBind();
                        }

                        // See Also

                        IList seeAlsoList = SeeAlso.getAll(modInfo.Id);

                        // If there are no alternate modules, display text that says so.
                        if (seeAlsoList.Count == 0)
                        {
                            SeeAlsoRepeater.Controls.Add(new LiteralControl("<p>No alternate modules.</p>"));
                        }
                        else
                        {
                            SeeAlsoRepeater.DataSource = seeAlsoList;
                            SeeAlsoRepeater.DataBind();
                        }

                        // Ratings

                        if (modInfo.Status == ModuleStatus.Approved)
                        {
                            ModuleRatingInfo mci = ModuleRatings.getRating(modInfo.Id);
                            RateModuleControl1.RatingInfo = mci;

                            // Submitters and authors can't rate their own modules.
                            if (isSubmitter || isAuthor)
                            {
                                RateModuleControl1.AddRatingEnabled = false;
                                RatingsMessage.Text = "<p>You may not rate a module that " +
                                                      "you have submitted or are listed as an author of.</p>";
                            }
                            else
                            {
                                Rating rating = ModuleRatingsControl.getRatingForUser(User.Identity.Name, mci);
                                RateModuleControl1.UserRating = rating;
                            }
                        }
                        else
                        {
                            disableRatings();
                        }

                        // Related Modules
                        IList related = ModuleGroups.getRelatedModules(modInfo.BaseId);

                        if (related.Count == 0)
                        {
                            H21.Visible = false;
                        }
                        else
                        {
                            RelatedRepeater.DataSource = related;
                            RelatedRepeater.DataBind();
                        }
                    }
                    else
                    {
                        lblMessage.Text =
                            "An error occurred while attempting to get the requested module." +
                            "  No module was specified.  <a href=\"browseModules.aspx\">Browse</a>" +
                            " or <a href=\"search.aspx\">search</a> to select a module to view.";
                    }
                } catch (Exception ex) {
                    lblMessage.Text =
                        "An error occurred while attempting to get the requested module." +
                        "  " + ex.Message + " " + ex.StackTrace;
                }
            }
        }
 public static Rating getRatingForUser(string username, ModuleRatingInfo ratingInfo)
 {
     return(Ratings.GetRatingForUser(username, ratingInfo.ThreadID));
 }