Merge branch 'MDL-27293-customlang-timeout_20_STABLE' of git://github.com/mudrd8mz...
[moodle.git] / mod / url / lib.php
blobf79913b991f1086d7c184c953f32c99ff87725f7
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
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
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
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;
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 of view style log actions
69 * @return array
71 function url_get_view_actions() {
72 return array('view', 'view all');
75 /**
76 * List of update style log actions
77 * @return array
79 function url_get_post_actions() {
80 return array('update', 'add');
83 /**
84 * Add url instance.
85 * @param object $data
86 * @param object $mform
87 * @return int new url instance id
89 function url_add_instance($data, $mform) {
90 global $DB;
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)) {
97 continue;
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);
121 return $data->id;
125 * Update url instance.
126 * @param object $data
127 * @param object $mform
128 * @return bool true
130 function url_update_instance($data, $mform) {
131 global $CFG, $DB;
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)) {
138 continue;
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);
164 return true;
168 * Delete url instance.
169 * @param int $id
170 * @return bool true
172 function url_delete_instance($id) {
173 global $DB;
175 if (!$url = $DB->get_record('url', array('id'=>$id))) {
176 return false;
179 // note: all context files are deleted automatically
181 $DB->delete_records('url', array('id'=>$url->id));
183 return true;
187 * Return use outline
188 * @param object $course
189 * @param object $user
190 * @param object $mod
191 * @param object $url
192 * @return object|null
194 function url_user_outline($course, $user, $mod, $url) {
195 global $DB;
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;
207 return $result;
209 return NULL;
213 * Return use complete
214 * @param object $course
215 * @param object $user
216 * @param object $mod
217 * @param object $url
219 function url_user_complete($course, $user, $mod, $url) {
220 global $CFG, $DB;
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);
232 } else {
233 print_string('neverseen', 'url');
238 * Returns the users with data in one url
240 * @param int $urlid
241 * @return bool false
243 function url_get_participants($urlid) {
244 return false;
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) {
258 global $CFG, $DB;
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')) {
262 return NULL;
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&amp;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&amp;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&amp;redirect=1";
287 $info->extra = "onclick=\"window.location.href ='$fullurl';return false;\"";
290 return $info;
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
297 * available
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
308 * you content.
310 $navigation->nodetype = navigation_node::NODETYPE_LEAF;