|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +/** |
| 18 | + * Table listing of all courses that are already in a process of the workflow. |
| 19 | + * |
| 20 | + * @package tool_lifecycle |
| 21 | + * @copyright 2025 Thomas Niedermaier Universität Münster |
| 22 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 23 | + */ |
| 24 | +namespace tool_lifecycle\local\table; |
| 25 | + |
| 26 | +use core_date; |
| 27 | +use stdClass; |
| 28 | +use tool_lifecycle\local\entity\workflow; |
| 29 | +use tool_lifecycle\local\manager\delayed_courses_manager; |
| 30 | +use tool_lifecycle\local\manager\lib_manager; |
| 31 | +use tool_lifecycle\local\manager\trigger_manager; |
| 32 | +use tool_lifecycle\local\manager\workflow_manager; |
| 33 | +use tool_lifecycle\local\response\trigger_response; |
| 34 | +use tool_lifecycle\urls; |
| 35 | + |
| 36 | +defined('MOODLE_INTERNAL') || die; |
| 37 | + |
| 38 | +require_once($CFG->libdir . '/tablelib.php'); |
| 39 | + |
| 40 | +/** |
| 41 | + * Table listing of all courses that are already in a process of the workflow. |
| 42 | + * |
| 43 | + * @package tool_lifecycle |
| 44 | + * @copyright 2025 Thomas Niedermaier Universität Münster |
| 45 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 46 | + */ |
| 47 | +class courses_in_process_table extends \table_sql { |
| 48 | + |
| 49 | + /** @var int $workflowid Id of the workflow */ |
| 50 | + private $workflowid; |
| 51 | + |
| 52 | + /** @var int $tablerows number of table rows effectively written */ |
| 53 | + public $tablerows = 0; |
| 54 | + |
| 55 | + /** |
| 56 | + * Builds a table of courses. |
| 57 | + * @param workflow $workflow of which the courses are listed |
| 58 | + * @param string $filterdata optional, term to filter the table by course id or -name |
| 59 | + * @throws \coding_exception |
| 60 | + * @throws \dml_exception |
| 61 | + */ |
| 62 | + public function __construct($workflow, $filterdata = '') { |
| 63 | + parent::__construct('tool_lifecycle-courses-in-process'); |
| 64 | + global $PAGE; |
| 65 | + |
| 66 | + $this->define_baseurl($PAGE->url); |
| 67 | + $this->workflowid = $workflow->id; |
| 68 | + |
| 69 | + $a = new \stdClass(); |
| 70 | + $a->title = $workflow->title; |
| 71 | + $this->caption = get_string('coursesinprocess', 'tool_lifecycle', $a); |
| 72 | + $this->captionattributes = ['class' => 'ml-3']; |
| 73 | + |
| 74 | + $columns = ['courseid', 'coursefullname', 'coursecategory', 'processtype']; |
| 75 | + $this->define_columns($columns); |
| 76 | + $headers = [ |
| 77 | + get_string('courseid', 'tool_lifecycle'), |
| 78 | + get_string('coursename', 'tool_lifecycle'), |
| 79 | + get_string('coursecategory', 'moodle'), |
| 80 | + get_string('step', 'tool_lifecycle')."/".get_string('error'), |
| 81 | + ]; |
| 82 | + $this->define_headers($headers); |
| 83 | + |
| 84 | + $fields = " c.id as courseid, |
| 85 | + c.fullname as coursefullname, |
| 86 | + c.shortname as courseshortname, |
| 87 | + cc.name as coursecategory, |
| 88 | + pe.id as errorid, |
| 89 | + s.instancename as stepname"; |
| 90 | + $from = " {course} c LEFT JOIN {course_categories} cc ON c.category = cc.id |
| 91 | + LEFT JOIN {tool_lifecycle_process} p ON c.id = p.courseid AND p.workflowid = $workflow->id |
| 92 | + LEFT JOIN {tool_lifecycle_proc_error} pe ON c.id = pe.courseid AND pe.workflowid = $workflow->id |
| 93 | + LEFT JOIN {tool_lifecycle_step} s ON p.stepindex = s.sortindex AND s.workflowid = $workflow->id |
| 94 | + "; |
| 95 | + |
| 96 | + $where = " p.workflowid IS NOT NULL OR pe.workflowid IS NOT NULL "; |
| 97 | + |
| 98 | + if (!$workflow->includesitecourse) { |
| 99 | + $where = "($where) AND c.id <> 1 "; |
| 100 | + } |
| 101 | + |
| 102 | + if ($filterdata) { |
| 103 | + if (is_numeric($filterdata)) { |
| 104 | + $where = "($where) AND c.id = $filterdata "; |
| 105 | + } else { |
| 106 | + $where = "($where) AND (c.fullname LIKE '%$filterdata%' OR c.shortname LIKE '%$filterdata%')"; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + $this->set_sql($fields, $from, $where, []); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Build the table from the fetched data. |
| 115 | + * |
| 116 | + * Take the data returned from the db_query and go through all the rows |
| 117 | + * processing each col using either col_{columnname} method or other_cols |
| 118 | + * method or if other_cols return NULL, then put the data straight into the |
| 119 | + * table. |
| 120 | + * |
| 121 | + * After calling this function, remember to call close_recordset. |
| 122 | + */ |
| 123 | + public function build_table() { |
| 124 | + if (!$this->rawdata) { |
| 125 | + return; |
| 126 | + } |
| 127 | + foreach ($this->rawdata as $row) { |
| 128 | + $formattedrow = $this->format_row($row); |
| 129 | + $this->add_data_keyed($formattedrow, $this->get_row_class($row)); |
| 130 | + $this->tablerows++; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Render coursefullname column. |
| 136 | + * @param object $row Row data. |
| 137 | + * @return string course link |
| 138 | + */ |
| 139 | + public function col_coursefullname($row) { |
| 140 | + $courselink = \html_writer::link(course_get_url($row->courseid), |
| 141 | + format_string($row->coursefullname), ['target' => '_blank']); |
| 142 | + return $courselink . '<br><span class="secondary-info">' . $row->courseshortname . '</span>'; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Render processtype column. |
| 147 | + * |
| 148 | + * @param object $row Row data. |
| 149 | + * @return string of link to processerror-page or string 'step' |
| 150 | + * @throws \coding_exception |
| 151 | + * @throws \moodle_exception |
| 152 | + */ |
| 153 | + public function col_processtype($row) { |
| 154 | + if ($row->errorid) { |
| 155 | + $params = [ |
| 156 | + 'workflow' => $this->workflowid, |
| 157 | + 'course' => $row->courseid, |
| 158 | + ]; |
| 159 | + return \html_writer::link( |
| 160 | + new \moodle_url(urls::PROCESS_ERRORS, $params), |
| 161 | + get_string('process_error', 'tool_lifecycle'), |
| 162 | + ['class' => 'error']); |
| 163 | + } else { |
| 164 | + if ($row->stepname) { |
| 165 | + return $row->stepname; |
| 166 | + } else { |
| 167 | + return get_string('step', 'tool_lifecycle'); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Hook that can be overridden in child classes to wrap a table in a form |
| 174 | + * for example. Called only when there is data to display and not |
| 175 | + * downloading. |
| 176 | + */ |
| 177 | + public function wrap_html_finish() { |
| 178 | + echo \html_writer::div(get_string('total')." ".get_string('page').": ".$this->tablerows." ". |
| 179 | + get_string('courses'), 'm-3'); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Prints a customized "nothing to display" message. |
| 184 | + */ |
| 185 | + public function print_nothing_to_display() { |
| 186 | + global $OUTPUT; |
| 187 | + echo \html_writer::div($OUTPUT->notification(get_string('nothingtodisplay', 'moodle'), 'info'), |
| 188 | + 'm-3'); |
| 189 | + echo \html_writer::div(" ".\html_writer::link(new \moodle_url(urls::WORKFLOW_DETAILS, |
| 190 | + ["wf" => $this->workflowid, "showsql" => "1", "showtablesql" => "1", "showdetails" => "1"]), |
| 191 | + " ", ["class" => "text-muted fs-6 text-decoration-none"])); |
| 192 | + } |
| 193 | +} |
0 commit comments