Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 402 and 403]
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_data activity entries. 19 * 20 * @package mod_data 21 * @copyright 2016 Devang Gaur 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace mod_data\search; 26 27 use mod_data\manager; 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 require_once($CFG->dirroot . '/mod/data/lib.php'); 32 require_once($CFG->dirroot . '/lib/grouplib.php'); 33 34 /** 35 * Search area for mod_data activity entries. 36 * 37 * @package mod_data 38 * @copyright 2016 Devang Gaur 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 class entry extends \core_search\base_mod { 42 43 /** 44 * @var array Internal quick static cache. 45 */ 46 protected $entriesdata = array(); 47 48 /** 49 * Returns recordset containing required data for indexing database entries. 50 * 51 * @param int $modifiedfrom timestamp 52 * @param \context|null $context Optional context to restrict scope of returned results 53 * @return moodle_recordset|null Recordset (or null if no results) 54 */ 55 public function get_document_recordset($modifiedfrom = 0, \context $context = null) { 56 global $DB; 57 58 list ($contextjoin, $contextparams) = $this->get_context_restriction_sql( 59 $context, 'data', 'd', SQL_PARAMS_NAMED); 60 if ($contextjoin === null) { 61 return null; 62 } 63 64 $sql = "SELECT dr.*, d.course 65 FROM {data_records} dr 66 JOIN {data} d ON d.id = dr.dataid 67 $contextjoin 68 WHERE dr.timemodified >= :timemodified"; 69 return $DB->get_recordset_sql($sql, 70 array_merge($contextparams, ['timemodified' => $modifiedfrom])); 71 } 72 73 /** 74 * Returns the documents associated with this glossary entry id. 75 * 76 * @param stdClass $entry glossary entry. 77 * @param array $options 78 * @return \core_search\document 79 */ 80 public function get_document($entry, $options = array()) { 81 try { 82 $cm = $this->get_cm('data', $entry->dataid, $entry->course); 83 $context = \context_module::instance($cm->id); 84 } catch (\dml_missing_record_exception $ex) { 85 // Notify it as we run here as admin, we should see everything. 86 debugging('Error retrieving mod_data ' . $entry->id . ' document, not all required data is available: ' . 87 $ex->getMessage(), DEBUG_DEVELOPER); 88 return false; 89 } catch (\dml_exception $ex) { 90 // Notify it as we run here as admin, we should see everything. 91 debugging('Error retrieving mod_data' . $entry->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER); 92 return false; 93 } 94 95 // Prepare associative array with data from DB. 96 $doc = \core_search\document_factory::instance($entry->id, $this->componentname, $this->areaname); 97 $doc->set('contextid', $context->id); 98 $doc->set('courseid', $entry->course); 99 $doc->set('userid', $entry->userid); 100 if ($entry->groupid > 0) { 101 $doc->set('groupid', $entry->groupid); 102 } 103 $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); 104 $doc->set('modified', $entry->timemodified); 105 106 $indexfields = $this->get_fields_for_entries($entry); 107 108 if (count($indexfields) < 2) { 109 return false; 110 } 111 112 // All fields should be already returned as plain text by data_field_base::get_content_value. 113 $doc->set('title', $indexfields[0]); 114 $doc->set('content', $indexfields[1]); 115 116 if (isset($indexfields[2])) { 117 $doc->set('description1', $indexfields[2]); 118 } 119 120 if (isset($indexfields[3])) { 121 $doc->set('description2', $indexfields[3]); 122 } 123 124 return $doc; 125 } 126 127 /** 128 * Whether the user can access the document or not. 129 * 130 * @throws \dml_missing_record_exception 131 * @throws \dml_exception 132 * @param int $id Glossary entry id 133 * @return bool 134 */ 135 public function check_access($id) { 136 global $DB, $USER; 137 138 if (isguestuser()) { 139 return \core_search\manager::ACCESS_DENIED; 140 } 141 142 $now = time(); 143 144 $sql = "SELECT dr.*, d.* 145 FROM {data_records} dr 146 JOIN {data} d ON d.id = dr.dataid 147 WHERE dr.id = ?"; 148 149 $entry = $DB->get_record_sql($sql, array( $id ), IGNORE_MISSING); 150 151 if (!$entry) { 152 return \core_search\manager::ACCESS_DELETED; 153 } 154 155 if (($entry->timeviewfrom && $now < $entry->timeviewfrom) || ($entry->timeviewto && $now > $entry->timeviewto)) { 156 return \core_search\manager::ACCESS_DENIED; 157 } 158 159 $cm = $this->get_cm('data', $entry->dataid, $entry->course); 160 $context = \context_module::instance($cm->id); 161 162 $canmanageentries = has_capability('mod/data:manageentries', $context); 163 164 if (!has_capability('mod/data:viewentry', $context)) { 165 return \core_search\manager::ACCESS_DENIED; 166 } 167 168 $numberofentriesindb = $DB->count_records('data_records', array('dataid' => $entry->dataid)); 169 $requiredentriestoview = $entry->requiredentriestoview; 170 171 if ($requiredentriestoview && ($requiredentriestoview > $numberofentriesindb) && 172 ($USER->id != $entry->userid) && !$canmanageentries) { 173 return \core_search\manager::ACCESS_DENIED; 174 } 175 176 if ($entry->approval && !$entry->approved && ($entry->userid != $USER->id) && !$canmanageentries) { 177 return \core_search\manager::ACCESS_DENIED; 178 } 179 180 $currentgroup = groups_get_activity_group($cm, true); 181 $groupmode = groups_get_activity_groupmode($cm); 182 183 if (($groupmode == 1) && ($entry->groupid != $currentgroup) && !$canmanageentries) { 184 return \core_search\manager::ACCESS_DENIED; 185 } 186 187 return \core_search\manager::ACCESS_GRANTED; 188 } 189 190 /** 191 * Link to database entry. 192 * 193 * @param \core_search\document $doc 194 * @return \moodle_url 195 */ 196 public function get_doc_url(\core_search\document $doc) { 197 $entry = $this->get_entry($doc->get('itemid')); 198 return new \moodle_url('/mod/data/view.php', array( 'd' => $entry->dataid, 'rid' => $entry->id )); 199 } 200 201 /** 202 * Link to the database activity. 203 * 204 * @param \core_search\document $doc 205 * @return \moodle_url 206 */ 207 public function get_context_url(\core_search\document $doc) { 208 $entry = $this->get_entry($doc->get('itemid')); 209 return new \moodle_url('/mod/data/view.php', array('d' => $entry->dataid)); 210 } 211 212 /** 213 * Returns true if this area uses file indexing. 214 * 215 * @return bool 216 */ 217 public function uses_file_indexing() { 218 return true; 219 } 220 221 /** 222 * Add the database entries attachments. 223 * 224 * @param \core_search\document $doc 225 * @return void 226 */ 227 public function attach_files($doc) { 228 global $DB; 229 230 $entryid = $doc->get('itemid'); 231 232 try { 233 $entry = $this->get_entry($entryid); 234 } catch (\dml_missing_record_exception $e) { 235 debugging('Could not get record to attach files to '.$doc->get('id'), DEBUG_DEVELOPER); 236 return; 237 } 238 239 $cm = $this->get_cm('data', $entry->dataid, $doc->get('courseid')); 240 $context = \context_module::instance($cm->id); 241 242 // Get all content fields which have files in them. 243 $contentssql = " 244 SELECT con.* 245 FROM {data_content} con 246 JOIN {files} fil 247 ON fil.component = :component 248 AND fil.filearea = :filearea 249 AND fil.itemid = con.id 250 WHERE con.recordid = :recordid 251 "; 252 $contents = $DB->get_recordset_sql($contentssql, [ 253 'recordid' => $entryid, 254 'component' => 'mod_data', 255 'filearea' => 'content', 256 ]); 257 foreach ($contents as $content) { 258 // Get the files and attach them. 259 $fs = get_file_storage(); 260 $files = $fs->get_area_files($context->id, 'mod_data', 'content', $content->id, 'filename', false); 261 foreach ($files as $file) { 262 $doc->add_stored_file($file); 263 } 264 } 265 $contents->close(); 266 } 267 268 /** 269 * Get database entry data 270 * 271 * @throws \dml_exception 272 * @param int $entryid 273 * @return stdClass 274 */ 275 protected function get_entry($entryid) { 276 global $DB; 277 278 if (empty($this->entriesdata[$entryid])) { 279 $this->entriesdata[$entryid] = $DB->get_record('data_records', array( 'id' => $entryid ), '*', MUST_EXIST); 280 } 281 282 return $this->entriesdata[$entryid]; 283 } 284 285 /** 286 * get_fields_for_entries 287 * 288 * @param StdClass $entry 289 * @return array 290 */ 291 protected function get_fields_for_entries($entry) { 292 global $DB; 293 294 $indexfields = array(); 295 $validfieldtypes = array('text', 'textarea', 'menu', 'radiobutton', 'checkbox', 'multimenu', 'url'); 296 297 $sql = "SELECT dc.*, df.name AS fldname, 298 df.type AS fieldtype, df.required 299 FROM {data_content} dc, {data_fields} df 300 WHERE dc.fieldid = df.id 301 AND dc.recordid = :recordid"; 302 303 $contents = $DB->get_records_sql($sql, ['recordid' => $entry->id]); 304 $filteredcontents = []; 305 306 $data = $DB->get_record('data', ['id' => $entry->dataid]); 307 $manager = manager::create_from_instance($data); 308 $template = $manager->get_template('addtemplate'); 309 $template = $template->get_template_content(); 310 311 // Filtering out the data_content records having invalid fieldtypes. 312 foreach ($contents as $content) { 313 if (in_array($content->fieldtype, $validfieldtypes)) { 314 $filteredcontents[] = $content; 315 } 316 } 317 318 foreach ($filteredcontents as $content) { 319 $classname = $this->get_field_class_name($content->fieldtype); 320 if (!$classname) { 321 $content->addtemplateposition = -1; 322 continue; 323 } 324 $content->priority = $classname::get_priority(); 325 $content->addtemplateposition = strpos($template ?? '', '[['.$content->fldname.']]'); 326 } 327 328 $orderqueue = new \SPLPriorityQueue(); 329 330 // Filtering out contents which belong to fields that aren't present in the addtemplate of the database activity instance. 331 foreach ($filteredcontents as $content) { 332 333 if ($content->addtemplateposition >= 0) { 334 $orderqueue->insert($content, $content->addtemplateposition); 335 } 336 } 337 338 $filteredcontents = array(); 339 340 while ($orderqueue->valid()) { 341 $filteredcontents[] = $orderqueue->extract(); 342 } 343 344 // SPLPriorityQueue sorts according to descending order of the priority (here, addtemplateposition). 345 $filteredcontents = array_reverse($filteredcontents); 346 347 // Using a CUSTOM SPLPriorityQueure instance to sort out the filtered contents according to these rules : 348 // 1. Priorities in $fieldtypepriorities 349 // 2. Compulsory fieldtypes are to be given the top priority. 350 $contentqueue = new sortedcontentqueue($filteredcontents); 351 352 foreach ($filteredcontents as $key => $content) { 353 $contentqueue->insert($content, $key); 354 } 355 356 while ($contentqueue->valid()) { 357 358 $content = $contentqueue->extract(); 359 $classname = $this->get_field_class_name($content->fieldtype); 360 $indexfields[] = $classname::get_content_value($content); 361 } 362 363 // Limited to 4 fields as a document only has 4 content fields. 364 if (count($indexfields) > 4) { 365 $indexfields[3] = implode(' ', array_slice($indexfields, 3)); 366 } 367 return $indexfields; 368 } 369 370 /** 371 * Returns the class name for the given field type and includes it. 372 * 373 * @param string $fieldtype 374 * @return string|null It will return the class name or null if the field type is not available. 375 */ 376 protected function get_field_class_name($fieldtype) { 377 global $CFG; 378 379 $fieldtype = trim($fieldtype); 380 381 $fieldpath = $CFG->dirroot . '/mod/data/field/' . $fieldtype . '/field.class.php'; 382 if (!file_exists($fieldpath)) { 383 return null; 384 } 385 386 require_once($fieldpath); 387 return 'data_field_' . $fieldtype; 388 } 389 390 /** 391 * Confirms that data entries support group restrictions. 392 * 393 * @return bool True 394 */ 395 public function supports_group_restriction() { 396 return true; 397 } 398 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body