/** * fix_flashvars * * @param xxx $match * @param xxx $options (passed by reference) * @return xxx */ function fix_flashvars($options, $match) { global $CFG; $before = $match[1]; $flashvars = $match[2]; $after = $match[3]; // entities_to_utf8() is required undo the call to htmlentities(), see MDL-5223 // this is necessary to allow waitForPlay and autoPlay to be effective on Firefox $flashvars = hotpot_textlib('entities_to_utf8', $flashvars); $vars = explode('&', $flashvars); foreach ($this->moodle_flashvars as $var) { if (array_key_exists($var, $options)) { $vars = preg_grep("/^{$var}=/", $vars, PREG_GREP_INVERT); $vars[] = "{$var}=" . hotpot_textlib('utf8_to_entities', $options[$var]); } } return $before . implode('&', $vars) . $after; }
/** * add_activity_list * * @param xxx $type */ function add_activity_list($type) { global $PAGE; // if activity name is longer than $namelength, it will be truncated // to first $headlength chars + " ... " + last $taillength chars $namelength = 40; $headlength = 16; $taillength = 16; $mform = $this->_form; $optgroups = array(get_string('none') => array(hotpot::ACTIVITY_NONE => get_string('none')), get_string($type == 'entry' ? 'previous' : 'next') => array(hotpot::ACTIVITY_COURSE_ANY => get_string($type . 'cmcourse', 'hotpot'), hotpot::ACTIVITY_SECTION_ANY => get_string($type . 'cmsection', 'hotpot'), hotpot::ACTIVITY_COURSE_HOTPOT => get_string($type . 'hotpotcourse', 'hotpot'), hotpot::ACTIVITY_SECTION_HOTPOT => get_string($type . 'hotpotsection', 'hotpot'))); if ($modinfo = get_fast_modinfo($PAGE->course)) { switch ($PAGE->course->format) { case 'weeks': $strsection = get_string('strftimedateshort'); break; case 'topics': $strsection = get_string('topic'); break; default: $strsection = get_string('section'); } $sectionnum = -1; foreach ($modinfo->cms as $cmid => $mod) { if ($mod->modname == 'label') { continue; // ignore labels } if ($type == 'entry' && $mod->modname == 'resource') { continue; // ignore resources as entry activities } if (isset($form->update) && $form->update == $cmid) { continue; // ignore this hotpot } if ($sectionnum == $mod->sectionnum) { // do nothing (same section) } else { // start new optgroup for this course section $sectionnum = $mod->sectionnum; if ($sectionnum == 0) { $optgroup = get_string('activities'); } else { if ($PAGE->course->format == 'weeks') { $date = $PAGE->course->startdate + 7200 + $sectionnum * 604800; $optgroup = userdate($date, $strsection) . ' - ' . userdate($date + 518400, $strsection); } else { $optgroup = $strsection . ': ' . $sectionnum; } } if (empty($options[$optgroup])) { $options[$optgroup] = array(); } } $name = format_string($mod->name); $strlen = hotpot_textlib('strlen', $name); if ($strlen > $namelength) { $head = hotpot_textlib('substr', $name, 0, $headlength); $tail = hotpot_textlib('substr', $name, $strlen - $taillength, $taillength); $name = $head . ' ... ' . $tail; } $optgroups[$optgroup][$cmid] = $name; } } $options = array(); for ($i = 100; $i >= 0; $i--) { $options[$i] = $i . '%'; } $elements = array($mform->createElement('selectgroups', $type . 'cm', '', $optgroups), $mform->createElement('select', $type . 'grade', '', $options)); $mform->addGroup($elements, $type . 'cm_elements', get_string($type . 'cm', 'hotpot'), array(' '), false); $mform->addHelpButton($type . 'cm_elements', $type . 'cm', 'hotpot'); if ($type == 'entry') { $defaultcm = hotpot::ACTIVITY_NONE; $defaultgrade = 100; } else { $defaultcm = hotpot::ACTIVITY_SECTION_HOTPOT; $defaultgrade = 0; } $mform->setDefault($type . 'cm', get_user_preferences('hotpot_' . $type . 'cm', $defaultcm)); $mform->setDefault($type . 'grade', get_user_preferences('hotpot_' . $type . 'grade', $defaultgrade)); $mform->disabledIf($type . 'cm_elements', $type . 'cm', 'eq', 0); if ($type == 'entry') { $mform->setAdvanced($type . 'cm_elements'); } // add module icons, if possible if ($modinfo) { $element = reset($mform->getElement($type . 'cm_elements')->getElements()); for ($i = 0; $i < count($element->_optGroups); $i++) { $optgroup =& $element->_optGroups[$i]; for ($ii = 0; $ii < count($optgroup['options']); $ii++) { $option =& $optgroup['options'][$ii]; if (isset($option['attr']['value']) && $option['attr']['value'] > 0) { $cmid = $option['attr']['value']; $url = $PAGE->theme->pix_url('icon', $modinfo->cms[$cmid]->modname)->out(); $option['attr']['style'] = "background-image: url({$url}); background-repeat: no-repeat; background-position: 1px 2px; min-height: 20px;"; } } } } }
/** * expand_jcross_clues * * @param xxx $direction * @return xxx */ function expand_jcross_clues($direction) { // $direction: A(cross) or D(own) $row = null; $r_max = 0; $c_max = 0; $this->get_jcross_grid($row, $r_max, $c_max); $clue_i = 0; // clue index; $str = ''; for ($r = 0; $r <= $r_max; $r++) { for ($c = 0; $c <= $c_max; $c++) { $aword = $this->get_jcross_aword($row, $r, $r_max, $c, $c_max); $dword = $this->get_jcross_dword($row, $r, $r_max, $c, $c_max); if ($aword || $dword) { $clue_i++; // increment clue index // get the definition for this word $def = ''; $word = $direction == 'A' ? $aword : $dword; $word = hotpot_textlib('utf8_to_entities', $word); $i = 0; $clues = 'data,crossword,clues,item'; while (($clue = "[{$i}]['#']") && $this->hotpot->source->xml_value($clues, $clue)) { if ($word == $this->hotpot->source->xml_value($clues, $clue . "['word'][0]['#']")) { $def = $this->hotpot->source->xml_value($clues, $clue . "['def'][0]['#']"); break; } $i++; } if ($def) { $str .= '<tr><td class="ClueNum">' . $clue_i . '. </td><td id="Clue_' . $direction . '_' . $clue_i . '" class="Clue">' . $def . '</td></tr>'; } } } } return $str; }
/** * get_title * * @return xxx */ function get_title() { $format_string = false; switch ($this->hotpot->title & hotpot::TITLE_SOURCE) { case hotpot::TEXTSOURCE_FILE: $title = $this->hotpot->source->get_title(); break; case hotpot::TEXTSOURCE_FILENAME: $title = basename($this->hotpot->sourcefile); break; case hotpot::TEXTSOURCE_FILEPATH: $title = ltrim($this->hotpot->sourcefile, '/'); break; case hotpot::TEXTSOURCE_SPECIFIC: default: $title = $this->hotpot->name; $title = format_string($title); // this will strip tags } if ($this->hotpot->title & hotpot::TITLE_UNITNAME) { $title = $this->hotpot->name . ': ' . $title; } if ($this->hotpot->title & hotpot::TITLE_SORTORDER) { $title .= ' (' . $this->sortorder . ')'; } $title = hotpot_textlib('utf8_to_entities', $title); return $title; }
/** * get_filecontents * * @return xxx */ function get_filecontents() { global $DB; if (isset($this->filecontents)) { return $this->filecontents ? true : false; } // initialize $this->filecontent $this->filecontents = false; if ($this->location == hotpot::LOCATION_WWW) { if (!$this->url) { // no url given - shouldn't happen return false; } if (!$this->get_filecontents_url()) { // url is (no longer) accessible return false; } } else { if (!$this->file) { // no file object - shouldn't happen !! return false; } $this->filecontents = self::get_content($this->file, $this->hotpot); } // file contents were successfully read // remove BOMs - http://en.wikipedia.org/wiki/Byte_order_mark switch (true) { case substr($this->filecontents, 0, 4) == "ÿþ": $start = 4; $encoding = 'UTF-32LE'; break; case substr($this->filecontents, 0, 4) == "þÿ": $start = 4; $encoding = 'UTF-32BE'; break; case substr($this->filecontents, 0, 2) == "ÿþ": $start = 2; $encoding = 'UTF-16LE'; break; case substr($this->filecontents, 0, 2) == "þÿ": $start = 2; $encoding = 'UTF-16BE'; break; case substr($this->filecontents, 0, 3) == "": $start = 3; $encoding = 'UTF-8'; break; default: $start = 0; $encoding = ''; } // remove BOM, if necessary if ($start) { $this->filecontents = substr($this->filecontents, $start); } // convert to UTF-8, if necessary if ($encoding == '' || $encoding == 'UTF-8') { // do nothing } else { $this->filecontents = hotpot_textlib('convert', $this->filecontents, $encoding); } return true; }
/** * require_moreattempts * * @param xxx $shorterror (optional, default=false) * @return xxx */ public function require_moreattempts($shorterror = false) { if (!$this->attemptlimit) { return false; } if (!($countattempts = $this->get_attempts())) { return false; } if ($this->attemptlimit > $countattempts) { return false; } // maximum number of unit/quiz attempts reached if ($shorterror) { return get_string('nomoreattempts', 'hotpot'); } else { $attemptlimitstr = hotpot_textlib('moodle_strtolower', get_string('attemptlimit', 'hotpot')); $msg = html_writer::tag('b', format_string($this->name)) . " ({$attemptlimitstr} = {$this->attemptlimit})"; return html_writer::tag('p', get_string('nomoreattempts', 'hotpot')) . html_writer::tag('p', $msg); } }
/** * js_value_safe * * @param xxx $str * @param xxx $convert_to_unicode (optional, default=false) * @return xxx */ function js_value_safe($str, $convert_to_unicode = false) { // encode a string for javascript static $replace_pairs = array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r\n" => '\\n', "\r" => '\\n', "\n" => '\\n', "" => '\\0', '</' => '<\\/'); // convert unicode chars to html entities, if required // Note that this will also decode named entities such as ' and " // so we have to put "strtr()" AFTER this call to textlib::utf8_to_entities() if ($convert_to_unicode) { $str = hotpot_textlib('utf8_to_entities', $str, false, true); } $str = strtr($str, $replace_pairs); // convert (hex and decimal) html entities to javascript unicode, if required if ($convert_to_unicode) { $search = '/&#x([0-9A-F]+);/i'; $callback = array($this, 'js_unicode_char'); $str = preg_replace_callback($search, $callback, $str); } return $str; }
/** * js_unicode_char * * @param xxx $match * @return xxx */ function js_unicode_char($match) { $num = $match[0]; // the UTF-8 char $num = hotpot_textlib('utf8ord', $num); $num = strtoupper(dechex($num)); return sprintf('\\u%04s', $num); }
/** * expand_WordsArray * * @return xxx */ function expand_WordsArray() { $str = ''; $text = $this->hotpot->source->xml_value('data,rhubarb-text'); $text = hotpot_textlib('entities_to_utf8', $text); $space = ' \\x09\\x0A\\x0C\\x0D'; // " \t\n\r\l" $punc = preg_quote('!"#$%&()*+,-./:;+<=>?@[]\\^_`{|}~', '/'); // not apostrophe \' $search = '/([^' . $punc . $space . ']+)|([' . $punc . '][' . $punc . $space . ']*)/s'; if (preg_match_all($search, $text, $matches)) { foreach ($matches[0] as $i => $word) { $str .= "Words[{$i}] = '" . $this->hotpot->source->js_value_safe($word, true) . "';\n"; } } return $str; }