public function to_html($format = null, $addstr = null) { global $CFG, $COURSE, $OUTPUT, $USER; if (!$this->is_renderable()) { return; } if (empty($this->callbackclass) || empty($this->callbackfile)) { throw new portfolio_button_exception('mustsetcallbackoptions', 'portfolio'); } if (empty($this->formats)) { // use the caller defaults $this->set_formats(); } $url = new moodle_url('/portfolio/add.php'); foreach ($this->callbackargs as $key => $value) { if (!empty($value) && !is_string($value) && !is_numeric($value)) { $a = new stdClass(); $a->key = $key; $a->value = print_r($value, true); debugging(get_string('nonprimative', 'portfolio', $a)); return; } $url->param('ca_' . $key, $value); } $url->param('sesskey', sesskey()); $url->param('callbackfile', $this->callbackfile); $url->param('callbackclass', $this->callbackclass); $url->param('course', !empty($COURSE) ? $COURSE->id : 0); $url->param('callerformats', implode(',', $this->formats)); $mimetype = null; if ($this->file instanceof stored_file) { $mimetype = $this->file->get_mimetype(); } else { if ($this->intendedmimetype) { $mimetype = $this->intendedmimetype; } } $selectoutput = ''; if (count($this->instances) == 1) { $tmp = array_values($this->instances); $instance = $tmp[0]; $formats = portfolio_supported_formats_intersect($this->formats, $instance->supported_formats()); if (count($formats) == 0) { // bail. no common formats. //debugging(get_string('nocommonformats', 'portfolio', (object)array('location' => $this->callbackclass, 'formats' => implode(',', $this->formats)))); return; } if ($error = portfolio_instance_sanity_check($instance)) { // bail, plugin is misconfigured //debugging(get_string('instancemisconfigured', 'portfolio', get_string($error[$instance->get('id')], 'portfolio_' . $instance->get('plugin')))); return; } if (!$instance->allows_multiple_exports() && ($already = portfolio_existing_exports($USER->id, $instance->get('plugin')))) { //debugging(get_string('singleinstancenomultiallowed', 'portfolio')); return; } if ($mimetype && !$instance->file_mime_check($mimetype)) { // bail, we have a specific file or mimetype and this plugin doesn't support it //debugging(get_string('mimecheckfail', 'portfolio', (object)array('plugin' => $instance->get('plugin'), 'mimetype' => $mimetype))); return; } $url->param('instance', $instance->get('id')); } else { if (!($selectoutput = portfolio_instance_select($this->instances, $this->formats, $this->callbackclass, $mimetype, 'instance', true))) { return; } } // if we just want a url to redirect to, do it now if ($format == PORTFOLIO_ADD_FAKE_URL) { return $url->out(false); } if (empty($addstr)) { $addstr = get_string('addtoportfolio', 'portfolio'); } if (empty($format)) { $format = PORTFOLIO_ADD_FULL_FORM; } $formoutput = '<form method="post" action="' . $CFG->wwwroot . '/portfolio/add.php" id="portfolio-add-button">' . "\n"; $formoutput .= html_writer::input_hidden_params($url); $linkoutput = '<a class="portfolio-add-link" title="' . $addstr . '" href="' . $url->out(); switch ($format) { case PORTFOLIO_ADD_FULL_FORM: $formoutput .= $selectoutput; $formoutput .= "\n" . '<input type="submit" value="' . $addstr . '" />'; $formoutput .= "\n" . '</form>'; break; case PORTFOLIO_ADD_ICON_FORM: $formoutput .= $selectoutput; $formoutput .= "\n" . '<input class="portfolio-add-icon" type="image" src="' . $OUTPUT->pix_url('t/portfolioadd') . '" alt=' . $addstr . '" />'; $formoutput .= "\n" . '</form>'; break; case PORTFOLIO_ADD_ICON_LINK: $linkoutput .= '"><img class="portfolio-add-icon" src="' . $OUTPUT->pix_url('t/portfolioadd') . '" alt="' . $addstr . '" /></a>'; break; case PORTFOLIO_ADD_TEXT_LINK: $linkoutput .= '">' . $addstr . '</a>'; break; default: debugging(get_string('invalidaddformat', 'portfolio', $format)); } $output = in_array($format, array(PORTFOLIO_ADD_FULL_FORM, PORTFOLIO_ADD_ICON_FORM)) ? $formoutput : $linkoutput; return $output; }
/** * Verifies a rewoken object. * Checks to make sure it belongs to the same user and session as is currently in use. * * @param bool $readonly if we're reawakening this for a user to just display in the log view, don't verify the sessionkey * @throws portfolio_exception */ public function verify_rewaken($readonly = false) { global $USER, $CFG; if ($this->get('user')->id != $USER->id) { // make sure it belongs to the right user throw new portfolio_exception('notyours', 'portfolio'); } if (!$readonly && $this->get('instance') && !$this->get('instance')->allows_multiple_exports()) { $already = portfolio_existing_exports($this->get('user')->id, $this->get('instance')->get('plugin')); $already = array_keys($already); if (array_shift($already) != $this->get('id')) { $a = (object) array('plugin' => $this->get('instance')->get('plugin'), 'link' => $CFG->wwwroot . '/user/portfoliologs.php'); throw new portfolio_exception('nomultipleexports', 'portfolio', '', $a); } } if (!$this->caller->check_permissions()) { // recall the caller permission check throw new portfolio_caller_exception('nopermissions', 'portfolio', $this->caller->get_return_url()); } }