Skip to content

Commit

Permalink
CTP-1685 add unfreeze function to lifecycle block
Browse files Browse the repository at this point in the history
  • Loading branch information
aydevworks committed Nov 15, 2024
1 parent b4a5c5d commit b87cd6e
Show file tree
Hide file tree
Showing 16 changed files with 335 additions and 13 deletions.
2 changes: 1 addition & 1 deletion amd/build/lifecycle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/lifecycle.min.js.map

Large diffs are not rendered by default.

54 changes: 53 additions & 1 deletion amd/src/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import Ajax from 'core/ajax';
import notification from 'core/notification';
import {getStrings} from 'core/str';

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

export const init = (courseid) => {
// The course is read-only. Do nothing.
// The course is read-only. Initialize the unfreeze button and return.
if (!document.getElementById('lifecycle-settings-container')) {
// Initialize the unfreeze button.
initUnfreezeButton(courseid);
return;
}

Expand Down Expand Up @@ -162,3 +165,52 @@ function updatepreferences(courseid) {
});
}
}

/**
* Initialize the unfreeze button.
*
* @param {int} courseid
*/
function initUnfreezeButton(courseid) {
// Get the unfreeze button.
let unfreezeButton = document.getElementById('unfreeze-button');

// The course is not frozen. Do nothing.
if (!unfreezeButton) {
return;
}

let contextname = unfreezeButton.getAttribute('data-contextname');

unfreezeButton.addEventListener('click', event => {
event.preventDefault();

const requiredStrings = [
{key: 'confirmcontextunlock', component: 'admin', param: {'contextname': contextname}},
];

getStrings(requiredStrings).then(([unlockBody]) => {
return notification.confirm('Enable editing', unlockBody, 'Confirm', null, () => {
Ajax.call([{
methodname: 'block_lifecycle_unfreeze_course',
args: {
'courseid': courseid
},
}])[0].done(function(response) {
if (response.success) {
window.location.reload();
} else {
notification.addNotification({
message: response.message || 'An error occurred while enabling editing.',
type: 'error'
});
// Scroll to the top of the page to show the error message.
window.scrollTo({top: 0, behavior: "instant"});
}
}).fail(function(err) {
window.console.log(err);
});
});
});
});
}
5 changes: 5 additions & 0 deletions block_lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public function get_content() {
$html .= $renderer->fetch_clc_content($courseid);
if (manager::is_course_frozen($courseid)) {
$html .= $renderer->fetch_course_read_only_notification();

// Check if user has the capability to unfreeze the course.
if (has_capability("block/lifecycle:unfreezecourse", $context)) {
$html .= $renderer->fetch_unfreeze_button_html($context);
}
}
$html .= $renderer->fetch_course_dates($courseid);
}
Expand Down
86 changes: 86 additions & 0 deletions classes/external/unfreeze_course.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace block_lifecycle\external;

use block_lifecycle\manager;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;

/**
* External API for unfreeze a course
*
* @package block_lifecycle
* @copyright 2024 onwards University College London {@link https://www.ucl.ac.uk/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Alex Yeung <[email protected]>
*/
class unfreeze_course extends external_api {
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters() {
return new external_function_parameters([
'courseid' => new external_value(PARAM_INT, 'Coruse ID', VALUE_REQUIRED),
]);
}

/**
* Returns description of method result value.
*
* @return \external_single_structure
*/
public static function execute_returns() {
return new external_single_structure([
'success' => new external_value(PARAM_BOOL, 'Result of request', VALUE_REQUIRED),
'message' => new external_value(PARAM_TEXT, 'Error message', VALUE_OPTIONAL),
]);
}

/**
* Unfreeze a course
*
* @param int $courseid
* @return array
*/
public static function execute(int $courseid) {
try {
$params = self::validate_parameters(
self::execute_parameters(),
[
'courseid' => $courseid,
]
);

// Unfreeze the course.
manager::unfreeze_course($params['courseid']);

return [
'success' => true,
'message' => 'Enabled course editing successfully.',
];
} catch (\Exception $e) {
return [
'success' => false,
'message' => $e->getMessage(),
];
}
}
}
17 changes: 17 additions & 0 deletions classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,23 @@ public static function get_weeks_delay_in_seconds() {
return $enddateextend;
}

/**
* Unfreeze course context.
*
* @param int $courseid Course id.
* @return void
* @throws \coding_exception
* @throws \moodle_exception
*/
public static function unfreeze_course(int $courseid): void {
// Check user's permission.
if (!has_capability('block/lifecycle:unfreezecourse', context_course::instance($courseid))) {
throw new \moodle_exception('error:unfreeze_course', 'block_lifecycle');
}
$context = context_course::instance($courseid);
$context->set_locked(false);
}

