Пример #1
0
  public String getTimeDifference(long timeMillis) {
    long difference = Math.round((System.currentTimeMillis() - timeMillis) / 1000.0d); // In seconds

    // These constants are calculated to take rounding into effect, and try to give a fairly
    // accurate representation of the time difference using words.

    String descrip = "";

    if (difference < 45) {
      descrip = messageLocator.getMessage("simplepage.seconds");
    } else if (difference < 90) {
      descrip = messageLocator.getMessage("simplepage.one_min");
    } else if (difference < 3570) { // 2 mins --> 59 mins
      long minutes = Math.max(2, Math.round(difference / 60.0d));
      descrip =
          messageLocator.getMessage("simplepage.x_min").replace("{}", String.valueOf(minutes));
    } else if (difference < 7170) {
      descrip = messageLocator.getMessage("simplepage.one_hour");
    } else if (difference < 84600) { // 2 hours --> 23 hours
      long hours = Math.max(2, Math.round(difference / 3600.0d));
      descrip = messageLocator.getMessage("simplepage.x_hour").replace("{}", String.valueOf(hours));
    } else if (difference < 129600) {
      descrip = messageLocator.getMessage("simplepage.one_day");
    } else if (difference < 2548800) { // 2 days --> 29 days
      long days = Math.max(2, Math.round(difference / 86400.0d));
      descrip = messageLocator.getMessage("simplepage.x_day").replace("{}", String.valueOf(days));
    } else if (difference < 3888000) {
      descrip = messageLocator.getMessage("simplepage.one_month");
    } else if (difference < 29808000) { // 2 months --> 11 months
      long months = Math.max(2, Math.round(difference / 2592000.0d));
      descrip =
          messageLocator.getMessage("simplepage.x_month").replace("{}", String.valueOf(months));
    } else if (difference < 47304000) {
      descrip = messageLocator.getMessage("simplepage.one_year");
    } else { // 2+ years
      long years = Math.max(2, Math.round(difference / 31536000.0d));
      descrip = messageLocator.getMessage("simplepage.x_year").replace("{}", String.valueOf(years));
    }

    Date d = new Date(timeMillis);
    Date now = new Date();

    Calendar cpost = Calendar.getInstance(TimeService.getLocalTimeZone(), M_locale);
    Calendar cnow = Calendar.getInstance(TimeService.getLocalTimeZone(), M_locale);
    cpost.setTime(d);
    cnow.setTime(now);

    if (cpost.get(Calendar.MONTH) == cnow.get(Calendar.MONTH)
        && cpost.get(Calendar.DATE) == cnow.get(Calendar.DATE)
        && cpost.get(Calendar.YEAR) == cnow.get(Calendar.YEAR)) {
      return dfTime.format(d) + " (" + descrip + ")";
    } else if (d.getYear() == now.getYear()) {
      return dfDate.format(d) + " (" + descrip + ")";
    } else {
      return dfDate.format(d) + " (" + descrip + ")";
    }
  }
 public String formatDateTime(Date date, HttpServletRequest request) {
   if (date == null) {
     return "unkown-date";
   }
   ResourceLoader rl = new ResourceLoader();
   DateFormat formatter =
       DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, rl.getLocale());
   formatter.setTimeZone(TimeService.getLocalTimeZone());
   return formatter.format(date);
 }
