Пример #1
0
 /**
  * Updates cohort name and returns instance of this object
  *
  * @param int $cohortid
  * @param string $newvalue
  * @return static
  */
 public static function update($cohortid, $newvalue)
 {
     global $DB;
     $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
     $cohortcontext = \context::instance_by_id($cohort->contextid);
     require_capability('moodle/cohort:manage', $cohortcontext);
     $record = (object) array('id' => $cohort->id, 'idnumber' => $newvalue, 'contextid' => $cohort->contextid);
     cohort_update_cohort($record);
     $cohort->idnumber = $newvalue;
     return new static($cohort);
 }
Пример #2
0
 /**
  * Updates cohort name and returns instance of this object
  *
  * @param int $cohortid
  * @param string $newvalue
  * @return static
  */
 public static function update($cohortid, $newvalue)
 {
     global $DB;
     $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
     $cohortcontext = \context::instance_by_id($cohort->contextid);
     require_capability('moodle/cohort:manage', $cohortcontext);
     $newvalue = clean_param($newvalue, PARAM_TEXT);
     if (strval($newvalue) !== '') {
         $record = (object) array('id' => $cohort->id, 'name' => $newvalue, 'contextid' => $cohort->contextid);
         cohort_update_cohort($record);
         $cohort->name = $newvalue;
     }
     return new static($cohort);
 }
Пример #3
0
 public function test_cohort_update_cohort_event()
 {
     global $DB;
     $this->resetAfterTest();
     // Setup the cohort data structure.
     $cohort = new stdClass();
     $cohort->contextid = context_system::instance()->id;
     $cohort->name = 'test cohort';
     $cohort->idnumber = 'testid';
     $cohort->description = 'test cohort desc';
     $cohort->descriptionformat = FORMAT_HTML;
     $id = cohort_add_cohort($cohort);
     $this->assertNotEmpty($id);
     $cohort->name = 'test cohort 2';
     // Catch Events.
     $sink = $this->redirectEvents();
     // Peform the update.
     cohort_update_cohort($cohort);
     $events = $sink->get_events();
     $sink->close();
     // Validate the event.
     $this->assertCount(1, $events);
     $event = $events[0];
     $updatedcohort = $DB->get_record('cohort', array('id' => $id));
     $this->assertInstanceOf('\\core\\event\\cohort_updated', $event);
     $this->assertEquals('cohort', $event->objecttable);
     $this->assertEquals($updatedcohort->id, $event->objectid);
     $this->assertEquals($updatedcohort->contextid, $event->contextid);
     $url = new moodle_url('/cohort/edit.php', array('id' => $event->objectid));
     $this->assertEquals($url, $event->get_url());
     $this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id));
     $this->assertEventLegacyData($cohort, $event);
     $this->assertEventContextNotUsed($event);
 }
Пример #4
0
if ($cohort->id) {
    // edit existing
    $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
    $strheading = get_string('editcohort', 'cohort');
} else {
    // add new
    $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context);
    $strheading = get_string('addcohort', 'cohort');
}
$PAGE->set_title($strheading);
$PAGE->set_heading($COURSE->fullname);
$PAGE->navbar->add($strheading);
$editform = new cohort_edit_form(null, array('editoroptions' => $editoroptions, 'data' => $cohort));
if ($editform->is_cancelled()) {
    redirect($returnurl);
} else {
    if ($data = $editform->get_data()) {
        $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
        if ($data->id) {
            cohort_update_cohort($data);
        } else {
            cohort_add_cohort($data);
        }
        // use new context id, it could have been changed
        redirect(new moodle_url('/cohort/index.php', array('contextid' => $data->contextid)));
    }
}
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
echo $editform->display();
echo $OUTPUT->footer();
Пример #5
0
 /**
  * Update cohorts
  *
  * @param array $cohorts
  * @return null
  * @since Moodle 2.5
  */
 public static function update_cohorts($cohorts)
 {
     global $CFG, $DB;
     require_once "{$CFG->dirroot}/cohort/lib.php";
     $params = self::validate_parameters(self::update_cohorts_parameters(), array('cohorts' => $cohorts));
     $transaction = $DB->start_delegated_transaction();
     $syscontext = context_system::instance();
     foreach ($params['cohorts'] as $cohort) {
         $cohort = (object) $cohort;
         if (trim($cohort->name) == '') {
             throw new invalid_parameter_exception('Invalid cohort name');
         }
         $oldcohort = $DB->get_record('cohort', array('id' => $cohort->id), '*', MUST_EXIST);
         $oldcontext = context::instance_by_id($oldcohort->contextid, MUST_EXIST);
         require_capability('moodle/cohort:manage', $oldcontext);
         // Category type (context id).
         $categorytype = $cohort->categorytype;
         if (!in_array($categorytype['type'], array('idnumber', 'id', 'system'))) {
             throw new invalid_parameter_exception('category type must be id, idnumber or system:' . $categorytype['type']);
         }
         if ($categorytype['type'] === 'system') {
             $cohort->contextid = $syscontext->id;
         } else {
             if ($catid = $DB->get_field('course_categories', 'id', array($categorytype['type'] => $categorytype['value']))) {
                 $cohort->contextid = $DB->get_field('context', 'id', array('instanceid' => $catid, 'contextlevel' => CONTEXT_COURSECAT));
             } else {
                 throw new invalid_parameter_exception('category not exists: category=' . $categorytype['value']);
             }
         }
         if ($cohort->contextid != $oldcohort->contextid) {
             $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
             if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
                 throw new invalid_parameter_exception('Invalid context');
             }
             self::validate_context($context);
             require_capability('moodle/cohort:manage', $context);
         }
         if (!empty($cohort->description)) {
             $cohort->descriptionformat = external_validate_format($cohort->descriptionformat);
         }
         cohort_update_cohort($cohort);
     }
     $transaction->allow_commit();
     return null;
 }
