Skip to content

Commit

Permalink
qtypes: MDL-18711 Allow qtype plugins to have custom CSS and JavaScri…
Browse files Browse the repository at this point in the history
…pt on the editing page.
  • Loading branch information
tjhunt committed Mar 30, 2009
1 parent b810a4d commit 50da63e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
13 changes: 13 additions & 0 deletions lib/questionlib.php
Expand Up @@ -2084,6 +2084,19 @@ function get_html_head_contributions($questionlist, &$questions, &$states) {
return implode("\n", array_unique($contributions));
}

/**
* 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
*
Expand Down
5 changes: 3 additions & 2 deletions question/question.php
Expand Up @@ -239,6 +239,7 @@
} else {

$streditingquestion = $QTYPES[$question->qtype]->get_heading();
$headtags = get_editing_head_contributions($question);
if ($cm !== null) {
$strmodule = get_string('modulename', $cm->modname);
$strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $COURSE->id))
Expand All @@ -256,7 +257,7 @@
}
$navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
$navigation = build_navigation($navlinks);
print_header_simple($streditingquestion, '', $navigation, "", "", true, $strupdatemodule);
print_header_simple($streditingquestion, '', $navigation, '', $headtags, true, $strupdatemodule);

} else {
$navlinks = array();
Expand All @@ -265,7 +266,7 @@
$strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.
get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
$navigation = build_navigation($navlinks);
print_header_simple($streditingquestion, '', $navigation);
print_header_simple($streditingquestion, '', $navigation, '', $headtags);
}

// Display a heading, question editing form and possibly some extra content needed for
Expand Down
58 changes: 45 additions & 13 deletions question/type/questiontype.php
Expand Up @@ -804,13 +804,16 @@ function check_response(&$question, &$state){
}

// 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 <link ...> 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.
Expand All @@ -821,33 +824,62 @@ function check_response(&$question, &$state){
* 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';
}
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[] = '<link rel="stylesheet" type="text/css" href="' .
Expand Down

0 comments on commit 50da63e

Please sign in to comment.