Example #1
0
/**
 * takes a list of records, the current data, a search string,
 * and mode to display prints the translated template
 *
 * @global object
 * @global object
 * @param string $template
 * @param array $records
 * @param object $data
 * @param string $search
 * @param int $page
 * @param bool $return
 * @param object $jumpurl a moodle_url by which to jump back to the record list (can be null)
 * @return mixed
 */
function data_print_template($template, $records, $data, $search = '', $page = 0, $return = false, moodle_url $jumpurl = null)
{
    global $CFG, $DB, $OUTPUT;
    $cm = get_coursemodule_from_instance('data', $data->id);
    $context = context_module::instance($cm->id);
    static $fields = array();
    static $dataid = null;
    if (empty($dataid)) {
        $dataid = $data->id;
    } else {
        if ($dataid != $data->id) {
            $fields = array();
        }
    }
    if (empty($fields)) {
        $fieldrecords = $DB->get_records('data_fields', array('dataid' => $data->id));
        foreach ($fieldrecords as $fieldrecord) {
            $fields[] = data_get_field($fieldrecord, $data);
        }
    }
    if (empty($records)) {
        return;
    }
    if (!$jumpurl) {
        $jumpurl = new moodle_url('/mod/data/view.php', array('d' => $data->id));
    }
    $jumpurl = new moodle_url($jumpurl, array('page' => $page, 'sesskey' => sesskey()));
    foreach ($records as $record) {
        // Might be just one for the single template
        // Replacing tags
        $patterns = array();
        $replacement = array();
        // Then we generate strings to replace for normal tags
        foreach ($fields as $field) {
            $patterns[] = '[[' . $field->field->name . ']]';
            $replacement[] = highlight($search, $field->display_browse_field($record->id, $template));
        }
        $canmanageentries = has_capability('mod/data:manageentries', $context);
        // Replacing special tags (##Edit##, ##Delete##, ##More##)
        $patterns[] = '##edit##';
        $patterns[] = '##delete##';
        if (data_user_can_manage_entry($record, $data, $context)) {
            $replacement[] = '<a href="' . $CFG->wwwroot . '/mod/data/edit.php?d=' . $data->id . '&amp;rid=' . $record->id . '&amp;sesskey=' . sesskey() . '"><img src="' . $OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="' . get_string('edit') . '" title="' . get_string('edit') . '" /></a>';
            $replacement[] = '<a href="' . $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&amp;delete=' . $record->id . '&amp;sesskey=' . sesskey() . '"><img src="' . $OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="' . get_string('delete') . '" title="' . get_string('delete') . '" /></a>';
        } else {
            $replacement[] = '';
            $replacement[] = '';
        }
        $moreurl = $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&amp;rid=' . $record->id;
        if ($search) {
            $moreurl .= '&amp;filter=1';
        }
        $patterns[] = '##more##';
        $replacement[] = '<a href="' . $moreurl . '"><img src="' . $OUTPUT->pix_url('t/preview') . '" class="iconsmall" alt="' . get_string('more', 'data') . '" title="' . get_string('more', 'data') . '" /></a>';
        $patterns[] = '##moreurl##';
        $replacement[] = $moreurl;
        $patterns[] = '##delcheck##';
        if ($canmanageentries) {
            $replacement[] = html_writer::checkbox('delcheck[]', $record->id, false, '', array('class' => 'recordcheckbox'));
        } else {
            $replacement[] = '';
        }
        $patterns[] = '##user##';
        $replacement[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $record->userid . '&amp;course=' . $data->course . '">' . fullname($record) . '</a>';
        $patterns[] = '##userpicture##';
        $ruser = user_picture::unalias($record, null, 'userid');
        $replacement[] = $OUTPUT->user_picture($ruser, array('courseid' => $data->course));
        $patterns[] = '##export##';
        if (!empty($CFG->enableportfolios) && ($template == 'singletemplate' || $template == 'listtemplate') && (has_capability('mod/data:exportentry', $context) || data_isowner($record->id) && has_capability('mod/data:exportownentry', $context))) {
            require_once $CFG->libdir . '/portfoliolib.php';
            $button = new portfolio_add_button();
            $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id, 'recordid' => $record->id), 'mod_data');
            list($formats, $files) = data_portfolio_caller::formats($fields, $record);
            $button->set_formats($formats);
            $replacement[] = $button->to_html(PORTFOLIO_ADD_ICON_LINK);
        } else {
            $replacement[] = '';
        }
        $patterns[] = '##timeadded##';
        $replacement[] = userdate($record->timecreated);
        $patterns[] = '##timemodified##';
        $replacement[] = userdate($record->timemodified);
        $patterns[] = '##approve##';
        if (has_capability('mod/data:approve', $context) && $data->approval && !$record->approved) {
            $approveurl = new moodle_url($jumpurl, array('approve' => $record->id));
            $approveicon = new pix_icon('t/approve', get_string('approve', 'data'), '', array('class' => 'iconsmall'));
            $replacement[] = html_writer::tag('span', $OUTPUT->action_icon($approveurl, $approveicon), array('class' => 'approve'));
        } else {
            $replacement[] = '';
        }
        $patterns[] = '##disapprove##';
        if (has_capability('mod/data:approve', $context) && $data->approval && $record->approved) {
            $disapproveurl = new moodle_url($jumpurl, array('disapprove' => $record->id));
            $disapproveicon = new pix_icon('t/block', get_string('disapprove', 'data'), '', array('class' => 'iconsmall'));
            $replacement[] = html_writer::tag('span', $OUTPUT->action_icon($disapproveurl, $disapproveicon), array('class' => 'disapprove'));
        } else {
            $replacement[] = '';
        }
        $patterns[] = '##approvalstatus##';
        if (!$data->approval) {
            $replacement[] = '';
        } else {
            if ($record->approved) {
                $replacement[] = get_string('approved', 'data');
            } else {
                $replacement[] = get_string('notapproved', 'data');
            }
        }
        $patterns[] = '##comments##';
        if ($template == 'listtemplate' && $data->comments) {
            if (!empty($CFG->usecomments)) {
                require_once $CFG->dirroot . '/comment/lib.php';
                list($context, $course, $cm) = get_context_info_array($context->id);
                $cmt = new stdClass();
                $cmt->context = $context;
                $cmt->course = $course;
                $cmt->cm = $cm;
                $cmt->area = 'database_entry';
                $cmt->itemid = $record->id;
                $cmt->showcount = true;
                $cmt->component = 'mod_data';
                $comment = new comment($cmt);
                $replacement[] = $comment->output(true);
            }
        } else {
            $replacement[] = '';
        }
        // actual replacement of the tags
        $newtext = str_ireplace($patterns, $replacement, $data->{$template});
        // no more html formatting and filtering - see MDL-6635
        if ($return) {
            return $newtext;
        } else {
            echo $newtext;
            // hack alert - return is always false in singletemplate anyway ;-)
            /**********************************
             *    Printing Ratings Form       *
             *********************************/
            if ($template == 'singletemplate') {
                //prints ratings options
                data_print_ratings($data, $record);
            }
            /**********************************
             *    Printing Comments Form       *
             *********************************/
            if ($template == 'singletemplate' && $data->comments) {
                if (!empty($CFG->usecomments)) {
                    require_once $CFG->dirroot . '/comment/lib.php';
                    list($context, $course, $cm) = get_context_info_array($context->id);
                    $cmt = new stdClass();
                    $cmt->context = $context;
                    $cmt->course = $course;
                    $cmt->cm = $cm;
                    $cmt->area = 'database_entry';
                    $cmt->itemid = $record->id;
                    $cmt->showcount = true;
                    $cmt->component = 'mod_data';
                    $comment = new comment($cmt);
                    $comment->output(false);
                }
            }
        }
    }
}
Example #2
0
 /**
  * Checks that data_user_can_manage_entry will return the 'manageapproved'
  * value if the record has already been approved.
  */
 public function test_data_user_can_manage_entry_return_manageapproved()
 {
     $this->resetAfterTest();
     $testdata = $this->create_user_test_data();
     $user = $testdata['user'];
     $course = $testdata['course'];
     $roleid = $testdata['roleid'];
     $context = $testdata['context'];
     $record = $testdata['record'];
     $data = new stdClass();
     // Causes readonly mode to be disabled.
     $now = time();
     $data->timeviewfrom = $now + 100;
     $data->timeviewto = $now - 100;
     // The record needs approval.
     $data->approval = true;
     // Can the user managed approved records?
     $data->manageapproved = false;
     // Make sure the record is owned by this user.
     $record->userid = $user->id;
     // The record has been approved.
     $record->approved = true;
     $this->setUser($user);
     // Need to make sure they don't have this capability in order to fall back to
     // the other checks.
     assign_capability('mod/data:manageentries', CAP_PROHIBIT, $roleid, $context);
     $canmanageentry = data_user_can_manage_entry($record, $data, $context);
     // Make sure the result of the check is what ever the manageapproved setting
     // is set to.
     $this->assertEquals($data->manageapproved, $canmanageentry, 'data_user_can_manage_entry() returns the manageapproved setting on approved records');
 }
