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 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  namespace mod_data\output;
  18  
  19  use mod_data\manager;
  20  use mod_data\preset;
  21  use moodle_url;
  22  use url_select;
  23  
  24  /**
  25   * Class responsible for generating the action bar elements in the database module pages.
  26   *
  27   * @package    mod_data
  28   * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class action_bar {
  32  
  33      /** @var int $id The database module id. */
  34      private $id;
  35  
  36      /** @var int $cmid The database course module id. */
  37      private $cmid;
  38  
  39      /** @var moodle_url $currenturl The URL of the current page. */
  40      private $currenturl;
  41  
  42      /**
  43       * The class constructor.
  44       *
  45       * @param int $id The database module id.
  46       * @param moodle_url $pageurl The URL of the current page.
  47       */
  48      public function __construct(int $id, moodle_url $pageurl) {
  49          $this->id = $id;
  50          [$course, $cm] = get_course_and_cm_from_instance($this->id, 'data');
  51          $this->cmid = $cm->id;
  52          $this->currenturl = $pageurl;
  53      }
  54  
  55      /**
  56       * Generate the output for the action bar in the field page.
  57       *
  58       * @param bool $hasfieldselect Whether the field selector element should be rendered.
  59       * @param null $unused1 This parameter has been deprecated since 4.1 and should not be used anymore.
  60       * @param null $unused2 This parameter has been deprecated since 4.1 and should not be used anymore.
  61       * @return string The HTML code for the action bar.
  62       */
  63      public function get_fields_action_bar(
  64          bool $hasfieldselect = false,
  65          ?bool $unused1 = null,
  66          ?bool $unused2 = null
  67      ): string {
  68          global $PAGE;
  69  
  70          if ($unused1 !== null || $unused2 !== null) {
  71              debugging('Deprecated argument passed to get_fields_action_bar method', DEBUG_DEVELOPER);
  72          }
  73  
  74          $fieldselect = null;
  75          if ($hasfieldselect) {
  76              $fieldselect = $this->get_create_fields();
  77          }
  78  
  79          $renderer = $PAGE->get_renderer('mod_data');
  80          $fieldsactionbar = new fields_action_bar($this->id, null, null, null, null, $fieldselect);
  81  
  82          return $renderer->render_fields_action_bar($fieldsactionbar);
  83      }
  84  
  85      /**
  86       * Generate the output for the action bar in the field mappings page.
  87       *
  88       * @return string The HTML code for the action bar.
  89       */
  90      public function get_fields_mapping_action_bar(): string {
  91          global $PAGE;
  92  
  93          $renderer = $PAGE->get_renderer('mod_data');
  94          $fieldsactionbar = new fields_mappings_action_bar($this->id);
  95  
  96          $data = $fieldsactionbar->export_for_template($renderer);
  97          return $renderer->render_from_template('mod_data/fields_action_bar', $data);
  98      }
  99  
 100      /**
 101       * Generate the output for the create a new field action menu.
 102       *
 103       * @return \action_menu Action menu to create a new field
 104       */
 105      public function get_create_fields(): \action_menu {
 106          // Get the list of possible fields (plugins).
 107          $plugins = \core_component::get_plugin_list('datafield');
 108          $menufield = [];
 109          foreach ($plugins as $plugin => $fulldir) {
 110              $menufield[$plugin] = get_string('pluginname', "datafield_{$plugin}");
 111          }
 112          asort($menufield);
 113  
 114          $fieldselect = new \action_menu();
 115          $fieldselect->set_menu_trigger(get_string('newfield', 'mod_data'), 'btn btn-secondary');
 116          $fieldselectparams = ['d' => $this->id, 'mode' => 'new'];
 117          foreach ($menufield as $fieldtype => $fieldname) {
 118              $fieldselectparams['newtype'] = $fieldtype;
 119              $fieldselect->add(new \action_menu_link(
 120                  new moodle_url('/mod/data/field.php', $fieldselectparams),
 121                  new \pix_icon('field/' . $fieldtype, $fieldname, 'data'),
 122                  $fieldname,
 123                  false
 124              ));
 125          }
 126          $fieldselect->set_additional_classes('singlebutton');
 127  
 128          return $fieldselect;
 129      }
 130  
 131      /**
 132       * Generate the output for the action selector in the view page.
 133       *
 134       * @param bool $hasentries Whether entries exist.
 135       * @param string $mode The current view mode (list, view...).
 136       * @return string The HTML code for the action selector.
 137       */
 138      public function get_view_action_bar(bool $hasentries, string $mode): string {
 139          global $PAGE;
 140  
 141          $viewlistlink = new moodle_url('/mod/data/view.php', ['d' => $this->id]);
 142          $viewsinglelink = new moodle_url('/mod/data/view.php', ['d' => $this->id, 'mode' => 'single']);
 143  
 144          $menu = [
 145              $viewlistlink->out(false) => get_string('listview', 'mod_data'),
 146              $viewsinglelink->out(false) => get_string('singleview', 'mod_data'),
 147          ];
 148  
 149          $activeurl = $this->currenturl;
 150  
 151          if ($this->currenturl->get_param('rid') || $this->currenturl->get_param('mode') == 'single') {
 152              $activeurl = $viewsinglelink;
 153          }
 154  
 155          $urlselect = new url_select($menu, $activeurl->out(false), null, 'viewactionselect');
 156          $urlselect->set_label(get_string('viewnavigation', 'mod_data'), ['class' => 'sr-only']);
 157          $renderer = $PAGE->get_renderer('mod_data');
 158          $viewactionbar = new view_action_bar($this->id, $urlselect, $hasentries, $mode);
 159  
 160          return $renderer->render_view_action_bar($viewactionbar);
 161      }
 162  
 163      /**
 164       * Generate the output for the action selector in the templates page.
 165       *
 166       * @return string The HTML code for the action selector.
 167       */
 168      public function get_templates_action_bar(): string {
 169          global $PAGE;
 170  
 171          $listtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id,
 172              'mode' => 'listtemplate']);
 173          $singletemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id,
 174              'mode' => 'singletemplate']);
 175          $advancedsearchtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id,
 176              'mode' => 'asearchtemplate']);
 177          $addtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'addtemplate']);
 178          $rsstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'rsstemplate']);
 179          $csstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'csstemplate']);
 180          $jstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'jstemplate']);
 181  
 182          $menu = [
 183              $addtemplatelink->out(false) => get_string('addtemplate', 'mod_data'),
 184              $singletemplatelink->out(false) => get_string('singletemplate', 'mod_data'),
 185              $listtemplatelink->out(false) => get_string('listtemplate', 'mod_data'),
 186              $advancedsearchtemplatelink->out(false) => get_string('asearchtemplate', 'mod_data'),
 187              $csstemplatelink->out(false) => get_string('csstemplate', 'mod_data'),
 188              $jstemplatelink->out(false) => get_string('jstemplate', 'mod_data'),
 189              $rsstemplatelink->out(false) => get_string('rsstemplate', 'mod_data'),
 190          ];
 191  
 192          $selectmenu = new \core\output\select_menu('presetsactions', $menu, $this->currenturl->out(false));
 193          $selectmenu->set_label(get_string('templatesnavigation', 'mod_data'), ['class' => 'sr-only']);
 194  
 195          $renderer = $PAGE->get_renderer('mod_data');
 196  
 197          $presetsactions = $this->get_presets_actions_select(false);
 198  
 199          // Reset all templates action.
 200          $resetallurl = new moodle_url($this->currenturl);
 201          $resetallurl->params([
 202              'action' => 'resetalltemplates',
 203              'sesskey' => sesskey(),
 204          ]);
 205          $presetsactions->add(new \action_menu_link(
 206              $resetallurl,
 207              null,
 208              get_string('resetalltemplates', 'mod_data'),
 209              false,
 210              ['data-action' => 'resetalltemplates', 'data-dataid' => $this->id]
 211          ));
 212  
 213          $templatesactionbar = new templates_action_bar($this->id, $selectmenu, null, null, $presetsactions);
 214  
 215          return $renderer->render_templates_action_bar($templatesactionbar);
 216      }
 217  
 218      /**
 219       * Generate the output for the action selector in the presets page.
 220       *
 221       * @return string The HTML code for the action selector.
 222       */
 223      public function get_presets_action_bar(): string {
 224          global $PAGE;
 225  
 226          $renderer = $PAGE->get_renderer('mod_data');
 227          $presetsactionbar = new presets_action_bar($this->cmid, $this->get_presets_actions_select(true));
 228  
 229          return $renderer->render_presets_action_bar($presetsactionbar);
 230      }
 231  
 232      /**
 233       * Generate the output for the action selector in the presets preview page.
 234       *
 235       * @param manager $manager the manager instance
 236       * @param string $fullname the preset fullname
 237       * @param string $current the current template name
 238       * @return string The HTML code for the action selector
 239       */
 240      public function get_presets_preview_action_bar(manager $manager, string $fullname, string $current): string {
 241          global $PAGE;
 242  
 243          $renderer = $PAGE->get_renderer(manager::PLUGINNAME);
 244  
 245          $cm = $manager->get_coursemodule();
 246  
 247          $menu = [];
 248          $selected = null;
 249          foreach (['listtemplate', 'singletemplate'] as $templatename) {
 250              $link = new moodle_url('/mod/data/preset.php', [
 251                  'd' => $this->id,
 252                  'template' => $templatename,
 253                  'fullname' => $fullname,
 254                  'action' => 'preview',
 255              ]);
 256              $menu[$link->out(false)] = get_string($templatename, manager::PLUGINNAME);
 257              if (!$selected || $templatename == $current) {
 258                  $selected = $link->out(false);
 259              }
 260          }
 261          $urlselect = new url_select($menu, $selected, null);
 262          $urlselect->set_label(get_string('templatesnavigation', manager::PLUGINNAME), ['class' => 'sr-only']);
 263  
 264          $data = [
 265              'title' => get_string('preview', manager::PLUGINNAME, preset::get_name_from_plugin($fullname)),
 266              'hasback' => true,
 267              'backtitle' => get_string('back'),
 268              'backurl' => new moodle_url('/mod/data/preset.php', ['id' => $cm->id]),
 269              'extraurlselect' => $urlselect->export_for_template($renderer),
 270          ];
 271          return $renderer->render_from_template('mod_data/action_bar', $data);
 272      }
 273  
 274      /**
 275       * Helper method to get the selector for the presets action.
 276       *
 277       * @param bool $hasimport Whether the Import buttons must be included or not.
 278       * @return \action_menu|null The selector object used to display the presets actions. Null when the import button is not
 279       * displayed and the database hasn't any fields.
 280       */
 281      protected function get_presets_actions_select(bool $hasimport = false): ?\action_menu {
 282          global $DB;
 283  
 284          $hasfields = $DB->record_exists('data_fields', ['dataid' => $this->id]);
 285  
 286          // Early return if the database has no fields and the import action won't be displayed.
 287          if (!$hasfields && !$hasimport) {
 288              return null;
 289          }
 290  
 291          $actionsselect = new \action_menu();
 292          $actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-secondary');
 293  
 294          if ($hasimport) {
 295              // Import.
 296              $actionsselectparams = ['id' => $this->cmid];
 297              $actionsselect->add(new \action_menu_link(
 298                  new moodle_url('/mod/data/preset.php', $actionsselectparams),
 299                  null,
 300                  get_string('importpreset', 'mod_data'),
 301                  false,
 302                  ['data-action' => 'importpresets', 'data-dataid' => $this->cmid]
 303              ));
 304          }
 305  
 306          // If the database has no fields, export and save as preset options shouldn't be displayed.
 307          if ($hasfields) {
 308              // Export.
 309              $actionsselectparams = ['id' => $this->cmid, 'action' => 'export'];
 310              $actionsselect->add(new \action_menu_link(
 311                  new moodle_url('/mod/data/preset.php', $actionsselectparams),
 312                  null,
 313                  get_string('exportpreset', 'mod_data'),
 314                  false
 315              ));
 316              // Save as preset.
 317              $actionsselect->add(new \action_menu_link(
 318                  new moodle_url('/mod/data/preset.php', $actionsselectparams),
 319                  null,
 320                  get_string('saveaspreset', 'mod_data'),
 321                  false,
 322                  ['data-action' => 'saveaspreset', 'data-dataid' => $this->id]
 323              ));
 324          }
 325  
 326          return $actionsselect;
 327      }
 328  }