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 401 and 403]

   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          $exportfeedback = isset($CFG->grade_export_exportfeedback) ? $CFG->grade_export_exportfeedback : 0;
  86          $mform->setDefault('export_feedback', $exportfeedback);
  87          $coursecontext = context_course::instance($COURSE->id);
  88          if (has_capability('moodle/course:viewsuspendedusers', $coursecontext)) {
  89              $mform->addElement('advcheckbox', 'export_onlyactive', get_string('exportonlyactive', 'grades'));
  90              $mform->setType('export_onlyactive', PARAM_BOOL);
  91              $mform->setDefault('export_onlyactive', 1);
  92              $mform->addHelpButton('export_onlyactive', 'exportonlyactive', 'grades');
  93          } else {
  94              $mform->addElement('hidden', 'export_onlyactive', 1);
  95              $mform->setType('export_onlyactive', PARAM_BOOL);
  96              $mform->setConstant('export_onlyactive', 1);
  97          }
  98  
  99          if (empty($features['simpleui'])) {
 100              $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
 101              $mform->addElement('select', 'previewrows', get_string('previewrows', 'grades'), $options);
 102          }
 103  
 104  
 105  
 106          if (!empty($features['updategradesonly'])) {
 107              $mform->addElement('advcheckbox', 'updatedgradesonly', get_string('updatedgradesonly', 'grades'));
 108          }
 109          /// selections for decimal points and format, MDL-11667, defaults to site settings, if set
 110          //$default_gradedisplaytype = $CFG->grade_export_displaytype;
 111          $options = array(GRADE_DISPLAY_TYPE_REAL       => get_string('real', 'grades'),
 112                           GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
 113                           GRADE_DISPLAY_TYPE_LETTER     => get_string('letter', 'grades'));
 114  
 115          /*
 116          foreach ($options as $key=>$option) {
 117              if ($key == $default_gradedisplaytype) {
 118                  $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
 119                  break;
 120              }
 121          }
 122          */
 123          if ($features['multipledisplaytypes']) {
 124              /*
 125               * Using advcheckbox because we need the grade display type (name) as key and grade display type (constant) as value.
 126               * The method format_column_name requires the lang file string and the format_grade method requires the constant.
 127               */
 128              $checkboxes = array();
 129              $checkboxes[] = $mform->createElement('advcheckbox', 'display[real]', null, get_string('real', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_REAL));
 130              $checkboxes[] = $mform->createElement('advcheckbox', 'display[percentage]', null, get_string('percentage', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_PERCENTAGE));
 131              $checkboxes[] = $mform->createElement('advcheckbox', 'display[letter]', null, get_string('letter', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_LETTER));
 132              $mform->addGroup($checkboxes, 'displaytypes', get_string('gradeexportdisplaytypes', 'grades'), ' ', false);
 133              $mform->setDefault('display[real]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_REAL);
 134              $mform->setDefault('display[percentage]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE);
 135              $mform->setDefault('display[letter]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_LETTER);
 136          } else {
 137              // Only used by XML grade export format.
 138              $mform->addElement('select', 'display', get_string('gradeexportdisplaytype', 'grades'), $options);
 139              $mform->setDefault('display', $CFG->grade_export_displaytype);
 140          }
 141  
 142          //$default_gradedecimals = $CFG->grade_export_decimalpoints;
 143          $options = array(0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
 144          $mform->addElement('select', 'decimals', get_string('gradeexportdecimalpoints', 'grades'), $options);
 145          $mform->setDefault('decimals', $CFG->grade_export_decimalpoints);
 146          $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
 147          /*
 148          if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
 149              $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
 150          }
 151          */
 152  
 153          if (!empty($features['includeseparator'])) {
 154              $radio = array();
 155              $radio[] = $mform->createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
 156              $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
 157              $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcolon', 'grades'), 'colon');
 158              $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepsemicolon', 'grades'), 'semicolon');
 159              $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
 160              $mform->setDefault('separator', 'comma');
 161          }
 162  
 163          if (!empty($CFG->gradepublishing) and !empty($features['publishing'])) {
 164              $mform->addElement('header', 'publishing', get_string('publishingoptions', 'grades'));
 165              if (!empty($features['simpleui'])) {
 166                  $mform->setExpanded('publishing', false);
 167              }
 168              $options = array(get_string('nopublish', 'grades'), get_string('createnewkey', 'userkey'));
 169              $keys = $DB->get_records_select('user_private_key', "script='grade/export' AND instance=? AND userid=?",
 170                              array($COURSE->id, $USER->id));
 171              if ($keys) {
 172                  foreach ($keys as $key) {
 173                      $options[$key->value] = $key->value; // TODO: add more details - ip restriction, valid until ??
 174                  }
 175              }
 176              $mform->addElement('select', 'key', get_string('userkey', 'userkey'), $options);
 177              $mform->addHelpButton('key', 'userkey', 'userkey');
 178              $mform->addElement('static', 'keymanagerlink', get_string('keymanager', 'userkey'),
 179                      '<a href="'.$CFG->wwwroot.'/grade/export/keymanager.php?id='.$COURSE->id.'">'.get_string('keymanager', 'userkey').'</a>');
 180  
 181              $mform->addElement('text', 'iprestriction', get_string('keyiprestriction', 'userkey'), array('size'=>80));
 182              $mform->addHelpButton('iprestriction', 'keyiprestriction', 'userkey');
 183              $mform->setDefault('iprestriction', getremoteaddr()); // own IP - just in case somebody does not know what user key is
 184              $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
 185  
 186              $mform->addElement('date_time_selector', 'validuntil', get_string('keyvaliduntil', 'userkey'), array('optional'=>true));
 187              $mform->addHelpButton('validuntil', 'keyvaliduntil', 'userkey');
 188              $mform->setDefault('validuntil', time()+3600*24*7); // only 1 week default duration - just in case somebody does not know what user key is
 189              $mform->setType('validuntil', PARAM_INT);
 190  
 191              $mform->disabledIf('iprestriction', 'key', 'noteq', 1);
 192              $mform->disabledIf('validuntil', 'key', 'noteq', 1);
 193          }
 194  
 195          $mform->addElement('hidden', 'id', $COURSE->id);
 196          $mform->setType('id', PARAM_INT);
 197          $submitstring = get_string('download');
 198          if (empty($features['simpleui'])) {
 199              $submitstring = get_string('submit');
 200          } else if (!empty($CFG->gradepublishing)) {
 201              $submitstring = get_string('export', 'grades');
 202          }
 203  
 204          $this->add_action_buttons(false, $submitstring);
 205      }
 206  
 207      /**
 208       * Overrides the mform get_data method.
 209       *
 210       * Created to force a value since the validation method does not work with multiple checkbox.
 211       *
 212       * @return stdClass form data object.
 213       */
 214      public function get_data() {
 215          global $CFG;
 216          $data = parent::get_data();
 217          if ($data && $this->_customdata['multipledisplaytypes']) {
 218              if (count(array_filter($data->display)) == 0) {
 219                  // Ensure that a value was selected as the export plugins expect at least one value.
 220                  if ($CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_LETTER) {
 221                      $data->display['letter'] = GRADE_DISPLAY_TYPE_LETTER;
 222                  } else if ($CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE) {
 223                      $data->display['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
 224                  } else {
 225                      $data->display['real'] = GRADE_DISPLAY_TYPE_REAL;
 226                  }
 227              }
 228          }
 229          return $data;
 230      }
 231  }
 232