Esempio n. 1
0
 /**
  * Prepares the form before data are set
  *
  * Additional wysiwyg editors are prepared here
  * along with the stopbutton switch, type and text
  *
  * @param array $data to be set
  * @return void
  */
 function data_preprocessing(&$data)
 {
     // Note: if you call "file_prepare_draft_area()" without setting itemid
     // (the first argument), then it will be assigned automatically, and the files
     // for this context will be transferred automatically, which is what we want
     $data['sourceitemid'] = 0;
     if ($this->is_add()) {
         $contextid = null;
     } else {
         $contextid = $this->context->id;
     }
     $options = hotpot::sourcefile_options();
     // array('subdirs' => 1, 'maxbytes' => 0, 'maxfiles' => -1);
     file_prepare_draft_area($data['sourceitemid'], $contextid, 'mod_hotpot', 'sourcefile', 0, $options);
     if ($this->is_add()) {
         // set fields from user preferences, where possible
         foreach (hotpot::user_preferences_fieldnames() as $fieldname) {
             if (!isset($data[$fieldname])) {
                 $data[$fieldname] = get_user_preferences('hotpot_' . $fieldname, '');
             }
         }
     }
     // set entry/exit page settings
     foreach (hotpot::text_page_types() as $type) {
         // extract boolean switches for page options
         foreach (hotpot::text_page_options($type) as $name => $mask) {
             $data[$type . '_' . $name] = $data[$type . 'options'] & $mask;
         }
         // setup custom wysiwyg editor
         $draftitemid = 0;
         if ($this->is_add()) {
             // adding a new hotpot instance
             $data[$type . 'editor'] = array('text' => file_prepare_draft_area($draftitemid, $contextid, 'mod_hotpot', $type, 0), 'format' => editors_get_preferred_format(), 'itemid' => file_get_submitted_draft_itemid($type));
         } else {
             // editing an existing hotpot
             $data[$type . 'editor'] = array('text' => file_prepare_draft_area($draftitemid, $contextid, 'mod_hotpot', $type, 0, $options, $data[$type . 'text']), 'format' => $data[$type . 'format'], 'itemid' => file_get_submitted_draft_itemid($type));
         }
     }
     // timelimit
     if ($data['timelimit'] > 0) {
         $data['timelimitspecific'] = $data['timelimit'];
         $data['timelimit'] = hotpot::TIME_SPECIFIC;
     } else {
         $data['timelimitspecific'] = 0;
     }
     // delay3
     if ($data['delay3'] > 0) {
         $data['delay3specific'] = $data['delay3'];
         $data['delay3'] = hotpot::TIME_SPECIFIC;
     } else {
         $data['delay3specific'] = 0;
     }
     // set stopbutton options
     switch ($data['stopbutton']) {
         case hotpot::STOPBUTTON_SPECIFIC:
             $data['stopbutton_yesno'] = 1;
             $data['stopbutton_type'] = 'specific';
             $data['stopbutton_text'] = $data['stoptext'];
             break;
         case hotpot::STOPBUTTON_LANGPACK:
             $data['stopbutton_yesno'] = 1;
             $data['stopbutton_type'] = $data['stoptext'];
             $data['stopbutton_text'] = '';
             break;
         case hotpot::STOPBUTTON_NONE:
         default:
             $data['stopbutton_yesno'] = 0;
             $data['stopbutton_type'] = '';
             $data['stopbutton_text'] = '';
     }
     // set review options
     if (empty($data['reviewoptions'])) {
         $default = 0;
     } else {
         $default = $data['reviewoptions'];
     }
     list($times, $items) = hotpot::reviewoptions_times_items();
     foreach ($times as $timename => $timevalue) {
         foreach ($items as $itemname => $itemvalue) {
             $data[$timename . $itemname] = min(1, $default & $timevalue & $itemvalue);
         }
     }
 }
Esempio n. 2
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);
}