/// <summary>
 /// Renders the File Submission column cell
 /// </summary>
 /// <param name="renderedCell">The value to render, from the query results.</param>
 /// <param name="webNameRenderedCell"></param>
 /// <param name="learnerAssignmentGUID">The GUID of the current assignment</param>
 /// <param name="hw">The HtmlTextWriter to write to.</param>
 void RenderFileSubmissionCell(RenderedCell renderedCell, WebNameRenderedCell webNameRenderedCell, Guid learnerAssignmentGUID, HtmlTextWriter hw)
 {
     // This is a bit of a hack since the query returns an unlocalized string. Next time the schema is changed would be best
     // to change to return integer values representing these. No point making a sql change just for this though.
     if (renderedCell.ToString() == "Submit File(s)")
     {
         string baseUrl = "{0}{1}FilesUploadPage.aspx?LearnerAssignmentId={2}&Source={3}";
         // Creating a format string from a format string
         baseUrl = string.Format(CultureInfo.InvariantCulture, baseUrl, "{0}", Constants.SlkUrlPath, "{1}", RawSourceUrl);
         RenderFileSubmissionCellAsSubmitLink(baseUrl, webNameRenderedCell.SPWebUrl, learnerAssignmentGUID,
                                              PageCulture.Format(PageCulture.Resources.AlwpFileSubmissionSubmitText), hw);
     }
     else if (renderedCell.ToString() == "Submitted LINK")
     {
         RenderFileSubmissionCellAsSubmittedLink(
             "{0}" + Constants.SlkUrlPath + "SubmittedFiles.aspx?LearnerAssignmentId={1}",
             webNameRenderedCell,
             learnerAssignmentGUID,
             PageCulture.Format(PageCulture.Resources.LearnerAssignmentStatusCompleted),
             hw);
     }
     else if (renderedCell.ToString() == "Submitted")
     {
         hw.WriteEncodedText(PageCulture.Format(PageCulture.Resources.LearnerAssignmentStatusCompleted));
     }
     else if (renderedCell.ToString() == "Not Available")
     {
         hw.WriteEncodedText(PageCulture.Format(PageCulture.Resources.GradingFileSubmissionNA));
     }
     else
     {
         hw.WriteEncodedText(renderedCell.ToString());
     }
 }
        /// <summary>
        /// Renders The File Submission column cell as "Submitted" link
        /// </summary>
        /// <param name="fileURL">The URL of the file to be redirected to when the cell link is clicked</param>
        /// <param name="webNameRenderedCell"></param>
        /// <param name="learnerAssignmentGUID">The GUID of the current assignment</param>
        /// <param name="renderedCellValue">The text to be displayed in the cell</param>
        /// <param name="hw">The HtmlTextWriter to write to.</param>
        void RenderFileSubmissionCellAsSubmittedLink(string fileURL, WebNameRenderedCell webNameRenderedCell, Guid learnerAssignmentGUID,
                                                     string renderedCellValue, HtmlTextWriter hw)
        {
            // Optimise to always to to submitted files. The check involves
            // 1.   Loading the assignment properties which required opening the web.
            // 2.   Loading all the submitted files via drop box manager which required opening the web.
            // 3.   Then if there's only 1 of them, having a link direct to that.
            // That's a lot of work just to create a link.
            string url = string.Empty;

            //string url = CheckSubmittedFilesNumber(learnerAssignmentGUID);

            if (url.Equals(string.Empty))
            {
                StringBuilder pageURL = new StringBuilder();
                pageURL.AppendFormat(fileURL, webNameRenderedCell.SPWebUrl, learnerAssignmentGUID.ToString());

                url = pageURL.ToString();
            }

            StringBuilder anchorOnClick = new StringBuilder();

            anchorOnClick.AppendFormat("{0}{1}{2}", "window.open('", url,
                                       "','popupwindow','width=400,height=300,scrollbars,resizable');return false; ");

            hw.AddAttribute(HtmlTextWriterAttribute.Onclick, anchorOnClick.ToString());
            hw.AddAttribute(HtmlTextWriterAttribute.Href, url);

            using (new HtmlBlock(HtmlTextWriterTag.A, 0, hw))
            {
                hw.WriteEncodedText(renderedCellValue);
            }
        }
 /// <summary>
 /// Renders a column cell, i.e. one column of one row in the query results.
 /// </summary>
 /// <param name="renderedCell">The value to render, from the query results.</param>
 /// <param name="webNameRenderedCell">If the row containing this cell also contains a cell
 ///     of type <c>WebNameRenderedCell</c>, i.e. a cell referring to a SharePoint Web site,
 ///     this parameter refers to that cell.  Otherwise, this parameter is <c>null</c>.
 ///     </param>
 /// <param name="hw">The <c>HtmlTextWriter</c> to write to.</param>
 /// <param name="slkStore">The SlkStore to use to get assignment information from.</param>
 void RenderColumnCell(RenderedCell renderedCell, WebNameRenderedCell webNameRenderedCell, HtmlTextWriter hw, SlkStore slkStore)
 {
     // render the cell contents inside a "<span>" (not sure why SharePoint uses a "<span>"
     // here, but I'm copying what they do)
     if (renderedCell.ToolTip != null)
     {
         hw.AddAttribute(HtmlTextWriterAttribute.Title, renderedCell.ToolTip);
     }
     using (new HtmlBlock(HtmlTextWriterTag.Span, 0, hw))
     {
         if (webNameRenderedCell == renderedCell && webNameRenderedCell.RenderAsLink)
         {
             RenderCellWithLink(webNameRenderedCell, hw, renderedCell.ToString(), "#", "_parent");
         }
         else if (renderedCell.Id != null)
         {
             if (renderedCell.Id.ItemTypeName == Schema.AssignmentItem.ItemTypeName)
             {
                 // render a link to the Instructor Assignment Properties page
                 string url = "Grading.aspx?AssignmentId={0}";
                 RenderCellWithLink(webNameRenderedCell, hw, renderedCell.ToString(), url, "_parent", renderedCell.Id.GetKey().ToString(CultureInfo.InvariantCulture));
             }
             else if (renderedCell.Id.ItemTypeName == Schema.LearnerAssignmentItem.ItemTypeName)
             {
                 Guid learnerAssignmentGuidId = slkStore.GetLearnerAssignmentGuidId(renderedCell.Id);
                 if (IsObserver)
                 {
                     // Display this cell as an url and clicking this url will launch frameset in StudentReview mode
                     string url = "Frameset/Frameset.aspx?SlkView=StudentReview&{0}={1}";
                     RenderCellWithLink(webNameRenderedCell, hw, renderedCell.ToString(), url, "_blank", FramesetQueryParameter.LearnerAssignmentId, learnerAssignmentGuidId.ToString());
                 }
                 else
                 {
                     // render a link to the Learner Assignment Properties page
                     string url = "Lobby.aspx?{0}={1}&{2}={3}";
                     RenderCellWithLink(webNameRenderedCell, hw, renderedCell.ToString(), url, "_parent",
                                        FramesetQueryParameter.LearnerAssignmentId, learnerAssignmentGuidId.ToString(), QueryStringKeys.Source, RawSourceUrl);
                 }
             }
             else
             {
                 hw.WriteEncodedText(renderedCell.ToString());
             }
         }
         else
         {
             hw.WriteEncodedText(renderedCell.ToString());
         }
     }
 }
        private void RenderCellWithLink(WebNameRenderedCell webNameRenderedCell, HtmlTextWriter writer, string text, string url, string target, params string[] urlArguments)
        {
            if ((webNameRenderedCell != null) && (webNameRenderedCell.SPWebUrl != null))
            {
                if (url == "#")
                {
                    url = webNameRenderedCell.SPWebUrl;
                }
                else
                {
                    url = webNameRenderedCell.SPWebUrl + Constants.SlkUrlPath + url;
                }
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Target, target);
            writer.AddAttribute(HtmlTextWriterAttribute.Href, String.Format(CultureInfo.InvariantCulture, url, urlArguments));
            using (new HtmlBlock(HtmlTextWriterTag.A, 0, writer))
            {
                writer.WriteEncodedText(text);
            }
        }
        /// <summary>
        /// Renders the entire query results in the inner table.
        /// </summary>
        /// <param name="queryDef">The definition of the query that was executed.</param>
        /// <param name="renderedRows">Rendered query results.</param>
        /// <param name="hw">The <c>HtmlTextWriter</c> to write to.</param>
        ///
        void RenderQueryResults(QueryDefinition queryDef,
                                List <RenderedCell[]> renderedRows,
                                HtmlTextWriter hw)
        {
            // get the column definitions
            IList <ColumnDefinition> columnDefs = queryDef.Columns;

            //Get the column index to sort on and sort order
            int  sortColumnIndex;
            bool sortAscending = GetSortIndex(out sortColumnIndex);

            // sort <renderedRows> if so specified
            if ((sortColumnIndex >= 0) && (sortColumnIndex < queryDef.Columns.Count))
            {
                queryDef.SortRenderedRows(renderedRows, sortColumnIndex, sortAscending);
            }

            // render the "<table>" element and its contents
            hw.AddAttribute(HtmlTextWriterAttribute.Class, "ms-summarystandardbody");
            // skipped: id=TABLE1 dir=None
            hw.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            hw.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "1");
            hw.AddAttribute(HtmlTextWriterAttribute.Style, "width:100%;");
            hw.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            using (new HtmlBlock(HtmlTextWriterTag.Table, 1, hw))
            {
                // render the header row
                hw.AddAttribute(HtmlTextWriterAttribute.Class, "ms-viewheadertr");
                hw.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
                using (new HtmlBlock(HtmlTextWriterTag.Tr, 0, hw))
                {
                    // render the column headers
                    int columnIndex = 0;
                    foreach (ColumnDefinition columnDef in columnDefs)
                    {
                        bool?ascendingSort;
                        if (sortColumnIndex == columnIndex)
                        {
                            ascendingSort = sortAscending;
                        }
                        else
                        {
                            ascendingSort = null;
                        }
                        RenderColumnHeader(columnDef, columnIndex, ascendingSort, hw);

                        columnIndex++;
                    }
                }

                //If No Items Found
                if (renderedRows.Count == 0)
                {
                    using (new HtmlBlock(HtmlTextWriterTag.Tr, 1, hw))
                    {
                        hw.AddAttribute(HtmlTextWriterAttribute.Colspan,
                                        columnDefs.Count.ToString(CultureInfo.InvariantCulture));
                        using (new HtmlBlock(HtmlTextWriterTag.Td, 1, hw))
                        {
                            SlkError slkError
                                = new SlkError(ErrorType.Info,
                                               Constants.Space +
                                               Constants.Space +
                                               PageCulture.Resources.AlwpNoItemFound);
                            WebParts.ErrorBanner.RenderErrorItems(hw, slkError);
                        }
                    }
                }
                else
                {
                    // render the rows
                    int rowIndex = 0;
                    foreach (RenderedCell[] renderedRow in renderedRows)
                    {
                        // render the "<tr>"; note that every other row is shaded ("ms-alternating")
                        hw.AddAttribute(HtmlTextWriterAttribute.Class,
                                        (((rowIndex & 1) == 0) ? "ms-alternating" : ""));
                        using (new HtmlBlock(HtmlTextWriterTag.Tr, 1, hw))
                        {
                            // set <webNameRenderedCell> to any cell in the row which is of type
                            // WebNameRenderedCell, i.e. which refers to a SharePoint Web site,
                            // or null if none
                            WebNameRenderedCell webNameRenderedCell = null;
                            Guid learnerAssignmentGUID = Guid.Empty;
                            foreach (RenderedCell renderedCell in renderedRow)
                            {
                                if (webNameRenderedCell == null)
                                {
                                    webNameRenderedCell = renderedCell as WebNameRenderedCell;
                                }

                                if (learnerAssignmentGUID == Guid.Empty)
                                {
                                    if (renderedCell.Id != null)
                                    {
                                        if (renderedCell.Id.ItemTypeName == Schema.LearnerAssignmentItem.ItemTypeName)
                                        {
                                            learnerAssignmentGUID = SlkStore.GetLearnerAssignmentGuidId(renderedCell.Id);
                                        }
                                    }
                                }

                                if (webNameRenderedCell != null && learnerAssignmentGUID != Guid.Empty)
                                {
                                    break;
                                }
                            }

                            // render the cells in this row
                            int columnIndex = 0;
                            foreach (RenderedCell renderedCell in renderedRow)
                            {
                                ColumnDefinition columnDef = columnDefs[columnIndex];
                                hw.AddAttribute(HtmlTextWriterAttribute.Class, "ms-vb2");
                                if (!columnDef.Wrap)
                                {
                                    hw.AddAttribute(HtmlTextWriterAttribute.Nowrap, "true");
                                }
                                using (new HtmlBlock(HtmlTextWriterTag.Td, 1, hw))
                                {
                                    if (columnDef.Title.Equals(PageCulture.Resources.AlwpFileSubmissionColumnTitle))
                                    {
                                        RenderFileSubmissionCell(renderedCell, webNameRenderedCell, learnerAssignmentGUID, hw);
                                    }
                                    else
                                    {
                                        RenderColumnCell(renderedCell, webNameRenderedCell, hw, SlkStore);
                                    }
                                }

                                columnIndex++;
                            }
                        }
                        rowIndex++;
                    }
                }
            }
        }