Skip to content

Commit

Permalink
Implemented the course categories data source
Browse files Browse the repository at this point in the history
  • Loading branch information
prasanna-lmsace committed Apr 5, 2024
1 parent a5922f5 commit f8fa545
Show file tree
Hide file tree
Showing 7 changed files with 615 additions and 1 deletion.
134 changes: 134 additions & 0 deletions classes/local/dash_framework/structure/course_category_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?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/>.

/**
* Class course category table.
*
* @package block_dash
* @copyright 2024 bdecent gmbh <https://bdecent.de>
* @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()
]),

];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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/>.

/**
* Generate the category image url from the fetched record.
*
* @package block_dash
* @copyright 2024 bdecent gmbh <https://bdecent.de>
* @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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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/>.

/**
* Generate the categories recent courses from the record.
*
* @package block_dash
* @copyright 2024 bdecent gmbh <https://bdecent.de>
* @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 '-';
}
}
Loading

0 comments on commit f8fa545

Please sign in to comment.