[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/grade/grading/form/guide/ -> edit_form.php (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1