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_feedback\form;
  18  
  19  use core_form\dynamic_form;
  20  use moodle_url;
  21  use context;
  22  use context_module;
  23  
  24  /**
  25   * Prints the confirm use template form
  26   *
  27   * @copyright 2021 Peter Dias
  28   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  29   * @package mod_feedback
  30   */
  31  class use_template_form extends dynamic_form {
  32      /**
  33       * Define the form
  34       */
  35      public function definition() {
  36          $mform =& $this->_form;
  37  
  38          $mform->addElement('static', 'generalheader', '', get_string("whatfor", 'feedback'));
  39          $mform->addElement('radio', 'deleteolditems', '', get_string('delete_old_items', 'feedback'), 1);
  40          $mform->addElement('radio', 'deleteolditems', '', get_string('append_new_items', 'feedback'), 0);
  41          $mform->setType('deleteolditems', PARAM_INT);
  42          $mform->setDefault('deleteolditems', 1);
  43  
  44          $mform->addElement('hidden', 'id');
  45          $mform->setType('id', PARAM_INT);
  46          $mform->addElement('hidden', 'templateid');
  47          $mform->setType('templateid', PARAM_INT);
  48      }
  49  
  50      /**
  51       * Returns context where this form is used
  52       *
  53       * @return context
  54       */
  55      protected function get_context_for_dynamic_submission(): context {
  56          $id = $this->optional_param('id', null, PARAM_INT);
  57          list($course, $cm) = get_course_and_cm_from_cmid($id, 'feedback');
  58          return context_module::instance($cm->id);
  59      }
  60  
  61      /**
  62       * Checks if current user has access to this form, otherwise throws exception
  63       *
  64       * @throws \moodle_exception User does not have capability to access the form
  65       */
  66      protected function check_access_for_dynamic_submission(): void {
  67          if (!has_capability('mod/feedback:edititems', $this->get_context_for_dynamic_submission())) {
  68              throw new \moodle_exception('nocapabilitytousethisservice');
  69          }
  70      }
  71  
  72      /**
  73       * Process the form submission, used if form was submitted via AJAX
  74       *
  75       * @return array Returns the following information
  76       *               - the template was successfully created/updated from the provided template
  77       *               - the redirect url.
  78       */
  79      public function process_dynamic_submission(): array {
  80          global $PAGE;
  81          $formdata = $this->get_data();
  82          $templateid = $this->optional_param('templateid', null, PARAM_INT);
  83          $id = $this->optional_param('id', null, PARAM_INT);
  84          $response = feedback_items_from_template($PAGE->activityrecord, $templateid, $formdata->deleteolditems);
  85          $url = new moodle_url('/mod/feedback/edit.php', ['id' => $id]);
  86  
  87          if ($response !== false) {
  88              // Provide a notification on success as the user will be redirected.
  89              \core\notification::add(get_string('feedbackupdated', 'feedback'), \core\notification::SUCCESS);
  90          }
  91  
  92          return [
  93              'result' => $response !== false,
  94              'url' => $url->out()
  95          ];
  96      }
  97  
  98      /**
  99       * Load in existing data as form defaults
 100       */
 101      public function set_data_for_dynamic_submission(): void {
 102          $this->set_data((object)[
 103              'id' => $this->optional_param('id', null, PARAM_INT),
 104              'templateid' => $this->optional_param('templateid', null, PARAM_INT)
 105          ]);
 106      }
 107  
 108      /**
 109       * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
 110       *
 111       * @return moodle_url
 112       */
 113      protected function get_page_url_for_dynamic_submission(): moodle_url {
 114          $params = [
 115              'id' => $this->optional_param('id', null, PARAM_INT),
 116              'templateid' => $this->optional_param('templateid', null, PARAM_INT)
 117          ];
 118          return new moodle_url('/mod/feedback/use_templ.php', $params);
 119      }
 120  }