PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/repository/recent/lib.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 216 lines | 125 code | 14 blank | 77 comment | 7 complexity | f147f71fd505807d705852adeb3b64c6 MD5 | raw file
  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. * This plugin is used to access recent used files
  18. *
  19. * @since 2.0
  20. * @package repository_recent
  21. * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once($CFG->dirroot . '/repository/lib.php');
  25. /**
  26. * repository_recent class is used to browse recent used files
  27. *
  28. * @since 2.0
  29. * @package repository_recent
  30. * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. define('DEFAULT_RECENT_FILES_NUM', 50);
  34. class repository_recent extends repository {
  35. /**
  36. * Initialize recent plugin
  37. * @param int $repositoryid
  38. * @param int $context
  39. * @param array $options
  40. */
  41. public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
  42. parent::__construct($repositoryid, $context, $options);
  43. $number = get_config('recent', 'recentfilesnumber');
  44. $number = (int)$number;
  45. if (empty($number)) {
  46. $this->number = DEFAULT_RECENT_FILES_NUM;
  47. } else {
  48. $this->number = $number;
  49. }
  50. }
  51. /**
  52. * recent plugin doesn't require login, so list all files
  53. * @return mixed
  54. */
  55. public function print_login() {
  56. return $this->get_listing();
  57. }
  58. private function get_recent_files($limitfrom = 0, $limit = DEFAULT_RECENT_FILES_NUM) {
  59. // XXX: get current itemid
  60. global $USER, $DB, $itemid;
  61. // This SQL will ignore draft files if not owned by current user.
  62. // Ignore all file references.
  63. $sql = 'SELECT files1.*
  64. FROM {files} files1
  65. LEFT JOIN {files_reference} r
  66. ON files1.referencefileid = r.id
  67. JOIN (
  68. SELECT contenthash, filename, MAX(id) AS id
  69. FROM {files}
  70. WHERE userid = :userid
  71. AND filename != :filename
  72. AND ((filearea = :filearea1 AND itemid = :itemid) OR filearea != :filearea2)
  73. GROUP BY contenthash, filename
  74. ) files2 ON files1.id = files2.id
  75. WHERE r.repositoryid is NULL
  76. ORDER BY files1.timemodified DESC';
  77. $params = array(
  78. 'userid' => $USER->id,
  79. 'filename' => '.',
  80. 'filearea1' => 'draft',
  81. 'itemid' => $itemid,
  82. 'filearea2' => 'draft');
  83. $rs = $DB->get_recordset_sql($sql, $params, $limitfrom, $limit);
  84. $result = array();
  85. foreach ($rs as $file_record) {
  86. $info = array();
  87. $info['contextid'] = $file_record->contextid;
  88. $info['itemid'] = $file_record->itemid;
  89. $info['filearea'] = $file_record->filearea;
  90. $info['component'] = $file_record->component;
  91. $info['filepath'] = $file_record->filepath;
  92. $info['filename'] = $file_record->filename;
  93. $result[$file_record->pathnamehash] = $info;
  94. }
  95. $rs->close();
  96. return $result;
  97. }
  98. /**
  99. * Get file listing
  100. *
  101. * @param string $encodedpath
  102. * @param string $path not used by this plugin
  103. * @return mixed
  104. */
  105. public function get_listing($encodedpath = '', $page = '') {
  106. global $OUTPUT;
  107. $ret = array();
  108. $ret['dynload'] = true;
  109. $ret['nosearch'] = true;
  110. $ret['nologin'] = true;
  111. $list = array();
  112. $files = $this->get_recent_files(0, $this->number);
  113. try {
  114. foreach ($files as $file) {
  115. // Check that file exists and accessible, retrieve size/date info
  116. $browser = get_file_browser();
  117. $context = context::instance_by_id($file['contextid']);
  118. $fileinfo = $browser->get_file_info($context, $file['component'],
  119. $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']);
  120. if ($fileinfo) {
  121. $params = base64_encode(serialize($file));
  122. $node = array(
  123. 'title' => $fileinfo->get_visible_name(),
  124. 'size' => $fileinfo->get_filesize(),
  125. 'datemodified' => $fileinfo->get_timemodified(),
  126. 'datecreated' => $fileinfo->get_timecreated(),
  127. 'author' => $fileinfo->get_author(),
  128. 'license' => $fileinfo->get_license(),
  129. 'source'=> $params,
  130. 'icon' => $OUTPUT->pix_url(file_file_icon($fileinfo, 24))->out(false),
  131. 'thumbnail' => $OUTPUT->pix_url(file_file_icon($fileinfo, 90))->out(false),
  132. );
  133. if ($imageinfo = $fileinfo->get_imageinfo()) {
  134. $fileurl = new moodle_url($fileinfo->get_url());
  135. $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $fileinfo->get_timemodified()));
  136. $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $fileinfo->get_timemodified()));
  137. $node['image_width'] = $imageinfo['width'];
  138. $node['image_height'] = $imageinfo['height'];
  139. }
  140. $list[] = $node;
  141. }
  142. }
  143. } catch (Exception $e) {
  144. throw new repository_exception('emptyfilelist', 'repository_recent');
  145. }
  146. $ret['list'] = array_filter($list, array($this, 'filter'));
  147. return $ret;
  148. }
  149. public static function get_type_option_names() {
  150. return array('recentfilesnumber', 'pluginname');
  151. }
  152. public static function type_config_form($mform, $classname = 'repository') {
  153. parent::type_config_form($mform, $classname);
  154. $number = get_config('repository_recent', 'recentfilesnumber');
  155. if (empty($number)) {
  156. $number = DEFAULT_RECENT_FILES_NUM;
  157. }
  158. $mform->addElement('text', 'recentfilesnumber', get_string('recentfilesnumber', 'repository_recent'));
  159. $mform->setType('recentfilesnumber', PARAM_INT);
  160. $mform->setDefault('recentfilesnumber', $number);
  161. }
  162. /**
  163. * This plugin doesn't support to link to external links
  164. *
  165. * @return int
  166. */
  167. public function supported_returntypes() {
  168. return FILE_INTERNAL;
  169. }
  170. /**
  171. * Repository method to make sure that user can access particular file.
  172. *
  173. * This is checked when user tries to pick the file from repository to deal with
  174. * potential parameter substitutions is request
  175. *
  176. * @todo MDL-33805 remove this function when recent files are managed correctly
  177. *
  178. * @param string $source
  179. * @return bool whether the file is accessible by current user
  180. */
  181. public function file_is_accessible($source) {
  182. global $USER;
  183. $file = self::get_moodle_file($source);
  184. return (!empty($file) && $file->get_userid() == $USER->id);
  185. }
  186. /**
  187. * Does this repository used to browse moodle files?
  188. *
  189. * @return boolean
  190. */
  191. public function has_moodle_files() {
  192. return true;
  193. }
  194. /**
  195. * Is this repository accessing private data?
  196. *
  197. * @return bool
  198. */
  199. public function contains_private_data() {
  200. return false;
  201. }
  202. }