MDL-36266 Improve update notification message subject
[moodle.git] / mod / workshop / fileinfolib.php
blob0427819d90bca0173d527c9e5facca8ecdc4ae8d
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Provides code used during file browsing
21 * @category files
22 * @package mod_workshop
23 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Represents virtual root node for all submissions
32 * Workshop submission uses two fileareas: submission_content for editor's embeded media
33 * and submission_attachment for attachments. In both, the itemid represents the submission id.
34 * This container is used to display the list of all submissions in these areas (ie when
35 * these areas are browsed with itemid == null).
37 class workshop_file_info_submissions_container extends file_info {
38 protected $course;
39 protected $cm;
40 protected $areas;
41 protected $filearea;
43 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
44 parent::__construct($browser, $context);
45 $this->course = $course;
46 $this->cm = $cm;
47 $this->areas = $areas;
48 $this->filearea = $filearea;
51 /**
52 * Returns list of standard virtual file/directory identification.
53 * The difference from stored_file parameters is that null values
54 * are allowed in all fields
55 * @return array with keys contextid, filearea, itemid, filepath and filename
57 public function get_params() {
58 return array('contextid'=>$this->context->id,
59 'component'=>'mod_workshop',
60 'filearea' =>$this->filearea,
61 'itemid' =>null,
62 'filepath' =>null,
63 'filename' =>null);
66 /**
67 * Returns localised visible name.
68 * @return string
70 public function get_visible_name() {
71 return $this->areas[$this->filearea];
74 /**
75 * Can I add new files or directories?
76 * @return bool
78 public function is_writable() {
79 return false;
82 /**
83 * Is directory?
84 * @return bool
86 public function is_directory() {
87 return true;
91 /**
92 * Returns list of children nodes
94 * @return array of file_info instances
96 public function get_children() {
97 return $this->get_filtered_children('*', false, true);
101 * Helper function to return files matching extensions or their count
103 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
104 * @param bool|int $countonly if false returns the children, if an int returns just the
105 * count of children but stops counting when $countonly number of children is reached
106 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
107 * @return array|int array of file_info instances or the count
109 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
110 global $DB;
111 $params = array('contextid' => $this->context->id,
112 'component' => 'mod_workshop',
113 'filearea' => $this->filearea);
114 $sql = 'SELECT DISTINCT itemid
115 FROM {files}
116 WHERE contextid = :contextid
117 AND component = :component
118 AND filearea = :filearea';
119 if (!$returnemptyfolders) {
120 $sql .= ' AND filename <> :emptyfilename';
121 $params['emptyfilename'] = '.';
123 list($sql2, $params2) = $this->build_search_files_sql($extensions);
124 $sql .= ' '.$sql2;
125 $params = array_merge($params, $params2);
126 if ($countonly !== false) {
127 $sql .= ' ORDER BY itemid DESC';
130 $rs = $DB->get_recordset_sql($sql, $params);
131 $children = array();
132 foreach ($rs as $record) {
133 if (($child = $this->browser->get_file_info($this->context, 'mod_workshop', $this->filearea, $record->itemid))
134 && ($returnemptyfolders || $child->count_non_empty_children($extensions))) {
135 $children[] = $child;
136 if ($countonly !== false && count($children) >= $countonly) {
137 break;
141 $rs->close();
142 if ($countonly !== false) {
143 return count($children);
145 return $children;
149 * Returns list of children which are either files matching the specified extensions
150 * or folders that contain at least one such file.
152 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
153 * @return array of file_info instances
155 public function get_non_empty_children($extensions = '*') {
156 return $this->get_filtered_children($extensions, false);
160 * Returns the number of children which are either files matching the specified extensions
161 * or folders containing at least one such file.
163 * NOTE: We don't need the exact number of non empty children if it is >=2
164 * In this function 1 is never returned to avoid skipping the single subfolder
166 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
167 * @param int $limit stop counting after at least $limit non-empty children are found
168 * @return int
170 public function count_non_empty_children($extensions = '*', $limit = 1) {
171 return $this->get_filtered_children($extensions, $limit);
175 * Returns parent file_info instance
176 * @return file_info or null for root
178 public function get_parent() {
179 return $this->browser->get_file_info($this->context);