MDL-36908 Assignment: Additional fixes for preventing empty submissions
[moodle.git] / mod / assign / submission / comments / locallib.php
CommitLineData
bbd0e548
DW
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/>.
16
17/**
18 * This file contains the definition for the library class for online comment submission plugin
19 *
20 * @package assignsubmission_comments
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25 defined('MOODLE_INTERNAL') || die();
26
27 /** Include comment core lib.php */
28 require_once($CFG->dirroot . '/comment/lib.php');
29 /** Include submissionplugin.php */
30 require_once($CFG->dirroot . '/mod/assign/submissionplugin.php');
31
32/**
33 * library class for comment submission plugin extending submission plugin base class
34 *
35 * @package assignsubmission_comments
36 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class assign_submission_comments extends assign_submission_plugin {
40
41 /**
42 * get the name of the online comment submission plugin
43 * @return string
44 */
45 public function get_name() {
46 return get_string('pluginname', 'assignsubmission_comments');
47 }
48
49 /**
50 * display AJAX based comment in the submission status table
51 *
52 * @param stdClass $submission
53 * @param bool $showviewlink - If the comments are long this is set to true so they can be shown in a separate page
54 * @return string
55 */
7ee1548a 56 public function view_summary(stdClass $submission, & $showviewlink) {
bbd0e548
DW
57
58 // never show a link to view full submission
59 $showviewlink = false;
60 // need to used this init() otherwise it shows up undefined !
61 // require js for commenting
62 comment::init();
63
64 $options = new stdClass();
65 $options->area = 'submission_comments';
66 $options->course = $this->assignment->get_course();
67 $options->context = $this->assignment->get_context();
68 $options->itemid = $submission->id;
69 $options->component = 'assignsubmission_comments';
70 $options->showcount = true;
71 $options->displaycancel = true;
72
73 $comment = new comment($options);
74 $comment->set_view_permission(true);
75
76 return $comment->output(true);
77
78 }
79
80 /**
7a9fd6da
DW
81 * Always return true because the submission comments are not part of the submission form.
82 *
bbd0e548
DW
83 * @param stdClass $submission
84 * @return bool
85 */
86 public function is_empty(stdClass $submission) {
7a9fd6da 87 return true;
bbd0e548
DW
88 }
89
90 /**
91 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
92 * and version.
93 *
94 * @param string $type old assignment subtype
95 * @param int $version old assignment version
96 * @return bool True if upgrade is possible
97 */
98 public function can_upgrade($type, $version) {
99
100 if ($type == 'upload' && $version >= 2011112900) {
101 return true;
102 }
103 return false;
104 }
105
106
107 /**
108 * Upgrade the settings from the old assignment to the new plugin based one
109 *
110 * @param context $oldcontext - the context for the old assignment
111 * @param stdClass $oldassignment - the data for the old assignment
112 * @param string $log - can be appended to by the upgrade
113 * @return bool was it a success? (false will trigger a rollback)
114 */
115 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
116 // first upgrade settings (nothing to do)
117 return true;
118 }
119
120 /**
121 * Upgrade the submission from the old assignment to the new one
122 *
123 * @param context $oldcontext The context for the old assignment
124 * @param stdClass $oldassignment The data record for the old assignment
125 * @param stdClass $oldsubmission The data record for the old submission
126 * @param stdClass $submission The new submission record
127 * @param string $log Record upgrade messages in the log
128 * @return bool true or false - false will trigger a rollback
129 */
130 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
131
132 if ($oldsubmission->data1 != '') {
133
134 // need to used this innit() otherwise it shows up undefined !
135 // require js for commenting
136 comment::init();
137
138 $options = new stdClass();
139 $options->area = 'submission_comments_upgrade';
140 $options->course = $this->assignment->get_course();
141 $options->context = $this->assignment->get_context();
142 $options->itemid = $submission->id;
143 $options->component = 'assignsubmission_comments';
144 $options->showcount = true;
145 $options->displaycancel = true;
146
147 $comment = new comment($options);
148 $comment->add($oldsubmission->data1);
149 $comment->set_view_permission(true);
150
151
152 return $comment->output(true);
153 }
154
155
156 return true;
157 }
158
159 /**
160 * The submission comments plugin has no submission component so should not be counted
161 * when determining whether to show the edit submission link.
162 * @return boolean
163 */
164 public function allow_submissions() {
165 return false;
166 }
167
b473171a
DW
168 /**
169 * If blind marking is enabled then disable this plugin (it shows names)
170 *
171 * @return bool
172 */
173 public function is_enabled() {
174 if ($this->assignment->has_instance() && $this->assignment->is_blind_marking()) {
175 return false;
176 }
177 return parent::is_enabled();
178 }
179
bbd0e548 180}