Skip to content

Commit

Permalink
MDL-62899 search: add document icons
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Aug 7, 2018
1 parent 6e8235c commit 66f145e
Show file tree
Hide file tree
Showing 21 changed files with 403 additions and 1 deletion.
10 changes: 10 additions & 0 deletions course/classes/search/mycourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,14 @@ public function get_search_fileareas() {
public function get_component_name() {
return 'course';
}

/**
* Returns an icon instance for the document.
*
* @param \core_search\document $doc
* @return \core_search\document_icon
*/
public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon {
return new \core_search\document_icon('i/course');
}
}
10 changes: 10 additions & 0 deletions course/classes/search/section.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,14 @@ public function get_search_fileareas() {
public function get_component_name() {
return 'course';
}

/**
* Returns an icon instance for the document.
*
* @param \core_search\document $doc
* @return \core_search\document_icon
*/
public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon {
return new \core_search\document_icon('i/section');
}
}
32 changes: 32 additions & 0 deletions course/tests/search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,36 @@ public function test_section_access() {
$this->assertEquals(\core_search\manager::ACCESS_DELETED,
$searcharea->check_access($documents[1]->get('itemid')));
}

/**
* Test document icon for mycourse area.
*/
public function test_get_doc_icon_for_mycourse_area() {
$searcharea = \core_search\manager::get_search_area($this->mycoursesareaid);

$document = $this->getMockBuilder('\core_search\document')
->disableOriginalConstructor()
->getMock();

$result = $searcharea->get_doc_icon($document);

$this->assertEquals('i/course', $result->get_name());
$this->assertEquals('moodle', $result->get_component());
}

/**
* Test document icon for section area.
*/
public function test_get_doc_icon_for_section_area() {
$searcharea = \core_search\manager::get_search_area($this->sectionareaid);

$document = $this->getMockBuilder('\core_search\document')
->disableOriginalConstructor()
->getMock();

$result = $searcharea->get_doc_icon($document);

$this->assertEquals('i/section', $result->get_name());
$this->assertEquals('moodle', $result->get_component());
}
}
11 changes: 11 additions & 0 deletions message/classes/search/base_message.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,15 @@ protected function get_document_recordset_helper($modifiedfrom, \context $contex
ORDER BY m.timecreated ASC";
return $DB->get_recordset_sql($sql, $params);
}

/**
* Returns an icon instance for the document.
*
* @param \core_search\document $doc
*
* @return \core_search\document_icon
*/
public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon {
return new \core_search\document_icon('t/message');
}
}
16 changes: 16 additions & 0 deletions message/tests/search_received_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,20 @@ public function test_message_received_deleted_user() {

$this->assertFalse($doc);
}

/**
* Test document icon.
*/
public function test_get_doc_icon() {
$searcharea = \core_search\manager::get_search_area($this->messagereceivedareaid);

$document = $this->getMockBuilder('\core_search\document')
->disableOriginalConstructor()
->getMock();

$result = $searcharea->get_doc_icon($document);

$this->assertEquals('t/message', $result->get_name());
$this->assertEquals('moodle', $result->get_component());
}
}
16 changes: 16 additions & 0 deletions message/tests/search_sent_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,20 @@ public function test_message_sent_deleted_user() {
$this->assertFalse($doc);

}

/**
* Test document icon.
*/
public function test_get_doc_icon() {
$searcharea = \core_search\manager::get_search_area($this->messagesentareaid);

$document = $this->getMockBuilder('\core_search\document')
->disableOriginalConstructor()
->getMock();

$result = $searcharea->get_doc_icon($document);

$this->assertEquals('t/message', $result->get_name());
$this->assertEquals('moodle', $result->get_component());
}
}
10 changes: 10 additions & 0 deletions search/classes/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,14 @@ protected function get_course_level_context_restriction_sql(\context $context =
public function get_contexts_to_reindex() {
return new \ArrayIterator([\context_system::instance()]);
}

/**
* Returns an icon instance for the document.
*
* @param \core_search\document $doc
* @return \core_search\document_icon
*/
public function get_doc_icon(document $doc) : document_icon {
return new document_icon('i/empty');
}
}
10 changes: 10 additions & 0 deletions search/classes/base_block.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,14 @@ public function get_contexts_to_reindex() {
return \context::instance_by_id($id);
});
}

