Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Database activity renderer.
  19   *
  20   * @copyright 2010 Sam Hemelryk
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package mod_data
  23   */
  24  
  25  use mod_data\local\importer\preset_existing_importer;
  26  use mod_data\manager;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  class mod_data_renderer extends plugin_renderer_base {
  31  
  32      /**
  33       * Rendering setting and mapping page to import a preset.
  34       *
  35       * @param stdClass $datamodule  Database module to import to.
  36       * @param data_preset_importer $importer Importer instance to use for the importing.
  37       * @return string
  38       * @deprecated since Moodle 4.1 MDL-75140 - please do not use this class any more.
  39       * @todo MDL-75189 Final deprecation in Moodle 4.5.
  40       */
  41      public function import_setting_mappings($datamodule, data_preset_importer $importer) {
  42          debugging('import_setting_mappings is deprecated. Please use importing_preset instead', DEBUG_DEVELOPER);
  43  
  44          $manager = \mod_data\manager::create_from_coursemodule($datamodule);
  45          $fullname = $importer->get_directory();
  46          return $this->importing_preset($datamodule, new preset_existing_importer($manager, $fullname));
  47      }
  48  
  49      /**
  50       * Importing a preset on a database module.
  51       *
  52       * @param stdClass $datamodule  Database module to import to.
  53       * @param \mod_data\local\importer\preset_importer $importer Importer instance to use for the importing.
  54       *
  55       * @return string
  56       */
  57      public function importing_preset(stdClass $datamodule, \mod_data\local\importer\preset_importer $importer): string {
  58  
  59          $strwarning = get_string('mappingwarning', 'data');
  60  
  61          $params = $importer->settings;
  62          $newfields = $params->importfields;
  63          $currentfields = $params->currentfields;
  64  
  65          $html = html_writer::start_tag('div', ['class' => 'presetmapping']);
  66          $html .= html_writer::start_tag('form', ['method' => 'post', 'action' => '']);
  67          $html .= html_writer::start_tag('div');
  68          $html .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'action', 'value' => 'finishimport']);
  69          $html .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
  70          $html .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'd', 'value' => $datamodule->id]);
  71  
  72          $inputselector = $importer->get_preset_selector();
  73          $html .= html_writer::empty_tag(
  74                  'input',
  75                  ['type' => 'hidden', 'name' => $inputselector['name'], 'value' => $inputselector['value']]
  76          );
  77  
  78          if (!empty($newfields)) {
  79              $table = new html_table();
  80              $table->data = array();
  81  
  82              foreach ($newfields as $nid => $newfield) {
  83                  $row = array();
  84                  $row[0] = html_writer::tag('label', $newfield->name, array('for'=>'id_'.$newfield->name));
  85                  $attrs = array('name' => 'field_' . $nid, 'id' => 'id_' . $newfield->name, 'class' => 'custom-select');
  86                  $row[1] = html_writer::start_tag('select', $attrs);
  87  
  88                  $selected = false;
  89                  foreach ($currentfields as $cid => $currentfield) {
  90                      if ($currentfield->type != $newfield->type) {
  91                          continue;
  92                      }
  93                      if ($currentfield->name == $newfield->name) {
  94                          $row[1] .= html_writer::tag(
  95                              'option',
  96                              get_string('mapexistingfield', 'data', $currentfield->name),
  97                              ['value' => $cid, 'selected' => 'selected']
  98                          );
  99                          $selected = true;
 100                      } else {
 101                          $row[1] .= html_writer::tag(
 102                              'option',
 103                              get_string('mapexistingfield', 'data', $currentfield->name),
 104                              ['value' => $cid]
 105                          );
 106                      }
 107                  }
 108  
 109                  if ($selected) {
 110                      $row[1] .= html_writer::tag('option', get_string('mapnewfield', 'data'), array('value'=>'-1'));
 111                  } else {
 112                      $row[1] .= html_writer::tag('option', get_string('mapnewfield', 'data'), array('value'=>'-1', 'selected'=>'selected'));
 113                  }
 114  
 115                  $row[1] .= html_writer::end_tag('select');
 116                  $table->data[] = $row;
 117              }
 118              $html .= html_writer::table($table);
 119              $html .= html_writer::tag('p', $strwarning);
 120          } else {
 121              $html .= $this->output->notification(get_string('nodefinedfields', 'data'));
 122          }
 123  
 124          $html .= html_writer::start_tag('div', array('class'=>'overwritesettings'));
 125          $attrs = ['type' => 'checkbox', 'name' => 'overwritesettings', 'id' => 'overwritesettings', 'class' => 'mr-2'];
 126          $html .= html_writer::empty_tag('input', $attrs);
 127          $html .= html_writer::tag('label', get_string('overwritesettings', 'data'), ['for' => 'overwritesettings']);
 128          $html .= html_writer::end_tag('div');
 129  
 130          $actionbuttons = html_writer::start_div();
 131          $cancelurl = new moodle_url('/mod/data/field.php', ['d' => $datamodule->id]);
 132          $actionbuttons .= html_writer::tag('a', get_string('cancel') , [
 133              'href' => $cancelurl->out(false),
 134              'class' => 'btn btn-secondary mr-2',
 135              'role' => 'button',
 136          ]);
 137          $actionbuttons .= html_writer::empty_tag('input', [
 138              'type' => 'submit',
 139              'class' => 'btn btn-primary',
 140              'value' => get_string('continue'),
 141          ]);
 142          $actionbuttons .= html_writer::end_div();
 143  
 144          $stickyfooter = new core\output\sticky_footer($actionbuttons);
 145          $html .= $this->render($stickyfooter);
 146  
 147          $html .= html_writer::end_tag('div');
 148          $html .= html_writer::end_tag('form');
 149          $html .= html_writer::end_tag('div');
 150  
 151          return $html;
 152      }
 153  
 154      /**
 155       * Renders the action bar for the field page.
 156       *
 157       * @param \mod_data\output\fields_action_bar $actionbar
 158       * @return string The HTML output
 159       */
 160      public function render_fields_action_bar(\mod_data\output\fields_action_bar $actionbar): string {
 161          $data = $actionbar->export_for_template($this);
 162          $data['title'] = get_string('nofields', 'mod_data');
 163          $data['intro'] = get_string('createfields', 'mod_data');
 164          $data['noitemsimgurl'] = $this->output->image_url('fields_zero_state', 'mod_data')->out();
 165          return $this->render_from_template('mod_data/fields_action_bar', $data);
 166      }
 167  
 168      /**
 169       * Renders the fields page footer.
 170       *
 171       * @param manager $manager the instance manager
 172       * @return string The HTML output
 173       */
 174      public function render_fields_footer(manager $manager): string {
 175          $cm = $manager->get_coursemodule();
 176          $pageurl = new moodle_url('/mod/data/templates.php', ['id' => $cm->id]);
 177          return $this->render_from_template('mod_data/fields_footer', [
 178              'pageurl' => $pageurl->out(false),
 179          ]);
 180      }
 181  
 182      /**
 183       * Renders the action bar for the view page.
 184       *
 185       * @param \mod_data\output\view_action_bar $actionbar
 186       * @return string The HTML output
 187       */
 188      public function render_view_action_bar(\mod_data\output\view_action_bar $actionbar): string {
 189          $data = $actionbar->export_for_template($this);
 190          return $this->render_from_template('mod_data/view_action_bar', $data);
 191      }
 192  
 193      /**
 194       * Renders the action bar for the template page.
 195       *
 196       * @param \mod_data\output\templates_action_bar $actionbar
 197       * @return string The HTML output
 198       */
 199      public function render_templates_action_bar(\mod_data\output\templates_action_bar $actionbar): string {
 200          $data = $actionbar->export_for_template($this);
 201          return $this->render_from_template('mod_data/templates_action_bar', $data);
 202      }
 203  
 204      /**
 205       * Renders the action bar for the preset page.
 206       *
 207       * @param \mod_data\output\presets_action_bar $actionbar
 208       * @return string The HTML output
 209       */
 210      public function render_presets_action_bar(\mod_data\output\presets_action_bar $actionbar): string {
 211          $data = $actionbar->export_for_template($this);
 212          return $this->render_from_template('mod_data/presets_action_bar', $data);
 213      }
 214  
 215      /**
 216       * Renders the presets table in the preset page.
 217       *
 218       * @param \mod_data\output\presets $presets
 219       * @return string The HTML output
 220       */
 221      public function render_presets(\mod_data\output\presets $presets): string {
 222          $data = $presets->export_for_template($this);
 223          return $this->render_from_template('mod_data/presets', $data);
 224      }
 225  
 226      /**
 227       * Renders the default template.
 228       *
 229       * @param \mod_data\output\defaulttemplate $template
 230       * @return string The HTML output
 231       */
 232      public function render_defaulttemplate(\mod_data\output\defaulttemplate $template): string {
 233          $data = $template->export_for_template($this);
 234          return $this->render_from_template($template->get_templatename(), $data);
 235      }
 236  
 237      /**
 238       * Renders the action bar for the zero state (no fields created) page.
 239       *
 240       * @param \mod_data\manager $manager The manager instance.
 241       *
 242       * @return string The HTML output
 243       */
 244      public function render_database_zero_state(\mod_data\manager $manager): string {
 245          $actionbar = new \mod_data\output\zero_state_action_bar($manager);
 246          $data = $actionbar->export_for_template($this);
 247          if (empty($data)) {
 248              // No actions for the user.
 249              $data['title'] = get_string('activitynotready');
 250              $data['intro'] = get_string('comebacklater');
 251              $data['noitemsimgurl'] = $this->output->image_url('noentries_zero_state', 'mod_data')->out();
 252          } else {
 253              $data['title'] = get_string('startbuilding', 'mod_data');
 254              $data['intro'] = get_string('createactivity', 'mod_data');
 255              $data['noitemsimgurl'] = $this->output->image_url('view_zero_state', 'mod_data')->out();
 256          }
 257  
 258          return $this->render_from_template('mod_data/zero_state', $data);
 259      }
 260  
 261      /**
 262       * Renders the action bar for an empty database view page.
 263       *
 264       * @param \mod_data\manager $manager The manager instance.
 265       *
 266       * @return string The HTML output
 267       */
 268      public function render_empty_database(\mod_data\manager $manager): string {
 269          $actionbar = new \mod_data\output\empty_database_action_bar($manager);
 270          $data = $actionbar->export_for_template($this);
 271          $data['noitemsimgurl'] = $this->output->image_url('view_zero_state', 'mod_data')->out();
 272  
 273          return $this->render_from_template('mod_data/view_noentries', $data);
 274      }
 275  
 276      /**
 277       * Renders the action bar for the zero state (no fields created) page.
 278       *
 279       * @param \mod_data\manager $manager The manager instance.
 280       *
 281       * @return string The HTML output
 282       */
 283      public function render_fields_zero_state(\mod_data\manager $manager): string {
 284          $data = [
 285              'noitemsimgurl' => $this->output->image_url('fields_zero_state', 'mod_data')->out(),
 286              'title' => get_string('nofields', 'mod_data'),
 287              'intro' => get_string('createfields', 'mod_data'),
 288              ];
 289          if ($manager->can_manage_templates()) {
 290              $actionbar = new \mod_data\output\action_bar($manager->get_instance()->id, $this->page->url);
 291              $createfieldbutton = $actionbar->get_create_fields();
 292              $data['createfieldbutton'] = $createfieldbutton->export_for_template($this);
 293          }
 294  
 295          return $this->render_from_template('mod_data/zero_state', $data);
 296      }
 297  
 298      /**
 299       * Renders the action bar for the templates zero state (no fields created) page.
 300       *
 301       * @param \mod_data\manager $manager The manager instance.
 302       *
 303       * @return string The HTML output
 304       */
 305      public function render_templates_zero_state(\mod_data\manager $manager): string {
 306          $actionbar = new \mod_data\output\zero_state_action_bar($manager);
 307          $data = $actionbar->export_for_template($this);
 308          $data['title'] = get_string('notemplates', 'mod_data');
 309          $data['intro'] = get_string('createtemplates', 'mod_data');
 310          $data['noitemsimgurl'] = $this->output->image_url('templates_zero_state', 'mod_data')->out();
 311  
 312          return $this->render_from_template('mod_data/zero_state', $data);
 313      }
 314  }