3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Mandatory public API of url module
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;
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 or string for the module purpose.
33 function url_supports($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_MOD_INTRO
: return true;
39 case FEATURE_COMPLETION_TRACKS_VIEWS
: return true;
40 case FEATURE_GRADE_HAS_GRADE
: return false;
41 case FEATURE_GRADE_OUTCOMES
: return false;
42 case FEATURE_BACKUP_MOODLE2
: return true;
43 case FEATURE_SHOW_DESCRIPTION
: return true;
44 case FEATURE_MOD_PURPOSE
: return MOD_PURPOSE_CONTENT
;
51 * This function is used by the reset_course_userdata function in moodlelib.
52 * @param $data the data submitted from the reset course.
53 * @return array status array
55 function url_reset_userdata($data) {
57 // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
64 * List the actions that correspond to a view of this module.
65 * This is used by the participation report.
67 * Note: This is not used by new logging system. Event with
68 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
69 * be considered as view action.
73 function url_get_view_actions() {
74 return array('view', 'view all');
78 * List the actions that correspond to a post of this module.
79 * This is used by the participation report.
81 * Note: This is not used by new logging system. Event with
82 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
83 * will be considered as post action.
87 function url_get_post_actions() {
88 return array('update', 'add');
94 * @param object $mform
95 * @return int new url instance id
97 function url_add_instance($data, $mform) {
100 require_once($CFG->dirroot
.'/mod/url/locallib.php');
102 $parameters = array();
103 for ($i=0; $i < 100; $i++
) {
104 $parameter = "parameter_$i";
105 $variable = "variable_$i";
106 if (empty($data->$parameter) or empty($data->$variable)) {
109 $parameters[$data->$parameter] = $data->$variable;
111 $data->parameters
= serialize($parameters);
113 $displayoptions = array();
114 if ($data->display
== RESOURCELIB_DISPLAY_POPUP
) {
115 $displayoptions['popupwidth'] = $data->popupwidth
;
116 $displayoptions['popupheight'] = $data->popupheight
;
118 if (in_array($data->display
, array(RESOURCELIB_DISPLAY_AUTO
, RESOURCELIB_DISPLAY_EMBED
, RESOURCELIB_DISPLAY_FRAME
))) {
119 $displayoptions['printintro'] = (int)!empty($data->printintro
);
121 $data->displayoptions
= serialize($displayoptions);
123 $data->externalurl
= url_fix_submitted_url($data->externalurl
);
125 $data->timemodified
= time();
126 $data->id
= $DB->insert_record('url', $data);
128 $completiontimeexpected = !empty($data->completionexpected
) ?
$data->completionexpected
: null;
129 \core_completion\api
::update_completion_date_event($data->coursemodule
, 'url', $data->id
, $completiontimeexpected);
135 * Update url instance.
136 * @param object $data
137 * @param object $mform
140 function url_update_instance($data, $mform) {
143 require_once($CFG->dirroot
.'/mod/url/locallib.php');
145 $parameters = array();
146 for ($i=0; $i < 100; $i++
) {
147 $parameter = "parameter_$i";
148 $variable = "variable_$i";
149 if (empty($data->$parameter) or empty($data->$variable)) {
152 $parameters[$data->$parameter] = $data->$variable;
154 $data->parameters
= serialize($parameters);
156 $displayoptions = array();
157 if ($data->display
== RESOURCELIB_DISPLAY_POPUP
) {
158 $displayoptions['popupwidth'] = $data->popupwidth
;
159 $displayoptions['popupheight'] = $data->popupheight
;
161 if (in_array($data->display
, array(RESOURCELIB_DISPLAY_AUTO
, RESOURCELIB_DISPLAY_EMBED
, RESOURCELIB_DISPLAY_FRAME
))) {
162 $displayoptions['printintro'] = (int)!empty($data->printintro
);
164 $data->displayoptions
= serialize($displayoptions);
166 $data->externalurl
= url_fix_submitted_url($data->externalurl
);
168 $data->timemodified
= time();
169 $data->id
= $data->instance
;
171 $DB->update_record('url', $data);
173 $completiontimeexpected = !empty($data->completionexpected
) ?
$data->completionexpected
: null;
174 \core_completion\api
::update_completion_date_event($data->coursemodule
, 'url', $data->id
, $completiontimeexpected);
180 * Delete url instance.
184 function url_delete_instance($id) {
187 if (!$url = $DB->get_record('url', array('id'=>$id))) {
191 $cm = get_coursemodule_from_instance('url', $id);
192 \core_completion\api
::update_completion_date_event($cm->id
, 'url', $id, null);
194 // note: all context files are deleted automatically
196 $DB->delete_records('url', array('id'=>$url->id
));
202 * Given a course_module object, this function returns any
203 * "extra" information that may be needed when printing
204 * this activity in a course listing.
206 * See {@link course_modinfo::get_array_of_activities()}
208 * @param object $coursemodule
209 * @return cached_cm_info info
211 function url_get_coursemodule_info($coursemodule) {
213 require_once("$CFG->dirroot/mod/url/locallib.php");
215 if (!$url = $DB->get_record('url', array('id'=>$coursemodule->instance
),
216 'id, name, display, displayoptions, externalurl, parameters, intro, introformat')) {
220 $info = new cached_cm_info();
221 $info->name
= $url->name
;
223 //note: there should be a way to differentiate links from normal resources
224 $info->icon
= url_guess_icon($url->externalurl
, 24);
226 $display = url_get_final_display_type($url);
228 if ($display == RESOURCELIB_DISPLAY_POPUP
) {
229 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&redirect=1";
230 $options = empty($url->displayoptions
) ?
[] : (array) unserialize_array($url->displayoptions
);
231 $width = empty($options['popupwidth']) ?
620 : $options['popupwidth'];
232 $height = empty($options['popupheight']) ?
450 : $options['popupheight'];
233 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
234 $info->onclick
= "window.open('$fullurl', '', '$wh'); return false;";
236 } else if ($display == RESOURCELIB_DISPLAY_NEW
) {
237 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&redirect=1";
238 $info->onclick
= "window.open('$fullurl'); return false;";
242 if ($coursemodule->showdescription
) {
243 // Convert intro to html. Do not filter cached version, filters run at display time.
244 $info->content
= format_module_intro('url', $url, $coursemodule->id
, false);
247 $info->customdata
['display'] = $display;
248 // The icon will be filtered if it will be the default module icon.
249 $info->customdata
['filtericon'] = empty($info->icon
);
255 * Return a list of page types
256 * @param string $pagetype current page type
257 * @param stdClass $parentcontext Block's parent context
258 * @param stdClass $currentcontext Current context of block
260 function url_page_type_list($pagetype, $parentcontext, $currentcontext) {
261 $module_pagetype = array('mod-url-*'=>get_string('page-mod-url-x', 'url'));
262 return $module_pagetype;
266 * Export URL resource contents
268 * @return array of file content
270 function url_export_contents($cm, $baseurl) {
272 require_once("$CFG->dirroot/mod/url/locallib.php");
274 $context = context_module
::instance($cm->id
);
276 $course = $DB->get_record('course', array('id'=>$cm->course
), '*', MUST_EXIST
);
277 $urlrecord = $DB->get_record('url', array('id'=>$cm->instance
), '*', MUST_EXIST
);
279 $fullurl = str_replace('&', '&', url_get_full_url($urlrecord, $cm, $course));
280 $isurl = clean_param($fullurl, PARAM_URL
);
286 $url['type'] = 'url';
287 $url['filename'] = clean_param(format_string($urlrecord->name
), PARAM_FILE
);
288 $url['filepath'] = null;
289 $url['filesize'] = 0;
290 $url['fileurl'] = $fullurl;
291 $url['timecreated'] = null;
292 $url['timemodified'] = $urlrecord->timemodified
;
293 $url['sortorder'] = null;
294 $url['userid'] = null;
295 $url['author'] = null;
296 $url['license'] = null;
303 * Register the ability to handle drag and drop file uploads
304 * @return array containing details of the files / types the mod can handle
306 function url_dndupload_register() {
307 return array('types' => array(
308 array('identifier' => 'url', 'message' => get_string('createurl', 'url'))
313 * Handle a file that has been uploaded
314 * @param object $uploadinfo details of the file / content that has been uploaded
315 * @return int instance id of the newly created mod
317 function url_dndupload_handle($uploadinfo) {
318 // Gather all the required data.
319 $data = new stdClass();
320 $data->course
= $uploadinfo->course
->id
;
321 $data->name
= $uploadinfo->displayname
;
322 $data->intro
= '<p>'.$uploadinfo->displayname
.'</p>';
323 $data->introformat
= FORMAT_HTML
;
324 $data->externalurl
= clean_param($uploadinfo->content
, PARAM_URL
);
325 $data->timemodified
= time();
326 $data->coursemodule
= $uploadinfo->coursemodule
;
328 // Set the display options to the site defaults.
329 $config = get_config('url');
330 $data->display
= $config->display
;
331 $data->popupwidth
= $config->popupwidth
;
332 $data->popupheight
= $config->popupheight
;
333 $data->printintro
= $config->printintro
;
335 return url_add_instance($data, null);
339 * Mark the activity completed (if required) and trigger the course_module_viewed event.
341 * @param stdClass $url url object
342 * @param stdClass $course course object
343 * @param stdClass $cm course module object
344 * @param stdClass $context context object
347 function url_view($url, $course, $cm, $context) {
349 // Trigger course_module_viewed event.
351 'context' => $context,
352 'objectid' => $url->id
355 $event = \mod_url\event\course_module_viewed
::create($params);
356 $event->add_record_snapshot('course_modules', $cm);
357 $event->add_record_snapshot('course', $course);
358 $event->add_record_snapshot('url', $url);
362 $completion = new completion_info($course);
363 $completion->set_module_viewed($cm);
367 * Check if the module has any update that affects the current user since a given time.
369 * @param cm_info $cm course module data
370 * @param int $from the time to check updates from
371 * @param array $filter if we need to check only specific updates
372 * @return stdClass an object with the different type of areas indicating if they were updated or not
375 function url_check_updates_since(cm_info
$cm, $from, $filter = array()) {
376 $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
381 * This function receives a calendar event and returns the action associated with it, or null if there is none.
383 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
384 * is not displayed on the block.
386 * @param calendar_event $event
387 * @param \core_calendar\action_factory $factory
388 * @param int $userid ID override for calendar events
389 * @return \core_calendar\local\event\entities\action_interface|null
391 function mod_url_core_calendar_provide_event_action(calendar_event
$event,
392 \core_calendar\action_factory
$factory, $userid = 0) {
395 if (empty($userid)) {
399 $cm = get_fast_modinfo($event->courseid
, $userid)->instances
['url'][$event->instance
];
401 $completion = new \
completion_info($cm->get_course());
403 $completiondata = $completion->get_data($cm, false, $userid);
405 if ($completiondata->completionstate
!= COMPLETION_INCOMPLETE
) {
409 return $factory->create_instance(
411 new \
moodle_url('/mod/url/view.php', ['id' => $cm->id
]),