Example #3
0
//this links has been Settings (database activity administration) block
/*if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
      echo '<div style="float:right;">';
      rss_print_link($context->id, $USER->id, 'mod_data', $data->id, get_string('rsstype'));
      echo '</div>';
      echo '<div style="clear:both;"></div>';
  }*/
if ($data->intro and empty($page) and empty($record) and $mode != 'single') {
    $options = new stdClass();
    $options->noclean = true;
}
echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
$returnurl = $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&amp;search=' . s($search) . '&amp;sort=' . s($sort) . '&amp;order=' . s($order) . '&amp;';
groups_print_activity_menu($cm, $returnurl);
/// Delete any requested records
if ($delete && confirm_sesskey() && data_user_can_manage_entry($delete, $data, $context)) {
    if ($confirm = optional_param('confirm', 0, PARAM_INT)) {
        if (data_delete_record($delete, $data, $course->id, $cm->id)) {
            echo $OUTPUT->notification(get_string('recorddeleted', 'data'), 'notifysuccess');
        }
    } else {
        // Print a confirmation page
        $allnamefields = user_picture::fields('u');
        // Remove the id from the string. This already exists in the sql statement.
        $allnamefields = str_replace('u.id,', '', $allnamefields);
        $dbparams = array($delete);
        if ($deleterecord = $DB->get_record_sql("SELECT dr.*, {$allnamefields}\n                                                       FROM {data_records} dr\n                                                            JOIN {user} u ON dr.userid = u.id\n                                                      WHERE dr.id = ?", $dbparams, MUST_EXIST)) {
            // Need to check this is valid.
            if ($deleterecord->dataid == $data->id) {
                // Must be from this database
                $deletebutton = new single_button(new moodle_url('/mod/data/view.php?d=' . $data->id . '&delete=' . $delete . '&confirm=1'), get_string('delete'), 'post');
Example #4
0
        // Brand new database!
        redirect($CFG->wwwroot . '/mod/data/field.php?d=' . $data->id);
        // Redirect to field entry
    }
}
if ($rid) {
    // When editing an existing record, we require the session key
    require_sesskey();
}
// Get Group information for permission testing and record creation
$currentgroup = groups_get_activity_group($cm);
$groupmode = groups_get_activity_groupmode($cm);
if (!has_capability('mod/data:manageentries', $context)) {
    if ($rid) {
        // User is editing an existing record
        if (!data_user_can_manage_entry($record, $data, $context)) {
            print_error('noaccess', 'data');
        }
    } else {
        if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) {
            // User is trying to create a new record
            print_error('noaccess', 'data');
        }
    }
}
if ($cancel) {
    redirect('view.php?d=' . $data->id);
}
/// RSS and CSS and JS meta
if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
    $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));