[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/glossary/ -> import.php (source)

   1  <?php
   2  
   3  require_once("../../config.php");
   4  require_once ("lib.php");
   5  require_once("$CFG->dirroot/course/lib.php");
   6  require_once("$CFG->dirroot/course/modlib.php");
   7  require_once ('import_form.php');
   8  
   9  $id = required_param('id', PARAM_INT);    // Course Module ID
  10  
  11  $mode     = optional_param('mode', 'letter', PARAM_ALPHA );
  12  $hook     = optional_param('hook', 'ALL', PARAM_ALPHANUM);
  13  
  14  $url = new moodle_url('/mod/glossary/import.php', array('id'=>$id));
  15  if ($mode !== 'letter') {
  16      $url->param('mode', $mode);
  17  }
  18  if ($hook !== 'ALL') {
  19      $url->param('hook', $hook);
  20  }
  21  $PAGE->set_url($url);
  22  
  23  if (! $cm = get_coursemodule_from_id('glossary', $id)) {
  24      print_error('invalidcoursemodule');
  25  }
  26  
  27  if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
  28      print_error('coursemisconf');
  29  }
  30  
  31  if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
  32      print_error('invalidid', 'glossary');
  33  }
  34  
  35  require_login($course, false, $cm);
  36  
  37  $context = context_module::instance($cm->id);
  38  require_capability('mod/glossary:import', $context);
  39  
  40  $strglossaries = get_string("modulenameplural", "glossary");
  41  $strglossary = get_string("modulename", "glossary");
  42  $strallcategories = get_string("allcategories", "glossary");
  43  $straddentry = get_string("addentry", "glossary");
  44  $strnoentries = get_string("noentries", "glossary");
  45  $strsearchindefinition = get_string("searchindefinition", "glossary");
  46  $strsearch = get_string("search");
  47  $strimportentries = get_string('importentriesfromxml', 'glossary');
  48  
  49  $PAGE->navbar->add($strimportentries);
  50  $PAGE->set_title($glossary->name);
  51  $PAGE->set_heading($course->fullname);
  52  
  53  echo $OUTPUT->header();
  54  echo $OUTPUT->heading($strimportentries);
  55  
  56  $form = new mod_glossary_import_form();
  57  
  58  if ( !$data = $form->get_data() ) {
  59      echo $OUTPUT->box_start('glossarydisplay generalbox');
  60      // display upload form
  61      $data = new stdClass();
  62      $data->id = $id;
  63      $form->set_data($data);
  64      $form->display();
  65      echo $OUTPUT->box_end();
  66      echo $OUTPUT->footer();
  67      exit;
  68  }
  69  
  70  $result = $form->get_file_content('file');
  71  
  72  if (empty($result)) {
  73      echo $OUTPUT->box_start('glossarydisplay generalbox');
  74      echo $OUTPUT->continue_button('import.php?id='.$id);
  75      echo $OUTPUT->box_end();
  76      echo $OUTPUT->footer();
  77      die();
  78  }
  79  
  80  // Large exports are likely to take their time and memory.
  81  core_php_time_limit::raise();
  82  raise_memory_limit(MEMORY_EXTRA);
  83  
  84  if ($xml = glossary_read_imported_file($result)) {
  85      $importedentries = 0;
  86      $importedcats    = 0;
  87      $entriesrejected = 0;
  88      $rejections      = '';
  89      $glossarycontext = $context;
  90  
  91      if ($data->dest == 'newglossary') {
  92          // If the user chose to create a new glossary
  93          $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
  94  
  95          if ( $xmlglossary['NAME'][0]['#'] ) {
  96              $glossary = new stdClass();
  97              $glossary->modulename = 'glossary';
  98              $glossary->module = $cm->module;
  99              $glossary->name = ($xmlglossary['NAME'][0]['#']);
 100              $glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
 101              $glossary->intro = ($xmlglossary['INTRO'][0]['#']);
 102              $glossary->introformat = isset($xmlglossary['INTROFORMAT'][0]['#']) ? $xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE;
 103              $glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']);
 104              $glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']);
 105              $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']);
 106              $glossary->cmidnumber = null;
 107  
 108              // Setting the default values if no values were passed
 109              if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
 110                  $glossary->entbypage = ($xmlglossary['ENTBYPAGE'][0]['#']);
 111              } else {
 112                  $glossary->entbypage = $CFG->glossary_entbypage;
 113              }
 114              if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
 115                  $glossary->allowduplicatedentries = ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
 116              } else {
 117                  $glossary->allowduplicatedentries = $CFG->glossary_dupentries;
 118              }
 119              if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
 120                  $glossary->displayformat = ($xmlglossary['DISPLAYFORMAT'][0]['#']);
 121              } else {
 122                  $glossary->displayformat = 2;
 123              }
 124              if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
 125                  $glossary->allowcomments = ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
 126              } else {
 127                  $glossary->allowcomments = $CFG->glossary_allowcomments;
 128              }
 129              if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
 130                  $glossary->usedynalink = ($xmlglossary['USEDYNALINK'][0]['#']);
 131              } else {
 132                  $glossary->usedynalink = $CFG->glossary_linkentries;
 133              }
 134              if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
 135                  $glossary->defaultapproval = ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
 136              } else {
 137                  $glossary->defaultapproval = $CFG->glossary_defaultapproval;
 138              }
 139  
 140              // These fields were not included in export, assume zero.
 141              $glossary->assessed = 0;
 142              $glossary->availability = null;
 143  
 144              // New glossary is to be inserted in section 0, it is always visible.
 145              $glossary->section = 0;
 146              $glossary->visible = 1;
 147  
 148              // Include new glossary and return the new ID
 149              if ( !($glossary = add_moduleinfo($glossary, $course)) ) {
 150                  echo $OUTPUT->notification("Error while trying to create the new glossary.");
 151                  glossary_print_tabbed_table_end();
 152                  echo $OUTPUT->footer();
 153                  exit;
 154              } else {
 155                  $glossarycontext = context_module::instance($glossary->coursemodule);
 156                  glossary_xml_import_files($xmlglossary, 'INTROFILES', $glossarycontext->id, 'intro', 0);
 157                  echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
 158              }
 159          } else {
 160              echo $OUTPUT->notification("Error while trying to create the new glossary.");
 161              echo $OUTPUT->footer();
 162              exit;
 163          }
 164      }
 165  
 166      $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
 167      $sizeofxmlentries = sizeof($xmlentries);
 168      for($i = 0; $i < $sizeofxmlentries; $i++) {
 169          // Inserting the entries
 170          $xmlentry = $xmlentries[$i];
 171          $newentry = new stdClass();
 172          $newentry->concept = trim($xmlentry['#']['CONCEPT'][0]['#']);
 173          $newentry->definition = trusttext_strip($xmlentry['#']['DEFINITION'][0]['#']);
 174          if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
 175              $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
 176          } else {
 177              $newentry->casesensitive = $CFG->glossary_casesensitive;
 178          }
 179  
 180          $permissiongranted = 1;
 181          if ( $newentry->concept and $newentry->definition ) {
 182              if ( !$glossary->allowduplicatedentries ) {
 183                  // checking if the entry is valid (checking if it is duplicated when should not be)
 184                  if ( $newentry->casesensitive ) {
 185                      $dupentry = $DB->record_exists_select('glossary_entries',
 186                                      'glossaryid = :glossaryid AND concept = :concept', array(
 187                                          'glossaryid' => $glossary->id,
 188                                          'concept'    => $newentry->concept));
 189                  } else {
 190                      $dupentry = $DB->record_exists_select('glossary_entries',
 191                                      'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
 192                                          'glossaryid' => $glossary->id,
 193                                          'concept'    => core_text::strtolower($newentry->concept)));
 194                  }
 195                  if ($dupentry) {
 196                      $permissiongranted = 0;
 197                  }
 198              }
 199          } else {
 200              $permissiongranted = 0;
 201          }
 202          if ($permissiongranted) {
 203              $newentry->glossaryid       = $glossary->id;
 204              $newentry->sourceglossaryid = 0;
 205              $newentry->approved         = 1;
 206              $newentry->userid           = $USER->id;
 207              $newentry->teacherentry     = 1;
 208              $newentry->definitionformat = $xmlentry['#']['FORMAT'][0]['#'];
 209              $newentry->timecreated      = time();
 210              $newentry->timemodified     = time();
 211  
 212              // Setting the default values if no values were passed
 213              if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
 214                  $newentry->usedynalink      = $xmlentry['#']['USEDYNALINK'][0]['#'];
 215              } else {
 216                  $newentry->usedynalink      = $CFG->glossary_linkentries;
 217              }
 218              if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
 219                  $newentry->fullmatch        = $xmlentry['#']['FULLMATCH'][0]['#'];
 220              } else {
 221                  $newentry->fullmatch      = $CFG->glossary_fullmatch;
 222              }
 223  
 224              $newentry->id = $DB->insert_record("glossary_entries",$newentry);
 225              $importedentries++;
 226  
 227              $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
 228              $sizeofxmlaliases = sizeof($xmlaliases);
 229              for($k = 0; $k < $sizeofxmlaliases; $k++) {
 230              /// Importing aliases
 231                  $xmlalias = $xmlaliases[$k];
 232                  $aliasname = $xmlalias['#']['NAME'][0]['#'];
 233  
 234                  if (!empty($aliasname)) {
 235                      $newalias = new stdClass();
 236                      $newalias->entryid = $newentry->id;
 237                      $newalias->alias = trim($aliasname);
 238                      $newalias->id = $DB->insert_record("glossary_alias",$newalias);
 239                  }
 240              }
 241  
 242              if (!empty($data->catsincl)) {
 243                  // If the categories must be imported...
 244                  $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
 245                  $sizeofxmlcats = sizeof($xmlcats);
 246                  for($k = 0; $k < $sizeofxmlcats; $k++) {
 247                      $xmlcat = $xmlcats[$k];
 248  
 249                      $newcat = new stdClass();
 250                      $newcat->name = $xmlcat['#']['NAME'][0]['#'];
 251                      $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
 252                      if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id,"name"=>$newcat->name))) {
 253                          // Create the category if it does not exist
 254                          $category = new stdClass();
 255                          $category->name = $newcat->name;
 256                          $category->glossaryid = $glossary->id;
 257                          $category->id = $DB->insert_record("glossary_categories",$category);
 258                          $importedcats++;
 259                      }
 260                      if ( $category ) {
 261                          // inserting the new relation
 262                          $entrycat = new stdClass();
 263                          $entrycat->entryid    = $newentry->id;
 264                          $entrycat->categoryid = $category->id;
 265                          $DB->insert_record("glossary_entries_categories",$entrycat);
 266                      }
 267                  }
 268              }
 269  
 270              // Import files embedded in the entry text.
 271              glossary_xml_import_files($xmlentry['#'], 'ENTRYFILES', $glossarycontext->id, 'entry', $newentry->id);
 272  
 273              // Import files attached to the entry.
 274              if (glossary_xml_import_files($xmlentry['#'], 'ATTACHMENTFILES', $glossarycontext->id, 'attachment', $newentry->id)) {
 275                  $DB->update_record("glossary_entries", array('id' => $newentry->id, 'attachment' => '1'));
 276              }
 277  
 278          } else {
 279              $entriesrejected++;
 280              if ( $newentry->concept and $newentry->definition ) {
 281                  // add to exception report (duplicated entry))
 282                  $rejections .= "<tr><td>$newentry->concept</td>" .
 283                                 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
 284              } else {
 285                  // add to exception report (no concept or definition found))
 286                  $rejections .= "<tr><td>---</td>" .
 287                                 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
 288              }
 289          }
 290      }
 291  
 292      // Reset caches.
 293      \mod_glossary\local\concept_cache::reset_glossary($glossary);
 294  
 295      // processed entries
 296      echo $OUTPUT->box_start('glossarydisplay generalbox');
 297      echo '<table class="glossaryimportexport">';
 298      echo '<tr>';
 299      echo '<td width="50%" align="right">';
 300      echo get_string("totalentries","glossary");
 301      echo ':</td>';
 302      echo '<td width="50%" align="left">';
 303      echo $importedentries + $entriesrejected;
 304      echo '</td>';
 305      echo '</tr>';
 306      echo '<tr>';
 307      echo '<td width="50%" align="right">';
 308      echo get_string("importedentries","glossary");
 309      echo ':</td>';
 310      echo '<td width="50%" align="left">';
 311      echo $importedentries;
 312      if ( $entriesrejected ) {
 313          echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
 314      }
 315      echo '</td>';
 316      echo '</tr>';
 317      if (!empty($data->catsincl)) {
 318          echo '<tr>';
 319          echo '<td width="50%" align="right">';
 320          echo get_string("importedcategories","glossary");
 321          echo ':</td>';
 322          echo '<td width="50%">';
 323          echo $importedcats;
 324          echo '</td>';
 325          echo '</tr>';
 326      }
 327      echo '</table><hr />';
 328  
 329      // rejected entries
 330      if ($rejections) {
 331          echo $OUTPUT->heading(get_string("rejectionrpt","glossary"), 4);
 332          echo '<table class="glossaryimportexport">';
 333          echo $rejections;
 334          echo '</table><hr />';
 335      }
 336  /// Print continue button, based on results
 337      if ($importedentries) {
 338          echo $OUTPUT->continue_button('view.php?id='.$id);
 339      } else {
 340          echo $OUTPUT->continue_button('import.php?id='.$id);
 341      }
 342      echo $OUTPUT->box_end();
 343  } else {
 344      echo $OUTPUT->box_start('glossarydisplay generalbox');
 345      echo get_string('errorparsingxml', 'glossary');
 346      echo $OUTPUT->continue_button('import.php?id='.$id);
 347      echo $OUTPUT->box_end();
 348  }
 349  
 350  /// Finish the page
 351  echo $OUTPUT->footer();


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1