Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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              // Check if we're creating the new glossary on the front page or inside a course.
 145              if ($cm->course == SITEID) {
 146                  // On the front page, activities are in section 1.
 147                  $glossary->section = 1;
 148              } else {
 149                  // Inside a course, add to the general section (0).
 150                  $glossary->section = 0;
 151              }
 152              // New glossary is always visible.
 153              $glossary->visible = 1;
 154              $glossary->visibleoncoursepage = 1;
 155  
 156              // Include new glossary and return the new ID
 157              if ( !($glossary = add_moduleinfo($glossary, $course)) ) {
 158                  echo $OUTPUT->notification("Error while trying to create the new glossary.");
 159                  glossary_print_tabbed_table_end();
 160                  echo $OUTPUT->footer();
 161                  exit;
 162              } else {
 163                  $glossarycontext = context_module::instance($glossary->coursemodule);
 164                  glossary_xml_import_files($xmlglossary, 'INTROFILES', $glossarycontext->id, 'intro', 0);
 165                  echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
 166              }
 167          } else {
 168              echo $OUTPUT->notification("Error while trying to create the new glossary.");
 169              echo $OUTPUT->footer();
 170              exit;
 171          }
 172      }
 173  
 174      $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
 175      $sizeofxmlentries = is_array($xmlentries) ? count($xmlentries) : 0;
 176      for($i = 0; $i < $sizeofxmlentries; $i++) {
 177          // Inserting the entries
 178          $xmlentry = $xmlentries[$i];
 179          $newentry = new stdClass();
 180          $newentry->concept = trim($xmlentry['#']['CONCEPT'][0]['#']);
 181          $definition = $xmlentry['#']['DEFINITION'][0]['#'];
 182          if (!is_string($definition)) {
 183              print_error('errorparsingxml', 'glossary');
 184          }
 185          $newentry->definition = trusttext_strip($definition);
 186          if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
 187              $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
 188          } else {
 189              $newentry->casesensitive = $CFG->glossary_casesensitive;
 190          }
 191  
 192          $permissiongranted = 1;
 193          if ( $newentry->concept and $newentry->definition ) {
 194              if ( !$glossary->allowduplicatedentries ) {
 195                  // checking if the entry is valid (checking if it is duplicated when should not be)
 196                  if ( $newentry->casesensitive ) {
 197                      $dupentry = $DB->record_exists_select('glossary_entries',
 198                                      'glossaryid = :glossaryid AND concept = :concept', array(
 199                                          'glossaryid' => $glossary->id,
 200                                          'concept'    => $newentry->concept));
 201                  } else {
 202                      $dupentry = $DB->record_exists_select('glossary_entries',
 203                                      'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
 204                                          'glossaryid' => $glossary->id,
 205                                          'concept'    => core_text::strtolower($newentry->concept)));
 206                  }
 207                  if ($dupentry) {
 208                      $permissiongranted = 0;
 209                  }
 210              }
 211          } else {
 212              $permissiongranted = 0;
 213          }
 214          if ($permissiongranted) {
 215              $newentry->glossaryid       = $glossary->id;
 216              $newentry->sourceglossaryid = 0;
 217              $newentry->approved         = 1;
 218              $newentry->userid           = $USER->id;
 219              $newentry->teacherentry     = 1;
 220              $newentry->definitionformat = $xmlentry['#']['FORMAT'][0]['#'];
 221              $newentry->timecreated      = time();
 222              $newentry->timemodified     = time();
 223  
 224              // Setting the default values if no values were passed
 225              if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
 226                  $newentry->usedynalink      = $xmlentry['#']['USEDYNALINK'][0]['#'];
 227              } else {
 228                  $newentry->usedynalink      = $CFG->glossary_linkentries;
 229              }
 230              if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
 231                  $newentry->fullmatch        = $xmlentry['#']['FULLMATCH'][0]['#'];
 232              } else {
 233                  $newentry->fullmatch      = $CFG->glossary_fullmatch;
 234              }
 235  
 236              $newentry->id = $DB->insert_record("glossary_entries",$newentry);
 237              $importedentries++;
 238  
 239              $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
 240              $sizeofxmlaliases = is_array($xmlaliases) ? count($xmlaliases) : 0;
 241              for($k = 0; $k < $sizeofxmlaliases; $k++) {
 242              /// Importing aliases
 243                  $xmlalias = $xmlaliases[$k];
 244                  $aliasname = $xmlalias['#']['NAME'][0]['#'];
 245  
 246                  if (!empty($aliasname)) {
 247                      $newalias = new stdClass();
 248                      $newalias->entryid = $newentry->id;
 249                      $newalias->alias = trim($aliasname);
 250                      $newalias->id = $DB->insert_record("glossary_alias",$newalias);
 251                  }
 252              }
 253  
 254              if (!empty($data->catsincl)) {
 255                  // If the categories must be imported...
 256                  $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
 257                  $sizeofxmlcats = is_array($xmlcats) ? count($xmlcats) : 0;
 258                  for($k = 0; $k < $sizeofxmlcats; $k++) {
 259                      $xmlcat = $xmlcats[$k];
 260  
 261                      $newcat = new stdClass();
 262                      $newcat->name = $xmlcat['#']['NAME'][0]['#'];
 263                      $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
 264                      if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id,"name"=>$newcat->name))) {
 265                          // Create the category if it does not exist
 266                          $category = new stdClass();
 267                          $category->name = $newcat->name;
 268                          $category->glossaryid = $glossary->id;
 269                          $category->id = $DB->insert_record("glossary_categories",$category);
 270                          $importedcats++;
 271                      }
 272                      if ( $category ) {
 273                          // inserting the new relation
 274                          $entrycat = new stdClass();
 275                          $entrycat->entryid    = $newentry->id;
 276                          $entrycat->categoryid = $category->id;
 277                          $DB->insert_record("glossary_entries_categories",$entrycat);
 278                      }
 279                  }
 280              }
 281  
 282              // Import files embedded in the entry text.
 283              glossary_xml_import_files($xmlentry['#'], 'ENTRYFILES', $glossarycontext->id, 'entry', $newentry->id);
 284  
 285              // Import files attached to the entry.
 286              if (glossary_xml_import_files($xmlentry['#'], 'ATTACHMENTFILES', $glossarycontext->id, 'attachment', $newentry->id)) {
 287                  $DB->update_record("glossary_entries", array('id' => $newentry->id, 'attachment' => '1'));
 288              }
 289  
 290              // Import tags associated with the entry.
 291              if (core_tag_tag::is_enabled('mod_glossary', 'glossary_entries')) {
 292                  $xmltags = @$xmlentry['#']['TAGS'][0]['#']['TAG']; // Ignore missing TAGS.
 293                  $sizeofxmltags = is_array($xmltags) ? count($xmltags) : 0;
 294                  for ($k = 0; $k < $sizeofxmltags; $k++) {
 295                      // Importing tags.
 296                      $tag = $xmltags[$k]['#'];
 297                      if (!empty($tag)) {
 298                          core_tag_tag::add_item_tag('mod_glossary', 'glossary_entries', $newentry->id, $glossarycontext, $tag);
 299                      }
 300                  }
 301              }
 302  
 303          } else {
 304              $entriesrejected++;
 305              if ( $newentry->concept and $newentry->definition ) {
 306                  // add to exception report (duplicated entry))
 307                  $rejections .= "<tr><td>$newentry->concept</td>" .
 308                                 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
 309              } else {
 310                  // add to exception report (no concept or definition found))
 311                  $rejections .= "<tr><td>---</td>" .
 312                                 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
 313              }
 314          }
 315      }
 316  
 317      // Reset caches.
 318      \mod_glossary\local\concept_cache::reset_glossary($glossary);
 319  
 320      // processed entries
 321      echo $OUTPUT->box_start('glossarydisplay generalbox');
 322      echo '<table class="glossaryimportexport">';
 323      echo '<tr>';
 324      echo '<td width="50%" align="right">';
 325      echo get_string("totalentries","glossary");
 326      echo ':</td>';
 327      echo '<td width="50%" align="left">';
 328      echo $importedentries + $entriesrejected;
 329      echo '</td>';
 330      echo '</tr>';
 331      echo '<tr>';
 332      echo '<td width="50%" align="right">';
 333      echo get_string("importedentries","glossary");
 334      echo ':</td>';
 335      echo '<td width="50%" align="left">';
 336      echo $importedentries;
 337      if ( $entriesrejected ) {
 338          echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
 339      }
 340      echo '</td>';
 341      echo '</tr>';
 342      if (!empty($data->catsincl)) {
 343          echo '<tr>';
 344          echo '<td width="50%" align="right">';
 345          echo get_string("importedcategories","glossary");
 346          echo ':</td>';
 347          echo '<td width="50%">';
 348          echo $importedcats;
 349          echo '</td>';
 350          echo '</tr>';
 351      }
 352      echo '</table><hr />';
 353  
 354      // rejected entries
 355      if ($rejections) {
 356          echo $OUTPUT->heading(get_string("rejectionrpt","glossary"), 4);
 357          echo '<table class="glossaryimportexport">';
 358          echo $rejections;
 359          echo '</table><hr />';
 360      }
 361  /// Print continue button, based on results
 362      if ($importedentries) {
 363          echo $OUTPUT->continue_button('view.php?id='.$id);
 364      } else {
 365          echo $OUTPUT->continue_button('import.php?id='.$id);
 366      }
 367      echo $OUTPUT->box_end();
 368  } else {
 369      echo $OUTPUT->box_start('glossarydisplay generalbox');
 370      echo get_string('errorparsingxml', 'glossary');
 371      echo $OUTPUT->continue_button('import.php?id='.$id);
 372      echo $OUTPUT->box_end();
 373  }
 374  
 375  /// Finish the page
 376  echo $OUTPUT->footer();