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_import_form extends moodleform {
  25      function definition () {
  26          global $COURSE, $USER, $CFG, $DB;
  27  
  28          $mform =& $this->_form;
  29  
  30          if (isset($this->_customdata)) {
  31              $features = $this->_customdata;
  32          } else {
  33              $features = array();
  34          }
  35  
  36          // course id needs to be passed for auth purposes
  37          $mform->addElement('hidden', 'id', optional_param('id', 0, PARAM_INT));
  38          $mform->setType('id', PARAM_INT);
  39  
  40          $mform->addElement('header', 'general', get_string('importfile', 'grades'));
  41  
  42          $mform->addElement('advcheckbox', 'feedback', get_string('importfeedback', 'grades'));
  43          $mform->setDefault('feedback', 0);
  44  
  45          // Restrict the possible upload file types.
  46          if (!empty($features['acceptedtypes'])) {
  47              $acceptedtypes = $features['acceptedtypes'];
  48          } else {
  49              $acceptedtypes = '*';
  50          }
  51  
  52          // File upload.
  53          $mform->addElement('filepicker', 'userfile', get_string('file'), null, array('accepted_types' => $acceptedtypes));
  54          $mform->disabledIf('userfile', 'url', 'noteq', '');
  55  
  56          $mform->addElement('text', 'url', get_string('fileurl', 'gradeimport_xml'), 'size="80"');
  57          $mform->setType('url', PARAM_URL);
  58          $mform->disabledIf('url', 'userfile', 'noteq', '');
  59          $mform->addHelpButton('url', 'fileurl', 'gradeimport_xml');
  60  
  61          if (!empty($CFG->gradepublishing)) {
  62              $mform->addElement('header', 'publishing', get_string('publishing', 'grades'));
  63              $options = array(get_string('nopublish', 'grades'), get_string('createnewkey', 'userkey'));
  64              $keys = $DB->get_records_select('user_private_key',
  65                              "script='grade/import' AND instance=? AND userid=?",
  66                              array($COURSE->id, $USER->id));
  67              if ($keys) {
  68                  foreach ($keys as $key) {
  69                      $options[$key->value] = $key->value; // TODO: add more details - ip restriction, valid until ??
  70                  }
  71              }
  72              $mform->addElement('select', 'key', get_string('userkey', 'userkey'), $options);
  73              $mform->addHelpButton('key', 'userkey', 'userkey');
  74              $mform->addElement('static', 'keymanagerlink', get_string('keymanager', 'userkey'),
  75                      '<a href="'.$CFG->wwwroot.'/grade/import/keymanager.php?id='.$COURSE->id.'">'.get_string('keymanager', 'userkey').'</a>');
  76  
  77              $mform->addElement('text', 'iprestriction', get_string('keyiprestriction', 'userkey'), array('size'=>80));
  78              $mform->addHelpButton('iprestriction', 'keyiprestriction', 'userkey');
  79              $mform->setDefault('iprestriction', getremoteaddr()); // own IP - just in case somebody does not know what user key is
  80  
  81              $mform->addElement('date_time_selector', 'validuntil', get_string('keyvaliduntil', 'userkey'), array('optional'=>true));
  82              $mform->addHelpButton('validuntil', 'keyvaliduntil', 'userkey');
  83              $mform->setDefault('validuntil', time()+3600*24*7); // only 1 week default duration - just in case somebody does not know what user key is
  84  
  85              $mform->disabledIf('iprestriction', 'key', 'noteq', 1);
  86              $mform->disabledIf('validuntil', 'key', 'noteq', 1);
  87  
  88              $mform->disabledIf('iprestriction', 'url', 'eq', '');
  89              $mform->disabledIf('validuntil', 'url', 'eq', '');
  90              $mform->disabledIf('key', 'url', 'eq', '');
  91          }
  92  
  93          $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
  94      }
  95  
  96      function validation($data, $files) {
  97          $err = parent::validation($data, $files);
  98          if (empty($data['url']) and empty($data['userfile'])) {
  99              if (array_key_exists('url', $data)) {
 100                  $err['url'] = get_string('required');
 101              }
 102              if (array_key_exists('userfile', $data)) {
 103                  $err['userfile'] = get_string('required');
 104              }
 105  
 106          } else if (array_key_exists('url', $data) and $data['url'] != clean_param($data['url'], PARAM_URL)) {
 107              $err['url'] = get_string('error');
 108          }
 109  
 110          return $err;
 111      }
 112  }
 113