/// <summary>Initializes a new instance of <see cref="Package"/>.</summary>
        /// <param name="store">The ISlkStore to use.</param>
        /// <param name="file">The package file in SharePoint.</param>
        /// <param name="location">The location of the file.</param>
        public Package(ISlkStore store, SPFile file, SharePointFileLocation location)
        {
            this.store = store;
            this.file  = file;
            Location   = location;
            try
            {
                reader = store.CreatePackageReader(file, location);
                Initialize();
            }
            catch (CacheException ex)
            {
                throw new SafeToDisplayException(ex.Message);
            }
            catch (InvalidPackageException ex)
            {
                string extension = System.IO.Path.GetExtension(file.Name).ToUpperInvariant();
                switch (extension)
                {
                case ".ZIP":
                case ".LRM":
                    throw new SafeToDisplayException(culture.Format(culture.Resources.PackageNotValid, ex.Message));

                default:
                    IsNonELearning = true;
                    break;
                }
            }
        }
Пример #2
0
        /// <summary>Validates a macro.</summary>
        /// <param name="macroName">The macro to validate.</param>
        public static void ValidateMacro(string macroName)
        {
            string name  = macroName;
            int    index = name.IndexOf(":");

            if (index > 0)
            {
                name = name.Substring(0, index);
            }

            switch (name)
            {
            case "SPWebScope":
            case "CurrentUserKey":
            case "Now":
            case "StartOfToday":
            case "StartOfTomorrow":
            case "StartOfThisWeek":
            case "StartOfNextWeek":
            case "StartOfWeekAfterNext":
                break;

            default:
                SlkCulture culture = new SlkCulture();
                throw new SlkSettingsException(culture.Format(culture.Resources.InvalidMacro, macroName));
            }
        }
Пример #3
0
        Exception InvalidTransitionException(LearnerAssignmentState oldStatus, LearnerAssignmentState newStatus)
        {
            SlkCulture culture = new SlkCulture();
            string     message = culture.Format(culture.Resources.LearnerAssignmentTransitionNotSupported, oldStatus, newStatus);

            return(new InvalidOperationException(message));
        }
