PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/label/lib.php

http://github.com/moodle/moodle
PHP | 383 lines | 201 code | 50 blank | 132 comment | 33 complexity | 41c30c1914c3cedfeeac43d6bb0827e1 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. $id = $DB->insert_record("label", $label);
  57. $completiontimeexpected = !empty($label->completionexpected) ? $label->completionexpected : null;
  58. \core_completion\api::update_completion_date_event($label->coursemodule, 'label', $id, $completiontimeexpected);
  59. return $id;
  60. }
  61. /**
  62. * Given an object containing all the necessary data,
  63. * (defined by the form in mod_form.php) this function
  64. * will update an existing instance with new data.
  65. *
  66. * @global object
  67. * @param object $label
  68. * @return bool
  69. */
  70. function label_update_instance($label) {
  71. global $DB;
  72. $label->name = get_label_name($label);
  73. $label->timemodified = time();
  74. $label->id = $label->instance;
  75. $completiontimeexpected = !empty($label->completionexpected) ? $label->completionexpected : null;
  76. \core_completion\api::update_completion_date_event($label->coursemodule, 'label', $label->id, $completiontimeexpected);
  77. return $DB->update_record("label", $label);
  78. }
  79. /**
  80. * Given an ID of an instance of this module,
  81. * this function will permanently delete the instance
  82. * and any data that depends on it.
  83. *
  84. * @global object
  85. * @param int $id
  86. * @return bool
  87. */
  88. function label_delete_instance($id) {
  89. global $DB;
  90. if (! $label = $DB->get_record("label", array("id"=>$id))) {
  91. return false;
  92. }
  93. $result = true;
  94. $cm = get_coursemodule_from_instance('label', $id);
  95. \core_completion\api::update_completion_date_event($cm->id, 'label', $label->id, null);
  96. if (! $DB->delete_records("label", array("id"=>$label->id))) {
  97. $result = false;
  98. }
  99. return $result;
  100. }
  101. /**
  102. * Given a course_module object, this function returns any
  103. * "extra" information that may be needed when printing
  104. * this activity in a course listing.
  105. * See get_array_of_activities() in course/lib.php
  106. *
  107. * @global object
  108. * @param object $coursemodule
  109. * @return cached_cm_info|null
  110. */
  111. function label_get_coursemodule_info($coursemodule) {
  112. global $DB;
  113. if ($label = $DB->get_record('label', array('id'=>$coursemodule->instance), 'id, name, intro, introformat')) {
  114. if (empty($label->name)) {
  115. // label name missing, fix it
  116. $label->name = "label{$label->id}";
  117. $DB->set_field('label', 'name', $label->name, array('id'=>$label->id));
  118. }
  119. $info = new cached_cm_info();
  120. // no filtering hre because this info is cached and filtered later
  121. $info->content = format_module_intro('label', $label, $coursemodule->id, false);
  122. $info->name = $label->name;
  123. return $info;
  124. } else {
  125. return null;
  126. }
  127. }
  128. /**
  129. * This function is used by the reset_course_userdata function in moodlelib.
  130. *
  131. * @param object $data the data submitted from the reset course.
  132. * @return array status array
  133. */
  134. function label_reset_userdata($data) {
  135. // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
  136. // See MDL-9367.
  137. return array();
  138. }
  139. /**
  140. * @uses FEATURE_IDNUMBER
  141. * @uses FEATURE_GROUPS
  142. * @uses FEATURE_GROUPINGS
  143. * @uses FEATURE_MOD_INTRO
  144. * @uses FEATURE_COMPLETION_TRACKS_VIEWS
  145. * @uses FEATURE_GRADE_HAS_GRADE
  146. * @uses FEATURE_GRADE_OUTCOMES
  147. * @param string $feature FEATURE_xx constant for requested feature
  148. * @return bool|null True if module supports feature, false if not, null if doesn't know
  149. */
  150. function label_supports($feature) {
  151. switch($feature) {
  152. case FEATURE_IDNUMBER: return true;
  153. case FEATURE_GROUPS: return false;
  154. case FEATURE_GROUPINGS: return false;
  155. case FEATURE_MOD_INTRO: return true;
  156. case FEATURE_COMPLETION_TRACKS_VIEWS: return false;
  157. case FEATURE_GRADE_HAS_GRADE: return false;
  158. case FEATURE_GRADE_OUTCOMES: return false;
  159. case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
  160. case FEATURE_BACKUP_MOODLE2: return true;
  161. case FEATURE_NO_VIEW_LINK: return true;
  162. default: return null;
  163. }
  164. }
  165. /**
  166. * Register the ability to handle drag and drop file uploads
  167. * @return array containing details of the files / types the mod can handle
  168. */
  169. function label_dndupload_register() {
  170. $strdnd = get_string('dnduploadlabel', 'mod_label');
  171. if (get_config('label', 'dndmedia')) {
  172. $mediaextensions = file_get_typegroup('extension', ['web_image', 'web_video', 'web_audio']);
  173. $files = array();
  174. foreach ($mediaextensions as $extn) {
  175. $extn = trim($extn, '.');
  176. $files[] = array('extension' => $extn, 'message' => $strdnd);
  177. }
  178. $ret = array('files' => $files);
  179. } else {
  180. $ret = array();
  181. }
  182. $strdndtext = get_string('dnduploadlabeltext', 'mod_label');
  183. return array_merge($ret, array('types' => array(
  184. array('identifier' => 'text/html', 'message' => $strdndtext, 'noname' => true),
  185. array('identifier' => 'text', 'message' => $strdndtext, 'noname' => true)
  186. )));
  187. }
  188. /**
  189. * Handle a file that has been uploaded
  190. * @param object $uploadinfo details of the file / content that has been uploaded
  191. * @return int instance id of the newly created mod
  192. */
  193. function label_dndupload_handle($uploadinfo) {
  194. global $USER;
  195. // Gather the required info.
  196. $data = new stdClass();
  197. $data->course = $uploadinfo->course->id;
  198. $data->name = $uploadinfo->displayname;
  199. $data->intro = '';
  200. $data->introformat = FORMAT_HTML;
  201. $data->coursemodule = $uploadinfo->coursemodule;
  202. // Extract the first (and only) file from the file area and add it to the label as an img tag.
  203. if (!empty($uploadinfo->draftitemid)) {
  204. $fs = get_file_storage();
  205. $draftcontext = context_user::instance($USER->id);
  206. $context = context_module::instance($uploadinfo->coursemodule);
  207. $files = $fs->get_area_files($draftcontext->id, 'user', 'draft', $uploadinfo->draftitemid, '', false);
  208. if ($file = reset($files)) {
  209. if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) {
  210. // It is an image - resize it, if too big, then insert the img tag.
  211. $config = get_config('label');
  212. $data->intro = label_generate_resized_image($file, $config->dndresizewidth, $config->dndresizeheight);
  213. } else {
  214. // We aren't supposed to be supporting non-image types here, but fallback to adding a link, just in case.
  215. $url = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
  216. $data->intro = html_writer::link($url, $file->get_filename());
  217. }
  218. $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0,
  219. null, $data->intro);
  220. }
  221. } else if (!empty($uploadinfo->content)) {
  222. $data->intro = $uploadinfo->content;
  223. if ($uploadinfo->type != 'text/html') {
  224. $data->introformat = FORMAT_PLAIN;
  225. }
  226. }
  227. return label_add_instance($data, null);
  228. }
  229. /**
  230. * Resize the image, if required, then generate an img tag and, if required, a link to the full-size image
  231. * @param stored_file $file the image file to process
  232. * @param int $maxwidth the maximum width allowed for the image
  233. * @param int $maxheight the maximum height allowed for the image
  234. * @return string HTML fragment to add to the label
  235. */
  236. function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight) {
  237. global $CFG;
  238. $fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
  239. $link = null;
  240. $attrib = array('alt' => $file->get_filename(), 'src' => $fullurl);
  241. if ($imginfo = $file->get_imageinfo()) {
  242. // Work out the new width / height, bounded by maxwidth / maxheight
  243. $width = $imginfo['width'];
  244. $height = $imginfo['height'];
  245. if (!empty($maxwidth) && $width > $maxwidth) {
  246. $height *= (float)$maxwidth / $width;
  247. $width = $maxwidth;
  248. }
  249. if (!empty($maxheight) && $height > $maxheight) {
  250. $width *= (float)$maxheight / $height;
  251. $height = $maxheight;
  252. }
  253. $attrib['width'] = $width;
  254. $attrib['height'] = $height;
  255. // If the size has changed and the image is of a suitable mime type, generate a smaller version
  256. if ($width != $imginfo['width']) {
  257. $mimetype = $file->get_mimetype();
  258. if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') {
  259. require_once($CFG->libdir.'/gdlib.php');
  260. $data = $file->generate_image_thumbnail($width, $height);
  261. if (!empty($data)) {
  262. $fs = get_file_storage();
  263. $record = array(
  264. 'contextid' => $file->get_contextid(),
  265. 'component' => $file->get_component(),
  266. 'filearea' => $file->get_filearea(),
  267. 'itemid' => $file->get_itemid(),
  268. 'filepath' => '/',
  269. 'filename' => 's_'.$file->get_filename(),
  270. );
  271. $smallfile = $fs->create_file_from_string($record, $data);
  272. // Replace the image 'src' with the resized file and link to the original
  273. $attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(),
  274. $smallfile->get_filename());
  275. $link = $fullurl;
  276. }
  277. }
  278. }
  279. } else {
  280. // Assume this is an image type that get_imageinfo cannot handle (e.g. SVG)
  281. $attrib['width'] = $maxwidth;
  282. }
  283. $img = html_writer::empty_tag('img', $attrib);
  284. if ($link) {
  285. return html_writer::link($link, $img);
  286. } else {
  287. return $img;
  288. }
  289. }
  290. /**
  291. * Check if the module has any update that affects the current user since a given time.
  292. *
  293. * @param cm_info $cm course module data
  294. * @param int $from the time to check updates from
  295. * @param array $filter if we need to check only specific updates
  296. * @return stdClass an object with the different type of areas indicating if they were updated or not
  297. * @since Moodle 3.2
  298. */
  299. function label_check_updates_since(cm_info $cm, $from, $filter = array()) {
  300. $updates = course_check_module_updates_since($cm, $from, array(), $filter);
  301. return $updates;
  302. }
  303. /**
  304. * This function receives a calendar event and returns the action associated with it, or null if there is none.
  305. *
  306. * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
  307. * is not displayed on the block.
  308. *
  309. * @param calendar_event $event
  310. * @param \core_calendar\action_factory $factory
  311. * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
  312. * @return \core_calendar\local\event\entities\action_interface|null
  313. */
  314. function mod_label_core_calendar_provide_event_action(calendar_event $event,
  315. \core_calendar\action_factory $factory,
  316. int $userid = 0) {
  317. $cm = get_fast_modinfo($event->courseid, $userid)->instances['label'][$event->instance];
  318. if (!$cm->uservisible) {
  319. // The module is not visible to the user for any reason.
  320. return null;
  321. }
  322. $completion = new \completion_info($cm->get_course());
  323. $completiondata = $completion->get_data($cm, false, $userid);
  324. if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
  325. return null;
  326. }
  327. return $factory->create_instance(
  328. get_string('view'),
  329. new \moodle_url('/mod/label/view.php', ['id' => $cm->id]),
  330. 1,
  331. true
  332. );
  333. }