Exemplo n.º 1
0
/**
 * Set secondary fields (i.e. fields derived from the form fields)
 * for this HotPot acitivity
 *
 * @param stdclass $data (passed by reference)
 * @param moodle_form $mform
 */
function hotpot_process_formdata(stdclass &$data, $mform)
{
    global $CFG;
    require_once $CFG->dirroot . '/mod/hotpot/locallib.php';
    if ($mform->is_add()) {
        $data->timecreated = time();
    } else {
        $data->timemodified = time();
    }
    // get context for this HotPot instance
    $context = hotpot_get_context(CONTEXT_MODULE, $data->coursemodule);
    $sourcefile = null;
    $data->sourcefile = '';
    $data->sourcetype = '';
    if ($data->sourceitemid) {
        $options = hotpot::sourcefile_options();
        file_save_draft_area_files($data->sourceitemid, $context->id, 'mod_hotpot', 'sourcefile', 0, $options);
        $fs = get_file_storage();
        $files = $fs->get_area_files($context->id, 'mod_hotpot', 'sourcefile');
        // do we need to remove the draft files ?
        // otherwise the "files" table seems to get full of "draft" records
        // $fs->delete_area_files($context->id, 'user', 'draft', $data->sourceitemid);
        foreach ($files as $hash => $file) {
            if ($file->get_sortorder() == 1) {
                $data->sourcefile = $file->get_filepath() . $file->get_filename();
                $data->sourcetype = hotpot::get_sourcetype($file);
                $sourcefile = $file;
                break;
            }
        }
        unset($fs, $files, $file, $hash, $options);
    }
    if (is_null($sourcefile) || $data->sourcefile == '' || $data->sourcetype == '') {
        // sourcefile was missing or not a recognized type - shouldn't happen !!
    }
    // process text fields that may come from source file
    $source = false;
    $textfields = array('name', 'entrytext', 'exittext');
    foreach ($textfields as $textfield) {
        $textsource = $textfield . 'source';
        if (!isset($data->{$textsource})) {
            $data->{$textsource} = hotpot::TEXTSOURCE_SPECIFIC;
        }
        switch ($data->{$textsource}) {
            case hotpot::TEXTSOURCE_FILE:
                if ($data->sourcetype && $sourcefile && empty($source)) {
                    $class = 'hotpot_source_' . $data->sourcetype;
                    $source = new $class($sourcefile, $data);
                }
                $method = 'get_' . $textfield;
                if ($source && method_exists($source, $method)) {
                    $data->{$textfield} = $source->{$method}();
                } else {
                    $data->{$textfield} = '';
                }
                break;
            case hotpot::TEXTSOURCE_FILENAME:
                $data->{$textfield} = basename($data->sourcefile);
                break;
            case hotpot::TEXTSOURCE_FILEPATH:
                $data->{$textfield} = str_replace(array('/', '\\'), ' ', $data->sourcefile);
                break;
            case hotpot::TEXTSOURCE_SPECIFIC:
            default:
                if (isset($data->{$textfield})) {
                    $data->{$textfield} = trim($data->{$textfield});
                } else {
                    $data->{$textfield} = $mform->get_original_value($textfield, '');
                }
        }
        // default activity name is simply "HotPot"
        if ($textfield == 'name' && $data->{$textfield} == '') {
            $data->{$textfield} = get_string('modulename', 'hotpot');
        }
    }
    // process entry/exit page settings
    foreach (hotpot::text_page_types() as $type) {
        // show page (boolean switch)
        $pagefield = $type . 'page';
        if (!isset($data->{$pagefield})) {
            $data->{$pagefield} = 0;
        }
        // set field names
        $textfield = $type . 'text';
        $formatfield = $type . 'format';
        $editorfield = $type . 'editor';
        $sourcefield = $type . 'textsource';
        $optionsfield = $type . 'options';
        // ensure text, format and option fields are set
        // (these fields can't be null in the database)
        if (!isset($data->{$textfield})) {
            $data->{$textfield} = $mform->get_original_value($textfield, '');
        }
        if (!isset($data->{$formatfield})) {
            $data->{$formatfield} = $mform->get_original_value($formatfield, FORMAT_HTML);
        }
        if (!isset($data->{$optionsfield})) {
            $data->{$optionsfield} = $mform->get_original_value($optionsfield, 0);
        }
        // set text and format fields
        if ($data->{$sourcefield} == hotpot::TEXTSOURCE_SPECIFIC) {
            // transfer wysiwyg editor text
            if ($itemid = $data->{$editorfield}['itemid']) {
                if (isset($data->{$editorfield}['text'])) {
                    // get the text that was sent from the browser
                    $editoroptions = hotpot::text_editors_options($context);
                    $text = file_save_draft_area_files($itemid, $context->id, 'mod_hotpot', $type, 0, $editoroptions, $data->{$editorfield}['text']);
                    // remove leading and trailing white space,
                    //  - empty html paragraphs (from IE)
                    //  - and blank lines (from Firefox)
                    $text = preg_replace('/^((<p>\\s*<\\/p>)|(<br[^>]*>)|\\s)+/is', '', $text);
                    $text = preg_replace('/((<p>\\s*<\\/p>)|(<br[^>]*>)|\\s)+$/is', '', $text);
                    $data->{$textfield} = $text;
                    $data->{$formatfield} = $data->{$editorfield}['format'];
                }
            }
        }
        // set entry/exit page options
        foreach (hotpot::text_page_options($type) as $name => $mask) {
            $optionfield = $type . '_' . $name;
            if ($data->{$pagefield}) {
                if (empty($data->{$optionfield})) {
                    // disable this option
                    $data->{$optionsfield} = $data->{$optionsfield} & ~$mask;
                } else {
                    // enable this option
                    $data->{$optionsfield} = $data->{$optionsfield} | $mask;
                }
            }
        }
        // don't show exit page if no content is specified
        if ($type == 'exit' && empty($data->{$optionsfield}) && empty($data->{$textfield})) {
            $data->{$pagefield} = 0;
        }
    }
    // timelimit
    if ($data->timelimit == hotpot::TIME_SPECIFIC) {
        $data->timelimit = $data->timelimitspecific;
    }
    // delay3
    if ($data->delay3 == hotpot::TIME_SPECIFIC) {
        $data->delay3 = $data->delay3specific;
    }
    // set stopbutton and stoptext
    if (empty($data->stopbutton_yesno)) {
        $data->stopbutton = hotpot::STOPBUTTON_NONE;
        $data->stoptext = $mform->get_original_value('stoptext', '');
    } else {
        if (!isset($data->stopbutton_type)) {
            $data->stopbutton_type = '';
        }
        if (!isset($data->stopbutton_text)) {
            $data->stopbutton_text = '';
        }
        if ($data->stopbutton_type == 'specific') {
            $data->stopbutton = hotpot::STOPBUTTON_SPECIFIC;
            $data->stoptext = $data->stopbutton_text;
        } else {
            $data->stopbutton = hotpot::STOPBUTTON_LANGPACK;
            $data->stoptext = $data->stopbutton_type;
        }
    }
    // set review options
    $data->reviewoptions = 0;
    list($times, $items) = hotpot::reviewoptions_times_items();
    foreach ($times as $timename => $timevalue) {
        foreach ($items as $itemname => $itemvalue) {
            $name = $timename . $itemname;
            // e.g. duringattemptresponses
            if (isset($data->{$name})) {
                if ($data->{$name}) {
                    $data->reviewoptions += $timevalue & $itemvalue;
                }
                unset($data->{$name});
            }
        }
    }
    // save these form settings as user preferences
    $preferences = array();
    foreach (hotpot::user_preferences_fieldnames() as $fieldname) {
        if (isset($data->{$fieldname})) {
            $preferences['hotpot_' . $fieldname] = $data->{$fieldname};
        }
    }
    set_user_preferences($preferences);
}