[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/assign/feedback/offline/ -> locallib.php (source)

   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 definition for the library class for file feedback plugin
  19   *
  20   *
  21   * @package   assignfeedback_offline
  22   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->dirroot.'/grade/grading/lib.php');
  29  
  30  /**
  31   * library class for file feedback plugin extending feedback plugin base class
  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 assign_feedback_offline extends assign_feedback_plugin {
  38  
  39      /** @var boolean|null $enabledcache Cached lookup of the is_enabled function */
  40      private $enabledcache = null;
  41  
  42      /**
  43       * Get the name of the file feedback plugin
  44       * @return string
  45       */
  46      public function get_name() {
  47          return get_string('pluginname', 'assignfeedback_offline');
  48      }
  49  
  50      /**
  51       * Get form elements for grading form
  52       *
  53       * @param stdClass $grade
  54       * @param MoodleQuickForm $mform
  55       * @param stdClass $data
  56       * @return bool true if elements were added to the form
  57       */
  58      public function get_form_elements($grade, MoodleQuickForm $mform, stdClass $data) {
  59          return false;
  60      }
  61  
  62      /**
  63       * Return true if there are no feedback files
  64       * @param stdClass $grade
  65       */
  66      public function is_empty(stdClass $grade) {
  67          return true;
  68      }
  69  
  70      /**
  71       * Loop through uploaded grades and update the grades for this assignment
  72       *
  73       * @param int $draftid - The unique draft item id for this import
  74       * @param int $importid - The unique import ID for this csv import operation
  75       * @param bool $ignoremodified - Ignore the last modified date when checking fields
  76       * @return string - The html response
  77       */
  78      public function process_import_grades($draftid, $importid, $ignoremodified) {
  79          global $USER, $DB;
  80  
  81          require_sesskey();
  82          require_capability('mod/assign:grade', $this->assignment->get_context());
  83  
  84          $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
  85  
  86          $context = context_user::instance($USER->id);
  87          $fs = get_file_storage();
  88          if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
  89              redirect(new moodle_url('view.php',
  90                                  array('id'=>$this->assignment->get_course_module()->id,
  91                                        'action'=>'grading')));
  92              return;
  93          }
  94          $file = reset($files);
  95  
  96          $csvdata = $file->get_content();
  97  
  98          if ($csvdata) {
  99              $gradeimporter->parsecsv($csvdata);
 100          }
 101          if (!$gradeimporter->init()) {
 102              $thisurl = new moodle_url('/mod/assign/view.php', array('action'=>'viewpluginpage',
 103                                                                       'pluginsubtype'=>'assignfeedback',
 104                                                                       'plugin'=>'offline',
 105                                                                       'pluginaction'=>'uploadgrades',
 106                                                                       'id'=>$assignment->get_course_module()->id));
 107              print_error('invalidgradeimport', 'assignfeedback_offline', $thisurl);
 108              return;
 109          }
 110          // Does this assignment use a scale?
 111          $scaleoptions = null;
 112          if ($this->assignment->get_instance()->grade < 0) {
 113              if ($scale = $DB->get_record('scale', array('id'=>-($this->assignment->get_instance()->grade)))) {
 114                  $scaleoptions = make_menu_from_list($scale->scale);
 115              }
 116          }
 117          // We may need to upgrade the gradebook comments after this update.
 118          $adminconfig = $this->assignment->get_admin_config();
 119          $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook;
 120  
 121          $updatecount = 0;
 122          while ($record = $gradeimporter->next()) {
 123              $user = $record->user;
 124              $modified = $record->modified;
 125              $userdesc = fullname($user);
 126              $usergrade = $this->assignment->get_user_grade($user->id, false);
 127  
 128              if (!empty($scaleoptions)) {
 129                  // This is a scale - we need to convert any grades to indexes in the scale.
 130                  $scaleindex = array_search($record->grade, $scaleoptions);
 131                  if ($scaleindex !== false) {
 132                      $record->grade = $scaleindex;
 133                  } else {
 134                      $record->grade = '';
 135                  }
 136              } else {
 137                  $record->grade = unformat_float($record->grade);
 138              }
 139  
 140              // Note: Do not count the seconds when comparing modified dates.
 141              $skip = false;
 142              $stalemodificationdate = ($usergrade && $usergrade->timemodified > ($modified + 60));
 143  
 144              if ($usergrade && $usergrade->grade == $record->grade) {
 145                  // Skip - grade not modified.
 146                  $skip = true;
 147              } else if (!isset($record->grade) || $record->grade === '' || $record->grade < 0) {
 148                  // Skip - grade has no value.
 149                  $skip = true;
 150              } else if (!$ignoremodified && $stalemodificationdate) {
 151                  // Skip - grade has been modified.
 152                  $skip = true;
 153              } else if ($this->assignment->grading_disabled($record->user->id)) {
 154                  // Skip grade is locked.
 155                  $skip = true;
 156              } else if (($this->assignment->get_instance()->grade > -1) &&
 157                        (($record->grade < 0) || ($record->grade > $this->assignment->get_instance()->grade))) {
 158                  // Out of range.
 159                  $skip = true;
 160              }
 161  
 162              if (!$skip) {
 163                  $grade = $this->assignment->get_user_grade($record->user->id, true);
 164  
 165                  $grade->grade = $record->grade;
 166                  $grade->grader = $USER->id;
 167                  if ($this->assignment->update_grade($grade)) {
 168                      $this->assignment->notify_grade_modified($grade);
 169                      $updatecount += 1;
 170                  }
 171              }
 172  
 173              if ($ignoremodified || !$stalemodificationdate) {
 174                  foreach ($record->feedback as $feedback) {
 175                      $plugin = $feedback['plugin'];
 176                      $field = $feedback['field'];
 177                      $newvalue = $feedback['value'];
 178                      $description = $feedback['description'];
 179                      $oldvalue = '';
 180                      if ($usergrade) {
 181                          $oldvalue = $plugin->get_editor_text($field, $usergrade->id);
 182                          if (empty($oldvalue)) {
 183                              $oldvalue = '';
 184                          }
 185                      }
 186                      if ($newvalue != $oldvalue) {
 187                          $updatecount += 1;
 188                          $grade = $this->assignment->get_user_grade($record->user->id, true);
 189                          $this->assignment->notify_grade_modified($grade);
 190                          $plugin->set_editor_text($field, $newvalue, $grade->id);
 191  
 192                          // If this is the gradebook comments plugin - post an update to the gradebook.
 193                          if (($plugin->get_subtype() . '_' . $plugin->get_type()) == $gradebookplugin) {
 194                              $grade->feedbacktext = $plugin->text_for_gradebook($grade);
 195                              $grade->feedbackformat = $plugin->format_for_gradebook($grade);
 196                              $this->assignment->update_grade($grade);
 197                          }
 198                      }
 199                  }
 200              }
 201          }
 202          $gradeimporter->close(true);
 203  
 204          $renderer = $this->assignment->get_renderer();
 205          $o = '';
 206  
 207          $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
 208                                                    $this->assignment->get_context(),
 209                                                    false,
 210                                                    $this->assignment->get_course_module()->id,
 211                                                    get_string('importgrades', 'assignfeedback_offline')));
 212          $o .= $renderer->box(get_string('updatedgrades', 'assignfeedback_offline', $updatecount));
 213          $url = new moodle_url('view.php',
 214                                array('id'=>$this->assignment->get_course_module()->id,
 215                                      'action'=>'grading'));
 216          $o .= $renderer->continue_button($url);
 217          $o .= $renderer->render_footer();
 218          return $o;
 219      }
 220  
 221      /**
 222       * Display upload grades form
 223       *
 224       * @return string The response html
 225       */
 226      public function upload_grades() {
 227          global $CFG, $USER;
 228  
 229          require_capability('mod/assign:grade', $this->assignment->get_context());
 230          require_once($CFG->dirroot . '/mod/assign/feedback/offline/uploadgradesform.php');
 231          require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradesform.php');
 232          require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradeslib.php');
 233          require_once($CFG->libdir . '/csvlib.class.php');
 234  
 235          $mform = new assignfeedback_offline_upload_grades_form(null,
 236                                                                array('context'=>$this->assignment->get_context(),
 237                                                                      'cm'=>$this->assignment->get_course_module()->id));
 238  
 239          $o = '';
 240  
 241          $confirm = optional_param('confirm', 0, PARAM_BOOL);
 242          $renderer = $this->assignment->get_renderer();
 243  
 244          if ($mform->is_cancelled()) {
 245              redirect(new moodle_url('view.php',
 246                                      array('id'=>$this->assignment->get_course_module()->id,
 247                                            'action'=>'grading')));
 248              return;
 249          } else if (($data = $mform->get_data()) &&
 250                     ($csvdata = $mform->get_file_content('gradesfile'))) {
 251  
 252              $importid = csv_import_reader::get_new_iid('assignfeedback_offline');
 253              $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
 254              // File exists and was valid.
 255              $ignoremodified = !empty($data->ignoremodified);
 256  
 257              $draftid = $data->gradesfile;
 258  
 259              // Preview import.
 260  
 261              $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
 262                                                                         'csvdata'=>$csvdata,
 263                                                                         'ignoremodified'=>$ignoremodified,
 264                                                                         'gradeimporter'=>$gradeimporter,
 265                                                                         'draftid'=>$draftid));
 266  
 267              $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
 268                                                              $this->assignment->get_context(),
 269                                                              false,
 270                                                              $this->assignment->get_course_module()->id,
 271                                                              get_string('confirmimport', 'assignfeedback_offline')));
 272              $o .= $renderer->render(new assign_form('confirmimport', $mform));
 273              $o .= $renderer->render_footer();
 274          } else if ($confirm) {
 275  
 276              $importid = optional_param('importid', 0, PARAM_INT);
 277              $draftid = optional_param('draftid', 0, PARAM_INT);
 278              $ignoremodified = optional_param('ignoremodified', 0, PARAM_BOOL);
 279              $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
 280              $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
 281                                                                         'csvdata'=>'',
 282                                                                         'ignoremodified'=>$ignoremodified,
 283                                                                         'gradeimporter'=>$gradeimporter,
 284                                                                         'draftid'=>$draftid));
 285              if ($mform->is_cancelled()) {
 286                  redirect(new moodle_url('view.php',
 287                                          array('id'=>$this->assignment->get_course_module()->id,
 288                                                'action'=>'grading')));
 289                  return;
 290              }
 291  
 292              $o .= $this->process_import_grades($draftid, $importid, $ignoremodified);
 293          } else {
 294  
 295              $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
 296                                                              $this->assignment->get_context(),
 297                                                              false,
 298                                                              $this->assignment->get_course_module()->id,
 299                                                              get_string('uploadgrades', 'assignfeedback_offline')));
 300              $o .= $renderer->render(new assign_form('batchuploadfiles', $mform));
 301              $o .= $renderer->render_footer();
 302          }
 303  
 304          return $o;
 305      }
 306  
 307      /**
 308       * Download a marking worksheet
 309       *
 310       * @return string The response html
 311       */
 312      public function download_grades() {
 313          global $CFG;
 314  
 315          require_capability('mod/assign:grade', $this->assignment->get_context());
 316          require_once($CFG->dirroot . '/mod/assign/gradingtable.php');
 317  
 318          $groupmode = groups_get_activity_groupmode($this->assignment->get_course_module());
 319          // All users.
 320          $groupid = 0;
 321          $groupname = '';
 322          if ($groupmode) {
 323              $groupid = groups_get_activity_group($this->assignment->get_course_module(), true);
 324              $groupname = groups_get_group_name($groupid) . '-';
 325          }
 326          $filename = clean_filename(get_string('offlinegradingworksheet', 'assignfeedback_offline') . '-' .
 327                                     $this->assignment->get_course()->shortname . '-' .
 328                                     $this->assignment->get_instance()->name . '-' .
 329                                     $groupname .
 330                                     $this->assignment->get_course_module()->id);
 331  
 332          $table = new assign_grading_table($this->assignment, 0, '', 0, false, $filename);
 333  
 334          $table->out(0, false);
 335          return;
 336      }
 337  
 338      /**
 339       * Print a sub page in this plugin
 340       *
 341       * @param string $action - The plugin action
 342       * @return string The response html
 343       */
 344      public function view_page($action) {
 345          if ($action == 'downloadgrades') {
 346              return $this->download_grades();
 347          } else if ($action == 'uploadgrades') {
 348              return $this->upload_grades();
 349          }
 350  
 351          return '';
 352      }
 353  
 354      /**
 355       * Return a list of the grading actions performed by this plugin
 356       * This plugin supports upload zip
 357       *
 358       * @return array The list of grading actions
 359       */
 360      public function get_grading_actions() {
 361          return array('uploadgrades'=>get_string('uploadgrades', 'assignfeedback_offline'),
 362                      'downloadgrades'=>get_string('downloadgrades', 'assignfeedback_offline'));
 363      }
 364  
 365      /**
 366       * Override the default is_enabled to disable this plugin if advanced grading is active
 367       *
 368       * @return bool
 369       */
 370      public function is_enabled() {
 371          if ($this->enabledcache === null) {
 372              $gradingmanager = get_grading_manager($this->assignment->get_context(), 'mod_assign', 'submissions');
 373              $controller = $gradingmanager->get_active_controller();
 374              $active = !empty($controller);
 375  
 376              if ($active) {
 377                  $this->enabledcache = false;
 378              } else {
 379                  $this->enabledcache = parent::is_enabled();
 380              }
 381          }
 382          return $this->enabledcache;
 383      }
 384  
 385      /**
 386       * Do not show this plugin in the grading table or on the front page
 387       *
 388       * @return bool
 389       */
 390      public function has_user_summary() {
 391          return false;
 392      }
 393  
 394  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1