/**
* Get the furthest date among LSA end date and course end date, plus weeks delay.
*
Expand Down
8 changes: 8 additions & 0 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@
'manager' => CAP_ALLOW,
],
],
'block/lifecycle:unfreezecourse' => [
'captype' => 'read',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
],
],
];
10 changes: 10 additions & 0 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'description' => 'Update auto context freezing preferences',
'ajax' => true,
'type' => 'write',
'readonlysession' => true,
'loginrequired' => true,
],
'block_lifecycle_get_scheduled_freeze_date' => [
Expand All @@ -43,6 +44,15 @@
'description' => 'Get scheduled freeze date',
'ajax' => true,
'type' => 'read',
'readonlysession' => true,
'loginrequired' => true,
],
'block_lifecycle_unfreeze_course' => [
'classname' => 'block_lifecycle\external\unfreeze_course',
'description' => 'Unfreeze a course',
'ajax' => true,
'type' => 'write',
'readonlysession' => true,
'loginrequired' => true,
],
];
Expand Down
15 changes: 9 additions & 6 deletions lang/en/block_lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,30 @@
* @author Alex Yeung <[email protected]>
*/

$string['pluginname'] = 'Lifecycle';
$string['button:editsettings'] = 'Edit automatic Read-Only settings';
$string['button:toggleautoreadonly'] = 'Disable Automatic Read-Only';
$string['error:dateformat'] = 'Date must be in format YYYY-MM-DD';
$string['error:cannotgetscheduledfreezedate'] = 'Could not get the automatically suggested date.';
$string['error:updatepreferencessuccess'] = 'Auto read only settings updated successfully.';
$string['error:dateformat'] = 'Date must be in format YYYY-MM-DD';
$string['error:unfreeze_course'] = 'You do not have permission to enable editing.';
$string['error:updatepreferencesfailed'] = 'Failed to update read only settings.';
$string['error:updatepreferencessuccess'] = 'Auto read only settings updated successfully.';
$string['generalsettings'] = 'General Settings';
$string['help:togglefreezing'] = 'Disable Automatic Read-Only';
$string['help:togglefreezing_help'] = 'Disable Automatic Read-Only.';
$string['help:delayfreezedate'] = 'override Read-Only date';
$string['help:delayfreezedate_help'] = 'The date for a Read-Only override must be post the automatically suggested date, earlier dates may not be used.';
$string['help:togglefreezing'] = 'Disable Automatic Read-Only';
$string['help:togglefreezing_help'] = 'Disable Automatic Read-Only.';
$string['label:readonlydate'] = 'This course will be made automatically Read Only on: ';
$string['label:readonlydateinput'] = 'Overrides Read-Only date:';
$string['label:unfreezebutton'] = 'Enable editing';
$string['lifecycle:addinstance'] = 'Add lifecycle block';
$string['lifecycle:coursereadonly'] = 'This Course is Read Only';
$string['lifecycle:enddate'] = 'This course\'s end date: {$a}';
$string['lifecycle:myaddinstance'] = 'Add my lifecycle block';
$string['lifecycle:overridecontextfreeze'] = 'Override default course context freezing settings';
$string['lifecycle:startdate'] = 'This course\'s start date: {$a}';
$string['lifecycle:coursereadonly'] = 'This Course is Read Only';
$string['lifecycle:unfreezecourse'] = 'Unfreeze course';
$string['lifecycle:view'] = 'View lifecycle block';
$string['pluginname'] = 'Lifecycle';
$string['privacy:metadata'] = 'The Lifecycle block does not store personal data';
$string['settings:academicyearstartdate'] = 'Academic year start date';
$string['settings:academicyearstartdate:desc'] = 'This field is used to calculate the current academic year period and in MM-DD format';
Expand Down
16 changes: 16 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

use block_lifecycle\manager;
use core\context\course;

/**
* Class block_lifecycle_renderer
Expand Down Expand Up @@ -151,4 +152,19 @@ public function fetch_course_read_only_notification(): string {

return $content;
}

/**
* Return the html for the unfreeze button.
*
* @param course $context
* @return string
* @throws coding_exception
*/
public function fetch_unfreeze_button_html(core\context\course $context): string {
return html_writer::link(
'#',
'<i class="fa-edit fa fa-fw"></i>' . get_string('label:unfreezebutton', 'block_lifecycle'),
['id' => 'unfreeze-button', 'class' => 'btn btn-primary w-100', 'data-contextname' => $context->get_context_name()]
);
}
}
Loading

0 comments on commit b87cd6e

Please sign in to comment.