settings_form.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. if ($CFG->action_ilm != 'import') {
  63. if ($CFG->action_ilm == 'add') {
  64. // $params = array('parent' => '0');
  65. // $tmps = $DB->get_records_sql("SELECT s.id, s.name, s.extension, s.file_jar
  66. // FROM {iassign_ilm} s WHERE s.parent = :parent", $params); // " - jed/emacs
  67. $tmps = $DB->get_records('iassign_ilm', array('parent' => 0));
  68. }
  69. else {
  70. // $params = array('id' => $CFG->ilm_id);
  71. // $tmps = $DB->get_records_sql("SELECT s.id, s.name, s.extension, s.file_jar
  72. // FROM {iassign_ilm} s WHERE s.id != :id AND s.parent = 0", $params); // " - jed/emacs
  73. $tmps = $DB->get_records('iassign_ilm', array('id' => $CFG->ilm_id));
  74. }
  75. $extensions = "";
  76. $names = "";
  77. $versions = "";
  78. $filejars = "";
  79. foreach ($tmps as $tmp) {
  80. $exts = explode(",", $tmp->extension);
  81. foreach ($exts as $ext) {
  82. $extensions .= "'" . $ext . "',";
  83. }
  84. $names .= "'" . $tmp->name . "',";
  85. $filejars .= "'" . $tmp->file_jar . "',";
  86. }
  87. if (!$tmps)
  88. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $CFG->ilm_id));
  89. else
  90. $iassign_ilm = $tmps; // it already has these data
  91. if (!$iassign_ilm) {
  92. $iassign_ilm = new stdClass();
  93. $iassign_ilm->parent = 0;
  94. }
  95. //TODO JavaScript code, verify alternatives.
  96. $code_javascript = "\n <script type='text/javascript'>
  97. //<![CDATA[
  98. document.getElementById('id_data_file_html').style.display = 'none'
  99. document.getElementById('id_data_file_jar').classList.remove('collapsed');
  100. document.getElementById('id_data_ilm').classList.remove('collapsed');
  101. function search_name (name) {
  102. var i;
  103. var names = new Array(";
  104. if ($names != '')
  105. $code_javascript .= addslashes($names) . "');\n"; // if the name has ' it implies close JavaScript string => error!
  106. $code_javascript .= "
  107. for (i=0;i<names.length;i++) {
  108. if (names[i].toLowerCase()==name.toLowerCase()) {
  109. document.forms['mform1'].name.value='';
  110. confirm('" . get_string('invalid_name_ilm', 'iassign') . " '+names[i]+'!');
  111. }
  112. }";
  113. $code_javascript .= " }
  114. function search_extension(extensions) {
  115. var i;
  116. var ext_inserteds = extensions.split(',');
  117. var ext_exists = new Array(";
  118. $code_javascript .= $extensions . "'');" . chr(13);
  119. $code_javascript .= "
  120. for (k=0;k<ext_inserteds.length;k++) {
  121. for (i=0;i<ext_exists.length;i++) {
  122. if (ext_exists[i].toLowerCase()==ext_inserteds[k].toLowerCase()) {
  123. document.forms['mform1'].extension.value='';
  124. confirm('" . get_string('invalid_extension_ilm', 'iassign') . " '+ext_exists[i]+'!');
  125. }
  126. }
  127. }\n";
  128. $code_javascript .= " }
  129. function search_filejar (fullfilejar) {
  130. var i;
  131. var tmp = fullfilejar.split('/');
  132. var filejar = tmp[tmp.length-1];
  133. var filejars = new Array("; // close for JavaScript code
  134. if ($filejars!='')
  135. $code_javascript .= addslashes($filejars) . "');" . chr(13); // if the name has ' it implies close JavaScript string => error!
  136. $code_javascript .= "
  137. for (i=0;i<filejars.length;i++) {
  138. if (filejars[i].toLowerCase()==filejar.toLowerCase()) {
  139. document.forms['mform1'].file_jar[0].value='';
  140. confirm('" . get_string('invalid_filejar_ilm', 'iassign') . " '+filejars[i]+'!');
  141. }
  142. }\n";
  143. $code_javascript .= " }
  144. function change_language (lang) {
  145. if (document.forms['mform1'].description_lang.value != '') {
  146. descriptions = eval('(' + document.forms['mform1'].description_lang.value + ')');
  147. descriptions[document.forms['mform1'].set_lang.value] = tinyMCE.activeEditor.getContent();
  148. document.forms['mform1'].description_lang.value = JSON.stringify(descriptions);
  149. if (descriptions[lang] != undefined)
  150. tinyMCE.activeEditor.setContent(descriptions[lang]);
  151. else
  152. tinyMCE.activeEditor.setContent('');
  153. }
  154. else {
  155. document.forms['mform1'].description_lang.value = '{ \"' + document.forms['mform1'].set_lang.value + '\" : \"' + tinyMCE.activeEditor.getContent() + '\" }';
  156. tinyMCE.activeEditor.setContent('');
  157. }
  158. document.forms['mform1'].set_lang.value = lang;
  159. }
  160. //]]>
  161. </script>\n";
  162. //-------------------------------------------------------------------------------
  163. /// Adding the "data_ilm" fieldset, where all the common settings are showed
  164. if ($CFG->action_ilm != 'add') {
  165. // $mform->addElement('header', 'type_ilm', get_string('type_ilm', 'iassign'));
  166. // // Adding the select option for HTML5 or Java
  167. // $options = array('Java' => 'Java', 'HTML5' => 'HTML5');
  168. // $mform->addElement('select', 'ilm_type', get_string('ilm_type', 'iassign'), $options);
  169. // $mform->setDefault('ilm_type', PARAM_TEXT);
  170. // $mform->addHelpButton('ilm_type', 'ilm_type', 'iassign');
  171. // $mform->addElement('header', 'data_ilm', get_string('data_ilm', 'iassign'));
  172. // Adding the standard "name" field
  173. if ($CFG->action_ilm != 'add') {
  174. $mform->addElement('static', 'name_ilm', get_string('name_ilm', 'iassign'));
  175. $mform->addElement('hidden', 'name');
  176. $mform->setType('name', PARAM_TEXT);
  177. }
  178. else {
  179. $mform->addElement('text', 'name', get_string('name_ilm', 'iassign'), array('size' => '55', 'onchange' => 'search_name(this.value);'));
  180. $mform->setType('name', PARAM_TEXT);
  181. }
  182. // Adding the standard "version" field
  183. $mform->addElement('text', 'version', get_string('version_ilm', 'iassign'), array('size' => '55'));
  184. $mform->setType('version', PARAM_TEXT);
  185. $mform->addHelpButton('version', 'version_ilm', 'iassign');
  186. // Adding the type of iLM
  187. $mform->addElement('static', 'ilm_type', get_string('type_ilm', 'iassign'));
  188. $mform->addElement('hidden', 'type');
  189. $mform->setType('type', PARAM_TEXT);
  190. // Adding the standard "url" field
  191. $mform->addElement('text', 'url', get_string('url_ilm', 'iassign'), array('size' => '55'));
  192. $mform->setType('url', PARAM_TEXT);
  193. $mform->addElement('select', 'lang', get_string('language_label', 'iassign'), get_string_manager()->get_list_of_translations(), array('onChange' => 'change_language(this.value);'));
  194. $mform->setDefault('lang', current_language());
  195. //D Came from 'settings_ilm.php: if ($action == 'edit')
  196. //D With 'editor' does not work!
  197. //D $param = $description; // ilm_settings::add_edit_copy_ilm($ilm_id, $action);
  198. //D $param = $mform->get_data(); - erro, function not defined
  199. //D echo "settings_form.php: param="; print_r($param); echo "<br/>";
  200. //D echo "description=" . $description . "<br/>";
  201. // Adding the standard "description" field
  202. //moodle2 $mform->addElement('htmleditor', 'description', get_string('description', 'iassign'));
  203. //TODO $mform->addElement('editor', 'description', get_string('description', 'iassign'), $description); // moodle3 => 'editor', but not working
  204. $mform->addElement('text', 'description', get_string('description', 'iassign'), array('size' => '80'));//, $description, 200); //moodle3
  205. $mform->setType('description', PARAM_RAW);
  206. // Adding the "data_file_jar" fieldset, where all the common settings are showed ---
  207. //REVER texto 'Pacote do novo iMA (Java) a ser inserido (JAR)'
  208. $mform->addElement('header', 'data_file_jar', get_string('data_file_jar', 'iassign'));
  209. $mform->addElement('static', 'file_jar_static', get_string('fields_info', 'iassign')); // non editable text
  210. // Fields: file_jar file_class
  211. $mform->addElement('text', 'file_jar', get_string('file_jar_path', 'iassign'), array('size' => '55')); // editable text
  212. $mform->setType('file_jar', PARAM_TEXT);
  213. $mform->addElement('text', 'file_class', get_string('file_class', 'iassign')); // editable text
  214. $mform->setType('file_class', PARAM_TEXT);
  215. //TODO : acho que vale a pena incluir o campo 'file_jar'...
  216. // $mform->addElement('static', 'file_jar', get_string('file_jar', 'iassign'));
  217. // $mform->addElement('hidden', 'file_jar'); - already bellow! line 288/320
  218. // $mform->setType('file_jar', PARAM_TEXT);
  219. //TD // Adding the standard "file_jar" field
  220. //TD $mform->addElement('text', 'file_jar', get_string('file_jar', 'iassign'), array('size' => '55'));
  221. //TD $mform->setType('file_jar', PARAM_TEXT);
  222. //TD $mform->addHelpButton('file_jar', 'file_jar_form_help', 'iassign'); // first => field name; second => text to be presented
  223. // Adding the standard "extension" field
  224. $mform->addElement('text', 'extension', get_string('extension', 'iassign'), array('size' => '30', 'onchange' => 'search_extension(this.value);'));
  225. $mform->setType('extension', PARAM_TEXT);
  226. //$mform->addRule('extension', get_string('required', 'iassign'), 'required');
  227. $mform->addHelpButton('extension', 'extension', 'iassign');
  228. // Adding the standard "width" field
  229. $mform->addElement('text', 'width', get_string('width', 'iassign'), array('size' => '10'));
  230. $mform->setType('width', PARAM_TEXT);
  231. //$mform->addRule('width', get_string('required', 'iassign'), 'required');
  232. // Adding the standard "height" field
  233. $mform->addElement('text', 'height', get_string('height', 'iassign'), array('size' => '10'));
  234. $mform->setType('height', PARAM_TEXT);
  235. //$mform->addRule('height', get_string('required', 'iassign'), 'required');
  236. // Adding the standard "evaluate" field
  237. $mform->addElement('selectyesno', 'evaluate', get_string('auto_evaluate', 'iassign'));
  238. $mform->setDefault('evaluate', 1);
  239. //$mform->addRule('evaluate', get_string('required', 'iassign'), 'required');
  240. $mform->addHelpButton('evaluate', 'auto_evaluate', 'iassign');
  241. // Adding the "data_file_jar" fieldset, where all the common settings are showed
  242. $mform->addElement('header', 'data_file_html', get_string('data_file_html', 'iassign'));
  243. // Adding static text
  244. $mform->addElement('static', 'data_file_html_static', get_string('data_file_html_static', 'iassign'));
  245. }
  246. // // Upload file ilm
  247. // $mform->addElement('header', 'upload_jar', get_string('upload_jar', 'iassign'));
  248. // //$mform->setExpanded('upload_jar');
  249. // $options = array('subdirs' => 0, 'maxbytes' => $CFG->userquota, 'maxfiles' => -1, 'accepted_types' => '*');
  250. // $mform->addElement('filemanager', 'file', null, null, $options);
  251. $mform->addElement('header', 'upload_jar', get_string('upload_jar', 'iassign'));
  252. $options = array('subdirs' => 0, 'maxbytes' => $CFG->userquota, 'maxfiles' => 1, 'accepted_types' => array('*'));
  253. $mform->addElement('filepicker', 'file', null, null, $options);
  254. $mform->addRule('file', get_string('required', 'iassign'), 'required');
  255. if ($CFG->action_ilm == 'add' || $CFG->action_ilm == 'copy' || $CFG->action_ilm == 'new_version')
  256. $mform->addRule('file', get_string('required', 'iassign'), 'required');
  257. /// Adding the standard "hidden" field
  258. if ($CFG->action_ilm == 'edit') {
  259. $mform->addElement('hidden', 'id');
  260. $mform->setType('id', PARAM_INT);
  261. }
  262. $mform->addElement('hidden', 'set_lang');
  263. $mform->setType('set_lang', PARAM_TEXT);
  264. $mform->setDefault('set_lang', current_language());
  265. $mform->addElement('hidden', 'description_lang');
  266. $mform->setType('description_lang', PARAM_TEXT);
  267. // $mform->addElement('hidden', 'file_jar'); //Remove inserted above to allow to edit this field
  268. // $mform->setType('file_jar', PARAM_TEXT); //Remove idem
  269. $mform->addElement('hidden', 'author');
  270. $mform->setType('author', PARAM_TEXT);
  271. $mform->addElement('hidden', 'action');
  272. $mform->setType('action', PARAM_TEXT);
  273. $mform->addElement('hidden', 'timecreated');
  274. $mform->setType('timecreated', PARAM_TEXT);
  275. $mform->addElement('hidden', 'timemodified');
  276. $mform->setType('timemodified', PARAM_TEXT);
  277. $mform->addElement('hidden', 'parent');
  278. $mform->setType('parent', PARAM_INT);
  279. $mform->addElement('hidden', 'enable');
  280. $mform->setType('enable', PARAM_INT);
  281. $mform->addElement('html', $code_javascript);
  282. } // if ($CFG->action_ilm != 'import')
  283. else {
  284. $mform->addElement('header', 'upload_ilm', get_string('upload_ilm', 'iassign'));
  285. //$mform->setExpanded('upload_ilm');
  286. $options = array('subdirs' => 0, 'maxbytes' => $CFG->userquota, 'maxfiles' => 1, 'accepted_types' => array('*'));
  287. $mform->addElement('filepicker', 'file', null, null, $options);
  288. $mform->addRule('file', get_string('required', 'iassign'), 'required');
  289. $mform->addElement('hidden', 'action');
  290. $mform->setType('action', PARAM_TEXT);
  291. }
  292. $this->add_action_buttons();
  293. }
  294. }