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 310 and 401] [Versions 311 and 401] [Versions 39 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  /**
  18   * The form used at the rubric editor page is defined here
  19   *
  20   * @package    gradingform_rubric
  21   * @copyright  2011 Marina Glancy <marina@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->dirroot.'/lib/formslib.php');
  28  require_once (__DIR__.'/rubriceditor.php');
  29  MoodleQuickForm::registerElementType('rubriceditor', $CFG->dirroot.'/grade/grading/form/rubric/rubriceditor.php', 'MoodleQuickForm_rubriceditor');
  30  
  31  /**
  32   * Defines the rubric edit form
  33   *
  34   * @package    gradingform_rubric
  35   * @copyright  2011 Marina Glancy <marina@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class gradingform_rubric_editrubric extends moodleform {
  39  
  40      /**
  41       * Form element definition
  42       */
  43      public function definition() {
  44          $form = $this->_form;
  45  
  46          $form->addElement('hidden', 'areaid');
  47          $form->setType('areaid', PARAM_INT);
  48  
  49          $form->addElement('hidden', 'returnurl');
  50          $form->setType('returnurl', PARAM_LOCALURL);
  51  
  52          // name
  53          $form->addElement('text', 'name', get_string('name', 'gradingform_rubric'), array('size' => 52, 'aria-required' => 'true'));
  54          $form->addRule('name', get_string('required'), 'required', null, 'client');
  55          $form->setType('name', PARAM_TEXT);
  56  
  57          // description
  58          $options = gradingform_rubric_controller::description_form_field_options($this->_customdata['context']);
  59          $form->addElement('editor', 'description_editor', get_string('description', 'gradingform_rubric'), null, $options);
  60          $form->setType('description_editor', PARAM_RAW);
  61  
  62          // rubric completion status
  63          $choices = array();
  64          $choices[gradingform_controller::DEFINITION_STATUS_DRAFT]    = html_writer::tag('span', get_string('statusdraft', 'core_grading'), array('class' => 'status draft'));
  65          $choices[gradingform_controller::DEFINITION_STATUS_READY]    = html_writer::tag('span', get_string('statusready', 'core_grading'), array('class' => 'status ready'));
  66          $form->addElement('select', 'status', get_string('rubricstatus', 'gradingform_rubric'), $choices)->freeze();
  67  
  68          // rubric editor
  69          $form->addElement('rubriceditor', 'rubric', get_string('rubric', 'gradingform_rubric'));
  70          $form->setType('rubric', PARAM_RAW);
  71  
  72          $buttonarray = array();
  73          $buttonarray[] = &$form->createElement('submit', 'saverubric', get_string('saverubric', 'gradingform_rubric'));
  74          if ($this->_customdata['allowdraft']) {
  75              $buttonarray[] = &$form->createElement('submit', 'saverubricdraft', get_string('saverubricdraft', 'gradingform_rubric'));
  76          }
  77          $editbutton = &$form->createElement('submit', 'editrubric', ' ');
  78          $editbutton->freeze();
  79          $buttonarray[] = &$editbutton;
  80          $buttonarray[] = &$form->createElement('cancel');
  81          $form->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  82          $form->closeHeaderBefore('buttonar');
  83      }
  84  
  85      /**
  86       * Setup the form depending on current values. This method is called after definition(),
  87       * data submission and set_data().
  88       * All form setup that is dependent on form values should go in here.
  89       *
  90       * We remove the element status if there is no current status (i.e. rubric is only being created)
  91       * so the users do not get confused
  92       */
  93      public function definition_after_data() {
  94          $form = $this->_form;
  95          $el = $form->getElement('status');
  96          if (!$el->getValue()) {
  97              $form->removeElement('status');
  98          } else {
  99              $vals = array_values($el->getValue());
 100              if ($vals[0] == gradingform_controller::DEFINITION_STATUS_READY) {
 101                  $this->findButton('saverubric')->setValue(get_string('save', 'gradingform_rubric'));
 102              }
 103          }
 104      }
 105  
 106      /**
 107       * Form vlidation.
 108       * If there are errors return array of errors ("fieldname"=>"error message"),
 109       * otherwise true if ok.
 110       *
 111       * @param array $data array of ("fieldname"=>value) of submitted data
 112       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 113       * @return array of "element_name"=>"error_description" if there are errors,
 114       *               or an empty array if everything is OK (true allowed for backwards compatibility too).
 115       */
 116      public function validation($data, $files) {
 117          $err = parent::validation($data, $files);
 118          $err = array();
 119          $form = $this->_form;
 120          $rubricel = $form->getElement('rubric');
 121          if ($rubricel->non_js_button_pressed($data['rubric'])) {
 122              // if JS is disabled and button such as 'Add criterion' is pressed - prevent from submit
 123              $err['rubricdummy'] = 1;
 124          } else if (isset($data['editrubric'])) {
 125              // continue editing
 126              $err['rubricdummy'] = 1;
 127          } else if (isset($data['saverubric']) && $data['saverubric']) {
 128              // If user attempts to make rubric active - it needs to be validated
 129              if ($rubricel->validate($data['rubric']) !== false) {
 130                  $err['rubricdummy'] = 1;
 131              }
 132          }
 133          return $err;
 134      }
 135  
 136      /**
 137       * Return submitted data if properly submitted or returns NULL if validation fails or
 138       * if there is no submitted data.
 139       *
 140       * @return object submitted data; NULL if not valid or not submitted or cancelled
 141       */
 142      public function get_data() {
 143          $data = parent::get_data();
 144          if (!empty($data->saverubric)) {
 145              $data->status = gradingform_controller::DEFINITION_STATUS_READY;
 146          } else if (!empty($data->saverubricdraft)) {
 147              $data->status = gradingform_controller::DEFINITION_STATUS_DRAFT;
 148          }
 149          return $data;
 150      }
 151  
 152      /**
 153       * Check if there are changes in the rubric and it is needed to ask user whether to
 154       * mark the current grades for re-grading. User may confirm re-grading and continue,
 155       * return to editing or cancel the changes
 156       *
 157       * @param gradingform_rubric_controller $controller
 158       */
 159      public function need_confirm_regrading($controller) {
 160          $data = $this->get_data();
 161          if (isset($data->rubric['regrade'])) {
 162              // we have already displayed the confirmation on the previous step
 163              return false;
 164          }
 165          if (!isset($data->saverubric) || !$data->saverubric) {
 166              // we only need confirmation when button 'Save rubric' is pressed
 167              return false;
 168          }
 169          if (!$controller->has_active_instances()) {
 170              // nothing to re-grade, confirmation not needed
 171              return false;
 172          }
 173          $changelevel = $controller->update_or_check_rubric($data);
 174          if ($changelevel == 0) {
 175              // no changes in the rubric, no confirmation needed
 176              return false;
 177          }
 178  
 179          // freeze form elements and pass the values in hidden fields
 180          // TODO MDL-29421 description_editor does not freeze the normal way, uncomment below when fixed
 181          $form = $this->_form;
 182          foreach (array('rubric', 'name'/*, 'description_editor'*/) as $fieldname) {
 183              $el =& $form->getElement($fieldname);
 184              $el->freeze();
 185              $el->setPersistantFreeze(true);
 186              if ($fieldname == 'rubric') {
 187                  $el->add_regrade_confirmation($changelevel);
 188              }
 189          }
 190  
 191          // replace button text 'saverubric' and unfreeze 'Back to edit' button
 192          $this->findButton('saverubric')->setValue(get_string('continue'));
 193          $el =& $this->findButton('editrubric');
 194          $el->setValue(get_string('backtoediting', 'gradingform_rubric'));
 195          $el->unfreeze();
 196  
 197          return true;
 198      }
 199  
 200      /**
 201       * Returns a form element (submit button) with the name $elementname
 202       *
 203       * @param string $elementname
 204       * @return HTML_QuickForm_element
 205       */
 206      protected function &findButton($elementname) {
 207          $form = $this->_form;
 208          $buttonar =& $form->getElement('buttonar');
 209          $elements =& $buttonar->getElements();
 210          foreach ($elements as $el) {
 211              if ($el->getName() == $elementname) {
 212                  return $el;
 213              }
 214          }
 215          return null;
 216      }
 217  }