"MDL-23308, bring course files back"
[moodle.git] / course / publish / lib.php
blob40730454fe69cd6a6c254e992b37e9114c36c127
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/>.
17 /// TIME PERIOD ///
19 define('HUB_LASTMODIFIED_WEEK', 7);
21 define('HUB_LASTMODIFIED_FORTEENNIGHT', 14);
23 define('HUB_LASTMODIFIED_MONTH', 30);
27 //// AUDIENCE ////
29 /**
30 * Audience: educators
32 define('HUB_AUDIENCE_EDUCATORS', 'educators');
34 /**
35 * Audience: students
37 define('HUB_AUDIENCE_STUDENTS', 'students');
39 /**
40 * Audience: admins
42 define('HUB_AUDIENCE_ADMINS', 'admins');
46 ///// EDUCATIONAL LEVEL /////
48 /**
49 * Educational level: primary
51 define('HUB_EDULEVEL_PRIMARY', 'primary');
53 /**
54 * Educational level: secondary
56 define('HUB_EDULEVEL_SECONDARY', 'secondary');
58 /**
59 * Educational level: tertiary
61 define('HUB_EDULEVEL_TERTIARY', 'tertiary');
63 /**
64 * Educational level: government
66 define('HUB_EDULEVEL_GOVERNMENT', 'government');
68 /**
69 * Educational level: association
71 define('HUB_EDULEVEL_ASSOCIATION', 'association');
73 /**
74 * Educational level: corporate
76 define('HUB_EDULEVEL_CORPORATE', 'corporate');
78 /**
79 * Educational level: other
81 define('HUB_EDULEVEL_OTHER', 'other');
85 ///// FILE TYPES /////
87 /**
88 * FILE TYPE: COURSE SCREENSHOT
90 define('HUB_SCREENSHOT_FILE_TYPE', 'screenshot');
92 /**
93 * FILE TYPE: HUB SCREENSHOT
95 define('HUB_HUBSCREENSHOT_FILE_TYPE', 'hubscreenshot');
97 /**
98 * FILE TYPE: BACKUP
100 define('HUB_BACKUP_FILE_TYPE', 'backup');
104 * Course publication library
106 * @package course
107 * @copyright 2010 Moodle Pty Ltd (http://moodle.com)
108 * @author Jerome Mouneyrac
109 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
110 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
112 class course_publish_manager {
115 * Record a course publication
116 * @param int $hubid the hub id from the 'registered on hub' table
117 * @param int $courseid the course id from site point of view
118 * @param int $enrollable if the course is enrollable = 1, if downloadable = 0
119 * @param int $hubcourseid the course id from the hub point of view
121 public function add_course_publication($huburl, $courseid, $enrollable, $hubcourseid) {
122 global $DB;
123 $publication = new stdClass();
124 $publication->huburl = $huburl;
125 $publication->courseid = $courseid;
126 $publication->hubcourseid = $hubcourseid;
127 $publication->enrollable = $enrollable;
128 $publication->timepublished = time();
129 $DB->insert_record('course_published', $publication);
133 * Update a enrollable course publication
134 * @param int $publicationid
136 public function update_enrollable_course_publication($publicationid) {
137 global $DB;
138 $publication = new stdClass();
139 $publication->id = $publicationid;
140 $publication->timepublished = time();
141 $DB->update_record('course_published', $publication);
145 * Update a course publication
146 * @param object $publication
148 public function update_publication($publication) {
149 global $DB;
150 $DB->update_record('course_published', $publication);
154 * Get courses publications
155 * @param int $hubid specify a hub
156 * @param int $courseid specify a course
157 * @param int $enrollable specify type of publication (enrollable or downloadable)
158 * @return array of publications
160 public function get_publications($huburl = null, $courseid = null, $enrollable = -1) {
161 global $DB;
162 $params = array();
164 if (!empty($huburl)) {
165 $params['huburl'] = $huburl;
168 if (!empty($courseid)) {
169 $params['courseid'] = $courseid;
172 if ($enrollable != -1) {
173 $params['enrollable'] = $enrollable;
176 return $DB->get_records('course_published', $params);
180 * Get a publication for a course id on a hub
181 * (which is either the id of the unique possible enrollable publication of a course,
182 * either an id of one of the downloadable publication)
183 * @param int $hubcourseid
184 * @param string $huburl
185 * @return object publication
187 public function get_publication($hubcourseid, $huburl) {
188 global $DB;
189 return $DB->get_record('course_published',
190 array('hubcourseid' => $hubcourseid, 'huburl' => $huburl));
194 * Get all publication for a course
195 * @param int $courseid
196 * @return array of publication
198 public function get_course_publications($courseid) {
199 global $DB;
200 $sql = 'SELECT cp.id, cp.status, cp.timechecked, cp.timepublished, rh.hubname,
201 rh.huburl, cp.courseid, cp.enrollable, cp.hubcourseid
202 FROM {course_published} cp, {registration_hubs} rh
203 WHERE cp.huburl = rh.huburl and cp.courseid = :courseid
204 ORDER BY cp.enrollable DESC, rh.hubname, cp.timepublished';
205 $params = array('courseid' => $courseid);
206 return $DB->get_records_sql($sql, $params);
210 * Get the hub concerned by a publication
211 * @param int $publicationid
212 * @return object the hub (id, name, url, token)
214 public function get_registeredhub_by_publication($publicationid) {
215 global $DB;
216 $sql = 'SELECT rh.huburl, rh.hubname, rh.token
217 FROM {course_published} cp, {registration_hubs} rh
218 WHERE cp.huburl = rh.huburl and cp.id = :publicationid';
219 $params = array('publicationid' => $publicationid);
220 return $DB->get_record_sql($sql, $params);
224 * Delete a publication
225 * @param int $publicationid
227 public function delete_publication($publicationid) {
228 global $DB;
229 $DB->delete_records('course_published', array('id' => $publicationid));
233 * Delete publications for a hub
234 * @param string $huburl
235 * @param int $enrollable
237 public function delete_hub_publications($huburl, $enrollable = -1) {
238 global $DB;
240 $params = array();
242 if (!empty($huburl)) {
243 $params['huburl'] = $huburl;
246 if ($enrollable != -1) {
247 $params['enrollable'] = $enrollable;
250 $DB->delete_records('course_published', $params);
254 * Get an array of all block instances for a given context
255 * @param int $contextid a context id
256 * @return array of block instances.
258 public function get_block_instances_by_context($contextid, $sort = '') {
259 global $DB;
260 return $DB->get_records('block_instances', array('parentcontextid' => $contextid), $sort);
264 * Retrieve all the sorted course subjects
265 * @return array $subjects
267 public function get_sorted_subjects() {
268 $subjects = get_string_manager()->load_component_strings('edufields', current_language());
270 //sort the subjects
271 asort($subjects);
272 foreach ($subjects as $key => $option) {
273 $keylength = strlen($key);
274 if ($keylength == 8) {
275 $toplevel[$key] = $option;
276 } else if ($keylength == 10) {
277 $sublevel[substr($key, 0, 8)][$key] = $option;
278 } else if ($keylength == 12) {
279 $subsublevel[substr($key, 0, 8)][substr($key, 0, 10)][$key] = $option;
283 //recreate the initial structure returned by get_string_manager()
284 $subjects = array();
285 foreach ($toplevel as $key => $name) {
286 $subjects[$key] = $name;
287 foreach ($sublevel[$key] as $subkey => $subname) {
288 $subjects[$subkey] = $subname;
289 foreach ($subsublevel[$key][$subkey] as $subsubkey => $subsubname) {
290 $subjects[$subsubkey] = $subsubname;
295 return $subjects;