From 8511e518762e1ba49f20ade8f4eaf475f95a4771 Mon Sep 17 00:00:00 2001 From: Gareth Barnard <1058419+gjb2048@users.noreply.github.com> Date: Sat, 14 Oct 2023 12:58:37 +0100 Subject: [PATCH] CC --- .../backup_format_grid_plugin.class.php | 1 - .../restore_format_grid_plugin.class.php | 45 +++-- classes/admin_setting_information.php | 19 ++- classes/admin_setting_markdown.php | 7 +- classes/courseformat/stateactions.php | 1 - classes/observer.php | 1 - classes/output/courseformat/content.php | 46 +++-- .../output/courseformat/content/section.php | 1 - .../content/section/controlmenu.php | 1 - .../courseformat/content/section/summary.php | 21 ++- classes/output/renderer.php | 1 - classes/privacy/provider.php | 3 +- classes/toolbox.php | 159 +++++++++++++----- db/upgrade.php | 31 +++- form/sectionfilemanager.php | 49 ++++-- format.php | 4 +- lib.php | 63 ++++--- settings.php | 34 ++-- 18 files changed, 327 insertions(+), 160 deletions(-) diff --git a/backup/moodle2/backup_format_grid_plugin.class.php b/backup/moodle2/backup_format_grid_plugin.class.php index a06a1007..a019aa21 100644 --- a/backup/moodle2/backup_format_grid_plugin.class.php +++ b/backup/moodle2/backup_format_grid_plugin.class.php @@ -30,7 +30,6 @@ * Provides the information to backup grid course format */ class backup_format_grid_plugin extends backup_format_plugin { - /** * Returns the format information to attach to course element */ diff --git a/backup/moodle2/restore_format_grid_plugin.class.php b/backup/moodle2/restore_format_grid_plugin.class.php index c09b1b2b..8c9ce34e 100644 --- a/backup/moodle2/restore_format_grid_plugin.class.php +++ b/backup/moodle2/restore_format_grid_plugin.class.php @@ -33,7 +33,6 @@ * needed to restore one grid format course. */ class restore_format_grid_plugin extends restore_format_plugin { - /** @var int */ protected $originalnumsections = 0; @@ -50,7 +49,8 @@ protected function define_course_plugin_structure() { global $DB; $maxsection = $DB->get_field_sql( 'SELECT max(section) FROM {course_sections} WHERE course = ?', - [$task->get_courseid()]); + [$task->get_courseid()] + ); $this->originalnumsections = (int)$maxsection; } @@ -88,8 +88,12 @@ public function process_grid($data) { $data->courseid = $courseid; if (!($course = $DB->get_record('course', ['id' => $data->courseid]))) { - throw new \moodle_exception('invalidcourseid', 'format_grid', '', - get_string('invalidcourseid', 'error')); + throw new \moodle_exception( + 'invalidcourseid', + 'format_grid', + '', + get_string('invalidcourseid', 'error') + ); } // From /course/view.php. // No need to annotate anything here. } @@ -133,8 +137,12 @@ public function after_restore_course() { $filerecord->filename = $filename; $newfile = $fs->create_file_from_storedfile($filerecord, $file); if ($newfile) { - $DB->set_field('format_grid_image', 'contenthash', $newfile->get_contenthash(), - ['sectionid' => $filesectionid]); + $DB->set_field( + 'format_grid_image', + 'contenthash', + $newfile->get_contenthash(), + ['sectionid' => $filesectionid] + ); } } } @@ -167,8 +175,10 @@ public function after_restore_course() { if ($this->step->get_task()->get_setting_value($key . '_included')) { $sectionnum = (int)$section->title; if ($sectionnum > $settings['numsections'] && $sectionnum > $this->originalnumsections) { - $DB->execute("UPDATE {course_sections} SET visible = 0 WHERE course = ? AND section = ?", - [$this->step->get_task()->get_courseid(), $sectionnum]); + $DB->execute( + "UPDATE {course_sections} SET visible = 0 WHERE course = ? AND section = ?", + [$this->step->get_task()->get_courseid(), $sectionnum] + ); } } } @@ -198,10 +208,12 @@ public function process_gridsection($data) { $data = (object) $data; $target = $this->step->get_task()->get_target(); - if (($target == backup::TARGET_NEW_COURSE) || + if ( + ($target == backup::TARGET_NEW_COURSE) || ($target == backup::TARGET_CURRENT_ADDING) || ($target == backup::TARGET_CURRENT_DELETING) || - ($target == backup::TARGET_EXISTING_DELETING)) { + ($target == backup::TARGET_EXISTING_DELETING) + ) { /* This ensures that when a course is created from an uploaded CSV file that the number of sections is correct. Thus when an existing course or course file is used but the course restore code is not called. Because the backup file / course being restored from has the correct 'sections', i.e. that will be in the @@ -216,11 +228,13 @@ public function process_gridsection($data) { /* Allow this to process even if not in the grid format so that our event observer on 'course_restored' can perform a clean up of restored grid image files after all the data is in place in the database for this to happen properly. */ - if (($target == backup::TARGET_NEW_COURSE) || + if ( + ($target == backup::TARGET_NEW_COURSE) || ($target == backup::TARGET_CURRENT_DELETING) || ($target == backup::TARGET_EXISTING_DELETING) || ($target == backup::TARGET_CURRENT_ADDING) || - ($target == backup::TARGET_EXISTING_ADDING)) { // All of them, but just in case a new one is added! + ($target == backup::TARGET_EXISTING_ADDING) + ) { // All of them, but just in case a new one is added! if (empty($data->contenthash)) { // Less than M4.0 backup file. if (!empty($data->imagepath)) { @@ -233,8 +247,10 @@ public function process_gridsection($data) { if (!empty($data->image)) { $newsectionid = $this->task->get_sectionid(); $existinggridimage = false; - if (($target == backup::TARGET_CURRENT_ADDING) || - ($target == backup::TARGET_EXISTING_ADDING)) { + if ( + ($target == backup::TARGET_CURRENT_ADDING) || + ($target == backup::TARGET_EXISTING_ADDING) + ) { $existinggridimage = $DB->get_record('format_grid_image', ['sectionid' => $newsectionid], 'image'); } if (!$existinggridimage) { @@ -256,6 +272,5 @@ public function process_gridsection($data) { } } } - } } diff --git a/classes/admin_setting_information.php b/classes/admin_setting_information.php index 3b2e5ca8..0945b5c3 100644 --- a/classes/admin_setting_information.php +++ b/classes/admin_setting_information.php @@ -34,7 +34,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. */ class admin_setting_information extends \admin_setting { - /** @var int The branch this is for. */ protected $mbranch; @@ -88,7 +87,7 @@ public function write_setting($data) { * @param string $query * @return string Returns an HTML string */ - public function output_html($data, $query='') { + public function output_html($data, $query = '') { global $CFG, $OUTPUT; $formats = \core_plugin_manager::instance()->get_present_plugins('format'); @@ -106,7 +105,9 @@ public function output_html($data, $query='') { $attributes['title'] = get_string('love', 'format_grid'); $content = \html_writer::tag('span', $attributes['title'], ['class' => 'sr-only']); $content = \html_writer::tag('span', $content, $attributes); - $context['versioninfo'] = get_string('versioninfo', 'format_grid', + $context['versioninfo'] = get_string( + 'versioninfo', + 'format_grid', [ 'moodle' => $CFG->release, 'release' => $plugininfo->release, @@ -120,25 +121,25 @@ public function output_html($data, $query='') { case MATURITY_ALPHA: $context['maturity'] = get_string('versionalpha', 'format_grid'); $context['maturityalert'] = 'danger'; - break; + break; case MATURITY_BETA: $context['maturity'] = get_string('versionbeta', 'format_grid'); $context['maturityalert'] = 'danger'; - break; + break; case MATURITY_RC: $context['maturity'] = get_string('versionrc', 'format_grid'); $context['maturityalert'] = 'warning'; - break; + break; case MATURITY_STABLE: $context['maturity'] = get_string('versionstable', 'format_grid'); $context['maturityalert'] = 'info'; - break; + break; } } if ($CFG->branch != $this->mbranch) { - $context['versioncheck'] = 'Release '.$plugininfo->release.', version '.$plugininfo->version; - $context['versioncheck'] .= ' is incompatible with Moodle '.$CFG->release; + $context['versioncheck'] = 'Release ' . $plugininfo->release . ', version ' . $plugininfo->version; + $context['versioncheck'] .= ' is incompatible with Moodle ' . $CFG->release; $context['versioncheck'] .= ', please get the correct version from '; $context['versioncheck'] .= 'Moodle.org. '; $context['versioncheck'] .= 'If none is available, then please consider supporting the format by funding it. '; diff --git a/classes/admin_setting_markdown.php b/classes/admin_setting_markdown.php index 1c1a62de..eee9d11e 100644 --- a/classes/admin_setting_markdown.php +++ b/classes/admin_setting_markdown.php @@ -34,7 +34,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. */ class admin_setting_markdown extends \admin_setting { - /** @var string Filename */ private $filename; @@ -88,7 +87,7 @@ public function write_setting($data) { * @param string $query * @return string Returns an HTML string */ - public function output_html($data, $query='') { + public function output_html($data, $query = '') { global $CFG, $OUTPUT; $context = new \stdClass(); @@ -96,9 +95,9 @@ public function output_html($data, $query='') { $context->description = $this->description; if (file_exists("{$CFG->dirroot}/course/format/grid/{$this->filename}")) { - $filecontents = file_get_contents($CFG->dirroot.'/course/format/grid/'.$this->filename); + $filecontents = file_get_contents($CFG->dirroot . '/course/format/grid/' . $this->filename); } else { - $filecontents = 'Grid format admin_setting_markdown -> file not found: '.$this->filename; + $filecontents = 'Grid format admin_setting_markdown -> file not found: ' . $this->filename; } $context->markdown = format_text($filecontents, FORMAT_MARKDOWN); diff --git a/classes/courseformat/stateactions.php b/classes/courseformat/stateactions.php index 1276768c..2b4eeaab 100644 --- a/classes/courseformat/stateactions.php +++ b/classes/courseformat/stateactions.php @@ -31,7 +31,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class stateactions extends stateactions_base { - /** * Create a course section. * diff --git a/classes/observer.php b/classes/observer.php index 5d058cee..265894e4 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -31,7 +31,6 @@ * Event observers supported by this format. */ class format_grid_observer { - /** * Observer for the event course_content_deleted. * diff --git a/classes/output/courseformat/content.php b/classes/output/courseformat/content.php index 6d980152..f02531ab 100644 --- a/classes/output/courseformat/content.php +++ b/classes/output/courseformat/content.php @@ -38,7 +38,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class content extends content_base { - private $sectioncompletionpercentage = []; private $sectioncompletionmarkup = []; private $sectioncompletioncalculated = []; @@ -115,8 +114,14 @@ public function export_for_template(\renderer_base $output) { $fs = get_file_storage(); $coursecontext = \context_course::instance($course->id); foreach ($coursesectionimages as $coursesectionimage) { - $replacement = $toolbox->check_displayed_image($coursesectionimage, $course->id, $coursecontext->id, - $coursesectionimage->sectionid, $format, $fs); + $replacement = $toolbox->check_displayed_image( + $coursesectionimage, + $course->id, + $coursecontext->id, + $coursesectionimage->sectionid, + $format, + $fs + ); if (!empty($replacement)) { $coursesectionimages[$coursesectionimage->id] = $replacement; } @@ -162,10 +167,14 @@ public function export_for_template(\renderer_base $output) { // Do we have an image? if ((array_key_exists($section->id, $sectionimages)) && ($sectionimages[$section->id]->displayedimagestate >= 1)) { $sectionimages[$section->id]->imageuri = $toolbox->get_displayed_image_uri( - $sectionimages[$section->id], $coursecontext->id, $section->id, $displayediswebp); + $sectionimages[$section->id], + $coursecontext->id, + $section->id, + $displayediswebp + ); } else { // No. - $sectionimages[$section->id] = new stdClass; + $sectionimages[$section->id] = new stdClass(); $sectionimages[$section->id]->generatedimageuri = $output->get_generated_image_for_id($section->id); } // Number. @@ -206,7 +215,7 @@ public function export_for_template(\renderer_base $output) { // Section break. if ($sectionformatoptions['sectionbreak'] == 2) { // Yes. $sectionimages[$section->id]->sectionbreak = true; - if (!empty ($sectionformatoptions['sectionbreakheading'])) { + if (!empty($sectionformatoptions['sectionbreakheading'])) { // Note: As a PARAM_TEXT, then does need to be passed through 'format_string' for multi-lang or not? $sectionimages[$section->id]->sectionbreakheading = format_text( $sectionformatoptions['sectionbreakheading'], @@ -287,10 +296,16 @@ protected function get_grid_sections(\renderer_base $output, $settings): array { foreach ($sectioninfos as $thissection) { // The course/view.php check the section existence but the output can be called from other parts so we need to check it. if (!$thissection) { - throw new \moodle_exception('unknowncoursesection', 'error', '', - get_string('unknowncoursesection', 'error', - course_get_url($course).' - '.format_string($course->fullname)) - ); + throw new \moodle_exception( + 'unknowncoursesection', + 'error', + '', + get_string( + 'unknowncoursesection', + 'error', + course_get_url($course) . ' - ' . format_string($course->fullname) + ) + ); } if ($thissection->section > $numsections) { @@ -301,7 +316,7 @@ protected function get_grid_sections(\renderer_base $output, $settings): array { continue; } - $section = new stdClass; + $section = new stdClass(); $section->id = $thissection->id; $section->num = $thissection->section; $section->name = $output->section_title_without_link($thissection, $course); @@ -349,8 +364,10 @@ protected function calculate_section_activity_completion($section, $course, $mod if ($completioninfo->is_enabled($thismod) != COMPLETION_TRACKING_NONE) { $total++; $completiondata = $completioninfo->get_data($thismod, true); - if ($completiondata->completionstate == COMPLETION_COMPLETE || - $completiondata->completionstate == COMPLETION_COMPLETE_PASS) { + if ( + $completiondata->completionstate == COMPLETION_COMPLETE || + $completiondata->completionstate == COMPLETION_COMPLETE_PASS + ) { $complete++; } } @@ -389,7 +406,8 @@ protected function calculate_section_activity_completion($section, $course, $mod } else { $data->percentagequarter = 4; } - $this->sectioncompletionmarkup[$section->section] = $output->render_from_template('format_grid/grid_completion', $data); + $this->sectioncompletionmarkup[$section->section] = + $output->render_from_template('format_grid/grid_completion', $data); } $this->sectioncompletioncalculated[$section->section] = true; diff --git a/classes/output/courseformat/content/section.php b/classes/output/courseformat/content/section.php index 49eb82f5..4c66605b 100644 --- a/classes/output/courseformat/content/section.php +++ b/classes/output/courseformat/content/section.php @@ -42,7 +42,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class section extends section_base { - /** @var course_format the course format */ protected $format; diff --git a/classes/output/courseformat/content/section/controlmenu.php b/classes/output/courseformat/content/section/controlmenu.php index caa0f78a..0efd3e19 100644 --- a/classes/output/courseformat/content/section/controlmenu.php +++ b/classes/output/courseformat/content/section/controlmenu.php @@ -42,7 +42,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class controlmenu extends controlmenu_base { - /** @var course_format the course format class */ protected $format; diff --git a/classes/output/courseformat/content/section/summary.php b/classes/output/courseformat/content/section/summary.php index 048cb381..18d2d6ae 100644 --- a/classes/output/courseformat/content/section/summary.php +++ b/classes/output/courseformat/content/section/summary.php @@ -43,7 +43,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class summary extends summary_base { - /** @var section_info the course section class - core is 'private' */ private $thesection; @@ -92,8 +91,8 @@ protected function singlepagesummaryimage($summary, $output): string { $o = $summary; $coursesettings = $this->format->get_settings(); if ($coursesettings['singlepagesummaryimage'] > 1) { // I.e. not 'off'. - $data = new stdClass; - switch($coursesettings['singlepagesummaryimage']) { + $data = new stdClass(); + switch ($coursesettings['singlepagesummaryimage']) { case 2: $data->left = true; break; @@ -117,8 +116,14 @@ protected function singlepagesummaryimage($summary, $output): string { $fs = get_file_storage(); $coursecontext = \context_course::instance($courseid); $toolbox = \format_grid\toolbox::get_instance(); - $replacement = $toolbox->check_displayed_image($coursesectionimage, $courseid, $coursecontext->id, $sectionid, - $this->format, $fs); + $replacement = $toolbox->check_displayed_image( + $coursesectionimage, + $courseid, + $coursecontext->id, + $sectionid, + $this->format, + $fs + ); if (!empty($replacement)) { $coursesectionimage = $replacement; } @@ -127,7 +132,11 @@ protected function singlepagesummaryimage($summary, $output): string { // Yes. $displayediswebp = (get_config('format_grid', 'defaultdisplayedimagefiletype') == 2); $data->imageuri = $toolbox->get_displayed_image_uri( - $coursesectionimage, $coursecontext->id, $sectionid, $displayediswebp); + $coursesectionimage, + $coursecontext->id, + $sectionid, + $displayediswebp + ); $sectionformatoptions = $this->format->get_format_options($this->thesection); $data->alttext = $sectionformatoptions['sectionimagealttext']; diff --git a/classes/output/renderer.php b/classes/output/renderer.php index 65c4d8c8..c2ec893b 100644 --- a/classes/output/renderer.php +++ b/classes/output/renderer.php @@ -38,7 +38,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class renderer extends section_renderer { - /** * Constructor method, calls the parent constructor. * diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php index 9a3d261a..473de8b7 100644 --- a/classes/privacy/provider.php +++ b/classes/privacy/provider.php @@ -33,14 +33,13 @@ * The Grid format does not store any user data. */ class provider implements \core_privacy\local\metadata\null_provider { - /** * Get the language string identifier with the component's language * file to explain why this plugin stores no data. * * @return string */ - public static function get_reason() : string { + public static function get_reason(): string { return 'privacy:nop'; } } diff --git a/classes/toolbox.php b/classes/toolbox.php index fcc0e607..032610fc 100644 --- a/classes/toolbox.php +++ b/classes/toolbox.php @@ -123,7 +123,7 @@ public function get_displayed_image_uri($coursesectionimage, $coursecontextid, $ 'format_grid', 'displayedsectionimage', $sectionid, - '/'.$coursesectionimage->displayedimagestate.'/', + '/' . $coursesectionimage->displayedimagestate . '/', $filename ); return $image->out(); @@ -147,7 +147,7 @@ public function check_displayed_image($sectionimage, $courseid, $coursecontextid $lock = true; if (!defined('BEHAT_SITE_RUNNING')) { $lockfactory = \core\lock\lock_config::get_lock_factory('format_grid'); - $lock = $lockfactory->get_lock('sectionid'.$sectionid, 5); + $lock = $lockfactory->get_lock('sectionid' . $sectionid, 5); } if ($lock) { $files = $fs->get_area_files($coursecontextid, 'format_grid', 'sectionimage', $sectionimage->sectionid); @@ -165,7 +165,10 @@ public function check_displayed_image($sectionimage, $courseid, $coursecontextid $lock->release(); } } else { - throw new \moodle_exception('cannotgetimagelock', 'format_grid', '', + throw new \moodle_exception( + 'cannotgetimagelock', + 'format_grid', + '', get_string('cannotgetmanagesectionimagelock', 'format_grid') ); } @@ -198,7 +201,7 @@ public function setup_displayed_image($sectionimage, $sectionfile, $courseid, $s $filetype = strtolower(pathinfo($filename ?? '', PATHINFO_EXTENSION)); if ((!empty($filetype)) && ($filetype == 'webp')) { $mime = 'image/webp'; - $updatedrecord = new \stdClass; + $updatedrecord = new \stdClass(); $updatedrecord->id = $sectionfile->get_id(); $updatedrecord->mimetype = $mime; $DB->update_record('files', $updatedrecord); @@ -227,8 +230,14 @@ public function setup_displayed_image($sectionimage, $sectionfile, $courseid, $s 'filename' => $filename, 'sectionid' => $sectionid, ]; - $data = self::generate_image($tmpfilepath, $displayedimageinfo['width'], $displayedimageinfo['height'], $crop, $newmime, - $debugdata); + $data = self::generate_image( + $tmpfilepath, + $displayedimageinfo['width'], + $displayedimageinfo['height'], + $crop, + $newmime, + $debugdata + ); if (!empty($data)) { // Updated image. $coursecontext = \context_course::instance($courseid); @@ -259,7 +268,7 @@ public function setup_displayed_image($sectionimage, $sectionfile, $courseid, $s if ($isdisplayedwebponly) { // Displayed WebP image from non-WebP original. // Displayed image is a webp image from the original, so change a few things. - $displayedimagefilerecord['filename'] = $displayedimagefilerecord['filename'].'.webp'; + $displayedimagefilerecord['filename'] = $displayedimagefilerecord['filename'] . '.webp'; $displayedimagefilerecord['mimetype'] = $newmime; } $fs->create_file_from_string($displayedimagefilerecord, $data); @@ -269,14 +278,23 @@ public function setup_displayed_image($sectionimage, $sectionfile, $courseid, $s } unlink($tmpfilepath); - $DB->set_field('format_grid_image', 'displayedimagestate', $sectionimage->displayedimagestate, - ['sectionid' => $sectionid]); + $DB->set_field( + 'format_grid_image', + 'displayedimagestate', + $sectionimage->displayedimagestate, + ['sectionid' => $sectionid] + ); if ($sectionimage->displayedimagestate == -1) { - throw new \moodle_exception('cannotconvertuploadedimagetodisplayedimage', 'format_grid', '', - get_string('cannotconvertuploadedimagetodisplayedimage', 'format_grid', - $CFG->wwwroot."/course/view.php?id=".$courseid. - ', SI: '.var_export($displayedimagefilerecord, true). - ', DII: '.var_export($displayedimageinfo, true) + throw new \moodle_exception( + 'cannotconvertuploadedimagetodisplayedimage', + 'format_grid', + '', + get_string( + 'cannotconvertuploadedimagetodisplayedimage', + 'format_grid', + $CFG->wwwroot . "/course/view.php?id=" . $courseid . + ', SI: ' . var_export($displayedimagefilerecord, true) . + ', DII: ' . var_export($displayedimageinfo, true) ) ); } @@ -373,8 +391,13 @@ private static function generate_image($filepath, $requestedwidth, $requestedhei if (empty($imageinfo)) { unlink($filepath); - throw new \moodle_exception('noimageinformation', 'format_grid', '', - get_string('noimageinformation', 'format_grid', + throw new \moodle_exception( + 'noimageinformation', + 'format_grid', + '', + get_string( + 'noimageinformation', + 'format_grid', self::debugdata_decode($debugdata) ) ); @@ -385,16 +408,26 @@ private static function generate_image($filepath, $requestedwidth, $requestedhei if (empty($originalheight)) { unlink($filepath); - throw new \moodle_exception('originalheightempty', 'format_grid', '', - get_string('originalheightempty', 'format_grid', + throw new \moodle_exception( + 'originalheightempty', + 'format_grid', + '', + get_string( + 'originalheightempty', + 'format_grid', self::debugdata_decode($debugdata) ) ); } if (empty($originalwidth)) { unlink($filepath); - throw new \moodle_exception('originalwidthempty', 'format_grid', '', - get_string('originalwidthempty', 'format_grid', + throw new \moodle_exception( + 'originalwidthempty', + 'format_grid', + '', + get_string( + 'originalwidthempty', + 'format_grid', self::debugdata_decode($debugdata) ) ); @@ -413,9 +446,14 @@ private static function generate_image($filepath, $requestedwidth, $requestedhei $imageargs[3] = PNG_NO_FILTER; // Filter. } else { unlink($filepath); - throw new \moodle_exception('formatnotsupported', 'format_grid', '', - get_string('formatnotsupported', 'format_grid', - 'PNG, '.self::debugdata_decode($debugdata) + throw new \moodle_exception( + 'formatnotsupported', + 'format_grid', + '', + get_string( + 'formatnotsupported', + 'format_grid', + 'PNG, ' . self::debugdata_decode($debugdata) ) ); } @@ -426,9 +464,14 @@ private static function generate_image($filepath, $requestedwidth, $requestedhei $imageargs[2] = 90; // Quality. } else { unlink($filepath); - throw new \moodle_exception('formatnotsupported', 'format_grid', '', - get_string('formatnotsupported', 'format_grid', - 'JPG, '.self::debugdata_decode($debugdata) + throw new \moodle_exception( + 'formatnotsupported', + 'format_grid', + '', + get_string( + 'formatnotsupported', + 'format_grid', + 'JPG, ' . self::debugdata_decode($debugdata) ) ); } @@ -441,9 +484,14 @@ private static function generate_image($filepath, $requestedwidth, $requestedhei $imageargs[2] = 90; // Quality. } else { unlink($filepath); - throw new \moodle_exception('formatnotsupported', 'format_grid', '', - get_string('formatnotsupported', 'format_grid', - 'WEBP, '.self::debugdata_decode($debugdata) + throw new \moodle_exception( + 'formatnotsupported', + 'format_grid', + '', + get_string( + 'formatnotsupported', + 'format_grid', + 'WEBP, ' . self::debugdata_decode($debugdata) ) ); } @@ -453,18 +501,28 @@ private static function generate_image($filepath, $requestedwidth, $requestedhei $imagefnc = 'imagegif'; } else { unlink($filepath); - throw new \moodle_exception('formatnotsupported', 'format_grid', '', - get_string('formatnotsupported', 'format_grid', - 'GIF, '.self::debugdata_decode($debugdata) + throw new \moodle_exception( + 'formatnotsupported', + 'format_grid', + '', + get_string( + 'formatnotsupported', + 'format_grid', + 'GIF, ' . self::debugdata_decode($debugdata) ) ); } break; default: unlink($filepath); - throw new \moodle_exception('mimetypenotsupported', 'format_grid', '', - get_string('mimetypenotsupported', 'format_grid', - $mime.', '.self::debugdata_decode($debugdata) + throw new \moodle_exception( + 'mimetypenotsupported', + 'format_grid', + '', + get_string( + 'mimetypenotsupported', + 'format_grid', + $mime . ', ' . self::debugdata_decode($debugdata) ) ); } @@ -559,9 +617,14 @@ private static function generate_image($filepath, $requestedwidth, $requestedhei if (!call_user_func_array($imagefnc, $imageargs)) { ob_end_clean(); unlink($filepath); - throw new \moodle_exception('functionfailed', 'format_grid', '', - get_string('functionfailed', 'format_grid', - $imagefnc.', '.self::debugdata_decode($debugdata) + throw new \moodle_exception( + 'functionfailed', + 'format_grid', + '', + get_string( + 'functionfailed', + 'format_grid', + $imagefnc . ', ' . self::debugdata_decode($debugdata) ) ); } @@ -574,9 +637,9 @@ private static function generate_image($filepath, $requestedwidth, $requestedhei } private static function debugdata_decode($debugdata) { - $o = 'itemid > '.$debugdata['itemid']; - $o .= ', filename > '.$debugdata['filename']; - $o .= ' and sectionid > '.$debugdata['sectionid'].'. '; + $o = 'itemid > ' . $debugdata['itemid']; + $o .= ', filename > ' . $debugdata['filename']; + $o .= ' and sectionid > ' . $debugdata['sectionid'] . '. '; $o .= get_string('reporterror', 'format_grid'); return $o; @@ -628,7 +691,7 @@ private static function update_the_displayed_images($courseid = null) { } $coursecontext = \context_course::instance($courseid); if (!defined('BEHAT_SITE_RUNNING')) { - $lock = $lockfactory->get_lock('sectionid'.$coursesectionimage->sectionid, 5); + $lock = $lockfactory->get_lock('sectionid' . $coursesectionimage->sectionid, 5); } if ($lock) { $files = $fs->get_area_files($coursecontext->id, 'format_grid', 'sectionimage', $coursesectionimage->sectionid); @@ -652,7 +715,10 @@ private static function update_the_displayed_images($courseid = null) { $lock->release(); } } else { - throw new \moodle_exception('cannotgetimagelock', 'format_grid', '', + throw new \moodle_exception( + 'cannotgetimagelock', + 'format_grid', + '', get_string('cannotgetmanagesectionimagelock', 'format_grid') ); } @@ -702,7 +768,7 @@ public static function delete_image($sectionid, $courseid) { $lock = true; if (!defined('BEHAT_SITE_RUNNING')) { $lockfactory = \core\lock\lock_config::get_lock_factory('format_grid'); - $lock = $lockfactory->get_lock('sectionid'.$coursesectionimage->sectionid, 5); + $lock = $lockfactory->get_lock('sectionid' . $coursesectionimage->sectionid, 5); } if ($lock) { $coursecontext = \context_course::instance($courseid); @@ -729,7 +795,10 @@ public static function delete_image($sectionid, $courseid) { $lock->release(); } } else { - throw new \moodle_exception('cannotgetimagelock', 'format_grid', '', + throw new \moodle_exception( + 'cannotgetimagelock', + 'format_grid', + '', get_string('cannotgetmanagesectionimagelock', 'format_grid') ); } diff --git a/db/upgrade.php b/db/upgrade.php index 379d7047..91d66d5d 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -107,9 +107,11 @@ function xmldb_format_grid_upgrade($oldversion = 0) { } else { $filename = $file->get_filename(); $filesectionid = $file->get_itemid(); - if (array_key_exists($filesectionid, $newimagecoursearray)) { // Ensure we know about this section. + // Ensure we know about this section. + if (array_key_exists($filesectionid, $newimagecoursearray)) { $gridimage = $newimagecoursearray[$filesectionid]; - if (($gridimage) && ($gridimage->image == $filename)) { // Ensure the correct file. + // Ensure the correct file. + if (($gridimage) && ($gridimage->image == $filename)) { $filerecord = new stdClass(); $filerecord->contextid = $coursecontext->id; $filerecord->component = 'format_grid'; @@ -126,14 +128,19 @@ function xmldb_format_grid_upgrade($oldversion = 0) { $filerecord->filearea, $filerecord->itemid, $filerecord->filepath, - $filerecord->filename); + $filerecord->filename + ); } if ($thefile === false) { $thefile = $fs->create_file_from_storedfile($filerecord, $file); } if ($thefile !== false) { - $DB->set_field('format_grid_image', 'contenthash', - $thefile->get_contenthash(), ['sectionid' => $filesectionid]); + $DB->set_field( + 'format_grid_image', + 'contenthash', + $thefile->get_contenthash(), + ['sectionid' => $filesectionid] + ); // Don't delete the section file in case used in the summary. } } @@ -169,19 +176,25 @@ function xmldb_format_grid_upgrade($oldversion = 0) { if ($oldversion < 2023051001) { // Has the upgrade already happened? Thus in versions for Moodle 4.1. - $records = $DB->get_records('course_format_options', + $records = $DB->get_records( + 'course_format_options', [ 'format' => 'grid', 'name' => 'gnumsections', - ], '', 'id' + ], + '', + 'id' ); if (empty($records)) { - $records = $DB->get_records('course_format_options', + $records = $DB->get_records( + 'course_format_options', [ 'format' => 'grid', 'name' => 'numsections', - ], '', 'id' + ], + '', + 'id' ); $records = array_keys($records); diff --git a/form/sectionfilemanager.php b/form/sectionfilemanager.php index c6c0656c..e78eafde 100644 --- a/form/sectionfilemanager.php +++ b/form/sectionfilemanager.php @@ -26,16 +26,15 @@ global $CFG; -require_once($CFG->dirroot."/lib/form/filemanager.php"); +require_once($CFG->dirroot . "/lib/form/filemanager.php"); class MoodleQuickForm_sectionfilemanager extends MoodleQuickForm_filemanager implements templatable { - - private static $options = array( + private static $options = [ 'maxfiles' => 1, 'subdirs' => 0, - 'accepted_types' => array('gif', 'jpe', 'jpeg', 'jpg', 'png', 'webp'), - 'return_types' => FILE_INTERNAL - ); + 'accepted_types' => ['gif', 'jpe', 'jpeg', 'jpg', 'png', 'webp'], + 'return_types' => FILE_INTERNAL, + ]; /** * Constructor @@ -45,8 +44,8 @@ class MoodleQuickForm_sectionfilemanager extends MoodleQuickForm_filemanager imp * @param array $attributes (optional) Either a typical HTML attribute string * or an associative array */ - public function __construct($elementName=null, $elementLabel=null, $attributes=null) { - parent::__construct($elementName, $elementLabel, $attributes, self::$options); + public function __construct($elementname = null, $elementlabel = null, $attributes = null) { + parent::__construct($elementname, $elementlabel, $attributes, self::$options); } /** @@ -55,7 +54,7 @@ public function __construct($elementName=null, $elementLabel=null, $attributes=n * @param int $value Draft item id with the uploaded files. * @return string|null Validation error message or null. */ - public function validateSubmitValue($value) { + public function validatesubmitvalue($value) { $failure = parent::validateSubmitValue($value); if (!$failure) { $course = $this->getAttribute('course'); @@ -66,7 +65,7 @@ public function validateSubmitValue($value) { $lock = true; if (!defined('BEHAT_SITE_RUNNING')) { $lockfactory = \core\lock\lock_config::get_lock_factory('format_grid'); - $lock = $lockfactory->get_lock('sectionid'.$sectionid, 5); + $lock = $lockfactory->get_lock('sectionid' . $sectionid, 5); } if ($lock) { $fs = get_file_storage(); @@ -74,8 +73,14 @@ public function validateSubmitValue($value) { $indata->sectionimage_filemanager = $value; // The file manager deals with the files table when the image is deleted. $outdata = file_postupdate_standard_filemanager( - $indata, 'sectionimage', self::$options, $coursecontext, - 'format_grid', 'sectionimage', $sectionid); + $indata, + 'sectionimage', + self::$options, + $coursecontext, + 'format_grid', + 'sectionimage', + $sectionid + ); global $DB; if ($outdata->sectionimage == '1') { // We have draft file(s), however they could also be left over ones! @@ -84,7 +89,7 @@ public function validateSubmitValue($value) { $sectionimage = $DB->get_record_select( 'format_grid_image', 'courseid = ? AND sectionid = ?', - array($course->id, $sectionid) + [$course->id, $sectionid] ); $havefiles = false; $havechangedfiles = false; @@ -152,7 +157,10 @@ public function validateSubmitValue($value) { $lock->release(); } } else { - throw new \moodle_exception('cannotgetimagelock', 'format_grid', '', + throw new \moodle_exception( + 'cannotgetimagelock', + 'format_grid', + '', get_string('cannotgetmanagesectionimagelock', 'format_grid') ); } @@ -175,7 +183,7 @@ private function delete_existing_image($coursecontextid, $courseid, $sectionid, global $DB; try { - $DB->delete_records('format_grid_image', array('courseid' => $courseid, 'sectionid' => $sectionid)); + $DB->delete_records('format_grid_image', ['courseid' => $courseid, 'sectionid' => $sectionid]); } catch (\Exception $e) { if (!defined('BEHAT_SITE_RUNNING')) { $lock->release(); @@ -196,7 +204,7 @@ private function delete_existing_image($coursecontextid, $courseid, $sectionid, * * @return string */ - public function toHtml() { + public function tohtml() { $this->init(); return parent::toHtml(); } @@ -210,7 +218,14 @@ private function init() { $coursecontext = context_course::instance($course->id); $fmd = file_prepare_standard_filemanager( - $course, 'sectionimage', self::$options, $coursecontext, 'format_grid', 'sectionimage', $sectionid); + $course, + 'sectionimage', + self::$options, + $coursecontext, + 'format_grid', + 'sectionimage', + $sectionid + ); $this->setValue($fmd->sectionimage_filemanager); } } diff --git a/format.php b/format.php index 6e31aeed..23c700e4 100755 --- a/format.php +++ b/format.php @@ -30,8 +30,8 @@ */ defined('MOODLE_INTERNAL') || die(); -require_once($CFG->libdir.'/filelib.php'); -require_once($CFG->libdir.'/completionlib.php'); +require_once($CFG->libdir . '/filelib.php'); +require_once($CFG->libdir . '/completionlib.php'); // Horrible backwards compatible parameter aliasing. if ($topic = optional_param('topic', 0, PARAM_INT)) { diff --git a/lib.php b/lib.php index 7682d350..b9b7124e 100755 --- a/lib.php +++ b/lib.php @@ -86,10 +86,12 @@ public function get_settings($invalidate = false) { foreach ($this->settings as $settingname => $settingvalue) { if (isset($settingvalue)) { $settingvtype = gettype($settingvalue); - if ((($settingvtype == 'string') && ($settingvalue === '-')) || - (($settingvtype == 'integer') && ($settingvalue === 0))) { + if ( + (($settingvtype == 'string') && ($settingvalue === '-')) || + (($settingvtype == 'integer') && ($settingvalue === 0)) + ) { // Default value indicator is a hyphen or a number equal to 0. - $this->settings[$settingname] = get_config('format_grid', 'default'.$settingname); + $this->settings[$settingname] = get_config('format_grid', 'default' . $settingname); } } } @@ -123,8 +125,11 @@ public function uses_indentation(): bool { public function get_section_name($section) { $thesection = $this->get_section($section); if ((string)$thesection->name !== '') { - return format_string($thesection->name, true, - ['context' => context_course::instance($this->courseid)]); + return format_string( + $thesection->name, + true, + ['context' => context_course::instance($this->courseid)] + ); } else { return $this->get_default_section_name($thesection); } @@ -201,7 +206,7 @@ public function get_view_url($section, $options = []) { if (empty($CFG->linkcoursesections) && !empty($options['navigation'])) { return null; } - $url->set_anchor('section-'.$sectionno); + $url->set_anchor('section-' . $sectionno); } } return $url; @@ -238,8 +243,10 @@ public function extend_course_navigation($navigation, navigation_node $node) { // If section is specified in course/view.php, make sure it is expanded in navigation. if ($navigation->includesectionnum === false) { $selectedsection = optional_param('section', null, PARAM_INT); - if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') && - $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) { + if ( + $selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') && + $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE) + ) { $navigation->includesectionnum = $selectedsection; } } @@ -447,7 +454,7 @@ public function course_format_options($foreditform = false) { 0, [ 1 => new lang_string('scale', 'format_grid'), // Scale. - 2 => new lang_string('crop', 'format_grid'), // Crop. + 2 => new lang_string('crop', 'format_grid'), // Crop. ], ); $courseformatoptionsedit['imageresizemethod'] = [ @@ -507,7 +514,7 @@ public function course_format_options($foreditform = false) { * @return array Updated value array with the added default entry. */ private function generate_default_entry($settingname, $defaultindex, $values) { - $defaultvalue = get_config('format_grid', 'default'.$settingname); + $defaultvalue = get_config('format_grid', 'default' . $settingname); $defarray = [$defaultindex => new lang_string('default', 'format_grid', $values[$defaultvalue])]; return array_replace($defarray, $values); @@ -527,7 +534,8 @@ public function create_edit_form_elements(&$mform, $forsection = false) { MoodleQuickForm::registerElementType( 'sectionfilemanager', "$CFG->dirroot/course/format/grid/form/sectionfilemanager.php", - 'MoodleQuickForm_sectionfilemanager'); + 'MoodleQuickForm_sectionfilemanager' + ); $elements = parent::create_edit_form_elements($mform, $forsection); @@ -601,7 +609,8 @@ public function update_course_format_options($data, $oldcourse = null) { $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections} WHERE course = ?', [$this->courseid]); if ($numsections < $maxsection) { - // The setting gnumsections has decreased, try to completely delete the orphaned sections (unless they are not empty). + // The setting gnumsections has decreased, try to completely delete the orphaned + // sections (unless they are not empty). for ($sectionnum = $maxsection; $sectionnum > $numsections; $sectionnum--) { if (!$this->delete_section($sectionnum, false)) { break; @@ -610,14 +619,16 @@ public function update_course_format_options($data, $oldcourse = null) { } else if ($numsections > $maxsection) { // The setting gnumsections has increased then create the sections. $course = $this->get_course(); - course_create_sections_if_missing($course, range(0, $numsections )); + course_create_sections_if_missing($course, range(0, $numsections)); } } $newsettings = $this->get_settings(true); // Ensure we get the new values. - if (($currentsettings['imagecontainerwidth'] != $newsettings['imagecontainerwidth']) || - ($currentsettings['imagecontainerratio'] != $newsettings['imagecontainerratio'])) { + if ( + ($currentsettings['imagecontainerwidth'] != $newsettings['imagecontainerwidth']) || + ($currentsettings['imagecontainerratio'] != $newsettings['imagecontainerratio']) + ) { $performimagecontainersize = true; } else { $performimagecontainersize = false; @@ -719,8 +730,11 @@ public function delete_section($section, $forcedeleteifnotempty = false) { } if (!is_object($section)) { global $DB; - $section = $DB->get_record('course_sections', ['course' => $this->get_courseid(), 'section' => $section], - 'id,section,sequence,summary'); + $section = $DB->get_record( + 'course_sections', + ['course' => $this->get_courseid(), 'section' => $section], + 'id,section,sequence,summary' + ); } if (!$section || !$section->section) { // Not possible to delete 0-section. @@ -759,8 +773,13 @@ public function can_delete_section($section) { * @param null|lang_string|string $editlabel * @return \core\output\inplace_editable */ - public function inplace_editable_render_section_name($section, $linkifneeded = true, - $editable = null, $edithint = null, $editlabel = null) { + public function inplace_editable_render_section_name( + $section, + $linkifneeded = true, + $editable = null, + $edithint = null, + $editlabel = null + ) { if (empty($edithint)) { $edithint = new lang_string('editsectionname', 'format_grid'); } @@ -879,7 +898,7 @@ public static function update_displayed_images_callback() { * @param array $options additional options affecting the file serving * @return bool */ -function format_grid_pluginfile($course, $birecordorcm, $context, $filearea, $args, $forcedownload, array $options=[]) { +function format_grid_pluginfile($course, $birecordorcm, $context, $filearea, $args, $forcedownload, array $options = []) { if ($context->contextlevel != CONTEXT_COURSE) { send_file_not_found(); } @@ -923,7 +942,9 @@ function format_grid_inplace_editable($itemtype, $itemid, $newvalue) { if ($itemtype === 'sectionname' || $itemtype === 'sectionnamenl') { $section = $DB->get_record_sql( 'SELECT s.* FROM {course_sections} s JOIN {course} c ON s.course = c.id WHERE s.id = ? AND c.format = ?', - [$itemid, 'grid'], MUST_EXIST); + [$itemid, 'grid'], + MUST_EXIST + ); return course_get_format($section->course)->inplace_editable_update_section_name($section, $itemtype, $newvalue); } } diff --git a/settings.php b/settings.php index 7007e31c..671fe6a3 100644 --- a/settings.php +++ b/settings.php @@ -36,12 +36,17 @@ $ADMIN->add('formatsettings', new admin_category('format_grid', get_string('pluginname', 'format_grid'))); // Information. -$page = new admin_settingpage('format_grid_information', - get_string('information', 'format_grid')); +$page = new admin_settingpage( + 'format_grid_information', + get_string('information', 'format_grid') +); if ($ADMIN->fulltree) { - $page->add(new admin_setting_heading('format_grid_information', '', - format_text(get_string('informationsettingsdesc', 'format_grid'), FORMAT_MARKDOWN))); + $page->add(new admin_setting_heading( + 'format_grid_information', + '', + format_text(get_string('informationsettingsdesc', 'format_grid'), FORMAT_MARKDOWN) + )); // Information. $page->add(new admin_setting_information('format_grid/formatinformation', '', '', 402)); @@ -50,17 +55,26 @@ $page->add(new admin_setting_markdown('format_grid/formatsupport', '', '', 'Support.md')); // Changes.md. - $page->add(new admin_setting_markdown('format_grid/formatchanges', - get_string('informationchanges', 'format_grid'), '', 'Changes.md')); + $page->add(new admin_setting_markdown( + 'format_grid/formatchanges', + get_string('informationchanges', 'format_grid'), + '', + 'Changes.md' + )); } $ADMIN->add('format_grid', $page); // Settings. -$page = new admin_settingpage('format_grid_settings', - get_string('settings', 'format_grid')); +$page = new admin_settingpage( + 'format_grid_settings', + get_string('settings', 'format_grid') +); if ($ADMIN->fulltree) { - $page->add(new admin_setting_heading('format_grid_settings', '', - format_text(get_string('settingssettingsdesc', 'format_grid'), FORMAT_MARKDOWN))); + $page->add(new admin_setting_heading( + 'format_grid_settings', + '', + format_text(get_string('settingssettingsdesc', 'format_grid'), FORMAT_MARKDOWN) + )); // Popup. $name = 'format_grid/defaultpopup';