Search moodle.org's
Developer Documentation

   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                      $formattedgrade = format_float($grade, 2);
 138                  }
 139                  $updates[] = get_string('gradeupdate', 'assignfeedback_offline',
 140                                              array('grade'=>$formattedgrade, 'student'=>$userdesc));
 141              }
 142  
 143              if ($ignoremodified || !$stalemodificationdate) {
 144                  foreach ($record->feedback as $feedback) {
 145                      $plugin = $feedback['plugin'];
 146                      $field = $feedback['field'];
 147                      $newvalue = $feedback['value'];
 148                      $description = $feedback['description'];
 149                      $oldvalue = '';
 150                      if ($usergrade) {
 151                          $oldvalue = $plugin->get_editor_text($field, $usergrade->id);
 152                      }
 153                      if ($newvalue != $oldvalue) {
 154                          $update = true;
 155                          $updates[] = get_string('feedbackupdate', 'assignfeedback_offline',
 156                                                      array('text'=>$newvalue, 'field'=>$description, 'student'=>$userdesc));
 157                      }
 158                  }
 159              }
 160  
 161          }
 162          $gradeimporter->close(false);
 163  
 164          if ($update) {
 165              $mform->addElement('html', $renderer->list_block_contents(array(), $updates));
 166          } else {
 167              $mform->addElement('html', get_string('nochanges', 'assignfeedback_offline'));
 168          }
 169  
 170          $mform->addElement('hidden', 'id', $assignment->get_course_module()->id);
 171          $mform->setType('id', PARAM_INT);
 172          $mform->addElement('hidden', 'action', 'viewpluginpage');
 173          $mform->setType('action', PARAM_ALPHA);
 174          $mform->addElement('hidden', 'confirm', 'true');
 175          $mform->setType('confirm', PARAM_BOOL);
 176          $mform->addElement('hidden', 'plugin', 'offline');
 177          $mform->setType('plugin', PARAM_PLUGIN);
 178          $mform->addElement('hidden', 'pluginsubtype', 'assignfeedback');
 179          $mform->setType('pluginsubtype', PARAM_PLUGIN);
 180          $mform->addElement('hidden', 'pluginaction', 'uploadgrades');
 181          $mform->setType('pluginaction', PARAM_ALPHA);
 182          $mform->addElement('hidden', 'importid', $gradeimporter->importid);
 183          $mform->setType('importid', PARAM_INT);
 184          $mform->addElement('hidden', 'ignoremodified', $ignoremodified);
 185          $mform->setType('ignoremodified', PARAM_BOOL);
 186          $mform->addElement('hidden', 'draftid', $draftid);
 187          $mform->setType('draftid', PARAM_INT);
 188          if ($update) {
 189              $this->add_action_buttons(true, get_string('confirm'));
 190          } else {
 191              $mform->addElement('cancel');
 192              $mform->closeHeaderBefore('cancel');
 193          }
 194  
 195      }
 196  }
 197  

Search This Site: