MDL-22574, use moodle form to import glossary
authorDongsheng Cai <unoter@gmail.com>
Fri, 4 Jun 2010 03:14:21 +0000 (03:14 +0000)
committerDongsheng Cai <unoter@gmail.com>
Fri, 4 Jun 2010 03:14:21 +0000 (03:14 +0000)
mod/glossary/import.html [deleted file]
mod/glossary/import.php
mod/glossary/import_form.php [new file with mode: 0644]

diff --git a/mod/glossary/import.html b/mod/glossary/import.html
deleted file mode 100644 (file)
index f58c04f..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-<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"><?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');
-        ?>
-        </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>
-<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 054d17e..cf2161a 100644 (file)
@@ -3,31 +3,14 @@
 require_once("../../config.php");
 require_once("lib.php");
 require_once("$CFG->dirroot/course/lib.php");
+require_once('import_form.php');
 
 $id = required_param('id', PARAM_INT);    // Course Module ID
 
-$step     = optional_param('step', 0, PARAM_INT);
-$dest     = optional_param('dest', 'current', PARAM_ALPHA);   // current | new
-$file     = optional_param('file', '', PARAM_FILE);         // file to import
-$catsincl = optional_param('catsincl', 0, PARAM_INT);         // Import Categories too?
-
 $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) {
-    $url->param('step', $step);
-}
-if ($dest !== 'current') {
-    $url->param('dest', $dest);
-}
-if ($file !== '') {
-    $url->param('file', $file);
-}
-if ($catsincl !== 0) {
-    $url->param('catsincl', $catsincl);
-}
 if ($mode !== 'letter') {
     $url->param('mode', $mode);
 }
@@ -53,9 +36,6 @@ require_login($course->id, false, $cm);
 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 require_capability('mod/glossary:import', $context);
 
-if ($dest != 'new' and $dest != 'current') {
-    $dest = 'current';
-}
 $strglossaries = get_string("modulenameplural", "glossary");
 $strglossary = get_string("modulename", "glossary");
 $strallcategories = get_string("allcategories", "glossary");
@@ -66,7 +46,6 @@ $strsearchindefinition = get_string("searchindefinition", "glossary");
 $strsearch = get_string("search");
 $strimportentries = get_string('importentriesfromxml', 'glossary');
 
-$PAGE->set_url('/mod/glossary/import.php', array('id'=>$cm->id, 'mode'=>$mode, 'hook'=>$hook));
 $PAGE->navbar->add($strimportentries);
 $PAGE->set_title(format_string($glossary->name));
 $PAGE->set_heading($course->fullname);
@@ -74,32 +53,23 @@ $PAGE->set_heading($course->fullname);
 echo $OUTPUT->header();
 echo $OUTPUT->heading($strimportentries);
 
-if ( !$step ) {
-    // display upload form
+$form = new mod_glossary_import_form();
+
+if ( !$data = $form->get_data() ) {
     echo $OUTPUT->box_start('glossarydisplay generalbox');
-    include("import.html");
+    // display upload form
+    $data = new stdclass;
+    $data->id = $id;
+    $form->set_data($data);
+    $form->display();
     echo $OUTPUT->box_end();
     echo $OUTPUT->footer();
     exit;
 }
 
-require_sesskey();
-$form = data_submitted();
-$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);
-    }
-}
+$result = $form->get_file_content('file');
 
-if (!$result) {
+if (empty($result)) {
     echo $OUTPUT->box_start('glossarydisplay generalbox');
     echo $OUTPUT->continue_button('import.php?id='.$id);
     echo $OUTPUT->box_end();
@@ -107,14 +77,13 @@ if (!$result) {
     die();
 }
 
-if ($xml = glossary_read_imported_file($xmlfile->get_content())) {
-    $xmlfile->delete();
-
+if ($xml = glossary_read_imported_file($result)) {
     $importedentries = 0;
     $importedcats    = 0;
     $entriesrejected = 0;
     $rejections      = '';
-    if ($dest == 'new') {
+
+    if ($data->dest == 'newglossary') {
         // If the user chose to create a new glossary
         $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
 
@@ -129,6 +98,7 @@ if ($xml = glossary_read_imported_file($xmlfile->get_content())) {
             $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']);
             $glossary->timecreated = time();
             $glossary->timemodified = time();
+            $glossary->cmidnumber = $cm->idnumber;
 
             // Setting the default values if no values were passed
             if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
@@ -165,7 +135,6 @@ if ($xml = glossary_read_imported_file($xmlfile->get_content())) {
             // Include new glossary and return the new ID
             if ( !$glossary->id = glossary_add_instance($glossary) ) {
                 echo $OUTPUT->notification("Error while trying to create the new glossary.");
-                echo '</center>';
                 glossary_print_tabbed_table_end();
                 echo $OUTPUT->footer();
                 exit;
@@ -209,7 +178,6 @@ if ($xml = glossary_read_imported_file($xmlfile->get_content())) {
                 rebuild_course_cache($course->id);
 
                 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
-                echo '<p>';
             }
         } else {
             echo $OUTPUT->notification("Error while trying to create the new glossary.");
@@ -286,7 +254,7 @@ if ($xml = glossary_read_imported_file($xmlfile->get_content())) {
                     }
                 }
 
-                if ( $catsincl ) {
+                if (!empty($data->catsincl)) {
                     // If the categories must be imported...
                     $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
                     for($k = 0; $k < sizeof($xmlcats); $k++) {
@@ -353,7 +321,7 @@ if ($xml = glossary_read_imported_file($xmlfile->get_content())) {
     }
     echo '</td>';
     echo '</tr>';
-    if ( $catsincl ) {
+    if (!empty($data->catsincl)) {
         echo '<tr>';
         echo '<td width="50%" align="right">';
         echo get_string("importedcategories","glossary");
@@ -388,4 +356,3 @@ if ($xml = glossary_read_imported_file($xmlfile->get_content())) {
 
 /// Finish the page
 echo $OUTPUT->footer();
-
diff --git a/mod/glossary/import_form.php b/mod/glossary/import_form.php
new file mode 100644 (file)
index 0000000..475644b
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+if (!defined('MOODLE_INTERNAL')) {
+    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
+}
+
+require_once($CFG->libdir.'/formslib.php');
+
+class mod_glossary_import_form extends moodleform {
+
+    function definition() {
+        global $CFG;
+        $mform =& $this->_form;
+        $cmid = $this->_customdata['id'];
+
+        $mform->addElement('filepicker', 'file', get_string('filetoimport', 'glossary'));
+        $mform->addHelpButton('file', 'filetoimport', 'glossary');
+        $options = array();
+        $options['current'] = get_string('currentglossary', 'glossary');
+        $options['newglossary'] = get_string('newglossary', 'glossary');
+        $mform->addElement('select', 'dest', get_string('currentglossary', 'glossary'), $options);
+        $mform->addHelpButton('dest', 'destination', 'glossary');
+        $mform->addElement('checkbox', 'catsincl', get_string('importcategories', 'glossary'));
+        $submit_string = get_string('submit');
+        $mform->addElement('hidden', 'id');
+        $mform->setType('id', PARAM_INT);
+        $this->add_action_buttons(false, $submit_string);
+    }
+}