Skip to content

Commit

Permalink
mod quiz + questions MDL-22370 convert JavaScript to YUI3 and modules.
Browse files Browse the repository at this point in the history
Note, quiz editing JS has not been done yet.
  • Loading branch information
timhunt committed Aug 4, 2010
1 parent 24f17d7 commit ff065f9
Show file tree
Hide file tree
Showing 15 changed files with 418 additions and 478 deletions.
39 changes: 26 additions & 13 deletions lib/questionlib.php
Expand Up @@ -2083,16 +2083,31 @@ function question_format_grade($cmoptions, $grade) {
*/
function question_init_qengine_js() {
global $CFG, $PAGE, $OUTPUT;
$config = array(
'actionurl' => $CFG->wwwroot . '/question/toggleflag.php',
'flagicon' => '' . $OUTPUT->pix_url('i/flagged'),
'unflagicon' => '' . $OUTPUT->pix_url('i/unflagged'),
'flagtooltip' => get_string('clicktoflag', 'question'),
'unflagtooltip' => get_string('clicktounflag', 'question'),
'flaggedalt' => get_string('flagged', 'question'),
'unflaggedalt' => get_string('notflagged', 'question'),
static $done = false;
if ($done) {
return;
}
$module = array(
'name' => 'core_question_flags',
'fullpath' => '/question/flags.js',
'requires' => array('base', 'dom', 'event-delegate', 'io-base'),
);
$actionurl = $CFG->wwwroot . '/question/toggleflag.php';
$flagattributes = array(
0 => array(
'src' => $OUTPUT->pix_url('i/unflagged') . '',
'title' => get_string('clicktoflag', 'question'),
'alt' => get_string('notflagged', 'question'),
),
1 => array(
'src' => $OUTPUT->pix_url('i/flagged') . '',
'title' => get_string('clicktounflag', 'question'),
'alt' => get_string('flagged', 'question'),
),
);
$PAGE->requires->data_for_js('qengine_config', $config);
$PAGE->requires->js_init_call('M.core_question_flags.init',
array($actionurl, $flagattributes), false, $module);
$done = true;
}

/// FUNCTIONS THAT SIMPLY WRAP QUESTIONTYPE METHODS //////////////////////////////////
Expand All @@ -2109,12 +2124,10 @@ function question_init_qengine_js() {
* @param array $states an array of question state objects, whose keys are question ids.
* Must contain the state of all the questions in $questionlist
*/
function get_html_head_contributions($questionlist, &$questions, &$states) {
function question_get_html_head_contributions($questionlist, &$questions, &$states) {
global $CFG, $PAGE, $QTYPES;

// The question engine's own JavaScript.
$PAGE->requires->yui2_lib('connection');
$PAGE->requires->js('/question/qengine.js');
question_init_qengine_js();

// Anything that questions on this page need.
Expand All @@ -2131,7 +2144,7 @@ function get_html_head_contributions($questionlist, &$questions, &$states) {
* @param $question A question object. Only $question->qtype is used.
* @return string Deprecated. Some HTML code that can go inside the head tag.
*/
function get_editing_head_contributions($question) {
function question_get_editing_head_contributions($question) {
global $QTYPES;
$QTYPES[$question->qtype]->get_editing_head_contributions();
}
Expand Down
106 changes: 41 additions & 65 deletions mod/quiz/accessrules.php
Expand Up @@ -176,8 +176,8 @@ public function show_attempt_timer_if_needed($attempt, $timenow) {
/// Make sure the timer starts just above zero. If $timeleft was <= 0, then
/// this will just have the effect of causing the quiz to be submitted immediately.
$timerstartvalue = max($timeleft, 1);
$PAGE->requires->js_function_call('quiz_timer.initialise',
array(get_string('timesup', 'quiz'), $timerstartvalue));
$PAGE->requires->js_init_call('M.mod_quiz.timer.init',
array($timerstartvalue), false, quiz_get_js_module());
}
}

Expand Down Expand Up @@ -207,26 +207,45 @@ public function safebrowser_required($canpreview) {
* or start a new one. This affects whether a javascript alert is shown.
*/
public function print_start_attempt_button($canpreview, $buttontext, $unfinished) {
/// Do we need a confirm javascript alert?
global $OUTPUT;
if ($unfinished) {
$strconfirmstartattempt = '';
} else {
$strconfirmstartattempt = $this->confirm_start_attempt_message();
}

/// Show the start button, in a div that is initially hidden.
echo '<div id="quizstartbuttondiv">';
if ($this->securewindow_required($canpreview)) {
$this->_securewindowrule->print_start_attempt_button($buttontext, $strconfirmstartattempt);
} else {
$button = new single_button(new moodle_url($this->_quizobj->start_attempt_url(), array('cmid' => $this->_quizobj->get_cmid())), $buttontext);
$url = new moodle_url($this->_quizobj->start_attempt_url(),
array('cmid' => $this->_quizobj->get_cmid()));
$button = new single_button($url, $buttontext);
$button->class .= ' quizstartbuttondiv';

if (!$unfinished) {
$strconfirmstartattempt = $this->confirm_start_attempt_message();
if ($strconfirmstartattempt) {
$button->add_confirm_action($strconfirmstartattempt);
}
echo $OUTPUT->render($button);
}
echo "</div>\n";

$warning = '';

if ($this->securewindow_required($canpreview)) {
$button->class .= ' quizsecuremoderequired';

$popupoptions = array(
'left' => 0,
'top' => 0,
'fullscreen' => true,
'scrollbars' => true,
'resizeable' => false,
'directories' => false,
'toolbar' => false,
'titlebar' => false,
'location' => false,
'status' => false,
'menubar' => false,
);
$button->popup_action(new popup_action('click', $url, 'quizpopup', $popupoptions));

$warning = html_writer::tag('noscript',
$OUTPUT->heading(get_string('noscript', 'quiz')));
}

echo $OUTPUT->render($button) . $warning;
}

/**
Expand All @@ -249,7 +268,7 @@ public function back_to_view_page($canpreview, $message = '') {
$delay = 0;
}
echo $OUTPUT->box_end();
$PAGE->requires->js_function_call('quiz_secure_window.close', array($url, $delay));
$PAGE->requires->js_function_call('M.mod_quiz.secure_window.close', array($url, $delay));
echo $OUTPUT->footer();
die();
} else {
Expand All @@ -259,7 +278,7 @@ public function back_to_view_page($canpreview, $message = '') {

/**
* Print a control to finish the review. Normally this is just a link, but if we are
* in a secure window, it needs to be a button that does quiz_secure_window.close.
* in a secure window, it needs to be a button that does M.mod_quiz.secure_window.close.
*
* @param boolean $canpreview This affects whether we have to worry about secure window stuff.
*/
Expand All @@ -271,7 +290,7 @@ public function print_finish_review_link($canpreview, $return = false) {
if ($this->securewindow_required($canpreview)) {
$url = addslashes_js(htmlspecialchars($url));
$output .= '<input type="button" value="' . get_string('finishreview', 'quiz') . '" ' .
"onclick=\"quiz_secure_window.close('$url', 0)\" />\n";
"onclick=\"M.mod_quiz.secure_window.close('$url', 0)\" />\n";
} else {
$output .= '<a href="' . $url . '">' . get_string('finishreview', 'quiz') . "</a>\n";
}
Expand Down Expand Up @@ -699,45 +718,6 @@ public function time_left($attempt, $timenow) {
* A rule implementing the ipaddress check against the ->submet setting.
*/
class securewindow_access_rule extends quiz_access_rule_base {
private $windowoptions = "left=0, top=0, height='+window.screen.height+', width='+window.screen.width+', channelmode=yes, fullscreen=yes, scrollbars=yes, resizeable=no, directories=no, toolbar=no, titlebar=no, location=no, status=no, menubar=no";

/**
* Output the start attempt button. The button will initially be hidden,
* with JavaScript to reveal it, and a noscript tag saying that the quiz
* requires JavaScript.
*
* @param string $buttontext the desired button caption.
* @param string $strconfirmstartattempt optional message to diplay in
* a JavaScript altert before the button submits.
*/
public function print_start_attempt_button($buttontext, $strconfirmstartattempt) {
global $CFG, $SESSION, $PAGE, $OUTPUT;

$attempturl = $this->_quizobj->start_attempt_url() . '?cmid=' . $this->_quizobj->get_cmid() .
'&sesskey=' . sesskey();
$window = 'quizpopup';

if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) {
$attempturl = $SESSION->sid_process_url($attempturl);
}

echo '<input id="quizstartbutton" type="button" value="' .
s($buttontext) . '" style="display: none;" onclick="javascript:';
if ($strconfirmstartattempt) {
echo "if (confirm('" . addslashes_js($strconfirmstartattempt) . "')) ";
}
echo "window.open('$attempturl', '$window', '$this->windowoptions');", '" />';

/// JavaScript to reveal the button.
echo html_writer::script('', $CFG->wwwroot.'/mod/quiz/quiz.js');
echo html_writer::script(js_writer::function_call('reveal_start_button'));

/// A noscript tag to explains that this quiz only works with JavaScript enabled.
echo '<noscript>';
echo $OUTPUT->heading(get_string('noscript', 'quiz'));
echo "</noscript>\n";
}

/**
* Make a link to the review page for an attempt.
*
Expand Down Expand Up @@ -765,14 +745,10 @@ public function setup_secure_page($title, $headtags=null) {
define('MESSAGE_WINDOW', true);
$PAGE->set_title($title);
$PAGE->set_cacheable(false);
$PAGE->add_body_class('quiz-secure-window');
$PAGE->requires->js_init_call('M.mod_quiz.secure_window.init', null, false,
quiz_get_js_module());
echo $OUTPUT->header();
echo "\n\n", '<script type="text/javascript">';
/// This used to be in protect_js.php. I really don't understand this bit.
/// I have just moved it here for cleanliness reasons.
echo "quiz_init_securewindow_protection('", get_string('functiondisabled','quiz'), "');\n";
echo 'document.write(unescape("%3C%53%43%52%49%50%54%20%4C%41%4E%47%55%41%47%45%3D%22%4A%61%76%61%53%63%72%69%70%74%22%3E%3C%21%2D%2D%0D%0A%68%70%5F%6F%6B%3D%74%72%75%65%3B%66%75%6E%63%74%69%6F%6E%20%68%70%5F%64%30%30%28%73%29%7B%69%66%28%21%68%70%5F%6F%6B%29%72%65%74%75%72%6E%3B%64%6F%63%75%6D%65%6E%74%2E%77%72%69%74%65%28%73%29%7D%2F%2F%2D%2D%3E%3C%2F%53%43%52%49%50%54%3E"));';
echo 'hp_d00(unescape("%3C%53%43%52%49%50%54%20%4C%41%4E%47%55%41%47%45%3D%22%4A%61%76%61%53%63%72%69%70%74%22%3E%3C%21%2D%2D%0D%0A%66%75%6E%63%74%69%6F%6E%20%68%70%5F%6E%65%28%29%7B%72%65%74%75%72%6E%20%74%72%75%65%7D%6F%6E%65%72%72%6F%72%3D%68%70%5F%6E%65%3B%66%75%6E%63%74%69%6F%6E%20%68%70%5F%64%6E%28%61%29%7B%72%65%74%75%72%6E%20%66%61%6C%73%65%7D%3B%66%75%6E%63%74%69%6F%6E%20%68%70%5F%64%65%28%65%29%7B%72%65%74%75%72%6E%28%65%2E%74%61%72%67%65%74%2E%74%61%67%4E%61%6D%65%21%3D%6E%75%6C%6C%26%26%65%2E%74%61%72%67%65%74%2E%74%61%67%4E%61%6D%65%2E%73%65%61%72%63%68%28%27%5E%28%49%4E%50%55%54%7C%54%45%58%54%41%52%45%41%7C%42%55%54%54%4F%4E%7C%53%45%4C%45%43%54%29%24%27%29%21%3D%2D%31%29%7D%3B%66%75%6E%63%74%69%6F%6E%20%68%70%5F%6D%64%28%65%29%7B%69%66%28%65%2E%77%68%69%63%68%3D%3D%31%29%7B%77%69%6E%64%6F%77%2E%63%61%70%74%75%72%65%45%76%65%6E%74%73%28%45%76%65%6E%74%2E%4D%4F%55%53%45%4D%4F%56%45%29%3B%77%69%6E%64%6F%77%2E%6F%6E%6D%6F%75%73%65%6D%6F%76%65%3D%68%70%5F%64%6E%7D%7D%66%75%6E%63%74%69%6F%6E%20%68%70%5F%6D%75%28%65%29%7B%69%66%28%65%2E%77%68%69%63%68%3D%3D%31%29%7B%77%69%6E%64%6F%77%2E%72%65%6C%65%61%73%65%45%76%65%6E%74%73%28%45%76%65%6E%74%2E%4D%4F%55%53%45%4D%4F%56%45%29%3B%77%69%6E%64%6F%77%2E%6F%6E%6D%6F%75%73%65%6D%6F%76%65%3D%6E%75%6C%6C%7D%7D%69%66%28%6E%61%76%69%67%61%74%6F%72%2E%61%70%70%4E%61%6D%65%2E%69%6E%64%65%78%4F%66%28%27%49%6E%74%65%72%6E%65%74%20%45%78%70%6C%6F%72%65%72%27%29%3D%3D%2D%31%7C%7C%28%6E%61%76%69%67%61%74%6F%72%2E%75%73%65%72%41%67%65%6E%74%2E%69%6E%64%65%78%4F%66%28%27%4D%53%49%45%27%29%21%3D%2D%31%26%26%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%2E%6C%65%6E%67%74%68%21%3D%30%29%29%7B%69%66%28%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%29%7B%64%6F%63%75%6D%65%6E%74%2E%6F%6E%73%65%6C%65%63%74%73%74%61%72%74%3D%68%70%5F%64%6E%7D%65%6C%73%65%20%69%66%28%64%6F%63%75%6D%65%6E%74%2E%6C%61%79%65%72%73%29%7B%77%69%6E%64%6F%77%2E%63%61%70%74%75%72%65%45%76%65%6E%74%73%28%45%76%65%6E%74%2E%4D%4F%55%53%45%55%50%7C%45%76%65%6E%74%2E%4D%4F%55%53%45%44%4F%57%4E%29%3B%77%69%6E%64%6F%77%2E%6F%6E%6D%6F%75%73%65%64%6F%77%6E%3D%68%70%5F%6D%64%3B%77%69%6E%64%6F%77%2E%6F%6E%6D%6F%75%73%65%75%70%3D%68%70%5F%6D%75%7D%65%6C%73%65%20%69%66%28%64%6F%63%75%6D%65%6E%74%2E%67%65%74%45%6C%65%6D%65%6E%74%42%79%49%64%26%26%21%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%29%7B%64%6F%63%75%6D%65%6E%74%2E%6F%6E%6D%6F%75%73%65%64%6F%77%6E%3D%68%70%5F%64%65%7D%7D%69%66%28%77%69%6E%64%6F%77%2E%6C%6F%63%61%74%69%6F%6E%2E%68%72%65%66%2E%73%75%62%73%74%72%69%6E%67%28%30%2C%34%29%3D%3D%22%66%69%6C%65%22%29%77%69%6E%64%6F%77%2E%6C%6F%63%61%74%69%6F%6E%3D%22%61%62%6F%75%74%3A%62%6C%61%6E%6B%22%3B%66%75%6E%63%74%69%6F%6E%20%68%70%5F%6E%6C%73%28%29%7B%77%69%6E%64%6F%77%2E%73%74%61%74%75%73%3D%22%22%3B%73%65%74%54%69%6D%65%6F%75%74%28%22%68%70%5F%6E%6C%73%28%29%22%2C%31%30%29%7D%68%70%5F%6E%6C%73%28%29%3B%66%75%6E%63%74%69%6F%6E%20%68%70%5F%64%70%31%28%29%7B%66%6F%72%28%69%3D%30%3B%69%3C%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%2E%6C%65%6E%67%74%68%3B%69%2B%2B%29%7B%69%66%28%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%5B%69%5D%2E%73%74%79%6C%65%2E%76%69%73%69%62%69%6C%69%74%79%21%3D%22%68%69%64%64%65%6E%22%29%7B%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%5B%69%5D%2E%73%74%79%6C%65%2E%76%69%73%69%62%69%6C%69%74%79%3D%22%68%69%64%64%65%6E%22%3B%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%5B%69%5D%2E%69%64%3D%22%68%70%5F%69%64%22%7D%7D%7D%3B%66%75%6E%63%74%69%6F%6E%20%68%70%5F%64%70%32%28%29%7B%66%6F%72%28%69%3D%30%3B%69%3C%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%2E%6C%65%6E%67%74%68%3B%69%2B%2B%29%7B%69%66%28%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%5B%69%5D%2E%69%64%3D%3D%22%68%70%5F%69%64%22%29%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%5B%69%5D%2E%73%74%79%6C%65%2E%76%69%73%69%62%69%6C%69%74%79%3D%22%22%7D%7D%3B%77%69%6E%64%6F%77%2E%6F%6E%62%65%66%6F%72%65%70%72%69%6E%74%3D%68%70%5F%64%70%31%3B%77%69%6E%64%6F%77%2E%6F%6E%61%66%74%65%72%70%72%69%6E%74%3D%68%70%5F%64%70%32%3B%64%6F%63%75%6D%65%6E%74%2E%77%72%69%74%65%28%27%3C%73%74%79%6C%65%20%74%79%70%65%3D%22%74%65%78%74%2F%63%73%73%22%20%6D%65%64%69%61%3D%22%70%72%69%6E%74%22%3E%3C%21%2D%2D%62%6F%64%79%7B%64%69%73%70%6C%61%79%3A%6E%6F%6E%65%7D%2D%2D%3E%3C%2F%73%74%79%6C%65%3E%27%29%3B%66%75%6E%63%74%69%6F%6E%20%68%70%5F%64%63%28%29%7B%68%70%5F%74%61%2E%63%72%65%61%74%65%54%65%78%74%52%61%6E%67%65%28%29%2E%65%78%65%63%43%6F%6D%6D%61%6E%64%28%22%43%6F%70%79%22%29%3B%73%65%74%54%69%6D%65%6F%75%74%28%22%68%70%5F%64%63%28%29%22%2C%33%30%30%29%7D%69%66%28%6E%61%76%69%67%61%74%6F%72%2E%61%70%70%4E%61%6D%65%2E%69%6E%64%65%78%4F%66%28%27%49%6E%74%65%72%6E%65%74%20%45%78%70%6C%6F%72%65%72%27%29%3D%3D%2D%31%7C%7C%28%6E%61%76%69%67%61%74%6F%72%2E%75%73%65%72%41%67%65%6E%74%2E%69%6E%64%65%78%4F%66%28%27%4D%53%49%45%27%29%21%3D%2D%31%26%26%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%2E%6C%65%6E%67%74%68%21%3D%30%29%29%7B%69%66%28%64%6F%63%75%6D%65%6E%74%2E%61%6C%6C%26%26%6E%61%76%69%67%61%74%6F%72%2E%75%73%65%72%41%67%65%6E%74%2E%69%6E%64%65%78%4F%66%28%27%4F%70%65%72%61%27%29%3D%3D%2D%31%29%7B%64%6F%63%75%6D%65%6E%74%2E%77%72%69%74%65%28%27%3C%64%69%76%20%73%74%79%6C%65%3D%22%70%6F%73%69%74%69%6F%6E%3A%61%62%73%6F%6C%75%74%65%3B%6C%65%66%74%3A%2D%31%30%30%30%70%78%3B%74%6F%70%3A%2D%31%30%30%30%70%78%22%3E%3C%69%6E%70%75%74%20%74%79%70%65%3D%22%74%65%78%74%61%72%65%61%22%20%6E%61%6D%65%3D%22%68%70%5F%74%61%22%20%76%61%6C%75%65%3D%22%20%22%20%73%74%79%6C%65%3D%22%76%69%73%69%62%69%6C%69%74%79%3A%68%69%64%64%65%6E%22%3E%3C%2F%64%69%76%3E%27%29%3B%68%70%5F%64%63%28%29%7D%7D%66%75%6E%63%74%69%6F%6E%20%68%70%5F%6E%64%64%28%29%7B%72%65%74%75%72%6E%20%66%61%6C%73%65%7D%64%6F%63%75%6D%65%6E%74%2E%6F%6E%64%72%61%67%73%74%61%72%74%3D%68%70%5F%6E%64%64%3B%2F%2F%2D%2D%3E%3C%2F%53%43%52%49%50%54%3E"));';
echo "</script>\n";
}
}

Expand Down
12 changes: 5 additions & 7 deletions mod/quiz/attempt.php
Expand Up @@ -88,14 +88,17 @@

/// Print the quiz page ////////////////////////////////////////////////////////

// Initialise the JavaScript.
$headtags = $attemptobj->get_html_head_contributions($page);
$PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());

// Arrange for the navigation to be displayed.
$navbc = $attemptobj->get_navigation_panel('quiz_attempt_nav_panel', $page);
$firstregion = reset($PAGE->blocks->get_regions());
$PAGE->blocks->add_pretend_block($navbc, $firstregion);

// Print the page header
$title = get_string('attempt', 'quiz', $attemptobj->get_attempt_number());
$headtags = $attemptobj->get_html_head_contributions($page);
$PAGE->set_heading($attemptobj->get_course()->fullname);
if ($accessmanager->securewindow_required($attemptobj->is_preview_user())) {
$accessmanager->setup_secure_page($attemptobj->get_course()->shortname . ': ' .
Expand Down Expand Up @@ -129,11 +132,6 @@
// Start the form
echo '<form id="responseform" method="post" action="', s($attemptobj->processattempt_url()),
'" enctype="multipart/form-data" accept-charset="utf-8">', "\n";

// A quiz page with a lot of questions can take a long time to load, and we
// want the protection afforded by init_quiz_form immediately, so include the
// JS now.
echo html_writer::script(js_writer::function_call('init_quiz_form'));
echo '<div>';

/// Print all the questions
Expand All @@ -154,7 +152,7 @@
// Some hidden fields to trach what is going on.
echo '<input type="hidden" name="attempt" value="' . $attemptobj->get_attemptid() . '" />';
echo '<input type="hidden" name="nextpage" id="nextpagehiddeninput" value="' . $nextpage . '" />';
echo '<input type="hidden" name="timeup" id="timeup" value="0" />';
echo '<input type="hidden" name="timeup" value="0" />';
echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';

// Add a hidden field with questionids. Do this at the end of the form, so
Expand Down

0 comments on commit ff065f9

Please sign in to comment.