|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 require_once($CFG->libdir . '/portfolio/caller.php'); 00028 00032 class glossary_full_portfolio_caller extends portfolio_module_caller_base { 00033 00034 private $glossary; 00035 private $exportdata; 00036 private $keyedfiles = array(); // keyed on entry 00037 00044 public static function expected_callbackargs() { 00045 return array( 00046 'id' => true, 00047 ); 00048 } 00049 00055 public function load_data() { 00056 global $DB; 00057 if (!$this->cm = get_coursemodule_from_id('glossary', $this->id)) { 00058 throw new portfolio_caller_exception('invalidid', 'glossary'); 00059 } 00060 if (!$this->glossary = $DB->get_record('glossary', array('id' => $this->cm->instance))) { 00061 throw new portfolio_caller_exception('invalidid', 'glossary'); 00062 } 00063 $entries = $DB->get_records('glossary_entries', array('glossaryid' => $this->glossary->id)); 00064 list($where, $params) = $DB->get_in_or_equal(array_keys($entries)); 00065 00066 $aliases = $DB->get_records_select('glossary_alias', 'entryid ' . $where, $params); 00067 $categoryentries = $DB->get_records_sql('SELECT ec.entryid, c.name FROM {glossary_entries_categories} ec 00068 JOIN {glossary_categories} c 00069 ON c.id = ec.categoryid 00070 WHERE ec.entryid ' . $where, $params); 00071 00072 $this->exportdata = array('entries' => $entries, 'aliases' => $aliases, 'categoryentries' => $categoryentries); 00073 $fs = get_file_storage(); 00074 $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); 00075 $this->multifiles = array(); 00076 foreach (array_keys($entries) as $entry) { 00077 $this->keyedfiles[$entry] = array_merge( 00078 $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $entry, "timemodified", false), 00079 $fs->get_area_files($context->id, 'mod_glossary', 'entry', $entry, "timemodified", false) 00080 ); 00081 $this->multifiles = array_merge($this->multifiles, $this->keyedfiles[$entry]); 00082 } 00083 } 00084 00090 public function expected_time() { 00091 $filetime = portfolio_expected_time_file($this->multifiles); 00092 $dbtime = portfolio_expected_time_db(count($this->exportdata['entries'])); 00093 return ($filetime > $dbtime) ? $filetime : $dbtime; 00094 } 00095 00101 public function get_sha1() { 00102 $file = ''; 00103 if ($this->multifiles) { 00104 $file = $this->get_sha1_file(); 00105 } 00106 return sha1(serialize($this->exportdata) . $file); 00107 } 00108 00114 public function prepare_package() { 00115 $entries = $this->exportdata['entries']; 00116 $aliases = array(); 00117 $categories = array(); 00118 if (is_array($this->exportdata['aliases'])) { 00119 foreach ($this->exportdata['aliases'] as $alias) { 00120 if (!array_key_exists($alias->entryid, $aliases)) { 00121 $aliases[$alias->entryid] = array(); 00122 } 00123 $aliases[$alias->entryid][] = $alias->alias; 00124 } 00125 } 00126 if (is_array($this->exportdata['categoryentries'])) { 00127 foreach ($this->exportdata['categoryentries'] as $cat) { 00128 if (!array_key_exists($cat->entryid, $categories)) { 00129 $categories[$cat->entryid] = array(); 00130 } 00131 $categories[$cat->entryid][] = $cat->name; 00132 } 00133 } 00134 if ($this->get('exporter')->get('formatclass') == PORTFOLIO_FORMAT_SPREADSHEET) { 00135 $csv = glossary_generate_export_csv($entries, $aliases, $categories); 00136 $this->exporter->write_new_file($csv, clean_filename($this->cm->name) . '.csv', false); 00137 return; 00138 } else if ($this->get('exporter')->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) { 00139 $ids = array(); // keep track of these to make into a selection later 00140 global $USER, $DB; 00141 $writer = $this->get('exporter')->get('format')->leap2a_writer($USER); 00142 $format = $this->exporter->get('format'); 00143 $filename = $this->get('exporter')->get('format')->manifest_name(); 00144 foreach ($entries as $e) { 00145 $content = glossary_entry_portfolio_caller::entry_content( 00146 $this->course, 00147 $this->cm, 00148 $this->glossary, 00149 $e, 00150 (array_key_exists($e->id, $aliases) ? $aliases[$e->id] : array()), 00151 $format 00152 ); 00153 $entry = new portfolio_format_leap2a_entry('glossaryentry' . $e->id, $e->concept, 'entry', $content); 00154 $entry->author = $DB->get_record('user', array('id' => $e->userid), 'id,firstname,lastname,email'); 00155 $entry->published = $e->timecreated; 00156 $entry->updated = $e->timemodified; 00157 if (!empty($this->keyedfiles[$e->id])) { 00158 $writer->link_files($entry, $this->keyedfiles[$e->id], 'glossaryentry' . $e->id . 'file'); 00159 foreach ($this->keyedfiles[$e->id] as $file) { 00160 $this->exporter->copy_existing_file($file); 00161 } 00162 } 00163 if (!empty($categories[$e->id])) { 00164 foreach ($categories[$e->id] as $cat) { 00165 // this essentially treats them as plain tags 00166 // leap has the idea of category schemes 00167 // but I think this is overkill here 00168 $entry->add_category($cat); 00169 } 00170 } 00171 $writer->add_entry($entry); 00172 $ids[] = $entry->id; 00173 } 00174 $selection = new portfolio_format_leap2a_entry('wholeglossary' . $this->glossary->id, get_string('modulename', 'glossary'), 'selection'); 00175 $writer->add_entry($selection); 00176 $writer->make_selection($selection, $ids, 'Grouping'); 00177 $content = $writer->to_xml(); 00178 } 00179 $this->exporter->write_new_file($content, $filename, true); 00180 } 00181 00187 public function check_permissions() { 00188 return has_capability('mod/glossary:export', get_context_instance(CONTEXT_MODULE, $this->cm->id)); 00189 } 00190 00196 public static function display_name() { 00197 return get_string('modulename', 'glossary'); 00198 } 00199 00205 public static function base_supported_formats() { 00206 return array(PORTFOLIO_FORMAT_SPREADSHEET, PORTFOLIO_FORMAT_LEAP2A); 00207 } 00208 } 00209 00217 class glossary_entry_portfolio_caller extends portfolio_module_caller_base { 00218 00219 private $glossary; 00220 private $entry; 00221 protected $entryid; 00222 /* 00223 * @return array 00224 */ 00225 public static function expected_callbackargs() { 00226 return array( 00227 'entryid' => true, 00228 'id' => true, 00229 ); 00230 } 00231 00237 public function load_data() { 00238 global $DB; 00239 if (!$this->cm = get_coursemodule_from_id('glossary', $this->id)) { 00240 throw new portfolio_caller_exception('invalidid', 'glossary'); 00241 } 00242 if (!$this->glossary = $DB->get_record('glossary', array('id' => $this->cm->instance))) { 00243 throw new portfolio_caller_exception('invalidid', 'glossary'); 00244 } 00245 if ($this->entryid) { 00246 if (!$this->entry = $DB->get_record('glossary_entries', array('id' => $this->entryid))) { 00247 throw new portfolio_caller_exception('noentry', 'glossary'); 00248 } 00249 // in case we don't have USER this will make the entry be printed 00250 $this->entry->approved = true; 00251 } 00252 $this->categories = $DB->get_records_sql('SELECT ec.entryid, c.name FROM {glossary_entries_categories} ec 00253 JOIN {glossary_categories} c 00254 ON c.id = ec.categoryid 00255 WHERE ec.entryid = ?', array($this->entryid)); 00256 $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); 00257 if ($this->entry->sourceglossaryid == $this->cm->instance) { 00258 if ($maincm = get_coursemodule_from_instance('glossary', $this->entry->glossaryid)) { 00259 $context = get_context_instance(CONTEXT_MODULE, $maincm->id); 00260 } 00261 } 00262 $this->aliases = $DB->get_record('glossary_alias', array('entryid'=>$this->entryid)); 00263 $fs = get_file_storage(); 00264 $this->multifiles = array_merge( 00265 $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $this->entry->id, "timemodified", false), 00266 $fs->get_area_files($context->id, 'mod_glossary', 'entry', $this->entry->id, "timemodified", false) 00267 ); 00268 } 00269 00275 public function expected_time() { 00276 return PORTFOLIO_TIME_LOW; 00277 } 00278 00284 public function check_permissions() { 00285 $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); 00286 return has_capability('mod/glossary:exportentry', $context) 00287 || ($this->entry->userid == $this->user->id && has_capability('mod/glossary:exportownentry', $context)); 00288 } 00289 00295 public static function display_name() { 00296 return get_string('modulename', 'glossary'); 00297 } 00298 00304 public function prepare_package() { 00305 global $DB; 00306 00307 $format = $this->exporter->get('format'); 00308 $content = self::entry_content($this->course, $this->cm, $this->glossary, $this->entry, $this->aliases, $format); 00309 00310 if ($this->exporter->get('formatclass') === PORTFOLIO_FORMAT_PLAINHTML) { 00311 $filename = clean_filename($this->entry->concept) . '.html'; 00312 $this->exporter->write_new_file($content, $filename); 00313 00314 } else if ($this->exporter->get('formatclass') === PORTFOLIO_FORMAT_RICHHTML) { 00315 if ($this->multifiles) { 00316 foreach ($this->multifiles as $file) { 00317 $this->exporter->copy_existing_file($file); 00318 } 00319 } 00320 $filename = clean_filename($this->entry->concept) . '.html'; 00321 $this->exporter->write_new_file($content, $filename); 00322 00323 } else if ($this->exporter->get('formatclass') === PORTFOLIO_FORMAT_LEAP2A) { 00324 $writer = $this->get('exporter')->get('format')->leap2a_writer(); 00325 $entry = new portfolio_format_leap2a_entry('glossaryentry' . $this->entry->id, $this->entry->concept, 'entry', $content); 00326 $entry->author = $DB->get_record('user', array('id' => $this->entry->userid), 'id,firstname,lastname,email'); 00327 $entry->published = $this->entry->timecreated; 00328 $entry->updated = $this->entry->timemodified; 00329 if ($this->multifiles) { 00330 $writer->link_files($entry, $this->multifiles); 00331 foreach ($this->multifiles as $file) { 00332 $this->exporter->copy_existing_file($file); 00333 } 00334 } 00335 if ($this->categories) { 00336 foreach ($this->categories as $cat) { 00337 // this essentially treats them as plain tags 00338 // leap has the idea of category schemes 00339 // but I think this is overkill here 00340 $entry->add_category($cat->name); 00341 } 00342 } 00343 $writer->add_entry($entry); 00344 $content = $writer->to_xml(); 00345 $filename = $this->get('exporter')->get('format')->manifest_name(); 00346 $this->exporter->write_new_file($content, $filename); 00347 00348 } else { 00349 throw new portfolio_caller_exception('unexpected_format_class', 'glossary'); 00350 } 00351 } 00352 00358 public function get_sha1() { 00359 if ($this->multifiles) { 00360 return sha1(serialize($this->entry) . $this->get_sha1_file()); 00361 } 00362 return sha1(serialize($this->entry)); 00363 } 00364 00370 public static function base_supported_formats() { 00371 return array(PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_LEAP2A); 00372 } 00373 00388 public static function entry_content($course, $cm, $glossary, $entry, $aliases, $format) { 00389 global $OUTPUT, $DB; 00390 $entry = clone $entry; 00391 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00392 $options = portfolio_format_text_options(); 00393 $options->trusted = $entry->definitiontrust; 00394 $options->context = $context; 00395 00396 $output = '<table class="glossarypost dictionary" cellspacing="0">' . "\n"; 00397 $output .= '<tr valign="top">' . "\n"; 00398 $output .= '<td class="entry">' . "\n"; 00399 00400 $output .= '<div class="concept">'; 00401 $output .= format_text($OUTPUT->heading($entry->concept, 3), FORMAT_MOODLE, $options); 00402 $output .= '</div> ' . "\n"; 00403 00404 $entry->definition = format_text($entry->definition, $entry->definitionformat, $options); 00405 $output .= portfolio_rewrite_pluginfile_urls($entry->definition, $context->id, 'mod_glossary', 'entry', $entry->id, $format); 00406 00407 if (isset($entry->footer)) { 00408 $output .= $entry->footer; 00409 } 00410 00411 $output .= '</td></tr>' . "\n"; 00412 00413 if (!empty($aliases)) { 00414 $aliases = explode(',', $aliases->alias); 00415 $output .= '<tr valign="top"><td class="entrylowersection">'; 00416 $key = (count($aliases) == 1) ? 'alias' : 'aliases'; 00417 $output .= get_string($key, 'glossary') . ': '; 00418 foreach ($aliases as $alias) { 00419 $output .= s($alias) . ','; 00420 } 00421 $output = substr($output, 0, -1); 00422 $output .= '</td></tr>' . "\n"; 00423 } 00424 00425 if ($entry->sourceglossaryid == $cm->instance) { 00426 if (!$maincm = get_coursemodule_from_instance('glossary', $entry->glossaryid)) { 00427 return ''; 00428 } 00429 $filecontext = get_context_instance(CONTEXT_MODULE, $maincm->id); 00430 00431 } else { 00432 $filecontext = $context; 00433 } 00434 $fs = get_file_storage(); 00435 if ($files = $fs->get_area_files($filecontext->id, 'mod_glossary', 'attachment', $entry->id, "timemodified", false)) { 00436 $output .= '<table border="0" width="100%"><tr><td>' . "\n"; 00437 00438 foreach ($files as $file) { 00439 $output .= $format->file_output($file); 00440 } 00441 $output .= '</td></tr></table>' . "\n"; 00442 } 00443 00444 $output .= '</table>' . "\n"; 00445 00446 return $output; 00447 } 00448 } 00449