Package org.olat.course.statistic.hourofday

Source Code of org.olat.course.statistic.hourofday.HourOfDayStatisticManager

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS,
* <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.course.statistic.hourofday;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.commons.persistence.DBQuery;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.table.ColumnDescriptor;
import org.olat.core.gui.components.table.DefaultColumnDescriptor;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.course.ICourse;
import org.olat.course.statistic.IStatisticManager;
import org.olat.course.statistic.StatisticDisplayController;
import org.olat.course.statistic.StatisticResult;
import org.olat.course.statistic.TotalAwareColumnDescriptor;

/**
* Implementation of the IStatisticManager for 'hourofday' statistic
* <P>
* Initial Date:  12.02.2010 <br>
* @author Stefan
*/
public class HourOfDayStatisticManager implements IStatisticManager {

  /** the logging object used in this class **/
  private static final OLog log_ = Tracing.createLoggerFor(HourOfDayStatisticManager.class);

  @Override
  public StatisticResult generateStatisticResult(UserRequest ureq, ICourse course, long courseRepositoryEntryKey) {
    DBQuery dbQuery = DBFactory.getInstance().createQuery("select businessPath,hour,value from org.olat.course.statistic.hourofday.HourOfDayStat sv "
        + "where sv.resId=:resId");
    dbQuery.setLong("resId", courseRepositoryEntryKey);

    StatisticResult statisticResult = new StatisticResult(course, dbQuery.list());
    List<String> columnHeaders = statisticResult.getColumnHeaders();
    if (columnHeaders!=null && columnHeaders.size()>1) {
      try{
        int start = Integer.parseInt(columnHeaders.get(0));
        int end = Integer.parseInt(columnHeaders.get(columnHeaders.size()-1));
        List<String> resultingColumnHeaders = new ArrayList<String>((end-start)+1);
        for(int hour=start; hour<=end; hour++) {
          resultingColumnHeaders.add(String.valueOf(hour));
        }
        statisticResult.setColumnHeaders(resultingColumnHeaders);
      } catch(NumberFormatException nfe) {
        log_.warn("generateStatisticResult: Got a NumberFormatException: "+nfe, nfe);
      }
    }
    return statisticResult;
  }
 
  @Override
  public ColumnDescriptor createColumnDescriptor(UserRequest ureq, int column, String headerId) {
    if (column==0) {
      return new DefaultColumnDescriptor("stat.table.header.node", 0, null, ureq.getLocale());
    }
    String hourOfDayLocaled = headerId;
   
    try{
      Calendar c = Calendar.getInstance(ureq.getLocale());
      c.setTime(new Date());
      c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(headerId));
      c.set(Calendar.MINUTE, 0);
      DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, ureq.getLocale());
      hourOfDayLocaled = df.format(c.getTime());
    } catch(RuntimeException re) {
      re.printStackTrace(System.out);
    }

    TotalAwareColumnDescriptor cd = new TotalAwareColumnDescriptor(hourOfDayLocaled, column,
        StatisticDisplayController.CLICK_TOTAL_ACTION+column, ureq.getLocale(), ColumnDescriptor.ALIGNMENT_RIGHT);
    cd.setTranslateHeaderKey(false);
    return cd;
  }

  @Override
  public StatisticResult generateStatisticResult(UserRequest ureq, ICourse course, long courseRepositoryEntryKey, Date fromDate, Date toDate) {
    return generateStatisticResult(ureq, course, courseRepositoryEntryKey);
  }

}
TOP

Related Classes of org.olat.course.statistic.hourofday.HourOfDayStatisticManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.