MDL-44620 behat: Automate MDLQA-7
[moodle.git] / mod / url / lib.php
blob64d7ce0591f03ccf0b14f1194237b9e79258d58d
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 * Mandatory public API of url module
21 * @package mod_url
22 * @copyright 2009 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die;
28 /**
29 * List of features supported in URL module
30 * @param string $feature FEATURE_xx constant for requested feature
31 * @return mixed True if module supports feature, false if not, null if doesn't know
33 function url_supports($feature) {
34 switch($feature) {
35 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
36 case FEATURE_GROUPS: return false;
37 case FEATURE_GROUPINGS: return false;
38 case FEATURE_GROUPMEMBERSONLY: return true;
39 case FEATURE_MOD_INTRO: return true;
40 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
41 case FEATURE_GRADE_HAS_GRADE: return false;
42 case FEATURE_GRADE_OUTCOMES: return false;
43 case FEATURE_BACKUP_MOODLE2: return true;
44 case FEATURE_SHOW_DESCRIPTION: return true;
46 default: return null;
50 /**
51 * Returns all other caps used in module
52 * @return array
54 function url_get_extra_capabilities() {
55 return array('moodle/site:accessallgroups');
58 /**
59 * This function is used by the reset_course_userdata function in moodlelib.
60 * @param $data the data submitted from the reset course.
61 * @return array status array
63 function url_reset_userdata($data) {
64 return array();
67 /**
68 * List the actions that correspond to a view of this module.
69 * This is used by the participation report.
71 * Note: This is not used by new logging system. Event with
72 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
73 * be considered as view action.
75 * @return array
77 function url_get_view_actions() {
78 return array('view', 'view all');
81 /**
82 * List the actions that correspond to a post of this module.
83 * This is used by the participation report.
85 * Note: This is not used by new logging system. Event with
86 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
87 * will be considered as post action.
89 * @return array
91 function url_get_post_actions() {
92 return array('update', 'add');
95 /**
96 * Add url instance.
97 * @param object $data
98 * @param object $mform
99 * @return int new url instance id
101 function url_add_instance($data, $mform) {
102 global $CFG, $DB;
104 require_once($CFG->dirroot.'/mod/url/locallib.php');
106 $parameters = array();
107 for ($i=0; $i < 100; $i++) {
108 $parameter = "parameter_$i";
109 $variable = "variable_$i";
110 if (empty($data->$parameter) or empty($data->$variable)) {
111 continue;
113 $parameters[$data->$parameter] = $data->$variable;
115 $data->parameters = serialize($parameters);
117 $displayoptions = array();
118 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
119 $displayoptions['popupwidth'] = $data->popupwidth;
120 $displayoptions['popupheight'] = $data->popupheight;
122 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
123 $displayoptions['printintro'] = (int)!empty($data->printintro);
125 $data->displayoptions = serialize($displayoptions);
127 $data->externalurl = url_fix_submitted_url($data->externalurl);
129 $data->timemodified = time();
130 $data->id = $DB->insert_record('url', $data);
132 return $data->id;
136 * Update url instance.
137 * @param object $data
138 * @param object $mform
139 * @return bool true
141 function url_update_instance($data, $mform) {
142 global $CFG, $DB;
144 require_once($CFG->dirroot.'/mod/url/locallib.php');
146 $parameters = array();
147 for ($i=0; $i < 100; $i++) {
148 $parameter = "parameter_$i";
149 $variable = "variable_$i";
150 if (empty($data->$parameter) or empty($data->$variable)) {
151 continue;
153 $parameters[$data->$parameter] = $data->$variable;
155 $data->parameters = serialize($parameters);
157 $displayoptions = array();
158 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
159 $displayoptions['popupwidth'] = $data->popupwidth;
160 $displayoptions['popupheight'] = $data->popupheight;
162 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
163 $displayoptions['printintro'] = (int)!empty($data->printintro);
165 $data->displayoptions = serialize($displayoptions);
167 $data->externalurl = url_fix_submitted_url($data->externalurl);
169 $data->timemodified = time();
170 $data->id = $data->instance;
172 $DB->update_record('url', $data);
174 return true;
178 * Delete url instance.
179 * @param int $id
180 * @return bool true
182 function url_delete_instance($id) {
183 global $DB;
185 if (!$url = $DB->get_record('url', array('id'=>$id))) {
186 return false;
189 // note: all context files are deleted automatically
191 $DB->delete_records('url', array('id'=>$url->id));
193 return true;
197 * Given a course_module object, this function returns any
198 * "extra" information that may be needed when printing
199 * this activity in a course listing.
201 * See {@link get_array_of_activities()} in course/lib.php
203 * @param object $coursemodule
204 * @return cached_cm_info info
206 function url_get_coursemodule_info($coursemodule) {
207 global $CFG, $DB;
208 require_once("$CFG->dirroot/mod/url/locallib.php");
210 if (!$url = $DB->get_record('url', array('id'=>$coursemodule->instance),
211 'id, name, display, displayoptions, externalurl, parameters, intro, introformat')) {
212 return NULL;
215 $info = new cached_cm_info();
216 $info->name = $url->name;
218 //note: there should be a way to differentiate links from normal resources
219 $info->icon = url_guess_icon($url->externalurl, 24);
221 $display = url_get_final_display_type($url);
223 if ($display == RESOURCELIB_DISPLAY_POPUP) {
224 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&amp;redirect=1";
225 $options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
226 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
227 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
228 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
229 $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
231 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
232 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&amp;redirect=1";
233 $info->onclick = "window.open('$fullurl'); return false;";
237 if ($coursemodule->showdescription) {
238 // Convert intro to html. Do not filter cached version, filters run at display time.
239 $info->content = format_module_intro('url', $url, $coursemodule->id, false);
242 return $info;
246 * Return a list of page types
247 * @param string $pagetype current page type
248 * @param stdClass $parentcontext Block's parent context
249 * @param stdClass $currentcontext Current context of block
251 function url_page_type_list($pagetype, $parentcontext, $currentcontext) {
252 $module_pagetype = array('mod-url-*'=>get_string('page-mod-url-x', 'url'));
253 return $module_pagetype;
257 * Export URL resource contents
259 * @return array of file content
261 function url_export_contents($cm, $baseurl) {
262 global $CFG, $DB;
263 require_once("$CFG->dirroot/mod/url/locallib.php");
264 $contents = array();
265 $context = context_module::instance($cm->id);
267 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
268 $url = $DB->get_record('url', array('id'=>$cm->instance), '*', MUST_EXIST);
270 $fullurl = str_replace('&amp;', '&', url_get_full_url($url, $cm, $course));
271 $isurl = clean_param($fullurl, PARAM_URL);
272 if (empty($isurl)) {
273 return null;
276 $url = array();
277 $url['type'] = 'url';
278 $url['filename'] = $url->name;
279 $url['filepath'] = null;
280 $url['filesize'] = 0;
281 $url['fileurl'] = $fullurl;
282 $url['timecreated'] = null;
283 $url['timemodified'] = $url->timemodified;
284 $url['sortorder'] = null;
285 $url['userid'] = null;
286 $url['author'] = null;
287 $url['license'] = null;
288 $contents[] = $url;
290 return $contents;
294 * Register the ability to handle drag and drop file uploads
295 * @return array containing details of the files / types the mod can handle
297 function url_dndupload_register() {
298 return array('types' => array(
299 array('identifier' => 'url', 'message' => get_string('createurl', 'url'))
304 * Handle a file that has been uploaded
305 * @param object $uploadinfo details of the file / content that has been uploaded
306 * @return int instance id of the newly created mod
308 function url_dndupload_handle($uploadinfo) {
309 // Gather all the required data.
310 $data = new stdClass();
311 $data->course = $uploadinfo->course->id;
312 $data->name = $uploadinfo->displayname;
313 $data->intro = '<p>'.$uploadinfo->displayname.'</p>';
314 $data->introformat = FORMAT_HTML;
315 $data->externalurl = clean_param($uploadinfo->content, PARAM_URL);
316 $data->timemodified = time();
318 // Set the display options to the site defaults.
319 $config = get_config('url');
320 $data->display = $config->display;
321 $data->popupwidth = $config->popupwidth;
322 $data->popupheight = $config->popupheight;
323 $data->printintro = $config->printintro;
325 return url_add_instance($data, null);