[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * Search area for mod_book chapters. 19 * 20 * @package mod_book 21 * @copyright 2016 Eric Merrill {@link http://www.merrilldigital.com} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace mod_book\search; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Search area for mod_book chapters. 31 * 32 * @package mod_book 33 * @copyright 2016 Eric Merrill {@link http://www.merrilldigital.com} 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class chapter extends \core_search\base_mod { 37 /** 38 * @var array Cache of book records. 39 */ 40 protected $bookscache = array(); 41 42 /** 43 * Returns a recordset with all required chapter information. 44 * 45 * @param int $modifiedfrom 46 * @return moodle_recordset 47 */ 48 public function get_recordset_by_timestamp($modifiedfrom = 0) { 49 global $DB; 50 51 $sql = 'SELECT c.*, b.id AS bookid, b.course AS courseid 52 FROM {book_chapters} c 53 JOIN {book} b ON b.id = c.bookid 54 WHERE c.timemodified >= ? ORDER BY c.timemodified ASC'; 55 return $DB->get_recordset_sql($sql, array($modifiedfrom)); 56 } 57 58 /** 59 * Returns the document for a particular chapter. 60 * 61 * @param \stdClass $record A record containing, at least, the indexed document id and a modified timestamp 62 * @param array $options Options for document creation 63 * @return \core_search\document 64 */ 65 public function get_document($record, $options = array()) { 66 try { 67 $cm = $this->get_cm('book', $record->bookid, $record->courseid); 68 $context = \context_module::instance($cm->id); 69 } catch (\dml_missing_record_exception $ex) { 70 // Notify it as we run here as admin, we should see everything. 71 debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' . 72 $ex->getMessage(), DEBUG_DEVELOPER); 73 return false; 74 } catch (\dml_exception $ex) { 75 // Notify it as we run here as admin, we should see everything. 76 debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER); 77 return false; 78 } 79 80 // Prepare associative array with data from DB. 81 $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname); 82 $doc->set('title', content_to_text($record->title, false)); 83 $doc->set('content', content_to_text($record->content, $record->contentformat)); 84 $doc->set('contextid', $context->id); 85 $doc->set('courseid', $record->courseid); 86 $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); 87 $doc->set('modified', $record->timemodified); 88 89 // Check if this document should be considered new. 90 if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $record->timecreated)) { 91 // If the document was created after the last index time, it must be new. 92 $doc->set_is_new(true); 93 } 94 95 return $doc; 96 } 97 98 /** 99 * Can the current user see the document. 100 * 101 * @param int $id The internal search area entity id. 102 * @return bool True if the user can see it, false otherwise 103 */ 104 public function check_access($id) { 105 global $DB; 106 107 try { 108 $chapter = $DB->get_record('book_chapters', array('id' => $id), '*', MUST_EXIST); 109 if (!isset($this->bookscache[$chapter->bookid])) { 110 $this->bookscache[$chapter->bookid] = $DB->get_record('book', array('id' => $chapter->bookid), '*', MUST_EXIST); 111 } 112 $book = $this->bookscache[$chapter->bookid]; 113 $cminfo = $this->get_cm('book', $chapter->bookid, $book->course); 114 } catch (\dml_missing_record_exception $ex) { 115 return \core_search\manager::ACCESS_DELETED; 116 } catch (\dml_exception $ex) { 117 return \core_search\manager::ACCESS_DENIED; 118 } 119 120 // Recheck uservisible although it should have already been checked in core_search. 121 if ($cminfo->uservisible === false) { 122 return \core_search\manager::ACCESS_DENIED; 123 } 124 125 $context = \context_module::instance($cminfo->id); 126 127 if (!has_capability('mod/book:read', $context)) { 128 return \core_search\manager::ACCESS_DENIED; 129 } 130 131 // See if the user can see chapter if it is hidden. 132 if ($chapter->hidden && !has_capability('mod/book:viewhiddenchapters', $context)) { 133 return \core_search\manager::ACCESS_DENIED; 134 } 135 136 return \core_search\manager::ACCESS_GRANTED; 137 } 138 139 /** 140 * Returns a url to the chapter. 141 * 142 * @param \core_search\document $doc 143 * @return \moodle_url 144 */ 145 public function get_doc_url(\core_search\document $doc) { 146 $contextmodule = \context::instance_by_id($doc->get('contextid')); 147 $params = array('id' => $contextmodule->instanceid, 'chapterid' => $doc->get('itemid')); 148 return new \moodle_url('/mod/book/view.php', $params); 149 } 150 151 /** 152 * Returns a url to the book. 153 * 154 * @param \core_search\document $doc 155 * @return \moodle_url 156 */ 157 public function get_context_url(\core_search\document $doc) { 158 $contextmodule = \context::instance_by_id($doc->get('contextid')); 159 return new \moodle_url('/mod/book/view.php', array('id' => $contextmodule->instanceid)); 160 } 161 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |