Exemple #1
0
  public static String getUserPreference(
      long userId, long plid, String portletId, String preferenceName) {
    String _preference = StringPool.BLANK;

    PortletPreferences preferences = null;
    try {
      preferences =
          PortletPreferencesLocalServiceUtil.getPortletPreferences(
              userId, PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletId);
    } catch (PortalException e) {
      // ignore
    } catch (SystemException e) {
      // ignore
    }

    if (Validator.isNull(preferences)) return _preference;

    Document document = null;
    try {
      document = SAXReaderUtil.read(preferences.getPreferences());
    } catch (DocumentException e) {
      e.printStackTrace();
    }

    for (Iterator<Element> itr = document.getRootElement().elementIterator("preference");
        itr.hasNext(); ) {
      Element preference = (Element) itr.next();

      if (preference.element("name").getText().equalsIgnoreCase(preferenceName)) {
        _preference = preference.element("value").getText();
      }
    }

    return _preference;
  }
  @Override
  public boolean updateCourse(Course course, long userId) throws SystemException {

    CourseResult courseResult =
        CourseResultLocalServiceUtil.getByUserAndCourse(course.getCourseId(), userId);
    // .fetchByuc(mresult.getUserId(), course.getCourseId());
    if (courseResult == null) {
      courseResult = CourseResultLocalServiceUtil.create(course.getCourseId(), userId);
    }

    if (courseResult.getStartDate() == null) {
      courseResult.setStartDate(new Date());
    }

    try {
      java.util.Map<Long, Long> weights = PonderatedCourseEval.getActivitiesWeight(course);
      long score = PonderatedCourseEval.getScore(course);

      boolean passed = true;
      long result = 0;
      long weight = 0;
      List<LearningActivity> learningActivities =
          LearningActivityLocalServiceUtil.getMandatoryLearningActivitiesOfGroup(
              course.getGroupCreatedId());

      // Guardo los resultados de las actividades del usuario en el curso en un hashmap para no
      // tener que acceder a bbdd por cada uno de ellos
      List<LearningActivityResult> lresult =
          LearningActivityResultLocalServiceUtil.getMandatoryByGroupIdUserId(
              course.getGroupCreatedId(), userId);
      HashMap<Long, LearningActivityResult> results = new HashMap<Long, LearningActivityResult>();
      for (LearningActivityResult ar : lresult) {
        results.put(ar.getActId(), ar);
      }

      boolean isFailed = false;
      LearningActivityResult learningActivityResult = null;

      for (LearningActivity act : learningActivities) {

        if (!weights.containsKey(
            act
                .getActId())) { // Solo se tienen en cuenta las actividades obligatorias que tienen
                                // peso definido
          continue;
        }

        if (results.containsKey(act.getActId())) {
          learningActivityResult = results.get(act.getActId());
        } else {
          learningActivityResult = null;
        }

        if (learningActivityResult != null) {
          if (learningActivityResult.getEndDate() != null) {
            if (!learningActivityResult.isPassed()) {
              passed = false;
              if (act.getTries() > 0) {
                long userTries =
                    LearningActivityTryLocalServiceUtil.getLearningActivityTryByActUserCount(
                        act.getActId(), userId);
                if (userTries >= act.getTries()) {
                  isFailed = true;
                }
              }
            }
            result = result + (learningActivityResult.getResult() * weights.get(act.getActId()));

          } else {
            passed = false;
          }
        } else {
          passed = false;
        }
        weight += weights.get(act.getActId());
      }

      if (result > 0 && weight > 0) {
        result = result / weight;
      }

      if (result < score) {
        passed = false;
      }

      // Si el usuario se ha marcado como isFailed es porque lo tiene suspenso. Se le asigna un
      // passed a false y se marca la fecha de finalización del curso (passedDate).
      courseResult.setPassed(passed && !isFailed);
      // Se almacena el result del resultado del usuario en el curso.
      courseResult.setResult(result);
      if ((passed || isFailed) && courseResult.getPassedDate() == null) {
        courseResult.setPassedDate(new Date());
      }
      CourseResultLocalServiceUtil.update(courseResult);
      return true;

    } catch (PortalException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (DocumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return true;
  }
  @Override
  public Fields getFields(DDMStructure structure, XPath xPath, String xml, List<String> fieldNames)
      throws PortalException {

    Document document = null;

    try {
      document = _saxReader.read(xml);
    } catch (DocumentException de) {
      if (_log.isDebugEnabled()) {
        _log.debug(de.getMessage(), de);
      }

      return null;
    }

    if ((xPath != null) && !xPath.booleanValueOf(document)) {
      return null;
    }

    Fields fields = new Fields();

    Element rootElement = document.getRootElement();

    List<Element> dynamicElementElements = rootElement.elements("dynamic-element");

    for (Element dynamicElementElement : dynamicElementElements) {
      String fieldName = dynamicElementElement.attributeValue("name");

      if (!structure.hasField(fieldName)
          || ((fieldNames != null) && !fieldNames.contains(fieldName))) {

        continue;
      }

      String fieldDataType = structure.getFieldDataType(fieldName);

      List<Element> dynamicContentElements = dynamicElementElement.elements("dynamic-content");

      for (Element dynamicContentElement : dynamicContentElements) {
        String fieldValue = dynamicContentElement.getText();

        String languageId = dynamicContentElement.attributeValue("language-id");

        Locale locale = LocaleUtil.fromLanguageId(languageId);

        Serializable fieldValueSerializable =
            FieldConstants.getSerializable(fieldDataType, fieldValue);

        Field field = fields.get(fieldName);

        if (field == null) {
          field = new Field();

          String defaultLanguageId = dynamicElementElement.attributeValue("default-language-id");

          if (Validator.isNull(defaultLanguageId)) {
            defaultLanguageId = rootElement.attributeValue("default-locale");
          }

          Locale defaultLocale = LocaleUtil.fromLanguageId(defaultLanguageId);

          field.setDefaultLocale(defaultLocale);

          field.setDDMStructureId(structure.getStructureId());
          field.setName(fieldName);
          field.setValue(locale, fieldValueSerializable);

          fields.put(field);
        } else {
          field.addValue(locale, fieldValueSerializable);
        }
      }
    }

    return fields;
  }