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] [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  require_once($CFG->libdir.'/gradelib.php');
  24  
  25  class grade_import_form extends moodleform {
  26      function definition (){
  27          global $COURSE;
  28  
  29          $mform =& $this->_form;
  30  
  31          if (isset($this->_customdata)) {  // hardcoding plugin names here is hacky
  32              $features = $this->_customdata;
  33          } else {
  34              $features = array();
  35          }
  36  
  37          // course id needs to be passed for auth purposes
  38          $mform->addElement('hidden', 'id', optional_param('id', 0, PARAM_INT));
  39          $mform->setType('id', PARAM_INT);
  40          $mform->addElement('header', 'general', get_string('importfile', 'grades'));
  41  
  42          // Restrict the possible upload file types.
  43          if (!empty($features['acceptedtypes'])) {
  44              $acceptedtypes = $features['acceptedtypes'];
  45          } else {
  46              $acceptedtypes = '*';
  47          }
  48  
  49          // File upload.
  50          $mform->addElement('filepicker', 'userfile', get_string('file'), null, array('accepted_types' => $acceptedtypes));
  51          $mform->addRule('userfile', null, 'required');
  52          $encodings = core_text::get_encodings();
  53          $mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings);
  54          $mform->addHelpButton('encoding', 'encoding', 'grades');
  55  
  56          if (!empty($features['includeseparator'])) {
  57              $radio = array();
  58              $radio[] = $mform->createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
  59              $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
  60              $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcolon', 'grades'), 'colon');
  61              $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepsemicolon', 'grades'), 'semicolon');
  62              $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
  63              $mform->addHelpButton('separator', 'separator', 'grades');
  64              $mform->setDefault('separator', 'comma');
  65          }
  66  
  67          if (!empty($features['verbosescales'])) {
  68              $options = array(1=>get_string('yes'), 0=>get_string('no'));
  69              $mform->addElement('select', 'verbosescales', get_string('verbosescales', 'grades'), $options);
  70              $mform->addHelpButton('verbosescales', 'verbosescales', 'grades');
  71          }
  72  
  73          $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
  74          $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'grades'), $options); // TODO: localize
  75          $mform->addHelpButton('previewrows', 'rowpreviewnum', 'grades');
  76          $mform->setType('previewrows', PARAM_INT);
  77          $mform->addElement('checkbox', 'forceimport', get_string('forceimport', 'grades'));
  78          $mform->addHelpButton('forceimport', 'forceimport', 'grades');
  79          $mform->setDefault('forceimport', false);
  80          $mform->setType('forceimport', PARAM_BOOL);
  81          $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE));
  82          $mform->setType('groupid', PARAM_INT);
  83          $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
  84      }
  85  }
  86  
  87  class grade_import_mapping_form extends moodleform {
  88  
  89      function definition () {
  90          global $CFG, $COURSE;
  91          $mform =& $this->_form;
  92  
  93          // this is an array of headers
  94          $header = $this->_customdata['header'];
  95          // course id
  96  
  97          $mform->addElement('header', 'general', get_string('identifier', 'grades'));
  98          $mapfromoptions = array();
  99  
 100          if ($header) {
 101              foreach ($header as $i=>$h) {
 102                  $mapfromoptions[$i] = s($h);
 103              }
 104          }
 105          $mform->addElement('select', 'mapfrom', get_string('mapfrom', 'grades'), $mapfromoptions);
 106          $mform->addHelpButton('mapfrom', 'mapfrom', 'grades');
 107  
 108          $maptooptions = array(
 109              'userid'       => get_string('userid', 'grades'),
 110              'username'     => get_string('username'),
 111              'useridnumber' => get_string('idnumber'),
 112              'useremail'    => get_string('email'),
 113              '0'            => get_string('ignore', 'grades')
 114          );
 115          $mform->addElement('select', 'mapto', get_string('mapto', 'grades'), $maptooptions);
 116  
 117          $mform->addHelpButton('mapto', 'mapto', 'grades');
 118          $mform->addElement('header', 'general_map', get_string('mappings', 'grades'));
 119          $mform->addHelpButton('general_map', 'mappings', 'grades');
 120  
 121          // Add a feedback option.
 122          $feedbacks = [];
 123          $gradeitems = (array) $this->_customdata['gradeitems'];
 124          foreach ($gradeitems as $itemid => $itemname) {
 125              $feedbacks['feedback_'.$itemid] = get_string('feedbackforgradeitems', 'grades', $itemname);
 126          }
 127  
 128          if ($header) {
 129              $i = 0; // index
 130              foreach ($header as $h) {
 131                  $h = trim($h);
 132                  // This is what each header maps to.
 133                  $headermapsto = array(
 134                      get_string('others', 'grades')     => array(
 135                          '0'   => get_string('ignore', 'grades'),
 136                          'new' => get_string('newitem', 'grades')
 137                      ),
 138                      get_string('gradeitems', 'grades') => $gradeitems,
 139                      get_string('feedbacks', 'grades')  => $feedbacks
 140                  );
 141                  $mform->addElement('selectgroups', 'mapping_'.$i, s($h), $headermapsto);
 142                  $i++;
 143              }
 144          }
 145  
 146          // course id needs to be passed for auth purposes
 147          $mform->addElement('hidden', 'map', 1);
 148          $mform->setType('map', PARAM_INT);
 149          $mform->setConstant('map', 1);
 150          $mform->addElement('hidden', 'id', $this->_customdata['id']);
 151          $mform->setType('id', PARAM_INT);
 152          $mform->setConstant('id', $this->_customdata['id']);
 153          $mform->addElement('hidden', 'iid', $this->_customdata['iid']);
 154          $mform->setType('iid', PARAM_INT);
 155          $mform->setConstant('iid', $this->_customdata['iid']);
 156          $mform->addElement('hidden', 'importcode', $this->_customdata['importcode']);
 157          $mform->setType('importcode', PARAM_FILE);
 158          $mform->setConstant('importcode', $this->_customdata['importcode']);
 159          $mform->addElement('hidden', 'verbosescales', 1);
 160          $mform->setType('verbosescales', PARAM_INT);
 161          $mform->setConstant('verbosescales', $this->_customdata['verbosescales']);
 162          $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE));
 163          $mform->setType('groupid', PARAM_INT);
 164          $mform->setConstant('groupid', groups_get_course_group($COURSE));
 165          $mform->addElement('hidden', 'forceimport', $this->_customdata['forceimport']);
 166          $mform->setType('forceimport', PARAM_BOOL);
 167          $mform->setConstant('forceimport', $this->_customdata['forceimport']);
 168          $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
 169  
 170      }
 171  }