MDL-37539 Assignment upgrade: Disable submission comments if allow notes was not...
[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
bbd0e548 27 require_once($CFG->dirroot . '/comment/lib.php');
bbd0e548
DW
28 require_once($CFG->dirroot . '/mod/assign/submissionplugin.php');
29
30/**
e5403f8c 31 * Library class for comment submission plugin extending submission plugin base class
bbd0e548
DW
32 *
33 * @package assignsubmission_comments
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 */
37class assign_submission_comments extends assign_submission_plugin {
38
e5403f8c
DW
39 /**
40 * Get the name of the online comment submission plugin
41 * @return string
42 */
bbd0e548
DW
43 public function get_name() {
44 return get_string('pluginname', 'assignsubmission_comments');
45 }
46
e5403f8c
DW
47 /**
48 * Display AJAX based comment in the submission status table
49 *
50 * @param stdClass $submission
51 * @param bool $showviewlink - If the comments are long this is
52 * set to true so they can be shown in a separate page
53 * @return string
54 */
55 public function view_summary(stdClass $submission, & $showviewlink) {
bbd0e548 56
e5403f8c 57 // Never show a link to view full submission.
bbd0e548 58 $showviewlink = false;
e5403f8c 59 // Need to used this init() otherwise it does not have the javascript includes.
bbd0e548
DW
60 comment::init();
61
62 $options = new stdClass();
63 $options->area = 'submission_comments';
64 $options->course = $this->assignment->get_course();
65 $options->context = $this->assignment->get_context();
66 $options->itemid = $submission->id;
67 $options->component = 'assignsubmission_comments';
68 $options->showcount = true;
69 $options->displaycancel = true;
70
71 $comment = new comment($options);
72 $comment->set_view_permission(true);
73
74 return $comment->output(true);
bbd0e548
DW
75 }
76
77 /**
7a9fd6da
DW
78 * Always return true because the submission comments are not part of the submission form.
79 *
bbd0e548
DW
80 * @param stdClass $submission
81 * @return bool
82 */
83 public function is_empty(stdClass $submission) {
7a9fd6da 84 return true;
bbd0e548
DW
85 }
86
e5403f8c 87 /**
bbd0e548
DW
88 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
89 * and version.
90 *
91 * @param string $type old assignment subtype
92 * @param int $version old assignment version
93 * @return bool True if upgrade is possible
94 */
95 public function can_upgrade($type, $version) {
96
97 if ($type == 'upload' && $version >= 2011112900) {
98 return true;
99 }
100 return false;
101 }
102
103
e5403f8c 104 /**
bbd0e548
DW
105 * Upgrade the settings from the old assignment to the new plugin based one
106 *
107 * @param context $oldcontext - the context for the old assignment
108 * @param stdClass $oldassignment - the data for the old assignment
109 * @param string $log - can be appended to by the upgrade
110 * @return bool was it a success? (false will trigger a rollback)
111 */
112 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
5389dfb0
DW
113 if ($oldassignment->assignmenttype == 'upload') {
114 // Disable if allow notes was not enabled.
115 if (!$oldassignment->var2) {
116 $this->disable();
117 }
118 }
bbd0e548
DW
119 return true;
120 }
121
122 /**
123 * Upgrade the submission from the old assignment to the new one
124 *
125 * @param context $oldcontext The context for the old assignment
126 * @param stdClass $oldassignment The data record for the old assignment
127 * @param stdClass $oldsubmission The data record for the old submission
128 * @param stdClass $submission The new submission record
129 * @param string $log Record upgrade messages in the log
130 * @return bool true or false - false will trigger a rollback
131 */
e5403f8c
DW
132 public function upgrade(context $oldcontext,
133 stdClass $oldassignment,
134 stdClass $oldsubmission,
135 stdClass $submission,
136 & $log) {
bbd0e548
DW
137
138 if ($oldsubmission->data1 != '') {
139
e5403f8c 140 // Need to used this init() otherwise it does not have the javascript includes.
bbd0e548
DW
141 comment::init();
142
143 $options = new stdClass();
144 $options->area = 'submission_comments_upgrade';
145 $options->course = $this->assignment->get_course();
146 $options->context = $this->assignment->get_context();
147 $options->itemid = $submission->id;
148 $options->component = 'assignsubmission_comments';
149 $options->showcount = true;
150 $options->displaycancel = true;
151
152 $comment = new comment($options);
153 $comment->add($oldsubmission->data1);
154 $comment->set_view_permission(true);
155
bbd0e548
DW
156 return $comment->output(true);
157 }
158
bbd0e548
DW
159 return true;
160 }
161
162 /**
163 * The submission comments plugin has no submission component so should not be counted
164 * when determining whether to show the edit submission link.
165 * @return boolean
166 */
167 public function allow_submissions() {
168 return false;
169 }
170
b473171a
DW
171 /**
172 * If blind marking is enabled then disable this plugin (it shows names)
173 *
174 * @return bool
175 */
176 public function is_enabled() {
177 if ($this->assignment->has_instance() && $this->assignment->is_blind_marking()) {
178 return false;
179 }
180 return parent::is_enabled();
181 }
182
bbd0e548 183}