<!-- This page defines the form to create or edit an instance of this module -->
<!-- It is used from /course/mod.php.The whole instance is available as $form. -->

<?php 

require_once("$CFG->dirroot/mod/hotpot/lib.php");

// set default values in form fields, if necessary
set_form_fields($form);

// commonly used array of <select> options
$yes_no_options = array(
    HOTPOT_NO => get_string("no"), 
    HOTPOT_YES => get_string("yes")
);
$text_source_options = array(
    HOTPOT_TEXTSOURCE_QUIZ => get_string("textsourcequiz", "hotpot"), 
    HOTPOT_TEXTSOURCE_FILENAME => get_string("textsourcefilename", "hotpot"),
    HOTPOT_TEXTSOURCE_FILEPATH => get_string("textsourcefilepath", "hotpot"),
    HOTPOT_TEXTSOURCE_SPECIFIC => get_string("textsourcespecific", "hotpot")
);
?>
<script type="text/javascript">
//<![CDATA[
    var nameoffvalues = new Array(0,1,2);
    var namelockitems = new Array('name');

    var timeoffvalues = new Array('0'); // needs to be '0', not just 0
    var timeopenlockitems = new Array('openday','openmonth','openyear','openhour', 'openminute');
    var timecloselockitems = new Array('closeday','closemonth','closeyear','closehour','closeminute');

    var feedbackoffvalues = new Array(0,3,4,5);
    var feedbacklockitems = new Array('studentfeedbackurl');

    function hotpot_lockoptions(form, master, subitems, offvalues) {
        var disabled = false;
        var Frm = document.forms[form];
        if (Frm) {
            var obj = Frm.elements[master];
            if (obj) {
                var v = '';
                if (obj.type=='select-one') {
                    var v = obj.options[obj.selectedIndex].value;
                    for (i=0; i<offvalues.length; i++) {
                        if (v==offvalues[i]) {
                            disabled = true;
                        }
                    }
                } else if (obj.type=='checkbox') {
                    if (obj.checked==false) {
                        disabled = true;
                    }
                }
            }
            for (i=0; i<subitems.length; i++) {
                var obj = Frm.elements[subitems[i]];
                if (obj) {
                    obj.disabled = disabled;
                }
            }
        }
        return(true);
    }
