mod_form.php 9.3 KB

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