/**
   * Returns the <code>SubscriptionContext</code> to use for assessment notification about specified
   * <code>ICourse</code>.<br>
   * <br>
   * <b>PRE CONDITIONS</b>
   *
   * <ul>
   *   <li><code>course != null</code>
   * </ul>
   *
   * If <code>ident == null</code>, the subscription context is (created and) returned without
   * authorization control
   *
   * @param ident the identity, if null, no subscription check will be made
   * @param course
   * @return the subscription context to use or <code>null</code> if the identity associated to the
   *     request is not allowed to be notified
   * @see #canSubscribeForAssessmentNotification(Identity, ICourse)
   */
  protected SubscriptionContext getAssessmentSubscriptionContext(Identity ident, ICourse course) {
    SubscriptionContext sctx = null;

    if (ident == null || canSubscribeForAssessmentNotification(ident, course)) {
      // Creates a new SubscriptionContext only if not found into cache
      Long courseId = course.getResourceableId();
      synchronized (
          subsContexts) { // o_clusterOK by:ld - no problem to have independent subsContexts caches
                          // for each cluster node
        sctx = subsContexts.get(courseId);
        if (sctx == null) {
          // a subscription context showing to the root node (the course's root
          // node is started when clicking such a notification)
          CourseNode cn = course.getRunStructure().getRootNode();
          CourseEnvironment ce = course.getCourseEnvironment();
          // FIXME:fg:b little problem is that the assessment tool and the course are not "the same"
          // anymore, that is you can open the same course twice in the
          // dynamic tabs by a) klicking e.g. via repo, and b via notifications link to the
          // assementtool
          sctx =
              new SubscriptionContext(
                  CourseModule.ORES_COURSE_ASSESSMENT, ce.getCourseResourceableId(), cn.getIdent());
          subsContexts.put(courseId, sctx);
        }
      }
    }

    return sctx;
  }
  private void applyChanges(boolean setAsDone) {
    List<AssessmentRow> rows = model.getObjects();
    ICourse course = CourseFactory.loadCourse(courseEnv.getCourseResourceableId());
    if (applyToAllEl.isAtLeastSelected(1)) {
      Float score = null;

      if (withScore) {
        String scoreValue = groupScoreEl.getValue();
        if (StringHelper.containsNonWhitespace(scoreValue)) {
          score = Float.parseFloat(scoreValue);
        }
      }

      Boolean passed = null;
      if (withPassed) {
        if (cutValue == null) {
          passed = groupPassedEl.isSelected(0);
        } else if (score != null) {
          passed = (score.floatValue() >= cutValue.floatValue()) ? Boolean.TRUE : Boolean.FALSE;
        }
      }

      for (AssessmentRow row : rows) {
        UserCourseEnvironment userCourseEnv = row.getUserCourseEnvironment(course);
        ScoreEvaluation newScoreEval;
        if (setAsDone) {
          newScoreEval = new ScoreEvaluation(score, passed, AssessmentEntryStatus.done, true, null);
        } else {
          newScoreEval = new ScoreEvaluation(score, passed);
        }
        gtaNode.updateUserScoreEvaluation(newScoreEval, userCourseEnv, getIdentity(), false);
      }

      if (withComment) {
        String comment = groupCommentEl.getValue();
        if (StringHelper.containsNonWhitespace(comment)) {
          for (AssessmentRow row : rows) {
            UserCourseEnvironment userCourseEnv = row.getUserCourseEnvironment(course);
            gtaNode.updateUserUserComment(comment, userCourseEnv, getIdentity());
          }
        }
      }

    } else {
      for (AssessmentRow row : rows) {
        UserCourseEnvironment userCourseEnv = row.getUserCourseEnvironment(course);

        Float score = null;
        if (withScore) {
          String value = row.getScoreEl().getValue();
          if (StringHelper.containsNonWhitespace(value)) {
            score = Float.parseFloat(value);
          }
        }

        Boolean passed = null;
        if (withPassed) {
          if (cutValue == null) {
            passed = row.getPassedEl().isSelected(0);
          } else if (score != null) {
            passed = (score.floatValue() >= cutValue.floatValue()) ? Boolean.TRUE : Boolean.FALSE;
          }
        }

        ScoreEvaluation newScoreEval;
        if (setAsDone) {
          newScoreEval = new ScoreEvaluation(score, passed, AssessmentEntryStatus.done, true, null);
        } else {
          newScoreEval = new ScoreEvaluation(score, passed);
        }
        gtaNode.updateUserScoreEvaluation(newScoreEval, userCourseEnv, getIdentity(), false);

        if (withComment) {
          String comment = row.getComment();
          if (StringHelper.containsNonWhitespace(comment)) {
            gtaNode.updateUserUserComment(comment, userCourseEnv, getIdentity());
          }
        }
      }
    }
  }
 @Override
 protected void formOK(UserRequest ureq) {
   boolean projectChanged = false;
   if (!project.getTitle().equals(projectTitle.getValue())) {
     // title has been changed => change project-group name too
     String newProjectGroupName = translate("project.member.groupname", projectTitle.getValue());
     String newProjectGroupDescription =
         translate("project.member.groupdescription", projectTitle.getValue());
     OLATResource courseResource = courseEnv.getCourseGroupManager().getCourseResource();
     ProjectBrokerManagerFactory.getProjectGroupManager()
         .changeProjectGroupName(
             getIdentity(),
             project.getProjectGroup(),
             newProjectGroupName,
             newProjectGroupDescription,
             courseResource);
     ProjectBrokerManagerFactory.getProjectGroupManager()
         .sendGroupChangeEvent(project, courseEnv.getCourseResourceableId(), ureq.getIdentity());
     projectChanged = true;
   }
   if (!project.getTitle().equals(projectTitle.getValue())) {
     project.setTitle(projectTitle.getValue());
     projectChanged = true;
   }
   if (!project.getDescription().equals(projectDescription.getValue())) {
     project.setDescription(projectDescription.getValue());
     projectChanged = true;
   }
   if (projectState.isSelected(0)) {
     if (!project.getState().equals(stateKeys[0])) {
       project.setState(stateKeys[0]);
       projectChanged = true;
     }
   } else {
     if (!project.getState().equals(stateKeys[1])) {
       project.setState(stateKeys[1]);
       projectChanged = true;
     }
   }
   if (project.getMaxMembers() != maxMembers.getIntValue()) {
     project.setMaxMembers(maxMembers.getIntValue());
     projectChanged = true;
   }
   if (attachmentFileName.getUploadFileName() != null
       && !attachmentFileName.getUploadFileName().equals("")) {
     project.setAttachedFileName(attachmentFileName.getUploadFileName());
     uploadFiles(attachmentFileName);
   }
   // store customfields
   int index = 0;
   for (Iterator iterator = customfieldElementList.iterator(); iterator.hasNext(); ) {
     Object element = iterator.next();
     String value = "";
     if (element instanceof TextElement) {
       TextElement textElement = (TextElement) element;
       value = textElement.getValue();
     } else if (element instanceof SingleSelection) {
       SingleSelection selectionElement = (SingleSelection) element;
       if (!selectionElement.getSelectedKey().equals(DROPDOWN_NO_SELECETION)) {
         value = selectionElement.getSelectedKey();
       } else {
         value = "";
       }
     }
     if (!project.getCustomFieldValue(index).equals(value)) {
       project.setCustomFieldValue(index, value);
       projectChanged = true;
     }
     index++;
   }
   // store events
   for (Project.EventType eventType : eventStartElementList.keySet()) {
     Date startDate = eventStartElementList.get(eventType).getDate();
     Date endDate = eventEndElementList.get(eventType).getDate();
     if (!project.getProjectEvent(eventType).getStartDate().equals(startDate)
         || !project.getProjectEvent(eventType).getEndDate().equals(endDate)) {
       project.setProjectEvent(new ProjectEvent(eventType, startDate, endDate));
       projectChanged = true;
     }
   }
   if (projectChanged) {
     ProjectBrokerManagerFactory.getProjectBrokerManager().updateProject(project);
     ProjectBrokerManagerFactory.getProjectBrokerEmailer()
         .sendProjectChangedEmailToParticipants(ureq.getIdentity(), project, this.getTranslator());
     fireEvent(ureq, Event.DONE_EVENT);
   }
 }
  /** @return True if all results are the same */
  private ModelInfos loadModel() {
    // load participants, load datas
    ICourse course = CourseFactory.loadCourse(courseEnv.getCourseResourceableId());
    List<Identity> identities =
        businessGroupService.getMembers(assessedGroup, GroupRoles.participant.name());

    Map<Identity, AssessmentEntry> identityToEntryMap = new HashMap<>();
    List<AssessmentEntry> entries =
        course
            .getCourseEnvironment()
            .getAssessmentManager()
            .getAssessmentEntries(assessedGroup, gtaNode);
    for (AssessmentEntry entry : entries) {
      identityToEntryMap.put(entry.getIdentity(), entry);
    }

    int count = 0;
    boolean same = true;
    StringBuilder duplicateWarning = new StringBuilder();
    Float scoreRef = null;
    Boolean passedRef = null;
    String commentRef = null;

    List<AssessmentRow> rows = new ArrayList<>(identities.size());
    for (Identity identity : identities) {
      AssessmentEntry entry = identityToEntryMap.get(identity);

      ScoreEvaluation scoreEval = null;
      if (withScore || withPassed) {
        scoreEval = gtaNode.getUserScoreEvaluation(entry);
        if (scoreEval == null) {
          scoreEval = ScoreEvaluation.EMPTY_EVALUATION;
        }
      }

      String comment = null;
      if (withComment && entry != null) {
        comment = entry.getComment();
      }

      boolean duplicate = duplicateMemberKeys.contains(identity.getKey());
      if (duplicate) {
        if (duplicateWarning.length() > 0) duplicateWarning.append(", ");
        duplicateWarning.append(StringHelper.escapeHtml(userManager.getUserDisplayName(identity)));
      }

      AssessmentRow row = new AssessmentRow(identity, duplicate);
      rows.add(row);

      if (withScore) {
        Float score = scoreEval.getScore();
        String pointVal = AssessmentHelper.getRoundedScore(score);
        TextElement pointEl = uifactory.addTextElement("point" + count, null, 5, pointVal, flc);
        pointEl.setDisplaySize(5);
        row.setScoreEl(pointEl);
        if (count == 0) {
          scoreRef = score;
        } else if (!same(scoreRef, score)) {
          same = false;
        }
      }

      if (withPassed && cutValue == null) {
        Boolean passed = scoreEval.getPassed();
        MultipleSelectionElement passedEl =
            uifactory.addCheckboxesHorizontal("check" + count, null, flc, onKeys, onValues);
        if (passed != null && passed.booleanValue()) {
          passedEl.select(onKeys[0], passed.booleanValue());
        }
        row.setPassedEl(passedEl);
        if (count == 0) {
          passedRef = passed;
        } else if (!same(passedRef, passed)) {
          same = false;
        }
      }

      if (withComment) {
        FormLink commentLink =
            uifactory.addFormLink(
                "comment-" + CodeHelper.getRAMUniqueID(),
                "comment",
                "comment",
                null,
                flc,
                Link.LINK);
        if (StringHelper.containsNonWhitespace(comment)) {
          commentLink.setIconLeftCSS("o_icon o_icon_comments");
        } else {
          commentLink.setIconLeftCSS("o_icon o_icon_comments_none");
        }
        commentLink.setUserObject(row);
        row.setComment(comment);
        row.setCommentEditLink(commentLink);

        if (count == 0) {
          commentRef = comment;
        } else if (!same(commentRef, comment)) {
          same = false;
        }
      }

      count++;
    }

    model.setObjects(rows);
    table.reset();

    return new ModelInfos(same, scoreRef, passedRef, commentRef, duplicateWarning.toString());
  }
 @Override
 protected void formOK(UserRequest ureq) {
   boolean projectChanged = false;
   if (!project.getTitle().equals(projectTitle.getValue())) {
     // title has been changed => change project-group name too
     String newProjectGroupName = translate("project.member.groupname", projectTitle.getValue());
     String newProjectGroupDescription =
         translate("project.member.groupdescription", projectTitle.getValue());
     OLATResource courseResource = courseEnv.getCourseGroupManager().getCourseResource();
     ProjectBrokerManagerFactory.getProjectGroupManager()
         .changeProjectGroupName(
             getIdentity(),
             project.getProjectGroup(),
             newProjectGroupName,
             newProjectGroupDescription,
             courseResource);
     ProjectBrokerManagerFactory.getProjectGroupManager()
         .sendGroupChangeEvent(project, courseEnv.getCourseResourceableId(), ureq.getIdentity());
     projectChanged = true;
   }
   if (!project.getTitle().equals(projectTitle.getValue())) {
     project.setTitle(projectTitle.getValue());
     projectChanged = true;
   }
   if (!project.getDescription().equals(projectDescription.getValue())) {
     project.setDescription(projectDescription.getValue());
     projectChanged = true;
   }
   if (project.getMaxMembers() != maxMembers.getIntValue()) {
     project.setMaxMembers(maxMembers.getIntValue());
     ProjectBrokerManagerFactory.getProjectGroupManager()
         .setProjectGroupMaxMembers(
             getIdentity(), project.getProjectGroup(), maxMembers.getIntValue());
     projectChanged = true;
   }
   if (attachmentFileName.getUploadFileName() != null
       && !attachmentFileName.getUploadFileName().equals("")) {
     // First call uploadFiles than setAttachedFileName because uploadFiles needs old attachment
     // name
     uploadFiles(attachmentFileName);
     project.setAttachedFileName(attachmentFileName.getUploadFileName());
     projectChanged = true;
   } else if (project.getAttachmentFileName() != null
       && !project.getAttachmentFileName().equals("")) {
     // Attachment file has been removed
     project.setAttachedFileName("");
     projectChanged = true;
   }
   // store customfields
   int index = 0;
   for (Iterator iterator = customfieldElementList.iterator(); iterator.hasNext(); ) {
     Object element = iterator.next();
     String value = "";
     if (element instanceof TextElement) {
       TextElement textElement = (TextElement) element;
       value = textElement.getValue();
     } else if (element instanceof SingleSelection) {
       SingleSelection selectionElement = (SingleSelection) element;
       if (!selectionElement.getSelectedKey().equals(DROPDOWN_NO_SELECETION)) {
         value = selectionElement.getValue(selectionElement.getSelected());
       } else {
         value = "";
       }
     }
     getLogger()
         .debug(
             "customfield index="
                 + index
                 + " value="
                 + value
                 + " project.getCustomFieldValue(index)="
                 + project.getCustomFieldValue(index));
     if (!project.getCustomFieldValue(index).equals(value)) {
       project.setCustomFieldValue(index, value);
       projectChanged = true;
     }
     index++;
   }
   // store events
   for (Project.EventType eventType : eventStartElementList.keySet()) {
     Date startDate = eventStartElementList.get(eventType).getDate();
     Date endDate = eventEndElementList.get(eventType).getDate();
     // First handle startdate
     if (hasBeenChanged(project.getProjectEvent(eventType).getStartDate(), startDate)
         || hasBeenChanged(project.getProjectEvent(eventType).getEndDate(), endDate)) {
       project.setProjectEvent(new ProjectEvent(eventType, startDate, endDate));
       projectChanged = true;
     }
   }
   if (mailNotification.isSelected(0) != project.isMailNotificationEnabled()) {
     project.setMailNotificationEnabled(mailNotification.isSelected(0));
     projectChanged = true;
   }
   if (projectChanged) {
     if (ProjectBrokerManagerFactory.getProjectBrokerManager().existsProject(project.getKey())) {
       ProjectBrokerManagerFactory.getProjectBrokerManager().updateProject(project);
       ProjectBrokerManagerFactory.getProjectBrokerEmailer()
           .sendProjectChangedEmailToParticipants(
               ureq.getIdentity(), project, this.getTranslator());
     } else {
       this.showInfo("info.project.nolonger.exist", project.getTitle());
     }
   }
   fireEvent(ureq, Event.DONE_EVENT);
 }