<?php
    require_once($CFG->dirroot.'/mod/scorm/locallib.php');
    if (!isset($form->name)) {
        $form->name = '';
    }
    if (!isset($form->reference)) {
        $form->reference = '';
    }
    if (!isset($form->summary)) {
        $form->summary = '';
    }
    if (!isset($form->launch)) {
        $form->launch = '';
    }
    if (!isset($form->auto)) {
        $form->auto = '';
    }
    if (!isset($form->popup)) {
        $form->popup = 0;
    }
    if (!isset($form->datadir)) {
        $form->datadir = '';
    }
    if (!isset($form->maxgrade)) {
        $form->maxgrade = '';
    }
    if (!isset($form->grademethod)) {
        $form->grademethod = 0;
        $form->whatgrade = 0;
    } else {
        $form->whatgrade = intval($form->grademethod / 10);
        $form->grademethod = $form->grademethod % 10;
    }
    if (!isset($form->maxattempt)) {
        $form->maxattempt = 1;
    }
    if (!isset($form->hidebrowse)) {
        $form->hidebrowse = 0;
    }
    if (!isset($form->hidetoc)) {
        $form->hidetoc = 0;
    }
    if (!isset($form->hidenav)) {
        $form->hidenav = 0;
    }
    if (!isset($form->width)) {
        $form->width = $CFG->scorm_framewidth;
    }
    if ((strpos($form->width,'%') === false) && ($form->width <= 100)) {
        $form->width .= '%';
    }
    if (!isset($form->height)) {
        $form->height = $CFG->scorm_frameheight;
    }
    if ((strpos($form->height,'%') === false) && ($form->height <= 100)) {
        $form->height .= '%';
    }
    if (!isset($form->options) || empty($form->options)) {
        $form->options = $stdoptions;
    }
    $options = explode(',',$form->options);
    $window = new stdClass();
    foreach ($options as $option) {
        list($element,$value) = explode('=',$option);
        $element = trim($element);
        $window->$element = trim($value)==1?'checked':''; 
    }
    if (!isset($form->pkgtype)) {
        $form->pkgtype = '';
    }
    $scormid = '';
    if (!empty($form->instance)) {
        $scormid = '&instance='.$form->instance;
    }
    $datadir = '';
    if (!empty($form->datadir)) {
        $datadir = '&datadir='.$form->datadir;
    }
    $sessionkey = '';
    if (!empty($form->sesskey)) {
        $sessionkey = '&sesskey='.$form->sesskey;
    }

    $strfilename = get_string('coursepacket', 'scorm');
    $strchooseafile = get_string('chooseapacket', 'scorm');
    $strbrowserepository = get_string('browserepository', 'scorm');
    $striframe = get_string('iframe', 'scorm');
    $striframedisplay = get_string('iframedisplay', 'scorm');
    $strnewwindow = get_string('popup', 'scorm');
    $strnewwindowopen = get_string('popupopen', 'scorm');
    $strheight = get_string('height', 'scorm');
    $strwidth = get_string('width', 'scorm');
    $strresizable = get_string('resizable', 'scorm');
    $strscrollbars = get_string('scrollbars', 'scorm');
    $strdirectories = get_string('directories', 'scorm');
    $strlocation = get_string('location', 'scorm');
    $strmenubar = get_string('menubar', 'scorm');
    $strtoolbar = get_string('toolbar', 'scorm');
    $strstatus = get_string('statusbar', 'scorm');
?>

