iassign_form.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <?php
  2. /**
  3. * Form to add and edit interactive activities
  4. *
  5. *
  6. * Release Notes:
  7. * - v 1.5.1 2020/01/21
  8. * + Filter 'addslach(.)' to avoid ' and " close command JavaScript and verify if field 'grade' in JS is 'undefined'
  9. * - v 1.5 2013/09/19
  10. * + Insert function for validation form (mod_iassign_form::validation).
  11. * + Fix bugs in download exercise file.
  12. * - v 1.4 2013/08/21
  13. * + Change title link with message for get file for donwload file.
  14. * - v 1.3 2013/08/15
  15. * + Change view file for allow download file.
  16. * - v 1.2 2013/08/01
  17. * + Fix error in sql query for var $igeom.
  18. * - v 1.1 2013/07/12
  19. * + Fix error messages of 'setType' in debug mode for hidden fields.
  20. *
  21. * @calledby ./locallib.php: function add_edit_iassign(): $mform = new mod_iassign_form(null, null, null, null, array('id'=>'mform1'));
  22. *
  23. * @author Patricia Alves Rodrigues
  24. * @author Leônidas O. Brandão
  25. * @version v 1.5 2013/09/19
  26. * @package mod_iassign
  27. * @since 2010/09/27
  28. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  29. *
  30. * <b>License</b>
  31. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. // Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  34. if (!defined('MOODLE_INTERNAL')) {
  35. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  36. }
  37. require_once($CFG->libdir . '/formslib.php');
  38. require_once($CFG->dirroot . '/course/moodleform_mod.php');
  39. require_once($CFG->dirroot . '/mod/iassign/lib.php');
  40. /// This class create form based moodleform.
  41. // @see moodleform
  42. class mod_iassign_form extends moodleform {
  43. /// Add elements to form
  44. function definition () {
  45. global $CFG, $COURSE, $USER, $DB, $OUTPUT; // $OUTPUT only used to get '$OUTPUT->help_icon(...)'
  46. $mform = $this->_form;
  47. $instance = $this->_customdata;
  48. // iGeom has special parameter 'script'
  49. $params = array('name' => '%iGeom%', 'parent' => 0);
  50. $igeom = $DB->get_records_sql(
  51. "SELECT s.id, s.name, s.parent FROM {iassign_ilm} s
  52. WHERE s.name LIKE :name AND s.parent = :parent", $params);
  53. $id = $COURSE->cm;
  54. foreach ($igeom as $item)
  55. $idigeom = $item->id; // get the last id of iGeom
  56. // Get recently used ilm id: @igor
  57. $params = array('curso' => $COURSE->id);
  58. $result_recent = $DB->get_record_sql(
  59. "SELECT stm.iassign_ilmid FROM {iassign_statement} stm, {iassign} ias
  60. WHERE stm.iassignid = ias.id AND ias.course=:curso ORDER BY stm.id DESC LIMIT 1", $params);
  61. $all_ilm = $DB->get_records('iassign_ilm', array('enable' => 1)); // or use ./lib.php function: $all_ilm = search_iLM(1);
  62. $iassigns = $DB->get_records('iassign_statement', array('iassignid' => $COURSE->iassignid));
  63. if ($all_ilm) {
  64. $ids = "";
  65. $names = "";
  66. $evaluates = "";
  67. foreach ($all_ilm as $one_ilm) {
  68. $ids .= "'" . $one_ilm->id . "',";
  69. $names .= "'" . $one_ilm->name . "',";
  70. $evaluates .= "'" . $one_ilm->evaluate . "',";
  71. }
  72. $ids .= "'0'";
  73. $evaluates .= "'0'";
  74. }
  75. $name_iassigns = "";
  76. if ($iassigns) {
  77. foreach ($iassigns as $iassign) {
  78. $name_iassigns .= "'" . addslashes($iassign->name) . "',"; // if the name has ' it implies close JavaScript string => error!
  79. }
  80. }
  81. $name_iassigns .= "''";
  82. $error_name = get_string('error_iassign_name', 'iassign');
  83. // @todo Código Javascript, verificar alternativa.
  84. $code_javascript = "
  85. <script type='text/javascript'>
  86. //<![CDATA[
  87. var i;
  88. var ids = new Array($ids);
  89. var evaluates = new Array($evaluates);
  90. document.forms['mform1'].filename.disabled=1;
  91. if (document.forms['mform1'].type_iassign.value==1) {
  92. if (typeof document.forms['mform1'].grade === 'undefined') ; // do nothing
  93. else document.forms['mform1'].grade.style.display='none';
  94. document.forms['mform1'].max_experiment.style.display='none';
  95. } else {
  96. if (typeof document.forms['mform1'].grade === 'undefined') ; // do nothing
  97. else document.forms['mform1'].grade.style.display='block';
  98. document.forms['mform1'].max_experiment.style.display='block';
  99. }
  100. for (i=0;i<ids.length;i++) {
  101. if (ids[i]==document.forms['mform1'].iassign_ilmid.value && evaluates[i]==0) {
  102. document.forms['mform1'].automatic_evaluate.style.display='none';
  103. document.forms['mform1'].show_answer.style.display='none';
  104. //document.forms['mform1'].automatic_evaluate.disabled=1;
  105. // document.forms['mform1'].show_answer.disabled=1;
  106. }
  107. }
  108. if (document.forms['mform1'].iassign_ilmid.value==" . $idigeom .") { // iGeom has special parameter 'script'
  109. document.forms['mform1'].special_param1.style.display='block';
  110. document.forms['mform1'].special_param1.disabled=0;
  111. }
  112. else {
  113. document.forms['mform1'].special_param1.style.display='none';
  114. document.forms['mform1'].special_param1.value=0;
  115. document.forms['mform1'].special_param1.disabled=1;
  116. }
  117. function confirm_name (name) {
  118. var i;
  119. var names = new Array(" . $name_iassigns . ");
  120. for (i=0;i<names.length;i++) {
  121. if (names[i]==name)
  122. alert('" . $error_name . "');
  123. }
  124. }
  125. function config_ilm (id) {
  126. //alert('config_ilm('+id+'): idigeom=$idigeom, ilmid='+document.forms['mform1'].iassign_ilmid.value);
  127. if (id==$idigeom) {
  128. document.forms['mform1'].special_param1.style.display='block';
  129. document.forms['mform1'].special_param1.disabled=0;
  130. }
  131. else {
  132. document.forms['mform1'].special_param1.style.display='none';
  133. document.forms['mform1'].special_param1.value=0;
  134. document.forms['mform1'].special_param1.disabled=1;
  135. }
  136. var i;
  137. var ids = new Array($ids);
  138. var evaluates = new Array($evaluates);
  139. if (document.forms['mform1'].type_iassign.value==1) {
  140. document.forms['mform1'].automatic_evaluate.disabled=1;
  141. document.forms['mform1'].show_answer.disabled=1;
  142. document.forms['mform1'].automatic_evaluate.value=0;
  143. document.forms['mform1'].show_answer.value=0;
  144. }
  145. else { // if (document.forms['mform1'].type_iassign.value==1)
  146. for (i=0;i<ids.length;i++) {
  147. if (ids[i]==id) {
  148. if (document.forms['mform1'].action.value=='edit') {
  149. if (evaluates[i]==0){
  150. document.forms['mform1'].automatic_evaluate.style.display='none';
  151. document.forms['mform1'].show_answer.style.display='none';
  152. document.forms['mform1'].automatic_evaluate.disabled=1;
  153. document.forms['mform1'].show_answer.disabled=1;
  154. document.forms['mform1'].automatic_evaluate.value=0;
  155. document.forms['mform1'].show_answer.value=0;
  156. }
  157. else {
  158. document.forms['mform1'].automatic_evaluate.style.display='block';
  159. document.forms['mform1'].show_answer.style.display='block';
  160. document.forms['mform1'].automatic_evaluate.disabled=0;
  161. document.forms['mform1'].show_answer.disabled=0;
  162. document.forms['mform1'].automatic_evaluate.value=1;
  163. document.forms['mform1'].show_answer.value=1;
  164. }
  165. }
  166. if (document.forms['mform1'].action.value=='add') {
  167. if (evaluates[i]==0) {
  168. document.forms['mform1'].automatic_evaluate.style.display='none';
  169. document.forms['mform1'].show_answer.style.display='none';
  170. document.forms['mform1'].automatic_evaluate.disabled=1;
  171. document.forms['mform1'].show_answer.disabled=1;
  172. document.forms['mform1'].automatic_evaluate.value=0;
  173. document.forms['mform1'].show_answer.value=0;
  174. }
  175. else {
  176. document.forms['mform1'].automatic_evaluate.style.display='block';
  177. document.forms['mform1'].show_answer.style.display='block';
  178. document.forms['mform1'].automatic_evaluate.disabled=0;
  179. document.forms['mform1'].show_answer.disabled=0;
  180. document.forms['mform1'].automatic_evaluate.value=1;
  181. document.forms['mform1'].show_answer.value=1;
  182. }
  183. }
  184. } // if (ids[i]==id)
  185. } // for (i=0;i<ids.length;i++)
  186. } // else if (document.forms['mform1'].type_iassign.value==1)
  187. } // function config_ilm(id)
  188. function disable_answer (resp) {
  189. if (resp==0) {
  190. document.forms['mform1'].show_answer.value=0;
  191. document.forms['mform1'].show_answer.disabled=1;
  192. }
  193. else {
  194. document.forms['mform1'].show_answer.disabled=0;
  195. }
  196. }
  197. function view_ilm_manager () {
  198. document.forms['mform1'].filename.disabled=1;
  199. open_ilm_manager=window.open('$CFG->wwwroot/mod/iassign/ilm_manager.php?id=$COURSE->id&from=iassign&ilmid='+document.forms['mform1'].iassign_ilmid.value,'','width=1000,height=880,menubar=0,location=0,scrollbars,status,fullscreen,resizable');
  200. }
  201. //]]>
  202. </script>";
  203. //-------------------------------------------------------------------------------
  204. // Adding the "title_type_iassign" fieldset, where all the common settings are showed
  205. // Data is insert through 'locallib.php ! action() -> new_iassign($param) : 3433/6441'
  206. $mform->addElement('header', 'title_type_iassign', get_string('type_iassign', 'iassign'));
  207. $type_iassign = array();
  208. $type_iassign[1] = get_string('example', 'iassign');
  209. $type_iassign[2] = get_string('test', 'iassign');
  210. $type_iassign[3] = get_string('exercise', 'iassign');
  211. $mform->addElement('select', 'type_iassign', get_string('choose_type_activity', 'iassign'), $type_iassign, array('onChange' => 'config_type(this.value);'));
  212. $mform->setDefault('type_iassign', 3); // default type_iassign = 3
  213. $mform->addHelpButton('type_iassign', 'helptypeiassign', 'iassign');
  214. //-------------------------------------------------------------------------------
  215. // Adding the "data_activity" fieldset, where all the common settings are showed
  216. $mform->addElement('header', 'data_activity', get_string('data_activity', 'iassign'));
  217. $mform->addElement('static', 'author', get_string('author_id', 'iassign'));
  218. $mform->addElement('static', 'author_modified', get_string('author_id_modified', 'iassign'));
  219. // Adding the standard "name" field
  220. $mform->addElement('text', 'name', get_string('iassigntitle', 'iassign'), array('size' => '55', 'onChange' => 'confirm_name(this.value);'));
  221. $mform->setType('name', PARAM_TEXT);
  222. $mform->addRule('name', get_string('required', 'iassign'), 'required');
  223. // Adding the standard "proposition" field
  224. //moodle2: $mform->addElement('htmleditor', 'proposition', get_string('proposition', 'iassign'));
  225. $mform->addElement('editor', 'proposition', get_string('proposition', 'iassign')); //moodle 3
  226. $mform->setType('proposition', PARAM_RAW);
  227. $mform->addRule('proposition', get_string('required', 'iassign'), 'required');
  228. //-----------------------------------------------------------------------------
  229. // Adding the "interactivy_learning_module" fieldset, where all the common settings are showed
  230. $mform->addElement('header', 'interactivy_learning_module', get_string('interactivy_learning_module', 'iassign'));
  231. $mform->setExpanded('interactivy_learning_module');
  232. // Search iLM registered in the database
  233. // Field : 'iassign_ilmid'
  234. // $ilms = search_iLM(1);
  235. $ilms = $all_ilm;
  236. //TODO Trick: was difficult to use 'mform->addGroup(...)', then I made by hand <select name='iassign_ilmid'...>' with 'optgroup' by hand!
  237. //TODO But MoodleForm clear/do not register the 'iassign_ilmid' in 'locallib.php!new_iassign($param)'
  238. //TODO Then (in 'new_iassign(...)') get $_POST['iassign_ilmid'] directly!!
  239. //TODO See: 'locallib.php!add_edit_iassign()', 'locallib.php!function add_edit_iassign()' and 'locallib.php!function new_iassign($param)'
  240. //2019 $applets = array();
  241. //2019 foreach ($ilms as $ilm)
  242. //2019 $applets[$ilm->id] = $ilm->name . ' ' . $ilm->version;
  243. //2019 $mform->addElement('select', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $applets, array('onChange' => 'config_ilm(this.value);'));
  244. //2019 $mform->addHelpButton('iassign_ilmid', 'helpchoose_ilm', 'iassign');
  245. //D echo "<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>iassign_form.php:<br/>";
  246. // Split all iLM in those HTML from Java
  247. $list_html = array(); $list_html_id = array();
  248. $list_applets = array(); $list_applets_id = array();
  249. $num_iLM_html = $num_iLM_java = 0;
  250. foreach ($ilms as $ilm) {
  251. $type4 = substr($ilm->type, 0,4); // ensure the use of the only first 4 character (avoid difference if type is defined as 'HTML5')
  252. if (strcasecmp($type4, "HTML") == 0) { // Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
  253. $list_html[$num_iLM_html] = $ilm->name . ' (' . $ilm->version . ')';
  254. $list_html_id[$num_iLM_html] = $ilm->id;
  255. $num_iLM_html++;
  256. } else {
  257. $list_applets[$num_iLM_java] = $ilm->name . ' (' . $ilm->version . ')';
  258. $list_applets_id[$num_iLM_java] = $ilm->id;
  259. $num_iLM_java++;
  260. } //D echo $ilm->id . " : " . $ilm->type . " <br/>";
  261. }
  262. //D echo "" . get_string('interactivy_learning_module', 'iassign') . ": num_iLM_html=$num_iLM_html, num_iLM_java=$num_iLM_java<br/>";
  263. //D Interactive Learning Modules: num_iLM_html=0, num_iLM_java=9
  264. //TODO Assim o '/lib/formslib.php : get_data()' destroi o campo 'iassign_ilmid'...
  265. $html_group = get_string("group", "iassign") . " HTML";
  266. $java_group = get_string("group", "iassign") . " Java";
  267. $arrayHTML = array();
  268. $arrayJava = array();
  269. for ($ii=0; $ii<$num_iLM_html; $ii++) {
  270. $arrayHTML[$list_html_id[$ii]] = $list_html[$ii];
  271. }
  272. for ($ii=0; $ii<$num_iLM_java; $ii++) {
  273. $arrayJava[$list_applets_id[$ii]] = $list_applets[$ii];
  274. }
  275. $selectElems = array(
  276. $html_group => $arrayHTML,
  277. $java_group => $arrayJava
  278. );
  279. $ilm_select = $mform->addElement('selectgroups', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $selectElems);
  280. $mform->addHelpButton('iassign_ilmid', 'choose_iLM', 'iassign');
  281. if (isset($this->_customdata['iassign_ilmid'])) { // if it is first acces, define 'iassign_ilmid'
  282. // It is defined in "locallib.php:add_edit_iassign()": $mform = new mod_iassign_form(null, array('id'=>'mform1', 'iassign_ilmid'=>$param->iassign_ilmid));
  283. $iassign_ilmid = $this->_customdata['iassign_ilmid'];
  284. $code_javascript .= ' <script> document.forms[0].iassign_ilmid.value = ' . $iassign_ilmid . '</script>' . "\n";
  285. }
  286. else
  287. if ($result_recent && isset($result_recent->iassign_ilmid)) { // try the last iLM used...
  288. $code_javascript .= ' <script> document.forms[0].iassign_ilmid.value = ' . $result_recent->iassign_ilmid . '</script>' . "\n";
  289. }
  290. //TODO Adaptives to use API of MoodleForm
  291. // addOption($optgroup, $text, $value, $attributes=null)
  292. // $allOptions = array(); $ilmHtml = array(); $ilmJava = array();
  293. // $ilmHtml[] = $mform->createElement('optgroup', 'groupHtml', 'Group HTML');
  294. // for ($ii=0; $ii<$num_iLM_html; $ii++) $ilmHtml[] = $mform->createElement('optgroup', 'groupHtml', 'Group HTML'); //$allOptions[] = $mform->addOption('Group HTML', $list_html[$ii], $list_html_id[$ii]);
  295. // $mform->addGroup($ilmHtml, 'groupHtml', '', array(' '), false);
  296. // $list_html $list_html_id $num_iLM_html
  297. // $list_applets $list_applets_id $num_iLM_java
  298. // echo "<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>iassign_form.php: num_iLM_html=$num_iLM_html, num_iLM_java=$num_iLM_java<br/>";exit;
  299. // $allOptions[] = array('-1' => 'Group HTML'); for ($ii=0; $ii<$num_iLM_html; $ii++) $allOptions[] = array(''.$list_html_id[$ii] => $list_html[$ii]);
  300. // $allOptions[] = array('-2' => 'Group Java'); for ($ii=0; $ii<$num_iLM_java; $ii++) $allOptions[] = array(''.$list_applets_id[$ii] => $list_applets[$ii]);
  301. // $allOptions[$ilm->id] = $ilm->name . ' ' . $ilm->version;
  302. // $mform->addElement('select', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $allOptions, array('onChange' => 'config_ilm(this.value);'));
  303. // for ($ii=0; $ii<$num_iLM_html; $ii++) $allOptions[] = $list_html[$ii];
  304. // for ($jj=0; $jj<$num_iLM_java; $jj++) $allOptions[] = $list_applets[$jj];
  305. // $mform->addElement('select', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $allOptions, array('onChange' => 'config_ilm(this.value);'));
  306. // $mform->addHelpButton('iassign_ilmid', 'helpchoose_ilm', 'iassign');
  307. // Field 'action'
  308. $fileurl = "";
  309. $filename = "";
  310. if (!is_null($COURSE->iassign_file_id)) {
  311. $mform->addElement('hidden', 'action', 'viewpluginpage');
  312. $fs = get_file_storage();
  313. $file = $fs->get_file_by_id($COURSE->iassign_file_id);
  314. $fileurl = $CFG->wwwroot . "/pluginfile.php/" . $file->get_contextid() . "/mod_iassign/exercise" . '/' . $file->get_itemid() . $file->get_filepath() . $file->get_filename();
  315. $filename = $file->get_filename();
  316. }
  317. $html_div = '<div id="fitem_id_iassign_file_id" class="fitem required fitem_fgroup" style="padding: 35px; padding-left: 0;">';
  318. $html_div .= '<div class="fitemtitle col-md-3" style="padding: 0; float: left;">' . get_string('choose_file', 'iassign'); // 'Choose the file with the iLM activity'
  319. $html_div .= '<span><a><i class="icon fa fa-exclamation-circle text-danger fa-fw " title="' . get_string('requiredelement', 'form') . '" aria-label="' . get_string('requiredelement', 'form') . '" style="float: right; cursor: help; padding-right: 15px;"></i></a></span></div>';
  320. $html_div .= '<div class="felement fselect" id="error_message_file"><div class="file_iassign" id="file_border" style="display: inline;margin-left: 14px;border: 1px solid #cecfd1;padding: 8px;padding-right: 8px;border-radius: 4px;padding-right: 2px;">';
  321. $html_div .= '<i class="icon fa fa-file-text-o fa-fw" id="icon_doc" style="color: #8f8f8f;"></i><span id="iassign_file_link" style="color:#000000;"><a href="' . $fileurl . '" target="_blank" title="' . get_string('download_file', 'iassign') . $filename . '">' . $filename . '</a></span>';
  322. if ($fileurl != "")
  323. $html_div .= '&nbsp;&nbsp;&nbsp;';
  324. $html_div .= '<input onclick="view_ilm_manager()" name="add_ilm" value="' . get_string('add_ilm', 'iassign') . '" type="button" id="id_add_ilm"/></div>';
  325. $html_div .= '</div>';
  326. $html_div .= '</div>';
  327. $mform->addElement('html', $html_div);
  328. $mform->addElement('selectyesno', 'store_all_submissions', get_string('store_all_submissions', 'iassign'));
  329. $mform->setDefault('store_all_submissions', 1);
  330. $mform->addHelpButton('store_all_submissions', 'store_all_submissions_help', 'iassign');
  331. //-----------------------------------------------------------------------------
  332. //Applies only when the iLM is automatic evaluate.
  333. $mform->addElement('header', 'id_automatic_evaluate', get_string('only_automatic_evaluate', 'iassign'));
  334. // Using automatic evaluation activity? 0 - no / 1 – yes
  335. $mform->addElement('selectyesno', 'automatic_evaluate', get_string('automatic_evaluate', 'iassign'), array('onChange' => 'disable_answer(this.value);'));
  336. $mform->disabledIf('automatic_evaluate', 'type_iassign', 'eq', 1); //activity does not display if the type example
  337. $mform->setDefault('automatic_evaluate', 0);
  338. // @todo Ver código comentado
  339. //$mform->addHelpButton('automatic_evaluate', 'helpautomatic_evaluate', 'iassign');
  340. //Show automatic evaluation results to students? 0 - no / 1 - yes
  341. $mform->addElement('selectyesno', 'show_answer', get_string('show_answer', 'iassign'));
  342. $mform->disabledIf('show_answer', 'type_iassign', 'eq', 1); //activity does not display if the type example
  343. // $mform->disabledIf('show_answer', 'automatic_evaluate', 'neq', 0);
  344. $mform->setDefault('show_answer', 0);
  345. //$mform->addHelpButton('show_answer', 'helpshow_answer', 'iassign');
  346. //-----------------------------------------------------------------------------
  347. // Adding the "duration_activity" fieldset, where all the common settings are showed
  348. $mform->addElement('header', 'duration_activity', get_string('duration_activity', 'iassign'));
  349. $mform->addElement('date_time_selector', 'timeavailable', get_string('availabledate', 'iassign'));
  350. $mform->setDefault('timeavailable', time());
  351. $mform->disabledIf('timeavailable', 'type_iassign', 'eq', 1); // activity does not display if the type example
  352. $mform->addElement('date_time_selector', 'timedue', get_string('duedate', 'iassign'));
  353. $mform->setDefault('timedue', time() + 7 * 24 * 3600);
  354. $mform->disabledIf('timedue', 'type_iassign', 'eq', 1); //activity does not display if the type example
  355. //Allow sending late? 0 - no or unlocked / 1 - yes or locked
  356. $mform->addElement('selectyesno', 'preventlate', get_string('preventlate', 'iassign'));
  357. $mform->setDefault('preventlate', 0);
  358. $mform->addHelpButton('preventlate', 'helppreventlate', 'iassign');
  359. $mform->disabledIf('preventlate', 'type_iassign', 'eq', 1); //activity does not display if the type example
  360. $mform->disabledIf('preventlate', 'type_iassign', 'eq', 2); //activity does not display if the type test
  361. //Allow test after delivery? 0 - no or unlocked / 1 - yes or locked
  362. $mform->addElement('selectyesno', 'test', get_string('permission_test', 'iassign'));
  363. $mform->setDefault('test', 0);
  364. $mform->addHelpButton('test', 'helptest', 'iassign');
  365. $mform->disabledIf('test', 'type_iassign', 'eq', 1); //activity does not display if the type example
  366. $mform->disabledIf('test', 'type_iassign', 'eq', 2); //activity does not display if the type test
  367. //--------------
  368. $mform->addElement('header', 'op_val', get_string('op_val', 'iassign'));
  369. $mform->addElement('modgrade', 'grade', get_string('grade', 'iassign'));
  370. $mform->setDefault('grade', 100);
  371. $mform->disabledIf('grade', 'type_iassign', 'eq', 1); //activity does not display if the type example
  372. $mform->disabledIf('grade', 'type_iassign', 'eq', 2); //activity does not display if the type test
  373. $max_experiment_options = array(0 => get_string('ilimit', 'iassign'));
  374. for ($i = 1; $i <= 20; $i++)
  375. $max_experiment_options[$i] = $i;
  376. $mform->addElement('select', 'max_experiment', get_string('experiment', 'iassign'), $max_experiment_options);
  377. $mform->setDefault('max_experiment', 0);
  378. $mform->addHelpButton('max_experiment', 'helpexperiment', 'iassign');
  379. $mform->disabledIf('max_experiment', 'type_iassign', 'eq', 1); //activity does not display if the type example
  380. $mform->disabledIf('max_experiment', 'type_iassign', 'eq', 2); //activity does not display if the type test
  381. if ($COURSE->iassign_list) {
  382. //-------------- dependency
  383. $mform->addElement('header', 'headerdependency', get_string('dependency', 'iassign'));
  384. $mform->addHelpButton('headerdependency', 'helpdependency', 'iassign');
  385. foreach ($COURSE->iassign_list as $iassign) {
  386. $tmp = 'iassign_list[' . $iassign->id . ']';
  387. if ($iassign->enable == 1)
  388. $mform->addElement('checkbox', $tmp, $iassign->name);
  389. } //foreach ($COURSE->iassign_list as $iassign)
  390. } //if ($COURSE->iassign_list)
  391. $mform->addElement('hidden', 'dependency');
  392. $mform->setType('dependency', PARAM_RAW);
  393. //-------------- config
  394. $mform->addElement('header', 'config', get_string('general', 'iassign'));
  395. $visibleoptions = array(1 => get_string('show'), 0 => get_string('hide'));
  396. $mform->addElement('select', 'visible', get_string('visible', 'iassign'), $visibleoptions);
  397. $mform->setDefault('visible', 0);
  398. //-------------------------------------------------------------------------------
  399. // Hidden fields
  400. $mform->addElement('hidden', 'action');
  401. $mform->setType('action', PARAM_TEXT);
  402. $mform->addElement('hidden', 'oldname');
  403. $mform->setType('oldname', PARAM_TEXT);
  404. $mform->addElement('hidden', 'id');
  405. $mform->setType('id', PARAM_TEXT);
  406. $mform->addElement('hidden', 'iassign_id');
  407. $mform->setType('iassign_id', PARAM_TEXT);
  408. $mform->addElement('hidden', 'file', '0');
  409. $mform->setType('file', PARAM_INT);
  410. $mform->addElement('hidden', 'filename');
  411. $mform->setType('filename', PARAM_TEXT);
  412. $mform->addElement('hidden', 'fileold');
  413. $mform->setType('fileold', PARAM_TEXT);
  414. $mform->addElement('hidden', 'iassignid');
  415. $mform->setType('iassignid', PARAM_TEXT);
  416. $mform->addElement('hidden', 'author_name');
  417. $mform->setType('author_name', PARAM_TEXT);
  418. $mform->addElement('hidden', 'author_modified_name');
  419. $mform->setType('author_modified_name', PARAM_TEXT);
  420. $mform->addElement('hidden', 'timecreated');
  421. $mform->setType('timecreated', PARAM_TEXT);
  422. $mform->addElement('hidden', 'position');
  423. $mform->setType('position', PARAM_TEXT);
  424. $mform->addElement('html', $code_javascript);
  425. // add standard elements, common to all modules
  426. $this->add_action_buttons();
  427. } // function definition()
  428. function validation ($data, $files) {
  429. global $COURSE, $DB;
  430. $errors = parent::validation($data, $files);
  431. $mform = & $this->_form;
  432. $errors = array();
  433. if ($mform->elementExists('name')) {
  434. $value = trim($data['name']);
  435. if ($value == '') {
  436. $errors['name'] = get_string('required', 'iassign');
  437. }
  438. }
  439. if ($mform->elementExists('proposition')) {
  440. // echo "iassign_form.php : data['proposition'] :<br/>"; print_r($data['proposition']);
  441. // $data['proposition'] = Array ([text] => ... [format] => ) - segundo esta' vazio!
  442. if (is_array($data['proposition'])) {
  443. // foreach ($data['proposition'] as $key => $value) echo "key=" . $key . " - value=" . $value . "<br/>";
  444. // $data['proposition'] = [format] => ) key=text - value=
  445. $aux = $data['proposition']; // Format JSON: {"em":"...","pt":"..."}
  446. if (isset($aux['text']))
  447. $value = trim($aux['text']);
  448. else
  449. $value = trim($aux[0]);
  450. }
  451. else
  452. $value = trim($data['proposition']);
  453. if ($value == '') {
  454. $errors['proposition'] = get_string('required', 'iassign');
  455. }
  456. }
  457. // echo "iassign_form.php : mform :<br/>"; // print_r($mform); exit();
  458. if ($mform->elementExists('file')) {
  459. $value = trim($data['file']);
  460. if ($value == 0) {
  461. $errors['iassign_ilmid_t'] = get_string('required_iassign_file', 'iassign');
  462. print "<script>
  463. window.onload = function (e) {
  464. document.getElementById('error_message_file').innerHTML += '<span style=\"font-size: 80%;color: #d9534f; margin-left: 1em;\">".get_string('required_iassign_file', 'iassign')."</span>';
  465. document.getElementById('file_border').style.borderColor = '#d9534f';
  466. } </script>\n";
  467. }
  468. else {
  469. $fs = get_file_storage(); // Get reference to all files in Moodle data
  470. $file = $fs->get_file_by_id($value);
  471. if ($file) {
  472. // Verify if file extension is correct to iLM
  473. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $data['iassign_ilmid']));
  474. print "<script>
  475. window.onload = function (e) {
  476. document.getElementById('iassign_file_link').innerHTML = '".$file->get_filename()."&nbsp;&nbsp;&nbsp;';\n";
  477. if ($iassign_ilm->extension != pathinfo($file->get_filename(), PATHINFO_EXTENSION)) {
  478. $errors['iassign_ilmid_t'] = get_string('incompatible_extension_file', 'iassign');
  479. print " document.getElementById('error_message_file').innerHTML += '<span style=\"font-size: 80%;color: #d9534f; margin-left: 1em;\">".get_string('incompatible_extension_file', 'iassign')."</span>';
  480. document.getElementById('file_border').style.borderColor = '#d9534f';\n";
  481. }
  482. print " } </script>\n";
  483. }
  484. }
  485. }
  486. return $errors;
  487. } // function validation($data, $files)
  488. } // class mod_iassign_form extends moodleform