Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/glossary/import.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once("../../config.php");
00004 require_once("lib.php");
00005 require_once("$CFG->dirroot/course/lib.php");
00006 require_once('import_form.php');
00007 
00008 $id = required_param('id', PARAM_INT);    // Course Module ID
00009 
00010 $mode     = optional_param('mode', 'letter', PARAM_ALPHA );
00011 $hook     = optional_param('hook', 'ALL', PARAM_ALPHANUM);
00012 
00013 $url = new moodle_url('/mod/glossary/import.php', array('id'=>$id));
00014 if ($mode !== 'letter') {
00015     $url->param('mode', $mode);
00016 }
00017 if ($hook !== 'ALL') {
00018     $url->param('hook', $hook);
00019 }
00020 $PAGE->set_url($url);
00021 
00022 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
00023     print_error('invalidcoursemodule');
00024 }
00025 
00026 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
00027     print_error('coursemisconf');
00028 }
00029 
00030 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
00031     print_error('invalidid', 'glossary');
00032 }
00033 
00034 require_login($course->id, false, $cm);
00035 
00036 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00037 require_capability('mod/glossary:import', $context);
00038 
00039 $strglossaries = get_string("modulenameplural", "glossary");
00040 $strglossary = get_string("modulename", "glossary");
00041 $strallcategories = get_string("allcategories", "glossary");
00042 $straddentry = get_string("addentry", "glossary");
00043 $strnoentries = get_string("noentries", "glossary");
00044 $strsearchindefinition = get_string("searchindefinition", "glossary");
00045 $strsearch = get_string("search");
00046 $strimportentries = get_string('importentriesfromxml', 'glossary');
00047 
00048 $PAGE->navbar->add($strimportentries);
00049 $PAGE->set_title(format_string($glossary->name));
00050 $PAGE->set_heading($course->fullname);
00051 
00052 echo $OUTPUT->header();
00053 echo $OUTPUT->heading($strimportentries);
00054 
00055 $form = new mod_glossary_import_form();
00056 
00057 if ( !$data = $form->get_data() ) {
00058     echo $OUTPUT->box_start('glossarydisplay generalbox');
00059     // display upload form
00060     $data = new stdClass();
00061     $data->id = $id;
00062     $form->set_data($data);
00063     $form->display();
00064     echo $OUTPUT->box_end();
00065     echo $OUTPUT->footer();
00066     exit;
00067 }
00068 
00069 $result = $form->get_file_content('file');
00070 
00071 if (empty($result)) {
00072     echo $OUTPUT->box_start('glossarydisplay generalbox');
00073     echo $OUTPUT->continue_button('import.php?id='.$id);
00074     echo $OUTPUT->box_end();
00075     echo $OUTPUT->footer();
00076     die();
00077 }
00078 
00079 if ($xml = glossary_read_imported_file($result)) {
00080     $importedentries = 0;
00081     $importedcats    = 0;
00082     $entriesrejected = 0;
00083     $rejections      = '';
00084 
00085     if ($data->dest == 'newglossary') {
00086         // If the user chose to create a new glossary
00087         $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
00088 
00089         if ( $xmlglossary['NAME'][0]['#'] ) {
00090             unset($glossary);
00091             $glossary->name = ($xmlglossary['NAME'][0]['#']);
00092             $glossary->course = $course->id;
00093             $glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
00094             $glossary->intro = ($xmlglossary['INTRO'][0]['#']);
00095             $glossary->introformat = isset($xmlglossary['INTROFORMAT'][0]['#']) ? $xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE;
00096             $glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']);
00097             $glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']);
00098             $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']);
00099             $glossary->timecreated = time();
00100             $glossary->timemodified = time();
00101             $glossary->cmidnumber = $cm->idnumber;
00102 
00103             // Setting the default values if no values were passed
00104             if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
00105                 $glossary->entbypage = ($xmlglossary['ENTBYPAGE'][0]['#']);
00106             } else {
00107                 $glossary->entbypage = $CFG->glossary_entbypage;
00108             }
00109             if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
00110                 $glossary->allowduplicatedentries = ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
00111             } else {
00112                 $glossary->allowduplicatedentries = $CFG->glossary_dupentries;
00113             }
00114             if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
00115                 $glossary->displayformat = ($xmlglossary['DISPLAYFORMAT'][0]['#']);
00116             } else {
00117                 $glossary->displayformat = 2;
00118             }
00119             if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
00120                 $glossary->allowcomments = ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
00121             } else {
00122                 $glossary->allowcomments = $CFG->glossary_allowcomments;
00123             }
00124             if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
00125                 $glossary->usedynalink = ($xmlglossary['USEDYNALINK'][0]['#']);
00126             } else {
00127                 $glossary->usedynalink = $CFG->glossary_linkentries;
00128             }
00129             if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
00130                 $glossary->defaultapproval = ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
00131             } else {
00132                 $glossary->defaultapproval = $CFG->glossary_defaultapproval;
00133             }
00134 
00135             // Include new glossary and return the new ID
00136             if ( !$glossary->id = glossary_add_instance($glossary) ) {
00137                 echo $OUTPUT->notification("Error while trying to create the new glossary.");
00138                 glossary_print_tabbed_table_end();
00139                 echo $OUTPUT->footer();
00140                 exit;
00141             } else {
00142                 //The instance has been created, so lets do course_modules
00143                 //and course_sections
00144                 $mod->groupmode = $course->groupmode;  
00145 
00146                 $mod->instance = $glossary->id;
00147                 // course_modules and course_sections each contain a reference
00148                 // to each other, so we have to update one of them twice.
00149 
00150                 if (! $currmodule = $DB->get_record("modules", array("name"=>'glossary'))) {
00151                     print_error('modulenotexist', 'debug', '', 'Glossary');
00152                 }
00153                 $mod->module = $currmodule->id;
00154                 $mod->course = $course->id;
00155                 $mod->modulename = 'glossary';
00156                 $mod->section = 0;
00157 
00158                 if (! $mod->coursemodule = add_course_module($mod) ) {
00159                     print_error('cannotaddcoursemodule');
00160                 }
00161 
00162                 if (! $sectionid = add_mod_to_section($mod) ) {
00163                     print_error('cannotaddcoursemoduletosection');
00164                 }
00165                 //We get the section's visible field status
00166                 $visible = $DB->get_field("course_sections", "visible", array("id"=>$sectionid));
00167 
00168                 $DB->set_field("course_modules", "visible", $visible, array("id"=>$mod->coursemodule));
00169                 $DB->set_field("course_modules", "section", $sectionid, array("id"=>$mod->coursemodule));
00170 
00171                 add_to_log($course->id, "course", "add mod",
00172                            "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
00173                            "$mod->modulename $mod->instance");
00174                 add_to_log($course->id, $mod->modulename, "add",
00175                            "view.php?id=$mod->coursemodule",
00176                            "$mod->instance", $mod->coursemodule);
00177 
00178                 rebuild_course_cache($course->id);
00179 
00180                 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
00181             }
00182         } else {
00183             echo $OUTPUT->notification("Error while trying to create the new glossary.");
00184             echo $OUTPUT->footer();
00185             exit;
00186         }
00187     }
00188 
00189     $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
00190     $sizeofxmlentries = sizeof($xmlentries);
00191     for($i = 0; $i < $sizeofxmlentries; $i++) {
00192         // Inserting the entries
00193         $xmlentry = $xmlentries[$i];
00194         unset($newentry);
00195         $newentry->concept = trim($xmlentry['#']['CONCEPT'][0]['#']);
00196         $newentry->definition = trusttext_strip($xmlentry['#']['DEFINITION'][0]['#']);
00197         if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
00198             $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
00199         } else {
00200             $newentry->casesensitive = $CFG->glossary_casesensitive;
00201         }
00202 
00203         $permissiongranted = 1;
00204         if ( $newentry->concept and $newentry->definition ) {
00205             if ( !$glossary->allowduplicatedentries ) {
00206                 // checking if the entry is valid (checking if it is duplicated when should not be)
00207                 if ( $newentry->casesensitive ) {
00208                     $dupentry = $DB->record_exists_select('glossary_entries',
00209                                     'glossaryid = :glossaryid AND concept = :concept', array(
00210                                         'glossaryid' => $glossary->id,
00211                                         'concept'    => $newentry->concept));
00212                 } else {
00213                     $dupentry = $DB->record_exists_select('glossary_entries',
00214                                     'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
00215                                         'glossaryid' => $glossary->id,
00216                                         'concept'    => moodle_strtolower($newentry->concept)));
00217                 }
00218                 if ($dupentry) {
00219                     $permissiongranted = 0;
00220                 }
00221             }
00222         } else {
00223             $permissiongranted = 0;
00224         }
00225         if ($permissiongranted) {
00226             $newentry->glossaryid       = $glossary->id;
00227             $newentry->sourceglossaryid = 0;
00228             $newentry->approved         = 1;
00229             $newentry->userid           = $USER->id;
00230             $newentry->teacherentry     = 1;
00231             $newentry->definitionformat = $xmlentry['#']['FORMAT'][0]['#'];
00232             $newentry->timecreated      = time();
00233             $newentry->timemodified     = time();
00234 
00235             // Setting the default values if no values were passed
00236             if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
00237                 $newentry->usedynalink      = $xmlentry['#']['USEDYNALINK'][0]['#'];
00238             } else {
00239                 $newentry->usedynalink      = $CFG->glossary_linkentries;
00240             }
00241             if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
00242                 $newentry->fullmatch        = $xmlentry['#']['FULLMATCH'][0]['#'];
00243             } else {
00244                 $newentry->fullmatch      = $CFG->glossary_fullmatch;
00245             }
00246 
00247             $newentry->id = $DB->insert_record("glossary_entries",$newentry);
00248             $importedentries++;
00249 
00250             $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
00251             $sizeofxmlaliases = sizeof($xmlaliases);
00252             for($k = 0; $k < $sizeofxmlaliases; $k++) {
00254                 $xmlalias = $xmlaliases[$k];
00255                 $aliasname = $xmlalias['#']['NAME'][0]['#'];
00256 
00257                 if (!empty($aliasname)) {
00258                     $newalias = new stdClass();
00259                     $newalias->entryid = $newentry->id;
00260                     $newalias->alias = trim($aliasname);
00261                     $newalias->id = $DB->insert_record("glossary_alias",$newalias);
00262                 }
00263             }
00264 
00265             if (!empty($data->catsincl)) {
00266                 // If the categories must be imported...
00267                 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
00268                 $sizeofxmlcats = sizeof($xmlcats);
00269                 for($k = 0; $k < $sizeofxmlcats; $k++) {
00270                     $xmlcat = $xmlcats[$k];
00271 
00272                     $newcat = new stdClass();
00273                     $newcat->name = $xmlcat['#']['NAME'][0]['#'];
00274                     $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
00275                     if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id,"name"=>$newcat->name))) {
00276                         // Create the category if it does not exist
00277                         $category = new stdClass();
00278                         $category->name = $newcat->name;
00279                         $category->glossaryid = $glossary->id;
00280                         $category->id = $DB->insert_record("glossary_categories",$category);
00281                         $importedcats++;
00282                     }
00283                     if ( $category ) {
00284                         // inserting the new relation
00285                         $entrycat = new stdClass();
00286                         $entrycat->entryid    = $newentry->id;
00287                         $entrycat->categoryid = $category->id;
00288                         $DB->insert_record("glossary_entries_categories",$entrycat);
00289                     }
00290                 }
00291             }
00292         } else {
00293             $entriesrejected++;
00294             if ( $newentry->concept and $newentry->definition ) {
00295                 // add to exception report (duplicated entry))
00296                 $rejections .= "<tr><td>$newentry->concept</td>" .
00297                                "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
00298             } else {
00299                 // add to exception report (no concept or definition found))
00300                 $rejections .= "<tr><td>---</td>" .
00301                                "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
00302             }
00303         }
00304     }
00305     // processed entries
00306     echo $OUTPUT->box_start('glossarydisplay generalbox');
00307     echo '<table class="glossaryimportexport">';
00308     echo '<tr>';
00309     echo '<td width="50%" align="right">';
00310     echo get_string("totalentries","glossary");
00311     echo ':</td>';
00312     echo '<td width="50%" align="left">';
00313     echo $importedentries + $entriesrejected;
00314     echo '</td>';
00315     echo '</tr>';
00316     echo '<tr>';
00317     echo '<td width="50%" align="right">';
00318     echo get_string("importedentries","glossary");
00319     echo ':</td>';
00320     echo '<td width="50%" align="left">';
00321     echo $importedentries;
00322     if ( $entriesrejected ) {
00323         echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
00324     }
00325     echo '</td>';
00326     echo '</tr>';
00327     if (!empty($data->catsincl)) {
00328         echo '<tr>';
00329         echo '<td width="50%" align="right">';
00330         echo get_string("importedcategories","glossary");
00331         echo ':</td>';
00332         echo '<td width="50%">';
00333         echo $importedcats;
00334         echo '</td>';
00335         echo '</tr>';
00336     }
00337     echo '</table><hr />';
00338 
00339     // rejected entries
00340     if ($rejections) {
00341         echo $OUTPUT->heading(get_string("rejectionrpt","glossary"), 4);
00342         echo '<table class="glossaryimportexport">';
00343         echo $rejections;
00344         echo '</table><hr />';
00345     }
00347     if ($importedentries) {
00348         echo $OUTPUT->continue_button('view.php?id='.$id);
00349     } else {
00350         echo $OUTPUT->continue_button('import.php?id='.$id);
00351     }
00352     echo $OUTPUT->box_end();
00353 } else {
00354     echo $OUTPUT->box_start('glossarydisplay generalbox');
00355     echo get_string('errorparsingxml', 'glossary');
00356     echo $OUTPUT->continue_button('import.php?id='.$id);
00357     echo $OUTPUT->box_end();
00358 }
00359 
00361 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations