backuplib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * This script contains all the functionality allowing SLOODLE data in Moodle to be backed-up.
  4. * The bulk of visible SLOODLE data resides in module instances.
  5. * Each module has a "primary" record in the "sloodle" database table.
  6. * However, there are several sub-types of modules, most of which have one or more "secondary" records elsewhere in the database.
  7. * This file directly backs-up the primary data, but then relies on individual module code (in "lib/modules") to backup secondary data.
  8. *
  9. * The current 'active objects' (i.e. objects which are authorised to access the course when the backup is initiated) will NOT currently be backed-up.
  10. * This is for security, since in the event that a course is being transfered it is not desirable to allow existing objects access to the new course.
  11. * An option may be added in future to allow object authorisations to be transferred in a safe way.
  12. *
  13. * NOTE: despite the above restriction, any objects relying on a prim password will still have access, since the prim password does not change during backup.
  14. *
  15. * @package sloodle
  16. * @todo Implement backup of site data, including avatar registrations
  17. * @todo Implement backup fo course data, including autoreg/enrol settings, and object layouts/configurations
  18. *
  19. */
  20. require_once($CFG->dirroot.'/mod/sloodle/init.php');
  21. require_once(SLOODLE_LIBROOT.'/modules.php');
  22. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  23. /**
  24. * Backs-up multiple modules of SLOODLE data.
  25. * @param $bf File handle for writing backup data to.
  26. * @param $preferences Contains the options controlling this backup.
  27. * @return bool True if successful or false on failure.
  28. */
  29. function sloodle_backup_mods($bf, $preferences)
  30. {
  31. $status = true;
  32. // Backup course-specific data
  33. //...
  34. // Go through each SLOODLE module in the specified course.
  35. $mods = sloodle_get_records('sloodle', 'course', $preferences->backup_course, 'id');
  36. if ($mods === false) return false;
  37. foreach ($mods as $mod) {
  38. // Is this module due for backup?
  39. if (backup_mod_selected($preferences, 'sloodle', $mod->id)) {
  40. // Backup this particular module
  41. if (!sloodle_backup_one_mod($bf, $preferences, $mod)) $status = false;
  42. }
  43. }
  44. return $status;
  45. }
  46. /**
  47. * Backs-up one instance of a SLOODLE module.
  48. * @param $bf Handle to the file to which backup data should be written.
  49. * @param $preferences Structure defining preferences which govern the backup.
  50. * @param object|int $mod If an object, then it is a record from the 'sloodle' db table. If it is a number, then it is the ID of a record in the 'sloodle' db table.
  51. * @return bool True if successful, or false if not.
  52. */
  53. function sloodle_backup_one_mod($bf, $preferences, $mod)
  54. {
  55. // Load a 'sloodle' record if necessary
  56. if (is_numeric($mod)) {
  57. $mod = sloodle_get_record('sloodle', 'id', $mod);
  58. if ($mod === false) return false;
  59. }
  60. $status = true;
  61. // Attempt to load the course module record from the database
  62. $cm = get_coursemodule_from_instance('sloodle', $mod->id);
  63. if ($cm === false) return false;
  64. // Attempt to get a SloodleModule object for this module sub-type
  65. $dummysession = new SloodleSession(false); // We need to provide this to keep the module happy!
  66. $moduleobj = sloodle_load_module($mod->type, $dummysession, $cm->id);
  67. if ($moduleobj == false) return false;
  68. // Start an element for this module instance, and backup the primary table data
  69. fwrite($bf, start_tag('MOD', 3, true));
  70. fwrite($bf, full_tag('ID', 4, false, $mod->id)); // Instance ID of this SLOODLE module
  71. fwrite($bf, full_tag('MODTYPE', 4, false, 'sloodle')); // Main type of module (always 'sloodle' in this case)
  72. fwrite($bf, full_tag('SUBTYPE', 4, false, $mod->type)); // Sub-type of module (e.g. 'controller' or 'presenter')
  73. fwrite($bf, full_tag('NAME', 4, false, $mod->name));
  74. fwrite($bf, full_tag('INTRO', 4, false, $mod->intro));
  75. fwrite($bf, full_tag('TIMECREATED', 4, false, $mod->timecreated));
  76. fwrite($bf, full_tag('TIMEMODIFIED', 4, false, $mod->timemodified));
  77. // Backup any secondary data
  78. fwrite($bf, start_tag('SECONDARYDATA', 4, true));
  79. if (!$moduleobj->backup($bf, backup_userdata_selected($preferences, 'sloodle', $mod->id))) $status = false;
  80. fwrite($bf, end_tag('SECONDARYDATA', 4, true));
  81. // Finish off
  82. if (!fwrite($bf, end_tag('MOD', 3, true))) $status = false;
  83. return $status;
  84. }
  85. /**
  86. * Checks the specified course (or specific instances therein) for SLOODLE data that can be backed-up.
  87. * @param $course Identifies the Moodle course to be backed-up.
  88. * @param bool $user_data Indicates whether or not user data is to be included in the check.
  89. * @param $backup_unique_code A unique code identifying this backup.
  90. * @param array $instances Optional. An array of instances of SLOODLE modules which should be checked.
  91. * @return Array of information that can be backed up.
  92. */
  93. function sloodle_check_backup_mods($course, $user_data, $backup_unique_code, $instances = null)
  94. {
  95. // Has information about specified instances been requested?
  96. if (!empty($instances) && is_array($instances) && count($instances)) {
  97. // Yes - construct an array of information about those instances.
  98. $info = array();
  99. foreach ($instances as $id => $instance) {
  100. $info += sloodle_check_backup_mods_instances($instance, $backup_unique_code);
  101. }
  102. return $info;
  103. }
  104. // We're getting backup information about the whole course.
  105. // Return structural information about the whole course
  106. $info[0][0] = get_string('modulenameplural', 'sloodle');
  107. $ids = sloodle_ids($course);
  108. if ($ids) {
  109. $info[0][1] = count($ids);
  110. } else {
  111. $info[0][1] = 0;
  112. }
  113. // Now, if requested, the user_data
  114. if ($user_data) {
  115. $info[1][0] = get_string('userdata', 'sloodle');
  116. $info[1][1] = 0;
  117. // Go through each SLOODLE module in the course
  118. if (is_array($ids)) {
  119. foreach ($ids as $id) {
  120. // Attempt to load a module object for this instance
  121. $module = sloodle_quick_load_module_from_instance($id[0]);
  122. if ($module) $info[1][1] += $module->get_user_data_count();
  123. }
  124. }
  125. }
  126. return $info;
  127. }
  128. /**
  129. * Check the backup infomation for a specific instance of the SLOODLE module.
  130. * @param $instance An object representing a record from the 'sloodle' table.
  131. * @param $backup_unique_code A unique code for this backup
  132. * @return array Backup information
  133. */
  134. function sloodle_check_backup_mods_instances($instance, $backup_unique_code) {
  135. // Add the course data
  136. $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
  137. $info[$instance->id.'0'][1] = '';
  138. // Now, if requested, the user_data
  139. if (!empty($instance->userdata)) {
  140. $info[$instance->id.'1'][0] = get_string('userdata', 'sloodle');
  141. $info[$instance->id.'1'][1] = 0;
  142. // Attempt to load the module data for this instance
  143. $module = sloodle_quick_load_module_from_instance($instance->id);
  144. if ($module) {
  145. $info[$instance->id.'1'][0] = $module->get_user_data_name();
  146. $info[$instance->id.'1'][1] = $module->get_user_data_count();
  147. }
  148. }
  149. return $info;
  150. }
  151. /**
  152. * Return the given content, encoded to support interactivities linking.
  153. * This is necessary so that any links between activities remain intact after backup, since the instance IDs change.
  154. * Function is called automatically by Moodle backup functionality.
  155. * @param string $content The content to be encoded.
  156. * @param $preferences An object defining preferences in this encoding.
  157. * @return string The content with encoded links.
  158. */
  159. function sloodle_encode_content_links ($content,$preferences)
  160. {
  161. global $CFG;
  162. // Define the base link.
  163. $base = preg_quote($CFG->wwwroot,"/");
  164. // Link to the list of SLOODLE module instances
  165. $buscar = "/(".$base."\/mod\/sloodle\/index.php\?id\=)([0-9]+)/";
  166. $result = preg_replace($buscar,'$@SLOODLEINDEX*$2@$', $content);
  167. // Link to an implicit module view
  168. $buscar = "/(".$base."\/mod\/sloodle\/view.php\?id\=)([0-9]+)/";
  169. $result = preg_replace($buscar,'$@SLOODLEVIEWBYID*$2@$', $result);
  170. // Link to an explicit module view
  171. $buscar = "/(".$base."\/mod\/sloodle\/view.php\?_type=module&id\=)([0-9]+)/";
  172. $result = preg_replace($buscar,'$@SLOODLEVIEWBYID*$2@$', $result);
  173. // Link to an explicit module view with HTML entity for &
  174. $buscar = "/(".$base."\/mod\/sloodle\/view.php\?_type=module&amp;id\=)([0-9]+)/";
  175. $result = preg_replace($buscar,'$@SLOODLEVIEWBYID*$2@$', $result);
  176. // To pass through any view type, use the following line:
  177. // $buscar = "/(".$base."\/mod\/sloodle\/view.php\?_type=[a-zA-Z0-9.-]+&amp;id\=)([0-9]+)/";
  178. // It is not known whether Moodle provides the ability to encode for the following view scripts in SLOODLE:
  179. // - view_course.php
  180. // - view.php?_type=course (course settings... same as view_course.php)
  181. // - view_layout.php
  182. return $result;
  183. }
  184. // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
  185. /**
  186. * Gets an array of SLOODLE module instance IDs.
  187. * (Corresponding to the 'id' field of the 'sloodle' db table.)
  188. * @param int $course ID number of the course get the instances from
  189. * @return array An array of instance IDs of SLOODLE modules.
  190. */
  191. function sloodle_ids($course)
  192. {
  193. global $CFG;
  194. return sloodle_get_records_sql_params("
  195. SELECT s.id, s.course
  196. FROM {$CFG->prefix}sloodle s
  197. WHERE s.course = ?
  198. ", array($course));
  199. }
  200. ?>