Skip to content

Commit

Permalink
SAK-33349 Fix prerequisite calculations for the lessons subnav to use…
Browse files Browse the repository at this point in the history
… the LessonsAccess API and take into account all lesson item types (sakaiproject#4805)
  • Loading branch information
payten authored and ottenhoff committed Sep 22, 2017
1 parent a290cf9 commit 79e87ab
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,7 @@ public class PageData {

public boolean doesPageFolderExist(final String siteId, final String folder);

public String getLessonSubPageJSON(String userId, boolean isInstructor, List pages);
public String getLessonSubPageJSON(String userId, boolean isInstructor, String siteId, List pages);

public List<SimplePage> getTopLevelPages(String siteId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,14 @@ public List<SimplePageItem> findAllChecklistsInSite(String siteId) {
}


public String getLessonSubPageJSON(final String userId, final boolean isInstructor, final List pages) {
public String getLessonSubPageJSON(final String userId, final boolean isInstructor, final String siteId, final List pages) {
final List<String> pageIds = LessonsSubNavBuilder.collectPageIds(pages);

if (pageIds.isEmpty()) {
// no lesson pages, so no JSON!
return null;
}

final String sql = ("SELECT p.toolId AS sakaiPageId," +
" p.pageId AS lessonsPageId," +
" s.site_id AS sakaiSiteId," +
Expand All @@ -1833,18 +1840,17 @@ public String getLessonSubPageJSON(final String userId, final boolean isInstruct
" LEFT OUTER JOIN lesson_builder_log log" +
" ON (log.itemId = i.id AND log.userId = ?)" +
" WHERE p.parent IS NULL" +
" AND p.toolId IN (" + pages.stream().map(i -> "?").collect(Collectors.joining(",")) + ")" +
" AND p.toolId IN (" + pageIds.stream().map(i -> "?").collect(Collectors.joining(",")) + ")" +
" ORDER BY i.sequence");

final Object [] fields = new Object[pages.size() + 1];
final Object [] fields = new Object[pageIds.size() + 1];
fields[0] = userId;

final List<String> pageIds = LessonsSubNavBuilder.collectPageIds(pages);
for (int i=0; i<pageIds.size(); i++) {
fields[i+1] = pageIds.get(i);
}

final LessonsSubNavBuilder lessonsSubNavBuilder = new LessonsSubNavBuilder(isInstructor);
final LessonsSubNavBuilder lessonsSubNavBuilder = new LessonsSubNavBuilder(siteId, isInstructor);

sqlService.dbRead(sql, fields, new SqlReader() {
public Object readSqlResultRecord(final ResultSet result) {
Expand All @@ -1858,4 +1864,20 @@ public Object readSqlResultRecord(final ResultSet result) {

return lessonsSubNavBuilder.toJSON();
}

public List<SimplePage> getTopLevelPages(final String siteId) {
DetachedCriteria d = DetachedCriteria.forClass(SimplePage.class).add(Restrictions.eq("siteId", siteId))
.add(Restrictions.disjunction()
.add(Restrictions.isNull("owner"))
.add(Restrictions.eq("owned", true)))
.add(Restrictions.isNull("parent"));

List<SimplePage> l = (List<SimplePage>) getHibernateTemplate().findByCriteria(d);

if (l != null && l.size() > 0) {
return l;
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ public class LessonsSubNavBuilder {

private static ResourceLoader rb = new ResourceLoader("subnav");

private String siteId;
private boolean isInstructor;
private Map<String, ArrayList<Map<String, String>>> subnavData;

public LessonsSubNavBuilder(final boolean isInstructor) {
public LessonsSubNavBuilder(final String siteId, final boolean isInstructor) {
this.siteId = siteId;
this.isInstructor = isInstructor;
this.subnavData = new HashMap<>();
}
Expand All @@ -59,6 +61,8 @@ public String toJSON() {
final Map<String, Object> objectToSerialize = new HashMap<>();
objectToSerialize.put("pages", this.subnavData);
objectToSerialize.put("i18n", getI18n());
objectToSerialize.put("siteId", this.siteId);
objectToSerialize.put("isInstructor", this.isInstructor);

return JSONObject.toJSONString(objectToSerialize);
}
Expand All @@ -69,7 +73,10 @@ public static List<String> collectPageIds(final List pages) {
final List<String> pageIds = new ArrayList<>(typedPages.size());

for (Map<String, String> page : typedPages) {
pageIds.add(page.get("pageId"));
// try to limit to only lesson pages
if (!page.containsKey("wellKnownToolId") || "sakai.lessonbuildertool".equals(page.get("wellKnownToolId"))) {
pageIds.add(page.get("pageId"));
}
}

return pageIds;
Expand All @@ -91,6 +98,7 @@ public Map<String, String> processResult(final ResultSet rs) throws SQLException

subnavItem.put("toolId", rs.getString("sakaiToolId"));
subnavItem.put("siteId", rs.getString("sakaiSiteId"));
subnavItem.put("sakaiPageId", rs.getString("sakaiPageId"));
subnavItem.put("itemId", rs.getString("itemId"));
subnavItem.put("sendingPage", rs.getString("itemSakaiId"));
subnavItem.put("name", rs.getString("itemName"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import lombok.Setter;

import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -411,6 +413,52 @@ public Object getLesson(EntityView view, Map<String, Object> params) {
return ret.get(0);
return ret;
}


/**
* subnav-prerequisites
* example: /direct/lessons/subnav-prerequisites/SITEID.xml
*/
@EntityCustomAction(action = "subnav-prerequisites", viewKey = EntityView.VIEW_LIST)
public String getPrerequisiteDataForSubNav(final EntityView view, final Map<String, Object> params) {
final String siteId = view.getPathSegment(2);

if (siteId == null || "".equals(siteId)) {
throw new IllegalArgumentException("siteId is required");
}
Site site = null;
try {
site = siteService.getSiteVisit(siteId);
} catch (IdUnusedException e) {
throw new EntityNotFoundException("Invalid siteId: " + siteId, siteId);
} catch (PermissionException e) {
throw new EntityNotFoundException("No access to site: " + siteId, siteId);
}

setToolSession(site);

final JSONObject result = new JSONObject();

final List<SimplePage> topLevelPages = simplePageToolDao.getTopLevelPages(siteId);
SimplePageBean simplePageBean = null;
final String currentUserId = sessionManager.getCurrentSessionUserId();

for (SimplePage topLevelPage : topLevelPages) {
final List<SimplePageItem> itemsOnPage = simplePageToolDao.findItemsOnPage(topLevelPage.getPageId());
final JSONArray inaccessibleItems = new JSONArray();

for (SimplePageItem item : itemsOnPage) {
simplePageBean = makeSimplePageBean(simplePageBean, siteId, item);
if (!lessonsAccess.isItemAccessible(item.getId(), siteId, currentUserId, simplePageBean)) {
inaccessibleItems.add(String.valueOf(item.getId()));
}
}

result.put(topLevelPage.getToolId(), inaccessibleItems);
}

return result.toJSONString();
}


@Override
Expand Down
63 changes: 50 additions & 13 deletions library/src/webapp/js/lessons-subnav.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@

this.data = data.pages;
this.i18n = data.i18n;
this.siteId = data.siteId;
this.isInstructor = data.isInstructor;

this.has_current = false;
this.setup();
this.setupPrerequisiteCallback();
};

LessonsSubPageNavigation.prototype.setup = function() {
Expand Down Expand Up @@ -71,17 +75,7 @@
}
}

if (sub_page.disabledDueToPrerequisite == 'true') {
$submenu_action.classList.add('has-prerequisite');
if (sub_page.disabled == 'true') {
$submenu_action.classList.add('disabled');
$submenu_action.setAttribute('href', 'javascript:void(0);')
title_string += ' ' + self.i18n.prerequisite_and_disabled;
} else {
title_string += ' ' + self.i18n.prerequisite;
}

} else if(sub_page.required == 'true') {
if(sub_page.required == 'true') {
if (sub_page.completed == 'false') {
$submenu_action.classList.add('is-required');
} else {
Expand All @@ -107,7 +101,7 @@
$expandMe.hide().show(0);
$expandMe.addClass('sliding-down');
$expandMe.find('.lessons-sub-page-menu').slideDown((doNotAnimate == true) ? 0 : 500, function() {
var $submenu = $(this);
var $submenu = $PBJQ(this);

$expandMe.removeClass('sliding-down');
$expandMe.addClass('expanded');
Expand Down Expand Up @@ -137,7 +131,7 @@
LessonsSubPageNavigation.prototype.collapse = function($collapseMe, callback) {
$collapseMe.addClass('sliding-up');
$collapseMe.find('.lessons-sub-page-menu').slideUp(500, function() {
var $submenu = $(this);
var $submenu = $PBJQ(this);

$collapseMe.removeClass('sliding-up');
$collapseMe.removeClass('expanded');
Expand Down Expand Up @@ -383,5 +377,48 @@
};


LessonsSubPageNavigation.prototype.setupPrerequisiteCallback = function() {
var self = this;

document.addEventListener("DOMContentLoaded", function(event) {
$PBJQ.ajax({
url: '/direct/lessons/subnav-prerequisites/' + self.siteId + '.json',
cache: false,
dataType: 'json',
success: function(json) {
self.applyPrerequisites(json);
}
});
});
};


LessonsSubPageNavigation.prototype.applyPrerequisites = function(prereqData) {
var self = this;

for (var page_id in self.data) {
if (self.data.hasOwnProperty(page_id)) {
var sub_pages = self.data[page_id];
sub_pages.forEach(function(sub_page) {
if (prereqData.hasOwnProperty(sub_page.sakaiPageId)) {
if (sub_page.prerequisite == 'true' && $PBJQ.inArray(sub_page.itemId, prereqData[sub_page.sakaiPageId]) >= 0) {
var $link = $PBJQ(sub_page.submenu_item).find('> a');
$link.addClass('has-prerequisite');
var title_string = $link.attr('title');
if (self.isInstructor) {
title_string += ' ' + self.i18n.prerequisite;
} else {
$link.addClass('disabled');
$link.attr('href', 'javascript:void(0);')
title_string += ' ' + self.i18n.prerequisite_and_disabled;
}
$link.attr('title', title_string);
}
}
});
}
}
};

window.LessonsSubPageNavigation = LessonsSubPageNavigation;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ else if ("always".equals(showHelpGlobal))
if (includeSummary) summarizePage(m, site, p);
if (firstTool != null)
{
m.put("wellKnownToolId", firstTool.getToolId());
String menuClass = firstTool.getToolId();
menuClass = ICON_SAKAI + menuClass.replace('.', '-');
m.put("menuClass", menuClass);
Expand Down Expand Up @@ -812,6 +813,7 @@ else if ("always".equals(showHelpGlobal))
m.put("toolrefUrl", toolrefUrl);
m.put("toolpopup", Boolean.valueOf(source!=null));
m.put("toolpopupurl", source);
m.put("wellKnownToolId", placement.getToolId());
String menuClass = placement.getToolId();
menuClass = ICON_SAKAI + menuClass.replace('.', '-');
m.put("menuClass", menuClass);
Expand Down Expand Up @@ -843,7 +845,8 @@ else if ("always".equals(showHelpGlobal))
theMap.put("pageNavTools", l);

if ("true".equals(site.getProperties().getProperty("lessons_submenu")) && !l.isEmpty()) {
theMap.put("additionalLessonsPages", getSimplePageToolDao().getLessonSubPageJSON(UserDirectoryService.getCurrentUser().getId(), siteUpdate, l));
theMap.put("additionalLessonsPages",
getSimplePageToolDao().getLessonSubPageJSON(UserDirectoryService.getCurrentUser().getId(), siteUpdate, site.getId(), l));
}

theMap.put("pageNavTools", l);
Expand Down

0 comments on commit 79e87ab

Please sign in to comment.