From 908d82f258f12eed63d8279828e62f6a16977653 Mon Sep 17 00:00:00 2001 From: Nikola Bozhkov Date: Mon, 22 Jan 2024 10:03:11 +0000 Subject: [PATCH] Add unfreeze button to lifecycle block --- block_lifecycle.php | 5 +++ db/access.php | 8 ++++ lang/en/block_lifecycle.php | 4 ++ renderer.php | 18 ++++++++ unfreeze.php | 82 +++++++++++++++++++++++++++++++++++++ version.php | 2 +- 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 unfreeze.php diff --git a/block_lifecycle.php b/block_lifecycle.php index 719f2ae..fdc44b0 100644 --- a/block_lifecycle.php +++ b/block_lifecycle.php @@ -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; diff --git a/db/access.php b/db/access.php index b320df8..d273ba1 100644 --- a/db/access.php +++ b/db/access.php @@ -57,4 +57,12 @@ 'manager' => CAP_ALLOW, ], ], + 'block/lifecycle:unfreezecoursecontext' => [ + 'captype' => 'read', + 'contextlevel' => CONTEXT_COURSE, + 'archetypes' => [ + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW, + ], + ], ]; diff --git a/lang/en/block_lifecycle.php b/lang/en/block_lifecycle.php index 1078b04..63668aa 100644 --- a/lang/en/block_lifecycle.php +++ b/lang/en/block_lifecycle.php @@ -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.'; @@ -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'; + diff --git a/renderer.php b/renderer.php index 2be3701..29639ab 100644 --- a/renderer.php +++ b/renderer.php @@ -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( + '' . + get_string('button:unfreezecoursecontext', 'block_lifecycle') . + '' + ); + + } } diff --git a/unfreeze.php b/unfreeze.php new file mode 100644 index 0000000..632f03d --- /dev/null +++ b/unfreeze.php @@ -0,0 +1,82 @@ +. + +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(); +} diff --git a/version.php b/version.php index c792932..cef4a8d 100644 --- a/version.php +++ b/version.php @@ -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;