//]]>
</script>
<center>
<form id="form" method="post" action="mod.php">
<table cellpadding="5">
    <tr valign="top">
        <td align="right"><b><?php print_string("name") ?>:</b></td>
        <td align="left"><?php
            if ($form->mode=='add') {
                choose_from_menu(
                    $text_source_options, "namesource", "$form->namesource", "", 
                    "return hotpot_lockoptions('form', 'namesource', namelockitems, nameoffvalues)"
                );
            } else {
                print '<input type="hidden" name="namesource" value="'.HOTPOT_TEXTSOURCE_SPECIFIC.'" />';
            }
            print '<input type="text" name="name" size=40 value="'.$form->name.'">';
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right">
            <b><?php 
                print_string("summary") 
            ?>:</b><br />
            <font size="1"><?php
                helpbutton("summary", get_string("summary"), "resource", true, true);
                print '<br />';
                helpbutton("writing", get_string("helpwriting"), "moodle", true, true);
                print '<br />';
                if (isset($usehtmleditor) && $usehtmleditor) {
                    helpbutton("richtext", get_string("helprichtext"), "moodle", true, true);
                    print '<br />';
                } else {
                    helpbutton("text", get_string("helptext"), "moodle", true, true); 
                    print '<br />';
                    emoticonhelpbutton("form", "description");
                }
            ?></font>
            </span>
        </td>
        <td align="left"><?php 
            if ($form->mode=='add') {
                choose_from_menu($text_source_options, "summarysource", "$form->summarysource", "");
                print '<br />';
            } else {
                print '<input type="hidden" name="summarysource" value="'.HOTPOT_TEXTSOURCE_SPECIFIC.'" />';
            }
            if (function_exists("print_textarea") && isset($usehtmleditor)) {
                print_textarea($usehtmleditor, 10, 65, 680, 400, "summary", $form->summary);
            } else {
                // Moodle 1.1.1 (original size was rows="5" cols="50")
                print '<textarea name="summary" rows="10" cols="65" wrap="virtual">'.$form->summary.'</textarea>';
            }
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("quizopen", "quiz") ?>:</b></td>
        <td align="left"><?php
            if (!$form->timeopen && $course->format == "weeks") {
                $form->timeopen = $course->startdate + (($form->section - 1) * 608400);
            }
            print '<input name="enabletimeopen" type="checkbox" value="1" alt="'.get_string("quizopen", "quiz").'" onclick="'."return hotpot_lockoptions('form', 'enabletimeopen', timeopenlockitems)".'" '.(empty($form->timeopen) ? '' : 'checked="checked"').' />';
            print_date_selector("openday", "openmonth", "openyear", $form->timeopen);
            print ' - ';
            print_time_selector("openhour", "openminute", $form->timeopen);
            helpbutton("timeopen", get_string("quizopen","quiz"), "quiz");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("quizclose", "quiz") ?>:</b></td>
        <td align="left"><?php
            if (!$form->timeclose && $course->format == "weeks") {
                $form->timeclose= $course->startdate + (($form->section) * 608400);
            }
            print '<input name="enabletimeclose" type="checkbox" value="1" alt="'.get_string("quizclose", "quiz").'" onclick="'."return hotpot_lockoptions('form', 'enabletimeclose', timecloselockitems)".'" '.(empty($form->timeclose) ? '' : 'checked="checked"').' />';
            print_date_selector("closeday", "closemonth", "closeyear", $form->timeclose);
            print ' - ';
            print_time_selector("closehour", "closeminute", $form->timeclose);
            helpbutton("timeopen", get_string("quizclose","quiz"), "quiz");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right" nowrap>
            <b><?php print get_string("location","hotpot") ?>:</b>
        </td>
        <td align="left"><?php

            if (has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
                $site = get_site();
                if ($course->id==$site->id) {
                    $id = $site->id;
                    $location = HOTPOT_LOCATION_SITEFILES;
                } else {
                    $id = "'+(getObjValue(this.form.location)==".HOTPOT_LOCATION_SITEFILES."?".$site->id.":".$course->id.")+'";
                    $location = '';
                }
            } else { // ordinary teacher or content creator
                    $id = $course->id;
                    $location = HOTPOT_LOCATION_COURSEFILES;
            }

            if (array_key_exists($location, $HOTPOT_LOCATION)) {
                print '<input type="hidden" name="location" value="'.$location.'" />';
                print '<i><font size="-1">'.$HOTPOT_LOCATION[$location].'</font></i> &nbsp; ';

            } else { // admin can select from "site" or "course" files
                choose_from_menu($HOTPOT_LOCATION, "location", "$form->location", "");
            }


            if (function_exists("button_to_popup_window")) {

                // use javascript to extract wdir from the reference field
                $wdir = "'+getDir(getObjValue(this.form.reference))+'";

                // set button url depending on Moodle version
                if ($CFG->version < 2004083125) { // version may need refining
                    // up to and including Moodle 1.4.1
                    $url = "/mod/resource/coursefiles.php?id=$id&wdir=$wdir";
                } else {
                    // Moodle 1.4.2 and beyond
                    $url = "/files/index.php?id=$id&wdir=$wdir&choose=form.reference";
                }

                $strchooseafile = get_string("chooseafile", "resource");
                button_to_popup_window ($url, 'coursefiles', $strchooseafile, 500, 750, $strchooseafile);
            }
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right" nowrap>
            <b><?php print_string("filename", "resource") ?>:</b>
        </td>
        <td align="left"><?php
            if (function_exists("button_to_popup_window")) {
                print '<input name="reference" size="50" value="'.$form->reference.'">&nbsp;';

            } else if (function_exists("get_directory_list")) {
                // Moodle 1.1 (and perhaps some others)
                $dirs = get_directory_list("$CFG->dataroot/$course->id");
                $options = array();
                foreach ($dirs as $dir) {
                    $options["$dir"] = $dir;
                }
                choose_from_menu ($options, "reference", $form->reference);

            } else {
                // a very old Moodle (may be none left :-)
                print '<input name="reference" size="50" value="'.$form->reference.'">&nbsp;';
            }
            print '<br />';
            hotpot_print_show_links($form->course, $form->location, $form->reference, '', ' &nbsp; ', true);
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php 
            $quizchain = "{$form->mode}quizchain";
            print_string($quizchain, "hotpot"); 
        ?>:</b></td>
        <td align="left"><?php
            choose_from_menu($yes_no_options, "quizchain", $form->quizchain, "");
            helpbutton($quizchain, get_string($quizchain,"hotpot"), "hotpot");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("outputformat", "hotpot") ?>:</b></td>
        <td align="left"><?php
            choose_from_menu($HOTPOT_OUTPUTFORMAT, "outputformat", "$form->outputformat", "");
            helpbutton("outputformat", get_string("outputformat","hotpot"), "hotpot");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("navigation", "hotpot") ?>:</b></td>
        <td align="left"><?php
            choose_from_menu($HOTPOT_NAVIGATION, "navigation", "$form->navigation", "");
            helpbutton("navigation", get_string("navigation","hotpot"), "hotpot");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("studentfeedback", "hotpot") ?>:</b></td>
        <td align="left"><?php
            choose_from_menu(
                $HOTPOT_FEEDBACK, "studentfeedback", "$form->studentfeedback", "",
                "return hotpot_lockoptions('form', 'studentfeedback', feedbacklockitems, feedbackoffvalues)"
            );
			print '<span id="studentfeedbackurlSPAN">';
            print "<input name=\"studentfeedbackurl\" size=\"50\" value=\"$form->studentfeedbackurl\">";
			print '</span>';
            helpbutton("studentfeedback", get_string("studentfeedback","hotpot"), "hotpot");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("forceplugins", "hotpot") ?>:</b></td>
        <td align="left"><?php
            choose_from_menu($yes_no_options, "forceplugins", "$form->forceplugins", "");
            helpbutton("forceplugins", get_string("forceplugins","hotpot"), "hotpot");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("shownextquiz", "hotpot") ?>:</b></td>
        <td align="left"><?php
            choose_from_menu($yes_no_options, "shownextquiz", "$form->shownextquiz", "");
            helpbutton("shownextquiz", get_string("shownextquiz","hotpot"), "hotpot");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("allowreview", "quiz") ?>:</b></td>
        <td align="left"><?php
            choose_from_menu($yes_no_options, "review", "$form->review", "");
            helpbutton("review", get_string("allowreview","quiz"), "quiz");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("maximumgrade") ?>:</b></td>
        <td align="left"><?php
            $options = array();
            for ($i=100; $i>=1; $i--) {
                $options[$i] = $i;
            }
            $options[0] = get_string("nograde");

            choose_from_menu($options, "grade", "$form->grade", "");
            helpbutton("maxgrade", get_string("maximumgrade"), "quiz");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("grademethod", "quiz") ?>:</b></td>
        <td align="left"><?php
            choose_from_menu($HOTPOT_GRADEMETHOD, "grademethod", "$form->grademethod", "");
            helpbutton("grademethod", get_string("grademethod","quiz"), "quiz");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("attemptsallowed", "quiz") ?>:</b></td>
        <td align="left"><?php
            $options = array(
                0 => get_string("attemptsunlimited", "quiz"),
                1 => '1 '.strtolower(get_string("attempt", "quiz"))
            );
            for ($i=2; $i<=10; $i++) {
                $options[$i] = "$i ".strtolower(get_string("attempts", "quiz"));
            }
            choose_from_menu($options, "attempts", "$form->attempts", "");
            helpbutton("attempts", get_string("attemptsallowed","quiz"), "quiz");
        ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("requirepassword", "quiz") ?>:</b></td>
        <td align="left">
            <input type="text" name="password" size=40 value="<?php p($form->password) ?>">
            <?php helpbutton("requirepassword", get_string("requirepassword", "quiz"), "quiz"); ?>
        </td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("requiresubnet", "quiz") ?>:</b></td>
        <td align="left">
            <input type="text" name="subnet" size=40 value="<?php p($form->subnet) ?>">
            <?php helpbutton("requiresubnet", get_string("requiresubnet", "quiz"), "quiz"); ?>
        </td>
    </tr>
    <tr valign="top">
        <td align="right"><b><?php print_string("clickreporting", "hotpot") ?>:</b></td>
        <td align="left"><?php
            choose_from_menu($yes_no_options, "clickreporting", "$form->clickreporting", "");
            helpbutton("clickreporting", get_string("clickreporting","hotpot"), "hotpot");
        ?></td>
    </tr>
<?php 
    print_standard_coursemodule_settings($form); 
?>
    <tr valign="top">
        <td align="right">&nbsp;</td>
        <td align="left">
            <!-- buttons -->
            <input type="submit" value="<?php print_string("savechanges") ?>"> &nbsp; &nbsp;
            <input type="submit" name="cancel" value="<?php print_string("cancel") ?>" /><br />
            <?php
                if (isset($CFG->release) && substr($CFG->release, 0, 3)>=1.5) {
                    $options = array(
                        HOTPOT_DISPLAYNEXT_QUIZ => get_string("displayhotpotnext", "hotpot"),
                        HOTPOT_DISPLAYNEXT_COURSE => get_string("displaycoursenext", "hotpot"),
                        HOTPOT_DISPLAYNEXT_INDEX => get_string("displayindexnext", "hotpot")
                    );
                    choose_from_menu($options, "displaynext", "$form->displaynext", "");
                } else {
                    print '<input type="hidden" name="displaynext" value="'.HOTPOT_NO.'" />'."\n";
                }
            ?>
</table>

<!-- hidden fields -->
<input type="hidden" name="course"        value="<?php p($form->course) ?>">
<input type="hidden" name="coursemodule"  value="<?php p($form->coursemodule) ?>">
<input type="hidden" name="section"       value="<?php p($form->section) ?>">
<input type="hidden" name="module"        value="<?php p($form->module) ?>">
<input type="hidden" name="modulename"    value="<?php p($form->modulename) ?>">
<input type="hidden" name="instance"      value="<?php p($form->instance) ?>">
<input type="hidden" name="mode"          value="<?php p($form->mode) ?>">
<?php if (isset($USER->sesskey)) { ?>
<input type="hidden" name="sesskey"       value="<?php p($USER->sesskey) ?>">
<?php } ?>

