示例#1
0
 /**
  * Create method for this handler
  *
  * @return string - javascript to be run
  **/
 public function create()
 {
     // Include needed files
     require_once dirname(__DIR__) . DS . 'form.php';
     // Check to make sure a file was provided
     if (isset($_FILES['files'])) {
         $file = $_FILES['files']['name'][0];
         // Get the file extension
         $pathinfo = pathinfo($file);
         $filename = $pathinfo['filename'];
         $ext = $pathinfo['extension'];
     } else {
         return array('error' => 'No files provided');
     }
     // Instantiate form object
     $pdf = PdfForm::fromPostedFile('files');
     // No error, then render the images
     if (!$pdf->hasErrors()) {
         $pdf->renderPageImages();
     } else {
         return array('error' => $pdf->getErrors());
     }
     // Grab the newly created form id
     $id = $pdf->getId();
     $subtype = 'quiz';
     if (strstr($filename, 'exam') !== false) {
         $subtype = 'exam';
     } elseif (strstr($filename, 'homework') !== false) {
         $subtype = 'homework';
     }
     // Save the actual asset
     $this->asset['title'] = $filename;
     $this->asset['type'] = 'form';
     $this->asset['subtype'] = $subtype;
     $this->asset['url'] = $id;
     $this->asset['graded'] = 1;
     $this->asset['grade_weight'] = $subtype;
     // Call the primary create method on the file asset handler
     $return = parent::create();
     // Check for errors in response
     if (array_key_exists('error', $return)) {
         return array('error' => $return['error']);
     } else {
         // Set the asset id on the form
         $pdf->setAssetId($return['assets']['asset_id']);
         $gid = Request::getVar('course_id');
         $oid = Request::getVar('offering');
         // Build our JavaScript to return to the view to be executed
         $js = "// Open up forms in a lightbox\n\t\t\t\t\$.fancybox({\n\t\t\t\t\tfitToView: false,\n\t\t\t\t\tautoResize: false,\n\t\t\t\t\tautoSize: false,\n\t\t\t\t\theight: (\$(window).height())*2/3,\n\t\t\t\t\tcloseBtn: false,\n\t\t\t\t\tmodal: true,\n\t\t\t\t\ttype: 'iframe',\n\t\t\t\t\tiframe: {\n\t\t\t\t\t\tpreload : false\n\t\t\t\t\t},\n\t\t\t\t\thref: '/courses/" . $gid . "/" . $oid . "/form.layout?formId=" . $id . "&tmpl=component',\n\t\t\t\t\tafterLoad: function() {\n\t\t\t\t\t\tvar iframe = \$('.fancybox-iframe');\n\t\t\t\t\t\tiframe.load(function() {\n\t\t\t\t\t\t\tvar frameContents = \$('.fancybox-iframe').contents();\n\n\t\t\t\t\t\t\tvar navHeight = frameContents.find('.navbar').height();\n\t\t\t\t\t\t\tframeContents.find('.main.section.courses-form').css('margin-bottom', navHeight);\n\n\t\t\t\t\t\t\t// Highjack the 'done' button to close the iframe\n\t\t\t\t\t\t\tframeContents.find('#done').bind('click', function(e) {\n\t\t\t\t\t\t\t\te.preventDefault();\n\n\t\t\t\t\t\t\t\t\$.fancybox.close();\n\n\t\t\t\t\t\t\t\t// Remove progress bar\n\t\t\t\t\t\t\t\tHUB.CoursesOutline.asset.resetProgresBar(progressBarId, 0);\n\n\t\t\t\t\t\t\t\t// Get the form data and set the published value to 2 for deleted\n\t\t\t\t\t\t\t\tvar formData = form.serializeArray();\n\t\t\t\t\t\t\t\tformData.push({'name':'published', 'value':'2'});\n\t\t\t\t\t\t\t\tformData.push({'name':'id', 'value':'" . $this->assoc['asset_id'] . "'});\n\n\t\t\t\t\t\t\t\t// We've already saved the asset, so we need to mark asset as deleted\n\t\t\t\t\t\t\t\t\$.ajax({\n\t\t\t\t\t\t\t\t\turl: '/api/courses/asset/save',\n\t\t\t\t\t\t\t\t\tdata: formData\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\t// Listen for savesuccessful call from iframe\n\t\t\t\t\t\t\$('body').on('savesuccessful', function( e, title ) {\n\t\t\t\t\t\t\t\$.fancybox.close();\n\n\t\t\t\t\t\t\tvar data = " . json_encode(array('assets' => array($return['assets']))) . ";\n\n\t\t\t\t\t\t\tdata.assets[0].asset_title = title;\n\n\t\t\t\t\t\t\tHUB.CoursesOutline.asset.insert(data, assetslist, {'progressBarId':progressBarId});\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t});";
     }
     return array('js' => $js);
 }
示例#2
0
 /**
  * Upload a PDF and render images
  *
  * @return  void
  */
 public function uploadTask()
 {
     // Check authorization
     $this->authorize();
     $pdf = PdfForm::fromPostedFile('pdf');
     // No error, then render the images
     if (!$pdf->hasErrors()) {
         $pdf->renderPageImages();
     }
     // If there were errors, jump back to the index view and display them
     if ($pdf->hasErrors()) {
         $this->setView('form', 'index');
         $this->view->errors = $pdf->getErrors();
         $this->indexTask();
     } else {
         // Just return JSON
         if (Request::getInt('no_html', false)) {
             echo json_encode(array('success' => true, 'id' => $pdf->getId()));
             exit;
         } else {
             App::redirect(Route::url('index.php?option=com_courses&controller=form&task=layout&formId=' . $pdf->getId(), false), Lang::txt('COM_COURSES_PDF_UPLOAD_SUCCESSFUL'), 'passed');
             return;
         }
     }
 }