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

/mod/assignment/mod_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 146 lines | 107 code | 29 blank | 10 comment | 20 complexity | e92c2402f1cd85b248e2c7fe51e7ca04 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
  1. <?php
  2. if (!defined('MOODLE_INTERNAL')) {
  3. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  4. }
  5. require_once ($CFG->dirroot.'/course/moodleform_mod.php');
  6. class mod_assignment_mod_form extends moodleform_mod {
  7. protected $_assignmentinstance = null;
  8. function definition() {
  9. global $CFG, $DB, $PAGE, $COURSE;
  10. $mform =& $this->_form;
  11. // this hack is needed for different settings of each subtype
  12. if (!empty($this->_instance)) {
  13. if($ass = $DB->get_record('assignment', array('id'=>$this->_instance))) {
  14. $type = $ass->assignmenttype;
  15. } else {
  16. print_error('invalidassignment', 'assignment');
  17. }
  18. } else {
  19. $type = required_param('type', PARAM_ALPHA);
  20. }
  21. $mform->addElement('hidden', 'assignmenttype', $type);
  22. $mform->setType('assignmenttype', PARAM_ALPHA);
  23. $mform->setDefault('assignmenttype', $type);
  24. $mform->addElement('hidden', 'type', $type);
  25. $mform->setType('type', PARAM_ALPHA);
  26. $mform->setDefault('type', $type);
  27. $classfile = $CFG->dirroot.'/mod/assignment/type/'.$type.'/assignment.class.php';
  28. if (!file_exists($classfile)) {
  29. throw new moodle_exception('unsupportedsubplugin', 'assignment', new moodle_url('/course/view.php', array('id' => $COURSE->id)), $type);
  30. }
  31. require_once($classfile);
  32. $assignmentclass = 'assignment_'.$type;
  33. $assignmentinstance = new $assignmentclass();
  34. //-------------------------------------------------------------------------------
  35. $mform->addElement('header', 'general', get_string('general', 'form'));
  36. // $mform->addElement('static', 'statictype', get_string('assignmenttype', 'assignment'), get_string('type'.$type,'assignment'));
  37. $mform->addElement('text', 'name', get_string('assignmentname', 'assignment'), array('size'=>'64'));
  38. if (!empty($CFG->formatstringstriptags)) {
  39. $mform->setType('name', PARAM_TEXT);
  40. } else {
  41. $mform->setType('name', PARAM_CLEANHTML);
  42. }
  43. $mform->addRule('name', null, 'required', null, 'client');
  44. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  45. $this->add_intro_editor(true, get_string('description', 'assignment'));
  46. $mform->addElement('date_time_selector', 'timeavailable', get_string('availabledate', 'assignment'), array('optional'=>true));
  47. $mform->setDefault('timeavailable', time());
  48. $mform->addElement('date_time_selector', 'timedue', get_string('duedate', 'assignment'), array('optional'=>true));
  49. $mform->setDefault('timedue', time()+7*24*3600);
  50. $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
  51. if ($assignmentinstance->supports_lateness()) {
  52. $mform->addElement('select', 'preventlate', get_string('preventlate', 'assignment'), $ynoptions);
  53. $mform->setDefault('preventlate', 0);
  54. }
  55. // hack to support pluggable assignment type titles
  56. if (get_string_manager()->string_exists('type'.$type, 'assignment')) {
  57. $typetitle = get_string('type'.$type, 'assignment');
  58. } else {
  59. $typetitle = get_string('type'.$type, 'assignment_'.$type);
  60. }
  61. $this->standard_grading_coursemodule_elements();
  62. $mform->addElement('header', 'typedesc', $typetitle);
  63. $assignmentinstance->setup_elements($mform);
  64. $this->standard_coursemodule_elements();
  65. $this->add_action_buttons();
  66. // Add warning popup/noscript tag, if grades are changed by user.
  67. if ($mform->elementExists('grade') && !empty($this->_instance) && $DB->record_exists_select('assignment_submissions', 'assignment = ? AND grade <> -1', array($this->_instance))) {
  68. $module = array(
  69. 'name' => 'mod_assignment',
  70. 'fullpath' => '/mod/assignment/assignment.js',
  71. 'requires' => array('node', 'event'),
  72. 'strings' => array(array('changegradewarning', 'mod_assignment'))
  73. );
  74. $PAGE->requires->js_init_call('M.mod_assignment.init_grade_change', null, false, $module);
  75. // Add noscript tag in case
  76. $noscriptwarning = $mform->createElement('static', 'warning', null, html_writer::tag('noscript', get_string('changegradewarning', 'mod_assignment')));
  77. $mform->insertElementBefore($noscriptwarning, 'grade');
  78. }
  79. }
  80. // Needed by plugin assignment types if they include a filemanager element in the settings form
  81. function has_instance() {
  82. return ($this->_instance != NULL);
  83. }
  84. // Needed by plugin assignment types if they include a filemanager element in the settings form
  85. function get_context() {
  86. return $this->context;
  87. }
  88. protected function get_assignment_instance() {
  89. global $CFG, $DB;
  90. if ($this->_assignmentinstance) {
  91. return $this->_assignmentinstance;
  92. }
  93. if (!empty($this->_instance)) {
  94. if($ass = $DB->get_record('assignment', array('id'=>$this->_instance))) {
  95. $type = $ass->assignmenttype;
  96. } else {
  97. print_error('invalidassignment', 'assignment');
  98. }
  99. } else {
  100. $type = required_param('type', PARAM_ALPHA);
  101. }
  102. require_once($CFG->dirroot.'/mod/assignment/type/'.$type.'/assignment.class.php');
  103. $assignmentclass = 'assignment_'.$type;
  104. $this->assignmentinstance = new $assignmentclass();
  105. return $this->assignmentinstance;
  106. }
  107. function data_preprocessing(&$default_values) {
  108. // Allow plugin assignment types to preprocess form data (needed if they include any filemanager elements)
  109. $this->get_assignment_instance()->form_data_preprocessing($default_values, $this);
  110. }
  111. function validation($data, $files) {
  112. // Allow plugin assignment types to do any extra validation after the form has been submitted
  113. $errors = parent::validation($data, $files);
  114. $errors = array_merge($errors, $this->get_assignment_instance()->form_validation($data, $files));
  115. return $errors;
  116. }
  117. }