Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [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  use mod_data\local\importer\preset_importer;
  32  use mod_data\local\importer\preset_upload_importer;
  33  use mod_data\manager;
  34  use mod_data\preset;
  35  use mod_data\output\action_bar;
  36  use mod_data\output\preset_preview;
  37  
  38  require_once('../../config.php');
  39  require_once($CFG->dirroot.'/mod/data/lib.php');
  40  require_once($CFG->dirroot.'/mod/data/preset_form.php');
  41  
  42  // The course module id.
  43  $id = optional_param('id', 0, PARAM_INT);
  44  
  45  $manager = null;
  46  if ($id) {
  47      list($course, $cm) = get_course_and_cm_from_cmid($id, manager::MODULE);
  48      $manager = manager::create_from_coursemodule($cm);
  49      $data = $manager->get_instance();
  50  } else {
  51      // We must have the database activity id.
  52      $d = required_param('d', PARAM_INT);
  53      $data = $DB->get_record('data', ['id' => $d], '*', MUST_EXIST);
  54      $manager = manager::create_from_instance($data);
  55      $cm = $manager->get_coursemodule();
  56      $course = get_course($cm->course);
  57  }
  58  
  59  $action = optional_param('action', 'view', PARAM_ALPHA); // The page action.
  60  $allowedactions = ['view', 'importzip', 'finishimport',
  61      'export', 'preview'];
  62  if (!in_array($action, $allowedactions)) {
  63      throw new moodle_exception('invalidaccess');
  64  }
  65  
  66  $context = $manager->get_context();
  67  
  68  require_login($course, false, $cm);
  69  require_capability('mod/data:managetemplates', $context);
  70  
  71  $url = new moodle_url('/mod/data/preset.php', array('d' => $data->id));
  72  
  73  $PAGE->add_body_class('mediumwidth');
  74  $PAGE->set_url($url);
  75  $titleparts = [
  76      get_string('presets', 'data'),
  77      format_string($cm->name),
  78      format_string($course->fullname),
  79  ];
  80  $PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $titleparts));
  81  $PAGE->set_heading($course->fullname);
  82  $PAGE->force_settings_menu(true);
  83  $PAGE->activityheader->disable();
  84  $PAGE->requires->js_call_amd('mod_data/deletepreset', 'init');
  85  
  86  // fill in missing properties needed for updating of instance
  87  $data->course     = $cm->course;
  88  $data->cmidnumber = $cm->idnumber;
  89  $data->instance   = $cm->instance;
  90  
  91  $renderer = $manager->get_renderer();
  92  $presets = $manager->get_available_presets();
  93  
  94  if ($action === 'export') {
  95      if (headers_sent()) {
  96          throw new \moodle_exception('headersent');
  97      }
  98  
  99      // Check if we should export a given preset or the current one.
 100      $presetname = optional_param('presetname', $data->name, PARAM_FILE);
 101  
 102      $preset = preset::create_from_instance($manager, $presetname);
 103      $exportfile = $preset->export();
 104      $exportfilename = basename($exportfile);
 105      header("Content-Type: application/download\n");
 106      header("Content-Disposition: attachment; filename=\"$exportfilename\"");
 107      header('Expires: 0');
 108      header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
 109      header('Pragma: public');
 110  
 111      // If this file was requested from a form, then mark download as complete.
 112      \core_form\util::form_download_complete();
 113  
 114      $exportfilehandler = fopen($exportfile, 'rb');
 115      print fread($exportfilehandler, filesize($exportfile));
 116      fclose($exportfilehandler);
 117      unlink($exportfile);
 118      exit(0);
 119  }
 120  
 121  
 122  if ($action == 'importzip') {
 123      $filepath = optional_param('filepath', '', PARAM_PATH);
 124      $importer = new preset_upload_importer($manager, $CFG->tempdir . $filepath);
 125      if ($importer->needs_mapping()) {
 126          echo $OUTPUT->header();
 127          echo $OUTPUT->heading(get_string('fieldmappings', 'data'), 2, 'mb-4');
 128          echo $renderer->importing_preset($data, $importer);
 129          echo $OUTPUT->footer();
 130          exit(0);
 131      }
 132      $importer->import(false);
 133      core\notification::success(get_string('importsuccess', 'mod_data'));
 134      redirect(new moodle_url('/mod/data/field.php', ['id' => $cm->id]));
 135      exit(0);
 136  }
 137  
 138  // Preset preview injects CSS and JS to the page and should be done before the page header.
 139  if ($action === 'preview') {
 140      $fullname = optional_param('fullname', '', PARAM_PATH); // The directory the preset is in.
 141      $templatename = optional_param('template', 'listtemplate', PARAM_ALPHA);
 142      // Find out preset owner userid and shortname.
 143      $preset = preset::create_from_fullname($manager, $fullname);
 144      // Validate if the user can view this preset.
 145      if (!$manager->can_view_preset($preset)) {
 146          throw new \moodle_exception('cannotaccesspresentsother', manager::PLUGINNAME);
 147      }
 148      $preview = new preset_preview($manager, $preset, $templatename);
 149      $preview->prepare_page($PAGE);
 150      $url->params([
 151          'fullname' => $fullname,
 152          'template' => $templatename,
 153      ]);
 154      $PAGE->set_url($url);
 155      $titleparts = [
 156          get_string('preview', 'data', $preset->name),
 157          format_string($cm->name),
 158          format_string($course->fullname),
 159      ];
 160      $PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $titleparts));
 161      // Print the preview screen.
 162      echo $OUTPUT->header();
 163      $actionbar = new action_bar($data->id, $url);
 164      echo $actionbar->get_presets_preview_action_bar($manager, $fullname, $templatename);
 165      echo $renderer->render($preview);
 166      echo $OUTPUT->footer();
 167      exit(0);
 168  }
 169  
 170  if ($action === 'finishimport') {
 171      if (!confirm_sesskey()) {
 172          throw new moodle_exception('invalidsesskey');
 173      }
 174      $overwritesettings = optional_param('overwritesettings', false, PARAM_BOOL);
 175      $importer = preset_importer::create_from_parameters($manager);
 176      $importer->finish_import_process($overwritesettings, $data);
 177  }
 178  
 179  echo $OUTPUT->header();
 180  
 181  $actionbar = new \mod_data\output\action_bar($data->id, $url);
 182  echo $actionbar->get_presets_action_bar();
 183  $presets = new \mod_data\output\presets($manager, $presets, new \moodle_url('/mod/data/field.php'), true);
 184  echo $renderer->render_presets($presets);
 185  
 186  echo $OUTPUT->footer();