MDL-37883 Assignment Grade import/export. Additional fix to prevent different grade...
[moodle.git] / mod / assign / feedback / offline / importgradesform.php
CommitLineData
df47b77f 1<?php
df47b77f
DW
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
25defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
26
27require_once($CFG->libdir.'/formslib.php');
28require_once($CFG->dirroot.'/mod/assign/feedback/offline/importgradeslib.php');
29
7a2b911c 30/**
df47b77f
DW
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 */
37class assignfeedback_offline_import_grades_form extends moodleform implements renderable {
38
39 /**
40 * Create this grade import form
41 */
7a2b911c 42 public function definition() {
df47b77f
DW
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 $scale = $DB->get_record('scale', array('id'=>-($assignment->get_instance()->grade)));
71 if ($scale) {
72 $scaleoptions = explode(',', $scale->scale);
73 }
74 }
75 if (!$gradeimporter->init()) {
76 $thisurl = new moodle_url('/mod/assign/view.php', array('action'=>'viewpluginpage',
77 'pluginsubtype'=>'assignfeedback',
78 'plugin'=>'offline',
79 'pluginaction'=>'uploadgrades',
80 'id'=>$assignment->get_course_module()->id));
81 print_error('invalidgradeimport', 'assignfeedback_offline', $thisurl);
82 return;
83 }
84
85 $mform->addElement('header', 'importgrades', get_string('importgrades', 'assignfeedback_offline'));
86
87 $updates = array();
88 while ($record = $gradeimporter->next()) {
89 $user = $record->user;
90 $grade = $record->grade;
df47b77f
DW
91 $modified = $record->modified;
92 $userdesc = fullname($user);
93 if ($assignment->is_blind_marking()) {
cc2ed228 94 $userdesc = get_string('hiddenuser', 'assign') . $assignment->get_uniqueid_for_user($user->id);
df47b77f
DW
95 }
96
97 $usergrade = $assignment->get_user_grade($user->id, false);
98 // Note: we lose the seconds when converting to user date format - so must not count seconds in comparision.
99 $skip = false;
100
101 $stalemodificationdate = ($usergrade && $usergrade->timemodified > ($modified + 60));
102
103 if (!empty($scaleoptions)) {
104 // This is a scale - we need to convert any grades to indexes in the scale.
105 $scaleindex = array_search($grade, $scaleoptions);
106 if ($scaleindex !== false) {
107 $grade = $scaleindex + 1;
108 } else {
109 $grade = '';
110 }
070d4317
DW
111 } else {
112 $grade = unformat_float($grade);
df47b77f
DW
113 }
114
115 if ($usergrade && $usergrade->grade == $grade) {
116 // Skip - grade not modified.
117 $skip = true;
118 } else if (!isset($grade) || $grade === '' || $grade < 0) {
119 // Skip - grade has no value.
120 $skip = true;
121 } else if (!$ignoremodified && $stalemodificationdate) {
122 // Skip - grade has been modified.
123 $skip = true;
124 } else if ($assignment->grading_disabled($user->id)) {
125 // Skip grade is locked.
126 $skip = true;
45a64c19 127 } else if (($assignment->get_instance()->grade > -1) &&
070d4317
DW
128 (($grade < 0) || ($grade > $assignment->get_instance()->grade))) {
129 // Out of range.
45a64c19 130 $skip = true;
df47b77f
DW
131 }
132
133 if (!$skip) {
134 $update = true;
135 $updates[] = get_string('gradeupdate', 'assignfeedback_offline',
7688a319 136 array('grade'=>format_float($grade, 2), 'student'=>$userdesc));
df47b77f
DW
137 }
138
139 if ($ignoremodified || !$stalemodificationdate) {
140 foreach ($record->feedback as $feedback) {
141 $plugin = $feedback['plugin'];
142 $field = $feedback['field'];
143 $newvalue = $feedback['value'];
144 $description = $feedback['description'];
145 $oldvalue = '';
146 if ($usergrade) {
147 $oldvalue = $plugin->get_editor_text($field, $usergrade->id);
148 }
149 if ($newvalue != $oldvalue) {
150 $update = true;
151 $updates[] = get_string('feedbackupdate', 'assignfeedback_offline',
152 array('text'=>$newvalue, 'field'=>$description, 'student'=>$userdesc));
153 }
154 }
155 }
156
157 }
158 $gradeimporter->close(false);
159
160 if ($update) {
161 $mform->addElement('html', $renderer->list_block_contents(array(), $updates));
162 } else {
163 $mform->addElement('html', get_string('nochanges', 'assignfeedback_offline'));
164 }
165
166 $mform->addElement('hidden', 'id', $assignment->get_course_module()->id);
167 $mform->addElement('hidden', 'action', 'viewpluginpage');
168 $mform->addElement('hidden', 'confirm', 'true');
169 $mform->addElement('hidden', 'plugin', 'offline');
170 $mform->addElement('hidden', 'pluginsubtype', 'assignfeedback');
171 $mform->addElement('hidden', 'pluginaction', 'uploadgrades');
172 $mform->addElement('hidden', 'importid', $gradeimporter->importid);
173 $mform->addElement('hidden', 'ignoremodified', $ignoremodified);
174 $mform->addElement('hidden', 'draftid', $draftid);
175 if ($update) {
176 $this->add_action_buttons(true, get_string('confirm'));
177 } else {
178 $mform->addElement('cancel');
179 $mform->closeHeaderBefore('cancel');
180 }
181
182 }
183}
184