Skip to content

Commit

Permalink
Add unfreeze button to lifecycle block
Browse files Browse the repository at this point in the history
  • Loading branch information
nbozhkov-ucl authored and aydevworks committed Feb 15, 2024
1 parent cfb8ed6 commit 908d82f
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
5 changes: 5 additions & 0 deletions block_lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public function get_content() {
if (manager::should_show_auto_freezing_preferences($courseid)) {
$html .= $renderer->fetch_block_content($courseid);
}
if (manager::is_course_frozen($courseid)) {
if (has_capability('block/lifecycle:unfreezecoursecontext', $context)) {
$html .= $renderer->show_unfreeze_button($courseid);
}
}

$this->content->text = $html;

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:unfreezecoursecontext' => [
'captype' => 'read',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
],
],
];
4 changes: 4 additions & 0 deletions lang/en/block_lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
$string['error:cannotgetscheduledfreezedate'] = 'Could not get the automatically suggested date.';
$string['error:updatepreferencessuccess'] = 'Auto read only settings updated successfully.';
$string['error:updatepreferencesfailed'] = 'Failed to update read only settings.';
$string['error:courseisnotreadonly'] = 'This course is not Read Only. No need to unfreeze.';
$string['generalsettings'] = 'General Settings';
$string['help:togglefreezing'] = 'Disable Automatic Read-Only';
$string['help:togglefreezing_help'] = 'Disable Automatic Read-Only.';
Expand All @@ -56,5 +57,8 @@
$string['settings:weeksdelay'] = 'Weeks Delay';
$string['settings:weeksdelay:desc'] = 'The number of weeks after course end date to delay context freezing';
$string['task:freezecontext'] = 'Task to freeze course context';
$string['button:unfreezecoursecontext'] = 'Unfreeze course';
$string['lifecycle:unfreezecoursecontext'] = 'Unfreeze course context';



18 changes: 18 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,22 @@ public function fetch_course_read_only_notification(): string {

return $content;
}

/**
* Return the html for 'Unfreeze button'.
*
* @param int $courseid
* @return string
* @throws coding_exception
*/
public function show_unfreeze_button(int $courseid): string {
$context = context_course::instance($courseid);
$freezeurl = new moodle_url('/blocks/lifecycle/unfreeze.php', ['id' => $context->id]);
return html_writer::div(
'<a href="' . $freezeurl .' " class="btn btn-primary">' .
get_string('button:unfreezecoursecontext', 'block_lifecycle') .
'</a>'
);

}
}
82 changes: 82 additions & 0 deletions unfreeze.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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/>.

require_once('../../config.php');
require_once($CFG->libdir . '/adminlib.php');

// Parameters.
$contextid = required_param('id', PARAM_INT);
$confirm = optional_param('confirm', null, PARAM_INT);
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);

// Set page URL.
$PAGE->set_url('/blocks/lifecycle/unfreeze.php', ['id' => $contextid]);

// Get context information.
list($context, $course, $cm) = get_context_info_array($contextid);

// Check user is logged in and enrolled in the course.
require_login($course, false);

// Check permissions.
require_capability('block/lifecycle:unfreezecoursecontext', $context);

// Check if the context is locked. We only allow unlocking of locked contexts.
if (!$context->locked) {
throw new moodle_exception('error:courseisnotreadonly', 'block_lifecycle');
}

// It is a course context and not the site course.
if ($course && $course->id != SITEID) {
$PAGE->set_pagelayout('admin');
$PAGE->navigation->clear_cache();

if (null !== $confirm && confirm_sesskey()) {
// Set context lock status.
$context->set_locked(!empty($confirm));

// Prepare message.
$lockmessage = '';

$a = (object)['contextname' => $context->get_context_name()];
$lockmessage = get_string('managecontextlockunlocked', 'admin', $a);

// Set return URL.
$returnurl = empty($returnurl) ? $context->get_url() : new moodle_url($returnurl);

// Redirect with message.
redirect($returnurl, $lockmessage);
}

// Set page title and heading.
$heading = get_string('managecontextlock', 'admin');
$PAGE->set_title($heading);
$PAGE->set_heading($heading);

// Display header.
echo $OUTPUT->header();

// Display confirmation message.
$confirmstring = get_string('confirmcontextunlock', 'admin', (object)['contextname' => $context->get_context_name()]);
$confirmurl = new moodle_url($PAGE->url, ['confirm' => 0]);
if (!empty($returnurl)) {
$confirmurl->param('returnurl', $returnurl);
}
echo $OUTPUT->confirm($confirmstring, $confirmurl, $context->get_url());

// Display footer.
echo $OUTPUT->footer();
}
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 = 2022120800;
$plugin->version = 2024021501;
$plugin->release = '0.1';
$plugin->maturity = MATURITY_ALPHA;
$plugin->requires = 2020061512;
Expand Down

0 comments on commit 908d82f

Please sign in to comment.