<script type="text/javascript" src="<?php p($CFG->wwwroot) ?>/mod/scorm/request.js" ></script>
<script type="text/javascript">
    function validate_scorm(theform,filename,confirmed) {
        var confirmedstr = '';
        if (confirmed == true) {
           confirmedstr = '&confirmed=true';
        }

        var myRequest = NewHttpReq();
        result = DoRequest(myRequest,"<?php p($CFG->wwwroot) ?>/mod/scorm/validate.php","id=<?php p($form->course) ?>&reference="+filename+"<?php echo $sessionkey.$scormid.$datadir ?>"+confirmedstr);
        //alert(result);
        results = result.split('\n');
        result = '';
        errorlogs = '';
        datadir = '';
        for (i=0;i<results.length;i++) {
            element = results[i].split('=');
            switch(element[0]) {
               case 'result':
                  result = element[1];
               break;
               case 'launch':
                  launch = element[1];
               break;
               case 'datadir':
                  datadir = element[1];
               break;
               case 'pkgtype':
                  pkgtype = element[1];
               break;
               case 'errorlogs':
                   i++;
                   do {
                      errorlogs.concat(results[i]+'\n');
                      i++;
                   } while (i<results.lenght());

               break;
            }
        }
        if ((result == "found") || (result == "regular")) {
            theform.datadir.value = datadir;
            theform.pkgtype.value = pkgtype;
            theform.launch.value = launch;
            if (theform.mode.value == 'add') {
                theform.parse.value = 1;
            } else if (launch == 0) {
                theform.parse.value = 1;
            }
            return true;
        } else {
            if (result == "confirm") {
                response = confirm("<?php print_string('confirmloosetracks','scorm') ?>");
                if (response == true) {
                    return validate_scorm(theform,filename,true);
                } else {
                    return false;
                }
            } else {
                result = '<?php print_string('validation','scorm') ?>: '+ result + '\n';
                if (errorlogs != '') {
                    result.concat('<?php print_string('errorlogs','scorm') ?>:\n'+errorlogs);
                }
                alert(result);
                return false;
            }
        }
    }

    function checkscormform (whatcheck,checkvalue,whatset) {
        if (whatcheck.options[whatcheck.selectedIndex].value == checkvalue) {
            whatset.disabled = true;
        } else {
            whatset.disabled = false;
        }
    }
    
    function showhide (id, set) {
        divobj = document.getElementById(id);
        butobj = document.getElementById(id+'button');
        prefobj = document.getElementById(id+'pref');
        if (set == true) {
            if (prefobj.value == '1') {
                divobj.style.display = 'block';
                butobj.value = '<?php print_string('hidesettings') ?>';
            } else {
                divobj.style.display = 'none';
                butobj.value = '<?php print_string('showsettings') ?>...';
            }
        } else {
            if (prefobj.value == '1') {
                divobj.style.display = 'none';
                butobj.value = '<?php print_string('showsettings') ?>...';
                prefobj.value = '0';
            } else {
                divobj.style.display = 'block';
                butobj.value = '<?php print_string('hidesettings') ?>';
                prefobj.value = '1';
            }
        }
    }
</script>

