Search moodle.org's
Developer Documentation

See Release Notes

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

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

   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 mx-1',
 135              'role' => 'button',
 136          ]);
 137          $actionbuttons .= html_writer::empty_tag('input', [
 138              'type' => 'submit',
 139              'class' => 'btn btn-primary mx-1',
 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          return $this->render_from_template('mod_data/action_bar', $data);
 163      }
 164  
 165      /**
 166       * Renders the fields page footer.
 167       *
 168       * @param manager $manager the instance manager
 169       * @return string The HTML output
 170       */
 171      public function render_fields_footer(manager $manager): string {
 172          $cm = $manager->get_coursemodule();
 173          $pageurl = new moodle_url('/mod/data/templates.php', ['id' => $cm->id]);
 174          return $this->render_from_template('mod_data/fields_footer', [
 175              'pageurl' => $pageurl->out(false),
 176          ]);
 177      }
 178  
 179      /**
 180       * Renders the action bar for the view page.
 181       *
 182       * @param \mod_data\output\view_action_bar $actionbar
 183       * @return string The HTML output
 184       */
 185      public function render_view_action_bar(\mod_data\output\view_action_bar $actionbar): string {
 186          $data = $actionbar->export_for_template($this);
 187          return $this->render_from_template('mod_data/view_action_bar', $data);
 188      }
 189  
 190      /**
 191       * Renders the action bar for the template page.
 192       *
 193       * @param \mod_data\output\templates_action_bar $actionbar
 194       * @return string The HTML output
 195       */
 196      public function render_templates_action_bar(\mod_data\output\templates_action_bar $actionbar): string {
 197          $data = $actionbar->export_for_template($this);
 198          return $this->render_from_template('mod_data/templates_action_bar', $data);
 199      }
 200  
 201      /**
 202       * Renders the action bar for the preset page.
 203       *
 204       * @param \mod_data\output\presets_action_bar $actionbar
 205       * @return string The HTML output
 206       */
 207      public function render_presets_action_bar(\mod_data\output\presets_action_bar $actionbar): string {
 208          $data = $actionbar->export_for_template($this);
 209          return $this->render_from_template('mod_data/presets_action_bar', $data);
 210      }
 211  
 212      /**
 213       * Renders the presets table in the preset page.
 214       *
 215       * @param \mod_data\output\presets $presets
 216       * @return string The HTML output
 217       */
 218      public function render_presets(\mod_data\output\presets $presets): string {
 219          $data = $presets->export_for_template($this);
 220          return $this->render_from_template('mod_data/presets', $data);
 221      }
 222  
 223      /**
 224       * Renders the default template.
 225       *
 226       * @param \mod_data\output\defaulttemplate $template
 227       * @return string The HTML output
 228       */
 229      public function render_defaulttemplate(\mod_data\output\defaulttemplate $template): string {
 230          $data = $template->export_for_template($this);
 231          return $this->render_from_template($template->get_templatename(), $data);
 232      }
 233  
 234      /**
 235       * Renders the action bar for the zero state (no fields created) page.
 236       *
 237       * @param \mod_data\manager $manager The manager instance.
 238       *
 239       * @return string The HTML output
 240       */
 241      public function render_database_zero_state(\mod_data\manager $manager): string {
 242          $actionbar = new \mod_data\output\zero_state_action_bar($manager);
 243          $data = $actionbar->export_for_template($this);
 244          if (empty($data)) {
 245              // No actions for the user.
 246              $data['title'] = get_string('activitynotready');
 247              $data['intro'] = get_string('comebacklater');
 248              $data['noitemsimgurl'] = $this->output->image_url('noentries_zero_state', 'mod_data')->out();
 249          } else {
 250              $data['title'] = get_string('startbuilding', 'mod_data');
 251              $data['intro'] = get_string('createactivity', 'mod_data');
 252              $data['noitemsimgurl'] = $this->output->image_url('view_zero_state', 'mod_data')->out();
 253          }
 254  
 255          return $this->render_from_template('mod_data/zero_state', $data);
 256      }
 257  
 258      /**
 259       * Renders the action bar for an empty database view page.
 260       *
 261       * @param \mod_data\manager $manager The manager instance.
 262       *
 263       * @return string The HTML output
 264       */
 265      public function render_empty_database(\mod_data\manager $manager): string {
 266          $actionbar = new \mod_data\output\empty_database_action_bar($manager);
 267          $data = $actionbar->export_for_template($this);
 268          $data['noitemsimgurl'] = $this->output->image_url('view_zero_state', 'mod_data')->out();
 269  
 270          return $this->render_from_template('mod_data/view_noentries', $data);
 271      }
 272  
 273      /**
 274       * Renders the action bar for the zero state (no fields created) page.
 275       *
 276       * @param \mod_data\manager $manager The manager instance.
 277       *
 278       * @return string The HTML output
 279       */
 280      public function render_fields_zero_state(\mod_data\manager $manager): string {
 281          $data = [
 282              'noitemsimgurl' => $this->output->image_url('fields_zero_state', 'mod_data')->out(),
 283              'title' => get_string('nofields', 'mod_data'),
 284              'intro' => get_string('createfields', 'mod_data'),
 285              ];
 286          if ($manager->can_manage_templates()) {
 287              $actionbar = new \mod_data\output\action_bar($manager->get_instance()->id, $this->page->url);
 288              $createfieldbutton = $actionbar->get_create_fields();
 289              $data['createfieldbutton'] = $createfieldbutton->export_for_template($this);
 290          }
 291  
 292          return $this->render_from_template('mod_data/zero_state', $data);
 293      }
 294  
 295      /**
 296       * Renders the action bar for the templates zero state (no fields created) page.
 297       *
 298       * @param \mod_data\manager $manager The manager instance.
 299       *
 300       * @return string The HTML output
 301       */
 302      public function render_templates_zero_state(\mod_data\manager $manager): string {
 303          $actionbar = new \mod_data\output\zero_state_action_bar($manager);
 304          $data = $actionbar->export_for_template($this);
 305          $data['title'] = get_string('notemplates', 'mod_data');
 306          $data['intro'] = get_string('createtemplates', 'mod_data');
 307          $data['noitemsimgurl'] = $this->output->image_url('templates_zero_state', 'mod_data')->out();
 308  
 309          return $this->render_from_template('mod_data/zero_state', $data);
 310      }
 311  }