PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/assignment/backuplib.php

https://github.com/bobpuffer/LAE
PHP | 283 lines | 176 code | 49 blank | 58 comment | 21 complexity | c9acb087a8d7a0ffa2f1555cb83a3ef7 MD5 | raw file
  1. <?php //$Id$
  2. //This php script contains all the stuff to backup/restore
  3. //assignment mods
  4. //This is the "graphical" structure of the assignment mod:
  5. //
  6. // assignment
  7. // (CL,pk->id)
  8. // |
  9. // |
  10. // |
  11. // assignment_submisions
  12. // (UL,pk->id, fk->assignment,files)
  13. //
  14. // Meaning: pk->primary key field of the table
  15. // fk->foreign key to link with parent
  16. // nt->nested field (recursive data)
  17. // CL->course level info
  18. // UL->user level info
  19. // files->table may have files)
  20. //
  21. //-----------------------------------------------------------
  22. //This function executes all the backup procedure about this mod
  23. function assignment_backup_mods($bf,$preferences) {
  24. global $CFG;
  25. $status = true;
  26. //Iterate over assignment table
  27. $assignments = get_records ("assignment","course",$preferences->backup_course,"id");
  28. if ($assignments) {
  29. foreach ($assignments as $assignment) {
  30. if (backup_mod_selected($preferences,'assignment',$assignment->id)) {
  31. $status = assignment_backup_one_mod($bf,$preferences,$assignment);
  32. // backup files happens in backup_one_mod now too.
  33. }
  34. }
  35. }
  36. return $status;
  37. }
  38. function assignment_backup_one_mod($bf,$preferences,$assignment) {
  39. global $CFG;
  40. if (is_numeric($assignment)) {
  41. $assignment = get_record('assignment','id',$assignment);
  42. }
  43. $status = true;
  44. //Start mod
  45. fwrite ($bf,start_tag("MOD",3,true));
  46. //Print assignment data
  47. fwrite ($bf,full_tag("ID",4,false,$assignment->id));
  48. fwrite ($bf,full_tag("MODTYPE",4,false,"assignment"));
  49. fwrite ($bf,full_tag("NAME",4,false,$assignment->name));
  50. fwrite ($bf,full_tag("DESCRIPTION",4,false,$assignment->description));
  51. fwrite ($bf,full_tag("FORMAT",4,false,$assignment->format));
  52. fwrite ($bf,full_tag("RESUBMIT",4,false,$assignment->resubmit));
  53. fwrite ($bf,full_tag("PREVENTLATE",4,false,$assignment->preventlate));
  54. fwrite ($bf,full_tag("EMAILTEACHERS",4,false,$assignment->emailteachers));
  55. fwrite ($bf,full_tag("VAR1",4,false,$assignment->var1));
  56. fwrite ($bf,full_tag("VAR2",4,false,$assignment->var2));
  57. fwrite ($bf,full_tag("VAR3",4,false,$assignment->var3));
  58. fwrite ($bf,full_tag("VAR4",4,false,$assignment->var4));
  59. fwrite ($bf,full_tag("VAR5",4,false,$assignment->var5));
  60. fwrite ($bf,full_tag("ASSIGNMENTTYPE",4,false,$assignment->assignmenttype));
  61. fwrite ($bf,full_tag("MAXBYTES",4,false,$assignment->maxbytes));
  62. fwrite ($bf,full_tag("TIMEDUE",4,false,$assignment->timedue));
  63. fwrite ($bf,full_tag("TIMEAVAILABLE",4,false,$assignment->timeavailable));
  64. fwrite ($bf,full_tag("GRADE",4,false,$assignment->grade));
  65. fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$assignment->timemodified));
  66. $class = 'assignment_' . $assignment->assignmenttype;
  67. require_once($CFG->dirroot . '/mod/assignment/lib.php');
  68. require_once($CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php');
  69. call_user_func(array($class, 'backup_one_mod'), $bf, $preferences, $assignment);
  70. //if we've selected to backup users info, then execute backup_assignment_submisions and
  71. //backup_assignment_files_instance
  72. if (backup_userdata_selected($preferences,'assignment',$assignment->id)) {
  73. $status = backup_assignment_submissions($bf,$preferences,$assignment);
  74. if ($status) {
  75. $status = backup_assignment_files_instance($bf,$preferences,$assignment->id);
  76. }
  77. }
  78. //End mod
  79. $status =fwrite ($bf,end_tag("MOD",3,true));
  80. return $status;
  81. }
  82. //Backup assignment_submissions contents (executed from assignment_backup_mods)
  83. function backup_assignment_submissions ($bf,$preferences,$assignment) {
  84. global $CFG;
  85. $status = true;
  86. $assignment_submissions = get_records("assignment_submissions","assignment",$assignment->id,"id");
  87. //If there is submissions
  88. if ($assignment_submissions) {
  89. //Write start tag
  90. $status =fwrite ($bf,start_tag("SUBMISSIONS",4,true));
  91. //Iterate over each submission
  92. foreach ($assignment_submissions as $ass_sub) {
  93. //Start submission
  94. $status =fwrite ($bf,start_tag("SUBMISSION",5,true));
  95. //Print submission contents
  96. fwrite ($bf,full_tag("ID",6,false,$ass_sub->id));
  97. fwrite ($bf,full_tag("USERID",6,false,$ass_sub->userid));
  98. fwrite ($bf,full_tag("TIMECREATED",6,false,$ass_sub->timecreated));
  99. fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$ass_sub->timemodified));
  100. fwrite ($bf,full_tag("NUMFILES",6,false,$ass_sub->numfiles));
  101. fwrite ($bf,full_tag("DATA1",6,false,$ass_sub->data1));
  102. fwrite ($bf,full_tag("DATA2",6,false,$ass_sub->data2));
  103. fwrite ($bf,full_tag("GRADE",6,false,$ass_sub->grade));
  104. fwrite ($bf,full_tag("SUBMISSIONCOMMENT",6,false,$ass_sub->submissioncomment));
  105. fwrite ($bf,full_tag("FORMAT",6,false,$ass_sub->format));
  106. fwrite ($bf,full_tag("TEACHER",6,false,$ass_sub->teacher));
  107. fwrite ($bf,full_tag("TIMEMARKED",6,false,$ass_sub->timemarked));
  108. fwrite ($bf,full_tag("MAILED",6,false,$ass_sub->mailed));
  109. $class = 'assignment_' . $assignment->assignmenttype;
  110. require_once($CFG->dirroot . '/mod/assignment/lib.php');
  111. require_once($CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php');
  112. call_user_func(array($class, 'backup_one_submission'), $bf, $preferences, $assignment, $ass_sub);
  113. //End submission
  114. $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
  115. }
  116. //Write end tag
  117. $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
  118. }
  119. return $status;
  120. }
  121. //Backup assignment files because we've selected to backup user info
  122. //and files are user info's level
  123. function backup_assignment_files($bf,$preferences) {
  124. global $CFG;
  125. $status = true;
  126. //First we check to moddata exists and create it as necessary
  127. //in temp/backup/$backup_code dir
  128. $status = check_and_create_moddata_dir($preferences->backup_unique_code);
  129. //Now copy the assignment dir
  130. if ($status) {
  131. //Only if it exists !! Thanks to Daniel Miksik.
  132. if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment")) {
  133. $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment",
  134. $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment");
  135. }
  136. }
  137. return $status;
  138. }
  139. function backup_assignment_files_instance($bf,$preferences,$instanceid) {
  140. global $CFG;
  141. $status = true;
  142. //First we check to moddata exists and create it as necessary
  143. //in temp/backup/$backup_code dir
  144. $status = check_and_create_moddata_dir($preferences->backup_unique_code);
  145. $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment/",true);
  146. //Now copy the assignment dir
  147. if ($status) {
  148. //Only if it exists !! Thanks to Daniel Miksik.
  149. if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment/".$instanceid)) {
  150. $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment/".$instanceid,
  151. $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment/".$instanceid);
  152. }
  153. }
  154. return $status;
  155. }
  156. //Return an array of info (name,value)
  157. function assignment_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
  158. if (!empty($instances) && is_array($instances) && count($instances)) {
  159. $info = array();
  160. foreach ($instances as $id => $instance) {
  161. $info += assignment_check_backup_mods_instances($instance,$backup_unique_code);
  162. }
  163. return $info;
  164. }
  165. //First the course data
  166. $info[0][0] = get_string("modulenameplural","assignment");
  167. if ($ids = assignment_ids ($course)) {
  168. $info[0][1] = count($ids);
  169. } else {
  170. $info[0][1] = 0;
  171. }
  172. //Now, if requested, the user_data
  173. if ($user_data) {
  174. $info[1][0] = get_string("submissions","assignment");
  175. if ($ids = assignment_submission_ids_by_course ($course)) {
  176. $info[1][1] = count($ids);
  177. } else {
  178. $info[1][1] = 0;
  179. }
  180. }
  181. return $info;
  182. }
  183. //Return an array of info (name,value)
  184. function assignment_check_backup_mods_instances($instance,$backup_unique_code) {
  185. $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
  186. $info[$instance->id.'0'][1] = '';
  187. if (!empty($instance->userdata)) {
  188. $info[$instance->id.'1'][0] = get_string("submissions","assignment");
  189. if ($ids = assignment_submission_ids_by_instance ($instance->id)) {
  190. $info[$instance->id.'1'][1] = count($ids);
  191. } else {
  192. $info[$instance->id.'1'][1] = 0;
  193. }
  194. }
  195. return $info;
  196. }
  197. //Return a content encoded to support interactivities linking. Every module
  198. //should have its own. They are called automatically from the backup procedure.
  199. function assignment_encode_content_links ($content,$preferences) {
  200. global $CFG;
  201. $base = preg_quote($CFG->wwwroot,"/");
  202. //Link to the list of assignments
  203. $buscar="/(".$base."\/mod\/assignment\/index.php\?id\=)([0-9]+)/";
  204. $result= preg_replace($buscar,'$@ASSIGNMENTINDEX*$2@$',$content);
  205. //Link to assignment view by moduleid
  206. $buscar="/(".$base."\/mod\/assignment\/view.php\?id\=)([0-9]+)/";
  207. $result= preg_replace($buscar,'$@ASSIGNMENTVIEWBYID*$2@$',$result);
  208. return $result;
  209. }
  210. // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
  211. //Returns an array of assignments id
  212. function assignment_ids ($course) {
  213. global $CFG;
  214. return get_records_sql ("SELECT a.id, a.course
  215. FROM {$CFG->prefix}assignment a
  216. WHERE a.course = '$course'");
  217. }
  218. //Returns an array of assignment_submissions id
  219. function assignment_submission_ids_by_course ($course) {
  220. global $CFG;
  221. return get_records_sql ("SELECT s.id , s.assignment
  222. FROM {$CFG->prefix}assignment_submissions s,
  223. {$CFG->prefix}assignment a
  224. WHERE a.course = '$course' AND
  225. s.assignment = a.id");
  226. }
  227. //Returns an array of assignment_submissions id
  228. function assignment_submission_ids_by_instance ($instanceid) {
  229. global $CFG;
  230. return get_records_sql ("SELECT s.id , s.assignment
  231. FROM {$CFG->prefix}assignment_submissions s
  232. WHERE s.assignment = $instanceid");
  233. }
  234. ?>