2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the definition for the library class for file feedback plugin
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
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot.'/grade/grading/lib.php');
31 * library class for file feedback plugin extending feedback plugin base class
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
37 class assign_feedback_offline extends assign_feedback_plugin {
39 /** @var boolean|null $enabledcache Cached lookup of the is_enabled function */
40 private $enabledcache = null;
43 * Get the name of the file feedback plugin
46 public function get_name() {
47 return get_string('pluginname', 'assignfeedback_offline');
51 * Get form elements for grading form
53 * @param stdClass $grade
54 * @param MoodleQuickForm $mform
55 * @param stdClass $data
56 * @return bool true if elements were added to the form
58 public function get_form_elements($grade, MoodleQuickForm $mform, stdClass $data) {
63 * Return true if there are no feedback files
64 * @param stdClass $grade
66 public function is_empty(stdClass $grade) {
71 * Loop through uploaded grades and update the grades for this assignment
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
78 public function process_import_grades($draftid, $importid, $ignoremodified) {
82 require_capability('mod/assign:grade', $this->assignment->get_context());
84 $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
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')));
94 $file = reset($files);
96 $csvdata = $file->get_content();
99 $gradeimporter->parsecsv($csvdata);
101 if (!$gradeimporter->init()) {
102 $thisurl = new moodle_url('/mod/assign/view.php', array('action'=>'viewpluginpage',
103 'pluginsubtype'=>'assignfeedback',
105 'pluginaction'=>'uploadgrades',
106 'id'=>$assignment->get_course_module()->id));
107 print_error('invalidgradeimport', 'assignfeedback_offline', $thisurl);
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);
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;
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);
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;
137 $record->grade = unformat_float($record->grade);
140 // Note: Do not count the seconds when comparing modified dates.
142 $stalemodificationdate = ($usergrade && $usergrade->timemodified > ($modified + 60));
144 if ($usergrade && $usergrade->grade == $record->grade) {
145 // Skip - grade not modified.
147 } else if (!isset($record->grade) || $record->grade === '' || $record->grade < 0) {
148 // Skip - grade has no value.
150 } else if (!$ignoremodified && $stalemodificationdate) {
151 // Skip - grade has been modified.
153 } else if ($this->assignment->grading_disabled($record->user->id)) {
154 // Skip grade is locked.
156 } else if (($this->assignment->get_instance()->grade > -1) &&
157 (($record->grade < 0) || ($record->grade > $this->assignment->get_instance()->grade))) {
163 $grade = $this->assignment->get_user_grade($record->user->id, true);
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 $this->assignment->add_to_log('grade submission', $this->assignment->format_grade_for_log($grade));
174 if ($ignoremodified || !$stalemodificationdate) {
175 foreach ($record->feedback as $feedback) {
176 $plugin = $feedback['plugin'];
177 $field = $feedback['field'];
178 $newvalue = $feedback['value'];
179 $description = $feedback['description'];
182 $oldvalue = $plugin->get_editor_text($field, $usergrade->id);
183 if (empty($oldvalue)) {
187 if ($newvalue != $oldvalue) {
189 $grade = $this->assignment->get_user_grade($record->user->id, true);
190 $this->assignment->notify_grade_modified($grade);
191 if ($plugin->set_editor_text($field, $newvalue, $grade->id)) {
192 $logdesc = get_string('feedbackupdate', 'assignfeedback_offline',
193 array('field'=>$description,
194 'student'=>$userdesc,
197 $this->assignment->add_to_log('save grading feedback', $logdesc);
200 // If this is the gradebook comments plugin - post an update to the gradebook.
201 if (($plugin->get_subtype() . '_' . $plugin->get_type()) == $gradebookplugin) {
202 $grade->feedbacktext = $plugin->text_for_gradebook($grade);
203 $grade->feedbackformat = $plugin->format_for_gradebook($grade);
204 $this->assignment->update_grade($grade);
210 $gradeimporter->close(true);
212 $renderer = $this->assignment->get_renderer();
215 $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
216 $this->assignment->get_context(),
218 $this->assignment->get_course_module()->id,
219 get_string('importgrades', 'assignfeedback_offline')));
220 $o .= $renderer->box(get_string('updatedgrades', 'assignfeedback_offline', $updatecount));
221 $url = new moodle_url('view.php',
222 array('id'=>$this->assignment->get_course_module()->id,
223 'action'=>'grading'));
224 $o .= $renderer->continue_button($url);
225 $o .= $renderer->render_footer();
230 * Display upload grades form
232 * @return string The response html
234 public function upload_grades() {
237 require_capability('mod/assign:grade', $this->assignment->get_context());
238 require_once($CFG->dirroot . '/mod/assign/feedback/offline/uploadgradesform.php');
239 require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradesform.php');
240 require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradeslib.php');
241 require_once($CFG->libdir . '/csvlib.class.php');
243 $mform = new assignfeedback_offline_upload_grades_form(null,
244 array('context'=>$this->assignment->get_context(),
245 'cm'=>$this->assignment->get_course_module()->id));
249 $confirm = optional_param('confirm', 0, PARAM_BOOL);
250 $renderer = $this->assignment->get_renderer();
252 if ($mform->is_cancelled()) {
253 redirect(new moodle_url('view.php',
254 array('id'=>$this->assignment->get_course_module()->id,
255 'action'=>'grading')));
257 } else if (($data = $mform->get_data()) &&
258 ($csvdata = $mform->get_file_content('gradesfile'))) {
260 $importid = csv_import_reader::get_new_iid('assignfeedback_offline');
261 $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
262 // File exists and was valid.
263 $ignoremodified = !empty($data->ignoremodified);
265 $draftid = $data->gradesfile;
269 $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
271 'ignoremodified'=>$ignoremodified,
272 'gradeimporter'=>$gradeimporter,
273 'draftid'=>$draftid));
275 $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
276 $this->assignment->get_context(),
278 $this->assignment->get_course_module()->id,
279 get_string('confirmimport', 'assignfeedback_offline')));
280 $o .= $renderer->render(new assign_form('confirmimport', $mform));
281 $o .= $renderer->render_footer();
282 } else if ($confirm) {
284 $importid = optional_param('importid', 0, PARAM_INT);
285 $draftid = optional_param('draftid', 0, PARAM_INT);
286 $ignoremodified = optional_param('ignoremodified', 0, PARAM_BOOL);
287 $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
288 $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
290 'ignoremodified'=>$ignoremodified,
291 'gradeimporter'=>$gradeimporter,
292 'draftid'=>$draftid));
293 if ($mform->is_cancelled()) {
294 redirect(new moodle_url('view.php',
295 array('id'=>$this->assignment->get_course_module()->id,
296 'action'=>'grading')));
300 $o .= $this->process_import_grades($draftid, $importid, $ignoremodified);
303 $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
304 $this->assignment->get_context(),
306 $this->assignment->get_course_module()->id,
307 get_string('uploadgrades', 'assignfeedback_offline')));
308 $o .= $renderer->render(new assign_form('batchuploadfiles', $mform));
309 $o .= $renderer->render_footer();
316 * Download a marking worksheet
318 * @return string The response html
320 public function download_grades() {
323 require_capability('mod/assign:grade', $this->assignment->get_context());
324 require_once($CFG->dirroot . '/mod/assign/gradingtable.php');
326 $groupmode = groups_get_activity_groupmode($this->assignment->get_course_module());
331 $groupid = groups_get_activity_group($this->assignment->get_course_module(), true);
332 $groupname = groups_get_group_name($groupid) . '-';
334 $filename = clean_filename(get_string('offlinegradingworksheet', 'assignfeedback_offline') . '-' .
335 $this->assignment->get_course()->shortname . '-' .
336 $this->assignment->get_instance()->name . '-' .
338 $this->assignment->get_course_module()->id);
340 $table = new assign_grading_table($this->assignment, 0, '', 0, false, $filename);
342 $table->out(0, false);
347 * Print a sub page in this plugin
349 * @param string $action - The plugin action
350 * @return string The response html
352 public function view_page($action) {
353 if ($action == 'downloadgrades') {
354 return $this->download_grades();
355 } else if ($action == 'uploadgrades') {
356 return $this->upload_grades();
363 * Return a list of the grading actions performed by this plugin
364 * This plugin supports upload zip
366 * @return array The list of grading actions
368 public function get_grading_actions() {
369 return array('uploadgrades'=>get_string('uploadgrades', 'assignfeedback_offline'),
370 'downloadgrades'=>get_string('downloadgrades', 'assignfeedback_offline'));
374 * Override the default is_enabled to disable this plugin if advanced grading is active
378 public function is_enabled() {
379 if ($this->enabledcache === null) {
380 $gradingmanager = get_grading_manager($this->assignment->get_context(), 'mod_assign', 'submissions');
381 $controller = $gradingmanager->get_active_controller();
382 $active = !empty($controller);
385 $enabledcache = false;
387 $enabledcache = parent::is_enabled();
390 return $this->enabledcache;
394 * Do not show this plugin in the grading table or on the front page
398 public function has_user_summary() {