Skip to content

Commit

Permalink
CTP-1685 add unfreeze function in lifecycle block
Browse files Browse the repository at this point in the history
  • Loading branch information
aydevworks committed Nov 16, 2024
1 parent 66ba4e7 commit 2d21346
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 173 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.

59 changes: 1 addition & 58 deletions amd/src/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
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. Initialize the unfreeze button and return.
// The course is read-only. Do nothing.
if (!document.getElementById('lifecycle-settings-container')) {
// Initialize the unfreeze button.
initUnfreezeButton(courseid);
return;
}

Expand Down Expand Up @@ -165,57 +162,3 @@ 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);
});
});
}).catch(
(error) => {
window.console.log(error);
return error;
}
);
});
}
86 changes: 0 additions & 86 deletions classes/external/unfreeze_course.php

This file was deleted.

5 changes: 4 additions & 1 deletion classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ public static function unfreeze_course(int $courseid): void {
throw new \moodle_exception('error:unfreeze_course', 'block_lifecycle');
}
$context = context_course::instance($courseid);
$context->set_locked(false);
if ($context->is_locked()) {
// Unlock the course context.
$context->set_locked(false);
}
}

/**
Expand Down
8 changes: 0 additions & 8 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,4 @@
'manager' => CAP_ALLOW,
],
],
'block/lifecycle:unfreezecourse' => [
'captype' => 'read',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
],
],
];
10 changes: 0 additions & 10 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
'description' => 'Update auto context freezing preferences',
'ajax' => true,
'type' => 'write',
'readonlysession' => true,
'loginrequired' => true,
],
'block_lifecycle_get_scheduled_freeze_date' => [
Expand All @@ -44,15 +43,6 @@
'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
19 changes: 12 additions & 7 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,19 @@ public function fetch_course_read_only_notification(): string {
/**
* Return the html for the unfreeze button.
*
* @param course $context
* @param context_course $context
* @return string
* @throws coding_exception
* @throws coding_exception|moodle_exception
*/
public function fetch_unfreeze_button_html(core\context\course $context): string {
return
'<a id="unfreeze-button" class="btn btn-primary w-100" data-contextname="' . $context->get_context_name() . '">' .
'<i class="fa-edit fa fa-fw"></i>' . get_string('label:unfreezebutton', 'block_lifecycle') .
'</a>';
public function fetch_unfreeze_button_html(context_course $context): string {
$url = new moodle_url('/blocks/lifecycle/unfreeze.php', ['id' => $context->instanceid]);
return $this->output->render_from_template(
'block_lifecycle/unfreeze_button_form',
[
'url' => $url->out(false),
'courseid' => $context->instanceid,
'coursefullname' => $context->get_context_name(),
]
);
}
}
47 changes: 47 additions & 0 deletions templates/unfreeze_button_form.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{{!
This file is part the Local Analytics plugin for Moodle
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/>.
}}
{{!
@template local_courserollover/start_button
Template for displaying the start button to begin the rollover step process.
Classes required for JS:
* none
Data attributes required for JS:
* none
Context variables required for this template:
* url - The URL to navigate to the first step form.
* str - bool
Example context (json):
{
"url": "#",
"str": true
}
}}
<button type="button"
class="btn btn-primary w-100"
data-confirmation="modal"
data-confirmation-title-str='["label:unfreezebutton", "block_lifecycle"]'
data-confirmation-content-str='["confirmcontextunlock", "admin", {"contextname": "{{coursefullname}}"}]'
data-confirmation-yes-button-str='["confirm"]'
data-confirmation-destination="{{url}}"
>
Enable editing
<span class="sr-only"> for course {{coursefullname}}</span>
</button>
59 changes: 59 additions & 0 deletions unfreeze.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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/>.

/**
* unfreeze page for block_lifecycle to unfreeze a frozen 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]>
*/

namespace block_lifecycle;

use context_course;
use core\output\notification;
use moodle_exception;
use moodle_url;

require_once('../../config.php');

// Course ID.
$courseid = required_param('id', PARAM_INT);

// Get course context.
$context = context_course::instance($courseid);

// Get course instance.
if (!$course = get_course($courseid)) {
throw new moodle_exception('course not found.', 'block_lifecycle');
}

// Make sure user is authenticated.
require_login($course);

// Check user's capability.
require_capability('block/lifecycle:unfreezecourse', $context);

// Unfreeze the course.
try {
manager::unfreeze_course($courseid);
// Redirect to course page.
redirect(new moodle_url('/course/view.php', ['id' => $courseid]));
} catch (moodle_exception $e) {
redirect(new moodle_url('/course/view.php', ['id' => $courseid]), $e->getMessage(), null, notification::NOTIFY_ERROR);
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2022120803;
$plugin->version = 2022120800;
$plugin->release = '0.1';
$plugin->maturity = MATURITY_ALPHA;
$plugin->requires = 2020061512;
Expand Down

0 comments on commit 2d21346

Please sign in to comment.