Пример #6
0
        if ($data->id) {
            if ($data->contextid != $oldcontextid) {
                // Cohort was moved to another context.
                get_file_storage()->move_area_files_to_new_context($oldcontextid, $context->id, 'cohort', 'description', $data->id);
            }
            $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context, 'cohort', 'description', $data->id);
            cohort_update_cohort($data);
        } else {
            $data->descriptionformat = $data->description_editor['format'];
            $data->description = $description = $data->description_editor['text'];
            $data->id = cohort_add_cohort($data);
            $editoroptions['context'] = $context = context::instance_by_id($data->contextid);
            $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context, 'cohort', 'description', $data->id);
            if ($description != $data->description) {
                $updatedata = (object) array('id' => $data->id, 'description' => $data->description, 'contextid' => $context->id);
                cohort_update_cohort($updatedata);
            }
        }
        if ($returnurl->get_param('showall') || $returnurl->get_param('contextid') == $data->contextid) {
            // Redirect to where we were before.
            redirect($returnurl);
        } else {
            // Use new context id, it has been changed.
            redirect(new moodle_url('/cohort/index.php', array('contextid' => $data->contextid)));
        }
    }
}
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
if (!$id && ($editcontrols = cohort_edit_controls($context, $baseurl))) {
    echo $OUTPUT->render($editcontrols);
 public function test_cohort_update_cohort()
 {
     global $DB;
     $this->resetAfterTest();
     $cohort = new stdClass();
     $cohort->contextid = context_system::instance()->id;
     $cohort->name = 'test cohort';
     $cohort->idnumber = 'testid';
     $cohort->description = 'test cohort desc';
     $cohort->descriptionformat = FORMAT_HTML;
     $id = cohort_add_cohort($cohort);
     $this->assertNotEmpty($id);
     $DB->set_field('cohort', 'timecreated', $cohort->timecreated - 10, array('id' => $id));
     $DB->set_field('cohort', 'timemodified', $cohort->timemodified - 10, array('id' => $id));
     $cohort = $DB->get_record('cohort', array('id' => $id));
     $cohort->name = 'test cohort 2';
     cohort_update_cohort($cohort);
     $newcohort = $DB->get_record('cohort', array('id' => $id));
     $this->assertSame($cohort->contextid, $newcohort->contextid);
     $this->assertSame($cohort->name, $newcohort->name);
     $this->assertSame($cohort->description, $newcohort->description);
     $this->assertSame($cohort->descriptionformat, $newcohort->descriptionformat);
     $this->assertSame($cohort->timecreated, $newcohort->timecreated);
     $this->assertSame($cohort->component, $newcohort->component);
     $this->assertGreaterThan($newcohort->timecreated, $newcohort->timemodified);
     $this->assertLessThanOrEqual(time(), $newcohort->timemodified);
 }