mod_form.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /**
  3. * Sloodle module add/edit form.
  4. * This script defines a form used to add/edit instances of the Sloodle module.
  5. *
  6. * @package sloodle
  7. * @copyright Copyright (c) 2008 Sloodle (various contributors)
  8. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9. *
  10. * @contributor Peter R. Bloomfield
  11. *
  12. */
  13. /** Core Sloodle configuration/functionality */
  14. require_once($CFG->dirroot.'/mod/sloodle/init.php');
  15. /** The base class for the Moodle module form */
  16. require_once ($CFG->dirroot.'/course/moodleform_mod.php');
  17. /** General Sloodle functionality */
  18. require_once(SLOODLE_LIBROOT.'/general.php');
  19. /**
  20. * Used to define the Sloodle module instance add/edit form.
  21. * @package sloodle
  22. */
  23. class mod_sloodle_mod_form extends moodleform_mod {
  24. /**
  25. * Defines the form
  26. * @return void
  27. * @uses $CFG
  28. * @uses $COURSE
  29. * @uses $SLOODLE_TYPES
  30. */
  31. function definition() {
  32. global $CFG, $COURSE, $SLOODLE_TYPES;
  33. $mform =& $this->_form;
  34. //-------------------------------------------------------------------------------
  35. // We need to know which type is being added/edited
  36. $sloodletype = SLOODLE_TYPE_CTRL; // default
  37. // Are we adding a new instance?
  38. if (empty($this->_instance)) {
  39. // Yes - check for a 'type' parameter
  40. $sloodletype = required_param('type', PARAM_TEXT);
  41. } else {
  42. // Fetch the instance data
  43. $rec = sloodle_get_record('sloodle', 'id', $this->_instance);
  44. if (!$rec) error(get_string('modulenotfound'));
  45. // Get the module type
  46. if (empty($rec->type)) {
  47. error(get_string('moduletypeunknown', 'sloodle'));
  48. }
  49. $sloodletype = $rec->type;
  50. }
  51. // Store the fullname of the type
  52. $sloodletypefull = get_string("moduletype:$sloodletype", 'sloodle');
  53. //-------------------------------------------------------------------------------
  54. // General info section
  55. $mform->addElement('header', 'general', get_string('general', 'form'));
  56. // The type is not changeable with this form
  57. // However, well present it as a frozen selection box, so it appears to the user with the full name,
  58. // but has the short-name underlying value
  59. $typeelem = &$mform->addElement('select', 'type', get_string('moduletype','sloodle'), array($sloodletype => $sloodletypefull));
  60. $mform->setDefault('type', $sloodletype);
  61. $typeelem->freeze();
  62. $typeelem->setPersistantFreeze(true);
  63. if (method_exists($mform, 'setHelpButton')) { // Deprecated in Moodle 2, gone from 2.4
  64. $mform->setHelpButton('type', array("moduletype_$sloodletype", get_string('moduletype','sloodle'), 'sloodle'));
  65. } else {
  66. $mform->addHelpButton('type', "moduletype_$sloodletype", 'sloodle');
  67. }
  68. // Make a text box for the name of the module
  69. $mform->addElement('text', 'name', get_string('name', 'sloodle'), array('size'=>'64'));
  70. // Make it text type
  71. $mform->setType('name', PARAM_TEXT);
  72. // Set a client-size rule that an entry is required
  73. $mform->addRule('name', null, 'required', null, 'client');
  74. if (method_exists($this,'add_intro_editor')) {
  75. $this->add_intro_editor(true);
  76. } else {
  77. // Create an HTML editor for module description (intro text)
  78. $mform->addElement('htmleditor', 'intro', get_string('description'));
  79. // Make it raw type (so the HTML isn't filtered out)
  80. $mform->setType('intro', PARAM_RAW);
  81. // Make it required
  82. $mform->addRule('intro', get_string('required'), 'required', null, 'client'); // Don't require description - PRB
  83. }
  84. //-------------------------------------------------------------------------------
  85. // This section adds form elements which are specific to module types.
  86. // Each type-specific data element will be prefixed with the type name, to avoid confusion.
  87. // The "add_instance" and "update_instance" functions in Sloodle's "lib.php" file will then
  88. // process these data items, and write them to the appropriate tables.
  89. // NOTE: the Moodle framework will NOT automatically put the default/existing values here.
  90. // Instead, they need to be added later, in the functions below.
  91. // Check which type is being added
  92. switch ($sloodletype) {
  93. // // CONTROLLER // //
  94. case SLOODLE_TYPE_CTRL:
  95. // Add the type-specific Header
  96. $mform->addElement('header', 'typeheader', $sloodletypefull);
  97. // Add a checkbox for whether or not this module is enabled
  98. $mform->addElement('checkbox', 'controller_enabled', get_string('enabled', 'sloodle'), get_string('controlaccess', 'sloodle'));
  99. $mform->setDefault('controller_enabled', 1);
  100. // Add a text-box for the prim password, with a help button describing it
  101. $mform->addElement('text', 'controller_password', get_string('primpass', 'sloodle'), array('size'=>'12','maxlength'=>'9'));
  102. if (method_exists($mform, 'setHelpButton')) { // Deprecated in Moodle 2, gone from 2.4
  103. $mform->setHelpButton('controller_password', array('prim_password', get_string('help:primpassword','sloodle'), 'sloodle'));
  104. } else {
  105. $mform->addHelpButton('controller_password', 'primpass','sloodle');
  106. }
  107. // Set the field requirements
  108. $mform->setDefault('controller_password', mt_rand(100000000, 999999999));
  109. $mform->addRule('controller_password', null, 'numeric', null, 'client');
  110. // Prim Password can be omitted to disable it now (so don't require it)
  111. //$mform->addRule('controller_password', null, 'required', null, 'client');
  112. break;
  113. // // DISTRIBUTOR // //
  114. case SLOODLE_TYPE_DISTRIB:
  115. // Add the type-specific Header
  116. $mform->addElement('header', 'typeheader', $sloodletypefull);
  117. // Add a note of the current distributor channel (read-only)
  118. $mform->addElement('text', 'distributor_channel', get_string('xmlrpc:channel', 'sloodle').': ', array('size'=>'40', 'readonly'=>'true', 'disabled'=>'true'));
  119. $mform->setDefault('distributor_channel', '');
  120. // Add a note of the number of objects associated with this Distributor
  121. $mform->addElement('text', 'distributor_numobjects', get_string('numobjects', 'sloodle').': ', array('size'=>'4', 'readonly'=>'true', 'disabled'=>'true'));
  122. $mform->setDefault('distributor_numobjects', '0');
  123. // Add a checkbox option to reset the Distributor, but only if this is an existing entry being updated
  124. if (!empty($this->_instance)) {
  125. $mform->addElement('checkbox', 'distributor_reset', get_string('reset').': ', get_string('sloodleobjectdistributor:reset', 'sloodle'));
  126. }
  127. break;
  128. // // SLIDESHOW // //
  129. case SLOODLE_TYPE_PRESENTER:
  130. // Add the type-specific Header
  131. $mform->addElement('header', 'typeheader', $sloodletypefull);
  132. // Add boxes to enter the size of the frame
  133. $mform->addElement('text', 'presenter_framewidth', get_string('framewidth', 'sloodle').': ', array('size'=>'4'));
  134. $mform->addRule('presenter_framewidth', null, 'numeric', null, 'client');
  135. $mform->setDefault('presenter_framewidth', 512);
  136. $mform->addElement('text', 'presenter_frameheight', get_string('frameheight', 'sloodle').': ', array('size'=>'4'));
  137. $mform->addRule('presenter_frameheight', null, 'numeric', null, 'client');
  138. $mform->setDefault('presenter_frameheight', 512);
  139. break;
  140. // // TRACKER // //
  141. case SLOODLE_TYPE_TRACKER:
  142. // Nothing to do
  143. break;
  144. // // MAP // //
  145. case SLOODLE_TYPE_MAP:
  146. // Add the type-specific header
  147. $mform->addElement('header', 'typeheader', $sloodletypefull);
  148. // Add boxes for the initial coordinates of the map
  149. $mform->addElement('text', 'map_initialx', 'Initial position (X): ', array('size'=>'10')); $mform->setDefault('map_initialx', '1000.0');
  150. $mform->addElement('text', 'map_initialy', 'Initial position (Y): ', array('size'=>'10')); $mform->setDefault('map_initialy', '1000.0');
  151. // Add the initial zoom factor
  152. $mform->addElement('text', 'map_initialzoom', 'Initial zoom level (1-6): ', array('size'=>'3')); $mform->setDefault('map_initialzoom', '2');
  153. $mform->addRule('map_initialzoom', null, 'numeric', null, 'client');
  154. // Add a checkbox for showing pan controls
  155. $mform->addElement('checkbox', 'map_showpan', 'Pan controls: ', 'If checked, pan controls will be visible on the map.');
  156. $mform->setDefault('map_showpan', 1);
  157. // Add a checkbox for showing zoom controls
  158. $mform->addElement('checkbox', 'map_showzoom', 'Zoom controls: ', 'If checked, zoom controls will be visible on the map.');
  159. $mform->setDefault('map_showzoom', 1);
  160. // Add a checkbox for allowing dragging of the map
  161. $mform->addElement('checkbox', 'map_allowdrag', 'Allow dragging: ', 'If checked, users will be able to click-and-drag the map to pan it.');
  162. $mform->setDefault('map_allowdrag', 1);
  163. break;
  164. case SLOODLE_TYPE_AWARDS:
  165. global $CFG;
  166. //This switch occures when the user adds a new award activity
  167. // Add the type-specific header
  168. $mform->addElement('header', 'typeheader', $sloodletypefull);
  169. //get all the assignments for the course
  170. $mform->addElement('image','SloodleAwardImage',SLOODLE_WWWROOT.'/lib/media/awardsmall.gif' );
  171. break;
  172. }
  173. //-------------------------------------------------------------------------------
  174. // Add the standard course module elements, except the group stuff (as Sloodle doesn't support it)
  175. $this->standard_coursemodule_elements(false);
  176. //-------------------------------------------------------------------------------
  177. // Form buttons
  178. $this->add_action_buttons();
  179. }
  180. /**
  181. * Performs extra processing on the form after existing/default data has been specified.
  182. * @return void
  183. */
  184. function definition_after_data() {
  185. }
  186. /**
  187. * Pre-processes form initial values.
  188. * Given an array of default values (element name => value) by reference, this function
  189. * can edit the initial values the user sees.
  190. * Note that this is typically only used for editing existing modules, as the initial defaults
  191. * can be coded into the form definition.
  192. * @param array $default_values Array of element names to values/
  193. * @return void
  194. */
  195. function data_preprocessing(&$default_values) {
  196. // Get the form
  197. $mform =& $this->_form;
  198. // Is this a new instance?
  199. if (empty($this->_instance)) return;
  200. // Check which type this is
  201. switch ($default_values['type']) {
  202. case SLOODLE_TYPE_CTRL:
  203. // Fetch the controller record
  204. $controller = sloodle_get_record('sloodle_controller', 'sloodleid', $this->_instance);
  205. if (!$controller) error(get_string('secondarytablenotfound', 'sloodle'));
  206. // Add in the 'enabled' value
  207. $default_values['controller_enabled'] = $controller->enabled;
  208. // Add in the prim password value
  209. $default_values['controller_password'] = $controller->password;
  210. break;
  211. case SLOODLE_TYPE_DISTRIB:
  212. // Fetch the distributor record
  213. $distributor = sloodle_get_record('sloodle_distributor', 'sloodleid', $this->_instance);
  214. if (!$distributor) error(get_string('secondarytablenotfound', 'sloodle'));
  215. // Add in the 'channel' value
  216. $default_values['distributor_channel'] = $distributor->channel;
  217. // Retrieve all object entries for this Distributor
  218. $objects = sloodle_get_records('sloodle_distributor_entry', 'distributorid', $distributor->id);
  219. if (is_array($objects)) {
  220. $default_values['distributor_numobjects'] = count($objects);
  221. }
  222. break;
  223. case SLOODLE_TYPE_PRESENTER:
  224. // Fetch the Presenter record.
  225. $presenter = sloodle_get_record('sloodle_presenter', 'sloodleid', $this->_instance);
  226. if (!$presenter) error(get_string('secondarytablenotfound', 'sloodle'));
  227. // Add in the dimensions of the frame
  228. $default_values['presenter_framewidth'] = (int)$presenter->framewidth;
  229. $default_values['presenter_frameheight'] = (int)$presenter->frameheight;
  230. break;
  231. case SLOODLE_TYPE_TRACKER:
  232. // Nothing to do
  233. break;
  234. case SLOODLE_TYPE_MAP:
  235. // Fetch the map record
  236. $map = sloodle_get_record('sloodle_map', 'sloodleid', $this->_instance);
  237. if (!$map) error(get_string('secondarytablenotfound', 'sloodle'));
  238. // Add in all the values from the database
  239. $default_values['map_initialx'] = $map->initialx;
  240. $default_values['map_initialy'] = $map->initialy;
  241. $default_values['map_initialzoom'] = $map->initialzoom;
  242. $default_values['map_showpan'] = $map->showpan;
  243. $default_values['map_showzoom'] = $map->showzoom;
  244. $default_values['map_allowdrag'] = $map->allowdrag;
  245. break;
  246. default:
  247. // Nothing to do?
  248. break;
  249. }
  250. }
  251. /**
  252. * Validates the form data.
  253. * If there are errors return array of errors ("fieldname"=>"error message"),
  254. * otherwise true if ok.
  255. *
  256. * @param array $data array of ("fieldname"=>value) of submitted data
  257. * @return bool,array Array of fieldnames to error messages, or boolean true if OK
  258. */
  259. function validation($data) {
  260. global $SLOODLE_TYPES;
  261. // Prepare an array of error messages
  262. $errors = array();
  263. // Check which type is being used
  264. switch ($data['type']) {
  265. case SLOODLE_TYPE_CTRL:
  266. // Check that the prim password is OK
  267. $pwd = '';
  268. if (isset($data['controller_password'])) $pwd = $data['controller_password'];
  269. // The password can be left unspecified
  270. if (empty($pwd)) break;
  271. // Validate the password we have been given
  272. $pwderrors = array();
  273. if (!sloodle_validate_prim_password_verbose($pwd, $pwderrors)) {
  274. $errors['controller_password'] = '';
  275. // Add our password errors
  276. foreach ($pwderrors as $pe) {
  277. $errors['controller_password'] .= get_string("primpass:$pe", 'sloodle') . '<br>';
  278. }
  279. }
  280. break;
  281. case SLOODLE_TYPE_DISTRIB:
  282. // Nothing to error check
  283. break;
  284. case SLOODLE_TYPE_PRESENTER:
  285. // Nothing to error check
  286. break;
  287. case SLOODLE_TYPE_TRACKER:
  288. // Nothing to error check
  289. break;
  290. case SLOODLE_TYPE_MAP:
  291. // Nothing to error check
  292. break;
  293. // ADD FUTURE TYPES HERE
  294. case SLOODLE_TYPE_AWARDS: //MOVED TO 0.41
  295. // Nothing to error check
  296. break;
  297. // ADD FUTURE TYPES HERE
  298. default:
  299. // We don't know the type
  300. $errors['type'] = get_string('moduletypeunknown', 'sloodle');
  301. break;
  302. }
  303. // Return the errors if there were any
  304. if (count($errors) > 0) return $errors;
  305. // Everything seems OK
  306. return true;
  307. }
  308. }
  309. ?>