[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/grade/export/ -> grade_export_form.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  if (!defined('MOODLE_INTERNAL')) {
  19      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  20  }
  21  
  22  require_once $CFG->libdir.'/formslib.php';
  23  
  24  class grade_export_form extends moodleform {
  25      function definition() {
  26          global $CFG, $COURSE, $USER, $DB;
  27  
  28          $isdeprecatedui = false;
  29  
  30          $mform =& $this->_form;
  31          if (isset($this->_customdata)) {  // hardcoding plugin names here is hacky
  32              $features = $this->_customdata;
  33          } else {
  34              $features = array();
  35          }
  36  
  37          if (empty($features['simpleui'])) {
  38              debugging('Grade export plugin needs updating to support one step exports.', DEBUG_DEVELOPER);
  39          }
  40  
  41          $mform->addElement('header', 'gradeitems', get_string('gradeitemsinc', 'grades'));
  42          $mform->setExpanded('gradeitems', true);
  43  
  44          if (!empty($features['idnumberrequired'])) {
  45              $mform->addElement('static', 'idnumberwarning', get_string('useridnumberwarning', 'grades'));
  46          }
  47  
  48          $switch = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
  49  
  50          // Grab the grade_seq for this course
  51          $gseq = new grade_seq($COURSE->id, $switch);
  52  
  53          if ($grade_items = $gseq->items) {
  54              $needs_multiselect = false;
  55              $canviewhidden = has_capability('moodle/grade:viewhidden', context_course::instance($COURSE->id));
  56  
  57              foreach ($grade_items as $grade_item) {
  58                  // Is the grade_item hidden? If so, can the user see hidden grade_items?
  59                  if ($grade_item->is_hidden() && !$canviewhidden) {
  60                      continue;
  61                  }
  62  
  63                  if (!empty($features['idnumberrequired']) and empty($grade_item->idnumber)) {
  64                      $mform->addElement('checkbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), get_string('noidnumber', 'grades'));
  65                      $mform->hardFreeze('itemids['.$grade_item->id.']');
  66                  } else {
  67                      $mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), null, array('group' => 1));
  68                      $mform->setDefault('itemids['.$grade_item->id.']', 1);
  69                      $needs_multiselect = true;
  70                  }
  71              }
  72  
  73              if ($needs_multiselect) {
  74                  $this->add_checkbox_controller(1, null, null, 1); // 1st argument is group name, 2nd is link text, 3rd is attributes and 4th is original value
  75              }
  76          }
  77  
  78  
  79          $mform->addElement('header', 'options', get_string('exportformatoptions', 'grades'));
  80          if (!empty($features['simpleui'])) {
  81              $mform->setExpanded('options', false);
  82          }
  83  
  84          $mform->addElement('advcheckbox', 'export_feedback', get_string('exportfeedback', 'grades'));
  85          $mform->setDefault('export_feedback', 0);
  86          $coursecontext = context_course::instance($COURSE->id);
  87          if (has_capability('moodle/course:viewsuspendedusers', $coursecontext)) {
  88              $mform->addElement('advcheckbox', 'export_onlyactive', get_string('exportonlyactive', 'grades'));
  89              $mform->setType('export_onlyactive', PARAM_BOOL);
  90              $mform->setDefault('export_onlyactive', 1);
  91              $mform->addHelpButton('export_onlyactive', 'exportonlyactive', 'grades');
  92          } else {
  93              $mform->addElement('hidden', 'export_onlyactive', 1);
  94              $mform->setType('export_onlyactive', PARAM_BOOL);
  95              $mform->setConstant('export_onlyactive', 1);
  96          }
  97  
  98          if (empty($features['simpleui'])) {
  99              $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
 100              $mform->addElement('select', 'previewrows', get_string('previewrows', 'grades'), $options);
 101          }
 102  
 103  
 104  
 105          if (!empty($features['updategradesonly'])) {
 106              $mform->addElement('advcheckbox', 'updatedgradesonly', get_string('updatedgradesonly', 'grades'));
 107          }
 108          /// selections for decimal points and format, MDL-11667, defaults to site settings, if set
 109          //$default_gradedisplaytype = $CFG->grade_export_displaytype;
 110          $options = array(GRADE_DISPLAY_TYPE_REAL       => get_string('real', 'grades'),
 111                           GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
 112                           GRADE_DISPLAY_TYPE_LETTER     => get_string('letter', 'grades'));
 113  
 114          /*
 115          foreach ($options as $key=>$option) {
 116              if ($key == $default_gradedisplaytype) {
 117                  $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
 118                  break;
 119              }
 120          }
 121          */
 122          if ($features['multipledisplaytypes']) {
 123              /*
 124               * Using advcheckbox because we need the grade display type (name) as key and grade display type (constant) as value.
 125               * The method format_column_name requires the lang file string and the format_grade method requires the constant.
 126               */
 127              $checkboxes = array();
 128              $checkboxes[] = $mform->createElement('advcheckbox', 'display[real]', null, get_string('real', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_REAL));
 129              $checkboxes[] = $mform->createElement('advcheckbox', 'display[percentage]', null, get_string('percentage', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_PERCENTAGE));
 130              $checkboxes[] = $mform->createElement('advcheckbox', 'display[letter]', null, get_string('letter', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_LETTER));
 131              $mform->addGroup($checkboxes, 'displaytypes', get_string('gradeexportdisplaytypes', 'grades'), ' ', false);
 132              $mform->setDefault('display[real]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_REAL);
 133              $mform->setDefault('display[percentage]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE);
 134              $mform->setDefault('display[letter]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_LETTER);
 135          } else {
 136              // Only used by XML grade export format.
 137              $mform->addElement('select', 'display', get_string('gradeexportdisplaytype', 'grades'), $options);
 138              $mform->setDefault('display', $CFG->grade_export_displaytype);
 139          }
 140  
 141          //$default_gradedecimals = $CFG->grade_export_decimalpoints;
 142          $options = array(0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
 143          $mform->addElement('select', 'decimals', get_string('gradeexportdecimalpoints', 'grades'), $options);
 144          $mform->setDefault('decimals', $CFG->grade_export_decimalpoints);
 145          $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
 146          /*
 147          if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
 148              $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
 149          }
 150          */
 151  
 152          if (!empty($features['includeseparator'])) {
 153              $radio = array();
 154              $radio[] = $mform->createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
 155              $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
 156              $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcolon', 'grades'), 'colon');
 157              $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepsemicolon', 'grades'), 'semicolon');
 158              $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
 159              $mform->setDefault('separator', 'comma');
 160          }
 161  
 162          if (!empty($CFG->gradepublishing) and !empty($features['publishing'])) {
 163              $mform->addElement('header', 'publishing', get_string('publishingoptions', 'grades'));
 164              if (!empty($features['simpleui'])) {
 165                  $mform->setExpanded('publishing', false);
 166              }
 167              $options = array(get_string('nopublish', 'grades'), get_string('createnewkey', 'userkey'));
 168              $keys = $DB->get_records_select('user_private_key', "script='grade/export' AND instance=? AND userid=?",
 169                              array($COURSE->id, $USER->id));
 170              if ($keys) {
 171                  foreach ($keys as $key) {
 172                      $options[$key->value] = $key->value; // TODO: add more details - ip restriction, valid until ??
 173                  }
 174              }
 175              $mform->addElement('select', 'key', get_string('userkey', 'userkey'), $options);
 176              $mform->addHelpButton('key', 'userkey', 'userkey');
 177              $mform->addElement('static', 'keymanagerlink', get_string('keymanager', 'userkey'),
 178                      '<a href="'.$CFG->wwwroot.'/grade/export/keymanager.php?id='.$COURSE->id.'">'.get_string('keymanager', 'userkey').'</a>');
 179  
 180              $mform->addElement('text', 'iprestriction', get_string('keyiprestriction', 'userkey'), array('size'=>80));
 181              $mform->addHelpButton('iprestriction', 'keyiprestriction', 'userkey');
 182              $mform->setDefault('iprestriction', getremoteaddr()); // own IP - just in case somebody does not know what user key is
 183              $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
 184  
 185              $mform->addElement('date_time_selector', 'validuntil', get_string('keyvaliduntil', 'userkey'), array('optional'=>true));
 186              $mform->addHelpButton('validuntil', 'keyvaliduntil', 'userkey');
 187              $mform->setDefault('validuntil', time()+3600*24*7); // only 1 week default duration - just in case somebody does not know what user key is
 188              $mform->setType('validuntil', PARAM_INT);
 189  
 190              $mform->disabledIf('iprestriction', 'key', 'noteq', 1);
 191              $mform->disabledIf('validuntil', 'key', 'noteq', 1);
 192          }
 193  
 194          $mform->addElement('hidden', 'id', $COURSE->id);
 195          $mform->setType('id', PARAM_INT);
 196          $submitstring = get_string('download');
 197          if (empty($features['simpleui'])) {
 198              $submitstring = get_string('submit');
 199          } else if (!empty($CFG->gradepublishing)) {
 200              $submitstring = get_string('export', 'grades');
 201          }
 202  
 203          $this->add_action_buttons(false, $submitstring);
 204      }
 205  
 206      /**
 207       * Overrides the mform get_data method.
 208       *
 209       * Created to force a value since the validation method does not work with multiple checkbox.
 210       *
 211       * @return stdClass form data object.
 212       */
 213      public function get_data() {
 214          global $CFG;
 215          $data = parent::get_data();
 216          if ($data && $this->_customdata['multipledisplaytypes']) {
 217              if (count(array_filter($data->display)) == 0) {
 218                  // Ensure that a value was selected as the export plugins expect at least one value.
 219                  if ($CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_LETTER) {
 220                      $data->display['letter'] = GRADE_DISPLAY_TYPE_LETTER;
 221                  } else if ($CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE) {
 222                      $data->display['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
 223                  } else {
 224                      $data->display['real'] = GRADE_DISPLAY_TYPE_REAL;
 225                  }
 226              }
 227          }
 228          return $data;
 229      }
 230  }
 231  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1