/// <summary> /// Gets the renderer that can render an assessmentItem. /// </summary> /// <param name="assessmentItem">The assessment item to render. This is the <img> element that is in /// the original LRM content.</param> /// <remarks>RenderContext must have been set prior to calling this method.</remarks> internal AssessmentItemRenderer GetRenderer(AssessmentItem assessmentItem) { AssessmentItemRenderer renderer = m_renderers[assessmentItem.Type]; renderer.AssessmentItem = assessmentItem; return renderer; }
/// <summary> /// Use an assessmentItem from an LRM page.htm to create an AssessmentItem object. /// </summary> /// <param name="assessmentItem">The src attribute value of the img tag that corresponds to the assessment item.</param> /// <returns>The object representing the parsed value.</returns> /// <remarks> /// If more than one of the same name/value pairs exists with the same name, only the first is used. /// </remarks> /// <exception cref="FormatException">Thrown if the <paramref name="assessmentItem"/> cannot be parsed.</exception> public static AssessmentItem Parse(string assessmentItem) { AIResources.Culture = LocalizationManager.GetCurrentCulture(); // assessmentItem contains the value of the src attribute, which is of the form: // "http://localhost:3535/mslamrk,type=1,id=3,cols=10,akey=over,uak=0". // Split this string by the ',' character. string[] commaSplit = assessmentItem.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); Dictionary<string, string> parsedValues = new Dictionary<string, string>(10); foreach (string pair in commaSplit) { string[] equalSplit = pair.Split(new char[] { '=' }, 2, StringSplitOptions.None); if (equalSplit.Length == 2) // gets rid of the "protocol://server/mslamrk" portion, and any non-value markup { // convert id to all upper case string id = equalSplit[0].ToUpperInvariant(); if (!parsedValues.ContainsKey(id)) { parsedValues.Add(id, equalSplit[1]); } } } // if (id contains invalid values per LRDG) throw FormatException. id is required. if (!parsedValues.ContainsKey(AiStrings.Id) || !IsLegalId(parsedValues[AiStrings.Id])) { throw new FormatException(String.Format(CultureInfo.InvariantCulture, AIResources.InvalidId, assessmentItem)); } // if (there is no type value) throw FormatException. see switch statement below. if (!parsedValues.ContainsKey(AiStrings.Type)) { throw new FormatException(String.Format(CultureInfo.InvariantCulture, AIResources.InvalidType, assessmentItem)); } // if other values are not correct (eg, rows is not numeric) then just ignore the invalid values // and 'be nice' about returning defaults that are appropriate for the type. AssessmentItem item = new AssessmentItem(); item.m_id = parsedValues[AiStrings.Id]; // id is valid because of above check switch(parsedValues[AiStrings.Type]) // type is valid because of above check { default: // If the type is not one of the cases, it is an invalid value. throw new FormatException(String.Format(CultureInfo.InvariantCulture, AIResources.InvalidType, assessmentItem)); case "1": item.m_type = AssessmentItemType.Text; item.m_cols = GetCols(parsedValues); if (parsedValues.ContainsKey(AiStrings.Akey)) // optional argument { item.m_akey = LrdgDecodeString(parsedValues[AiStrings.Akey]); // optional argument // according the LRDG, uak can be "0" or "1", and if it exists then // akey must exist. Here, if uak exists but akey doesn't, uak is never // parsed. Also, if uak exists but is anything other than "0" it is // assumed to be "1" by default. if (parsedValues.ContainsKey(AiStrings.Uak)) { if (parsedValues[AiStrings.Uak] == "0") item.m_uak = false; else item.m_uak = true; } } else { item.m_akey = String.Empty; } break; case "2": item.m_type = AssessmentItemType.TextArea; item.m_cols = GetCols(parsedValues); int rows; if (parsedValues.ContainsKey(AiStrings.Rows) && Int32.TryParse(parsedValues[AiStrings.Rows], NumberStyles.Integer, CultureInfo.InvariantCulture, out rows)) { item.m_rows = rows; } else item.m_rows = 5; break; case "3": item.m_type = AssessmentItemType.Radio; item.m_maxPts = GetMaxPts(parsedValues); break; case "4": item.m_type = AssessmentItemType.Checkbox; item.m_maxPts = GetMaxPts(parsedValues); break; case "5": item.m_type = AssessmentItemType.ItemScore; item.m_maxPts = GetMaxPts(parsedValues); break; case "7": item.m_type = AssessmentItemType.Select; if (parsedValues.ContainsKey(AiStrings.Text)) { item.m_text = LrdgDecodeString(parsedValues[AiStrings.Text]); } else { item.m_text = String.Empty; } break; case "8": item.m_type = AssessmentItemType.Rubric; item.m_maxPts = GetMaxPts(parsedValues); break; case "9": item.m_type = AssessmentItemType.File; item.m_cols = GetCols(parsedValues); break; } return item; }