Пример #4
0
        public RenderedCell[] RenderQueryRowCells(DataRow dataRow, int[,] columnMap, SPWebResolver spWebResolver, SPTimeZone timeZone)
        {
            if (dataRow == null)
            {
                throw new ArgumentNullException("dataRow");
            }
            if (columnMap == null)
            {
                throw new ArgumentNullException("columnMap");
            }

            RenderedCell[] renderedCells     = new RenderedCell[m_columnDefinitions.Count];
            int            iColumnDefinition = 0;

            foreach (ColumnDefinition columnDefinition in m_columnDefinitions)
            {
                // set <cellValue> to the rendered value to be displayed in this cell (i.e. this
                // column of this row); set <cellSortKey> to the cell's sort key value (i.e. the value
                // to use for sorting); set <cellId> to the LearningStoreItemIdentifier associated
                // with this cell (null if none); set <cellToolTip> to the tooltip associated with
                // this cell (null if none)
                object cellValue, cellSortKey;
                LearningStoreItemIdentifier cellId;
                string cellToolTip;
                string text, textNotRounded;
                Guid?  cellSiteGuid = null, cellWebGuid = null;
                string cellWebUrl   = null;
                bool   renderAsLink = false;
                switch (columnDefinition.RenderAs)
                {
                case ColumnRenderAs.Default:

                    cellValue = dataRow[columnMap[iColumnDefinition, 0]];
                    if (cellValue is DBNull)
                    {
                        cellValue   = (columnDefinition.NullDisplayString ?? String.Empty);
                        cellSortKey = String.Empty;
                        cellId      = null;
                        cellToolTip = null;
                    }
                    else
                    {
                        if ((cellId = cellValue as LearningStoreItemIdentifier) != null)
                        {
                            cellValue = cellSortKey = cellId.GetKey();
                        }
                        else
                        {
                            cellSortKey = cellValue;
                        }
                        if (columnDefinition.ToolTipFormat != null)
                        {
                            cellToolTip = culture.Format(columnDefinition.ToolTipFormat, cellValue);
                        }
                        else
                        {
                            cellToolTip = null;
                        }
                        if (columnDefinition.CellFormat != null)
                        {
                            text        = FormatValue(cellValue, columnDefinition.CellFormat);
                            cellValue   = text;
                            cellSortKey = text.ToLower(culture.Culture);
                        }
                    }
                    break;

                case ColumnRenderAs.UtcAsLocalDateTime:

                    cellValue = dataRow[columnMap[iColumnDefinition, 0]];
                    if (cellValue is DBNull)
                    {
                        cellValue   = (columnDefinition.NullDisplayString ?? String.Empty);
                        cellSortKey = String.Empty;
                        cellId      = null;
                        cellToolTip = null;
                    }
                    else
                    {
                        DateTime dateTime;
                        try
                        {
                            dateTime = timeZone.UTCToLocalTime((DateTime)cellValue);
                        }
                        catch (InvalidCastException)
                        {
                            throw new SlkSettingsException(columnDefinition.LineNumber, culture.Resources.SlkUtilitiesViewColumnNameNonDateTime);
                        }
                        cellValue = cellSortKey = dateTime;
                        if (columnDefinition.CellFormat != null)
                        {
                            cellValue = FormatValue(dateTime, columnDefinition.CellFormat);
                        }
                        cellId = null;
                        if (columnDefinition.ToolTipFormat != null)
                        {
                            cellToolTip = culture.Format(columnDefinition.ToolTipFormat, dateTime);
                        }
                        else
                        {
                            cellToolTip = null;
                        }
                    }
                    break;

                case ColumnRenderAs.SPWebTitle:
                case ColumnRenderAs.SPWebName:

                    renderAsLink = (columnDefinition.RenderAs == ColumnRenderAs.SPWebName);
                    cellWebGuid  = GetQueryCell <Guid>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0);
                    cellSiteGuid = GetQueryCell <Guid>(dataRow, columnMap, columnDefinition, iColumnDefinition, 1);
                    if (spWebResolver != null)
                    {
                        spWebResolver(cellWebGuid.Value, cellSiteGuid.Value, out text, out cellWebUrl);
                    }
                    else
                    {
                        text = null;
                    }

                    if (text == null)
                    {
                        text = cellWebGuid.Value.ToString();
                    }

                    cellValue   = text;
                    cellSortKey = text.ToLower(culture.Culture);
                    cellId      = null;
                    if (columnDefinition.ToolTipFormat != null)
                    {
                        cellToolTip = culture.Format(columnDefinition.ToolTipFormat, text);
                    }
                    else
                    {
                        cellToolTip = null;
                    }

                    break;

                case ColumnRenderAs.Link:

                    text        = GetQueryCell <string>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0);
                    cellId      = GetQueryCell <LearningStoreItemIdentifier>(dataRow, columnMap, columnDefinition, iColumnDefinition, 1);
                    cellValue   = text;
                    cellSortKey = text.ToLower(culture.Culture);
                    if (columnDefinition.ToolTipFormat != null)
                    {
                        cellToolTip = culture.Format(columnDefinition.ToolTipFormat, text);
                    }
                    else
                    {
                        cellToolTip = null;
                    }
                    break;

                case ColumnRenderAs.LearnerAssignmentStatus:

                    bool unused;
                    LearnerAssignmentState learnerAssignmentState =
                        (LearnerAssignmentState)GetQueryCell <int>(dataRow, columnMap,
                                                                   columnDefinition, iColumnDefinition, 0, out unused);
                    text        = SlkUtilities.GetLearnerAssignmentState(learnerAssignmentState);
                    cellValue   = text;
                    cellSortKey = learnerAssignmentState;
                    cellId      = null;
                    if (columnDefinition.ToolTipFormat != null)
                    {
                        cellToolTip = culture.Format(columnDefinition.ToolTipFormat, text);
                    }
                    else
                    {
                        cellToolTip = null;
                    }
                    break;

                case ColumnRenderAs.IfEmpty:
                    cellToolTip = null;
                    bool   noColumn1;
                    object column1 = GetQueryCell <object>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0, out noColumn1);
                    bool   noColumn2;
                    object column2 = GetQueryCell <object>(dataRow, columnMap, columnDefinition, iColumnDefinition, 1, out noColumn2);

                    if (noColumn1 == false)
                    {
                        cellValue = column1;
                        if (noColumn2 == false)
                        {
                            if (columnDefinition.ToolTipFormat != null)
                            {
                                cellToolTip = culture.Format("{0} ({1})", cellValue, column2);
                            }
                        }
                    }
                    else if (noColumn2)
                    {
                        cellValue = culture.Resources.SlkUtilitiesPointsNoValue;
                    }
                    else
                    {
                        cellValue = column2;
                    }

                    cellId      = null;
                    cellSortKey = cellValue.ToString().ToLower(culture.Culture);
                    if (string.IsNullOrEmpty(cellToolTip))
                    {
                        cellToolTip = cellValue.ToString();
                    }

                    break;

                case ColumnRenderAs.ScoreAndPossible:

                    // get <finalPoints> and <pointsPossible> from <dataRow>
                    bool  noFinalPoints;
                    float finalPoints = GetQueryCell <float>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0, out noFinalPoints);
                    bool  noPointsPossible;
                    float pointsPossible = GetQueryCell <float>(dataRow, columnMap, columnDefinition, iColumnDefinition, 1, out noPointsPossible);

                    // round to two decimal places
                    float finalPointsRounded    = (float)Math.Round(finalPoints, 2);
                    float pointsPossibleRounded = (float)Math.Round(pointsPossible, 2);

                    // format the result
                    if (!noFinalPoints)
                    {
                        // FinalPoints is not NULL
                        text           = culture.Format(culture.Resources.SlkUtilitiesPointsValue, FormatValue(finalPointsRounded, columnDefinition.CellFormat));
                        textNotRounded = culture.Format(culture.Resources.SlkUtilitiesPointsValue, finalPoints);
                    }
                    else
                    {
                        // FinalPoints is NULL
                        text           = culture.Resources.SlkUtilitiesPointsNoValue;
                        textNotRounded = culture.Resources.SlkUtilitiesPointsNoValue;
                    }
                    if (!noPointsPossible)
                    {
                        // PointsPossible is not NULL
                        text = culture.Format(culture.Resources.SlkUtilitiesPointsPossible, text,
                                              FormatValue(pointsPossibleRounded, columnDefinition.CellFormat));
                        textNotRounded = culture.Format(culture.Resources.SlkUtilitiesPointsPossible, textNotRounded,
                                                        pointsPossible);
                    }
                    cellValue = text;
                    cellId    = null;
                    if ((columnDefinition.ToolTipFormat != null) &&
                        (!noFinalPoints || !noPointsPossible))
                    {
                        cellToolTip = culture.Format(columnDefinition.ToolTipFormat, textNotRounded);
                    }
                    else
                    {
                        cellToolTip = null;
                    }

                    // set <cellSortKey>
                    if (!noFinalPoints)
                    {
                        if (!noPointsPossible)
                        {
                            cellSortKey = ((double)finalPoints) / pointsPossible;
                        }
                        else
                        {
                            cellSortKey = (double)finalPoints;
                        }
                    }
                    else
                    {
                        cellSortKey = (double)0;
                    }
                    break;

                case ColumnRenderAs.Submitted:

                    int countCompletedOrFinal = GetQueryCell <int>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0);
                    int countTotal            = GetQueryCell <int>(dataRow, columnMap, columnDefinition, iColumnDefinition, 1);
                    text        = culture.Format(culture.Resources.SlkUtilitiesSubmitted, countCompletedOrFinal, countTotal);
                    cellValue   = text;
                    cellId      = null;
                    cellToolTip = null;
                    cellSortKey = countCompletedOrFinal;
                    break;

                default:

                    throw new SlkSettingsException(columnDefinition.LineNumber, culture.Resources.SlkUtilitiesUnknownRenderAsValue, columnDefinition.RenderAs);
                }

                // add to <renderedCells>
                RenderedCell renderedCell;
                if (cellSiteGuid != null)
                {
                    renderedCell = new WebNameRenderedCell(cellValue, cellSortKey, cellId, cellToolTip, columnDefinition.Wrap, cellSiteGuid.Value, cellWebGuid.Value, cellWebUrl);
                    ((WebNameRenderedCell)renderedCell).RenderAsLink = renderAsLink;
                }
                else
                {
                    renderedCell = new RenderedCell(cellValue, cellSortKey, cellId, cellToolTip, columnDefinition.Wrap);
                }
                renderedCells[iColumnDefinition++] = renderedCell;
            }

            return(renderedCells);
        }
        /// <summary>Sets the location of the package.</summary>
        /// <remarks>Security checks:Fails if the user doesn't have access to the package/file</remarks>
        /// <param name="location">The MLC SharePoint location string that refers to the e-learning
        ///     package or non-e-learning document to assign.  Use
        ///     <c>SharePointPackageStore.GetLocation</c> to construct this string.</param>
        /// <param name="organizationIndex">The zero-based index of the organization within the
        ///     e-learning content to assign; this is the value that's used as an index to
        ///     <c>ManifestReader.Organizations</c>.  If the content being assigned is a non-e-learning
        ///     document, use <c>null</c> for <paramref name="organizationIndex"/>.</param>
        /// <param name="title">Any title that has been passed in.</param>
        public void SetLocation(SharePointFileLocation location, Nullable <int> organizationIndex, string title)
        {
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }

            if (location.ToString() == Package.NoPackageLocation.ToString())
            {
                MakeNoPackageAssignment(title);
                return;
            }

            // Access the properties of the file to verify that the user has access to the file
            SPFile file = location.LoadFile();

            System.Collections.Hashtable fileProperties = file.Properties;

            // set <rootActivityId> to the ID of the organization, or null if a non-e-learning document is being assigned
            if (organizationIndex != null)
            {
                using (Package package = new Package(Store, file, location))
                {
                    package.Register();

                    RootActivityId = package.FindRootActivity(organizationIndex.Value);

                    bool existingTitle = (string.IsNullOrEmpty(Title) == false);

                    if (existingTitle == false)
                    {
                        Title = package.Title;
                    }

                    if (string.IsNullOrEmpty(Description))
                    {
                        Description = package.Description;
                    }

                    // validate <organizationIndex>
                    if ((organizationIndex.Value < 0) || (organizationIndex.Value >= package.Organizations.Count))
                    {
                        throw new SafeToDisplayException(SlkCulture.GetResources().InvalidOrganizationIndex);
                    }

                    PackageFormat = package.PackageFormat;

                    // set <organizationNodeReader> to refer to the organization
                    OrganizationNodeReader organizationNodeReader = package.Organizations[organizationIndex.Value];

                    // if there is more than one organization, append the organization title, if any
                    if (existingTitle == false && package.Organizations.Count > 1)
                    {
                        if (!String.IsNullOrEmpty(organizationNodeReader.Title))
                        {
                            SlkCulture culture = new SlkCulture();
                            Title = culture.Format(culture.Resources.SlkPackageAndOrganizationTitle, Title, organizationNodeReader.Title);
                        }
                    }

                    // set <pointsPossible> to the points-possible value stored in the manifest, or null if none
                    switch (PackageFormat)
                    {
                    case Microsoft.LearningComponents.PackageFormat.Lrm:
                        PointsPossible = organizationNodeReader.PointsPossible;
                        break;

                    case Microsoft.LearningComponents.PackageFormat.V1p3:
                        PointsPossible = 100;
                        break;

                    default:
                        PointsPossible = null;
                        break;
                    }
                }
            }
            else  // Non-elearning content
            {
                RootActivityId = null;
                Location       = location.ToString();
                if (string.IsNullOrEmpty(Title))
                {
                    Title = file.Title;
                    if (String.IsNullOrEmpty(Title))
                    {
                        Title = System.IO.Path.GetFileNameWithoutExtension(file.Name);
                    }
                }

                if (string.IsNullOrEmpty(Description))
                {
                    Description = string.Empty;
                }

                if (PointsPossible == null)
                {
                    PointsPossible  = null;     // "Points Possible" defaults to null for non-e-learning content
                    PackageWarnings = null;     // no package warnings
                    PackageFormat   = null;     // non-e-learning package
                }
            }
        }
