MDL-30005 fix general URI support in URL module
[moodle.git] / mod / url / lib.php
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.
14 //
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
20  *
21  * @package    mod
22  * @subpackage url
23  * @copyright  2009 Petr Skoda  {@link http://skodak.org}
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die;
29 /**
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
33  */
34 function url_supports($feature) {
35     switch($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;
47         default: return null;
48     }
49 }
51 /**
52  * Returns all other caps used in module
53  * @return array
54  */
55 function url_get_extra_capabilities() {
56     return array('moodle/site:accessallgroups');
57 }
59 /**
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
63  */
64 function url_reset_userdata($data) {
65     return array();
66 }
68 /**
69  * List of view style log actions
70  * @return array
71  */
72 function url_get_view_actions() {
73     return array('view', 'view all');
74 }
76 /**
77  * List of update style log actions
78  * @return array
79  */
80 function url_get_post_actions() {
81     return array('update', 'add');
82 }
84 /**
85  * Add url instance.
86  * @param object $data
87  * @param object $mform
88  * @return int new url instance id
89  */
90 function url_add_instance($data, $mform) {
91     global $CFG, $DB;
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)) {
100             continue;
101         }
102         $parameters[$data->$parameter] = $data->$variable;
103     }
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;
110     }
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);
114     }
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);
122     return $data->id;
125 /**
126  * Update url instance.
127  * @param object $data
128  * @param object $mform
129  * @return bool true
130  */
131 function url_update_instance($data, $mform) {
132     global $CFG, $DB;
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)) {
141             continue;
142         }
143         $parameters[$data->$parameter] = $data->$variable;
144     }
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;
151     }
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);
155     }
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);
165     return true;
168 /**
169  * Delete url instance.
170  * @param int $id
171  * @return bool true
172  */
173 function url_delete_instance($id) {
174     global $DB;
176     if (!$url = $DB->get_record('url', array('id'=>$id))) {
177         return false;
178     }
180     // note: all context files are deleted automatically
182     $DB->delete_records('url', array('id'=>$url->id));
184     return true;
187 /**
188  * Return use outline
189  * @param object $course
190  * @param object $user
191  * @param object $mod
192  * @param object $url
193  * @return object|null
194  */
195 function url_user_outline($course, $user, $mod, $url) {
196     global $DB;
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;
208         return $result;
209     }
210     return NULL;
213 /**
214  * Return use complete
215  * @param object $course
216  * @param object $user
217  * @param object $mod
218  * @param object $url
219  */
220 function url_user_complete($course, $user, $mod, $url) {
221     global $CFG, $DB;
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);
233     } else {
234         print_string('neverseen', 'url');
235     }
238 /**
239  * Returns the users with data in one url
240  *
241  * @todo: deprecated - to be deleted in 2.2
242  *
243  * @param int $urlid
244  * @return bool false
245  */
246 function url_get_participants($urlid) {
247     return false;
250 /**
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.
254  *
255  * See {@link get_array_of_activities()} in course/lib.php
256  *
257  * @param object $coursemodule
258  * @return object info
259  */
260 function url_get_coursemodule_info($coursemodule) {
261     global $CFG, $DB;
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')) {
266         return NULL;
267     }
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&amp;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&amp;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&amp;redirect=1";
291         $info->onclick = "window.location.href ='$fullurl';return false;";
292     }
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);
297     }
299     return $info;
302 /**
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
306  * available
307  *
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
312  */
313 function url_extend_navigation($navigation, $course, $module, $cm) {
314     /**
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
317      * you content.
318      */
319     $navigation->nodetype = navigation_node::NODETYPE_LEAF;
322 /**
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
327  */
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;