Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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  
  35  $id = optional_param('id', 0, PARAM_INT); // The course module id.
  36  
  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  $action = optional_param('action', 'view', PARAM_ALPHA); // The page action.
  49  $allowedactions = ['view', 'delete', 'confirmdelete', 'import', 'importzip', 'finishimport',
  50      'export'];
  51  if (!in_array($action, $allowedactions)) {
  52      throw new moodle_exception('invalidaccess');
  53  }
  54  
  55  $context = context_module::instance($cm->id, MUST_EXIST);
  56  require_login($course, false, $cm);
  57  require_capability('mod/data:managetemplates', $context);
  58  
  59  $url = new moodle_url('/mod/data/preset.php', array('d' => $data->id));
  60  
  61  $PAGE->set_url($url);
  62  $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
  63  $PAGE->set_heading($course->fullname);
  64  $PAGE->force_settings_menu(true);
  65  $PAGE->activityheader->disable();
  66  
  67  // fill in missing properties needed for updating of instance
  68  $data->course     = $cm->course;
  69  $data->cmidnumber = $cm->idnumber;
  70  $data->instance   = $cm->instance;
  71  
  72  $renderer = $PAGE->get_renderer('mod_data');
  73  $presets = data_get_available_presets($context);
  74  
  75  if ($action === 'export') {
  76      if (headers_sent()) {
  77          print_error('headersent');
  78      }
  79  
  80      $exportfile = data_presets_export($course, $cm, $data);
  81      $exportfilename = basename($exportfile);
  82      header("Content-Type: application/download\n");
  83      header("Content-Disposition: attachment; filename=\"$exportfilename\"");
  84      header('Expires: 0');
  85      header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
  86      header('Pragma: public');
  87  
  88      // If this file was requested from a form, then mark download as complete.
  89      \core_form\util::form_download_complete();
  90  
  91      $exportfilehandler = fopen($exportfile, 'rb');
  92      print fread($exportfilehandler, filesize($exportfile));
  93      fclose($exportfilehandler);
  94      unlink($exportfile);
  95      exit(0);
  96  }
  97  
  98  $formimportzip = new data_import_preset_zip_form();
  99  $formimportzip->set_data(array('d' => $data->id));
 100  
 101  if ($formimportzip->is_cancelled()) {
 102      redirect(new moodle_url('/mod/data/preset.php', ['d' => $data->id]));
 103  }
 104  
 105  echo $OUTPUT->header();
 106  
 107  if ($formdata = $formimportzip->get_data()) {
 108      echo $OUTPUT->heading(get_string('importpreset', 'data'), 2, 'mb-4');
 109      $file = new stdClass;
 110      $file->name = $formimportzip->get_new_filename('importfile');
 111      $file->path = $formimportzip->save_temp_file('importfile');
 112      $importer = new data_preset_upload_importer($course, $cm, $data, $file->path);
 113      echo $renderer->import_setting_mappings($data, $importer);
 114      echo $OUTPUT->footer();
 115      exit(0);
 116  }
 117  
 118  if (in_array($action, ['confirmdelete', 'delete', 'finishimport'])) {
 119      $fullname = optional_param('fullname', '' , PARAM_PATH); // The directory the preset is in.
 120      // Find out preset owner userid and shortname.
 121      $parts = explode('/', $fullname, 2);
 122      $userid = empty($parts[0]) ? 0 : (int)$parts[0];
 123      $shortname = empty($parts[1]) ? '' : $parts[1];
 124      echo html_writer::start_div('overflow-hidden');
 125  
 126      if ($action === 'confirmdelete') {
 127          $path = data_preset_path($course, $userid, $shortname);
 128          $strwarning = get_string('deletewarning', 'data').'<br />'.$shortname;
 129          $optionsyes = [
 130              'fullname' => $fullname,
 131              'action' => 'delete',
 132              'd' => $data->id,
 133          ];
 134          $optionsno = ['d' => $data->id];
 135          echo $OUTPUT->confirm($strwarning, new moodle_url('/mod/data/preset.php', $optionsyes),
 136              new moodle_url('/mod/data/preset.php', $optionsno));
 137          echo $OUTPUT->footer();
 138          exit(0);
 139      } else if ($action === 'delete') {
 140          if (!confirm_sesskey()) {
 141              throw new moodle_exception('invalidsesskey');
 142          }
 143          $selectedpreset = new stdClass();
 144          foreach ($presets as $preset) {
 145              if ($preset->shortname == $shortname) {
 146                  $selectedpreset = $preset;
 147              }
 148          }
 149          if (!isset($selectedpreset->shortname) || !data_user_can_delete_preset($context, $selectedpreset)) {
 150              print_error('invalidrequest');
 151          }
 152  
 153          data_delete_site_preset($shortname);
 154          $strdeleted = get_string('deleted', 'data');
 155          echo $OUTPUT->notification("$shortname $strdeleted", 'notifysuccess');
 156      } else if ($action === 'finishimport') {
 157          if (!confirm_sesskey()) {
 158              throw new moodle_exception('invalidsesskey');
 159          }
 160          $overwritesettings = optional_param('overwritesettings', false, PARAM_BOOL);
 161          if (!$fullname) {
 162              $presetdir = $CFG->tempdir.'/forms/'.required_param('directory', PARAM_FILE);
 163              if (!file_exists($presetdir) || !is_dir($presetdir)) {
 164                  print_error('cannotimport');
 165              }
 166              $importer = new data_preset_upload_importer($course, $cm, $data, $presetdir);
 167          } else {
 168              $importer = new data_preset_existing_importer($course, $cm, $data, $fullname);
 169          }
 170          $importer->import($overwritesettings);
 171          $strimportsuccess = get_string('importsuccess', 'data');
 172          $straddentries = get_string('addentries', 'data');
 173          $strtodatabase = get_string('todatabase', 'data');
 174          if (!$DB->get_records('data_records', array('dataid'=>$data->id))) {
 175              echo $OUTPUT->notification("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
 176          } else {
 177              echo $OUTPUT->notification("$strimportsuccess", 'notifysuccess');
 178          }
 179      }
 180      echo $OUTPUT->continue_button(new moodle_url('/mod/data/preset.php', ['d' => $data->id]));
 181      echo html_writer::end_div();
 182      echo $OUTPUT->footer();
 183      exit(0);
 184  }
 185  
 186  if ($action === 'import') {
 187      echo $OUTPUT->heading(get_string('importpreset', 'data'), 2, 'mb-4');
 188      echo $formimportzip->display();
 189  } else {
 190      $actionbar = new \mod_data\output\action_bar($data->id, $url);
 191      echo $actionbar->get_presets_action_bar();
 192      echo $OUTPUT->heading(get_string('presets', 'data'), 2, 'mb-4');
 193      $presets = new \mod_data\output\presets($data->id, $presets, new \moodle_url('/mod/data/field.php'), true);
 194      echo $renderer->render_presets($presets);
 195  }
 196  
 197  echo $OUTPUT->footer();