PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/assignment/type/uploadsingle/assignment.class.php

http://github.com/moodle/moodle
PHP | 425 lines | 314 code | 76 blank | 35 comment | 58 complexity | 501851c6104a97a907a9a5ecaef6652f MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Extend the base assignment class for assignments where you upload a single file
  18. *
  19. * @package mod-assignment
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. require_once($CFG->dirroot.'/mod/assignment/lib.php');
  23. require_once(dirname(__FILE__).'/upload_form.php');
  24. class assignment_uploadsingle extends assignment_base {
  25. function print_student_answer($userid, $return=false){
  26. global $CFG, $USER, $OUTPUT;
  27. $fs = get_file_storage();
  28. $browser = get_file_browser();
  29. $output = '';
  30. if ($submission = $this->get_submission($userid)) {
  31. if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
  32. foreach ($files as $file) {
  33. $filename = $file->get_filename();
  34. $found = true;
  35. $mimetype = $file->get_mimetype();
  36. $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
  37. $output .= '<a href="'.$path.'" ><img class="icon" src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" alt="'.$mimetype.'" />'.s($filename).'</a><br />';
  38. $output .= plagiarism_get_links(array('userid'=>$userid, 'file'=>$file, 'cmid'=>$this->cm->id, 'course'=>$this->course, 'assignment'=>$this->assignment));
  39. $output .='<br/>';
  40. }
  41. }
  42. }
  43. $output = '<div class="files">'.$output.'</div>';
  44. return $output;
  45. }
  46. function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
  47. parent::assignment_base($cmid, $assignment, $cm, $course);
  48. $this->type = 'uploadsingle';
  49. }
  50. function view() {
  51. global $USER, $OUTPUT;
  52. $context = get_context_instance(CONTEXT_MODULE,$this->cm->id);
  53. require_capability('mod/assignment:view', $context);
  54. add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
  55. $this->view_header();
  56. $this->view_intro();
  57. $this->view_dates();
  58. $filecount = false;
  59. if ($submission = $this->get_submission($USER->id)) {
  60. $filecount = $this->count_user_files($submission->id);
  61. if ($submission->timemarked) {
  62. $this->view_feedback();
  63. }
  64. if ($filecount) {
  65. echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter');
  66. }
  67. }
  68. if (is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
  69. $this->view_upload_form();
  70. }
  71. $this->view_footer();
  72. }
  73. function process_feedback() {
  74. if (!$feedback = data_submitted() or !confirm_sesskey()) { // No incoming data?
  75. return false;
  76. }
  77. $userid = required_param('userid', PARAM_INT);
  78. $offset = required_param('offset', PARAM_INT);
  79. $mform = $this->display_submission($offset, $userid, false);
  80. parent::process_feedback($mform);
  81. }
  82. function print_responsefiles($userid, $return=false) {
  83. global $CFG, $USER, $OUTPUT, $PAGE;
  84. $mode = optional_param('mode', '', PARAM_ALPHA);
  85. $offset = optional_param('offset', 0, PARAM_INT);
  86. $output = $OUTPUT->box_start('responsefiles');
  87. $candelete = $this->can_manage_responsefiles();
  88. $strdelete = get_string('delete');
  89. $fs = get_file_storage();
  90. $browser = get_file_browser();
  91. if ($submission = $this->get_submission($userid)) {
  92. $renderer = $PAGE->get_renderer('mod_assignment');
  93. $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
  94. $output .= $OUTPUT->box_end();
  95. }
  96. if ($return) {
  97. return $output;
  98. }
  99. echo $output;
  100. }
  101. function can_manage_responsefiles() {
  102. if (has_capability('mod/assignment:grade', $this->context)) {
  103. return true;
  104. } else {
  105. return false;
  106. }
  107. }
  108. function view_upload_form() {
  109. global $OUTPUT, $USER;
  110. echo $OUTPUT->box_start('uploadbox');
  111. $fs = get_file_storage();
  112. // edit files in another page
  113. if ($submission = $this->get_submission($USER->id)) {
  114. if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
  115. $str = get_string('editthisfile', 'assignment');
  116. } else {
  117. $str = get_string('uploadafile', 'assignment');
  118. }
  119. } else {
  120. $str = get_string('uploadafile', 'assignment');
  121. }
  122. echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/uploadsingle/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
  123. echo $OUTPUT->box_end();
  124. }
  125. function upload($mform) {
  126. $action = required_param('action', PARAM_ALPHA);
  127. switch ($action) {
  128. case 'uploadresponse':
  129. $this->upload_responsefile($mform);
  130. break;
  131. case 'uploadfile':
  132. $this->upload_file($mform);
  133. }
  134. }
  135. function upload_file($mform) {
  136. global $CFG, $USER, $DB, $OUTPUT;
  137. $viewurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  138. if (!is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
  139. redirect($viewurl);
  140. }
  141. $submission = $this->get_submission($USER->id);
  142. $filecount = 0;
  143. if ($submission) {
  144. $filecount = $this->count_user_files($submission->id);
  145. }
  146. if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
  147. if ($submission = $this->get_submission($USER->id)) {
  148. //TODO: change later to ">= 0", to prevent resubmission when graded 0
  149. if (($submission->grade > 0) and !$this->assignment->resubmit) {
  150. redirect($viewurl, get_string('alreadygraded', 'assignment'));
  151. }
  152. }
  153. if ($formdata = $mform->get_data()) {
  154. $fs = get_file_storage();
  155. $submission = $this->get_submission($USER->id, true); //create new submission if needed
  156. $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
  157. if ($newfilename = $mform->get_new_filename('assignment_file')) {
  158. $file = $mform->save_stored_file('assignment_file', $this->context->id, 'mod_assignment', 'submission',
  159. $submission->id, '/', $newfilename);
  160. $updates = new stdClass(); //just enough data for updating the submission
  161. $updates->timemodified = time();
  162. $updates->numfiles = 1;
  163. $updates->id = $submission->id;
  164. $DB->update_record('assignment_submissions', $updates);
  165. add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
  166. $this->update_grade($submission);
  167. $this->email_teachers($submission);
  168. // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
  169. $eventdata = new stdClass();
  170. $eventdata->modulename = 'assignment';
  171. $eventdata->cmid = $this->cm->id;
  172. $eventdata->itemid = $submission->id;
  173. $eventdata->courseid = $this->course->id;
  174. $eventdata->userid = $USER->id;
  175. $eventdata->file = $file;
  176. events_trigger('assessable_file_uploaded', $eventdata);
  177. }
  178. redirect($viewurl, get_string('uploadedfile'));
  179. } else {
  180. redirect($viewurl, get_string('uploaderror', 'assignment')); //submitting not allowed!
  181. }
  182. }
  183. redirect($viewurl);
  184. }
  185. function upload_responsefile($mform) {
  186. global $CFG, $USER, $OUTPUT, $PAGE;
  187. $userid = required_param('userid', PARAM_INT);
  188. $mode = required_param('mode', PARAM_ALPHA);
  189. $offset = required_param('offset', PARAM_INT);
  190. $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
  191. if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
  192. $fs = get_file_storage();
  193. $submission = $this->get_submission($userid, true); //create new submission if needed
  194. $fs->delete_area_files($this->context->id, 'mod_assignment', 'response', $submission->id);
  195. if ($newfilename = $mform->get_new_filename('assignment_file')) {
  196. $file = $mform->save_stored_file('assignment_file', $this->context->id,
  197. 'mod_assignment', 'response',$submission->id, '/', $newfilename);
  198. }
  199. redirect($returnurl, get_string('uploadedfile'));
  200. } else {
  201. redirect($returnurl, get_string('uploaderror', 'assignment')); //submitting not allowed!
  202. }
  203. }
  204. function setup_elements(&$mform) {
  205. global $CFG, $COURSE;
  206. $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
  207. $mform->addElement('select', 'resubmit', get_string('allowresubmit', 'assignment'), $ynoptions);
  208. $mform->addHelpButton('resubmit', 'allowresubmit', 'assignment');
  209. $mform->setDefault('resubmit', 0);
  210. $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
  211. $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
  212. $mform->setDefault('emailteachers', 0);
  213. $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
  214. $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
  215. $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
  216. $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
  217. $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
  218. plagiarism_get_form_elements_module($mform, $course_context);
  219. }
  220. function portfolio_exportable() {
  221. return true;
  222. }
  223. function send_file($filearea, $args) {
  224. global $CFG, $DB, $USER;
  225. require_once($CFG->libdir.'/filelib.php');
  226. require_login($this->course, false, $this->cm);
  227. if ($filearea !== 'submission' && $filearea !== 'response') {
  228. return false;
  229. }
  230. $submissionid = (int)array_shift($args);
  231. if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
  232. return false;
  233. }
  234. if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
  235. return false;
  236. }
  237. $relativepath = implode('/', $args);
  238. $fullpath = '/'.$this->context->id.'/mod_assignment/'.$filearea.'/'.$submissionid.'/'.$relativepath;
  239. $fs = get_file_storage();
  240. if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  241. return false;
  242. }
  243. send_stored_file($file, 0, 0, true); // download MUST be forced - security!
  244. }
  245. function extend_settings_navigation($node) {
  246. global $CFG, $USER, $OUTPUT;
  247. // get users submission if there is one
  248. $submission = $this->get_submission();
  249. if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
  250. $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
  251. } else {
  252. $editable = false;
  253. }
  254. // If the user has submitted something add some related links and data
  255. if (isset($submission->numfiles) AND $submission->numfiles) {
  256. // Add a view link to the settings nav
  257. $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  258. $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
  259. if (!empty($submission->timemodified)) {
  260. $submissionnode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
  261. $submissionnode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submissionnode->text);
  262. $submissionnode->add_class('note');
  263. if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
  264. $submissionnode->add_class('early');
  265. } else {
  266. $submissionnode->add_class('late');
  267. }
  268. }
  269. }
  270. // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
  271. if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
  272. $fs = get_file_storage();
  273. if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
  274. $filenode = $node->add(get_string('submission', 'assignment'));
  275. foreach ($files as $file) {
  276. $filename = $file->get_filename();
  277. $mimetype = $file->get_mimetype();
  278. $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment', 'submission/'.$submission->id.'/'.$filename);
  279. $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype), ''));
  280. }
  281. }
  282. }
  283. }
  284. /**
  285. * creates a zip of all assignment submissions and sends a zip to the browser
  286. */
  287. function download_submissions() {
  288. global $CFG,$DB;
  289. require_once($CFG->libdir.'/filelib.php');
  290. $submissions = $this->get_submissions('','');
  291. if (empty($submissions)) {
  292. print_error('errornosubmissions', 'assignment');
  293. }
  294. $filesforzipping = array();
  295. $fs = get_file_storage();
  296. $groupmode = groups_get_activity_groupmode($this->cm);
  297. $groupid = 0; // All users
  298. $groupname = '';
  299. if ($groupmode) {
  300. $groupid = groups_get_activity_group($this->cm, true);
  301. $groupname = groups_get_group_name($groupid).'-';
  302. }
  303. $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
  304. foreach ($submissions as $submission) {
  305. $a_userid = $submission->userid; //get userid
  306. if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
  307. $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
  308. $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
  309. $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
  310. foreach ($files as $file) {
  311. //get files new name.
  312. $fileext = strstr($file->get_filename(), '.');
  313. $fileoriginal = str_replace($fileext, '', $file->get_filename());
  314. $fileforzipname = clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
  315. //save file name to array for zipping.
  316. $filesforzipping[$fileforzipname] = $file;
  317. }
  318. }
  319. } // End of foreach
  320. if ($zipfile = assignment_pack_files($filesforzipping)) {
  321. send_temp_file($zipfile, $filename); //send file and delete after sending.
  322. }
  323. }
  324. }
  325. class mod_assignment_uploadsingle_response_form extends moodleform {
  326. function definition() {
  327. $mform = $this->_form;
  328. $instance = $this->_customdata;
  329. // visible elements
  330. $mform->addElement('filepicker', 'assignment_file', get_string('uploadafile'), null, $instance->options);
  331. // hidden params
  332. $mform->addElement('hidden', 'id', $instance->cm->id);
  333. $mform->setType('id', PARAM_INT);
  334. $mform->addElement('hidden', 'contextid', $instance->contextid);
  335. $mform->setType('contextid', PARAM_INT);
  336. $mform->addElement('hidden', 'action', 'uploadresponse');
  337. $mform->setType('action', PARAM_ALPHA);
  338. $mform->addElement('hidden', 'mode', $instance->mode);
  339. $mform->setType('mode', PARAM_ALPHA);
  340. $mform->addElement('hidden', 'offset', $instance->offset);
  341. $mform->setType('offset', PARAM_INT);
  342. $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
  343. $mform->setType('forcerefresh', PARAM_INT);
  344. $mform->addElement('hidden', 'userid', $instance->userid);
  345. $mform->setType('userid', PARAM_INT);
  346. // buttons
  347. $this->add_action_buttons(false, get_string('uploadthisfile'));
  348. }
  349. }