MDL-75423 gradereport_singleview: Add read-only support

This commit is contained in:
Shamim Rezaie 2022-10-19 15:29:51 +11:00 committed by Mihail Geshoski
parent 49595ffa2f
commit 9dd8749ebd
10 changed files with 154 additions and 29 deletions

View file

@ -0,0 +1,38 @@
<?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/>.
/**
* be_readonly interface.
*
* @package gradereport_singleview
* @copyright 2022 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace gradereport_singleview\local\ui;
/**
* Simple interface implemented to add behaviour that an element can be checked to see
* if it should be read-only.
*/
interface be_readonly {
/**
* Return true if this is read-only.
*
* @return bool
*/
public function is_readonly(): bool;
}

View file

@ -39,6 +39,9 @@ class checkbox_attribute extends element {
/** @var bool $ischecked Is it checked? */ /** @var bool $ischecked Is it checked? */
private $ischecked; private $ischecked;
/** @var bool If this is a read-only input. */
private bool $isreadonly;
/** /**
* Constructor * Constructor
* *
@ -46,10 +49,12 @@ class checkbox_attribute extends element {
* @param string $label The label for the form element * @param string $label The label for the form element
* @param bool $ischecked Is this thing on? * @param bool $ischecked Is this thing on?
* @param int $locked Is this element locked either 0 or a time. * @param int $locked Is this element locked either 0 or a time.
* @param bool $isreadonly If this is a read-only input.
*/ */
public function __construct(string $name, string $label, bool $ischecked = false, int $locked=0) { public function __construct(string $name, string $label, bool $ischecked = false, int $locked=0, bool $isreadonly = false) {
$this->ischecked = $ischecked; $this->ischecked = $ischecked;
$this->locked = $locked; $this->locked = $locked;
$this->isreadonly = $isreadonly;
parent::__construct($name, 1, $label); parent::__construct($name, 1, $label);
} }
@ -67,6 +72,7 @@ class checkbox_attribute extends element {
* @return string * @return string
*/ */
public function html(): string { public function html(): string {
global $OUTPUT;
$attributes = [ $attributes = [
'type' => 'checkbox', 'type' => 'checkbox',
@ -95,6 +101,7 @@ class checkbox_attribute extends element {
$type = "exclude"; $type = "exclude";
} }
if (!$this->isreadonly) {
return ( return (
html_writer::tag('label', html_writer::tag('label',
get_string($type . 'for', 'gradereport_singleview', $this->label), get_string($type . 'for', 'gradereport_singleview', $this->label),
@ -102,5 +109,10 @@ class checkbox_attribute extends element {
html_writer::empty_tag('input', $attributes) . html_writer::empty_tag('input', $attributes) .
html_writer::empty_tag('input', $hidden) html_writer::empty_tag('input', $hidden)
); );
} else if ($this->ischecked) {
return $OUTPUT->pix_icon('i/checked', get_string('selected', 'core_form'));
} else {
return '';
}
} }
} }

View file

@ -53,6 +53,9 @@ class dropdown_attribute extends element {
*/ */
private $isdisabled; private $isdisabled;
/** @var bool If this is a read-only input. */
private bool $isreadonly;
/** /**
* Constructor * Constructor
* *
@ -61,11 +64,20 @@ class dropdown_attribute extends element {
* @param string $label The form label for this input. * @param string $label The form label for this input.
* @param string $selected The name of the selected item in this input. * @param string $selected The name of the selected item in this input.
* @param bool $isdisabled Are we disabled? * @param bool $isdisabled Are we disabled?
* @param bool $isreadonly If this is a read-only input.
*/ */
public function __construct(string $name, array $options, string $label, string $selected = '', bool $isdisabled = false) { public function __construct(
string $name,
array $options,
string $label,
string $selected = '',
bool $isdisabled = false,
bool $isreadonly = false
) {
$this->selected = $selected; $this->selected = $selected;
$this->options = $options; $this->options = $options;
$this->isdisabled = $isdisabled; $this->isdisabled = $isdisabled;
$this->isreadonly = $isreadonly;
parent::__construct($name, $selected, $label); parent::__construct($name, $selected, $label);
} }
@ -92,8 +104,10 @@ class dropdown_attribute extends element {
$context = [ $context = [
'name' => $this->name, 'name' => $this->name,
'value' => $this->selected, 'value' => $this->selected,
'text' => $options[$selected],
'tabindex' => 1, 'tabindex' => 1,
'disabled' => !empty($this->isdisabled), 'disabled' => !empty($this->isdisabled),
'readonly' => $this->isreadonly,
'options' => array_map(function($option) use ($options, $selected) { 'options' => array_map(function($option) use ($options, $selected) {
return [ return [
'name' => $options[$option], 'name' => $options[$option],

View file

@ -35,7 +35,7 @@ use grade_grade;
* @copyright 2014 Moodle Pty Ltd (http://moodle.com) * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class exclude extends grade_attribute_format implements be_checked, be_disabled { class exclude extends grade_attribute_format implements be_checked, be_disabled, be_readonly {
/** /**
* The name of the input * The name of the input
@ -67,6 +67,16 @@ class exclude extends grade_attribute_format implements be_checked, be_disabled
return $this->disabled; return $this->disabled;
} }
/**
* Return true if this is read-only.
*
* @return bool
*/
public function is_readonly(): bool {
global $USER;
return empty($USER->editing);
}
/** /**
* Generate the element used to render the UI * Generate the element used to render the UI
* *
@ -77,7 +87,8 @@ class exclude extends grade_attribute_format implements be_checked, be_disabled
$this->get_name(), $this->get_name(),
$this->get_label(), $this->get_label(),
$this->is_checked(), $this->is_checked(),
$this->is_disabled() $this->is_disabled(),
$this->is_readonly()
); );
} }

View file

@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die;
* @copyright 2014 Moodle Pty Ltd (http://moodle.com) * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class feedback extends grade_attribute_format implements unique_value, be_disabled { class feedback extends grade_attribute_format implements unique_value, be_disabled, be_readonly {
/** /**
* Name of this input * Name of this input
@ -88,6 +88,16 @@ class feedback extends grade_attribute_format implements unique_value, be_disabl
return ($locked || $gradeitemlocked || $overridden); return ($locked || $gradeitemlocked || $overridden);
} }
/**
* Return true if this is read-only.
*
* @return bool
*/
public function is_readonly(): bool {
global $USER;
return empty($USER->editing);
}
/** /**
* Create a text_attribute for this ui element. * Create a text_attribute for this ui element.
* *
@ -98,7 +108,8 @@ class feedback extends grade_attribute_format implements unique_value, be_disabl
$this->get_name(), $this->get_name(),
$this->get_value(), $this->get_value(),
$this->get_label(), $this->get_label(),
$this->is_disabled() $this->is_disabled(),
$this->is_readonly()
); );
} }

View file

@ -34,7 +34,7 @@ use stdClass;
* @copyright 2014 Moodle Pty Ltd (http://moodle.com) * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class finalgrade extends grade_attribute_format implements unique_value, be_disabled { class finalgrade extends grade_attribute_format implements unique_value, be_disabled, be_readonly {
/** /**
* Name of this input * Name of this input
@ -97,6 +97,16 @@ class finalgrade extends grade_attribute_format implements unique_value, be_disa
return ($locked || $gradeitemlocked || $overridden); return ($locked || $gradeitemlocked || $overridden);
} }
/**
* Return true if this is read-only.
*
* @return bool
*/
public function is_readonly(): bool {
global $USER;
return empty($USER->editing);
}
/** /**
* Create the element for this column. * Create the element for this column.
* *
@ -117,14 +127,16 @@ class finalgrade extends grade_attribute_format implements unique_value, be_disa
$options, $options,
$this->get_label(), $this->get_label(),
$this->get_value(), $this->get_value(),
$this->is_disabled() $this->is_disabled(),
$this->is_readonly()
); );
} else { } else {
return new text_attribute( return new text_attribute(
$this->get_name(), $this->get_name(),
$this->get_value(), $this->get_value(),
$this->get_label(), $this->get_label(),
$this->is_disabled() $this->is_disabled(),
$this->is_readonly()
); );
} }
} }

View file

@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die;
* @copyright 2014 Moodle Pty Ltd (http://moodle.com) * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class override extends grade_attribute_format implements be_checked, be_disabled { class override extends grade_attribute_format implements be_checked, be_disabled, be_readonly {
/** /**
* The name for this input * The name for this input
@ -66,6 +66,16 @@ class override extends grade_attribute_format implements be_checked, be_disabled
return ($lockedgrade || $lockedgradeitem); return ($lockedgrade || $lockedgradeitem);
} }
/**
* Return true if this is read-only.
*
* @return bool
*/
public function is_readonly(): bool {
global $USER;
return empty($USER->editing);
}
/** /**
* Get the label for this form element. * Get the label for this form element.
* *
@ -91,7 +101,8 @@ class override extends grade_attribute_format implements be_checked, be_disabled
$this->get_name(), $this->get_name(),
$this->get_label(), $this->get_label(),
$this->is_checked(), $this->is_checked(),
$this->is_disabled() $this->is_disabled(),
$this->is_readonly()
); );
} }

View file

@ -41,6 +41,9 @@ class text_attribute extends element {
*/ */
private $isdisabled; private $isdisabled;
/** @var bool If this is a read-only input. */
private bool $isreadonly;
/** /**
* Constructor * Constructor
* *
@ -48,9 +51,11 @@ class text_attribute extends element {
* @param string $value The input initial value. * @param string $value The input initial value.
* @param string $label The label for this input field. * @param string $label The label for this input field.
* @param bool $isdisabled Is this input disabled. * @param bool $isdisabled Is this input disabled.
* @param bool $isreadonly If this is a read-only input.
*/ */
public function __construct(string $name, string $value, string $label, bool $isdisabled = false) { public function __construct(string $name, string $value, string $label, bool $isdisabled = false, bool $isreadonly = false) {
$this->isdisabled = $isdisabled; $this->isdisabled = $isdisabled;
$this->isreadonly = $isreadonly;
parent::__construct($name, $value, $label); parent::__construct($name, $value, $label);
} }
@ -74,6 +79,7 @@ class text_attribute extends element {
'name' => $this->name, 'name' => $this->name,
'value' => $this->value, 'value' => $this->value,
'disabled' => $this->isdisabled, 'disabled' => $this->isdisabled,
'readonly' => $this->isreadonly,
]; ];
$context->label = ''; $context->label = '';

View file

@ -28,6 +28,10 @@
"selected": "true" "selected": "true"
} }
}} }}
{{#readonly}}
{{text}}
{{/readonly}}
{{^readonly}}
<label for="{{name}}" class="accesshide">{{label}}</label> <label for="{{name}}" class="accesshide">{{label}}</label>
<select id="{{name}}" name="{{name}}" class="custom-select" tabindex="1" {{#disabled}}disabled{{/disabled}}> <select id="{{name}}" name="{{name}}" class="custom-select" tabindex="1" {{#disabled}}disabled{{/disabled}}>
{{#options}} {{#options}}
@ -35,3 +39,4 @@
{{/options}} {{/options}}
</select> </select>
<input type="hidden" name="old{{name}}" value="{{value}}"> <input type="hidden" name="old{{name}}" value="{{value}}">
{{/readonly}}

View file

@ -28,6 +28,11 @@
"disabled": "true" "disabled": "true"
} }
}} }}
{{#readonly}}
{{value}}
{{/readonly}}
{{^readonly}}
{{#label}}<label for="{{name}}" class="accesshide">{{label}}</label>{{/label}} {{#label}}<label for="{{name}}" class="accesshide">{{label}}</label>{{/label}}
<input id="{{name}}" name="{{name}}" type="text" value="{{value}}" class="form-control" {{#tabindex}}tabindex="{{.}}"{{/tabindex}} {{#disabled}}disabled{{/disabled}}> <input id="{{name}}" name="{{name}}" type="text" value="{{value}}" class="form-control" {{#tabindex}}tabindex="{{.}}"{{/tabindex}} {{#disabled}}disabled{{/disabled}}>
<input type="hidden" name="old{{name}}" value="{{value}}"> <input type="hidden" name="old{{name}}" value="{{value}}">
{{/readonly}}