$PAGE->set_button(update_module_button($cm->id, $course->id, get_string('modulename', 'lightboxgallery')));
$mform = new mod_lightboxgallery_imageadd_form(null, array('id' => $cm->id));
if ($mform->is_cancelled()) {
    redirect($CFG->wwwroot . '/mod/lightboxgallery/view.php?id=' . $cm->id);
} else {
    if (($formdata = $mform->get_data()) && confirm_sesskey()) {
        require_once $CFG->dirroot . '/lib/uploadlib.php';
        $fs = get_file_storage();
        $draftid = file_get_submitted_draft_itemid('image');
        if (!($files = $fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $draftid, 'id DESC', false))) {
            redirect($PAGE->url);
        }
        $stored_file = reset($files);
        if ($gallery->autoresize == AUTO_RESIZE_UPLOAD || $gallery->autoresize == AUTO_RESIZE_BOTH) {
            $resize = $gallery->resize;
        } else {
            if (isset($formdata->resize)) {
                $resize = $formdata->resize;
            } else {
                $resize = 0;
                // No resize.
            }
        }
        lightboxgallery_add_images($stored_file, $context, $cm, $gallery, $resize);
        redirect($CFG->wwwroot . '/mod/lightboxgallery/view.php?id=' . $cm->id);
    }
}
echo $OUTPUT->header();
echo $OUTPUT->box($OUTPUT->notification(get_string('acceptablefiletypebriefing', 'mod_lightboxgallery')));
$mform->display();
echo $OUTPUT->footer();
$PAGE->set_url('/mod/lightboxgallery/view.php', array('id' => $cm->id));
$PAGE->set_title($gallery->name);
$PAGE->set_heading($course->shortname);
$mform = new mod_lightboxgallery_imageadd_form(null, array('id' => $cm->id));
if ($mform->is_cancelled()) {
    redirect($CFG->wwwroot . '/mod/lightboxgallery/view.php?id=' . $cm->id);
} else {
    if (($formdata = $mform->get_data()) && confirm_sesskey()) {
        require_once $CFG->dirroot . '/lib/uploadlib.php';
        $fs = get_file_storage();
        $draftid = file_get_submitted_draft_itemid('image');
        if (!($files = $fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $draftid, 'id DESC', false))) {
            redirect($PAGE->url);
        }
        if ($gallery->autoresize == AUTO_RESIZE_UPLOAD || $gallery->autoresize == AUTO_RESIZE_BOTH) {
            $resize = $gallery->resize;
        } else {
            if (isset($formdata->resize)) {
                $resize = $formdata->resize;
            } else {
                $resize = 0;
                // No resize.
            }
        }
        lightboxgallery_add_images($files, $context, $cm, $gallery, $resize);
        redirect($CFG->wwwroot . '/mod/lightboxgallery/view.php?id=' . $cm->id);
    }
}
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();