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.
   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_survey\output;
  18  
  19  use moodle_url;
  20  use renderable;
  21  use renderer_base;
  22  use templatable;
  23  use url_select;
  24  
  25  /**
  26   * Output the rendered elements for the tertiary nav page action
  27   *
  28   * @package mod_survey
  29   * @copyright 2021 Sujith Haridasan <sujith@moodle.com>
  30   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class actionbar implements renderable, templatable {
  33      /**
  34       * The course id.
  35       *
  36       * @var int $id
  37       */
  38      private $id;
  39  
  40      /**
  41       * The action decides the url to navigate to.
  42       *
  43       * @var string $action
  44       */
  45      private $action;
  46  
  47      /**
  48       * Current url.
  49       *
  50       * @var moodle_url $currenturl
  51       */
  52      private $currenturl;
  53  
  54      /**
  55       * actionbar constructor.
  56       *
  57       * @param int $id The course module id.
  58       * @param string $action The action string.
  59       * @param moodle_url $currenturl The current URL.
  60       */
  61      public function __construct(int $id, string $action, moodle_url $currenturl) {
  62          $this->id = $id;
  63          $this->action = $action;
  64          $this->currenturl = $currenturl;
  65      }
  66  
  67      /**
  68       * Create select menu for the reports
  69       *
  70       * @return url_select url_select object.
  71       */
  72      private function create_select_menu(): url_select {
  73          $menu = [];
  74          $actions = $this->get_available_reports();
  75  
  76          foreach ($actions as $action => $straction) {
  77              $url = new moodle_url($this->currenturl, ['id' => $this->id, 'action' => $action]);
  78              $menu[$url->out(false)] = $straction;
  79          }
  80          return new url_select($menu, $this->currenturl->out(false), null, 'surveyresponseselect');
  81      }
  82  
  83      /**
  84       * Generate available reports list
  85       *
  86       * @return array The list of available action => action string.
  87       */
  88      private function get_available_reports(): array {
  89          global $DB;
  90  
  91          $cm = get_coursemodule_from_id('survey', $this->id);
  92          $survey = $DB->get_record("survey", ["id" => $cm->instance]);
  93  
  94          $actions = [];
  95          if ($survey && ($survey->template != SURVEY_CIQ)) {
  96              $actions['summary'] = get_string('summary', 'survey');
  97              $actions['scales'] = get_string('scales', 'survey');
  98          }
  99          $actions['questions'] = get_string('questions', 'survey');
 100          $actions['students'] = get_string('participants');
 101  
 102          return $actions;
 103      }
 104  
 105      /**
 106       * Data for the template.
 107       *
 108       * @param renderer_base $output renderer_base object.
 109       * @return array data for the template
 110       */
 111      public function export_for_template(renderer_base $output): array {
 112          global $PAGE;
 113  
 114          $selecturl = $this->create_select_menu();
 115          $data = [
 116              'urlselect' => $selecturl->export_for_template($output)
 117          ];
 118  
 119          if (has_capability('mod/survey:download', $PAGE->cm->context)) {
 120              $downloadlink = (new moodle_url('/mod/survey/report.php', ['id' => $this->id, 'action' => 'download']))->out(false);
 121              $data['download'] = [
 122                  'link' => $downloadlink,
 123                  'text' => get_string('downloadresults', 'mod_survey'),
 124              ];
 125          }
 126          return $data;
 127      }
 128  }