public override IList <SearchDocument> GetModifiedSearchDocuments(ModuleInfo moduleInfo, DateTime beginDateUtc)
        {
            List <SearchDocument> docs    = new List <SearchDocument>();
            List <SurveysInfo>    surveys = SurveysController.GetAll(moduleInfo.ModuleID);

            foreach (SurveysInfo survey in surveys)
            {
                SearchDocument surveyDoc = new SearchDocument();
                surveyDoc.UniqueKey = string.Format("{0}_{1}_{2}", moduleInfo.ModuleDefinition.DefinitionName, moduleInfo.PortalID, survey.SurveyID);
                surveyDoc.NumericKeys.Add("SurveyID", survey.SurveyID);
                surveyDoc.Title           = moduleInfo.ModuleTitle;
                surveyDoc.Body            = survey.Question;
                surveyDoc.AuthorUserId    = (survey.LastModifiedByUserID == null ? survey.CreatedByUserID : survey.LastModifiedByUserID.Value);
                surveyDoc.ModuleId        = moduleInfo.ModuleID;
                surveyDoc.ModuleDefId     = moduleInfo.ModuleDefID;
                surveyDoc.PortalId        = moduleInfo.PortalID;
                surveyDoc.TabId           = moduleInfo.ParentTab.TabID;
                surveyDoc.SearchTypeId    = ModuleSearchTypeId;
                surveyDoc.ModifiedTimeUtc = (survey.LastModifiedDate == null ? survey.CreatedDate : survey.LastModifiedDate.Value).ToUniversalTime();
                // Important, if false, the document will be deleted from the search index
                surveyDoc.IsActive = true;
                docs.Add(surveyDoc);
                List <SurveyOptionsInfo> surveyOptions = SurveyOptionsController.GetAll(survey.SurveyID);
                foreach (SurveyOptionsInfo surveyOption in surveyOptions)
                {
                    SearchDocument surveyOptionDoc = new SearchDocument();
                    surveyOptionDoc.UniqueKey = string.Format("{0}_{1}_{2}_{3}", moduleInfo.ModuleDefinition.DefinitionName, moduleInfo.PortalID, survey.SurveyID, surveyOption.SurveyOptionID);
                    surveyOptionDoc.NumericKeys.Add("SurveyOptionID", surveyOption.SurveyOptionID);
                    surveyOptionDoc.Title           = survey.Question;
                    surveyOptionDoc.Body            = surveyOption.OptionName;
                    surveyOptionDoc.AuthorUserId    = (surveyOption.LastModifiedByUserID == null ? surveyOption.CreatedByUserID : surveyOption.LastModifiedByUserID.Value);
                    surveyOptionDoc.ModuleId        = moduleInfo.ModuleID;
                    surveyOptionDoc.ModuleDefId     = moduleInfo.ModuleDefID;
                    surveyOptionDoc.PortalId        = moduleInfo.PortalID;
                    surveyOptionDoc.TabId           = moduleInfo.ParentTab.TabID;
                    surveyOptionDoc.SearchTypeId    = ModuleSearchTypeId;
                    surveyOptionDoc.ModifiedTimeUtc = (surveyOption.LastModifiedDate == null ? surveyOption.CreatedDate : surveyOption.LastModifiedDate.Value).ToUniversalTime();
                    surveyOptionDoc.IsActive        = true;
                    docs.Add(surveyOptionDoc);
                }
            }
            return(docs);
        }
        public string UpgradeModule(string version)
        {
            string[] _version    = version.Split(new char[] { '.' });
            int      major       = Convert.ToInt32(_version[0]);
            int      minor       = Convert.ToInt32(_version[1]);
            int      maintenance = Convert.ToInt32(_version[2]);

            if (major == 9)
            {
                if (minor == 0)
                {
                    if (maintenance == 0)
                    {
                        List <ModuleInfo> modulesList = ModuleController.GetAllModules().Cast <ModuleInfo>().ToList();

                        foreach (object m in modulesList)
                        {
                            ModuleInfo module = (ModuleInfo)m;
                            if (module.DesktopModule.FriendlyName == "Survey")
                            {
                                ModulePermissionCollection modulePermissions = module.ModulePermissions;
                                // Setting surveyresultstype: 0 = Public, 1 = Private
                                // goes to Permission
                                string surveyResultsTypeSetting = module.ModuleSettings["surveyresultstype"].ToString();
                                if (string.IsNullOrEmpty(surveyResultsTypeSetting))
                                {
                                    // if not defined: make it private to be safe...
                                    surveyResultsTypeSetting = "1";
                                }
                                // If it is public: All Users (RoleID: -1) have the permission to view the results
                                if (surveyResultsTypeSetting == "0")
                                {
                                    List <ModulePermissionInfo> viewResultsPermissions = modulePermissions.Where(mp => mp.ModuleID == module.ModuleID && mp.PermissionCode == ModuleSecurity.PERMISSION_CODE && mp.PermissionKey == ModuleSecurity.VIEW_RESULTS_PERMISSION && mp.RoleID == -1).ToList();
                                    if (viewResultsPermissions.Count() == 0)
                                    {
                                        ModulePermissionInfo viewResultPermission = new ModulePermissionInfo();
                                        viewResultPermission.AllowAccess  = true;
                                        viewResultPermission.RoleID       = -1;
                                        viewResultPermission.PermissionID = ((PermissionInfo)PermissionController.GetPermissionByCodeAndKey(ModuleSecurity.PERMISSION_CODE, ModuleSecurity.VIEW_RESULTS_PERMISSION)[0]).PermissionID;
                                        viewResultPermission.ModuleID     = module.ModuleID;
                                        modulePermissions.Add(viewResultPermission);
                                        ModulePermissionController.SaveModulePermissions(module);
                                    }
                                }
                                // All roles and user who have edit permissions get the View results permission as well
                                List <ModulePermissionInfo> editModulePermissions = modulePermissions.Where(mp => mp.ModuleID == module.ModuleID && mp.PermissionCode == "SYSTEM_MODULE_DEFINITION" && mp.PermissionKey == "EDIT").ToList();
                                foreach (ModulePermissionInfo editModulePermission in editModulePermissions)
                                {
                                    List <ModulePermissionInfo> viewResultsPermissions;
                                    ModulePermissionInfo        viewResultPermission = new ModulePermissionInfo();
                                    if (String.IsNullOrEmpty(editModulePermission.RoleName))
                                    {
                                        // when the role name is empty it is a user poermission
                                        viewResultsPermissions        = modulePermissions.Where(mp => mp.PermissionCode == ModuleSecurity.PERMISSION_CODE && mp.PermissionKey == ModuleSecurity.VIEW_RESULTS_PERMISSION && mp.UserID == editModulePermission.UserID).ToList();
                                        viewResultPermission.UserID   = editModulePermission.UserID;
                                        viewResultPermission.Username = editModulePermission.Username;
                                    }
                                    else
                                    {
                                        // role permission
                                        viewResultsPermissions        = modulePermissions.Where(mp => mp.PermissionCode == ModuleSecurity.PERMISSION_CODE && mp.PermissionKey == ModuleSecurity.VIEW_RESULTS_PERMISSION && mp.RoleID == editModulePermission.RoleID).ToList();
                                        viewResultPermission.RoleID   = editModulePermission.RoleID;
                                        viewResultPermission.RoleName = editModulePermission.RoleName;
                                    }
                                    if (viewResultsPermissions.Count() == 0)
                                    {
                                        // if the permission for this user/role is not already set...
                                        viewResultPermission.AllowAccess  = true;
                                        viewResultPermission.PermissionID = ((PermissionInfo)PermissionController.GetPermissionByCodeAndKey(ModuleSecurity.PERMISSION_CODE, ModuleSecurity.VIEW_RESULTS_PERMISSION)[0]).PermissionID;
                                        viewResultPermission.ModuleID     = module.ModuleID;
                                        modulePermissions.Add(viewResultPermission);
                                        ModulePermissionController.SaveModulePermissions(module);
                                    }
                                }
                                // Setting surveytracking: 0 = Cookie, 1 = Registered user
                                // goes to Permission
                                string surveyTrackingSetting = module.ModuleSettings["surveytracking"].ToString();
                                if (string.IsNullOrEmpty(surveyTrackingSetting))
                                {
                                    // if not defined: make it per user
                                    surveyTrackingSetting = "1";
                                }
                                // If it is Cookie tracking: All users (RoleId: -1) have the permissions to participate in the survey
                                // Otherwise: Registered Users have the permission to participate in the survey
                                // Is there a better way than using the hard coded role IDs?
                                int permittedRoleID = (surveyTrackingSetting == "0" ? -1 : 1);
                                List <ModulePermissionInfo> participatePermissions = modulePermissions.Where(mp => mp.ModuleID == module.ModuleID && mp.PermissionCode == ModuleSecurity.PERMISSION_CODE && mp.PermissionKey == ModuleSecurity.PARTICIPATE_PERMISSION && mp.RoleID == permittedRoleID).ToList();
                                if (participatePermissions.Count() == 0)
                                {
                                    ModulePermissionInfo participatePermission = new ModulePermissionInfo();
                                    participatePermission.AllowAccess  = true;
                                    participatePermission.RoleID       = permittedRoleID;
                                    participatePermission.PermissionID = ((PermissionInfo)PermissionController.GetPermissionByCodeAndKey(ModuleSecurity.PERMISSION_CODE, ModuleSecurity.PARTICIPATE_PERMISSION)[0]).PermissionID;
                                    participatePermission.ModuleID     = module.ModuleID;
                                    modulePermissions.Add(participatePermission);
                                    ModulePermissionController.SaveModulePermissions(module);
                                }
                                // Is Module a quiz?
                                List <SurveysInfo> surveys = SurveysController.GetAll(module.ModuleID);
                                bool isQuiz = false;
                                List <SurveysInfo> statisticalSurveys = new List <SurveysInfo>();
                                foreach (SurveysInfo survey in surveys)
                                {
                                    List <SurveyOptionsInfo> surveyOptions = SurveyOptionsController.GetAll(survey.SurveyID);
                                    int countCorrect = surveyOptions.Where(so => so.IsCorrect).Count();
                                    if (countCorrect > 0)
                                    {
                                        isQuiz = true;
                                    }
                                    else
                                    {
                                        statisticalSurveys.Add(survey);
                                    }
                                }
                                if (isQuiz)
                                {
                                    ModuleController.Instance.UpdateModuleSetting(module.ModuleID, "SurveyType", ((int)SurveyType.Quiz).ToString());
                                    foreach (SurveysInfo statisticalSurvey in statisticalSurveys)
                                    {
                                        statisticalSurvey.IsStatistical = true;
                                        SurveysController.AddOrChange(statisticalSurvey, XmlDataProvider.SurveyOptionsToXml(SurveyOptionsController.GetAll(statisticalSurvey.SurveyID)), -1);
                                    }
                                }
                                string surveyClosingDate = module.ModuleSettings["surveyclosingdate"].ToString();
                                if (!(string.IsNullOrEmpty(surveyClosingDate)))
                                {
                                    ModuleController.Instance.DeleteModuleSetting(module.ModuleID, "surveyclosingdate");
                                    ModuleController.Instance.UpdateModuleSetting(module.ModuleID, "SurveyClosingDate", surveyClosingDate);
                                }
                                // Remove unused old settings
                                ModuleController.Instance.DeleteModuleSetting(module.ModuleID, "surveyresultstype");
                                ModuleController.Instance.DeleteModuleSetting(module.ModuleID, "surveytracking");
                                ModuleController.Instance.DeleteModuleSetting(module.ModuleID, "surveyresulttemplate");
                                ModuleController.Instance.DeleteTabModuleSetting(module.TabModuleID, "surveygraphwidth");
                            }
                        }
                    }
                }
            }
            return(string.Format("Upgrading to version {0}.", version));
        }