Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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 moodle_url;
  20  
  21  /**
  22   * Class responsible for generating the action bar elements in the database module pages.
  23   *
  24   * @package    mod_data
  25   * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class action_bar {
  29  
  30      /** @var int $id The database module id. */
  31      private $id;
  32  
  33      /** @var moodle_url $currenturl The URL of the current page. */
  34      private $currenturl;
  35  
  36      /**
  37       * The class constructor.
  38       *
  39       * @param int $id The database module id.
  40       * @param moodle_url $pageurl The URL of the current page.
  41       */
  42      public function __construct(int $id, moodle_url $pageurl) {
  43          $this->id = $id;
  44          $this->currenturl = $pageurl;
  45      }
  46  
  47      /**
  48       * Generate the output for the action bar in the field page.
  49       *
  50       * @param bool $hasfieldselect Whether the field selector element should be rendered.
  51       * @param bool $hassaveaspreset Whether the save as preset button element should be rendered.
  52       * @param bool $hasexportpreset Whether the export as preset button element should be rendered.
  53       * @return string The HTML code for the action bar.
  54       */
  55      public function get_fields_action_bar(bool $hasfieldselect = false, bool $hassaveaspreset = false,
  56              bool $hasexportpreset = false): string {
  57          global $PAGE, $DB;
  58  
  59          $createfieldlink = new moodle_url('/mod/data/field.php', ['d' => $this->id]);
  60          $importlink = new moodle_url('/mod/data/field.php', ['d' => $this->id, 'mode' => 'import']);
  61          $presetslink = new moodle_url('/mod/data/field.php', ['d' => $this->id, 'mode' => 'usepreset']);
  62  
  63          $menu = [
  64              $createfieldlink->out(false) => get_string('managefields', 'mod_data'),
  65              $importlink->out(false) => get_string('importpreset', 'mod_data'),
  66              $presetslink->out(false) => get_string('usestandard', 'mod_data'),
  67          ];
  68  
  69          $selected = $createfieldlink->out(false);
  70          $mode = $this->currenturl->get_param('mode');
  71  
  72          if ($mode == 'import') {
  73              $selected = $importlink->out(false);
  74          } else if ($mode === 'usepreset') {
  75              $selected = $presetslink->out(false);
  76          }
  77  
  78          $urlselect = new \url_select($menu, $selected, null, 'fieldactionselect');
  79          $urlselect->set_label(get_string('fieldsnavigation', 'mod_data'), ['class' => 'sr-only']);
  80  
  81          $fieldselect = null;
  82          if ($hasfieldselect) {
  83              // Get the list of possible fields (plugins).
  84              $plugins = \core_component::get_plugin_list('datafield');
  85              $menufield = [];
  86  
  87              foreach ($plugins as $plugin => $fulldir) {
  88                  $menufield[$plugin] = get_string('pluginname', "datafield_{$plugin}");
  89              }
  90              asort($menufield);
  91  
  92              $fieldselecturl = new moodle_url('/mod/data/field.php', ['d' => $this->id, 'mode' => 'new']);
  93              $fieldselect = new \single_select($fieldselecturl, 'newtype', $menufield, null, get_string('newfield', 'data'),
  94                  'fieldform');
  95              $fieldselect->set_label(get_string('newfield', 'mod_data'), ['class' => 'sr-only']);
  96          }
  97  
  98          $saveaspresetbutton = null;
  99          $exportpresetbutton = null;
 100          $hasfields = $DB->record_exists('data_fields', ['dataid' => $this->id]);
 101  
 102          if ($hasfields) {
 103              if ($hassaveaspreset) {
 104                  $saveaspresetlink = new moodle_url('/mod/data/preset.php',
 105                      ['d' => $this->id, 'action' => 'export']);
 106                  $saveaspresetbutton = new \single_button($saveaspresetlink,
 107                      get_string('saveaspreset', 'mod_data'), 'post', false);
 108              }
 109  
 110              if ($hasexportpreset) {
 111                  $exportpresetlink = new moodle_url('/mod/data/preset.php',
 112                      ['d' => $this->id, 'action' => 'export']);
 113                  $exportpresetbutton = new \single_button($exportpresetlink,
 114                      get_string('exportpreset', 'mod_data'), 'get', false);
 115              }
 116          }
 117          $renderer = $PAGE->get_renderer('mod_data');
 118          $fieldsactionbar = new fields_action_bar($this->id, $urlselect, $fieldselect, $saveaspresetbutton,
 119              $exportpresetbutton);
 120  
 121          return $renderer->render_fields_action_bar($fieldsactionbar);
 122      }
 123  
 124      /**
 125       * Generate the output for the action selector in the view page.
 126       *
 127       * @param bool $hasentries Whether entries exist.
 128       * @return string The HTML code for the action selector.
 129       */
 130      public function get_view_action_bar(bool $hasentries): string {
 131          global $PAGE;
 132  
 133          $viewlistlink = new moodle_url('/mod/data/view.php', ['d' => $this->id]);
 134          $viewsinglelink = new moodle_url('/mod/data/view.php', ['d' => $this->id, 'mode' => 'single']);
 135  
 136          $menu = [
 137              $viewlistlink->out(false) => get_string('listview', 'mod_data'),
 138              $viewsinglelink->out(false) => get_string('singleview', 'mod_data'),
 139          ];
 140  
 141          $activeurl = $this->currenturl;
 142  
 143          if ($this->currenturl->get_param('rid') || $this->currenturl->get_param('mode') == 'single') {
 144              $activeurl = $viewsinglelink;
 145          }
 146  
 147          $urlselect = new \url_select($menu, $activeurl->out(false), null, 'viewactionselect');
 148          $urlselect->set_label(get_string('viewnavigation', 'mod_data'), ['class' => 'sr-only']);
 149          $renderer = $PAGE->get_renderer('mod_data');
 150          $viewactionbar = new view_action_bar($this->id, $urlselect, $hasentries);
 151  
 152          return $renderer->render_view_action_bar($viewactionbar);
 153      }
 154  
 155      /**
 156       * Generate the output for the action selector in the templates page.
 157       *
 158       * @return string The HTML code for the action selector.
 159       */
 160      public function get_templates_action_bar(): string {
 161          global $PAGE, $DB;
 162  
 163          $listtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id,
 164              'mode' => 'listtemplate']);
 165          $singletemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id,
 166              'mode' => 'singletemplate']);
 167          $advancedsearchtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id,
 168              'mode' => 'asearchtemplate']);
 169          $addtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'addtemplate']);
 170          $rsstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'rsstemplate']);
 171          $csstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'csstemplate']);
 172          $jstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'jstemplate']);
 173  
 174          $menu = [
 175              $listtemplatelink->out(false) => get_string('listtemplate', 'mod_data'),
 176              $singletemplatelink->out(false) => get_string('singletemplate', 'mod_data'),
 177              $advancedsearchtemplatelink->out(false) => get_string('asearchtemplate', 'mod_data'),
 178              $addtemplatelink->out(false) => get_string('addtemplate', 'mod_data'),
 179              $rsstemplatelink->out(false) => get_string('rsstemplate', 'mod_data'),
 180              $csstemplatelink->out(false) => get_string('csstemplate', 'mod_data'),
 181              $jstemplatelink->out(false) => get_string('jstemplate', 'mod_data'),
 182          ];
 183  
 184          $urlselect = new \url_select($menu, $this->currenturl->out(false), null, 'templatesactionselect');
 185          $urlselect->set_label(get_string('templatesnavigation', 'mod_data'), ['class' => 'sr-only']);
 186  
 187          $hasfields = $DB->record_exists('data_fields', ['dataid' => $this->id]);
 188  
 189          $saveaspresetbutton = null;
 190          $exportpresetbutton = null;
 191  
 192          if ($hasfields) {
 193              $saveaspresetlink = new moodle_url('/mod/data/preset.php',
 194                  ['d' => $this->id, 'action' => 'export']);
 195              $saveaspresetbutton = new \single_button($saveaspresetlink,
 196                  get_string('saveaspreset', 'mod_data'), 'post', false);
 197  
 198              $exportpresetlink = new moodle_url('/mod/data/preset.php',
 199                  ['d' => $this->id, 'action' => 'export', 'sesskey' => sesskey()]);
 200              $exportpresetbutton = new \single_button($exportpresetlink,
 201                  get_string('exportpreset', 'mod_data'), 'get', false);
 202          }
 203  
 204          $renderer = $PAGE->get_renderer('mod_data');
 205          $templatesactionbar = new templates_action_bar($this->id, $urlselect, $saveaspresetbutton,
 206              $exportpresetbutton);
 207  
 208          return $renderer->render_templates_action_bar($templatesactionbar);
 209      }
 210  
 211      /**
 212       * Generate the output for the action selector in the presets page.
 213       *
 214       * @return string The HTML code for the action selector.
 215       */
 216      public function get_presets_action_bar(): string {
 217          global $PAGE;
 218  
 219          $renderer = $PAGE->get_renderer('mod_data');
 220          $presetsactionbar = new presets_action_bar($this->id);
 221  
 222          return $renderer->render_presets_action_bar($presetsactionbar);
 223      }
 224  }