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;
 }
Example #2
0
 /**
  * Processes the 'config' stage of the export
  *
  * @return bool whether or not to process the next stage. this is important as the control function is called recursively.
  */
 public function process_stage_config()
 {
     global $OUTPUT, $CFG;
     $pluginobj = $callerobj = null;
     if ($this->instance->has_export_config()) {
         $pluginobj = $this->instance;
     }
     if ($this->caller->has_export_config()) {
         $callerobj = $this->caller;
     }
     $formats = portfolio_supported_formats_intersect($this->caller->supported_formats(), $this->instance->supported_formats());
     $expectedtime = $this->instance->expected_time($this->caller->expected_time());
     if (count($formats) == 0) {
         // something went wrong, we should not have gotten this far.
         throw new portfolio_export_exception($this, 'nocommonformats', 'portfolio', null, array('location' => get_class($this->caller), 'formats' => implode(',', $formats)));
     }
     // even if neither plugin or caller wants any config, we have to let the user choose their format, and decide to wait.
     if ($pluginobj || $callerobj || count($formats) > 1 || $expectedtime != PORTFOLIO_TIME_LOW && $expectedtime != PORTFOLIO_TIME_FORCEQUEUE) {
         $customdata = array('instance' => $this->instance, 'id' => $this->id, 'plugin' => $pluginobj, 'caller' => $callerobj, 'userid' => $this->user->id, 'formats' => $formats, 'expectedtime' => $expectedtime);
         require_once $CFG->libdir . '/portfolio/forms.php';
         $mform = new portfolio_export_form('', $customdata);
         if ($mform->is_cancelled()) {
             $this->cancel_request();
         } else {
             if ($fromform = $mform->get_data()) {
                 if (!confirm_sesskey()) {
                     throw new portfolio_export_exception($this, 'confirmsesskeybad');
                 }
                 $pluginbits = array();
                 $callerbits = array();
                 foreach ($fromform as $key => $value) {
                     if (strpos($key, 'plugin_') === 0) {
                         $pluginbits[substr($key, 7)] = $value;
                     } else {
                         if (strpos($key, 'caller_') === 0) {
                             $callerbits[substr($key, 7)] = $value;
                         }
                     }
                 }
                 $callerbits['format'] = $pluginbits['format'] = $fromform->format;
                 $pluginbits['wait'] = $fromform->wait;
                 if ($expectedtime == PORTFOLIO_TIME_LOW) {
                     $pluginbits['wait'] = 1;
                     $pluginbits['hidewait'] = 1;
                 } else {
                     if ($expectedtime == PORTFOLIO_TIME_FORCEQUEUE) {
                         $pluginbits['wait'] = 0;
                         $pluginbits['hidewait'] = 1;
                         $this->forcequeue = true;
                     }
                 }
                 $callerbits['hideformat'] = $pluginbits['hideformat'] = count($formats) == 1;
                 $this->caller->set_export_config($callerbits);
                 $this->instance->set_export_config($pluginbits);
                 $this->set('format', $fromform->format);
                 return true;
             } else {
                 $this->print_header(get_string('configexport', 'portfolio'));
                 echo $OUTPUT->box_start();
                 $mform->display();
                 echo $OUTPUT->box_end();
                 echo $OUTPUT->footer();
                 return false;
             }
         }
     } else {
         $this->noexportconfig = true;
         $format = array_shift($formats);
         $config = array('hidewait' => 1, 'wait' => $expectedtime == PORTFOLIO_TIME_LOW ? 1 : 0, 'format' => $format, 'hideformat' => 1);
         $this->set('format', $format);
         $this->instance->set_export_config($config);
         $this->caller->set_export_config(array('format' => $format, 'hideformat' => 1));
         if ($expectedtime == PORTFOLIO_TIME_FORCEQUEUE) {
             $this->forcequeue = true;
         }
         return true;
         // do not break - fall through to confirm
     }
 }
