PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/lightboxgallery/lib.php

https://github.com/galitush2005/RTL-BIDI-Hebrew-Moodle-Plugins
PHP | 403 lines | 309 code | 92 blank | 2 comment | 45 complexity | c414aeccdfcf31abe6acc7e5308656f5 MD5 | raw file
  1. <?php
  2. require_once($CFG->libdir . '/gdlib.php');
  3. require_once($CFG->libdir . '/filelib.php');
  4. define('THUMB_WIDTH', 120);
  5. define('THUMB_HEIGHT', 105);
  6. define('MAX_IMAGE_LABEL', 14);
  7. define('AUTO_RESIZE_SCREEN', 1);
  8. define('AUTO_RESIZE_UPLOAD', 2);
  9. define('AUTO_RESIZE_BOTH', 3);
  10. if (! get_config('lightboxgallery', 'disabledplugins')) {
  11. set_config('disabledplugins', '', 'lightboxgallery');
  12. }
  13. if (! get_config('lightboxgallery', 'enablerssfeeds')) {
  14. set_config('enablerssfeeds', 0, 'lightboxgallery');
  15. }
  16. function lightboxgallery_add_instance($gallery) {
  17. global $CFG;
  18. if (! lightboxgallery_rss_enabled()) {
  19. $gallery->rss = 0;
  20. }
  21. $gallery->timemodified = time();
  22. return insert_record('lightboxgallery', $gallery);
  23. }
  24. function lightboxgallery_update_instance($gallery) {
  25. global $CFG;
  26. $gallery->id = $gallery->instance;
  27. if (! lightboxgallery_rss_enabled()) {
  28. $gallery->rss = 0;
  29. }
  30. if ($gallery->autoresizedisabled) {
  31. $gallery->autoresize = 0;
  32. $gallery->resize = 0;
  33. }
  34. $gallery->timemodified = time();
  35. return update_record('lightboxgallery', $gallery);
  36. }
  37. function lightboxgallery_delete_instance($id) {
  38. if ($gallery = get_record('lightboxgallery', 'id', $id)) {
  39. $result = true;
  40. $result = $result && delete_records('lightboxgallery', 'id', $gallery->id);
  41. $result = $result && delete_records('lightboxgallery_comments', 'gallery', $gallery->id);
  42. $result = $result && delete_records('lightboxgallery_image_meta', 'gallery', $gallery->id);
  43. } else {
  44. $result = false;
  45. }
  46. return $result;
  47. }
  48. function lightboxgallery_user_outline($course, $user, $mod, $resource) {
  49. if ($logs = get_records_select('log', "userid='$user->id' AND module='lightboxgallery' AND action='view' AND info='$resource->id'", 'time ASC')) {
  50. $numviews = count($logs);
  51. $lastlog = array_pop($logs);
  52. $result = new object;
  53. $result->info = get_string('numviews', '', $numviews);
  54. $result->time = $lastlog->time;
  55. return $result;
  56. } else {
  57. return null;
  58. }
  59. }
  60. function lightboxgallery_user_complete($course, $user, $mod, $resource) {
  61. if ($logs = get_records_select('log', "userid='$user->id' AND module='lightboxgallery' AND action='view' AND info='$resource->id'", 'time ASC')) {
  62. $numviews = count($logs);
  63. $lastlog = array_pop($logs);
  64. $strmostrecently = get_string('mostrecently');
  65. $strnumviews = get_string('numviews', '', $numviews);
  66. echo($strnumviews . ' - ' . $strmostrecently . ' ' . userdate($lastlog->time));
  67. } else {
  68. print_string('neverseen', 'resource');
  69. }
  70. }
  71. function lightboxgallery_get_participants($lightboxgalleryid) {
  72. return false;
  73. }
  74. function lightboxgallery_get_coursemodule_info($coursemodule) {
  75. $info = null;
  76. if ($gallery = get_record('lightboxgallery', 'id', $coursemodule->instance)) {
  77. $info->extra = urlencode($gallery->name);
  78. }
  79. return $info;
  80. }
  81. function lightboxgallery_get_view_actions() {
  82. return array('view', 'view all', 'search');
  83. }
  84. function lightboxgallery_get_post_actions() {
  85. return array('comment', 'addimage', 'editimage');
  86. }
  87. function lightboxgallery_get_types() {
  88. $types = array();
  89. $type = new object;
  90. $type->modclass = MOD_CLASS_RESOURCE;
  91. $type->type = 'lightboxgallery';
  92. $type->typestr = get_string('modulenameadd', 'lightboxgallery');
  93. $types[] = $type;
  94. return $types;
  95. }
  96. // Custom lightboxgallery methods
  97. function lightboxgallery_allowed_filetypes() {
  98. return array('jpg', 'jpeg', 'gif', 'png');
  99. }
  100. function lightboxgallery_allowed_filetype($element) {
  101. $extension = strtolower(substr(strrchr($element, '.'), 1));
  102. return in_array($extension, lightboxgallery_allowed_filetypes());
  103. }
  104. function lightboxgallery_directory_images($directory) {
  105. $files = get_directory_list($directory, '', false, false, true);
  106. return array_filter($files, 'lightboxgallery_allowed_filetype');
  107. }
  108. function lightboxgallery_get_image_url($galleryid, $image = false, $thumb = false) {
  109. global $CFG;
  110. $script = $CFG->wwwroot . '/mod/lightboxgallery/pic.php';
  111. $path = $galleryid . ($image ? '/' . rawurlencode($image) : '');
  112. if ($CFG->slasharguments) {
  113. $url = $script . '/' . $path . ($thumb ? '?thumb=1' : '');
  114. } else {
  115. $url = $script . '?file=/' . $path . ($thumb ? '&amp;thumb=1' : '');
  116. }
  117. return $url;
  118. }
  119. function lightboxgallery_make_img($path, $imageid = '') {
  120. return '<img src="' . $path . '" alt="" id="' . $imageid . '" />';
  121. }
  122. function lightboxgallery_imagecreatefromtype($type, $path) {
  123. switch ($type) {
  124. case 1:
  125. $function = 'ImageCreateFromGIF';
  126. break;
  127. case 2:
  128. $function = 'ImageCreateFromJPEG';
  129. break;
  130. case 3:
  131. $function = 'ImageCreateFromPNG';
  132. break;
  133. }
  134. if (function_exists($function)) {
  135. return $function($path);
  136. } else {
  137. return false;
  138. }
  139. }
  140. function lightboxgallery_image_create($width, $height) {
  141. global $CFG;
  142. if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) {
  143. return ImageCreateTrueColor($width, $height);
  144. } else {
  145. return ImageCreate($width, $height);
  146. }
  147. }
  148. function lightboxgallery_resize_image($image, $infoobj, $width, $height, $offsetx = 0, $offsety = 0) {
  149. global $CFG;
  150. $resized = lightboxgallery_image_create($width, $height);
  151. $oldwidth = $infoobj->imagesize[0];
  152. $oldheight = $infoobj->imagesize[1];
  153. $cx = $oldwidth / 2;
  154. $cy = $oldheight / 2;
  155. $ratiow = $width / $oldwidth;
  156. $ratioh = $height / $oldheight;
  157. if ($ratiow < $ratioh) {
  158. $srcw = floor($width / $ratioh);
  159. $srch = $oldheight;
  160. $srcx = floor($cx - ($srcw / 2)) + $offsetx;
  161. $srcy = $offsety;
  162. } else {
  163. $srcw = $oldwidth;
  164. $srch = floor($height / $ratiow);
  165. $srcx = $offsetx;
  166. $srcy = floor($cy - ($srch / 2)) + $offsety;
  167. }
  168. ImageCopyBicubic($resized, $image, 0, 0, $srcx, $srcy, $width, $height, $srcw, $srch);
  169. return $resized;
  170. }
  171. function lightboxgallery_image_thumbnail($courseid, $gallery, $file, $offsetx = 0, $offsety = 0) {
  172. global $CFG;
  173. // If anything fails when retrieving the thumbnail, we'll fallback to just printing a label
  174. $fallback = '['.$file.']';
  175. $oldpath = $CFG->dataroot.'/'.$courseid.'/'.$gallery->folder.'/'.$file;
  176. $newpath = $courseid.'/'.$gallery->folder.'/_thumb/'.$file.'.jpg';
  177. if (empty($CFG->gdversion)) {
  178. return $fallback;
  179. }
  180. umask(0000);
  181. if (file_exists($CFG->dataroot.'/'.$newpath)) {
  182. return lightboxgallery_make_img(lightboxgallery_get_image_url($gallery->id, $file, true));
  183. } else {
  184. $thumbdir = $CFG->dataroot.'/'.dirname($newpath);
  185. if (!file_exists($thumbdir) && !mkdir($thumbdir, $CFG->directorypermissions)) {
  186. return $fallback;
  187. }
  188. }
  189. $info = lightboxgallery_image_info($oldpath);
  190. if (! $im = lightboxgallery_imagecreatefromtype($info->imagesize[2], $oldpath)) {
  191. return $fallback;
  192. }
  193. $thumb = lightboxgallery_resize_image($im, $info, THUMB_WIDTH, THUMB_HEIGHT, $offsetx, $offsety);
  194. if (function_exists('ImageJpeg')) {
  195. @touch($CFG->dataroot.'/'.$newpath);
  196. if (ImageJpeg($thumb, $CFG->dataroot.'/'.$newpath, 90)) {
  197. @chmod($CFG->dataroot.'/'.$newpath, 0666);
  198. return lightboxgallery_make_img(lightboxgallery_get_image_url($gallery->id, $file, true));
  199. }
  200. } else {
  201. return $fallback;
  202. }
  203. }
  204. function lightboxgallery_print_comment($comment, $context) {
  205. global $CFG, $COURSE;
  206. $user = get_record('user', 'id', $comment->userid);
  207. echo('<table cellspacing="0" align="center" width="50%" class="datacomment forumpost">');
  208. echo('<tr class="header"><td class="picture left">');
  209. print_user_picture($user->id, $COURSE->id, $user->picture);
  210. echo('</td>');
  211. echo('<td class="topic starter" align="left"><div class="author">');
  212. echo('<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$COURSE->id.'">' . fullname($user, has_capability('moodle/site:viewfullnames', $context)) . '</a> - '.userdate($comment->timemodified));
  213. echo('</div></td></tr>');
  214. echo('<tr><td class="left side">');
  215. if ($groups = user_group($COURSE->id, $user->id)) {
  216. print_group_picture($groups, $COURSE->id, false, false, true);
  217. } else {
  218. echo('&nbsp;');
  219. }
  220. echo('</td><td class="content" align="left">');
  221. echo(format_text($comment->comment, FORMAT_MOODLE));
  222. echo('<div class="commands">');
  223. if (has_capability('mod/lightboxgallery:edit', $context)) {
  224. echo('<a href="'.$CFG->wwwroot.'/mod/lightboxgallery/comment.php?id='.$comment->gallery.'&amp;cid='.$comment->id.'&amp;action=delete">'.get_string('delete').'</a>');
  225. }
  226. echo('</div>');
  227. echo('</td></tr></table>');
  228. }
  229. function lightboxgallery_image_modified($file) {
  230. $timestamp = 0;
  231. if (function_exists('exif_read_data') && $exif = @exif_read_data($file, 0, true)) {
  232. if (isset($exif['IFD0']['DateTime'])) {
  233. $date = preg_split('/[:]|[ ]/', $exif['IFD0']['DateTime']);
  234. $timestamp = mktime($date[3], $date[4], $date[5], $date[1], $date[2], $date[0]);
  235. }
  236. }
  237. if (! $timestamp > 0) {
  238. $timestamp = filemtime($file);
  239. }
  240. return date('d/m/y H:i', $timestamp);
  241. }
  242. function lightboxgallery_image_info($file) {
  243. $result = new object;
  244. $result->filesize = display_size(filesize($file));
  245. $result->modified = lightboxgallery_image_modified($file);
  246. $result->imagesize = getimagesize($file);
  247. return $result;
  248. }
  249. function lightboxgallery_set_image_caption($galleryid, $image, $caption) {
  250. if ($oldcaption = get_record('lightboxgallery_image_meta', 'metatype', 'caption', 'gallery', $galleryid, 'image', $image)) {
  251. $oldcaption->description = $caption;
  252. return update_record('lightboxgallery_image_meta', $oldcaption);
  253. } else {
  254. $newcaption = new object;
  255. $newcaption->gallery = $galleryid;
  256. $newcaption->image = $image;
  257. $newcaption->metatype = 'caption';
  258. $newcaption->description = $caption;
  259. return insert_record('lightboxgallery_image_meta', $newcaption);
  260. }
  261. }
  262. function lightboxgallery_edit_types($showall = false) {
  263. global $CFG;
  264. $result = array();
  265. $disabledplugins = explode(',', get_config('lightboxgallery', 'disabledplugins'));
  266. $edittypes = get_directory_list($CFG->dirroot . '/mod/lightboxgallery/edittype/', '', false, true, false);
  267. foreach ($edittypes as $edittype) {
  268. if ($showall || !in_array($edittype, $disabledplugins)) {
  269. $result[$edittype] = get_string('edit_' . $edittype, 'lightboxgallery');
  270. }
  271. }
  272. return $result;
  273. }
  274. function lightboxgallery_print_tags($heading, $tags, $courseid, $galleryid) {
  275. global $CFG;
  276. print_simple_box_start('center');
  277. echo('<form action="search.php" style="float: right; margin-left: 4px;">' .
  278. ' <input type="hidden" name="id" value="' . $courseid . '" />' .
  279. ' <input type="hidden" name="l" value="' . $galleryid . '" />' .
  280. ' <input type="text" name="search" size="8" />' .
  281. ' <input type="submit" value="' . get_string('search') . '" />' .
  282. '</form>');
  283. echo($heading . ': ');
  284. $tagarray = array();
  285. foreach ($tags as $tag) {
  286. $tagarray[] = '<a class="taglink" href="' . $CFG->wwwroot . '/mod/lightboxgallery/search.php?id=' . $courseid . '&amp;l=' . $galleryid . '&amp;search=' . urlencode($tag->description) . '">' . $tag->description . '</a>';
  287. }
  288. echo(implode(', ', $tagarray));
  289. print_simple_box_end();
  290. }
  291. function lightboxgallery_resize_options() {
  292. return array(1 => '1280x1024', 2 => '1024x768', 3 => '800x600', 4 => '640x480');
  293. }
  294. function lightboxgallery_rss_enabled() {
  295. global $CFG;
  296. return ($CFG->enablerssfeeds && get_config('lightboxgallery', 'enablerssfeeds'));
  297. }
  298. function lightboxgallery_print_js_config($autoresize) {
  299. $resizetoscreen = (int)in_array($autoresize, array(AUTO_RESIZE_SCREEN, AUTO_RESIZE_BOTH));
  300. $jsconf = array('resizetoscreen' => $resizetoscreen, 'download' => get_string('imagedownload', 'lightboxgallery'));
  301. $jsconfvalues = array();
  302. foreach ($jsconf as $key => $value) {
  303. $jsconfvalues[] = "$key: '$value'";
  304. }
  305. echo('<script type="text/javascript">
  306. //<![CDATA[
  307. lightboxgallery_config = {' . implode(', ', $jsconfvalues) . '};
  308. //]]>
  309. </script>');
  310. }
  311. ?>