Merge branch 'MDL-62899-search-icons-master' of https://github.com/dmitriim/moodle
authorAndrew Nicols <andrew@nicols.co.uk>
Wed, 8 Aug 2018 05:17:34 +0000 (13:17 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 8 Aug 2018 05:17:34 +0000 (13:17 +0800)
1  2 
search/classes/base.php
search/classes/document.php
user/classes/search/user.php
user/tests/search_test.php

diff --combined search/classes/base.php
@@@ -340,19 -340,6 +340,19 @@@ abstract class base 
       */
      abstract public function get_document($record, $options = array());
  
 +    /**
 +     * Returns the document title to display.
 +     *
 +     * Allow to customize the document title string to display.
 +     *
 +     * @param \core_search\document $doc
 +     * @return string Document title to display in the search results page
 +     */
 +    public function get_document_display_title(\core_search\document $doc) {
 +
 +        return $doc->get('title');
 +    }
 +
      /**
       * Return the context info required to index files for
       * this search area.
      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');
+     }
  }
@@@ -58,6 -58,11 +58,11 @@@ class document implements \renderable, 
       */
      protected $contexturl = null;
  
+     /**
+      * @var \core_search\document_icon Document icon instance.
+      */
+     protected $docicon = null;
      /**
       * @var int|null The content field filearea.
       */
          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;
      }
      public function export_for_template(\renderer_base $output) {
          list($componentname, $areaname) = \core_search\manager::extract_areaid_parts($this->get('areaid'));
  
 -        $title = $this->is_set('title') ? $this->format_text($this->get('title')) : '';
 +        $searcharea = \core_search\manager::get_search_area($this->data['areaid']);
 +        $title = $this->is_set('title') ? $this->format_text($searcharea->get_document_display_title($this)) : '';
          $data = [
              'componentname' => $componentname,
              'areaname' => $areaname,
              $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;
      }
  
@@@ -92,14 -92,8 +92,14 @@@ class user extends \core_search\base 
  
          // Prepare associative array with data from DB.
          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
 +        // Include all alternate names in title.
 +        $array = [];
 +        foreach (get_all_user_name_fields(false, null, null, null, true) as $field) {
 +            $array[$field] = $record->$field;
 +        }
 +        $fullusername = join(' ', $array);
          // Assigning properties to our document.
 -        $doc->set('title', content_to_text(fullname($record), false));
 +        $doc->set('title', content_to_text($fullusername, false));
          $doc->set('contextid', $context->id);
          $doc->set('courseid', SITEID);
          $doc->set('itemid', $record->id);
          return $doc;
      }
  
 +    /**
 +     * Returns the user fullname to display as document title
 +     *
 +     * @param \core_search\document $doc
 +     * @return string User fullname
 +     */
 +    public function get_document_display_title(\core_search\document $doc) {
 +
 +        $user = \core_user::get_user($doc->get('itemid'));
 +        return fullname($user);
 +    }
 +
      /**
       * Checking whether I can access a document
       *
          return 'user';
      }
  
+     /**
+      * 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/user');
+     }
  }
@@@ -119,7 -119,7 +119,7 @@@ class user_search_testcase extends adva
          $this->assertEquals(SITEID, $doc->get('courseid'));
          $this->assertFalse($doc->is_set('userid'));
          $this->assertEquals(\core_search\manager::NO_OWNER_ID, $doc->get('owneruserid'));
 -        $this->assertEquals(content_to_text(fullname($user), false), $doc->get('title'));
 +        $this->assertEquals(content_to_text(fullname($user), false), $searcharea->get_document_display_title($doc));
          $this->assertEquals(content_to_text($user->description, $user->descriptionformat), $doc->get('content'));
      }
  
          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user2->id));
          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user3->id));
      }
+     /**
+      * Test document icon.
+      */
+     public function test_get_doc_icon() {
+         $searcharea = \core_search\manager::get_search_area($this->userareaid);
+         $user = self::getDataGenerator()->create_user();
+         $doc = $searcharea->get_document($user);
+         $result = $searcharea->get_doc_icon($doc);
+         $this->assertEquals('i/user', $result->get_name());
+         $this->assertEquals('moodle', $result->get_component());
+     }
  }