MDL-22574, use filepicker import glossary xml
authorDongsheng Cai <unoter@gmail.com>
Wed, 2 Jun 2010 07:30:13 +0000 (07:30 +0000)
committerDongsheng Cai <unoter@gmail.com>
Wed, 2 Jun 2010 07:30:13 +0000 (07:30 +0000)
mod/glossary/import.html
mod/glossary/import.php
mod/glossary/lib.php

index 72b7de5..f58c04f 100644 (file)
@@ -1,50 +1,56 @@
 <form method="post" action="import.php" enctype="multipart/form-data">
 <table border="0" cellpadding="3" cellspacing="3" width="100%">
-<tr>
-    <td align="right" style="width:30%;vertical-align:top"><b><?php print_string("filetoimport","glossary") ?>:<br />
+    <tr>
+        <td align="right" style="width:30%;vertical-align:top"><?php print_string("filetoimport","glossary") ?>:
         <?php
            echo '(';
            print_string("maxsize", "", display_size(get_max_upload_file_size($CFG->maxbytes, $course->maxbytes)));
            echo ') ';
            echo $OUTPUT->help_icon('filetoimport', 'glossary');
         ?>
-    </b></td>
-    <td style="width:70%">
-    <?php
-       require_once($CFG->dirroot.'/lib/uploadlib.php');
-       upload_print_form_fragment(1,array('file'),null,false,null,0,0);
-    ?>
-    </td>
-</tr>
-<tr valign="top">
-    <td align="right" colspan="2">
-    <table border="0" cellpadding="0" cellspacing="3" width="100%">
-      <tr>
-        <td align="right" style="width:25%"><?php print_string("destination","glossary") ?>:</td>
-        <td style="width:25%"><select size="1" name="dest">
-        <option selected="selected" value="current"><?php print_string("currentglossary","glossary") ?></option>
-        <option value="new"><?php print_string("newglossary","glossary") ?></option>
-        </select> <?php echo $OUTPUT->help_icon('destination', 'glossary') ?></td>
+        </td>
+        <td style="width:70%">
+        <?php
+        require_once($CFG->dirroot.'/lib/uploadlib.php');
+        $options = new stdclass;
+        $options->maxbytes  = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes);
+        $options->itemid    = file_get_unused_draft_itemid();
+        $options->accepted_types = array('markup');
+        $options->return_types = FILE_INTERNAL;
+        $options->context = $PAGE->context;
+        if (!empty($file)) {
+            $options->filename = $file->get_filename();
+            $options->filepath = '/';
+        }
+        $fp = new file_picker($options);
+        echo $OUTPUT->render($fp);
+        echo '<input type="hidden" name="file" id="id_file" value="'.$options->itemid.'" />';
+        $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker'));
+        $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);
+        // upload_print_form_fragment(1,array('file'),null,false,null,0,0);
+        ?>
+        </td>
+    </tr>
+    <tr>
+        <td align="right" style="width:25%"><?php print_string("destination","glossary") ?>: <?php echo $OUTPUT->help_icon('destination', 'glossary') ?></td>
+        <td style="width:25%">
+        <select size="1" name="dest">
+            <option selected="selected" value="current"><?php print_string("currentglossary","glossary") ?></option>
+            <option value="new"><?php print_string("newglossary","glossary") ?></option>
+        </select>
+        </td>
+    </tr>
+
+    <tr>
         <td style="width:25%" align="right"><?php print_string("importcategories","glossary") ?>:</td>
         <td style="width:25%"><input type="checkbox" name="catsincl" value="1" alt="<?php print_string("importcategories","glossary") ?>" /></td>
-      </tr>
-    </table>
-    </td>
-</tr>
-<tr>
-    <td align="right" colspan="2">
-    <table border="0" cellpadding="3" cellspacing="3" width="100%">
-      <tr>
-        <td style="width:50%" align="right"><input type="submit" value="<?php print_string("savechanges") ?>" /></td>
-        <td style="width:50%"><input type="reset" value="<?php print_string("revert") ?>" /></td>
-      </tr>
-    </table>
-    </td>
-</tr>
+    </tr>
 </table>
