Esempio n. 1
0
     echo '<input type="hidden" name="action" value="save2" />';
     echo '<input type="hidden" name="d" value="' . $data->id . '" />';
     echo '<input type="hidden" name="sesskey" value="' . $sesskey . '" />';
     echo '<input type="submit" value="' . $strcontinue . '" /></fieldset></form></div>';
     print_footer($course);
     exit;
     break;
 case 'save2':
     if (!data_submitted() or !confirm_sesskey()) {
         error("Invalid request");
     }
     $strcontinue = get_string('continue');
     $stroverwrite = get_string('overwrite', 'data');
     $strname = get_string('shortname');
     $name = optional_param('name', $data->name, PARAM_FILE);
     if (is_directory_a_preset("{$CFG->dataroot}/data/preset/{$USER->id}/{$name}")) {
         notify("Preset already exists: Pick another name or overwrite");
         echo '<div style="text-align:center">';
         echo '<form action="preset.php" method="post">';
         echo '<fieldset class="invisiblefieldset">';
         echo '<label for="shorname">' . $strname . '</label> <input type="textbox" name="name" value="' . $name . '" />';
         echo '<input type="hidden" name="action" value="save2" />';
         echo '<input type="hidden" name="d" value="' . $data->id . '" />';
         echo '<input type="hidden" name="sesskey" value="' . $sesskey . '" />';
         echo '<input type="submit" value="' . $strcontinue . '" /></fieldset></form>';
         echo '<form action="preset.php" method="post">';
         echo '<div>';
         echo '<input type="hidden" name="name" value="' . $name . '" />';
         echo '<input type="hidden" name="action" value="save3" />';
         echo '<input type="hidden" name="d" value="' . $data->id . '" />';
         echo '<input type="hidden" name="sesskey" value="' . $sesskey . '" />';
Esempio n. 2
0
    /**
     * Gets the preset settings
     * @global moodle_database $DB
     * @return stdClass
     */
    public function get_preset_settings() {
        global $DB;

        $fs = $fileobj = null;
        if (!is_directory_a_preset($this->directory)) {
            //maybe the user requested a preset stored in the Moodle file storage

            $fs = get_file_storage();
            $files = $fs->get_area_files(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA);

            //preset name to find will be the final element of the directory
            $presettofind = end(explode('/',$this->directory));

            //now go through the available files available and see if we can find it
            foreach ($files as $file) {
                if (($file->is_directory() && $file->get_filepath()=='/') || !$file->is_directory()) {
                    continue;
                }
                $presetname = trim($file->get_filepath(), '/');
                if ($presetname==$presettofind) {
                    $this->directory = $presetname;
                    $fileobj = $file;
                }
            }

            if (empty($fileobj)) {
                print_error('invalidpreset', 'data', '', $this->directory);
            }
        }

        $allowed_settings = array(
            'intro',
            'comments',
            'requiredentries',
            'requiredentriestoview',
            'maxentries',
            'rssarticles',
            'approval',
            'defaultsortdir',
            'defaultsort');

        $result = new stdClass;
        $result->settings = new stdClass;
        $result->importfields = array();
        $result->currentfields = $DB->get_records('data_fields', array('dataid'=>$this->module->id));
        if (!$result->currentfields) {
            $result->currentfields = array();
        }


        /* Grab XML */
        $presetxml = $this->data_preset_get_file_contents($fs, $fileobj, $this->directory,'preset.xml');
        $parsedxml = xmlize($presetxml, 0);

        /* First, do settings. Put in user friendly array. */
        $settingsarray = $parsedxml['preset']['#']['settings'][0]['#'];
        $result->settings = new StdClass();
        foreach ($settingsarray as $setting => $value) {
            if (!is_array($value) || !in_array($setting, $allowed_settings)) {
                // unsupported setting
                continue;
            }
            $result->settings->$setting = $value[0]['#'];
        }

        /* Now work out fields to user friendly array */
        $fieldsarray = $parsedxml['preset']['#']['field'];
        foreach ($fieldsarray as $field) {
            if (!is_array($field)) {
                continue;
            }
            $f = new StdClass();
            foreach ($field['#'] as $param => $value) {
                if (!is_array($value)) {
                    continue;
                }
                $f->$param = $value[0]['#'];
            }
            $f->dataid = $this->module->id;
            $f->type = clean_param($f->type, PARAM_ALPHA);
            $result->importfields[] = $f;
        }
        /* Now add the HTML templates to the settings array so we can update d */
        $result->settings->singletemplate     = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"singletemplate.html");
        $result->settings->listtemplate       = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"listtemplate.html");
        $result->settings->listtemplateheader = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"listtemplateheader.html");
        $result->settings->listtemplatefooter = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"listtemplatefooter.html");
        $result->settings->addtemplate        = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"addtemplate.html");
        $result->settings->rsstemplate        = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"rsstemplate.html");
        $result->settings->rsstitletemplate   = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"rsstitletemplate.html");
        $result->settings->csstemplate        = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"csstemplate.css");
        $result->settings->jstemplate         = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"jstemplate.js");

        //optional
        if (file_exists($this->directory."/asearchtemplate.html")) {
            $result->settings->asearchtemplate = $this->data_preset_get_file_contents($fs, $fileobj,$this->directory,"asearchtemplate.html");
        } else {
            $result->settings->asearchtemplate = NULL;
        }
        $result->settings->instance = $this->module->id;

        return $result;
    }
