Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Preset Menu
  20   *
  21   * This is the page that is the menu item in the config database
  22   * pages.
  23   *
  24   * This file is part of the Database module for Moodle
  25   *
  26   * @copyright 2005 Martin Dougiamas  http://dougiamas.com
  27   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   * @package mod_data
  29   */
  30  
  31  require_once('../../config.php');
  32  require_once($CFG->dirroot.'/mod/data/lib.php');
  33  require_once($CFG->dirroot.'/mod/data/preset_form.php');
  34  require_once($CFG->libdir.'/xmlize.php');
  35  
  36  $id     = optional_param('id', 0, PARAM_INT);           // course module id
  37  if ($id) {
  38      $cm = get_coursemodule_from_id('data', $id, null, null, MUST_EXIST);
  39      $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  40      $data = $DB->get_record('data', array('id'=>$cm->instance), '*', MUST_EXIST);
  41  } else {
  42      $d = required_param('d', PARAM_INT);     // database activity id
  43      $data = $DB->get_record('data', array('id'=>$d), '*', MUST_EXIST);
  44      $course = $DB->get_record('course', array('id'=>$data->course), '*', MUST_EXIST);
  45      $cm = get_coursemodule_from_instance('data', $data->id, $course->id, null, MUST_EXIST);
  46  }
  47  
  48  $context = context_module::instance($cm->id, MUST_EXIST);
  49  require_login($course, false, $cm);
  50  require_capability('mod/data:managetemplates', $context);
  51  $PAGE->set_url(new moodle_url('/mod/data/preset.php', array('d'=>$data->id)));
  52  $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
  53  $PAGE->set_heading($course->fullname);
  54  $PAGE->force_settings_menu(true);
  55  
  56  // fill in missing properties needed for updating of instance
  57  $data->course     = $cm->course;
  58  $data->cmidnumber = $cm->idnumber;
  59  $data->instance   = $cm->instance;
  60  
  61  $presets = data_get_available_presets($context);
  62  $strdelete = get_string('deleted', 'data');
  63  foreach ($presets as &$preset) {
  64      if (!empty($preset->userid)) {
  65          $namefields = get_all_user_name_fields(true);
  66          $presetuser = $DB->get_record('user', array('id' => $preset->userid), 'id, ' . $namefields, MUST_EXIST);
  67          $preset->description = $preset->name.' ('.fullname($presetuser, true).')';
  68      } else {
  69          $preset->userid = 0;
  70          $preset->description = $preset->name;
  71          if (data_user_can_delete_preset($context, $preset) && $preset->name != 'Image gallery') {
  72              $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
  73              $delicon = $OUTPUT->pix_icon('t/delete', $strdelete . ' ' . $preset->description);
  74              $preset->description .= html_writer::link($delurl, $delicon);
  75          }
  76      }
  77      if ($preset->userid > 0 && data_user_can_delete_preset($context, $preset)) {
  78          $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
  79          $delicon = $OUTPUT->pix_icon('t/delete', $strdelete . ' ' . $preset->description);
  80          $preset->description .= html_writer::link($delurl, $delicon);
  81      }
  82  }
  83  // This is required because its currently bound to the last element in the array.
  84  // If someone were to inadvently use it again and this call were not here
  85  unset($preset);
  86  
  87  $form_importexisting = new data_existing_preset_form(null, array('presets'=>$presets));
  88  $form_importexisting->set_data(array('d' => $data->id));
  89  
  90  $form_importzip = new data_import_preset_zip_form();
  91  $form_importzip->set_data(array('d' => $data->id));
  92  
  93  $form_export = new data_export_form();
  94  $form_export->set_data(array('d' => $data->id));
  95  
  96  $form_save = new data_save_preset_form();
  97  $form_save->set_data(array('d' => $data->id, 'name'=>$data->name));
  98  
  99  /* Output */
 100  if (!$form_export->is_submitted()) {
 101      echo $OUTPUT->header();
 102      echo $OUTPUT->heading(format_string($data->name), 2);
 103  
 104      // Needed for tabs.php
 105      $currenttab = 'presets';
 106      $currentgroup = groups_get_activity_group($cm);
 107      $groupmode = groups_get_activity_groupmode($cm);
 108      echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
 109  
 110      include ('tabs.php');
 111  }
 112  
 113  if (optional_param('sesskey', false, PARAM_BOOL) && confirm_sesskey()) {
 114  
 115      $renderer = $PAGE->get_renderer('mod_data');
 116  
 117      if ($formdata = $form_importexisting->get_data()) {
 118          $importer = new data_preset_existing_importer($course, $cm, $data, $formdata->fullname);
 119          echo $renderer->import_setting_mappings($data, $importer);
 120          echo $OUTPUT->footer();
 121          exit(0);
 122      } else if ($formdata = $form_importzip->get_data()) {
 123          $file = new stdClass;
 124          $file->name = $form_importzip->get_new_filename('importfile');
 125          $file->path = $form_importzip->save_temp_file('importfile');
 126          $importer = new data_preset_upload_importer($course, $cm, $data, $file->path);
 127          echo $renderer->import_setting_mappings($data, $importer);
 128          echo $OUTPUT->footer();
 129          exit(0);
 130      } else if ($formdata = $form_export->get_data()) {
 131  
 132          if (headers_sent()) {
 133              print_error('headersent');
 134          }
 135  
 136          $exportfile = data_presets_export($course, $cm, $data);
 137          $exportfilename = basename($exportfile);
 138          header("Content-Type: application/download\n");
 139          header("Content-Disposition: attachment; filename=\"$exportfilename\"");
 140          header('Expires: 0');
 141          header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
 142          header('Pragma: public');
 143  
 144          // If this file was requested from a form, then mark download as complete.
 145          \core_form\util::form_download_complete();
 146  
 147          $exportfilehandler = fopen($exportfile, 'rb');
 148          print fread($exportfilehandler, filesize($exportfile));
 149          fclose($exportfilehandler);
 150          unlink($exportfile);
 151          exit(0);
 152  
 153      } else if ($formdata = $form_save->get_data()) {
 154          if (!empty($formdata->overwrite)) {
 155              $selectedpreset = new stdClass();
 156              foreach ($presets as $preset) {
 157                  if ($preset->name == $formdata->name) {
 158                      $selectedpreset = $preset;
 159                      break;
 160                  }
 161              }
 162              if (isset($selectedpreset->name)) {
 163                  if (data_user_can_delete_preset($context, $selectedpreset)) {
 164                      data_delete_site_preset($formdata->name);
 165                  } else {
 166                      print_error('cannotoverwritepreset', 'data');
 167                  }
 168              }
 169          }
 170  
 171          // If the preset exists now then we need to throw an error.
 172          $sitepresets = data_get_available_site_presets($context);
 173          foreach ($sitepresets as $key=>$preset) {
 174              if ($formdata->name == $preset->name) {
 175                  print_error('errorpresetexists', 'preset');
 176              }
 177          }
 178  
 179          // Save the preset now
 180          data_presets_save($course, $cm, $data, $formdata->name);
 181  
 182          echo $OUTPUT->notification(get_string('savesuccess', 'data'), 'notifysuccess');
 183          echo $OUTPUT->continue_button($PAGE->url);
 184          echo $OUTPUT->footer();
 185          exit(0);
 186      } else {
 187          $action = optional_param('action', null, PARAM_ALPHANUM);
 188          $fullname = optional_param('fullname', '', PARAM_PATH); // directory the preset is in
 189          //
 190          // find out preset owner userid and shortname
 191          $parts = explode('/', $fullname, 2);
 192          $userid = empty($parts[0]) ? 0 : (int)$parts[0];
 193          $shortname = empty($parts[1]) ? '' : $parts[1];
 194  
 195          if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
 196              print_error('cannotaccesspresentsother', 'data');
 197          }
 198  
 199          if ($action == 'confirmdelete') {
 200              $path = data_preset_path($course, $userid, $shortname);
 201              $strwarning = get_string('deletewarning', 'data').'<br />'.$shortname;
 202              $optionsyes = array('fullname' => $userid.'/'.$shortname,
 203                               'action' => 'delete',
 204                               'd' => $data->id);
 205              $optionsno = array('d' => $data->id);
 206              echo $OUTPUT->confirm($strwarning, new moodle_url('preset.php', $optionsyes), new moodle_url('preset.php', $optionsno));
 207              echo $OUTPUT->footer();
 208              exit(0);
 209          } else if ($action == 'delete') {
 210              $selectedpreset = new stdClass();
 211              foreach ($presets as $preset) {
 212                  if ($preset->shortname == $shortname) {
 213                      $selectedpreset = $preset;
 214                  }
 215              }
 216              if (!isset($selectedpreset->shortname) || !data_user_can_delete_preset($context, $selectedpreset)) {
 217                 print_error('invalidrequest');
 218              }
 219  
 220              data_delete_site_preset($shortname);
 221  
 222              $strdeleted = get_string('deleted', 'data');
 223              echo $OUTPUT->notification("$shortname $strdeleted", 'notifysuccess');
 224          } else if ($action == 'finishimport') {
 225              $overwritesettings = optional_param('overwritesettings', false, PARAM_BOOL);
 226              if (!$fullname) {
 227                  $presetdir = $CFG->tempdir.'/forms/'.required_param('directory', PARAM_FILE);
 228                  if (!file_exists($presetdir) || !is_dir($presetdir)) {
 229                      print_error('cannotimport');
 230                  }
 231                  $importer = new data_preset_upload_importer($course, $cm, $data, $presetdir);
 232              } else {
 233                  $importer = new data_preset_existing_importer($course, $cm, $data, $fullname);
 234              }
 235              $importer->import($overwritesettings);
 236              $strimportsuccess = get_string('importsuccess', 'data');
 237              $straddentries = get_string('addentries', 'data');
 238              $strtodatabase = get_string('todatabase', 'data');
 239              if (!$DB->get_records('data_records', array('dataid'=>$data->id))) {
 240                  echo $OUTPUT->notification("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
 241              } else {
 242                  echo $OUTPUT->notification("$strimportsuccess", 'notifysuccess');
 243              }
 244          }
 245          echo $OUTPUT->continue_button($PAGE->url);
 246          echo $OUTPUT->footer();
 247          exit(0);
 248      }
 249  }
 250  
 251  // Export forms
 252  echo $OUTPUT->heading(get_string('export', 'data'), 3);
 253  $form_export->display();
 254  $form_save->display();
 255  
 256  // Import forms
 257  echo $OUTPUT->heading(get_string('import'), 3);
 258  $form_importzip->display();
 259  $form_importexisting->display();
 260  
 261  echo $OUTPUT->footer();