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
23 * @copyright 2009 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die;
30 * List of features supported in URL module
31 * @param string $feature FEATURE_xx constant for requested feature
32 * @return mixed True if module supports feature, false if not, null if doesn't know
34 function url_supports($feature) {
36 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
37 case FEATURE_GROUPS: return false;
38 case FEATURE_GROUPINGS: return false;
39 case FEATURE_GROUPMEMBERSONLY: return true;
40 case FEATURE_MOD_INTRO: return true;
41 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
42 case FEATURE_GRADE_HAS_GRADE: return false;
43 case FEATURE_GRADE_OUTCOMES: return false;
44 case FEATURE_BACKUP_MOODLE2: return true;
45 case FEATURE_SHOW_DESCRIPTION: return true;
52 * Returns all other caps used in module
55 function url_get_extra_capabilities() {
56 return array('moodle/site:accessallgroups');
60 * This function is used by the reset_course_userdata function in moodlelib.
61 * @param $data the data submitted from the reset course.
62 * @return array status array
64 function url_reset_userdata($data) {
69 * List of view style log actions
72 function url_get_view_actions() {
73 return array('view', 'view all');
77 * List of update style log actions
80 function url_get_post_actions() {
81 return array('update', 'add');
87 * @param object $mform
88 * @return int new url instance id
90 function url_add_instance($data, $mform) {
93 require_once($CFG->dirroot.'/mod/url/locallib.php');
95 $parameters = array();
96 for ($i=0; $i < 100; $i++) {
97 $parameter = "parameter_$i";
98 $variable = "variable_$i";
99 if (empty($data->$parameter) or empty($data->$variable)) {
102 $parameters[$data->$parameter] = $data->$variable;
104 $data->parameters = serialize($parameters);
106 $displayoptions = array();
107 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
108 $displayoptions['popupwidth'] = $data->popupwidth;
109 $displayoptions['popupheight'] = $data->popupheight;
111 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
112 $displayoptions['printheading'] = (int)!empty($data->printheading);
113 $displayoptions['printintro'] = (int)!empty($data->printintro);
115 $data->displayoptions = serialize($displayoptions);
117 $data->externalurl = url_fix_submitted_url($data->externalurl);
119 $data->timemodified = time();
120 $data->id = $DB->insert_record('url', $data);
126 * Update url instance.
127 * @param object $data
128 * @param object $mform
131 function url_update_instance($data, $mform) {
134 require_once($CFG->dirroot.'/mod/url/locallib.php');
136 $parameters = array();
137 for ($i=0; $i < 100; $i++) {
138 $parameter = "parameter_$i";
139 $variable = "variable_$i";
140 if (empty($data->$parameter) or empty($data->$variable)) {
143 $parameters[$data->$parameter] = $data->$variable;
145 $data->parameters = serialize($parameters);
147 $displayoptions = array();
148 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
149 $displayoptions['popupwidth'] = $data->popupwidth;
150 $displayoptions['popupheight'] = $data->popupheight;
152 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
153 $displayoptions['printheading'] = (int)!empty($data->printheading);
154 $displayoptions['printintro'] = (int)!empty($data->printintro);
156 $data->displayoptions = serialize($displayoptions);
158 $data->externalurl = url_fix_submitted_url($data->externalurl);
160 $data->timemodified = time();
161 $data->id = $data->instance;
163 $DB->update_record('url', $data);
169 * Delete url instance.
173 function url_delete_instance($id) {
176 if (!$url = $DB->get_record('url', array('id'=>$id))) {
180 // note: all context files are deleted automatically
182 $DB->delete_records('url', array('id'=>$url->id));
189 * @param object $course
190 * @param object $user
193 * @return object|null
195 function url_user_outline($course, $user, $mod, $url) {
198 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'url',
199 'action'=>'view', 'info'=>$url->id), 'time ASC')) {
201 $numviews = count($logs);
202 $lastlog = array_pop($logs);
204 $result = new stdClass();
205 $result->info = get_string('numviews', '', $numviews);
206 $result->time = $lastlog->time;
214 * Return use complete
215 * @param object $course
216 * @param object $user
220 function url_user_complete($course, $user, $mod, $url) {
223 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'url',
224 'action'=>'view', 'info'=>$url->id), 'time ASC')) {
225 $numviews = count($logs);
226 $lastlog = array_pop($logs);
228 $strmostrecently = get_string('mostrecently');
229 $strnumviews = get_string('numviews', '', $numviews);
231 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
234 print_string('neverseen', 'url');
239 * Returns the users with data in one url
241 * @todo: deprecated - to be deleted in 2.2
246 function url_get_participants($urlid) {
251 * Given a course_module object, this function returns any
252 * "extra" information that may be needed when printing
253 * this activity in a course listing.
255 * See {@link get_array_of_activities()} in course/lib.php
257 * @param object $coursemodule
258 * @return object info
260 function url_get_coursemodule_info($coursemodule) {
262 require_once("$CFG->dirroot/mod/url/locallib.php");
264 if (!$url = $DB->get_record('url', array('id'=>$coursemodule->instance),
265 'id, name, display, displayoptions, externalurl, parameters, intro, introformat')) {
269 $info = new cached_cm_info();
270 $info->name = $url->name;
272 //note: there should be a way to differentiate links from normal resources
273 $info->icon = url_guess_icon($url->externalurl);
275 $display = url_get_final_display_type($url);
277 if ($display == RESOURCELIB_DISPLAY_POPUP) {
278 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&redirect=1";
279 $options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
280 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
281 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
282 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
283 $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
285 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
286 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&redirect=1";
287 $info->onclick = "window.open('$fullurl'); return false;";
289 } else if ($display == RESOURCELIB_DISPLAY_OPEN) {
290 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&redirect=1";
291 $info->onclick = "window.location.href ='$fullurl';return false;";
294 if ($coursemodule->showdescription) {
295 // Convert intro to html. Do not filter cached version, filters run at display time.
296 $info->content = format_module_intro('url', $url, $coursemodule->id, false);
303 * This function extends the global navigation for the site.
304 * It is important to note that you should not rely on PAGE objects within this
305 * body of code as there is no guarantee that during an AJAX request they are
308 * @param navigation_node $navigation The url node within the global navigation
309 * @param stdClass $course The course object returned from the DB
310 * @param stdClass $module The module object returned from the DB
311 * @param stdClass $cm The course module instance returned from the DB
313 function url_extend_navigation($navigation, $course, $module, $cm) {
315 * This is currently just a stub so that it can be easily expanded upon.
316 * When expanding just remove this comment and the line below and then add
319 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
323 * Return a list of page types
324 * @param string $pagetype current page type
325 * @param stdClass $parentcontext Block's parent context
326 * @param stdClass $currentcontext Current context of block
328 function url_page_type_list($pagetype, $parentcontext, $currentcontext) {
329 $module_pagetype = array('mod-url-*'=>get_string('page-mod-url-x', 'url'));
330 return $module_pagetype;