Esempio n. 1
0
 /**
  * @return bool
  */
 public static function validate($params)
 {
     $dataformid = $params['dataformid'];
     $df = \mod_dataform_dataform::instance($dataformid);
     // Unspecified entry.
     if (empty($params['entry'])) {
         return self::has_capability('mod/dataform:entryanydelete', $params);
     }
     // Early entries.
     if ($df->is_early()) {
         $params['capabilities'] = array('mod/dataform:entryearlydelete');
         if (!parent::validate($params)) {
             return false;
         }
     }
     // Late entries.
     if ($df->is_past_due()) {
         $params['capabilities'] = array('mod/dataform:entrylatedelete');
         if (!parent::validate($params)) {
             return false;
         }
     }
     $entry = !empty($params['entry']) ? $params['entry'] : \mod_dataform\pluginbase\dataformentry::blank_instance($df);
     // Own entry.
     if (\mod_dataform\pluginbase\dataformentry::is_own($entry)) {
         $params['capabilities'] = array('mod/dataform:entryowndelete');
         return parent::validate($params);
     }
     // Group entry.
     if (\mod_dataform\pluginbase\dataformentry::is_grouped($entry)) {
         if (groups_is_member($entry->groupid)) {
             $params['capabilities'] = array('mod/dataform:entrygroupdelete');
             return parent::validate($params);
         }
     }
     // Anonymous entry.
     if (\mod_dataform\pluginbase\dataformentry::is_anonymous($entry)) {
         $params['capabilities'] = array('mod/dataform:entryanonymousdelete');
         return parent::validate($params);
     }
     // Any entry.
     if (\mod_dataform\pluginbase\dataformentry::is_others($entry)) {
         $params['capabilities'] = array('mod/dataform:entryanydelete');
         return parent::validate($params);
     }
     return false;
 }
Esempio n. 2
0
 /**
  * @return bool
  */
 public static function validate($params)
 {
     $dataformid = $params['dataformid'];
     $df = \mod_dataform_dataform::instance($dataformid);
     // Cannot add in a view that does not allow submission.
     if (!empty($params['viewid'])) {
         $view = $df->view_manager->get_view_by_id($params['viewid']);
         if (!$view or !$view->allows_submission()) {
             return false;
         }
     }
     // User at max entries (per interval).
     if ($df->user_at_max_entries(true)) {
         // No more entries for you (come back next interval or so).
         return false;
     }
     // Early entries.
     if ($df->is_early()) {
         $params['capabilities'] = array('mod/dataform:entryearlyadd');
         if (!parent::validate($params)) {
             return false;
         }
     }
     // Late entries.
     if ($df->is_past_due()) {
         $params['capabilities'] = array('mod/dataform:entrylateadd');
         if (!parent::validate($params)) {
             return false;
         }
     }
     $entry = !empty($params['entry']) ? $params['entry'] : \mod_dataform\pluginbase\dataformentry::blank_instance($df);
     // Own entry.
     if (\mod_dataform\pluginbase\dataformentry::is_own($entry)) {
         $params['capabilities'] = array('mod/dataform:entryownadd');
         return parent::validate($params);
     }
     // Group entry.
     if (\mod_dataform\pluginbase\dataformentry::is_grouped($entry)) {
         if (groups_is_member($entry->groupid)) {
             $params['capabilities'] = array('mod/dataform:entrygroupadd');
             return parent::validate($params);
         }
     }
     // Anonymous entry.
     if (\mod_dataform\pluginbase\dataformentry::is_anonymous($entry)) {
         if ((isguestuser() or !isloggedin()) and $df->anonymous) {
             return true;
         }
         $params['capabilities'] = array('mod/dataform:entryanonymousadd');
         return parent::validate($params);
     }
     // Any entry.
     if (\mod_dataform\pluginbase\dataformentry::is_others($entry)) {
         $params['capabilities'] = array('mod/dataform:entryanyadd');
         return parent::validate($params);
     }
     return false;
 }
Esempio n. 3
0
 /**
  * Generates a dataform entry.
  * @param array|stdClass $record
  * @param array $options
  * @return stdClass generated object
  */
 public function create_entry($record, array $options = null)
 {
     global $DB;
     // Convert timecreated and timemodified.
     $record['timecreated'] = !empty($record['timecreated']) ? strtotime($record['timecreated']) : 0;
     $record['timemodified'] = !empty($record['timemodified']) ? strtotime($record['timemodified']) : 0;
     $df = new \mod_dataform_dataform($record['dataid']);
     $entry = \mod_dataform\pluginbase\dataformentry::blank_instance($df, (object) (array) $record);
     $entry->id = $DB->insert_record('dataform_entries', $entry);
     // Add content.
     if ($fields = $df->field_manager->get_fields()) {
         $fieldsbyname = array();
         foreach ($fields as $field) {
             $fieldsbyname[$field->name] = $field;
         }
         foreach ($record as $name => $value) {
             list($fieldname, $contentname) = array_pad(explode('_', $name), 2, '');
             if (array_key_exists($fieldname, $fieldsbyname)) {
                 $values = array($contentname => $value);
                 $fieldsbyname[$fieldname]->update_content($entry, $values);
             }
         }
     }
     return $entry;
 }
Esempio n. 4
0
 /**
  * Validates that current user can view the field content.
  *
  * @return bool
  */
 public static function validate($params)
 {
     global $USER;
     $dataformid = $params['dataformid'];
     $df = \mod_dataform_dataform::instance($dataformid);
     // Must have fieldid.
     if (empty($params['fieldid'])) {
         return false;
     }
     $field = $df->field_manager->get_field_by_id($params['fieldid']);
     $params['field'] = $field;
     // Must have entry.
     $entry = $params['entry'];
     // Get blank instance for new entries.
     if ($entry->id < 0) {
         $entry = \mod_dataform\pluginbase\dataformentry::blank_instance($df);
     }
     if ($field->visible != $field::VISIBLE_ALL) {
         // Entry managers can access any field.
         if (!($canmanageentries = has_capability('mod/dataform:manageentries', $field->df->context))) {
             if ($field->visible == $field::VISIBLE_NONE) {
                 return false;
             }
             $userid = !empty($entry->userid) ? $entry->userid : 0;
             $isentryowner = $USER->id == $userid;
             if ($field->visible == $field::VISIBLE_OWNER and !$isentryowner) {
                 return false;
             }
         }
     }
     // Early access.
     if ($df->is_early()) {
         $params['capabilities'] = array('mod/dataform:entryearlyview');
         if (!parent::validate($params)) {
             return false;
         }
     }
     // Late access.
     if ($df->is_past_due()) {
         $params['capabilities'] = array('mod/dataform:entrylateview');
         if (!parent::validate($params)) {
             return false;
         }
     }
     // Own entry.
     if (\mod_dataform\pluginbase\dataformentry::is_own($entry)) {
         $params['capabilities'] = array('mod/dataform:entryownview');
         return parent::validate($params);
     }
     // Group entry.
     if (\mod_dataform\pluginbase\dataformentry::is_grouped($entry)) {
         $params['capabilities'] = array('mod/dataform:entrygroupview');
         return parent::validate($params);
     }
     // Anonymous entry.
     if (\mod_dataform\pluginbase\dataformentry::is_anonymous($entry)) {
         $params['capabilities'] = array('mod/dataform:entryanonymousview');
         return parent::validate($params);
     }
     // Any entry.
     if (\mod_dataform\pluginbase\dataformentry::is_others($entry)) {
         $params['capabilities'] = array('mod/dataform:entryanyview');
         return parent::validate($params);
     }
     return false;
 }
