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 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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          $userfieldsapi = \core_user\fields::for_name();
  66          $namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
  67          $presetuser = $DB->get_record('user', array('id' => $preset->userid), 'id, ' . $namefields, MUST_EXIST);
  68          $preset->description = $preset->name.' ('.fullname($presetuser, true).')';
  69      } else {
  70          $preset->userid = 0;
  71          $preset->description = $preset->name;
  72          if (data_user_can_delete_preset($context, $preset) && $preset->name != 'Image gallery') {
  73              $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
  74              $delicon = $OUTPUT->pix_icon('t/delete', $strdelete . ' ' . $preset->description);
  75              $preset->description .= html_writer::link($delurl, $delicon);
  76          }
  77      }
  78      if ($preset->userid > 0 && data_user_can_delete_preset($context, $preset)) {
  79          $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
  80          $delicon = $OUTPUT->pix_icon('t/delete', $strdelete . ' ' . $preset->description);
  81          $preset->description .= html_writer::link($delurl, $delicon);
  82      }
  83  }
  84  // This is required because its currently bound to the last element in the array.
  85  // If someone were to inadvently use it again and this call were not here
  86  unset($preset);
  87  
  88  $form_importexisting = new data_existing_preset_form(null, array('presets'=>$presets));
  89  $form_importexisting->set_data(array('d' => $data->id));
  90  
  91  $form_importzip = new data_import_preset_zip_form();
  92  $form_importzip->set_data(array('d' => $data->id));
  93  
  94  $form_export = new data_export_form();
  95  $form_export->set_data(array('d' => $data->id));
  96  
  97  $form_save = new data_save_preset_form();
  98  $form_save->set_data(array('d' => $data->id, 'name'=>$data->name));
  99  
 100  /* Output */
 101  if (!$form_export->is_submitted()) {
 102      echo $OUTPUT->header();
 103      echo $OUTPUT->heading(format_string($data->name), 2);
 104  
 105      // Render the activity information.
 106      $cminfo = cm_info::create($cm);
 107      $completiondetails = \core_completion\cm_completion_details::get_instance($cminfo, $USER->id);
 108      $activitydates = \core\activity_dates::get_dates_for_module($cminfo, $USER->id);
 109      echo $OUTPUT->activity_information($cminfo, $completiondetails, $activitydates);
 110  
 111      // Needed for tabs.php
 112      $currenttab = 'presets';
 113      $currentgroup = groups_get_activity_group($cm);
 114      $groupmode = groups_get_activity_groupmode($cm);
 115      echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
 116  
 117      include ('tabs.php');
 118  }
 119  
 120  if (optional_param('sesskey', false, PARAM_BOOL) && confirm_sesskey()) {
 121  
 122      $renderer = $PAGE->get_renderer('mod_data');
 123  
 124      if ($formdata = $form_importexisting->get_data()) {
 125          $importer = new data_preset_existing_importer($course, $cm, $data, $formdata->fullname);
 126          echo $renderer->import_setting_mappings($data, $importer);
 127          echo $OUTPUT->footer();
 128          exit(0);
 129      } else if ($formdata = $form_importzip->get_data()) {
 130          $file = new stdClass;
 131          $file->name = $form_importzip->get_new_filename('importfile');
 132          $file->path = $form_importzip->save_temp_file('importfile');
 133          $importer = new data_preset_upload_importer($course, $cm, $data, $file->path);
 134          echo $renderer->import_setting_mappings($data, $importer);
 135          echo $OUTPUT->footer();
 136          exit(0);
 137      } else if ($formdata = $form_export->get_data()) {
 138  
 139          if (headers_sent()) {
 140              print_error('headersent');
 141          }
 142  
 143          $exportfile = data_presets_export($course, $cm, $data);
 144          $exportfilename = basename($exportfile);
 145          header("Content-Type: application/download\n");
 146          header("Content-Disposition: attachment; filename=\"$exportfilename\"");
 147          header('Expires: 0');
 148          header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
 149          header('Pragma: public');
 150  
 151          // If this file was requested from a form, then mark download as complete.
 152          \core_form\util::form_download_complete();
 153  
 154          $exportfilehandler = fopen($exportfile, 'rb');
 155          print fread($exportfilehandler, filesize($exportfile));
 156          fclose($exportfilehandler);
 157          unlink($exportfile);
 158          exit(0);
 159  
 160      } else if ($formdata = $form_save->get_data()) {
 161          if (!empty($formdata->overwrite)) {
 162              $selectedpreset = new stdClass();
 163              foreach ($presets as $preset) {
 164                  if ($preset->name == $formdata->name) {
 165                      $selectedpreset = $preset;
 166                      break;
 167                  }
 168              }
 169              if (isset($selectedpreset->name)) {
 170                  if (data_user_can_delete_preset($context, $selectedpreset)) {
 171                      data_delete_site_preset($formdata->name);
 172                  } else {
 173                      print_error('cannotoverwritepreset', 'data');
 174                  }
 175              }
 176          }
 177  
 178          // If the preset exists now then we need to throw an error.
 179          $sitepresets = data_get_available_site_presets($context);
 180          foreach ($sitepresets as $key=>$preset) {
 181              if ($formdata->name == $preset->name) {
 182                  print_error('errorpresetexists', 'preset');
 183              }
 184          }
 185  
 186          // Save the preset now
 187          data_presets_save($course, $cm, $data, $formdata->name);
 188  
 189          echo $OUTPUT->notification(get_string('savesuccess', 'data'), 'notifysuccess');
 190          echo $OUTPUT->continue_button($PAGE->url);
 191          echo $OUTPUT->footer();
 192          exit(0);
 193      } else {
 194          $action = optional_param('action', null, PARAM_ALPHANUM);
 195          $fullname = optional_param('fullname', '', PARAM_PATH); // directory the preset is in
 196          //
 197          // find out preset owner userid and shortname
 198          $parts = explode('/', $fullname, 2);
 199          $userid = empty($parts[0]) ? 0 : (int)$parts[0];
 200          $shortname = empty($parts[1]) ? '' : $parts[1];
 201  
 202          if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
 203              print_error('cannotaccesspresentsother', 'data');
 204          }
 205  
 206          if ($action == 'confirmdelete') {
 207              $path = data_preset_path($course, $userid, $shortname);
 208              $strwarning = get_string('deletewarning', 'data').'<br />'.$shortname;
 209              $optionsyes = array('fullname' => $userid.'/'.$shortname,
 210                               'action' => 'delete',
 211                               'd' => $data->id);
 212              $optionsno = array('d' => $data->id);
 213              echo $OUTPUT->confirm($strwarning, new moodle_url('preset.php', $optionsyes), new moodle_url('preset.php', $optionsno));
 214              echo $OUTPUT->footer();
 215              exit(0);
 216          } else if ($action == 'delete') {
 217              $selectedpreset = new stdClass();
 218              foreach ($presets as $preset) {
 219                  if ($preset->shortname == $shortname) {
 220                      $selectedpreset = $preset;
 221                  }
 222              }
 223              if (!isset($selectedpreset->shortname) || !data_user_can_delete_preset($context, $selectedpreset)) {
 224                 print_error('invalidrequest');
 225              }
 226  
 227              data_delete_site_preset($shortname);
 228  
 229              $strdeleted = get_string('deleted', 'data');
 230              echo $OUTPUT->notification("$shortname $strdeleted", 'notifysuccess');
 231          } else if ($action == 'finishimport') {
 232              $overwritesettings = optional_param('overwritesettings', false, PARAM_BOOL);
 233              if (!$fullname) {
 234                  $presetdir = $CFG->tempdir.'/forms/'.required_param('directory', PARAM_FILE);
 235                  if (!file_exists($presetdir) || !is_dir($presetdir)) {
 236                      print_error('cannotimport');
 237                  }
 238                  $importer = new data_preset_upload_importer($course, $cm, $data, $presetdir);
 239              } else {
 240                  $importer = new data_preset_existing_importer($course, $cm, $data, $fullname);
 241              }
 242              $importer->import($overwritesettings);
 243              $strimportsuccess = get_string('importsuccess', 'data');
 244              $straddentries = get_string('addentries', 'data');
 245              $strtodatabase = get_string('todatabase', 'data');
 246              if (!$DB->get_records('data_records', array('dataid'=>$data->id))) {
 247                  echo $OUTPUT->notification("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
 248              } else {
 249                  echo $OUTPUT->notification("$strimportsuccess", 'notifysuccess');
 250              }
 251          }
 252          echo $OUTPUT->continue_button($PAGE->url);
 253          echo $OUTPUT->footer();
 254          exit(0);
 255      }
 256  }
 257  
 258  // Export forms
 259  echo $OUTPUT->heading(get_string('export', 'data'), 3);
 260  $form_export->display();
 261  $form_save->display();
 262  
 263  // Import forms
 264  echo $OUTPUT->heading(get_string('import'), 3);
 265  $form_importzip->display();
 266  $form_importexisting->display();
 267  
 268  echo $OUTPUT->footer();