Search in sources :

Example 56 with HTMLWriter

use of web.HTMLWriter in project common by zenlunatics.

the class Survey method writeAnswers.

// ----------------------------------------------------------------------
public void writeAnswers(Request request) throws IOException {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
    HTMLWriter writer = request.writer;
    try {
        ResultSet rs = request.db.select(new Select("title,show_names,number_questions").from("surveys").whereIdEquals(m_id));
        rs.next();
        writer.h3(rs.getString(1));
        String answers_table = "survey_answers" + m_id;
        boolean show_names = (rs.getBoolean(2) || request.userIsAdministrator()) && request.db.getTable(answers_table).getColumn("_owner_") != null;
        boolean number_questions = rs.getBoolean(3);
        rs.getStatement().close();
        int num_participants = request.db.countRows(answers_table, "surveys_id=" + m_id);
        writer.write(num_participants);
        writer.write(" participant");
        if (num_participants != 1)
            writer.write('s');
        ResultSet questions = request.db.select("SELECT id,question,answer_type,type_data FROM survey_questions WHERE surveys_id=" + m_id + " ORDER BY _order_");
        View answers = request.site.newView(answers_table, request);
        answers.setWhere("surveys_id=" + m_id);
        int question_num = 0;
        while (questions.next()) {
            String type = questions.getString(3);
            if (type.equals("Divider") || type.equals("HTML") || type.equals("Page Break"))
                continue;
            String question_id = questions.getString(1);
            String answer_column = "a" + question_id;
            int from = 0;
            int[] ratings = null;
            if (type.equals("Rating")) {
                String[] td = questions.getString(4).split("\\|");
                from = Integer.parseInt(td[0]);
                int to = Integer.parseInt(td[1]);
                ratings = new int[to - from + 1];
            }
            Map<String, Integer> choices = null;
            if (type.equals("Choices"))
                choices = new TreeMap<String, Integer>();
            writer.write("<p><div class=\"survey_block\"><div class=\"survey_title\">");
            if (number_questions) {
                writer.write(++question_num);
                writer.write(". ");
            }
            writer.write(questions.getString(2));
            writer.write("</div>");
            if (show_names)
                answers.setSelectColumns(answer_column + ",_owner_");
            else
                answers.setSelectColumns(answer_column);
            answers.select();
            int num_answers = 0;
            while (answers.next()) {
                String answer = answers.getColumnHTML(answer_column);
                if (type.equals("Choices")) {
                    if (!answers.data().wasNull()) {
                        Integer i = choices.get(answer);
                        if (i == null)
                            choices.put(answer, new Integer(1));
                        else
                            choices.put(answer, new Integer(i.intValue() + 1));
                        ++num_answers;
                    }
                } else if (type.equals("Rating")) {
                    if (!answers.data().wasNull()) {
                        ratings[Integer.parseInt(answer) - from]++;
                        ++num_answers;
                    }
                } else if (show_names || answer.length() > 0) {
                    if (num_answers > 0)
                        writer.hr();
                    writer.write("<div style=\"padding:2px\">");
                    if (answer.length() > 0)
                        writer.write(answer);
                    if (show_names) {
                        writer.write("<br /><span style=\"font-weight:bold\">");
                        answers.writeColumnHTML("_owner_");
                        writer.write("</span>");
                    }
                    writer.write("</div>");
                    ++num_answers;
                }
            }
            if (type.equals("Choices")) {
                writer.write("totals: ");
                Iterator<String> i = choices.keySet().iterator();
                boolean first = true;
                while (i.hasNext()) {
                    if (first)
                        first = false;
                    else
                        writer.write(", ");
                    String key = i.next();
                    writer.write(key);
                    writer.write(": ");
                    writer.write(choices.get(key).intValue());
                }
            } else if (type.equals("Rating")) {
                int total = 0;
                writer.write("totals: ");
                for (int i = 0; i < ratings.length; i++) {
                    if (i > 0)
                        writer.write(", ");
                    writer.write(i + from);
                    writer.write(": ");
                    writer.write(ratings[i]);
                    total += ratings[i] * (i + 1);
                }
                writer.write(". average: ");
                writer.write(nf.format((double) total / num_answers + from));
            }
            writer.write("</div>");
        }
        questions.getStatement().close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : HTMLWriter(web.HTMLWriter) SQLException(java.sql.SQLException) TreeMap(java.util.TreeMap) View(db.View) ResultSet(java.sql.ResultSet) Select(db.Select) NumberFormat(java.text.NumberFormat)

Example 57 with HTMLWriter

use of web.HTMLWriter in project common by zenlunatics.

the class Survey method formClose.

// ----------------------------------------------------------------------
private void formClose(Request request) throws IOException {
    HTMLWriter writer = request.writer;
    writer.tagClose();
    try {
        if (!m_questions.isAfterLast()) {
            writer.write("<p align=\"center\">");
            m_form.setSubmitText("next").writeSubmit();
            writer.write("</p>");
        } else
            setPageNum(-1, request);
        m_questions.getStatement().close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    m_form.close();
    if (getPageNum(request) == 0)
        writer.write("</td></tr></table>");
}
Also used : HTMLWriter(web.HTMLWriter) SQLException(java.sql.SQLException)

Example 58 with HTMLWriter

use of web.HTMLWriter in project common by zenlunatics.

the class Survey method formOpen.

// ----------------------------------------------------------------------
private void formOpen(Request request) throws IOException {
    String answer_table = "survey_answers" + m_id;
    View answers = request.site.newView(answer_table, request);
    ViewState.setShowReturnLink(answer_table, false, request);
    HTMLWriter writer = request.writer;
    if (getPageNum(request) == 0) {
        writer.write("<table align=\"center\" style=\"width:600px\"><tr><td>");
        if (m_show_title) {
            writer.tag("h3", m_title);
            writer.write("<p style=\"width:600px\">");
            if (m_description != null)
                writer.write(m_description);
            writer.write("</p>");
            if (m_write_settings_text) {
                writer.write("<p style=\"width:600px\">");
                if (m_one_answer)
                    writer.write("After you save your answers you may edit them again later. ");
                if (!m_show_answers)
                    writer.write("Your answers will not be viewable by others. ");
                else {
                    writer.write("Your answers will be viewable by others. ");
                    if (m_show_names)
                        writer.write("Your name will be shown with your answers, you do not need to type it. ");
                    else
                        writer.write("Your answers are anonymous, your name will not be shown with your answers. You may type your name at the end of each answer if you want to sign them. ");
                }
                writer.write("</p>");
            }
        }
        writer.write("</td></tr><tr><td id=\"survey_page\" survey_id=\"").write(m_id).write("\">");
    }
    Integer id = (Integer) request.getSessionAttribute("survey" + m_id + "_id");
    if (id != null) {
        answers.setWhere("id=" + id);
        answers.select();
        answers.next();
        m_form = answers.newEditForm();
        m_form.open();
    } else if (request.db.getTable(answer_table).getColumn("_owner_") != null) {
        answers.setWhere("surveys_id=" + m_id + " AND _owner_=" + request.getUser().getId());
        answers.select();
        if (answers.next()) {
            m_form = answers.newEditForm();
            m_form.open();
        } else {
            m_form = answers.newAddForm();
            m_form.open();
            writer.hiddenInput("surveys_id", Integer.toString(m_id));
            writer.hiddenInput("_owner_", Integer.toString(request.getUser().getId()));
        }
    } else {
        m_form = answers.newAddForm();
        m_form.open();
        writer.hiddenInput("surveys_id", Integer.toString(m_id));
    }
    writer.setAttribute("style", "padding:0 10px");
    writer.tagOpen("div");
    try {
        m_questions = request.db.select(new Select("*").from("survey_questions").whereEquals("surveys_id", m_id).orderBy("_order_"));
        if (!m_questions.isBeforeFirst()) {
            m_questions.getStatement().close();
            throw new RuntimeException("no questions defined");
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : HTMLWriter(web.HTMLWriter) SQLException(java.sql.SQLException) Select(db.Select) View(db.View)

Example 59 with HTMLWriter

use of web.HTMLWriter in project common by zenlunatics.

the class TagsColumn method writeInput.

// --------------------------------------------------------------------------
@Override
public boolean writeInput(View view, Form form, ColumnBase<?> column, View.Mode mode, String default_value, Request request) throws IOException {
    String tags_table = m_table + "_tags";
    String many_many_table = m_table + "_" + tags_table;
    HTMLWriter writer = request.writer;
    writer.setAttribute("id", column.getName());
    writer.textInput(column.getName(), null, null);
    writer.scriptOpen();
    writer.write("JS.get('tags',function(){var tl=new TextboxList('").write(column.getName()).write("',{bitsOptions:{editable:{addOnBlur:true}},encode:function(a){var n=[];for(var i=0,l=a.length;i<l;i++)n[i]=a[i][1];return n.join('\t');},plugins:{autocomplete:{maxResults:-1}}})");
    if (mode == View.Mode.EDIT_FORM) {
        List<String> tags = request.db.readValues(new Select("tag").from(tags_table + " JOIN " + many_many_table + " ON " + many_many_table + "." + tags_table + "_id=" + tags_table + ".id").where(many_many_table + "." + m_table + "_id=" + view.data().getString("id")));
        for (String tag : tags) writer.write(".add(").jsString(tag).write(")");
    }
    writer.write(";tl.plugins['autocomplete'].setValues([");
    List<String> tags = request.db.readValues(new Select("tag").distinct().from(tags_table).orderBy("tag"));
    for (int i = 0, n = tags.size(); i < n; i++) {
        if (i > 0)
            writer.write(',');
        writer.write("[null,").jsString(tags.get(i)).write(",null]");
    }
    writer.write("]);tl.container.setStyle('max-width',tl.container.getSize().x);$('").write(column.getName()).write("').validate=function(){tl.blur();return true;};});");
    writer.scriptClose();
    return true;
}
Also used : HTMLWriter(web.HTMLWriter) Select(db.Select)

Example 60 with HTMLWriter

use of web.HTMLWriter in project common by zenlunatics.

the class MenuItemsInputRenderer method writeInput.

// --------------------------------------------------------------------------
@Override
public boolean writeInput(View view, Form form, ColumnBase<?> column, View.Mode mode, String default_value, Request request) throws IOException {
    HTMLWriter writer = request.writer;
    if (mode == View.Mode.ADD_FORM) {
        writer.jsFunction("handle_menu_choice", "s", "if(s.selectedIndex==s.options.length-1){" + "s.style.display='none';" + "$('hidden_name').setProperty('name','');" + "$('name_input').setProperty('name','name');" + "$('hidden_name').style.display='none';" + "$('hidden_url').setProperty('name','');" + "$('url_input').setProperty('name','url');" + "$('hidden_url').style.display='none';" + "$('miir').style.display='';" + "}else{" + "$('hidden_name').value=s.options[s.selectedIndex].text;" + "$('hidden_url').value=s.options[s.selectedIndex].value;" + "}");
        writer.write("<table id=\"miir\" border=\"0\" style=\"display:none\"><tr><td class=\"form_label\">name</td><td>");
        writer.setAttribute("id", "name_input");
        writer.textInput(null, "30", null);
        writer.write("</td></tr><tr><td class=\"form_label\">url</td><td>");
        writer.setAttribute("id", "url_input");
        writer.textInput(null, null, null);
        writer.write("</tr></table>");
        writer.setAttribute("id", "hidden_name");
        Page page = m_all_pages.iterator().next();
        writer.hiddenInput("name", page.getName());
        writer.setAttribute("id", "hidden_url");
        writer.hiddenInput("url", page.getURL());
        new Select(null, m_all_pages).setOnChange("handle_menu_choice(this)").write(request);
    } else {
        writer.write("<table border=\"0\"><tr><td class=\"form_label\">name</td><td>");
        writer.textInput("name", "30", view.data().getString("name"));
        writer.write("</td></tr><tr><td class=\"form_label\">url</td><td>");
        writer.textInput("url", null, view.data().getString("url"));
        writer.write("</tr></table>");
    }
    return true;
}
Also used : HTMLWriter(web.HTMLWriter) Select(web.Select) Page(app.Page)

Aggregations

HTMLWriter (web.HTMLWriter)109 Select (db.Select)18 SQLException (java.sql.SQLException)16 ResultSet (java.sql.ResultSet)14 Table (web.Table)12 JDBCTable (db.JDBCTable)10 Person (app.Person)7 Calendar (java.util.Calendar)7 ArrayList (java.util.ArrayList)6 JDBCColumn (db.JDBCColumn)5 View (db.View)5 DBObject (db.DBObject)4 Form (db.Form)4 File (java.io.File)4 AdminTask (web.AdminTask)4 FilePathStringBuilder (web.FilePathStringBuilder)4 Select (web.Select)4 Mode (db.View.Mode)3 IOException (java.io.IOException)3 NumberFormat (java.text.NumberFormat)3