Пример #6
0
        /// <summary>Initializes a new instance of <see cref="UserWebList"/>.</summary>
        /// <param name="store">The ISlkStore to retrieve the web list from.</param>
        /// <param name="currentSite">The current site.</param>
        public UserWebList(ISlkStore store, SPWeb currentSite)
        {
            LimitedSize = store.Settings.UserWebListMruSize;
            bool addCurrentToList = true;
            ReadOnlyCollection <SlkUserWebListItem> userWebList = store.FetchUserWebList();

            bool previousValue = SPSecurity.CatchAccessDeniedException;

            SPSecurity.CatchAccessDeniedException = false;

            SlkCulture culture = new SlkCulture(currentSite);

            try
            {
                foreach (SlkUserWebListItem item in userWebList)
                {
                    try
                    {
                        using (SPSite site = new SPSite(item.SPSiteGuid, SPContext.Current.Site.Zone))
                        {
                            using (SPWeb web = site.OpenWeb(item.SPWebGuid))
                            {
                                WebListItem listItem;
                                if (item.SPWebGuid == currentSite.ID)
                                {
                                    addCurrentToList = false;
                                    string title = culture.Format("{0} {1}", web.Title, culture.Resources.ActionslblMRUCurrentSite);
                                    listItem = new WebListItem(item, web.Url, title);
                                }
                                else
                                {
                                    listItem = new WebListItem(item, web.Url, web.Title);
                                }

                                webList.Add(listItem);
                            }
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        // the user doesn't have permission to access this site, so ignore it
                        continue;
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        // the site doesn't exist
                        continue;
                    }

                    if (addCurrentToList)
                    {
                        string title = culture.Format("{0} {1}", currentSite.Title, culture.Resources.ActionslblMRUCurrentSite);
                        webList.Add(new WebListItem(currentSite.Site.ID, currentSite.ID, DateTime.Now, currentSite.Url, title));
                    }
                }
            }
            finally
            {
                SPSecurity.CatchAccessDeniedException = previousValue;
            }

            if (addCurrentToList)
            {
                string title = culture.Format("{0} {1}", currentSite.Title, culture.Resources.ActionslblMRUCurrentSite);
                webList.Add(new WebListItem(currentSite.Site.ID, currentSite.ID, DateTime.Now, currentSite.Url, title));
            }
        }
Пример #7
0
        /// <summary>Resolves the macro.</summary>
        /// <param name="macroName">The name of the macro.</param>
        /// <returns>The value of the macro, or <c>null</c> if the macro is not defined.</returns>
        public object Resolve(string macroName)
        {
            string name     = macroName;
            int    modifier = 0;

            int index = name.IndexOf(":");

            if (index > 0)
            {
                name = name.Substring(0, index);
                if (name.Length > index + 1)
                {
                    try
                    {
                        modifier = int.Parse(name.Substring(index + 1), CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                        SlkCulture culture = new SlkCulture();
                        throw new SafeToDisplayException(culture.Format(culture.Resources.InvalidModifierForMacro, macroName));
                    }
                }
            }

            DateTime value;

            switch (name)
            {
            case "SPWebScope":
                // return the GUID of the SPWeb that query results will be limited to (i.e.
                // filtered by), or null for no filter
                return(webIds);

            case "CurrentUserKey":
                // return the LearningStore user key string value of the current user
                return(store.CurrentUserKey);

            case "Now":
                value = DateTime.Now.ToUniversalTime();
                break;

            case "StartOfToday":
                // return midnight of today
                value = DateTime.Today.ToUniversalTime();
                break;

            case "StartOfTomorrow":
                // return midnight of tomorrow
                value = DateTime.Today.AddDays(1).ToUniversalTime();
                break;

            case "StartOfThisWeek":
                // return midnight of the preceding Sunday** (or "Today" if "Today" is Sunday**)
                // ** Actually, it's only Sunday for regional setting for which Sunday is the first
                // day of the week.  For example, using Icelandic regional settings, the first day of
                // the week is Monday, and that's what's used below.
                value = StartOfWeek(DateTime.Today).ToUniversalTime();
                break;

            case "StartOfNextWeek":
                // return midnight of the following Sunday**
                value = StartOfWeek(DateTime.Today).AddDays(7).ToUniversalTime();
                break;

            case "StartOfWeekAfterNext":
                // return midnight of the Sunday** after the following Sunday**
                value = StartOfWeek(DateTime.Today).AddDays(14).ToUniversalTime();
                break;

            default:
                return(null);
            }

            if (modifier == 0)
            {
                return(value);
            }
            else
            {
                return(value.AddDays(modifier));
            }
        }