Skip to content

Commit

Permalink
MDL-73708 course: Return geo pattern generated course images via WS
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva authored and rmady committed Mar 22, 2023
1 parent 794f107 commit 27be439
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 15 deletions.
8 changes: 8 additions & 0 deletions course/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2434,6 +2434,7 @@ public static function search_courses_parameters() {
* @since Moodle 3.2
*/
protected static function get_course_public_information(core_course_list_element $course, $coursecontext) {
global $OUTPUT;

static $categoriescache = array();

Expand Down Expand Up @@ -2522,6 +2523,12 @@ protected static function get_course_public_information(core_course_list_element
}
}

$courseimage = \core_course\external\course_summary_exporter::get_course_image($course);
if (!$courseimage) {
$courseimage = $OUTPUT->get_generated_url_for_course($coursecontext);
}
$coursereturns['courseimage'] = $courseimage;

return $coursereturns;
}

Expand Down Expand Up @@ -2639,6 +2646,7 @@ protected static function get_course_structure($onlypublicdata = true) {
'fullname' => new external_value(PARAM_RAW, 'course full name'),
'displayname' => new external_value(PARAM_RAW, 'course display name'),
'shortname' => new external_value(PARAM_RAW, 'course short name'),
'courseimage' => new external_value(PARAM_URL, 'Course image', VALUE_OPTIONAL),
'categoryid' => new external_value(PARAM_INT, 'category id'),
'categoryname' => new external_value(PARAM_RAW, 'category name'),
'sortorder' => new external_value(PARAM_INT, 'Sort order in the category', VALUE_OPTIONAL),
Expand Down
47 changes: 35 additions & 12 deletions course/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2694,9 +2694,11 @@ public function test_get_user_administration_options() {
* Test get_courses_by_fields
*/
public function test_get_courses_by_field() {
global $DB;
global $DB, $USER;
$this->resetAfterTest(true);

$this->setAdminUser();

$category1 = self::getDataGenerator()->create_category(array('name' => 'Cat 1'));
$category2 = self::getDataGenerator()->create_category(array('parent' => $category1->id));
$course1 = self::getDataGenerator()->create_course(
Expand All @@ -2707,7 +2709,25 @@ public function test_get_courses_by_field() {
'categoryid' => $fieldcategory->get('id')];
$field = self::getDataGenerator()->create_custom_field($customfield);
$customfieldvalue = ['shortname' => 'test', 'value' => 'Test value'];
$course2 = self::getDataGenerator()->create_course(array('visible' => 0, 'category' => $category2->id, 'idnumber' => 'i2', 'customfields' => [$customfieldvalue]));
// Create course image.
$draftid = file_get_unused_draft_itemid();
$filerecord = [
'component' => 'user',
'filearea' => 'draft',
'contextid' => context_user::instance($USER->id)->id,
'itemid' => $draftid,
'filename' => 'image.jpg',
'filepath' => '/',
];
$fs = get_file_storage();
$fs->create_file_from_pathname($filerecord, __DIR__ . '/fixtures/image.jpg');
$course2 = self::getDataGenerator()->create_course([
'visible' => 0,
'category' => $category2->id,
'idnumber' => 'i2',
'customfields' => [$customfieldvalue],
'overviewfiles_filemanager' => $draftid
]);

$student1 = self::getDataGenerator()->create_user();
$user1 = self::getDataGenerator()->create_user();
Expand All @@ -2721,16 +2741,16 @@ public function test_get_courses_by_field() {
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(3, $result['courses']);
// Expect to receive all the fields.
$this->assertCount(40, $result['courses'][0]);
$this->assertCount(41, $result['courses'][1]); // One more field because is not the site course.
$this->assertCount(41, $result['courses'][2]); // One more field because is not the site course.
$this->assertCount(41, $result['courses'][0]);
$this->assertCount(42, $result['courses'][1]); // One more field because is not the site course.
$this->assertCount(42, $result['courses'][2]); // One more field because is not the site course.

$result = core_course_external::get_courses_by_field('id', $course1->id);
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(1, $result['courses']);
$this->assertEquals($course1->id, $result['courses'][0]['id']);
// Expect to receive all the fields.
$this->assertCount(41, $result['courses'][0]);
$this->assertCount(42, $result['courses'][0]);
// Check default values for course format topics.
$this->assertCount(3, $result['courses'][0]['courseformatoptions']);
foreach ($result['courses'][0]['courseformatoptions'] as $option) {
Expand All @@ -2747,6 +2767,7 @@ public function test_get_courses_by_field() {
default:
}
}
$this->assertStringContainsString('/course/generated', $result['courses'][0]['courseimage']);

$result = core_course_external::get_courses_by_field('id', $course2->id);
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
Expand All @@ -2760,6 +2781,7 @@ public function test_get_courses_by_field() {
'value' => $customfieldvalue['value'],
'valueraw' => $customfieldvalue['value'],
], $result['courses'][0]['customfields'][0]);
$this->assertStringContainsString('/course/overviewfiles', $result['courses'][0]['courseimage']);

$result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
Expand Down Expand Up @@ -2797,15 +2819,15 @@ public function test_get_courses_by_field() {
$result = core_course_external::get_courses_by_field();
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(2, $result['courses']);
$this->assertCount(33, $result['courses'][0]);
$this->assertCount(34, $result['courses'][1]); // One field more (course format options), not present in site course.
$this->assertCount(34, $result['courses'][0]);
$this->assertCount(35, $result['courses'][1]); // One field more (course format options), not present in site course.

$result = core_course_external::get_courses_by_field('id', $course1->id);
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(1, $result['courses']);
$this->assertEquals($course1->id, $result['courses'][0]['id']);
// Expect to receive all the files that a student can see.
$this->assertCount(34, $result['courses'][0]);
$this->assertCount(35, $result['courses'][0]);

// Check default filters.
$filters = $result['courses'][0]['filters'];
Expand Down Expand Up @@ -2850,15 +2872,15 @@ public function test_get_courses_by_field() {
$result = core_course_external::get_courses_by_field();
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(2, $result['courses']);
$this->assertCount(33, $result['courses'][0]); // Site course.
$this->assertCount(16, $result['courses'][1]); // Only public information, not enrolled.
$this->assertCount(34, $result['courses'][0]); // Site course.
$this->assertCount(17, $result['courses'][1]); // Only public information, not enrolled.

$result = core_course_external::get_courses_by_field('id', $course1->id);
$result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
$this->assertCount(1, $result['courses']);
$this->assertEquals($course1->id, $result['courses'][0]['id']);
// Expect to receive all the files that a authenticated can see.
$this->assertCount(16, $result['courses'][0]);
$this->assertCount(17, $result['courses'][0]);

// Course 2 is not visible.
$result = core_course_external::get_courses_by_field('id', $course2->id);
Expand Down Expand Up @@ -3882,4 +3904,5 @@ public function test_toggle_activity_recommendation() {
$this->assertFalse($favouritefactory->favourite_exists($component, $areaname, $areaid, $context));
$this->assertFalse($result['status']);
}

}
2 changes: 2 additions & 0 deletions course/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ information provided here is intended especially for developers.
- $sectionid to specify section the duplicated course module is placed in
- $changename to disable changing the name of the course module using the 'duplicatedmodule' lang string
* The method `make_categories_options`, deprecated since 3.10, has been removed
* External function core_course_external::get_courses_by_field and core_course_external::search_courses now return a field
called "courseimage" containing a URL pointing to the course image.

=== 4.1 ===
* The function course_modchooser() has been finally deprecated and can not be used anymore. Please use
Expand Down
9 changes: 8 additions & 1 deletion enrol/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public static function get_users_courses_parameters() {
* @return array of courses
*/
public static function get_users_courses($userid, $returnusercount = true) {
global $CFG, $USER, $DB;
global $CFG, $USER, $DB, $OUTPUT;

require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/user/lib.php');
Expand Down Expand Up @@ -411,6 +411,11 @@ function($favourite) {
);
}

$courseimage = \core_course\external\course_summary_exporter::get_course_image($course);
if (!$courseimage) {
$courseimage = $OUTPUT->get_generated_url_for_course($context);
}

$courseresult = [
'id' => $course->id,
'shortname' => $course->shortname,
Expand All @@ -421,6 +426,7 @@ function($favourite) {
'summary' => $course->summary,
'summaryformat' => $course->summaryformat,
'format' => $course->format,
'courseimage' => $courseimage,
'showgrades' => $course->showgrades,
'lang' => clean_param($course->lang, PARAM_LANG),
'enablecompletion' => $course->enablecompletion,
Expand Down Expand Up @@ -469,6 +475,7 @@ public static function get_users_courses_returns() {
'summary' => new external_value(PARAM_RAW, 'summary', VALUE_OPTIONAL),
'summaryformat' => new external_format_value('summary', VALUE_OPTIONAL),
'format' => new external_value(PARAM_PLUGIN, 'course format: weeks, topics, social, site', VALUE_OPTIONAL),
'courseimage' => new external_value(PARAM_URL, 'The course image URL', VALUE_OPTIONAL),
'showgrades' => new external_value(PARAM_BOOL, 'true if grades are shown, otherwise false', VALUE_OPTIONAL),
'lang' => new external_value(PARAM_LANG, 'forced course language', VALUE_OPTIONAL),
'enablecompletion' => new external_value(PARAM_BOOL, 'true if completion is enabled, otherwise false',
Expand Down
4 changes: 4 additions & 0 deletions enrol/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,8 @@ public function test_get_users_courses() {
$this->assertTrue($courseenrol['isfavourite']);
$this->assertEquals(2, $courseenrol['enrolledusercount']);
$this->assertEquals($course1->timemodified, $courseenrol['timemodified']);
$url = "https://www.example.com/moodle/pluginfile.php/{$contexts[$course1->id]->id}/course/generated/course.svg";
$this->assertEquals($url, $courseenrol['courseimage']);
} else {
// Check language pack. Should be empty since an incorrect one was used when creating the course.
$this->assertEmpty($courseenrol['lang']);
Expand All @@ -576,6 +578,8 @@ public function test_get_users_courses() {
$this->assertFalse($courseenrol['isfavourite']);
$this->assertEquals(1, $courseenrol['enrolledusercount']);
$this->assertEquals($course2->timemodified, $courseenrol['timemodified']);
$url = "https://www.example.com/moodle/pluginfile.php/{$contexts[$course2->id]->id}/course/generated/course.svg";
$this->assertEquals($url, $courseenrol['courseimage']);
}
}

Expand Down
2 changes: 2 additions & 0 deletions enrol/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ information provided here is intended especially for developers.
=== 4.2 ===

* New is_self_enrol_available() function has been created. Similar to can_self_enrol but without checking user capabilities.
* External function core_enrol_external::get_users_courses now returns a field called "courseimage" containing a URL pointing to
the course image.

=== 4.0 ===

Expand Down
1 change: 0 additions & 1 deletion lib/classes/geopattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ public function patternbyid($uniqueid) {
public function datauri() {
return $this->toDataURI();
}

}
14 changes: 13 additions & 1 deletion lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4364,7 +4364,7 @@ public function refresh() {
* @todo MDL-31088 file serving improments
*/
function file_pluginfile($relativepath, $forcedownload, $preview = null, $offline = false, $embed = false) {
global $DB, $CFG, $USER;
global $DB, $CFG, $USER, $OUTPUT;
// relative path must start with '/'
if (!$relativepath) {
throw new \moodle_exception('invalidargorconf');
Expand Down Expand Up @@ -4917,6 +4917,18 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null, $offlin
\core\session\manager::write_close(); // Unlock session during file serving.
send_stored_file($file, 60*60, 0, $forcedownload, $sendfileoptions);

} else if ($filearea === 'generated') {
if ($CFG->forcelogin) {
require_login($course);
} else if ($course->id != SITEID) {
require_login($course);
}

$svg = $OUTPUT->get_generated_svg_for_id($course->id);

\core\session\manager::write_close(); // Unlock session during file serving.
send_file($svg, 'course.svg', 60 * 60, 0, true, $forcedownload);

} else {
send_file_not_found();
}
Expand Down
24 changes: 24 additions & 0 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,30 @@ public function get_generated_image_for_id($id) {
return $pattern->datauri();
}

/**
* Get the course pattern image URL.
*
* @param context_course $context course context object
* @return string URL of the course pattern image in SVG format
*/
public function get_generated_url_for_course(context_course $context): string {
return moodle_url::make_pluginfile_url($context->id, 'course', 'generated', null, '/', 'course.svg')->out();
}

/**
* Get the course pattern in SVG format to show on a course card.
*
* @param int $id id to use when generating the pattern
* @return string SVG file contents
*/
public function get_generated_svg_for_id(int $id): string {
$color = $this->get_generated_color_for_id($id);
$pattern = new \core_geopattern();
$pattern->setColor($color);
$pattern->patternbyid($id);
return $pattern->toSVG();
}

/**
* Get the course color to show on a course card.
*
Expand Down
47 changes: 47 additions & 0 deletions lib/tests/outputrenderers_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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/>.

namespace core;

/**
* Unit tests for lib/outputrenderers.
*
* @package core
* @category test
* @copyright 2023 Rodrigo Mady
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_renderer
*/
class outputrenderers_test extends \advanced_testcase {
/**
* Test generated url from course image.
*
* @covers ::get_generated_url_for_course
*/
public function test_get_generated_url_for_course_image() {
global $OUTPUT;

$this->resetAfterTest();

$course = self::getDataGenerator()->create_course();
$context = \context_course::instance($course->id, IGNORE_MISSING);

// Get the image with correct course context.
$courseimage = $OUTPUT->get_generated_url_for_course($context);
$url = "https://www.example.com/moodle/pluginfile.php/{$context->id}/course/generated/course.svg";
$this->assertEquals($url, $courseimage);
}
}

0 comments on commit 27be439

Please sign in to comment.