PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/label/lib.php

https://github.com/cmiic/moodle
PHP | 340 lines | 178 code | 39 blank | 123 comment | 30 complexity | 05a35784ecc9c86054d411a96c26168d 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. * Library of functions and constants for module label
  18. *
  19. * @package mod_label
  20. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die;
  24. /** LABEL_MAX_NAME_LENGTH = 50 */
  25. define("LABEL_MAX_NAME_LENGTH", 50);
  26. /**
  27. * @uses LABEL_MAX_NAME_LENGTH
  28. * @param object $label
  29. * @return string
  30. */
  31. function get_label_name($label) {
  32. $name = strip_tags(format_string($label->intro,true));
  33. if (core_text::strlen($name) > LABEL_MAX_NAME_LENGTH) {
  34. $name = core_text::substr($name, 0, LABEL_MAX_NAME_LENGTH)."...";
  35. }
  36. if (empty($name)) {
  37. // arbitrary name
  38. $name = get_string('modulename','label');
  39. }
  40. return $name;
  41. }
  42. /**
  43. * Given an object containing all the necessary data,
  44. * (defined by the form in mod_form.php) this function
  45. * will create a new instance and return the id number
  46. * of the new instance.
  47. *
  48. * @global object
  49. * @param object $label
  50. * @return bool|int
  51. */
  52. function label_add_instance($label) {
  53. global $DB;
  54. $label->name = get_label_name($label);
  55. $label->timemodified = time();
  56. return $DB->insert_record("label", $label);
  57. }
  58. /**
  59. * Given an object containing all the necessary data,
  60. * (defined by the form in mod_form.php) this function
  61. * will update an existing instance with new data.
  62. *
  63. * @global object
  64. * @param object $label
  65. * @return bool
  66. */
  67. function label_update_instance($label) {
  68. global $DB;
  69. $label->name = get_label_name($label);
  70. $label->timemodified = time();
  71. $label->id = $label->instance;
  72. return $DB->update_record("label", $label);
  73. }
  74. /**
  75. * Given an ID of an instance of this module,
  76. * this function will permanently delete the instance
  77. * and any data that depends on it.
  78. *
  79. * @global object
  80. * @param int $id
  81. * @return bool
  82. */
  83. function label_delete_instance($id) {
  84. global $DB;
  85. if (! $label = $DB->get_record("label", array("id"=>$id))) {
  86. return false;
  87. }
  88. $result = true;
  89. if (! $DB->delete_records("label", array("id"=>$label->id))) {
  90. $result = false;
  91. }
  92. return $result;
  93. }
  94. /**
  95. * Given a course_module object, this function returns any
  96. * "extra" information that may be needed when printing
  97. * this activity in a course listing.
  98. * See get_array_of_activities() in course/lib.php
  99. *
  100. * @global object
  101. * @param object $coursemodule
  102. * @return cached_cm_info|null
  103. */
  104. function label_get_coursemodule_info($coursemodule) {
  105. global $DB;
  106. if ($label = $DB->get_record('label', array('id'=>$coursemodule->instance), 'id, name, intro, introformat')) {
  107. if (empty($label->name)) {
  108. // label name missing, fix it
  109. $label->name = "label{$label->id}";
  110. $DB->set_field('label', 'name', $label->name, array('id'=>$label->id));
  111. }
  112. $info = new cached_cm_info();
  113. // no filtering hre because this info is cached and filtered later
  114. $info->content = format_module_intro('label', $label, $coursemodule->id, false);
  115. $info->name = $label->name;
  116. return $info;
  117. } else {
  118. return null;
  119. }
  120. }
  121. /**
  122. * This function is used by the reset_course_userdata function in moodlelib.
  123. *
  124. * @param object $data the data submitted from the reset course.
  125. * @return array status array
  126. */
  127. function label_reset_userdata($data) {
  128. return array();
  129. }
  130. /**
  131. * Returns all other caps used in module
  132. *
  133. * @return array
  134. */
  135. function label_get_extra_capabilities() {
  136. return array('moodle/site:accessallgroups');
  137. }
  138. /**
  139. * @uses FEATURE_IDNUMBER
  140. * @uses FEATURE_GROUPS
  141. * @uses FEATURE_GROUPINGS
  142. * @uses FEATURE_MOD_INTRO
  143. * @uses FEATURE_COMPLETION_TRACKS_VIEWS
  144. * @uses FEATURE_GRADE_HAS_GRADE
  145. * @uses FEATURE_GRADE_OUTCOMES
  146. * @param string $feature FEATURE_xx constant for requested feature
  147. * @return bool|null True if module supports feature, false if not, null if doesn't know
  148. */
  149. function label_supports($feature) {
  150. switch($feature) {
  151. case FEATURE_IDNUMBER: return false;
  152. case FEATURE_GROUPS: return false;
  153. case FEATURE_GROUPINGS: return false;
  154. case FEATURE_MOD_INTRO: return true;
  155. case FEATURE_COMPLETION_TRACKS_VIEWS: return false;
  156. case FEATURE_GRADE_HAS_GRADE: return false;
  157. case FEATURE_GRADE_OUTCOMES: return false;
  158. case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
  159. case FEATURE_BACKUP_MOODLE2: return true;
  160. case FEATURE_NO_VIEW_LINK: return true;
  161. default: return null;
  162. }
  163. }
  164. /**
  165. * Register the ability to handle drag and drop file uploads
  166. * @return array containing details of the files / types the mod can handle
  167. */
  168. function label_dndupload_register() {
  169. $strdnd = get_string('dnduploadlabel', 'mod_label');
  170. if (get_config('label', 'dndmedia')) {
  171. $mediaextensions = file_get_typegroup('extension', 'web_image');
  172. $files = array();
  173. foreach ($mediaextensions as $extn) {
  174. $extn = trim($extn, '.');
  175. $files[] = array('extension' => $extn, 'message' => $strdnd);
  176. }
  177. $ret = array('files' => $files);
  178. } else {
  179. $ret = array();
  180. }
  181. $strdndtext = get_string('dnduploadlabeltext', 'mod_label');
  182. return array_merge($ret, array('types' => array(
  183. array('identifier' => 'text/html', 'message' => $strdndtext, 'noname' => true),
  184. array('identifier' => 'text', 'message' => $strdndtext, 'noname' => true)
  185. )));
  186. }
  187. /**
  188. * Handle a file that has been uploaded
  189. * @param object $uploadinfo details of the file / content that has been uploaded
  190. * @return int instance id of the newly created mod
  191. */
  192. function label_dndupload_handle($uploadinfo) {
  193. global $USER;
  194. // Gather the required info.
  195. $data = new stdClass();
  196. $data->course = $uploadinfo->course->id;
  197. $data->name = $uploadinfo->displayname;
  198. $data->intro = '';
  199. $data->introformat = FORMAT_HTML;
  200. $data->coursemodule = $uploadinfo->coursemodule;
  201. // Extract the first (and only) file from the file area and add it to the label as an img tag.
  202. if (!empty($uploadinfo->draftitemid)) {
  203. $fs = get_file_storage();
  204. $draftcontext = context_user::instance($USER->id);
  205. $context = context_module::instance($uploadinfo->coursemodule);
  206. $files = $fs->get_area_files($draftcontext->id, 'user', 'draft', $uploadinfo->draftitemid, '', false);
  207. if ($file = reset($files)) {
  208. if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) {
  209. // It is an image - resize it, if too big, then insert the img tag.
  210. $config = get_config('label');
  211. $data->intro = label_generate_resized_image($file, $config->dndresizewidth, $config->dndresizeheight);
  212. } else {
  213. // We aren't supposed to be supporting non-image types here, but fallback to adding a link, just in case.
  214. $url = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
  215. $data->intro = html_writer::link($url, $file->get_filename());
  216. }
  217. $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0,
  218. null, $data->intro);
  219. }
  220. } else if (!empty($uploadinfo->content)) {
  221. $data->intro = $uploadinfo->content;
  222. if ($uploadinfo->type != 'text/html') {
  223. $data->introformat = FORMAT_PLAIN;
  224. }
  225. }
  226. return label_add_instance($data, null);
  227. }
  228. /**
  229. * Resize the image, if required, then generate an img tag and, if required, a link to the full-size image
  230. * @param stored_file $file the image file to process
  231. * @param int $maxwidth the maximum width allowed for the image
  232. * @param int $maxheight the maximum height allowed for the image
  233. * @return string HTML fragment to add to the label
  234. */
  235. function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight) {
  236. global $CFG;
  237. $fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
  238. $link = null;
  239. $attrib = array('alt' => $file->get_filename(), 'src' => $fullurl);
  240. if ($imginfo = $file->get_imageinfo()) {
  241. // Work out the new width / height, bounded by maxwidth / maxheight
  242. $width = $imginfo['width'];
  243. $height = $imginfo['height'];
  244. if (!empty($maxwidth) && $width > $maxwidth) {
  245. $height *= (float)$maxwidth / $width;
  246. $width = $maxwidth;
  247. }
  248. if (!empty($maxheight) && $height > $maxheight) {
  249. $width *= (float)$maxheight / $height;
  250. $height = $maxheight;
  251. }
  252. $attrib['width'] = $width;
  253. $attrib['height'] = $height;
  254. // If the size has changed and the image is of a suitable mime type, generate a smaller version
  255. if ($width != $imginfo['width']) {
  256. $mimetype = $file->get_mimetype();
  257. if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') {
  258. require_once($CFG->libdir.'/gdlib.php');
  259. $data = $file->generate_image_thumbnail($width, $height);
  260. if (!empty($data)) {
  261. $fs = get_file_storage();
  262. $record = array(
  263. 'contextid' => $file->get_contextid(),
  264. 'component' => $file->get_component(),
  265. 'filearea' => $file->get_filearea(),
  266. 'itemid' => $file->get_itemid(),
  267. 'filepath' => '/',
  268. 'filename' => 's_'.$file->get_filename(),
  269. );
  270. $smallfile = $fs->create_file_from_string($record, $data);
  271. // Replace the image 'src' with the resized file and link to the original
  272. $attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(),
  273. $smallfile->get_filename());
  274. $link = $fullurl;
  275. }
  276. }
  277. }
  278. } else {
  279. // Assume this is an image type that get_imageinfo cannot handle (e.g. SVG)
  280. $attrib['width'] = $maxwidth;
  281. }
  282. $img = html_writer::empty_tag('img', $attrib);
  283. if ($link) {
  284. return html_writer::link($link, $img);
  285. } else {
  286. return $img;
  287. }
  288. }
  289. /**
  290. * Check if the module has any update that affects the current user since a given time.
  291. *
  292. * @param cm_info $cm course module data
  293. * @param int $from the time to check updates from
  294. * @param array $filter if we need to check only specific updates
  295. * @return stdClass an object with the different type of areas indicating if they were updated or not
  296. * @since Moodle 3.2
  297. */
  298. function label_check_updates_since(cm_info $cm, $from, $filter = array()) {
  299. $updates = course_check_module_updates_since($cm, $from, array(), $filter);
  300. return $updates;
  301. }