</form>
</center>
<script type="text/javascript">
<!--
    hotpot_lockoptions('form', 'namesource', namelockitems, nameoffvalues);
    hotpot_lockoptions('form', 'enabletimeopen', timeopenlockitems, timeoffvalues);
    hotpot_lockoptions('form', 'enabletimeclose', timecloselockitems, timeoffvalues);
    hotpot_lockoptions('form', 'studentfeedback', feedbacklockitems, feedbackoffvalues);

    // override the standard Moodle "setfocus" function, 
    // which gives a js error if the "name" field is hidden
    function setfocus() {
    	var Frm = document.getElementById('form');
    	if (Frm) {
    		if (canfocus(Frm, 'namesource')) {
    			Frm.namesource.focus();
    		} else if (canfocus(Frm, 'name')) {
    			Frm.name.focus();
    		}
    	}
    }
    function canfocus(Frm, name) {
    	var obj = eval('Frm.' + name);
    	return (obj==null || obj.type=='hidden' || obj.focus==null || (obj.style && obj.style.display=='none')) ? false : true;
    }
//-->
</script>
<?php

// ======================
//  functions
// ======================

function set_form_fields(&$form) {
    set_form_field($form, 'name');
    set_form_field($form, 'summary');
    set_form_field($form, 'timeopen', get_user_preferences('hotpot_timeopen', 0));
    set_form_field($form, 'enabletimeopen', empty($form->timeopen) ? HOTPOT_NO : HOTPOT_YES);
    set_form_field($form, 'timeclose', get_user_preferences('hotpot_timeclose', 0));
    set_form_field($form, 'enabletimeclose', empty($form->timeclose) ? HOTPOT_NO : HOTPOT_YES);
    set_form_field($form, 'location', HOTPOT_LOCATION_COURSEFILES);
    set_form_field($form, 'reference');
    set_form_field($form, 'navigation', get_user_preferences('hotpot_navigation', HOTPOT_NAVIGATION_BAR));
    set_form_field($form, 'outputformat', get_user_preferences('hotpot_outputformat', HOTPOT_OUTPUTFORMAT_BEST));
    set_form_field($form, 'studentfeedback', get_user_preferences('hotpot_studentfeedback', HOTPOT_FEEDBACK_NONE));
    set_form_field($form, 'studentfeedbackurl', get_user_preferences('hotpot_studentfeedbackurl', 'http://'));
    set_form_field($form, 'forceplugins', get_user_preferences('hotpot_forceplugins', HOTPOT_NO));
    if ($form->mode=='add') {
        set_form_field($form, 'namesource', get_user_preferences('hotpot_namesource', HOTPOT_TEXTSOURCE_QUIZ));
        set_form_field($form, 'summarysource', get_user_preferences('hotpot_summarysource', HOTPOT_TEXTSOURCE_QUIZ));
        $quizchain = get_user_preferences('hotpot_quizchain', HOTPOT_NO);
    } else {
        $quizchain = empty($form->quizchain) ? HOTPOT_NO : HOTPOT_YES;
    }
    set_form_field($form, 'quizchain', $quizchain);
    set_form_field($form, 'shownextquiz', get_user_preferences('hotpot_shownextquiz', HOTPOT_NO));
    set_form_field($form, 'review', get_user_preferences('hotpot_review', HOTPOT_YES));
    set_form_field($form, 'grade', get_user_preferences('hotpot_grade', 100));
    set_form_field($form, 'grademethod', get_user_preferences('hotpot_grademethod', HOTPOT_GRADEMETHOD_HIGHEST));
    set_form_field($form, 'attempts', get_user_preferences('hotpot_attempts', 0)); // 0=unlimited
    set_form_field($form, 'password');
    set_form_field($form, 'subnet', get_user_preferences('hotpot_subnet'));
    set_form_field($form, 'clickreporting', HOTPOT_NO);
    set_form_field($form, 'displaynext', get_user_preferences('hotpot_displaynext', HOTPOT_DISPLAYNEXT_QUIZ));
}
function set_form_field(&$form, $fieldname, $defaultvalue='') {
    if (!isset($form->$fieldname)) {
        $form->$fieldname = $defaultvalue;
    }
}
?>