Esempio n. 5
0
 /**
  * @return bool
  */
 public static function validate($params)
 {
     $dataformid = $params['dataformid'];
     $df = \mod_dataform_dataform::instance($dataformid);
     // Must have fieldid.
     if (empty($params['fieldid'])) {
         return false;
     }
     $field = $df->field_manager->get_field_by_id($params['fieldid']);
     $params['field'] = $field;
     if (!$field->editable and !has_capability('mod/dataform:manageentries', $field->df->context)) {
         return false;
     }
     // Must have entry.
     $entry = $params['entry'];
     // Get blank instance for new entries.
     if ($entry->id < 0) {
         $entry = \mod_dataform\pluginbase\dataformentry::blank_instance($df);
     }
     // Early access.
     if ($df->is_early()) {
         $params['capabilities'] = array('mod/dataform:entryearlyupdate');
         if (!parent::validate($params)) {
             return false;
         }
     }
     // Late access.
     if ($df->is_past_due()) {
         $params['capabilities'] = array('mod/dataform:entrylateupdate');
         if (!parent::validate($params)) {
             return false;
         }
     }
     // Own entry.
     if (\mod_dataform\pluginbase\dataformentry::is_own($entry)) {
         $params['capabilities'] = array('mod/dataform:entryownupdate');
         return parent::validate($params);
     }
     // Group entry.
     if (\mod_dataform\pluginbase\dataformentry::is_grouped($entry)) {
         $params['capabilities'] = array('mod/dataform:entrygroupupdate');
         return parent::validate($params);
     }
     // Anonymous entry.
     if (\mod_dataform\pluginbase\dataformentry::is_anonymous($entry)) {
         $params['capabilities'] = array('mod/dataform:entryanonymousupdate');
         return parent::validate($params);
     }
     // Any entry.
     if (\mod_dataform\pluginbase\dataformentry::is_others($entry)) {
         $params['capabilities'] = array('mod/dataform:entryanyupdate');
         return parent::validate($params);
     }
     return false;
 }
Esempio n. 6
0
 /**
  * @return bool
  */
 public static function validate($params)
 {
     $dataformid = $params['dataformid'];
     $df = \mod_dataform_dataform::instance($dataformid);
     // Cannot update in a view that does not allow submission.
     if (!empty($params['viewid'])) {
         $view = $df->view_manager->get_view_by_id($params['viewid']);
         if (!$view or !$view->allows_submission()) {
             return false;
         }
     }
     // Unspecified entry
     // if (empty($params['entry'])) {
     //    return self::has_capability('mod/dataform:entryanyupdate', $params);
     // }.
     // Early entries.
     if ($df->is_early()) {
         $params['capabilities'] = array('mod/dataform:entryearlyupdate');
         if (!parent::validate($params)) {
             return false;
         }
     }
     // Late entries.
     if ($df->is_past_due()) {
         $params['capabilities'] = array('mod/dataform:entrylateupdate');
         if (!parent::validate($params)) {
             return false;
         }
     }
     $entry = !empty($params['entry']) ? $params['entry'] : \mod_dataform\pluginbase\dataformentry::blank_instance($df);
     // Own entry.
     if (\mod_dataform\pluginbase\dataformentry::is_own($entry)) {
         $params['capabilities'] = array('mod/dataform:entryownupdate');
         return parent::validate($params);
     }
     // Group entry.
     if (\mod_dataform\pluginbase\dataformentry::is_grouped($entry)) {
         if (groups_is_member($entry->groupid)) {
             $params['capabilities'] = array('mod/dataform:entrygroupupdate');
             return parent::validate($params);
         }
     }
     // Anonymous entry.
     if (\mod_dataform\pluginbase\dataformentry::is_anonymous($entry)) {
         $params['capabilities'] = array('mod/dataform:entryanonymousupdate');
         return parent::validate($params);
     }
     // Any entry.
     if (\mod_dataform\pluginbase\dataformentry::is_others($entry)) {
         $params['capabilities'] = array('mod/dataform:entryanyupdate');
         return parent::validate($params);
     }
     return false;
 }