MDL-41101 mod_assign: replaced 'grade submission' add_to_log call with an event
[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     /** @var boolean|null $enabledcache Cached lookup of the is_enabled function */
40     private $enabledcache = null;
42     /**
43      * Get the name of the file feedback plugin
44      * @return string
45      */
46     public function get_name() {
47         return get_string('pluginname', 'assignfeedback_offline');
48     }
50     /**
51      * Get form elements for grading form
52      *
53      * @param stdClass $grade
54      * @param MoodleQuickForm $mform
55      * @param stdClass $data
56      * @return bool true if elements were added to the form
57      */
58     public function get_form_elements($grade, MoodleQuickForm $mform, stdClass $data) {
59         return false;
60     }
62     /**
63      * Return true if there are no feedback files
64      * @param stdClass $grade
65      */
66     public function is_empty(stdClass $grade) {
67         return true;
68     }
70     /**
71      * Loop through uploaded grades and update the grades for this assignment
72      *
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
77      */
78     public function process_import_grades($draftid, $importid, $ignoremodified) {
79         global $USER, $DB;
81         require_sesskey();
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')));
92             return;
93         }
94         $file = reset($files);
96         $csvdata = $file->get_content();
98         if ($csvdata) {
99             $gradeimporter->parsecsv($csvdata);
100         }
101         if (!$gradeimporter->init()) {
102             $thisurl = new moodle_url('/mod/assign/view.php', array('action'=>'viewpluginpage',
103                                                                      'pluginsubtype'=>'assignfeedback',
104                                                                      'plugin'=>'offline',
105                                                                      'pluginaction'=>'uploadgrades',
106                                                                      'id'=>$assignment->get_course_module()->id));
107             print_error('invalidgradeimport', 'assignfeedback_offline', $thisurl);
108             return;
109         }
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);
115             }
116         }
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;
121         $updatecount = 0;
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;
133                 } else {
134                     $record->grade = '';
135                 }
136             } else {
137                 $record->grade = unformat_float($record->grade);
138             }
140             // Note: Do not count the seconds when comparing modified dates.
141             $skip = false;
142             $stalemodificationdate = ($usergrade && $usergrade->timemodified > ($modified + 60));
144             if ($usergrade && $usergrade->grade == $record->grade) {
145                 // Skip - grade not modified.
146                 $skip = true;
147             } else if (!isset($record->grade) || $record->grade === '' || $record->grade < 0) {
148                 // Skip - grade has no value.
149                 $skip = true;
150             } else if (!$ignoremodified && $stalemodificationdate) {
151                 // Skip - grade has been modified.
152                 $skip = true;
153             } else if ($this->assignment->grading_disabled($record->user->id)) {
154                 // Skip grade is locked.
155                 $skip = true;
156             } else if (($this->assignment->get_instance()->grade > -1) &&
157                       (($record->grade < 0) || ($record->grade > $this->assignment->get_instance()->grade))) {
158                 // Out of range.
159                 $skip = true;
160             }
162             if (!$skip) {
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                     $updatecount += 1;
170                 }
171             }
173             if ($ignoremodified || !$stalemodificationdate) {
174                 foreach ($record->feedback as $feedback) {
175                     $plugin = $feedback['plugin'];
176                     $field = $feedback['field'];
177                     $newvalue = $feedback['value'];
178                     $description = $feedback['description'];
179                     $oldvalue = '';
180                     if ($usergrade) {
181                         $oldvalue = $plugin->get_editor_text($field, $usergrade->id);
182                         if (empty($oldvalue)) {
183                             $oldvalue = '';
184                         }
185                     }
186                     if ($newvalue != $oldvalue) {
187                         $updatecount += 1;
188                         $grade = $this->assignment->get_user_grade($record->user->id, true);
189                         $this->assignment->notify_grade_modified($grade);
190                         if ($plugin->set_editor_text($field, $newvalue, $grade->id)) {
191                             $logdesc = get_string('feedbackupdate', 'assignfeedback_offline',
192                                                   array('field'=>$description,
193                                                         'student'=>$userdesc,
194                                                         'text'=>$newvalue));
196                             $this->assignment->add_to_log('save grading feedback', $logdesc);
197                         }
199                         // If this is the gradebook comments plugin - post an update to the gradebook.
200                         if (($plugin->get_subtype() . '_' . $plugin->get_type()) == $gradebookplugin) {
201                             $grade->feedbacktext = $plugin->text_for_gradebook($grade);
202                             $grade->feedbackformat = $plugin->format_for_gradebook($grade);
203                             $this->assignment->update_grade($grade);
204                         }
205                     }
206                 }
207             }
208         }
209         $gradeimporter->close(true);
211         $renderer = $this->assignment->get_renderer();
212         $o = '';
214         $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
215                                                   $this->assignment->get_context(),
216                                                   false,
217                                                   $this->assignment->get_course_module()->id,
218                                                   get_string('importgrades', 'assignfeedback_offline')));
219         $o .= $renderer->box(get_string('updatedgrades', 'assignfeedback_offline', $updatecount));
220         $url = new moodle_url('view.php',
221                               array('id'=>$this->assignment->get_course_module()->id,
222                                     'action'=>'grading'));
223         $o .= $renderer->continue_button($url);
224         $o .= $renderer->render_footer();
225         return $o;
226     }
228     /**
229      * Display upload grades form
230      *
231      * @return string The response html
232      */
233     public function upload_grades() {
234         global $CFG, $USER;
236         require_capability('mod/assign:grade', $this->assignment->get_context());
237         require_once($CFG->dirroot . '/mod/assign/feedback/offline/uploadgradesform.php');
238         require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradesform.php');
239         require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradeslib.php');
240         require_once($CFG->libdir . '/csvlib.class.php');
242         $mform = new assignfeedback_offline_upload_grades_form(null,
243                                                               array('context'=>$this->assignment->get_context(),
244                                                                     'cm'=>$this->assignment->get_course_module()->id));
246         $o = '';
248         $confirm = optional_param('confirm', 0, PARAM_BOOL);
249         $renderer = $this->assignment->get_renderer();
251         if ($mform->is_cancelled()) {
252             redirect(new moodle_url('view.php',
253                                     array('id'=>$this->assignment->get_course_module()->id,
254                                           'action'=>'grading')));
255             return;
256         } else if (($data = $mform->get_data()) &&
257                    ($csvdata = $mform->get_file_content('gradesfile'))) {
259             $importid = csv_import_reader::get_new_iid('assignfeedback_offline');
260             $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
261             // File exists and was valid.
262             $ignoremodified = !empty($data->ignoremodified);
264             $draftid = $data->gradesfile;
266             // Preview import.
268             $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
269                                                                        'csvdata'=>$csvdata,
270                                                                        'ignoremodified'=>$ignoremodified,
271                                                                        'gradeimporter'=>$gradeimporter,
272                                                                        'draftid'=>$draftid));
274             $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
275                                                             $this->assignment->get_context(),
276                                                             false,
277                                                             $this->assignment->get_course_module()->id,
278                                                             get_string('confirmimport', 'assignfeedback_offline')));
279             $o .= $renderer->render(new assign_form('confirmimport', $mform));
280             $o .= $renderer->render_footer();
281         } else if ($confirm) {
283             $importid = optional_param('importid', 0, PARAM_INT);
284             $draftid = optional_param('draftid', 0, PARAM_INT);
285             $ignoremodified = optional_param('ignoremodified', 0, PARAM_BOOL);
286             $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
287             $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
288                                                                        'csvdata'=>'',
289                                                                        'ignoremodified'=>$ignoremodified,
290                                                                        'gradeimporter'=>$gradeimporter,
291                                                                        'draftid'=>$draftid));
292             if ($mform->is_cancelled()) {
293                 redirect(new moodle_url('view.php',
294                                         array('id'=>$this->assignment->get_course_module()->id,
295                                               'action'=>'grading')));
296                 return;
297             }
299             $o .= $this->process_import_grades($draftid, $importid, $ignoremodified);
300         } else {
302             $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
303                                                             $this->assignment->get_context(),
304                                                             false,
305                                                             $this->assignment->get_course_module()->id,
306                                                             get_string('uploadgrades', 'assignfeedback_offline')));
307             $o .= $renderer->render(new assign_form('batchuploadfiles', $mform));
308             $o .= $renderer->render_footer();
309         }
311         return $o;
312     }
314     /**
315      * Download a marking worksheet
316      *
317      * @return string The response html
318      */
319     public function download_grades() {
320         global $CFG;
322         require_capability('mod/assign:grade', $this->assignment->get_context());
323         require_once($CFG->dirroot . '/mod/assign/gradingtable.php');
325         $groupmode = groups_get_activity_groupmode($this->assignment->get_course_module());
326         // All users.
327         $groupid = 0;
328         $groupname = '';
329         if ($groupmode) {
330             $groupid = groups_get_activity_group($this->assignment->get_course_module(), true);
331             $groupname = groups_get_group_name($groupid) . '-';
332         }
333         $filename = clean_filename(get_string('offlinegradingworksheet', 'assignfeedback_offline') . '-' .
334                                    $this->assignment->get_course()->shortname . '-' .
335                                    $this->assignment->get_instance()->name . '-' .
336                                    $groupname .
337                                    $this->assignment->get_course_module()->id);
339         $table = new assign_grading_table($this->assignment, 0, '', 0, false, $filename);
341         $table->out(0, false);
342         return;
343     }
345     /**
346      * Print a sub page in this plugin
347      *
348      * @param string $action - The plugin action
349      * @return string The response html
350      */
351     public function view_page($action) {
352         if ($action == 'downloadgrades') {
353             return $this->download_grades();
354         } else if ($action == 'uploadgrades') {
355             return $this->upload_grades();
356         }
358         return '';
359     }
361     /**
362      * Return a list of the grading actions performed by this plugin
363      * This plugin supports upload zip
364      *
365      * @return array The list of grading actions
366      */
367     public function get_grading_actions() {
368         return array('uploadgrades'=>get_string('uploadgrades', 'assignfeedback_offline'),
369                     'downloadgrades'=>get_string('downloadgrades', 'assignfeedback_offline'));
370     }
372     /**
373      * Override the default is_enabled to disable this plugin if advanced grading is active
374      *
375      * @return bool
376      */
377     public function is_enabled() {
378         if ($this->enabledcache === null) {
379             $gradingmanager = get_grading_manager($this->assignment->get_context(), 'mod_assign', 'submissions');
380             $controller = $gradingmanager->get_active_controller();
381             $active = !empty($controller);
383             if ($active) {
384                 $enabledcache = false;
385             } else {
386                 $enabledcache = parent::is_enabled();
387             }
388         }
389         return $this->enabledcache;
390     }
392     /**
393      * Do not show this plugin in the grading table or on the front page
394      *
395      * @return bool
396      */
397     public function has_user_summary() {
398         return false;
399     }