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 functions for assign_plugin abstract class
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();
29 * Abstract class for assign_plugin (submission/feedback).
32 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 abstract class assign_plugin {
37 /** @var assign $assignment the assignment record that contains the global settings for this assign instance */
38 protected $assignment;
39 /** @var string $type assignment plugin type */
41 /** @var string $error error message */
46 * Constructor for the abstract plugin type class
48 * @param assign $assignment
51 public final function __construct(assign $assignment, $type) {
52 $this->assignment = $assignment;
57 * Is this the first plugin in the list?
61 public final function is_first() {
62 $order = get_config($this->get_subtype() . '_' . $this->get_type(), 'sortorder');
71 * Is this the last plugin in the list?
75 public final function is_last() {
76 if ((count(get_plugin_list($this->get_subtype()))-1) == get_config($this->get_subtype() . '_' . $this->get_type(), 'sortorder')) {
84 * This function should be overridden to provide an array of elements that can be added to a moodle
85 * form for display in the settings page for the assignment.
86 * @param MoodleQuickForm $mform The form to add the elements to
89 public function get_settings(MoodleQuickForm $mform) {
94 * Allows the plugin to update the defaultvalues passed in to
95 * the settings form (needed to set up draft areas for editor
96 * and filemanager elements)
97 * @param array $defaultvalues
99 public function data_preprocessing(&$defaultvalues) {
104 * The assignment subtype is responsible for saving it's own settings as the database table for the
105 * standard type cannot be modified.
107 * @param stdClass $formdata - the data submitted from the form
108 * @return bool - on error the subtype should call set_error and return false.
110 public function save_settings(stdClass $formdata) {
115 * Save the error message from the last error
117 * @param string $msg - the error description
119 protected final function set_error($msg) {
124 * What was the last error?
129 public final function get_error() {
134 * Should return the name of this plugin type.
136 * @return string - the name
138 public abstract function get_name();
141 * Should return the subtype of this plugin.
143 * @return string - either 'assignsubmission' or 'feedback'
145 public abstract function get_subtype();
148 * Should return the type of this plugin.
150 * @return string - the type
152 public final function get_type() {
157 * Get the installed version of this plugin
161 public final function get_version() {
162 $version = get_config($this->get_subtype() . '_' . $this->get_type(), 'version');
171 * Get the required moodle version for this plugin
175 public final function get_requires() {
176 $requires = get_config($this->get_subtype() . '_' . $this->get_type(), 'requires');
185 * Save any custom data for this form submission
187 * @param stdClass $submissionorgrade - assign_submission or assign_grade
188 * For submission plugins this is the submission data, for feedback plugins it is the grade data
189 * @param stdClass $data - the data submitted from the form
190 * @return bool - on error the subtype should call set_error and return false.
192 public function save(stdClass $submissionorgrade, stdClass $data) {
197 * Set this plugin to enabled
201 public final function enable() {
202 return $this->set_config('enabled', 1);
206 * Set this plugin to disabled
210 public final function disable() {
211 return $this->set_config('enabled', 0);
215 * Allows hiding this plugin from the submission/feedback screen if it is not enabled.
217 * @return bool - if false - this plugin will not accept submissions / feedback
219 public function is_enabled() {
220 return $this->get_config('enabled');
225 * Get any additional fields for the submission/grading form for this assignment.
227 * @param mixed $submissionorgrade submission|grade - For submission plugins this is the submission data,
228 * for feedback plugins it is the grade data
229 * @param MoodleQuickForm $mform - This is the form
230 * @param stdClass $data - This is the form data that can be modified for example by a filemanager element
231 * @param int $userid - This is the userid for the current submission.
232 * This is passed separately as there may not yet be a submission or grade.
233 * @return boolean - true if we added anything to the form
235 public function get_form_elements_for_user($submissionorgrade, MoodleQuickForm $mform, stdClass $data, $userid) {
236 return $this->get_form_elements($submissionorgrade, $mform, $data);
240 * Get any additional fields for the submission/grading form for this assignment.
241 * This function is retained for backwards compatibility - new plugins should override {@link get_form_elements_for_user()}.
243 * @param mixed $submissionorgrade submission|grade - For submission plugins this is the submission data, for feedback plugins it is the grade data
244 * @param MoodleQuickForm $mform - This is the form
245 * @param stdClass $data - This is the form data that can be modified for example by a filemanager element
246 * @return boolean - true if we added anything to the form
248 public function get_form_elements($submissionorgrade, MoodleQuickForm $mform, stdClass $data) {
253 * Should not output anything - return the result as a string so it can be consumed by webservices.
255 * @param stdClass $submissionorgrade assign_submission or assign_grade
256 * For submission plugins this is the submission data, for feedback plugins it is the grade data
257 * @return string - return a string representation of the submission in full
259 public function view(stdClass $submissionorgrade) {
264 * Get the numerical sort order for this plugin
268 public final function get_sort_order() {
269 $order = get_config($this->get_subtype() . '_' . $this->get_type(), 'sortorder');
270 return $order?$order:0;
274 * Is this plugin enaled?
278 public final function is_visible() {
279 $disabled = get_config($this->get_subtype() . '_' . $this->get_type(), 'disabled');
285 * Has this plugin got a custom settings.php file?
289 public final function has_admin_settings() {
292 return file_exists($CFG->dirroot . '/mod/assign/' . substr($this->get_subtype(), strlen('assign')) . '/' . $this->get_type() . '/settings.php');
296 * Set a configuration value for this plugin
298 * @param string $name The config key
299 * @param string $value The config value
302 public final function set_config($name, $value) {
305 $current = $DB->get_record('assign_plugin_config', array('assignment'=>$this->assignment->get_instance()->id, 'subtype'=>$this->get_subtype(), 'plugin'=>$this->get_type(), 'name'=>$name), '*', IGNORE_MISSING);
308 $current->value = $value;
309 return $DB->update_record('assign_plugin_config', $current);
311 $setting = new stdClass();
312 $setting->assignment = $this->assignment->get_instance()->id;
313 $setting->subtype = $this->get_subtype();
314 $setting->plugin = $this->get_type();
315 $setting->name = $name;
316 $setting->value = $value;
318 return $DB->insert_record('assign_plugin_config', $setting) > 0;
323 * Get a configuration value for this plugin
325 * @param mixed $setting The config key (string) or null
326 * @return mixed string | false
328 public final function get_config($setting = null) {
332 if (!$this->assignment->has_instance()) {
335 $assignment = $this->assignment->get_instance();
337 $result = $DB->get_record('assign_plugin_config', array('assignment'=>$assignment->id, 'subtype'=>$this->get_subtype(), 'plugin'=>$this->get_type(), 'name'=>$setting), '*', IGNORE_MISSING);
339 return $result->value;
344 $results = $DB->get_records('assign_plugin_config', array('assignment'=>$this->assignment->get_instance()->id, 'subtype'=>$this->get_subtype(), 'plugin'=>$this->get_type()));
346 $config = new stdClass();
347 if (is_array($results)) {
348 foreach ($results as $setting) {
349 $name = $setting->name;
350 $config->$name = $setting->value;
357 * Should not output anything - return the result as a string so it can be consumed by webservices.
359 * @param stdClass $submissionorgrade assign_submission or assign_grade
360 * For submission plugins this is the submission data, for feedback plugins it is the grade data
361 * @param bool $showviewlink Modifed to return whether or not to show a link to the full submission/feedback
362 * @return string - return a string representation of the submission in full
364 public function view_summary(stdClass $submissionorgrade, & $showviewlink) {
369 * Given a field name and value should update the text for this field in the plugins submission or grade
371 * @param string $name Name of the field.
372 * @param string $value Updated text
373 * @param int $submissionorgradeid The id of the submission or grade
374 * @return bool - true if the value was updated
376 public function set_editor_text($name, $value, $submissionorgradeid) {
381 * Given a field name and value should update the format for this field in the plugins submission or grade
383 * @param string $name Name of the field.
384 * @param int $format Updated format.
385 * @param int $submissionorgradeid The id of the submission or grade.
386 * @return bool - true if the value was updated
388 public function set_editor_format($name, $format, $submissionorgradeid) {
393 * Return a list of the fields that can be exported or imported via text.
395 * @return array - The list of field names (strings) and descriptions. ($name => $description)
397 public function get_editor_fields() {
402 * Given a field name, should return the text of an editor field that is part of
403 * this plugin. This is used when exporting to portfolio.
405 * @param string $name Name of the field.
406 * @param int $submissionorgradeid The id of the submission or grade
407 * @return string - The text for the editor field
409 public function get_editor_text($name, $submissionorgradeid) {
414 * Produce a list of files suitable for export that represent this feedback or submission
416 * @param stdClass $submissionorgrade assign_submission or assign_grade
417 * For submission plugins this is the submission data, for feedback plugins it is the grade data
418 * @return array - return an array of files indexed by filename
420 public function get_files(stdClass $submissionorgrade) {
425 * Given a field name, should return the format of an editor field that is part of
426 * this plugin. This is used when exporting to portfolio.
428 * @param string $name Name of the field.
429 * @param int $submissionid The id of the submission
430 * @return int - The format for the editor field
432 public function get_editor_format($name, $submissionid) {
437 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
440 * @param string $type The old assignment subtype
441 * @param int $version The old assignment version
442 * @return bool True if upgrade is possible
444 public function can_upgrade($type, $version) {
449 * Upgrade the settings from the old assignment to the new one
451 * @param context $oldcontext The context for the old assignment module
452 * @param stdClass $oldassignment The data record for the old assignment
453 * @param string $log Record upgrade messages in the log
454 * @return bool true or false - false will trigger a rollback
456 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
457 $log = $log . ' ' . get_string('upgradenotimplemented', 'mod_assign', array('type'=>$this->type, 'subtype'=>$this->get_subtype()));
462 * Upgrade the submission from the old assignment to the new one
464 * @param context $oldcontext The data record for the old context
465 * @param stdClass $oldassignment The data record for the old assignment
466 * @param stdClass $oldsubmissionorgrade The data record for the old submission
467 * @param stdClass $submissionorgrade assign_submission or assign_grade The new submission or grade
468 * @param string $log Record upgrade messages in the log
469 * @return boolean true or false - false will trigger a rollback
471 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmissionorgrade, stdClass $submissionorgrade, & $log) {
472 $log = $log . ' ' . get_string('upgradenotimplemented', 'mod_assign', array('type'=>$this->type, 'subtype'=>$this->get_subtype()));
477 * Formatting for log info
479 * @param stdClass $submissionorgrade assign_submission or assign_grade The new submission or grade
482 public function format_for_log(stdClass $submissionorgrade) {
483 // format the info for each submission plugin add_to_log
488 * The assignment has been deleted - remove the plugin specific data
492 public function delete_instance() {
497 * Run cron for this plugin
499 public static function cron() {
503 * Is this assignment plugin empty? (ie no submission or feedback)
504 * @param stdClass $submissionorgrade assign_submission or assign_grade
507 public function is_empty(stdClass $submissionorgrade) {
512 * Get file areas returns a list of areas this plugin stores files
513 * @return array - An array of fileareas (keys) and descriptions (values)
515 public function get_file_areas() {
521 * Default implementation of file_get_info for plugins.
522 * This is used by the filebrowser to browse a plugins file areas.
524 * This implementation should work for most plugins but can be overridden if required.
525 * @param file_browser $browser
526 * @param string $filearea
528 * @param string $filepath
529 * @param string $filename
530 * @return file_info_stored
532 public function get_file_info($browser, $filearea, $itemid, $filepath, $filename) {
533 global $CFG, $DB, $USER;
534 $urlbase = $CFG->wwwroot.'/pluginfile.php';
536 // permission check on the itemid
538 if ($this->get_subtype() == 'assignsubmission') {
540 $record = $DB->get_record('assign_submission', array('id'=>$itemid), 'userid', IGNORE_MISSING);
544 if (!$this->assignment->can_view_submission($record->userid)) {
549 // not supported for feedback plugins
553 $fs = get_file_storage();
554 $filepath = is_null($filepath) ? '/' : $filepath;
555 $filename = is_null($filename) ? '.' : $filename;
556 if (!($storedfile = $fs->get_file($this->assignment->get_context()->id,
557 $this->get_subtype() . '_' . $this->get_type(),
564 return new file_info_stored($browser,
565 $this->assignment->get_context(),
576 * This allows a plugin to render a page in the context of the assignment
578 * If the plugin creates a link to the assignment view.php page with
579 * The following required parameters:
582 * pluginsubtype=assignfeedback|assignsubmission
583 * pluginaction=customaction
585 * Then this function will be called to display the page with the pluginaction passed as action
586 * @param string $action The plugin specified action
589 public function view_page($action) {
594 * If this plugin should not include a column in the grading table or a row on the summary page
599 public function has_user_summary() {