[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 /** 18 * This file contains the definition for the library class for file submission plugin 19 * 20 * This class provides all the functionality for the new assign module. 21 * 22 * @package assignsubmission_file 23 * @copyright 2012 NetSpot {@link http://www.netspot.com.au} 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 require_once($CFG->libdir.'/eventslib.php'); 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 // File areas for file submission assignment. 32 define('ASSIGNSUBMISSION_FILE_MAXFILES', 20); 33 define('ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES', 5); 34 define('ASSIGNSUBMISSION_FILE_FILEAREA', 'submission_files'); 35 36 /** 37 * Library class for file submission plugin extending submission plugin base class 38 * 39 * @package assignsubmission_file 40 * @copyright 2012 NetSpot {@link http://www.netspot.com.au} 41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 42 */ 43 class assign_submission_file extends assign_submission_plugin { 44 45 /** 46 * Get the name of the file submission plugin 47 * @return string 48 */ 49 public function get_name() { 50 return get_string('file', 'assignsubmission_file'); 51 } 52 53 /** 54 * Get file submission information from the database 55 * 56 * @param int $submissionid 57 * @return mixed 58 */ 59 private function get_file_submission($submissionid) { 60 global $DB; 61 return $DB->get_record('assignsubmission_file', array('submission'=>$submissionid)); 62 } 63 64 /** 65 * Get the default setting for file submission plugin 66 * 67 * @param MoodleQuickForm $mform The form to add elements to 68 * @return void 69 */ 70 public function get_settings(MoodleQuickForm $mform) { 71 global $CFG, $COURSE; 72 73 $defaultmaxfilesubmissions = $this->get_config('maxfilesubmissions'); 74 $defaultmaxsubmissionsizebytes = $this->get_config('maxsubmissionsizebytes'); 75 76 $settings = array(); 77 $options = array(); 78 for ($i = 1; $i <= ASSIGNSUBMISSION_FILE_MAXFILES; $i++) { 79 $options[$i] = $i; 80 } 81 82 $name = get_string('maxfilessubmission', 'assignsubmission_file'); 83 $mform->addElement('select', 'assignsubmission_file_maxfiles', $name, $options); 84 $mform->addHelpButton('assignsubmission_file_maxfiles', 85 'maxfilessubmission', 86 'assignsubmission_file'); 87 $mform->setDefault('assignsubmission_file_maxfiles', $defaultmaxfilesubmissions); 88 $mform->disabledIf('assignsubmission_file_maxfiles', 'assignsubmission_file_enabled', 'notchecked'); 89 90 $choices = get_max_upload_sizes($CFG->maxbytes, 91 $COURSE->maxbytes, 92 get_config('assignsubmission_file', 'maxbytes')); 93 94 $settings[] = array('type' => 'select', 95 'name' => 'maxsubmissionsizebytes', 96 'description' => get_string('maximumsubmissionsize', 'assignsubmission_file'), 97 'options'=> $choices, 98 'default'=> $defaultmaxsubmissionsizebytes); 99 100 $name = get_string('maximumsubmissionsize', 'assignsubmission_file'); 101 $mform->addElement('select', 'assignsubmission_file_maxsizebytes', $name, $choices); 102 $mform->addHelpButton('assignsubmission_file_maxsizebytes', 103 'maximumsubmissionsize', 104 'assignsubmission_file'); 105 $mform->setDefault('assignsubmission_file_maxsizebytes', $defaultmaxsubmissionsizebytes); 106 $mform->disabledIf('assignsubmission_file_maxsizebytes', 107 'assignsubmission_file_enabled', 108 'notchecked'); 109 } 110 111 /** 112 * Save the settings for file submission plugin 113 * 114 * @param stdClass $data 115 * @return bool 116 */ 117 public function save_settings(stdClass $data) { 118 $this->set_config('maxfilesubmissions', $data->assignsubmission_file_maxfiles); 119 $this->set_config('maxsubmissionsizebytes', $data->assignsubmission_file_maxsizebytes); 120 return true; 121 } 122 123 /** 124 * File format options 125 * 126 * @return array 127 */ 128 private function get_file_options() { 129 $fileoptions = array('subdirs'=>1, 130 'maxbytes'=>$this->get_config('maxsubmissionsizebytes'), 131 'maxfiles'=>$this->get_config('maxfilesubmissions'), 132 'accepted_types'=>'*', 133 'return_types'=>FILE_INTERNAL); 134 if ($fileoptions['maxbytes'] == 0) { 135 // Use module default. 136 $fileoptions['maxbytes'] = get_config('assignsubmission_file', 'maxbytes'); 137 } 138 return $fileoptions; 139 } 140 141 /** 142 * Add elements to submission form 143 * 144 * @param mixed $submission stdClass|null 145 * @param MoodleQuickForm $mform 146 * @param stdClass $data 147 * @return bool 148 */ 149 public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) { 150 151 if ($this->get_config('maxfilesubmissions') <= 0) { 152 return false; 153 } 154 155 $fileoptions = $this->get_file_options(); 156 $submissionid = $submission ? $submission->id : 0; 157 158 $data = file_prepare_standard_filemanager($data, 159 'files', 160 $fileoptions, 161 $this->assignment->get_context(), 162 'assignsubmission_file', 163 ASSIGNSUBMISSION_FILE_FILEAREA, 164 $submissionid); 165 $mform->addElement('filemanager', 'files_filemanager', $this->get_name(), null, $fileoptions); 166 167 return true; 168 } 169 170 /** 171 * Count the number of files 172 * 173 * @param int $submissionid 174 * @param string $area 175 * @return int 176 */ 177 private function count_files($submissionid, $area) { 178 179 $fs = get_file_storage(); 180 $files = $fs->get_area_files($this->assignment->get_context()->id, 181 'assignsubmission_file', 182 $area, 183 $submissionid, 184 'id', 185 false); 186 187 return count($files); 188 } 189 190 /** 191 * Save the files and trigger plagiarism plugin, if enabled, 192 * to scan the uploaded files via events trigger 193 * 194 * @param stdClass $submission 195 * @param stdClass $data 196 * @return bool 197 */ 198 public function save(stdClass $submission, stdClass $data) { 199 global $USER, $DB; 200 201 $fileoptions = $this->get_file_options(); 202 203 $data = file_postupdate_standard_filemanager($data, 204 'files', 205 $fileoptions, 206 $this->assignment->get_context(), 207 'assignsubmission_file', 208 ASSIGNSUBMISSION_FILE_FILEAREA, 209 $submission->id); 210 211 $filesubmission = $this->get_file_submission($submission->id); 212 213 // Plagiarism code event trigger when files are uploaded. 214 215 $fs = get_file_storage(); 216 $files = $fs->get_area_files($this->assignment->get_context()->id, 217 'assignsubmission_file', 218 ASSIGNSUBMISSION_FILE_FILEAREA, 219 $submission->id, 220 'id', 221 false); 222 223 $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA); 224 225 $params = array( 226 'context' => context_module::instance($this->assignment->get_course_module()->id), 227 'courseid' => $this->assignment->get_course()->id, 228 'objectid' => $submission->id, 229 'other' => array( 230 'content' => '', 231 'pathnamehashes' => array_keys($files) 232 ) 233 ); 234 if (!empty($submission->userid) && ($submission->userid != $USER->id)) { 235 $params['relateduserid'] = $submission->userid; 236 } 237 $event = \assignsubmission_file\event\assessable_uploaded::create($params); 238 $event->set_legacy_files($files); 239 $event->trigger(); 240 241 $groupname = null; 242 $groupid = 0; 243 // Get the group name as other fields are not transcribed in the logs and this information is important. 244 if (empty($submission->userid) && !empty($submission->groupid)) { 245 $groupname = $DB->get_field('groups', 'name', array('id' => $submission->groupid), '*', MUST_EXIST); 246 $groupid = $submission->groupid; 247 } else { 248 $params['relateduserid'] = $submission->userid; 249 } 250 251 // Unset the objectid and other field from params for use in submission events. 252 unset($params['objectid']); 253 unset($params['other']); 254 $params['other'] = array( 255 'submissionid' => $submission->id, 256 'submissionattempt' => $submission->attemptnumber, 257 'submissionstatus' => $submission->status, 258 'filesubmissioncount' => $count, 259 'groupid' => $groupid, 260 'groupname' => $groupname 261 ); 262 263 if ($filesubmission) { 264 $filesubmission->numfiles = $this->count_files($submission->id, 265 ASSIGNSUBMISSION_FILE_FILEAREA); 266 $updatestatus = $DB->update_record('assignsubmission_file', $filesubmission); 267 $params['objectid'] = $filesubmission->id; 268 269 $event = \assignsubmission_file\event\submission_updated::create($params); 270 $event->set_assign($this->assignment); 271 $event->trigger(); 272 return $updatestatus; 273 } else { 274 $filesubmission = new stdClass(); 275 $filesubmission->numfiles = $this->count_files($submission->id, 276 ASSIGNSUBMISSION_FILE_FILEAREA); 277 $filesubmission->submission = $submission->id; 278 $filesubmission->assignment = $this->assignment->get_instance()->id; 279 $filesubmission->id = $DB->insert_record('assignsubmission_file', $filesubmission); 280 $params['objectid'] = $filesubmission->id; 281 282 $event = \assignsubmission_file\event\submission_created::create($params); 283 $event->set_assign($this->assignment); 284 $event->trigger(); 285 return $filesubmission->id > 0; 286 } 287 } 288 289 /** 290 * Produce a list of files suitable for export that represent this feedback or submission 291 * 292 * @param stdClass $submission The submission 293 * @param stdClass $user The user record - unused 294 * @return array - return an array of files indexed by filename 295 */ 296 public function get_files(stdClass $submission, stdClass $user) { 297 $result = array(); 298 $fs = get_file_storage(); 299 300 $files = $fs->get_area_files($this->assignment->get_context()->id, 301 'assignsubmission_file', 302 ASSIGNSUBMISSION_FILE_FILEAREA, 303 $submission->id, 304 'timemodified', 305 false); 306 307 foreach ($files as $file) { 308 $result[$file->get_filename()] = $file; 309 } 310 return $result; 311 } 312 313 /** 314 * Display the list of files in the submission status table 315 * 316 * @param stdClass $submission 317 * @param bool $showviewlink Set this to true if the list of files is long 318 * @return string 319 */ 320 public function view_summary(stdClass $submission, & $showviewlink) { 321 $count = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA); 322 323 // Show we show a link to view all files for this plugin? 324 $showviewlink = $count > ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES; 325 if ($count <= ASSIGNSUBMISSION_FILE_MAXSUMMARYFILES) { 326 return $this->assignment->render_area_files('assignsubmission_file', 327 ASSIGNSUBMISSION_FILE_FILEAREA, 328 $submission->id); 329 } else { 330 return get_string('countfiles', 'assignsubmission_file', $count); 331 } 332 } 333 334 /** 335 * No full submission view - the summary contains the list of files and that is the whole submission 336 * 337 * @param stdClass $submission 338 * @return string 339 */ 340 public function view(stdClass $submission) { 341 return $this->assignment->render_area_files('assignsubmission_file', 342 ASSIGNSUBMISSION_FILE_FILEAREA, 343 $submission->id); 344 } 345 346 347 348 /** 349 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type 350 * and version. 351 * 352 * @param string $type 353 * @param int $version 354 * @return bool True if upgrade is possible 355 */ 356 public function can_upgrade($type, $version) { 357 358 $uploadsingletype ='uploadsingle'; 359 $uploadtype ='upload'; 360 361 if (($type == $uploadsingletype || $type == $uploadtype) && $version >= 2011112900) { 362 return true; 363 } 364 return false; 365 } 366 367 368 /** 369 * Upgrade the settings from the old assignment 370 * to the new plugin based one 371 * 372 * @param context $oldcontext - the old assignment context 373 * @param stdClass $oldassignment - the old assignment data record 374 * @param string $log record log events here 375 * @return bool Was it a success? (false will trigger rollback) 376 */ 377 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) { 378 global $DB; 379 380 if ($oldassignment->assignmenttype == 'uploadsingle') { 381 $this->set_config('maxfilesubmissions', 1); 382 $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes); 383 return true; 384 } else if ($oldassignment->assignmenttype == 'upload') { 385 $this->set_config('maxfilesubmissions', $oldassignment->var1); 386 $this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes); 387 388 // Advanced file upload uses a different setting to do the same thing. 389 $DB->set_field('assign', 390 'submissiondrafts', 391 $oldassignment->var4, 392 array('id'=>$this->assignment->get_instance()->id)); 393 394 // Convert advanced file upload "hide description before due date" setting. 395 $alwaysshow = 0; 396 if (!$oldassignment->var3) { 397 $alwaysshow = 1; 398 } 399 $DB->set_field('assign', 400 'alwaysshowdescription', 401 $alwaysshow, 402 array('id'=>$this->assignment->get_instance()->id)); 403 return true; 404 } 405 } 406 407 /** 408 * Upgrade the submission from the old assignment to the new one 409 * 410 * @param context $oldcontext The context of the old assignment 411 * @param stdClass $oldassignment The data record for the old oldassignment 412 * @param stdClass $oldsubmission The data record for the old submission 413 * @param stdClass $submission The data record for the new submission 414 * @param string $log Record upgrade messages in the log 415 * @return bool true or false - false will trigger a rollback 416 */ 417 public function upgrade(context $oldcontext, 418 stdClass $oldassignment, 419 stdClass $oldsubmission, 420 stdClass $submission, 421 & $log) { 422 global $DB; 423 424 $filesubmission = new stdClass(); 425 426 $filesubmission->numfiles = $oldsubmission->numfiles; 427 $filesubmission->submission = $submission->id; 428 $filesubmission->assignment = $this->assignment->get_instance()->id; 429 430 if (!$DB->insert_record('assignsubmission_file', $filesubmission) > 0) { 431 $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid); 432 return false; 433 } 434 435 // Now copy the area files. 436 $this->assignment->copy_area_files_for_upgrade($oldcontext->id, 437 'mod_assignment', 438 'submission', 439 $oldsubmission->id, 440 $this->assignment->get_context()->id, 441 'assignsubmission_file', 442 ASSIGNSUBMISSION_FILE_FILEAREA, 443 $submission->id); 444 445 return true; 446 } 447 448 /** 449 * The assignment has been deleted - cleanup 450 * 451 * @return bool 452 */ 453 public function delete_instance() { 454 global $DB; 455 // Will throw exception on failure. 456 $DB->delete_records('assignsubmission_file', 457 array('assignment'=>$this->assignment->get_instance()->id)); 458 459 return true; 460 } 461 462 /** 463 * Formatting for log info 464 * 465 * @param stdClass $submission The submission 466 * @return string 467 */ 468 public function format_for_log(stdClass $submission) { 469 // Format the info for each submission plugin (will be added to log). 470 $filecount = $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA); 471 472 return get_string('numfilesforlog', 'assignsubmission_file', $filecount); 473 } 474 475 /** 476 * Return true if there are no submission files 477 * @param stdClass $submission 478 */ 479 public function is_empty(stdClass $submission) { 480 return $this->count_files($submission->id, ASSIGNSUBMISSION_FILE_FILEAREA) == 0; 481 } 482 483 /** 484 * Get file areas returns a list of areas this plugin stores files 485 * @return array - An array of fileareas (keys) and descriptions (values) 486 */ 487 public function get_file_areas() { 488 return array(ASSIGNSUBMISSION_FILE_FILEAREA=>$this->get_name()); 489 } 490 491 /** 492 * Copy the student's submission from a previous submission. Used when a student opts to base their resubmission 493 * on the last submission. 494 * @param stdClass $sourcesubmission 495 * @param stdClass $destsubmission 496 */ 497 public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) { 498 global $DB; 499 500 // Copy the files across. 501 $contextid = $this->assignment->get_context()->id; 502 $fs = get_file_storage(); 503 $files = $fs->get_area_files($contextid, 504 'assignsubmission_file', 505 ASSIGNSUBMISSION_FILE_FILEAREA, 506 $sourcesubmission->id, 507 'id', 508 false); 509 foreach ($files as $file) { 510 $fieldupdates = array('itemid' => $destsubmission->id); 511 $fs->create_file_from_storedfile($fieldupdates, $file); 512 } 513 514 // Copy the assignsubmission_file record. 515 if ($filesubmission = $this->get_file_submission($sourcesubmission->id)) { 516 unset($filesubmission->id); 517 $filesubmission->submission = $destsubmission->id; 518 $DB->insert_record('assignsubmission_file', $filesubmission); 519 } 520 return true; 521 } 522 523 /** 524 * Return a description of external params suitable for uploading a file submission from a webservice. 525 * 526 * @return external_description|null 527 */ 528 public function get_external_parameters() { 529 return array( 530 'files_filemanager' => new external_value( 531 PARAM_INT, 532 'The id of a draft area containing files for this submission.' 533 ) 534 ); 535 } 536 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |