Skip to content

Commit 2d21346

Browse files
committed
CTP-1685 add unfreeze function in lifecycle block
1 parent 66ba4e7 commit 2d21346

File tree

11 files changed

+126
-173
lines changed

11 files changed

+126
-173
lines changed

amd/build/lifecycle.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/lifecycle.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/lifecycle.js

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import Ajax from 'core/ajax';
22
import notification from 'core/notification';
3-
import {getStrings} from 'core/str';
43

54
// Default auto suggested read-only date.
65
let defaultfreezedate = '';
76
// The datepicker original value before user make any changes.
87
let originalfreezedatevalue = '';
98

109
export const init = (courseid) => {
11-
// The course is read-only. Initialize the unfreeze button and return.
10+
// The course is read-only. Do nothing.
1211
if (!document.getElementById('lifecycle-settings-container')) {
13-
// Initialize the unfreeze button.
14-
initUnfreezeButton(courseid);
1512
return;
1613
}
1714

@@ -165,57 +162,3 @@ function updatepreferences(courseid) {
165162
});
166163
}
167164
}
168-
169-
/**
170-
* Initialize the unfreeze button.
171-
*
172-
* @param {int} courseid
173-
*/
174-
function initUnfreezeButton(courseid) {
175-
// Get the unfreeze button.
176-
let unfreezeButton = document.getElementById('unfreeze-button');
177-
178-
// The course is not frozen. Do nothing.
179-
if (!unfreezeButton) {
180-
return;
181-
}
182-
183-
let contextname = unfreezeButton.getAttribute('data-contextname');
184-
185-
unfreezeButton.addEventListener('click', event => {
186-
event.preventDefault();
187-
188-
const requiredStrings = [
189-
{key: 'confirmcontextunlock', component: 'admin', param: {'contextname': contextname}},
190-
];
191-
192-
getStrings(requiredStrings).then(([unlockBody]) => {
193-
return notification.confirm('Enable editing', unlockBody, 'Confirm', null, () => {
194-
Ajax.call([{
195-
methodname: 'block_lifecycle_unfreeze_course',
196-
args: {
197-
'courseid': courseid
198-
},
199-
}])[0].done(function(response) {
200-
if (response.success) {
201-
window.location.reload();
202-
} else {
203-
notification.addNotification({
204-
message: response.message || 'An error occurred while enabling editing.',
205-
type: 'error'
206-
});
207-
// Scroll to the top of the page to show the error message.
208-
window.scrollTo({top: 0, behavior: "instant"});
209-
}
210-
}).fail(function(err) {
211-
window.console.log(err);
212-
});
213-
});
214-
}).catch(
215-
(error) => {
216-
window.console.log(error);
217-
return error;
218-
}
219-
);
220-
});
221-
}

classes/external/unfreeze_course.php

Lines changed: 0 additions & 86 deletions
This file was deleted.

classes/manager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,10 @@ public static function unfreeze_course(int $courseid): void {
360360
throw new \moodle_exception('error:unfreeze_course', 'block_lifecycle');
361361
}
362362
$context = context_course::instance($courseid);
363-
$context->set_locked(false);
363+
if ($context->is_locked()) {
364+
// Unlock the course context.
365+
$context->set_locked(false);
366+
}
364367
}
365368

