[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/assign/feedback/offline/ -> importgradeslib.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 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  /**
  28   * CSV Grade importer
  29   *
  30   * @package   assignfeedback_offline
  31   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class assignfeedback_offline_grade_importer {
  35  
  36      /** @var string $importid - unique id for this import operation - must be passed between requests */
  37      public $importid;
  38  
  39      /** @var csv_import_reader $csvreader - the csv importer class */
  40      private $csvreader;
  41  
  42      /** @var assignment $assignment - the assignment class */
  43      private $assignment;
  44  
  45      /** @var int $gradeindex the column index containing the grades */
  46      private $gradeindex = -1;
  47  
  48      /** @var int $idindex the column index containing the unique id  */
  49      private $idindex = -1;
  50  
  51      /** @var int $modifiedindex the column index containing the last modified time */
  52      private $modifiedindex = -1;
  53  
  54      /** @var array $validusers only the enrolled users with the correct capability in this course */
  55      private $validusers;
  56  
  57      /** @var array $feedbackcolumnindexes A lookup of column indexes for feedback plugin text import columns */
  58      private $feedbackcolumnindexes = array();
  59  
  60      /**
  61       * Constructor
  62       *
  63       * @param string $importid A unique id for this import
  64       * @param assign $assignment The current assignment
  65       */
  66      public function __construct($importid, assign $assignment) {
  67          $this->importid = $importid;
  68          $this->assignment = $assignment;
  69      }
  70  
  71      /**
  72       * Parse a csv file and save the content to a temp file
  73       * Should be called before init()
  74       *
  75       * @param string $csvdata The csv data
  76       * @return bool false is a failed import
  77       */
  78      public function parsecsv($csvdata) {
  79          $this->csvreader = new csv_import_reader($this->importid, 'assignfeedback_offline');
  80          $this->csvreader->load_csv_content($csvdata, 'utf-8', 'comma');
  81      }
  82  
  83      /**
  84       * Initialise the import reader and locate the column indexes.
  85       *
  86       * @return bool false is a failed import
  87       */
  88      public function init() {
  89          if ($this->csvreader == null) {
  90              $this->csvreader = new csv_import_reader($this->importid, 'assignfeedback_offline');
  91          }
  92          $this->csvreader->init();
  93  
  94          $columns = $this->csvreader->get_columns();
  95  
  96          $strgrade = get_string('grade');
  97          $strid = get_string('recordid', 'assign');
  98          $strmodified = get_string('lastmodifiedgrade', 'assign');
  99  
 100          foreach ($this->assignment->get_feedback_plugins() as $plugin) {
 101              if ($plugin->is_enabled() && $plugin->is_visible()) {
 102                  foreach ($plugin->get_editor_fields() as $field => $description) {
 103                      $this->feedbackcolumnindexes[$description] = array('plugin'=>$plugin,
 104                                                                         'field'=>$field,
 105                                                                         'description'=>$description);
 106                  }
 107              }
 108          }
 109  
 110          if ($columns) {
 111              foreach ($columns as $index => $column) {
 112                  if (isset($this->feedbackcolumnindexes[$column])) {
 113                      $this->feedbackcolumnindexes[$column]['index'] = $index;
 114                  }
 115                  if ($column == $strgrade) {
 116                      $this->gradeindex = $index;
 117                  }
 118                  if ($column == $strid) {
 119                      $this->idindex = $index;
 120                  }
 121                  if ($column == $strmodified) {
 122                      $this->modifiedindex = $index;
 123                  }
 124              }
 125          }
 126  
 127          if ($this->idindex < 0 || $this->gradeindex < 0 || $this->modifiedindex < 0) {
 128              return false;
 129          }
 130  
 131          $groupmode = groups_get_activity_groupmode($this->assignment->get_course_module());
 132          // All users.
 133          $groupid = 0;
 134          $groupname = '';
 135          if ($groupmode) {
 136              $groupid = groups_get_activity_group($this->assignment->get_course_module(), true);
 137              $groupname = groups_get_group_name($groupid).'-';
 138          }
 139          $this->validusers = $this->assignment->list_participants($groupid, false);
 140          return true;
 141      }
 142  
 143      /**
 144       * Get the next row of data from the csv file (only the columns we care about)
 145       *
 146       * @return stdClass or false The stdClass is an object containing user, grade and lastmodified
 147       */
 148      public function next() {
 149          global $DB;
 150          $result = new stdClass();
 151  
 152          while ($record = $this->csvreader->next()) {
 153              $idstr = $record[$this->idindex];
 154              // Strip the integer from the end of the participant string.
 155              $id = substr($idstr, strlen(get_string('hiddenuser', 'assign')));
 156              if ($userid = $this->assignment->get_user_id_for_uniqueid($id)) {
 157                  if (array_key_exists($userid, $this->validusers)) {
 158                      $result->grade = $record[$this->gradeindex];
 159                      $result->modified = strtotime($record[$this->modifiedindex]);
 160                      $result->user = $this->validusers[$userid];
 161                      $result->feedback = array();
 162                      foreach ($this->feedbackcolumnindexes as $description => $details) {
 163                          if (!empty($details['index'])) {
 164                              $details['value'] = $record[$details['index']];
 165                              $result->feedback[] = $details;
 166                          }
 167                      }
 168  
 169                      return $result;
 170                  }
 171              }
 172          }
 173  
 174          // If we got here the csvreader had no more rows.
 175          return false;
 176      }
 177  
 178      /**
 179       * Close the grade importer file and optionally delete any temp files
 180       *
 181       * @param bool $delete
 182       */
 183      public function close($delete) {
 184          $this->csvreader->close();
 185          if ($delete) {
 186              $this->csvreader->cleanup();
 187          }
 188      }
 189  }
 190  


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