Skip to content

Commit 3d14c7a

Browse files
committed
Add options and styles
1 parent 7faae68 commit 3d14c7a

File tree

7 files changed

+255
-80
lines changed

7 files changed

+255
-80
lines changed

block_featured_courses.php

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

25+
use block_featured_courses\output\featured_courses;
26+
2527
defined('MOODLE_INTERNAL') || die();
2628

2729
/**
@@ -38,7 +40,17 @@ class block_featured_courses extends block_base {
3840
* @throws coding_exception
3941
*/
4042
public function init() {
41-
$this->title = get_string('pluginname', 'block_featured_courses');
43+
$this->title = get_string('title', 'block_featured_courses');
44+
}
45+
46+
47+
/**
48+
* Update the block title from config values
49+
*/
50+
public function specialization() {
51+
if (!empty($this->config->title)) {
52+
$this->title = $this->config->title;
53+
}
4254
}
4355

4456
/**
@@ -57,31 +69,17 @@ public function get_content() {
5769
$this->content = '';
5870
return $this->content;
5971
}
60-
61-
$this->content = new stdClass();
62-
$this->content->items = array();
63-
$this->content->icons = array();
64-
$this->content->footer = '';
65-
66-
// User/index.php expect course context, so get one if page has module context.
67-
$currentcontext = $this->page->context->get_course_context(false);
68-
69-
if (!empty($this->config->text)) {
70-
$this->content->text = $this->config->text;
71-
}
72-
7372
$this->content = '';
74-
if (empty($currentcontext)) {
75-
return $this->content;
76-
}
77-
if ($this->page->course->id == SITEID) {
78-
$this->context->text .= "site context";
79-
}
8073

81-
if (!empty($this->config->text)) {
82-
$this->content->text .= $this->config->text;
74+
if ($this->config && !empty($this->config->selectedcourses)) {
75+
$this->content = new stdClass();
76+
$this->content->footer = '';
77+
$renderer = $this->page->get_renderer('core');
78+
$this->content->text = $renderer->render(
79+
new featured_courses(
80+
$this->config->selectedcourses
81+
));
8382
}
84-
8583
return $this->content;
8684
}
8785

@@ -109,19 +107,6 @@ public function instance_allow_multiple() {
109107
* @return bool
110108
*/
111109
public function has_config() {
112-
return true;
113-
}
114-
115-
/**
116-
* Cron Job
117-
*
118-
* @return bool
119-
*/
120-
public function cron() {
121-
mtrace("Hey, my cron script is running");
122-
123-
// Do something.
124-
125-
return true;
110+
return false;
126111
}
127112
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
* Thumblinks Action block renderable.
19+
*
20+
* @package block_thumblinks_action
21+
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace block_featured_courses\output;
26+
global $CFG;
27+
28+
defined('MOODLE_INTERNAL') || die();
29+
30+
use context_course;
31+
use context_helper;
32+
use core_course\external\course_summary_exporter;
33+
use moodle_url;
34+
use renderable;
35+
use renderer_base;
36+
use templatable;
37+
38+
class mini_course_summary_exporter extends course_summary_exporter {
39+
40+
/**
41+
* Only a subset of the usual.
42+
*
43+
* @return array|array[]
44+
*/
45+
public static function define_other_properties() {
46+
return array(
47+
'fullnamedisplay' => array(
48+
'type' => PARAM_TEXT,
49+
),
50+
'viewurl' => array(
51+
'type' => PARAM_URL,
52+
),
53+
'courseimage' => array(
54+
'type' => PARAM_RAW,
55+
),
56+
'showshortname' => array(
57+
'type' => PARAM_BOOL
58+
),
59+
'coursecategory' => array(
60+
'type' => PARAM_TEXT
61+
)
62+
);
63+
}
64+
65+
/**
66+
* Constructor - saves the persistent object, and the related objects.
67+
*
68+
* @param mixed $data - Either an stdClass or an array of values.
69+
* @param array $related - An optional list of pre-loaded objects related to this object.
70+
*/
71+
public function __construct($data, $related = array()) {
72+
\core\external\exporter::__construct($data, $related);
73+
}
74+
75+
protected static function define_related() {
76+
// We cache the context so it does not need to be retrieved from the course.
77+
return array('context' => '\\context');
78+
}
79+
80+
protected function get_other_values(renderer_base $output) {
81+
global $CFG;
82+
$courseimage = self::get_course_image($this->data);
83+
if (!$courseimage) {
84+
$courseimage = $output->get_generated_image_for_id($this->data->id);
85+
}
86+
$coursecategory = \core_course_category::get($this->data->category, MUST_EXIST, true);
87+
return array(
88+
'fullnamedisplay' => get_course_display_name_for_list($this->data),
89+
'viewurl' => (new moodle_url('/course/view.php', array('id' => $this->data->id)))->out(false),
90+
'courseimage' => $courseimage,
91+
'showshortname' => $CFG->courselistshortnames ? true : false,
92+
'coursecategory' => $coursecategory->name
93+
);
94+
}
95+
96+
}
97+
98+
/**
99+
* Class containing data for featured_courses block.
100+
*
101+
* @package block_mcms
102+
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
103+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
104+
*/
105+
class featured_courses implements renderable, templatable {
106+
107+
/**
108+
* @var array course
109+
*/
110+
public $courses = [];
111+
112+
/**
113+
* featured_courses constructor.
114+
* Retrieve matchin courses
115+
*
116+
* @param $coursesid
117+
* @throws \coding_exception
118+
* @throws \dml_exception
119+
*/
120+
public function __construct($coursesid) {
121+
global $DB;
122+
list($sql, $params) = $DB->get_in_or_equal($coursesid, SQL_PARAMS_NAMED);
123+
$this->courses = $DB->get_records_select('course', 'id ' . $sql, $params);
124+
}
125+
126+
public function export_for_template(renderer_base $renderer) {
127+
$formattedcourses = array_map(function($course) use ($renderer) {
128+
context_helper::preload_from_record($course);
129+
$context = context_course::instance($course->id);
130+
$exporter = new mini_course_summary_exporter($course, ['context' => $context]);
131+
$exported = (array) $exporter->export($renderer);
132+
return $exported;
133+
}, $this->courses);
134+
$exportedvalue = [
135+
'courses' => array_values((array) $formattedcourses),
136+
];
137+
return $exportedvalue;
138+
}
139+
}