366369
/**

db/access.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,4 @@
5757
'manager' => CAP_ALLOW,
5858
],
5959
],
60-
'block/lifecycle:unfreezecourse' => [
61-
'captype' => 'read',
62-
'contextlevel' => CONTEXT_COURSE,
63-
'archetypes' => [
64-
'editingteacher' => CAP_ALLOW,
65-
'manager' => CAP_ALLOW,
66-
],
67-
],
6860
];

db/services.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
'description' => 'Update auto context freezing preferences',
3535
'ajax' => true,
3636
'type' => 'write',
37-
'readonlysession' => true,
3837
'loginrequired' => true,
3938
],
4039
'block_lifecycle_get_scheduled_freeze_date' => [
@@ -44,15 +43,6 @@
4443
'description' => 'Get scheduled freeze date',
4544
'ajax' => true,
4645
'type' => 'read',
47-
'readonlysession' => true,
48-
'loginrequired' => true,
49-
],
50-
'block_lifecycle_unfreeze_course' => [
51-
'classname' => 'block_lifecycle\external\unfreeze_course',
52-
'description' => 'Unfreeze a course',
53-
'ajax' => true,
54-
'type' => 'write',
55-
'readonlysession' => true,
5646
'loginrequired' => true,
5747
],
5848
];

renderer.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,19 @@ public function fetch_course_read_only_notification(): string {
156156
/**
157157
* Return the html for the unfreeze button.
158158
*
159-
* @param course $context
159+
* @param context_course $context
160160
* @return string
161-
* @throws coding_exception
161+
* @throws coding_exception|moodle_exception
162162
*/
163-
public function fetch_unfreeze_button_html(core\context\course $context): string {
164-
return
165-
'<a id="unfreeze-button" class="btn btn-primary w-100" data-contextname="' . $context->get_context_name() . '">' .
166-
'<i class="fa-edit fa fa-fw"></i>' . get_string('label:unfreezebutton', 'block_lifecycle') .
167-
'</a>';
163+
public function fetch_unfreeze_button_html(context_course $context): string {
164+
$url = new moodle_url('/blocks/lifecycle/unfreeze.php', ['id' => $context->instanceid]);
165+
return $this->output->render_from_template(
166+
'block_lifecycle/unfreeze_button_form',
167+
[
168+
'url' => $url->out(false),
169+
'courseid' => $context->instanceid,
170+
'coursefullname' => $context->get_context_name(),
171+
]
172+
);
168173
}
169174
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{{!
2+
This file is part the Local Analytics plugin for Moodle
3+
Moodle is free software: you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation, either version 3 of the License, or
6+
(at your option) any later version.
7+
8+
Moodle is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15+
}}
16+
{{!
17+
@template local_courserollover/start_button
18+
19+
Template for displaying the start button to begin the rollover step process.
20+
21+
Classes required for JS:
22+
* none
23+
24+
Data attributes required for JS:
25+
* none
26+
27+
Context variables required for this template:
28+
* url - The URL to navigate to the first step form.
29+
* str - bool
30+
31+
Example context (json):
32+
{
33+
"url": "#",
34+
"str": true
35+
}
36+
}}
37+
<button type="button"
38+
class="btn btn-primary w-100"
39+
data-confirmation="modal"
40+
data-confirmation-title-str='["label:unfreezebutton", "block_lifecycle"]'
41+
data-confirmation-content-str='["confirmcontextunlock", "admin", {"contextname": "{{coursefullname}}"}]'
42+
data-confirmation-yes-button-str='["confirm"]'
43+
data-confirmation-destination="{{url}}"
44+
>
45+
Enable editing
46+
<span class="sr-only"> for course {{coursefullname}}</span>
47+
</button>

unfreeze.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
* unfreeze page for block_lifecycle to unfreeze a frozen course.
19+
*
20+
* @package block_lifecycle
21+
* @copyright 2024 onwards University College London {@link https://www.ucl.ac.uk/}
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
* @author Alex Yeung <k.yeung@ucl.ac.uk>
24+
*/
25+
26+
namespace block_lifecycle;
27+
28+
use context_course;
29+
use core\output\notification;
30+
use moodle_exception;
31+
use moodle_url;
32+
33+
require_once('../../config.php');
34+
35+
// Course ID.
36+
$courseid = required_param('id', PARAM_INT);
37+
38+
// Get course context.
39+
$context = context_course::instance($courseid);
40+
41+
// Get course instance.
42+
if (!$course = get_course($courseid)) {
43+
throw new moodle_exception('course not found.', 'block_lifecycle');
44+
}
45+
46+
// Make sure user is authenticated.
47+
require_login($course);
48+
49+
// Check user's capability.
50+
require_capability('block/lifecycle:unfreezecourse', $context);
51+
52+
// Unfreeze the course.
53+
try {
54+
manager::unfreeze_course($courseid);
55+
// Redirect to course page.
56+
redirect(new moodle_url('/course/view.php', ['id' => $courseid]));
57+
} catch (moodle_exception $e) {
58+
redirect(new moodle_url('/course/view.php', ['id' => $courseid]), $e->getMessage(), null, notification::NOTIFY_ERROR);
59+
}

0 commit comments

Comments
 (0)