PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/synergylearning/campusconnect
PHP | 1213 lines | 902 code | 206 blank | 105 comment | 172 complexity | 11c2b8ef1ce0282b9ea29db03353ec3b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file