1
0

settings_form.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * Form to add and edit interactive Learning Module (iLM).
  4. * The fields are initially filled at locallib.php:add_edit_copy_ilm($ilm_id, $action)
  5. * The final processing is performed by 'locallib.php', that is called bye './settings_ilm.php' (with ilm_settings::copy_new_version_ilm($formdata))
  6. * then by 'ilm_handlers/(html5 or java).php' with copy_new_version_ilm($param, $files_extract)'
  7. *
  8. * Release Notes:
  9. * - v 1.6.3 2020/04/28
  10. * + Fixed detail: undefinde variable 'if ($filejars!='')' - it was '$filejar'
  11. * - v 1.6.2 2020/01/20
  12. * + Avoid presence of ' and " in DB to close string to put JavaScript (avoid error)
  13. * - v 1.6.1 2017/12/02
  14. * + New help button, improvements on some helps, new code comments and indentation.
  15. * - v 1.6 2013/12/12
  16. * + Insert support of import iLM from zip packages.
  17. * - v 1.5 2013/10/31
  18. * + Insert support of import iLM from zip packages.
  19. * - v 1.4 2013/10/24
  20. * + Fix required upload an file for new iLM.
  21. * - v 1.3 2013/07/12
  22. * + Fix error messages of 'setType' in debug mode for hidden fields.
  23. * + Form now accept actions: add, edit, copy (new version from an iLM), and new version (empty new version).
  24. *
  25. * To use help button. For instance, to a field named 'file_class' use
  26. * $mform->addHelpButton('file_class', 'file_class_form_help', 'iassign');
  27. * it means that the first parameter is used as 'title' (mouseover) and the second when user "clickover"
  28. * In lang, it is necessary
  29. * file_class_form_help = 'to the title - when mouse over'
  30. * file_class_form_help_help = 'to the complete explanation, when the user click over the help signal'
  31. * 'auto_evaluate_help'] = 'If the iLM has automatic evaluation select Yes to use an activity with it.'; // 'What is automatic evaluation
  32. *
  33. * @see ./locallib.php : 'add_edit_copy_ilm($ilm_id,$action)' load this script and provides data to it (defines initial values to the form fields)
  34. * @see ./settings_ilm.php : load and process this form, it uses data under the name $param
  35. * @see ./ilm_handlers/html5.php : save_ilm_by_xml($application_xml, $files_extract): return null;
  36. *
  37. * @author Patricia Alves Rodrigues
  38. * @author Leônidas O. Brandão
  39. * @version v 1.6 2013/12/12
  40. * @package mod_iassign_settings
  41. * @since 2010/09/27
  42. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  43. *
  44. * <b>License</b>
  45. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46. */
  47. // Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  48. if (!defined('MOODLE_INTERNAL')) {
  49. die('Direct access to this script is forbidden.'); // By security reasons, this must be included in all Moodle pages!
  50. }
  51. require_once($CFG->libdir . '/formslib.php');
  52. require_once($CFG->dirroot . '/course/moodleform_mod.php');
  53. require_once($CFG->dirroot . '/mod/iassign/lib.php');
  54. /// This class create form based moodleform.
  55. // @see moodleform
  56. class mod_ilm_form extends moodleform {
  57. /// Add elements to form
  58. function definition () {
  59. global $CFG, $COURSE, $USER, $DB;
  60. global $description; // defined in 'settings_ilm.php'
  61. // $mform = & $this->_form;
  62. $mform = $this->_form; //leo
  63. if ($CFG->action_ilm != 'import') {
  64. if ($CFG->action_ilm == 'add') {
  65. // $params = array('parent' => '0');
  66. // $tmps = $DB->get_records_sql("SELECT s.id, s.name, s.extension, s.file_jar
  67. // FROM {iassign_ilm} s WHERE s.parent = :parent", $params); // " - jed/emacs
  68. $tmps = $DB->get_records('iassign_ilm', array('parent' => 0));
  69. }
  70. else {
  71. // $params = array('id' => $CFG->ilm_id);
  72. // $tmps = $DB->get_records_sql("SELECT s.id, s.name, s.extension, s.file_jar
  73. // FROM {iassign_ilm} s WHERE s.id != :id AND s.parent = 0", $params); // " - jed/emacs
  74. $tmps = $DB->get_records('iassign_ilm', array('id' => $CFG->ilm_id));
  75. }
  76. $extensions = "";
  77. $names = "";
  78. $versions = "";
  79. $filejars = "";
  80. foreach ($tmps as $tmp) {
  81. $exts = explode(",", $tmp->extension);
  82. foreach ($exts as $ext) {
  83. $extensions .= "'" . $ext . "',";
  84. }
  85. $names .= "'" . $tmp->name . "',";
  86. $filejars .= "'" . $tmp->file_jar . "',";
  87. }
  88. if (!$tmps)
  89. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $CFG->ilm_id));
  90. else
  91. $iassign_ilm = $tmps; // it already has these data
  92. if (!$iassign_ilm) {
  93. $iassign_ilm = new stdClass();
  94. $iassign_ilm->parent = 0;
  95. }
  96. //TODO JavaScript code, verify alternatives.
  97. $code_javascript = "\n <script type='text/javascript'>
  98. //<![CDATA[
  99. document.getElementById('id_data_file_html').style.display = 'none'
  100. document.getElementById('id_data_file_jar').classList.remove('collapsed');
  101. document.getElementById('id_data_ilm').classList.remove('collapsed');
  102. function search_name (name) {
  103. var i;
  104. var names = new Array(";
  105. if ($names != '')
  106. $code_javascript .= addslashes($names) . "');\n"; // if the name has ' it implies close JavaScript string => error!
  107. $code_javascript .= "
  108. for (i=0;i<names.length;i++) {
  109. if (names[i].toLowerCase()==name.toLowerCase()) {
  110. document.forms['mform1'].name.value='';
  111. confirm('" . get_string('invalid_name_ilm', 'iassign') . " '+names[i]+'!');
  112. }
  113. }";
  114. $code_javascript .= " }
  115. function search_extension(extensions) {
  116. var i;
  117. var ext_inserteds = extensions.split(',');
  118. var ext_exists = new Array(";
  119. $code_javascript .= $extensions . "'');" . chr(13);
  120. $code_javascript .= "
  121. for (k=0;k<ext_inserteds.length;k++) {
  122. for (i=0;i<ext_exists.length;i++) {
  123. if (ext_exists[i].toLowerCase()==ext_inserteds[k].toLowerCase()) {
  124. document.forms['mform1'].extension.value='';
  125. confirm('" . get_string('invalid_extension_ilm', 'iassign') . " '+ext_exists[i]+'!');
  126. }
  127. }
  128. }\n";
  129. $code_javascript .= " }
  130. function search_filejar (fullfilejar) {
  131. var i;
  132. var tmp = fullfilejar.split('/');
  133. var filejar = tmp[tmp.length-1];
  134. var filejars = new Array("; // close for JavaScript code
  135. if ($filejars!='')
  136. $code_javascript .= addslashes($filejars) . "');" . chr(13); // if the name has ' it implies close JavaScript string => error!
  137. $code_javascript .= "
  138. for (i=0;i<filejars.length;i++) {
  139. if (filejars[i].toLowerCase()==filejar.toLowerCase()) {
  140. document.forms['mform1'].file_jar[0].value='';
  141. confirm('" . get_string('invalid_filejar_ilm', 'iassign') . " '+filejars[i]+'!');
  142. }
  143. }\n";
  144. $code_javascript .= " }
  145. function change_language (lang) {
  146. if (document.forms['mform1'].description_lang.value != '') {
  147. descriptions = eval('(' + document.forms['mform1'].description_lang.value + ')');
  148. descriptions[document.forms['mform1'].set_lang.value] = tinyMCE.activeEditor.getContent();
  149. document.forms['mform1'].description_lang.value = JSON.stringify(descriptions);
  150. if (descriptions[lang] != undefined)
  151. tinyMCE.activeEditor.setContent(descriptions[lang]);
  152. else
  153. tinyMCE.activeEditor.setContent('');
  154. }
  155. else {
  156. document.forms['mform1'].description_lang.value = '{ \"' + document.forms['mform1'].set_lang.value + '\" : \"' + tinyMCE.activeEditor.getContent() + '\" }';
  157. tinyMCE.activeEditor.setContent('');
  158. }
  159. document.forms['mform1'].set_lang.value = lang;
  160. }
  161. //]]>
  162. </script>\n";
  163. //-------------------------------------------------------------------------------
  164. /// Adding the "data_ilm" fieldset, where all the common settings are showed
  165. if ($CFG->action_ilm != 'add') {
  166. // $mform->addElement('header', 'type_ilm', get_string('type_ilm', 'iassign'));
  167. // // Adding the select option for HTML5 or Java
  168. // $options = array('Java' => 'Java', 'HTML5' => 'HTML5');
  169. // $mform->addElement('select', 'ilm_type', get_string('ilm_type', 'iassign'), $options);
  170. // $mform->setDefault('ilm_type', PARAM_TEXT);
  171. // $mform->addHelpButton('ilm_type', 'ilm_type', 'iassign');
  172. // $mform->addElement('header', 'data_ilm', get_string('data_ilm', 'iassign'));
  173. // Adding the standard "name" field
  174. if ($CFG->action_ilm != 'add') {
  175. $mform->addElement('static', 'name_ilm', get_string('name_ilm', 'iassign'));
  176. $mform->addElement('hidden', 'name');
  177. $mform->setType('name', PARAM_TEXT);
  178. }
  179. else {
  180. $mform->addElement('text', 'name', get_string('name_ilm', 'iassign'), array('size' => '55', 'onchange' => 'search_name(this.value);'));
  181. $mform->setType('name', PARAM_TEXT);
  182. }
  183. // Adding the standard "version" field
  184. $mform->addElement('text', 'version', get_string('version_ilm', 'iassign'), array('size' => '55'));
  185. $mform->setType('version', PARAM_TEXT);
  186. $mform->addHelpButton('version', 'version_ilm', 'iassign');
  187. // Adding the type of iLM
  188. $mform->addElement('static', 'ilm_type', get_string('type_ilm', 'iassign'));
  189. $mform->addElement('hidden', 'type');
  190. $mform->setType('type', PARAM_TEXT);
  191. // Adding the standard "url" field
  192. $mform->addElement('text', 'url', get_string('url_ilm', 'iassign'), array('size' => '55'));
  193. $mform->setType('url', PARAM_TEXT);
  194. $mform->addElement('select', 'lang', get_string('language_label', 'iassign'), get_string_manager()->get_list_of_translations(), array('onChange' => 'change_language(this.value);'));
  195. $mform->setDefault('lang', current_language());
  196. //D Came from 'settings_ilm.php: if ($action == 'edit')
  197. //D With 'editor' does not work!
  198. //D $param = $description; // ilm_settings::add_edit_copy_ilm($ilm_id, $action);
  199. //D $param = $mform->get_data(); - erro, function not defined
  200. //D echo "settings_form.php: param="; print_r($param); echo "<br/>";
  201. //D echo "description=" . $description . "<br/>";
  202. // Adding the standard "description" field
  203. //moodle2 $mform->addElement('htmleditor', 'description', get_string('description', 'iassign'));
  204. //TODO $mform->addElement('editor', 'description', get_string('description', 'iassign'), $description); // moodle3 => 'editor', but not working
  205. $mform->addElement('text', 'description', get_string('description', 'iassign'), array('size' => '80'));//, $description, 200); //moodle3
  206. $mform->setType('description', PARAM_RAW);
  207. // Adding the "data_file_jar" fieldset, where all the common settings are showed ---
  208. //REVER texto 'Pacote do novo iMA (Java) a ser inserido (JAR)'
  209. $mform->addElement('header', 'data_file_jar', get_string('data_file_jar', 'iassign'));
  210. $mform->addElement('static', 'file_jar_static', get_string('fields_info', 'iassign')); // non editable text
  211. // Fields: file_jar file_class
  212. $mform->addElement('text', 'file_jar', get_string('file_jar_path', 'iassign'), array('size' => '55')); // editable text
  213. $mform->setType('file_jar', PARAM_TEXT);
  214. $mform->addElement('text', 'file_class', get_string('file_class', 'iassign')); // editable text
  215. $mform->setType('file_class', PARAM_TEXT);
  216. //TODO : acho que vale a pena incluir o campo 'file_jar'...
  217. // $mform->addElement('static', 'file_jar', get_string('file_jar', 'iassign'));
  218. // $mform->addElement('hidden', 'file_jar'); - already bellow! line 288/320
  219. // $mform->setType('file_jar', PARAM_TEXT);
  220. //TD // Adding the standard "file_jar" field
  221. //TD $mform->addElement('text', 'file_jar', get_string('file_jar', 'iassign'), array('size' => '55'));
  222. //TD $mform->setType('file_jar', PARAM_TEXT);
  223. //TD $mform->addHelpButton('file_jar', 'file_jar_form_help', 'iassign'); // first => field name; second => text to be presented
  224. // Adding the standard "extension" field
  225. $mform->addElement('text', 'extension', get_string('extension', 'iassign'), array('size' => '30', 'onchange' => 'search_extension(this.value);'));
  226. $mform->setType('extension', PARAM_TEXT);
  227. //$mform->addRule('extension', get_string('required', 'iassign'), 'required');
  228. $mform->addHelpButton('extension', 'extension', 'iassign');
  229. // Adding the standard "width" field
  230. $mform->addElement('text', 'width', get_string('width', 'iassign'), array('size' => '10'));
  231. $mform->setType('width', PARAM_TEXT);
  232. //$mform->addRule('width', get_string('required', 'iassign'), 'required');
  233. // Adding the standard "height" field
  234. $mform->addElement('text', 'height', get_string('height', 'iassign'), array('size' => '10'));
  235. $mform->setType('height', PARAM_TEXT);
  236. //$mform->addRule('height', get_string('required', 'iassign'), 'required');
  237. // Adding the standard "evaluate" field
  238. $mform->addElement('selectyesno', 'evaluate', get_string('auto_evaluate', 'iassign'));
  239. $mform->setDefault('evaluate', 1);
  240. //$mform->addRule('evaluate', get_string('required', 'iassign'), 'required');
  241. $mform->addHelpButton('evaluate', 'auto_evaluate', 'iassign');
  242. // Adding the standard "submissionbehavior" field
  243. $mform->addElement('text', 'submissionbehavior', get_string('auto_evaluate', 'iassign'));
  244. $mform->setType('submissionbehavior', PARAM_INT);
  245. // Adding the "data_file_jar" fieldset, where all the common settings are showed
  246. $mform->addElement('header', 'data_file_html', get_string('data_file_html', 'iassign'));
  247. // Adding static text
  248. $mform->addElement('static', 'data_file_html_static', get_string('data_file_html_static', 'iassign'));
  249. } // if ($CFG->action_ilm != 'add')
  250. // // Upload file ilm
  251. // $mform->addElement('header', 'upload_jar', get_string('upload_jar', 'iassign'));
  252. // //$mform->setExpanded('upload_jar');
  253. // $options = array('subdirs' => 0, 'maxbytes' => $CFG->userquota, 'maxfiles' => -1, 'accepted_types' => '*');
  254. // $mform->addElement('filemanager', 'file', null, null, $options);
  255. $mform->addElement('header', 'upload_jar', get_string('upload_jar', 'iassign'));
  256. $options = array('subdirs' => 0, 'maxbytes' => $CFG->userquota, 'maxfiles' => 1, 'accepted_types' => array('*'));
  257. $mform->addElement('filepicker', 'file', null, null, $options);
  258. $mform->addRule('file', get_string('required', 'iassign'), 'required');
  259. if ($CFG->action_ilm == 'add' || $CFG->action_ilm == 'copy' || $CFG->action_ilm == 'new_version')
  260. $mform->addRule('file', get_string('required', 'iassign'), 'required');
  261. /// Adding the standard "hidden" field
  262. if ($CFG->action_ilm == 'edit') {
  263. $mform->addElement('hidden', 'id');
  264. $mform->setType('id', PARAM_INT);
  265. }
  266. $mform->addElement('hidden', 'set_lang');
  267. $mform->setType('set_lang', PARAM_TEXT);
  268. $mform->setDefault('set_lang', current_language());
  269. $mform->addElement('hidden', 'description_lang');
  270. $mform->setType('description_lang', PARAM_TEXT);
  271. // $mform->addElement('hidden', 'file_jar'); //Remove inserted above to allow to edit this field
  272. // $mform->setType('file_jar', PARAM_TEXT); //Remove idem
  273. $mform->addElement('hidden', 'author');
  274. $mform->setType('author', PARAM_TEXT);
  275. $mform->addElement('hidden', 'action');
  276. $mform->setType('action', PARAM_TEXT); //DEBUG: is it necessary?
  277. $mform->addElement('hidden', 'timecreated');
  278. $mform->setType('timecreated', PARAM_TEXT);
  279. $mform->addElement('hidden', 'timemodified');
  280. $mform->setType('timemodified', PARAM_TEXT);
  281. $mform->addElement('hidden', 'parent');
  282. $mform->setType('parent', PARAM_INT);
  283. $mform->addElement('hidden', 'enable');
  284. $mform->setType('enable', PARAM_INT);
  285. $mform->addElement('html', $code_javascript);
  286. } // if ($CFG->action_ilm != 'import')
  287. else {
  288. $mform->addElement('header', 'upload_ilm', get_string('upload_ilm', 'iassign'));
  289. //$mform->setExpanded('upload_ilm');
  290. $options = array('subdirs' => 0, 'maxbytes' => $CFG->userquota, 'maxfiles' => 1, 'accepted_types' => array('*'));
  291. $mform->addElement('filepicker', 'file', null, null, $options);
  292. $mform->addRule('file', get_string('required', 'iassign'), 'required');
  293. $mform->addElement('hidden', 'action'); //DEBUG: is it necessary?
  294. $mform->setType('action', PARAM_TEXT);
  295. }
  296. $this->add_action_buttons();
  297. } // function definition()
  298. } // class mod_ilm_form extends moodleform