/**
* Returns an icon instance for the document.
*
* @param \core_search\document $doc
* @return \core_search\document_icon
*/
public function get_doc_icon(document $doc) : document_icon {
return new document_icon('e/anchor');
}
}
10 changes: 10 additions & 0 deletions search/classes/base_mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,14 @@ public function supports_group_restriction() {
public function restrict_cm_access_by_group(\cm_info $cm) {
return $cm->effectivegroupmode == SEPARATEGROUPS;
}

/**
* Returns an icon instance for the document.
*
* @param \core_search\document $doc
* @return \core_search\document_icon
*/
public function get_doc_icon(document $doc) : document_icon {
return new document_icon('icon', $this->get_module_name());
}
}
27 changes: 27 additions & 0 deletions search/classes/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class document implements \renderable, \templatable {
*/
protected $contexturl = null;

/**
* @var \core_search\document_icon Document icon instance.
*/
protected $docicon = null;

/**
* @var int|null The content field filearea.
*/
Expand Down Expand Up @@ -496,6 +501,24 @@ public function get_doc_url() {
return $this->docurl;
}

/**
* Sets document icon instance.
*
* @param \core_search\document_icon $docicon
*/
public function set_doc_icon(document_icon $docicon) {
$this->docicon = $docicon;
}

/**
* Gets document icon instance.
*
* @return \core_search\document_icon
*/
public function get_doc_icon() {
return $this->docicon;
}

public function set_context_url(\moodle_url $url) {
$this->contexturl = $url;
}
Expand Down Expand Up @@ -628,6 +651,10 @@ public function export_for_template(\renderer_base $output) {
$data['userfullname'] = format_string($this->get('userfullname'), true, array('context' => $this->get('contextid')));
}

if ($docicon = $this->get_doc_icon()) {
$data['icon'] = $output->image_url($docicon->get_name(), $docicon->get_component());
}

return $data;
}

Expand Down
77 changes: 77 additions & 0 deletions search/classes/document_icon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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/>.

/**
* Document icon class.
*
* @package core_search
* @copyright 2018 Dmitrii Metelkin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_search;

defined('MOODLE_INTERNAL') || die();

/**
* Represents a document icon.
*
* @package core_search
* @copyright 2018 Dmitrii Metelkin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class document_icon {
/**
* Icon file name.
* @var string
*/
protected $name;

/** Icon file component.
* @var string
*/
protected $component;

/**
* Constructor.
*
* @param string $name Icon name.
* @param string $component Icon component.
*/
public function __construct($name, $component = 'moodle') {
$this->name = $name;
$this->component = $component;
}

/**
* Returns name of the icon file.
*
* @return string
*/
public function get_name() {
return $this->name;
}

/**
* Returns the component of the icon file.
*
* @return string
*/
public function get_component() {
return $this->component;
}

}
1 change: 1 addition & 0 deletions search/classes/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ protected function to_document(\core_search\base $searcharea, $docdata) {
$doc->set_data_from_engine($docdata);
$doc->set_doc_url($searcharea->get_doc_url($doc));
$doc->set_context_url($searcharea->get_context_url($doc));
$doc->set_doc_icon($searcharea->get_doc_icon($doc));

// Uses the internal caches to get required data needed to render the document later.
$course = $this->get_course($doc->get('courseid'));
Expand Down
2 changes: 1 addition & 1 deletion search/templates/result.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
}}
<div class="result">
<h4 class="result-title">
<a href="{{{docurl}}}">{{{title}}}</a>
{{#icon}}<img class="icon" alt="" src="{{{icon}}}">{{/icon}}<a href="{{{docurl}}}">{{{title}}}</a>
</h4>
{{#content}}
<div class="result-content">{{{content}}}</div>
Expand Down
21 changes: 21 additions & 0 deletions search/tests/base_activity_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,25 @@ public function test_get_contexts_to_reindex() {
$contexts = iterator_to_array($glossaryarea->get_contexts_to_reindex(), false);
$this->assertEquals([], $contexts);
}

/**
* Test document icon.
*/
public function test_get_doc_icon() {
$baseactivity = $this->getMockBuilder('\core_search\base_activity')
->disableOriginalConstructor()
->setMethods(array('get_module_name'))
->getMockForAbstractClass();

$baseactivity->method('get_module_name')->willReturn('test_activity');

$document = $this->getMockBuilder('\core_search\document')
->disableOriginalConstructor()
->getMock();

$result = $baseactivity->get_doc_icon($document);

$this->assertEquals('icon', $result->get_name());
$this->assertEquals('test_activity', $result->get_component());
}
}
18 changes: 18 additions & 0 deletions search/tests/base_block_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,22 @@ protected function get_doc($courseid, $blockinstanceid) {
'areaid' => $area->get_area_id(), 'itemid' => 0];
return $engine->to_document($area, $docdata);
}

/**
* Test document icon.
*/
public function test_get_doc_icon() {
$baseblock = $this->getMockBuilder('\core_search\base_block')
->disableOriginalConstructor()
->getMockForAbstractClass();

$document = $this->getMockBuilder('\core_search\document')
->disableOriginalConstructor()
->getMock();

$result = $baseblock->get_doc_icon($document);

$this->assertEquals('e/anchor', $result->get_name());
$this->assertEquals('moodle', $result->get_component());
}
}
18 changes: 18 additions & 0 deletions search/tests/base_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,22 @@ public function test_get_contexts_to_reindex() {
$this->assertEquals([\context_system::instance()],
iterator_to_array($area->get_contexts_to_reindex(), false));
}

/**
* Test default document icon.
*/
public function test_get_default_doc_icon() {
$basearea = $this->getMockBuilder('\core_search\base')
->disableOriginalConstructor()
->getMockForAbstractClass();

$document = $this->getMockBuilder('\core_search\document')
->disableOriginalConstructor()
->getMock();

$result = $basearea->get_doc_icon($document);

$this->assertEquals('i/empty', $result->get_name());
$this->assertEquals('moodle', $result->get_component());
}
}
Loading

0 comments on commit 66f145e

Please sign in to comment.