/mod/lightboxgallery/backuplib.php

https://github.com/cgtaylor/moodle · PHP · 207 lines · 156 code · 49 blank · 2 comment · 23 complexity · e36dc4eacf16d5b01483d579e93df3c0 MD5 · raw file

  1. <?php
  2. require_once($CFG->dirroot . '/mod/lightboxgallery/lib.php');
  3. function lightboxgallery_backup_mods($bf, $preferences) {
  4. $status = true;
  5. if ($galleries = get_records('lightboxgallery', 'course', $preferences->backup_course, 'id')) {
  6. foreach ($galleries as $gallery) {
  7. if (backup_mod_selected($preferences, 'lightboxgallery', $gallery->id)) {
  8. $status = lightboxgallery_backup_one_mod($bf, $preferences, $gallery);
  9. }
  10. }
  11. }
  12. return $status;
  13. }
  14. function lightboxgallery_backup_one_mod($bf, $preferences, $gallery) {
  15. $status = true;
  16. if (is_numeric($gallery)) {
  17. $gallery = get_record('lightboxgallery', 'id', $gallery);
  18. }
  19. fwrite($bf, start_tag('MOD', 3, true));
  20. fwrite($bf, full_tag('ID', 4, false, $gallery->id));
  21. fwrite($bf, full_tag('MODTYPE', 4, false, 'lightboxgallery'));
  22. fwrite($bf, full_tag('FOLDER', 4, false, $gallery->folder));
  23. fwrite($bf, full_tag('NAME', 4, false, $gallery->name));
  24. fwrite($bf, full_tag('DESCRIPTION', 4, false, $gallery->description));
  25. fwrite($bf, full_tag('PERPAGE', 4, false, $gallery->perpage));
  26. fwrite($bf, full_tag('COMMENTS', 4, false, $gallery->comments));
  27. fwrite($bf, full_tag('PUBLIC', 4, false, $gallery->ispublic));
  28. fwrite($bf, full_tag('RSS', 4, false, $gallery->rss));
  29. fwrite($bf, full_tag('AUTORESIZE', 4, false, $gallery->autoresize));
  30. fwrite($bf, full_tag('RESIZE', 4, false, $gallery->resize));
  31. fwrite($bf, full_tag('EXTINFO', 4, false, $gallery->extinfo));
  32. $status = backup_lightboxgallery_files_instance($bf, $preferences, $gallery);
  33. if ($status) {
  34. if (backup_userdata_selected($preferences, 'lightboxgallery', $gallery->id)) {
  35. $status = backup_lightboxgallery_metadata($bf, $preferences, $gallery->id);
  36. }
  37. }
  38. $status = fwrite($bf, end_tag('MOD', 3, true));
  39. return $status;
  40. }
  41. function backup_lightboxgallery_metadata($bf, $preferences, $galleryid) {
  42. $status = true;
  43. if ($records = get_records('lightboxgallery_image_meta', 'gallery', $galleryid, 'id')) {
  44. $status = fwrite($bf, start_tag('IMAGEMETAS', 4, true));
  45. foreach ($records as $record) {
  46. fwrite($bf, start_tag('IMAGEMETA', 5, true));
  47. fwrite($bf, full_tag('ID', 6, false, $record->id));
  48. fwrite($bf, full_tag('IMAGE', 6, false, $record->image));
  49. fwrite($bf, full_tag('METATYPE', 6, false, $record->metatype));
  50. fwrite($bf, full_tag('DESCRIPTION', 6, false, $record->description));
  51. $status = fwrite($bf, end_tag('IMAGEMETA', 5,true));
  52. }
  53. $status = fwrite($bf, end_tag('IMAGEMETAS', 4, true));
  54. }
  55. return $status;
  56. }
  57. function backup_lightboxgallery_files_instance($bf, $preferences, $gallery) {
  58. global $CFG;
  59. $status = true;
  60. if (is_numeric($gallery)) {
  61. $gallery = get_record('lightboxgallery', 'id', $gallery);
  62. }
  63. $tmppath = $CFG->dataroot . '/temp/backup/' . $preferences->backup_unique_code . '/' . $gallery->folder;
  64. $status = check_dir_exists($tmppath, true, true);
  65. if ($status) {
  66. $oldpath = $CFG->dataroot . '/' . $preferences->backup_course . '/' . $gallery->folder;
  67. if (is_dir($oldpath)) {
  68. $status = backup_copy_file($oldpath, $tmppath);
  69. }
  70. }
  71. return $status;
  72. }
  73. /***************************************************************************************************************************************************/
  74. function lightboxgallery_check_backup_mods($course, $user_data = false, $backup_unique_code, $instances = null) {
  75. if (!empty($instances) && is_array($instances) && count($instances)) {
  76. $info = array();
  77. foreach ($instances as $id => $instance) {
  78. $info += lightboxgallery_check_backup_mods_instances($instance, $backup_unique_code);
  79. }
  80. return $info;
  81. }
  82. $info[0][0] = get_string('modulenameplural', 'lightboxgallery');
  83. if ($ids = lightboxgallery_ids($course)) {
  84. $info[0][1] = count($ids);
  85. } else {
  86. $info[0][1] = 0;
  87. }
  88. if ($user_data) {
  89. $info[2][0] = get_string('metadata', 'lightboxgallery');
  90. if ($ids = lightboxgallery_meta_ids_by_course($course)) {
  91. $info[2][1] = count($ids);
  92. } else {
  93. $info[2][1] = 0;
  94. }
  95. }
  96. return $info;
  97. }
  98. function lightboxgallery_check_backup_mods_instances($instance, $backup_unique_code) {
  99. $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
  100. $info[$instance->id.'0'][1] = '';
  101. $info[$instance->id.'1'][0] = get_string('imagecount', 'lightboxgallery');
  102. if ($images = lightboxgallery_images_by_instance($instance->id)) {
  103. $info[$instance->id.'1'][1] = count($images);
  104. } else {
  105. $info[$instance->id.'1'][1] = 0;
  106. }
  107. if (!empty($instance->userdata)) {
  108. $info[$instance->id.'2'][0] = get_string('metadata', 'lightboxgallery');
  109. if ($ids = lightboxgallery_meta_ids_by_instance($instance->id)) {
  110. $info[$instance->id.'2'][1] = count($ids);
  111. } else {
  112. $info[$instance->id.'2'][1] = 0;
  113. }
  114. }
  115. return $info;
  116. }
  117. /***************************************************************************************************************************************************/
  118. function lightboxgallery_encode_content_links($content, $preferences) {
  119. global $CFG;
  120. $base = preg_quote($CFG->wwwroot, '/');
  121. $search = "/(".$base."\/mod\/lightboxgallery\/index.php\?id\=)([0-9]+)/";
  122. $result = preg_replace($search, '$@GALLERYINDEX*$2@$', $content);
  123. $search = "/(".$base."\/mod\/lightboxgallery\/view.php\?id\=)([0-9]+)/";
  124. $result = preg_replace($search, '$@GALLERYVIEWBYID*$2@$', $result);
  125. $search = "/(".$base."\/mod\/lightboxgallery\/view.php\?l\=)([0-9]+)/";
  126. $result = preg_replace($search, '$@GALLERYVIEWBYL*$2@$', $result);
  127. return $result;
  128. }
  129. function lightboxgallery_ids($course) {
  130. global $CFG;
  131. return get_records_sql("SELECT l.id, l.course
  132. FROM {$CFG->prefix}lightboxgallery l
  133. WHERE l.course = '$course'");
  134. }
  135. function lightboxgallery_meta_ids_by_course($course) {
  136. global $CFG;
  137. return get_records_sql("SELECT m.id, m.gallery
  138. FROM {$CFG->prefix}lightboxgallery_image_meta m,
  139. {$CFG->prefix}lightboxgallery l
  140. WHERE l.course = '$course' AND
  141. m.gallery = l.id");
  142. }
  143. function lightboxgallery_meta_ids_by_instance($instanceid) {
  144. global $CFG;
  145. return get_records_sql("SELECT m.id, m.gallery
  146. FROM {$CFG->prefix}lightboxgallery_image_meta m
  147. WHERE m.gallery = $instanceid");
  148. }
  149. function lightboxgallery_images_by_instance($instanceid) {
  150. global $CFG;
  151. $result = false;
  152. if ($gallery = get_record('lightboxgallery', 'id', $instanceid)) {
  153. $directory = $CFG->dataroot . '/' . $gallery->course . '/' . $gallery->folder;
  154. $result = lightboxgallery_directory_images($directory);
  155. }
  156. return $result;
  157. }
  158. ?>