edit_form.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
/**
2626
* Class block_featured_courses_edit_form
27+
*
2728
* @package block_vetagro_news
2829
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
2930
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -41,10 +42,34 @@ protected function specific_definition($mform) {
4142
// Section header title according to language file.
4243
$mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
4344

44-
// A sample string variable with a default value.
45-
$mform->addElement('text', 'config_text', get_string('blockstring', 'block_featured_courses'));
46-
$mform->setDefault('config_text', 'default value');
47-
$mform->setType('config_text', PARAM_MULTILANG);
45+
// Title of the block.
46+
$mform->addElement('text', 'config_title', get_string('config:title', 'block_featured_courses'));
47+
$mform->setDefault('config_title', get_string('title', 'block_featured_courses'));
48+
$mform->setType('config_title', PARAM_TEXT);
49+
50+
51+
$courses = (core_course_category::get(0))->get_courses(['recursive'=>true]);
52+
$courseitems = [];
53+
foreach ($courses as $c) {
54+
$courseitems[$c->id] = $c->get_formatted_name();
55+
}
56+
57+
$repeatarray = array();
58+
$repeatedoptions = array();
59+
60+
$repeatarray[] = $mform->createElement('searchableselector',
61+
'config_selectedcourses',
62+
get_string('config:selectedcourses', 'block_featured_courses'),
63+
$courseitems
64+
);
65+
$repeatedoptions['config_selectedcourses']['type'] = PARAM_RAW;
66+
67+
$numbcourses = empty($this->block->config->selectedcourses) ? 1 : count($this->block->config->selectedcourses);
68+
$this->repeat_elements($repeatarray, $numbcourses,
69+
$repeatedoptions,
70+
'selectcourses_repeats', 'selectcourse_add_fields', 3,
71+
get_string('addmorecourses', 'block_featured_courses')
72+
);
4873

4974
}
5075
}

lang/en/block_featured_courses.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2222
*/
2323

24-
$string['blockstring'] = 'Block string';
25-
$string['descconfig'] = 'Description of the config section';
26-
$string['descfoo'] = 'Config description';
27-
$string['headerconfig'] = 'Config section header';
28-
$string['labelfoo'] = 'Config label';
2924
$string['featured_courses:addinstance'] = 'Add a featured_courses block';
3025
$string['featured_courses:myaddinstance'] = 'Add a featured_courses block to my moodle';
3126
$string['pluginname'] = 'Featured Courses';
27+
$string['title'] = 'Featured Courses';
28+
29+
$string['config:title'] = 'Title';
30+
$string['config:selectedcourses'] = 'Selected course';
31+
$string['viewcourse'] = 'View Course';
32+
$string['addmorecourses'] = 'Add more courses';

settings.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

styles.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.block_featured_courses .course-image {
2+
background-size: cover;
3+
height: 70%;
4+
width: 100%;
5+
}
6+
.block_featured_courses .thumbnail {
7+
height: 300px;
8+
}
9+
.block_featured_courses .link i {
10+
width: 24px;
11+
height: 24px;
12+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{{!
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+
@template block_thumblinks_action/thumblinks_actions
19+
20+
This template renders the main content area for the mcms block.
21+
22+
Example context (json):
23+
{}
24+
}}
25+
26+
<div id="block-featured-courses-{{uniqid}}" class="block-featured-courses block-cards">
27+
<div class="container">
28+
<div class="row">
29+
{{#courses}}
30+
<div class="thumbnail col mx-2 my-1 d-flex flex-column justify-content-start p-0">
31+
<div class="course-image"
32+
style='background-image: url("{{{ courseimage}}}");'>
33+
</div>
34+
<div class="title p-1 text-primary font-weight-bolder text-truncate title p-2 text-primary flex-grow-1">{{ fullname }}</div>
35+
<div class="d-flex flex-row p-2 ">
36+
<div class="otherinfo text-muted font-italic flex-grow-1">
37+
{{#startdate}}{{#userdate}}{{.}}, {{#str}} strftimedate {{/str}}{{/userdate}}{{/startdate}}</div>
38+
<div class="link">
39+
<a class="btn btn-primary rounded-circle p-1" href="{{viewurl}}">{{#pix}}t/right, core, {{#str}}viewcourse, block_featured_courses {{/str}}{{/pix}}</a>
40+
</div>
41+
</div>
42+
43+
</div>
44+
{{/courses}}
45+
</div>
46+
</div>
47+
</div>

0 commit comments

Comments
 (0)