/**
* implements a hook for the page_module block to add
* the link allowing live refreshing of the content
*
*
*/
function customlabel_set_instance(&$block)
{
    global $USER, $CFG, $COURSE, $DB;
    // transfer content from title to content
    $block->title = '';
    // fake unpacks object's load
    $data = json_decode(base64_decode($block->moduleinstance->content));
    // If failed in getting content. It happens sometimes, ... do nothing to let content be safed manually
    if (is_null($data) || !is_object($data)) {
        return false;
    }
    // realize a pseudo update
    $data->title = $block->moduleinstance->title;
    $data->content = $block->moduleinstance->content;
    $data->labelclass = $block->moduleinstance->labelclass;
    // fixes broken serialized contents
    if (!isset($block->moduleinstance->title)) {
        $block->moduleinstance->title = '';
    }
    // fixes broken serialized contents
    $instance = customlabel_load_class($data);
    $block->moduleinstance->processedcontent = $instance->make_content();
    $block->moduleinstance->name = $instance->title;
    // this realizes the template
    $block->moduleinstance->timemodified = time();
    $block->content->text = $block->moduleinstance->processedcontent;
    $block->moduleinstance->title = str_replace("'", "''", $block->moduleinstance->title);
    $result = $DB->update_record('customlabel', $block->moduleinstance);
    return true;
}
 public function module_block_setup()
 {
     global $CFG, $COURSE, $DB;
     $cm = $this->get_cm();
     $customlabel = $DB->get_record('customlabel', array('id' => $cm->instance));
     $instance = customlabel_load_class($customlabel, $customlabel->labelclass);
     $block = null;
     if ($customlabel and !customlabel_is_hidden_byrole($block, $cm->id)) {
         $this->append_content($instance->get_content());
     }
 }
Пример #3
0
 public function module_block_setup()
 {
     global $CFG, $COURSE, $DB;
     $cm = $this->get_cm();
     $customlabel = $DB->get_record('customlabel', array('id' => $cm->instance));
     $instance = customlabel_load_class($customlabel, $customlabel->labelclass);
     $block = null;
     $context = context_module::instance($cm->id);
     if ($customlabel && has_capability('customlabeltype/' . $instance->labelclass . ':view', $context)) {
         $this->append_content($instance->get_content());
     }
 }
Пример #4
0
/**
* Given an object containing all the necessary data, 
* (defined by the form in mod.html) this function 
* will update an existing instance with new data.
*/
function customlabel_update_instance($customlabel)
{
    global $CFG;
    $oldinstance = get_record('customlabel', 'id', $customlabel->instance);
    if ($oldinstance->labelclass != $customlabel->labelclass) {
        $customlabel->content = '';
        $customlabel->name = '';
    } else {
        $customlabel->content = json_encode($customlabel);
        $instance = customlabel_load_class($customlabel);
        $instance->process_form_fields();
        $instance->preprocess_data();
        $customlabel->name = $instance->get_name();
    }
    $customlabel->timemodified = time();
    $customlabel->id = $customlabel->instance;
    $customlabel = customlabel_addslashes_fields($customlabel);
    $result = update_record('customlabel', $customlabel);
    // needed to update modinfo
    rebuild_course_cache();
    return $result;
}
Пример #5
0
function customlabel_course_preprocess_filepickers($c)
{
    global $DB;
    $fs = get_file_storage();
    if ($customlabels = $DB->get_records('customlabel', array('course' => $c->id))) {
        foreach ($customlabels as $c) {
            mtrace("preprocessing customlabel {$c->name} ");
            $cm = get_coursemodule_from_instance('customlabel', $c->id);
            $instance = customlabel_load_class($c);
            foreach ($instance->fields as $f) {
                if ($f->type == 'filepicker') {
                    mtrace("preprocessing filepicker {$f->name} ");
                    $oldname = $f->name . 'url';
                    $content = json_decode(base64_decode($c->content));
                    mtrace("checking field content : " . @$content->{$oldname});
                    if (!empty($content->{$oldname})) {
                        // We have an old data there, we need get the file record it and add it to the filestore
                        if (preg_match('#^http#', $content->{$oldname})) {
                            // Fire a CURL to get the file, store into filestore
                            $filerecord = new Stdclass();
                            $filerecord->contextid = context_module::instance($cm->id)->id;
                            $filerecord->component = 'mod_customlabel';
                            $filerecord->filearea = $f->name;
                            $filerecord->itemid = 0;
                            $filerecord->filepath = '/';
                            $filerecord->filename = basename($content->{$oldname});
                            try {
                                $fs->create_file_from_url($filerecord, $content->{$oldname}, null, true);
                            } catch (Exception $e) {
                            }
                            mtrace("file created ");
                        }
                    }
                }
            }
        }
    }
}
Пример #6
0
/**
 * implements a hook for the page_module block to add
 * the link allowing live refreshing of the content
 *
 *
 */
