Exemplo n.º 1
0
function latexAppendQuestion($name, $desc, $type, $answer)
{
    $typeArray = getTypeArray($type);
    $question_string = "";
    if ($typeArray['type'] == "text") {
        if ($name != "") {
            $question_string .= '\\textbf{' . latexSpecialChars($name) . '}';
            //add main in bold
        }
        if ($desc != "") {
            if ($name != "") {
                $question_string .= '\\newline';
            }
            $question_string .= '\\emph{' . latexSpecialChars($desc) . '}';
            //add description in italics
        }
        $question_string .= '\\newline\\newline';
        return $question_string;
    } else {
        if ($typeArray['type'] == "latex") {
            $question_string .= $desc;
            return $question_string;
        } else {
            if ($typeArray['type'] == "code") {
                $question_string .= '\\text{' . get_html_to_latex(page_convert($desc)) . '}';
                return $question_string;
            } else {
                if ($typeArray['type'] == "repeat") {
                    $num = $typeArray['num'];
                    $subtype_array = explode("|", $typeArray['subtype']);
                    $desc_array = explode("|", $desc);
                    $name_array = explode("|", $name);
                    if ($answer != '') {
                        $answer_array = toArray($answer, "|", "=");
                    } else {
                        $answer_array = array_fill(0, count($name_array) * $num, '');
                    }
                    //find minimum length, which will be the number to repeat for
                    $min_length = min(count($subtype_array), count($desc_array), count($name_array));
                    for ($i = 0; $i < $min_length * $num; $i++) {
                        $index = $i % $min_length;
                        $n = intval($i / $min_length);
                        $thisName = getRepeatThisValue($name_array, $index, $n);
                        $thisDesc = getRepeatThisValue($desc_array, $index, $n);
                        $thisType = str_replace(",", ";", getRepeatThisValue($subtype_array, $index, $n));
                        $question_string .= latexAppendQuestion($thisName, $thisDesc, $thisType, $answer_array[$i]);
                    }
                } else {
                    if ($name != "") {
                        $question_string .= '\\textbf{' . latexSpecialChars($name) . '}';
                        //add question in bold
                    }
                    //add description (in bold) for essays and short answer
                    if (($typeArray['type'] == "essay" || $typeArray['type'] == "short") && $desc != "") {
                        if ($name != "") {
                            $question_string .= '\\newline';
                        }
                        $question_string .= '\\emph{' . latexSpecialChars($desc) . '}';
                        //add description in italics
                    }
                    //add a separator depending on main type of the question
                    if ($typeArray['type'] == "essay") {
                        $question_string .= "\n\n";
                    }
                    if ($typeArray['type'] == "select" && $typeArray['method'] != "dropdown") {
                        //in this case, we add tick marks and check the correct ones
                        $choices = explode(";", $desc);
                        //get answer as array in case we're using multiple selection
                        $config = $GLOBALS['config'];
                        $answerArray = explode($config['form_array_delimiter'], $answer);
                        //this is used to indent the answer choices
                        $question_string .= "\n\\begin{quote}\n";
                        //output each choice with check box before it on a separate line in the quote
                        for ($i = 0; $i < count($choices); $i++) {
                            $choice = $choices[$i];
                            if ($i != 0) {
                                $question_string .= "\\\\\n ";
                            }
                            if (in_array($choice, $answerArray)) {
                                $question_string .= '\\xbox';
                            } else {
                                $question_string .= '\\tickbox';
                            }
                            $question_string .= " \\hspace{4pt} " . latexSpecialChars($choice);
                        }
                        $question_string .= "\\end{quote}";
                    } else {
                        //append the response
                        if ($typeArray['type'] == "essay") {
                            if ($answer != "") {
                                $question_string .= '\\begin{quote} ' . latexSpecialChars($answer) . '\\end{quote}';
                            } else {
                                $question_string .= '\\vspace{5ex}';
                            }
                        } else {
                            if ($answer != "") {
                                $question_string .= '\\begin{quote} ' . latexSpecialChars($answer) . ' \\end{quote}';
                            } else {
                                $question_string .= '\\vspace{1ex}';
                            }
                        }
                    }
                    $question_string .= "\n\n";
                }
            }
        }
    }
    return $question_string;
}
Exemplo n.º 2
0
function writeField($id, $answer_id, $name, $desc, $type, $answer = "", $mutable = true, $repeat_id = 256)
{
    //see if style provides this function
    $styleFunction = style_function("writeField");
    if ($styleFunction !== FALSE) {
        return $styleFunction($str);
    }
    $mutableString = "";
    if (!$mutable) {
        $mutableString = "readonly=\"readonly\"";
    }
    //trim the string fields
    $name = trim($name);
    $desc = trim($desc);
    $type = trim($type);
    $fieldName = "a_" . $id . "_" . $answer_id . "_" . $repeat_id;
    $type_array = getTypeArray($type);
    $maxLength = $type_array['length'];
    $lengthRemaining = $maxLength - strlen($answer);
    if ($type_array['type'] == "essay") {
        $height = "";
        if ($type_array['size'] == "large") {
            $height = "height:200px";
        } else {
            if ($type_array['size'] == "huge") {
                $height = "height:400px";
            }
        }
        echo '<label for="">';
        if ($type_array['status'] != "optional") {
            echo "<em>*</em>";
        }
        echo "{$name}</label>";
        echo "<textarea ";
        if ($type_array['showchars']) {
            echo "onKeyDown=\"limitText(this.form.{$fieldName}, this.form.countdown{$fieldName}, {$maxLength});\" ";
            echo "onKeyUp=\"limitText(this.form.{$fieldName}, this.form.countdown{$fieldName}, {$maxLength});\" ";
        }
        echo "name=\"{$fieldName}\" {$mutableString} style=\"resize:vertical;{$height}\" />" . htmlspecialchars($answer) . "</textarea>";
        echo "<p class=\"formHint\">{$desc}";
        if ($type_array['showchars']) {
            echo "<br />Characters Remaining: <input type=\"text\" name=\"countdown{$fieldName}\" style=\"background-color:none;border:solid 1px #FFFFFF;font-size:10px;max-width:50px;color:#71777D\" value=\"{$lengthRemaining}\" readonly=\"readonly\" class=\"changeBackground\" />";
        }
        echo "</p>";
    } else {
        if ($type_array['type'] == "short") {
            echo '<label for="">';
            if ($type_array['status'] != "optional") {
                echo "<em>*</em>";
            }
            echo "{$name}</label>";
            echo "<input ";
            if ($type_array['showchars']) {
                echo "onKeyDown=\"limitText(this.form.{$fieldName}, this.form.countdown{$fieldName}, {$maxLength});\" ";
                echo "onKeyUp=\"limitText(this.form.{$fieldName}, this.form.countdown{$fieldName}, {$maxLength});\" maxlength=\"{$maxLength}\" ";
            }
            echo "type=\"text\" name=\"{$fieldName}\" {$mutableString} value=\"" . htmlspecialchars($answer) . "\" /> ";
            echo "<p class=\"formHint\">{$desc}";
            if ($type_array['showchars']) {
                echo "<br />Characters remaining: <input type=\"text\" name=\"countdown{$fieldName}\" style=\"background-color:none;border:solid 1px #FFFFFF;font-size:10px;max-width:50px;color:#71777D\" value=\"{$lengthRemaining}\" readonly=\"readonly\" class=\"changeBackground\" />";
            }
            echo "</p>";
        } else {
            if ($type_array['type'] == "select") {
                echo '<p class="label">';
                if ($type_array['status'] != "optional") {
                    echo "<em>*</em>";
                }
                echo "{$name}</p>";
                $choices = explode(";", $desc);
                $tname = "checkbox";
                if ($type_array['method'] == "multiple") {
                    $tname = "checkbox";
                    $fieldName .= "[]";
                    //for multiple selection, PHP needs to know with an [] at the end of field name
                    echo "<ul>";
                } else {
                    if ($type_array['method'] == "single") {
                        $tname = "radio";
                        echo "<ul>";
                    } else {
                        if ($type_array['method'] == "dropdown") {
                            $tname = false;
                            echo "<select name=\"{$fieldName}\"{$mutableString}>";
                        }
                    }
                }
                //for checkboxes, answer will be an array separated by $config['form_array_delimiter']
                // we just explode it anyway for convenience and get one element if it's single selection
                $config = $GLOBALS['config'];
                $answerArray = explode($config['form_array_delimiter'], $answer);
                foreach ($choices as $choice) {
                    $selectedString = "";
                    if (in_array($choice, $answerArray)) {
                        if ($tname === false) {
                            $selectedString = " selected";
                        } else {
                            $selectedString = " checked";
                        }
                    }
                    if ($tname == false) {
                        echo "<option{$selectedString} value=\"{$choice}\">{$choice}</option>";
                    } else {
                        echo "<li><label for=\"\"><input{$selectedString} type=\"{$tname}\" name=\"{$fieldName}\"{$mutableString} value=\"{$choice}\" /> {$choice}</label></li>";
                    }
                }
                if ($tname == false) {
                    //select
                    echo "</select>";
                } else {
                    echo "</ul>";
                }
                //need to add some sort of hint factor for selects (change hint so that it is a type variable)
            } else {
                if ($type_array['type'] == "text") {
                    echo '<label for="">';
                    if ($type_array['status'] != "optional") {
                        echo "<em>*</em>";
                    }
                    echo "{$name}</label>{$desc}";
                } else {
                    if ($type_array['type'] == "repeat") {
                        $num = $type_array['num'];
                        $subtype_array = explode("|", $type_array['subtype']);
                        $desc_array = explode("|", $desc);
                        $name_array = explode("|", $name);
                        if ($answer != '') {
                            $answer_array = toArray($answer, "|", "=");
                        } else {
                            $answer_array = array_fill(0, count($name_array) * $num, '');
                        }
                        //find minimum length, which will be the number to repeat for
                        $min_length = min(count($subtype_array), count($desc_array), count($name_array));
                        for ($i = 0; $i < $min_length * $num; $i++) {
                            $index = $i % $min_length;
                            $n = intval($i / $min_length);
                            $thisName = getRepeatThisValue($name_array, $index, $n);
                            $thisDesc = getRepeatThisValue($desc_array, $index, $n);
                            $thisType = str_replace(",", ";", getRepeatThisValue($subtype_array, $index, $n));
                            if ($type_array['globalstatus'] == 1) {
                                $thisType .= "; status:optional";
                            }
                            writeField($id, $answer_id, $thisName, $thisDesc, $thisType, $answer_array[$i], $mutable, $i);
                        }
                    } else {
                        if ($type_array['type'] == "code") {
                            echo page_convert($desc);
                        } else {
                            if ($type_array['type'] == "upload") {
                                echo '<label for="">';
                                if ($type_array['status'] != "optional") {
                                    echo "<em>*</em>";
                                }
                                echo "{$name}</label>";
                                echo "<input type=\"file\" name=\"{$fieldName}\" {$mutableString} />";
                                echo "<p class=\"formHint\">{$desc}<br />Currently Uploaded: ";
                                if ($answer != "") {
                                    $answer_parts = explode(":", $answer, 3);
                                    $file_id = $answer_parts[1];
                                    $file_name = $answer_parts[2];
                                    echo "<a href=\"../download.php?file={$file_id}&filename={$file_name}\">View Here</a>";
                                } else {
                                    echo "None";
                                }
                                echo '</p>';
                            }
                        }
                    }
                }
            }
        }
    }
}