Esempio n. 3
0
function data_presets_export($course, $cm, $data)
{
    global $CFG;
    $presetname = clean_filename($data->name) . '-preset-' . gmdate("Ymd_Hi");
    $exportsubdir = "{$course->id}/moddata/data/{$data->id}/{$presetname}";
    make_upload_directory($exportsubdir);
    $exportdir = "{$CFG->dataroot}/{$exportsubdir}";
    // Assemble "preset.xml":
    $presetxmldata = "<preset>\n\n";
    // Raw settings are not preprocessed during saving of presets
    $raw_settings = array('intro', 'comments', 'requiredentries', 'requiredentriestoview', 'maxentries', 'rssarticles', 'approval', 'defaultsortdir');
    $presetxmldata .= "<settings>\n";
    // First, settings that do not require any conversion
    foreach ($raw_settings as $setting) {
        $presetxmldata .= "<{$setting}>" . htmlspecialchars($data->{$setting}) . "</{$setting}>\n";
    }
    // Now specific settings
    if ($data->defaultsort > 0 && ($sortfield = data_get_field_from_id($data->defaultsort, $data))) {
        $presetxmldata .= '<defaultsort>' . htmlspecialchars($sortfield->field->name) . "</defaultsort>\n";
    } else {
        $presetxmldata .= "<defaultsort>0</defaultsort>\n";
    }
    $presetxmldata .= "</settings>\n\n";
    // Now for the fields. Grab all that are non-empty
    $fields = get_records('data_fields', 'dataid', $data->id);
    ksort($fields);
    if (!empty($fields)) {
        foreach ($fields as $field) {
            $presetxmldata .= "<field>\n";
            foreach ($field as $key => $value) {
                if ($value != '' && $key != 'id' && $key != 'dataid') {
                    $presetxmldata .= "<{$key}>" . htmlspecialchars($value) . "</{$key}>\n";
                }
            }
            $presetxmldata .= "</field>\n\n";
        }
    }
    $presetxmldata .= '</preset>';
    // After opening a file in write mode, close it asap
    $presetxmlfile = fopen($exportdir . '/preset.xml', 'w');
    fwrite($presetxmlfile, $presetxmldata);
    fclose($presetxmlfile);
    // Now write the template files
    $singletemplate = fopen($exportdir . '/singletemplate.html', 'w');
    fwrite($singletemplate, $data->singletemplate);
    fclose($singletemplate);
    $listtemplateheader = fopen($exportdir . '/listtemplateheader.html', 'w');
    fwrite($listtemplateheader, $data->listtemplateheader);
    fclose($listtemplateheader);
    $listtemplate = fopen($exportdir . '/listtemplate.html', 'w');
    fwrite($listtemplate, $data->listtemplate);
    fclose($listtemplate);
    $listtemplatefooter = fopen($exportdir . '/listtemplatefooter.html', 'w');
    fwrite($listtemplatefooter, $data->listtemplatefooter);
    fclose($listtemplatefooter);
    $addtemplate = fopen($exportdir . '/addtemplate.html', 'w');
    fwrite($addtemplate, $data->addtemplate);
    fclose($addtemplate);
    $rsstemplate = fopen($exportdir . '/rsstemplate.html', 'w');
    fwrite($rsstemplate, $data->rsstemplate);
    fclose($rsstemplate);
    $rsstitletemplate = fopen($exportdir . '/rsstitletemplate.html', 'w');
    fwrite($rsstitletemplate, $data->rsstitletemplate);
    fclose($rsstitletemplate);
    $csstemplate = fopen($exportdir . '/csstemplate.css', 'w');
    fwrite($csstemplate, $data->csstemplate);
    fclose($csstemplate);
    $jstemplate = fopen($exportdir . '/jstemplate.js', 'w');
    fwrite($jstemplate, $data->jstemplate);
    fclose($jstemplate);
    $asearchtemplate = fopen($exportdir . '/asearchtemplate.html', 'w');
    fwrite($asearchtemplate, $data->asearchtemplate);
    fclose($asearchtemplate);
    // Check if all files have been generated
    if (!is_directory_a_preset($exportdir)) {
        error('Not all files generated!');
        // should be migrated to print_error()
    }
    $filelist = array('preset.xml', 'singletemplate.html', 'listtemplateheader.html', 'listtemplate.html', 'listtemplatefooter.html', 'addtemplate.html', 'rsstemplate.html', 'rsstitletemplate.html', 'csstemplate.css', 'jstemplate.js', 'asearchtemplate.html');
    foreach ($filelist as $key => $file) {
        $filelist[$key] = $exportdir . '/' . $filelist[$key];
    }
    $exportfile = "{$CFG->dataroot}/{$course->id}/moddata/data/{$data->id}/{$presetname}.zip";
    file_exists($exportfile) && unlink($exportfile);
    $status = zip_files($filelist, $exportfile);
    // ToDo: status check
    foreach ($filelist as $file) {
        unlink($file);
    }
    rmdir($exportdir);
    // Return the full path to the exported preset file:
    return $exportfile;
}
Esempio n. 4
0
 function get_settings()
 {
     global $CFG;
     if (!is_directory_a_preset($this->folder)) {
         error("{$this->userid}/{$this->shortname} Not a preset");
     }
     /* Grab XML */
     $presetxml = file_get_contents($this->folder . '/preset.xml');
     $parsedxml = xmlize($presetxml, 0);
     $allowed_settings = array('intro', 'comments', 'requiredentries', 'requiredentriestoview', 'maxentries', 'rssarticles', 'approval', 'defaultsortdir', 'defaultsort');
     /* First, do settings. Put in user friendly array. */
     $settingsarray = $parsedxml['preset']['#']['settings'][0]['#'];
     $settings = new StdClass();
     foreach ($settingsarray as $setting => $value) {
         if (!is_array($value)) {
             continue;
         }
         if (!in_array($setting, $allowed_settings)) {
             // unsupported setting
             continue;
         }
         $settings->{$setting} = $value[0]['#'];
     }
     /* Now work out fields to user friendly array */
     $fieldsarray = $parsedxml['preset']['#']['field'];
     $fields = array();
     foreach ($fieldsarray as $field) {
         if (!is_array($field)) {
             continue;
         }
         $f = new StdClass();
         foreach ($field['#'] as $param => $value) {
             if (!is_array($value)) {
                 continue;
             }
             $f->{$param} = addslashes($value[0]['#']);
         }
         $f->dataid = $this->data->id;
         $f->type = clean_param($f->type, PARAM_ALPHA);
         $fields[] = $f;
     }
     /* Now add the HTML templates to the settings array so we can update d */
     $settings->singletemplate = file_get_contents($this->folder . "/singletemplate.html");
     $settings->listtemplate = file_get_contents($this->folder . "/listtemplate.html");
     $settings->listtemplateheader = file_get_contents($this->folder . "/listtemplateheader.html");
     $settings->listtemplatefooter = file_get_contents($this->folder . "/listtemplatefooter.html");
     $settings->addtemplate = file_get_contents($this->folder . "/addtemplate.html");
     $settings->rsstemplate = file_get_contents($this->folder . "/rsstemplate.html");
     $settings->rsstitletemplate = file_get_contents($this->folder . "/rsstitletemplate.html");
     $settings->csstemplate = file_get_contents($this->folder . "/csstemplate.css");
     $settings->jstemplate = file_get_contents($this->folder . "/jstemplate.js");
     //optional
     if (file_exists($this->folder . "/asearchtemplate.html")) {
         $settings->asearchtemplate = file_get_contents($this->folder . "/asearchtemplate.html");
     } else {
         $settings->asearchtemplate = NULL;
     }
     $settings->instance = $this->data->id;
     /* Now we look at the current structure (if any) to work out whether we need to clear db
        or save the data */
     if (!($currentfields = get_records('data_fields', 'dataid', $this->data->id))) {
         $currentfields = array();
     }
     return array($settings, $fields, $currentfields);
 }
