示例#1
0
  public void fillComponents(
      UIContainer tofill, ViewParameters viewparams, ComponentChecker checker) {
    if (((GeneralViewParameters) viewparams).getSendingPage() != -1) {
      // will fail if page not in this site
      // security then depends upon making sure that we only deal with this page
      try {
        simplePageBean.updatePageObject(((GeneralViewParameters) viewparams).getSendingPage());
      } catch (Exception e) {
        System.out.println("PagePicker permission exception " + e);
        return;
      }
    }

    UIOutput.make(tofill, "html")
        .decorate(new UIFreeAttributeDecorator("lang", localeGetter.get().getLanguage()))
        .decorate(new UIFreeAttributeDecorator("xml:lang", localeGetter.get().getLanguage()));

    boolean canEditPage = (simplePageBean.getEditPrivs() == 0);

    String source = ((GeneralViewParameters) viewparams).getSource();
    // summaryPage is the "index of pages". It has status icons and links, but isn't a chooser
    // otherwise we have the chooser page for the "add subpage" command
    boolean summaryPage = "summary".equals(source);

    if (summaryPage) {
      GeneralViewParameters view = new GeneralViewParameters(ShowPageProducer.VIEW_ID);
      // path defaults to null, which is next
      UIOutput.make(tofill, "return-div");
      UIInternalLink.make(tofill, "return", messageLocator.getMessage("simplepage.return"), view);
      UIOutput.make(tofill, "title", messageLocator.getMessage("simplepage.page.index"));
    } else {
      UIOutput.make(tofill, "title", messageLocator.getMessage("simplepage.page.chooser"));
    }

    // Explain which pages may be deleted
    if (summaryPage && canEditPage) {
      UIOutput.make(tofill, "deleteAlert");
    }

    // this looks at pages in the site, which should be safe
    // but need to make sure the item we update is legal

    Long itemId = ((GeneralViewParameters) viewparams).getItemId();

    simplePageBean.setItemId(itemId);

    SimplePage page = simplePageBean.getCurrentPage();
    currentPageId = page.getPageId();

    if (itemId != null && itemId != -1) {
      SimplePageItem currentItem = simplePageToolDao.findItem(itemId);
      if (currentItem == null) return;
      // trying to hack on item not on this page
      if (currentItem.getPageId() != page.getPageId()) return;
    }

    // list we're going to display
    List<PageEntry> entries = new ArrayList<PageEntry>();

    // build map of all pages, so we can see if any are left over
    Map<Long, SimplePage> pageMap = new HashMap<Long, SimplePage>();

    Set<Long> sharedPages = new HashSet<Long>();

    // all pages
    List<SimplePage> pages = simplePageToolDao.getSitePages(simplePageBean.getCurrentSiteId());
    for (SimplePage p : pages) pageMap.put(p.getPageId(), p);

    // set of all top level pages, actually the items pointing to them
    List<SimplePageItem> sitePages =
        simplePageToolDao.findItemsInSite(toolManager.getCurrentPlacement().getContext());
    Set<Long> topLevelPages = new HashSet<Long>();
    for (SimplePageItem i : sitePages) topLevelPages.add(Long.valueOf(i.getSakaiId()));

    // this adds everything you can find from top level pages to entries. But make sure user can see
    // the tool
    for (SimplePageItem sitePageItem : sitePages) {
      // System.out.println("findallpages " + sitePageItem.getName() + " " + true);
      findAllPages(
          sitePageItem, entries, pageMap, topLevelPages, sharedPages, 0, true, canEditPage);
    }

    // warn students if we aren't showing all the pages
    if (!canEditPage && somePagesHavePrerequisites) UIOutput.make(tofill, "onlyseen");

    // now add everything we didn't find that way
    if (canEditPage && pageMap.size() > 0) {
      // marker
      PageEntry marker = new PageEntry();
      marker.level = -1;
      entries.add(marker);
      for (SimplePage p : pageMap.values()) {
        if (p.getOwner() == null) {
          PageEntry entry = new PageEntry();
          entry.pageId = p.getPageId();
          entry.itemId = null;
          entry.title = p.getTitle();
          entry.level = 0;

          // TopLevel determines if we can select the page.
          // Since this means that the page is detached, it isn't
          // a conflict to be able to select it.
          entry.toplevel = false;

          entries.add(entry);
        }
      }
    }

    if (canEditPage && sharedPages.size() > 0) UIOutput.make(tofill, "sharedpageexplanation");

    UIForm form = UIForm.make(tofill, "page-picker");
    Object sessionToken = SessionManager.getCurrentSession().getAttribute("sakai.csrf.token");
    if (sessionToken != null)
      UIInput.make(form, "csrf", "simplePageBean.csrfToken", sessionToken.toString());

    ArrayList<String> values = new ArrayList<String>();
    ArrayList<String> initValues = new ArrayList<String>();
    for (PageEntry entry : entries) {
      if (entry.level >= 0) {
        values.add(entry.pageId.toString());
        initValues.add("");
      }
    }

    UISelect select = null;
    if (summaryPage)
      select =
          UISelect.makeMultiple(
              form,
              "page-span",
              values.toArray(new String[1]),
              "#{simplePageBean.selectedEntities}",
              initValues.toArray(new String[1]));
    else
      select =
          UISelect.make(
              form,
              "page-span",
              values.toArray(new String[1]),
              "#{simplePageBean.selectedEntity}",
              null);

    int index = 0;
    boolean showDeleteButton = false;

    for (PageEntry entry : entries) {

      UIBranchContainer row = UIBranchContainer.make(form, "page:");
      if (entry.toplevel)
        row.decorate(
            new UIFreeAttributeDecorator("style", "list-style-type:none; margin-top:0.5em"));

      if (entry.level < 0) {
        UIOutput.make(row, "heading", messageLocator.getMessage("simplepage.chooser.unused"));
        if (summaryPage) UIOutput.make(row, "chooseall");
      }
      // if no itemid, it's unused. Only canedit people will see it
      else if (summaryPage && entry.itemId != null) {
        int level = entry.level;
        if (level > 5) level = 5;
        String imagePath = "/lessonbuilder-tool/images/";
        SimplePageItem item = simplePageBean.findItem(entry.itemId);
        SimplePageLogEntry logEntry = simplePageBean.getLogEntry(entry.itemId);
        String note = null;
        if (logEntry != null && logEntry.isComplete()) {
          imagePath += "checkmark.png";
          note = messageLocator.getMessage("simplepage.status.completed");
        } else if (logEntry != null && !logEntry.getDummy()) {
          imagePath += "hourglass.png";
          note = messageLocator.getMessage("simplepage.status.inprogress");
        } else if (!canEditPage && somePagesHavePrerequisites) {
          // it's too complex to compute prerequisites for all pages, and
          // I'm concerned that faculty may not be careful in setting them
          // for pages that would normally not be accessible. So if there are
          // any prerequisites in the site, only show pages that are
          // in progress or done.
          continue;
        } else {
          imagePath += "not-required.png";
        }
        UIOutput.make(row, "status-image").decorate(new UIFreeAttributeDecorator("src", imagePath));
        GeneralViewParameters p = new GeneralViewParameters(ShowPageProducer.VIEW_ID);
        p.setSendingPage(entry.pageId);
        p.setItemId(entry.itemId);
        // reset the path to the saved one
        p.setPath("log");
        UIInternalLink.make(row, "link", p)
            .decorate(new UIFreeAttributeDecorator("style", "padding-left: " + (2 * level) + "em"));
        String levelstr = null;
        if (level > 0)
          levelstr =
              messageLocator
                  .getMessage("simplepage.status.level")
                  .replace("{}", Integer.toString(level));
        if (levelstr != null) {
          if (note != null) note = levelstr + " " + note;
          else note = levelstr;
        }
        if (note != null) UIOutput.make(row, "link-note", note + " ");
        UIOutput.make(row, "link-text", entry.title);

        if (enableShowItems) {
          UIOutput.make(row, "item-list-toggle");
          UIOutput.make(row, "itemListContainer")
              .decorate(
                  new UIFreeAttributeDecorator("style", "margin-left: " + (3 * level) + "em"));
          UIOutput.make(row, "itemList");

          Set<String> myGroups = simplePageBean.getMyGroups();

          for (SimplePageItem pageItem : simplePageToolDao.findItemsOnPage(entry.pageId)) {

            // if item is group controlled, skip if user isn't in one of the groups
            Collection<String> itemGroups = null;
            try {
              itemGroups = simplePageBean.getItemGroups(pageItem, null, false);
            } catch (IdUnusedException e) {
              // underlying assignment, etc, doesn't exist. skip the item
              continue;
            }
            if (itemGroups != null) {
              boolean groupsOk = false;
              for (String group : itemGroups) {
                if (myGroups.contains(group)) {
                  groupsOk = true;
                  break;
                }
              }
              if (!groupsOk) continue;
            }

            UIBranchContainer itemListItem = UIBranchContainer.make(row, "item:");

            if (pageItem.isRequired()) {
              UIOutput.make(itemListItem, "required-image");
            } else {
              UIOutput.make(itemListItem, "not-required-image");
            }

            UIOutput.make(itemListItem, "item-icon").decorate(getImageSourceDecorator(pageItem));

            if (pageItem.isPrerequisite()) {
              itemListItem.decorate(new UIFreeAttributeDecorator("class", "disabled-text-item"));
            }

            if (SimplePageItem.TEXT == pageItem.getType()) {
              UIOutput.make(
                      itemListItem,
                      "name",
                      messageLocator.getMessage("simplepage.chooser.textitemplaceholder"))
                  .decorate(new UIFreeAttributeDecorator("class", "text-item-placeholder"));
            } else if (SimplePageItem.BREAK == pageItem.getType()) {
              String text = null;
              if ("section".equals(pageItem.getFormat()))
                text = messageLocator.getMessage("simplepage.break-here");
              else text = messageLocator.getMessage("simplepage.break-column-here");
              UIOutput.make(itemListItem, "name", text)
                  .decorate(new UIFreeAttributeDecorator("class", "text-item-placeholder"));

            } else {
              UIOutput.make(itemListItem, "name", pageItem.getName());
            }
            // UIOutput.make(itemListItem, "page1",
            // Boolean.toString(lessonsAccess.isItemAccessible(pageItem.getId(),simplePageBean.getCurrentSiteId(),"c08d3ac9-c717-472a-ad91-7ce0b434f42f", simplePageBean)));
          }
        }
        index++;

        // for pagepicker or summary if canEdit and page doesn't have an item
      } else {
        int level = entry.level;
        if (level > 5) level = 5;
        if (!summaryPage /*&& !entry.toplevel*/) { // i.e. pagepicker; for the moment to edit
                                                   // something you need to attach it to something
          UISelectChoice.make(row, "select", select.getFullID(), index)
              .decorate(
                  new UIFreeAttributeDecorator(
                      "title", entry.title + " " + messageLocator.getMessage("simplepage.select")));
        } else if (summaryPage) { // i.e. summary if canEdit and page doesn't have an item
          UISelectChoice.make(row, "select-for-deletion", select.getFullID(), index)
              .decorate(
                  new UIFreeAttributeDecorator(
                      "title",
                      entry.title
                          + " "
                          + messageLocator.getMessage("simplepage.select-for-deletion")));
          showDeleteButton = true; // at least one item to delete
        }

        GeneralViewParameters params = new GeneralViewParameters();
        params.viewID = PreviewProducer.VIEW_ID;
        params.setSendingPage(entry.pageId);

        UIInternalLink.make(row, "link", params)
            .decorate(new UIFreeAttributeDecorator("style", "padding-left: " + (2 * level) + "em"))
            .decorate(new UIFreeAttributeDecorator("target", "_blank"));
        String levelstr =
            messageLocator
                    .getMessage("simplepage.status.level")
                    .replace("{}", Integer.toString(level))
                + " ";
        if (level > 0) UIOutput.make(row, "link-note", levelstr);
        UIOutput.make(row, "link-text", entry.title);

        index++;
      }

      if (canEditPage
          && entry != null
          && entry.pageId != null
          && sharedPages.contains(entry.pageId)) {
        UIOutput.make(row, "shared");
      }

      // debug code for development. this will be removed at some point
      // UIOutput.make(row, "page2",
      // lessonsAccess.printPath(lessonsAccess.getPagePaths(entry.pageId)));
      // UIOutput.make(row, "page1", Boolean.toString(lessonsAccess.isPageAccessible(entry.pageId,
      // simplePageBean.getCurrentSiteId(), "c08d3ac9-c717-472a-ad91-7ce0b434f42f",
      // simplePageBean)));

      if (ServerConfigurationService.getBoolean("lessonbuilder.accessibilitydebug", false)) {
        if (entry != null
            && entry.pageId != null
            && lessonsAccess.isPageAccessible(
                entry.pageId,
                simplePageBean.getCurrentSiteId(),
                "c08d3ac9-c717-472a-ad91-7ce0b434f42f",
                null)) {
          UIOutput.make(row, "page1");
        }
        if (entry != null
            && entry.pageId != null
            && lessonsAccess.isPageAccessible(
                entry.pageId,
                simplePageBean.getCurrentSiteId(),
                "c08d3ac9-c717-472a-ad91-7ce0b434f42f",
                simplePageBean)) {
          UIOutput.make(row, "page2");
        }
        if (entry != null
            && entry.pageId != null
            && lessonsAccess.isItemAccessible(
                entry.itemId,
                simplePageBean.getCurrentSiteId(),
                "c08d3ac9-c717-472a-ad91-7ce0b434f42f",
                null)) {
          UIOutput.make(row, "item1");
        }
        if (entry != null
            && entry.pageId != null
            && lessonsAccess.isItemAccessible(
                entry.itemId,
                simplePageBean.getCurrentSiteId(),
                "c08d3ac9-c717-472a-ad91-7ce0b434f42f",
                simplePageBean)) {
          UIOutput.make(row, "item2");
        }
      }
    }

    if (!summaryPage) {

      UIInput.make(form, "item-id", "#{simplePageBean.itemId}");

      if (itemId == -1 && !((GeneralViewParameters) viewparams).newTopLevel) {
        UIOutput.make(form, "hr");
        UIOutput.make(form, "options");
        UIBoundBoolean.make(form, "subpage-next", "#{simplePageBean.subpageNext}", false);
        UIBoundBoolean.make(form, "subpage-button", "#{simplePageBean.subpageButton}", false);
      }

      String returnView = ((GeneralViewParameters) viewparams).getReturnView();
      if (returnView != null && returnView.equals("reorder")) {
        // return to Reorder, to add items from this page
        UICommand.make(
            form,
            "submit",
            messageLocator.getMessage("simplepage.chooser.select"),
            "#{simplePageBean.selectPage}");
      } else if (((GeneralViewParameters) viewparams).newTopLevel) {
        UIInput.make(
            form,
            "addBefore",
            "#{simplePageBean.addBefore}",
            ((GeneralViewParameters) viewparams).getAddBefore());
        UICommand.make(
            form,
            "submit",
            messageLocator.getMessage("simplepage.chooser.select"),
            "#{simplePageBean.addOldPage}");
      } else {
        UIInput.make(
            form,
            "addBefore",
            "#{simplePageBean.addBefore}",
            ((GeneralViewParameters) viewparams).getAddBefore());
        UICommand.make(
            form,
            "submit",
            messageLocator.getMessage("simplepage.chooser.select"),
            "#{simplePageBean.createSubpage}");
      }
      UICommand.make(
          form,
          "cancel",
          messageLocator.getMessage("simplepage.cancel"),
          "#{simplePageBean.cancel}");
    } else if (showDeleteButton) {
      UICommand.make(
          form,
          "submit",
          messageLocator.getMessage("simplepage.delete-selected"),
          "#{simplePageBean.deletePages}");
    }
  }