Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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   * This file contains the forms to create and edit an instance of this module
  19   *
  20   * @package   assignfeedback_offline
  21   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
  26  
  27  require_once($CFG->libdir.'/formslib.php');
  28  require_once($CFG->dirroot.'/mod/assign/feedback/offline/importgradeslib.php');
  29  
  30  /**
  31   * Import grades form
  32   *
  33   * @package   assignfeedback_offline
  34   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class assignfeedback_offline_import_grades_form extends moodleform implements renderable {
  38  
  39      /**
  40       * Create this grade import form
  41       */
  42      public function definition() {
  43          global $CFG, $PAGE, $DB;
  44  
  45          $mform = $this->_form;
  46          $params = $this->_customdata;
  47  
  48          $renderer = $PAGE->get_renderer('assign');
  49  
  50          // Visible elements.
  51          $assignment = $params['assignment'];
  52          $csvdata = $params['csvdata'];
  53          $gradeimporter = $params['gradeimporter'];
  54          $update = false;
  55  
  56          $ignoremodified = $params['ignoremodified'];
  57          $draftid = $params['draftid'];
  58  
  59          if (!$gradeimporter) {
  60              print_error('invalidarguments');
  61              return;
  62          }
  63  
  64          if ($csvdata) {
  65              $gradeimporter->parsecsv($csvdata);
  66          }
  67  
  68          $scaleoptions = null;
  69          if ($assignment->get_instance()->grade < 0) {
  70              if ($scale = $DB->get_record('scale', array('id'=>-($assignment->get_instance()->grade)))) {
  71                  $scaleoptions = make_menu_from_list($scale->scale);
  72              }
  73          }
  74          if (!$gradeimporter->init()) {
  75              $thisurl = new moodle_url('/mod/assign/view.php', array('action'=>'viewpluginpage',
  76                                                                       'pluginsubtype'=>'assignfeedback',
  77                                                                       'plugin'=>'offline',
  78                                                                       'pluginaction'=>'uploadgrades',
  79                                                                       'id'=>$assignment->get_course_module()->id));
  80              print_error('invalidgradeimport', 'assignfeedback_offline', $thisurl);
  81              return;
  82          }
  83  
  84          $mform->addElement('header', 'importgrades', get_string('importgrades', 'assignfeedback_offline'));
  85  
  86          $updates = array();
  87          while ($record = $gradeimporter->next()) {
  88              $user = $record->user;
  89              $grade = $record->grade;
  90              $modified = $record->modified;
  91              $userdesc = fullname($user);
  92              if ($assignment->is_blind_marking()) {
  93                  $userdesc = get_string('hiddenuser', 'assign') . $assignment->get_uniqueid_for_user($user->id);
  94              }
  95  
  96              $usergrade = $assignment->get_user_grade($user->id, false);
  97              // Note: we lose the seconds when converting to user date format - so must not count seconds in comparision.
  98              $skip = false;
  99  
 100              $stalemodificationdate = ($usergrade && $usergrade->timemodified > ($modified + 60));
 101  
 102              if (!empty($scaleoptions)) {
 103                  // This is a scale - we need to convert any grades to indexes in the scale.
 104                  $scaleindex = array_search($grade, $scaleoptions);
 105                  if ($scaleindex !== false) {
 106                      $grade = $scaleindex;
 107                  } else {
 108                      $grade = '';
 109                  }
 110              } else {
 111                  $grade = unformat_float($grade);
 112              }
 113  
 114              if ($usergrade && $usergrade->grade == $grade) {
 115                  // Skip - grade not modified.
 116                  $skip = true;
 117              } else if (!isset($grade) || $grade === '' || $grade < 0) {
 118                  // Skip - grade has no value.
 119                  $skip = true;
 120              } else if (!$ignoremodified && $stalemodificationdate) {
 121                  // Skip - grade has been modified.
 122                  $skip = true;
 123              } else if ($assignment->grading_disabled($user->id)) {
 124                  // Skip grade is locked.
 125                  $skip = true;
 126              } else if (($assignment->get_instance()->grade > -1) &&
 127                        (($grade < 0) || ($grade > $assignment->get_instance()->grade))) {
 128                  // Out of range.
 129                  $skip = true;
 130              }
 131  
 132              if (!$skip) {
 133                  $update = true;
 134                  if (!empty($scaleoptions)) {
 135                      $formattedgrade = $scaleoptions[$grade];
 136                  } else {
 137                      $gradeitem = $assignment->get_grade_item();
 138                      $formattedgrade = format_float($grade, $gradeitem->get_decimals());
 139                  }
 140                  $updates[] = get_string('gradeupdate', 'assignfeedback_offline',
 141                                              array('grade'=>$formattedgrade, 'student'=>$userdesc));
 142              }
 143  
 144              if ($ignoremodified || !$stalemodificationdate) {
 145                  foreach ($record->feedback as $feedback) {
 146                      $plugin = $feedback['plugin'];
 147                      $field = $feedback['field'];
 148                      $newvalue = $feedback['value'];
 149                      $description = $feedback['description'];
 150                      $oldvalue = '';
 151                      if ($usergrade) {
 152                          $oldvalue = $plugin->get_editor_text($field, $usergrade->id);
 153                      }
 154                      if ($newvalue != $oldvalue) {
 155                          $update = true;
 156                          $updates[] = get_string('feedbackupdate', 'assignfeedback_offline',
 157                                                      array('text'=>$newvalue, 'field'=>$description, 'student'=>$userdesc));
 158                      }
 159                  }
 160              }
 161  
 162          }
 163          $gradeimporter->close(false);
 164  
 165          if ($update) {
 166              $mform->addElement('html', $renderer->list_block_contents(array(), $updates));
 167          } else {
 168              $mform->addElement('html', get_string('nochanges', 'assignfeedback_offline'));
 169          }
 170  
 171          $mform->addElement('hidden', 'id', $assignment->get_course_module()->id);
 172          $mform->setType('id', PARAM_INT);
 173          $mform->addElement('hidden', 'action', 'viewpluginpage');
 174          $mform->setType('action', PARAM_ALPHA);
 175          $mform->addElement('hidden', 'confirm', 'true');
 176          $mform->setType('confirm', PARAM_BOOL);
 177          $mform->addElement('hidden', 'plugin', 'offline');
 178          $mform->setType('plugin', PARAM_PLUGIN);
 179          $mform->addElement('hidden', 'pluginsubtype', 'assignfeedback');
 180          $mform->setType('pluginsubtype', PARAM_PLUGIN);
 181          $mform->addElement('hidden', 'pluginaction', 'uploadgrades');
 182          $mform->setType('pluginaction', PARAM_ALPHA);
 183          $mform->addElement('hidden', 'importid', $gradeimporter->importid);
 184          $mform->setType('importid', PARAM_INT);
 185  
 186          $mform->addElement('hidden', 'encoding', $gradeimporter->get_encoding());
 187          $mform->setType('encoding', PARAM_ALPHAEXT);
 188          $mform->addElement('hidden', 'separator', $gradeimporter->get_separator());
 189          $mform->setType('separator', PARAM_ALPHA);
 190  
 191          $mform->addElement('hidden', 'ignoremodified', $ignoremodified);
 192          $mform->setType('ignoremodified', PARAM_BOOL);
 193          $mform->addElement('hidden', 'draftid', $draftid);
 194          $mform->setType('draftid', PARAM_INT);
 195          if ($update) {
 196              $this->add_action_buttons(true, get_string('confirm'));
 197          } else {
 198              $mform->addElement('cancel');
 199              $mform->closeHeaderBefore('cancel');
 200          }
 201  
 202      }
 203  }
 204