Skip to content

Commit def346d

Browse files
authored
Merge commit from fork
Bind grid tile fragment to the checked course module
2 parents 186166e + ed3a17f commit def346d

2 files changed

Lines changed: 98 additions & 2 deletions

File tree

‎type/grid/classes/content_type.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,18 @@ public function get_fragment($args) {
484484
$id = (int) $args['id'];
485485

486486
// Double check the given contextid with the context of the course module.
487-
$context = \context_module::instance($cmid);
487+
$cm = get_coursemodule_from_id('unilabel', $cmid, 0, false, MUST_EXIST);
488+
$context = \context_module::instance($cm->id);
488489
if ($context->id !== $contextid) {
489490
throw new \moodle_exception('wrong contextid');
490491
}
491492
require_capability('mod/unilabel:view', $context);
492493

493-
$tile = $DB->get_record('unilabeltype_grid_tile', ['id' => $id], '*', MUST_EXIST);
494+
$sql = 'SELECT t.*
495+
FROM {unilabeltype_grid_tile} t
496+
JOIN {unilabeltype_grid} g ON g.id = t.gridid
497+
WHERE t.id = :tileid AND g.unilabelid = :unilabelid AND t.visible = 1';
498+
$tile = $DB->get_record_sql($sql, ['tileid' => $id, 'unilabelid' => $cm->instance], MUST_EXIST);
494499
$content = $this->format_content($tile, $context);
495500

496501
return $content;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
namespace unilabeltype_grid;
18+
19+
/**
20+
* Unit tests for the grid content type.
21+
*
22+
* @package unilabeltype_grid
23+
* @category test
24+
* @copyright 2026 ISB Bayern
25+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
*/
27+
final class content_type_test extends \advanced_testcase {
28+
/**
29+
* Create a grid activity with one tile.
30+
*
31+
* @param \stdClass $course
32+
* @param string $content
33+
* @return array [course module, tile id]
34+
*/
35+
private function create_grid_with_tile(\stdClass $course, string $content): array {
36+
global $DB;
37+
$unilabel = $this->getDataGenerator()->create_module('unilabel', ['course' => $course->id, 'unilabeltype' => 'grid']);
38+
$gridid = $DB->insert_record('unilabeltype_grid', (object) ['unilabelid' => $unilabel->id, 'columns' => 4]);
39+
$tileid = $DB->insert_record('unilabeltype_grid_tile', (object) [
40+
'gridid' => $gridid,
41+
'title' => 'Tile',
42+
'content' => $content,
43+
'visible' => 1,
44+
]);
45+
return [get_coursemodule_from_instance('unilabel', $unilabel->id), $tileid];
46+
}
47+
48+
#[\PHPUnit\Framework\Attributes\Group('baseline')]
49+
/**
50+
* A tile of another grid is not returned for the own course module.
51+
*
52+
* @covers \unilabeltype_grid\content_type::get_fragment
53+
*/
54+
public function test_get_fragment_rejects_foreign_tile(): void {
55+
$this->resetAfterTest();
56+
$own = $this->getDataGenerator()->create_course();
57+
$foreign = $this->getDataGenerator()->create_course();
58+
[$cm] = $this->create_grid_with_tile($own, 'Own tile');
59+
[, $foreigntileid] = $this->create_grid_with_tile($foreign, 'Foreign secret');
60+
$student = $this->getDataGenerator()->create_and_enrol($own, 'student');
61+
$this->setUser($student);
62+
63+
$this->expectException(\dml_missing_record_exception::class);
64+
(new content_type())->get_fragment([
65+
'contextid' => \context_module::instance($cm->id)->id,
66+
'cmid' => $cm->id,
67+
'id' => $foreigntileid,
68+
]);
69+
}
70+
71+
#[\PHPUnit\Framework\Attributes\Group('baseline')]
72+
/**
73+
* A tile of the own grid is returned.
74+
*
75+
* @covers \unilabeltype_grid\content_type::get_fragment
76+
*/
77+
public function test_get_fragment_returns_own_tile(): void {
78+
$this->resetAfterTest();
79+
$own = $this->getDataGenerator()->create_course();
80+
[$cm, $tileid] = $this->create_grid_with_tile($own, 'Own tile');
81+
$student = $this->getDataGenerator()->create_and_enrol($own, 'student');
82+
$this->setUser($student);
83+
84+
$content = (new content_type())->get_fragment([
85+
'contextid' => \context_module::instance($cm->id)->id,
86+
'cmid' => $cm->id,
87+
'id' => $tileid,
88+
]);
89+
$this->assertStringContainsString('Own tile', $content);
90+
}
91+
}

0 commit comments

Comments
 (0)