PageRenderTime 22ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/assign/feedback/offline/importgradeslib.php

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