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? */
private $ischecked;
/** @var bool If this is a read-only input. */
private bool $isreadonly;
/**
* Constructor
*
@ -46,10 +49,12 @@ class checkbox_attribute extends element {
* @param string $label The label for the form element
* @param bool $ischecked Is this thing on?
* @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->locked = $locked;
$this->isreadonly = $isreadonly;
parent::__construct($name, 1, $label);
}
@ -67,6 +72,7 @@ class checkbox_attribute extends element {
* @return string
*/
public function html(): string {
global $OUTPUT;
$attributes = [
'type' => 'checkbox',
@ -95,12 +101,18 @@ class checkbox_attribute extends element {
$type = "exclude";
}
return (
html_writer::tag('label',
get_string($type . 'for', 'gradereport_singleview', $this->label),
['for' => $this->name, 'class' => 'accesshide']) .
html_writer::empty_tag('input', $attributes) .
html_writer::empty_tag('input', $hidden)
);
if (!$this->isreadonly) {
return (
html_writer::tag('label',
get_string($type . 'for', 'gradereport_singleview', $this->label),
['for' => $this->name, 'class' => 'accesshide']) .
html_writer::empty_tag('input', $attributes) .
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;
/** @var bool If this is a read-only input. */
private bool $isreadonly;
/**
* Constructor
*
@ -61,11 +64,20 @@ class dropdown_attribute extends element {
* @param string $label The form label for this input.
* @param string $selected The name of the selected item in this input.
* @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->options = $options;
$this->isdisabled = $isdisabled;
$this->isreadonly = $isreadonly;
parent::__construct($name, $selected, $label);
}
@ -92,8 +104,10 @@ class dropdown_attribute extends element {
$context = [
'name' => $this->name,
'value' => $this->selected,
'text' => $options[$selected],
'tabindex' => 1,
'disabled' => !empty($this->isdisabled),
'readonly' => $this->isreadonly,
'options' => array_map(function($option) use ($options, $selected) {
return [
'name' => $options[$option],

View file

@ -35,7 +35,7 @@ use grade_grade;
* @copyright 2014 Moodle Pty Ltd (http://moodle.com)
* @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
@ -67,6 +67,16 @@ class exclude extends grade_attribute_format implements be_checked, be_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
*
@ -77,7 +87,8 @@ class exclude extends grade_attribute_format implements be_checked, be_disabled
$this->get_name(),
$this->get_label(),
$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)
* @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
@ -88,6 +88,16 @@ class feedback extends grade_attribute_format implements unique_value, be_disabl
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.
*
@ -98,7 +108,8 @@ class feedback extends grade_attribute_format implements unique_value, be_disabl
$this->get_name(),
$this->get_value(),
$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)
* @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
@ -97,6 +97,16 @@ class finalgrade extends grade_attribute_format implements unique_value, be_disa
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.
*
@ -117,14 +127,16 @@ class finalgrade extends grade_attribute_format implements unique_value, be_disa
$options,
$this->get_label(),
$this->get_value(),
$this->is_disabled()
$this->is_disabled(),
$this->is_readonly()
);
} else {
return new text_attribute(
$this->get_name(),
$this->get_value(),
$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)
* @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
@ -66,6 +66,16 @@ class override extends grade_attribute_format implements be_checked, be_disabled
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.
*
@ -91,7 +101,8 @@ class override extends grade_attribute_format implements be_checked, be_disabled
$this->get_name(),
$this->get_label(),
$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;
/** @var bool If this is a read-only input. */
private bool $isreadonly;
/**
* Constructor
*
@ -48,9 +51,11 @@ class text_attribute extends element {
* @param string $value The input initial value.
* @param string $label The label for this input field.
* @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->isreadonly = $isreadonly;
parent::__construct($name, $value, $label);
}
@ -74,6 +79,7 @@ class text_attribute extends element {
'name' => $this->name,
'value' => $this->value,
'disabled' => $this->isdisabled,
'readonly' => $this->isreadonly,
];
$context->label = '';

View file

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

View file

@ -28,6 +28,11 @@
"disabled": "true"
}
}}
{{#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 type="hidden" name="old{{name}}" value="{{value}}">
{{#readonly}}
{{value}}
{{/readonly}}
{{^readonly}}
{{#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 type="hidden" name="old{{name}}" value="{{value}}">
{{/readonly}}