diff --git a/classes/local/dash_framework/structure/course_category_table.php b/classes/local/dash_framework/structure/course_category_table.php new file mode 100644 index 0000000..94fe06f --- /dev/null +++ b/classes/local/dash_framework/structure/course_category_table.php @@ -0,0 +1,134 @@ +. + +/** + * Class course category table. + * + * @package block_dash + * @copyright 2024 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace block_dash\local\dash_framework\structure; + +use block_dash\local\dash_framework\structure\field; +use block_dash\local\dash_framework\structure\field_interface; +use block_dash\local\dash_framework\structure\table; +use block_dash\local\data_grid\field\attribute\category_image_url_attribute; +use block_dash\local\data_grid\field\attribute\category_recent_course_attribute; +use block_dash\local\data_grid\field\attribute\identifier_attribute; +use block_dash\local\data_grid\field\attribute\image_attribute; +use block_dash\local\data_grid\field\attribute\image_url_attribute; +use block_dash\local\data_grid\field\attribute\moodle_url_attribute; +use block_dash\local\data_grid\field\attribute\link_attribute; +use block_dash\local\data_grid\field\attribute\linked_data_attribute; +use block_dash\local\data_grid\field\attribute\widget_attribute; +use lang_string; +use moodle_url; + +/** + * Class course_category_table. + * + * @package block_dash + */ +class course_category_table extends table { + + /** + * Build a new table. + */ + public function __construct() { + parent::__construct('course_categories', 'cc'); + } + + /** + * Get human readable title for table. + * + * @return string + */ + public function get_title(): string { + return get_string('tablealias_cc', 'block_dash'); + } + + /** + * Define the fields available in the reports for this table data source. + * + * @return field_interface[] + */ + public function get_fields(): array { + return [ + new field('id', new lang_string('category'), $this, null, [ + new identifier_attribute(), + ]), + + // Category name. + new field('name', new lang_string('categoryname'), $this, 'cc.name'), + + // Category name linked. + new field('categoryurl', new lang_string('categoryurl', 'block_dash'), $this, 'cc.name', [ + new moodle_url_attribute(['url' => new moodle_url('/course/management.php', ['categoryid' => 'cc_id'])]), + new link_attribute(['label_field' => 'cc_name']) + ]), + + // Category ID number. + new field('idnumber', new lang_string('idnumber'), $this), + + // Description. + new field('description', new lang_string('summary'), $this, 'cc.description, cc.descriptionformat', [ + new widget_attribute([ + 'callback' => function($coursecat, $data) { + if (!isset($coursecat->cc_descriptionformat)) { + $descriptionformat = FORMAT_MOODLE; + } else { + $descriptionformat = $coursecat->cc_descriptionformat; + } + + $options = array('noclean' => true, 'overflowdiv' => true); + $context = \context_coursecat::instance($coursecat->cc_id); + $options['context'] = $context; + $text = file_rewrite_pluginfile_urls($coursecat->description, + 'pluginfile.php', $context->id, 'coursecat', 'description', null); + return format_text($text, $descriptionformat, $options); + } + ]) + ]), + + // Category image linked. + new field('image_link', new lang_string('categoryimagelink', 'block_dash'), $this, 'cc.id', [ + new category_image_url_attribute(), new image_attribute(), + new linked_data_attribute(['url' => new moodle_url('/course/management.php', ['categoryid' => 'cc_id'])]), + ]), + + // Category image. + new field('image', new lang_string('categoryimage', 'block_dash'), $this, 'cc.id', [ + new category_image_url_attribute(), new image_attribute(), + ]), + + // Category image. + new field('imageurl', new lang_string('categoryimageurl', 'block_dash'), $this, 'cc.id', [ + new category_image_url_attribute(), new image_url_attribute() + ]), + + // Number of courses. + new field('coursecount', new lang_string('categorycoursecount', 'block_dash'), $this, null), + + // Most recent course name (by created date). + new field('mostrecentcourse', new lang_string('recentcoursename', 'block_dash'), $this, 'cc.id', [ + new category_recent_course_attribute() + ]), + + ]; + } +} diff --git a/classes/local/data_grid/field/attribute/category_image_url_attribute.php b/classes/local/data_grid/field/attribute/category_image_url_attribute.php new file mode 100644 index 0000000..936ef5e --- /dev/null +++ b/classes/local/data_grid/field/attribute/category_image_url_attribute.php @@ -0,0 +1,112 @@ +. + +/** + * Generate the category image url from the fetched record. + * + * @package block_dash + * @copyright 2024 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace block_dash\local\data_grid\field\attribute; + +use block_dash\local\data_grid\field\attribute\abstract_field_attribute; + +/** + * Transform data to URL of category image. + * + * @package block_dash + */ +class category_image_url_attribute extends abstract_field_attribute { + + /** + * After records are relieved from database each field has a chance to transform the data. + * Example: Convert unix timestamp into a human readable date format + * + * @param \stdClass $data + * @param \stdClass $record Entire row + * @return mixed + * @throws \moodle_exception + */ + public function transform_data($data, \stdClass $record) { + global $CFG, $OUTPUT; + + require_once("$CFG->dirroot/blocks/dash/lib.php"); + + // Data is not integer then use the cc_id. + if (!is_int($data)) { + $data = $record->cc_id ?? 0; + } + + $files = $this->get_category_files(); + + if (!isset($files[$data]) && !isset($files[0])) { + return $OUTPUT->image_url('courses', 'block_myoverview'); + } + + // Verify the category images are added for the category or the fallback image is uploaded then use that. + if (isset($files[$data]) || isset($files[0])) { + + // Category imaage or fallback image. + $file = $files[$data] ?? $files[0]; + // Generate the URL. + $fileurl = \moodle_url::make_pluginfile_url( + $file->get_contextid(), + $file->get_component(), + $file->get_filearea(), + $file->get_itemid(), + $file->get_filepath(), + $file->get_filename(), false + ); + + return $fileurl->out(false); + } + + return $data; + } + + /** + * Get the list of category images. + * + * @return array|null + */ + function get_category_files() { + + static $list = null; + + if ($list == null) { + // Get the system context. + $systemcontext = \context_system::instance(); + + // File storage. + $fs = get_file_storage(); + + // Get all files from category image filearea. + $files = $fs->get_area_files($systemcontext->id, 'block_dash', 'categoryimg', false, 'itemid', false); + + $list = []; + // Update the files index as itemid. + if (!empty($files)) { + foreach ($files as $id => $file) { + $list[$file->get_itemid()] = $file; + } + } + } + + return $list; + } +} diff --git a/classes/local/data_grid/field/attribute/category_recent_course_attribute.php b/classes/local/data_grid/field/attribute/category_recent_course_attribute.php new file mode 100644 index 0000000..9e80662 --- /dev/null +++ b/classes/local/data_grid/field/attribute/category_recent_course_attribute.php @@ -0,0 +1,54 @@ +. + +/** + * Generate the categories recent courses from the record. + * + * @package block_dash + * @copyright 2024 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace block_dash\local\data_grid\field\attribute; + +use block_dash\local\data_grid\field\attribute\abstract_field_attribute; + +/** + * Find the recent course of the categories. + * + * @package block_dash + */ +class category_recent_course_attribute extends abstract_field_attribute { + + /** + * After records are relieved from database each field has a chance to transform the data. + * Example: Convert unix timestamp into a human readable date format + * + * @param \stdClass $data + * @param \stdClass $record Entire row + * @return mixed + * @throws \moodle_exception + */ + public function transform_data($data, \stdClass $record) { + global $DB; + + if ($course = $DB->get_records('course', ['category' => $data], 'timecreated DESC', 'id, fullname', 0, 1)) { + return format_string(current($course)->fullname); + } + + return '-'; + } +} diff --git a/classes/local/data_grid/field/field_definition_interface.php b/classes/local/data_grid/field/field_definition_interface.php new file mode 100644 index 0000000..365846e --- /dev/null +++ b/classes/local/data_grid/field/field_definition_interface.php @@ -0,0 +1,205 @@ +. + +/** + * Represents a predefined field that can be added to a data grid. + * + * @package block_dash + * @copyright 2019 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace block_dash\local\data_grid\field; + +use block_dash\local\data_grid\field\attribute\field_attribute_interface; +use block_dash\local\data_grid\data_grid_interface; + +/** + * Represents a predefined field that can be added to a data grid. + * + * @package block_dash + */ +interface field_definition_interface { + + /** + * Visible to user. + */ + const VISIBILITY_VISIBLE = 1; + + /** + * Not visible to user. + */ + const VISIBILITY_HIDDEN = 0; + + /** + * Value to display when empty or null. + */ + const DEFAULT_EMPTY_VALUE = '-'; + + /** + * Set data source this field definition is attached to. + * + * @param data_grid_interface $datagrid + * @return mixed + */ + public function set_data_grid(data_grid_interface $datagrid); + + /** + * Get data source this field definition is attached to. + * + * @return data_grid_interface + */ + public function get_data_grid(); + + /** + * After records are relieved from database each field has a chance to transform the data. + * Example: Convert unix timestamp into a human readable date format + * + * @param mixed $data Raw data associated with this field definition. + * @param \stdClass $record Full record from database. + * @return mixed + */ + public function transform_data($data, \stdClass $record); + + /** + * Get unique field name. + * + * @return string + */ + public function get_name(); + + /** + * Get field title. + * + * @return string + */ + public function get_title(); + + /** + * Get field visibility. + * + * @return int + */ + public function get_visibility(); + + /** + * Set field visibility. + * + * @param int $visibility + */ + public function set_visibility($visibility); + + /** + * Add attribute to this field definition. + * + * @param field_attribute_interface $attribute + */ + public function add_attribute(field_attribute_interface $attribute); + + /** + * Remove attribute to this field definition. + * + * @param field_attribute_interface $attribute + */ + public function remove_attribute(field_attribute_interface $attribute); + + /** + * Get all attributes associated with this field definition. + * + * @return field_attribute_interface[] + */ + public function get_attributes(); + + /** + * Check if field has an attribute type. + * + * @param string $classname Full class path to attribute + * @return bool + */ + public function has_attribute($classname); + + /** + * Get a single option. + * + * @param string $name + * @return mixed|null + */ + public function get_option($name); + + /** + * Set option on field. + * + * @param string $name + * @param string $value + */ + public function set_option($name, $value); + + /** + * Set options on field. + * + * @param array $options + */ + public function set_options($options); + + /** + * Get all options for this field. + * + * @return array + */ + public function get_options(); + + /** + * Set if field should be sorted. + * + * @param bool $sort + */ + public function set_sort($sort); + + /** + * Is the field sorted. + * + * @return bool + */ + public function get_sort(); + + /** + * Set direction sort should happen for this field. + * + * @param string $direction + */ + public function set_sort_direction($direction); + + /** + * Get sort direction. + * + * @return string + */ + public function get_sort_direction(); + + /** + * Set optional sort select (ORDER BY