1
0

mod_form.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Form to add and edit instance of iAssign.
  4. *
  5. * Release Notes:
  6. * - v 1.4 2016/05/12
  7. * + Function moodleform_mod::add_intro_editor() is deprecated, it was then replaced by moodleform_mod::standard_intro_elements().
  8. * - v 1.3 2013/10/22
  9. * + Clean tag of messages (mod_iassign_mod_form::data_preprocessing).
  10. * - v 1.2 2013/09/19
  11. * + Insert general fields for iassign statement (grade, timeavaliable, timedue, preventlate, test, max_experiment).
  12. * + Save general fields in iassign statement table (mod_iassign_mod_form::definition_after_data).
  13. *
  14. * @author Patricia Alves Rodrigues
  15. * @author Leônidas O. Brandão
  16. * @version v 1.4 2016/05/12
  17. * @package mod_iassign
  18. * @since 2010/09/27
  19. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  20. *
  21. * <b>License</b>
  22. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. // Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  25. if (!defined('MOODLE_INTERNAL')) {
  26. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  27. }
  28. require_once ($CFG->dirroot . '/course/moodleform_mod.php');
  29. /// This class create form based moodleform.
  30. // @see moodleform_mod
  31. class mod_iassign_mod_form extends moodleform_mod {
  32. function definition () {
  33. global $CFG, $DB;
  34. $mform = & $this->_form;
  35. $course_modules_id = optional_param('update', 0, PARAM_INT);
  36. //-------------------------------------------------------------------------------
  37. /// Adding the "title_type_iassign" fieldset, where all the common settings are showed
  38. $mform->addElement('header', 'general', get_string('general', 'iassign'));
  39. /// Adding the standard "name" field
  40. $mform->addElement('text', 'name', get_string('iassigntitle', 'iassign'), array('size' => '55'));
  41. if (!empty($CFG->formatstringstriptags)) {
  42. $mform->setType('name', PARAM_TEXT);
  43. }
  44. else {
  45. $mform->setType('name', PARAM_CLEANHTML);
  46. }
  47. $mform->addRule('name', null, 'required', null, 'client');
  48. $this->standard_intro_elements(get_string('description', 'iassign'));
  49. $mform->addElement('selectyesno', 'activity_group', get_string('activity_group', 'iassign'));
  50. $mform->setDefault('activity_group', 1);
  51. $mform->addHelpButton('activity_group', 'helpactivitygroup', 'iassign');
  52. // general fields
  53. $mform->addElement('header', 'general_fields', get_string('general_fields', 'iassign'));
  54. $timeavailable_group = array();
  55. $timeavailable_group[] = & $mform->createElement('date_time_selector', 'timeavailable', '');
  56. $mform->setDefault('timeavailable', time());
  57. if ($course_modules_id != 0)
  58. $timeavailable_group[] = & $mform->createElement('advcheckbox', 'timeavailable_enabled', '', get_string('enable'));
  59. $mform->addGroup($timeavailable_group, 'timeavailable_group', get_string('availabledate', 'iassign'), ' ', false);
  60. if ($course_modules_id != 0)
  61. $mform->disabledIf('timeavailable_group', 'timeavailable_enabled');
  62. $timedue_group = array();
  63. $timedue_group[] = & $mform->createElement('date_time_selector', 'timedue', '');
  64. $mform->setDefault('timedue', time() + 7 * 24 * 3600);
  65. if ($course_modules_id != 0)
  66. $timedue_group[] = & $mform->createElement('advcheckbox', 'timedue_enabled', '', get_string('enable'));
  67. $mform->addGroup($timedue_group, 'timedue_group', get_string('duedate', 'iassign'), ' ', false);
  68. if ($course_modules_id != 0)
  69. $mform->disabledIf('timedue_group', 'timedue_enabled');
  70. $preventlate_group = array();
  71. $preventlate_group[] = & $mform->createElement('selectyesno', 'preventlate', '');
  72. $mform->setDefault('preventlate', 0);
  73. if ($course_modules_id != 0)
  74. $preventlate_group[] = & $mform->createElement('advcheckbox', 'preventlate_enabled', '', get_string('enable'));
  75. $mform->addGroup($preventlate_group, 'preventlate_group', get_string('preventlate', 'iassign'), ' ', false);
  76. if ($course_modules_id != 0)
  77. $mform->disabledIf('preventlate_group', 'preventlate_enabled');
  78. $mform->addHelpButton('preventlate_group', 'helppreventlate', 'iassign');
  79. $test_group = array();
  80. $test_group[] = & $mform->createElement('selectyesno', 'test', '');
  81. $mform->setDefault('test', 0);
  82. if ($course_modules_id != 0)
  83. $test_group[] = & $mform->createElement('advcheckbox', 'test_enabled', '', get_string('enable'));
  84. $mform->addGroup($test_group, 'test_group', get_string('permission_test', 'iassign'), ' ', false);
  85. if ($course_modules_id != 0)
  86. $mform->disabledIf('test_group', 'test_enabled');
  87. $mform->addHelpButton('test_group', 'helptest', 'iassign');
  88. $mform->addElement('modgrade', 'grade', get_string('grade', 'iassign'));
  89. $max_experiment_options = array(0 => get_string('ilimit', 'iassign'));
  90. for ($i = 1; $i <= 20; $i++)
  91. $max_experiment_options[$i] = $i;
  92. $max_experiment_group = array();
  93. $max_experiment_group[] = & $mform->createElement('select', 'max_experiment', '', $max_experiment_options);
  94. $mform->setDefault('max_experiment', 0);
  95. if ($course_modules_id != 0)
  96. $max_experiment_group[] = & $mform->createElement('advcheckbox', 'max_experiment_enabled', '', get_string('enable'));
  97. $mform->addGroup($max_experiment_group, 'max_experiment_group', get_string('experiment', 'iassign'), ' ', false);
  98. if ($course_modules_id != 0)
  99. $mform->disabledIf('max_experiment_group', 'max_experiment_enabled');
  100. $mform->addHelpButton('max_experiment_group', 'helpexperiment', 'iassign');
  101. //-------------------------------------------------------------------------------
  102. // Hidden fields
  103. $mform->addElement('hidden', 'id');
  104. $mform->setType('id', PARAM_TEXT);
  105. if ($course_modules_id != 0) {
  106. $course_modules = $DB->get_record("course_modules", array('id' => $course_modules_id));
  107. $iassign_statement = $DB->get_records("iassign_statement", array('iassignid' => $course_modules->instance));
  108. if ($iassign_statement) {
  109. $mform->addElement('header', 'header_general_fields_apply', get_string('general_fields_apply', 'iassign'));
  110. $mform->addHelpButton('header_general_fields_apply', 'general_fields_apply', 'iassign');
  111. $this->add_checkbox_controller('atividades');
  112. foreach ($iassign_statement as $iassign) {
  113. $tmp = 'iassign_statement[' . $iassign->id . ']';
  114. $mform->addElement('advcheckbox', $tmp, $iassign->name, null, array('group' => 'atividades'));
  115. }
  116. }
  117. }
  118. $this->standard_coursemodule_elements();
  119. $this->add_action_buttons();
  120. } // function definition()
  121. function data_preprocessing (&$default_values) {
  122. parent::data_preprocessing($default_values);
  123. $mform = & $this->_form;
  124. if (!$mform->isSubmitted() && array_key_exists('name', $default_values)) {
  125. //TODO Remove when updating all the iassign that are tag &lt;ia_uc&gt;
  126. $ia_uc = explode('&lt;ia_uc&gt;', $default_values['name']);
  127. $default_values['name'] = $ia_uc[0];
  128. }
  129. }
  130. function definition_after_data() {
  131. global $DB;
  132. $mform = & $this->_form;
  133. $data = $mform->exportValues();
  134. if ($mform->isSubmitted() && array_key_exists('iassign_statement', $data)) {
  135. $iassign_statements = $data['iassign_statement'];
  136. foreach ($iassign_statements as $key => $value) {
  137. if ($value == 1) {
  138. $updateentry = new stdClass();
  139. $updateentry->id = $key;
  140. $updateentry->timemodified = time();
  141. if (isset($data['grade_enabled']) && $data['grade_enabled'] == 1)
  142. $updateentry->grade = $data['grade'];
  143. if (isset($data['timedue_enabled']) && $data['timedue_enabled'] == 1)
  144. $updateentry->timedue = $data['timedue'];
  145. if (isset($data['timeavailable_enabled']) && $data['timeavailable_enabled'] == 1)
  146. $updateentry->timeavailable = $data['timeavailable'];
  147. if (isset($data['preventlate_enabled']) && $data['preventlate_enabled'] == 1)
  148. $updateentry->preventlate = $data['preventlate'];
  149. if (isset($data['test_enabled']) && $data['test_enabled'] == 1)
  150. $updateentry->test = $data['test'];
  151. if (isset($data['max_experiment_enabled']) && $data['max_experiment_enabled'] == 1)
  152. $updateentry->max_experiment = $data['max_experiment'];
  153. if (!$DB->update_record("iassign_statement", $updateentry))
  154. print_error('error_update', 'iassign');
  155. }
  156. }
  157. }
  158. }
  159. } // class mod_iassign_mod_form extends moodleform_mod