__ __ __ __ _____ _ _ _____ _ _ _ | \/ | \ \ / / | __ \ (_) | | / ____| | | | | | \ / |_ __\ V / | |__) | __ ___ ____ _| |_ ___ | (___ | |__ ___| | | | |\/| | '__|> < | ___/ '__| \ \ / / _` | __/ _ \ \___ \| '_ \ / _ \ | | | | | | |_ / . \ | | | | | |\ V / (_| | || __/ ____) | | | | __/ | | |_| |_|_(_)_/ \_\ |_| |_| |_| \_/ \__,_|\__\___| |_____/|_| |_|\___V 2.1 if you need WebShell for Seo everyday contact me on Telegram Telegram Address : @jackleetFor_More_Tools:
<?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/>.
namespace core\output;
/**
* Stored progress bar class.
*
* @package core
* @copyright 2023 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Conn Warwicker <conn.warwicker@catalyst-eu.net>
*/
class stored_progress_bar extends progress_bar {
/** @var bool Can use output buffering. */
protected static $supportsoutputbuffering = true;
/** @var int DB record ID */
protected $recordid;
/** @var string|null Message to associate with bar */
protected $message = null;
/** @var \core\clock Clock object */
protected $clock;
/**
* This overwrites the progress_bar::__construct method.
*
* @param string $idnumber
*/
public function __construct($idnumber) {
$this->clock = \core\di::get(\core\clock::class);
// Construct from the parent.
parent::__construct($idnumber, 0, true);
}
/**
* Just set the timestart, do not render the bar immediately.
*
* @return void
*/
public function create(): void {
$this->timestart = $this->clock->time();
}
/**
* Load the stored progress bar from the database based on its uniqued idnumber
*
* @param string $idnumber Unique ID of the bar
* @return stored_progress_bar|null
*/
public static function get_by_idnumber(string $idnumber): ?stored_progress_bar {
global $DB;
$record = $DB->get_record('stored_progress', ['idnumber' => $idnumber]);
if ($record) {
return self::load($record);
} else {
return null;
}
}
/**
* Load the stored progress bar from the database, based on it's record ID
*
* @param int $id Database record ID
* @return stored_progress_bar|null
*/
public static function get_by_id(int $id): ?stored_progress_bar {
global $DB;
$record = $DB->get_record('stored_progress', ['id' => $id]);
if ($record) {
return self::load($record);
} else {
return null;
}
}
/**
* Load the stored progress bar object from its record in the database.
*
* @param stdClass $record
* @return stored_progress_bar
*/
public static function load(\stdClass $record): stored_progress_bar {
$progress = new stored_progress_bar($record->idnumber);
$progress->set_record_id($record->id);
$progress->set_time_started($record->timestart);
$progress->set_last_updated($record->lastupdate);
$progress->set_percent($record->percentcompleted);
$progress->set_message($record->message);
$progress->set_haserrored($record->haserrored);
return $progress;
}
/**
* Set the DB record ID
*
* @param int $id
* @return void
*/
protected function set_record_id(int $id): void {
$this->recordid = $id;
}
/**
* Set the time we started the process.
*
* @param int $value
* @return void
*/
protected function set_time_started(int $value): void {
$this->timestart = $value;
}
/**
* Set the time we started last updated the progress.
*
* @param int|null $value
* @return void
*/
protected function set_last_updated(?int $value = null): void {
$this->lastupdate = $value;
}
/**
* Set the percent completed.
*
* @param float|null $value
* @return void
*/
protected function set_percent($value = null): void {
$this->percent = $value;
}
/**
* Set the message.
*
* @param string|null $value
* @return void
*/
protected function set_message(?string $value = null): void {
$this->message = $value;
}
/**
* Set that the process running has errored and store that against the bar
*
* @param string $errormsg
* @return void
*/
public function error(string $errormsg): void {
// Update the error variables.
parent::error($errormsg);
// Update the record.
$this->update_record();
}
/**
* Get the progress bar message.
*
* @return string|null
*/
public function get_message(): ?string {
return $this->message;
}
/**
* Get the content to display the progress bar and start polling via AJAX
*
* @return string
*/
public function get_content(): string {
global $CFG, $PAGE, $OUTPUT;
$PAGE->requires->js_call_amd('core/stored_progress', 'init', [
self::get_timeout(),
]);
$context = $this->export_for_template($OUTPUT);
return $OUTPUT->render_from_template('core/progress_bar', $context);
}
/**
* Export for template.
*
* @param renderer_base $output The renderer.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
return [
'id' => $this->recordid,
'idnumber' => $this->idnumber,
'width' => $this->width,
'class' => 'stored-progress-bar',
'value' => $this->percent,
'message' => $this->message,
'error' => $this->haserrored,
];
}
/**
* Start the recording of the progress and store in the database
*
* @return int ID of the DB record
*/
public function start(): int {
global $OUTPUT, $DB;
// If we are running in an non-interactive CLI environment, call the progress bar renderer to avoid warnings
// when we do an update.
if (defined('STDOUT') && !stream_isatty(STDOUT)) {
$OUTPUT->render_progress_bar($this);
}
// Delete any existing records for this.
$this->clear_records();
// Create new progress record.
$this->recordid = $DB->insert_record('stored_progress', [
'idnumber' => $this->idnumber,
'timestart' => (int)$this->timestart,
]);
return $this->recordid;
}
/**
* End the polling progress and delete the DB record.
*
* @return void
*/
protected function clear_records(): void {
global $DB;
$DB->delete_records('stored_progress', [
'idnumber' => $this->idnumber,
]);
}
/**
* Update the database record with the percentage and message
*
* @param float $percent
* @param string $msg
* @return void
*/
protected function update_raw($percent, $msg): void {
$this->percent = $percent;
$this->message = $msg;
// Update the database record with the new data.
$this->update_record();
// Update any CLI script's progress with an ASCII progress bar.
$this->render_update();
}
/**
* Render an update to the CLI
*
* This will only work in CLI scripts, and not in scheduled/adhoc tasks even though they run via CLI,
* as they seem to use a different renderer (core_renderer instead of core_renderer_cli).
*
* We also can't check this based on "CLI_SCRIPT" const as that is true for tasks.
*
* So this will just check a flag to see if we want auto rendering of updates.
*
* @return void
*/
protected function render_update(): void {
global $OUTPUT;
// If no output buffering, don't render it at all.
if (defined('NO_OUTPUT_BUFFERING') && NO_OUTPUT_BUFFERING) {
$this->auto_update(false);
}
// If we want the screen to auto update, render it.
if ($this->autoupdate) {
echo $OUTPUT->render_progress_bar_update(
$this->idnumber, $this->percent, $this->message, $this->get_estimate_message($this->percent)
);
}
}
/**
* Update the database record
*
* @throws \moodle_exception
* @return void
*/
protected function update_record(): void {
global $DB;
if (is_null($this->recordid)) {
throw new \moodle_exception('Polling has not been started. Cannot set iteration.');
}
// Update time.
$this->lastupdate = $this->clock->time();
// Update the database record.
$record = new \stdClass();
$record->id = $this->recordid;
$record->lastupdate = (int)$this->lastupdate;
$record->percentcompleted = $this->percent;
$record->message = $this->message;
$record->haserrored = $this->haserrored;
$DB->update_record('stored_progress', $record);
}
/**
* We need a way to specify a unique idnumber for processes being monitored, so that
* firstly we don't accidentally overwrite a running process, and secondly so we can
* automatically load them in some cases, without having to manually code in its name.
*
* So this uses the classname of the object being monitored, along with its id.
*
* This method should be used when creating the stored_progress record to set it's idnumber.
*
* @param string $class Class name of the object being monitored, e.g. \local_something\task\my_task
* @param int|null $id ID of an object from database, e.g. 123
* @return string Converted string, e.g. local_something_task_my_task_123
*/
public static function convert_to_idnumber(string $class, ?int $id = null): string {
$idnumber = preg_replace("/[^a-z0-9_]/", "_", ltrim($class, '\\'));
if (!is_null($id)) {
$idnumber .= '_' . $id;
}
return $idnumber;
}
/**
* Get the polling timeout in seconds. Default: 5.
*
* @return int
*/
public static function get_timeout(): int {
global $CFG;
return $CFG->progresspollinterval ?? 5;
}
}
| Name | Type | Size | Permission | Actions |
|---|---|---|---|---|
| action_menu | Folder | 0777 |
|
|
| actions | Folder | 0777 |
|
|
| dynamic_tabs | Folder | 0777 |
|
|
| local | Folder | 0777 |
|
|
| progress_trace | Folder | 0777 |
|
|
| renderer_factory | Folder | 0777 |
|
|
| requirements | Folder | 0777 |
|
|
| action_link.php | File | 4.86 KB | 0777 |
|
| action_menu.php | File | 23.57 KB | 0777 |
|
| activity_header.php | File | 8.63 KB | 0777 |
|
| bootstrap_renderer.php | File | 12.05 KB | 0777 |
|
| checkbox_toggleall.php | File | 4.93 KB | 0777 |
|
| choicelist.php | File | 9.42 KB | 0777 |
|
| chooser.php | File | 3.78 KB | 0777 |
|
| chooser_item.php | File | 3.12 KB | 0777 |
|
| chooser_section.php | File | 2.25 KB | 0777 |
|
| comboboxsearch.php | File | 6.74 KB | 0777 |
|
| context_header.php | File | 6.01 KB | 0777 |
|
| core_renderer.php | File | 194.2 KB | 0777 |
|
| core_renderer_ajax.php | File | 5.7 KB | 0777 |
|
| core_renderer_cli.php | File | 9.34 KB | 0777 |
|
| core_renderer_maintenance.php | File | 7.86 KB | 0777 |
|
| custom_menu.php | File | 7.03 KB | 0777 |
|
| custom_menu_item.php | File | 7.88 KB | 0777 |
|
| datafilter.php | File | 3.45 KB | 0777 |
|
| dynamic_tabs.php | File | 2.26 KB | 0777 |
|
| external.php | File | 8.11 KB | 0777 |
|
| file_picker.php | File | 3.65 KB | 0777 |
|
| help_icon.php | File | 4.37 KB | 0777 |
|
| html_writer.php | File | 34.28 KB | 0777 |
|
| icon_system.php | File | 5.25 KB | 0777 |
|
| icon_system_font.php | File | 1.53 KB | 0777 |
|
| icon_system_fontawesome.php | File | 26.92 KB | 0777 |
|
| icon_system_standard.php | File | 1.28 KB | 0777 |
|
| image_icon.php | File | 1.27 KB | 0777 |
|
| initials_bar.php | File | 4.94 KB | 0777 |
|
| inplace_editable.php | File | 9.84 KB | 0777 |
|
| js_writer.php | File | 5.42 KB | 0777 |
|
| language_menu.php | File | 5.67 KB | 0777 |
|
| mustache_clean_string_helper.php | File | 2.17 KB | 0777 |
|
| mustache_engine.php | File | 2.62 KB | 0777 |
|
| mustache_filesystem_loader.php | File | 2.35 KB | 0777 |
|
| mustache_helper_collection.php | File | 6.96 KB | 0777 |
|
| mustache_javascript_helper.php | File | 1.9 KB | 0777 |
|
| mustache_pix_helper.php | File | 2.45 KB | 0777 |
|
| mustache_quote_helper.php | File | 1.87 KB | 0777 |
|
| mustache_shorten_text_helper.php | File | 1.76 KB | 0777 |
|
| mustache_string_helper.php | File | 2.31 KB | 0777 |
|
| mustache_template_finder.php | File | 4.12 KB | 0777 |
|
| mustache_template_source_loader.php | File | 14.9 KB | 0777 |
|
| mustache_uniqid_helper.php | File | 1.56 KB | 0777 |
|
| mustache_user_date_helper.php | File | 1.83 KB | 0777 |
|
| named_templatable.php | File | 1.23 KB | 0777 |
|
| notification.php | File | 6.41 KB | 0777 |
|
| paging_bar.php | File | 8.97 KB | 0777 |
|
| participants_action_bar.php | File | 8.83 KB | 0777 |
|
| pix_emoticon.php | File | 1.71 KB | 0777 |
|
| pix_icon.php | File | 4.89 KB | 0777 |
|
| pix_icon_font.php | File | 2.9 KB | 0777 |
|
| pix_icon_fontawesome.php | File | 1.21 KB | 0777 |
|
| plugin_renderer_base.php | File | 5.86 KB | 0777 |
|
| preferences_group.php | File | 1.62 KB | 0777 |
|
| preferences_groups.php | File | 1.47 KB | 0777 |
|
| progress_bar.php | File | 8.54 KB | 0777 |
|
| progress_trace.php | File | 1.68 KB | 0777 |
|
| renderable.php | File | 1.24 KB | 0777 |
|
| renderer_base.php | File | 16.75 KB | 0777 |
|
| routed_error_handler.php | File | 1.69 KB | 0777 |
|
| select_menu.php | File | 6.68 KB | 0777 |
|
| single_button.php | File | 7.88 KB | 0777 |
|
| single_select.php | File | 9.43 KB | 0777 |
|
| sticky_footer.php | File | 4.5 KB | 0777 |
|
| stored_progress_bar.php | File | 10.39 KB | 0777 |
|
| tabobject.php | File | 5.11 KB | 0777 |
|
| tabtree.php | File | 3.37 KB | 0777 |
|
| templatable.php | File | 1.75 KB | 0777 |
|
| theme_config.php | File | 84.2 KB | 0777 |
|
| theme_usage.php | File | 4.32 KB | 0777 |
|
| url_rewriter.php | File | 1.6 KB | 0777 |
|
| url_select.php | File | 9.63 KB | 0777 |
|
| user_picture.php | File | 13.98 KB | 0777 |
|
| xhtml_container_stack.php | File | 5.79 KB | 0777 |
|