Skip to content

Commit fbc9ad2

Browse files
committed
Finish up the block and tests
* Allow course removal * Refactor code and fix issues spotted by code checker
1 parent 574911c commit fbc9ad2

10 files changed

+462
-99
lines changed

.travis.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
language: php
2+
3+
addons:
4+
postgresql: "9.6"
5+
6+
services:
7+
- mysql
8+
- postgresql
9+
- docker
10+
11+
cache:
12+
directories:
13+
- $HOME/.composer/cache
14+
- $HOME/.npm
15+
16+
php:
17+
- 7.2
18+
- 7.3
19+
- 7.4
20+
21+
env:
22+
global:
23+
- MOODLE_BRANCH=MOODLE_39_STABLE
24+
matrix:
25+
- DB=pgsql
26+
- DB=mysqli
27+
28+
before_install:
29+
- phpenv config-rm xdebug.ini
30+
- cd ../..
31+
- composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3
32+
- export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH"
33+
34+
install:
35+
- moodle-plugin-ci install
36+
37+
script:
38+
- moodle-plugin-ci phplint
39+
- moodle-plugin-ci phpcpd
40+
- moodle-plugin-ci phpmd
41+
- moodle-plugin-ci codechecker
42+
- moodle-plugin-ci validate
43+
- moodle-plugin-ci savepoints
44+
- moodle-plugin-ci mustache
45+
# - moodle-plugin-ci grunt
46+
- moodle-plugin-ci phpdoc
47+
- moodle-plugin-ci phpunit
48+
- moodle-plugin-ci behat

README.txt renamed to README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
# Featured courses #
22

