MDL-36712: Assignment - Do not always send feedback available emails when grade is...
[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->notify_grade_modified($grade);
161                     $this->assignment->add_to_log('grade submission', $this->assignment->format_grade_for_log($grade));
162                     $updatecount += 1;
163                 }
164             }
166             if ($ignoremodified || !$stalemodificationdate) {
167                 foreach ($record->feedback as $feedback) {
168                     $plugin = $feedback['plugin'];
169                     $field = $feedback['field'];
170                     $newvalue = $feedback['value'];
171                     $description = $feedback['description'];
172                     $oldvalue = '';
173                     if ($usergrade) {
174                         $oldvalue = $plugin->get_editor_text($field, $usergrade->id);
175                         if (empty($oldvalue)) {
176                             $oldvalue = '';
177                         }
178                     }
179                     if ($newvalue != $oldvalue) {
180                         $updatecount += 1;
181                         $grade = $this->assignment->get_user_grade($record->user->id, true);
182                         $this->assignment->notify_grade_modified($grade);
183                         if ($plugin->set_editor_text($field, $newvalue, $grade->id)) {
184                             $logdesc = get_string('feedbackupdate', 'assignfeedback_offline',
185                                                   array('field'=>$description,
186                                                         'student'=>$userdesc,
187                                                         'text'=>$newvalue));
189                             $this->assignment->add_to_log('save grading feedback', $logdesc);
190                         }
192                         // If this is the gradebook comments plugin - post an update to the gradebook.
193                         if (($plugin->get_subtype() . '_' . $plugin->get_type()) == $gradebookplugin) {
194                             $grade->feedbacktext = $plugin->text_for_gradebook($grade);
195                             $grade->feedbackformat = $plugin->format_for_gradebook($grade);
196                             $this->assignment->update_grade($grade);
197                         }
198                     }
199                 }
200             }
201         }
202         $gradeimporter->close(true);
204         $renderer = $this->assignment->get_renderer();
205         $o = '';
207         $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
208                                                   $this->assignment->get_context(),
209                                                   false,
210                                                   $this->assignment->get_course_module()->id,
211                                                   get_string('importgrades', 'assignfeedback_offline')));
212         $o .= $renderer->box(get_string('updatedgrades', 'assignfeedback_offline', $updatecount));
213         $url = new moodle_url('view.php',
214                               array('id'=>$this->assignment->get_course_module()->id,
215                                     'action'=>'grading'));
216         $o .= $renderer->continue_button($url);
217         $o .= $renderer->render_footer();
218         return $o;
219     }
221     /**
222      * Display upload grades form
223      *
224      * @return string The response html
225      */
226     public function upload_grades() {
227         global $CFG, $USER;
229         require_capability('mod/assign:grade', $this->assignment->get_context());
230         require_once($CFG->dirroot . '/mod/assign/feedback/offline/uploadgradesform.php');
231         require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradesform.php');
232         require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradeslib.php');
233         require_once($CFG->libdir . '/csvlib.class.php');
235         $mform = new assignfeedback_offline_upload_grades_form(null,
236                                                               array('context'=>$this->assignment->get_context(),
237                                                                     'cm'=>$this->assignment->get_course_module()->id));
239         $o = '';
241         $confirm = optional_param('confirm', 0, PARAM_BOOL);
242         $renderer = $this->assignment->get_renderer();
244         if ($mform->is_cancelled()) {
245             redirect(new moodle_url('view.php',
246                                     array('id'=>$this->assignment->get_course_module()->id,
247                                           'action'=>'grading')));
248             return;
249         } else if (($data = $mform->get_data()) &&
250                    ($csvdata = $mform->get_file_content('gradesfile'))) {
252             $importid = csv_import_reader::get_new_iid('assignfeedback_offline');
253             $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
254             // File exists and was valid.
255             $ignoremodified = !empty($data->ignoremodified);
257             $draftid = $data->gradesfile;
259             // Preview import.
261             $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
262                                                                        'csvdata'=>$csvdata,
263                                                                        'ignoremodified'=>$ignoremodified,
264                                                                        'gradeimporter'=>$gradeimporter,
265                                                                        'draftid'=>$draftid));
267             $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
268                                                             $this->assignment->get_context(),
269                                                             false,
270                                                             $this->assignment->get_course_module()->id,
271                                                             get_string('confirmimport', 'assignfeedback_offline')));
272             $o .= $renderer->render(new assign_form('confirmimport', $mform));
273             $o .= $renderer->render_footer();
274         } else if ($confirm) {
276             $importid = optional_param('importid', 0, PARAM_INT);
277             $draftid = optional_param('draftid', 0, PARAM_INT);
278             $ignoremodified = optional_param('ignoremodified', 0, PARAM_BOOL);
279             $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
280             $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
281                                                                        'csvdata'=>'',
282                                                                        'ignoremodified'=>$ignoremodified,
283                                                                        'gradeimporter'=>$gradeimporter,
284                                                                        'draftid'=>$draftid));
285             if ($mform->is_cancelled()) {
286                 redirect(new moodle_url('view.php',
287                                         array('id'=>$this->assignment->get_course_module()->id,
288                                               'action'=>'grading')));
289                 return;
290             }
292             $o .= $this->process_import_grades($draftid, $importid, $ignoremodified);
293         } else {
295             $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
296                                                             $this->assignment->get_context(),
297                                                             false,
298                                                             $this->assignment->get_course_module()->id,
299                                                             get_string('uploadgrades', 'assignfeedback_offline')));
300             $o .= $renderer->render(new assign_form('batchuploadfiles', $mform));
301             $o .= $renderer->render_footer();
302         }
304         return $o;
305     }
307     /**
308      * Download a marking worksheet
309      *
310      * @return string The response html
311      */
312     public function download_grades() {
313         global $CFG;
315         require_capability('mod/assign:grade', $this->assignment->get_context());
316         require_once($CFG->dirroot . '/mod/assign/gradingtable.php');
318         $groupmode = groups_get_activity_groupmode($this->assignment->get_course_module());
319         // All users.
320         $groupid = 0;
321         $groupname = '';
322         if ($groupmode) {
323             $groupid = groups_get_activity_group($this->assignment->get_course_module(), true);
324             $groupname = groups_get_group_name($groupid) . '-';
325         }
326         $filename = clean_filename(get_string('offlinegradingworksheet', 'assignfeedback_offline') . '-' .
327                                    $this->assignment->get_course()->shortname . '-' .
328                                    $this->assignment->get_instance()->name . '-' .
329                                    $groupname .
330                                    $this->assignment->get_course_module()->id);
332         $table = new assign_grading_table($this->assignment, 0, '', 0, false, $filename);
334         $table->out(0, false);
335         return;
336     }
338     /**
339      * Print a sub page in this plugin
340      *
341      * @param string $action - The plugin action
342      * @return string The response html
343      */
344     public function view_page($action) {
345         if ($action == 'downloadgrades') {
346             return $this->download_grades();
347         } else if ($action == 'uploadgrades') {
348             return $this->upload_grades();
349         }
351         return '';
352     }
354     /**
355      * Return a list of the grading actions performed by this plugin
356      * This plugin supports upload zip
357      *
358      * @return array The list of grading actions
359      */
360     public function get_grading_actions() {
361         return array('uploadgrades'=>get_string('uploadgrades', 'assignfeedback_offline'),
362                     'downloadgrades'=>get_string('downloadgrades', 'assignfeedback_offline'));
363     }
365     /**
366      * Override the default is_enabled to disable this plugin if advanced grading is active
367      *
368      * @return bool
369      */
370     public function is_enabled() {
371         $gradingmanager = get_grading_manager($this->assignment->get_context(), 'mod_assign', 'submissions');
372         $controller = $gradingmanager->get_active_controller();
373         $active = !empty($controller);
375         if ($active) {
376             return false;
377         }
378         return parent::is_enabled();
379     }
381     /**
382      * Do not show this plugin in the grading table or on the front page
383      *
384      * @return bool
385      */
386     public function has_user_summary() {
387         return false;
388     }