From a884d3a6183bea0792012f791f8d45814a53074b Mon Sep 17 00:00:00 2001 From: Rossiani Wijaya Date: Thu, 11 Oct 2012 15:52:35 +0800 Subject: [PATCH] MDL-21801 Lesson Module: Removing import power point functionality --- mod/lesson/importppt.php | 211 -------------------------------- mod/lesson/importpptlib.php | 236 ------------------------------------ mod/lesson/renderer.php | 3 - 3 files changed, 450 deletions(-) delete mode 100644 mod/lesson/importppt.php delete mode 100644 mod/lesson/importpptlib.php diff --git a/mod/lesson/importppt.php b/mod/lesson/importppt.php deleted file mode 100644 index 243ff3de324..00000000000 --- a/mod/lesson/importppt.php +++ /dev/null @@ -1,211 +0,0 @@ -. - -/** - * This is a very rough importer for powerpoint slides - * Export a powerpoint presentation with powerpoint as html pages - * Do it with office 2002 (I think?) and no special settings - * Then zip the directory with all of the html pages - * and the zip file is what you want to upload - * - * The script supports book and lesson. - * - * @package mod - * @subpackage lesson - * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - **/ - -/** include required files */ -require_once("../../config.php"); -require_once($CFG->dirroot.'/mod/lesson/locallib.php'); -require_once($CFG->dirroot.'/mod/lesson/importpptlib.php'); - -$id = required_param('id', PARAM_INT); // Course Module ID -$pageid = optional_param('pageid', '', PARAM_INT); // Page ID - -$url = new moodle_url('/mod/lesson/importppt.php', array('id'=>$id)); -if ($pageid !== '') { - $url->param('pageid', $pageid); -} -$PAGE->set_url($url); - -$cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);; -$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); -$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST)); - -$modname = 'lesson'; -$mod = $cm; -require_login($course, false, $cm); - -require_login($course->id, false, $cm); -$context = get_context_instance(CONTEXT_MODULE, $cm->id); -require_capability('mod/lesson:edit', $context); - -$strimportppt = get_string("importppt", "lesson"); -$strlessons = get_string("modulenameplural", "lesson"); - -$data = new stdClass; -$data->id = $cm->id; -$data->pageid = $pageid; -$mform = new lesson_importppt_form(); -$mform->set_data($data); - -if ($data = $mform->get_data()) { - $manager = lesson_page_type_manager::get($lesson); - if (!$filename = $mform->get_new_filename('pptzip')) { - print_error('invalidfile', 'lesson'); - } - if (!$package = $mform->save_stored_file('pptzip', $context->id, 'mod_lesson', 'ppt_imports', $lesson->id, '/', $filename, true)) { - print_error('unabletosavefile', 'lesson'); - } - // extract package content - $packer = get_file_packer('application/zip'); - $package->extract_to_storage($packer, $context->id, 'mod_lesson', 'imported_files', $lesson->id, '/'); - - $fs = get_file_storage(); - if ($files = $fs->get_area_files($context->id, 'mod_lesson', 'imported_files', $lesson->id)) { - - $pages = array(); - foreach ($files as $key=>$file) { - if ($file->get_mimetype() != 'text/html') { - continue; - } - $filenameinfo = pathinfo($file->get_filepath().$file->get_filename()); - - $page = new stdClass; - $page->title = ''; - $page->contents = array(); - $page->images = array(); - $page->source = $filenameinfo['basename']; - - $string = strip_tags($file->get_content(),'
'); - $imgs = array(); - preg_match_all("/]*(src\=\"(".$filenameinfo['filename']."\_image[^>^\"]*)\"[^>]*)>/i", $string, $imgs); - foreach ($imgs[2] as $img) { - $imagename = basename($img); - foreach ($files as $file) { - if ($imagename === $file->get_filename()) { - $page->images[] = clone($file); - } - } - } - - $matches = array(); - // this will look for a non nested tag that is closed - // want to allow (maybe more) tags but when we do that - // the preg_match messes up. - preg_match_all("/(<([\w]+)[^>]*>)([^<\\2>]*)(<\/\\2>)/", $string, $matches); - $countmatches = count($matches[1]); - for($i = 0; $i < $countmatches; $i++) { // go through all of our div matches - - $class = lesson_importppt_isolate_class($matches[1][$i]); // first step in isolating the class - - // check for any static classes - switch ($class) { - case 'T': // class T is used for Titles - $page->title = $matches[3][$i]; - break; - case 'B': // I would guess that all bullet lists would start with B then go to B1, B2, etc - case 'B1': // B1-B4 are just insurance, should just hit B and all be taken care of - case 'B2': - case 'B3': - case 'B4': - $page->contents[] = lesson_importppt_build_list($matches, '
    ', $i, 0); // this is a recursive function that will grab all the bullets and rebuild the list in html - break; - default: - if ($matches[3][$i] != ' ') { // odd crap generated... sigh - if (substr($matches[3][$i], 0, 1) == ':') { // check for leading : ... hate MS ... - $page->contents[] = substr($matches[3][$i], 1); // get rid of : - } else { - $page->contents[] = $matches[3][$i]; - } - } - break; - } - } - $pages[] = $page; - } - - $branchtables = lesson_create_objects($pages, $lesson->id); - - // first set up the prevpageid and nextpageid - if (empty($pageid)) { // adding it to the top of the lesson - $prevpageid = 0; - // get the id of the first page. If not found, then no pages in the lesson - if (!$nextpageid = $DB->get_field('lesson_pages', 'id', array('prevpageid' => 0, 'lessonid' => $lesson->id))) { - $nextpageid = 0; - } - } else { - // going after an actual page - $prevpageid = $pageid; - $nextpageid = $DB->get_field('lesson_pages', 'nextpageid', array('id' => $pageid)); - } - - foreach ($branchtables as $branchtable) { - - // set the doubly linked list - $branchtable->page->nextpageid = $nextpageid; - $branchtable->page->prevpageid = $prevpageid; - - // insert the page - $id = $DB->insert_record('lesson_pages', $branchtable->page); - - if (!empty($branchtable->page->images)) { - $changes = array('contextid'=>$context->id, 'component'=>'mod_lesson', 'filearea'=>'page_contents', 'itemid'=>$id, 'timemodified'=>time()); - foreach ($branchtable->page->images as $image) { - $fs->create_file_from_storedfile($changes, $image); - } - } - - // update the link of the page previous to the one we just updated - if ($prevpageid != 0) { // if not the first page - $DB->set_field("lesson_pages", "nextpageid", $id, array("id" => $prevpageid)); - } - - // insert the answers - foreach ($branchtable->answers as $answer) { - $answer->pageid = $id; - $DB->insert_record('lesson_answers', $answer); - } - - $prevpageid = $id; - } - - // all done with inserts. Now check to update our last page (this is when we import between two lesson pages) - if ($nextpageid != 0) { // if the next page is not the end of lesson - $DB->set_field("lesson_pages", "prevpageid", $id, array("id" => $nextpageid)); - } - } - - // Remove all unzipped files! - $fs->delete_area_files($context->id, 'mod_lesson', 'imported_files', $lesson->id); - - redirect("$CFG->wwwroot/mod/$modname/view.php?id=$cm->id", get_string('pptsuccessfullimport', 'lesson'), 5); -} - -$PAGE->navbar->add($strimportppt); -$PAGE->set_title($strimportppt); -$PAGE->set_heading($strimportppt); -echo $OUTPUT->header(); - -/// Print upload form -echo $OUTPUT->heading_with_help($strimportppt, 'importppt', 'lesson'); -echo $OUTPUT->box_start('generalbox boxaligncenter'); -$mform->display(); -echo $OUTPUT->box_end(); -echo $OUTPUT->footer(); diff --git a/mod/lesson/importpptlib.php b/mod/lesson/importpptlib.php deleted file mode 100644 index 8550e6105fb..00000000000 --- a/mod/lesson/importpptlib.php +++ /dev/null @@ -1,236 +0,0 @@ -. - -/** - * Contains functions used by importppt.php that naturally pertain to importing - * powerpoint presentations into the lesson module - * - * @package mod - * @subpackage lesson - * @copyright 2009 Sam Hemelryk - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - **/ - -defined('MOODLE_INTERNAL') || die(); - -/** - * A recursive function to build a html list - * - * @param array $matches - * @param string $list - * @param int $i - * @param int $depth - * @return string - */ -function lesson_importppt_build_list(array &$matches, $list, &$i, $depth) { - while($i < count($matches[1])) { - - $class = lesson_importppt_isolate_class($matches[1][$i]); - - if (strstr($class, 'B')) { // make sure we are still working with bullet classes - if ($class == 'B') { - $this_depth = 0; // calling class B depth 0 - } else { - // set the depth number. So B1 is depth 1 and B2 is depth 2 and so on - $this_depth = substr($class, 1); - if (!is_numeric($this_depth)) { - print_error('invalidnum'); - } - } - if ($this_depth < $depth) { - // we are moving back a level in the nesting - break; - } - if ($this_depth > $depth) { - // we are moving in a lvl in nesting - $list .= '
      '; - $list = lesson_importppt_build_list($matches, $list, $i, $this_depth); - // once we return back, should go to the start of the while - continue; - } - // no depth changes, so add the match to our list - if ($cleanstring = lesson_importppt_clean_text($matches[3][$i])) { - $list .= '
    • '.lesson_importppt_clean_text($matches[3][$i]).'
    • '; - } - $i++; - } else { - // not a B class, so get out of here... - break; - } - } - // end the list and return it - $list .= '
    '; - return $list; - -} - -/** - * Given an html tag, this function will - * - * @param string $string - * @return string - */ -function lesson_importppt_isolate_class($string) { - if($class = strstr($string, 'class=')) { // first step in isolating the class - $class = substr($class, strpos($class, '=')+1); // this gets rid of
    - return substr($class, 0, strpos($class, '>')); - } - } else { - // no class defined in the tag - return ''; - } -} - -/** - * This function strips off the random chars that ppt puts infront of bullet lists - * - * @param string $string - * @return string - */ -function lesson_importppt_clean_text($string) { - $chop = 1; // default: just a single char infront of the content - - // look for any other crazy things that may be infront of the content - if (strstr($string, '<') and strpos($string, '<') == 0) { // look for the < in the sting and make sure it is in the front - $chop = 4; // increase the $chop - } - // may need to add more later.... - - $string = substr($string, $chop); - - if ($string != ' ') { - return $string; - } else { - return false; - } -} - -/** - * Creates objects an object with the page and answers that are to be inserted into the database - * - * @param array $pageobjects - * @param int $lessonid - * @return array - */ -function lesson_create_objects($pageobjects, $lessonid) { - - $branchtables = array(); - $branchtable = new stdClass; - - // all pages have this info - $page = new stdClass(); - $page->lessonid = $lessonid; - $page->prevpageid = 0; - $page->nextpageid = 0; - $page->qtype = LESSON_PAGE_BRANCHTABLE; - $page->qoption = 0; - $page->layout = 1; - $page->display = 1; - $page->timecreated = time(); - $page->timemodified = 0; - - // all answers are the same - $answer = new stdClass(); - $answer->lessonid = $lessonid; - $answer->jumpto = LESSON_NEXTPAGE; - $answer->grade = 0; - $answer->score = 0; - $answer->flags = 0; - $answer->timecreated = time(); - $answer->timemodified = 0; - $answer->answer = "Next"; - $answer->response = ""; - - $answers[] = clone($answer); - - $answer->jumpto = LESSON_PREVIOUSPAGE; - $answer->answer = "Previous"; - - $answers[] = clone($answer); - - $branchtable->answers = $answers; - - $i = 1; - - foreach ($pageobjects as $pageobject) { - if ($pageobject->title == '') { - $page->title = "Page $i"; // no title set so make a generic one - } else { - $page->title = $pageobject->title; - } - $page->contents = ''; - - // nab all the images first - $page->images = $pageobject->images; - foreach ($page->images as $image) { - $imagetag = ''; - $imagetag = str_replace("\n", '', $imagetag); - $imagetag = str_replace("\r", '', $imagetag); - $imagetag = str_replace("'", '"', $imagetag); // imgstyle - $page->contents .= $imagetag; - } - // go through the contents array and put

    tags around each element and strip out \n which I have found to be unneccessary - foreach ($pageobject->contents as $content) { - $content = str_replace("\n", '', $content); - $content = str_replace("\r", '', $content); - $content = str_replace(' ', '', $content); // puts in returns? - $content = '

    '.$content.'

    '; - $page->contents .= $content; - } - - $branchtable->page = clone($page); // add the page - $branchtables[] = clone($branchtable); // add it all to our array - $i++; - } - - return $branchtables; -} - -/** - * Form displayed to the user asking them to select a file to upload - * - * @copyright 2009 Sam Hemelryk - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class lesson_importppt_form extends moodleform { - - public function definition() { - global $COURSE; - - $mform = $this->_form; - - $mform->addElement('hidden', 'id'); - $mform->setType('id', PARAM_INT); - - $mform->addElement('hidden', 'pageid'); - $mform->setType('pageid', PARAM_INT); - - $filepickeroptions = array(); - $filepickeroptions['filetypes'] = array('*.zip'); - $filepickeroptions['maxbytes'] = $COURSE->maxbytes; - $mform->addElement('filepicker', 'pptzip', get_string('upload'), null, $filepickeroptions); - $mform->addRule('pptzip', null, 'required', null, 'client'); - - $this->add_action_buttons(null, get_string("uploadthisfile")); - } - -} \ No newline at end of file diff --git a/mod/lesson/renderer.php b/mod/lesson/renderer.php index 765b7ca6e07..a37f40b160f 100644 --- a/mod/lesson/renderer.php +++ b/mod/lesson/renderer.php @@ -364,9 +364,6 @@ class mod_lesson_renderer extends plugin_renderer_base { $importquestionsurl = new moodle_url('/mod/lesson/import.php',array('id'=>$this->page->cm->id, 'pageid'=>$prevpageid)); $links[] = html_writer::link($importquestionsurl, get_string('importquestions', 'lesson')); - $importppturl = new moodle_url('/mod/lesson/importppt.php',array('id'=>$this->page->cm->id, 'pageid'=>$prevpageid)); - $links[] = html_writer::link($importppturl, get_string('importppt', 'lesson')); - $manager = lesson_page_type_manager::get($lesson); foreach ($manager->get_add_page_type_links($prevpageid) as $link) { $link['addurl']->param('firstpage', 1);