<?php
    require_once("$CFG->dirroot/mod/scorm/lib.php");
    if (empty($form->name)) {
        $form->name = "";
    }
    if (empty($form->reference)) {
        $form->reference = "";
    }
    if (empty($form->summary)) {
        $form->summary = "";
    }
    if (empty($form->launch)) {
        $form->launch = "";
    }
    if (empty($form->auto)) {
        $form->auto = "";
    }
    if (empty($form->datadir)) {
        $form->datadir = "";
    }
    if (empty($form->maxgrade)) {
        $form->maxgrade = "";
    }
    if (empty($form->grademethod)) {
        $form->grademethod = "0";
    }
    if (empty($form->browsemode)) {
        $form->browsemode = "1";
    }
    if (empty($form->width)) {
        $form->width = $CFG->scorm_framewidth;
    }
    if (empty($form->height)) {
        $form->height = $CFG->scorm_frameheight;
    }

    $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;
    }
?>

<script type="text/javascript" src="<?php p($CFG->wwwroot) ?>/mod/scorm/request.js" ></script>
<script type="text/javascript">
    function validate_scorm(theform,filename) {
       //alert(filename);
        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 ?>");
       //alert(result);
        results = result.split('\n');
        result = '';
        errorlogs = '';
        datadir = '';
        for (i=0;i<results.length;i++) {
            element = results[i].split('=');
            //alert(element);
            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 (launch == 0) {
                if ('<?php echo $scormid ?>' != '') {
                    return confirm('<?php print_string('trackingloose','scorm') ?>');
                }
            }
            return true;
        } 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 checkmethod (grademethod,maxgrade) {
        if (grademethod.options[grademethod.selectedIndex].value == 0) {
            maxgrade.disabled=true;
        } else {
            maxgrade.disabled=false;
        }
    }
</script>

<form name="form" method="post" action="mod.php" onsubmit="return validate_scorm(document.form,document.form.reference.value);">
    <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>
<?php
    $strfilename = get_string("coursepacket", "scorm");
    $strchooseafile = get_string("chooseapacket", "scorm");
?>
        <tr valign="top">
            <td align="right"><b><?php print_string("summary") ?>:</b><br />
               <font size="1">
               <?php helpbutton("summary", get_string("summary"), "scorm", true, true) ?>
              </font>
            </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>
                <?php
                    echo "<input name=\"reference\" size=\"50\" value=\"$form->reference\" alt=\"$strfilename\" />&nbsp;";
                    button_to_popup_window ("/files/index.php?id=$course->id&amp;choose=form.reference",
                                         "coursefiles", $strchooseafile, 500, 750, $strchooseafile);
                    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', "$form->grademethod", '','checkmethod(this,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', "$form->maxgrade", '','','0',false,$disabled);
                helpbutton('maxgrade', get_string('maximumgrade'), 'scorm');
            ?>
            </td>
        </tr>
        <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", $form->auto,'');
                helpbutton("autocontinue", get_string("autocontinue", "scorm"), "scorm", true);
            ?>
            </td>
        </tr>
        <tr>
            <td align="right"><b><?php print_string("enablebrowse","scorm") ?>:</b></td>
            <td>
            <?php
                $options = array();
                $options[0]=get_string("no");
                $options[1]=get_string("yes");
                choose_from_menu ($options, "browsemode", $form->browsemode, '');
                helpbutton("browsemode", get_string("browsemode", "scorm"), "scorm", true);
            ?>
            </td>
        </tr>
        <tr valign="top">
            <td align="right"><b><?php print_string("width","scorm") ?>:</b></td>
            <td>
               <input type="text" name="width" value="<?php echo $form->width ?>" size="4" />
                <?php helpbutton("width", get_string("width", "scorm"), "scorm", true); ?>
            </td>
        </tr>
        <tr valign="top">
            <td align="right"><b><?php print_string("height","scorm") ?>:</b></td>
            <td>
               <input type="text" name="height" value="<?php echo $form->height ?>" size="4" />
                <?php helpbutton("height", get_string("height", "scorm"), "scorm", true); ?>
            </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="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>
