Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  /**
  18   * prints the forms to choose an item-typ to create items and to choose a template to use
  19   *
  20   * @author Andreas Grabs
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  22   * @package mod_feedback
  23   */
  24  
  25  //It must be included from a Moodle page
  26  if (!defined('MOODLE_INTERNAL')) {
  27      die('Direct access to this script is forbidden.');
  28  }
  29  
  30  require_once($CFG->libdir.'/formslib.php');
  31  
  32  class feedback_edit_use_template_form extends moodleform {
  33  
  34      /**
  35       * Form definition
  36       */
  37      public function definition() {
  38          $mform =& $this->_form;
  39  
  40          $course = $this->_customdata['course'];
  41  
  42          $elementgroup = array();
  43          //headline
  44          $mform->addElement('header', 'using_templates', get_string('using_templates', 'feedback'));
  45          // hidden elements
  46          $mform->addElement('hidden', 'id');
  47          $mform->setType('id', PARAM_INT);
  48  
  49          // visible elements
  50          $templates_options = array();
  51          $owntemplates = feedback_get_template_list($course, 'own');
  52          $publictemplates = feedback_get_template_list($course, 'public');
  53  
  54          $options = array();
  55          if ($owntemplates or $publictemplates) {
  56              $options[''] = array('' => get_string('choosedots'));
  57  
  58              if ($owntemplates) {
  59                  $courseoptions = array();
  60                  foreach ($owntemplates as $template) {
  61                      $courseoptions[$template->id] = format_string($template->name);
  62                  }
  63                  $options[get_string('course')] = $courseoptions;
  64              }
  65  
  66              if ($publictemplates) {
  67                  $publicoptions = array();
  68                  foreach ($publictemplates as $template) {
  69                      $publicoptions[$template->id] = format_string($template->name);
  70                  }
  71                  $options[get_string('public', 'feedback')] = $publicoptions;
  72              }
  73  
  74              $attributes = 'onChange="M.core_formchangechecker.set_form_submitted(); this.form.submit()"';
  75              $elementgroup[] = $mform->createElement('selectgroups',
  76                                                       'templateid',
  77                                                       get_string('using_templates', 'feedback'),
  78                                                       $options,
  79                                                       $attributes);
  80  
  81              $elementgroup[] = $mform->createElement('submit',
  82                                                       'use_template',
  83                                                       get_string('use_this_template', 'feedback'),
  84                                                       array('class' => 'hiddenifjs'));
  85  
  86              $mform->addGroup($elementgroup, 'elementgroup', '', array(' '), false);
  87          } else {
  88              $mform->addElement('static', 'info', get_string('no_templates_available_yet', 'feedback'));
  89          }
  90  
  91          $this->set_data(array('id' => $this->_customdata['id']));
  92      }
  93  }
  94  
  95  class feedback_edit_create_template_form extends moodleform {
  96  
  97      /**
  98       * Form definition
  99       */
 100      public function definition() {
 101          $mform =& $this->_form;
 102  
 103          // hidden elements
 104          $mform->addElement('hidden', 'id');
 105          $mform->setType('id', PARAM_INT);
 106          $mform->addElement('hidden', 'do_show');
 107          $mform->setType('do_show', PARAM_ALPHANUMEXT);
 108          $mform->setConstant('do_show', 'templates');
 109  
 110          //headline
 111          $mform->addElement('header', 'creating_templates', get_string('creating_templates', 'feedback'));
 112  
 113          // visible elements
 114          $elementgroup = array();
 115  
 116          $elementgroup[] = $mform->createElement('text',
 117                                                   'templatename',
 118                                                   get_string('name', 'feedback'),
 119                                                   array('size'=>'40', 'maxlength'=>'200'));
 120  
 121          if (has_capability('mod/feedback:createpublictemplate', context_system::instance())) {
 122              $elementgroup[] = $mform->createElement('checkbox',
 123                                                       'ispublic', '',
 124                                                       get_string('public', 'feedback'));
 125          }
 126  
 127  
 128          $mform->addGroup($elementgroup,
 129                           'elementgroup',
 130                           get_string('name', 'feedback'),
 131                           array(' '),
 132                           false);
 133  
 134          // Buttons.
 135          $mform->addElement('submit', 'create_template', get_string('save_as_new_template', 'feedback'));
 136  
 137          $mform->setType('templatename', PARAM_TEXT);
 138  
 139          $this->set_data(array('id' => $this->_customdata['id']));
 140      }
 141  
 142      /**
 143       * Form validation
 144       *
 145       * @param array $data array of ("fieldname"=>value) of submitted data
 146       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 147       * @return array of "element_name"=>"error_description" if there are errors,
 148       *         or an empty array if everything is OK (true allowed for backwards compatibility too).
 149       */
 150      public function validation($data, $files) {
 151          $errors = parent::validation($data, $files);
 152          if (!isset($data['templatename']) || trim(strval($data['templatename'])) === '') {
 153              $errors['elementgroup'] = get_string('name_required', 'feedback');
 154          }
 155          return $errors;
 156      }
 157  }
 158