Skip to content

Commit

Permalink
SAK-49641 Lessons support the Canvas module_meta.xml new_tab feature (s…
Browse files Browse the repository at this point in the history
…akaiproject#12275)

Co-authored-by: Earle Nietzel <[email protected]>
  • Loading branch information
csev and ern authored Mar 26, 2024
1 parent 40e7549 commit bcad32b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public class Parser extends AbstractParser {
private static final String CC_RESOURCES="resources";
private static final String CC_RES_TYPE="type";

private static final String CANVAS_MODULE_META = "course_settings/module_meta.xml";
private
Parser(CartridgeLoader the_cu) {
super();
Expand All @@ -131,17 +132,26 @@ public class Parser extends AbstractParser {

public void
parse(DefaultHandler the_handler) throws FileNotFoundException, ParseException {
Element canvas_module_meta = null;
try {
canvas_module_meta = this.getXML(utils, CANVAS_MODULE_META);
log.debug("Found - {} {}", CANVAS_MODULE_META, canvas_module_meta);
} catch (Exception e) {
log.debug("{} not found, {}", CANVAS_MODULE_META, e.toString());
}

try {
Element manifest=this.getXML(utils, IMS_MANIFEST);
processManifest(manifest, the_handler);
processManifest(manifest, canvas_module_meta, the_handler);
} catch (Exception e) {
the_handler.getSimplePageBean().setErrKey("simplepage.cc-error", "");
log.info("parse error, stack trace follows " + e);
}
}

private void
processManifest(Element the_manifest, DefaultHandler the_handler) throws ParseException {
processManifest(Element the_manifest, Element the_canvas_module_meta, DefaultHandler the_handler) throws ParseException {
log.debug("the_manifest {} the_canvas_module_meta {} the_handler", the_manifest, the_canvas_module_meta, the_handler);
ns = new Ns();
the_handler.setNs(ns);
// figure out which version we have, and set up ns to know about it
Expand Down Expand Up @@ -170,6 +180,9 @@ else if (the_manifest.getNamespace().equals(ns.cp_ns())) {

the_handler.startManifest();
the_handler.setManifestXml(the_manifest);
if ( the_canvas_module_meta != null && the_handler instanceof org.sakaiproject.lessonbuildertool.cc.PrintHandler) {
((org.sakaiproject.lessonbuildertool.cc.PrintHandler)the_handler).setCanvasModuleMetaXml(the_canvas_module_meta);
}
if (processAuthorization(the_manifest, the_handler))
return; // don't process CCs with authorization
processManifestMetadata(the_manifest, the_handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* limitations under the Licensee.
*/
package org.sakaiproject.lessonbuildertool.cc;

Expand Down Expand Up @@ -79,8 +79,11 @@
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.filter.ElementFilter;
import org.jdom2.filter.Filters;
import org.jdom2.output.DOMOutputter;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;

import org.jsoup.Jsoup;

Expand Down Expand Up @@ -158,6 +161,9 @@ public class PrintHandler extends DefaultHandler implements AssessmentHandler, D
private static final String BLTI="basiclti";
private static final String UNKNOWN="unknown";
private static final int MAX_ATTEMPTS = 100;
private static final String CANVAS_MODULE_META_NAMESPACE = "http://canvas.instructure.com/xsd/cccv1p0";
private static final String CANVAS_MODULE_NEW_TAB = "new_tab";
private static final String CANVAS_MODULE_NEW_TAB_FALSE = "false";

private List<SimplePage> pages = new ArrayList<SimplePage>();
// list parallel to pages containing sequence of last item on the page
Expand All @@ -181,6 +187,7 @@ public class PrintHandler extends DefaultHandler implements AssessmentHandler, D
boolean importtop = false;
Integer assignmentNumber = 1;
Element manifestXml = null;
Element canvasModuleMeta = null;
boolean forceInline;

// this is the CC file name for all files added
Expand Down Expand Up @@ -552,12 +559,14 @@ public void setCCItemXml(Element itemXml, Element resourceXml, AbstractParser pa

int seq = (isBank || noPage) ? 0 : sequences.get(top);
String title;
String identifier = null;
String identifierRef = null;

if (itemXml == null)
title = "Question Pool";
else {
String identifier = itemXml.getAttributeValue(IDENTIFIER);
String identifierRef = itemXml.getAttributeValue(IDENTIFIERREF);
identifier = itemXml.getAttributeValue(IDENTIFIER);
identifierRef = itemXml.getAttributeValue(IDENTIFIERREF);
title = itemXml.getChildText(CC_ITEM_TITLE, ns.getNs());
// metadata is used for special Sakai data
String id = StringUtils.defaultIfBlank(identifierRef, identifier); // prefer identifierref if exists on item
Expand All @@ -569,6 +578,28 @@ public void setCCItemXml(Element itemXml, Element resourceXml, AbstractParser pa
boolean inline = false;
String mmDisplayType = null;
String resourceId = resourceXml.getAttributeValue(IDENTIFIER);
log.debug("identifier {} identifierRef {} resourceId {} resourceXml {}", identifier, identifierRef, resourceId, resourceXml);

// Check if there is any Canvas module metadata in the import
Element canvas_module_item = null;
boolean open_same_window = true;
if ( identifier != null && canvasModuleMeta != null ) {

Namespace namespace = Namespace.getNamespace("cccv1p0", CANVAS_MODULE_META_NAMESPACE);

String xpathExpression = "//cccv1p0:item[@identifier='" + identifier + "']";

List<Element> items = XPathFactory.instance().compile(xpathExpression, Filters.element(), null, namespace).evaluate(canvasModuleMeta);

log.debug("Found {} items whilst looking for {}", items.size(), xpathExpression);
if ( items.size() >= 1 ) {
canvas_module_item = items.get(0);
String new_tab = canvas_module_item.getChildText(CANVAS_MODULE_NEW_TAB, namespace);
open_same_window = CANVAS_MODULE_NEW_TAB_FALSE.equals(new_tab);
log.debug("new_tab {} open_same_window {}", new_tab, open_same_window);
}
}

Map<String, String> itemsMetaData = itemsMetaDataAdded.get(resourceId);
if (itemsMetaData != null) {
inline = BooleanUtils.toBoolean(itemsMetaData.get("inline.lessonbuilder.sakaiproject.org"));
Expand All @@ -594,7 +625,7 @@ public void setCCItemXml(Element itemXml, Element resourceXml, AbstractParser pa
String intendedUse = resourceXml.getAttributeValue(INTENDEDUSE);
SimplePageItem item = simplePageToolDao.makeItem(page.getPageId(), seq, SimplePageItem.RESOURCE, sakaiId, title);
item.setHtml(mime);
item.setSameWindow(true);
item.setSameWindow(open_same_window);

// only text type files can be inlined, see addFile
if ((inline || forceInline) && StringUtils.startsWith(mime, "text/")) {
Expand Down Expand Up @@ -726,7 +757,7 @@ else if (assigntool != null && intendedUse.equals("assignment")) {
item.setType(SimplePageItem.MULTIMEDIA);
} else {
item.setHtml(simplePageBean.getTypeOfUrl(url)); // checks the web site to see what it actually is
item.setSameWindow(true);
item.setSameWindow(open_same_window);
}
simplePageBean.saveItem(item);
if (!roles.isEmpty()) simplePageBean.setItemGroups(item, roles.toArray(new String[0]));
Expand Down Expand Up @@ -931,7 +962,7 @@ else if (assigntool != null && intendedUse.equals("assignment")) {

String sakaiId = null;
if (bltitool != null) {
sakaiId = ((BltiInterface) bltitool).doImportTool(launchUrl, bltiTitle, strXml, custom);
sakaiId = ((BltiInterface) bltitool).doImportTool(launchUrl, bltiTitle, strXml, custom, open_same_window);
}

if (sakaiId != null) {
Expand Down Expand Up @@ -1089,9 +1120,12 @@ public void checkCurriculum(Element the_xml) {

public void setManifestXml(Element the_xml) {
manifestXml = the_xml;
log.debug("manifest xml: {}", the_xml);
}

log.debug("manifest xml: "+the_xml);

public void setCanvasModuleMetaXml(Element the_xml) {
canvasModuleMeta = the_xml;
log.debug("canvas_meta_module xml: {}", the_xml);
}

public void endManifest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public void setGroups(Collection<String> groups) {
// not group aware
}

public String doImportTool(String launchUrl, String bltiTitle, String strXml, String custom)
public String doImportTool(String launchUrl, String bltiTitle, String strXml, String custom, boolean open_same_window)
{
if ( ltiService == null ) return null;

Expand All @@ -536,6 +536,7 @@ public String doImportTool(String launchUrl, String bltiTitle, String strXml, St
props.setProperty(LTIService.LTI_ALLOWOUTCOMES, "1");
props.setProperty(LTIService.LTI_SENDNAME, "1");
props.setProperty(LTIService.LTI_SENDEMAILADDR, "1");
props.setProperty(LTIService.LTI_NEWPAGE, (open_same_window ? "0" : "1"));
props.setProperty(LTIService.LTI_XMLIMPORT,strXml);
if (custom != null)
props.setProperty(LTIService.LTI_CUSTOM, custom);
Expand All @@ -554,6 +555,7 @@ public String doImportTool(String launchUrl, String bltiTitle, String strXml, St
props.setProperty(LTIService.LTI_TITLE, bltiTitle);
props.setProperty( LTI_PAGETITLE, bltiTitle);
props.setProperty(LTIService.LTI_LAUNCH,launchUrl);
props.setProperty(LTIService.LTI_NEWPAGE, (open_same_window ? "0" : "1"));
props.setProperty(LTIService.LTI_XMLIMPORT,strXml);
if ( custom != null ) props.setProperty(LTIService.LTI_CUSTOM,custom);
Object result = ltiService.insertContent(props, simplePageBean.getCurrentSiteId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface BltiInterface {
public boolean servicePresent();
public boolean isPopUp();
public int frameSize();
public String doImportTool(String launchUrl, String bltiTitle, String strXml, String custom);
public String doImportTool(String launchUrl, String bltiTitle, String strXml, String custom, boolean open_same_window);
public String getIcon();
}

Original file line number Diff line number Diff line change
Expand Up @@ -7103,6 +7103,7 @@ private long importCcFromStream(InputStream fis) {
try {
cc = File.createTempFile("ccloader", "file");
root = File.createTempFile("ccloader", "root");
log.debug("cc = {} root={}", cc.getAbsoluteFile(), root.getAbsoluteFile());
if (root.exists()) {
if (!root.delete()) {
setErrMessage("unable to delete temp file for load");
Expand Down Expand Up @@ -7150,6 +7151,7 @@ private long importCcFromStream(InputStream fis) {
topicobject = q;
}

log.debug("parser.parse {} {} {} {} {} {} {}", this, cartridgeLoader, simplePageToolDao, quizobject, topicobject, bltiEntity, assignobject);
parser.parse(new PrintHandler(this, cartridgeLoader, simplePageToolDao, quizobject, topicobject, bltiEntity, assignobject, importtop));
} catch (Exception e) {
setErrKey("simplepage.cc-error", "");
Expand Down

0 comments on commit bcad32b

Please sign in to comment.