d64927fac300a9bb795818ee9f2d4e9238c942aa
[moodle.git] / mod / assign / feedback / offline / locallib.php
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/>.
17 /**
18  * This file contains the definition for the library class for file feedback plugin
19  *
20  *
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
24  */
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot.'/grade/grading/lib.php');
30 /**
31  * library class for file feedback plugin extending feedback plugin base class
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  */
37 class assign_feedback_offline extends assign_feedback_plugin {
39     /**
40      * Get the name of the file feedback plugin
41      * @return string
42      */
43     public function get_name() {
44         return get_string('pluginname', 'assignfeedback_offline');
45     }
47     /**
48      * Get form elements for grading form
49      *
50      * @param stdClass $grade
51      * @param MoodleQuickForm $mform
52      * @param stdClass $data
53      * @return bool true if elements were added to the form
54      */
55     public function get_form_elements($grade, MoodleQuickForm $mform, stdClass $data) {
56         return false;
57     }
59     /**
60      * Return true if there are no feedback files
61      * @param stdClass $grade
62      */
63     public function is_empty(stdClass $grade) {
64         return true;
65     }
67     /**
68      * Loop through uploaded grades and update the grades for this assignment
69      *
70      * @param int $draftid - The unique draft item id for this import
71      * @param int $importid - The unique import ID for this csv import operation
72      * @param bool $ignoremodified - Ignore the last modified date when checking fields
73      * @return string - The html response
74      */
75     public function process_import_grades($draftid, $importid, $ignoremodified) {
76         global $USER, $DB;
78         require_sesskey();
79         require_capability('mod/assign:grade', $this->assignment->get_context());
81         $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
83         $context = get_context_instance(CONTEXT_USER, $USER->id);
84         $fs = get_file_storage();
85         if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
86             redirect(new moodle_url('view.php',
87                                 array('id'=>$this->assignment->get_course_module()->id,
88                                       'action'=>'grading')));
89             return;
90         }
91         $file = reset($files);
93         $csvdata = $file->get_content();
95         if ($csvdata) {
96             $gradeimporter->parsecsv($csvdata);
97         }
98         if (!$gradeimporter->init()) {
99             $thisurl = new moodle_url('/mod/assign/view.php', array('action'=>'viewpluginpage',
100                                                                      'pluginsubtype'=>'assignfeedback',
101                                                                      'plugin'=>'offline',
102                                                                      'pluginaction'=>'uploadgrades',
103                                                                      'id'=>$assignment->get_course_module()->id));
104             print_error('invalidgradeimport', 'assignfeedback_offline', $thisurl);
105             return;
106         }
107         // Does this assignment use a scale?
108         $scaleoptions = null;
109         if ($this->assignment->get_instance()->grade < 0) {
110             $scale = $DB->get_record('scale', array('id'=>-($this->assignment->get_instance()->grade)));
111             if ($scale) {
112                 $scaleoptions = explode(',', $scale->scale);
113             }
114         }
115         // We may need to upgrade the gradebook comments after this update.
116         $adminconfig = $this->assignment->get_admin_config();
117         $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook;
119         $updatecount = 0;
120         while ($record = $gradeimporter->next()) {
121             $user = $record->user;
122             $modified = $record->modified;
123             $userdesc = fullname($user);
124             $usergrade = $this->assignment->get_user_grade($user->id, false);
126             if (!empty($scaleoptions)) {
127                 // This is a scale - we need to convert any grades to indexes in the scale.
128                 $scaleindex = array_search($record->grade, $scaleoptions);
129                 if ($scaleindex !== false) {
130                     $record->grade = $scaleindex + 1;
131                 } else {
132                     $record->grade = '';
133                 }
134             }
136             // Note: Do not count the seconds when comparing modified dates.
137             $skip = false;
138             $stalemodificationdate = ($usergrade && $usergrade->timemodified > ($modified + 60));
140             if ($usergrade && $usergrade->grade == $record->grade) {
141                 // Skip - grade not modified.
142                 $skip = true;
143             } else if (!isset($record->grade) || $record->grade === '' || $record->grade < 0) {
144                 // Skip - grade has no value.
145                 $skip = true;
146             } else if (!$ignoremodified && $stalemodificationdate) {
147                 // Skip - grade has been modified.
148                 $skip = true;
149             } else if ($this->assignment->grading_disabled($record->user->id)) {
150                 // Skip grade is locked.
151                 $skip = true;
152             }
154             if (!$skip) {
155                 $grade = $this->assignment->get_user_grade($record->user->id, true);
157                 $grade->grade = $record->grade;
158                 $grade->grader = $USER->id;
159                 if ($this->assignment->update_grade($grade)) {
160                     $this->assignment->add_to_log('grade submission', $this->assignment->format_grade_for_log($grade));
161                     $updatecount += 1;
162                 }
163             }
165             if ($ignoremodified || !$stalemodificationdate) {
166                 foreach ($record->feedback as $feedback) {
167                     $plugin = $feedback['plugin'];
168                     $field = $feedback['field'];
169                     $newvalue = $feedback['value'];
170                     $description = $feedback['description'];
171                     $oldvalue = '';
172                     if ($usergrade) {
173                         $oldvalue = $plugin->get_editor_text($field, $usergrade->id);
174                         if (empty($oldvalue)) {
175                             $oldvalue = '';
176                         }
177                     }
178                     if ($newvalue != $oldvalue) {
179                         $updatecount += 1;
180                         $grade = $this->assignment->get_user_grade($record->user->id, true);
181                         if ($plugin->set_editor_text($field, $newvalue, $grade->id)) {
182                             $logdesc = get_string('feedbackupdate', 'assignfeedback_offline',
183                                                   array('field'=>$description,
184                                                         'student'=>$userdesc,
185                                                         'text'=>$newvalue));
187                             $this->assignment->add_to_log('save grading feedback', $logdesc);
188                         }
190                         // If this is the gradebook comments plugin - post an update to the gradebook.
191                         if (($plugin->get_subtype() . '_' . $plugin->get_type()) == $gradebookplugin) {
192                             $grade->feedbacktext = $plugin->text_for_gradebook($grade);
193                             $grade->feedbackformat = $plugin->format_for_gradebook($grade);
194                             $this->assignment->update_grade($grade);
195                         }
196                     }
197                 }
198             }
199         }
200         $gradeimporter->close(true);
202         $renderer = $this->assignment->get_renderer();
203         $o = '';
205         $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
206                                                   $this->assignment->get_context(),
207                                                   false,
208                                                   $this->assignment->get_course_module()->id,
209                                                   get_string('importgrades', 'assignfeedback_offline')));
210         $o .= $renderer->box(get_string('updatedgrades', 'assignfeedback_offline', $updatecount));
211         $url = new moodle_url('view.php',
212                               array('id'=>$this->assignment->get_course_module()->id,
213                                     'action'=>'grading'));
214         $o .= $renderer->continue_button($url);
215         $o .= $renderer->render_footer();
216         return $o;
217     }
219     /**
220      * Display upload grades form
221      *
222      * @return string The response html
223      */
224     public function upload_grades() {
225         global $CFG, $USER;
227         require_capability('mod/assign:grade', $this->assignment->get_context());
228         require_once($CFG->dirroot . '/mod/assign/feedback/offline/uploadgradesform.php');
229         require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradesform.php');
230         require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradeslib.php');
231         require_once($CFG->libdir . '/csvlib.class.php');
233         $mform = new assignfeedback_offline_upload_grades_form(null,
234                                                               array('context'=>$this->assignment->get_context(),
235                                                                     'cm'=>$this->assignment->get_course_module()->id));
237         $o = '';
239         $confirm = optional_param('confirm', 0, PARAM_BOOL);
240         $renderer = $this->assignment->get_renderer();
242         if ($mform->is_cancelled()) {
243             redirect(new moodle_url('view.php',
244                                     array('id'=>$this->assignment->get_course_module()->id,
245                                           'action'=>'grading')));
246             return;
247         } else if (($data = $mform->get_data()) &&
248                    ($csvdata = $mform->get_file_content('gradesfile'))) {
250             $importid = csv_import_reader::get_new_iid('assignfeedback_offline');
251             $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
252             // File exists and was valid.
253             $ignoremodified = !empty($data->ignoremodified);
255             $draftid = $data->gradesfile;
257             // Preview import.
259             $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
260                                                                        'csvdata'=>$csvdata,
261                                                                        'ignoremodified'=>$ignoremodified,
262                                                                        'gradeimporter'=>$gradeimporter,
263                                                                        'draftid'=>$draftid));
265             $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
266                                                             $this->assignment->get_context(),
267                                                             false,
268                                                             $this->assignment->get_course_module()->id,
269                                                             get_string('confirmimport', 'assignfeedback_offline')));
270             $o .= $renderer->render(new assign_form('confirmimport', $mform));
271             $o .= $renderer->render_footer();
272         } else if ($confirm) {
274             $importid = optional_param('importid', 0, PARAM_INT);
275             $draftid = optional_param('draftid', 0, PARAM_INT);
276             $ignoremodified = optional_param('ignoremodified', 0, PARAM_BOOL);
277             $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
278             $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
279                                                                        'csvdata'=>'',
280                                                                        'ignoremodified'=>$ignoremodified,
281                                                                        'gradeimporter'=>$gradeimporter,
282                                                                        'draftid'=>$draftid));
283             if ($mform->is_cancelled()) {
284                 redirect(new moodle_url('view.php',
285                                         array('id'=>$this->assignment->get_course_module()->id,
286                                               'action'=>'grading')));
287                 return;
288             }
290             $o .= $this->process_import_grades($draftid, $importid, $ignoremodified);
291         } else {
293             $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
294                                                             $this->assignment->get_context(),
295                                                             false,
296                                                             $this->assignment->get_course_module()->id,
297                                                             get_string('uploadgrades', 'assignfeedback_offline')));
298             $o .= $renderer->render(new assign_form('batchuploadfiles', $mform));
299             $o .= $renderer->render_footer();
300         }
302         return $o;
303     }
305     /**
306      * Download a marking worksheet
307      *
308      * @return string The response html
309      */
310     public function download_grades() {
311         global $CFG;
313         require_capability('mod/assign:grade', $this->assignment->get_context());
314         require_once($CFG->dirroot . '/mod/assign/gradingtable.php');
316         $groupmode = groups_get_activity_groupmode($this->assignment->get_course_module());
317         // All users.
318         $groupid = 0;
319         $groupname = '';
320         if ($groupmode) {
321             $groupid = groups_get_activity_group($this->assignment->get_course_module(), true);
322             $groupname = groups_get_group_name($groupid) . '-';
323         }
324         $filename = clean_filename(get_string('offlinegradingworksheet', 'assignfeedback_offline') . '-' .
325                                    $this->assignment->get_course()->shortname . '-' .
326                                    $this->assignment->get_instance()->name . '-' .
327                                    $groupname .
328                                    $this->assignment->get_course_module()->id);
330         $table = new assign_grading_table($this->assignment, 0, '', 0, false, $filename);
332         $table->out(0, false);
333         return;
334     }
336     /**
337      * Print a sub page in this plugin
338      *
339      * @param string $action - The plugin action
340      * @return string The response html
341      */
342     public function view_page($action) {
343         if ($action == 'downloadgrades') {
344             return $this->download_grades();
345         } else if ($action == 'uploadgrades') {
346             return $this->upload_grades();
347         }
349         return '';
350     }
352     /**
353      * Return a list of the grading actions performed by this plugin
354      * This plugin supports upload zip
355      *
356      * @return array The list of grading actions
357      */
358     public function get_grading_actions() {
359         return array('uploadgrades'=>get_string('uploadgrades', 'assignfeedback_offline'),
360                     'downloadgrades'=>get_string('downloadgrades', 'assignfeedback_offline'));
361     }
363     /**
364      * Override the default is_enabled to disable this plugin if advanced grading is active
365      *
366      * @return bool
367      */
368     public function is_enabled() {
369         $gradingmanager = get_grading_manager($this->assignment->get_context(), 'mod_assign', 'submissions');
370         $controller = $gradingmanager->get_active_controller();
371         $active = !empty($controller);
373         if ($active) {
374             return false;
375         }
376         return parent::is_enabled();
377     }
379     /**
380      * Do not show this plugin in the grading table or on the front page
381      *
382      * @return bool
383      */
384     public function has_user_summary() {
385         return false;
386     }