Exemplo n.º 1
0
/**
 * Given an object containing all the necessary data, 
 * (defined by the form in mod.html) this function 
 * will create a new instance and return the id number 
 * of the new instance.
 */
function customlabel_add_instance($customlabel)
{
    global $CFG, $DB;
    $customlabel->name = customlabel_get_name($customlabel);
    if (!isset($customlabel->intro)) {
        $customlabel->intro = '';
    }
    $customlabel->introformat = 0;
    $customlabel->processedcontent = '';
    $instance = customlabel_load_class($customlabel);
    $customlabeldata = new StdClass();
    $context = context_module::instance($customlabel->coursemodule);
    foreach ($instance->fields as $field) {
        $fieldname = $field->name;
        if (!isset($customlabel->{$field->name})) {
            $customlabel->{$field->name} = @$_REQUEST[$field->name];
            // odd thing when bouncing
        }
        if ($field->type == 'date') {
            $timestamp = mktime(0, 0, 0, $customlabel->{$field->name}['month'], $customlabel->{$field->name}['day'], $customlabel->{$field->name}['year']);
            $customlabel->{$field->name} = $timestamp;
        }
        if ($field->type == 'datetime') {
            $timestamp = mktime($customlabel->{$field->name}['hour'], $customlabel->{$field->name}['min'], $customlabel->{$field->name}['sec'], $customlabel->{$field->name}['month'], $customlabel->{$field->name}['day'], $customlabel->{$field->name}['year']);
            $customlabel->{$field->name} = $timestamp;
        }
        if (preg_match('/editor|textarea/', $field->type)) {
            $editorname = $fieldname . '_editor';
            if (!isset($customlabel->{$editorname})) {
                $editordata = $_REQUEST[$editorname];
                // odd thing when bouncing
            } else {
                $editordata = $customlabel->{$editorname};
                // odd thing when bouncing
            }
            // Saves all embdeded images or files into elements in a single text area.
            $customlabel->{$fieldname} = file_save_draft_area_files($editordata['itemid'], $context->id, 'mod_customlabel', 'contentfiles', 0, array('subdirs' => false), $editordata['text']);
        }
        if ($field->type == 'filepicker') {
            customlabel_save_draft_file($customlabel, $field->name);
        }
        $customlabeldata->{$fieldname} = @$customlabel->{$fieldname};
        unset($customlabel->{$fieldname});
    }
    // this saves into readable data information about which legacy type to use
    // if this record is restored on a platform that do not implement the actual labelclass.
    $customlabel->fallbacktype = '' . @$instance->fallbacktype;
    $customlabel->content = base64_encode(json_encode($customlabeldata));
    $instance->data = $customlabeldata;
    // load data into instance
    $customlabel->processedcontent = $instance->make_content();
    $customlabel->timemodified = time();
    return $DB->insert_record('customlabel', $customlabel);
}
/**
* Given an object containing all the necessary data, 
* (defined by the form in mod.html) this function 
* will create a new instance and return the id number 
* of the new instance.
*
*/
function customlabel_add_instance($customlabel)
{
    global $CFG, $DB;
    $customlabel->name = customlabel_get_name($customlabel);
    if (!isset($customlabel->intro)) {
        $customlabel->intro = '';
    }
    $customlabel->introformat = 0;
    /*    
    $modcontext = context_module::instance($this->_cm->id);
    $maxfiles = 99;                // TODO: add some setting
    $maxbytes = $COURSE->maxbytes; // TODO: add some setting	
    $editoroptions = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes, 'context' => $modcontext);
    */
    $customlabel->processedcontent = '';
    $instance = customlabel_load_class($customlabel);
    $customlabeldata = new StdClass();
    foreach ($instance->fields as $field) {
        if (!isset($customlabel->{$field->name})) {
            $customlabel->{$field->name} = @$_POST[$field->name];
        }
        // odd thing when bouncing
        if (preg_match('/editor|textarea/', $field->type)) {
            /*
            			$draftid_editor = file_get_submitted_draft_itemid($field->name.'_editor');
            			$customlabel->{$field->name} = file_save_draft_area_files($draftid_editor, $modcontext->id, 'mod_customlabel', $field->name, $customlabel->id, array('subdirs' => true), $customlabel->{$field->name});
            $customlabel = file_postupdate_standard_editor($customlabel, $field->name, $editoroptions, $modcontext, 'mod_customlabel', $field->name, $customlabel->id);
            */
            $editorname = $field->name . '_editor';
            if (!isset($customlabel->{$editorname})) {
                $customlabel->{$field->name} = $_POST[$editorname];
                // odd thing when bouncing
            } else {
                $customlabel->{$field->name} = $customlabel->{$editorname};
                // odd thing when bouncing
            }
        }
        $customlabeldata->{$field->name} = @$customlabel->{$field->name};
        unset($customlabel->{$field->name});
    }
    // this saves into readable data information about which legacy type to use
    // if this record is restored on a platform that do not implement the actual labelclass.
    $customlabel->fallbacktype = '' . @$instance->fallbacktype;
    $customlabel->content = base64_encode(json_encode($customlabeldata));
    $instance->data = $customlabeldata;
    // load data into instance
    $customlabel->processedcontent = $instance->make_content();
    $customlabel->timemodified = time();
    return $DB->insert_record('customlabel', $customlabel);
}