3-
A block to push forward a set of courses from the global list of courses.
3+
[![Build Status](https://travis-ci.org/call-learning/moodle-block-featured_courses.svg?branch=master)](https://travis-ci.org/call-learning/moodle-block-featured_courses)
4+
5+
A block to display of courses from the global list of courses.
6+
7+
Once added, you will then be able to select a set of courses from the list of all courses.
8+
9+
# Features #
10+
11+
* Allow to remove the last course from the list if too many courses are added.
12+
* Integrates with the module local_syllabus if enabled and present.
13+
14+
# TODO
15+
16+
* Test for higher load of courses in the edit form ( we might be able to do this in ajax instead of loading all courses in memory )
417

518
## License ##
619

block_featured_courses.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class block_featured_courses extends block_base {
4040
* @throws coding_exception
4141
*/
4242
public function init() {
43-
$this->title = get_string('title', 'block_featured_courses');
43+
$this->title = get_string('pluginname', 'block_featured_courses');
4444
}
4545

4646
/**
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
* Course summary exporter with a specific purpose.
19+
*
20+
* @package block_featured_courses
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;
26+
defined('MOODLE_INTERNAL') || die();
27+
global $CFG;
28+
29+
use core_course\external\course_summary_exporter;
30+
use moodle_url;
31+
use renderer_base;
32+
33+
/**
34+
* Class mini_course_summary_exporter
35+
*
36+
* @package block_featured_courses
37+
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
38+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39+
*
40+
*/
41+
class mini_course_summary_exporter extends course_summary_exporter {
42+
43+
/**
44+
* Only a subset of the usual.
45+
*
46+
* @return array|array[]
47+
*/
48+
public static function define_other_properties() {
49+
return array(
50+
'fullnamedisplay' => array(
51+
'type' => PARAM_TEXT,
52+
),
53+
'viewurl' => array(
54+
'type' => PARAM_URL,
55+
),
56+
'courseimage' => array(
57+
'type' => PARAM_RAW,
58+
),
59+
'showshortname' => array(
60+
'type' => PARAM_BOOL
61+
),
62+
'coursecategory' => array(
63+
'type' => PARAM_TEXT
64+
)
65+
);
66+
}
67+
68+
/**
69+
* Constructor - saves the persistent object, and the related objects.
70+
*
71+
* @param mixed $data - Either an stdClass or an array of values.
72+
* @param array $related - An optional list of pre-loaded objects related to this object.
73+
*/
74+
public function __construct($data, $related = array()) {
75+
\core\external\exporter::__construct($data, $related);
76+
}
77+
78+
/**
79+
* Define related variables
80+
*
81+
* @return string[]
82+
*/
83+
protected static function define_related() {
84+
// We cache the context so it does not need to be retrieved from the course.
85+
return array('context' => '\\context');
86+
}
87+
88+
/**
89+
* Get other useful values
90+
*
91+
* @param renderer_base $output
92+
* @return array
93+
* @throws \moodle_exception
94+
*/
95+
protected function get_other_values(renderer_base $output) {
96+
global $CFG;
97+
$courseimage = self::get_course_image($this->data);
98+
if (!$courseimage) {
99+
$courseimage = $output->get_generated_image_for_id($this->data->id);
100+
}
101+
$coursecategory = \core_course_category::get($this->data->category, MUST_EXIST, true);
102+
$urlparam = array('id' => $this->data->id);
103+
$courseurl = new moodle_url('/course/view.php', $urlparam);
104+
if (!empty($CFG->enablesyllabus) && class_exists('\\local_syllabus\\locallib\utils')) {
105+
$courseurl = \local_syllabus\locallib\utils::get_syllabus_page_url($urlparam);
106+
}
107+
return array(
108+
'fullnamedisplay' => get_course_display_name_for_list($this->data),
109+
'viewurl' => $courseurl->out(false),
110+
'courseimage' => $courseimage,
111+
'showshortname' => $CFG->courselistshortnames ? true : false,
112+
'coursecategory' => $coursecategory->name
113+
);
114+
}
115+
116+
}

classes/output/featured_courses.php

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,95 +15,27 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18-
* Thumblinks Action block renderable.
18+
* Featured courses renderable
1919
*
20-
* @package block_thumblinks_action
20+
* @package block_featured_courses
2121
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

2525
namespace block_featured_courses\output;
26-
global $CFG;
27-
2826
defined('MOODLE_INTERNAL') || die();
2927

28+
use block_featured_courses\mini_course_summary_exporter;
3029
use context_course;
3130
use context_helper;
32-
use core_course\external\course_summary_exporter;
33-
use moodle_url;
3431
use renderable;
3532
use renderer_base;
3633
use templatable;
3734

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-
$urlparam = array('id' => $this->data->id);
88-
$courseurl = new moodle_url('/course/view.php', $urlparam);
89-
if (class_exists('\\local_syllabus\\locallib\utils')) {
90-
$courseurl = \local_syllabus\locallib\utils::get_syllabus_page_url($urlparam);
91-
}
92-
return array(
93-
'fullnamedisplay' => get_course_display_name_for_list($this->data),
94-
'viewurl' => $courseurl->out(false),
95-
'courseimage' => $courseimage,
96-
'showshortname' => $CFG->courselistshortnames ? true : false,
97-
'coursecategory' => $coursecategory->name
98-
);
99-
}
100-
101-
}
102-
10335
/**
10436
* Class containing data for featured_courses block.
10537
*
106-
* @package block_mcms
38+
* @package block_featured_courses
10739
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
10840
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
10941
*/
@@ -118,7 +50,7 @@ class featured_courses implements renderable, templatable {
11850
* featured_courses constructor.
11951
* Retrieve matchin courses
12052
*
121-
* @param $coursesid
53+
* @param int $coursesid
12254
* @throws \coding_exception
12355
* @throws \dml_exception
12456
*/
@@ -128,6 +60,13 @@ public function __construct($coursesid) {
12860
$this->courses = $DB->get_records_select('course', 'id ' . $sql, $params);
12961
}
13062

63+
/**
64+
* Export the renderable for template
65+
*
66+
* @param renderer_base $renderer
67+
* @return array
68+
* @throws \coding_exception
69+
*/
13170
public function export_for_template(renderer_base $renderer) {
13271
$formattedcourses = array_map(function($course) use ($renderer) {
13372
context_helper::preload_from_record($course);

0 commit comments

Comments
 (0)