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;
51 * Returns all other caps used in module
54 function url_get_extra_capabilities() {
55 return array('moodle/site:accessallgroups');
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) {
68 * List of view style log actions
71 function url_get_view_actions() {
72 return array('view', 'view all');
76 * List of update style log actions
79 function url_get_post_actions() {
80 return array('update', 'add');
86 * @param object $mform
87 * @return int new url instance id
89 function url_add_instance($data, $mform) {
92 $parameters = array();
93 for ($i=0; $i < 100; $i++
) {
94 $parameter = "parameter_$i";
95 $variable = "variable_$i";
96 if (empty($data->$parameter) or empty($data->$variable)) {
99 $parameters[$data->$parameter] = $data->$variable;
101 $data->parameters
= serialize($parameters);
103 $displayoptions = array();
104 if ($data->display
== RESOURCELIB_DISPLAY_POPUP
) {
105 $displayoptions['popupwidth'] = $data->popupwidth
;
106 $displayoptions['popupheight'] = $data->popupheight
;
108 if (in_array($data->display
, array(RESOURCELIB_DISPLAY_AUTO
, RESOURCELIB_DISPLAY_EMBED
, RESOURCELIB_DISPLAY_FRAME
))) {
109 $displayoptions['printheading'] = (int)!empty($data->printheading
);
110 $displayoptions['printintro'] = (int)!empty($data->printintro
);
112 $data->displayoptions
= serialize($displayoptions);
114 if (!empty($data->externalurl
) && (strpos($data->externalurl
, '://') === false) && (strpos($data->externalurl
, '/', 0) === false)) {
115 $data->externalurl
= 'http://'.$data->externalurl
;
118 $data->timemodified
= time();
119 $data->id
= $DB->insert_record('url', $data);
125 * Update url instance.
126 * @param object $data
127 * @param object $mform
130 function url_update_instance($data, $mform) {
133 $parameters = array();
134 for ($i=0; $i < 100; $i++
) {
135 $parameter = "parameter_$i";
136 $variable = "variable_$i";
137 if (empty($data->$parameter) or empty($data->$variable)) {
140 $parameters[$data->$parameter] = $data->$variable;
142 $data->parameters
= serialize($parameters);
144 $displayoptions = array();
145 if ($data->display
== RESOURCELIB_DISPLAY_POPUP
) {
146 $displayoptions['popupwidth'] = $data->popupwidth
;
147 $displayoptions['popupheight'] = $data->popupheight
;
149 if (in_array($data->display
, array(RESOURCELIB_DISPLAY_AUTO
, RESOURCELIB_DISPLAY_EMBED
, RESOURCELIB_DISPLAY_FRAME
))) {
150 $displayoptions['printheading'] = (int)!empty($data->printheading
);
151 $displayoptions['printintro'] = (int)!empty($data->printintro
);
153 $data->displayoptions
= serialize($displayoptions);
155 if (!empty($data->externalurl
) && (strpos($data->externalurl
, '://') === false) && (strpos($data->externalurl
, '/', 0) === false)) {
156 $data->externalurl
= 'http://'.$data->externalurl
;
159 $data->timemodified
= time();
160 $data->id
= $data->instance
;
162 $DB->update_record('url', $data);
168 * Delete url instance.
172 function url_delete_instance($id) {
175 if (!$url = $DB->get_record('url', array('id'=>$id))) {
179 // note: all context files are deleted automatically
181 $DB->delete_records('url', array('id'=>$url->id
));
188 * @param object $course
189 * @param object $user
192 * @return object|null
194 function url_user_outline($course, $user, $mod, $url) {
197 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'url',
198 'action'=>'view', 'info'=>$url->id
), 'time ASC')) {
200 $numviews = count($logs);
201 $lastlog = array_pop($logs);
203 $result = new stdClass();
204 $result->info
= get_string('numviews', '', $numviews);
205 $result->time
= $lastlog->time
;
213 * Return use complete
214 * @param object $course
215 * @param object $user
219 function url_user_complete($course, $user, $mod, $url) {
222 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'url',
223 'action'=>'view', 'info'=>$url->id
), 'time ASC')) {
224 $numviews = count($logs);
225 $lastlog = array_pop($logs);
227 $strmostrecently = get_string('mostrecently');
228 $strnumviews = get_string('numviews', '', $numviews);
230 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time
);
233 print_string('neverseen', 'url');
238 * Returns the users with data in one url
243 function url_get_participants($urlid) {
248 * Given a course_module object, this function returns any
249 * "extra" information that may be needed when printing
250 * this activity in a course listing.
252 * See {@link get_array_of_activities()} in course/lib.php
254 * @param object $coursemodule
255 * @return object info
257 function url_get_coursemodule_info($coursemodule) {
259 require_once("$CFG->dirroot/mod/url/locallib.php");
261 if (!$url = $DB->get_record('url', array('id'=>$coursemodule->instance
), 'id, name, display, displayoptions, externalurl, parameters')) {
265 $info = new stdClass();
266 $info->name
= $url->name
;
268 //note: there should be a way to differentiate links from normal resources
269 $info->icon
= url_guess_icon($url->externalurl
);
271 $display = url_get_final_display_type($url);
273 if ($display == RESOURCELIB_DISPLAY_POPUP
) {
274 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&redirect=1";
275 $options = empty($url->displayoptions
) ?
array() : unserialize($url->displayoptions
);
276 $width = empty($options['popupwidth']) ?
620 : $options['popupwidth'];
277 $height = empty($options['popupheight']) ?
450 : $options['popupheight'];
278 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
279 $info->extra
= "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
281 } else if ($display == RESOURCELIB_DISPLAY_NEW
) {
282 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&redirect=1";
283 $info->extra
= "onclick=\"window.open('$fullurl'); return false;\"";
285 } else if ($display == RESOURCELIB_DISPLAY_OPEN
) {
286 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&redirect=1";
287 $info->extra
= "onclick=\"window.location.href ='$fullurl';return false;\"";
294 * This function extends the global navigation for the site.
295 * It is important to note that you should not rely on PAGE objects within this
296 * body of code as there is no guarantee that during an AJAX request they are
299 * @param navigation_node $navigation The url node within the global navigation
300 * @param stdClass $course The course object returned from the DB
301 * @param stdClass $module The module object returned from the DB
302 * @param stdClass $cm The course module instance returned from the DB
304 function url_extend_navigation($navigation, $course, $module, $cm) {
306 * This is currently just a stub so that it can be easily expanded upon.
307 * When expanding just remove this comment and the line below and then add
310 $navigation->nodetype
= navigation_node
::NODETYPE_LEAF
;