MDL-21695 adding help string
[moodle.git] / mod / lesson / importpptlib.php
blob2161dae69ad4e3efe1a61d88d82a3a6b70df636e
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Contains functions used by importppt.php that naturally pertain to importing
20 * powerpoint presentations into the lesson module
22 * @package lesson
23 * @copyright 2009 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 **/
27 /**
28 * A recursive function to build a html list
30 * @param array $matches
31 * @param string $list
32 * @param int $i
33 * @param int $depth
34 * @return string
36 function lesson_importppt_build_list(array &$matches, $list, &$i, $depth) {
37 while($i < count($matches[1])) {
39 $class = lesson_importppt_isolate_class($matches[1][$i]);
41 if (strstr($class, 'B')) { // make sure we are still working with bullet classes
42 if ($class == 'B') {
43 $this_depth = 0; // calling class B depth 0
44 } else {
45 // set the depth number. So B1 is depth 1 and B2 is depth 2 and so on
46 $this_depth = substr($class, 1);
47 if (!is_numeric($this_depth)) {
48 print_error('invalidnum');
51 if ($this_depth < $depth) {
52 // we are moving back a level in the nesting
53 break;
55 if ($this_depth > $depth) {
56 // we are moving in a lvl in nesting
57 $list .= '<ul>';
58 $list = lesson_importppt_build_list($matches, $list, $i, $this_depth);
59 // once we return back, should go to the start of the while
60 continue;
62 // no depth changes, so add the match to our list
63 if ($cleanstring = lesson_importppt_clean_text($matches[3][$i])) {
64 $list .= '<li>'.lesson_importppt_clean_text($matches[3][$i]).'</li>';
66 $i++;
67 } else {
68 // not a B class, so get out of here...
69 break;
72 // end the list and return it
73 $list .= '</ul>';
74 return $list;
78 /**
79 * Given an html tag, this function will
81 * @param string $string
82 * @return string
84 function lesson_importppt_isolate_class($string) {
85 if($class = strstr($string, 'class=')) { // first step in isolating the class
86 $class = substr($class, strpos($class, '=')+1); // this gets rid of <div blawblaw class= there are no "" or '' around the class name ...sigh...
87 if (strstr($class, ' ')) {
88 // spaces found, so cut off everything off after the first space
89 return substr($class, 0, strpos($class, ' '));
90 } else {
91 // no spaces so nothing else in the div tag, cut off the >
92 return substr($class, 0, strpos($class, '>'));
94 } else {
95 // no class defined in the tag
96 return '';
101 * This function strips off the random chars that ppt puts infront of bullet lists
103 * @param string $string
104 * @return string
106 function lesson_importppt_clean_text($string) {
107 $chop = 1; // default: just a single char infront of the content
109 // look for any other crazy things that may be infront of the content
110 if (strstr($string, '&lt;') and strpos($string, '&lt;') == 0) { // look for the &lt; in the sting and make sure it is in the front
111 $chop = 4; // increase the $chop
113 // may need to add more later....
115 $string = substr($string, $chop);
117 if ($string != '&#13;') {
118 return $string;
119 } else {
120 return false;
125 * Creates objects an object with the page and answers that are to be inserted into the database
127 * @param array $pageobjects
128 * @param int $lessonid
129 * @return array
131 function lesson_create_objects($pageobjects, $lessonid) {
133 $branchtables = array();
134 $branchtable = new stdClass;
136 // all pages have this info
137 $page->lessonid = $lessonid;
138 $page->prevpageid = 0;
139 $page->nextpageid = 0;
140 $page->qtype = LESSON_PAGE_BRANCHTABLE;
141 $page->qoption = 0;
142 $page->layout = 1;
143 $page->display = 1;
144 $page->timecreated = time();
145 $page->timemodified = 0;
147 // all answers are the same
148 $answer->lessonid = $lessonid;
149 $answer->jumpto = LESSON_NEXTPAGE;
150 $answer->grade = 0;
151 $answer->score = 0;
152 $answer->flags = 0;
153 $answer->timecreated = time();
154 $answer->timemodified = 0;
155 $answer->answer = "Next";
156 $answer->response = "";
158 $answers[] = clone($answer);
160 $answer->jumpto = LESSON_PREVIOUSPAGE;
161 $answer->answer = "Previous";
163 $answers[] = clone($answer);
165 $branchtable->answers = $answers;
167 $i = 1;
169 foreach ($pageobjects as $pageobject) {
170 if ($pageobject->title == '') {
171 $page->title = "Page $i"; // no title set so make a generic one
172 } else {
173 $page->title = $pageobject->title;
175 $page->contents = '';
177 // nab all the images first
178 $page->images = $pageobject->images;
179 foreach ($page->images as $image) {
180 $imagetag = '<img src="@@PLUGINFILE@@'.$image->get_filepath().$image->get_filename().'" title="'.$image->get_filename().'" />';
181 $imagetag = str_replace("\n", '', $imagetag);
182 $imagetag = str_replace("\r", '', $imagetag);
183 $imagetag = str_replace("'", '"', $imagetag); // imgstyle
184 $page->contents .= $imagetag;
186 // go through the contents array and put <p> tags around each element and strip out \n which I have found to be uneccessary
187 foreach ($pageobject->contents as $content) {
188 $content = str_replace("\n", '', $content);
189 $content = str_replace("\r", '', $content);
190 $content = str_replace('&#13;', '', $content); // puts in returns?
191 $content = '<p>'.$content.'</p>';
192 $page->contents .= $content;
195 $branchtable->page = clone($page); // add the page
196 $branchtables[] = clone($branchtable); // add it all to our array
197 $i++;
200 return $branchtables;
204 * Form displayed to the user asking them to select a file to upload
206 * @copyright 2009 Sam Hemelryk
207 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
209 class lesson_importppt_form extends moodleform {
211 public function definition() {
212 global $COURSE;
214 $mform = $this->_form;
216 $mform->addElement('hidden', 'id');
217 $mform->setType('id', PARAM_INT);
219 $mform->addElement('hidden', 'pageid');
220 $mform->setType('pageid', PARAM_INT);
222 $filepickeroptions = array();
223 $filepickeroptions['filetypes'] = array('*.zip');
224 $filepickeroptions['maxbytes'] = $COURSE->maxbytes;
225 $mform->addElement('filepicker', 'pptzip', get_string('upload'), null, $filepickeroptions);
226 $mform->addRule('pptzip', null, 'required', null, 'client');
228 $this->add_action_buttons(null, get_string("uploadthisfile"));