PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/assignment/backuplib.php

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