PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/url/lib.php

https://github.com/tcubansk/moodle
PHP | 345 lines | 181 code | 56 blank | 108 comment | 23 complexity | c31e820e7331433bccd6b0f26a26c0e7 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Mandatory public API of url module
  18. *
  19. * @package mod
  20. * @subpackage url
  21. * @copyright 2009 Petr Skoda {@link http://skodak.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die;
  25. /**
  26. * List of features supported in URL module
  27. * @param string $feature FEATURE_xx constant for requested feature
  28. * @return mixed True if module supports feature, false if not, null if doesn't know
  29. */
  30. function url_supports($feature) {
  31. switch($feature) {
  32. case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
  33. case FEATURE_GROUPS: return false;
  34. case FEATURE_GROUPINGS: return false;
  35. case FEATURE_GROUPMEMBERSONLY: return true;
  36. case FEATURE_MOD_INTRO: return true;
  37. case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
  38. case FEATURE_GRADE_HAS_GRADE: return false;
  39. case FEATURE_GRADE_OUTCOMES: return false;
  40. case FEATURE_BACKUP_MOODLE2: return true;
  41. case FEATURE_SHOW_DESCRIPTION: return true;
  42. default: return null;
  43. }
  44. }
  45. /**
  46. * Returns all other caps used in module
  47. * @return array
  48. */
  49. function url_get_extra_capabilities() {
  50. return array('moodle/site:accessallgroups');
  51. }
  52. /**
  53. * This function is used by the reset_course_userdata function in moodlelib.
  54. * @param $data the data submitted from the reset course.
  55. * @return array status array
  56. */
  57. function url_reset_userdata($data) {
  58. return array();
  59. }
  60. /**
  61. * List of view style log actions
  62. * @return array
  63. */
  64. function url_get_view_actions() {
  65. return array('view', 'view all');
  66. }
  67. /**
  68. * List of update style log actions
  69. * @return array
  70. */
  71. function url_get_post_actions() {
  72. return array('update', 'add');
  73. }
  74. /**
  75. * Add url instance.
  76. * @param object $data
  77. * @param object $mform
  78. * @return int new url instance id
  79. */
  80. function url_add_instance($data, $mform) {
  81. global $CFG, $DB;
  82. require_once($CFG->dirroot.'/mod/url/locallib.php');
  83. $parameters = array();
  84. for ($i=0; $i < 100; $i++) {
  85. $parameter = "parameter_$i";
  86. $variable = "variable_$i";
  87. if (empty($data->$parameter) or empty($data->$variable)) {
  88. continue;
  89. }
  90. $parameters[$data->$parameter] = $data->$variable;
  91. }
  92. $data->parameters = serialize($parameters);
  93. $displayoptions = array();
  94. if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
  95. $displayoptions['popupwidth'] = $data->popupwidth;
  96. $displayoptions['popupheight'] = $data->popupheight;
  97. }
  98. if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
  99. $displayoptions['printheading'] = (int)!empty($data->printheading);
  100. $displayoptions['printintro'] = (int)!empty($data->printintro);
  101. }
  102. $data->displayoptions = serialize($displayoptions);
  103. $data->externalurl = url_fix_submitted_url($data->externalurl);
  104. $data->timemodified = time();
  105. $data->id = $DB->insert_record('url', $data);
  106. return $data->id;
  107. }
  108. /**
  109. * Update url instance.
  110. * @param object $data
  111. * @param object $mform
  112. * @return bool true
  113. */
  114. function url_update_instance($data, $mform) {
  115. global $CFG, $DB;
  116. require_once($CFG->dirroot.'/mod/url/locallib.php');
  117. $parameters = array();
  118. for ($i=0; $i < 100; $i++) {
  119. $parameter = "parameter_$i";
  120. $variable = "variable_$i";
  121. if (empty($data->$parameter) or empty($data->$variable)) {
  122. continue;
  123. }
  124. $parameters[$data->$parameter] = $data->$variable;
  125. }
  126. $data->parameters = serialize($parameters);
  127. $displayoptions = array();
  128. if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
  129. $displayoptions['popupwidth'] = $data->popupwidth;
  130. $displayoptions['popupheight'] = $data->popupheight;
  131. }
  132. if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
  133. $displayoptions['printheading'] = (int)!empty($data->printheading);
  134. $displayoptions['printintro'] = (int)!empty($data->printintro);
  135. }
  136. $data->displayoptions = serialize($displayoptions);
  137. $data->externalurl = url_fix_submitted_url($data->externalurl);
  138. $data->timemodified = time();
  139. $data->id = $data->instance;
  140. $DB->update_record('url', $data);
  141. return true;
  142. }
  143. /**
  144. * Delete url instance.
  145. * @param int $id
  146. * @return bool true
  147. */
  148. function url_delete_instance($id) {
  149. global $DB;
  150. if (!$url = $DB->get_record('url', array('id'=>$id))) {
  151. return false;
  152. }
  153. // note: all context files are deleted automatically
  154. $DB->delete_records('url', array('id'=>$url->id));
  155. return true;
  156. }
  157. /**
  158. * Return use outline
  159. * @param object $course
  160. * @param object $user
  161. * @param object $mod
  162. * @param object $url
  163. * @return object|null
  164. */
  165. function url_user_outline($course, $user, $mod, $url) {
  166. global $DB;
  167. if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'url',
  168. 'action'=>'view', 'info'=>$url->id), 'time ASC')) {
  169. $numviews = count($logs);
  170. $lastlog = array_pop($logs);
  171. $result = new stdClass();
  172. $result->info = get_string('numviews', '', $numviews);
  173. $result->time = $lastlog->time;
  174. return $result;
  175. }
  176. return NULL;
  177. }
  178. /**
  179. * Return use complete
  180. * @param object $course
  181. * @param object $user
  182. * @param object $mod
  183. * @param object $url
  184. */
  185. function url_user_complete($course, $user, $mod, $url) {
  186. global $CFG, $DB;
  187. if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'url',
  188. 'action'=>'view', 'info'=>$url->id), 'time ASC')) {
  189. $numviews = count($logs);
  190. $lastlog = array_pop($logs);
  191. $strmostrecently = get_string('mostrecently');
  192. $strnumviews = get_string('numviews', '', $numviews);
  193. echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
  194. } else {
  195. print_string('neverseen', 'url');
  196. }
  197. }
  198. /**
  199. * Returns the users with data in one url
  200. *
  201. * @todo: deprecated - to be deleted in 2.2
  202. *
  203. * @param int $urlid
  204. * @return bool false
  205. */
  206. function url_get_participants($urlid) {
  207. return false;
  208. }
  209. /**
  210. * Given a course_module object, this function returns any
  211. * "extra" information that may be needed when printing
  212. * this activity in a course listing.
  213. *
  214. * See {@link get_array_of_activities()} in course/lib.php
  215. *
  216. * @param object $coursemodule
  217. * @return object info
  218. */
  219. function url_get_coursemodule_info($coursemodule) {
  220. global $CFG, $DB;
  221. require_once("$CFG->dirroot/mod/url/locallib.php");
  222. if (!$url = $DB->get_record('url', array('id'=>$coursemodule->instance),
  223. 'id, name, display, displayoptions, externalurl, parameters, intro, introformat')) {
  224. return NULL;
  225. }
  226. $info = new cached_cm_info();
  227. $info->name = $url->name;
  228. //note: there should be a way to differentiate links from normal resources
  229. $info->icon = url_guess_icon($url->externalurl);
  230. $display = url_get_final_display_type($url);
  231. if ($display == RESOURCELIB_DISPLAY_POPUP) {
  232. $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&amp;redirect=1";
  233. $options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
  234. $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
  235. $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
  236. $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
  237. $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
  238. } else if ($display == RESOURCELIB_DISPLAY_NEW) {
  239. $fullurl = "$CFG->wwwroot/mod/url/view.php?id=$coursemodule->id&amp;redirect=1";
  240. $info->onclick = "window.open('$fullurl'); return false;";
  241. }
  242. if ($coursemodule->showdescription) {
  243. // Convert intro to html. Do not filter cached version, filters run at display time.
  244. $info->content = format_module_intro('url', $url, $coursemodule->id, false);
  245. }
  246. return $info;
  247. }
  248. /**
  249. * Return a list of page types
  250. * @param string $pagetype current page type
  251. * @param stdClass $parentcontext Block's parent context
  252. * @param stdClass $currentcontext Current context of block
  253. */
  254. function url_page_type_list($pagetype, $parentcontext, $currentcontext) {
  255. $module_pagetype = array('mod-url-*'=>get_string('page-mod-url-x', 'url'));
  256. return $module_pagetype;
  257. }
  258. /**
  259. * Export URL resource contents
  260. *
  261. * @return array of file content
  262. */
  263. function url_export_contents($cm, $baseurl) {
  264. global $CFG, $DB;
  265. require_once("$CFG->dirroot/mod/url/locallib.php");
  266. $contents = array();
  267. $context = get_context_instance(CONTEXT_MODULE, $cm->id);
  268. $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  269. $url = $DB->get_record('url', array('id'=>$cm->instance), '*', MUST_EXIST);
  270. $fullurl = str_replace('&amp;', '&', url_get_full_url($url, $cm, $course));
  271. $isurl = clean_param($fullurl, PARAM_URL);
  272. if (empty($isurl)) {
  273. return null;
  274. }
  275. $url = array();
  276. $url['type'] = 'url';
  277. $url['filename'] = $url->name;
  278. $url['filepath'] = null;
  279. $url['filesize'] = 0;
  280. $url['fileurl'] = $fullurl;
  281. $url['timecreated'] = null;
  282. $url['timemodified'] = $url->timemodified;
  283. $url['sortorder'] = null;
  284. $url['userid'] = null;
  285. $url['author'] = null;
  286. $url['license'] = null;
  287. $contents[] = $url;
  288. return $contents;
  289. }