function customlabel_set_instance(&$block)
{
    global $USER, $CFG, $COURSE, $DB;
    // Transfer content from title to content.
    $block->title = '';
    // Fake unpacks object's load.
    $data = json_decode(base64_decode($block->moduleinstance->content));
    // If failed in getting content. It happens sometimes, ... do nothing to let content be safed manually
    if (is_null($data) || !is_object($data)) {
        return false;
    }
    // Realize a pseudo update.
    $data->title = $block->moduleinstance->title;
    $data->content = $block->moduleinstance->content;
    $data->labelclass = $block->moduleinstance->labelclass;
    // fixes broken serialized contents
    $context = context_module::instance($block->cm->id);
    if (!has_capability('customlabeltype/' . $data->labelclass . ':view', $context)) {
        return false;
    }
    if (!isset($block->moduleinstance->title)) {
        // Fixes broken serialized contents.
        $block->moduleinstance->title = '';
    }
    $instance = customlabel_load_class($data);
    $block->moduleinstance->processedcontent = $instance->make_content();
    $block->moduleinstance->name = $instance->title;
    // this realizes the template
    $block->moduleinstance->timemodified = time();
    $block->content->text = $block->moduleinstance->processedcontent;
    $block->moduleinstance->title = str_replace("'", "''", $block->moduleinstance->title);
    $result = $DB->update_record('customlabel', $block->moduleinstance);
    $context = context_module::instance($block->cm->id);
    $block->content->text = file_rewrite_pluginfile_urls($block->content->text, 'pluginfile.php', $context->id, 'mod_customlabel', 'contentfiles', 0);
    return true;
}
Пример #7
0
 public function set_data($customlabel)
 {
     if (empty($customlabel->labelclass)) {
         $customlabel->labelclass = 'text';
         $customlabel->content = '';
         $customlabel->processedcontent = '';
         $customlabel->intro = '';
         $customlabel->introformat = 0;
     }
     $instance = customlabel_load_class($customlabel, $customlabel->labelclass);
     $formdata = $customlabel;
     // Get dynamic part of data and add to fixed model part from customlabel record.
     $formdatadyn = (array) json_decode(base64_decode($customlabel->content));
     foreach ($formdatadyn as $key => $value) {
         // Discard all moodle core data that should be there.
         if (in_array($key, array('coursemodule', 'instance', 'sesskey', 'module', 'section'))) {
             continue;
         }
         // Ignore old Moodle 1.9 stuff.
         if (in_array($key, array('safe_content', 'usesafe'))) {
             continue;
         }
         $formdata->{$key} = $value;
         if (is_object($formdata->{$key}) && isset($formdata->{$key}->text)) {
             $formdata->{$key} = (array) $formdata->{$key};
         }
     }
     // Prepare editor for intro standard field.
     // Prepare editors for all textarea|editor dynamic fields prepared in model.
     foreach ($instance->fields as $field) {
         if (preg_match('/editor|textarea/', $field->type)) {
             $fieldname = $field->name;
             $editorname = $fieldname . '_editor';
             $formdata->{$editorname} = array('text' => @$formdata->{$fieldname}, 'format' => FORMAT_HTML);
             $editoroptions = self::editor_options();
             $editoroptions['context'] = $this->context;
             // Fakes format field.
             $fieldnameformat = $fieldname . 'format';
             $customlabel->{$fieldnameformat} = FORMAT_HTML;
             $defaults = file_prepare_standard_editor($customlabel, $fieldname, $editoroptions, $this->context, 'mod_customlabel', 'contentfiles', @$customlabel->{$fieldname});
         }
         if (preg_match('/datasource$/', $field->type)) {
             $name = $field->name;
             $formdata->{$name} = @$formdatadyn[$name . 'option'];
         }
         // Todo : limit upload size on course settings
         $maxbytes = -1;
         if ($field->type == 'filepicker') {
             $draftitemid = file_get_submitted_draft_itemid($field->name);
             $groupname = $field->name . 'group';
             $maxfiles = 1;
             file_prepare_draft_area($draftitemid, $this->context->id, 'mod_customlabel', $field->name, 0, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => $maxfiles));
             $formdata->{$groupname} = array($field->name => $draftitemid);
         }
     }
     // Prepare type selector value.
     if ($tomodel = optional_param('type', '', PARAM_TEXT)) {
         $formdata->labelclass = $tomodel;
     }
     $formdata->sesskey = sesskey();
     parent::set_data($formdata);
 }
    if (!($course = $DB->get_record('course', array('id' => $cm->course)))) {
        error("Course is misconfigured");
    }
    if (!($customlabel = $DB->get_record('customlabel', array('id' => $cm->instance)))) {
        error("Course module is incorrect");
    }
} else {
    if (!($customlabel = $DB->get_record('customlabel', array('id' => $l)))) {
        error("Course module is incorrect");
    }
    if (!($course = $DB->get_record('course', array('id' => $customlabel->course)))) {
        error("Course is misconfigured");
    }
    if (!($cm = get_coursemodule_from_instance("customlabel", $customlabel->id, $course->id))) {
        error("Course Module ID was incorrect");
    }
}
require_login($course->id);
/// if we are exporting to XML
if ($what == 'xml') {
    echo $OUTPUT->header();
    $customlabel = $DB->get_record('customlabel', array('id' => $l));
    $instance = customlabel_load_class($customlabel);
    $xml = $instance->get_xml();
    $xml = str_replace('<', '&lt;', $xml);
    $xml = str_replace('>', '&gt;', $xml);
    echo "<pre>" . $xml . "</pre>";
    die;
}
/// normal view "in-situ"
redirect($CFG->wwwroot . "/course/view.php?id={$course->id}");
Пример #9
0
function customlabel_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array())
{
    global $CFG, $DB;
    if ($context->contextlevel != CONTEXT_MODULE) {
        return false;
    }
    require_course_login($course, true, $cm);
    $id = $cm->instance;
    if (!($customlabel = $DB->get_record('customlabel', array('id' => $id)))) {
        return false;
    }
    $instance = customlabel_load_class($customlabel);
    $fs = get_file_storage();
    $relativepath = implode('/', $args);
    $fullpath = "/{$context->id}/mod_customlabel/{$filearea}/{$relativepath}";
    if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) {
        return false;
    }
    // Nasty hack because we do not have file revisions in customlabels yet.
    $lifetime = 0 + @$CFG->filelifetime;
    if ($lifetime > 60 * 10) {
        $lifetime = 60 * 10;
    }
    // finally send the file
    send_stored_file($file, $lifetime, 0, $forcedownload, $options);
}
        } else {
            $preselection[$tmp] = $sel;
            if (is_array($sel)) {
                $constraintsarr = $constraintsarr + $sel;
            }
            $iskey = true;
        }
    }
}
if (!($targets = explode(',', $targetstr))) {
    exit;
}
// we just need the definition
$customlabel->labelclass = $type;
$customlabel->title = '';
$instance = customlabel_load_class($customlabel, true);
// make a structure with options and reduce possible options to
// acceptable ones
// this is another writing for "get_all_classification_linked_values".
$included = array();
$used = array();
$includedtrace = array();
if (!empty($constraints)) {
    while ($constraint = array_pop($constraintsarr)) {
        if (empty($constraint)) {
            continue;
        }
        $used[] = $constraint;
        if ($constraintpeerrecs = $DB->get_records_select('classification_constraint', " value1 = '{$constraint}' OR value2 = '{$constraint}' ")) {
            foreach ($constraintpeerrecs as $apeer) {
                if ($apeer->value1 == $constraint) {
Пример #11
0
/**
 * Processes a single customlabel by recalculating the content
 * @param ref $customlabel a customlabel record
 * @param string $labelclassname the real class of the element. 
 * @param string $course the current complete course record
 *
 */
function customlabel_regenerate(&$customlabel, $labelclassname, &$course)
{
    global $DB;
    mtrace("\tprocessing customlabel {$customlabel->id}");
    // Renew the template.
    // Fake unpacks object's load.
    $data = json_decode(base64_decode($customlabel->content));
    if (is_null($data)) {
        $data = new StdClass();
    }
    if (!is_object($data)) {
        $data = new StdClass();
        // reset old serialized data
    }
    // Realize a pseudo update.
    $data->content = $customlabel->content;
    $data->labelclass = $labelclassname;
    // Fixes broken serialized contents.
    if (!isset($data->title)) {
        // Fixes broken serialized contents.
        $data->title = '';
    }
    $instance = customlabel_load_class($data);
    $instance->preprocess_data();
    $instance->process_form_fields();
    $instance->process_datasource_fields();
    $instance->postprocess_data($course);
    $customlabel->processedcontent = $instance->make_content('', $course);
    // This realizes the template.
    $customlabel->timemodified = time();
    $result = $DB->update_record('customlabel', $customlabel);
    mtrace("\tfinished customlabel {$customlabel->id}");
}
Пример #12
0
/**
* part of standard API.
* returns a single resource search document based on a label id
* @param id the id of the accessible document
* @return a searchable object or null if failure
*/
function customlabel_single_document($id, $itemtype)
{
    global $CFG, $DB;
    $customlabel = $DB->get_record('customlabel', array('id' => $id));
    if ($customlabel) {
        $coursemodule = $DB->get_field('modules', 'id', array('name' => 'customlabel'));
        $cm = $DB->get_record('course_modules', array('module' => $coursemodule, 'instance' => $customlabel->id));
        $customclass = customlabel_load_class($customlabel, true);
        $context = context_module::instance($cm->id);
        return new CustomLabelSearchDocument(get_object_vars($customlabel), $customclass, $context->id);
    }
    return null;
}
Пример #13
0
/**
* part of standard API
* this function does not need a content iterator, returns all the info
* itself;
* @param notneeded to comply API, remember to fake the iterator array though
* @uses CFG
* @return an array of searchable documents
*/
function customlabel_get_content_for_index(&$customlabel)
{
    global $CFG;
    // starting with Moodle native resources
    $documents = array();
    $coursemodule = get_field('modules', 'id', 'name', 'customlabel');
    $cm = get_record('course_modules', 'course', $customlabel->course, 'module', $coursemodule, 'instance', $customlabel->id);
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
    $customclass = customlabel_load_class($customlabel->labelclass, true);
    if ($customclass) {
        $documents[] = new CustomLabelSearchDocument(get_object_vars($customlabel), $customclass, $context->id);
        mtrace("finished label {$customlabel->id}");
    } else {
        mtrace("ignoring unknown label type {$customlabel->labelclass} instance");
    }
    return $documents;
}
/**
 * Given a course_module object, this function returns any
 * "extra" information that may be needed when printing
 * this activity in a course listing.
 * See get_array_of_activities() in course/lib.php
 */
function customlabel_get_coursemodule_info($coursemodule)
{
    global $CFG, $DB;
    if ($customlabel = $DB->get_record('customlabel', array('id' => $coursemodule->instance), 'id, labelclass, intro, title, name, content, processedcontent')) {
        $instance = customlabel_load_class($customlabel, $customlabel->labelclass);
        $info = new stdClass();
        $info->name = $customlabel->name;
        $info->extra = '';
        // $customcontent = json_decode(base64_decode($customlabel->content));
        $info->extra = urlencode($customlabel->title);
        return $info;
    } else {
        return null;
    }
}
                 if (is_null($data)) {
                     $data = new StdClass();
                 }
                 if (!is_object($data)) {
                     $data = new StdClass();
                     // reset old serialized data
                 }
                 // realize a pseudo update
                 $data->content = $customlabel->content;
                 $data->labelclass = $labelclassname;
                 // fixes broken serialized contents
                 if (!isset($data->title)) {
                     $data->title = '';
                 }
                 // fixes broken serialized contents
                 $instance = customlabel_load_class($data);
                 $instance->preprocess_data();
                 $instance->process_form_fields();
                 $instance->process_datasource_fields();
                 $instance->postprocess_data($course);
                 $customlabel->processedcontent = $instance->make_content();
                 // this realizes the template
                 $customlabel->timemodified = time();
                 $result = $DB->update_record('customlabel', $customlabel);
                 mtrace("\tfinished customlabel {$customlabel->id}");
             }
         }
     }
 }
 echo "</pre>";
 echo $OUTPUT->container_end();