-<div>
+<div class="mdl-align">
 <input type="hidden" name="id" value="<?php p($id) ?>" />
 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
 <input type="hidden" name="step" value="1" />
+<input type="submit" value="<?php print_string("savechanges") ?>" />
+<input type="reset" value="<?php print_string("revert") ?>" />
 </div>
 </form>
index acced0f..054d17e 100644 (file)
@@ -13,6 +13,7 @@ $catsincl = optional_param('catsincl', 0, PARAM_INT);         // Import Categori
 
 $mode     = optional_param('mode', 'letter', PARAM_ALPHA );
 $hook     = optional_param('hook', 'ALL', PARAM_ALPHANUM);
+$file     = optional_param('file', 0, PARAM_INT); // xml file
 
 $url = new moodle_url('/mod/glossary/import.php', array('id'=>$id));
 if ($step !== 0) {
@@ -74,31 +75,40 @@ echo $OUTPUT->header();
 echo $OUTPUT->heading($strimportentries);
 
 if ( !$step ) {
+    // display upload form
     echo $OUTPUT->box_start('glossarydisplay generalbox');
     include("import.html");
     echo $OUTPUT->box_end();
-
     echo $OUTPUT->footer();
     exit;
 }
 
 require_sesskey();
 $form = data_submitted();
-$file = $_FILES["file"];
-
-require_once($CFG->dirroot.'/lib/uploadlib.php');
-$um = new upload_manager('file',false,false,$course,false,0);
+$result = true;
+if (empty($file)) {
+    $result = false;
+} else {
+    $fs = get_file_storage();
+    $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
+    $draftfiles = $fs->get_area_files($usercontext->id, 'user_draft', $file, 'id', false);
+    if (count($draftfiles)<1) {
+        $result = false;
+    } else {
+        $xmlfile = array_pop($draftfiles);
+    }
+}
 
-if (!$um->preprocess_files()) {
+if (!$result) {
     echo $OUTPUT->box_start('glossarydisplay generalbox');
     echo $OUTPUT->continue_button('import.php?id='.$id);
     echo $OUTPUT->box_end();
-
     echo $OUTPUT->footer();
     die();
 }
 
-if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
+if ($xml = glossary_read_imported_file($xmlfile->get_content())) {
+    $xmlfile->delete();
 
     $importedentries = 0;
     $importedcats    = 0;
@@ -357,8 +367,8 @@ if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
 
     // rejected entries
     if ($rejections) {
+        echo $OUTPUT->heading(get_string("rejectionrpt","glossary"), 4);
         echo '<table class="glossaryimportexport">';
-        echo '<tr><td align="center" colspan="2" width="100%"><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>';
         echo $rejections;
         echo '</table><hr />';
     }
@@ -370,7 +380,10 @@ if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
     }
     echo $OUTPUT->box_end();
 } else {
-    notice(get_string('errorparsingxml', 'glossary'));
+    echo $OUTPUT->box_start('glossarydisplay generalbox');
+    echo get_string('errorparsingxml', 'glossary');
+    echo $OUTPUT->continue_button('import.php?id='.$id);
+    echo $OUTPUT->box_end();
 }
 
 /// Finish the page
index fc519ca..eb530dd 100644 (file)
@@ -2054,20 +2054,11 @@ function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
  * @param string $file
  * @return string
  */
-function glossary_read_imported_file($file) {
+function glossary_read_imported_file($file_content) {
     require_once "../../lib/xmlize.php";
     global $CFG;
 
-    $h = fopen($file,"r");
-    $line = '';
-    if ($h) {
-        while ( !feof($h) ) {
-           $char = fread($h,1024);
-           $line .= $char;
-        }
-        fclose($h);
-    }
-    return xmlize($line, 0);
+    return xmlize($file_content, 0);
 }
 
 /**