Пример #3
0
  public void fillComponents(
      UIContainer tofill, ViewParameters viewparams, ComponentChecker checker) {
    CommentsViewParameters params = (CommentsViewParameters) viewparams;
    filter = params.filter;

    // set up locale
    String langLoc[] = localeGetter.get().toString().split("_");
    if (langLoc.length >= 2) {
      if ("en".equals(langLoc[0]) && "ZA".equals(langLoc[1])) {
        M_locale = new Locale("en", "GB");
      } else {
        M_locale = new Locale(langLoc[0], langLoc[1]);
      }
    } else {
      M_locale = new Locale(langLoc[0]);
    }

    df =
        DateFormat.getDateTimeInstance(
            DateFormat.DEFAULT, DateFormat.DEFAULT, new ResourceLoader().getLocale());
    df.setTimeZone(TimeService.getLocalTimeZone());
    dfTime = DateFormat.getTimeInstance(DateFormat.SHORT, M_locale);
    dfTime.setTimeZone(TimeService.getLocalTimeZone());
    dfDate = DateFormat.getDateInstance(DateFormat.MEDIUM, M_locale);
    dfDate.setTimeZone(TimeService.getLocalTimeZone());

    // errors redirect back to ShowPage. But if this is embedded in the page, ShowPage
    // will call us again. This is very hard for the user to recover from. So trap
    // all possible errors. It may result in an incomplete page or something invalid,
    // but better that than an infinite recursion.

    try {

      SimplePage currentPage = simplePageToolDao.getPage(params.pageId);
      simplePageBean.setCurrentSiteId(params.siteId);
      simplePageBean.setCurrentPage(currentPage);
      simplePageBean.setCurrentPageId(params.pageId);

      UIOutput.make(tofill, "mainlist")
          .decorate(
              new UIFreeAttributeDecorator(
                  "aria-label", messageLocator.getMessage("simplepage.comments-section")));

      SimplePageItem commentsItem = simplePageToolDao.findItem(params.itemId);
      if (commentsItem != null
          && commentsItem.getSakaiId() != null
          && !commentsItem.getSakaiId().equals("")) {
        SimpleStudentPage studentPage =
            simplePageToolDao.findStudentPage(Long.valueOf(commentsItem.getSakaiId()));
        if (studentPage != null) {
          owner = studentPage.getOwner();
        }
      }

      if (params.deleteComment != null) {
        simplePageBean.deleteComment(params.deleteComment);
      }

      List<SimplePageComment> comments;

      if (!filter && !params.studentContentItem) {
        comments = (List<SimplePageComment>) simplePageToolDao.findComments(params.itemId);
      } else if (filter && !params.studentContentItem) {
        comments =
            (List<SimplePageComment>)
                simplePageToolDao.findCommentsOnItemByAuthor(params.itemId, params.author);
      } else if (filter && params.studentContentItem) {
        List<SimpleStudentPage> studentPages = simplePageToolDao.findStudentPages(params.itemId);
        Site site = SiteService.getSite(currentPage.getSiteId());
        itemToPageowner = new HashMap<Long, String>();
        List<Long> commentsItemIds = new ArrayList<Long>();
        for (SimpleStudentPage p : studentPages) {
          // If the page is deleted, don't show the comments
          if (!p.isDeleted()) {
            commentsItemIds.add(p.getCommentsSection());
            String pageOwner = p.getOwner();
            String pageGroup = p.getGroup();
            if (pageGroup != null) pageOwner = pageGroup;
            try {
              String o = null;
              if (pageGroup != null) o = site.getGroup(pageGroup).getTitle();
              else o = UserDirectoryService.getUser(pageOwner).getDisplayName();
              if (o != null) pageOwner = o;
            } catch (Exception ignore) {
            }
            ;
            itemToPageowner.put(p.getCommentsSection(), pageOwner);
          }
        }

        comments = simplePageToolDao.findCommentsOnItemsByAuthor(commentsItemIds, params.author);
      } else {
        List<SimpleStudentPage> studentPages = simplePageToolDao.findStudentPages(params.itemId);

        List<Long> commentsItemIds = new ArrayList<Long>();
        for (SimpleStudentPage p : studentPages) {
          commentsItemIds.add(p.getCommentsSection());
        }

        comments = simplePageToolDao.findCommentsOnItems(commentsItemIds);
      }

      // Make sure everything is chronological
      Collections.sort(
          comments,
          new Comparator<SimplePageComment>() {
            public int compare(SimplePageComment c1, SimplePageComment c2) {
              if (itemToPageowner != null) {
                String o1 = itemToPageowner.get(c1.getItemId());
                String o2 = itemToPageowner.get(c2.getItemId());
                if (o1 != o2) {
                  if (o1 == null) return 1;
                  return o1.compareTo(o2);
                }
              }
              return c1.getTimePosted().compareTo(c2.getTimePosted());
            }
          });

      currentUserId = UserDirectoryService.getCurrentUser().getId();

      boolean anonymous = simplePageBean.findItem(params.itemId).isAnonymous();
      if (anonymous) {
        int i = 1;
        for (SimplePageComment comment : comments) {
          if (!anonymousLookup.containsKey(comment.getAuthor())) {
            anonymousLookup.put(
                comment.getAuthor(), messageLocator.getMessage("simplepage.anonymous") + " " + i);
            i++;
          }
        }
      }

      boolean highlighted = false;

      // We don't want page owners to edit or grade comments on their page
      // at the moment.  Perhaps add option?
      canEditPage = simplePageBean.getEditPrivs() == 0;

      boolean showGradingMessage =
          canEditPage && commentsItem.getGradebookId() != null && !params.filter;

      Date lastViewed = null; // remains null if never viewed before
      SimplePageLogEntry log = simplePageBean.getLogEntry(params.itemId);
      if (log != null) lastViewed = log.getLastViewed();

      int newItems = 0;

      // Remove any "phantom" comments. So that the anonymous order stays the same,
      // comments are deleted by removing all content.  Also check to see if any comments
      // have been graded yet.  Finally, if we're filtering, it takes out comments not by
      // this author.
      for (int i = comments.size() - 1; i >= 0; i--) {
        if (comments.get(i).getComment() == null || comments.get(i).getComment().equals("")) {
          comments.remove(i);
        } else if (params.filter && !comments.get(i).getAuthor().equals(params.author)) {
          comments.remove(i);
        } else {
          if (showGradingMessage && comments.get(i).getPoints() != null) showGradingMessage = false;
          if (lastViewed == null) newItems++; // all items are new if never viewed
          else if (comments.get(i).getTimePosted().after(lastViewed)) newItems++;
        }
      }

      // update date only if we actually are going to see all the comments, and it's
      // not a weird view (i.e. filter is on)
      //   The situation with items <= 5 is actually dubious. The user has them on the
      // screen, but there's no way to know whether he's actually seen them. Some users
      // are going to be surprised either way we do it.
      if (params.showAllComments || params.showNewComments || (!params.filter && newItems <= 5)) {
        if (log != null) {
          simplePageBean.update(log);
        } else {
          log = simplePageToolDao.makeLogEntry(currentUserId, params.itemId, null);
          simplePageBean.saveItem(log);
        }
      }

      // Make sure we don't show the grading message if there's nothing to grade.
      if (comments.size() == 0) {
        showGradingMessage = false;
      }

      boolean editable = false;

      if (comments.size() <= 5 || params.showAllComments || params.filter) {
        for (int i = 0; i < comments.size(); i++) {
          // We don't want them editing on the grading screen, which is why we also check filter.
          boolean canEdit =
              simplePageBean.canModifyComment(comments.get(i), canEditPage) && !params.filter;

          printComment(
              comments.get(i),
              tofill,
              (params.postedComment == comments.get(i).getId()),
              anonymous,
              canEdit,
              params,
              commentsItem,
              currentPage);
          if (!highlighted) {
            highlighted = (params.postedComment == comments.get(i).getId());
          }

          if (!editable) editable = canEdit;
        }
      } else {

        UIBranchContainer container = UIBranchContainer.make(tofill, "commentList:");
        UIOutput.make(container, "commentDiv");
        // UIBranchContainer container = UIBranchContainer.make(tofill, "commentDiv:");
        CommentsViewParameters eParams = new CommentsViewParameters(VIEW_ID);
        eParams.placementId = params.placementId;
        eParams.itemId = params.itemId;
        eParams.showAllComments = true;
        eParams.showNewComments = false;
        eParams.pageId = params.pageId;
        eParams.siteId = params.siteId;
        UIInternalLink.make(container, "to-load", eParams);
        UIOutput.make(
            container,
            "load-more-link",
            messageLocator
                .getMessage("simplepage.see_all_comments")
                .replace("{}", Integer.toString(comments.size())));

        if (!params.showNewComments && newItems > 5) {
          container = UIBranchContainer.make(tofill, "commentList:");
          UIOutput.make(container, "commentDiv");
          // UIBranchContainer container = UIBranchContainer.make(tofill, "commentDiv:");
          eParams = new CommentsViewParameters(VIEW_ID);
          eParams.placementId = params.placementId;
          eParams.itemId = params.itemId;
          eParams.showAllComments = false;
          eParams.showNewComments = true;
          eParams.pageId = params.pageId;
          eParams.siteId = params.siteId;
          UIInternalLink.make(container, "to-load", eParams);
          UIOutput.make(
              container,
              "load-more-link",
              messageLocator
                  .getMessage("simplepage.see_new_comments")
                  .replace("{}", Integer.toString(newItems)));
        }

        int start = comments.size() - 5;
        if (params.showNewComments) start = 0;

        // Show 5 most recent comments
        for (int i = start; i < comments.size(); i++) {
          if (!params.showNewComments
              || lastViewed == null
              || comments.get(i).getTimePosted().after(lastViewed)) {
            boolean canEdit = simplePageBean.canModifyComment(comments.get(i), canEditPage);
            printComment(
                comments.get(i),
                tofill,
                (params.postedComment == comments.get(i).getId()),
                anonymous,
                canEdit,
                params,
                commentsItem,
                currentPage);
            if (!highlighted) {
              highlighted = (params.postedComment == comments.get(i).getId());
            }

            if (!editable) editable = canEdit;
          }
        }
      }

      if (highlighted) {
        // We have something to highlight
        UIOutput.make(tofill, "highlightScript");
      }

      if (showGradingMessage) {
        UIOutput.make(tofill, "gradingAlert");
      }

      if (anonymous && canEditPage && comments.size() > 0 && lastViewed == null) {
        // Tells the admin that they can see the names, but everyone else can't
        UIOutput.make(tofill, "anonymousAlert");
      } else if (editable && simplePageBean.getEditPrivs() != 0) {
        // Warns user that they only have 30 mins to edit.
        UIOutput.make(tofill, "editAlert");
      }

    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("comments error " + e);
    }
    ;
  }