Example #3
0
 public function to_html($format = null, $addstr = null)
 {
     if ($this->alreadyexporting) {
         return $this->already_exporting($format, $addstr);
     }
     global $CFG, $COURSE, $OUTPUT;
     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();
     }
     $formoutput = '<form method="post" action="' . $CFG->wwwroot . '/portfolio/add.php" id="portfolio-add-button">' . "\n";
     $linkoutput = '<a href="' . $CFG->wwwroot . '/portfolio/add.php?';
     foreach ($this->callbackargs as $key => $value) {
         if (!empty($value) && !is_string($value) && !is_numeric($value)) {
             $a->key = $key;
             $a->value = print_r($value, true);
             debugging(get_string('nonprimative', 'portfolio', $a));
             return;
         }
         $linkoutput .= 'ca_' . $key . '=' . $value . '&amp;';
         $formoutput .= "\n" . '<input type="hidden" name="ca_' . $key . '" value="' . $value . '" />';
     }
     $formoutput .= "\n" . '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
     $linkoutput .= 'sesskey=' . sesskey() . '&amp;';
     $formoutput .= "\n" . '<input type="hidden" name="callbackfile" value="' . $this->callbackfile . '" />';
     $formoutput .= "\n" . '<input type="hidden" name="callbackclass" value="' . $this->callbackclass . '" />';
     $formoutput .= "\n" . '<input type="hidden" name="course" value="' . (!empty($COURSE) ? $COURSE->id : 0) . '" />';
     $linkoutput .= 'callbackfile=' . $this->callbackfile . '&amp;callbackclass=' . $this->callbackclass . '&amp;course=' . (!empty($COURSE) ? $COURSE->id : 0);
     $selectoutput = '';
     if (count($this->instances) == 1) {
         $tmp = array_values($this->instances);
         $instance = $tmp[0];
         //$instance = array_shift($this->instances);
         $formats = portfolio_supported_formats_intersect($this->formats, $instance->supported_formats());
         if (count($formats) == 0) {
             // bail. no common formats.
             debugging(get_string('nocommonformats', 'portfolio', $this->callbackclass));
             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;
         }
         $formoutput .= "\n" . '<input type="hidden" name="instance" value="' . $instance->get('id') . '" />';
         $linkoutput .= '&amp;instance=' . $instance->get('id');
     } else {
         $selectoutput = portfolio_instance_select($this->instances, $this->formats, $this->callbackclass, 'instance', true);
     }
     if (empty($addstr)) {
         $addstr = get_string('addtoportfolio', 'portfolio');
     }
     if (empty($format)) {
         $format = PORTFOLIO_ADD_FULL_FORM;
     }
     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 type="image" src="' . $OUTPUT->old_icon_url('t/portfolio') . '" alt=' . $addstr . '" />';
             $formoutput .= "\n" . '</form>';
             break;
         case PORTFOLIO_ADD_ICON_LINK:
             $linkoutput .= '"><img src="' . $OUTPUT->old_icon_url('t/portfolio') . '" 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;
 }
 public function test_caller_with_plugins()
 {
     if (!empty($this->caller)) {
         $plugins = get_list_of_plugins('portfolio/type');
         foreach ($plugins as $plugin) {
             // Instantiate a fake plugin instance
             $plugin_class = "partialmock_plugin_{$plugin}";
             $plugin = new $plugin_class($this);
             // figure out our format intersection and test all of them.
             $formats = portfolio_supported_formats_intersect($this->caller->supported_formats(), $plugin->supported_formats());
             if (count($formats) == 0) {
                 // bail. no common formats.
                 continue;
             }
             foreach ($formats as $format) {
                 // Create a new fake exporter
                 $exporter =& $this->caller->get('exporter');
                 // new partialmock_exporter(&$this);
                 $exporter->set('caller', $this->caller);
                 $exporter->set('instance', $plugin);
                 $exporter->set('format', $format);
                 $this->caller->set_export_config(array('format' => $format));
                 $plugin->set_export_config(array('format' => $format));
                 $exporter->set('user', $this->caller->get('user'));
                 $exception = false;
                 try {
                     $exporter->process_stage_package();
                 } catch (Exception $e) {
                     $exception = $e->getMessage();
                 }
                 $this->assertFalse($exception, "Unwanted exception: {$exception}");
             }
         }
     }
 }