MDL-21695 adding help string
[moodle.git] / mod / url / lib.php
bloba308be0017dfb5277d9b719b4e45318ac8b3e181
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-url
22 * @copyright 2009 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 /**
27 * List of features supported in URL module
28 * @param string $feature FEATURE_xx constant for requested feature
29 * @return mixed True if module supports feature, false if not, null if doesn't know
31 function url_supports($feature) {
32 switch($feature) {
33 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
34 case FEATURE_GROUPS: return false;
35 case FEATURE_GROUPINGS: return false;
36 case FEATURE_GROUPMEMBERSONLY: return true;
37 case FEATURE_MOD_INTRO: return true;
38 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
39 case FEATURE_GRADE_HAS_GRADE: return false;
40 case FEATURE_GRADE_OUTCOMES: return false;
41 case FEATURE_BACKUP_MOODLE2: return true;
43 default: return null;
47 /**
48 * Returns all other caps used in module
49 * @return array
51 function url_get_extra_capabilities() {
52 return array('moodle/site:accessallgroups');
55 /**
56 * This function is used by the reset_course_userdata function in moodlelib.
57 * @param $data the data submitted from the reset course.
58 * @return array status array
60 function url_reset_userdata($data) {
61 return array();
64 /**
65 * List of view style log actions
66 * @return array
68 function url_get_view_actions() {
69 return array('view', 'view all');
72 /**
73 * List of update style log actions
74 * @return array
76 function url_get_post_actions() {
77 return array('update', 'add');
80 /**
81 * Add url instance.
82 * @param object $data
83 * @param object $mform
84 * @return int new url instance id
86 function url_add_instance($data, $mform) {
87 global $DB;
89 $parameters = array();
90 for ($i=0; $i < 100; $i++) {
91 $parameter = "parameter_$i";
92 $variable = "variable_$i";
93 if (empty($data->$parameter) or empty($data->$variable)) {
94 continue;
96 $parameters[$data->$parameter] = $data->$variable;
98 $data->parameters = serialize($parameters);
100 $displayoptions = array();
101 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
102 $displayoptions['popupwidth'] = $data->popupwidth;
103 $displayoptions['popupheight'] = $data->popupheight;
105 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
106 $displayoptions['printheading'] = (int)!empty($data->printheading);
107 $displayoptions['printintro'] = (int)!empty($data->printintro);
109 $data->displayoptions = serialize($displayoptions);
111 if (strpos($data->externalurl, '://') === false) {
112 $data->externalurl = 'http://'.$data->externalurl;
115 $data->timemodified = time();
116 $data->id = $DB->insert_record('url', $data);
118 return $data->id;
122 * Update url instance.
123 * @param object $data
124 * @param object $mform
125 * @return bool true
127 function url_update_instance($data, $mform) {
128 global $CFG, $DB;
130 $parameters = array();
131 for ($i=0; $i < 100; $i++) {
132 $parameter = "parameter_$i";
133 $variable = "variable_$i";
134 if (empty($data->$parameter) or empty($data->$variable)) {
135 continue;
137 $parameters[$data->$parameter] = $data->$variable;
139 $data->parameters = serialize($parameters);
141 $displayoptions = array();
142 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
143 $displayoptions['popupwidth'] = $data->popupwidth;
144 $displayoptions['popupheight'] = $data->popupheight;
146 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
147 $displayoptions['printheading'] = (int)!empty($data->printheading);
148 $displayoptions['printintro'] = (int)!empty($data->printintro);
150 $data->displayoptions = serialize($displayoptions);
152 if (strpos($data->externalurl, '://') === false) {
153 $data->externalurl = 'http://'.$data->externalurl;
156 $data->timemodified = time();
157 $data->id = $data->instance;
159 $DB->update_record('url', $data);
161 return true;
165 * Delete url instance.
166 * @param int $id
167 * @return bool true
169 function url_delete_instance($id) {
170 global $DB;
172 if (!$url = $DB->get_record('url', array('id'=>$id))) {
173 return false;
176 // note: all context files are deleted automatically
178 $DB->delete_records('url', array('id'=>$url->id));
180 return true;
184 * Return use outline
185 * @param object $course
186 * @param object $user
187 * @param object $mod
188 * @param object $url
189 * @return object|null
191 function url_user_outline($course, $user, $mod, $url) {
192 global $DB;
194 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'url',
195 'action'=>'view', 'info'=>$url->id), 'time ASC')) {
197 $numviews = count($logs);
198 $lastlog = array_pop($logs);
200 $result = new object();
201 $result->info = get_string('numviews', '', $numviews);
202 $result->time = $lastlog->time;
204 return $result;
206 return NULL;
210 * Return use complete
211 * @param object $course
212 * @param object $user
213 * @param object $mod
214 * @param object $url
216 function url_user_complete($course, $user, $mod, $url) {
217 global $CFG, $DB;
219 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'url',
220 'action'=>'view', 'info'=>$url->id), 'time ASC')) {
221 $numviews = count($logs);
222 $lastlog = array_pop($logs);
224 $strmostrecently = get_string('mostrecently');
225 $strnumviews = get_string('numviews', '', $numviews);
227 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
229 } else {
230 print_string('neverseen', 'url');
235 * Returns the users with data in one url
237 * @param int $urlid
238 * @return bool false
240 function url_get_participants($urlid) {
241 return false;
245 * Given a course_module object, this function returns any
246 * "extra" information that may be needed when printing
247 * this activity in a course listing.
249 * See {@link get_array_of_activities()} in course/lib.php
251 * @param object $coursemodule
252 * @return object info
254 function url_get_coursemodule_info($coursemodule) {
255 global $CFG, $DB;
256 require_once("$CFG->dirroot/mod/url/locallib.php");
258 if (!$url = $DB->get_record('url', array('id'=>$coursemodule->instance), 'id, name, display, displayoptions, externalurl, parameters')) {
259 return NULL;
262 $info = new object();
263 $info->name = $url->name;
265 //note: there should be a way to differentiate links from normal resources
266 $info->icon = url_guess_icon($url->externalurl);
268 $display = url_get_final_display_type($url);
270 if ($display == RESOURCELIB_DISPLAY_POPUP) {
271 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&amp;redirect=1";
272 $options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
273 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
274 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
275 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
276 $info->extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
278 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
279 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&amp;redirect=1";
280 $info->extra = "onclick=\"window.open('$fullurl'); return false;\"";
282 } else if ($display == RESOURCELIB_DISPLAY_OPEN) {
283 $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&amp;redirect=1";
284 $info->extra = "onclick=\"window.location.href ='$fullurl';return false;\"";
287 return $info;
291 * This function extends the global navigaiton for the site.
292 * It is important to note that you should not rely on PAGE objects within this
293 * body of code as there is no guarantee that during an AJAX request they are
294 * available
296 * @param navigation_node $navigation The url node within the global navigation
297 * @param stdClass $course The course object returned from the DB
298 * @param stdClass $module The module object returned from the DB
299 * @param stdClass $cm The course module isntance returned from the DB
301 function url_extend_navigation($navigation, $course, $module, $cm) {
303 * This is currently just a stub so that it can be easily expanded upon.
304 * When expanding just remove this comment and the line below and then add
305 * you content.
307 $navigation->nodetype = navigation_node::NODETYPE_LEAF;