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;
                }
            }
        }
        /// <summary>
        /// Save a new module or changes to an existing module.
        /// </summary>
        /// <param name="status">The status to designate for the module.</param>
        private Modules.ModuleInfo saveModule(ModuleStatus status)
        {
            // tells whether old data exists that needs to be
            // removed before adding any new data
            bool removePrevious = false;

            // tells whether user is just updating an existing
            // module or if they want to create a new module
            // (possibly a new version of an existing module)
            bool isUpdate = false;

            Modules.ModuleInfo mi = createModuleInfo();

            ModuleID           = mi.Id;
            ErrorMessage.Text += "Getting base ID for module: " + ModuleID;

            if (ModuleID > 0)
            {
                mi.BaseId = Modules.getModuleInfo(ModuleID).BaseId;
            }
            else
            {
                mi.BaseId = 0;
            }

            mi.Status      = status;
            mi.Submitter   = User.Identity.Name;
            mi.SubmitterID = UserAccounts.getUserInfo(User.Identity.Name).SubmitterID;

            switch (ModuleEditType)
            {
            case EditType.New:

                isUpdate = false;

                break;

            case EditType.InProgress:

                isUpdate       = true;
                removePrevious = true;

                break;

            case EditType.Approved:
                Modules.ModuleInfo oldModule = Modules.getModuleInfo(ModuleID);
                mi.BaseId = oldModule.BaseId;
                // If this module was previously Approved, and an admin changed it,
                // just update the module without creating a new version.
                if (User.Identity.IsAuthenticated && User.IsInRole(UserRole.Admin.ToString()))
                {
                    string modSubmitter = oldModule.Submitter;
                    // If this module is the admin's own, behave as if not an admin.
                    // Admins deserve multiple versions too.
                    if (User.Identity.Name.Equals(modSubmitter))
                    {
                        isUpdate   = false;
                        mi.Version = oldModule.Version + 1;
                    }
                    else
                    {
                        mi.Submitter   = modSubmitter;
                        mi.SubmitterID = oldModule.SubmitterID;

                        isUpdate   = true;
                        mi.Version = oldModule.Version;
                    }
                }
                // If this module was previously Approved, and a non-admin changed
                // it, create a new version of the module, and check it out accordingly.
                else
                {
                    isUpdate = false;
                    MaterialsControl1.retrieveMaterials(oldModule.Id);
                    mi.Version = oldModule.Version + 1;
                }
                removePrevious = true;

                break;
            }

            try
            {
                ModuleID = ModulesControl.checkInModule(mi, isUpdate);

                ErrorMessage.Text += " Assigned ModuleID = " + ModuleID + ".  ";

                mi.Id = ModuleID;

                foreach (IEditControl ec in editControls)
                {
                    ec.insertAll(ModuleID, removePrevious);
                }

                /** HANDLE THE VARIANTS **/

                // is true if this module is new OR a variant is chosen (including <None>)
                if (VariantOf != -1)
                {
                    int groupID = ModuleGroups.getGroupID(mi.BaseId);

                    // if this module was reset as a variant of the same module
                    // as before, or if it was set as a variant of a module in
                    // the same group as it already is in, do nothing
                    if (groupID != -1 && groupID == ModuleGroups.getGroupID(VariantOf))
                    {
                    }
                    // if this module was already in a group by itself, and the
                    // user tries to put it in a new group by itself, ignore
                    // the request and do nothing
                    else if (groupID != -1 && VariantOf == 0 && ModuleGroups.getRelatedModules(mi.BaseId).Count == 0)
                    {
                    }
                    else
                    {
                        // if <None> was chosen, add this module to its own module group
                        if (VariantOf == 0)
                        {
                            ModuleGroups.addToNew(mi.BaseId);
                        }
                        // else add this module to the group that was chosen
                        // SQL code resolves duplicates
                        else
                        {
                            ModuleGroups.addToExisting(mi.BaseId, VariantOf);
                        }
                    }
                }
                else
                {
                    // If the module was not a variant, add this module to its own module group
                    ModuleGroups.addToNew(mi.BaseId);
                }
            }
            catch (Exception e)
            {
                string message = "An error occurred while saving your module.";

                // If a new module was being created and there was an error, remove
                // the module (and by cascading, any added pieces).
                if (ModuleEditType == EditType.New && ModuleID != 0)
                {
                    ModulesControl.removeModule(ModuleID);
                    message += "  Module was not saved.  Review the module and try to resubmit it.";
                }
                else
                {
                    message       += "  All of your changes were not saved.  Review the module and try to resubmit it.";
                    ModuleEditType = EditType.InProgress;
                }

                // send an email to admins reporting the error
                Email msg = Emails.getEmail(EmailType.CriticalError);
                Emails.formatEmail(msg, mi.Submitter, mi.Id);
                Emails.formatEmailBody(msg, e.StackTrace + "::" + e.Message);
                MailMessage mail = Emails.constructErrorMessage(msg, Globals.AdminsEmail);
                SmtpMail.SmtpServer = AspNetForums.Components.Globals.SmtpServer;
                SmtpMail.Send(mail);

                message += " An e-mail reporting the error has been sent to the SWEnet Administrators.";
                throw new Exception(message, e);
            }

            return(mi);
        }