iassign_form.php 28 KB

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