PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/assignment/type/upload/assignment.class.php

http://github.com/moodle/moodle
PHP | 1210 lines | 915 code | 216 blank | 79 comment | 166 complexity | c10c43859157509026eb5c16afbdcf4a 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. * Assignment upload type implementation
  18. *
  19. * @package mod-assignment
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. require_once(dirname(__FILE__).'/upload_form.php');
  23. require_once($CFG->libdir . '/portfoliolib.php');
  24. require_once($CFG->dirroot . '/mod/assignment/lib.php');
  25. define('ASSIGNMENT_STATUS_SUBMITTED', 'submitted'); // student thinks it is finished
  26. define('ASSIGNMENT_STATUS_CLOSED', 'closed'); // teacher prevents more submissions
  27. /**
  28. * Extend the base assignment class for assignments where you upload a single file
  29. *
  30. */
  31. class assignment_upload extends assignment_base {
  32. function assignment_upload($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
  33. parent::assignment_base($cmid, $assignment, $cm, $course);
  34. $this->type = 'upload';
  35. }
  36. function view() {
  37. global $USER, $OUTPUT;
  38. require_capability('mod/assignment:view', $this->context);
  39. $cansubmit = has_capability('mod/assignment:submit', $this->context);
  40. add_to_log($this->course->id, 'assignment', 'view', "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
  41. $this->view_header();
  42. if ($this->assignment->timeavailable > time()
  43. and !has_capability('mod/assignment:grade', $this->context) // grading user can see it anytime
  44. and $this->assignment->var3) { // force hiding before available date
  45. echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
  46. print_string('notavailableyet', 'assignment');
  47. echo $OUTPUT->box_end();
  48. } else {
  49. $this->view_intro();
  50. }
  51. $this->view_dates();
  52. if (is_enrolled($this->context, $USER)) {
  53. if ($submission = $this->get_submission($USER->id)) {
  54. $filecount = $this->count_user_files($submission->id);
  55. } else {
  56. $filecount = 0;
  57. }
  58. if ($cansubmit or !empty($filecount)) { //if a user has submitted files using a previous role we should still show the files
  59. $this->view_feedback();
  60. if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
  61. echo $OUTPUT->heading(get_string('submission', 'assignment'), 3);
  62. } else {
  63. echo $OUTPUT->heading(get_string('submissiondraft', 'assignment'), 3);
  64. }
  65. if ($filecount and $submission) {
  66. echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter', 'userfiles');
  67. } else {
  68. if (!$this->isopen() or $this->is_finalized($submission)) {
  69. echo $OUTPUT->box(get_string('nofiles', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
  70. } else {
  71. echo $OUTPUT->box(get_string('nofilesyet', 'assignment'), 'generalbox boxaligncenter nofiles', 'userfiles');
  72. }
  73. }
  74. $this->view_upload_form();
  75. if ($this->notes_allowed()) {
  76. echo $OUTPUT->heading(get_string('notes', 'assignment'), 3);
  77. $this->view_notes();
  78. }
  79. $this->view_final_submission();
  80. }
  81. }
  82. $this->view_footer();
  83. }
  84. function view_feedback($submission=NULL) {
  85. global $USER, $CFG, $DB, $OUTPUT;
  86. require_once($CFG->libdir.'/gradelib.php');
  87. if (!$submission) { /// Get submission for this assignment
  88. $submission = $this->get_submission($USER->id);
  89. }
  90. if (empty($submission->timemarked)) { /// Nothing to show, so print nothing
  91. return;
  92. }
  93. $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, $USER->id);
  94. $item = $grading_info->items[0];
  95. $grade = $item->grades[$USER->id];
  96. if ($grade->hidden or $grade->grade === false) { // hidden or error
  97. return;
  98. }
  99. if ($grade->grade === null and empty($grade->str_feedback)) { // No grade to show yet
  100. if ($this->count_responsefiles($USER->id)) { // but possibly response files are present
  101. echo $OUTPUT->heading(get_string('responsefiles', 'assignment'), 3);
  102. $responsefiles = $this->print_responsefiles($USER->id, true);
  103. echo $OUTPUT->box($responsefiles, 'generalbox boxaligncenter');
  104. }
  105. return;
  106. }
  107. $graded_date = $grade->dategraded;
  108. $graded_by = $grade->usermodified;
  109. /// We need the teacher info
  110. if (!$teacher = $DB->get_record('user', array('id'=>$graded_by))) {
  111. print_error('cannotfindteacher');
  112. }
  113. /// Print the feedback
  114. echo $OUTPUT->heading(get_string('submissionfeedback', 'assignment'), 3);
  115. echo '<table cellspacing="0" class="feedback">';
  116. echo '<tr>';
  117. echo '<td class="left picture">';
  118. echo $OUTPUT->user_picture($teacher);
  119. echo '</td>';
  120. echo '<td class="topic">';
  121. echo '<div class="from">';
  122. echo '<div class="fullname">'.fullname($teacher).'</div>';
  123. echo '<div class="time">'.userdate($graded_date).'</div>';
  124. echo '</div>';
  125. echo '</td>';
  126. echo '</tr>';
  127. echo '<tr>';
  128. echo '<td class="left side">&nbsp;</td>';
  129. echo '<td class="content">';
  130. if ($this->assignment->grade) {
  131. echo '<div class="grade">';
  132. echo get_string("grade").': '.$grade->str_long_grade;
  133. echo '</div>';
  134. echo '<div class="clearer"></div>';
  135. }
  136. echo '<div class="comment">';
  137. echo $grade->str_feedback;
  138. echo '</div>';
  139. echo '</tr>';
  140. echo '<tr>';
  141. echo '<td class="left side">&nbsp;</td>';
  142. echo '<td class="content">';
  143. echo $this->print_responsefiles($USER->id, true);
  144. echo '</tr>';
  145. echo '</table>';
  146. }
  147. function view_upload_form() {
  148. global $CFG, $USER, $OUTPUT;
  149. $submission = $this->get_submission($USER->id);
  150. if ($this->is_finalized($submission)) {
  151. // no uploading
  152. return;
  153. }
  154. if ($this->can_upload_file($submission)) {
  155. $fs = get_file_storage();
  156. // edit files in another page
  157. if ($submission) {
  158. if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
  159. $str = get_string('editthesefiles', 'assignment');
  160. } else {
  161. $str = get_string('uploadfiles', 'assignment');
  162. }
  163. } else {
  164. $str = get_string('uploadfiles', 'assignment');
  165. }
  166. echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
  167. }
  168. }
  169. function view_notes() {
  170. global $USER, $OUTPUT;
  171. if ($submission = $this->get_submission($USER->id)
  172. and !empty($submission->data1)) {
  173. echo $OUTPUT->box(format_text($submission->data1, FORMAT_HTML, array('overflowdiv'=>true)), 'generalbox boxaligncenter boxwidthwide');
  174. } else {
  175. echo $OUTPUT->box(get_string('notesempty', 'assignment'), 'generalbox boxaligncenter');
  176. }
  177. if ($this->can_update_notes($submission)) {
  178. $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
  179. echo '<div style="text-align:center">';
  180. echo $OUTPUT->single_button(new moodle_url('upload.php', $options), get_string('edit'));
  181. echo '</div>';
  182. }
  183. }
  184. function view_final_submission() {
  185. global $CFG, $USER, $OUTPUT;
  186. $submission = $this->get_submission($USER->id);
  187. if ($this->isopen() and $this->can_finalize($submission)) {
  188. //print final submit button
  189. echo $OUTPUT->heading(get_string('submitformarking','assignment'), 3);
  190. echo '<div style="text-align:center">';
  191. echo '<form method="post" action="upload.php">';
  192. echo '<fieldset class="invisiblefieldset">';
  193. echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
  194. echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
  195. echo '<input type="hidden" name="action" value="finalize" />';
  196. echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
  197. echo '</fieldset>';
  198. echo '</form>';
  199. echo '</div>';
  200. } else if (!$this->isopen()) {
  201. echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
  202. } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
  203. if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
  204. echo $OUTPUT->heading(get_string('submitedformarking','assignment'), 3);
  205. } else {
  206. echo $OUTPUT->heading(get_string('nomoresubmissions','assignment'), 3);
  207. }
  208. } else {
  209. //no submission yet
  210. }
  211. }
  212. /**
  213. * Return true if var3 == hide description till available day
  214. *
  215. *@return boolean
  216. */
  217. function description_is_hidden() {
  218. return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
  219. }
  220. function print_student_answer($userid, $return=false){
  221. global $CFG, $OUTPUT, $PAGE;
  222. $submission = $this->get_submission($userid);
  223. $output = '';
  224. if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
  225. $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
  226. }
  227. if ($this->notes_allowed() and !empty($submission->data1)) {
  228. $link = new moodle_url("/mod/assignment/type/upload/notes.php", array('id'=>$this->cm->id, 'userid'=>$userid));
  229. $action = new popup_action('click', $link, 'notes', array('height' => 500, 'width' => 780));
  230. $output .= $OUTPUT->action_link($link, get_string('notes', 'assignment'), $action, array('title'=>get_string('notes', 'assignment')));
  231. $output .= '&nbsp;';
  232. }
  233. $renderer = $PAGE->get_renderer('mod_assignment');
  234. $output = $OUTPUT->box_start('files').$output;
  235. $output .= $renderer->assignment_files($this->context, $submission->id);
  236. $output .= $OUTPUT->box_end();
  237. return $output;
  238. }
  239. /**
  240. * Produces a list of links to the files uploaded by a user
  241. *
  242. * @param $userid int optional id of the user. If 0 then $USER->id is used.
  243. * @param $return boolean optional defaults to false. If true the list is returned rather than printed
  244. * @return string optional
  245. */
  246. function print_user_files($userid=0, $return=false) {
  247. global $CFG, $USER, $OUTPUT, $PAGE;
  248. $mode = optional_param('mode', '', PARAM_ALPHA);
  249. $offset = optional_param('offset', 0, PARAM_INT);
  250. if (!$userid) {
  251. if (!isloggedin()) {
  252. return '';
  253. }
  254. $userid = $USER->id;
  255. }
  256. $output = $OUTPUT->box_start('files');
  257. $submission = $this->get_submission($userid);
  258. // only during grading
  259. if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) {
  260. $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
  261. }
  262. if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
  263. $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
  264. $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
  265. }
  266. if ($this->drafts_tracked() and $this->isopen() and has_capability('mod/assignment:grade', $this->context) and $mode != '') { // we do not want it on view.php page
  267. if ($this->can_unfinalize($submission)) {
  268. //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
  269. $output .= '<br /><input type="submit" name="unfinalize" value="'.get_string('unfinalize', 'assignment').'" />';
  270. $output .= $OUTPUT->help_icon('unfinalize', 'assignment');
  271. } else if ($this->can_finalize($submission)) {
  272. //$options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
  273. $output .= '<br /><input type="submit" name="finalize" value="'.get_string('finalize', 'assignment').'" />';
  274. }
  275. }
  276. if ($submission) {
  277. $renderer = $PAGE->get_renderer('mod_assignment');
  278. $output .= $renderer->assignment_files($this->context, $submission->id);
  279. }
  280. $output .= $OUTPUT->box_end();
  281. if ($return) {
  282. return $output;
  283. }
  284. echo $output;
  285. }
  286. function submissions($mode) {
  287. // redirects out of form to process (un)finalizing.
  288. $unfinalize = optional_param('unfinalize', FALSE, PARAM_TEXT);
  289. $finalize = optional_param('finalize', FALSE, PARAM_TEXT);
  290. if ($unfinalize) {
  291. $this->unfinalize('single');
  292. } else if ($finalize) {
  293. $this->finalize('single');
  294. }
  295. if ($unfinalize || $finalize) {
  296. $mode = 'singlenosave';
  297. }
  298. parent::submissions($mode);
  299. }
  300. function process_feedback() {
  301. if (!$feedback = data_submitted() or !confirm_sesskey()) { // No incoming data?
  302. return false;
  303. }
  304. $userid = required_param('userid', PARAM_INT);
  305. $offset = required_param('offset', PARAM_INT);
  306. $mform = $this->display_submission($offset, $userid, false);
  307. parent::process_feedback($mform);
  308. }
  309. function print_responsefiles($userid, $return=false) {
  310. global $CFG, $USER, $OUTPUT, $PAGE;
  311. $mode = optional_param('mode', '', PARAM_ALPHA);
  312. $offset = optional_param('offset', 0, PARAM_INT);
  313. $output = $OUTPUT->box_start('responsefiles');
  314. $candelete = $this->can_manage_responsefiles();
  315. $strdelete = get_string('delete');
  316. $fs = get_file_storage();
  317. $browser = get_file_browser();
  318. if ($submission = $this->get_submission($userid)) {
  319. $renderer = $PAGE->get_renderer('mod_assignment');
  320. $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
  321. }
  322. $output .= $OUTPUT->box_end();
  323. if ($return) {
  324. return $output;
  325. }
  326. echo $output;
  327. }
  328. /**
  329. * Upload files
  330. * upload_file function requires moodle form instance and file manager options
  331. * @param object $mform
  332. * @param array $options
  333. */
  334. function upload($mform = null, $filemanager_options = null) {
  335. $action = required_param('action', PARAM_ALPHA);
  336. switch ($action) {
  337. case 'finalize':
  338. $this->finalize();
  339. break;
  340. case 'finalizeclose':
  341. $this->finalizeclose();
  342. break;
  343. case 'unfinalize':
  344. $this->unfinalize();
  345. break;
  346. case 'uploadresponse':
  347. $this->upload_responsefile($mform, $filemanager_options);
  348. break;
  349. case 'uploadfile':
  350. $this->upload_file($mform, $filemanager_options);
  351. case 'savenotes':
  352. case 'editnotes':
  353. $this->upload_notes();
  354. default:
  355. print_error('unknowuploadaction', '', '', $action);
  356. }
  357. }
  358. function upload_notes() {
  359. global $CFG, $USER, $OUTPUT, $DB;
  360. $action = required_param('action', PARAM_ALPHA);
  361. $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  362. $mform = new mod_assignment_upload_notes_form();
  363. $defaults = new stdClass();
  364. $defaults->id = $this->cm->id;
  365. if ($submission = $this->get_submission($USER->id)) {
  366. $defaults->text = clean_text($submission->data1);
  367. } else {
  368. $defaults->text = '';
  369. }
  370. $mform->set_data($defaults);
  371. if ($mform->is_cancelled()) {
  372. $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  373. redirect($returnurl);
  374. }
  375. if (!$this->can_update_notes($submission)) {
  376. $this->view_header(get_string('upload'));
  377. echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
  378. echo $OUTPUT->continue_button($returnurl);
  379. $this->view_footer();
  380. die;
  381. }
  382. if ($data = $mform->get_data() and $action == 'savenotes') {
  383. $submission = $this->get_submission($USER->id, true); // get or create submission
  384. $updated = new stdClass();
  385. $updated->id = $submission->id;
  386. $updated->timemodified = time();
  387. $updated->data1 = $data->text;
  388. $DB->update_record('assignment_submissions', $updated);
  389. add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
  390. redirect($returnurl);
  391. $submission = $this->get_submission($USER->id);
  392. $this->update_grade($submission);
  393. }
  394. /// show notes edit form
  395. $this->view_header(get_string('notes', 'assignment'));
  396. echo $OUTPUT->heading(get_string('notes', 'assignment'));
  397. $mform->display();
  398. $this->view_footer();
  399. die;
  400. }
  401. function upload_responsefile($mform, $options) {
  402. global $CFG, $USER, $OUTPUT, $PAGE;
  403. $userid = required_param('userid', PARAM_INT);
  404. $mode = required_param('mode', PARAM_ALPHA);
  405. $offset = required_param('offset', PARAM_INT);
  406. $returnurl = new moodle_url("submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
  407. if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
  408. $fs = get_file_storage();
  409. $submission = $this->get_submission($userid, true, true);
  410. if ($formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'response', $submission->id)) {
  411. $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$formdata->userid,'mode'=>$formdata->mode,'offset'=>$formdata->offset));
  412. redirect($returnurl->out(false));
  413. }
  414. }
  415. $PAGE->set_title(get_string('upload'));
  416. echo $OUTPUT->header();
  417. echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
  418. echo $OUTPUT->continue_button($returnurl->out(true));
  419. echo $OUTPUT->footer();
  420. die;
  421. }
  422. function upload_file($mform, $options) {
  423. global $CFG, $USER, $DB, $OUTPUT;
  424. $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  425. $submission = $this->get_submission($USER->id);
  426. if (!$this->can_upload_file($submission)) {
  427. $this->view_header(get_string('upload'));
  428. echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
  429. echo $OUTPUT->continue_button($returnurl);
  430. $this->view_footer();
  431. die;
  432. }
  433. if ($formdata = $mform->get_data()) {
  434. $fs = get_file_storage();
  435. $submission = $this->get_submission($USER->id, true); //create new submission if needed
  436. $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
  437. $formdata = file_postupdate_standard_filemanager($formdata, 'files', $options, $this->context, 'mod_assignment', 'submission', $submission->id);
  438. $updates = new stdClass();
  439. $updates->id = $submission->id;
  440. $updates->timemodified = time();
  441. $DB->update_record('assignment_submissions', $updates);
  442. add_to_log($this->course->id, 'assignment', 'upload',
  443. 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
  444. $this->update_grade($submission);
  445. if (!$this->drafts_tracked()) {
  446. $this->email_teachers($submission);
  447. }
  448. // send files to event system
  449. $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
  450. // Let Moodle know that assessable files were uploaded (eg for plagiarism detection)
  451. $eventdata = new stdClass();
  452. $eventdata->modulename = 'assignment';
  453. $eventdata->cmid = $this->cm->id;
  454. $eventdata->itemid = $submission->id;
  455. $eventdata->courseid = $this->course->id;
  456. $eventdata->userid = $USER->id;
  457. if ($files) {
  458. $eventdata->files = $files;
  459. }
  460. events_trigger('assessable_file_uploaded', $eventdata);
  461. $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  462. redirect($returnurl);
  463. }
  464. $this->view_header(get_string('upload'));
  465. echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
  466. echo $OUTPUT->continue_button($returnurl);
  467. $this->view_footer();
  468. die;
  469. }
  470. function send_file($filearea, $args) {
  471. global $CFG, $DB, $USER;
  472. require_once($CFG->libdir.'/filelib.php');
  473. require_login($this->course, false, $this->cm);
  474. if ($filearea === 'submission') {
  475. $submissionid = (int)array_shift($args);
  476. if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
  477. return false;
  478. }
  479. if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
  480. return false;
  481. }
  482. $relativepath = implode('/', $args);
  483. $fullpath = "/{$this->context->id}/mod_assignment/submission/$submission->id/$relativepath";
  484. $fs = get_file_storage();
  485. if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  486. return false;
  487. }
  488. send_stored_file($file, 0, 0, true); // download MUST be forced - security!
  489. } else if ($filearea === 'response') {
  490. $submissionid = (int)array_shift($args);
  491. if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
  492. return false;
  493. }
  494. if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
  495. return false;
  496. }
  497. $relativepath = implode('/', $args);
  498. $fullpath = "/{$this->context->id}/mod_assignment/response/$submission->id/$relativepath";
  499. $fs = get_file_storage();
  500. if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  501. return false;
  502. }
  503. send_stored_file($file, 0, 0, true);
  504. }
  505. return false;
  506. }
  507. function finalize($forcemode=null) {
  508. global $USER, $DB, $OUTPUT;
  509. $userid = optional_param('userid', $USER->id, PARAM_INT);
  510. $offset = optional_param('offset', 0, PARAM_INT);
  511. $confirm = optional_param('confirm', 0, PARAM_BOOL);
  512. $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  513. $submission = $this->get_submission($userid);
  514. if ($forcemode!=null) {
  515. $returnurl = new moodle_url('/mod/assignment/submissions.php',
  516. array('id'=>$this->cm->id,
  517. 'userid'=>$userid,
  518. 'mode'=>$forcemode,
  519. 'offset'=>$offset
  520. ));
  521. }
  522. if (!$this->can_finalize($submission)) {
  523. redirect($returnurl->out(false)); // probably already graded, redirect to assignment page, the reason should be obvious
  524. }
  525. if ($forcemode==null) {
  526. if (!data_submitted() or !$confirm or !confirm_sesskey()) {
  527. $optionsno = array('id'=>$this->cm->id);
  528. $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize', 'sesskey'=>sesskey());
  529. $this->view_header(get_string('submitformarking', 'assignment'));
  530. echo $OUTPUT->heading(get_string('submitformarking', 'assignment'));
  531. echo $OUTPUT->confirm(get_string('onceassignmentsent', 'assignment'), new moodle_url('upload.php', $optionsyes),new moodle_url( 'view.php', $optionsno));
  532. $this->view_footer();
  533. die;
  534. }
  535. }
  536. $updated = new stdClass();
  537. $updated->id = $submission->id;
  538. $updated->data2 = ASSIGNMENT_STATUS_SUBMITTED;
  539. $updated->timemodified = time();
  540. $DB->update_record('assignment_submissions', $updated);
  541. add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
  542. 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
  543. $submission = $this->get_submission($userid);
  544. $this->update_grade($submission);
  545. $this->email_teachers($submission);
  546. // Trigger assessable_files_done event to show files are complete
  547. $eventdata = new stdClass();
  548. $eventdata->modulename = 'assignment';
  549. $eventdata->cmid = $this->cm->id;
  550. $eventdata->itemid = $submission->id;
  551. $eventdata->courseid = $this->course->id;
  552. $eventdata->userid = $userid;
  553. events_trigger('assessable_files_done', $eventdata);
  554. if ($forcemode==null) {
  555. redirect($returnurl->out(false));
  556. }
  557. }
  558. function finalizeclose() {
  559. global $DB;
  560. $userid = optional_param('userid', 0, PARAM_INT);
  561. $mode = required_param('mode', PARAM_ALPHA);
  562. $offset = required_param('offset', PARAM_INT);
  563. $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1));
  564. // create but do not add student submission date
  565. $submission = $this->get_submission($userid, true, true);
  566. if (!data_submitted() or !$this->can_finalize($submission) or !confirm_sesskey()) {
  567. redirect($returnurl); // probably closed already
  568. }
  569. $updated = new stdClass();
  570. $updated->id = $submission->id;
  571. $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
  572. $DB->update_record('assignment_submissions', $updated);
  573. add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
  574. 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
  575. $submission = $this->get_submission($userid, false, true);
  576. $this->update_grade($submission);
  577. redirect($returnurl);
  578. }
  579. function unfinalize($forcemode=null) {
  580. global $DB;
  581. $userid = required_param('userid', PARAM_INT);
  582. $mode = required_param('mode', PARAM_ALPHA);
  583. $offset = required_param('offset', PARAM_INT);
  584. if ($forcemode!=null) {
  585. $mode=$forcemode;
  586. }
  587. $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset, 'forcerefresh'=>1) );
  588. if (data_submitted()
  589. and $submission = $this->get_submission($userid)
  590. and $this->can_unfinalize($submission)
  591. and confirm_sesskey()) {
  592. $updated = new stdClass();
  593. $updated->id = $submission->id;
  594. $updated->data2 = '';
  595. $DB->update_record('assignment_submissions', $updated);
  596. //TODO: add unfinalize action to log
  597. add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->cm->id.'&userid='.$userid.'&mode='.$mode.'&offset='.$offset, $this->assignment->id, $this->cm->id);
  598. $submission = $this->get_submission($userid);
  599. $this->update_grade($submission);
  600. }
  601. if ($forcemode==null) {
  602. redirect($returnurl);
  603. }
  604. }
  605. function delete() {
  606. $action = optional_param('action', '', PARAM_ALPHA);
  607. switch ($action) {
  608. case 'response':
  609. $this->delete_responsefile();
  610. break;
  611. default:
  612. $this->delete_file();
  613. }
  614. die;
  615. }
  616. function delete_responsefile() {
  617. global $CFG, $OUTPUT,$PAGE;
  618. $file = required_param('file', PARAM_FILE);
  619. $userid = required_param('userid', PARAM_INT);
  620. $mode = required_param('mode', PARAM_ALPHA);
  621. $offset = required_param('offset', PARAM_INT);
  622. $confirm = optional_param('confirm', 0, PARAM_BOOL);
  623. $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'userid'=>$userid, 'mode'=>$mode, 'offset'=>$offset));
  624. if (!$this->can_manage_responsefiles()) {
  625. redirect($returnurl);
  626. }
  627. $urlreturn = 'submissions.php';
  628. $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
  629. if (!data_submitted() or !$confirm or !confirm_sesskey()) {
  630. $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
  631. $PAGE->set_title(get_string('delete'));
  632. echo $OUTPUT->header();
  633. echo $OUTPUT->heading(get_string('delete'));
  634. echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
  635. echo $OUTPUT->footer();
  636. die;
  637. }
  638. if ($submission = $this->get_submission($userid)) {
  639. $fs = get_file_storage();
  640. if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'response', $submission->id, '/', $file)) {
  641. $file->delete();
  642. }
  643. }
  644. redirect($returnurl);
  645. }
  646. function delete_file() {
  647. global $CFG, $DB, $OUTPUT, $PAGE;
  648. $file = required_param('file', PARAM_FILE);
  649. $userid = required_param('userid', PARAM_INT);
  650. $confirm = optional_param('confirm', 0, PARAM_BOOL);
  651. $mode = optional_param('mode', '', PARAM_ALPHA);
  652. $offset = optional_param('offset', 0, PARAM_INT);
  653. require_login($this->course->id, false, $this->cm);
  654. if (empty($mode)) {
  655. $urlreturn = 'view.php';
  656. $optionsreturn = array('id'=>$this->cm->id);
  657. $returnurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  658. } else {
  659. $urlreturn = 'submissions.php';
  660. $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
  661. $returnurl = new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id, 'offset'=>$offset, 'userid'=>$userid));
  662. }
  663. if (!$submission = $this->get_submission($userid) // incorrect submission
  664. or !$this->can_delete_files($submission)) { // can not delete
  665. $this->view_header(get_string('delete'));
  666. echo $OUTPUT->notification(get_string('cannotdeletefiles', 'assignment'));
  667. echo $OUTPUT->continue_button($returnurl);
  668. $this->view_footer();
  669. die;
  670. }
  671. if (!data_submitted() or !$confirm or !confirm_sesskey()) {
  672. $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
  673. if (empty($mode)) {
  674. $this->view_header(get_string('delete'));
  675. } else {
  676. $PAGE->set_title(get_string('delete'));
  677. echo $OUTPUT->header();
  678. }
  679. echo $OUTPUT->heading(get_string('delete'));
  680. echo $OUTPUT->confirm(get_string('confirmdeletefile', 'assignment', $file), new moodle_url('delete.php', $optionsyes), new moodle_url($urlreturn, $optionsreturn));
  681. if (empty($mode)) {
  682. $this->view_footer();
  683. } else {
  684. echo $OUTPUT->footer();
  685. }
  686. die;
  687. }
  688. $fs = get_file_storage();
  689. if ($file = $fs->get_file($this->context->id, 'mod_assignment', 'submission', $submission->id, '/', $file)) {
  690. $file->delete();
  691. $submission->timemodified = time();
  692. $DB->update_record('assignment_submissions', $submission);
  693. add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
  694. 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
  695. $this->update_grade($submission);
  696. }
  697. redirect($returnurl);
  698. }
  699. function can_upload_file($submission) {
  700. global $USER;
  701. if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
  702. and $this->isopen() // assignment not closed yet
  703. and (empty($submission) or ($submission->userid == $USER->id)) // his/her own submission
  704. and !$this->is_finalized($submission)) { // no uploading after final submission
  705. return true;
  706. } else {
  707. return false;
  708. }
  709. }
  710. function can_manage_responsefiles() {
  711. if (has_capability('mod/assignment:grade', $this->context)) {
  712. return true;
  713. } else {
  714. return false;
  715. }
  716. }
  717. function can_delete_files($submission) {
  718. global $USER;
  719. if (has_capability('mod/assignment:grade', $this->context)) {
  720. return true;
  721. }
  722. if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
  723. and $this->isopen() // assignment not closed yet
  724. and $this->assignment->resubmit // deleting allowed
  725. and $USER->id == $submission->userid // his/her own submission
  726. and !$this->is_finalized($submission)) { // no deleting after final submission
  727. return true;
  728. } else {
  729. return false;
  730. }
  731. }
  732. function drafts_tracked() {
  733. return !empty($this->assignment->var4);
  734. }
  735. /**
  736. * Returns submission status
  737. * @param object $submission - may be empty
  738. * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
  739. */
  740. function is_finalized($submission) {
  741. if (!$this->drafts_tracked()) {
  742. return '';
  743. } else if (empty($submission)) {
  744. return '';
  745. } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
  746. return $submission->data2;
  747. } else {
  748. return '';
  749. }
  750. }
  751. function can_unfinalize($submission) {
  752. if(is_bool($submission)) {
  753. return false;
  754. }
  755. if (!$this->drafts_tracked()) {
  756. return false;
  757. }
  758. if (has_capability('mod/assignment:grade', $this->context)
  759. and $this->isopen()
  760. and $this->is_finalized($submission)) {
  761. return true;
  762. } else {
  763. return false;
  764. }
  765. }
  766. function can_finalize($submission) {
  767. global $USER;
  768. if(is_bool($submission)) {
  769. return false;
  770. }
  771. if (!$this->drafts_tracked()) {
  772. return false;
  773. }
  774. if ($this->is_finalized($submission)) {
  775. return false;
  776. }
  777. if (has_capability('mod/assignment:grade', $this->context)) {
  778. return true;
  779. } else if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
  780. and $this->isopen() // assignment not closed yet
  781. and !empty($submission) // submission must exist
  782. and $submission->userid == $USER->id // his/her own submission
  783. and ($this->count_user_files($submission->id)
  784. or ($this->notes_allowed() and !empty($submission->data1)))) { // something must be submitted
  785. return true;
  786. } else {
  787. return false;
  788. }
  789. }
  790. function can_update_notes($submission) {
  791. global $USER;
  792. if (is_enrolled($this->context, $USER, 'mod/assignment:submit')
  793. and $this->notes_allowed() // notesd must be allowed
  794. and $this->isopen() // assignment not closed yet
  795. and (empty($submission) or $USER->id == $submission->userid) // his/her own submission
  796. and !$this->is_finalized($submission)) { // no updateingafter final submission
  797. return true;
  798. } else {
  799. return false;
  800. }
  801. }
  802. function notes_allowed() {
  803. return (boolean)$this->assignment->var2;
  804. }
  805. function count_responsefiles($userid) {
  806. if ($submission = $this->get_submission($userid)) {
  807. $fs = get_file_storage();
  808. $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'response', $submission->id, "id", false);
  809. return count($files);
  810. } else {
  811. return 0;
  812. }
  813. }
  814. function setup_elements(&$mform) {
  815. global $CFG, $COURSE;
  816. $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
  817. $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
  818. $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
  819. $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
  820. $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
  821. $mform->addElement('select', 'resubmit', get_string('allowdeleting', 'assignment'), $ynoptions);
  822. $mform->addHelpButton('resubmit', 'allowdeleting', 'assignment');
  823. $mform->setDefault('resubmit', 1);
  824. $options = array();
  825. for($i = 1; $i <= 20; $i++) {
  826. $options[$i] = $i;
  827. }
  828. $mform->addElement('select', 'var1', get_string('allowmaxfiles', 'assignment'), $options);
  829. $mform->addHelpButton('var1', 'allowmaxfiles', 'assignment');
  830. $mform->setDefault('var1', 3);
  831. $mform->addElement('select', 'var2', get_string('allownotes', 'assignment'), $ynoptions);
  832. $mform->addHelpButton('var2', 'allownotes', 'assignment');
  833. $mform->setDefault('var2', 0);
  834. $mform->addElement('select', 'var3', get_string('hideintro', 'assignment'), $ynoptions);
  835. $mform->addHelpButton('var3', 'hideintro', 'assignment');
  836. $mform->setDefault('var3', 0);
  837. $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
  838. $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
  839. $mform->setDefault('emailteachers', 0);
  840. $mform->addElement('select', 'var4', get_string('trackdrafts', 'assignment'), $ynoptions);
  841. $mform->addHelpButton('var4', 'trackdrafts', 'assignment');
  842. $mform->setDefault('var4', 1);
  843. $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
  844. plagiarism_get_form_elements_module($mform, $course_context);
  845. }
  846. function portfolio_exportable() {
  847. return true;
  848. }
  849. function extend_settings_navigation($node) {
  850. global $CFG, $USER, $OUTPUT;
  851. // get users submission if there is one
  852. $submission = $this->get_submission();
  853. if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
  854. $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
  855. } else {
  856. $editable = false;
  857. }
  858. // If the user has submitted something add a bit more stuff
  859. if ($submission) {
  860. // Add a view link to the settings nav
  861. $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  862. $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
  863. if (!empty($submission->timemodified)) {
  864. $submittednode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
  865. $submittednode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submittednode->text);
  866. $submittednode->add_class('note');
  867. if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
  868. $submittednode->add_class('early');
  869. } else {
  870. $submittednode->add_class('late');
  871. }
  872. }
  873. }
  874. // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
  875. if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
  876. $fs = get_file_storage();
  877. if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
  878. if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
  879. $filenode = $node->add(get_string('submission', 'assignment'));
  880. } else {
  881. $filenode = $node->add(get_string('submissiondraft', 'assignment'));
  882. }
  883. foreach ($files as $file) {
  884. $filename = $file->get_filename();
  885. $mimetype = $file->get_mimetype();
  886. $link = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
  887. $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_mimetype_icon($mimetype),''));
  888. }
  889. }
  890. }
  891. // Show a notes link if they are enabled
  892. if ($this->notes_allowed()) {
  893. $link = new moodle_url('/mod/assignment/upload.php', array('id'=>$this->cm->id, 'action'=>'editnotes', 'sesskey'=>sesskey()));
  894. $node->add(get_string('notes', 'assignment'), $link);
  895. }
  896. }
  897. /**
  898. * creates a zip of all assignment submissions and sends a zip to the browser
  899. */
  900. public function download_submissions() {
  901. global $CFG,$DB;
  902. require_once($CFG->libdir.'/filelib.php');
  903. $submissions = $this->get_submissions('','');
  904. if (empty($submissions)) {
  905. print_error('errornosubmissions', 'assignment');
  906. }
  907. $filesforzipping = array();
  908. $fs = get_file_storage();
  909. $groupmode = groups_get_activity_groupmode($this->cm);
  910. $groupid = 0; // All users
  911. $groupname = '';
  912. if ($groupmode) {
  913. $groupid = groups_get_activity_group($this->cm, true);
  914. $groupname = groups_get_group_name($groupid).'-';
  915. }
  916. $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
  917. foreach ($submissions as $submission) {
  918. $a_userid = $submission->userid; //get userid
  919. if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
  920. $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
  921. $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
  922. $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
  923. foreach ($files as $file) {
  924. //get files new name.
  925. $fileext = strstr($file->get_filename(), '.');
  926. $fileoriginal = str_replace($fileext, '', $file->get_filename());
  927. $fileforzipname = clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
  928. //save file name to array for zipping.
  929. $filesforzipping[$fileforzipname] = $file;
  930. }
  931. }
  932. } // end of foreach loop
  933. if ($zipfile = assignment_pack_files($filesforzipping)) {
  934. send_temp_file($zipfile, $filename); //send file and delete after sending.
  935. }
  936. }
  937. }
  938. class mod_assignment_upload_notes_form extends moodleform {
  939. function get_data() {
  940. $data = parent::get_data();
  941. if ($data) {
  942. $data->format = $data->text['format'];
  943. $data->text = $data->text['text'];
  944. }
  945. return $data;
  946. }
  947. function set_data($data) {
  948. if (!isset($data->format)) {
  949. $data->format = FORMAT_HTML;
  950. }
  951. if (isset($data->text)) {
  952. $data->text = array('text'=>$data->text, 'format'=>$data->format);
  953. }
  954. parent::set_data($data);
  955. }
  956. function definition() {
  957. $mform = $this->_form;
  958. // visible elements
  959. $mform->addElement('editor', 'text', get_string('notes', 'assignment'), null, null);
  960. $mform->setType('text', PARAM_RAW); // to be cleaned before display
  961. // hidden params
  962. $mform->addElement('hidden', 'id', 0);
  963. $mform->setType('id', PARAM_INT);
  964. $mform->addElement('hidden', 'action', 'savenotes');
  965. $mform->setType('action', PARAM_ALPHA);
  966. // buttons
  967. $this->add_action_buttons();
  968. }
  969. }
  970. class mod_assignment_upload_response_form extends moodleform {
  971. function definition() {
  972. $mform = $this->_form;
  973. $instance = $this->_customdata;
  974. // visible elements
  975. $mform->addElement('filemanager', 'files_filemanager', get_string('uploadafile'), null, $instance->options);
  976. // hidden params
  977. $mform->addElement('hidden', 'id', $instance->cm->id);
  978. $mform->setType('id', PARAM_INT);
  979. $mform->addElement('hidden', 'contextid', $instance->contextid);
  980. $mform->setType('contextid', PARAM_INT);
  981. $mform->addElement('hidden', 'action', 'uploadresponse');
  982. $mform->setType('action', PARAM_ALPHA);
  983. $mform->addElement('hidden', 'mode', $instance->mode);
  984. $mform->setType('mode', PARAM_ALPHA);
  985. $mform->addElement('hidden', 'offset', $instance->offset);
  986. $mform->setType('offset', PARAM_INT);
  987. $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
  988. $mform->setType('forcerefresh', PARAM_INT);
  989. $mform->addElement('hidden', 'userid', $instance->userid);
  990. $mform->setType('userid', PARAM_INT);
  991. // buttons
  992. $this->add_action_buttons(false, get_string('uploadthisfile'));
  993. }
  994. }