<form name="form" method="post" action="mod.php" onsubmit="return validate_scorm(document.form,document.form.reference.value,false,false);">
    <table cellpadding="5">
        <tr valign="top">
            <td align="right"><b><?php print_string('name') ?>:</b></td>
            <td>
                <input type="text" name="name" size="50" value="<?php p($form->name) ?>" alt="<?php print_string('name') ?>" />
            </td>
        </tr>
        <tr valign="top">
            <td align="right"><b><?php print_string('summary') ?>:</b><br />
                <?php helpbutton('summary', get_string('summary'), 'scorm', true, true) ?>
            </td>
            <td>
                <?php print_textarea($usehtmleditor, 10, 50, 680, 400, 'summary', $form->summary); ?>
            </td>
        </tr>
        <tr valign="top">
            <td align="right" nowrap="nowrap">
                <b><?php echo $strfilename?>:</b>
            </td>
            <td>
                <input name="reference" size="50" value="<?php echo $form->reference ?>" alt="<?php echo $strfilename ?>" />&nbsp;
                <?php
                    button_to_popup_window ('/files/index.php?id='.$course->id.'&amp;choose=form.reference',
                                            'coursefiles', $strchooseafile, 500, 750, $strchooseafile);
                    require_once($repositoryconfigfile);
                    if ($CFG->repositoryactivate) {
                        button_to_popup_window ($repositorybrowser.'?choose=form.reference',
                                                'browserepository', $strbrowserepository, 500, 750, $strbrowserepository);
                    }
                    helpbutton('package', get_string('coursepacket', 'scorm'), 'scorm', true);
                ?>
            </td>
        </tr>
        <tr valign="top">
            <td align="right"><b><?php print_string('grademethod', 'scorm') ?>:</b></td>
            <td>
            <?php
                choose_from_menu($SCORM_GRADE_METHOD, 'grademethod', (int) $form->grademethod, '','checkscormform(this,0,document.form.maxgrade);');
                helpbutton('grademethod', get_string('grademethod','scorm'), 'scorm');
            ?>
            </td>
        </tr>
        <tr valign="top">
            <td align="right"><b><?php print_string('maximumgrade') ?>:</b></td>
            <td>
            <?php
                for ($i=100; $i>=1; $i--) {
                    $grades[$i] = $i;
                }
                $disabled = $form->grademethod=='0';
                choose_from_menu($grades, 'maxgrade', (int) $form->maxgrade, '','','0',false,$disabled);
                helpbutton('maxgrade', get_string('maximumgrade'), 'scorm');
            ?>
            </td>
        </tr>
        <tr valign="top">
            <td align="right"><b><?php print_string('maximumattempts','scorm') ?>:</b></td>
            <td>
            <?php
                for ($i=1; $i<=$CFG->scorm_maxattempts; $i++) {
                    if ($i == 1) {
                        $attempts[$i] = $i . ' ' . get_string('attempt','scorm');
                    } else {
                        $attempts[$i] = $i . ' ' . get_string('attempts','scorm');
                    }
                }
                choose_from_menu($attempts, 'maxattempt', (int) $form->maxattempt, get_string('nolimit','scorm'),'checkscormform(this,1,document.form.whatgrade);');
                helpbutton('maxattempt', get_string('maximumattempts','scorm'), 'scorm');
            ?>
            </td>
        </tr>
        <tr valign="top">
            <td align="right"><b><?php print_string('whatgrade','scorm') ?>:</b></td>
            <td>
            <?php
                $disabled = (int) $form->maxattempt === 1;
                choose_from_menu($SCORM_WHAT_GRADE, 'whatgrade', (int) $form->whatgrade, '','','0',false,$disabled);
                helpbutton('whatgrade', get_string('whatgrade','scorm'), 'scorm');
            ?>
            </td>
        </tr>
        <tr><td colspan="2"><hr /></td></tr>

        <tr>
            <td align="right"><b><?php print_string('advanced', 'scorm') ?>:</b></td>
            <td>
                <input type="button" value="<?php print_string('hidesettings') ?>" id="advancedsettingsbutton" onclick="javascript: return showhide('advancedsettings');" />
                <input type="hidden" name="advancedsettingspref" id="advancedsettingspref" 
                       value="<?php echo get_user_preferences('scorm_advancedsettingspref', $CFG->scorm_advancedsettings); ?>" />
                <?php helpbutton('advanced', get_string('advanced', 'scorm'), 'scorm', true) ?>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <div id="advancedsettings">
                    <table align="center">
                        <tr>
                            <td align="right"><b><?php print_string('autocontinue','scorm') ?>:</b></td>
                            <td>
                            <?php
                                $options = array();
                                $options[0]=get_string('no');
                                $options[1]=get_string('yes');
                                choose_from_menu ($options, 'auto', (int) $form->auto,'');
                                helpbutton('autocontinue', get_string('autocontinue','scorm'), 'scorm', true);
                            ?>
                            </td>
                        </tr>
                        <tr>
                            <td align="right"><b><?php print_string('hidebrowse','scorm') ?>:</b></td>
                            <td>
                            <?php
                                $options = array();
                                $options[0]=get_string('no');
                                $options[1]=get_string('yes');
                                choose_from_menu ($options, 'hidebrowse', (int) $form->hidebrowse, '');
                                helpbutton('browsemode', get_string('hidebrowse','scorm'), 'scorm', true);
                            ?>
                            </td>
                        </tr>
                        <tr>
                            <td align="right"><b><?php print_string('hidetoc','scorm') ?>:</b></td>
                            <td>
                            <?php
                                $options = array();
                                $options[1]=get_string('hidden','scorm');
                                $options[0]=get_string('sided','scorm');
                                $options[2]=get_string('popupmenu','scorm');
                                choose_from_menu ($options, 'hidetoc', (int) $form->hidetoc, '');
                            ?>
                            </td>
                        </tr>
                        <tr>
                            <td align="right"><b><?php print_string('hidenav','scorm') ?>:</b></td>
                            <td>
                            <?php
                                $options = array();
                                $options[0]=get_string('no');
                                $options[1]=get_string('yes');
                                choose_from_menu ($options, 'hidenav', (int) $form->hidenav, '');
                            ?>
                            </td>
                        </tr>
                    </table>
                </div>
            </td>
        </tr>
        <tr>
            <td align="right"><b><?php print_string('window', 'scorm') ?>:</b></td>
            <td>
                <input type="button" value="<?php print_string('hidesettings') ?>" id="windowsettingsbutton" onclick="javascript: return showhide('windowsettings');" />
                <input type="hidden" name="windowsettingspref" id="windowsettingspref" 
                       value="<?php echo get_user_preferences('scorm_windowsettingspref', $CFG->scorm_windowsettings); ?>" />
                <?php helpbutton('window', get_string('window', 'scorm'), 'scorm', true) ?>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <div id="windowsettings">
                    <table align="center">
                        <tr valign="top">
                            <td>
                                <b><?php print_string('stagesize','scorm'); ?></b>
                                <?php helpbutton('size', get_string('stagesize', 'scorm'), 'scorm', true) ?><br />
                                <input name="width" type="text" size="4" value="<?php p($form->width) ?>" alt="<?php p($strwidth) ?>" />
                                <label for="width"><?php p($strwidth) ?></label><br />
                                <input name="height" type="text" size="4" value="<?php p($form->height) ?>" alt="<?php p($strheight) ?>" />
                                <label for="height"><?php p($strheight) ?></label>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td>
                                <script type="text/javascript">
                                    var popupitems = ["<?php echo implode('","',array_keys($SCORM_POPUP_OPTIONS)); ?>"];
                                </script>
                                <br /><b><?php print_string('display','scorm'); ?>:</b><br />
                                <input type="radio" 
                                       name="popup"
                                       value="0" alt="<?php p($striframe) ?>" 
                                       <?php echo ($form->popup == 0) ? "checked=\"checked\"" : "" ?> 
                                       onclick="return lockoptions('form', 'popup[1]', popupitems);"
                                />
                                <b title="<?php p($striframedisplay) ?>"><?php p($striframe) ?></b>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td>
                                <input name="popup" 
                                       type="radio" 
                                       value="1" 
                                       alt="<?php p($strnewwindow)?>" 
                                       <?php echo ($form->popup == 1) ? 'checked="checked"' : '' ?>
                                       onclick="return lockoptions('form', 'popup[1]', popupitems);"
                                />
                                <b title="<?php p($strnewwindowopen) ?>"><?php p($strnewwindow) ?></b>
                                <blockquote>
                                    <?php
                                        foreach ($window as $name => $value) {
                                            echo "<input name=\"h$name\" type=\"hidden\" value=\"0\"/>\n";
                                            echo "<input name=\"$name\" type=\"checkbox\" value=\"1\" ".$window->$name." alt=\"$name\" />\n";
                                            $stringname = "str$name";
                                            echo $$stringname."<br />\n";
                                        }
                                    ?>
                                    <script type="text/javascript">
                                        lockoptions('form','popup[1]', popupitems);
                                    </script>
                                </blockquote>
                            </td>
                        </tr>
                    </table>
                </div>
                <script language="javascript" type="text/javascript">
                    showhide('advancedsettings', true);
                    showhide('windowsettings', true);
                </script>
            </td>
        </tr>
    </table>
    <input type="hidden" name="datadir" value="<?php p($form->datadir) ?>" />
    <input type="hidden" name="pkgtype" value="<?php p($form->pkgtype) ?>" />
    <input type="hidden" name="launch"  value="<?php p($form->launch) ?>" />
    <input type="hidden" name="parse"  value="0" />
    <?php
        $scorms = get_all_instances_in_course('scorm', $course);
        $coursescorm = current($scorms);
        if (($course->format == 'scorm') && ((count($scorms) == 0) || ($form->instance == $coursescorm->id))) {
    ?>
    <input type="hidden" name="redirect"       value="yes" />
    <input type="hidden" name="redirecturl"    value="../course/view.php?id=<?php p($form->course) ?>" />    
    <?php
        } 
    ?>
    <input type="hidden" name="course"  value="<?php p($form->course) ?>" />
    <input type="hidden" name="sesskey" value="<?php p($form->sesskey) ?>" />
    <input type="hidden" name="section" value="<?php p($form->section) ?>" />
    <input type="hidden" name="module"  value="<?php p($form->module) ?>" />
    <input type="hidden" name="mode"    value="<?php p($form->mode) ?>" />
    <input type="hidden" name="coursemodule"    value="<?php p($form->coursemodule) ?>" />
    <input type="hidden" name="modulename"      value="<?php p($form->modulename) ?>" />
    <input type="hidden" name="instance"        value="<?php p($form->instance) ?>" />
    <center>
        <input type="submit" value="<?php print_string('savechanges') ?>" />
        <input type="button" name="cancel" value="<?php print_string('cancel') ?>" onclick="document.location='view.php?id=<?php echo $form->course ?>'" />
    </center>
</form>
