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]

   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 data_portfolio_caller;
  20  use mod_data\manager;
  21  use moodle_url;
  22  use portfolio_add_button;
  23  use templatable;
  24  use renderable;
  25  
  26  /**
  27   * Renderable class for the action bar elements in the view pages in the database activity.
  28   *
  29   * @package    mod_data
  30   * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class view_action_bar implements templatable, renderable {
  34  
  35      /** @var int $id The database module id. */
  36      private $id;
  37  
  38      /** @var \url_select $urlselect The URL selector object. */
  39      private $urlselect;
  40  
  41      /** @var bool $hasentries Whether entries exist. */
  42      private $hasentries;
  43  
  44      /** @var bool $mode The current view mode (list, view...). */
  45      private $mode;
  46  
  47      /**
  48       * The class constructor.
  49       *
  50       * @param int $id The database module id.
  51       * @param \url_select $urlselect The URL selector object.
  52       * @param bool $hasentries Whether entries exist.
  53       * @param string $mode The current view mode (list, view...).
  54       */
  55      public function __construct(int $id, \url_select $urlselect, bool $hasentries, string $mode) {
  56          $this->id = $id;
  57          $this->urlselect = $urlselect;
  58          $this->hasentries = $hasentries;
  59          $this->mode = $mode;
  60      }
  61  
  62      /**
  63       * Export the data for the mustache template.
  64       *
  65       * @param \renderer_base $output The renderer to be used to render the action bar elements.
  66       * @return array
  67       */
  68      public function export_for_template(\renderer_base $output): array {
  69          global $PAGE, $DB, $CFG;
  70  
  71          $data = [
  72              'urlselect' => $this->urlselect->export_for_template($output),
  73          ];
  74  
  75          $activity = $DB->get_record('data', ['id' => $this->id], '*', MUST_EXIST);
  76          $manager = manager::create_from_instance($activity);
  77  
  78          $actionsselect = null;
  79          // Import entries.
  80          if (has_capability('mod/data:manageentries', $manager->get_context())) {
  81              $actionsselect = new \action_menu();
  82              $actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-secondary');
  83  
  84              $importentrieslink = new moodle_url('/mod/data/import.php', ['d' => $this->id, 'backto' => $PAGE->url->out(false)]);
  85              $actionsselect->add(new \action_menu_link(
  86                  $importentrieslink,
  87                  null,
  88                  get_string('importentries', 'mod_data'),
  89                  false
  90              ));
  91          }
  92  
  93          // Export entries.
  94          if (has_capability(DATA_CAP_EXPORT, $manager->get_context()) && $this->hasentries) {
  95              if (!$actionsselect) {
  96                  $actionsselect = new \action_menu();
  97                  $actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-secondary');
  98              }
  99              $exportentrieslink = new moodle_url('/mod/data/export.php', ['d' => $this->id, 'backto' => $PAGE->url->out(false)]);
 100              $actionsselect->add(new \action_menu_link(
 101                  $exportentrieslink,
 102                  null,
 103                  get_string('exportentries', 'mod_data'),
 104                  false
 105              ));
 106          }
 107  
 108          // Export to portfolio. This is for exporting all records, not just the ones in the search.
 109          if ($this->mode == '' && !empty($CFG->enableportfolios) && $this->hasentries) {
 110              if ($manager->can_export_entries()) {
 111                  // Add the portfolio export button.
 112                  require_once($CFG->libdir . '/portfoliolib.php');
 113  
 114                  $cm = $manager->get_coursemodule();
 115  
 116                  $button = new portfolio_add_button();
 117                  $button->set_callback_options(
 118                      'data_portfolio_caller',
 119                      ['id' => $cm->id],
 120                      'mod_data'
 121                  );
 122                  if (data_portfolio_caller::has_files($activity)) {
 123                      // No plain HTML.
 124                      $button->set_formats([PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_LEAP2A]);
 125                  }
 126                  $exporturl = $button->to_html(PORTFOLIO_ADD_MOODLE_URL);
 127                  if (!is_null($exporturl)) {
 128                      if (!$actionsselect) {
 129                          $actionsselect = new \action_menu();
 130                          $actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-secondary');
 131                      }
 132                      $actionsselect->add(new \action_menu_link(
 133                          $exporturl,
 134                          null,
 135                          get_string('addtoportfolio', 'portfolio'),
 136                          false
 137                      ));
 138                  }
 139              }
 140          }
 141  
 142          if ($actionsselect) {
 143              $data['actionsselect'] = $actionsselect->export_for_template($output);
 144          }
 145  
 146          return $data;
 147      }
 148  }