PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/assign/feedback/offline/importgradesform.php

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