Index: lib/questionlib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/questionlib.php,v retrieving revision 1.119.2.26 diff -u -r1.119.2.26 questionlib.php --- lib/questionlib.php 18 Mar 2009 05:34:03 -0000 1.119.2.26 +++ lib/questionlib.php 30 Mar 2009 03:08:34 -0000 @@ -1694,6 +1694,19 @@ } /** + * Like @see{get_html_head_contributions} but for the editing page + * question/question.php. + * + * @param $question A question object. Only $question->qtype is used. + * @return string some HTML code that can go inside the head tag. + */ +function get_editing_head_contributions($question) { + global $QTYPES; + $contributions = $QTYPES[$question->qtype]->get_editing_head_contributions(); + return implode("\n", array_unique($contributions)); +} + +/** * Prints a question * * Simply calls the question type specific print_question() method. Index: question/type/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/questiontype.php,v retrieving revision 1.74.2.23 diff -u -r1.74.2.23 questiontype.php --- question/type/questiontype.php 16 Mar 2009 01:52:25 -0000 1.74.2.23 +++ question/type/questiontype.php 30 Mar 2009 03:08:35 -0000 @@ -738,13 +738,16 @@ } // Used by the following function, so that it only returns results once per quiz page. - var $already_done = false; + private $htmlheadalreadydone = false; /** * If this question type requires extra CSS or JavaScript to function, * then this method will return an array of tags that reference * those stylesheets. This function will also call require_js() * from ajaxlib.php, to get any necessary JavaScript linked in too. * + * Remember that there may be more than one question of this type on a page. + * try to avoid including JS and CSS more than once. + * * The two parameters match the first two parameters of print_question. * * @param object $question The question object. @@ -755,20 +758,55 @@ * integer array keys, which have no significance. */ function get_html_head_contributions(&$question, &$state) { + // We only do this once for this question type, no matter how often this + // method is called on one page. + if ($this->htmlheadalreadydone) { + return array(); + } + $this->htmlheadalreadydone = true; + // By default, we link to any of the files styles.css, styles.php, // script.js or script.php that exist in the plugin folder. // Core question types should not use this mechanism. Their styles // should be included in the standard theme. + return $this->find_standard_scripts_and_css(); + } - // We only do this once - // for this question type, no matter how often this method is called. - if ($this->already_done) { - return array(); - } - $this->already_done = true; + /** + * Like @see{get_html_head_contributions}, but this method is for CSS and + * JavaScript required on the question editing page question/question.php. + * + * @return an array of bits of HTML to add to the head of pages where + * this question is print_question-ed in the body. The array should use + * integer array keys, which have no significance. + */ + function get_editing_head_contributions() { + // By default, we link to any of the files styles.css, styles.php, + // script.js or script.php that exist in the plugin folder. + // Core question types should not use this mechanism. Their styles + // should be included in the standard theme. + return $this->find_standard_scripts_and_css(); + } + /** + * Utility method used by @see{get_html_head_contributions} and + * @see{get_editing_head_contributions}. This looks for any of the files + * styles.css, styles.php, script.js or script.php that exist in the plugin + * folder and ensures they get included. + * + * @return array as required by get_html_head_contributions or get_editing_head_contributions. + */ + protected function find_standard_scripts_and_css() { $plugindir = $this->plugin_dir(); $baseurl = $this->plugin_baseurl(); + + if (file_exists($plugindir . '/script.js')) { + require_js($baseurl . '/script.js'); + } + if (file_exists($plugindir . '/script.php')) { + require_js($baseurl . '/script.php'); + } + $stylesheets = array(); if (file_exists($plugindir . '/styles.css')) { $stylesheets[] = 'styles.css'; @@ -776,12 +814,6 @@ if (file_exists($plugindir . '/styles.php')) { $stylesheets[] = 'styles.php'; } - if (file_exists($plugindir . '/script.js')) { - require_js($baseurl . '/script.js'); - } - if (file_exists($plugindir . '/script.php')) { - require_js($baseurl . '/script.php'); - } $contributions = array(); foreach ($stylesheets as $stylesheet) { $contributions[] = 'id.'">'. get_string("editquestions", "quiz").' -> '.$streditingquestion; $navigation = build_navigation($navlinks); - print_header_simple($streditingquestion, '', $navigation); + print_header_simple($streditingquestion, '', $navigation, '', $headtags); }