e5ab1111ba998ff59c194c5f9a5f74388aaf0a65
[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      * This plugin does not save through the normal interface so this returns false.
72      *
73      * @param stdClass $grade The grade.
74      * @param stdClass $data Form data from the feedback form.
75      * @return boolean - False
76      */
77     public function is_feedback_modified(stdClass $grade, stdClass $data) {
78         return false;
79     }
81     /**
82      * Loop through uploaded grades and update the grades for this assignment
83      *
84      * @param int $draftid - The unique draft item id for this import
85      * @param int $importid - The unique import ID for this csv import operation
86      * @param bool $ignoremodified - Ignore the last modified date when checking fields
87      * @return string - The html response
88      */
89     public function process_import_grades($draftid, $importid, $ignoremodified) {
90         global $USER, $DB;
92         require_sesskey();
93         require_capability('mod/assign:grade', $this->assignment->get_context());
95         $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
97         $context = context_user::instance($USER->id);
98         $fs = get_file_storage();
99         if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
100             redirect(new moodle_url('view.php',
101                                 array('id'=>$this->assignment->get_course_module()->id,
102                                       'action'=>'grading')));
103             return;
104         }
105         $file = reset($files);
107         $csvdata = $file->get_content();
109         if ($csvdata) {
110             $gradeimporter->parsecsv($csvdata);
111         }
112         if (!$gradeimporter->init()) {
113             $thisurl = new moodle_url('/mod/assign/view.php', array('action'=>'viewpluginpage',
114                                                                      'pluginsubtype'=>'assignfeedback',
115                                                                      'plugin'=>'offline',
116                                                                      'pluginaction'=>'uploadgrades',
117                                                                      'id'=>$assignment->get_course_module()->id));
118             print_error('invalidgradeimport', 'assignfeedback_offline', $thisurl);
119             return;
120         }
121         // Does this assignment use a scale?
122         $scaleoptions = null;
123         if ($this->assignment->get_instance()->grade < 0) {
124             if ($scale = $DB->get_record('scale', array('id'=>-($this->assignment->get_instance()->grade)))) {
125                 $scaleoptions = make_menu_from_list($scale->scale);
126             }
127         }
128         // We may need to upgrade the gradebook comments after this update.
129         $adminconfig = $this->assignment->get_admin_config();
130         $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook;
132         $updatecount = 0;
133         while ($record = $gradeimporter->next()) {
134             $user = $record->user;
135             $modified = $record->modified;
136             $userdesc = fullname($user);
137             $usergrade = $this->assignment->get_user_grade($user->id, false);
139             if (!empty($scaleoptions)) {
140                 // This is a scale - we need to convert any grades to indexes in the scale.
141                 $scaleindex = array_search($record->grade, $scaleoptions);
142                 if ($scaleindex !== false) {
143                     $record->grade = $scaleindex;
144                 } else {
145                     $record->grade = '';
146                 }
147             } else {
148                 $record->grade = unformat_float($record->grade);
149             }
151             // Note: Do not count the seconds when comparing modified dates.
152             $skip = false;
153             $stalemodificationdate = ($usergrade && $usergrade->timemodified > ($modified + 60));
155             if ($usergrade && $usergrade->grade == $record->grade) {
156                 // Skip - grade not modified.
157                 $skip = true;
158             } else if (!isset($record->grade) || $record->grade === '' || $record->grade < 0) {
159                 // Skip - grade has no value.
160                 $skip = true;
161             } else if (!$ignoremodified && $stalemodificationdate) {
162                 // Skip - grade has been modified.
163                 $skip = true;
164             } else if ($this->assignment->grading_disabled($record->user->id)) {
165                 // Skip grade is locked.
166                 $skip = true;
167             } else if (($this->assignment->get_instance()->grade > -1) &&
168                       (($record->grade < 0) || ($record->grade > $this->assignment->get_instance()->grade))) {
169                 // Out of range.
170                 $skip = true;
171             }
173             if (!$skip) {
174                 $grade = $this->assignment->get_user_grade($record->user->id, true);
176                 $grade->grade = $record->grade;
177                 $grade->grader = $USER->id;
178                 if ($this->assignment->update_grade($grade)) {
179                     $this->assignment->notify_grade_modified($grade);
180                     $updatecount += 1;
181                 }
182             }
184             if ($ignoremodified || !$stalemodificationdate) {
185                 foreach ($record->feedback as $feedback) {
186                     $plugin = $feedback['plugin'];
187                     $field = $feedback['field'];
188                     $newvalue = $feedback['value'];
189                     $description = $feedback['description'];
190                     $oldvalue = '';
191                     if ($usergrade) {
192                         $oldvalue = $plugin->get_editor_text($field, $usergrade->id);
193                         if (empty($oldvalue)) {
194                             $oldvalue = '';
195                         }
196                     }
197                     if ($newvalue != $oldvalue) {
198                         $updatecount += 1;
199                         $grade = $this->assignment->get_user_grade($record->user->id, true);
200                         $this->assignment->notify_grade_modified($grade);
201                         $plugin->set_editor_text($field, $newvalue, $grade->id);
203                         // If this is the gradebook comments plugin - post an update to the gradebook.
204                         if (($plugin->get_subtype() . '_' . $plugin->get_type()) == $gradebookplugin) {
205                             $grade->feedbacktext = $plugin->text_for_gradebook($grade);
206                             $grade->feedbackformat = $plugin->format_for_gradebook($grade);
207                             $this->assignment->update_grade($grade);
208                         }
209                     }
210                 }
211             }
212         }
213         $gradeimporter->close(true);
215         $renderer = $this->assignment->get_renderer();
216         $o = '';
218         $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
219                                                   $this->assignment->get_context(),
220                                                   false,
221                                                   $this->assignment->get_course_module()->id,
222                                                   get_string('importgrades', 'assignfeedback_offline')));
223         $o .= $renderer->box(get_string('updatedgrades', 'assignfeedback_offline', $updatecount));
224         $url = new moodle_url('view.php',
225                               array('id'=>$this->assignment->get_course_module()->id,
226                                     'action'=>'grading'));
227         $o .= $renderer->continue_button($url);
228         $o .= $renderer->render_footer();
229         return $o;
230     }
232     /**
233      * Display upload grades form
234      *
235      * @return string The response html
236      */
237     public function upload_grades() {
238         global $CFG, $USER;
240         require_capability('mod/assign:grade', $this->assignment->get_context());
241         require_once($CFG->dirroot . '/mod/assign/feedback/offline/uploadgradesform.php');
242         require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradesform.php');
243         require_once($CFG->dirroot . '/mod/assign/feedback/offline/importgradeslib.php');
244         require_once($CFG->libdir . '/csvlib.class.php');
246         $mform = new assignfeedback_offline_upload_grades_form(null,
247                                                               array('context'=>$this->assignment->get_context(),
248                                                                     'cm'=>$this->assignment->get_course_module()->id));
250         $o = '';
252         $confirm = optional_param('confirm', 0, PARAM_BOOL);
253         $renderer = $this->assignment->get_renderer();
255         if ($mform->is_cancelled()) {
256             redirect(new moodle_url('view.php',
257                                     array('id'=>$this->assignment->get_course_module()->id,
258                                           'action'=>'grading')));
259             return;
260         } else if (($data = $mform->get_data()) &&
261                    ($csvdata = $mform->get_file_content('gradesfile'))) {
263             $importid = csv_import_reader::get_new_iid('assignfeedback_offline');
264             $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
265             // File exists and was valid.
266             $ignoremodified = !empty($data->ignoremodified);
268             $draftid = $data->gradesfile;
270             // Preview import.
272             $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
273                                                                        'csvdata'=>$csvdata,
274                                                                        'ignoremodified'=>$ignoremodified,
275                                                                        'gradeimporter'=>$gradeimporter,
276                                                                        'draftid'=>$draftid));
278             $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
279                                                             $this->assignment->get_context(),
280                                                             false,
281                                                             $this->assignment->get_course_module()->id,
282                                                             get_string('confirmimport', 'assignfeedback_offline')));
283             $o .= $renderer->render(new assign_form('confirmimport', $mform));
284             $o .= $renderer->render_footer();
285         } else if ($confirm) {
287             $importid = optional_param('importid', 0, PARAM_INT);
288             $draftid = optional_param('draftid', 0, PARAM_INT);
289             $ignoremodified = optional_param('ignoremodified', 0, PARAM_BOOL);
290             $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
291             $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
292                                                                        'csvdata'=>'',
293                                                                        'ignoremodified'=>$ignoremodified,
294                                                                        'gradeimporter'=>$gradeimporter,
295                                                                        'draftid'=>$draftid));
296             if ($mform->is_cancelled()) {
297                 redirect(new moodle_url('view.php',
298                                         array('id'=>$this->assignment->get_course_module()->id,
299                                               'action'=>'grading')));
300                 return;
301             }
303             $o .= $this->process_import_grades($draftid, $importid, $ignoremodified);
304         } else {
306             $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
307                                                             $this->assignment->get_context(),
308                                                             false,
309                                                             $this->assignment->get_course_module()->id,
310                                                             get_string('uploadgrades', 'assignfeedback_offline')));
311             $o .= $renderer->render(new assign_form('batchuploadfiles', $mform));
312             $o .= $renderer->render_footer();
313         }
315         return $o;
316     }
318     /**
319      * Download a marking worksheet
320      *
321      * @return string The response html
322      */
323     public function download_grades() {
324         global $CFG;
326         require_capability('mod/assign:grade', $this->assignment->get_context());
327         require_once($CFG->dirroot . '/mod/assign/gradingtable.php');
329         $groupmode = groups_get_activity_groupmode($this->assignment->get_course_module());
330         // All users.
331         $groupid = 0;
332         $groupname = '';
333         if ($groupmode) {
334             $groupid = groups_get_activity_group($this->assignment->get_course_module(), true);
335             $groupname = groups_get_group_name($groupid) . '-';
336         }
337         $filename = clean_filename(get_string('offlinegradingworksheet', 'assignfeedback_offline') . '-' .
338                                    $this->assignment->get_course()->shortname . '-' .
339                                    $this->assignment->get_instance()->name . '-' .
340                                    $groupname .
341                                    $this->assignment->get_course_module()->id);
343         $table = new assign_grading_table($this->assignment, 0, '', 0, false, $filename);
345         $table->out(0, false);
346         return;
347     }
349     /**
350      * Print a sub page in this plugin
351      *
352      * @param string $action - The plugin action
353      * @return string The response html
354      */
355     public function view_page($action) {
356         if ($action == 'downloadgrades') {
357             return $this->download_grades();
358         } else if ($action == 'uploadgrades') {
359             return $this->upload_grades();
360         }
362         return '';
363     }
365     /**
366      * Return a list of the grading actions performed by this plugin
367      * This plugin supports upload zip
368      *
369      * @return array The list of grading actions
370      */
371     public function get_grading_actions() {
372         return array('uploadgrades'=>get_string('uploadgrades', 'assignfeedback_offline'),
373                     'downloadgrades'=>get_string('downloadgrades', 'assignfeedback_offline'));
374     }
376     /**
377      * Override the default is_enabled to disable this plugin if advanced grading is active
378      *
379      * @return bool
380      */
381     public function is_enabled() {
382         if ($this->enabledcache === null) {
383             $gradingmanager = get_grading_manager($this->assignment->get_context(), 'mod_assign', 'submissions');
384             $controller = $gradingmanager->get_active_controller();
385             $active = !empty($controller);
387             if ($active) {
388                 $this->enabledcache = false;
389             } else {
390                 $this->enabledcache = parent::is_enabled();
391             }
392         }
393         return $this->enabledcache;
394     }
396     /**
397      * Do not show this plugin in the grading table or on the front page
398      *
399      * @return bool
400      */
401     public function has_user_summary() {
402         return false;
403     }