From f07b9627b605414282091dcd0719e9fee763c6c7 Mon Sep 17 00:00:00 2001 From: jamiesensei Date: Thu, 28 Dec 2006 09:32:45 +0000 Subject: [PATCH] changes to how we generate the id attribute of forms and the naming convention of forms migrate blog edit to formslib --- blog/edit.html | 194 ------------- blog/edit.php | 270 +++++++++++------- blog/edit_form.php | 103 +++++++ course/modedit.php | 2 +- enrol/authorize/enrol_form.php | 2 +- lang/en_utf8/blog.php | 4 +- lang/en_utf8/moodle.php | 9 +- lib/formslib.php | 112 +++++++- lib/javascript-static.js | 57 ++-- login/change_password_form.php | 2 +- login/forgot_password_form.php | 2 +- .../type/online/assignment.class.php | 2 +- .../type/upload/assignment.class.php | 2 +- mod/chat/mod_form.php | 2 +- mod/choice/mod_form.php | 2 +- mod/data/comment_form.php | 2 +- mod/data/mod_form.php | 2 +- mod/exercise/mod_form.php | 2 +- mod/forum/mod_form.php | 2 +- mod/forum/post_form.php | 2 +- mod/glossary/comment_form.php | 2 +- mod/glossary/edit_form.php | 2 +- mod/glossary/mod_form.php | 2 +- mod/journal/mod_form.php | 2 +- mod/label/mod_form.php | 2 +- mod/lesson/mod_form.php | 2 +- mod/quiz/lib.php | 2 - mod/quiz/mod_form.php | 3 +- mod/survey/details.php | 2 +- 29 files changed, 428 insertions(+), 366 deletions(-) delete mode 100755 blog/edit.html create mode 100644 blog/edit_form.php diff --git a/blog/edit.html b/blog/edit.html deleted file mode 100755 index a91e26a8df..0000000000 --- a/blog/edit.html +++ /dev/null @@ -1,194 +0,0 @@ -id)) { - if ($tagsused = get_records('blog_tag_instance', 'entryid', $post->id)) { - foreach ($tagsused as $usedtag) { - $usedtags[] = $usedtag -> tagid; - } - } - } - - if ($usehtmleditor) { - // clean and convert to html before editting - $options = new object(); - $options->smiley = false; - $options->filter = false; - $options->nocache = true; - $post->summary = format_text($post->summary, $post->format, $options); - } -?> - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  - -
: - - -
- : -

- - '; - if ($usehtmleditor) { - helpbutton('richtext', get_string('helprichtext'), 'moodle', true, true); - } else { - emoticonhelpbutton('entry', 'body'); - } - ?> - -
- summary); ?> -
: - '; - } else { - choose_from_menu(format_text_menu(), 'format', $post->format, ''); - } - ?> - - - - -
- - - - - - - - - - - - -
- - - -
- -    - id)) { - $idsql = " AND bti.entryid = {$post->id}"; - } else { - $idsql = " AND bti.entryid = 0"; - } - - $pptag = ''; - if ($ptags = get_records_sql("SELECT t.* FROM - {$CFG->prefix}tags t, - {$CFG->prefix}blog_tag_instance bti - WHERE t.id = bti.tagid - AND t.type = 'personal' - $idsql")) { - - foreach ($ptags as $ptag) { - $pptag .= $ptag->text.','; - } - $pptag = rtrim($pptag, ","); - } - - - ?> - - "; - link_to_popup_window("/blog/tags.php",'popup',get_string('tagmanagement')); - } - ?> -
-
:
(
- maxbytes); ?> - - " /> - -
- : - - publishstate, ''); - ?> - -
- - - - - -   - - - -   - - -
-
diff --git a/blog/edit.php b/blog/edit.php index 5c1639f716..d5343add1b 100755 --- a/blog/edit.php +++ b/blog/edit.php @@ -46,81 +46,104 @@ if (!empty($courseid)) { $returnurl .= '&courseid='.$courseid; } -$errors = array(); -$post = new object(); // editing form data -$usehtmleditor = can_use_richtext_editor(); $strblogs = get_string('blogs','blog'); -/// Main switch for processing blog entry -switch ($action) { +if ($action=='delete'){ + if (!$existing) { + error('Incorrect blog post id'); + } + if (data_submitted() and $confirm and confirm_sesskey()) { + do_delete($existing); + redirect($returnurl); + } else { + $optionsyes = array('id'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid); + $optionsno = array('userid'=>$existing->userid, 'courseid'=>$courseid); + print_header("$SITE->shortname: $strblogs", $SITE->fullname); + blog_print_entry($existing); + echo '
'; + notice_yesno(get_string('blogdeleteconfirm', 'blog'), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get'); + print_footer(); + die; + } +} - case 'add': - if (data_submitted() and confirm_sesskey()) { - do_add($post, $errors); - if (empty($errors)) { - redirect($returnurl); +require_once('edit_form.php'); +$blogpostform = new blog_post_form(null, compact('existing', 'sitecontext')); + +if ($blogpostform->is_cancelled()){ + redirect($returnurl); +} elseif ($blogpostform->no_submit_button_pressed()) { + no_submit_button_actions($blogpostform, $sitecontext); + + +} elseif ($fromform = $blogpostform->data_submitted()){ + //save stuff in db + switch ($action) { + case 'add': + do_add($fromform); + break; + + case 'edit': + if (!$existing) { + error('Incorrect blog post id'); } - $post = stripslashes_safe($post); // no db access after this!! - // print form again - } else { - // prepare new empty form - $post->subject = ''; - $post->summary = ''; - $post->publishstate = 'draft'; - $post->format = $usehtmleditor ? FORMAT_HTML : FORMAT_MOODLE; + do_edit($fromform); + break; + default : + error('Unknown action!'); + } + redirect($returnurl); +} - } + +// gui setup +switch ($action) { + case 'add': + // prepare new empty form + $post->publishstate = 'draft'; $strformheading = get_string('addnewentry', 'blog'); + $post->action = $action; break; case 'edit': if (!$existing) { error('Incorrect blog post id'); } - if (data_submitted() and confirm_sesskey()) { - do_edit($post, $errors); - if (empty($errors)) { - redirect($returnurl); - } - $post = stripslashes_safe($post); // no db access after this!! - // print form again - } else { - $post->id = $existing->id; - $post->subject = $existing->subject; - $post->summary = $existing->summary; - $post->publishstate = $existing->publishstate; - $post->format = $existing->format; - } + $post->id = $existing->id; + $post->subject = $existing->subject; + $post->summary = $existing->summary; + $post->publishstate = $existing->publishstate; + $post->format = $existing->format; + $post->action = $action; $strformheading = get_string('updateentrywithid', 'blog'); - break; - case 'delete': - if (!$existing) { - error('Incorrect blog post id'); - } - if (data_submitted() and $confirm and confirm_sesskey()) { - do_delete($existing); - redirect($returnurl); + if ($ptags = get_records_sql_menu("SELECT t.id, t.text FROM + {$CFG->prefix}tags t, + {$CFG->prefix}blog_tag_instance bti + WHERE t.id = bti.tagid + AND t.type = 'personal' + AND bti.entryid = {$post->id}")) { + + $post->ptags = implode(', ', $ptags); } else { - $optionsyes = array('id'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid); - $optionsno = array('userid'=>$existing->userid, 'courseid'=>$courseid); - print_header("$SITE->shortname: $strblogs", $SITE->fullname); - blog_print_entry($existing); - echo '
'; - notice_yesno(get_string('blogdeleteconfirm', 'blog'), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get'); - print_footer(); - die; + //$idsql = " AND bti.entryid = 0"; + //was used but seems redundant. + $post->ptags = ''; + } + if ($otags = get_records_sql_menu("SELECT t.id, t.text FROM + {$CFG->prefix}tags t, + {$CFG->prefix}blog_tag_instance bti + WHERE t.id = bti.tagid + AND t.type = 'official' + AND bti.entryid = {$post->id}")){ + $post->otags = array_keys($otags); } break; - - default: + default : error('Unknown action!'); - break; } -// gui setup - // done here in order to allow deleting of posts with wrong user id above if (!$user = get_record('user', 'id', $userid)) { error('Incorrect user id'); @@ -129,21 +152,87 @@ if (!$user = get_record('user', 'id', $userid)) { print_header("$SITE->shortname: $strblogs", $SITE->fullname, ''.fullname($user).' -> '.$strblogs.' -> '.$strformheading,'','',true); +$blogpostform->set_defaults($post); +$blogpostform->display(); -echo '
'; -print_simple_box_start('center'); -require('edit.html'); -print_simple_box_end(); - -if ($usehtmleditor) { - use_html_editor(); -} print_footer(); + die; /***************************** edit.php functions ***************************/ +function no_submit_button_actions(&$blogpostform, $sitecontext){ + $mform =& $blogpostform->_form; + $data = $mform->exportValues(); + //sesskey has been checked already no need to check that + //check for official tags to add + if (!empty($data['addotags']) && !empty($data['otagsadd'])){ // adding official tag + $error = add_otag($data['otagsadd']); + } + if (!empty($error)){ + $mform->setElementError('otagsgrp', $error); + } + if (!empty($data['deleteotags']) && !empty($data['otags'])){ // adding official tag + delete_otags($data['otags'], $sitecontext); + } + $blogpostform->otags_select_setup(); +} +function delete_otags($tagids, $sitecontext){ + foreach ($tagids as $tagid) { + + if (!$tag = get_record('tags', 'id', $tagid)) { + error('Can not delete tag, tag doesn\'t exist'); + } + + if ($tag->type == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) { + //can not delete + error('Can not delete tag, you don\'t have permission to delete an official tag'); + } + + if ($tag->type == 'personal' and !has_capability('moodle/blog:managepersonaltags', $sitecontext)) { + //can not delete + error('Can not delete tag, you don\'t have permission to delete a personal tag'); + } + + // Delete the tag itself + if (!delete_records('tags', 'id', $tagid)) { + error('Can not delete tag'); + } + + // Deleteing all references to this tag + if (!delete_records('blog_tag_instance', 'tagid', $tagid)) { + error('Can not delete blog tag instances'); + } + + + } +} +function add_otag($otag){ + global $USER; + $error = ''; + if ($tag = get_record('tags', 'text', $otag)) { + if ($tag->type == 'official') { + // official tag already exist + $error = get_string('tagalready'); + } else { + $tag->type = 'official'; + update_record('tags', $tag); + $tagid = $tag->id; + } + } else { // Brand new offical tag + + $tag = new object(); + $tag->userid = $USER->id; + $tag->text = $otag; + $tag->type = 'official'; + + if (!$tagid = insert_record('tags', $tag)) { + error('Can not create tag!'); + } + } + return $error; +} /* * Delete blog post from database */ @@ -163,14 +252,9 @@ function do_delete($post) { /** * Write a new blog entry into database */ -function do_add(&$post, &$errors) { +function do_add($post) { global $CFG, $USER, $returnurl; - $post->subject = required_param('subject', PARAM_MULTILANG); - $post->summary = required_param('summary', PARAM_RAW); - $post->format = required_param('format', PARAM_INT); - $post->publishstate = required_param('publishstate', PARAM_ALPHA);; - if ($post->summary == '
') { $post->summary = ''; } @@ -190,16 +274,16 @@ function do_add(&$post, &$errors) { $post->userid = $USER->id; $post->lastmodified = time(); $post->created = time(); - + // Insert the new blog entry. if ($id = insert_record('post', $post)) { $post->id = $id; // add blog attachment if ($post->attachment = blog_add_attachment($post, 'attachment',$message)) { set_field("post", "attachment", $post->attachment, "id", $post->id); - } + } add_tags_info($post->id); - add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$posz->id, $post->subject); + add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject); } else { error('There was an error adding this post in the database', $returnurl); @@ -212,52 +296,32 @@ function do_add(&$post, &$errors) { * @param . $bloginfo_arg argument is reference to a blogInfo object. * @todo complete documenting this function. enable trackback and pingback between entries on the same server */ -function do_edit(&$post, &$errors) { +function do_edit($post) { global $CFG, $USER, $returnurl; - $post->id = required_param('id', PARAM_INT); - $post->subject = required_param('subject', PARAM_MULTILANG); - $post->summary = required_param('summary', PARAM_RAW); - $post->format = required_param('format', PARAM_INT); - $post->publishstate = required_param('publishstate', PARAM_ALPHA);; - - if ($post->summary == '
') { - $post->summary = ''; - } - - if ($post->subject == '') { - $errors['subject'] = get_string('emptytitle', 'blog'); - } - if ($post->summary == '') { - $errors['summary'] = get_string('emptybody', 'blog'); - } - - if (!empty($errors)) { - return; // no saving - } $post->lastmodified = time(); - + +/* TODO add attachment processing if ($newfilename = blog_add_attachment($post, 'attachment',$message)) { $post->attachment = $newfilename; } else { unset($post->attachment); - } + }*/ // update record if (update_record('post', $post)) { // delete all tags associated with this entry delete_records('blog_tag_instance', 'entryid', $post->id); // add them back add_tags_info($post->id); - + add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject); } else { error('There was an error updating this post in the database', $returnurl); } - } /** @@ -265,18 +329,18 @@ function do_edit(&$post, &$errors) { * @param int postid - id of the blog */ function add_tags_info($postid) { - + global $USER; - + $post = get_record('post', 'id', $postid); $tag = new object(); $tag->entryid = $post->id; $tag->userid = $post->userid; $tag->timemodified = time(); - + /// Attach official tags - if ($otags = optional_param('otags','', PARAM_INT)) { + if ($otags = optional_param('otags', '', PARAM_INT)) { foreach ($otags as $otag) { $tag->tagid = $otag; insert_record('blog_tag_instance', $tag); @@ -284,8 +348,8 @@ function add_tags_info($postid) { } /// Attach Personal Tags - if ($ptags = optional_param('ptags','', PARAM_NOTAGS)) { - $ptags = explode(',',$ptags); + if ($ptags = optional_param('ptags', '', PARAM_NOTAGS)) { + $ptags = explode(',', $ptags); foreach ($ptags as $ptag) { $ptag = trim($ptag); // check for existance @@ -302,7 +366,7 @@ function add_tags_info($postid) { if ($tagid = insert_record('tags', $ctag)) { $tag->tagid = $tagid; insert_record('blog_tag_instance', $tag); - } + } } } } diff --git a/blog/edit_form.php b/blog/edit_form.php new file mode 100644 index 0000000000..cef59f4e99 --- /dev/null +++ b/blog/edit_form.php @@ -0,0 +1,103 @@ +libdir.'/formslib.php'); + +class blog_edit_form extends moodleform { + + function definition() { + + global $CFG, $COURSE, $USER; + $mform =& $this->_form; + + $post = $this->_customdata['existing']; + $sitecontext = $this->_customdata['sitecontext']; + + // the upload manager is used directly in post precessing, moodleform::save_files() is not used yet + $this->_upload_manager = new upload_manager('attachment', true, false, $COURSE, false, 0, true, true); + $this->set_max_file_size($COURSE); + + $mform->addElement('header', 'general', get_string('general', 'form')); + $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), 'size="60"'); + $mform->setType('subject', PARAM_TEXT); + $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client'); + + $mform->addElement('htmleditor', 'summary', get_string('entrybody', 'blog'), array('rows'=>25)); + $mform->setType('summary', PARAM_RAW); + $mform->addRule('summary', get_string('emptybody', 'blog'), 'required', null, 'client'); + + $mform->addElement('format', 'format', get_string('format')); + + $mform->addElement('file', 'attachment', get_string('attachment', 'forum')); + + $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), blog_applicable_publish_states()); + $mform->setHelpButton('publishstate', array('publish_state', get_string('helppublish', 'blog'), 'blog')); + + $mform->addElement('header', 'tagshdr', get_string('tags', 'blog')); + + $mform->createElement('select', 'otags', get_string('otags','blog')); + + $js_escape = array( + "\r" => '\r', + "\n" => '\n', + "\t" => '\t', + "'" => "\\'", + '"' => '\"', + '\\' => '\\\\' + ); + + $otagsselEl =& $mform->addElement('select', 'otags', get_string('otags', 'blog'), array(), 'size="5"'); + $otagsselEl->setMultiple(true); + $this->otags_select_setup(); + + if (has_capability('moodle/blog:manageofficialtags', $sitecontext)){ + $deleteotagsmsg = strtr(get_string('deleteotagswarn', 'blog'), $js_escape); + $mform->registerNoSubmitButton('deleteotags'); + $mform->addElement('submit', 'deleteotags', get_string('delete'), + array('onclick'=>"return confirm('$deleteotagsmsg');")); + $mform->disabledIf('deleteotags', 'otags[]', 'noitemselected'); + $mform->setAdvanced('deleteotags'); + + $mform->registerNoSubmitButton('addotags'); + $otagsgrp = array(); + $otagsgrp[] =& $mform->createElement('text', 'otagsadd', get_string('addotags', 'blog')); + $otagsgrp[] =& $mform->createElement('submit', 'addotags', get_string('add')); + $mform->addGroup($otagsgrp, 'otagsgrp', get_string('addotags','blog'), array(' '), false); + $mform->setType('otagsadd', PARAM_NOTAGS); + $mform->setAdvanced('otagsgrp'); + } + + $mform->addElement('textarea', 'ptags', get_string('ptags', 'blog'), array('wrap'=>'soft')); + $mform->setType('ptagsadd', PARAM_NOTAGS); + + $this->add_action_buttons(); + + $mform->addElement('hidden', 'action'); + $mform->setType('action', PARAM_ACTION); + $mform->setDefault('action', ''); + + $mform->addElement('hidden', 'courseid'); + $mform->setType('courseid', PARAM_INT); + + $mform->addElement('hidden', 'id'); + $mform->setType('id', PARAM_INT); + $mform->setDefault('id', 0); + + } + + /** + * This function sets up options of otag select element. This is called from definition and also + * after adding new official tags with the add tag button. + * + */ + function otags_select_setup(){ + global $CFG; + $mform =& $this->_form; + $otagsselect =& $mform->getElement('otags'); + $otagsselect->removeOptions(); + if ($otags = get_records_sql_menu('SELECT id, text from '.$CFG->prefix.'tags WHERE type=\'official\' ORDER by text ASC')){ + $otagsselect->loadArray($otags); + } + } + +} +?> diff --git a/course/modedit.php b/course/modedit.php index 6eb81e3eb6..b4e7334f27 100644 --- a/course/modedit.php +++ b/course/modedit.php @@ -125,6 +125,7 @@ $mformclassname=$module->name.'_mod_form'; $cousesection=isset($cw->section)?$cw->section:$section; $mform=& new $mformclassname($form->instance, $cousesection, ((isset($cm))?$cm:null)); + $mform->set_defaults($form); if ($mform->is_cancelled()) { if ($return && isset($cm)){ @@ -276,7 +277,6 @@ $icon = ' '; print_heading_with_help($pageheading, "mods", $module->name, $icon); - $mform->set_defaults($form); $mform->display(); print_footer($course); } diff --git a/enrol/authorize/enrol_form.php b/enrol/authorize/enrol_form.php index 63a4453259..9edc15dff8 100755 --- a/enrol/authorize/enrol_form.php +++ b/enrol/authorize/enrol_form.php @@ -2,7 +2,7 @@ require_once($CFG->libdir.'/formslib.php'); -class authorize_enrol_form extends moodleform +class enrol_authorize_enrol_form extends moodleform { function definition() { diff --git a/lang/en_utf8/blog.php b/lang/en_utf8/blog.php index 3bbe80128e..db66304422 100755 --- a/lang/en_utf8/blog.php +++ b/lang/en_utf8/blog.php @@ -1,4 +1,4 @@ -firstname, -A new account has been created for you at \'$a->sitename\' +A new account has been created for you at \'$a->sitename\' and you have been issued with a new temporary password. Your current login information is now: username: $a->username password: $a->newpassword - (you will have to change your password + (you will have to change your password when you login for the first time) -To start using \'$a->sitename\', login at +To start using \'$a->sitename\', login at $a->link In most mail programs, this should appear as a blue link @@ -975,7 +975,7 @@ $string['nopotentialcreators'] = 'No potential course creators'; $string['nopotentialstudents'] = 'No potential students'; $string['nopotentialteachers'] = 'No potential teachers'; $string['norecentactivity'] = 'No recent activity'; -$string['noreplybouncemessage'] = 'You have replied to a no-reply email address. If you were attempting to reply to a forum post, please instead reply using the $a forums. +$string['noreplybouncemessage'] = 'You have replied to a no-reply email address. If you were attempting to reply to a forum post, please instead reply using the $a forums. Following is the content of your email:'; $string['noreplybouncesubject'] = '$a - bounced email.'; @@ -1325,6 +1325,7 @@ $string['supplyinfo'] = 'Please supply some information about yourself'; $string['switchrolereturn'] = 'Return to my normal role'; $string['switchroleto'] = 'Switch role to...'; $string['tag'] = 'Tag'; +$string['tagalready'] = 'This tag already exists'; $string['tagmanagement'] = 'Add/delete tags ...'; $string['tags'] = 'Tags'; $string['targetrole'] = 'Target role'; diff --git a/lib/formslib.php b/lib/formslib.php index 88c0f51b9a..68246a3b53 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -49,6 +49,7 @@ if ($CFG->debug >= DEBUG_ALL){ PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'pear_handle_error'); } + /** * Moodle specific wrapper that separates quickforms syntax from moodle code. You won't directly * use this class you should write a class defintion which extends this class or a more specific @@ -107,7 +108,8 @@ class moodleform { if (empty($action)){ $action = strip_querystring(qualified_me()); } - //strip '_form' from the end of class name to make form 'id' attribute. + + //strip '_form' from the end of class name to make form name attribute. $this->_formname = preg_replace('/_form$/', '', get_class($this), 1); $this->_customdata = $customdata; $this->_form =& new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes); @@ -279,6 +281,9 @@ class moodleform { } $mform =& $this->_form; $nosubmit = false; + if (!$this->is_submitted()){ + return false; + } foreach ($mform->_noSubmitButtons as $nosubmitbutton){ if (optional_param($nosubmitbutton, 0, PARAM_RAW)){ $nosubmit = true; @@ -297,9 +302,7 @@ class moodleform { function is_validated() { static $validated = null; // one validation is enough $mform =& $this->_form; - if ($this->no_submit_button_pressed()){ - return false; - } + if ($validated === null) { $internal_val = $mform->validate(); $moodle_val = $this->validation($mform->exportValues(null, true)); @@ -324,7 +327,11 @@ class moodleform { } $validated = ($internal_val and $moodle_val and $file_val); } - return $validated; + if ($this->no_submit_button_pressed()){ + return false; + } else { + return $validated; + } } /** @@ -576,6 +583,15 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { */ var $_showAdvanced = null; + /** + * The form name is derrived from the class name of the wrapper minus the trailing form + * It is a name with words joined by underscores whereas the id attribute is words joined by + * underscores. + * + * @var unknown_type + */ + var $_formName = ''; + /** * Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless * @param string $formName Form's name. @@ -591,8 +607,9 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { HTML_Common::HTML_Common($attributes); $target = empty($target) ? array() : array('target' => $target); + $this->_formName = $formName; //no 'name' atttribute for form in xhtml strict : - $attributes = array('action'=>$action, 'method'=>$method, 'id'=>$formName) + $target; + $attributes = array('action'=>$action, 'method'=>$method, 'id'=>strtr($formName, '_', '-')) + $target; $this->updateAttributes($attributes); //this is custom stuff for Moodle : @@ -648,9 +665,9 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { return; } else { //if setShowAdvanced is called without any preference //make the default to not show advanced elements. - $showadvancedNow = 0; + $showadvancedNow = get_user_preferences( + moodle_strtolower($this->_formName.'_showadvanced', 0)); } - } //value of hidden element $hiddenLast = optional_param('mform_showadvanced_last', -1, PARAM_INT); @@ -665,6 +682,9 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { $next = $hiddenLast; } $this->_showAdvanced = $next; + if ($showadvancedNow != $next){ + set_user_preference($this->_formName.'_showadvanced', $next); + } $this->setConstants(array('mform_showadvanced_last'=>$next)); } function getShowAdvanced(){ @@ -858,6 +878,74 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { return $unfiltered; } } + /** + * Adds a validation rule for the given field + * + * If the element is in fact a group, it will be considered as a whole. + * To validate grouped elements as separated entities, + * use addGroupRule instead of addRule. + * + * @param string $element Form element name + * @param string $message Message to display for invalid data + * @param string $type Rule type, use getRegisteredRules() to get types + * @param string $format (optional)Required for extra rule data + * @param string $validation (optional)Where to perform validation: "server", "client" + * @param boolean $reset Client-side validation: reset the form element to its original value if there is an error? + * @param boolean $force Force the rule to be applied, even if the target form element does not exist + * @since 1.0 + * @access public + * @throws HTML_QuickForm_Error + */ + function addRule($element, $message, $type, $format=null, $validation='server', $reset = false, $force = false) + { + parent::addRule($element, $message, $type, $format, $validation, $reset, $force); + if ($validation == 'client') { + $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);')); + } + + } // end func addRule + /** + * Adds a validation rule for the given group of elements + * + * Only groups with a name can be assigned a validation rule + * Use addGroupRule when you need to validate elements inside the group. + * Use addRule if you need to validate the group as a whole. In this case, + * the same rule will be applied to all elements in the group. + * Use addRule if you need to validate the group against a function. + * + * @param string $group Form group name + * @param mixed $arg1 Array for multiple elements or error message string for one element + * @param string $type (optional)Rule type use getRegisteredRules() to get types + * @param string $format (optional)Required for extra rule data + * @param int $howmany (optional)How many valid elements should be in the group + * @param string $validation (optional)Where to perform validation: "server", "client" + * @param bool $reset Client-side: whether to reset the element's value to its original state if validation failed. + * @since 2.5 + * @access public + * @throws HTML_QuickForm_Error + */ + function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $validation = 'server', $reset = false) + { + parent::addGroupRule($group, $arg1, $type, $format, $howmany, $validation, $reset); + if (is_array($arg1)) { + foreach ($arg1 as $elementIndex => $rules) { + foreach ($rules as $rule) { + $validation = (isset($rule[3]) && 'client' == $rule[3])? 'client': 'server'; + + if ('client' == $validation) { + $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_formName . '; } catch(e) { return true; } return myValidator(this);')); + } + } + } + } elseif (is_string($arg1)) { + + if ($validation == 'client') { + $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $formname . '; } catch(e) { return true; } return myValidator(this);')); + } + } + } // end func addGroupRule + + // }}} /** * Returns the client side validation script * @@ -986,7 +1074,7 @@ function qf_errorHandler(element, _qfMsg) { list($jsArr,$element)=$jsandelement; //end of fix $js .= ' -function validate_' . $this->_attributes['id'] . '_' . $elementName . '(element) { +function validate_' . $this->_formName . '_' . $elementName . '(element) { var value = \'\'; var errFlag = new Array(); var _qfGroups = {}; @@ -1000,12 +1088,12 @@ function validate_' . $this->_attributes['id'] . '_' . $elementName . '(element) } '; $validateJS .= ' - ret = validate_' . $this->_attributes['id'] . '_' . $elementName.'(frm.elements[\''.$elementName.'\']) && ret;'; + ret = validate_' . $this->_formName . '_' . $elementName.'(frm.elements[\''.$elementName.'\']) && ret;'; // Fix for bug displaying errors for elements in a group //unset($element); //$element =& $this->getElement($elementName); //end of fix - $valFunc = 'validate_' . $this->_attributes['id'] . '_' . $elementName . '(this)'; + $valFunc = 'validate_' . $this->_formName . '_' . $elementName . '(this)'; $onBlur = $element->getAttribute('onBlur'); $onChange = $element->getAttribute('onChange'); $element->updateAttributes(array('onBlur' => $onBlur . $valFunc, @@ -1013,7 +1101,7 @@ function validate_' . $this->_attributes['id'] . '_' . $elementName . '(element) } // do not rely on frm function parameter, because htmlarea breaks it when overloading the onsubmit method $js .= ' -function validate_' . $this->_attributes['id'] . '(frm) { +function validate_' . $this->_formName . '(frm) { if (skipClientValidation) { return true; } diff --git a/lib/javascript-static.js b/lib/javascript-static.js index eb0b8e8e8b..c7e4444dc1 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -58,10 +58,24 @@ function lockoptions(form, master, subitems) { return(true); } +function lockoption(form,item) { + eval("document."+form+"."+item+".disabled=true");/* IE thing */ + if(document.forms[form].elements['h'+item]) { + eval("document."+form+".h"+item+".value=1"); + } +} + +function unlockoption(form,item) { + eval("document."+form+"."+item+".disabled=false");/* IE thing */ + if(document.forms[form].elements['h'+item]) { + eval("document."+form+".h"+item+".value=0"); + } +} function lockoptionsall(formid) { - var lock=new Object(); - var items=eval(formid+'items'); - var form=document.forms[formid]; + var lock = new Object(); + var varname = formid.replace(/\-/g, '_'); + var items = eval(varname+'items'); + var form = document.forms[formid]; for (var master in items){ var subitems=items[master].dependents; var thislock; @@ -94,7 +108,8 @@ function lockoptionsall(formid) { } function lockoptionsallsetup(formid) { - var items=eval(formid+'items'); + var varname = formid.replace(/\-/g, '_'); + var items=eval(varname+'items'); var form=document.forms[formid]; for (var i = 0; i < form.elements.length; i++){ var found=false; @@ -113,26 +128,11 @@ function lockoptionsallsetup(formid) { } } } - if (found){ - } } return lockoptionsall(formid); } -function lockoption(form,item) { - eval("document."+form+"."+item+".disabled=true");/* IE thing */ - if(document.forms[form].elements['h'+item]) { - eval("document."+form+".h"+item+".value=1"); - } -} - -function unlockoption(form,item) { - eval("document."+form+"."+item+".disabled=false");/* IE thing */ - if(document.forms[form].elements['h'+item]) { - eval("document."+form+".h"+item+".value=0"); - } -} function submitFormById(id) { var theform = document.getElementById(id); @@ -219,15 +219,16 @@ function findChildNodes(start, tagName, elementClass, elementID, elementName) { } } } - if( (child.nodeType == 1) &&//element node type - (!tagName || child.nodeName == tagName) && - (!elementClass || classfound)&& - (!elementID || child.id == elementID) && - (!elementName || child.name == elementName)) - { - children = children.concat(child); - } else { - children = children.concat(findChildNodes(child, tagName, elementClass, elementID, elementName)); + if(child.nodeType == 1) { //element node type + if ( (!tagName || child.nodeName == tagName) && + (!elementClass || classfound)&& + (!elementID || child.id == elementID) && + (!elementName || child.name == elementName)) + { + children = children.concat(child); + } else { + children = children.concat(findChildNodes(child, tagName, elementClass, elementID, elementName)); + } } } return children; diff --git a/login/change_password_form.php b/login/change_password_form.php index 1a3438dda9..56cf683ec3 100644 --- a/login/change_password_form.php +++ b/login/change_password_form.php @@ -2,7 +2,7 @@ require_once $CFG->libdir.'/formslib.php'; -class change_password_form extends moodleform { +class login_change_password_form extends moodleform { function definition() { global $USER; diff --git a/login/forgot_password_form.php b/login/forgot_password_form.php index bf2b386e60..f035df54c6 100644 --- a/login/forgot_password_form.php +++ b/login/forgot_password_form.php @@ -2,7 +2,7 @@ require_once $CFG->libdir.'/formslib.php'; -class forgot_password_form extends moodleform { +class login_forgot_password_form extends moodleform { function definition() { $mform =& $this->_form; diff --git a/mod/assignment/type/online/assignment.class.php b/mod/assignment/type/online/assignment.class.php index 592cc73799..28178ade69 100644 --- a/mod/assignment/type/online/assignment.class.php +++ b/mod/assignment/type/online/assignment.class.php @@ -240,7 +240,7 @@ class assignment_online extends assignment_base { } -class assignment_online_edit_form extends moodleform { +class mod_assignment_online_edit_form extends moodleform { function definition() { $mform =& $this->_form; diff --git a/mod/assignment/type/upload/assignment.class.php b/mod/assignment/type/upload/assignment.class.php index 274f2ce568..3c741c8ea9 100644 --- a/mod/assignment/type/upload/assignment.class.php +++ b/mod/assignment/type/upload/assignment.class.php @@ -914,7 +914,7 @@ class assignment_upload extends assignment_base { } -class assignment_upload_notes_form extends moodleform { +class mod_assignment_upload_notes_form extends moodleform { function definition() { $mform =& $this->_form; diff --git a/mod/chat/mod_form.php b/mod/chat/mod_form.php index 047913bf9b..b8f688f3be 100644 --- a/mod/chat/mod_form.php +++ b/mod/chat/mod_form.php @@ -1,7 +1,7 @@ libdir.'/formslib.php'; -class data_comment_form extends moodleform { +class mod_data_comment_form extends moodleform { function definition() { $mform =& $this->_form; diff --git a/mod/data/mod_form.php b/mod/data/mod_form.php index 695bc78321..4a5fff23be 100644 --- a/mod/data/mod_form.php +++ b/mod/data/mod_form.php @@ -1,7 +1,7 @@ libdir.'/formslib.php'); -class forum_post_form extends moodleform { +class mod_forum_post_form extends moodleform { function definition() { diff --git a/mod/glossary/comment_form.php b/mod/glossary/comment_form.php index b1667d6e24..a72674084b 100644 --- a/mod/glossary/comment_form.php +++ b/mod/glossary/comment_form.php @@ -2,7 +2,7 @@ require_once $CFG->libdir.'/formslib.php'; -class glossary_comment_form extends moodleform { +class mod_glossary_comment_form extends moodleform { function definition() { $mform =& $this->_form; diff --git a/mod/glossary/edit_form.php b/mod/glossary/edit_form.php index d803c3d485..a40703090e 100644 --- a/mod/glossary/edit_form.php +++ b/mod/glossary/edit_form.php @@ -1,7 +1,7 @@ dirroot.'/lib/formslib.php'); -class glossary_entry_form extends moodleform { +class mod_glossary_entry_form extends moodleform { function definition() { diff --git a/mod/glossary/mod_form.php b/mod/glossary/mod_form.php index 32a6f52243..52ea260a22 100644 --- a/mod/glossary/mod_form.php +++ b/mod/glossary/mod_form.php @@ -1,7 +1,7 @@ mform_showadvanced_last); // Update the events relating to this quiz. // This is slightly inefficient, deleting the old events and creating new ones. However, diff --git a/mod/quiz/mod_form.php b/mod/quiz/mod_form.php index fd1374537b..172c8c7711 100644 --- a/mod/quiz/mod_form.php +++ b/mod/quiz/mod_form.php @@ -3,7 +3,7 @@ require_once ('moodleform_mod.php'); require_once("$CFG->dirroot/mod/quiz/locallib.php"); -class quiz_mod_form extends moodleform_mod { +class mod_quiz_mod_form extends moodleform_mod { var $_feedbacks; function definition() { @@ -11,7 +11,6 @@ class quiz_mod_form extends moodleform_mod { global $COURSE, $CFG, $QUIZ_GRADE_METHOD; $mform =& $this->_form; - $mform->setShowAdvanced(get_user_preferences('quiz_optionsettingspref', 0)); //------------------------------------------------------------------------------- $mform->addElement('header', 'general', get_string('general', 'form')); diff --git a/mod/survey/details.php b/mod/survey/details.php index 4e4630d755..43289e0765 100644 --- a/mod/survey/details.php +++ b/mod/survey/details.php @@ -2,7 +2,7 @@ require_once("../../config.php"); include_once $CFG->libdir.'/formslib.php'; - class survey_details_form extends moodleform { + class mod_survey_details_form extends moodleform { function definition() { $mform =& $this->_form; -- 2.39.5