Esempio n. 5
0
 function get_settings()
 {
     global $CFG;
     if (!is_directory_a_preset($this->folder)) {
         error("{$this->userid}/{$this->shortname} Not a preset");
     }
     /* Grab XML */
     $presetxml = file_get_contents($this->folder . '/preset.xml');
     $parsedxml = xmlize($presetxml);
     /* First, do settings. Put in user friendly array. */
     $settingsarray = $parsedxml['preset']['#']['settings'][0]['#'];
     $settings = new StdClass();
     foreach ($settingsarray as $setting => $value) {
         $settings->{$setting} = $value[0]['#'];
     }
     /* Now work out fields to user friendly array */
     $fieldsarray = $parsedxml['preset']['#']['field'];
     $fields = array();
     foreach ($fieldsarray as $field) {
         $f = new StdClass();
         foreach ($field['#'] as $param => $value) {
             $f->{$param} = $value[0]['#'];
         }
         $f->dataid = $this->data->id;
         $f->type = clean_param($f->type, PARAM_ALPHA);
         $fields[] = $f;
     }
     /* Now add the HTML templates to the settings array so we can update d */
     $settings->singletemplate = file_get_contents($this->folder . "/singletemplate.html");
     $settings->listtemplate = file_get_contents($this->folder . "/listtemplate.html");
     $settings->listtemplateheader = file_get_contents($this->folder . "/listtemplateheader.html");
     $settings->listtemplatefooter = file_get_contents($this->folder . "/listtemplatefooter.html");
     $settings->addtemplate = file_get_contents($this->folder . "/addtemplate.html");
     $settings->rsstemplate = file_get_contents($this->folder . "/rsstemplate.html");
     $settings->rsstitletemplate = file_get_contents($this->folder . "/rsstitletemplate.html");
     $settings->csstemplate = file_get_contents($this->folder . "/csstemplate.css");
     $settings->jstemplate = file_get_contents($this->folder . "/jstemplate.js");
     $settings->instance = $this->data->id;
     /* Now we look at the current structure (if any) to work out whether we need to clear db
        or save the data */
     $currentfields = array();
     $currentfields = get_records('data_fields', 'dataid', $this->data->id);
     return array($settings, $fields, $currentfields);
 }